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