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