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