cselib.h (cselib_add_permanent_equiv): Declare.
[gcc.git] / gcc / cselib.c
1 /* Common subexpression elimination library for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "emit-rtl.h"
36 #include "diagnostic-core.h"
37 #include "output.h"
38 #include "ggc.h"
39 #include "hashtab.h"
40 #include "tree-pass.h"
41 #include "cselib.h"
42 #include "params.h"
43 #include "alloc-pool.h"
44 #include "target.h"
45 #include "bitmap.h"
46
47 /* A list of cselib_val structures. */
48 struct elt_list {
49 struct elt_list *next;
50 cselib_val *elt;
51 };
52
53 static bool cselib_record_memory;
54 static bool cselib_preserve_constants;
55 static int entry_and_rtx_equal_p (const void *, const void *);
56 static hashval_t get_value_hash (const void *);
57 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
58 static void new_elt_loc_list (cselib_val *, rtx);
59 static void unchain_one_value (cselib_val *);
60 static void unchain_one_elt_list (struct elt_list **);
61 static void unchain_one_elt_loc_list (struct elt_loc_list **);
62 static int discard_useless_locs (void **, void *);
63 static int discard_useless_values (void **, void *);
64 static void remove_useless_values (void);
65 static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
66 static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
67 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
68 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
69 static cselib_val *cselib_lookup_mem (rtx, int);
70 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
71 static void cselib_invalidate_mem (rtx);
72 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
73 static void cselib_record_sets (rtx);
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 /* There are three ways in which cselib can look up an rtx:
86 - for a REG, the reg_values table (which is indexed by regno) is used
87 - for a MEM, we recursively look up its address and then follow the
88 addr_list of that value
89 - for everything else, we compute a hash value and go through the hash
90 table. Since different rtx's can still have the same hash value,
91 this involves walking the table entries for a given value and comparing
92 the locations of the entries with the rtx we are looking up. */
93
94 /* A table that enables us to look up elts by their value. */
95 static htab_t cselib_hash_table;
96
97 /* This is a global so we don't have to pass this through every function.
98 It is used in new_elt_loc_list to set SETTING_INSN. */
99 static rtx cselib_current_insn;
100
101 /* The unique id that the next create value will take. */
102 static unsigned int next_uid;
103
104 /* The number of registers we had when the varrays were last resized. */
105 static unsigned int cselib_nregs;
106
107 /* Count values without known locations, or with only locations that
108 wouldn't have been known except for debug insns. Whenever this
109 grows too big, we remove these useless values from the table.
110
111 Counting values with only debug values is a bit tricky. We don't
112 want to increment n_useless_values when we create a value for a
113 debug insn, for this would get n_useless_values out of sync, but we
114 want increment it if all locs in the list that were ever referenced
115 in nondebug insns are removed from the list.
116
117 In the general case, once we do that, we'd have to stop accepting
118 nondebug expressions in the loc list, to avoid having two values
119 equivalent that, without debug insns, would have been made into
120 separate values. However, because debug insns never introduce
121 equivalences themselves (no assignments), the only means for
122 growing loc lists is through nondebug assignments. If the locs
123 also happen to be referenced in debug insns, it will work just fine.
124
125 A consequence of this is that there's at most one debug-only loc in
126 each loc list. If we keep it in the first entry, testing whether
127 we have a debug-only loc list takes O(1).
128
129 Furthermore, since any additional entry in a loc list containing a
130 debug loc would have to come from an assignment (nondebug) that
131 references both the initial debug loc and the newly-equivalent loc,
132 the initial debug loc would be promoted to a nondebug loc, and the
133 loc list would not contain debug locs any more.
134
135 So the only case we have to be careful with in order to keep
136 n_useless_values in sync between debug and nondebug compilations is
137 to avoid incrementing n_useless_values when removing the single loc
138 from a value that turns out to not appear outside debug values. We
139 increment n_useless_debug_values instead, and leave such values
140 alone until, for other reasons, we garbage-collect useless
141 values. */
142 static int n_useless_values;
143 static int n_useless_debug_values;
144
145 /* Count values whose locs have been taken exclusively from debug
146 insns for the entire life of the value. */
147 static int n_debug_values;
148
149 /* Number of useless values before we remove them from the hash table. */
150 #define MAX_USELESS_VALUES 32
151
152 /* This table maps from register number to values. It does not
153 contain pointers to cselib_val structures, but rather elt_lists.
154 The purpose is to be able to refer to the same register in
155 different modes. The first element of the list defines the mode in
156 which the register was set; if the mode is unknown or the value is
157 no longer valid in that mode, ELT will be NULL for the first
158 element. */
159 static struct elt_list **reg_values;
160 static unsigned int reg_values_size;
161 #define REG_VALUES(i) reg_values[i]
162
163 /* The largest number of hard regs used by any entry added to the
164 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
165 static unsigned int max_value_regs;
166
167 /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
168 in cselib_clear_table() for fast emptying. */
169 static unsigned int *used_regs;
170 static unsigned int n_used_regs;
171
172 /* We pass this to cselib_invalidate_mem to invalidate all of
173 memory for a non-const call instruction. */
174 static GTY(()) rtx callmem;
175
176 /* Set by discard_useless_locs if it deleted the last location of any
177 value. */
178 static int values_became_useless;
179
180 /* Used as stop element of the containing_mem list so we can check
181 presence in the list by checking the next pointer. */
182 static cselib_val dummy_val;
183
184 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
185 that is constant through the whole function and should never be
186 eliminated. */
187 static cselib_val *cfa_base_preserved_val;
188 static unsigned int cfa_base_preserved_regno = INVALID_REGNUM;
189
190 /* Used to list all values that contain memory reference.
191 May or may not contain the useless values - the list is compacted
192 each time memory is invalidated. */
193 static cselib_val *first_containing_mem = &dummy_val;
194 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
195
196 /* If nonnull, cselib will call this function before freeing useless
197 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
198 void (*cselib_discard_hook) (cselib_val *);
199
200 /* If nonnull, cselib will call this function before recording sets or
201 even clobbering outputs of INSN. All the recorded sets will be
202 represented in the array sets[n_sets]. new_val_min can be used to
203 tell whether values present in sets are introduced by this
204 instruction. */
205 void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
206 int n_sets);
207
208 #define PRESERVED_VALUE_P(RTX) \
209 (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
210
211 \f
212
213 /* Allocate a struct elt_list and fill in its two elements with the
214 arguments. */
215
216 static inline struct elt_list *
217 new_elt_list (struct elt_list *next, cselib_val *elt)
218 {
219 struct elt_list *el;
220 el = (struct elt_list *) pool_alloc (elt_list_pool);
221 el->next = next;
222 el->elt = elt;
223 return el;
224 }
225
226 /* Allocate a struct elt_loc_list with LOC and prepend it to VAL's loc
227 list. */
228
229 static inline void
230 new_elt_loc_list (cselib_val *val, rtx loc)
231 {
232 struct elt_loc_list *el, *next = val->locs;
233
234 gcc_checking_assert (!next || !next->setting_insn
235 || !DEBUG_INSN_P (next->setting_insn)
236 || cselib_current_insn == next->setting_insn);
237
238 /* If we're creating the first loc in a debug insn context, we've
239 just created a debug value. Count it. */
240 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
241 n_debug_values++;
242
243 val = canonical_cselib_val (val);
244 next = val->locs;
245
246 if (GET_CODE (loc) == VALUE)
247 {
248 loc = canonical_cselib_val (CSELIB_VAL_PTR (loc))->val_rtx;
249
250 gcc_checking_assert (PRESERVED_VALUE_P (loc)
251 == PRESERVED_VALUE_P (val->val_rtx));
252
253 if (val->val_rtx == loc)
254 return;
255 else if (val->uid > CSELIB_VAL_PTR (loc)->uid)
256 {
257 /* Reverse the insertion. */
258 new_elt_loc_list (CSELIB_VAL_PTR (loc), val->val_rtx);
259 return;
260 }
261
262 gcc_checking_assert (val->uid < CSELIB_VAL_PTR (loc)->uid);
263
264 if (CSELIB_VAL_PTR (loc)->locs)
265 {
266 /* Bring all locs from LOC to VAL. */
267 for (el = CSELIB_VAL_PTR (loc)->locs; el->next; el = el->next)
268 {
269 /* Adjust values that have LOC as canonical so that VAL
270 becomes their canonical. */
271 if (el->loc && GET_CODE (el->loc) == VALUE)
272 {
273 gcc_checking_assert (CSELIB_VAL_PTR (el->loc)->locs->loc
274 == loc);
275 CSELIB_VAL_PTR (el->loc)->locs->loc = val->val_rtx;
276 }
277 }
278 el->next = val->locs;
279 next = val->locs = CSELIB_VAL_PTR (loc)->locs;
280 }
281
282 /* Chain LOC back to VAL. */
283 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
284 el->loc = val->val_rtx;
285 el->setting_insn = cselib_current_insn;
286 el->next = NULL;
287 CSELIB_VAL_PTR (loc)->locs = el;
288 }
289
290 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
291 el->loc = loc;
292 el->setting_insn = cselib_current_insn;
293 el->next = next;
294 val->locs = el;
295 }
296
297 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
298 originating from a debug insn, maintaining the debug values
299 count. */
300
301 static inline void
302 promote_debug_loc (struct elt_loc_list *l)
303 {
304 if (l->setting_insn && DEBUG_INSN_P (l->setting_insn)
305 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
306 {
307 n_debug_values--;
308 l->setting_insn = cselib_current_insn;
309 if (cselib_preserve_constants && l->next)
310 {
311 gcc_assert (l->next->setting_insn
312 && DEBUG_INSN_P (l->next->setting_insn)
313 && !l->next->next);
314 l->next->setting_insn = cselib_current_insn;
315 }
316 else
317 gcc_assert (!l->next);
318 }
319 }
320
321 /* The elt_list at *PL is no longer needed. Unchain it and free its
322 storage. */
323
324 static inline void
325 unchain_one_elt_list (struct elt_list **pl)
326 {
327 struct elt_list *l = *pl;
328
329 *pl = l->next;
330 pool_free (elt_list_pool, l);
331 }
332
333 /* Likewise for elt_loc_lists. */
334
335 static void
336 unchain_one_elt_loc_list (struct elt_loc_list **pl)
337 {
338 struct elt_loc_list *l = *pl;
339
340 *pl = l->next;
341 pool_free (elt_loc_list_pool, l);
342 }
343
344 /* Likewise for cselib_vals. This also frees the addr_list associated with
345 V. */
346
347 static void
348 unchain_one_value (cselib_val *v)
349 {
350 while (v->addr_list)
351 unchain_one_elt_list (&v->addr_list);
352
353 pool_free (cselib_val_pool, v);
354 }
355
356 /* Remove all entries from the hash table. Also used during
357 initialization. */
358
359 void
360 cselib_clear_table (void)
361 {
362 cselib_reset_table (1);
363 }
364
365 /* Remove from hash table all VALUEs except constants
366 and function invariants. */
367
368 static int
369 preserve_only_constants (void **x, void *info ATTRIBUTE_UNUSED)
370 {
371 cselib_val *v = (cselib_val *)*x;
372 struct elt_loc_list *l;
373
374 if (v->locs != NULL
375 && v->locs->next == NULL)
376 {
377 if (CONSTANT_P (v->locs->loc)
378 && (GET_CODE (v->locs->loc) != CONST
379 || !references_value_p (v->locs->loc, 0)))
380 return 1;
381 /* Although a debug expr may be bound to different expressions,
382 we can preserve it as if it was constant, to get unification
383 and proper merging within var-tracking. */
384 if (GET_CODE (v->locs->loc) == DEBUG_EXPR
385 || GET_CODE (v->locs->loc) == DEBUG_IMPLICIT_PTR
386 || GET_CODE (v->locs->loc) == ENTRY_VALUE
387 || GET_CODE (v->locs->loc) == DEBUG_PARAMETER_REF)
388 return 1;
389 if (cfa_base_preserved_val)
390 {
391 if (v == cfa_base_preserved_val)
392 return 1;
393 if (GET_CODE (v->locs->loc) == PLUS
394 && CONST_INT_P (XEXP (v->locs->loc, 1))
395 && XEXP (v->locs->loc, 0) == cfa_base_preserved_val->val_rtx)
396 return 1;
397 }
398 }
399
400 /* Keep VALUE equivalences around. */
401 for (l = v->locs; l; l = l->next)
402 if (GET_CODE (l->loc) == VALUE)
403 return 1;
404
405 htab_clear_slot (cselib_hash_table, x);
406 return 1;
407 }
408
409 /* Remove all entries from the hash table, arranging for the next
410 value to be numbered NUM. */
411
412 void
413 cselib_reset_table (unsigned int num)
414 {
415 unsigned int i;
416
417 max_value_regs = 0;
418
419 if (cfa_base_preserved_val)
420 {
421 unsigned int regno = cfa_base_preserved_regno;
422 unsigned int new_used_regs = 0;
423 for (i = 0; i < n_used_regs; i++)
424 if (used_regs[i] == regno)
425 {
426 new_used_regs = 1;
427 continue;
428 }
429 else
430 REG_VALUES (used_regs[i]) = 0;
431 gcc_assert (new_used_regs == 1);
432 n_used_regs = new_used_regs;
433 used_regs[0] = regno;
434 max_value_regs
435 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
436 }
437 else
438 {
439 for (i = 0; i < n_used_regs; i++)
440 REG_VALUES (used_regs[i]) = 0;
441 n_used_regs = 0;
442 }
443
444 if (cselib_preserve_constants)
445 htab_traverse (cselib_hash_table, preserve_only_constants, NULL);
446 else
447 htab_empty (cselib_hash_table);
448
449 n_useless_values = 0;
450 n_useless_debug_values = 0;
451 n_debug_values = 0;
452
453 next_uid = num;
454
455 first_containing_mem = &dummy_val;
456 }
457
458 /* Return the number of the next value that will be generated. */
459
460 unsigned int
461 cselib_get_next_uid (void)
462 {
463 return next_uid;
464 }
465
466 /* See the documentation of cselib_find_slot below. */
467 static enum machine_mode find_slot_memmode;
468
469 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
470 INSERTing if requested. When X is part of the address of a MEM,
471 MEMMODE should specify the mode of the MEM. While searching the
472 table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
473 in X can be resolved. */
474
475 static void **
476 cselib_find_slot (rtx x, hashval_t hash, enum insert_option insert,
477 enum machine_mode memmode)
478 {
479 void **slot;
480 find_slot_memmode = memmode;
481 slot = htab_find_slot_with_hash (cselib_hash_table, x, hash, insert);
482 find_slot_memmode = VOIDmode;
483 return slot;
484 }
485
486 /* The equality test for our hash table. The first argument ENTRY is a table
487 element (i.e. a cselib_val), while the second arg X is an rtx. We know
488 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
489 CONST of an appropriate mode. */
490
491 static int
492 entry_and_rtx_equal_p (const void *entry, const void *x_arg)
493 {
494 struct elt_loc_list *l;
495 const cselib_val *const v = (const cselib_val *) entry;
496 rtx x = CONST_CAST_RTX ((const_rtx)x_arg);
497 enum machine_mode mode = GET_MODE (x);
498
499 gcc_assert (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
500 && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE));
501
502 if (mode != GET_MODE (v->val_rtx))
503 return 0;
504
505 /* Unwrap X if necessary. */
506 if (GET_CODE (x) == CONST
507 && (CONST_INT_P (XEXP (x, 0))
508 || GET_CODE (XEXP (x, 0)) == CONST_FIXED
509 || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
510 x = XEXP (x, 0);
511
512 /* We don't guarantee that distinct rtx's have different hash values,
513 so we need to do a comparison. */
514 for (l = v->locs; l; l = l->next)
515 if (rtx_equal_for_cselib_1 (l->loc, x, find_slot_memmode))
516 {
517 promote_debug_loc (l);
518 return 1;
519 }
520
521 return 0;
522 }
523
524 /* The hash function for our hash table. The value is always computed with
525 cselib_hash_rtx when adding an element; this function just extracts the
526 hash value from a cselib_val structure. */
527
528 static hashval_t
529 get_value_hash (const void *entry)
530 {
531 const cselib_val *const v = (const cselib_val *) entry;
532 return v->hash;
533 }
534
535 /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
536 only return true for values which point to a cselib_val whose value
537 element has been set to zero, which implies the cselib_val will be
538 removed. */
539
540 int
541 references_value_p (const_rtx x, int only_useless)
542 {
543 const enum rtx_code code = GET_CODE (x);
544 const char *fmt = GET_RTX_FORMAT (code);
545 int i, j;
546
547 if (GET_CODE (x) == VALUE
548 && (! only_useless ||
549 (CSELIB_VAL_PTR (x)->locs == 0 && !PRESERVED_VALUE_P (x))))
550 return 1;
551
552 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
553 {
554 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
555 return 1;
556 else if (fmt[i] == 'E')
557 for (j = 0; j < XVECLEN (x, i); j++)
558 if (references_value_p (XVECEXP (x, i, j), only_useless))
559 return 1;
560 }
561
562 return 0;
563 }
564
565 /* For all locations found in X, delete locations that reference useless
566 values (i.e. values without any location). Called through
567 htab_traverse. */
568
569 static int
570 discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
571 {
572 cselib_val *v = (cselib_val *)*x;
573 struct elt_loc_list **p = &v->locs;
574 bool had_locs = v->locs != NULL;
575 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
576
577 while (*p)
578 {
579 if (references_value_p ((*p)->loc, 1))
580 unchain_one_elt_loc_list (p);
581 else
582 p = &(*p)->next;
583 }
584
585 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
586 {
587 if (setting_insn && DEBUG_INSN_P (setting_insn))
588 n_useless_debug_values++;
589 else
590 n_useless_values++;
591 values_became_useless = 1;
592 }
593 return 1;
594 }
595
596 /* If X is a value with no locations, remove it from the hashtable. */
597
598 static int
599 discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
600 {
601 cselib_val *v = (cselib_val *)*x;
602
603 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
604 {
605 if (cselib_discard_hook)
606 cselib_discard_hook (v);
607
608 CSELIB_VAL_PTR (v->val_rtx) = NULL;
609 htab_clear_slot (cselib_hash_table, x);
610 unchain_one_value (v);
611 n_useless_values--;
612 }
613
614 return 1;
615 }
616
617 /* Clean out useless values (i.e. those which no longer have locations
618 associated with them) from the hash table. */
619
620 static void
621 remove_useless_values (void)
622 {
623 cselib_val **p, *v;
624
625 /* First pass: eliminate locations that reference the value. That in
626 turn can make more values useless. */
627 do
628 {
629 values_became_useless = 0;
630 htab_traverse (cselib_hash_table, discard_useless_locs, 0);
631 }
632 while (values_became_useless);
633
634 /* Second pass: actually remove the values. */
635
636 p = &first_containing_mem;
637 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
638 if (v->locs)
639 {
640 *p = v;
641 p = &(*p)->next_containing_mem;
642 }
643 *p = &dummy_val;
644
645 n_useless_values += n_useless_debug_values;
646 n_debug_values -= n_useless_debug_values;
647 n_useless_debug_values = 0;
648
649 htab_traverse (cselib_hash_table, discard_useless_values, 0);
650
651 gcc_assert (!n_useless_values);
652 }
653
654 /* Arrange for a value to not be removed from the hash table even if
655 it becomes useless. */
656
657 void
658 cselib_preserve_value (cselib_val *v)
659 {
660 PRESERVED_VALUE_P (v->val_rtx) = 1;
661 }
662
663 /* Test whether a value is preserved. */
664
665 bool
666 cselib_preserved_value_p (cselib_val *v)
667 {
668 return PRESERVED_VALUE_P (v->val_rtx);
669 }
670
671 /* Arrange for a REG value to be assumed constant through the whole function,
672 never invalidated and preserved across cselib_reset_table calls. */
673
674 void
675 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
676 {
677 if (cselib_preserve_constants
678 && v->locs
679 && REG_P (v->locs->loc))
680 {
681 cfa_base_preserved_val = v;
682 cfa_base_preserved_regno = regno;
683 }
684 }
685
686 /* Clean all non-constant expressions in the hash table, but retain
687 their values. */
688
689 void
690 cselib_preserve_only_values (void)
691 {
692 int i;
693
694 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
695 cselib_invalidate_regno (i, reg_raw_mode[i]);
696
697 cselib_invalidate_mem (callmem);
698
699 remove_useless_values ();
700
701 gcc_assert (first_containing_mem == &dummy_val);
702 }
703
704 /* Return the mode in which a register was last set. If X is not a
705 register, return its mode. If the mode in which the register was
706 set is not known, or the value was already clobbered, return
707 VOIDmode. */
708
709 enum machine_mode
710 cselib_reg_set_mode (const_rtx x)
711 {
712 if (!REG_P (x))
713 return GET_MODE (x);
714
715 if (REG_VALUES (REGNO (x)) == NULL
716 || REG_VALUES (REGNO (x))->elt == NULL)
717 return VOIDmode;
718
719 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
720 }
721
722 /* Return nonzero if we can prove that X and Y contain the same value, taking
723 our gathered information into account. */
724
725 int
726 rtx_equal_for_cselib_p (rtx x, rtx y)
727 {
728 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
729 }
730
731 /* If x is a PLUS or an autoinc operation, expand the operation,
732 storing the offset, if any, in *OFF. */
733
734 static rtx
735 autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
736 {
737 switch (GET_CODE (x))
738 {
739 case PLUS:
740 *off = XEXP (x, 1);
741 return XEXP (x, 0);
742
743 case PRE_DEC:
744 if (memmode == VOIDmode)
745 return x;
746
747 *off = GEN_INT (-GET_MODE_SIZE (memmode));
748 return XEXP (x, 0);
749 break;
750
751 case PRE_INC:
752 if (memmode == VOIDmode)
753 return x;
754
755 *off = GEN_INT (GET_MODE_SIZE (memmode));
756 return XEXP (x, 0);
757
758 case PRE_MODIFY:
759 return XEXP (x, 1);
760
761 case POST_DEC:
762 case POST_INC:
763 case POST_MODIFY:
764 return XEXP (x, 0);
765
766 default:
767 return x;
768 }
769 }
770
771 /* Return nonzero if we can prove that X and Y contain the same value,
772 taking our gathered information into account. MEMMODE holds the
773 mode of the enclosing MEM, if any, as required to deal with autoinc
774 addressing modes. If X and Y are not (known to be) part of
775 addresses, MEMMODE should be VOIDmode. */
776
777 static int
778 rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
779 {
780 enum rtx_code code;
781 const char *fmt;
782 int i;
783
784 if (REG_P (x) || MEM_P (x))
785 {
786 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
787
788 if (e)
789 x = e->val_rtx;
790 }
791
792 if (REG_P (y) || MEM_P (y))
793 {
794 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
795
796 if (e)
797 y = e->val_rtx;
798 }
799
800 if (x == y)
801 return 1;
802
803 if (GET_CODE (x) == VALUE)
804 {
805 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (x));
806 struct elt_loc_list *l;
807
808 if (GET_CODE (y) == VALUE)
809 return e == canonical_cselib_val (CSELIB_VAL_PTR (y));
810
811 for (l = e->locs; l; l = l->next)
812 {
813 rtx t = l->loc;
814
815 /* Avoid infinite recursion. We know we have the canonical
816 value, so we can just skip any values in the equivalence
817 list. */
818 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
819 continue;
820 else if (rtx_equal_for_cselib_1 (t, y, memmode))
821 return 1;
822 }
823
824 return 0;
825 }
826 else if (GET_CODE (y) == VALUE)
827 {
828 cselib_val *e = canonical_cselib_val (CSELIB_VAL_PTR (y));
829 struct elt_loc_list *l;
830
831 for (l = e->locs; l; l = l->next)
832 {
833 rtx t = l->loc;
834
835 if (REG_P (t) || MEM_P (t) || GET_CODE (t) == VALUE)
836 continue;
837 else if (rtx_equal_for_cselib_1 (x, t, memmode))
838 return 1;
839 }
840
841 return 0;
842 }
843
844 if (GET_MODE (x) != GET_MODE (y))
845 return 0;
846
847 if (GET_CODE (x) != GET_CODE (y))
848 {
849 rtx xorig = x, yorig = y;
850 rtx xoff = NULL, yoff = NULL;
851
852 x = autoinc_split (x, &xoff, memmode);
853 y = autoinc_split (y, &yoff, memmode);
854
855 if (!xoff != !yoff)
856 return 0;
857
858 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
859 return 0;
860
861 /* Don't recurse if nothing changed. */
862 if (x != xorig || y != yorig)
863 return rtx_equal_for_cselib_1 (x, y, memmode);
864
865 return 0;
866 }
867
868 /* These won't be handled correctly by the code below. */
869 switch (GET_CODE (x))
870 {
871 case CONST_DOUBLE:
872 case CONST_FIXED:
873 case DEBUG_EXPR:
874 return 0;
875
876 case DEBUG_IMPLICIT_PTR:
877 return DEBUG_IMPLICIT_PTR_DECL (x)
878 == DEBUG_IMPLICIT_PTR_DECL (y);
879
880 case DEBUG_PARAMETER_REF:
881 return DEBUG_PARAMETER_REF_DECL (x)
882 == DEBUG_PARAMETER_REF_DECL (y);
883
884 case ENTRY_VALUE:
885 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
886 use rtx_equal_for_cselib_1 to compare the operands. */
887 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
888
889 case LABEL_REF:
890 return XEXP (x, 0) == XEXP (y, 0);
891
892 case MEM:
893 /* We have to compare any autoinc operations in the addresses
894 using this MEM's mode. */
895 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
896
897 default:
898 break;
899 }
900
901 code = GET_CODE (x);
902 fmt = GET_RTX_FORMAT (code);
903
904 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
905 {
906 int j;
907
908 switch (fmt[i])
909 {
910 case 'w':
911 if (XWINT (x, i) != XWINT (y, i))
912 return 0;
913 break;
914
915 case 'n':
916 case 'i':
917 if (XINT (x, i) != XINT (y, i))
918 return 0;
919 break;
920
921 case 'V':
922 case 'E':
923 /* Two vectors must have the same length. */
924 if (XVECLEN (x, i) != XVECLEN (y, i))
925 return 0;
926
927 /* And the corresponding elements must match. */
928 for (j = 0; j < XVECLEN (x, i); j++)
929 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
930 XVECEXP (y, i, j), memmode))
931 return 0;
932 break;
933
934 case 'e':
935 if (i == 1
936 && targetm.commutative_p (x, UNKNOWN)
937 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
938 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
939 return 1;
940 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
941 return 0;
942 break;
943
944 case 'S':
945 case 's':
946 if (strcmp (XSTR (x, i), XSTR (y, i)))
947 return 0;
948 break;
949
950 case 'u':
951 /* These are just backpointers, so they don't matter. */
952 break;
953
954 case '0':
955 case 't':
956 break;
957
958 /* It is believed that rtx's at this level will never
959 contain anything but integers and other rtx's,
960 except for within LABEL_REFs and SYMBOL_REFs. */
961 default:
962 gcc_unreachable ();
963 }
964 }
965 return 1;
966 }
967
968 /* We need to pass down the mode of constants through the hash table
969 functions. For that purpose, wrap them in a CONST of the appropriate
970 mode. */
971 static rtx
972 wrap_constant (enum machine_mode mode, rtx x)
973 {
974 if (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
975 && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
976 return x;
977 gcc_assert (mode != VOIDmode);
978 return gen_rtx_CONST (mode, x);
979 }
980
981 /* Hash an rtx. Return 0 if we couldn't hash the rtx.
982 For registers and memory locations, we look up their cselib_val structure
983 and return its VALUE element.
984 Possible reasons for return 0 are: the object is volatile, or we couldn't
985 find a register or memory location in the table and CREATE is zero. If
986 CREATE is nonzero, table elts are created for regs and mem.
987 N.B. this hash function returns the same hash value for RTXes that
988 differ only in the order of operands, thus it is suitable for comparisons
989 that take commutativity into account.
990 If we wanted to also support associative rules, we'd have to use a different
991 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
992 MEMMODE indicates the mode of an enclosing MEM, and it's only
993 used to compute autoinc values.
994 We used to have a MODE argument for hashing for CONST_INTs, but that
995 didn't make sense, since it caused spurious hash differences between
996 (set (reg:SI 1) (const_int))
997 (plus:SI (reg:SI 2) (reg:SI 1))
998 and
999 (plus:SI (reg:SI 2) (const_int))
1000 If the mode is important in any context, it must be checked specifically
1001 in a comparison anyway, since relying on hash differences is unsafe. */
1002
1003 static unsigned int
1004 cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
1005 {
1006 cselib_val *e;
1007 int i, j;
1008 enum rtx_code code;
1009 const char *fmt;
1010 unsigned int hash = 0;
1011
1012 code = GET_CODE (x);
1013 hash += (unsigned) code + (unsigned) GET_MODE (x);
1014
1015 switch (code)
1016 {
1017 case MEM:
1018 case REG:
1019 e = cselib_lookup (x, GET_MODE (x), create, memmode);
1020 if (! e)
1021 return 0;
1022
1023 return e->hash;
1024
1025 case DEBUG_EXPR:
1026 hash += ((unsigned) DEBUG_EXPR << 7)
1027 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
1028 return hash ? hash : (unsigned int) DEBUG_EXPR;
1029
1030 case DEBUG_IMPLICIT_PTR:
1031 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1032 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1033 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1034
1035 case DEBUG_PARAMETER_REF:
1036 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1037 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1038 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1039
1040 case ENTRY_VALUE:
1041 /* ENTRY_VALUEs are function invariant, thus try to avoid
1042 recursing on argument if ENTRY_VALUE is one of the
1043 forms emitted by expand_debug_expr, otherwise
1044 ENTRY_VALUE hash would depend on the current value
1045 in some register or memory. */
1046 if (REG_P (ENTRY_VALUE_EXP (x)))
1047 hash += (unsigned int) REG
1048 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1049 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1050 else if (MEM_P (ENTRY_VALUE_EXP (x))
1051 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1052 hash += (unsigned int) MEM
1053 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1054 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1055 else
1056 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
1057 return hash ? hash : (unsigned int) ENTRY_VALUE;
1058
1059 case CONST_INT:
1060 hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
1061 return hash ? hash : (unsigned int) CONST_INT;
1062
1063 case CONST_DOUBLE:
1064 /* This is like the general case, except that it only counts
1065 the integers representing the constant. */
1066 hash += (unsigned) code + (unsigned) GET_MODE (x);
1067 if (GET_MODE (x) != VOIDmode)
1068 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
1069 else
1070 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1071 + (unsigned) CONST_DOUBLE_HIGH (x));
1072 return hash ? hash : (unsigned int) CONST_DOUBLE;
1073
1074 case CONST_FIXED:
1075 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1076 hash += fixed_hash (CONST_FIXED_VALUE (x));
1077 return hash ? hash : (unsigned int) CONST_FIXED;
1078
1079 case CONST_VECTOR:
1080 {
1081 int units;
1082 rtx elt;
1083
1084 units = CONST_VECTOR_NUNITS (x);
1085
1086 for (i = 0; i < units; ++i)
1087 {
1088 elt = CONST_VECTOR_ELT (x, i);
1089 hash += cselib_hash_rtx (elt, 0, memmode);
1090 }
1091
1092 return hash;
1093 }
1094
1095 /* Assume there is only one rtx object for any given label. */
1096 case LABEL_REF:
1097 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1098 differences and differences between each stage's debugging dumps. */
1099 hash += (((unsigned int) LABEL_REF << 7)
1100 + CODE_LABEL_NUMBER (XEXP (x, 0)));
1101 return hash ? hash : (unsigned int) LABEL_REF;
1102
1103 case SYMBOL_REF:
1104 {
1105 /* Don't hash on the symbol's address to avoid bootstrap differences.
1106 Different hash values may cause expressions to be recorded in
1107 different orders and thus different registers to be used in the
1108 final assembler. This also avoids differences in the dump files
1109 between various stages. */
1110 unsigned int h = 0;
1111 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1112
1113 while (*p)
1114 h += (h << 7) + *p++; /* ??? revisit */
1115
1116 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1117 return hash ? hash : (unsigned int) SYMBOL_REF;
1118 }
1119
1120 case PRE_DEC:
1121 case PRE_INC:
1122 /* We can't compute these without knowing the MEM mode. */
1123 gcc_assert (memmode != VOIDmode);
1124 i = GET_MODE_SIZE (memmode);
1125 if (code == PRE_DEC)
1126 i = -i;
1127 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1128 like (mem:MEMMODE (plus (reg) (const_int I))). */
1129 hash += (unsigned) PLUS - (unsigned)code
1130 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1131 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1132 return hash ? hash : 1 + (unsigned) PLUS;
1133
1134 case PRE_MODIFY:
1135 gcc_assert (memmode != VOIDmode);
1136 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1137
1138 case POST_DEC:
1139 case POST_INC:
1140 case POST_MODIFY:
1141 gcc_assert (memmode != VOIDmode);
1142 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1143
1144 case PC:
1145 case CC0:
1146 case CALL:
1147 case UNSPEC_VOLATILE:
1148 return 0;
1149
1150 case ASM_OPERANDS:
1151 if (MEM_VOLATILE_P (x))
1152 return 0;
1153
1154 break;
1155
1156 default:
1157 break;
1158 }
1159
1160 i = GET_RTX_LENGTH (code) - 1;
1161 fmt = GET_RTX_FORMAT (code);
1162 for (; i >= 0; i--)
1163 {
1164 switch (fmt[i])
1165 {
1166 case 'e':
1167 {
1168 rtx tem = XEXP (x, i);
1169 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1170
1171 if (tem_hash == 0)
1172 return 0;
1173
1174 hash += tem_hash;
1175 }
1176 break;
1177 case 'E':
1178 for (j = 0; j < XVECLEN (x, i); j++)
1179 {
1180 unsigned int tem_hash
1181 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1182
1183 if (tem_hash == 0)
1184 return 0;
1185
1186 hash += tem_hash;
1187 }
1188 break;
1189
1190 case 's':
1191 {
1192 const unsigned char *p = (const unsigned char *) XSTR (x, i);
1193
1194 if (p)
1195 while (*p)
1196 hash += *p++;
1197 break;
1198 }
1199
1200 case 'i':
1201 hash += XINT (x, i);
1202 break;
1203
1204 case '0':
1205 case 't':
1206 /* unused */
1207 break;
1208
1209 default:
1210 gcc_unreachable ();
1211 }
1212 }
1213
1214 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1215 }
1216
1217 /* Create a new value structure for VALUE and initialize it. The mode of the
1218 value is MODE. */
1219
1220 static inline cselib_val *
1221 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1222 {
1223 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1224
1225 gcc_assert (hash);
1226 gcc_assert (next_uid);
1227
1228 e->hash = hash;
1229 e->uid = next_uid++;
1230 /* We use an alloc pool to allocate this RTL construct because it
1231 accounts for about 8% of the overall memory usage. We know
1232 precisely when we can have VALUE RTXen (when cselib is active)
1233 so we don't need to put them in garbage collected memory.
1234 ??? Why should a VALUE be an RTX in the first place? */
1235 e->val_rtx = (rtx) pool_alloc (value_pool);
1236 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1237 PUT_CODE (e->val_rtx, VALUE);
1238 PUT_MODE (e->val_rtx, mode);
1239 CSELIB_VAL_PTR (e->val_rtx) = e;
1240 e->addr_list = 0;
1241 e->locs = 0;
1242 e->next_containing_mem = 0;
1243
1244 if (dump_file && (dump_flags & TDF_CSELIB))
1245 {
1246 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1247 if (flag_dump_noaddr || flag_dump_unnumbered)
1248 fputs ("# ", dump_file);
1249 else
1250 fprintf (dump_file, "%p ", (void*)e);
1251 print_rtl_single (dump_file, x);
1252 fputc ('\n', dump_file);
1253 }
1254
1255 return e;
1256 }
1257
1258 /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1259 contains the data at this address. X is a MEM that represents the
1260 value. Update the two value structures to represent this situation. */
1261
1262 static void
1263 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1264 {
1265 struct elt_loc_list *l;
1266
1267 /* Avoid duplicates. */
1268 for (l = mem_elt->locs; l; l = l->next)
1269 if (MEM_P (l->loc)
1270 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1271 {
1272 promote_debug_loc (l);
1273 return;
1274 }
1275
1276 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1277 new_elt_loc_list (mem_elt,
1278 replace_equiv_address_nv (x, addr_elt->val_rtx));
1279 if (mem_elt->next_containing_mem == NULL)
1280 {
1281 mem_elt->next_containing_mem = first_containing_mem;
1282 first_containing_mem = mem_elt;
1283 }
1284 }
1285
1286 /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1287 If CREATE, make a new one if we haven't seen it before. */
1288
1289 static cselib_val *
1290 cselib_lookup_mem (rtx x, int create)
1291 {
1292 enum machine_mode mode = GET_MODE (x);
1293 enum machine_mode addr_mode;
1294 void **slot;
1295 cselib_val *addr;
1296 cselib_val *mem_elt;
1297 struct elt_list *l;
1298
1299 if (MEM_VOLATILE_P (x) || mode == BLKmode
1300 || !cselib_record_memory
1301 || (FLOAT_MODE_P (mode) && flag_float_store))
1302 return 0;
1303
1304 addr_mode = GET_MODE (XEXP (x, 0));
1305 if (addr_mode == VOIDmode)
1306 addr_mode = Pmode;
1307
1308 /* Look up the value for the address. */
1309 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1310 if (! addr)
1311 return 0;
1312
1313 /* Find a value that describes a value of our mode at that address. */
1314 for (l = addr->addr_list; l; l = l->next)
1315 if (GET_MODE (l->elt->val_rtx) == mode)
1316 {
1317 promote_debug_loc (l->elt->locs);
1318 return l->elt;
1319 }
1320
1321 if (! create)
1322 return 0;
1323
1324 mem_elt = new_cselib_val (next_uid, mode, x);
1325 add_mem_for_addr (addr, mem_elt, x);
1326 slot = cselib_find_slot (wrap_constant (mode, x), mem_elt->hash,
1327 INSERT, mode);
1328 *slot = mem_elt;
1329 return mem_elt;
1330 }
1331
1332 /* Search thru the possible substitutions in P. We prefer a non reg
1333 substitution because this allows us to expand the tree further. If
1334 we find, just a reg, take the lowest regno. There may be several
1335 non-reg results, we just take the first one because they will all
1336 expand to the same place. */
1337
1338 static rtx
1339 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1340 int max_depth)
1341 {
1342 rtx reg_result = NULL;
1343 unsigned int regno = UINT_MAX;
1344 struct elt_loc_list *p_in = p;
1345
1346 for (; p; p = p -> next)
1347 {
1348 /* Avoid infinite recursion trying to expand a reg into a
1349 the same reg. */
1350 if ((REG_P (p->loc))
1351 && (REGNO (p->loc) < regno)
1352 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1353 {
1354 reg_result = p->loc;
1355 regno = REGNO (p->loc);
1356 }
1357 /* Avoid infinite recursion and do not try to expand the
1358 value. */
1359 else if (GET_CODE (p->loc) == VALUE
1360 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1361 continue;
1362 else if (!REG_P (p->loc))
1363 {
1364 rtx result, note;
1365 if (dump_file && (dump_flags & TDF_CSELIB))
1366 {
1367 print_inline_rtx (dump_file, p->loc, 0);
1368 fprintf (dump_file, "\n");
1369 }
1370 if (GET_CODE (p->loc) == LO_SUM
1371 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1372 && p->setting_insn
1373 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1374 && XEXP (note, 0) == XEXP (p->loc, 1))
1375 return XEXP (p->loc, 1);
1376 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1377 if (result)
1378 return result;
1379 }
1380
1381 }
1382
1383 if (regno != UINT_MAX)
1384 {
1385 rtx result;
1386 if (dump_file && (dump_flags & TDF_CSELIB))
1387 fprintf (dump_file, "r%d\n", regno);
1388
1389 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1390 if (result)
1391 return result;
1392 }
1393
1394 if (dump_file && (dump_flags & TDF_CSELIB))
1395 {
1396 if (reg_result)
1397 {
1398 print_inline_rtx (dump_file, reg_result, 0);
1399 fprintf (dump_file, "\n");
1400 }
1401 else
1402 fprintf (dump_file, "NULL\n");
1403 }
1404 return reg_result;
1405 }
1406
1407
1408 /* Forward substitute and expand an expression out to its roots.
1409 This is the opposite of common subexpression. Because local value
1410 numbering is such a weak optimization, the expanded expression is
1411 pretty much unique (not from a pointer equals point of view but
1412 from a tree shape point of view.
1413
1414 This function returns NULL if the expansion fails. The expansion
1415 will fail if there is no value number for one of the operands or if
1416 one of the operands has been overwritten between the current insn
1417 and the beginning of the basic block. For instance x has no
1418 expansion in:
1419
1420 r1 <- r1 + 3
1421 x <- r1 + 8
1422
1423 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1424 It is clear on return. */
1425
1426 rtx
1427 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1428 {
1429 struct expand_value_data evd;
1430
1431 evd.regs_active = regs_active;
1432 evd.callback = NULL;
1433 evd.callback_arg = NULL;
1434 evd.dummy = false;
1435
1436 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1437 }
1438
1439 /* Same as cselib_expand_value_rtx, but using a callback to try to
1440 resolve some expressions. The CB function should return ORIG if it
1441 can't or does not want to deal with a certain RTX. Any other
1442 return value, including NULL, will be used as the expansion for
1443 VALUE, without any further changes. */
1444
1445 rtx
1446 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1447 cselib_expand_callback cb, void *data)
1448 {
1449 struct expand_value_data evd;
1450
1451 evd.regs_active = regs_active;
1452 evd.callback = cb;
1453 evd.callback_arg = data;
1454 evd.dummy = false;
1455
1456 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1457 }
1458
1459 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1460 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1461 would return NULL or non-NULL, without allocating new rtx. */
1462
1463 bool
1464 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1465 cselib_expand_callback cb, void *data)
1466 {
1467 struct expand_value_data evd;
1468
1469 evd.regs_active = regs_active;
1470 evd.callback = cb;
1471 evd.callback_arg = data;
1472 evd.dummy = true;
1473
1474 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1475 }
1476
1477 /* Internal implementation of cselib_expand_value_rtx and
1478 cselib_expand_value_rtx_cb. */
1479
1480 static rtx
1481 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1482 int max_depth)
1483 {
1484 rtx copy, scopy;
1485 int i, j;
1486 RTX_CODE code;
1487 const char *format_ptr;
1488 enum machine_mode mode;
1489
1490 code = GET_CODE (orig);
1491
1492 /* For the context of dse, if we end up expand into a huge tree, we
1493 will not have a useful address, so we might as well just give up
1494 quickly. */
1495 if (max_depth <= 0)
1496 return NULL;
1497
1498 switch (code)
1499 {
1500 case REG:
1501 {
1502 struct elt_list *l = REG_VALUES (REGNO (orig));
1503
1504 if (l && l->elt == NULL)
1505 l = l->next;
1506 for (; l; l = l->next)
1507 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1508 {
1509 rtx result;
1510 unsigned regno = REGNO (orig);
1511
1512 /* The only thing that we are not willing to do (this
1513 is requirement of dse and if others potential uses
1514 need this function we should add a parm to control
1515 it) is that we will not substitute the
1516 STACK_POINTER_REGNUM, FRAME_POINTER or the
1517 HARD_FRAME_POINTER.
1518
1519 These expansions confuses the code that notices that
1520 stores into the frame go dead at the end of the
1521 function and that the frame is not effected by calls
1522 to subroutines. If you allow the
1523 STACK_POINTER_REGNUM substitution, then dse will
1524 think that parameter pushing also goes dead which is
1525 wrong. If you allow the FRAME_POINTER or the
1526 HARD_FRAME_POINTER then you lose the opportunity to
1527 make the frame assumptions. */
1528 if (regno == STACK_POINTER_REGNUM
1529 || regno == FRAME_POINTER_REGNUM
1530 || regno == HARD_FRAME_POINTER_REGNUM
1531 || regno == cfa_base_preserved_regno)
1532 return orig;
1533
1534 bitmap_set_bit (evd->regs_active, regno);
1535
1536 if (dump_file && (dump_flags & TDF_CSELIB))
1537 fprintf (dump_file, "expanding: r%d into: ", regno);
1538
1539 result = expand_loc (l->elt->locs, evd, max_depth);
1540 bitmap_clear_bit (evd->regs_active, regno);
1541
1542 if (result)
1543 return result;
1544 else
1545 return orig;
1546 }
1547 }
1548
1549 case CONST_INT:
1550 case CONST_DOUBLE:
1551 case CONST_VECTOR:
1552 case SYMBOL_REF:
1553 case CODE_LABEL:
1554 case PC:
1555 case CC0:
1556 case SCRATCH:
1557 /* SCRATCH must be shared because they represent distinct values. */
1558 return orig;
1559 case CLOBBER:
1560 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1561 return orig;
1562 break;
1563
1564 case CONST:
1565 if (shared_const_p (orig))
1566 return orig;
1567 break;
1568
1569 case SUBREG:
1570 {
1571 rtx subreg;
1572
1573 if (evd->callback)
1574 {
1575 subreg = evd->callback (orig, evd->regs_active, max_depth,
1576 evd->callback_arg);
1577 if (subreg != orig)
1578 return subreg;
1579 }
1580
1581 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1582 max_depth - 1);
1583 if (!subreg)
1584 return NULL;
1585 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1586 GET_MODE (SUBREG_REG (orig)),
1587 SUBREG_BYTE (orig));
1588 if (scopy == NULL
1589 || (GET_CODE (scopy) == SUBREG
1590 && !REG_P (SUBREG_REG (scopy))
1591 && !MEM_P (SUBREG_REG (scopy))))
1592 return NULL;
1593
1594 return scopy;
1595 }
1596
1597 case VALUE:
1598 {
1599 rtx result;
1600
1601 if (dump_file && (dump_flags & TDF_CSELIB))
1602 {
1603 fputs ("\nexpanding ", dump_file);
1604 print_rtl_single (dump_file, orig);
1605 fputs (" into...", dump_file);
1606 }
1607
1608 if (evd->callback)
1609 {
1610 result = evd->callback (orig, evd->regs_active, max_depth,
1611 evd->callback_arg);
1612
1613 if (result != orig)
1614 return result;
1615 }
1616
1617 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1618 return result;
1619 }
1620
1621 case DEBUG_EXPR:
1622 if (evd->callback)
1623 return evd->callback (orig, evd->regs_active, max_depth,
1624 evd->callback_arg);
1625 return orig;
1626
1627 default:
1628 break;
1629 }
1630
1631 /* Copy the various flags, fields, and other information. We assume
1632 that all fields need copying, and then clear the fields that should
1633 not be copied. That is the sensible default behavior, and forces
1634 us to explicitly document why we are *not* copying a flag. */
1635 if (evd->dummy)
1636 copy = NULL;
1637 else
1638 copy = shallow_copy_rtx (orig);
1639
1640 format_ptr = GET_RTX_FORMAT (code);
1641
1642 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1643 switch (*format_ptr++)
1644 {
1645 case 'e':
1646 if (XEXP (orig, i) != NULL)
1647 {
1648 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1649 max_depth - 1);
1650 if (!result)
1651 return NULL;
1652 if (copy)
1653 XEXP (copy, i) = result;
1654 }
1655 break;
1656
1657 case 'E':
1658 case 'V':
1659 if (XVEC (orig, i) != NULL)
1660 {
1661 if (copy)
1662 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1663 for (j = 0; j < XVECLEN (orig, i); j++)
1664 {
1665 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1666 evd, max_depth - 1);
1667 if (!result)
1668 return NULL;
1669 if (copy)
1670 XVECEXP (copy, i, j) = result;
1671 }
1672 }
1673 break;
1674
1675 case 't':
1676 case 'w':
1677 case 'i':
1678 case 's':
1679 case 'S':
1680 case 'T':
1681 case 'u':
1682 case 'B':
1683 case '0':
1684 /* These are left unchanged. */
1685 break;
1686
1687 default:
1688 gcc_unreachable ();
1689 }
1690
1691 if (evd->dummy)
1692 return orig;
1693
1694 mode = GET_MODE (copy);
1695 /* If an operand has been simplified into CONST_INT, which doesn't
1696 have a mode and the mode isn't derivable from whole rtx's mode,
1697 try simplify_*_operation first with mode from original's operand
1698 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1699 scopy = copy;
1700 switch (GET_RTX_CLASS (code))
1701 {
1702 case RTX_UNARY:
1703 if (CONST_INT_P (XEXP (copy, 0))
1704 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1705 {
1706 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1707 GET_MODE (XEXP (orig, 0)));
1708 if (scopy)
1709 return scopy;
1710 }
1711 break;
1712 case RTX_COMM_ARITH:
1713 case RTX_BIN_ARITH:
1714 /* These expressions can derive operand modes from the whole rtx's mode. */
1715 break;
1716 case RTX_TERNARY:
1717 case RTX_BITFIELD_OPS:
1718 if (CONST_INT_P (XEXP (copy, 0))
1719 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1720 {
1721 scopy = simplify_ternary_operation (code, mode,
1722 GET_MODE (XEXP (orig, 0)),
1723 XEXP (copy, 0), XEXP (copy, 1),
1724 XEXP (copy, 2));
1725 if (scopy)
1726 return scopy;
1727 }
1728 break;
1729 case RTX_COMPARE:
1730 case RTX_COMM_COMPARE:
1731 if (CONST_INT_P (XEXP (copy, 0))
1732 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1733 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1734 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1735 {
1736 scopy = simplify_relational_operation (code, mode,
1737 (GET_MODE (XEXP (orig, 0))
1738 != VOIDmode)
1739 ? GET_MODE (XEXP (orig, 0))
1740 : GET_MODE (XEXP (orig, 1)),
1741 XEXP (copy, 0),
1742 XEXP (copy, 1));
1743 if (scopy)
1744 return scopy;
1745 }
1746 break;
1747 default:
1748 break;
1749 }
1750 scopy = simplify_rtx (copy);
1751 if (scopy)
1752 return scopy;
1753 return copy;
1754 }
1755
1756 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1757 with VALUE expressions. This way, it becomes independent of changes
1758 to registers and memory.
1759 X isn't actually modified; if modifications are needed, new rtl is
1760 allocated. However, the return value can share rtl with X.
1761 If X is within a MEM, MEMMODE must be the mode of the MEM. */
1762
1763 rtx
1764 cselib_subst_to_values (rtx x, enum machine_mode memmode)
1765 {
1766 enum rtx_code code = GET_CODE (x);
1767 const char *fmt = GET_RTX_FORMAT (code);
1768 cselib_val *e;
1769 struct elt_list *l;
1770 rtx copy = x;
1771 int i;
1772
1773 switch (code)
1774 {
1775 case REG:
1776 l = REG_VALUES (REGNO (x));
1777 if (l && l->elt == NULL)
1778 l = l->next;
1779 for (; l; l = l->next)
1780 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1781 return l->elt->val_rtx;
1782
1783 gcc_unreachable ();
1784
1785 case MEM:
1786 e = cselib_lookup_mem (x, 0);
1787 /* This used to happen for autoincrements, but we deal with them
1788 properly now. Remove the if stmt for the next release. */
1789 if (! e)
1790 {
1791 /* Assign a value that doesn't match any other. */
1792 e = new_cselib_val (next_uid, GET_MODE (x), x);
1793 }
1794 return e->val_rtx;
1795
1796 case ENTRY_VALUE:
1797 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1798 if (! e)
1799 break;
1800 return e->val_rtx;
1801
1802 case CONST_DOUBLE:
1803 case CONST_VECTOR:
1804 case CONST_INT:
1805 case CONST_FIXED:
1806 return x;
1807
1808 case PRE_DEC:
1809 case PRE_INC:
1810 gcc_assert (memmode != VOIDmode);
1811 i = GET_MODE_SIZE (memmode);
1812 if (code == PRE_DEC)
1813 i = -i;
1814 return cselib_subst_to_values (plus_constant (XEXP (x, 0), i),
1815 memmode);
1816
1817 case PRE_MODIFY:
1818 gcc_assert (memmode != VOIDmode);
1819 return cselib_subst_to_values (XEXP (x, 1), memmode);
1820
1821 case POST_DEC:
1822 case POST_INC:
1823 case POST_MODIFY:
1824 gcc_assert (memmode != VOIDmode);
1825 return cselib_subst_to_values (XEXP (x, 0), memmode);
1826
1827 default:
1828 break;
1829 }
1830
1831 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1832 {
1833 if (fmt[i] == 'e')
1834 {
1835 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1836
1837 if (t != XEXP (x, i))
1838 {
1839 if (x == copy)
1840 copy = shallow_copy_rtx (x);
1841 XEXP (copy, i) = t;
1842 }
1843 }
1844 else if (fmt[i] == 'E')
1845 {
1846 int j;
1847
1848 for (j = 0; j < XVECLEN (x, i); j++)
1849 {
1850 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1851
1852 if (t != XVECEXP (x, i, j))
1853 {
1854 if (XVEC (x, i) == XVEC (copy, i))
1855 {
1856 if (x == copy)
1857 copy = shallow_copy_rtx (x);
1858 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1859 }
1860 XVECEXP (copy, i, j) = t;
1861 }
1862 }
1863 }
1864 }
1865
1866 return copy;
1867 }
1868
1869 /* Look up the rtl expression X in our tables and return the value it
1870 has. If CREATE is zero, we return NULL if we don't know the value.
1871 Otherwise, we create a new one if possible, using mode MODE if X
1872 doesn't have a mode (i.e. because it's a constant). When X is part
1873 of an address, MEMMODE should be the mode of the enclosing MEM if
1874 we're tracking autoinc expressions. */
1875
1876 static cselib_val *
1877 cselib_lookup_1 (rtx x, enum machine_mode mode,
1878 int create, enum machine_mode memmode)
1879 {
1880 void **slot;
1881 cselib_val *e;
1882 unsigned int hashval;
1883
1884 if (GET_MODE (x) != VOIDmode)
1885 mode = GET_MODE (x);
1886
1887 if (GET_CODE (x) == VALUE)
1888 return CSELIB_VAL_PTR (x);
1889
1890 if (REG_P (x))
1891 {
1892 struct elt_list *l;
1893 unsigned int i = REGNO (x);
1894
1895 l = REG_VALUES (i);
1896 if (l && l->elt == NULL)
1897 l = l->next;
1898 for (; l; l = l->next)
1899 if (mode == GET_MODE (l->elt->val_rtx))
1900 {
1901 promote_debug_loc (l->elt->locs);
1902 return l->elt;
1903 }
1904
1905 if (! create)
1906 return 0;
1907
1908 if (i < FIRST_PSEUDO_REGISTER)
1909 {
1910 unsigned int n = hard_regno_nregs[i][mode];
1911
1912 if (n > max_value_regs)
1913 max_value_regs = n;
1914 }
1915
1916 e = new_cselib_val (next_uid, GET_MODE (x), x);
1917 new_elt_loc_list (e, x);
1918 if (REG_VALUES (i) == 0)
1919 {
1920 /* Maintain the invariant that the first entry of
1921 REG_VALUES, if present, must be the value used to set the
1922 register, or NULL. */
1923 used_regs[n_used_regs++] = i;
1924 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
1925 }
1926 else if (cselib_preserve_constants
1927 && GET_MODE_CLASS (mode) == MODE_INT)
1928 {
1929 /* During var-tracking, try harder to find equivalences
1930 for SUBREGs. If a setter sets say a DImode register
1931 and user uses that register only in SImode, add a lowpart
1932 subreg location. */
1933 struct elt_list *lwider = NULL;
1934 l = REG_VALUES (i);
1935 if (l && l->elt == NULL)
1936 l = l->next;
1937 for (; l; l = l->next)
1938 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
1939 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
1940 > GET_MODE_SIZE (mode)
1941 && (lwider == NULL
1942 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
1943 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
1944 {
1945 struct elt_loc_list *el;
1946 if (i < FIRST_PSEUDO_REGISTER
1947 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
1948 continue;
1949 for (el = l->elt->locs; el; el = el->next)
1950 if (!REG_P (el->loc))
1951 break;
1952 if (el)
1953 lwider = l;
1954 }
1955 if (lwider)
1956 {
1957 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
1958 GET_MODE (lwider->elt->val_rtx));
1959 if (sub)
1960 new_elt_loc_list (e, sub);
1961 }
1962 }
1963 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
1964 slot = cselib_find_slot (x, e->hash, INSERT, memmode);
1965 *slot = e;
1966 return e;
1967 }
1968
1969 if (MEM_P (x))
1970 return cselib_lookup_mem (x, create);
1971
1972 hashval = cselib_hash_rtx (x, create, memmode);
1973 /* Can't even create if hashing is not possible. */
1974 if (! hashval)
1975 return 0;
1976
1977 slot = cselib_find_slot (wrap_constant (mode, x), hashval,
1978 create ? INSERT : NO_INSERT, memmode);
1979 if (slot == 0)
1980 return 0;
1981
1982 e = (cselib_val *) *slot;
1983 if (e)
1984 return e;
1985
1986 e = new_cselib_val (hashval, mode, x);
1987
1988 /* We have to fill the slot before calling cselib_subst_to_values:
1989 the hash table is inconsistent until we do so, and
1990 cselib_subst_to_values will need to do lookups. */
1991 *slot = (void *) e;
1992 new_elt_loc_list (e, cselib_subst_to_values (x, memmode));
1993 return e;
1994 }
1995
1996 /* Wrapper for cselib_lookup, that indicates X is in INSN. */
1997
1998 cselib_val *
1999 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
2000 int create, enum machine_mode memmode, rtx insn)
2001 {
2002 cselib_val *ret;
2003
2004 gcc_assert (!cselib_current_insn);
2005 cselib_current_insn = insn;
2006
2007 ret = cselib_lookup (x, mode, create, memmode);
2008
2009 cselib_current_insn = NULL;
2010
2011 return ret;
2012 }
2013
2014 /* Wrapper for cselib_lookup_1, that logs the lookup result and
2015 maintains invariants related with debug insns. */
2016
2017 cselib_val *
2018 cselib_lookup (rtx x, enum machine_mode mode,
2019 int create, enum machine_mode memmode)
2020 {
2021 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
2022
2023 /* ??? Should we return NULL if we're not to create an entry, the
2024 found loc is a debug loc and cselib_current_insn is not DEBUG?
2025 If so, we should also avoid converting val to non-DEBUG; probably
2026 easiest setting cselib_current_insn to NULL before the call
2027 above. */
2028
2029 if (dump_file && (dump_flags & TDF_CSELIB))
2030 {
2031 fputs ("cselib lookup ", dump_file);
2032 print_inline_rtx (dump_file, x, 2);
2033 fprintf (dump_file, " => %u:%u\n",
2034 ret ? ret->uid : 0,
2035 ret ? ret->hash : 0);
2036 }
2037
2038 return ret;
2039 }
2040
2041 /* Invalidate any entries in reg_values that overlap REGNO. This is called
2042 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2043 is used to determine how many hard registers are being changed. If MODE
2044 is VOIDmode, then only REGNO is being changed; this is used when
2045 invalidating call clobbered registers across a call. */
2046
2047 static void
2048 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
2049 {
2050 unsigned int endregno;
2051 unsigned int i;
2052
2053 /* If we see pseudos after reload, something is _wrong_. */
2054 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2055 || reg_renumber[regno] < 0);
2056
2057 /* Determine the range of registers that must be invalidated. For
2058 pseudos, only REGNO is affected. For hard regs, we must take MODE
2059 into account, and we must also invalidate lower register numbers
2060 if they contain values that overlap REGNO. */
2061 if (regno < FIRST_PSEUDO_REGISTER)
2062 {
2063 gcc_assert (mode != VOIDmode);
2064
2065 if (regno < max_value_regs)
2066 i = 0;
2067 else
2068 i = regno - max_value_regs;
2069
2070 endregno = end_hard_regno (mode, regno);
2071 }
2072 else
2073 {
2074 i = regno;
2075 endregno = regno + 1;
2076 }
2077
2078 for (; i < endregno; i++)
2079 {
2080 struct elt_list **l = &REG_VALUES (i);
2081
2082 /* Go through all known values for this reg; if it overlaps the range
2083 we're invalidating, remove the value. */
2084 while (*l)
2085 {
2086 cselib_val *v = (*l)->elt;
2087 bool had_locs;
2088 rtx setting_insn;
2089 struct elt_loc_list **p;
2090 unsigned int this_last = i;
2091
2092 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
2093 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
2094
2095 if (this_last < regno || v == NULL
2096 || (v == cfa_base_preserved_val
2097 && i == cfa_base_preserved_regno))
2098 {
2099 l = &(*l)->next;
2100 continue;
2101 }
2102
2103 /* We have an overlap. */
2104 if (*l == REG_VALUES (i))
2105 {
2106 /* Maintain the invariant that the first entry of
2107 REG_VALUES, if present, must be the value used to set
2108 the register, or NULL. This is also nice because
2109 then we won't push the same regno onto user_regs
2110 multiple times. */
2111 (*l)->elt = NULL;
2112 l = &(*l)->next;
2113 }
2114 else
2115 unchain_one_elt_list (l);
2116
2117 v = canonical_cselib_val (v);
2118
2119 had_locs = v->locs != NULL;
2120 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2121
2122 /* Now, we clear the mapping from value to reg. It must exist, so
2123 this code will crash intentionally if it doesn't. */
2124 for (p = &v->locs; ; p = &(*p)->next)
2125 {
2126 rtx x = (*p)->loc;
2127
2128 if (REG_P (x) && REGNO (x) == i)
2129 {
2130 unchain_one_elt_loc_list (p);
2131 break;
2132 }
2133 }
2134
2135 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2136 {
2137 if (setting_insn && DEBUG_INSN_P (setting_insn))
2138 n_useless_debug_values++;
2139 else
2140 n_useless_values++;
2141 }
2142 }
2143 }
2144 }
2145 \f
2146 /* Return 1 if X has a value that can vary even between two
2147 executions of the program. 0 means X can be compared reliably
2148 against certain constants or near-constants. */
2149
2150 static bool
2151 cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED, bool from_alias ATTRIBUTE_UNUSED)
2152 {
2153 /* We actually don't need to verify very hard. This is because
2154 if X has actually changed, we invalidate the memory anyway,
2155 so assume that all common memory addresses are
2156 invariant. */
2157 return 0;
2158 }
2159
2160 /* Invalidate any locations in the table which are changed because of a
2161 store to MEM_RTX. If this is called because of a non-const call
2162 instruction, MEM_RTX is (mem:BLK const0_rtx). */
2163
2164 static void
2165 cselib_invalidate_mem (rtx mem_rtx)
2166 {
2167 cselib_val **vp, *v, *next;
2168 int num_mems = 0;
2169 rtx mem_addr;
2170
2171 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2172 mem_rtx = canon_rtx (mem_rtx);
2173
2174 vp = &first_containing_mem;
2175 for (v = *vp; v != &dummy_val; v = next)
2176 {
2177 bool has_mem = false;
2178 struct elt_loc_list **p = &v->locs;
2179 bool had_locs = v->locs != NULL;
2180 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2181
2182 while (*p)
2183 {
2184 rtx x = (*p)->loc;
2185 cselib_val *addr;
2186 struct elt_list **mem_chain;
2187
2188 /* MEMs may occur in locations only at the top level; below
2189 that every MEM or REG is substituted by its VALUE. */
2190 if (!MEM_P (x))
2191 {
2192 p = &(*p)->next;
2193 continue;
2194 }
2195 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2196 && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr,
2197 x, NULL_RTX, cselib_rtx_varies_p))
2198 {
2199 has_mem = true;
2200 num_mems++;
2201 p = &(*p)->next;
2202 continue;
2203 }
2204
2205 /* This one overlaps. */
2206 /* We must have a mapping from this MEM's address to the
2207 value (E). Remove that, too. */
2208 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2209 mem_chain = &addr->addr_list;
2210 for (;;)
2211 {
2212 if ((*mem_chain)->elt == v)
2213 {
2214 unchain_one_elt_list (mem_chain);
2215 break;
2216 }
2217
2218 mem_chain = &(*mem_chain)->next;
2219 }
2220
2221 unchain_one_elt_loc_list (p);
2222 }
2223
2224 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2225 {
2226 if (setting_insn && DEBUG_INSN_P (setting_insn))
2227 n_useless_debug_values++;
2228 else
2229 n_useless_values++;
2230 }
2231
2232 next = v->next_containing_mem;
2233 if (has_mem)
2234 {
2235 *vp = v;
2236 vp = &(*vp)->next_containing_mem;
2237 }
2238 else
2239 v->next_containing_mem = NULL;
2240 }
2241 *vp = &dummy_val;
2242 }
2243
2244 /* Invalidate DEST, which is being assigned to or clobbered. */
2245
2246 void
2247 cselib_invalidate_rtx (rtx dest)
2248 {
2249 while (GET_CODE (dest) == SUBREG
2250 || GET_CODE (dest) == ZERO_EXTRACT
2251 || GET_CODE (dest) == STRICT_LOW_PART)
2252 dest = XEXP (dest, 0);
2253
2254 if (REG_P (dest))
2255 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2256 else if (MEM_P (dest))
2257 cselib_invalidate_mem (dest);
2258 }
2259
2260 /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2261
2262 static void
2263 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2264 void *data ATTRIBUTE_UNUSED)
2265 {
2266 cselib_invalidate_rtx (dest);
2267 }
2268
2269 /* Record the result of a SET instruction. DEST is being set; the source
2270 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2271 describes its address. */
2272
2273 static void
2274 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2275 {
2276 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2277
2278 if (src_elt == 0 || side_effects_p (dest))
2279 return;
2280
2281 if (dreg >= 0)
2282 {
2283 if (dreg < FIRST_PSEUDO_REGISTER)
2284 {
2285 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2286
2287 if (n > max_value_regs)
2288 max_value_regs = n;
2289 }
2290
2291 if (REG_VALUES (dreg) == 0)
2292 {
2293 used_regs[n_used_regs++] = dreg;
2294 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2295 }
2296 else
2297 {
2298 /* The register should have been invalidated. */
2299 gcc_assert (REG_VALUES (dreg)->elt == 0);
2300 REG_VALUES (dreg)->elt = src_elt;
2301 }
2302
2303 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2304 n_useless_values--;
2305 new_elt_loc_list (src_elt, dest);
2306 }
2307 else if (MEM_P (dest) && dest_addr_elt != 0
2308 && cselib_record_memory)
2309 {
2310 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2311 n_useless_values--;
2312 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2313 }
2314 }
2315
2316 /* Make ELT and X's VALUE equivalent to each other at INSN. */
2317
2318 void
2319 cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx insn)
2320 {
2321 cselib_val *nelt;
2322 rtx save_cselib_current_insn = cselib_current_insn;
2323
2324 gcc_checking_assert (elt);
2325 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2326 gcc_checking_assert (!side_effects_p (x));
2327
2328 cselib_current_insn = insn;
2329
2330 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2331
2332 if (nelt != elt)
2333 {
2334 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2335 cselib_preserve_value (nelt);
2336
2337 new_elt_loc_list (nelt, elt->val_rtx);
2338 }
2339
2340 cselib_current_insn = save_cselib_current_insn;
2341 }
2342
2343 /* There is no good way to determine how many elements there can be
2344 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2345 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2346
2347 struct cselib_record_autoinc_data
2348 {
2349 struct cselib_set *sets;
2350 int n_sets;
2351 };
2352
2353 /* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2354 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2355
2356 static int
2357 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2358 rtx dest, rtx src, rtx srcoff, void *arg)
2359 {
2360 struct cselib_record_autoinc_data *data;
2361 data = (struct cselib_record_autoinc_data *)arg;
2362
2363 data->sets[data->n_sets].dest = dest;
2364
2365 if (srcoff)
2366 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2367 else
2368 data->sets[data->n_sets].src = src;
2369
2370 data->n_sets++;
2371
2372 return -1;
2373 }
2374
2375 /* Record the effects of any sets and autoincs in INSN. */
2376 static void
2377 cselib_record_sets (rtx insn)
2378 {
2379 int n_sets = 0;
2380 int i;
2381 struct cselib_set sets[MAX_SETS];
2382 rtx body = PATTERN (insn);
2383 rtx cond = 0;
2384 int n_sets_before_autoinc;
2385 struct cselib_record_autoinc_data data;
2386
2387 body = PATTERN (insn);
2388 if (GET_CODE (body) == COND_EXEC)
2389 {
2390 cond = COND_EXEC_TEST (body);
2391 body = COND_EXEC_CODE (body);
2392 }
2393
2394 /* Find all sets. */
2395 if (GET_CODE (body) == SET)
2396 {
2397 sets[0].src = SET_SRC (body);
2398 sets[0].dest = SET_DEST (body);
2399 n_sets = 1;
2400 }
2401 else if (GET_CODE (body) == PARALLEL)
2402 {
2403 /* Look through the PARALLEL and record the values being
2404 set, if possible. Also handle any CLOBBERs. */
2405 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2406 {
2407 rtx x = XVECEXP (body, 0, i);
2408
2409 if (GET_CODE (x) == SET)
2410 {
2411 sets[n_sets].src = SET_SRC (x);
2412 sets[n_sets].dest = SET_DEST (x);
2413 n_sets++;
2414 }
2415 }
2416 }
2417
2418 if (n_sets == 1
2419 && MEM_P (sets[0].src)
2420 && !cselib_record_memory
2421 && MEM_READONLY_P (sets[0].src))
2422 {
2423 rtx note = find_reg_equal_equiv_note (insn);
2424
2425 if (note && CONSTANT_P (XEXP (note, 0)))
2426 sets[0].src = XEXP (note, 0);
2427 }
2428
2429 data.sets = sets;
2430 data.n_sets = n_sets_before_autoinc = n_sets;
2431 for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2432 n_sets = data.n_sets;
2433
2434 /* Look up the values that are read. Do this before invalidating the
2435 locations that are written. */
2436 for (i = 0; i < n_sets; i++)
2437 {
2438 rtx dest = sets[i].dest;
2439
2440 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2441 the low part after invalidating any knowledge about larger modes. */
2442 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2443 sets[i].dest = dest = XEXP (dest, 0);
2444
2445 /* We don't know how to record anything but REG or MEM. */
2446 if (REG_P (dest)
2447 || (MEM_P (dest) && cselib_record_memory))
2448 {
2449 rtx src = sets[i].src;
2450 if (cond)
2451 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2452 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2453 if (MEM_P (dest))
2454 {
2455 enum machine_mode address_mode
2456 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (dest));
2457
2458 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2459 address_mode, 1,
2460 GET_MODE (dest));
2461 }
2462 else
2463 sets[i].dest_addr_elt = 0;
2464 }
2465 }
2466
2467 if (cselib_record_sets_hook)
2468 cselib_record_sets_hook (insn, sets, n_sets);
2469
2470 /* Invalidate all locations written by this insn. Note that the elts we
2471 looked up in the previous loop aren't affected, just some of their
2472 locations may go away. */
2473 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2474
2475 for (i = n_sets_before_autoinc; i < n_sets; i++)
2476 cselib_invalidate_rtx (sets[i].dest);
2477
2478 /* If this is an asm, look for duplicate sets. This can happen when the
2479 user uses the same value as an output multiple times. This is valid
2480 if the outputs are not actually used thereafter. Treat this case as
2481 if the value isn't actually set. We do this by smashing the destination
2482 to pc_rtx, so that we won't record the value later. */
2483 if (n_sets >= 2 && asm_noperands (body) >= 0)
2484 {
2485 for (i = 0; i < n_sets; i++)
2486 {
2487 rtx dest = sets[i].dest;
2488 if (REG_P (dest) || MEM_P (dest))
2489 {
2490 int j;
2491 for (j = i + 1; j < n_sets; j++)
2492 if (rtx_equal_p (dest, sets[j].dest))
2493 {
2494 sets[i].dest = pc_rtx;
2495 sets[j].dest = pc_rtx;
2496 }
2497 }
2498 }
2499 }
2500
2501 /* Now enter the equivalences in our tables. */
2502 for (i = 0; i < n_sets; i++)
2503 {
2504 rtx dest = sets[i].dest;
2505 if (REG_P (dest)
2506 || (MEM_P (dest) && cselib_record_memory))
2507 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2508 }
2509 }
2510
2511 /* Record the effects of INSN. */
2512
2513 void
2514 cselib_process_insn (rtx insn)
2515 {
2516 int i;
2517 rtx x;
2518
2519 cselib_current_insn = insn;
2520
2521 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
2522 if (LABEL_P (insn)
2523 || (CALL_P (insn)
2524 && find_reg_note (insn, REG_SETJMP, NULL))
2525 || (NONJUMP_INSN_P (insn)
2526 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2527 && MEM_VOLATILE_P (PATTERN (insn))))
2528 {
2529 cselib_reset_table (next_uid);
2530 cselib_current_insn = NULL_RTX;
2531 return;
2532 }
2533
2534 if (! INSN_P (insn))
2535 {
2536 cselib_current_insn = NULL_RTX;
2537 return;
2538 }
2539
2540 /* If this is a call instruction, forget anything stored in a
2541 call clobbered register, or, if this is not a const call, in
2542 memory. */
2543 if (CALL_P (insn))
2544 {
2545 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2546 if (call_used_regs[i]
2547 || (REG_VALUES (i) && REG_VALUES (i)->elt
2548 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2549 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2550 cselib_invalidate_regno (i, reg_raw_mode[i]);
2551
2552 /* Since it is not clear how cselib is going to be used, be
2553 conservative here and treat looping pure or const functions
2554 as if they were regular functions. */
2555 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2556 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2557 cselib_invalidate_mem (callmem);
2558 }
2559
2560 cselib_record_sets (insn);
2561
2562 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2563 after we have processed the insn. */
2564 if (CALL_P (insn))
2565 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2566 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2567 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2568
2569 cselib_current_insn = NULL_RTX;
2570
2571 if (n_useless_values > MAX_USELESS_VALUES
2572 /* remove_useless_values is linear in the hash table size. Avoid
2573 quadratic behavior for very large hashtables with very few
2574 useless elements. */
2575 && ((unsigned int)n_useless_values
2576 > (cselib_hash_table->n_elements
2577 - cselib_hash_table->n_deleted
2578 - n_debug_values) / 4))
2579 remove_useless_values ();
2580 }
2581
2582 /* Initialize cselib for one pass. The caller must also call
2583 init_alias_analysis. */
2584
2585 void
2586 cselib_init (int record_what)
2587 {
2588 elt_list_pool = create_alloc_pool ("elt_list",
2589 sizeof (struct elt_list), 10);
2590 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2591 sizeof (struct elt_loc_list), 10);
2592 cselib_val_pool = create_alloc_pool ("cselib_val_list",
2593 sizeof (cselib_val), 10);
2594 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2595 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2596 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2597
2598 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2599 see canon_true_dependence. This is only created once. */
2600 if (! callmem)
2601 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2602
2603 cselib_nregs = max_reg_num ();
2604
2605 /* We preserve reg_values to allow expensive clearing of the whole thing.
2606 Reallocate it however if it happens to be too large. */
2607 if (!reg_values || reg_values_size < cselib_nregs
2608 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2609 {
2610 free (reg_values);
2611 /* Some space for newly emit instructions so we don't end up
2612 reallocating in between passes. */
2613 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2614 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2615 }
2616 used_regs = XNEWVEC (unsigned int, cselib_nregs);
2617 n_used_regs = 0;
2618 cselib_hash_table = htab_create (31, get_value_hash,
2619 entry_and_rtx_equal_p, NULL);
2620 next_uid = 1;
2621 }
2622
2623 /* Called when the current user is done with cselib. */
2624
2625 void
2626 cselib_finish (void)
2627 {
2628 cselib_discard_hook = NULL;
2629 cselib_preserve_constants = false;
2630 cfa_base_preserved_val = NULL;
2631 cfa_base_preserved_regno = INVALID_REGNUM;
2632 free_alloc_pool (elt_list_pool);
2633 free_alloc_pool (elt_loc_list_pool);
2634 free_alloc_pool (cselib_val_pool);
2635 free_alloc_pool (value_pool);
2636 cselib_clear_table ();
2637 htab_delete (cselib_hash_table);
2638 free (used_regs);
2639 used_regs = 0;
2640 cselib_hash_table = 0;
2641 n_useless_values = 0;
2642 n_useless_debug_values = 0;
2643 n_debug_values = 0;
2644 next_uid = 0;
2645 }
2646
2647 /* Dump the cselib_val *X to FILE *info. */
2648
2649 static int
2650 dump_cselib_val (void **x, void *info)
2651 {
2652 cselib_val *v = (cselib_val *)*x;
2653 FILE *out = (FILE *)info;
2654 bool need_lf = true;
2655
2656 print_inline_rtx (out, v->val_rtx, 0);
2657
2658 if (v->locs)
2659 {
2660 struct elt_loc_list *l = v->locs;
2661 if (need_lf)
2662 {
2663 fputc ('\n', out);
2664 need_lf = false;
2665 }
2666 fputs (" locs:", out);
2667 do
2668 {
2669 fprintf (out, "\n from insn %i ",
2670 INSN_UID (l->setting_insn));
2671 print_inline_rtx (out, l->loc, 4);
2672 }
2673 while ((l = l->next));
2674 fputc ('\n', out);
2675 }
2676 else
2677 {
2678 fputs (" no locs", out);
2679 need_lf = true;
2680 }
2681
2682 if (v->addr_list)
2683 {
2684 struct elt_list *e = v->addr_list;
2685 if (need_lf)
2686 {
2687 fputc ('\n', out);
2688 need_lf = false;
2689 }
2690 fputs (" addr list:", out);
2691 do
2692 {
2693 fputs ("\n ", out);
2694 print_inline_rtx (out, e->elt->val_rtx, 2);
2695 }
2696 while ((e = e->next));
2697 fputc ('\n', out);
2698 }
2699 else
2700 {
2701 fputs (" no addrs", out);
2702 need_lf = true;
2703 }
2704
2705 if (v->next_containing_mem == &dummy_val)
2706 fputs (" last mem\n", out);
2707 else if (v->next_containing_mem)
2708 {
2709 fputs (" next mem ", out);
2710 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2711 fputc ('\n', out);
2712 }
2713 else if (need_lf)
2714 fputc ('\n', out);
2715
2716 return 1;
2717 }
2718
2719 /* Dump to OUT everything in the CSELIB table. */
2720
2721 void
2722 dump_cselib_table (FILE *out)
2723 {
2724 fprintf (out, "cselib hash table:\n");
2725 htab_traverse (cselib_hash_table, dump_cselib_val, out);
2726 if (first_containing_mem != &dummy_val)
2727 {
2728 fputs ("first mem ", out);
2729 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2730 fputc ('\n', out);
2731 }
2732 fprintf (out, "next uid %i\n", next_uid);
2733 }
2734
2735 #include "gt-cselib.h"