Darwin: Update quotes in driver warning messages.
[official-gcc.git] / gcc / cselib.c
blobf5c8cbb6eb1b2347df0af548f9d41144b693f4d9
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 CONST_VECTOR:
1052 if (!same_vector_encodings_p (x, y))
1053 return false;
1054 break;
1056 case DEBUG_IMPLICIT_PTR:
1057 return DEBUG_IMPLICIT_PTR_DECL (x)
1058 == DEBUG_IMPLICIT_PTR_DECL (y);
1060 case DEBUG_PARAMETER_REF:
1061 return DEBUG_PARAMETER_REF_DECL (x)
1062 == DEBUG_PARAMETER_REF_DECL (y);
1064 case ENTRY_VALUE:
1065 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
1066 use rtx_equal_for_cselib_1 to compare the operands. */
1067 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1069 case LABEL_REF:
1070 return label_ref_label (x) == label_ref_label (y);
1072 case REG:
1073 return REGNO (x) == REGNO (y);
1075 case MEM:
1076 /* We have to compare any autoinc operations in the addresses
1077 using this MEM's mode. */
1078 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x),
1079 depth);
1081 default:
1082 break;
1085 code = GET_CODE (x);
1086 fmt = GET_RTX_FORMAT (code);
1088 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1090 int j;
1092 switch (fmt[i])
1094 case 'w':
1095 if (XWINT (x, i) != XWINT (y, i))
1096 return 0;
1097 break;
1099 case 'n':
1100 case 'i':
1101 if (XINT (x, i) != XINT (y, i))
1102 return 0;
1103 break;
1105 case 'p':
1106 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
1107 return 0;
1108 break;
1110 case 'V':
1111 case 'E':
1112 /* Two vectors must have the same length. */
1113 if (XVECLEN (x, i) != XVECLEN (y, i))
1114 return 0;
1116 /* And the corresponding elements must match. */
1117 for (j = 0; j < XVECLEN (x, i); j++)
1118 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1119 XVECEXP (y, i, j), memmode, depth))
1120 return 0;
1121 break;
1123 case 'e':
1124 if (i == 1
1125 && targetm.commutative_p (x, UNKNOWN)
1126 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode,
1127 depth)
1128 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode,
1129 depth))
1130 return 1;
1131 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode,
1132 depth))
1133 return 0;
1134 break;
1136 case 'S':
1137 case 's':
1138 if (strcmp (XSTR (x, i), XSTR (y, i)))
1139 return 0;
1140 break;
1142 case 'u':
1143 /* These are just backpointers, so they don't matter. */
1144 break;
1146 case '0':
1147 case 't':
1148 break;
1150 /* It is believed that rtx's at this level will never
1151 contain anything but integers and other rtx's,
1152 except for within LABEL_REFs and SYMBOL_REFs. */
1153 default:
1154 gcc_unreachable ();
1157 return 1;
1160 /* Helper function for cselib_hash_rtx. Arguments like for cselib_hash_rtx,
1161 except that it hashes (plus:P x c). */
1163 static unsigned int
1164 cselib_hash_plus_const_int (rtx x, HOST_WIDE_INT c, int create,
1165 machine_mode memmode)
1167 cselib_val *e = cselib_lookup (x, GET_MODE (x), create, memmode);
1168 if (! e)
1169 return 0;
1171 if (! SP_DERIVED_VALUE_P (e->val_rtx))
1172 for (struct elt_loc_list *l = e->locs; l; l = l->next)
1173 if (GET_CODE (l->loc) == PLUS
1174 && GET_CODE (XEXP (l->loc, 0)) == VALUE
1175 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
1176 && CONST_INT_P (XEXP (l->loc, 1)))
1178 e = CSELIB_VAL_PTR (XEXP (l->loc, 0));
1179 c = trunc_int_for_mode (c + UINTVAL (XEXP (l->loc, 1)), Pmode);
1180 break;
1182 if (c == 0)
1183 return e->hash;
1185 unsigned hash = (unsigned) PLUS + (unsigned) GET_MODE (x);
1186 hash += e->hash;
1187 unsigned int tem_hash = (unsigned) CONST_INT + (unsigned) VOIDmode;
1188 tem_hash += ((unsigned) CONST_INT << 7) + (unsigned HOST_WIDE_INT) c;
1189 if (tem_hash == 0)
1190 tem_hash = (unsigned int) CONST_INT;
1191 hash += tem_hash;
1192 return hash ? hash : 1 + (unsigned int) PLUS;
1195 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1196 For registers and memory locations, we look up their cselib_val structure
1197 and return its VALUE element.
1198 Possible reasons for return 0 are: the object is volatile, or we couldn't
1199 find a register or memory location in the table and CREATE is zero. If
1200 CREATE is nonzero, table elts are created for regs and mem.
1201 N.B. this hash function returns the same hash value for RTXes that
1202 differ only in the order of operands, thus it is suitable for comparisons
1203 that take commutativity into account.
1204 If we wanted to also support associative rules, we'd have to use a different
1205 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1206 MEMMODE indicates the mode of an enclosing MEM, and it's only
1207 used to compute autoinc values.
1208 We used to have a MODE argument for hashing for CONST_INTs, but that
1209 didn't make sense, since it caused spurious hash differences between
1210 (set (reg:SI 1) (const_int))
1211 (plus:SI (reg:SI 2) (reg:SI 1))
1213 (plus:SI (reg:SI 2) (const_int))
1214 If the mode is important in any context, it must be checked specifically
1215 in a comparison anyway, since relying on hash differences is unsafe. */
1217 static unsigned int
1218 cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1220 cselib_val *e;
1221 poly_int64 offset;
1222 int i, j;
1223 enum rtx_code code;
1224 const char *fmt;
1225 unsigned int hash = 0;
1227 code = GET_CODE (x);
1228 hash += (unsigned) code + (unsigned) GET_MODE (x);
1230 switch (code)
1232 case VALUE:
1233 e = CSELIB_VAL_PTR (x);
1234 return e->hash;
1236 case MEM:
1237 case REG:
1238 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1239 if (! e)
1240 return 0;
1242 return e->hash;
1244 case DEBUG_EXPR:
1245 hash += ((unsigned) DEBUG_EXPR << 7)
1246 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1247 return hash ? hash : (unsigned int) DEBUG_EXPR;
1249 case DEBUG_IMPLICIT_PTR:
1250 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1251 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1252 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1254 case DEBUG_PARAMETER_REF:
1255 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1256 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1257 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1259 case ENTRY_VALUE:
1260 /* ENTRY_VALUEs are function invariant, thus try to avoid
1261 recursing on argument if ENTRY_VALUE is one of the
1262 forms emitted by expand_debug_expr, otherwise
1263 ENTRY_VALUE hash would depend on the current value
1264 in some register or memory. */
1265 if (REG_P (ENTRY_VALUE_EXP (x)))
1266 hash += (unsigned int) REG
1267 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1268 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1269 else if (MEM_P (ENTRY_VALUE_EXP (x))
1270 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1271 hash += (unsigned int) MEM
1272 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1273 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1274 else
1275 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1276 return hash ? hash : (unsigned int) ENTRY_VALUE;
1278 case CONST_INT:
1279 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
1280 return hash ? hash : (unsigned int) CONST_INT;
1282 case CONST_WIDE_INT:
1283 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1284 hash += CONST_WIDE_INT_ELT (x, i);
1285 return hash;
1287 case CONST_POLY_INT:
1289 inchash::hash h;
1290 h.add_int (hash);
1291 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1292 h.add_wide_int (CONST_POLY_INT_COEFFS (x)[i]);
1293 return h.end ();
1296 case CONST_DOUBLE:
1297 /* This is like the general case, except that it only counts
1298 the integers representing the constant. */
1299 hash += (unsigned) code + (unsigned) GET_MODE (x);
1300 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1301 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1302 + (unsigned) CONST_DOUBLE_HIGH (x));
1303 else
1304 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1305 return hash ? hash : (unsigned int) CONST_DOUBLE;
1307 case CONST_FIXED:
1308 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1309 hash += fixed_hash (CONST_FIXED_VALUE (x));
1310 return hash ? hash : (unsigned int) CONST_FIXED;
1312 case CONST_VECTOR:
1314 int units;
1315 rtx elt;
1317 units = const_vector_encoded_nelts (x);
1319 for (i = 0; i < units; ++i)
1321 elt = CONST_VECTOR_ENCODED_ELT (x, i);
1322 hash += cselib_hash_rtx (elt, 0, memmode);
1325 return hash;
1328 /* Assume there is only one rtx object for any given label. */
1329 case LABEL_REF:
1330 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1331 differences and differences between each stage's debugging dumps. */
1332 hash += (((unsigned int) LABEL_REF << 7)
1333 + CODE_LABEL_NUMBER (label_ref_label (x)));
1334 return hash ? hash : (unsigned int) LABEL_REF;
1336 case SYMBOL_REF:
1338 /* Don't hash on the symbol's address to avoid bootstrap differences.
1339 Different hash values may cause expressions to be recorded in
1340 different orders and thus different registers to be used in the
1341 final assembler. This also avoids differences in the dump files
1342 between various stages. */
1343 unsigned int h = 0;
1344 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1346 while (*p)
1347 h += (h << 7) + *p++; /* ??? revisit */
1349 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1350 return hash ? hash : (unsigned int) SYMBOL_REF;
1353 case PRE_DEC:
1354 case PRE_INC:
1355 /* We can't compute these without knowing the MEM mode. */
1356 gcc_assert (memmode != VOIDmode);
1357 offset = GET_MODE_SIZE (memmode);
1358 if (code == PRE_DEC)
1359 offset = -offset;
1360 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1361 like (mem:MEMMODE (plus (reg) (const_int I))). */
1362 if (GET_MODE (x) == Pmode
1363 && (REG_P (XEXP (x, 0))
1364 || MEM_P (XEXP (x, 0))
1365 || GET_CODE (XEXP (x, 0)) == VALUE))
1367 HOST_WIDE_INT c;
1368 if (offset.is_constant (&c))
1369 return cselib_hash_plus_const_int (XEXP (x, 0),
1370 trunc_int_for_mode (c, Pmode),
1371 create, memmode);
1373 hash = ((unsigned) PLUS + (unsigned) GET_MODE (x)
1374 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1375 + cselib_hash_rtx (gen_int_mode (offset, GET_MODE (x)),
1376 create, memmode));
1377 return hash ? hash : 1 + (unsigned) PLUS;
1379 case PRE_MODIFY:
1380 gcc_assert (memmode != VOIDmode);
1381 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1383 case POST_DEC:
1384 case POST_INC:
1385 case POST_MODIFY:
1386 gcc_assert (memmode != VOIDmode);
1387 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1389 case PC:
1390 case CALL:
1391 case UNSPEC_VOLATILE:
1392 return 0;
1394 case ASM_OPERANDS:
1395 if (MEM_VOLATILE_P (x))
1396 return 0;
1398 break;
1400 case PLUS:
1401 if (GET_MODE (x) == Pmode
1402 && (REG_P (XEXP (x, 0))
1403 || MEM_P (XEXP (x, 0))
1404 || GET_CODE (XEXP (x, 0)) == VALUE)
1405 && CONST_INT_P (XEXP (x, 1)))
1406 return cselib_hash_plus_const_int (XEXP (x, 0), INTVAL (XEXP (x, 1)),
1407 create, memmode);
1408 break;
1410 default:
1411 break;
1414 i = GET_RTX_LENGTH (code) - 1;
1415 fmt = GET_RTX_FORMAT (code);
1416 for (; i >= 0; i--)
1418 switch (fmt[i])
1420 case 'e':
1422 rtx tem = XEXP (x, i);
1423 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1425 if (tem_hash == 0)
1426 return 0;
1428 hash += tem_hash;
1430 break;
1431 case 'E':
1432 for (j = 0; j < XVECLEN (x, i); j++)
1434 unsigned int tem_hash
1435 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1437 if (tem_hash == 0)
1438 return 0;
1440 hash += tem_hash;
1442 break;
1444 case 's':
1446 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1448 if (p)
1449 while (*p)
1450 hash += *p++;
1451 break;
1454 case 'i':
1455 hash += XINT (x, i);
1456 break;
1458 case 'p':
1459 hash += constant_lower_bound (SUBREG_BYTE (x));
1460 break;
1462 case '0':
1463 case 't':
1464 /* unused */
1465 break;
1467 default:
1468 gcc_unreachable ();
1472 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1475 /* Create a new value structure for VALUE and initialize it. The mode of the
1476 value is MODE. */
1478 static inline cselib_val *
1479 new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
1481 cselib_val *e = cselib_val_pool.allocate ();
1483 gcc_assert (hash);
1484 gcc_assert (next_uid);
1486 e->hash = hash;
1487 e->uid = next_uid++;
1488 /* We use an alloc pool to allocate this RTL construct because it
1489 accounts for about 8% of the overall memory usage. We know
1490 precisely when we can have VALUE RTXen (when cselib is active)
1491 so we don't need to put them in garbage collected memory.
1492 ??? Why should a VALUE be an RTX in the first place? */
1493 e->val_rtx = (rtx_def*) value_pool.allocate ();
1494 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1495 PUT_CODE (e->val_rtx, VALUE);
1496 PUT_MODE (e->val_rtx, mode);
1497 CSELIB_VAL_PTR (e->val_rtx) = e;
1498 e->addr_list = 0;
1499 e->locs = 0;
1500 e->next_containing_mem = 0;
1502 if (dump_file && (dump_flags & TDF_CSELIB))
1504 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1505 if (flag_dump_noaddr || flag_dump_unnumbered)
1506 fputs ("# ", dump_file);
1507 else
1508 fprintf (dump_file, "%p ", (void*)e);
1509 print_rtl_single (dump_file, x);
1510 fputc ('\n', dump_file);
1513 return e;
1516 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1517 contains the data at this address. X is a MEM that represents the
1518 value. Update the two value structures to represent this situation. */
1520 static void
1521 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1523 addr_elt = canonical_cselib_val (addr_elt);
1524 mem_elt = canonical_cselib_val (mem_elt);
1526 /* Avoid duplicates. */
1527 addr_space_t as = MEM_ADDR_SPACE (x);
1528 for (elt_loc_list *l = mem_elt->locs; l; l = l->next)
1529 if (MEM_P (l->loc)
1530 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt
1531 && MEM_ADDR_SPACE (l->loc) == as)
1533 promote_debug_loc (l);
1534 return;
1537 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1538 new_elt_loc_list (mem_elt,
1539 replace_equiv_address_nv (x, addr_elt->val_rtx));
1540 if (mem_elt->next_containing_mem == NULL)
1542 mem_elt->next_containing_mem = first_containing_mem;
1543 first_containing_mem = mem_elt;
1547 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1548 If CREATE, make a new one if we haven't seen it before. */
1550 static cselib_val *
1551 cselib_lookup_mem (rtx x, int create)
1553 machine_mode mode = GET_MODE (x);
1554 machine_mode addr_mode;
1555 cselib_val **slot;
1556 cselib_val *addr;
1557 cselib_val *mem_elt;
1559 if (MEM_VOLATILE_P (x) || mode == BLKmode
1560 || !cselib_record_memory
1561 || (FLOAT_MODE_P (mode) && flag_float_store))
1562 return 0;
1564 addr_mode = GET_MODE (XEXP (x, 0));
1565 if (addr_mode == VOIDmode)
1566 addr_mode = Pmode;
1568 /* Look up the value for the address. */
1569 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1570 if (! addr)
1571 return 0;
1572 addr = canonical_cselib_val (addr);
1574 /* Find a value that describes a value of our mode at that address. */
1575 addr_space_t as = MEM_ADDR_SPACE (x);
1576 for (elt_list *l = addr->addr_list; l; l = l->next)
1577 if (GET_MODE (l->elt->val_rtx) == mode)
1579 for (elt_loc_list *l2 = l->elt->locs; l2; l2 = l2->next)
1580 if (MEM_P (l2->loc) && MEM_ADDR_SPACE (l2->loc) == as)
1582 promote_debug_loc (l->elt->locs);
1583 return l->elt;
1587 if (! create)
1588 return 0;
1590 mem_elt = new_cselib_val (next_uid, mode, x);
1591 add_mem_for_addr (addr, mem_elt, x);
1592 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1593 *slot = mem_elt;
1594 return mem_elt;
1597 /* Search through the possible substitutions in P. We prefer a non reg
1598 substitution because this allows us to expand the tree further. If
1599 we find, just a reg, take the lowest regno. There may be several
1600 non-reg results, we just take the first one because they will all
1601 expand to the same place. */
1603 static rtx
1604 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1605 int max_depth)
1607 rtx reg_result = NULL;
1608 unsigned int regno = UINT_MAX;
1609 struct elt_loc_list *p_in = p;
1611 for (; p; p = p->next)
1613 /* Return these right away to avoid returning stack pointer based
1614 expressions for frame pointer and vice versa, which is something
1615 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1616 for more details. */
1617 if (REG_P (p->loc)
1618 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1619 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1620 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1621 || REGNO (p->loc) == cfa_base_preserved_regno))
1622 return p->loc;
1623 /* Avoid infinite recursion trying to expand a reg into a
1624 the same reg. */
1625 if ((REG_P (p->loc))
1626 && (REGNO (p->loc) < regno)
1627 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1629 reg_result = p->loc;
1630 regno = REGNO (p->loc);
1632 /* Avoid infinite recursion and do not try to expand the
1633 value. */
1634 else if (GET_CODE (p->loc) == VALUE
1635 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1636 continue;
1637 else if (!REG_P (p->loc))
1639 rtx result, note;
1640 if (dump_file && (dump_flags & TDF_CSELIB))
1642 print_inline_rtx (dump_file, p->loc, 0);
1643 fprintf (dump_file, "\n");
1645 if (GET_CODE (p->loc) == LO_SUM
1646 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1647 && p->setting_insn
1648 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1649 && XEXP (note, 0) == XEXP (p->loc, 1))
1650 return XEXP (p->loc, 1);
1651 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1652 if (result)
1653 return result;
1658 if (regno != UINT_MAX)
1660 rtx result;
1661 if (dump_file && (dump_flags & TDF_CSELIB))
1662 fprintf (dump_file, "r%d\n", regno);
1664 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1665 if (result)
1666 return result;
1669 if (dump_file && (dump_flags & TDF_CSELIB))
1671 if (reg_result)
1673 print_inline_rtx (dump_file, reg_result, 0);
1674 fprintf (dump_file, "\n");
1676 else
1677 fprintf (dump_file, "NULL\n");
1679 return reg_result;
1683 /* Forward substitute and expand an expression out to its roots.
1684 This is the opposite of common subexpression. Because local value
1685 numbering is such a weak optimization, the expanded expression is
1686 pretty much unique (not from a pointer equals point of view but
1687 from a tree shape point of view.
1689 This function returns NULL if the expansion fails. The expansion
1690 will fail if there is no value number for one of the operands or if
1691 one of the operands has been overwritten between the current insn
1692 and the beginning of the basic block. For instance x has no
1693 expansion in:
1695 r1 <- r1 + 3
1696 x <- r1 + 8
1698 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1699 It is clear on return. */
1702 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1704 struct expand_value_data evd;
1706 evd.regs_active = regs_active;
1707 evd.callback = NULL;
1708 evd.callback_arg = NULL;
1709 evd.dummy = false;
1711 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1714 /* Same as cselib_expand_value_rtx, but using a callback to try to
1715 resolve some expressions. The CB function should return ORIG if it
1716 can't or does not want to deal with a certain RTX. Any other
1717 return value, including NULL, will be used as the expansion for
1718 VALUE, without any further changes. */
1721 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1722 cselib_expand_callback cb, void *data)
1724 struct expand_value_data evd;
1726 evd.regs_active = regs_active;
1727 evd.callback = cb;
1728 evd.callback_arg = data;
1729 evd.dummy = false;
1731 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1734 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1735 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1736 would return NULL or non-NULL, without allocating new rtx. */
1738 bool
1739 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1740 cselib_expand_callback cb, void *data)
1742 struct expand_value_data evd;
1744 evd.regs_active = regs_active;
1745 evd.callback = cb;
1746 evd.callback_arg = data;
1747 evd.dummy = true;
1749 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1752 /* Internal implementation of cselib_expand_value_rtx and
1753 cselib_expand_value_rtx_cb. */
1755 static rtx
1756 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1757 int max_depth)
1759 rtx copy, scopy;
1760 int i, j;
1761 RTX_CODE code;
1762 const char *format_ptr;
1763 machine_mode mode;
1765 code = GET_CODE (orig);
1767 /* For the context of dse, if we end up expand into a huge tree, we
1768 will not have a useful address, so we might as well just give up
1769 quickly. */
1770 if (max_depth <= 0)
1771 return NULL;
1773 switch (code)
1775 case REG:
1777 struct elt_list *l = REG_VALUES (REGNO (orig));
1779 if (l && l->elt == NULL)
1780 l = l->next;
1781 for (; l; l = l->next)
1782 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1784 rtx result;
1785 unsigned regno = REGNO (orig);
1787 /* The only thing that we are not willing to do (this
1788 is requirement of dse and if others potential uses
1789 need this function we should add a parm to control
1790 it) is that we will not substitute the
1791 STACK_POINTER_REGNUM, FRAME_POINTER or the
1792 HARD_FRAME_POINTER.
1794 These expansions confuses the code that notices that
1795 stores into the frame go dead at the end of the
1796 function and that the frame is not effected by calls
1797 to subroutines. If you allow the
1798 STACK_POINTER_REGNUM substitution, then dse will
1799 think that parameter pushing also goes dead which is
1800 wrong. If you allow the FRAME_POINTER or the
1801 HARD_FRAME_POINTER then you lose the opportunity to
1802 make the frame assumptions. */
1803 if (regno == STACK_POINTER_REGNUM
1804 || regno == FRAME_POINTER_REGNUM
1805 || regno == HARD_FRAME_POINTER_REGNUM
1806 || regno == cfa_base_preserved_regno)
1807 return orig;
1809 bitmap_set_bit (evd->regs_active, regno);
1811 if (dump_file && (dump_flags & TDF_CSELIB))
1812 fprintf (dump_file, "expanding: r%d into: ", regno);
1814 result = expand_loc (l->elt->locs, evd, max_depth);
1815 bitmap_clear_bit (evd->regs_active, regno);
1817 if (result)
1818 return result;
1819 else
1820 return orig;
1822 return orig;
1825 CASE_CONST_ANY:
1826 case SYMBOL_REF:
1827 case CODE_LABEL:
1828 case PC:
1829 case SCRATCH:
1830 /* SCRATCH must be shared because they represent distinct values. */
1831 return orig;
1832 case CLOBBER:
1833 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1834 return orig;
1835 break;
1837 case CONST:
1838 if (shared_const_p (orig))
1839 return orig;
1840 break;
1842 case SUBREG:
1844 rtx subreg;
1846 if (evd->callback)
1848 subreg = evd->callback (orig, evd->regs_active, max_depth,
1849 evd->callback_arg);
1850 if (subreg != orig)
1851 return subreg;
1854 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1855 max_depth - 1);
1856 if (!subreg)
1857 return NULL;
1858 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1859 GET_MODE (SUBREG_REG (orig)),
1860 SUBREG_BYTE (orig));
1861 if (scopy == NULL
1862 || (GET_CODE (scopy) == SUBREG
1863 && !REG_P (SUBREG_REG (scopy))
1864 && !MEM_P (SUBREG_REG (scopy))))
1865 return NULL;
1867 return scopy;
1870 case VALUE:
1872 rtx result;
1874 if (dump_file && (dump_flags & TDF_CSELIB))
1876 fputs ("\nexpanding ", dump_file);
1877 print_rtl_single (dump_file, orig);
1878 fputs (" into...", dump_file);
1881 if (evd->callback)
1883 result = evd->callback (orig, evd->regs_active, max_depth,
1884 evd->callback_arg);
1886 if (result != orig)
1887 return result;
1890 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1891 return result;
1894 case DEBUG_EXPR:
1895 if (evd->callback)
1896 return evd->callback (orig, evd->regs_active, max_depth,
1897 evd->callback_arg);
1898 return orig;
1900 default:
1901 break;
1904 /* Copy the various flags, fields, and other information. We assume
1905 that all fields need copying, and then clear the fields that should
1906 not be copied. That is the sensible default behavior, and forces
1907 us to explicitly document why we are *not* copying a flag. */
1908 if (evd->dummy)
1909 copy = NULL;
1910 else
1911 copy = shallow_copy_rtx (orig);
1913 format_ptr = GET_RTX_FORMAT (code);
1915 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1916 switch (*format_ptr++)
1918 case 'e':
1919 if (XEXP (orig, i) != NULL)
1921 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1922 max_depth - 1);
1923 if (!result)
1924 return NULL;
1925 if (copy)
1926 XEXP (copy, i) = result;
1928 break;
1930 case 'E':
1931 case 'V':
1932 if (XVEC (orig, i) != NULL)
1934 if (copy)
1935 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1936 for (j = 0; j < XVECLEN (orig, i); j++)
1938 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1939 evd, max_depth - 1);
1940 if (!result)
1941 return NULL;
1942 if (copy)
1943 XVECEXP (copy, i, j) = result;
1946 break;
1948 case 't':
1949 case 'w':
1950 case 'i':
1951 case 's':
1952 case 'S':
1953 case 'T':
1954 case 'u':
1955 case 'B':
1956 case '0':
1957 /* These are left unchanged. */
1958 break;
1960 default:
1961 gcc_unreachable ();
1964 if (evd->dummy)
1965 return orig;
1967 mode = GET_MODE (copy);
1968 /* If an operand has been simplified into CONST_INT, which doesn't
1969 have a mode and the mode isn't derivable from whole rtx's mode,
1970 try simplify_*_operation first with mode from original's operand
1971 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1972 scopy = copy;
1973 switch (GET_RTX_CLASS (code))
1975 case RTX_UNARY:
1976 if (CONST_INT_P (XEXP (copy, 0))
1977 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1979 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1980 GET_MODE (XEXP (orig, 0)));
1981 if (scopy)
1982 return scopy;
1984 break;
1985 case RTX_COMM_ARITH:
1986 case RTX_BIN_ARITH:
1987 /* These expressions can derive operand modes from the whole rtx's mode. */
1988 break;
1989 case RTX_TERNARY:
1990 case RTX_BITFIELD_OPS:
1991 if (CONST_INT_P (XEXP (copy, 0))
1992 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1994 scopy = simplify_ternary_operation (code, mode,
1995 GET_MODE (XEXP (orig, 0)),
1996 XEXP (copy, 0), XEXP (copy, 1),
1997 XEXP (copy, 2));
1998 if (scopy)
1999 return scopy;
2001 break;
2002 case RTX_COMPARE:
2003 case RTX_COMM_COMPARE:
2004 if (CONST_INT_P (XEXP (copy, 0))
2005 && GET_MODE (XEXP (copy, 1)) == VOIDmode
2006 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
2007 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
2009 scopy = simplify_relational_operation (code, mode,
2010 (GET_MODE (XEXP (orig, 0))
2011 != VOIDmode)
2012 ? GET_MODE (XEXP (orig, 0))
2013 : GET_MODE (XEXP (orig, 1)),
2014 XEXP (copy, 0),
2015 XEXP (copy, 1));
2016 if (scopy)
2017 return scopy;
2019 break;
2020 default:
2021 break;
2023 scopy = simplify_rtx (copy);
2024 if (scopy)
2025 return scopy;
2026 return copy;
2029 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2030 with VALUE expressions. This way, it becomes independent of changes
2031 to registers and memory.
2032 X isn't actually modified; if modifications are needed, new rtl is
2033 allocated. However, the return value can share rtl with X.
2034 If X is within a MEM, MEMMODE must be the mode of the MEM. */
2037 cselib_subst_to_values (rtx x, machine_mode memmode)
2039 enum rtx_code code = GET_CODE (x);
2040 const char *fmt = GET_RTX_FORMAT (code);
2041 cselib_val *e;
2042 struct elt_list *l;
2043 rtx copy = x;
2044 int i;
2045 poly_int64 offset;
2047 switch (code)
2049 case REG:
2050 l = REG_VALUES (REGNO (x));
2051 if (l && l->elt == NULL)
2052 l = l->next;
2053 for (; l; l = l->next)
2054 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
2055 return l->elt->val_rtx;
2057 gcc_unreachable ();
2059 case MEM:
2060 e = cselib_lookup_mem (x, 0);
2061 /* This used to happen for autoincrements, but we deal with them
2062 properly now. Remove the if stmt for the next release. */
2063 if (! e)
2065 /* Assign a value that doesn't match any other. */
2066 e = new_cselib_val (next_uid, GET_MODE (x), x);
2068 return e->val_rtx;
2070 case ENTRY_VALUE:
2071 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
2072 if (! e)
2073 break;
2074 return e->val_rtx;
2076 CASE_CONST_ANY:
2077 return x;
2079 case PRE_DEC:
2080 case PRE_INC:
2081 gcc_assert (memmode != VOIDmode);
2082 offset = GET_MODE_SIZE (memmode);
2083 if (code == PRE_DEC)
2084 offset = -offset;
2085 return cselib_subst_to_values (plus_constant (GET_MODE (x),
2086 XEXP (x, 0), offset),
2087 memmode);
2089 case PRE_MODIFY:
2090 gcc_assert (memmode != VOIDmode);
2091 return cselib_subst_to_values (XEXP (x, 1), memmode);
2093 case POST_DEC:
2094 case POST_INC:
2095 case POST_MODIFY:
2096 gcc_assert (memmode != VOIDmode);
2097 return cselib_subst_to_values (XEXP (x, 0), memmode);
2099 case PLUS:
2100 if (GET_MODE (x) == Pmode && CONST_INT_P (XEXP (x, 1)))
2102 rtx t = cselib_subst_to_values (XEXP (x, 0), memmode);
2103 if (GET_CODE (t) == VALUE)
2105 if (SP_DERIVED_VALUE_P (t) && XEXP (x, 1) == const0_rtx)
2106 return t;
2107 for (struct elt_loc_list *l = CSELIB_VAL_PTR (t)->locs;
2108 l; l = l->next)
2109 if (GET_CODE (l->loc) == PLUS
2110 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2111 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2112 && CONST_INT_P (XEXP (l->loc, 1)))
2113 return plus_constant (Pmode, l->loc, INTVAL (XEXP (x, 1)));
2115 if (t != XEXP (x, 0))
2117 copy = shallow_copy_rtx (x);
2118 XEXP (copy, 0) = t;
2120 return copy;
2123 default:
2124 break;
2127 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2129 if (fmt[i] == 'e')
2131 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
2133 if (t != XEXP (x, i))
2135 if (x == copy)
2136 copy = shallow_copy_rtx (x);
2137 XEXP (copy, i) = t;
2140 else if (fmt[i] == 'E')
2142 int j;
2144 for (j = 0; j < XVECLEN (x, i); j++)
2146 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
2148 if (t != XVECEXP (x, i, j))
2150 if (XVEC (x, i) == XVEC (copy, i))
2152 if (x == copy)
2153 copy = shallow_copy_rtx (x);
2154 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
2156 XVECEXP (copy, i, j) = t;
2162 return copy;
2165 /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
2168 cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
2170 rtx ret;
2171 gcc_assert (!cselib_current_insn);
2172 cselib_current_insn = insn;
2173 ret = cselib_subst_to_values (x, memmode);
2174 cselib_current_insn = NULL;
2175 return ret;
2178 /* Look up the rtl expression X in our tables and return the value it
2179 has. If CREATE is zero, we return NULL if we don't know the value.
2180 Otherwise, we create a new one if possible, using mode MODE if X
2181 doesn't have a mode (i.e. because it's a constant). When X is part
2182 of an address, MEMMODE should be the mode of the enclosing MEM if
2183 we're tracking autoinc expressions. */
2185 static cselib_val *
2186 cselib_lookup_1 (rtx x, machine_mode mode,
2187 int create, machine_mode memmode)
2189 cselib_val **slot;
2190 cselib_val *e;
2191 unsigned int hashval;
2193 if (GET_MODE (x) != VOIDmode)
2194 mode = GET_MODE (x);
2196 if (GET_CODE (x) == VALUE)
2197 return CSELIB_VAL_PTR (x);
2199 if (REG_P (x))
2201 struct elt_list *l;
2202 unsigned int i = REGNO (x);
2204 l = REG_VALUES (i);
2205 if (l && l->elt == NULL)
2206 l = l->next;
2207 for (; l; l = l->next)
2208 if (mode == GET_MODE (l->elt->val_rtx))
2210 promote_debug_loc (l->elt->locs);
2211 return l->elt;
2214 if (! create)
2215 return 0;
2217 if (i < FIRST_PSEUDO_REGISTER)
2219 unsigned int n = hard_regno_nregs (i, mode);
2221 if (n > max_value_regs)
2222 max_value_regs = n;
2225 e = new_cselib_val (next_uid, GET_MODE (x), x);
2226 if (GET_MODE (x) == Pmode && x == stack_pointer_rtx)
2227 SP_DERIVED_VALUE_P (e->val_rtx) = 1;
2228 new_elt_loc_list (e, x);
2230 scalar_int_mode int_mode;
2231 if (REG_VALUES (i) == 0)
2233 /* Maintain the invariant that the first entry of
2234 REG_VALUES, if present, must be the value used to set the
2235 register, or NULL. */
2236 used_regs[n_used_regs++] = i;
2237 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2239 else if (cselib_preserve_constants
2240 && is_int_mode (mode, &int_mode))
2242 /* During var-tracking, try harder to find equivalences
2243 for SUBREGs. If a setter sets say a DImode register
2244 and user uses that register only in SImode, add a lowpart
2245 subreg location. */
2246 struct elt_list *lwider = NULL;
2247 scalar_int_mode lmode;
2248 l = REG_VALUES (i);
2249 if (l && l->elt == NULL)
2250 l = l->next;
2251 for (; l; l = l->next)
2252 if (is_int_mode (GET_MODE (l->elt->val_rtx), &lmode)
2253 && GET_MODE_SIZE (lmode) > GET_MODE_SIZE (int_mode)
2254 && (lwider == NULL
2255 || partial_subreg_p (lmode,
2256 GET_MODE (lwider->elt->val_rtx))))
2258 struct elt_loc_list *el;
2259 if (i < FIRST_PSEUDO_REGISTER
2260 && hard_regno_nregs (i, lmode) != 1)
2261 continue;
2262 for (el = l->elt->locs; el; el = el->next)
2263 if (!REG_P (el->loc))
2264 break;
2265 if (el)
2266 lwider = l;
2268 if (lwider)
2270 rtx sub = lowpart_subreg (int_mode, lwider->elt->val_rtx,
2271 GET_MODE (lwider->elt->val_rtx));
2272 if (sub)
2273 new_elt_loc_list (e, sub);
2276 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2277 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2278 *slot = e;
2279 return e;
2282 if (MEM_P (x))
2283 return cselib_lookup_mem (x, create);
2285 hashval = cselib_hash_rtx (x, create, memmode);
2286 /* Can't even create if hashing is not possible. */
2287 if (! hashval)
2288 return 0;
2290 slot = cselib_find_slot (mode, x, hashval,
2291 create ? INSERT : NO_INSERT, memmode);
2292 if (slot == 0)
2293 return 0;
2295 e = (cselib_val *) *slot;
2296 if (e)
2297 return e;
2299 e = new_cselib_val (hashval, mode, x);
2301 /* We have to fill the slot before calling cselib_subst_to_values:
2302 the hash table is inconsistent until we do so, and
2303 cselib_subst_to_values will need to do lookups. */
2304 *slot = e;
2305 rtx v = cselib_subst_to_values (x, memmode);
2307 /* If cselib_preserve_constants, we might get a SP_DERIVED_VALUE_P
2308 VALUE that isn't in the hash tables anymore. */
2309 if (GET_CODE (v) == VALUE && SP_DERIVED_VALUE_P (v) && PRESERVED_VALUE_P (v))
2310 PRESERVED_VALUE_P (e->val_rtx) = 1;
2312 new_elt_loc_list (e, v);
2313 return e;
2316 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2318 cselib_val *
2319 cselib_lookup_from_insn (rtx x, machine_mode mode,
2320 int create, machine_mode memmode, rtx_insn *insn)
2322 cselib_val *ret;
2324 gcc_assert (!cselib_current_insn);
2325 cselib_current_insn = insn;
2327 ret = cselib_lookup (x, mode, create, memmode);
2329 cselib_current_insn = NULL;
2331 return ret;
2334 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2335 maintains invariants related with debug insns. */
2337 cselib_val *
2338 cselib_lookup (rtx x, machine_mode mode,
2339 int create, machine_mode memmode)
2341 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2343 /* ??? Should we return NULL if we're not to create an entry, the
2344 found loc is a debug loc and cselib_current_insn is not DEBUG?
2345 If so, we should also avoid converting val to non-DEBUG; probably
2346 easiest setting cselib_current_insn to NULL before the call
2347 above. */
2349 if (dump_file && (dump_flags & TDF_CSELIB))
2351 fputs ("cselib lookup ", dump_file);
2352 print_inline_rtx (dump_file, x, 2);
2353 fprintf (dump_file, " => %u:%u\n",
2354 ret ? ret->uid : 0,
2355 ret ? ret->hash : 0);
2358 return ret;
2361 /* Invalidate the value at *L, which is part of REG_VALUES (REGNO). */
2363 static void
2364 cselib_invalidate_regno_val (unsigned int regno, struct elt_list **l)
2366 cselib_val *v = (*l)->elt;
2367 if (*l == REG_VALUES (regno))
2369 /* Maintain the invariant that the first entry of
2370 REG_VALUES, if present, must be the value used to set
2371 the register, or NULL. This is also nice because
2372 then we won't push the same regno onto user_regs
2373 multiple times. */
2374 (*l)->elt = NULL;
2375 l = &(*l)->next;
2377 else
2378 unchain_one_elt_list (l);
2380 v = canonical_cselib_val (v);
2382 bool had_locs = v->locs != NULL;
2383 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2385 /* Now, we clear the mapping from value to reg. It must exist, so
2386 this code will crash intentionally if it doesn't. */
2387 for (elt_loc_list **p = &v->locs; ; p = &(*p)->next)
2389 rtx x = (*p)->loc;
2391 if (REG_P (x) && REGNO (x) == regno)
2393 unchain_one_elt_loc_list (p);
2394 break;
2398 if (had_locs && cselib_useless_value_p (v))
2400 if (setting_insn && DEBUG_INSN_P (setting_insn))
2401 n_useless_debug_values++;
2402 else
2403 n_useless_values++;
2407 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2408 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2409 is used to determine how many hard registers are being changed. If MODE
2410 is VOIDmode, then only REGNO is being changed; this is used when
2411 invalidating call clobbered registers across a call. */
2413 static void
2414 cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2416 unsigned int endregno;
2417 unsigned int i;
2419 /* If we see pseudos after reload, something is _wrong_. */
2420 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2421 || reg_renumber[regno] < 0);
2423 /* Determine the range of registers that must be invalidated. For
2424 pseudos, only REGNO is affected. For hard regs, we must take MODE
2425 into account, and we must also invalidate lower register numbers
2426 if they contain values that overlap REGNO. */
2427 if (regno < FIRST_PSEUDO_REGISTER)
2429 gcc_assert (mode != VOIDmode);
2431 if (regno < max_value_regs)
2432 i = 0;
2433 else
2434 i = regno - max_value_regs;
2436 endregno = end_hard_regno (mode, regno);
2438 else
2440 i = regno;
2441 endregno = regno + 1;
2444 for (; i < endregno; i++)
2446 struct elt_list **l = &REG_VALUES (i);
2448 /* Go through all known values for this reg; if it overlaps the range
2449 we're invalidating, remove the value. */
2450 while (*l)
2452 cselib_val *v = (*l)->elt;
2453 unsigned int this_last = i;
2455 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2456 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2458 if (this_last < regno || v == NULL
2459 || (v == cfa_base_preserved_val
2460 && i == cfa_base_preserved_regno))
2462 l = &(*l)->next;
2463 continue;
2466 /* We have an overlap. */
2467 cselib_invalidate_regno_val (i, l);
2472 /* Invalidate any locations in the table which are changed because of a
2473 store to MEM_RTX. If this is called because of a non-const call
2474 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2476 static void
2477 cselib_invalidate_mem (rtx mem_rtx)
2479 cselib_val **vp, *v, *next;
2480 int num_mems = 0;
2481 rtx mem_addr;
2483 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2484 mem_rtx = canon_rtx (mem_rtx);
2486 vp = &first_containing_mem;
2487 for (v = *vp; v != &dummy_val; v = next)
2489 bool has_mem = false;
2490 struct elt_loc_list **p = &v->locs;
2491 bool had_locs = v->locs != NULL;
2492 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2494 while (*p)
2496 rtx x = (*p)->loc;
2497 cselib_val *addr;
2498 struct elt_list **mem_chain;
2500 /* MEMs may occur in locations only at the top level; below
2501 that every MEM or REG is substituted by its VALUE. */
2502 if (!MEM_P (x))
2504 p = &(*p)->next;
2505 continue;
2507 if (num_mems < param_max_cselib_memory_locations
2508 && ! canon_anti_dependence (x, false, mem_rtx,
2509 GET_MODE (mem_rtx), mem_addr))
2511 has_mem = true;
2512 num_mems++;
2513 p = &(*p)->next;
2514 continue;
2517 /* This one overlaps. */
2518 /* We must have a mapping from this MEM's address to the
2519 value (E). Remove that, too. */
2520 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2521 addr = canonical_cselib_val (addr);
2522 gcc_checking_assert (v == canonical_cselib_val (v));
2523 mem_chain = &addr->addr_list;
2524 for (;;)
2526 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2528 if (canon == v)
2530 unchain_one_elt_list (mem_chain);
2531 break;
2534 /* Record canonicalized elt. */
2535 (*mem_chain)->elt = canon;
2537 mem_chain = &(*mem_chain)->next;
2540 unchain_one_elt_loc_list (p);
2543 if (had_locs && cselib_useless_value_p (v))
2545 if (setting_insn && DEBUG_INSN_P (setting_insn))
2546 n_useless_debug_values++;
2547 else
2548 n_useless_values++;
2551 next = v->next_containing_mem;
2552 if (has_mem)
2554 *vp = v;
2555 vp = &(*vp)->next_containing_mem;
2557 else
2558 v->next_containing_mem = NULL;
2560 *vp = &dummy_val;
2563 /* Invalidate DEST. */
2565 void
2566 cselib_invalidate_rtx (rtx dest)
2568 while (GET_CODE (dest) == SUBREG
2569 || GET_CODE (dest) == ZERO_EXTRACT
2570 || GET_CODE (dest) == STRICT_LOW_PART)
2571 dest = XEXP (dest, 0);
2573 if (REG_P (dest))
2574 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2575 else if (MEM_P (dest))
2576 cselib_invalidate_mem (dest);
2579 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2581 static void
2582 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx,
2583 void *data ATTRIBUTE_UNUSED)
2585 cselib_invalidate_rtx (dest);
2588 /* Record the result of a SET instruction. DEST is being set; the source
2589 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2590 describes its address. */
2592 static void
2593 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2595 if (src_elt == 0 || side_effects_p (dest))
2596 return;
2598 if (REG_P (dest))
2600 unsigned int dreg = REGNO (dest);
2601 if (dreg < FIRST_PSEUDO_REGISTER)
2603 unsigned int n = REG_NREGS (dest);
2605 if (n > max_value_regs)
2606 max_value_regs = n;
2609 if (REG_VALUES (dreg) == 0)
2611 used_regs[n_used_regs++] = dreg;
2612 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2614 else
2616 /* The register should have been invalidated. */
2617 gcc_assert (REG_VALUES (dreg)->elt == 0);
2618 REG_VALUES (dreg)->elt = src_elt;
2621 if (cselib_useless_value_p (src_elt))
2622 n_useless_values--;
2623 new_elt_loc_list (src_elt, dest);
2625 else if (MEM_P (dest) && dest_addr_elt != 0
2626 && cselib_record_memory)
2628 if (cselib_useless_value_p (src_elt))
2629 n_useless_values--;
2630 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2634 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2636 void
2637 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2639 cselib_val *nelt;
2640 rtx_insn *save_cselib_current_insn = cselib_current_insn;
2642 gcc_checking_assert (elt);
2643 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2644 gcc_checking_assert (!side_effects_p (x));
2646 cselib_current_insn = insn;
2648 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2650 if (nelt != elt)
2652 cselib_any_perm_equivs = true;
2654 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2655 cselib_preserve_value (nelt);
2657 new_elt_loc_list (nelt, elt->val_rtx);
2660 cselib_current_insn = save_cselib_current_insn;
2663 /* Return TRUE if any permanent equivalences have been recorded since
2664 the table was last initialized. */
2665 bool
2666 cselib_have_permanent_equivalences (void)
2668 return cselib_any_perm_equivs;
2671 /* Record stack_pointer_rtx to be equal to
2672 (plus:P cfa_base_preserved_val offset). Used by var-tracking
2673 at the start of basic blocks for !frame_pointer_needed functions. */
2675 void
2676 cselib_record_sp_cfa_base_equiv (HOST_WIDE_INT offset, rtx_insn *insn)
2678 rtx sp_derived_value = NULL_RTX;
2679 for (struct elt_loc_list *l = cfa_base_preserved_val->locs; l; l = l->next)
2680 if (GET_CODE (l->loc) == VALUE
2681 && SP_DERIVED_VALUE_P (l->loc))
2683 sp_derived_value = l->loc;
2684 break;
2686 else if (GET_CODE (l->loc) == PLUS
2687 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2688 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2689 && CONST_INT_P (XEXP (l->loc, 1)))
2691 sp_derived_value = XEXP (l->loc, 0);
2692 offset = offset + UINTVAL (XEXP (l->loc, 1));
2693 break;
2695 if (sp_derived_value == NULL_RTX)
2696 return;
2697 cselib_val *val
2698 = cselib_lookup_from_insn (plus_constant (Pmode, sp_derived_value, offset),
2699 Pmode, 1, VOIDmode, insn);
2700 if (val != NULL)
2702 PRESERVED_VALUE_P (val->val_rtx) = 1;
2703 cselib_record_set (stack_pointer_rtx, val, NULL);
2707 /* Return true if V is SP_DERIVED_VALUE_P (or SP_DERIVED_VALUE_P + CONST_INT)
2708 that can be expressed using cfa_base_preserved_val + CONST_INT. */
2710 bool
2711 cselib_sp_derived_value_p (cselib_val *v)
2713 if (!SP_DERIVED_VALUE_P (v->val_rtx))
2714 for (struct elt_loc_list *l = v->locs; l; l = l->next)
2715 if (GET_CODE (l->loc) == PLUS
2716 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2717 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2718 && CONST_INT_P (XEXP (l->loc, 1)))
2719 v = CSELIB_VAL_PTR (XEXP (l->loc, 0));
2720 if (!SP_DERIVED_VALUE_P (v->val_rtx))
2721 return false;
2722 for (struct elt_loc_list *l = v->locs; l; l = l->next)
2723 if (l->loc == cfa_base_preserved_val->val_rtx)
2724 return true;
2725 else if (GET_CODE (l->loc) == PLUS
2726 && XEXP (l->loc, 0) == cfa_base_preserved_val->val_rtx
2727 && CONST_INT_P (XEXP (l->loc, 1)))
2728 return true;
2729 return false;
2732 /* There is no good way to determine how many elements there can be
2733 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2734 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2736 struct cselib_record_autoinc_data
2738 struct cselib_set *sets;
2739 int n_sets;
2742 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2743 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2745 static int
2746 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2747 rtx dest, rtx src, rtx srcoff, void *arg)
2749 struct cselib_record_autoinc_data *data;
2750 data = (struct cselib_record_autoinc_data *)arg;
2752 data->sets[data->n_sets].dest = dest;
2754 if (srcoff)
2755 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2756 else
2757 data->sets[data->n_sets].src = src;
2759 data->n_sets++;
2761 return 0;
2764 /* Record the effects of any sets and autoincs in INSN. */
2765 static void
2766 cselib_record_sets (rtx_insn *insn)
2768 int n_sets = 0;
2769 int i;
2770 struct cselib_set sets[MAX_SETS];
2771 rtx cond = 0;
2772 int n_sets_before_autoinc;
2773 int n_strict_low_parts = 0;
2774 struct cselib_record_autoinc_data data;
2776 rtx body = PATTERN (insn);
2777 if (GET_CODE (body) == COND_EXEC)
2779 cond = COND_EXEC_TEST (body);
2780 body = COND_EXEC_CODE (body);
2783 /* Find all sets. */
2784 if (GET_CODE (body) == SET)
2786 sets[0].src = SET_SRC (body);
2787 sets[0].dest = SET_DEST (body);
2788 n_sets = 1;
2790 else if (GET_CODE (body) == PARALLEL)
2792 /* Look through the PARALLEL and record the values being
2793 set, if possible. Also handle any CLOBBERs. */
2794 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2796 rtx x = XVECEXP (body, 0, i);
2798 if (GET_CODE (x) == SET)
2800 sets[n_sets].src = SET_SRC (x);
2801 sets[n_sets].dest = SET_DEST (x);
2802 n_sets++;
2807 if (n_sets == 1
2808 && MEM_P (sets[0].src)
2809 && !cselib_record_memory
2810 && MEM_READONLY_P (sets[0].src))
2812 rtx note = find_reg_equal_equiv_note (insn);
2814 if (note && CONSTANT_P (XEXP (note, 0)))
2815 sets[0].src = XEXP (note, 0);
2818 data.sets = sets;
2819 data.n_sets = n_sets_before_autoinc = n_sets;
2820 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
2821 n_sets = data.n_sets;
2823 /* Look up the values that are read. Do this before invalidating the
2824 locations that are written. */
2825 for (i = 0; i < n_sets; i++)
2827 rtx dest = sets[i].dest;
2828 rtx orig = dest;
2830 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2831 the low part after invalidating any knowledge about larger modes. */
2832 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2833 sets[i].dest = dest = XEXP (dest, 0);
2835 /* We don't know how to record anything but REG or MEM. */
2836 if (REG_P (dest)
2837 || (MEM_P (dest) && cselib_record_memory))
2839 rtx src = sets[i].src;
2840 if (cond)
2841 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2842 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2843 if (MEM_P (dest))
2845 machine_mode address_mode = get_address_mode (dest);
2847 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2848 address_mode, 1,
2849 GET_MODE (dest));
2851 else
2852 sets[i].dest_addr_elt = 0;
2855 /* Improve handling of STRICT_LOW_PART if the current value is known
2856 to be const0_rtx, then the low bits will be set to dest and higher
2857 bits will remain zero. Used in code like:
2859 {di:SI=0;clobber flags:CC;}
2860 flags:CCNO=cmp(bx:SI,0)
2861 strict_low_part(di:QI)=flags:CCNO<=0
2863 where we can note both that di:QI=flags:CCNO<=0 and
2864 also that because di:SI is known to be 0 and strict_low_part(di:QI)
2865 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0). */
2866 scalar_int_mode mode;
2867 if (dest != orig
2868 && cselib_record_sets_hook
2869 && REG_P (dest)
2870 && HARD_REGISTER_P (dest)
2871 && sets[i].src_elt
2872 && is_a <scalar_int_mode> (GET_MODE (dest), &mode)
2873 && n_sets + n_strict_low_parts < MAX_SETS)
2875 opt_scalar_int_mode wider_mode_iter;
2876 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2878 scalar_int_mode wider_mode = wider_mode_iter.require ();
2879 if (GET_MODE_PRECISION (wider_mode) > BITS_PER_WORD)
2880 break;
2882 rtx reg = gen_lowpart (wider_mode, dest);
2883 if (!REG_P (reg))
2884 break;
2886 cselib_val *v = cselib_lookup (reg, wider_mode, 0, VOIDmode);
2887 if (!v)
2888 continue;
2890 struct elt_loc_list *l;
2891 for (l = v->locs; l; l = l->next)
2892 if (l->loc == const0_rtx)
2893 break;
2895 if (!l)
2896 continue;
2898 sets[n_sets + n_strict_low_parts].dest = reg;
2899 sets[n_sets + n_strict_low_parts].src = dest;
2900 sets[n_sets + n_strict_low_parts++].src_elt = sets[i].src_elt;
2901 break;
2906 if (cselib_record_sets_hook)
2907 cselib_record_sets_hook (insn, sets, n_sets);
2909 /* Invalidate all locations written by this insn. Note that the elts we
2910 looked up in the previous loop aren't affected, just some of their
2911 locations may go away. */
2912 note_pattern_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2914 for (i = n_sets_before_autoinc; i < n_sets; i++)
2915 cselib_invalidate_rtx (sets[i].dest);
2917 /* If this is an asm, look for duplicate sets. This can happen when the
2918 user uses the same value as an output multiple times. This is valid
2919 if the outputs are not actually used thereafter. Treat this case as
2920 if the value isn't actually set. We do this by smashing the destination
2921 to pc_rtx, so that we won't record the value later. */
2922 if (n_sets >= 2 && asm_noperands (body) >= 0)
2924 for (i = 0; i < n_sets; i++)
2926 rtx dest = sets[i].dest;
2927 if (REG_P (dest) || MEM_P (dest))
2929 int j;
2930 for (j = i + 1; j < n_sets; j++)
2931 if (rtx_equal_p (dest, sets[j].dest))
2933 sets[i].dest = pc_rtx;
2934 sets[j].dest = pc_rtx;
2940 /* Now enter the equivalences in our tables. */
2941 for (i = 0; i < n_sets; i++)
2943 rtx dest = sets[i].dest;
2944 if (REG_P (dest)
2945 || (MEM_P (dest) && cselib_record_memory))
2946 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2949 /* And deal with STRICT_LOW_PART. */
2950 for (i = 0; i < n_strict_low_parts; i++)
2952 if (! PRESERVED_VALUE_P (sets[n_sets + i].src_elt->val_rtx))
2953 continue;
2954 machine_mode dest_mode = GET_MODE (sets[n_sets + i].dest);
2955 cselib_val *v
2956 = cselib_lookup (sets[n_sets + i].dest, dest_mode, 1, VOIDmode);
2957 cselib_preserve_value (v);
2958 rtx r = gen_rtx_ZERO_EXTEND (dest_mode,
2959 sets[n_sets + i].src_elt->val_rtx);
2960 cselib_add_permanent_equiv (v, r, insn);
2964 /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2966 bool
2967 fp_setter_insn (rtx_insn *insn)
2969 rtx expr, pat = NULL_RTX;
2971 if (!RTX_FRAME_RELATED_P (insn))
2972 return false;
2974 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2975 if (expr)
2976 pat = XEXP (expr, 0);
2977 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2978 return false;
2980 /* Don't return true for frame pointer restores in the epilogue. */
2981 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2982 return false;
2983 return true;
2986 /* V is one of the values in REG_VALUES (REGNO). Return true if it
2987 would be invalidated by CALLEE_ABI. */
2989 static bool
2990 cselib_invalidated_by_call_p (const function_abi &callee_abi,
2991 unsigned int regno, cselib_val *v)
2993 machine_mode mode = GET_MODE (v->val_rtx);
2994 if (mode == VOIDmode)
2996 v = REG_VALUES (regno)->elt;
2997 if (!v)
2998 /* If we don't know what the mode of the constant value is, and we
2999 don't know what mode the register was set in, conservatively
3000 assume that the register is clobbered. The value's going to be
3001 essentially useless in this case anyway. */
3002 return true;
3003 mode = GET_MODE (v->val_rtx);
3005 return callee_abi.clobbers_reg_p (mode, regno);
3008 /* Record the effects of INSN. */
3010 void
3011 cselib_process_insn (rtx_insn *insn)
3013 int i;
3014 rtx x;
3016 cselib_current_insn = insn;
3018 /* Forget everything at a CODE_LABEL or a setjmp. */
3019 if ((LABEL_P (insn)
3020 || (CALL_P (insn)
3021 && find_reg_note (insn, REG_SETJMP, NULL)))
3022 && !cselib_preserve_constants)
3024 cselib_reset_table (next_uid);
3025 cselib_current_insn = NULL;
3026 return;
3029 if (! INSN_P (insn))
3031 cselib_current_insn = NULL;
3032 return;
3035 /* If this is a call instruction, forget anything stored in a
3036 call clobbered register, or, if this is not a const call, in
3037 memory. */
3038 if (CALL_P (insn))
3040 function_abi callee_abi = insn_callee_abi (insn);
3041 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3043 elt_list **l = &REG_VALUES (i);
3044 while (*l)
3046 cselib_val *v = (*l)->elt;
3047 if (v && cselib_invalidated_by_call_p (callee_abi, i, v))
3048 cselib_invalidate_regno_val (i, l);
3049 else
3050 l = &(*l)->next;
3054 /* Since it is not clear how cselib is going to be used, be
3055 conservative here and treat looping pure or const functions
3056 as if they were regular functions. */
3057 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
3058 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
3059 cselib_invalidate_mem (callmem);
3060 else
3061 /* For const/pure calls, invalidate any argument slots because
3062 they are owned by the callee. */
3063 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3064 if (GET_CODE (XEXP (x, 0)) == USE
3065 && MEM_P (XEXP (XEXP (x, 0), 0)))
3066 cselib_invalidate_mem (XEXP (XEXP (x, 0), 0));
3069 cselib_record_sets (insn);
3071 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3072 after we have processed the insn. */
3073 if (CALL_P (insn))
3075 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3076 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
3077 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
3079 /* Flush everything on setjmp. */
3080 if (cselib_preserve_constants
3081 && find_reg_note (insn, REG_SETJMP, NULL))
3083 cselib_preserve_only_values ();
3084 cselib_reset_table (next_uid);
3088 /* On setter of the hard frame pointer if frame_pointer_needed,
3089 invalidate stack_pointer_rtx, so that sp and {,h}fp based
3090 VALUEs are distinct. */
3091 if (reload_completed
3092 && frame_pointer_needed
3093 && fp_setter_insn (insn))
3094 cselib_invalidate_rtx (stack_pointer_rtx);
3096 cselib_current_insn = NULL;
3098 if (n_useless_values > MAX_USELESS_VALUES
3099 /* remove_useless_values is linear in the hash table size. Avoid
3100 quadratic behavior for very large hashtables with very few
3101 useless elements. */
3102 && ((unsigned int)n_useless_values
3103 > (cselib_hash_table->elements () - n_debug_values) / 4))
3104 remove_useless_values ();
3107 /* Initialize cselib for one pass. The caller must also call
3108 init_alias_analysis. */
3110 void
3111 cselib_init (int record_what)
3113 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
3114 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
3115 cselib_any_perm_equivs = false;
3117 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
3118 see canon_true_dependence. This is only created once. */
3119 if (! callmem)
3120 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
3122 cselib_nregs = max_reg_num ();
3124 /* We preserve reg_values to allow expensive clearing of the whole thing.
3125 Reallocate it however if it happens to be too large. */
3126 if (!reg_values || reg_values_size < cselib_nregs
3127 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
3129 free (reg_values);
3130 /* Some space for newly emit instructions so we don't end up
3131 reallocating in between passes. */
3132 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
3133 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
3135 used_regs = XNEWVEC (unsigned int, cselib_nregs);
3136 n_used_regs = 0;
3137 /* FIXME: enable sanitization (PR87845) */
3138 cselib_hash_table
3139 = new hash_table<cselib_hasher> (31, /* ggc */ false,
3140 /* sanitize_eq_and_hash */ false);
3141 if (cselib_preserve_constants)
3142 cselib_preserved_hash_table
3143 = new hash_table<cselib_hasher> (31, /* ggc */ false,
3144 /* sanitize_eq_and_hash */ false);
3145 next_uid = 1;
3148 /* Called when the current user is done with cselib. */
3150 void
3151 cselib_finish (void)
3153 bool preserved = cselib_preserve_constants;
3154 cselib_discard_hook = NULL;
3155 cselib_preserve_constants = false;
3156 cselib_any_perm_equivs = false;
3157 cfa_base_preserved_val = NULL;
3158 cfa_base_preserved_regno = INVALID_REGNUM;
3159 elt_list_pool.release ();
3160 elt_loc_list_pool.release ();
3161 cselib_val_pool.release ();
3162 value_pool.release ();
3163 cselib_clear_table ();
3164 delete cselib_hash_table;
3165 cselib_hash_table = NULL;
3166 if (preserved)
3167 delete cselib_preserved_hash_table;
3168 cselib_preserved_hash_table = NULL;
3169 free (used_regs);
3170 used_regs = 0;
3171 n_useless_values = 0;
3172 n_useless_debug_values = 0;
3173 n_debug_values = 0;
3174 next_uid = 0;
3177 /* Dump the cselib_val *X to FILE *OUT. */
3180 dump_cselib_val (cselib_val **x, FILE *out)
3182 cselib_val *v = *x;
3183 bool need_lf = true;
3185 print_inline_rtx (out, v->val_rtx, 0);
3187 if (v->locs)
3189 struct elt_loc_list *l = v->locs;
3190 if (need_lf)
3192 fputc ('\n', out);
3193 need_lf = false;
3195 fputs (" locs:", out);
3198 if (l->setting_insn)
3199 fprintf (out, "\n from insn %i ",
3200 INSN_UID (l->setting_insn));
3201 else
3202 fprintf (out, "\n ");
3203 print_inline_rtx (out, l->loc, 4);
3205 while ((l = l->next));
3206 fputc ('\n', out);
3208 else
3210 fputs (" no locs", out);
3211 need_lf = true;
3214 if (v->addr_list)
3216 struct elt_list *e = v->addr_list;
3217 if (need_lf)
3219 fputc ('\n', out);
3220 need_lf = false;
3222 fputs (" addr list:", out);
3225 fputs ("\n ", out);
3226 print_inline_rtx (out, e->elt->val_rtx, 2);
3228 while ((e = e->next));
3229 fputc ('\n', out);
3231 else
3233 fputs (" no addrs", out);
3234 need_lf = true;
3237 if (v->next_containing_mem == &dummy_val)
3238 fputs (" last mem\n", out);
3239 else if (v->next_containing_mem)
3241 fputs (" next mem ", out);
3242 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
3243 fputc ('\n', out);
3245 else if (need_lf)
3246 fputc ('\n', out);
3248 return 1;
3251 /* Dump to OUT everything in the CSELIB table. */
3253 void
3254 dump_cselib_table (FILE *out)
3256 fprintf (out, "cselib hash table:\n");
3257 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
3258 fprintf (out, "cselib preserved hash table:\n");
3259 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
3260 if (first_containing_mem != &dummy_val)
3262 fputs ("first mem ", out);
3263 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
3264 fputc ('\n', out);
3266 fprintf (out, "next uid %i\n", next_uid);
3269 #include "gt-cselib.h"