Line data Source code
1 : /* Common subexpression elimination library for GNU compiler.
2 : Copyright (C) 1987-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
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.
10 :
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.
15 :
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/>. */
19 :
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"
35 : #include "alias.h"
36 : #include "predict.h"
37 : #include "rtl-iter.h"
38 :
39 : /* A list of cselib_val structures. */
40 : struct elt_list
41 : {
42 : struct elt_list *next;
43 : cselib_val *elt;
44 : };
45 :
46 : static bool cselib_record_memory;
47 : static bool cselib_preserve_constants;
48 : static bool cselib_any_perm_equivs;
49 : static inline void promote_debug_loc (struct elt_loc_list *l);
50 : static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
51 : static void new_elt_loc_list (cselib_val *, rtx);
52 : static void unchain_one_value (cselib_val *);
53 : static void unchain_one_elt_list (struct elt_list **);
54 : static void unchain_one_elt_loc_list (struct elt_loc_list **);
55 : static void remove_useless_values (void);
56 : static hashval_t cselib_hash_rtx (rtx, int, machine_mode);
57 : static cselib_val *new_cselib_val (unsigned int, machine_mode, rtx);
58 : static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
59 : static cselib_val *cselib_lookup_mem (rtx, int);
60 : static void cselib_invalidate_regno (unsigned int, machine_mode);
61 : static void cselib_invalidate_mem (rtx);
62 : static void cselib_record_set (rtx, cselib_val *, cselib_val *);
63 : static void cselib_record_sets (rtx_insn *);
64 : static rtx autoinc_split (rtx, rtx *, machine_mode);
65 :
66 : #define PRESERVED_VALUE_P(RTX) \
67 : (RTL_FLAG_CHECK1 ("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
68 :
69 : #define SP_BASED_VALUE_P(RTX) \
70 : (RTL_FLAG_CHECK1 ("SP_BASED_VALUE_P", (RTX), VALUE)->jump)
71 :
72 : #define SP_DERIVED_VALUE_P(RTX) \
73 : (RTL_FLAG_CHECK1 ("SP_DERIVED_VALUE_P", (RTX), VALUE)->call)
74 :
75 : struct expand_value_data
76 : {
77 : bitmap regs_active;
78 : cselib_expand_callback callback;
79 : void *callback_arg;
80 : bool dummy;
81 : };
82 :
83 : static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
84 :
85 : /* This is a global so we don't have to pass this through every function.
86 : It is used in new_elt_loc_list to set SETTING_INSN. */
87 : static rtx_insn *cselib_current_insn;
88 :
89 : /* There are three ways in which cselib can look up an rtx:
90 : - for a REG, the reg_values table (which is indexed by regno) is used
91 : - for a MEM, we recursively look up its address and then follow the
92 : addr_list of that value
93 : - for everything else, we compute a hash value and go through the hash
94 : table. Since different rtx's can still have the same hash value,
95 : this involves walking the table entries for a given value and comparing
96 : the locations of the entries with the rtx we are looking up. */
97 :
98 : struct cselib_hasher : nofree_ptr_hash <cselib_val>
99 : {
100 : struct key {
101 : /* The rtx value and its mode (needed separately for constant
102 : integers). */
103 : machine_mode mode;
104 : rtx x;
105 : /* The mode of the containing MEM, if any, otherwise VOIDmode. */
106 : machine_mode memmode;
107 : };
108 : typedef key *compare_type;
109 : static inline hashval_t hash (const cselib_val *);
110 : static inline bool equal (const cselib_val *, const key *);
111 : };
112 :
113 : /* The hash function for our hash table. The value is always computed with
114 : cselib_hash_rtx when adding an element; this function just extracts the
115 : hash value from a cselib_val structure. */
116 :
117 : inline hashval_t
118 101121503 : cselib_hasher::hash (const cselib_val *v)
119 : {
120 101121503 : return v->hash;
121 : }
122 :
123 : /* The equality test for our hash table. The first argument V is a table
124 : element (i.e. a cselib_val), while the second arg X is an rtx. We know
125 : that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
126 : CONST of an appropriate mode. */
127 :
128 : inline bool
129 572174648 : cselib_hasher::equal (const cselib_val *v, const key *x_arg)
130 : {
131 572174648 : struct elt_loc_list *l;
132 572174648 : rtx x = x_arg->x;
133 572174648 : machine_mode mode = x_arg->mode;
134 572174648 : machine_mode memmode = x_arg->memmode;
135 :
136 572174648 : if (mode != GET_MODE (v->val_rtx))
137 : return false;
138 :
139 462597955 : if (GET_CODE (x) == VALUE)
140 12650335 : return x == v->val_rtx;
141 :
142 459523529 : if (SP_DERIVED_VALUE_P (v->val_rtx) && GET_MODE (x) == Pmode)
143 : {
144 17643600 : rtx xoff = NULL;
145 17643600 : if (autoinc_split (x, &xoff, memmode) == v->val_rtx && xoff == NULL_RTX)
146 7911137 : return true;
147 : }
148 :
149 : /* We don't guarantee that distinct rtx's have different hash values,
150 : so we need to do a comparison. */
151 762461491 : for (l = v->locs; l; l = l->next)
152 496271768 : if (l->setting_insn && DEBUG_INSN_P (l->setting_insn)
153 38704068 : && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
154 : {
155 11031293 : rtx_insn *save_cselib_current_insn = cselib_current_insn;
156 : /* If l is so far a debug only loc, without debug stmts it
157 : would never be compared to x at all, so temporarily pretend
158 : current instruction is that DEBUG_INSN so that we don't
159 : promote other debug locs even for unsuccessful comparison. */
160 11031293 : cselib_current_insn = l->setting_insn;
161 11031293 : bool match = rtx_equal_for_cselib_1 (l->loc, x, memmode, 0);
162 11031293 : cselib_current_insn = save_cselib_current_insn;
163 11031293 : if (match)
164 : {
165 859863 : promote_debug_loc (l);
166 859863 : return true;
167 : }
168 : }
169 485240475 : else if (rtx_equal_for_cselib_1 (l->loc, x, memmode, 0))
170 : return true;
171 :
172 : return false;
173 : }
174 :
175 : /* A table that enables us to look up elts by their value. */
176 : static hash_table<cselib_hasher> *cselib_hash_table;
177 :
178 : /* A table to hold preserved values. */
179 : static hash_table<cselib_hasher> *cselib_preserved_hash_table;
180 :
181 : /* The subset of cselib_preserved_hash_table that might have useless locations.
182 : It excludes values for which all_locs_preserved_p is true.
183 :
184 : This is an important compile-time optimization for inputs that have
185 : many preserved values and many basic blocks (such as insn-extract.cc
186 : at the time of writing, especially with RTL checking enabled).
187 : If remove_useless_values iterated over the whole hash table for every
188 : block, it would repeat a lot of useless and cache-unfriendly work. */
189 : static vec<cselib_val *> cselib_preserved_prune_list;
190 :
191 : /* The unique id that the next create value will take. */
192 : static unsigned int next_uid;
193 :
194 : /* The number of registers we had when the varrays were last resized. */
195 : static unsigned int cselib_nregs;
196 :
197 : /* Count values without known locations, or with only locations that
198 : wouldn't have been known except for debug insns. Whenever this
199 : grows too big, we remove these useless values from the table.
200 :
201 : Counting values with only debug values is a bit tricky. We don't
202 : want to increment n_useless_values when we create a value for a
203 : debug insn, for this would get n_useless_values out of sync, but we
204 : want increment it if all locs in the list that were ever referenced
205 : in nondebug insns are removed from the list.
206 :
207 : In the general case, once we do that, we'd have to stop accepting
208 : nondebug expressions in the loc list, to avoid having two values
209 : equivalent that, without debug insns, would have been made into
210 : separate values. However, because debug insns never introduce
211 : equivalences themselves (no assignments), the only means for
212 : growing loc lists is through nondebug assignments. If the locs
213 : also happen to be referenced in debug insns, it will work just fine.
214 :
215 : A consequence of this is that there's at most one debug-only loc in
216 : each loc list. If we keep it in the first entry, testing whether
217 : we have a debug-only loc list takes O(1).
218 :
219 : Furthermore, since any additional entry in a loc list containing a
220 : debug loc would have to come from an assignment (nondebug) that
221 : references both the initial debug loc and the newly-equivalent loc,
222 : the initial debug loc would be promoted to a nondebug loc, and the
223 : loc list would not contain debug locs any more.
224 :
225 : So the only case we have to be careful with in order to keep
226 : n_useless_values in sync between debug and nondebug compilations is
227 : to avoid incrementing n_useless_values when removing the single loc
228 : from a value that turns out to not appear outside debug values. We
229 : increment n_useless_debug_values instead, and leave such values
230 : alone until, for other reasons, we garbage-collect useless
231 : values. */
232 : static int n_useless_values;
233 : static int n_useless_debug_values;
234 :
235 : /* Count values whose locs have been taken exclusively from debug
236 : insns for the entire life of the value. */
237 : static int n_debug_values;
238 :
239 : /* Number of useless values before we remove them from the hash table. */
240 : #define MAX_USELESS_VALUES 32
241 :
242 : /* This table maps from register number to values. It does not
243 : contain pointers to cselib_val structures, but rather elt_lists.
244 : The purpose is to be able to refer to the same register in
245 : different modes. The first element of the list defines the mode in
246 : which the register was set; if the mode is unknown or the value is
247 : no longer valid in that mode, ELT will be NULL for the first
248 : element. */
249 : static struct elt_list **reg_values;
250 : static unsigned int reg_values_size;
251 : #define REG_VALUES(i) reg_values[i]
252 :
253 : /* The largest number of hard regs used by any entry added to the
254 : REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
255 : static unsigned int max_value_regs;
256 :
257 : /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
258 : in cselib_clear_table() for fast emptying. */
259 : static unsigned int *used_regs;
260 : static unsigned int n_used_regs;
261 :
262 : /* We pass this to cselib_invalidate_mem to invalidate all of
263 : memory for a non-const call instruction and memory below stack pointer
264 : for const/pure calls. */
265 : static GTY(()) rtx callmem[2];
266 :
267 : /* Set by discard_useless_locs if it deleted the last location of any
268 : value. */
269 : static int values_became_useless;
270 :
271 : /* Used as stop element of the containing_mem list so we can check
272 : presence in the list by checking the next pointer. */
273 : static cselib_val dummy_val;
274 :
275 : /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
276 : that is constant through the whole function and should never be
277 : eliminated. */
278 : static cselib_val *cfa_base_preserved_val;
279 : static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
280 :
281 : /* Used to list all values that contain memory reference.
282 : May or may not contain the useless values - the list is compacted
283 : each time memory is invalidated. */
284 : static cselib_val *first_containing_mem = &dummy_val;
285 :
286 : static object_allocator<elt_list> elt_list_pool ("elt_list");
287 : static object_allocator<elt_loc_list> elt_loc_list_pool ("elt_loc_list");
288 : static object_allocator<cselib_val> cselib_val_pool ("cselib_val_list");
289 :
290 : static pool_allocator value_pool ("value", RTX_CODE_SIZE (VALUE));
291 :
292 : /* If nonnull, cselib will call this function before freeing useless
293 : VALUEs. A VALUE is deemed useless if its "locs" field is null. */
294 : void (*cselib_discard_hook) (cselib_val *);
295 :
296 : /* If nonnull, cselib will call this function before recording sets or
297 : even clobbering outputs of INSN. All the recorded sets will be
298 : represented in the array sets[n_sets]. new_val_min can be used to
299 : tell whether values present in sets are introduced by this
300 : instruction. */
301 : void (*cselib_record_sets_hook) (rtx_insn *insn, struct cselib_set *sets,
302 : int n_sets);
303 :
304 :
305 :
306 : /* Allocate a struct elt_list and fill in its two elements with the
307 : arguments. */
308 :
309 : static inline struct elt_list *
310 479346252 : new_elt_list (struct elt_list *next, cselib_val *elt)
311 : {
312 958692504 : elt_list *el = elt_list_pool.allocate ();
313 479346252 : el->next = next;
314 479346252 : el->elt = elt;
315 479346252 : return el;
316 : }
317 :
318 : /* Record that all_locs_preserved_p might no longer hold for VAL. */
319 :
320 : static inline void
321 716130463 : cselib_clear_all_locs_preserved (cselib_val *val)
322 : {
323 716130463 : if (val->all_locs_preserved_p)
324 : {
325 7584176 : val->all_locs_preserved_p = false;
326 7584176 : if (val->in_preserved_table_p)
327 : /* VAL would have been removed from cselib_preserved_prune_list
328 : but now needs to be considered by remove_useless_values. */
329 7530890 : cselib_preserved_prune_list.safe_push (val);
330 : }
331 716130463 : }
332 :
333 : /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
334 : list. */
335 :
336 : static inline void
337 712258904 : new_elt_loc_list (cselib_val *val, rtx loc)
338 : {
339 716135000 : struct elt_loc_list *el, *next = val->locs;
340 :
341 716135000 : gcc_checking_assert (!next || !next->setting_insn
342 : || !DEBUG_INSN_P (next->setting_insn)
343 : || cselib_current_insn == next->setting_insn);
344 :
345 : /* If we're creating the first loc in a debug insn context, we've
346 : just created a debug value. Count it. */
347 419222312 : if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
348 6636566 : n_debug_values++;
349 :
350 716135000 : val = canonical_cselib_val (val);
351 716135000 : next = val->locs;
352 :
353 716135000 : if (GET_CODE (loc) == VALUE)
354 : {
355 7757159 : loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
356 7757159 : auto *loc_val = CSELIB_VAL_PTR (loc);
357 :
358 7757159 : gcc_checking_assert (PRESERVED_VALUE_P (loc)
359 : == PRESERVED_VALUE_P (val->val_rtx));
360 :
361 7757159 : if (val->val_rtx == loc)
362 : return;
363 7752407 : else if (val->uid > loc_val->uid)
364 : {
365 : /* Reverse the insertion. */
366 : new_elt_loc_list (loc_val, val->val_rtx);
367 : return;
368 : }
369 :
370 3876311 : gcc_checking_assert (val->uid < loc_val->uid);
371 :
372 3876311 : if (loc_val->locs)
373 : {
374 : /* Bring all locs from LOC to VAL. */
375 2928321 : for (el = loc_val->locs; el->next; el = el->next)
376 : {
377 : /* Adjust values that have LOC as canonical so that VAL
378 : becomes their canonical. */
379 43 : if (el->loc && GET_CODE (el->loc) == VALUE)
380 : {
381 0 : auto *el_val = CSELIB_VAL_PTR (el->loc);
382 0 : gcc_checking_assert (el_val->locs->loc == loc);
383 0 : el_val->locs->loc = val->val_rtx;
384 0 : cselib_clear_all_locs_preserved (el_val);
385 : }
386 : }
387 2928278 : el->next = val->locs;
388 2928278 : next = val->locs = loc_val->locs;
389 : }
390 :
391 3876311 : if (loc_val->addr_list)
392 : {
393 : /* Bring in addr_list into canonical node. */
394 : struct elt_list *last = loc_val->addr_list;
395 7 : while (last->next)
396 : last = last->next;
397 7 : last->next = val->addr_list;
398 7 : val->addr_list = loc_val->addr_list;
399 7 : loc_val->addr_list = NULL;
400 : }
401 :
402 3876311 : if (loc_val->next_containing_mem != NULL
403 0 : && val->next_containing_mem == NULL)
404 : {
405 : /* Add VAL to the containing_mem list after LOC. LOC will
406 : be removed when we notice it doesn't contain any
407 : MEMs. */
408 0 : val->next_containing_mem = loc_val->next_containing_mem;
409 0 : loc_val->next_containing_mem = val;
410 : }
411 :
412 : /* Chain LOC back to VAL. */
413 3876311 : el = elt_loc_list_pool.allocate ();
414 3876311 : el->loc = val->val_rtx;
415 3876311 : el->setting_insn = cselib_current_insn;
416 3876311 : el->next = NULL;
417 3876311 : loc_val->locs = el;
418 3876311 : cselib_clear_all_locs_preserved (loc_val);
419 : }
420 :
421 712254152 : el = elt_loc_list_pool.allocate ();
422 712254152 : el->loc = loc;
423 712254152 : el->setting_insn = cselib_current_insn;
424 712254152 : el->next = next;
425 712254152 : val->locs = el;
426 712254152 : cselib_clear_all_locs_preserved (val);
427 : }
428 :
429 : /* Promote loc L to a nondebug cselib_current_insn if L is marked as
430 : originating from a debug insn, maintaining the debug values
431 : count. */
432 :
433 : static inline void
434 1217866937 : promote_debug_loc (struct elt_loc_list *l)
435 : {
436 1217866937 : if (l && l->setting_insn && DEBUG_INSN_P (l->setting_insn)
437 21680791 : && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
438 : {
439 2483876 : n_debug_values--;
440 2483876 : l->setting_insn = cselib_current_insn;
441 2483876 : if (cselib_preserve_constants && l->next)
442 : {
443 21331 : gcc_assert (l->next->setting_insn
444 : && DEBUG_INSN_P (l->next->setting_insn)
445 : && !l->next->next);
446 21331 : l->next->setting_insn = cselib_current_insn;
447 : }
448 : else
449 2462545 : gcc_assert (!l->next);
450 : }
451 1217866937 : }
452 :
453 : /* The elt_list at *PL is no longer needed. Unchain it and free its
454 : storage. */
455 :
456 : static inline void
457 79360604 : unchain_one_elt_list (struct elt_list **pl)
458 : {
459 79360604 : struct elt_list *l = *pl;
460 :
461 79360604 : *pl = l->next;
462 116417138 : elt_list_pool.remove (l);
463 42304070 : }
464 :
465 : /* Likewise for elt_loc_lists. */
466 :
467 : static void
468 220778626 : unchain_one_elt_loc_list (struct elt_loc_list **pl)
469 : {
470 220778626 : struct elt_loc_list *l = *pl;
471 :
472 220778626 : *pl = l->next;
473 0 : elt_loc_list_pool.remove (l);
474 38329464 : }
475 :
476 : /* Likewise for cselib_vals. This also frees the addr_list associated with
477 : V. */
478 :
479 : static void
480 2358656 : unchain_one_value (cselib_val *v)
481 : {
482 2378124 : while (v->addr_list)
483 19468 : unchain_one_elt_list (&v->addr_list);
484 :
485 2358656 : cselib_val_pool.remove (v);
486 2358656 : }
487 :
488 : /* Remove all entries from the hash table. Also used during
489 : initialization. */
490 :
491 : void
492 60116051 : cselib_clear_table (void)
493 : {
494 60116051 : cselib_reset_table (1);
495 60116051 : }
496 :
497 : /* Return TRUE if V is a constant, a function invariant or a VALUE
498 : equivalence; FALSE otherwise. */
499 :
500 : static bool
501 57093361 : invariant_or_equiv_p (cselib_val *v)
502 : {
503 57093361 : struct elt_loc_list *l;
504 :
505 57093361 : if (v == cfa_base_preserved_val)
506 : return true;
507 :
508 : /* Keep VALUE equivalences around. */
509 79338254 : for (l = v->locs; l; l = l->next)
510 38305345 : if (GET_CODE (l->loc) == VALUE)
511 : return true;
512 :
513 41032909 : if (v->locs != NULL
514 22546198 : && v->locs->next == NULL)
515 : {
516 22492441 : if (CONSTANT_P (v->locs->loc)
517 22492441 : && (GET_CODE (v->locs->loc) != CONST
518 159944 : || !references_value_p (v->locs->loc)))
519 3461005 : return true;
520 : /* Although a debug expr may be bound to different expressions,
521 : we can preserve it as if it was constant, to get unification
522 : and proper merging within var-tracking. */
523 19031436 : if (GET_CODE (v->locs->loc) == DEBUG_EXPR
524 17506795 : || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
525 17033097 : || GET_CODE (v->locs->loc) == ENTRY_VALUE
526 17032870 : || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
527 : return true;
528 :
529 : /* (plus (value V) (const_int C)) is invariant iff V is invariant. */
530 17016994 : if (GET_CODE (v->locs->loc) == PLUS
531 10609955 : && CONST_INT_P (XEXP (v->locs->loc, 1))
532 9470986 : && GET_CODE (XEXP (v->locs->loc, 0)) == VALUE
533 25751839 : && invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (v->locs->loc, 0))))
534 : return true;
535 : }
536 :
537 : return false;
538 : }
539 :
540 : /* Remove from hash table all VALUEs except constants, function
541 : invariants and VALUE equivalences. */
542 :
543 : int
544 44058713 : preserve_constants_and_equivs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
545 : {
546 44058713 : cselib_val *v = *x;
547 :
548 44058713 : if (invariant_or_equiv_p (v))
549 : {
550 17236096 : cselib_hasher::key lookup = {
551 17236096 : GET_MODE (v->val_rtx), v->val_rtx, VOIDmode
552 17236096 : };
553 17236096 : cselib_val **slot
554 34472192 : = cselib_preserved_hash_table->find_slot_with_hash (&lookup,
555 17236096 : v->hash, INSERT);
556 17236096 : gcc_assert (!*slot);
557 17236096 : *slot = v;
558 17236096 : v->in_preserved_table_p = true;
559 17236096 : if (!v->all_locs_preserved_p)
560 0 : cselib_preserved_prune_list.safe_push (v);
561 : }
562 :
563 44058713 : cselib_hash_table->clear_slot (x);
564 :
565 44058713 : return 1;
566 : }
567 :
568 : /* Remove all entries from the hash table, arranging for the next
569 : value to be numbered NUM. */
570 :
571 : void
572 99120057 : cselib_reset_table (unsigned int num)
573 : {
574 99120057 : unsigned int i;
575 :
576 99120057 : max_value_regs = 0;
577 :
578 99120057 : if (cfa_base_preserved_val)
579 : {
580 4299803 : unsigned int regno = cfa_base_preserved_regno;
581 4299803 : unsigned int new_used_regs = 0;
582 30615399 : for (i = 0; i < n_used_regs; i++)
583 26315596 : if (used_regs[i] == regno)
584 : {
585 4299803 : new_used_regs = 1;
586 4299803 : continue;
587 : }
588 : else
589 22015793 : REG_VALUES (used_regs[i]) = 0;
590 4299803 : gcc_assert (new_used_regs == 1);
591 4299803 : n_used_regs = new_used_regs;
592 4299803 : used_regs[0] = regno;
593 4299803 : max_value_regs
594 4299803 : = hard_regno_nregs (regno,
595 4299803 : GET_MODE (cfa_base_preserved_val->locs->loc));
596 :
597 : /* If cfa_base is sp + const_int, need to preserve also the
598 : SP_DERIVED_VALUE_P value. */
599 4299803 : for (struct elt_loc_list *l = cfa_base_preserved_val->locs;
600 8599606 : l; l = l->next)
601 8599606 : if (GET_CODE (l->loc) == PLUS
602 4299803 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
603 4299803 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
604 12899409 : && CONST_INT_P (XEXP (l->loc, 1)))
605 : {
606 4299803 : if (! invariant_or_equiv_p (CSELIB_VAL_PTR (XEXP (l->loc, 0))))
607 : {
608 0 : rtx val = cfa_base_preserved_val->val_rtx;
609 0 : rtx_insn *save_cselib_current_insn = cselib_current_insn;
610 0 : cselib_current_insn = l->setting_insn;
611 0 : new_elt_loc_list (CSELIB_VAL_PTR (XEXP (l->loc, 0)),
612 0 : plus_constant (Pmode, val,
613 0 : -UINTVAL (XEXP (l->loc, 1))));
614 0 : cselib_current_insn = save_cselib_current_insn;
615 : }
616 : break;
617 : }
618 : }
619 : else
620 : {
621 364280601 : for (i = 0; i < n_used_regs; i++)
622 269460347 : REG_VALUES (used_regs[i]) = 0;
623 94820254 : n_used_regs = 0;
624 : }
625 :
626 99120057 : if (cselib_preserve_constants)
627 48358516 : cselib_hash_table->traverse <void *, preserve_constants_and_equivs> (NULL);
628 : else
629 : {
630 94820254 : cselib_hash_table->empty ();
631 94820254 : gcc_checking_assert (!cselib_any_perm_equivs);
632 : }
633 :
634 99120057 : n_useless_values = 0;
635 99120057 : n_useless_debug_values = 0;
636 99120057 : n_debug_values = 0;
637 :
638 99120057 : next_uid = num;
639 :
640 99120057 : first_containing_mem = &dummy_val;
641 99120057 : }
642 :
643 : /* Return the number of the next value that will be generated. */
644 :
645 : unsigned int
646 4792901 : cselib_get_next_uid (void)
647 : {
648 4792901 : return next_uid;
649 : }
650 :
651 : /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
652 : INSERTing if requested. When X is part of the address of a MEM,
653 : MEMMODE should specify the mode of the MEM. */
654 :
655 : static cselib_val **
656 708227336 : cselib_find_slot (machine_mode mode, rtx x, hashval_t hash,
657 : enum insert_option insert, machine_mode memmode)
658 : {
659 708227336 : cselib_val **slot = NULL;
660 708227336 : cselib_hasher::key lookup = { mode, x, memmode };
661 708227336 : hash &= cselib_val::HASH_MASK;
662 708227336 : if (cselib_preserve_constants)
663 158941510 : slot = cselib_preserved_hash_table->find_slot_with_hash (&lookup, hash,
664 : NO_INSERT);
665 158941510 : if (!slot)
666 654097433 : slot = cselib_hash_table->find_slot_with_hash (&lookup, hash, insert);
667 708227336 : return slot;
668 : }
669 :
670 : /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
671 : only return true for values which point to a cselib_val whose value
672 : element has been set to zero, which implies the cselib_val will be
673 : removed. */
674 :
675 : bool
676 2038988 : references_value_p (const_rtx x)
677 : {
678 2038988 : subrtx_iterator::array_type array;
679 4809953 : FOR_EACH_SUBRTX (iter, array, x, ALL)
680 2770965 : if (GET_CODE (*iter) == VALUE)
681 0 : return true;
682 2038988 : return false;
683 2038988 : }
684 :
685 : /* Return true if V is a useless VALUE and can be discarded as such. */
686 :
687 : static bool
688 705849322 : cselib_useless_value_p (cselib_val *v)
689 : {
690 705849322 : return (v->locs == 0
691 78595494 : && !PRESERVED_VALUE_P (v->val_rtx)
692 751462585 : && !SP_DERIVED_VALUE_P (v->val_rtx));
693 : }
694 :
695 : /* For all locations found in V, delete locations that reference useless
696 : values (i.e. values without any location). */
697 :
698 : static void
699 65809810 : discard_useless_locs (cselib_val *v)
700 : {
701 65809810 : struct elt_loc_list **p = &v->locs;
702 65809810 : bool had_locs = v->locs != NULL;
703 65809810 : rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
704 :
705 65809810 : if (v->all_locs_preserved_p)
706 : return;
707 :
708 : bool all_locs_preserved_p = true;
709 102688032 : while (*p)
710 : {
711 : /* True if every value referenced by (*p)->loc is preserved. */
712 45305513 : bool preserved_p = true;
713 45305513 : bool keep_p = true;
714 45305513 : subrtx_iterator::array_type array;
715 152132280 : FOR_EACH_SUBRTX (iter, array, (*p)->loc, ALL)
716 : {
717 108099697 : const_rtx x = *iter;
718 108099697 : if (GET_CODE (x) == VALUE && !PRESERVED_VALUE_P (x))
719 : {
720 4677239 : preserved_p = false;
721 4677239 : if (CSELIB_VAL_PTR (x)->locs == 0)
722 : {
723 : keep_p = false;
724 : break;
725 : }
726 : }
727 : }
728 45305513 : if (keep_p)
729 : {
730 44032583 : all_locs_preserved_p &= preserved_p;
731 44032583 : p = &(*p)->next;
732 : }
733 : else
734 1272930 : unchain_one_elt_loc_list (p);
735 45305513 : }
736 :
737 57382519 : if (all_locs_preserved_p)
738 54423833 : v->all_locs_preserved_p = true;
739 :
740 57382519 : if (had_locs && cselib_useless_value_p (v))
741 : {
742 1139287 : if (setting_insn && DEBUG_INSN_P (setting_insn))
743 2 : n_useless_debug_values++;
744 : else
745 1139285 : n_useless_values++;
746 1139287 : values_became_useless = 1;
747 : }
748 : }
749 :
750 : /* A hash-table traversal callback for the above. */
751 :
752 : int
753 58278920 : discard_useless_locs (cselib_val **x, void *info ATTRIBUTE_UNUSED)
754 : {
755 58278920 : discard_useless_locs (*x);
756 58278920 : return 1;
757 : }
758 :
759 : /* If X is a value with no locations, remove it from the hashtable. */
760 :
761 : int
762 47910890 : discard_useless_values (cselib_val **x, void *info ATTRIBUTE_UNUSED)
763 : {
764 47910890 : cselib_val *v = *x;
765 :
766 47910890 : if (v->locs == 0 && cselib_useless_value_p (v))
767 : {
768 2358656 : if (cselib_discard_hook)
769 523805 : cselib_discard_hook (v);
770 :
771 2358656 : CSELIB_VAL_PTR (v->val_rtx) = NULL;
772 2358656 : cselib_hash_table->clear_slot (x);
773 2358656 : unchain_one_value (v);
774 2358656 : n_useless_values--;
775 : }
776 :
777 47910890 : return 1;
778 : }
779 :
780 : /* Clean out useless values (i.e. those which no longer have locations
781 : associated with them) from the hash table. */
782 :
783 : static void
784 4328527 : remove_useless_values (void)
785 : {
786 4382134 : cselib_val **p, *v;
787 :
788 : /* First pass: eliminate locations that reference the value. That in
789 : turn can make more values useless. */
790 4382134 : do
791 : {
792 4382134 : values_became_useless = 0;
793 62661054 : cselib_hash_table->traverse <void *, discard_useless_locs> (NULL);
794 : }
795 4382134 : while (values_became_useless);
796 :
797 : /* Second pass: actually remove the values. */
798 :
799 4328527 : p = &first_containing_mem;
800 4450717 : for (v = *p; v != &dummy_val; v = v->next_containing_mem)
801 122190 : if (v->locs && v == canonical_cselib_val (v))
802 : {
803 118911 : *p = v;
804 118911 : p = &(*p)->next_containing_mem;
805 : }
806 4328527 : *p = &dummy_val;
807 :
808 4328527 : if (cselib_preserve_constants)
809 : {
810 : /* Apply discard_useless_locs to each element of
811 : cselib_preserved_prune_list. Remove from consideration any values
812 : whose locations only reference preserved values, since those
813 : locations will never be useless in their current form. */
814 4299803 : unsigned int len = cselib_preserved_prune_list.length ();
815 4299803 : unsigned int dest_i = 0;
816 4299803 : unsigned int src_i = 0;
817 11830693 : for (; src_i < len; ++src_i)
818 : {
819 7530890 : auto *val = cselib_preserved_prune_list[src_i];
820 7530890 : discard_useless_locs (val);
821 7530890 : if (!val->all_locs_preserved_p)
822 : {
823 0 : if (dest_i < src_i)
824 0 : cselib_preserved_prune_list[dest_i] = val;
825 0 : dest_i += 1;
826 : }
827 : }
828 4299803 : if (src_i != dest_i)
829 3575513 : cselib_preserved_prune_list.truncate (dest_i);
830 : }
831 4328527 : gcc_assert (!values_became_useless);
832 :
833 4328527 : n_useless_values += n_useless_debug_values;
834 4328527 : n_debug_values -= n_useless_debug_values;
835 4328527 : n_useless_debug_values = 0;
836 :
837 52239417 : cselib_hash_table->traverse <void *, discard_useless_values> (NULL);
838 :
839 4328527 : gcc_assert (!n_useless_values);
840 4328527 : }
841 :
842 : /* Arrange for a value to not be removed from the hash table even if
843 : it becomes useless. */
844 :
845 : void
846 44674199 : cselib_preserve_value (cselib_val *v)
847 : {
848 44674199 : PRESERVED_VALUE_P (v->val_rtx) = 1;
849 44674199 : }
850 :
851 : /* Test whether a value is preserved. */
852 :
853 : bool
854 259300472 : cselib_preserved_value_p (cselib_val *v)
855 : {
856 259300472 : return PRESERVED_VALUE_P (v->val_rtx);
857 : }
858 :
859 : /* Arrange for a REG value to be assumed constant through the whole function,
860 : never invalidated and preserved across cselib_reset_table calls. */
861 :
862 : void
863 985694 : cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
864 : {
865 985694 : if (cselib_preserve_constants
866 985694 : && v->locs
867 985694 : && REG_P (v->locs->loc))
868 : {
869 493393 : cfa_base_preserved_val = v;
870 493393 : cfa_base_preserved_regno = regno;
871 : }
872 985694 : }
873 :
874 : /* Clean all non-constant expressions in the hash table, but retain
875 : their values. */
876 :
877 : void
878 4299803 : cselib_preserve_only_values (void)
879 : {
880 4299803 : int i;
881 :
882 399881679 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
883 395581876 : cselib_invalidate_regno (i, reg_raw_mode[i]);
884 :
885 4299803 : cselib_invalidate_mem (callmem[0]);
886 :
887 4299803 : remove_useless_values ();
888 :
889 4299803 : gcc_assert (first_containing_mem == &dummy_val);
890 4299803 : }
891 :
892 : /* Arrange for a value to be marked as based on stack pointer
893 : for find_base_term purposes. */
894 :
895 : void
896 1912773 : cselib_set_value_sp_based (cselib_val *v)
897 : {
898 1912773 : SP_BASED_VALUE_P (v->val_rtx) = 1;
899 1912773 : }
900 :
901 : /* Test whether a value is based on stack pointer for
902 : find_base_term purposes. */
903 :
904 : bool
905 836043444 : cselib_sp_based_value_p (cselib_val *v)
906 : {
907 836043444 : return SP_BASED_VALUE_P (v->val_rtx);
908 : }
909 :
910 : /* Return the mode in which a register was last set. If X is not a
911 : register, return its mode. If the mode in which the register was
912 : set is not known, or the value was already clobbered, return
913 : VOIDmode. */
914 :
915 : machine_mode
916 111711394 : cselib_reg_set_mode (const_rtx x)
917 : {
918 111711394 : if (!REG_P (x))
919 33727505 : return GET_MODE (x);
920 :
921 77983889 : if (REG_VALUES (REGNO (x)) == NULL
922 77983889 : || REG_VALUES (REGNO (x))->elt == NULL)
923 : return VOIDmode;
924 :
925 23753182 : return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
926 : }
927 :
928 : /* If x is a PLUS or an autoinc operation, expand the operation,
929 : storing the offset, if any, in *OFF. */
930 :
931 : static rtx
932 1725056990 : autoinc_split (rtx x, rtx *off, machine_mode memmode)
933 : {
934 1725056990 : switch (GET_CODE (x))
935 : {
936 893224574 : case PLUS:
937 893224574 : *off = XEXP (x, 1);
938 893224574 : x = XEXP (x, 0);
939 893224574 : break;
940 :
941 13879938 : case PRE_DEC:
942 13879938 : if (memmode == VOIDmode)
943 : return x;
944 :
945 27759876 : *off = gen_int_mode (-GET_MODE_SIZE (memmode), GET_MODE (x));
946 13879938 : x = XEXP (x, 0);
947 13879938 : break;
948 :
949 0 : case PRE_INC:
950 0 : if (memmode == VOIDmode)
951 : return x;
952 :
953 0 : *off = gen_int_mode (GET_MODE_SIZE (memmode), GET_MODE (x));
954 0 : x = XEXP (x, 0);
955 0 : break;
956 :
957 1628063 : case PRE_MODIFY:
958 1628063 : x = XEXP (x, 1);
959 1628063 : break;
960 :
961 1841617 : case POST_DEC:
962 1841617 : case POST_INC:
963 1841617 : case POST_MODIFY:
964 1841617 : x = XEXP (x, 0);
965 1841617 : break;
966 :
967 : default:
968 : break;
969 : }
970 :
971 1725056990 : if (GET_MODE (x) == Pmode
972 1676275524 : && (REG_P (x) || MEM_P (x) || GET_CODE (x) == VALUE)
973 2622605069 : && (*off == NULL_RTX || CONST_INT_P (*off)))
974 : {
975 892911841 : cselib_val *e;
976 892911841 : if (GET_CODE (x) == VALUE)
977 600228910 : e = CSELIB_VAL_PTR (x);
978 : else
979 292682931 : e = cselib_lookup (x, GET_MODE (x), 0, memmode);
980 892911841 : if (e)
981 : {
982 883725596 : if (SP_DERIVED_VALUE_P (e->val_rtx)
983 883725596 : && (*off == NULL_RTX || *off == const0_rtx))
984 : {
985 63045 : *off = NULL_RTX;
986 63045 : return e->val_rtx;
987 : }
988 1907737288 : for (struct elt_loc_list *l = e->locs; l; l = l->next)
989 1536342384 : if (GET_CODE (l->loc) == PLUS
990 671899963 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
991 670701280 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
992 2048612321 : && CONST_INT_P (XEXP (l->loc, 1)))
993 : {
994 512267647 : if (*off == NULL_RTX)
995 1460484 : *off = XEXP (l->loc, 1);
996 : else
997 510807163 : *off = plus_constant (Pmode, *off,
998 510807163 : INTVAL (XEXP (l->loc, 1)));
999 512267647 : if (*off == const0_rtx)
1000 346767083 : *off = NULL_RTX;
1001 512267647 : return XEXP (l->loc, 0);
1002 : }
1003 : }
1004 : }
1005 : return x;
1006 : }
1007 :
1008 : /* Return true if we can prove that X and Y contain the same value,
1009 : taking our gathered information into account. MEMMODE holds the
1010 : mode of the enclosing MEM, if any, as required to deal with autoinc
1011 : addressing modes. If X and Y are not (known to be) part of
1012 : addresses, MEMMODE should be VOIDmode. */
1013 :
1014 : bool
1015 1288092337 : rtx_equal_for_cselib_1 (rtx x, rtx y, machine_mode memmode, int depth)
1016 : {
1017 1752664533 : enum rtx_code code;
1018 1752664533 : const char *fmt;
1019 1752664533 : int i;
1020 :
1021 1752664533 : if (REG_P (x) || MEM_P (x))
1022 : {
1023 149816867 : cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1024 :
1025 149816867 : if (e)
1026 126748648 : x = e->val_rtx;
1027 : }
1028 :
1029 1752664533 : if (REG_P (y) || MEM_P (y))
1030 : {
1031 141287557 : cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
1032 :
1033 141287557 : if (e)
1034 129428141 : y = e->val_rtx;
1035 : }
1036 :
1037 1752664533 : if (x == y)
1038 : return true;
1039 :
1040 1431430153 : if (GET_CODE (x) == VALUE)
1041 : {
1042 517094517 : cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
1043 517094517 : struct elt_loc_list *l;
1044 :
1045 517094517 : if (GET_CODE (y) == VALUE)
1046 31749946 : return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
1047 :
1048 485344571 : if ((SP_DERIVED_VALUE_P (x)
1049 148595355 : || SP_DERIVED_VALUE_P (e->val_rtx))
1050 525156705 : && GET_MODE (y) == Pmode)
1051 : {
1052 339377662 : rtx yoff = NULL;
1053 339377662 : rtx yr = autoinc_split (y, &yoff, memmode);
1054 339377662 : if ((yr == x || yr == e->val_rtx) && yoff == NULL_RTX)
1055 53372 : return true;
1056 : }
1057 :
1058 485291199 : if (depth == 128)
1059 : return false;
1060 :
1061 1591496483 : for (l = e->locs; l; l = l->next)
1062 : {
1063 1128967808 : rtx t = l->loc;
1064 :
1065 : /* Avoid infinite recursion. We know we have the canonical
1066 : value, so we can just skip any values in the equivalence
1067 : list. */
1068 1128967808 : if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
1069 699127640 : continue;
1070 429840168 : else if (rtx_equal_for_cselib_1 (t, y, memmode, depth + 1))
1071 : return true;
1072 : }
1073 :
1074 : return false;
1075 : }
1076 914335636 : else if (GET_CODE (y) == VALUE)
1077 : {
1078 46256575 : cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
1079 46256575 : struct elt_loc_list *l;
1080 :
1081 46256575 : if ((SP_DERIVED_VALUE_P (y)
1082 44047446 : || SP_DERIVED_VALUE_P (e->val_rtx))
1083 46658284 : && GET_MODE (x) == Pmode)
1084 : {
1085 2142462 : rtx xoff = NULL;
1086 2142462 : rtx xr = autoinc_split (x, &xoff, memmode);
1087 2142462 : if ((xr == y || xr == e->val_rtx) && xoff == NULL_RTX)
1088 163 : return true;
1089 : }
1090 :
1091 46256412 : if (depth == 128)
1092 : return false;
1093 :
1094 111190643 : for (l = e->locs; l; l = l->next)
1095 : {
1096 64987719 : rtx t = l->loc;
1097 :
1098 64987719 : if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
1099 55887819 : continue;
1100 9099900 : else if (rtx_equal_for_cselib_1 (x, t, memmode, depth + 1))
1101 : return true;
1102 : }
1103 :
1104 : return false;
1105 : }
1106 :
1107 868079061 : if (GET_MODE (x) != GET_MODE (y))
1108 : return false;
1109 :
1110 831133551 : if (GET_CODE (x) != GET_CODE (y)
1111 831133551 : || (GET_CODE (x) == PLUS
1112 338125991 : && GET_MODE (x) == Pmode
1113 241941117 : && CONST_INT_P (XEXP (x, 1))
1114 231251158 : && CONST_INT_P (XEXP (y, 1))))
1115 : {
1116 682946633 : rtx xorig = x, yorig = y;
1117 682946633 : rtx xoff = NULL, yoff = NULL;
1118 :
1119 682946633 : x = autoinc_split (x, &xoff, memmode);
1120 682946633 : y = autoinc_split (y, &yoff, memmode);
1121 :
1122 : /* Don't recurse if nothing changed. */
1123 682946633 : if (x != xorig || y != yorig)
1124 : {
1125 643943992 : if (!xoff != !yoff)
1126 219020448 : return false;
1127 :
1128 562244070 : if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode, depth))
1129 : return false;
1130 :
1131 463926185 : return rtx_equal_for_cselib_1 (x, y, memmode, depth);
1132 : }
1133 :
1134 39002641 : if (GET_CODE (xorig) != GET_CODE (yorig))
1135 : return false;
1136 : }
1137 :
1138 : /* These won't be handled correctly by the code below. */
1139 148186918 : switch (GET_CODE (x))
1140 : {
1141 : CASE_CONST_UNIQUE:
1142 : case DEBUG_EXPR:
1143 : return false;
1144 :
1145 : case CONST_VECTOR:
1146 : if (!same_vector_encodings_p (x, y))
1147 : return false;
1148 : break;
1149 :
1150 2156504 : case DEBUG_IMPLICIT_PTR:
1151 2156504 : return DEBUG_IMPLICIT_PTR_DECL (x)
1152 2156504 : == DEBUG_IMPLICIT_PTR_DECL (y);
1153 :
1154 5382 : case DEBUG_PARAMETER_REF:
1155 5382 : return DEBUG_PARAMETER_REF_DECL (x)
1156 5382 : == DEBUG_PARAMETER_REF_DECL (y);
1157 :
1158 617326 : case ENTRY_VALUE:
1159 : /* ENTRY_VALUEs are function invariant, it is thus undesirable to
1160 : use rtx_equal_for_cselib_1 to compare the operands. */
1161 617326 : return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1162 :
1163 119 : case LABEL_REF:
1164 119 : return label_ref_label (x) == label_ref_label (y);
1165 :
1166 195 : case REG:
1167 195 : return REGNO (x) == REGNO (y);
1168 :
1169 646011 : case MEM:
1170 : /* We have to compare any autoinc operations in the addresses
1171 : using this MEM's mode. */
1172 646011 : return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x),
1173 646011 : depth);
1174 :
1175 : default:
1176 : break;
1177 : }
1178 :
1179 39716022 : code = GET_CODE (x);
1180 39716022 : fmt = GET_RTX_FORMAT (code);
1181 :
1182 69401803 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1183 : {
1184 58370652 : int j;
1185 :
1186 58370652 : switch (fmt[i])
1187 : {
1188 0 : case 'w':
1189 0 : if (XWINT (x, i) != XWINT (y, i))
1190 : return false;
1191 : break;
1192 :
1193 578161 : case 'n':
1194 578161 : case 'i':
1195 578161 : if (XINT (x, i) != XINT (y, i))
1196 : return false;
1197 : break;
1198 :
1199 5323 : case 'L':
1200 5323 : if (XLOC (x, i) != XLOC (y, i))
1201 : return false;
1202 : break;
1203 :
1204 598154 : case 'p':
1205 598154 : if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
1206 : return false;
1207 : break;
1208 :
1209 1475283 : case 'V':
1210 1475283 : case 'E':
1211 : /* Two vectors must have the same length. */
1212 1475283 : if (XVECLEN (x, i) != XVECLEN (y, i))
1213 : return false;
1214 :
1215 : /* And the corresponding elements must match. */
1216 3628748 : for (j = 0; j < XVECLEN (x, i); j++)
1217 3017449 : if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
1218 3017449 : XVECEXP (y, i, j), memmode, depth))
1219 : return false;
1220 : break;
1221 :
1222 43400061 : case 'e':
1223 43400061 : if (i == 1
1224 27566678 : && targetm.commutative_p (x, UNKNOWN)
1225 20027463 : && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode,
1226 : depth)
1227 43577068 : && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode,
1228 : depth))
1229 : return true;
1230 43312499 : if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode,
1231 : depth))
1232 : return false;
1233 : break;
1234 :
1235 6231160 : case 'S':
1236 6231160 : case 's':
1237 6231160 : if (strcmp (XSTR (x, i), XSTR (y, i)))
1238 : return false;
1239 : break;
1240 :
1241 : case 'u':
1242 : /* These are just backpointers, so they don't matter. */
1243 : break;
1244 :
1245 : case '0':
1246 : case 't':
1247 : break;
1248 :
1249 : /* It is believed that rtx's at this level will never
1250 : contain anything but integers and other rtx's,
1251 : except for within LABEL_REFs and SYMBOL_REFs. */
1252 0 : default:
1253 0 : gcc_unreachable ();
1254 : }
1255 : }
1256 : return true;
1257 : }
1258 :
1259 : /* Wrapper for rtx_equal_for_cselib_p to determine whether a SET is
1260 : truly redundant, taking into account aliasing information. */
1261 : bool
1262 111711394 : cselib_redundant_set_p (rtx set)
1263 : {
1264 111711394 : gcc_assert (GET_CODE (set) == SET);
1265 111711394 : rtx dest = SET_DEST (set);
1266 111711394 : if (cselib_reg_set_mode (dest) != GET_MODE (dest))
1267 : return false;
1268 :
1269 53545903 : rtx src = SET_SRC (set);
1270 3986733 : if ((MEM_P (src) && MEM_VOLATILE_P (src))
1271 57463731 : || !rtx_equal_for_cselib_p (dest, src))
1272 53131728 : return false;
1273 :
1274 414179 : while (GET_CODE (dest) == SUBREG
1275 414175 : || GET_CODE (dest) == ZERO_EXTRACT
1276 828354 : || GET_CODE (dest) == STRICT_LOW_PART)
1277 4 : dest = XEXP (dest, 0);
1278 :
1279 414175 : if (!flag_strict_aliasing || !MEM_P (dest))
1280 : return true;
1281 :
1282 36747 : if (MEM_VOLATILE_P (dest))
1283 : return false;
1284 :
1285 : /* For a store we need to check that suppressing it will not change
1286 : the effective alias set. */
1287 36747 : rtx dest_addr = XEXP (dest, 0);
1288 :
1289 : /* Lookup the equivalents to the original dest (rather than just the
1290 : MEM). */
1291 73494 : cselib_val *src_val = cselib_lookup (SET_DEST (set),
1292 36747 : GET_MODE (SET_DEST (set)),
1293 : 0, VOIDmode);
1294 :
1295 36747 : if (src_val)
1296 : {
1297 : /* Walk the list of source equivalents to find the MEM accessing
1298 : the same location. */
1299 83525 : for (elt_loc_list *l = src_val->locs; l; l = l->next)
1300 : {
1301 83525 : rtx src_equiv = l->loc;
1302 83525 : while (GET_CODE (src_equiv) == SUBREG
1303 83525 : || GET_CODE (src_equiv) == ZERO_EXTRACT
1304 167056 : || GET_CODE (src_equiv) == STRICT_LOW_PART)
1305 6 : src_equiv = XEXP (src_equiv, 0);
1306 :
1307 83525 : if (MEM_P (src_equiv))
1308 : {
1309 : /* Match the MEMs by comparing the addresses. We can
1310 : only remove the later store if the earlier aliases at
1311 : least all the accesses of the later one. */
1312 44374 : if (rtx_equal_for_cselib_1 (dest_addr, XEXP (src_equiv, 0),
1313 44374 : GET_MODE (dest), 0))
1314 36741 : return mems_same_for_tbaa_p (src_equiv, dest);
1315 : }
1316 : }
1317 : }
1318 :
1319 : /* We failed to find a recorded value in the cselib history, so try
1320 : the source of this set; this catches cases such as *p = *q when p
1321 : and q have the same value. */
1322 6 : while (GET_CODE (src) == SUBREG)
1323 0 : src = XEXP (src, 0);
1324 :
1325 6 : if (MEM_P (src)
1326 6 : && rtx_equal_for_cselib_1 (dest_addr, XEXP (src, 0), GET_MODE (dest), 0))
1327 6 : return mems_same_for_tbaa_p (src, dest);
1328 :
1329 : return false;
1330 : }
1331 :
1332 : /* Helper function for cselib_hash_rtx. Arguments like for cselib_hash_rtx,
1333 : except that it hashes (plus:P x c). */
1334 :
1335 : static hashval_t
1336 281320974 : cselib_hash_plus_const_int (rtx x, HOST_WIDE_INT c, int create,
1337 : machine_mode memmode)
1338 : {
1339 281320974 : cselib_val *e = cselib_lookup (x, GET_MODE (x), create, memmode);
1340 281320974 : if (! e)
1341 : return 0;
1342 :
1343 268438696 : if (! SP_DERIVED_VALUE_P (e->val_rtx))
1344 434876818 : for (struct elt_loc_list *l = e->locs; l; l = l->next)
1345 351632795 : if (GET_CODE (l->loc) == PLUS
1346 129185516 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
1347 128455440 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
1348 473641522 : && CONST_INT_P (XEXP (l->loc, 1)))
1349 : {
1350 122005473 : e = CSELIB_VAL_PTR (XEXP (l->loc, 0));
1351 122005473 : c = trunc_int_for_mode (c + UINTVAL (XEXP (l->loc, 1)), Pmode);
1352 122005473 : break;
1353 : }
1354 268438696 : if (c == 0)
1355 8113657 : return e->hash;
1356 :
1357 260325039 : inchash::hash hash;
1358 260325039 : hash.add_int (PLUS);
1359 260325039 : hash.add_int (GET_MODE (x));
1360 260325039 : hash.merge_hash (e->hash);
1361 260325039 : hash.add_hwi (c);
1362 :
1363 260325039 : return hash.end () ? hash.end () : 1 + (unsigned int) PLUS;
1364 : }
1365 :
1366 : /* Hash an rtx. Return 0 if we couldn't hash the rtx.
1367 : For registers and memory locations, we look up their cselib_val structure
1368 : and return its VALUE element.
1369 : Possible reasons for return 0 are: the object is volatile, or we couldn't
1370 : find a register or memory location in the table and CREATE is zero. If
1371 : CREATE is nonzero, table elts are created for regs and mem.
1372 : N.B. this hash function returns the same hash value for RTXes that
1373 : differ only in the order of operands, thus it is suitable for comparisons
1374 : that take commutativity into account.
1375 : If we wanted to also support associative rules, we'd have to use a different
1376 : strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
1377 : MEMMODE indicates the mode of an enclosing MEM, and it's only
1378 : used to compute autoinc values.
1379 : We used to have a MODE argument for hashing for CONST_INTs, but that
1380 : didn't make sense, since it caused spurious hash differences between
1381 : (set (reg:SI 1) (const_int))
1382 : (plus:SI (reg:SI 2) (reg:SI 1))
1383 : and
1384 : (plus:SI (reg:SI 2) (const_int))
1385 : If the mode is important in any context, it must be checked specifically
1386 : in a comparison anyway, since relying on hash differences is unsafe. */
1387 :
1388 : static hashval_t
1389 979803481 : cselib_hash_rtx (rtx x, int create, machine_mode memmode)
1390 : {
1391 979803481 : cselib_val *e;
1392 979803481 : poly_int64 offset;
1393 979803481 : int i, j;
1394 979803481 : enum rtx_code code;
1395 979803481 : const char *fmt;
1396 979803481 : inchash::hash hash;
1397 :
1398 979803481 : code = GET_CODE (x);
1399 979803481 : hash.add_int (code);
1400 979803481 : hash.add_int (GET_MODE (x));
1401 :
1402 979803481 : switch (code)
1403 : {
1404 420379 : case VALUE:
1405 420379 : e = CSELIB_VAL_PTR (x);
1406 420379 : return e->hash;
1407 :
1408 214588898 : case MEM:
1409 214588898 : case REG:
1410 214588898 : e = cselib_lookup (x, GET_MODE (x), create, memmode);
1411 214588898 : if (! e)
1412 : return 0;
1413 :
1414 190465791 : return e->hash;
1415 :
1416 13274865 : case DEBUG_EXPR:
1417 13274865 : hash.add_int (DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x)));
1418 13274865 : return hash.end () ? hash.end() : (unsigned int) DEBUG_EXPR;
1419 :
1420 2776528 : case DEBUG_IMPLICIT_PTR:
1421 2776528 : hash.add_int (DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x)));
1422 2776528 : return hash.end () ? hash.end () : (unsigned int) DEBUG_IMPLICIT_PTR;
1423 :
1424 36830 : case DEBUG_PARAMETER_REF:
1425 36830 : hash.add_int (DECL_UID (DEBUG_PARAMETER_REF_DECL (x)));
1426 36830 : return hash.end () ? hash.end () : (unsigned int) DEBUG_PARAMETER_REF;
1427 :
1428 1362844 : case ENTRY_VALUE:
1429 : /* ENTRY_VALUEs are function invariant, thus try to avoid
1430 : recursing on argument if ENTRY_VALUE is one of the
1431 : forms emitted by expand_debug_expr, otherwise
1432 : ENTRY_VALUE hash would depend on the current value
1433 : in some register or memory. */
1434 1362844 : if (REG_P (ENTRY_VALUE_EXP (x)))
1435 1345795 : hash.add_int ((unsigned int) REG
1436 1345795 : + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1437 1345795 : + (unsigned int) REGNO (ENTRY_VALUE_EXP (x)));
1438 17049 : else if (MEM_P (ENTRY_VALUE_EXP (x))
1439 17049 : && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1440 17049 : hash.add_int ((unsigned int) MEM
1441 17049 : + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1442 17049 : + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0)));
1443 : else
1444 0 : hash.add_int (cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode));
1445 1362844 : return hash.end () ? hash.end () : (unsigned int) ENTRY_VALUE;
1446 :
1447 170446276 : case CONST_INT:
1448 170446276 : hash.add_hwi (UINTVAL (x));
1449 170446276 : return hash.end () ? hash.end () : (unsigned int) CONST_INT;
1450 :
1451 : case CONST_WIDE_INT:
1452 2920167 : for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1453 1948627 : hash.add_hwi (CONST_WIDE_INT_ELT (x, i));
1454 971540 : return hash.end () ? hash.end () : (unsigned int) CONST_WIDE_INT;
1455 :
1456 : case CONST_POLY_INT:
1457 : {
1458 0 : for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1459 0 : hash.add_wide_int (CONST_POLY_INT_COEFFS (x)[i]);
1460 0 : return hash.end () ? hash.end () : (unsigned int) CONST_POLY_INT;
1461 : }
1462 :
1463 3503045 : case CONST_DOUBLE:
1464 : /* This is like the general case, except that it only counts
1465 : the integers representing the constant. */
1466 3503045 : if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
1467 : {
1468 : hash.add_hwi (CONST_DOUBLE_LOW (x));
1469 : hash.add_hwi (CONST_DOUBLE_HIGH (x));
1470 : }
1471 : else
1472 3503045 : hash.merge_hash (real_hash (CONST_DOUBLE_REAL_VALUE (x)));
1473 3503045 : return hash.end () ? hash.end () : (unsigned int) CONST_DOUBLE;
1474 :
1475 0 : case CONST_FIXED:
1476 0 : hash.merge_hash (fixed_hash (CONST_FIXED_VALUE (x)));
1477 0 : return hash.end () ? hash.end () : (unsigned int) CONST_FIXED;
1478 :
1479 3121987 : case CONST_VECTOR:
1480 3121987 : {
1481 3121987 : int units;
1482 3121987 : rtx elt;
1483 :
1484 3121987 : units = const_vector_encoded_nelts (x);
1485 :
1486 8105875 : for (i = 0; i < units; ++i)
1487 : {
1488 4983888 : elt = CONST_VECTOR_ENCODED_ELT (x, i);
1489 4983888 : hash.merge_hash (cselib_hash_rtx (elt, 0, memmode));
1490 : }
1491 :
1492 3121987 : return hash.end () ? hash.end () : (unsigned int) CONST_VECTOR;
1493 : }
1494 :
1495 : /* Assume there is only one rtx object for any given label. */
1496 141772 : case LABEL_REF:
1497 : /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1498 : differences and differences between each stage's debugging dumps. */
1499 141772 : hash.add_int (CODE_LABEL_NUMBER (label_ref_label (x)));
1500 141772 : return hash.end () ? hash.end () : (unsigned int) LABEL_REF;
1501 :
1502 64297707 : case SYMBOL_REF:
1503 64297707 : {
1504 : /* Don't hash on the symbol's address to avoid bootstrap differences.
1505 : Different hash values may cause expressions to be recorded in
1506 : different orders and thus different registers to be used in the
1507 : final assembler. This also avoids differences in the dump files
1508 : between various stages. */
1509 64297707 : const char *p = (const char *) XSTR (x, 0);
1510 :
1511 64297707 : if (*p)
1512 64297707 : hash.add (p, strlen (p));
1513 :
1514 64297707 : return hash.end () ? hash.end () : (unsigned int) SYMBOL_REF;
1515 : }
1516 :
1517 17580104 : case PRE_DEC:
1518 17580104 : case PRE_INC:
1519 17580104 : {
1520 : /* We can't compute these without knowing the MEM mode. */
1521 17580104 : gcc_assert (memmode != VOIDmode);
1522 35160208 : offset = GET_MODE_SIZE (memmode);
1523 17580104 : if (code == PRE_DEC)
1524 17580104 : offset = -offset;
1525 : /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1526 : like (mem:MEMMODE (plus (reg) (const_int I))). */
1527 17580104 : if (GET_MODE (x) == Pmode
1528 17580104 : && (REG_P (XEXP (x, 0))
1529 : || MEM_P (XEXP (x, 0))
1530 : || GET_CODE (XEXP (x, 0)) == VALUE))
1531 : {
1532 17580104 : HOST_WIDE_INT c;
1533 17580104 : if (offset.is_constant (&c))
1534 17580104 : return cselib_hash_plus_const_int (XEXP (x, 0),
1535 : trunc_int_for_mode (c, Pmode),
1536 : create, memmode);
1537 : }
1538 :
1539 0 : hashval_t tem_hash = cselib_hash_rtx (XEXP (x, 0), create, memmode);
1540 0 : if (tem_hash == 0)
1541 : return 0;
1542 0 : hash.merge_hash (tem_hash);
1543 0 : tem_hash = cselib_hash_rtx (gen_int_mode (offset, GET_MODE (x)),
1544 : create, memmode);
1545 0 : if (tem_hash == 0)
1546 : return 0;
1547 0 : hash.merge_hash (tem_hash);
1548 0 : return hash.end () ? hash.end () : 1 + (unsigned) PLUS;
1549 : }
1550 :
1551 508859 : case PRE_MODIFY:
1552 508859 : {
1553 508859 : gcc_assert (memmode != VOIDmode);
1554 508859 : hashval_t tem_hash = cselib_hash_rtx (XEXP (x, 1), create, memmode);
1555 508859 : if (tem_hash == 0)
1556 : return 0;
1557 502295 : hash.merge_hash (tem_hash);
1558 502295 : return hash.end () ? hash.end () : 1 + (unsigned) PRE_MODIFY;
1559 : }
1560 :
1561 1779054 : case POST_DEC:
1562 1779054 : case POST_INC:
1563 1779054 : case POST_MODIFY:
1564 1779054 : {
1565 1779054 : gcc_assert (memmode != VOIDmode);
1566 1779054 : hashval_t tem_hash = cselib_hash_rtx (XEXP (x, 0), create, memmode);
1567 1779054 : if (tem_hash == 0)
1568 : return 0;
1569 1779054 : hash.merge_hash (tem_hash);
1570 1779054 : return hash.end () ? hash.end () : 1 + (unsigned) code;
1571 : }
1572 :
1573 : case PC:
1574 : case CALL:
1575 : case UNSPEC_VOLATILE:
1576 : return 0;
1577 :
1578 466936 : case ASM_OPERANDS:
1579 466936 : if (MEM_VOLATILE_P (x))
1580 : return 0;
1581 :
1582 : break;
1583 :
1584 317422779 : case PLUS:
1585 317422779 : if (GET_MODE (x) == Pmode
1586 306579369 : && (REG_P (XEXP (x, 0))
1587 : || MEM_P (XEXP (x, 0))
1588 : || GET_CODE (XEXP (x, 0)) == VALUE)
1589 599548851 : && CONST_INT_P (XEXP (x, 1)))
1590 263740870 : return cselib_hash_plus_const_int (XEXP (x, 0), INTVAL (XEXP (x, 1)),
1591 263740870 : create, memmode);
1592 : break;
1593 :
1594 : default:
1595 : break;
1596 : }
1597 :
1598 206302826 : i = GET_RTX_LENGTH (code) - 1;
1599 206302826 : fmt = GET_RTX_FORMAT (code);
1600 :
1601 206302826 : if (COMMUTATIVE_P (x))
1602 : {
1603 78358709 : gcc_assert (i == 1 && fmt[0] == 'e' && fmt[1] == 'e');
1604 78358709 : hashval_t tem1_hash = cselib_hash_rtx (XEXP (x, 1), create, memmode);
1605 78358709 : if (tem1_hash == 0)
1606 : return 0;
1607 72927790 : hashval_t tem0_hash = cselib_hash_rtx (XEXP (x, 0), create, memmode);
1608 72927790 : if (tem0_hash == 0)
1609 : return 0;
1610 69323951 : hash.add_commutative (tem0_hash, tem1_hash);
1611 69323951 : return hash.end () ? hash.end () : 1 + (unsigned int) GET_CODE (x);
1612 : }
1613 :
1614 336929842 : for (; i >= 0; i--)
1615 : {
1616 226916843 : switch (fmt[i])
1617 : {
1618 206138106 : case 'e':
1619 206138106 : {
1620 206138106 : rtx tem = XEXP (x, i);
1621 206138106 : hashval_t tem_hash = cselib_hash_rtx (tem, create, memmode);
1622 206138106 : if (tem_hash == 0)
1623 : return 0;
1624 189144169 : hash.merge_hash (tem_hash);
1625 : }
1626 189144169 : break;
1627 : case 'E':
1628 26569225 : for (j = 0; j < XVECLEN (x, i); j++)
1629 : {
1630 18653444 : hashval_t tem_hash
1631 18653444 : = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1632 18653444 : if (tem_hash == 0)
1633 : return 0;
1634 17716263 : hash.merge_hash (tem_hash);
1635 : }
1636 : break;
1637 :
1638 515663 : case 's':
1639 515663 : {
1640 515663 : const char *p = (const char *) XSTR (x, i);
1641 :
1642 515663 : if (p && *p)
1643 483207 : hash.add (p, strlen (p));
1644 : break;
1645 : }
1646 :
1647 5351535 : case 'i':
1648 5351535 : hash.add_hwi (XINT (x, i));
1649 5351535 : break;
1650 :
1651 244807 : case 'L':
1652 244807 : hash.add_hwi (XLOC (x, i));
1653 244807 : break;
1654 :
1655 5813770 : case 'p':
1656 5813770 : hash.add_int (constant_lower_bound (SUBREG_BYTE (x)));
1657 5813770 : break;
1658 :
1659 : case '0':
1660 : case 't':
1661 : /* unused */
1662 : break;
1663 :
1664 0 : default:
1665 0 : gcc_unreachable ();
1666 : }
1667 : }
1668 :
1669 110012999 : return hash.end () ? hash.end () : 1 + (unsigned int) GET_CODE (x);
1670 : }
1671 :
1672 : /* Create a new value structure for VALUE and initialize it. The mode of the
1673 : value is MODE. */
1674 :
1675 : static inline cselib_val *
1676 413877015 : new_cselib_val (hashval_t hash, machine_mode mode, rtx x)
1677 : {
1678 413877015 : cselib_val *e = cselib_val_pool.allocate ();
1679 :
1680 413877015 : gcc_assert (hash);
1681 413877015 : gcc_assert (next_uid);
1682 :
1683 413877015 : e->hash = hash;
1684 413877015 : e->in_preserved_table_p = false;
1685 413877015 : e->all_locs_preserved_p = false;
1686 413877015 : e->uid = next_uid++;
1687 : /* We use an alloc pool to allocate this RTL construct because it
1688 : accounts for about 8% of the overall memory usage. We know
1689 : precisely when we can have VALUE RTXen (when cselib is active)
1690 : so we don't need to put them in garbage collected memory.
1691 : ??? Why should a VALUE be an RTX in the first place? */
1692 413877015 : e->val_rtx = (rtx_def*) value_pool.allocate ();
1693 413877015 : memset (e->val_rtx, 0, RTX_HDR_SIZE);
1694 413877015 : PUT_CODE (e->val_rtx, VALUE);
1695 413877015 : PUT_MODE (e->val_rtx, mode);
1696 413877015 : CSELIB_VAL_PTR (e->val_rtx) = e;
1697 413877015 : e->addr_list = 0;
1698 413877015 : e->locs = 0;
1699 413877015 : e->next_containing_mem = 0;
1700 :
1701 413877015 : scalar_int_mode int_mode;
1702 539116542 : if (REG_P (x) && is_int_mode (mode, &int_mode)
1703 125239527 : && GET_MODE_SIZE (int_mode) > 1
1704 119257070 : && REG_VALUES (REGNO (x)) != NULL
1705 420661936 : && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
1706 : {
1707 6637477 : rtx copy = shallow_copy_rtx (x);
1708 6637477 : scalar_int_mode narrow_mode_iter;
1709 23832542 : FOR_EACH_MODE_UNTIL (narrow_mode_iter, int_mode)
1710 : {
1711 17195065 : PUT_MODE_RAW (copy, narrow_mode_iter);
1712 17195065 : cselib_val *v = cselib_lookup (copy, narrow_mode_iter, 0, VOIDmode);
1713 17195065 : if (v)
1714 : {
1715 754000 : rtx sub = lowpart_subreg (narrow_mode_iter, e->val_rtx, int_mode);
1716 754000 : if (sub)
1717 754000 : new_elt_loc_list (v, sub);
1718 : }
1719 : }
1720 : }
1721 :
1722 413877015 : if (dump_file && (dump_flags & TDF_CSELIB))
1723 : {
1724 0 : fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1725 0 : if (flag_dump_noaddr || flag_dump_unnumbered)
1726 0 : fputs ("# ", dump_file);
1727 : else
1728 0 : fprintf (dump_file, "%p ", (void*)e);
1729 0 : print_rtl_single (dump_file, x);
1730 0 : fputc ('\n', dump_file);
1731 : }
1732 :
1733 413877015 : return e;
1734 : }
1735 :
1736 : /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1737 : contains the data at this address. X is a MEM that represents the
1738 : value. Update the two value structures to represent this situation. */
1739 :
1740 : static void
1741 52296970 : add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1742 : {
1743 52296970 : addr_elt = canonical_cselib_val (addr_elt);
1744 52296970 : mem_elt = canonical_cselib_val (mem_elt);
1745 :
1746 : /* Avoid duplicates. */
1747 52296970 : addr_space_t as = MEM_ADDR_SPACE (x);
1748 94315995 : for (elt_loc_list *l = mem_elt->locs; l; l = l->next)
1749 42019025 : if (MEM_P (l->loc)
1750 12871601 : && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt
1751 42019025 : && MEM_ADDR_SPACE (l->loc) == as)
1752 : {
1753 0 : promote_debug_loc (l);
1754 0 : return;
1755 : }
1756 :
1757 52296970 : addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1758 52296970 : new_elt_loc_list (mem_elt,
1759 : replace_equiv_address_nv (x, addr_elt->val_rtx));
1760 52296970 : if (mem_elt->next_containing_mem == NULL)
1761 : {
1762 45783213 : mem_elt->next_containing_mem = first_containing_mem;
1763 45783213 : first_containing_mem = mem_elt;
1764 : }
1765 : }
1766 :
1767 : /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1768 : If CREATE, make a new one if we haven't seen it before. */
1769 :
1770 : static cselib_val *
1771 236732731 : cselib_lookup_mem (rtx x, int create)
1772 : {
1773 236732731 : machine_mode mode = GET_MODE (x);
1774 236732731 : machine_mode addr_mode;
1775 236732731 : cselib_val **slot;
1776 236732731 : cselib_val *addr;
1777 236732731 : cselib_val *mem_elt;
1778 :
1779 469719553 : if (MEM_VOLATILE_P (x) || mode == BLKmode
1780 232789663 : || !cselib_record_memory
1781 424147699 : || (FLOAT_MODE_P (mode) && flag_float_store))
1782 : return 0;
1783 :
1784 187402297 : addr_mode = GET_MODE (XEXP (x, 0));
1785 187402297 : if (addr_mode == VOIDmode)
1786 1299423 : addr_mode = Pmode;
1787 :
1788 : /* Look up the value for the address. */
1789 187402297 : addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1790 187402297 : if (! addr)
1791 : return 0;
1792 115372835 : addr = canonical_cselib_val (addr);
1793 :
1794 : /* Find a value that describes a value of our mode at that address. */
1795 115372835 : addr_space_t as = MEM_ADDR_SPACE (x);
1796 117803266 : for (elt_list *l = addr->addr_list; l; l = l->next)
1797 72868399 : if (GET_MODE (l->elt->val_rtx) == mode)
1798 : {
1799 76744333 : for (elt_loc_list *l2 = l->elt->locs; l2; l2 = l2->next)
1800 80766181 : if (MEM_P (l2->loc) && MEM_ADDR_SPACE (l2->loc) == as)
1801 : {
1802 70437968 : promote_debug_loc (l->elt->locs);
1803 70437968 : return l->elt;
1804 : }
1805 : }
1806 :
1807 44934867 : if (! create)
1808 : return 0;
1809 :
1810 28155045 : mem_elt = new_cselib_val (next_uid, mode, x);
1811 28155045 : add_mem_for_addr (addr, mem_elt, x);
1812 28155045 : slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
1813 28155045 : *slot = mem_elt;
1814 28155045 : return mem_elt;
1815 : }
1816 :
1817 : /* Search through the possible substitutions in P. We prefer a non reg
1818 : substitution because this allows us to expand the tree further. If
1819 : we find, just a reg, take the lowest regno. There may be several
1820 : non-reg results, we just take the first one because they will all
1821 : expand to the same place. */
1822 :
1823 : static rtx
1824 24245492 : expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1825 : int max_depth)
1826 : {
1827 24245492 : rtx reg_result = NULL;
1828 24245492 : unsigned int regno = UINT_MAX;
1829 24245492 : struct elt_loc_list *p_in = p;
1830 :
1831 52648277 : for (; p; p = p->next)
1832 : {
1833 : /* Return these right away to avoid returning stack pointer based
1834 : expressions for frame pointer and vice versa, which is something
1835 : that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1836 : for more details. */
1837 32324401 : if (REG_P (p->loc)
1838 32324401 : && (REGNO (p->loc) == STACK_POINTER_REGNUM
1839 : || REGNO (p->loc) == FRAME_POINTER_REGNUM
1840 : || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1841 25659160 : || REGNO (p->loc) == cfa_base_preserved_regno))
1842 : return p->loc;
1843 : /* Avoid infinite recursion trying to expand a reg into a
1844 : the same reg. */
1845 31816136 : if ((REG_P (p->loc))
1846 25653678 : && (REGNO (p->loc) < regno)
1847 57167088 : && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1848 : {
1849 4475159 : reg_result = p->loc;
1850 4475159 : regno = REGNO (p->loc);
1851 : }
1852 : /* Avoid infinite recursion and do not try to expand the
1853 : value. */
1854 27340977 : else if (GET_CODE (p->loc) == VALUE
1855 345156 : && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1856 0 : continue;
1857 27340977 : else if (!REG_P (p->loc))
1858 : {
1859 6162458 : rtx result, note;
1860 6162458 : if (dump_file && (dump_flags & TDF_CSELIB))
1861 : {
1862 0 : print_inline_rtx (dump_file, p->loc, 0);
1863 0 : fprintf (dump_file, "\n");
1864 : }
1865 6162458 : if (GET_CODE (p->loc) == LO_SUM
1866 0 : && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1867 0 : && p->setting_insn
1868 0 : && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1869 6162458 : && XEXP (note, 0) == XEXP (p->loc, 1))
1870 : return XEXP (p->loc, 1);
1871 6162458 : result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1872 6162458 : if (result)
1873 : return result;
1874 : }
1875 :
1876 : }
1877 :
1878 20323876 : if (regno != UINT_MAX)
1879 : {
1880 3716722 : rtx result;
1881 3716722 : if (dump_file && (dump_flags & TDF_CSELIB))
1882 0 : fprintf (dump_file, "r%d\n", regno);
1883 :
1884 3716722 : result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1885 3716722 : if (result)
1886 : return result;
1887 : }
1888 :
1889 17517229 : if (dump_file && (dump_flags & TDF_CSELIB))
1890 : {
1891 0 : if (reg_result)
1892 : {
1893 0 : print_inline_rtx (dump_file, reg_result, 0);
1894 0 : fprintf (dump_file, "\n");
1895 : }
1896 : else
1897 0 : fprintf (dump_file, "NULL\n");
1898 : }
1899 : return reg_result;
1900 : }
1901 :
1902 :
1903 : /* Forward substitute and expand an expression out to its roots.
1904 : This is the opposite of common subexpression. Because local value
1905 : numbering is such a weak optimization, the expanded expression is
1906 : pretty much unique (not from a pointer equals point of view but
1907 : from a tree shape point of view.
1908 :
1909 : This function returns NULL if the expansion fails. The expansion
1910 : will fail if there is no value number for one of the operands or if
1911 : one of the operands has been overwritten between the current insn
1912 : and the beginning of the basic block. For instance x has no
1913 : expansion in:
1914 :
1915 : r1 <- r1 + 3
1916 : x <- r1 + 8
1917 :
1918 : REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1919 : It is clear on return. */
1920 :
1921 : rtx
1922 37102636 : cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1923 : {
1924 37102636 : struct expand_value_data evd;
1925 :
1926 37102636 : evd.regs_active = regs_active;
1927 37102636 : evd.callback = NULL;
1928 37102636 : evd.callback_arg = NULL;
1929 37102636 : evd.dummy = false;
1930 :
1931 37102636 : return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1932 : }
1933 :
1934 : /* Same as cselib_expand_value_rtx, but using a callback to try to
1935 : resolve some expressions. The CB function should return ORIG if it
1936 : can't or does not want to deal with a certain RTX. Any other
1937 : return value, including NULL, will be used as the expansion for
1938 : VALUE, without any further changes. */
1939 :
1940 : rtx
1941 88019090 : cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1942 : cselib_expand_callback cb, void *data)
1943 : {
1944 88019090 : struct expand_value_data evd;
1945 :
1946 88019090 : evd.regs_active = regs_active;
1947 88019090 : evd.callback = cb;
1948 88019090 : evd.callback_arg = data;
1949 88019090 : evd.dummy = false;
1950 :
1951 88019090 : return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1952 : }
1953 :
1954 : /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1955 : or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1956 : would return NULL or non-NULL, without allocating new rtx. */
1957 :
1958 : bool
1959 0 : cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1960 : cselib_expand_callback cb, void *data)
1961 : {
1962 0 : struct expand_value_data evd;
1963 :
1964 0 : evd.regs_active = regs_active;
1965 0 : evd.callback = cb;
1966 0 : evd.callback_arg = data;
1967 0 : evd.dummy = true;
1968 :
1969 0 : return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1970 : }
1971 :
1972 : /* Internal implementation of cselib_expand_value_rtx and
1973 : cselib_expand_value_rtx_cb. */
1974 :
1975 : static rtx
1976 206299126 : cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1977 : int max_depth)
1978 : {
1979 206299126 : rtx copy, scopy;
1980 206299126 : int i, j;
1981 206299126 : RTX_CODE code;
1982 206299126 : const char *format_ptr;
1983 206299126 : machine_mode mode;
1984 :
1985 206299126 : code = GET_CODE (orig);
1986 :
1987 : /* For the context of dse, if we end up expand into a huge tree, we
1988 : will not have a useful address, so we might as well just give up
1989 : quickly. */
1990 206299126 : if (max_depth <= 0)
1991 : return NULL;
1992 :
1993 203537164 : switch (code)
1994 : {
1995 50668849 : case REG:
1996 50668849 : {
1997 50668849 : struct elt_list *l = REG_VALUES (REGNO (orig));
1998 :
1999 50668849 : if (l && l->elt == NULL)
2000 24990652 : l = l->next;
2001 50683851 : for (; l; l = l->next)
2002 38146367 : if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
2003 : {
2004 38131365 : rtx result;
2005 38131365 : unsigned regno = REGNO (orig);
2006 :
2007 : /* The only thing that we are not willing to do (this
2008 : is requirement of dse and if others potential uses
2009 : need this function we should add a parm to control
2010 : it) is that we will not substitute the
2011 : STACK_POINTER_REGNUM, FRAME_POINTER or the
2012 : HARD_FRAME_POINTER.
2013 :
2014 : These expansions confuses the code that notices that
2015 : stores into the frame go dead at the end of the
2016 : function and that the frame is not effected by calls
2017 : to subroutines. If you allow the
2018 : STACK_POINTER_REGNUM substitution, then dse will
2019 : think that parameter pushing also goes dead which is
2020 : wrong. If you allow the FRAME_POINTER or the
2021 : HARD_FRAME_POINTER then you lose the opportunity to
2022 : make the frame assumptions. */
2023 38131365 : if (regno == STACK_POINTER_REGNUM
2024 38131365 : || regno == FRAME_POINTER_REGNUM
2025 20553674 : || regno == HARD_FRAME_POINTER_REGNUM
2026 20046484 : || regno == cfa_base_preserved_regno)
2027 : return orig;
2028 :
2029 19744553 : bitmap_set_bit (evd->regs_active, regno);
2030 :
2031 19744553 : if (dump_file && (dump_flags & TDF_CSELIB))
2032 0 : fprintf (dump_file, "expanding: r%d into: ", regno);
2033 :
2034 19744553 : result = expand_loc (l->elt->locs, evd, max_depth);
2035 19744553 : bitmap_clear_bit (evd->regs_active, regno);
2036 :
2037 19744553 : if (result)
2038 : return result;
2039 : else
2040 : return orig;
2041 : }
2042 : return orig;
2043 : }
2044 :
2045 : CASE_CONST_ANY:
2046 : case SYMBOL_REF:
2047 : case CODE_LABEL:
2048 : case PC:
2049 : case SCRATCH:
2050 : /* SCRATCH must be shared because they represent distinct values. */
2051 : return orig;
2052 0 : case CLOBBER:
2053 0 : if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
2054 : return orig;
2055 : break;
2056 :
2057 276802 : case CONST:
2058 276802 : if (shared_const_p (orig))
2059 : return orig;
2060 : break;
2061 :
2062 921274 : case SUBREG:
2063 921274 : {
2064 921274 : rtx subreg;
2065 :
2066 921274 : if (evd->callback)
2067 : {
2068 828940 : subreg = evd->callback (orig, evd->regs_active, max_depth,
2069 : evd->callback_arg);
2070 828940 : if (subreg != orig)
2071 : return subreg;
2072 : }
2073 :
2074 92334 : subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
2075 : max_depth - 1);
2076 92334 : if (!subreg)
2077 : return NULL;
2078 101788 : scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
2079 50894 : GET_MODE (SUBREG_REG (orig)),
2080 50894 : SUBREG_BYTE (orig));
2081 50894 : if (scopy == NULL
2082 49812 : || (GET_CODE (scopy) == SUBREG
2083 45871 : && !REG_P (SUBREG_REG (scopy))
2084 5945 : && !MEM_P (SUBREG_REG (scopy))))
2085 : return NULL;
2086 :
2087 : return scopy;
2088 : }
2089 :
2090 70561922 : case VALUE:
2091 70561922 : {
2092 70561922 : rtx result;
2093 :
2094 70561922 : if (dump_file && (dump_flags & TDF_CSELIB))
2095 : {
2096 0 : fputs ("\nexpanding ", dump_file);
2097 0 : print_rtl_single (dump_file, orig);
2098 0 : fputs (" into...", dump_file);
2099 : }
2100 :
2101 70561922 : if (evd->callback)
2102 : {
2103 66060983 : result = evd->callback (orig, evd->regs_active, max_depth,
2104 : evd->callback_arg);
2105 :
2106 66060983 : if (result != orig)
2107 : return result;
2108 : }
2109 :
2110 4500939 : result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
2111 4500939 : return result;
2112 : }
2113 :
2114 6131841 : case DEBUG_EXPR:
2115 6131841 : if (evd->callback)
2116 6131841 : return evd->callback (orig, evd->regs_active, max_depth,
2117 6131841 : evd->callback_arg);
2118 : return orig;
2119 :
2120 : default:
2121 : break;
2122 : }
2123 :
2124 : /* Copy the various flags, fields, and other information. We assume
2125 : that all fields need copying, and then clear the fields that should
2126 : not be copied. That is the sensible default behavior, and forces
2127 : us to explicitly document why we are *not* copying a flag. */
2128 44991201 : if (evd->dummy)
2129 : copy = NULL;
2130 : else
2131 44991201 : copy = shallow_copy_rtx (orig);
2132 :
2133 44991201 : format_ptr = GET_RTX_FORMAT (code);
2134 :
2135 118241833 : for (i = 0; i < GET_RTX_LENGTH (code); i++)
2136 77431594 : switch (*format_ptr++)
2137 : {
2138 70729997 : case 'e':
2139 70729997 : if (XEXP (orig, i) != NULL)
2140 : {
2141 70729997 : rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
2142 : max_depth - 1);
2143 70729997 : if (!result)
2144 : return NULL;
2145 66582834 : if (copy)
2146 66582834 : XEXP (copy, i) = result;
2147 : }
2148 : break;
2149 :
2150 297838 : case 'E':
2151 297838 : case 'V':
2152 297838 : if (XVEC (orig, i) != NULL)
2153 : {
2154 297838 : if (copy)
2155 297838 : XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
2156 739928 : for (j = 0; j < XVECLEN (orig, i); j++)
2157 : {
2158 475889 : rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
2159 : evd, max_depth - 1);
2160 475889 : if (!result)
2161 : return NULL;
2162 442090 : if (copy)
2163 442090 : XVECEXP (copy, i, j) = result;
2164 : }
2165 : }
2166 : break;
2167 :
2168 : case 't':
2169 : case 'w':
2170 : case 'i':
2171 : case 'L':
2172 : case 's':
2173 : case 'S':
2174 : case 'T':
2175 : case 'u':
2176 : case 'B':
2177 : case '0':
2178 : /* These are left unchanged. */
2179 : break;
2180 :
2181 0 : default:
2182 0 : gcc_unreachable ();
2183 : }
2184 :
2185 40810239 : if (evd->dummy)
2186 : return orig;
2187 :
2188 40810239 : mode = GET_MODE (copy);
2189 : /* If an operand has been simplified into CONST_INT, which doesn't
2190 : have a mode and the mode isn't derivable from whole rtx's mode,
2191 : try simplify_*_operation first with mode from original's operand
2192 : and as a fallback wrap CONST_INT into gen_rtx_CONST. */
2193 40810239 : scopy = copy;
2194 40810239 : switch (GET_RTX_CLASS (code))
2195 : {
2196 725197 : case RTX_UNARY:
2197 725197 : if (CONST_INT_P (XEXP (copy, 0))
2198 50377 : && GET_MODE (XEXP (orig, 0)) != VOIDmode)
2199 : {
2200 50369 : scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
2201 : GET_MODE (XEXP (orig, 0)));
2202 50369 : if (scopy)
2203 : return scopy;
2204 : }
2205 : break;
2206 : case RTX_COMM_ARITH:
2207 : case RTX_BIN_ARITH:
2208 : /* These expressions can derive operand modes from the whole rtx's mode. */
2209 : break;
2210 91352 : case RTX_TERNARY:
2211 91352 : case RTX_BITFIELD_OPS:
2212 91352 : if (CONST_INT_P (XEXP (copy, 0))
2213 77 : && GET_MODE (XEXP (orig, 0)) != VOIDmode)
2214 : {
2215 1 : scopy = simplify_ternary_operation (code, mode,
2216 : GET_MODE (XEXP (orig, 0)),
2217 : XEXP (copy, 0), XEXP (copy, 1),
2218 : XEXP (copy, 2));
2219 1 : if (scopy)
2220 : return scopy;
2221 : }
2222 : break;
2223 198563 : case RTX_COMPARE:
2224 198563 : case RTX_COMM_COMPARE:
2225 198563 : if (CONST_INT_P (XEXP (copy, 0))
2226 456 : && GET_MODE (XEXP (copy, 1)) == VOIDmode
2227 172 : && (GET_MODE (XEXP (orig, 0)) != VOIDmode
2228 6 : || GET_MODE (XEXP (orig, 1)) != VOIDmode))
2229 : {
2230 172 : scopy = simplify_relational_operation (code, mode,
2231 : (GET_MODE (XEXP (orig, 0))
2232 : != VOIDmode)
2233 : ? GET_MODE (XEXP (orig, 0))
2234 6 : : GET_MODE (XEXP (orig, 1)),
2235 : XEXP (copy, 0),
2236 : XEXP (copy, 1));
2237 172 : if (scopy)
2238 : return scopy;
2239 : }
2240 : break;
2241 : default:
2242 : break;
2243 : }
2244 40759698 : scopy = simplify_rtx (copy);
2245 40759698 : if (scopy)
2246 4889745 : return scopy;
2247 : return copy;
2248 : }
2249 :
2250 : /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2251 : with VALUE expressions. This way, it becomes independent of changes
2252 : to registers and memory.
2253 : X isn't actually modified; if modifications are needed, new rtl is
2254 : allocated. However, the return value can share rtl with X.
2255 : If X is within a MEM, MEMMODE must be the mode of the MEM. */
2256 :
2257 : rtx
2258 584171849 : cselib_subst_to_values (rtx x, machine_mode memmode)
2259 : {
2260 591878512 : enum rtx_code code = GET_CODE (x);
2261 591878512 : const char *fmt = GET_RTX_FORMAT (code);
2262 591878512 : cselib_val *e;
2263 591878512 : struct elt_list *l;
2264 591878512 : rtx copy = x;
2265 591878512 : int i;
2266 591878512 : poly_int64 offset;
2267 :
2268 591878512 : switch (code)
2269 : {
2270 231538939 : case REG:
2271 231538939 : l = REG_VALUES (REGNO (x));
2272 231538939 : if (l && l->elt == NULL)
2273 132689158 : l = l->next;
2274 233811889 : for (; l; l = l->next)
2275 233811889 : if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
2276 : return l->elt->val_rtx;
2277 :
2278 0 : gcc_unreachable ();
2279 :
2280 6095125 : case MEM:
2281 6095125 : e = cselib_lookup_mem (x, 0);
2282 : /* This used to happen for autoincrements, but we deal with them
2283 : properly now. Remove the if stmt for the next release. */
2284 6095125 : if (! e)
2285 : {
2286 : /* Assign a value that doesn't match any other. */
2287 0 : e = new_cselib_val (next_uid, GET_MODE (x), x);
2288 : }
2289 6095125 : return e->val_rtx;
2290 :
2291 664268 : case ENTRY_VALUE:
2292 664268 : e = cselib_lookup (x, GET_MODE (x), 0, memmode);
2293 664268 : if (! e)
2294 : break;
2295 934 : return e->val_rtx;
2296 :
2297 : CASE_CONST_ANY:
2298 : return x;
2299 :
2300 6376911 : case PRE_DEC:
2301 6376911 : case PRE_INC:
2302 6376911 : gcc_assert (memmode != VOIDmode);
2303 12753822 : offset = GET_MODE_SIZE (memmode);
2304 6376911 : if (code == PRE_DEC)
2305 6376911 : offset = -offset;
2306 6376911 : return cselib_subst_to_values (plus_constant (GET_MODE (x),
2307 : XEXP (x, 0), offset),
2308 6376911 : memmode);
2309 :
2310 381907 : case PRE_MODIFY:
2311 381907 : gcc_assert (memmode != VOIDmode);
2312 381907 : return cselib_subst_to_values (XEXP (x, 1), memmode);
2313 :
2314 947845 : case POST_DEC:
2315 947845 : case POST_INC:
2316 947845 : case POST_MODIFY:
2317 947845 : gcc_assert (memmode != VOIDmode);
2318 947845 : return cselib_subst_to_values (XEXP (x, 0), memmode);
2319 :
2320 115136196 : case PLUS:
2321 150588520 : if (GET_MODE (x) == Pmode && CONST_INT_P (XEXP (x, 1)))
2322 : {
2323 93469767 : rtx t = cselib_subst_to_values (XEXP (x, 0), memmode);
2324 93469767 : if (GET_CODE (t) == VALUE)
2325 : {
2326 87710686 : if (SP_DERIVED_VALUE_P (t) && XEXP (x, 1) == const0_rtx)
2327 : return t;
2328 87710686 : for (struct elt_loc_list *l = CSELIB_VAL_PTR (t)->locs;
2329 185161782 : l; l = l->next)
2330 118855412 : if (GET_CODE (l->loc) == PLUS
2331 25069675 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
2332 24863243 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2333 140261194 : && CONST_INT_P (XEXP (l->loc, 1)))
2334 35268772 : return plus_constant (Pmode, l->loc, INTVAL (XEXP (x, 1)));
2335 : }
2336 72065451 : if (t != XEXP (x, 0))
2337 : {
2338 67810535 : copy = shallow_copy_rtx (x);
2339 67810535 : XEXP (copy, 0) = t;
2340 : }
2341 72065451 : return copy;
2342 : }
2343 :
2344 : default:
2345 : break;
2346 : }
2347 :
2348 472764385 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2349 : {
2350 308711538 : if (fmt[i] == 'e')
2351 : {
2352 227059914 : rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
2353 :
2354 227059914 : if (t != XEXP (x, i))
2355 : {
2356 166346739 : if (x == copy)
2357 117901088 : copy = shallow_copy_rtx (x);
2358 166346739 : XEXP (copy, i) = t;
2359 : }
2360 : }
2361 81651624 : else if (fmt[i] == 'E')
2362 : {
2363 : int j;
2364 :
2365 19107914 : for (j = 0; j < XVECLEN (x, i); j++)
2366 : {
2367 13491877 : rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
2368 :
2369 13491877 : if (t != XVECEXP (x, i, j))
2370 : {
2371 2581709 : if (XVEC (x, i) == XVEC (copy, i))
2372 : {
2373 1928335 : if (x == copy)
2374 1928335 : copy = shallow_copy_rtx (x);
2375 1928335 : XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
2376 : }
2377 2581709 : XVECEXP (copy, i, j) = t;
2378 : }
2379 : }
2380 : }
2381 : }
2382 :
2383 : return copy;
2384 : }
2385 :
2386 : /* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
2387 :
2388 : rtx
2389 1463 : cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
2390 : {
2391 1463 : rtx ret;
2392 1463 : gcc_assert (!cselib_current_insn);
2393 1463 : cselib_current_insn = insn;
2394 1463 : ret = cselib_subst_to_values (x, memmode);
2395 1463 : cselib_current_insn = NULL;
2396 1463 : return ret;
2397 : }
2398 :
2399 : /* Look up the rtl expression X in our tables and return the value it
2400 : has. If CREATE is zero, we return NULL if we don't know the value.
2401 : Otherwise, we create a new one if possible, using mode MODE if X
2402 : doesn't have a mode (i.e. because it's a constant). When X is part
2403 : of an address, MEMMODE should be the mode of the enclosing MEM if
2404 : we're tracking autoinc expressions. */
2405 :
2406 : static cselib_val *
2407 2396696230 : cselib_lookup_1 (rtx x, machine_mode mode,
2408 : int create, machine_mode memmode)
2409 : {
2410 2396696230 : cselib_val **slot;
2411 2396696230 : cselib_val *e;
2412 :
2413 2396696230 : if (GET_MODE (x) != VOIDmode)
2414 2313951989 : mode = GET_MODE (x);
2415 :
2416 2396696230 : if (GET_CODE (x) == VALUE)
2417 64229224 : return CSELIB_VAL_PTR (x);
2418 :
2419 2332467006 : if (REG_P (x))
2420 : {
2421 1505375769 : struct elt_list *l;
2422 1505375769 : unsigned int i = REGNO (x);
2423 :
2424 1505375769 : l = REG_VALUES (i);
2425 1505375769 : if (l && l->elt == NULL)
2426 627320486 : l = l->next;
2427 1525723845 : for (; l; l = l->next)
2428 1166917182 : if (mode == GET_MODE (l->elt->val_rtx))
2429 : {
2430 1146569106 : promote_debug_loc (l->elt->locs);
2431 1146569106 : return l->elt;
2432 : }
2433 :
2434 358806663 : if (! create)
2435 : return 0;
2436 :
2437 135573142 : if (i < FIRST_PSEUDO_REGISTER)
2438 : {
2439 77231666 : unsigned int n = hard_regno_nregs (i, mode);
2440 :
2441 77231666 : if (n > max_value_regs)
2442 26647751 : max_value_regs = n;
2443 : }
2444 :
2445 135573142 : e = new_cselib_val (next_uid, GET_MODE (x), x);
2446 160090423 : if (GET_MODE (x) == Pmode && x == stack_pointer_rtx)
2447 11920171 : SP_DERIVED_VALUE_P (e->val_rtx) = 1;
2448 135573142 : new_elt_loc_list (e, x);
2449 :
2450 135573142 : scalar_int_mode int_mode;
2451 135573142 : if (REG_VALUES (i) == 0)
2452 : {
2453 : /* Maintain the invariant that the first entry of
2454 : REG_VALUES, if present, must be the value used to set the
2455 : register, or NULL. */
2456 125701185 : used_regs[n_used_regs++] = i;
2457 125701185 : REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2458 : }
2459 9871957 : else if (cselib_preserve_constants
2460 9871957 : && is_int_mode (mode, &int_mode))
2461 : {
2462 : /* During var-tracking, try harder to find equivalences
2463 : for SUBREGs. If a setter sets say a DImode register
2464 : and user uses that register only in SImode, add a lowpart
2465 : subreg location. */
2466 1531930 : struct elt_list *lwider = NULL;
2467 1531930 : scalar_int_mode lmode;
2468 1531930 : l = REG_VALUES (i);
2469 1531930 : if (l && l->elt == NULL)
2470 987044 : l = l->next;
2471 2311560 : for (; l; l = l->next)
2472 779630 : if (is_int_mode (GET_MODE (l->elt->val_rtx), &lmode)
2473 1095780 : && GET_MODE_SIZE (lmode) > GET_MODE_SIZE (int_mode)
2474 265909 : && (lwider == NULL
2475 4256 : || partial_subreg_p (lmode,
2476 4256 : GET_MODE (lwider->elt->val_rtx))))
2477 : {
2478 265794 : struct elt_loc_list *el;
2479 282215 : if (i < FIRST_PSEUDO_REGISTER
2480 265794 : && hard_regno_nregs (i, lmode) != 1)
2481 16421 : continue;
2482 488667 : for (el = l->elt->locs; el; el = el->next)
2483 456717 : if (!REG_P (el->loc))
2484 : break;
2485 249373 : if (el)
2486 779630 : lwider = l;
2487 : }
2488 1531930 : if (lwider)
2489 : {
2490 426566 : rtx sub = lowpart_subreg (int_mode, lwider->elt->val_rtx,
2491 213283 : GET_MODE (lwider->elt->val_rtx));
2492 213283 : if (sub)
2493 213283 : new_elt_loc_list (e, sub);
2494 : }
2495 : }
2496 135573142 : REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
2497 135573142 : slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
2498 135573142 : *slot = e;
2499 135573142 : return e;
2500 : }
2501 :
2502 827091237 : if (MEM_P (x))
2503 230637606 : return cselib_lookup_mem (x, create);
2504 :
2505 596453631 : hashval_t hashval = cselib_hash_rtx (x, create, memmode);
2506 : /* Can't even create if hashing is not possible. */
2507 596453631 : if (! hashval)
2508 : return 0;
2509 :
2510 765322831 : slot = cselib_find_slot (mode, x, hashval,
2511 : create ? INSERT : NO_INSERT, memmode);
2512 544499149 : if (slot == 0)
2513 : return 0;
2514 :
2515 433905945 : e = (cselib_val *) *slot;
2516 433905945 : if (e)
2517 : return e;
2518 :
2519 250148828 : e = new_cselib_val (hashval, mode, x);
2520 :
2521 : /* We have to fill the slot before calling cselib_subst_to_values:
2522 : the hash table is inconsistent until we do so, and
2523 : cselib_subst_to_values will need to do lookups. */
2524 250148828 : *slot = e;
2525 250148828 : rtx v = cselib_subst_to_values (x, memmode);
2526 :
2527 : /* If cselib_preserve_constants, we might get a SP_DERIVED_VALUE_P
2528 : VALUE that isn't in the hash tables anymore. */
2529 250148828 : if (GET_CODE (v) == VALUE && SP_DERIVED_VALUE_P (v) && PRESERVED_VALUE_P (v))
2530 0 : PRESERVED_VALUE_P (e->val_rtx) = 1;
2531 :
2532 250148828 : new_elt_loc_list (e, v);
2533 250148828 : return e;
2534 : }
2535 :
2536 : /* Wrapper for cselib_lookup, that indicates X is in INSN. */
2537 :
2538 : cselib_val *
2539 6156439 : cselib_lookup_from_insn (rtx x, machine_mode mode,
2540 : int create, machine_mode memmode, rtx_insn *insn)
2541 : {
2542 6156439 : cselib_val *ret;
2543 :
2544 6156439 : gcc_assert (!cselib_current_insn);
2545 6156439 : cselib_current_insn = insn;
2546 :
2547 6156439 : ret = cselib_lookup (x, mode, create, memmode);
2548 :
2549 6156439 : cselib_current_insn = NULL;
2550 :
2551 6156439 : return ret;
2552 : }
2553 :
2554 : /* Wrapper for cselib_lookup_1, that logs the lookup result and
2555 : maintains invariants related with debug insns. */
2556 :
2557 : cselib_val *
2558 2395643078 : cselib_lookup (rtx x, machine_mode mode,
2559 : int create, machine_mode memmode)
2560 : {
2561 2395643078 : cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2562 :
2563 : /* ??? Should we return NULL if we're not to create an entry, the
2564 : found loc is a debug loc and cselib_current_insn is not DEBUG?
2565 : If so, we should also avoid converting val to non-DEBUG; probably
2566 : easiest setting cselib_current_insn to NULL before the call
2567 : above. */
2568 :
2569 2395643078 : if (dump_file && (dump_flags & TDF_CSELIB))
2570 : {
2571 0 : fputs ("cselib lookup ", dump_file);
2572 0 : print_inline_rtx (dump_file, x, 2);
2573 0 : fprintf (dump_file, " => %u:%u\n",
2574 : ret ? ret->uid : 0,
2575 0 : ret ? ret->hash : 0);
2576 : }
2577 :
2578 2395643078 : return ret;
2579 : }
2580 :
2581 : /* Invalidate the value at *L, which is part of REG_VALUES (REGNO). */
2582 :
2583 : static void
2584 182449162 : cselib_invalidate_regno_val (unsigned int regno, struct elt_list **l)
2585 : {
2586 182449162 : cselib_val *v = (*l)->elt;
2587 182449162 : if (*l == REG_VALUES (regno))
2588 : {
2589 : /* Maintain the invariant that the first entry of
2590 : REG_VALUES, if present, must be the value used to set
2591 : the register, or NULL. This is also nice because
2592 : then we won't push the same regno onto user_regs
2593 : multiple times. */
2594 140164560 : (*l)->elt = NULL;
2595 140164560 : l = &(*l)->next;
2596 : }
2597 : else
2598 42284602 : unchain_one_elt_list (l);
2599 :
2600 182449162 : v = canonical_cselib_val (v);
2601 :
2602 182449162 : bool had_locs = v->locs != NULL;
2603 182449162 : rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2604 :
2605 : /* Now, we clear the mapping from value to reg. It must exist, so
2606 : this code will crash intentionally if it doesn't. */
2607 182449162 : for (elt_loc_list **p = &v->locs; ; p = &(*p)->next)
2608 : {
2609 210206114 : rtx x = (*p)->loc;
2610 :
2611 210206114 : if (REG_P (x) && REGNO (x) == regno)
2612 : {
2613 182449162 : unchain_one_elt_loc_list (p);
2614 182449162 : break;
2615 : }
2616 27756952 : }
2617 :
2618 182449162 : if (had_locs && cselib_useless_value_p (v))
2619 : {
2620 21317116 : if (setting_insn && DEBUG_INSN_P (setting_insn))
2621 0 : n_useless_debug_values++;
2622 : else
2623 21317116 : n_useless_values++;
2624 : }
2625 182449162 : }
2626 :
2627 : /* Invalidate any entries in reg_values that overlap REGNO. This is called
2628 : if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2629 : is used to determine how many hard registers are being changed. If MODE
2630 : is VOIDmode, then only REGNO is being changed; this is used when
2631 : invalidating call clobbered registers across a call. */
2632 :
2633 : static void
2634 790184495 : cselib_invalidate_regno (unsigned int regno, machine_mode mode)
2635 : {
2636 790184495 : unsigned int endregno;
2637 790184495 : unsigned int i;
2638 :
2639 : /* If we see pseudos after reload, something is _wrong_. */
2640 790184495 : gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2641 : || reg_renumber[regno] < 0);
2642 :
2643 : /* Determine the range of registers that must be invalidated. For
2644 : pseudos, only REGNO is affected. For hard regs, we must take MODE
2645 : into account, and we must also invalidate lower register numbers
2646 : if they contain values that overlap REGNO. */
2647 219626303 : if (regno < FIRST_PSEUDO_REGISTER)
2648 : {
2649 687155914 : gcc_assert (mode != VOIDmode);
2650 :
2651 687155914 : if (regno < max_value_regs)
2652 : i = 0;
2653 : else
2654 646421000 : i = regno - max_value_regs;
2655 :
2656 687155914 : endregno = end_hard_regno (mode, regno);
2657 : }
2658 : else
2659 : {
2660 103028581 : i = regno;
2661 103028581 : endregno = regno + 1;
2662 : }
2663 :
2664 2189817328 : for (; i < endregno; i++)
2665 : {
2666 1399632833 : struct elt_list **l = ®_VALUES (i);
2667 :
2668 : /* Go through all known values for this reg; if it overlaps the range
2669 : we're invalidating, remove the value. */
2670 1781708378 : while (*l)
2671 : {
2672 382075545 : cselib_val *v = (*l)->elt;
2673 382075545 : unsigned int this_last = i;
2674 :
2675 382075545 : if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2676 166199748 : this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2677 :
2678 382075545 : if (this_last < regno || v == NULL
2679 117385517 : || (v == cfa_base_preserved_val
2680 4301441 : && i == cfa_base_preserved_regno))
2681 : {
2682 268989831 : l = &(*l)->next;
2683 268989831 : continue;
2684 : }
2685 :
2686 : /* We have an overlap. */
2687 113085714 : cselib_invalidate_regno_val (i, l);
2688 : }
2689 : }
2690 790184495 : }
2691 :
2692 : /* Invalidate any locations in the table which are changed because of a
2693 : store to MEM_RTX. If this is called because of a non-const call
2694 : instruction, MEM_RTX is (mem:BLK const0_rtx). */
2695 :
2696 : static void
2697 113486542 : cselib_invalidate_mem (rtx mem_rtx)
2698 : {
2699 113486542 : cselib_val **vp, *v, *next;
2700 113486542 : int num_mems = 0;
2701 113486542 : rtx mem_addr;
2702 :
2703 113486542 : mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2704 113486542 : mem_rtx = canon_rtx (mem_rtx);
2705 :
2706 113486542 : vp = &first_containing_mem;
2707 283846151 : for (v = *vp; v != &dummy_val; v = next)
2708 : {
2709 170359609 : bool has_mem = false;
2710 170359609 : struct elt_loc_list **p = &v->locs;
2711 170359609 : bool had_locs = v->locs != NULL;
2712 170359609 : rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2713 170359609 : rtx sp_base = NULL_RTX;
2714 170359609 : HOST_WIDE_INT sp_off = 0;
2715 :
2716 507711512 : while (*p)
2717 : {
2718 337351903 : rtx x = (*p)->loc;
2719 337351903 : cselib_val *addr;
2720 337351903 : struct elt_list **mem_chain;
2721 :
2722 : /* MEMs may occur in locations only at the top level; below
2723 : that every MEM or REG is substituted by its VALUE. */
2724 337351903 : if (!MEM_P (x))
2725 : {
2726 102132574 : p = &(*p)->next;
2727 102132574 : continue;
2728 : }
2729 :
2730 : /* When invalidating memory below the stack pointer for const/pure
2731 : calls and alloca/VLAs aren't used, attempt to optimize. Values
2732 : stored into area sometimes below the stack pointer shouldn't be
2733 : addressable and should be stored just through stack pointer
2734 : derived expressions, so don't invalidate MEMs not using stack
2735 : derived addresses, or if the MEMs clearly aren't below the stack
2736 : pointer. This isn't a fully conservative approach, the hope is
2737 : that invalidating more MEMs than this isn't actually needed. */
2738 235219329 : if (mem_rtx == callmem[1]
2739 2915000 : && num_mems < param_max_cselib_memory_locations
2740 2914942 : && GET_CODE (XEXP (x, 0)) == VALUE
2741 2914942 : && !cfun->calls_alloca)
2742 : {
2743 2865517 : cselib_val *v2 = CSELIB_VAL_PTR (XEXP (x, 0));
2744 2865517 : rtx x_base = NULL_RTX;
2745 2865517 : HOST_WIDE_INT x_off = 0;
2746 2865517 : if (SP_DERIVED_VALUE_P (v2->val_rtx))
2747 : x_base = v2->val_rtx;
2748 : else
2749 4554695 : for (struct elt_loc_list *l = v2->locs; l; l = l->next)
2750 2836855 : if (GET_CODE (l->loc) == PLUS
2751 1520392 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
2752 1418322 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2753 3910570 : && CONST_INT_P (XEXP (l->loc, 1)))
2754 : {
2755 1073701 : x_base = XEXP (l->loc, 0);
2756 1073701 : x_off = INTVAL (XEXP (l->loc, 1));
2757 1073701 : break;
2758 : }
2759 : /* If x_base is NULL here, don't invalidate x as its address
2760 : isn't derived from sp such that it could be in outgoing
2761 : argument area of some call in !ACCUMULATE_OUTGOING_ARGS
2762 : function. */
2763 2865517 : if (x_base)
2764 : {
2765 1147677 : if (sp_base == NULL_RTX)
2766 : {
2767 2106304 : if (cselib_val *v3
2768 1056268 : = cselib_lookup_1 (stack_pointer_rtx, Pmode, 0,
2769 : VOIDmode))
2770 : {
2771 1048728 : if (SP_DERIVED_VALUE_P (v3->val_rtx))
2772 : sp_base = v3->val_rtx;
2773 : else
2774 273916 : for (struct elt_loc_list *l = v3->locs;
2775 549546 : l; l = l->next)
2776 549532 : if (GET_CODE (l->loc) == PLUS
2777 273902 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
2778 273902 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2779 823434 : && CONST_INT_P (XEXP (l->loc, 1)))
2780 : {
2781 273902 : sp_base = XEXP (l->loc, 0);
2782 273902 : sp_off = INTVAL (XEXP (l->loc, 1));
2783 273902 : break;
2784 : }
2785 : }
2786 1053152 : if (sp_base == NULL_RTX)
2787 4438 : sp_base = pc_rtx;
2788 : }
2789 : /* Otherwise, if x_base and sp_base are the same,
2790 : we know that x_base + x_off is the x's address and
2791 : sp_base + sp_off is current value of stack pointer,
2792 : so try to determine if x is certainly not below stack
2793 : pointer. */
2794 1147677 : if (sp_base == x_base)
2795 : {
2796 1140737 : if (STACK_GROWS_DOWNWARD)
2797 : {
2798 1140737 : HOST_WIDE_INT off = sp_off;
2799 : #ifdef STACK_ADDRESS_OFFSET
2800 : /* On SPARC take stack pointer bias into account as
2801 : well. */
2802 : off += (STACK_ADDRESS_OFFSET
2803 : - FIRST_PARM_OFFSET (current_function_decl));
2804 : #endif
2805 1140737 : if (x_off >= off)
2806 : /* x is at or above the current stack pointer,
2807 : no need to invalidate it. */
2808 : x_base = NULL_RTX;
2809 : }
2810 : else
2811 : {
2812 : HOST_WIDE_INT sz;
2813 : enum machine_mode mode = GET_MODE (x);
2814 : if ((MEM_SIZE_KNOWN_P (x)
2815 : && MEM_SIZE (x).is_constant (&sz))
2816 : || (mode != BLKmode
2817 : && GET_MODE_SIZE (mode).is_constant (&sz)))
2818 : if (x_off < sp_off
2819 : && ((HOST_WIDE_INT) ((unsigned HOST_WIDE_INT)
2820 : x_off + sz) <= sp_off))
2821 : /* x's end is below or at the current stack
2822 : pointer in !STACK_GROWS_DOWNWARD target,
2823 : no need to invalidate it. */
2824 : x_base = NULL_RTX;
2825 : }
2826 : }
2827 : }
2828 2857818 : if (x_base == NULL_RTX)
2829 : {
2830 2857818 : has_mem = true;
2831 2857818 : num_mems++;
2832 2857818 : p = &(*p)->next;
2833 2857818 : continue;
2834 : }
2835 : }
2836 :
2837 427666488 : if (num_mems < param_max_cselib_memory_locations
2838 464657693 : && ! canon_anti_dependence (x, false, mem_rtx,
2839 232296182 : GET_MODE (mem_rtx), mem_addr))
2840 : {
2841 195304977 : has_mem = true;
2842 195304977 : num_mems++;
2843 195304977 : p = &(*p)->next;
2844 195304977 : continue;
2845 : }
2846 :
2847 : /* This one overlaps. */
2848 : /* We must have a mapping from this MEM's address to the
2849 : value (E). Remove that, too. */
2850 37056534 : addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2851 37056534 : addr = canonical_cselib_val (addr);
2852 37056534 : gcc_checking_assert (v == canonical_cselib_val (v));
2853 37056534 : mem_chain = &addr->addr_list;
2854 37061508 : for (;;)
2855 : {
2856 37059021 : cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2857 :
2858 37059021 : if (canon == v)
2859 : {
2860 37056534 : unchain_one_elt_list (mem_chain);
2861 37056534 : break;
2862 : }
2863 :
2864 : /* Record canonicalized elt. */
2865 2487 : (*mem_chain)->elt = canon;
2866 :
2867 2487 : mem_chain = &(*mem_chain)->next;
2868 2487 : }
2869 :
2870 37056534 : unchain_one_elt_loc_list (p);
2871 : }
2872 :
2873 170359609 : if (had_locs && cselib_useless_value_p (v))
2874 : {
2875 8615189 : if (setting_insn && DEBUG_INSN_P (setting_insn))
2876 0 : n_useless_debug_values++;
2877 : else
2878 8615189 : n_useless_values++;
2879 : }
2880 :
2881 170359609 : next = v->next_containing_mem;
2882 170359609 : if (has_mem)
2883 : {
2884 138148174 : *vp = v;
2885 138148174 : vp = &(*vp)->next_containing_mem;
2886 : }
2887 : else
2888 32211435 : v->next_containing_mem = NULL;
2889 : }
2890 113486542 : *vp = &dummy_val;
2891 113486542 : }
2892 :
2893 : /* Invalidate DEST. */
2894 :
2895 : void
2896 517905874 : cselib_invalidate_rtx (rtx dest)
2897 : {
2898 517905874 : while (GET_CODE (dest) == SUBREG
2899 517905874 : || GET_CODE (dest) == ZERO_EXTRACT
2900 1035818364 : || GET_CODE (dest) == STRICT_LOW_PART)
2901 6616 : dest = XEXP (dest, 0);
2902 :
2903 517905874 : if (REG_P (dest))
2904 394602619 : cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2905 123303255 : else if (MEM_P (dest))
2906 75457484 : cselib_invalidate_mem (dest);
2907 517905874 : }
2908 :
2909 : /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2910 :
2911 : static void
2912 501927147 : cselib_invalidate_rtx_note_stores (rtx dest, const_rtx,
2913 : void *data ATTRIBUTE_UNUSED)
2914 : {
2915 501927147 : cselib_invalidate_rtx (dest);
2916 501927147 : }
2917 :
2918 : /* Record the result of a SET instruction. DEST is being set; the source
2919 : contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2920 : describes its address. */
2921 :
2922 : static void
2923 360192085 : cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2924 : {
2925 360192085 : if (src_elt == 0 || side_effects_p (dest))
2926 65710509 : return;
2927 :
2928 294481576 : if (REG_P (dest))
2929 : {
2930 270339651 : unsigned int dreg = REGNO (dest);
2931 270339651 : if (dreg < FIRST_PSEUDO_REGISTER)
2932 : {
2933 198524842 : unsigned int n = REG_NREGS (dest);
2934 :
2935 198524842 : if (n > max_value_regs)
2936 25316290 : max_value_regs = n;
2937 : }
2938 :
2939 270339651 : if (REG_VALUES (dreg) == 0)
2940 : {
2941 165774955 : used_regs[n_used_regs++] = dreg;
2942 165774955 : REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2943 : }
2944 : else
2945 : {
2946 : /* The register should have been invalidated. */
2947 104564696 : gcc_assert (REG_VALUES (dreg)->elt == 0);
2948 104564696 : REG_VALUES (dreg)->elt = src_elt;
2949 : }
2950 :
2951 270339651 : if (cselib_useless_value_p (src_elt))
2952 41905 : n_useless_values--;
2953 270339651 : new_elt_loc_list (src_elt, dest);
2954 : }
2955 24141925 : else if (MEM_P (dest) && dest_addr_elt != 0
2956 24141925 : && cselib_record_memory)
2957 : {
2958 24141925 : if (cselib_useless_value_p (src_elt))
2959 46 : n_useless_values--;
2960 24141925 : add_mem_for_addr (dest_addr_elt, src_elt, dest);
2961 : }
2962 : }
2963 :
2964 : /* Make ELT and X's VALUE equivalent to each other at INSN. */
2965 :
2966 : void
2967 3803491 : cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
2968 : {
2969 3803491 : cselib_val *nelt;
2970 3803491 : rtx_insn *save_cselib_current_insn = cselib_current_insn;
2971 :
2972 3803491 : gcc_checking_assert (elt);
2973 3803491 : gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2974 3803491 : gcc_checking_assert (!side_effects_p (x));
2975 :
2976 3803491 : cselib_current_insn = insn;
2977 :
2978 3803491 : nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2979 :
2980 3803491 : if (nelt != elt)
2981 : {
2982 2933030 : cselib_any_perm_equivs = true;
2983 :
2984 2933030 : if (!PRESERVED_VALUE_P (nelt->val_rtx))
2985 2928056 : cselib_preserve_value (nelt);
2986 :
2987 2933030 : new_elt_loc_list (nelt, elt->val_rtx);
2988 : }
2989 :
2990 3803491 : cselib_current_insn = save_cselib_current_insn;
2991 3803491 : }
2992 :
2993 : /* Return TRUE if any permanent equivalences have been recorded since
2994 : the table was last initialized. */
2995 : bool
2996 1389604389 : cselib_have_permanent_equivalences (void)
2997 : {
2998 1389604389 : return cselib_any_perm_equivs;
2999 : }
3000 :
3001 : /* Record stack_pointer_rtx to be equal to
3002 : (plus:P cfa_base_preserved_val offset). Used by var-tracking
3003 : at the start of basic blocks for !frame_pointer_needed functions. */
3004 :
3005 : void
3006 3375019 : cselib_record_sp_cfa_base_equiv (HOST_WIDE_INT offset, rtx_insn *insn)
3007 : {
3008 3375019 : rtx sp_derived_value = NULL_RTX;
3009 6750038 : for (struct elt_loc_list *l = cfa_base_preserved_val->locs; l; l = l->next)
3010 6750038 : if (GET_CODE (l->loc) == VALUE
3011 6750038 : && SP_DERIVED_VALUE_P (l->loc))
3012 : {
3013 : sp_derived_value = l->loc;
3014 : break;
3015 : }
3016 6750038 : else if (GET_CODE (l->loc) == PLUS
3017 3375019 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
3018 3375019 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
3019 10125057 : && CONST_INT_P (XEXP (l->loc, 1)))
3020 : {
3021 3375019 : sp_derived_value = XEXP (l->loc, 0);
3022 3375019 : offset = offset + UINTVAL (XEXP (l->loc, 1));
3023 3375019 : break;
3024 : }
3025 3375019 : if (sp_derived_value == NULL_RTX)
3026 : return;
3027 3375019 : cselib_val *val
3028 3375019 : = cselib_lookup_from_insn (plus_constant (Pmode, sp_derived_value, offset),
3029 3375019 : Pmode, 1, VOIDmode, insn);
3030 3375019 : if (val != NULL)
3031 : {
3032 3375019 : PRESERVED_VALUE_P (val->val_rtx) = 1;
3033 3375019 : cselib_record_set (stack_pointer_rtx, val, NULL);
3034 : }
3035 : }
3036 :
3037 : /* Return true if V is SP_DERIVED_VALUE_P (or SP_DERIVED_VALUE_P + CONST_INT)
3038 : that can be expressed using cfa_base_preserved_val + CONST_INT. */
3039 :
3040 : bool
3041 30175082 : cselib_sp_derived_value_p (cselib_val *v)
3042 : {
3043 30175082 : if (!SP_DERIVED_VALUE_P (v->val_rtx))
3044 66465032 : for (struct elt_loc_list *l = v->locs; l; l = l->next)
3045 36798863 : if (GET_CODE (l->loc) == PLUS
3046 6978206 : && GET_CODE (XEXP (l->loc, 0)) == VALUE
3047 6760944 : && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
3048 41795462 : && CONST_INT_P (XEXP (l->loc, 1)))
3049 4996599 : v = CSELIB_VAL_PTR (XEXP (l->loc, 0));
3050 30175082 : if (!SP_DERIVED_VALUE_P (v->val_rtx))
3051 : return false;
3052 11308718 : for (struct elt_loc_list *l = v->locs; l; l = l->next)
3053 11308718 : if (l->loc == cfa_base_preserved_val->val_rtx)
3054 : return true;
3055 11308718 : else if (GET_CODE (l->loc) == PLUS
3056 5505512 : && XEXP (l->loc, 0) == cfa_base_preserved_val->val_rtx
3057 5505512 : && CONST_INT_P (XEXP (l->loc, 1)))
3058 : return true;
3059 : return false;
3060 : }
3061 :
3062 : /* There is no good way to determine how many elements there can be
3063 : in a PARALLEL. Since it's fairly cheap, use a really large number. */
3064 : #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
3065 :
3066 : struct cselib_record_autoinc_data
3067 : {
3068 : struct cselib_set *sets;
3069 : int n_sets;
3070 : };
3071 :
3072 : /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
3073 : autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
3074 :
3075 : static int
3076 13431641 : cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
3077 : rtx dest, rtx src, rtx srcoff, void *arg)
3078 : {
3079 13431641 : struct cselib_record_autoinc_data *data;
3080 13431641 : data = (struct cselib_record_autoinc_data *)arg;
3081 :
3082 13431641 : data->sets[data->n_sets].dest = dest;
3083 :
3084 13431641 : if (srcoff)
3085 13060606 : data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
3086 : else
3087 371035 : data->sets[data->n_sets].src = src;
3088 :
3089 13431641 : data->n_sets++;
3090 :
3091 13431641 : return 0;
3092 : }
3093 :
3094 : /* Record the effects of any sets and autoincs in INSN. */
3095 : static void
3096 863348559 : cselib_record_sets (rtx_insn *insn)
3097 : {
3098 863348559 : int n_sets = 0;
3099 863348559 : int i;
3100 863348559 : struct cselib_set sets[MAX_SETS];
3101 863348559 : rtx cond = 0;
3102 863348559 : int n_sets_before_autoinc;
3103 863348559 : int n_strict_low_parts = 0;
3104 863348559 : struct cselib_record_autoinc_data data;
3105 :
3106 863348559 : rtx body = PATTERN (insn);
3107 863348559 : if (GET_CODE (body) == COND_EXEC)
3108 : {
3109 0 : cond = COND_EXEC_TEST (body);
3110 0 : body = COND_EXEC_CODE (body);
3111 : }
3112 :
3113 : /* Find all sets. */
3114 863348559 : if (GET_CODE (body) == SET)
3115 : {
3116 364044294 : sets[0].src = SET_SRC (body);
3117 364044294 : sets[0].dest = SET_DEST (body);
3118 364044294 : n_sets = 1;
3119 : }
3120 499304265 : else if (GET_CODE (body) == PARALLEL)
3121 : {
3122 : /* Look through the PARALLEL and record the values being
3123 : set, if possible. Also handle any CLOBBERs. */
3124 207524177 : for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
3125 : {
3126 139942161 : rtx x = XVECEXP (body, 0, i);
3127 :
3128 139942161 : if (GET_CODE (x) == SET)
3129 : {
3130 73200854 : sets[n_sets].src = SET_SRC (x);
3131 73200854 : sets[n_sets].dest = SET_DEST (x);
3132 73200854 : n_sets++;
3133 : }
3134 : }
3135 : }
3136 :
3137 364044294 : if (n_sets == 1
3138 425901287 : && MEM_P (sets[0].src)
3139 61401462 : && !cselib_record_memory
3140 106991898 : && MEM_READONLY_P (sets[0].src))
3141 : {
3142 3226697 : rtx note = find_reg_equal_equiv_note (insn);
3143 :
3144 3226697 : if (note && CONSTANT_P (XEXP (note, 0)))
3145 2040578 : sets[0].src = XEXP (note, 0);
3146 : }
3147 :
3148 863348559 : data.sets = sets;
3149 863348559 : data.n_sets = n_sets_before_autoinc = n_sets;
3150 863348559 : for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
3151 863348559 : n_sets = data.n_sets;
3152 :
3153 : /* Look up the values that are read. Do this before invalidating the
3154 : locations that are written. */
3155 1314025348 : for (i = 0; i < n_sets; i++)
3156 : {
3157 450676789 : rtx dest = sets[i].dest;
3158 450676789 : rtx orig = dest;
3159 :
3160 : /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
3161 : the low part after invalidating any knowledge about larger modes. */
3162 450676789 : if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
3163 50934 : sets[i].dest = dest = XEXP (dest, 0);
3164 :
3165 : /* We don't know how to record anything but REG or MEM. */
3166 450676789 : if (REG_P (dest)
3167 122160740 : || (MEM_P (dest) && cselib_record_memory))
3168 : {
3169 356817066 : rtx src = sets[i].src;
3170 356817066 : if (cond)
3171 0 : src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
3172 356817066 : sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
3173 356817066 : if (MEM_P (dest))
3174 : {
3175 28301017 : machine_mode address_mode = get_address_mode (dest);
3176 :
3177 28301017 : sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
3178 : address_mode, 1,
3179 28301017 : GET_MODE (dest));
3180 : }
3181 : else
3182 328516049 : sets[i].dest_addr_elt = 0;
3183 : }
3184 :
3185 : /* Improve handling of STRICT_LOW_PART if the current value is known
3186 : to be const0_rtx, then the low bits will be set to dest and higher
3187 : bits will remain zero. Used in code like:
3188 :
3189 : {di:SI=0;clobber flags:CC;}
3190 : flags:CCNO=cmp(bx:SI,0)
3191 : strict_low_part(di:QI)=flags:CCNO<=0
3192 :
3193 : where we can note both that di:QI=flags:CCNO<=0 and
3194 : also that because di:SI is known to be 0 and strict_low_part(di:QI)
3195 : preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0). */
3196 450676789 : scalar_int_mode mode;
3197 450676789 : if (dest != orig
3198 50934 : && cselib_record_sets_hook
3199 15984 : && REG_P (dest)
3200 15984 : && HARD_REGISTER_P (dest)
3201 15984 : && sets[i].src_elt
3202 450692773 : && is_a <scalar_int_mode> (GET_MODE (dest), &mode)
3203 450692773 : && n_sets + n_strict_low_parts < MAX_SETS)
3204 : {
3205 15984 : opt_scalar_int_mode wider_mode_iter;
3206 40026 : FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
3207 : {
3208 40026 : scalar_int_mode wider_mode = wider_mode_iter.require ();
3209 47478 : if (GET_MODE_PRECISION (wider_mode) > BITS_PER_WORD)
3210 : break;
3211 :
3212 38757 : rtx reg = gen_lowpart (wider_mode, dest);
3213 38757 : if (!REG_P (reg))
3214 : break;
3215 :
3216 38732 : cselib_val *v = cselib_lookup (reg, wider_mode, 0, VOIDmode);
3217 38732 : if (!v)
3218 24042 : continue;
3219 :
3220 15637 : struct elt_loc_list *l;
3221 33102 : for (l = v->locs; l; l = l->next)
3222 32155 : if (l->loc == const0_rtx)
3223 : break;
3224 :
3225 947 : if (!l)
3226 947 : continue;
3227 :
3228 14690 : sets[n_sets + n_strict_low_parts].dest = reg;
3229 14690 : sets[n_sets + n_strict_low_parts].src = dest;
3230 14690 : sets[n_sets + n_strict_low_parts++].src_elt = sets[i].src_elt;
3231 14690 : break;
3232 : }
3233 : }
3234 : }
3235 :
3236 863348559 : if (cselib_record_sets_hook)
3237 77373657 : cselib_record_sets_hook (insn, sets, n_sets);
3238 :
3239 : /* Invalidate all locations written by this insn. Note that the elts we
3240 : looked up in the previous loop aren't affected, just some of their
3241 : locations may go away. */
3242 863348559 : note_pattern_stores (body, cselib_invalidate_rtx_note_stores, NULL);
3243 :
3244 1740128759 : for (i = n_sets_before_autoinc; i < n_sets; i++)
3245 13431641 : cselib_invalidate_rtx (sets[i].dest);
3246 :
3247 : /* If this is an asm, look for duplicate sets. This can happen when the
3248 : user uses the same value as an output multiple times. This is valid
3249 : if the outputs are not actually used thereafter. Treat this case as
3250 : if the value isn't actually set. We do this by smashing the destination
3251 : to pc_rtx, so that we won't record the value later. */
3252 863348559 : if (n_sets >= 2 && asm_noperands (body) >= 0)
3253 : {
3254 495778 : for (i = 0; i < n_sets; i++)
3255 : {
3256 383915 : rtx dest = sets[i].dest;
3257 383915 : if (REG_P (dest) || MEM_P (dest))
3258 : {
3259 383882 : int j;
3260 900335 : for (j = i + 1; j < n_sets; j++)
3261 516453 : if (rtx_equal_p (dest, sets[j].dest))
3262 : {
3263 0 : sets[i].dest = pc_rtx;
3264 0 : sets[j].dest = pc_rtx;
3265 : }
3266 : }
3267 : }
3268 : }
3269 :
3270 : /* Now enter the equivalences in our tables. */
3271 1314025348 : for (i = 0; i < n_sets; i++)
3272 : {
3273 450676789 : rtx dest = sets[i].dest;
3274 450676789 : if (REG_P (dest)
3275 122160740 : || (MEM_P (dest) && cselib_record_memory))
3276 356817066 : cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
3277 : }
3278 :
3279 : /* And deal with STRICT_LOW_PART. */
3280 863363249 : for (i = 0; i < n_strict_low_parts; i++)
3281 : {
3282 14690 : if (! PRESERVED_VALUE_P (sets[n_sets + i].src_elt->val_rtx))
3283 0 : continue;
3284 14690 : machine_mode dest_mode = GET_MODE (sets[n_sets + i].dest);
3285 14690 : cselib_val *v
3286 14690 : = cselib_lookup (sets[n_sets + i].dest, dest_mode, 1, VOIDmode);
3287 14690 : cselib_preserve_value (v);
3288 14690 : rtx r = gen_rtx_ZERO_EXTEND (dest_mode,
3289 : sets[n_sets + i].src_elt->val_rtx);
3290 14690 : cselib_add_permanent_equiv (v, r, insn);
3291 : }
3292 863348559 : }
3293 :
3294 : /* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
3295 :
3296 : bool
3297 40756209 : fp_setter_insn (rtx_insn *insn)
3298 : {
3299 40756209 : rtx expr, pat = NULL_RTX;
3300 :
3301 40756209 : if (!RTX_FRAME_RELATED_P (insn))
3302 : return false;
3303 :
3304 623862 : expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
3305 623862 : if (expr)
3306 62 : pat = XEXP (expr, 0);
3307 623862 : if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
3308 : return false;
3309 :
3310 : /* Don't return true for frame pointer restores in the epilogue. */
3311 153118 : if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
3312 : return false;
3313 : return true;
3314 : }
3315 :
3316 : /* V is one of the values in REG_VALUES (REGNO). Return true if it
3317 : would be invalidated by CALLEE_ABI. */
3318 :
3319 : static bool
3320 111993433 : cselib_invalidated_by_call_p (const function_abi &callee_abi,
3321 : unsigned int regno, cselib_val *v)
3322 : {
3323 111993433 : machine_mode mode = GET_MODE (v->val_rtx);
3324 111993433 : if (mode == VOIDmode)
3325 : {
3326 0 : v = REG_VALUES (regno)->elt;
3327 0 : if (!v)
3328 : /* If we don't know what the mode of the constant value is, and we
3329 : don't know what mode the register was set in, conservatively
3330 : assume that the register is clobbered. The value's going to be
3331 : essentially useless in this case anyway. */
3332 : return true;
3333 0 : mode = GET_MODE (v->val_rtx);
3334 : }
3335 111993433 : return callee_abi.clobbers_reg_p (mode, regno);
3336 : }
3337 :
3338 : /* Record the effects of INSN. */
3339 :
3340 : void
3341 996509959 : cselib_process_insn (rtx_insn *insn)
3342 : {
3343 996509959 : int i;
3344 996509959 : rtx x;
3345 :
3346 996509959 : cselib_current_insn = insn;
3347 :
3348 : /* Forget everything at a CODE_LABEL or a setjmp. */
3349 996509959 : if ((LABEL_P (insn)
3350 961809863 : || (CALL_P (insn)
3351 33591903 : && find_reg_note (insn, REG_SETJMP, NULL)))
3352 996514294 : && !cselib_preserve_constants)
3353 : {
3354 34704203 : cselib_reset_table (next_uid);
3355 34704203 : cselib_current_insn = NULL;
3356 34704203 : return;
3357 : }
3358 :
3359 961805756 : if (! INSN_P (insn))
3360 : {
3361 98457197 : cselib_current_insn = NULL;
3362 98457197 : return;
3363 : }
3364 :
3365 : /* If this is a call instruction, forget anything stored in a
3366 : call clobbered register, or, if this is not a const call, in
3367 : memory. */
3368 863348559 : if (CALL_P (insn))
3369 : {
3370 33587796 : function_abi callee_abi = insn_callee_abi (insn);
3371 3123665028 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3372 : {
3373 3090077232 : elt_list **l = ®_VALUES (i);
3374 3307985425 : while (*l)
3375 : {
3376 217908193 : cselib_val *v = (*l)->elt;
3377 217908193 : if (v && cselib_invalidated_by_call_p (callee_abi, i, v))
3378 69363448 : cselib_invalidate_regno_val (i, l);
3379 : else
3380 148544745 : l = &(*l)->next;
3381 : }
3382 : }
3383 :
3384 : /* Since it is not clear how cselib is going to be used, be
3385 : conservative here and treat looping pure or const functions
3386 : as if they were regular functions. */
3387 33587796 : if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
3388 33587796 : || !(RTL_CONST_OR_PURE_CALL_P (insn)))
3389 30634430 : cselib_invalidate_mem (callmem[0]);
3390 : else
3391 : {
3392 : /* For const/pure calls, invalidate any argument slots because
3393 : they are owned by the callee. */
3394 8506136 : for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3395 5552770 : if (GET_CODE (XEXP (x, 0)) == USE
3396 5552494 : && MEM_P (XEXP (XEXP (x, 0), 0)))
3397 141902 : cselib_invalidate_mem (XEXP (XEXP (x, 0), 0));
3398 : /* And invalidate memory below the stack (or above for
3399 : !STACK_GROWS_DOWNWARD), as even const/pure call can invalidate
3400 : that. Do this only if !ACCUMULATE_OUTGOING_ARGS or if
3401 : cfun->calls_alloca, otherwise the stack pointer shouldn't be
3402 : changing in the middle of the function and nothing should be
3403 : stored below the stack pointer. */
3404 2953366 : if (!ACCUMULATE_OUTGOING_ARGS || cfun->calls_alloca)
3405 2952923 : cselib_invalidate_mem (callmem[1]);
3406 : }
3407 : }
3408 :
3409 863348559 : cselib_record_sets (insn);
3410 :
3411 : /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3412 : after we have processed the insn. */
3413 863348559 : if (CALL_P (insn))
3414 : {
3415 101071605 : for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3416 67483809 : if (GET_CODE (XEXP (x, 0)) == CLOBBER)
3417 1858939 : cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
3418 :
3419 : /* Flush everything on setjmp. */
3420 33587796 : if (cselib_preserve_constants
3421 33587796 : && find_reg_note (insn, REG_SETJMP, NULL))
3422 : {
3423 228 : cselib_preserve_only_values ();
3424 228 : cselib_reset_table (next_uid);
3425 : }
3426 : }
3427 :
3428 : /* On setter of the hard frame pointer if frame_pointer_needed,
3429 : invalidate stack_pointer_rtx, so that sp and {,h}fp based
3430 : VALUEs are distinct. */
3431 863348559 : if (reload_completed
3432 395354074 : && frame_pointer_needed
3433 903985193 : && fp_setter_insn (insn))
3434 93238 : cselib_invalidate_rtx (stack_pointer_rtx);
3435 :
3436 863348559 : cselib_current_insn = NULL;
3437 :
3438 863348559 : if (n_useless_values > MAX_USELESS_VALUES
3439 : /* remove_useless_values is linear in the hash table size. Avoid
3440 : quadratic behavior for very large hashtables with very few
3441 : useless elements. */
3442 863348559 : && ((unsigned int)n_useless_values
3443 2081875 : > (cselib_hash_table->elements () - n_debug_values) / 4))
3444 28724 : remove_useless_values ();
3445 : }
3446 :
3447 : /* Initialize cselib for one pass. The caller must also call
3448 : init_alias_analysis. */
3449 :
3450 : void
3451 10101455 : cselib_init (int record_what)
3452 : {
3453 10101455 : cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
3454 10101455 : cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
3455 10101455 : cselib_any_perm_equivs = false;
3456 :
3457 : /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
3458 : see canon_true_dependence. This is only created once. */
3459 10101455 : if (! callmem[0])
3460 146060 : callmem[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
3461 : /* Similarly create a MEM representing roughly everything below
3462 : the stack for STACK_GROWS_DOWNWARD targets or everything above
3463 : it otherwise. Do this only when !ACCUMULATE_OUTGOING_ARGS or
3464 : if cfun->calls_alloca, otherwise the stack pointer shouldn't be
3465 : changing in the middle of the function and nothing should be stored
3466 : below the stack pointer. */
3467 10101455 : if (!callmem[1] && (!ACCUMULATE_OUTGOING_ARGS || cfun->calls_alloca))
3468 : {
3469 145805 : if (STACK_GROWS_DOWNWARD)
3470 : {
3471 145805 : unsigned HOST_WIDE_INT off = -(GET_MODE_MASK (Pmode) >> 1);
3472 : #ifdef STACK_ADDRESS_OFFSET
3473 : /* On SPARC take stack pointer bias into account as well. */
3474 : off += (STACK_ADDRESS_OFFSET
3475 : - FIRST_PARM_OFFSET (current_function_decl));
3476 : #endif
3477 145805 : callmem[1] = plus_constant (Pmode, stack_pointer_rtx, off);
3478 : }
3479 : else
3480 : callmem[1] = stack_pointer_rtx;
3481 145805 : callmem[1] = gen_rtx_MEM (BLKmode, callmem[1]);
3482 151222 : set_mem_size (callmem[1], GET_MODE_MASK (Pmode) >> 1);
3483 : }
3484 :
3485 10101455 : cselib_nregs = max_reg_num ();
3486 :
3487 : /* We preserve reg_values to allow expensive clearing of the whole thing.
3488 : Reallocate it however if it happens to be too large. */
3489 10101455 : if (!reg_values || reg_values_size < cselib_nregs
3490 9802833 : || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
3491 : {
3492 313868 : free (reg_values);
3493 : /* Some space for newly emit instructions so we don't end up
3494 : reallocating in between passes. */
3495 313868 : reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
3496 313868 : reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
3497 : }
3498 10101455 : used_regs = XNEWVEC (unsigned int, cselib_nregs);
3499 10101455 : n_used_regs = 0;
3500 : /* FIXME: enable sanitization (PR87845) */
3501 10101455 : cselib_hash_table
3502 10101455 : = new hash_table<cselib_hasher> (31, /* ggc */ false,
3503 10101455 : /* sanitize_eq_and_hash */ false);
3504 10101455 : if (cselib_preserve_constants)
3505 493393 : cselib_preserved_hash_table
3506 493393 : = new hash_table<cselib_hasher> (31, /* ggc */ false,
3507 493393 : /* sanitize_eq_and_hash */ false);
3508 10101455 : next_uid = 1;
3509 10101455 : }
3510 :
3511 : /* Called when the current user is done with cselib. */
3512 :
3513 : void
3514 10101455 : cselib_finish (void)
3515 : {
3516 10101455 : bool preserved = cselib_preserve_constants;
3517 10101455 : cselib_discard_hook = NULL;
3518 10101455 : cselib_preserve_constants = false;
3519 10101455 : cselib_any_perm_equivs = false;
3520 10101455 : cfa_base_preserved_val = NULL;
3521 10101455 : cfa_base_preserved_regno = INVALID_REGNUM;
3522 10101455 : elt_list_pool.release ();
3523 10101455 : elt_loc_list_pool.release ();
3524 10101455 : cselib_val_pool.release ();
3525 10101455 : value_pool.release ();
3526 10101455 : cselib_clear_table ();
3527 10101455 : delete cselib_hash_table;
3528 10101455 : cselib_hash_table = NULL;
3529 10101455 : if (preserved)
3530 493393 : delete cselib_preserved_hash_table;
3531 10101455 : cselib_preserved_prune_list.release ();
3532 10101455 : cselib_preserved_hash_table = NULL;
3533 10101455 : free (used_regs);
3534 10101455 : used_regs = 0;
3535 10101455 : n_useless_values = 0;
3536 10101455 : n_useless_debug_values = 0;
3537 10101455 : n_debug_values = 0;
3538 10101455 : next_uid = 0;
3539 10101455 : }
3540 :
3541 : /* Dump the cselib_val V to FILE *OUT. */
3542 :
3543 : int
3544 93 : dump_cselib_val (cselib_val *v, FILE *out)
3545 : {
3546 93 : bool need_lf = true;
3547 :
3548 93 : print_inline_rtx (out, v->val_rtx, 0);
3549 :
3550 93 : if (v->locs)
3551 : {
3552 86 : struct elt_loc_list *l = v->locs;
3553 86 : if (need_lf)
3554 : {
3555 86 : fputc ('\n', out);
3556 86 : need_lf = false;
3557 : }
3558 86 : fputs (" locs:", out);
3559 149 : do
3560 : {
3561 149 : if (l->setting_insn)
3562 149 : fprintf (out, "\n from insn %i ",
3563 149 : INSN_UID (l->setting_insn));
3564 : else
3565 0 : fprintf (out, "\n ");
3566 149 : print_inline_rtx (out, l->loc, 4);
3567 : }
3568 149 : while ((l = l->next));
3569 86 : fputc ('\n', out);
3570 : }
3571 : else
3572 : {
3573 7 : fputs (" no locs", out);
3574 7 : need_lf = true;
3575 : }
3576 :
3577 93 : if (v->addr_list)
3578 : {
3579 6 : struct elt_list *e = v->addr_list;
3580 6 : if (need_lf)
3581 : {
3582 0 : fputc ('\n', out);
3583 0 : need_lf = false;
3584 : }
3585 6 : fputs (" addr list:", out);
3586 6 : do
3587 : {
3588 6 : fputs ("\n ", out);
3589 6 : print_inline_rtx (out, e->elt->val_rtx, 2);
3590 : }
3591 6 : while ((e = e->next));
3592 6 : fputc ('\n', out);
3593 : }
3594 : else
3595 : {
3596 87 : fputs (" no addrs", out);
3597 87 : need_lf = true;
3598 : }
3599 :
3600 93 : if (v->next_containing_mem == &dummy_val)
3601 6 : fputs (" last mem\n", out);
3602 87 : else if (v->next_containing_mem)
3603 : {
3604 0 : fputs (" next mem ", out);
3605 0 : print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
3606 0 : fputc ('\n', out);
3607 : }
3608 87 : else if (need_lf)
3609 81 : fputc ('\n', out);
3610 :
3611 93 : return 1;
3612 : }
3613 :
3614 : /* Dump the cselib_val *X to FILE *OUT. */
3615 :
3616 : static int
3617 93 : dump_cselib_val_ptr (cselib_val **x, FILE *out)
3618 : {
3619 93 : cselib_val *v = *x;
3620 93 : return dump_cselib_val (v, out);
3621 : }
3622 :
3623 : /* Dump to OUT everything in the CSELIB table. */
3624 :
3625 : void
3626 11 : dump_cselib_table (FILE *out)
3627 : {
3628 11 : fprintf (out, "cselib hash table:\n");
3629 104 : cselib_hash_table->traverse <FILE *, dump_cselib_val_ptr> (out);
3630 11 : if (cselib_preserved_hash_table)
3631 : {
3632 11 : fprintf (out, "cselib preserved hash table:\n");
3633 11 : cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val_ptr> (out);
3634 : }
3635 11 : if (first_containing_mem != &dummy_val)
3636 : {
3637 6 : fputs ("first mem ", out);
3638 6 : print_inline_rtx (out, first_containing_mem->val_rtx, 2);
3639 6 : fputc ('\n', out);
3640 : }
3641 11 : fprintf (out, "next uid %i\n", next_uid);
3642 11 : }
3643 :
3644 : #include "gt-cselib.h"
|