]>
Commit | Line | Data |
---|---|---|
6fb5fa3c | 1 | /* RTL dead store elimination. |
5624e564 | 2 | Copyright (C) 2005-2015 Free Software Foundation, Inc. |
6fb5fa3c DB |
3 | |
4 | Contributed by Richard Sandiford <rsandifor@codesourcery.com> | |
5 | and Kenneth Zadeck <zadeck@naturalbridge.com> | |
6 | ||
7 | This file is part of GCC. | |
8 | ||
9 | GCC is free software; you can redistribute it and/or modify it under | |
10 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 11 | Software Foundation; either version 3, or (at your option) any later |
6fb5fa3c DB |
12 | version. |
13 | ||
14 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 | for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
6fb5fa3c DB |
22 | |
23 | #undef BASELINE | |
24 | ||
25 | #include "config.h" | |
26 | #include "system.h" | |
27 | #include "coretypes.h" | |
703c8606 | 28 | #include "hash-table.h" |
6fb5fa3c DB |
29 | #include "tm.h" |
30 | #include "rtl.h" | |
40e23961 MC |
31 | #include "hash-set.h" |
32 | #include "machmode.h" | |
33 | #include "vec.h" | |
34 | #include "double-int.h" | |
35 | #include "input.h" | |
36 | #include "alias.h" | |
37 | #include "symtab.h" | |
38 | #include "wide-int.h" | |
39 | #include "inchash.h" | |
40 | #include "real.h" | |
6fb5fa3c | 41 | #include "tree.h" |
40e23961 | 42 | #include "fold-const.h" |
d8a2d370 | 43 | #include "stor-layout.h" |
18b526e8 | 44 | #include "tm_p.h" |
6fb5fa3c DB |
45 | #include "regs.h" |
46 | #include "hard-reg-set.h" | |
9e582b1d | 47 | #include "regset.h" |
6fb5fa3c | 48 | #include "flags.h" |
60393bbc AM |
49 | #include "dominance.h" |
50 | #include "cfg.h" | |
51 | #include "cfgrtl.h" | |
52 | #include "predict.h" | |
53 | #include "basic-block.h" | |
6fb5fa3c DB |
54 | #include "df.h" |
55 | #include "cselib.h" | |
6fb5fa3c DB |
56 | #include "tree-pass.h" |
57 | #include "alloc-pool.h" | |
6fb5fa3c | 58 | #include "insn-config.h" |
36566b39 PK |
59 | #include "hashtab.h" |
60 | #include "function.h" | |
61 | #include "statistics.h" | |
62 | #include "fixed-value.h" | |
63 | #include "expmed.h" | |
64 | #include "dojump.h" | |
65 | #include "explow.h" | |
66 | #include "calls.h" | |
67 | #include "emit-rtl.h" | |
68 | #include "varasm.h" | |
69 | #include "stmt.h" | |
6fb5fa3c DB |
70 | #include "expr.h" |
71 | #include "recog.h" | |
b0710fe1 | 72 | #include "insn-codes.h" |
8660aaae | 73 | #include "optabs.h" |
6fb5fa3c | 74 | #include "dbgcnt.h" |
67d0afe9 | 75 | #include "target.h" |
dabd47e7 | 76 | #include "params.h" |
2fb9a547 AM |
77 | #include "tree-ssa-alias.h" |
78 | #include "internal-fn.h" | |
79 | #include "gimple-expr.h" | |
80 | #include "is-a.h" | |
442b4905 AM |
81 | #include "gimple.h" |
82 | #include "gimple-ssa.h" | |
d7111da8 | 83 | #include "rtl-iter.h" |
6fb5fa3c DB |
84 | |
85 | /* This file contains three techniques for performing Dead Store | |
b8698a0f | 86 | Elimination (dse). |
6fb5fa3c DB |
87 | |
88 | * The first technique performs dse locally on any base address. It | |
89 | is based on the cselib which is a local value numbering technique. | |
90 | This technique is local to a basic block but deals with a fairly | |
91 | general addresses. | |
b8698a0f | 92 | |
6fb5fa3c DB |
93 | * The second technique performs dse globally but is restricted to |
94 | base addresses that are either constant or are relative to the | |
95 | frame_pointer. | |
96 | ||
97 | * The third technique, (which is only done after register allocation) | |
98 | processes the spill spill slots. This differs from the second | |
99 | technique because it takes advantage of the fact that spilling is | |
100 | completely free from the effects of aliasing. | |
101 | ||
102 | Logically, dse is a backwards dataflow problem. A store can be | |
103 | deleted if it if cannot be reached in the backward direction by any | |
104 | use of the value being stored. However, the local technique uses a | |
105 | forwards scan of the basic block because cselib requires that the | |
106 | block be processed in that order. | |
107 | ||
108 | The pass is logically broken into 7 steps: | |
109 | ||
110 | 0) Initialization. | |
111 | ||
112 | 1) The local algorithm, as well as scanning the insns for the two | |
113 | global algorithms. | |
114 | ||
115 | 2) Analysis to see if the global algs are necessary. In the case | |
116 | of stores base on a constant address, there must be at least two | |
117 | stores to that address, to make it possible to delete some of the | |
118 | stores. In the case of stores off of the frame or spill related | |
119 | stores, only one store to an address is necessary because those | |
120 | stores die at the end of the function. | |
121 | ||
b8698a0f | 122 | 3) Set up the global dataflow equations based on processing the |
6fb5fa3c DB |
123 | info parsed in the first step. |
124 | ||
125 | 4) Solve the dataflow equations. | |
126 | ||
127 | 5) Delete the insns that the global analysis has indicated are | |
128 | unnecessary. | |
129 | ||
073a8998 | 130 | 6) Delete insns that store the same value as preceding store |
8dd5516b JJ |
131 | where the earlier store couldn't be eliminated. |
132 | ||
133 | 7) Cleanup. | |
6fb5fa3c DB |
134 | |
135 | This step uses cselib and canon_rtx to build the largest expression | |
136 | possible for each address. This pass is a forwards pass through | |
137 | each basic block. From the point of view of the global technique, | |
138 | the first pass could examine a block in either direction. The | |
0d52bcc1 | 139 | forwards ordering is to accommodate cselib. |
6fb5fa3c | 140 | |
5ef0b50d | 141 | We make a simplifying assumption: addresses fall into four broad |
6fb5fa3c DB |
142 | categories: |
143 | ||
144 | 1) base has rtx_varies_p == false, offset is constant. | |
145 | 2) base has rtx_varies_p == false, offset variable. | |
146 | 3) base has rtx_varies_p == true, offset constant. | |
147 | 4) base has rtx_varies_p == true, offset variable. | |
148 | ||
149 | The local passes are able to process all 4 kinds of addresses. The | |
5ef0b50d | 150 | global pass only handles 1). |
6fb5fa3c DB |
151 | |
152 | The global problem is formulated as follows: | |
153 | ||
154 | A store, S1, to address A, where A is not relative to the stack | |
155 | frame, can be eliminated if all paths from S1 to the end of the | |
5ef0b50d | 156 | function contain another store to A before a read to A. |
6fb5fa3c DB |
157 | |
158 | If the address A is relative to the stack frame, a store S2 to A | |
5ef0b50d | 159 | can be eliminated if there are no paths from S2 that reach the |
6fb5fa3c | 160 | end of the function that read A before another store to A. In |
5ef0b50d | 161 | this case S2 can be deleted if there are paths from S2 to the |
6fb5fa3c DB |
162 | end of the function that have no reads or writes to A. This |
163 | second case allows stores to the stack frame to be deleted that | |
164 | would otherwise die when the function returns. This cannot be | |
165 | done if stores_off_frame_dead_at_return is not true. See the doc | |
166 | for that variable for when this variable is false. | |
167 | ||
168 | The global problem is formulated as a backwards set union | |
169 | dataflow problem where the stores are the gens and reads are the | |
170 | kills. Set union problems are rare and require some special | |
171 | handling given our representation of bitmaps. A straightforward | |
5ef0b50d | 172 | implementation requires a lot of bitmaps filled with 1s. |
6fb5fa3c DB |
173 | These are expensive and cumbersome in our bitmap formulation so |
174 | care has been taken to avoid large vectors filled with 1s. See | |
175 | the comments in bb_info and in the dataflow confluence functions | |
b8698a0f | 176 | for details. |
6fb5fa3c DB |
177 | |
178 | There are two places for further enhancements to this algorithm: | |
b8698a0f | 179 | |
6fb5fa3c DB |
180 | 1) The original dse which was embedded in a pass called flow also |
181 | did local address forwarding. For example in | |
182 | ||
183 | A <- r100 | |
184 | ... <- A | |
185 | ||
186 | flow would replace the right hand side of the second insn with a | |
6ed3da00 | 187 | reference to r100. Most of the information is available to add this |
6fb5fa3c DB |
188 | to this pass. It has not done it because it is a lot of work in |
189 | the case that either r100 is assigned to between the first and | |
190 | second insn and/or the second insn is a load of part of the value | |
191 | stored by the first insn. | |
192 | ||
193 | insn 5 in gcc.c-torture/compile/990203-1.c simple case. | |
194 | insn 15 in gcc.c-torture/execute/20001017-2.c simple case. | |
195 | insn 25 in gcc.c-torture/execute/20001026-1.c simple case. | |
196 | insn 44 in gcc.c-torture/execute/20010910-1.c simple case. | |
197 | ||
198 | 2) The cleaning up of spill code is quite profitable. It currently | |
199 | depends on reading tea leaves and chicken entrails left by reload. | |
200 | This pass depends on reload creating a singleton alias set for each | |
201 | spill slot and telling the next dse pass which of these alias sets | |
202 | are the singletons. Rather than analyze the addresses of the | |
203 | spills, dse's spill processing just does analysis of the loads and | |
204 | stores that use those alias sets. There are three cases where this | |
205 | falls short: | |
206 | ||
207 | a) Reload sometimes creates the slot for one mode of access, and | |
208 | then inserts loads and/or stores for a smaller mode. In this | |
209 | case, the current code just punts on the slot. The proper thing | |
210 | to do is to back out and use one bit vector position for each | |
211 | byte of the entity associated with the slot. This depends on | |
212 | KNOWING that reload always generates the accesses for each of the | |
213 | bytes in some canonical (read that easy to understand several | |
214 | passes after reload happens) way. | |
215 | ||
216 | b) Reload sometimes decides that spill slot it allocated was not | |
217 | large enough for the mode and goes back and allocates more slots | |
218 | with the same mode and alias set. The backout in this case is a | |
219 | little more graceful than (a). In this case the slot is unmarked | |
220 | as being a spill slot and if final address comes out to be based | |
b8698a0f | 221 | off the frame pointer, the global algorithm handles this slot. |
6fb5fa3c DB |
222 | |
223 | c) For any pass that may prespill, there is currently no | |
224 | mechanism to tell the dse pass that the slot being used has the | |
225 | special properties that reload uses. It may be that all that is | |
0d52bcc1 | 226 | required is to have those passes make the same calls that reload |
6fb5fa3c DB |
227 | does, assuming that the alias sets can be manipulated in the same |
228 | way. */ | |
229 | ||
230 | /* There are limits to the size of constant offsets we model for the | |
231 | global problem. There are certainly test cases, that exceed this | |
232 | limit, however, it is unlikely that there are important programs | |
233 | that really have constant offsets this size. */ | |
234 | #define MAX_OFFSET (64 * 1024) | |
235 | ||
3f9b14ff SB |
236 | /* Obstack for the DSE dataflow bitmaps. We don't want to put these |
237 | on the default obstack because these bitmaps can grow quite large | |
238 | (~2GB for the small (!) test case of PR54146) and we'll hold on to | |
239 | all that memory until the end of the compiler run. | |
240 | As a bonus, delete_tree_live_info can destroy all the bitmaps by just | |
241 | releasing the whole obstack. */ | |
242 | static bitmap_obstack dse_bitmap_obstack; | |
243 | ||
244 | /* Obstack for other data. As for above: Kinda nice to be able to | |
245 | throw it all away at the end in one big sweep. */ | |
246 | static struct obstack dse_obstack; | |
247 | ||
248 | /* Scratch bitmap for cselib's cselib_expand_value_rtx. */ | |
6fb5fa3c | 249 | static bitmap scratch = NULL; |
3f9b14ff | 250 | |
6fb5fa3c DB |
251 | struct insn_info; |
252 | ||
253 | /* This structure holds information about a candidate store. */ | |
b8698a0f | 254 | struct store_info |
6fb5fa3c DB |
255 | { |
256 | ||
257 | /* False means this is a clobber. */ | |
258 | bool is_set; | |
259 | ||
8dd5516b JJ |
260 | /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */ |
261 | bool is_large; | |
262 | ||
6fb5fa3c DB |
263 | /* The id of the mem group of the base address. If rtx_varies_p is |
264 | true, this is -1. Otherwise, it is the index into the group | |
265 | table. */ | |
266 | int group_id; | |
b8698a0f | 267 | |
6fb5fa3c DB |
268 | /* This is the cselib value. */ |
269 | cselib_val *cse_base; | |
270 | ||
271 | /* This canonized mem. */ | |
272 | rtx mem; | |
273 | ||
6216f94e | 274 | /* Canonized MEM address for use by canon_true_dependence. */ |
6fb5fa3c DB |
275 | rtx mem_addr; |
276 | ||
277 | /* If this is non-zero, it is the alias set of a spill location. */ | |
4862826d | 278 | alias_set_type alias_set; |
6fb5fa3c DB |
279 | |
280 | /* The offset of the first and byte before the last byte associated | |
281 | with the operation. */ | |
8dd5516b JJ |
282 | HOST_WIDE_INT begin, end; |
283 | ||
284 | union | |
285 | { | |
286 | /* A bitmask as wide as the number of bytes in the word that | |
287 | contains a 1 if the byte may be needed. The store is unused if | |
288 | all of the bits are 0. This is used if IS_LARGE is false. */ | |
289 | unsigned HOST_WIDE_INT small_bitmask; | |
290 | ||
291 | struct | |
292 | { | |
293 | /* A bitmap with one bit per byte. Cleared bit means the position | |
294 | is needed. Used if IS_LARGE is false. */ | |
dc491a25 | 295 | bitmap bmap; |
6fb5fa3c | 296 | |
8dd5516b JJ |
297 | /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is |
298 | equal to END - BEGIN, the whole store is unused. */ | |
299 | int count; | |
300 | } large; | |
301 | } positions_needed; | |
6fb5fa3c DB |
302 | |
303 | /* The next store info for this insn. */ | |
304 | struct store_info *next; | |
305 | ||
306 | /* The right hand side of the store. This is used if there is a | |
307 | subsequent reload of the mems address somewhere later in the | |
308 | basic block. */ | |
8dd5516b JJ |
309 | rtx rhs; |
310 | ||
311 | /* If rhs is or holds a constant, this contains that constant, | |
312 | otherwise NULL. */ | |
313 | rtx const_rhs; | |
314 | ||
315 | /* Set if this store stores the same constant value as REDUNDANT_REASON | |
316 | insn stored. These aren't eliminated early, because doing that | |
317 | might prevent the earlier larger store to be eliminated. */ | |
318 | struct insn_info *redundant_reason; | |
6fb5fa3c DB |
319 | }; |
320 | ||
4fe663b0 L |
321 | /* Return a bitmask with the first N low bits set. */ |
322 | ||
323 | static unsigned HOST_WIDE_INT | |
324 | lowpart_bitmask (int n) | |
325 | { | |
326 | unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0; | |
327 | return mask >> (HOST_BITS_PER_WIDE_INT - n); | |
328 | } | |
329 | ||
6fb5fa3c DB |
330 | typedef struct store_info *store_info_t; |
331 | static alloc_pool cse_store_info_pool; | |
332 | static alloc_pool rtx_store_info_pool; | |
333 | ||
334 | /* This structure holds information about a load. These are only | |
335 | built for rtx bases. */ | |
b8698a0f | 336 | struct read_info |
6fb5fa3c DB |
337 | { |
338 | /* The id of the mem group of the base address. */ | |
339 | int group_id; | |
340 | ||
341 | /* If this is non-zero, it is the alias set of a spill location. */ | |
4862826d | 342 | alias_set_type alias_set; |
6fb5fa3c DB |
343 | |
344 | /* The offset of the first and byte after the last byte associated | |
345 | with the operation. If begin == end == 0, the read did not have | |
346 | a constant offset. */ | |
347 | int begin, end; | |
348 | ||
349 | /* The mem being read. */ | |
350 | rtx mem; | |
351 | ||
352 | /* The next read_info for this insn. */ | |
353 | struct read_info *next; | |
354 | }; | |
355 | typedef struct read_info *read_info_t; | |
356 | static alloc_pool read_info_pool; | |
357 | ||
358 | ||
359 | /* One of these records is created for each insn. */ | |
360 | ||
b8698a0f | 361 | struct insn_info |
6fb5fa3c DB |
362 | { |
363 | /* Set true if the insn contains a store but the insn itself cannot | |
364 | be deleted. This is set if the insn is a parallel and there is | |
365 | more than one non dead output or if the insn is in some way | |
366 | volatile. */ | |
367 | bool cannot_delete; | |
368 | ||
369 | /* This field is only used by the global algorithm. It is set true | |
370 | if the insn contains any read of mem except for a (1). This is | |
371 | also set if the insn is a call or has a clobber mem. If the insn | |
372 | contains a wild read, the use_rec will be null. */ | |
373 | bool wild_read; | |
374 | ||
d26c7090 ER |
375 | /* This is true only for CALL instructions which could potentially read |
376 | any non-frame memory location. This field is used by the global | |
377 | algorithm. */ | |
378 | bool non_frame_wild_read; | |
379 | ||
50f0f366 EB |
380 | /* This field is only used for the processing of const functions. |
381 | These functions cannot read memory, but they can read the stack | |
64520bdc EB |
382 | because that is where they may get their parms. We need to be |
383 | this conservative because, like the store motion pass, we don't | |
384 | consider CALL_INSN_FUNCTION_USAGE when processing call insns. | |
385 | Moreover, we need to distinguish two cases: | |
386 | 1. Before reload (register elimination), the stores related to | |
387 | outgoing arguments are stack pointer based and thus deemed | |
388 | of non-constant base in this pass. This requires special | |
389 | handling but also means that the frame pointer based stores | |
390 | need not be killed upon encountering a const function call. | |
391 | 2. After reload, the stores related to outgoing arguments can be | |
392 | either stack pointer or hard frame pointer based. This means | |
393 | that we have no other choice than also killing all the frame | |
394 | pointer based stores upon encountering a const function call. | |
57534689 JJ |
395 | This field is set after reload for const function calls and before |
396 | reload for const tail function calls on targets where arg pointer | |
397 | is the frame pointer. Having this set is less severe than a wild | |
398 | read, it just means that all the frame related stores are killed | |
399 | rather than all the stores. */ | |
64520bdc EB |
400 | bool frame_read; |
401 | ||
402 | /* This field is only used for the processing of const functions. | |
403 | It is set if the insn may contain a stack pointer based store. */ | |
50f0f366 | 404 | bool stack_pointer_based; |
6fb5fa3c DB |
405 | |
406 | /* This is true if any of the sets within the store contains a | |
407 | cselib base. Such stores can only be deleted by the local | |
408 | algorithm. */ | |
409 | bool contains_cselib_groups; | |
410 | ||
411 | /* The insn. */ | |
dd60a84c | 412 | rtx_insn *insn; |
6fb5fa3c DB |
413 | |
414 | /* The list of mem sets or mem clobbers that are contained in this | |
415 | insn. If the insn is deletable, it contains only one mem set. | |
416 | But it could also contain clobbers. Insns that contain more than | |
417 | one mem set are not deletable, but each of those mems are here in | |
6ed3da00 | 418 | order to provide info to delete other insns. */ |
6fb5fa3c DB |
419 | store_info_t store_rec; |
420 | ||
421 | /* The linked list of mem uses in this insn. Only the reads from | |
422 | rtx bases are listed here. The reads to cselib bases are | |
423 | completely processed during the first scan and so are never | |
424 | created. */ | |
425 | read_info_t read_rec; | |
426 | ||
9e582b1d JR |
427 | /* The live fixed registers. We assume only fixed registers can |
428 | cause trouble by being clobbered from an expanded pattern; | |
429 | storing only the live fixed registers (rather than all registers) | |
430 | means less memory needs to be allocated / copied for the individual | |
431 | stores. */ | |
432 | regset fixed_regs_live; | |
433 | ||
6fb5fa3c DB |
434 | /* The prev insn in the basic block. */ |
435 | struct insn_info * prev_insn; | |
436 | ||
437 | /* The linked list of insns that are in consideration for removal in | |
073a8998 | 438 | the forwards pass through the basic block. This pointer may be |
6fb5fa3c | 439 | trash as it is not cleared when a wild read occurs. The only |
fa10beec | 440 | time it is guaranteed to be correct is when the traversal starts |
6fb5fa3c DB |
441 | at active_local_stores. */ |
442 | struct insn_info * next_local_store; | |
443 | }; | |
444 | ||
445 | typedef struct insn_info *insn_info_t; | |
446 | static alloc_pool insn_info_pool; | |
447 | ||
448 | /* The linked list of stores that are under consideration in this | |
b8698a0f | 449 | basic block. */ |
6fb5fa3c | 450 | static insn_info_t active_local_stores; |
dabd47e7 | 451 | static int active_local_stores_len; |
6fb5fa3c | 452 | |
11478306 | 453 | struct dse_bb_info |
6fb5fa3c DB |
454 | { |
455 | ||
456 | /* Pointer to the insn info for the last insn in the block. These | |
457 | are linked so this is how all of the insns are reached. During | |
458 | scanning this is the current insn being scanned. */ | |
459 | insn_info_t last_insn; | |
460 | ||
461 | /* The info for the global dataflow problem. */ | |
462 | ||
463 | ||
464 | /* This is set if the transfer function should and in the wild_read | |
465 | bitmap before applying the kill and gen sets. That vector knocks | |
466 | out most of the bits in the bitmap and thus speeds up the | |
467 | operations. */ | |
468 | bool apply_wild_read; | |
469 | ||
02b47899 KZ |
470 | /* The following 4 bitvectors hold information about which positions |
471 | of which stores are live or dead. They are indexed by | |
472 | get_bitmap_index. */ | |
473 | ||
6fb5fa3c DB |
474 | /* The set of store positions that exist in this block before a wild read. */ |
475 | bitmap gen; | |
b8698a0f | 476 | |
6fb5fa3c DB |
477 | /* The set of load positions that exist in this block above the |
478 | same position of a store. */ | |
479 | bitmap kill; | |
480 | ||
481 | /* The set of stores that reach the top of the block without being | |
482 | killed by a read. | |
483 | ||
484 | Do not represent the in if it is all ones. Note that this is | |
485 | what the bitvector should logically be initialized to for a set | |
486 | intersection problem. However, like the kill set, this is too | |
487 | expensive. So initially, the in set will only be created for the | |
488 | exit block and any block that contains a wild read. */ | |
489 | bitmap in; | |
490 | ||
491 | /* The set of stores that reach the bottom of the block from it's | |
492 | successors. | |
493 | ||
494 | Do not represent the in if it is all ones. Note that this is | |
495 | what the bitvector should logically be initialized to for a set | |
496 | intersection problem. However, like the kill and in set, this is | |
497 | too expensive. So what is done is that the confluence operator | |
498 | just initializes the vector from one of the out sets of the | |
499 | successors of the block. */ | |
500 | bitmap out; | |
02b47899 KZ |
501 | |
502 | /* The following bitvector is indexed by the reg number. It | |
503 | contains the set of regs that are live at the current instruction | |
504 | being processed. While it contains info for all of the | |
9e582b1d JR |
505 | registers, only the hard registers are actually examined. It is used |
506 | to assure that shift and/or add sequences that are inserted do not | |
073a8998 | 507 | accidentally clobber live hard regs. */ |
02b47899 | 508 | bitmap regs_live; |
6fb5fa3c DB |
509 | }; |
510 | ||
11478306 | 511 | typedef struct dse_bb_info *bb_info_t; |
6fb5fa3c DB |
512 | static alloc_pool bb_info_pool; |
513 | ||
514 | /* Table to hold all bb_infos. */ | |
515 | static bb_info_t *bb_table; | |
516 | ||
517 | /* There is a group_info for each rtx base that is used to reference | |
518 | memory. There are also not many of the rtx bases because they are | |
519 | very limited in scope. */ | |
520 | ||
b8698a0f | 521 | struct group_info |
6fb5fa3c DB |
522 | { |
523 | /* The actual base of the address. */ | |
524 | rtx rtx_base; | |
525 | ||
526 | /* The sequential id of the base. This allows us to have a | |
527 | canonical ordering of these that is not based on addresses. */ | |
528 | int id; | |
529 | ||
8f5929e1 JJ |
530 | /* True if there are any positions that are to be processed |
531 | globally. */ | |
532 | bool process_globally; | |
533 | ||
534 | /* True if the base of this group is either the frame_pointer or | |
535 | hard_frame_pointer. */ | |
536 | bool frame_related; | |
537 | ||
d32f725a OH |
538 | /* A mem wrapped around the base pointer for the group in order to do |
539 | read dependency. It must be given BLKmode in order to encompass all | |
540 | the possible offsets from the base. */ | |
6fb5fa3c | 541 | rtx base_mem; |
b8698a0f | 542 | |
6216f94e JJ |
543 | /* Canonized version of base_mem's address. */ |
544 | rtx canon_base_addr; | |
6fb5fa3c DB |
545 | |
546 | /* These two sets of two bitmaps are used to keep track of how many | |
6ed3da00 | 547 | stores are actually referencing that position from this base. We |
6fb5fa3c | 548 | only do this for rtx bases as this will be used to assign |
6ed3da00 | 549 | positions in the bitmaps for the global problem. Bit N is set in |
6fb5fa3c DB |
550 | store1 on the first store for offset N. Bit N is set in store2 |
551 | for the second store to offset N. This is all we need since we | |
552 | only care about offsets that have two or more stores for them. | |
553 | ||
554 | The "_n" suffix is for offsets less than 0 and the "_p" suffix is | |
555 | for 0 and greater offsets. | |
556 | ||
557 | There is one special case here, for stores into the stack frame, | |
558 | we will or store1 into store2 before deciding which stores look | |
559 | at globally. This is because stores to the stack frame that have | |
560 | no other reads before the end of the function can also be | |
561 | deleted. */ | |
562 | bitmap store1_n, store1_p, store2_n, store2_p; | |
563 | ||
d26c7090 ER |
564 | /* These bitmaps keep track of offsets in this group escape this function. |
565 | An offset escapes if it corresponds to a named variable whose | |
566 | addressable flag is set. */ | |
567 | bitmap escaped_n, escaped_p; | |
568 | ||
6ed3da00 | 569 | /* The positions in this bitmap have the same assignments as the in, |
6fb5fa3c | 570 | out, gen and kill bitmaps. This bitmap is all zeros except for |
6ed3da00 | 571 | the positions that are occupied by stores for this group. */ |
6fb5fa3c DB |
572 | bitmap group_kill; |
573 | ||
6fb5fa3c | 574 | /* The offset_map is used to map the offsets from this base into |
6ed3da00 | 575 | positions in the global bitmaps. It is only created after all of |
6fb5fa3c DB |
576 | the all of stores have been scanned and we know which ones we |
577 | care about. */ | |
b8698a0f L |
578 | int *offset_map_n, *offset_map_p; |
579 | int offset_map_size_n, offset_map_size_p; | |
6fb5fa3c DB |
580 | }; |
581 | typedef struct group_info *group_info_t; | |
5f754896 | 582 | typedef const struct group_info *const_group_info_t; |
6fb5fa3c DB |
583 | static alloc_pool rtx_group_info_pool; |
584 | ||
6fb5fa3c DB |
585 | /* Index into the rtx_group_vec. */ |
586 | static int rtx_group_next_id; | |
587 | ||
6fb5fa3c | 588 | |
9771b263 | 589 | static vec<group_info_t> rtx_group_vec; |
6fb5fa3c DB |
590 | |
591 | ||
592 | /* This structure holds the set of changes that are being deferred | |
593 | when removing read operation. See replace_read. */ | |
b8698a0f | 594 | struct deferred_change |
6fb5fa3c DB |
595 | { |
596 | ||
597 | /* The mem that is being replaced. */ | |
598 | rtx *loc; | |
599 | ||
600 | /* The reg it is being replaced with. */ | |
601 | rtx reg; | |
602 | ||
603 | struct deferred_change *next; | |
604 | }; | |
605 | ||
606 | typedef struct deferred_change *deferred_change_t; | |
607 | static alloc_pool deferred_change_pool; | |
608 | ||
609 | static deferred_change_t deferred_change_list = NULL; | |
610 | ||
6fb5fa3c DB |
611 | /* The group that holds all of the clear_alias_sets. */ |
612 | static group_info_t clear_alias_group; | |
613 | ||
614 | /* The modes of the clear_alias_sets. */ | |
615 | static htab_t clear_alias_mode_table; | |
616 | ||
617 | /* Hash table element to look up the mode for an alias set. */ | |
618 | struct clear_alias_mode_holder | |
619 | { | |
4862826d | 620 | alias_set_type alias_set; |
ef4bddc2 | 621 | machine_mode mode; |
6fb5fa3c DB |
622 | }; |
623 | ||
e3b5732b | 624 | /* This is true except if cfun->stdarg -- i.e. we cannot do |
9dd9bf80 | 625 | this for vararg functions because they play games with the frame. */ |
6fb5fa3c DB |
626 | static bool stores_off_frame_dead_at_return; |
627 | ||
628 | /* Counter for stats. */ | |
b8698a0f L |
629 | static int globally_deleted; |
630 | static int locally_deleted; | |
631 | static int spill_deleted; | |
632 | ||
6fb5fa3c DB |
633 | static bitmap all_blocks; |
634 | ||
d26c7090 ER |
635 | /* Locations that are killed by calls in the global phase. */ |
636 | static bitmap kill_on_calls; | |
637 | ||
6fb5fa3c DB |
638 | /* The number of bits used in the global bitmaps. */ |
639 | static unsigned int current_position; | |
6fb5fa3c DB |
640 | \f |
641 | /*---------------------------------------------------------------------------- | |
642 | Zeroth step. | |
643 | ||
b8698a0f | 644 | Initialization. |
6fb5fa3c DB |
645 | ----------------------------------------------------------------------------*/ |
646 | ||
6fb5fa3c DB |
647 | |
648 | /* Find the entry associated with ALIAS_SET. */ | |
649 | ||
650 | static struct clear_alias_mode_holder * | |
4862826d | 651 | clear_alias_set_lookup (alias_set_type alias_set) |
6fb5fa3c DB |
652 | { |
653 | struct clear_alias_mode_holder tmp_holder; | |
654 | void **slot; | |
b8698a0f | 655 | |
6fb5fa3c DB |
656 | tmp_holder.alias_set = alias_set; |
657 | slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT); | |
658 | gcc_assert (*slot); | |
b8698a0f | 659 | |
f883e0a7 | 660 | return (struct clear_alias_mode_holder *) *slot; |
6fb5fa3c DB |
661 | } |
662 | ||
663 | ||
664 | /* Hashtable callbacks for maintaining the "bases" field of | |
665 | store_group_info, given that the addresses are function invariants. */ | |
666 | ||
703c8606 LC |
667 | struct invariant_group_base_hasher : typed_noop_remove <group_info> |
668 | { | |
5831a5f0 LC |
669 | typedef group_info value_type; |
670 | typedef group_info compare_type; | |
671 | static inline hashval_t hash (const value_type *); | |
672 | static inline bool equal (const value_type *, const compare_type *); | |
703c8606 LC |
673 | }; |
674 | ||
675 | inline bool | |
5831a5f0 LC |
676 | invariant_group_base_hasher::equal (const value_type *gi1, |
677 | const compare_type *gi2) | |
6fb5fa3c | 678 | { |
6fb5fa3c DB |
679 | return rtx_equal_p (gi1->rtx_base, gi2->rtx_base); |
680 | } | |
681 | ||
703c8606 | 682 | inline hashval_t |
5831a5f0 | 683 | invariant_group_base_hasher::hash (const value_type *gi) |
6fb5fa3c | 684 | { |
6fb5fa3c DB |
685 | int do_not_record; |
686 | return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false); | |
687 | } | |
688 | ||
703c8606 | 689 | /* Tables of group_info structures, hashed by base value. */ |
c203e8a7 | 690 | static hash_table<invariant_group_base_hasher> *rtx_group_table; |
703c8606 | 691 | |
6fb5fa3c DB |
692 | |
693 | /* Get the GROUP for BASE. Add a new group if it is not there. */ | |
694 | ||
695 | static group_info_t | |
696 | get_group_info (rtx base) | |
697 | { | |
b8698a0f L |
698 | struct group_info tmp_gi; |
699 | group_info_t gi; | |
703c8606 | 700 | group_info **slot; |
6fb5fa3c DB |
701 | |
702 | if (base) | |
703 | { | |
704 | /* Find the store_base_info structure for BASE, creating a new one | |
705 | if necessary. */ | |
706 | tmp_gi.rtx_base = base; | |
c203e8a7 | 707 | slot = rtx_group_table->find_slot (&tmp_gi, INSERT); |
6fb5fa3c DB |
708 | gi = (group_info_t) *slot; |
709 | } | |
710 | else | |
711 | { | |
712 | if (!clear_alias_group) | |
713 | { | |
f883e0a7 KG |
714 | clear_alias_group = gi = |
715 | (group_info_t) pool_alloc (rtx_group_info_pool); | |
6fb5fa3c DB |
716 | memset (gi, 0, sizeof (struct group_info)); |
717 | gi->id = rtx_group_next_id++; | |
3f9b14ff SB |
718 | gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack); |
719 | gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack); | |
720 | gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack); | |
721 | gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack); | |
722 | gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack); | |
723 | gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack); | |
724 | gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack); | |
6fb5fa3c DB |
725 | gi->process_globally = false; |
726 | gi->offset_map_size_n = 0; | |
727 | gi->offset_map_size_p = 0; | |
728 | gi->offset_map_n = NULL; | |
729 | gi->offset_map_p = NULL; | |
9771b263 | 730 | rtx_group_vec.safe_push (gi); |
6fb5fa3c DB |
731 | } |
732 | return clear_alias_group; | |
733 | } | |
734 | ||
735 | if (gi == NULL) | |
736 | { | |
f883e0a7 | 737 | *slot = gi = (group_info_t) pool_alloc (rtx_group_info_pool); |
6fb5fa3c DB |
738 | gi->rtx_base = base; |
739 | gi->id = rtx_group_next_id++; | |
d32f725a | 740 | gi->base_mem = gen_rtx_MEM (BLKmode, base); |
6216f94e | 741 | gi->canon_base_addr = canon_rtx (base); |
3f9b14ff SB |
742 | gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack); |
743 | gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack); | |
744 | gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack); | |
745 | gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack); | |
746 | gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack); | |
747 | gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack); | |
748 | gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack); | |
6fb5fa3c | 749 | gi->process_globally = false; |
b8698a0f | 750 | gi->frame_related = |
6fb5fa3c DB |
751 | (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx); |
752 | gi->offset_map_size_n = 0; | |
753 | gi->offset_map_size_p = 0; | |
754 | gi->offset_map_n = NULL; | |
755 | gi->offset_map_p = NULL; | |
9771b263 | 756 | rtx_group_vec.safe_push (gi); |
6fb5fa3c DB |
757 | } |
758 | ||
759 | return gi; | |
760 | } | |
761 | ||
762 | ||
763 | /* Initialization of data structures. */ | |
764 | ||
765 | static void | |
766 | dse_step0 (void) | |
767 | { | |
768 | locally_deleted = 0; | |
769 | globally_deleted = 0; | |
770 | spill_deleted = 0; | |
771 | ||
3f9b14ff SB |
772 | bitmap_obstack_initialize (&dse_bitmap_obstack); |
773 | gcc_obstack_init (&dse_obstack); | |
774 | ||
775 | scratch = BITMAP_ALLOC (®_obstack); | |
776 | kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack); | |
6fb5fa3c DB |
777 | |
778 | rtx_store_info_pool | |
b8698a0f | 779 | = create_alloc_pool ("rtx_store_info_pool", |
6fb5fa3c DB |
780 | sizeof (struct store_info), 100); |
781 | read_info_pool | |
b8698a0f | 782 | = create_alloc_pool ("read_info_pool", |
6fb5fa3c DB |
783 | sizeof (struct read_info), 100); |
784 | insn_info_pool | |
b8698a0f | 785 | = create_alloc_pool ("insn_info_pool", |
6fb5fa3c DB |
786 | sizeof (struct insn_info), 100); |
787 | bb_info_pool | |
b8698a0f | 788 | = create_alloc_pool ("bb_info_pool", |
11478306 | 789 | sizeof (struct dse_bb_info), 100); |
6fb5fa3c | 790 | rtx_group_info_pool |
b8698a0f | 791 | = create_alloc_pool ("rtx_group_info_pool", |
6fb5fa3c DB |
792 | sizeof (struct group_info), 100); |
793 | deferred_change_pool | |
b8698a0f | 794 | = create_alloc_pool ("deferred_change_pool", |
6fb5fa3c DB |
795 | sizeof (struct deferred_change), 10); |
796 | ||
c203e8a7 | 797 | rtx_group_table = new hash_table<invariant_group_base_hasher> (11); |
6fb5fa3c | 798 | |
8b1c6fd7 | 799 | bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun)); |
6fb5fa3c DB |
800 | rtx_group_next_id = 0; |
801 | ||
e3b5732b | 802 | stores_off_frame_dead_at_return = !cfun->stdarg; |
6fb5fa3c DB |
803 | |
804 | init_alias_analysis (); | |
b8698a0f | 805 | |
4b943a49 | 806 | clear_alias_group = NULL; |
6fb5fa3c DB |
807 | } |
808 | ||
809 | ||
810 | \f | |
811 | /*---------------------------------------------------------------------------- | |
812 | First step. | |
813 | ||
814 | Scan all of the insns. Any random ordering of the blocks is fine. | |
0d52bcc1 | 815 | Each block is scanned in forward order to accommodate cselib which |
6fb5fa3c DB |
816 | is used to remove stores with non-constant bases. |
817 | ----------------------------------------------------------------------------*/ | |
818 | ||
819 | /* Delete all of the store_info recs from INSN_INFO. */ | |
820 | ||
b8698a0f | 821 | static void |
6fb5fa3c DB |
822 | free_store_info (insn_info_t insn_info) |
823 | { | |
824 | store_info_t store_info = insn_info->store_rec; | |
825 | while (store_info) | |
826 | { | |
827 | store_info_t next = store_info->next; | |
8dd5516b | 828 | if (store_info->is_large) |
dc491a25 | 829 | BITMAP_FREE (store_info->positions_needed.large.bmap); |
6fb5fa3c DB |
830 | if (store_info->cse_base) |
831 | pool_free (cse_store_info_pool, store_info); | |
832 | else | |
833 | pool_free (rtx_store_info_pool, store_info); | |
834 | store_info = next; | |
835 | } | |
836 | ||
837 | insn_info->cannot_delete = true; | |
838 | insn_info->contains_cselib_groups = false; | |
839 | insn_info->store_rec = NULL; | |
840 | } | |
841 | ||
9e582b1d JR |
842 | typedef struct |
843 | { | |
dc01c3d1 | 844 | rtx_insn *first, *current; |
9e582b1d JR |
845 | regset fixed_regs_live; |
846 | bool failure; | |
847 | } note_add_store_info; | |
848 | ||
849 | /* Callback for emit_inc_dec_insn_before via note_stores. | |
850 | Check if a register is clobbered which is live afterwards. */ | |
851 | ||
852 | static void | |
853 | note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data) | |
854 | { | |
dc01c3d1 | 855 | rtx_insn *insn; |
9e582b1d JR |
856 | note_add_store_info *info = (note_add_store_info *) data; |
857 | int r, n; | |
858 | ||
859 | if (!REG_P (loc)) | |
860 | return; | |
861 | ||
862 | /* If this register is referenced by the current or an earlier insn, | |
863 | that's OK. E.g. this applies to the register that is being incremented | |
864 | with this addition. */ | |
865 | for (insn = info->first; | |
866 | insn != NEXT_INSN (info->current); | |
867 | insn = NEXT_INSN (insn)) | |
868 | if (reg_referenced_p (loc, PATTERN (insn))) | |
869 | return; | |
870 | ||
871 | /* If we come here, we have a clobber of a register that's only OK | |
872 | if that register is not live. If we don't have liveness information | |
873 | available, fail now. */ | |
874 | if (!info->fixed_regs_live) | |
875 | { | |
876 | info->failure = true; | |
877 | return; | |
878 | } | |
879 | /* Now check if this is a live fixed register. */ | |
880 | r = REGNO (loc); | |
881 | n = hard_regno_nregs[r][GET_MODE (loc)]; | |
882 | while (--n >= 0) | |
883 | if (REGNO_REG_SET_P (info->fixed_regs_live, r+n)) | |
884 | info->failure = true; | |
885 | } | |
886 | ||
4deef538 AO |
887 | /* Callback for for_each_inc_dec that emits an INSN that sets DEST to |
888 | SRC + SRCOFF before insn ARG. */ | |
6fb5fa3c DB |
889 | |
890 | static int | |
4deef538 AO |
891 | emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED, |
892 | rtx op ATTRIBUTE_UNUSED, | |
893 | rtx dest, rtx src, rtx srcoff, void *arg) | |
6fb5fa3c | 894 | { |
9e582b1d | 895 | insn_info_t insn_info = (insn_info_t) arg; |
dc01c3d1 | 896 | rtx_insn *insn = insn_info->insn, *new_insn, *cur; |
9e582b1d | 897 | note_add_store_info info; |
b8698a0f | 898 | |
4deef538 AO |
899 | /* We can reuse all operands without copying, because we are about |
900 | to delete the insn that contained it. */ | |
9e582b1d | 901 | if (srcoff) |
ed079c4b JJ |
902 | { |
903 | start_sequence (); | |
904 | emit_insn (gen_add3_insn (dest, src, srcoff)); | |
905 | new_insn = get_insns (); | |
906 | end_sequence (); | |
907 | } | |
9e582b1d | 908 | else |
dc01c3d1 | 909 | new_insn = as_a <rtx_insn *> (gen_move_insn (dest, src)); |
9e582b1d JR |
910 | info.first = new_insn; |
911 | info.fixed_regs_live = insn_info->fixed_regs_live; | |
912 | info.failure = false; | |
913 | for (cur = new_insn; cur; cur = NEXT_INSN (cur)) | |
914 | { | |
915 | info.current = cur; | |
916 | note_stores (PATTERN (cur), note_add_store, &info); | |
917 | } | |
6fb5fa3c | 918 | |
9e582b1d JR |
919 | /* If a failure was flagged above, return 1 so that for_each_inc_dec will |
920 | return it immediately, communicating the failure to its caller. */ | |
921 | if (info.failure) | |
922 | return 1; | |
923 | ||
924 | emit_insn_before (new_insn, insn); | |
6fb5fa3c | 925 | |
8d8e205b | 926 | return 0; |
6fb5fa3c DB |
927 | } |
928 | ||
9e582b1d JR |
929 | /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it |
930 | is there, is split into a separate insn. | |
931 | Return true on success (or if there was nothing to do), false on failure. */ | |
6fb5fa3c | 932 | |
9e582b1d JR |
933 | static bool |
934 | check_for_inc_dec_1 (insn_info_t insn_info) | |
6fb5fa3c | 935 | { |
dd60a84c | 936 | rtx_insn *insn = insn_info->insn; |
6fb5fa3c DB |
937 | rtx note = find_reg_note (insn, REG_INC, NULL_RTX); |
938 | if (note) | |
8d8e205b RS |
939 | return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before, |
940 | insn_info) == 0; | |
9e582b1d | 941 | return true; |
6fb5fa3c DB |
942 | } |
943 | ||
944 | ||
9e582b1d JR |
945 | /* Entry point for postreload. If you work on reload_cse, or you need this |
946 | anywhere else, consider if you can provide register liveness information | |
947 | and add a parameter to this function so that it can be passed down in | |
948 | insn_info.fixed_regs_live. */ | |
949 | bool | |
dd60a84c | 950 | check_for_inc_dec (rtx_insn *insn) |
9e582b1d JR |
951 | { |
952 | struct insn_info insn_info; | |
953 | rtx note; | |
954 | ||
955 | insn_info.insn = insn; | |
956 | insn_info.fixed_regs_live = NULL; | |
957 | note = find_reg_note (insn, REG_INC, NULL_RTX); | |
958 | if (note) | |
8d8e205b RS |
959 | return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before, |
960 | &insn_info) == 0; | |
9e582b1d JR |
961 | return true; |
962 | } | |
963 | ||
b8698a0f | 964 | /* Delete the insn and free all of the fields inside INSN_INFO. */ |
6fb5fa3c DB |
965 | |
966 | static void | |
967 | delete_dead_store_insn (insn_info_t insn_info) | |
968 | { | |
969 | read_info_t read_info; | |
970 | ||
971 | if (!dbg_cnt (dse)) | |
972 | return; | |
973 | ||
9e582b1d JR |
974 | if (!check_for_inc_dec_1 (insn_info)) |
975 | return; | |
456610d3 | 976 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 977 | { |
b8698a0f | 978 | fprintf (dump_file, "Locally deleting insn %d ", |
6fb5fa3c DB |
979 | INSN_UID (insn_info->insn)); |
980 | if (insn_info->store_rec->alias_set) | |
b8698a0f | 981 | fprintf (dump_file, "alias set %d\n", |
4862826d | 982 | (int) insn_info->store_rec->alias_set); |
6fb5fa3c DB |
983 | else |
984 | fprintf (dump_file, "\n"); | |
985 | } | |
986 | ||
987 | free_store_info (insn_info); | |
988 | read_info = insn_info->read_rec; | |
b8698a0f | 989 | |
6fb5fa3c DB |
990 | while (read_info) |
991 | { | |
992 | read_info_t next = read_info->next; | |
993 | pool_free (read_info_pool, read_info); | |
994 | read_info = next; | |
995 | } | |
996 | insn_info->read_rec = NULL; | |
997 | ||
998 | delete_insn (insn_info->insn); | |
999 | locally_deleted++; | |
1000 | insn_info->insn = NULL; | |
1001 | ||
1002 | insn_info->wild_read = false; | |
1003 | } | |
1004 | ||
88d8330d EB |
1005 | /* Return whether DECL, a local variable, can possibly escape the current |
1006 | function scope. */ | |
1007 | ||
1008 | static bool | |
1009 | local_variable_can_escape (tree decl) | |
1010 | { | |
1011 | if (TREE_ADDRESSABLE (decl)) | |
1012 | return true; | |
1013 | ||
1014 | /* If this is a partitioned variable, we need to consider all the variables | |
1015 | in the partition. This is necessary because a store into one of them can | |
1016 | be replaced with a store into another and this may not change the outcome | |
1017 | of the escape analysis. */ | |
1018 | if (cfun->gimple_df->decls_to_pointers != NULL) | |
1019 | { | |
39c8aaa4 | 1020 | tree *namep = cfun->gimple_df->decls_to_pointers->get (decl); |
88d8330d | 1021 | if (namep) |
39c8aaa4 | 1022 | return TREE_ADDRESSABLE (*namep); |
88d8330d EB |
1023 | } |
1024 | ||
1025 | return false; | |
1026 | } | |
1027 | ||
1028 | /* Return whether EXPR can possibly escape the current function scope. */ | |
1029 | ||
d26c7090 ER |
1030 | static bool |
1031 | can_escape (tree expr) | |
1032 | { | |
1033 | tree base; | |
1034 | if (!expr) | |
1035 | return true; | |
1036 | base = get_base_address (expr); | |
1037 | if (DECL_P (base) | |
88d8330d EB |
1038 | && !may_be_aliased (base) |
1039 | && !(TREE_CODE (base) == VAR_DECL | |
1040 | && !DECL_EXTERNAL (base) | |
1041 | && !TREE_STATIC (base) | |
1042 | && local_variable_can_escape (base))) | |
d26c7090 ER |
1043 | return false; |
1044 | return true; | |
1045 | } | |
6fb5fa3c DB |
1046 | |
1047 | /* Set the store* bitmaps offset_map_size* fields in GROUP based on | |
1048 | OFFSET and WIDTH. */ | |
1049 | ||
1050 | static void | |
d26c7090 ER |
1051 | set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width, |
1052 | tree expr) | |
6fb5fa3c DB |
1053 | { |
1054 | HOST_WIDE_INT i; | |
d26c7090 | 1055 | bool expr_escapes = can_escape (expr); |
8dd5516b | 1056 | if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET) |
6fb5fa3c DB |
1057 | for (i=offset; i<offset+width; i++) |
1058 | { | |
1059 | bitmap store1; | |
1060 | bitmap store2; | |
d26c7090 | 1061 | bitmap escaped; |
6fb5fa3c DB |
1062 | int ai; |
1063 | if (i < 0) | |
1064 | { | |
1065 | store1 = group->store1_n; | |
1066 | store2 = group->store2_n; | |
d26c7090 | 1067 | escaped = group->escaped_n; |
6fb5fa3c DB |
1068 | ai = -i; |
1069 | } | |
1070 | else | |
1071 | { | |
1072 | store1 = group->store1_p; | |
1073 | store2 = group->store2_p; | |
d26c7090 | 1074 | escaped = group->escaped_p; |
6fb5fa3c DB |
1075 | ai = i; |
1076 | } | |
b8698a0f | 1077 | |
fcaa4ca4 | 1078 | if (!bitmap_set_bit (store1, ai)) |
6fb5fa3c | 1079 | bitmap_set_bit (store2, ai); |
b8698a0f | 1080 | else |
6fb5fa3c | 1081 | { |
6fb5fa3c DB |
1082 | if (i < 0) |
1083 | { | |
1084 | if (group->offset_map_size_n < ai) | |
1085 | group->offset_map_size_n = ai; | |
1086 | } | |
1087 | else | |
1088 | { | |
1089 | if (group->offset_map_size_p < ai) | |
1090 | group->offset_map_size_p = ai; | |
1091 | } | |
1092 | } | |
d26c7090 ER |
1093 | if (expr_escapes) |
1094 | bitmap_set_bit (escaped, ai); | |
6fb5fa3c DB |
1095 | } |
1096 | } | |
1097 | ||
d26c7090 ER |
1098 | static void |
1099 | reset_active_stores (void) | |
1100 | { | |
1101 | active_local_stores = NULL; | |
1102 | active_local_stores_len = 0; | |
1103 | } | |
6fb5fa3c | 1104 | |
d26c7090 | 1105 | /* Free all READ_REC of the LAST_INSN of BB_INFO. */ |
6fb5fa3c DB |
1106 | |
1107 | static void | |
d26c7090 | 1108 | free_read_records (bb_info_t bb_info) |
6fb5fa3c DB |
1109 | { |
1110 | insn_info_t insn_info = bb_info->last_insn; | |
1111 | read_info_t *ptr = &insn_info->read_rec; | |
6fb5fa3c DB |
1112 | while (*ptr) |
1113 | { | |
1114 | read_info_t next = (*ptr)->next; | |
4862826d | 1115 | if ((*ptr)->alias_set == 0) |
6fb5fa3c DB |
1116 | { |
1117 | pool_free (read_info_pool, *ptr); | |
1118 | *ptr = next; | |
d26c7090 | 1119 | } |
b8698a0f | 1120 | else |
d26c7090 | 1121 | ptr = &(*ptr)->next; |
6fb5fa3c | 1122 | } |
d26c7090 ER |
1123 | } |
1124 | ||
1125 | /* Set the BB_INFO so that the last insn is marked as a wild read. */ | |
1126 | ||
1127 | static void | |
1128 | add_wild_read (bb_info_t bb_info) | |
1129 | { | |
1130 | insn_info_t insn_info = bb_info->last_insn; | |
6fb5fa3c | 1131 | insn_info->wild_read = true; |
d26c7090 ER |
1132 | free_read_records (bb_info); |
1133 | reset_active_stores (); | |
6fb5fa3c DB |
1134 | } |
1135 | ||
d26c7090 ER |
1136 | /* Set the BB_INFO so that the last insn is marked as a wild read of |
1137 | non-frame locations. */ | |
1138 | ||
1139 | static void | |
1140 | add_non_frame_wild_read (bb_info_t bb_info) | |
1141 | { | |
1142 | insn_info_t insn_info = bb_info->last_insn; | |
1143 | insn_info->non_frame_wild_read = true; | |
1144 | free_read_records (bb_info); | |
1145 | reset_active_stores (); | |
1146 | } | |
6fb5fa3c | 1147 | |
50f0f366 EB |
1148 | /* Return true if X is a constant or one of the registers that behave |
1149 | as a constant over the life of a function. This is equivalent to | |
1150 | !rtx_varies_p for memory addresses. */ | |
6fb5fa3c DB |
1151 | |
1152 | static bool | |
1153 | const_or_frame_p (rtx x) | |
1154 | { | |
d8116890 KZ |
1155 | if (CONSTANT_P (x)) |
1156 | return true; | |
1157 | ||
1158 | if (GET_CODE (x) == REG) | |
6fb5fa3c | 1159 | { |
6fb5fa3c DB |
1160 | /* Note that we have to test for the actual rtx used for the frame |
1161 | and arg pointers and not just the register number in case we have | |
1162 | eliminated the frame and/or arg pointer and are using it | |
1163 | for pseudos. */ | |
1164 | if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx | |
1165 | /* The arg pointer varies if it is not a fixed register. */ | |
1166 | || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]) | |
1167 | || x == pic_offset_table_rtx) | |
1168 | return true; | |
1169 | return false; | |
6fb5fa3c | 1170 | } |
d8116890 KZ |
1171 | |
1172 | return false; | |
6fb5fa3c DB |
1173 | } |
1174 | ||
b8698a0f L |
1175 | /* Take all reasonable action to put the address of MEM into the form |
1176 | that we can do analysis on. | |
6fb5fa3c DB |
1177 | |
1178 | The gold standard is to get the address into the form: address + | |
1179 | OFFSET where address is something that rtx_varies_p considers a | |
1180 | constant. When we can get the address in this form, we can do | |
1181 | global analysis on it. Note that for constant bases, address is | |
1182 | not actually returned, only the group_id. The address can be | |
1183 | obtained from that. | |
1184 | ||
1185 | If that fails, we try cselib to get a value we can at least use | |
b8698a0f L |
1186 | locally. If that fails we return false. |
1187 | ||
6fb5fa3c DB |
1188 | The GROUP_ID is set to -1 for cselib bases and the index of the |
1189 | group for non_varying bases. | |
1190 | ||
1191 | FOR_READ is true if this is a mem read and false if not. */ | |
1192 | ||
1193 | static bool | |
1194 | canon_address (rtx mem, | |
4862826d | 1195 | alias_set_type *alias_set_out, |
6fb5fa3c | 1196 | int *group_id, |
b8698a0f | 1197 | HOST_WIDE_INT *offset, |
6fb5fa3c DB |
1198 | cselib_val **base) |
1199 | { | |
ef4bddc2 | 1200 | machine_mode address_mode = get_address_mode (mem); |
6fb5fa3c DB |
1201 | rtx mem_address = XEXP (mem, 0); |
1202 | rtx expanded_address, address; | |
403c7520 JJ |
1203 | int expanded; |
1204 | ||
6fb5fa3c DB |
1205 | *alias_set_out = 0; |
1206 | ||
4deef538 | 1207 | cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem)); |
6fb5fa3c | 1208 | |
456610d3 | 1209 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
1210 | { |
1211 | fprintf (dump_file, " mem: "); | |
1212 | print_inline_rtx (dump_file, mem_address, 0); | |
1213 | fprintf (dump_file, "\n"); | |
1214 | } | |
1215 | ||
403c7520 JJ |
1216 | /* First see if just canon_rtx (mem_address) is const or frame, |
1217 | if not, try cselib_expand_value_rtx and call canon_rtx on that. */ | |
1218 | address = NULL_RTX; | |
1219 | for (expanded = 0; expanded < 2; expanded++) | |
1220 | { | |
1221 | if (expanded) | |
1222 | { | |
1223 | /* Use cselib to replace all of the reg references with the full | |
b8698a0f | 1224 | expression. This will take care of the case where we have |
6fb5fa3c | 1225 | |
403c7520 JJ |
1226 | r_x = base + offset; |
1227 | val = *r_x; | |
b8698a0f L |
1228 | |
1229 | by making it into | |
6fb5fa3c | 1230 | |
403c7520 | 1231 | val = *(base + offset); */ |
6fb5fa3c | 1232 | |
403c7520 JJ |
1233 | expanded_address = cselib_expand_value_rtx (mem_address, |
1234 | scratch, 5); | |
6fb5fa3c | 1235 | |
403c7520 JJ |
1236 | /* If this fails, just go with the address from first |
1237 | iteration. */ | |
1238 | if (!expanded_address) | |
1239 | break; | |
1240 | } | |
1241 | else | |
1242 | expanded_address = mem_address; | |
6fb5fa3c | 1243 | |
403c7520 JJ |
1244 | /* Split the address into canonical BASE + OFFSET terms. */ |
1245 | address = canon_rtx (expanded_address); | |
6fb5fa3c | 1246 | |
403c7520 | 1247 | *offset = 0; |
6fb5fa3c | 1248 | |
456610d3 | 1249 | if (dump_file && (dump_flags & TDF_DETAILS)) |
403c7520 JJ |
1250 | { |
1251 | if (expanded) | |
1252 | { | |
1253 | fprintf (dump_file, "\n after cselib_expand address: "); | |
1254 | print_inline_rtx (dump_file, expanded_address, 0); | |
1255 | fprintf (dump_file, "\n"); | |
1256 | } | |
6fb5fa3c | 1257 | |
403c7520 JJ |
1258 | fprintf (dump_file, "\n after canon_rtx address: "); |
1259 | print_inline_rtx (dump_file, address, 0); | |
1260 | fprintf (dump_file, "\n"); | |
1261 | } | |
6fb5fa3c | 1262 | |
403c7520 JJ |
1263 | if (GET_CODE (address) == CONST) |
1264 | address = XEXP (address, 0); | |
6fb5fa3c | 1265 | |
403c7520 JJ |
1266 | if (GET_CODE (address) == PLUS |
1267 | && CONST_INT_P (XEXP (address, 1))) | |
1268 | { | |
1269 | *offset = INTVAL (XEXP (address, 1)); | |
1270 | address = XEXP (address, 0); | |
1271 | } | |
6fb5fa3c | 1272 | |
09e881c9 BE |
1273 | if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem)) |
1274 | && const_or_frame_p (address)) | |
6fb5fa3c | 1275 | { |
403c7520 JJ |
1276 | group_info_t group = get_group_info (address); |
1277 | ||
456610d3 | 1278 | if (dump_file && (dump_flags & TDF_DETAILS)) |
403c7520 JJ |
1279 | fprintf (dump_file, " gid=%d offset=%d \n", |
1280 | group->id, (int)*offset); | |
1281 | *base = NULL; | |
1282 | *group_id = group->id; | |
1283 | return true; | |
6fb5fa3c | 1284 | } |
403c7520 JJ |
1285 | } |
1286 | ||
4deef538 | 1287 | *base = cselib_lookup (address, address_mode, true, GET_MODE (mem)); |
403c7520 JJ |
1288 | *group_id = -1; |
1289 | ||
1290 | if (*base == NULL) | |
1291 | { | |
456610d3 | 1292 | if (dump_file && (dump_flags & TDF_DETAILS)) |
403c7520 JJ |
1293 | fprintf (dump_file, " no cselib val - should be a wild read.\n"); |
1294 | return false; | |
6fb5fa3c | 1295 | } |
456610d3 | 1296 | if (dump_file && (dump_flags & TDF_DETAILS)) |
5440c0e7 AO |
1297 | fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n", |
1298 | (*base)->uid, (*base)->hash, (int)*offset); | |
6fb5fa3c DB |
1299 | return true; |
1300 | } | |
1301 | ||
1302 | ||
1303 | /* Clear the rhs field from the active_local_stores array. */ | |
1304 | ||
1305 | static void | |
1306 | clear_rhs_from_active_local_stores (void) | |
1307 | { | |
1308 | insn_info_t ptr = active_local_stores; | |
1309 | ||
1310 | while (ptr) | |
1311 | { | |
1312 | store_info_t store_info = ptr->store_rec; | |
1313 | /* Skip the clobbers. */ | |
1314 | while (!store_info->is_set) | |
1315 | store_info = store_info->next; | |
1316 | ||
1317 | store_info->rhs = NULL; | |
8dd5516b | 1318 | store_info->const_rhs = NULL; |
6fb5fa3c DB |
1319 | |
1320 | ptr = ptr->next_local_store; | |
1321 | } | |
1322 | } | |
1323 | ||
1324 | ||
8dd5516b JJ |
1325 | /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */ |
1326 | ||
1327 | static inline void | |
1328 | set_position_unneeded (store_info_t s_info, int pos) | |
1329 | { | |
1330 | if (__builtin_expect (s_info->is_large, false)) | |
1331 | { | |
fcaa4ca4 NF |
1332 | if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos)) |
1333 | s_info->positions_needed.large.count++; | |
8dd5516b JJ |
1334 | } |
1335 | else | |
1336 | s_info->positions_needed.small_bitmask | |
1337 | &= ~(((unsigned HOST_WIDE_INT) 1) << pos); | |
1338 | } | |
1339 | ||
1340 | /* Mark the whole store S_INFO as unneeded. */ | |
1341 | ||
1342 | static inline void | |
1343 | set_all_positions_unneeded (store_info_t s_info) | |
1344 | { | |
1345 | if (__builtin_expect (s_info->is_large, false)) | |
1346 | { | |
1347 | int pos, end = s_info->end - s_info->begin; | |
1348 | for (pos = 0; pos < end; pos++) | |
dc491a25 | 1349 | bitmap_set_bit (s_info->positions_needed.large.bmap, pos); |
8dd5516b JJ |
1350 | s_info->positions_needed.large.count = end; |
1351 | } | |
1352 | else | |
1353 | s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0; | |
1354 | } | |
1355 | ||
1356 | /* Return TRUE if any bytes from S_INFO store are needed. */ | |
1357 | ||
1358 | static inline bool | |
1359 | any_positions_needed_p (store_info_t s_info) | |
1360 | { | |
1361 | if (__builtin_expect (s_info->is_large, false)) | |
1362 | return (s_info->positions_needed.large.count | |
1363 | < s_info->end - s_info->begin); | |
1364 | else | |
1365 | return (s_info->positions_needed.small_bitmask | |
1366 | != (unsigned HOST_WIDE_INT) 0); | |
1367 | } | |
1368 | ||
1369 | /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO | |
1370 | store are needed. */ | |
1371 | ||
1372 | static inline bool | |
1373 | all_positions_needed_p (store_info_t s_info, int start, int width) | |
1374 | { | |
1375 | if (__builtin_expect (s_info->is_large, false)) | |
1376 | { | |
1377 | int end = start + width; | |
1378 | while (start < end) | |
dc491a25 | 1379 | if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++)) |
8dd5516b JJ |
1380 | return false; |
1381 | return true; | |
1382 | } | |
1383 | else | |
1384 | { | |
1385 | unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start; | |
1386 | return (s_info->positions_needed.small_bitmask & mask) == mask; | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | ||
ef4bddc2 | 1391 | static rtx get_stored_val (store_info_t, machine_mode, HOST_WIDE_INT, |
8dd5516b JJ |
1392 | HOST_WIDE_INT, basic_block, bool); |
1393 | ||
1394 | ||
6fb5fa3c DB |
1395 | /* BODY is an instruction pattern that belongs to INSN. Return 1 if |
1396 | there is a candidate store, after adding it to the appropriate | |
1397 | local store group if so. */ | |
1398 | ||
1399 | static int | |
1400 | record_store (rtx body, bb_info_t bb_info) | |
1401 | { | |
6216f94e | 1402 | rtx mem, rhs, const_rhs, mem_addr; |
6fb5fa3c DB |
1403 | HOST_WIDE_INT offset = 0; |
1404 | HOST_WIDE_INT width = 0; | |
4862826d | 1405 | alias_set_type spill_alias_set; |
6fb5fa3c DB |
1406 | insn_info_t insn_info = bb_info->last_insn; |
1407 | store_info_t store_info = NULL; | |
1408 | int group_id; | |
1409 | cselib_val *base = NULL; | |
8dd5516b | 1410 | insn_info_t ptr, last, redundant_reason; |
6fb5fa3c DB |
1411 | bool store_is_unused; |
1412 | ||
1413 | if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER) | |
1414 | return 0; | |
1415 | ||
8dd5516b JJ |
1416 | mem = SET_DEST (body); |
1417 | ||
6fb5fa3c DB |
1418 | /* If this is not used, then this cannot be used to keep the insn |
1419 | from being deleted. On the other hand, it does provide something | |
1420 | that can be used to prove that another store is dead. */ | |
1421 | store_is_unused | |
8dd5516b | 1422 | = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL); |
6fb5fa3c DB |
1423 | |
1424 | /* Check whether that value is a suitable memory location. */ | |
6fb5fa3c DB |
1425 | if (!MEM_P (mem)) |
1426 | { | |
1427 | /* If the set or clobber is unused, then it does not effect our | |
1428 | ability to get rid of the entire insn. */ | |
1429 | if (!store_is_unused) | |
1430 | insn_info->cannot_delete = true; | |
1431 | return 0; | |
1432 | } | |
1433 | ||
1434 | /* At this point we know mem is a mem. */ | |
1435 | if (GET_MODE (mem) == BLKmode) | |
1436 | { | |
1437 | if (GET_CODE (XEXP (mem, 0)) == SCRATCH) | |
1438 | { | |
456610d3 | 1439 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
1440 | fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n"); |
1441 | add_wild_read (bb_info); | |
1442 | insn_info->cannot_delete = true; | |
8dd5516b | 1443 | return 0; |
6fb5fa3c | 1444 | } |
8dd5516b JJ |
1445 | /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0)) |
1446 | as memset (addr, 0, 36); */ | |
f5541398 RS |
1447 | else if (!MEM_SIZE_KNOWN_P (mem) |
1448 | || MEM_SIZE (mem) <= 0 | |
1449 | || MEM_SIZE (mem) > MAX_OFFSET | |
8dd5516b | 1450 | || GET_CODE (body) != SET |
8dd5516b | 1451 | || !CONST_INT_P (SET_SRC (body))) |
6fb5fa3c | 1452 | { |
8dd5516b JJ |
1453 | if (!store_is_unused) |
1454 | { | |
1455 | /* If the set or clobber is unused, then it does not effect our | |
1456 | ability to get rid of the entire insn. */ | |
1457 | insn_info->cannot_delete = true; | |
1458 | clear_rhs_from_active_local_stores (); | |
1459 | } | |
1460 | return 0; | |
6fb5fa3c | 1461 | } |
6fb5fa3c DB |
1462 | } |
1463 | ||
1464 | /* We can still process a volatile mem, we just cannot delete it. */ | |
1465 | if (MEM_VOLATILE_P (mem)) | |
8dd5516b | 1466 | insn_info->cannot_delete = true; |
6fb5fa3c DB |
1467 | |
1468 | if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base)) | |
1469 | { | |
1470 | clear_rhs_from_active_local_stores (); | |
1471 | return 0; | |
1472 | } | |
1473 | ||
8dd5516b | 1474 | if (GET_MODE (mem) == BLKmode) |
f5541398 | 1475 | width = MEM_SIZE (mem); |
8dd5516b | 1476 | else |
6f557e0e | 1477 | width = GET_MODE_SIZE (GET_MODE (mem)); |
6fb5fa3c DB |
1478 | |
1479 | if (spill_alias_set) | |
1480 | { | |
1481 | bitmap store1 = clear_alias_group->store1_p; | |
1482 | bitmap store2 = clear_alias_group->store2_p; | |
8dd5516b JJ |
1483 | |
1484 | gcc_assert (GET_MODE (mem) != BLKmode); | |
b8698a0f | 1485 | |
fcaa4ca4 | 1486 | if (!bitmap_set_bit (store1, spill_alias_set)) |
6fb5fa3c | 1487 | bitmap_set_bit (store2, spill_alias_set); |
b8698a0f | 1488 | |
6fb5fa3c DB |
1489 | if (clear_alias_group->offset_map_size_p < spill_alias_set) |
1490 | clear_alias_group->offset_map_size_p = spill_alias_set; | |
b8698a0f | 1491 | |
f883e0a7 | 1492 | store_info = (store_info_t) pool_alloc (rtx_store_info_pool); |
6fb5fa3c | 1493 | |
456610d3 | 1494 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 1495 | fprintf (dump_file, " processing spill store %d(%s)\n", |
4862826d | 1496 | (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem))); |
6fb5fa3c DB |
1497 | } |
1498 | else if (group_id >= 0) | |
1499 | { | |
1500 | /* In the restrictive case where the base is a constant or the | |
1501 | frame pointer we can do global analysis. */ | |
b8698a0f L |
1502 | |
1503 | group_info_t group | |
9771b263 | 1504 | = rtx_group_vec[group_id]; |
d26c7090 | 1505 | tree expr = MEM_EXPR (mem); |
b8698a0f | 1506 | |
f883e0a7 | 1507 | store_info = (store_info_t) pool_alloc (rtx_store_info_pool); |
d26c7090 | 1508 | set_usage_bits (group, offset, width, expr); |
6fb5fa3c | 1509 | |
456610d3 | 1510 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
1511 | fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n", |
1512 | group_id, (int)offset, (int)(offset+width)); | |
1513 | } | |
1514 | else | |
1515 | { | |
9e412ca3 | 1516 | if (may_be_sp_based_p (XEXP (mem, 0))) |
50f0f366 | 1517 | insn_info->stack_pointer_based = true; |
6fb5fa3c | 1518 | insn_info->contains_cselib_groups = true; |
50f0f366 | 1519 | |
f883e0a7 | 1520 | store_info = (store_info_t) pool_alloc (cse_store_info_pool); |
6fb5fa3c DB |
1521 | group_id = -1; |
1522 | ||
456610d3 | 1523 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
1524 | fprintf (dump_file, " processing cselib store [%d..%d)\n", |
1525 | (int)offset, (int)(offset+width)); | |
1526 | } | |
1527 | ||
8dd5516b JJ |
1528 | const_rhs = rhs = NULL_RTX; |
1529 | if (GET_CODE (body) == SET | |
1530 | /* No place to keep the value after ra. */ | |
1531 | && !reload_completed | |
1532 | && (REG_P (SET_SRC (body)) | |
1533 | || GET_CODE (SET_SRC (body)) == SUBREG | |
1534 | || CONSTANT_P (SET_SRC (body))) | |
1535 | && !MEM_VOLATILE_P (mem) | |
1536 | /* Sometimes the store and reload is used for truncation and | |
1537 | rounding. */ | |
1538 | && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store))) | |
1539 | { | |
1540 | rhs = SET_SRC (body); | |
1541 | if (CONSTANT_P (rhs)) | |
1542 | const_rhs = rhs; | |
1543 | else if (body == PATTERN (insn_info->insn)) | |
1544 | { | |
1545 | rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX); | |
1546 | if (tem && CONSTANT_P (XEXP (tem, 0))) | |
1547 | const_rhs = XEXP (tem, 0); | |
1548 | } | |
1549 | if (const_rhs == NULL_RTX && REG_P (rhs)) | |
1550 | { | |
1551 | rtx tem = cselib_expand_value_rtx (rhs, scratch, 5); | |
1552 | ||
1553 | if (tem && CONSTANT_P (tem)) | |
1554 | const_rhs = tem; | |
1555 | } | |
1556 | } | |
1557 | ||
6fb5fa3c DB |
1558 | /* Check to see if this stores causes some other stores to be |
1559 | dead. */ | |
1560 | ptr = active_local_stores; | |
1561 | last = NULL; | |
8dd5516b | 1562 | redundant_reason = NULL; |
6216f94e JJ |
1563 | mem = canon_rtx (mem); |
1564 | /* For alias_set != 0 canon_true_dependence should be never called. */ | |
1565 | if (spill_alias_set) | |
1566 | mem_addr = NULL_RTX; | |
1567 | else | |
1568 | { | |
1569 | if (group_id < 0) | |
1570 | mem_addr = base->val_rtx; | |
1571 | else | |
1572 | { | |
1573 | group_info_t group | |
9771b263 | 1574 | = rtx_group_vec[group_id]; |
6216f94e JJ |
1575 | mem_addr = group->canon_base_addr; |
1576 | } | |
1577 | if (offset) | |
372d6395 | 1578 | mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset); |
6216f94e | 1579 | } |
6fb5fa3c DB |
1580 | |
1581 | while (ptr) | |
1582 | { | |
1583 | insn_info_t next = ptr->next_local_store; | |
1584 | store_info_t s_info = ptr->store_rec; | |
60564289 | 1585 | bool del = true; |
6fb5fa3c DB |
1586 | |
1587 | /* Skip the clobbers. We delete the active insn if this insn | |
6ed3da00 | 1588 | shadows the set. To have been put on the active list, it |
6fb5fa3c DB |
1589 | has exactly on set. */ |
1590 | while (!s_info->is_set) | |
1591 | s_info = s_info->next; | |
1592 | ||
1593 | if (s_info->alias_set != spill_alias_set) | |
60564289 | 1594 | del = false; |
6fb5fa3c DB |
1595 | else if (s_info->alias_set) |
1596 | { | |
b8698a0f | 1597 | struct clear_alias_mode_holder *entry |
6fb5fa3c DB |
1598 | = clear_alias_set_lookup (s_info->alias_set); |
1599 | /* Generally, spills cannot be processed if and of the | |
1600 | references to the slot have a different mode. But if | |
1601 | we are in the same block and mode is exactly the same | |
1602 | between this store and one before in the same block, | |
1603 | we can still delete it. */ | |
1604 | if ((GET_MODE (mem) == GET_MODE (s_info->mem)) | |
1605 | && (GET_MODE (mem) == entry->mode)) | |
1606 | { | |
60564289 | 1607 | del = true; |
8dd5516b | 1608 | set_all_positions_unneeded (s_info); |
6fb5fa3c | 1609 | } |
456610d3 | 1610 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 1611 | fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n", |
4862826d | 1612 | INSN_UID (ptr->insn), (int) s_info->alias_set); |
6fb5fa3c | 1613 | } |
b8698a0f | 1614 | else if ((s_info->group_id == group_id) |
6fb5fa3c DB |
1615 | && (s_info->cse_base == base)) |
1616 | { | |
1617 | HOST_WIDE_INT i; | |
456610d3 | 1618 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 1619 | fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n", |
b8698a0f | 1620 | INSN_UID (ptr->insn), s_info->group_id, |
6fb5fa3c | 1621 | (int)s_info->begin, (int)s_info->end); |
8dd5516b JJ |
1622 | |
1623 | /* Even if PTR won't be eliminated as unneeded, if both | |
1624 | PTR and this insn store the same constant value, we might | |
1625 | eliminate this insn instead. */ | |
1626 | if (s_info->const_rhs | |
1627 | && const_rhs | |
1628 | && offset >= s_info->begin | |
1629 | && offset + width <= s_info->end | |
1630 | && all_positions_needed_p (s_info, offset - s_info->begin, | |
1631 | width)) | |
1632 | { | |
1633 | if (GET_MODE (mem) == BLKmode) | |
1634 | { | |
1635 | if (GET_MODE (s_info->mem) == BLKmode | |
1636 | && s_info->const_rhs == const_rhs) | |
1637 | redundant_reason = ptr; | |
1638 | } | |
1639 | else if (s_info->const_rhs == const0_rtx | |
1640 | && const_rhs == const0_rtx) | |
1641 | redundant_reason = ptr; | |
1642 | else | |
1643 | { | |
1644 | rtx val; | |
1645 | start_sequence (); | |
1646 | val = get_stored_val (s_info, GET_MODE (mem), | |
1647 | offset, offset + width, | |
1648 | BLOCK_FOR_INSN (insn_info->insn), | |
1649 | true); | |
1650 | if (get_insns () != NULL) | |
1651 | val = NULL_RTX; | |
1652 | end_sequence (); | |
1653 | if (val && rtx_equal_p (val, const_rhs)) | |
1654 | redundant_reason = ptr; | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | for (i = MAX (offset, s_info->begin); | |
1659 | i < offset + width && i < s_info->end; | |
1660 | i++) | |
1661 | set_position_unneeded (s_info, i - s_info->begin); | |
6fb5fa3c DB |
1662 | } |
1663 | else if (s_info->rhs) | |
1664 | /* Need to see if it is possible for this store to overwrite | |
1665 | the value of store_info. If it is, set the rhs to NULL to | |
1666 | keep it from being used to remove a load. */ | |
1667 | { | |
b8698a0f | 1668 | if (canon_true_dependence (s_info->mem, |
6fb5fa3c DB |
1669 | GET_MODE (s_info->mem), |
1670 | s_info->mem_addr, | |
53d9622b | 1671 | mem, mem_addr)) |
8dd5516b JJ |
1672 | { |
1673 | s_info->rhs = NULL; | |
1674 | s_info->const_rhs = NULL; | |
1675 | } | |
6fb5fa3c | 1676 | } |
6216f94e | 1677 | |
6fb5fa3c DB |
1678 | /* An insn can be deleted if every position of every one of |
1679 | its s_infos is zero. */ | |
1b6fa860 | 1680 | if (any_positions_needed_p (s_info)) |
60564289 | 1681 | del = false; |
8dd5516b | 1682 | |
60564289 | 1683 | if (del) |
6fb5fa3c DB |
1684 | { |
1685 | insn_info_t insn_to_delete = ptr; | |
b8698a0f | 1686 | |
dabd47e7 | 1687 | active_local_stores_len--; |
6fb5fa3c DB |
1688 | if (last) |
1689 | last->next_local_store = ptr->next_local_store; | |
1690 | else | |
1691 | active_local_stores = ptr->next_local_store; | |
b8698a0f | 1692 | |
1b6fa860 JJ |
1693 | if (!insn_to_delete->cannot_delete) |
1694 | delete_dead_store_insn (insn_to_delete); | |
6fb5fa3c DB |
1695 | } |
1696 | else | |
1697 | last = ptr; | |
b8698a0f | 1698 | |
6fb5fa3c DB |
1699 | ptr = next; |
1700 | } | |
b8698a0f | 1701 | |
6fb5fa3c DB |
1702 | /* Finish filling in the store_info. */ |
1703 | store_info->next = insn_info->store_rec; | |
1704 | insn_info->store_rec = store_info; | |
6216f94e | 1705 | store_info->mem = mem; |
6fb5fa3c | 1706 | store_info->alias_set = spill_alias_set; |
6216f94e | 1707 | store_info->mem_addr = mem_addr; |
6fb5fa3c | 1708 | store_info->cse_base = base; |
8dd5516b JJ |
1709 | if (width > HOST_BITS_PER_WIDE_INT) |
1710 | { | |
1711 | store_info->is_large = true; | |
1712 | store_info->positions_needed.large.count = 0; | |
3f9b14ff | 1713 | store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack); |
8dd5516b JJ |
1714 | } |
1715 | else | |
1716 | { | |
1717 | store_info->is_large = false; | |
1718 | store_info->positions_needed.small_bitmask = lowpart_bitmask (width); | |
1719 | } | |
6fb5fa3c DB |
1720 | store_info->group_id = group_id; |
1721 | store_info->begin = offset; | |
1722 | store_info->end = offset + width; | |
1723 | store_info->is_set = GET_CODE (body) == SET; | |
8dd5516b JJ |
1724 | store_info->rhs = rhs; |
1725 | store_info->const_rhs = const_rhs; | |
1726 | store_info->redundant_reason = redundant_reason; | |
6fb5fa3c | 1727 | |
6fb5fa3c DB |
1728 | /* If this is a clobber, we return 0. We will only be able to |
1729 | delete this insn if there is only one store USED store, but we | |
1730 | can use the clobber to delete other stores earlier. */ | |
1731 | return store_info->is_set ? 1 : 0; | |
1732 | } | |
1733 | ||
1734 | ||
1735 | static void | |
1736 | dump_insn_info (const char * start, insn_info_t insn_info) | |
1737 | { | |
b8698a0f | 1738 | fprintf (dump_file, "%s insn=%d %s\n", start, |
6fb5fa3c DB |
1739 | INSN_UID (insn_info->insn), |
1740 | insn_info->store_rec ? "has store" : "naked"); | |
1741 | } | |
1742 | ||
1743 | ||
8660aaae EC |
1744 | /* If the modes are different and the value's source and target do not |
1745 | line up, we need to extract the value from lower part of the rhs of | |
1746 | the store, shift it, and then put it into a form that can be shoved | |
1747 | into the read_insn. This function generates a right SHIFT of a | |
1748 | value that is at least ACCESS_SIZE bytes wide of READ_MODE. The | |
1749 | shift sequence is returned or NULL if we failed to find a | |
1750 | shift. */ | |
1751 | ||
1752 | static rtx | |
18b526e8 | 1753 | find_shift_sequence (int access_size, |
8660aaae | 1754 | store_info_t store_info, |
ef4bddc2 | 1755 | machine_mode read_mode, |
8dd5516b | 1756 | int shift, bool speed, bool require_cst) |
8660aaae | 1757 | { |
ef4bddc2 RS |
1758 | machine_mode store_mode = GET_MODE (store_info->mem); |
1759 | machine_mode new_mode; | |
18b526e8 | 1760 | rtx read_reg = NULL; |
8660aaae EC |
1761 | |
1762 | /* Some machines like the x86 have shift insns for each size of | |
1763 | operand. Other machines like the ppc or the ia-64 may only have | |
1764 | shift insns that shift values within 32 or 64 bit registers. | |
1765 | This loop tries to find the smallest shift insn that will right | |
1766 | justify the value we want to read but is available in one insn on | |
1767 | the machine. */ | |
1768 | ||
18b526e8 RS |
1769 | for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT, |
1770 | MODE_INT); | |
1771 | GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD; | |
1772 | new_mode = GET_MODE_WIDER_MODE (new_mode)) | |
8660aaae | 1773 | { |
dc01c3d1 DM |
1774 | rtx target, new_reg, new_lhs; |
1775 | rtx_insn *shift_seq, *insn; | |
d898d29b | 1776 | int cost; |
348eea5f | 1777 | |
72a2609f JJ |
1778 | /* If a constant was stored into memory, try to simplify it here, |
1779 | otherwise the cost of the shift might preclude this optimization | |
1780 | e.g. at -Os, even when no actual shift will be needed. */ | |
8dd5516b | 1781 | if (store_info->const_rhs) |
72a2609f JJ |
1782 | { |
1783 | unsigned int byte = subreg_lowpart_offset (new_mode, store_mode); | |
8dd5516b JJ |
1784 | rtx ret = simplify_subreg (new_mode, store_info->const_rhs, |
1785 | store_mode, byte); | |
72a2609f JJ |
1786 | if (ret && CONSTANT_P (ret)) |
1787 | { | |
1788 | ret = simplify_const_binary_operation (LSHIFTRT, new_mode, | |
1789 | ret, GEN_INT (shift)); | |
1790 | if (ret && CONSTANT_P (ret)) | |
1791 | { | |
1792 | byte = subreg_lowpart_offset (read_mode, new_mode); | |
1793 | ret = simplify_subreg (read_mode, ret, new_mode, byte); | |
1794 | if (ret && CONSTANT_P (ret) | |
5e8f01f4 | 1795 | && set_src_cost (ret, speed) <= COSTS_N_INSNS (1)) |
72a2609f JJ |
1796 | return ret; |
1797 | } | |
1798 | } | |
1799 | } | |
1800 | ||
8dd5516b JJ |
1801 | if (require_cst) |
1802 | return NULL_RTX; | |
1803 | ||
18b526e8 RS |
1804 | /* Try a wider mode if truncating the store mode to NEW_MODE |
1805 | requires a real instruction. */ | |
1806 | if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode) | |
d0edd768 | 1807 | && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode)) |
348eea5f RS |
1808 | continue; |
1809 | ||
18b526e8 RS |
1810 | /* Also try a wider mode if the necessary punning is either not |
1811 | desirable or not possible. */ | |
1812 | if (!CONSTANT_P (store_info->rhs) | |
1813 | && !MODES_TIEABLE_P (new_mode, store_mode)) | |
1814 | continue; | |
18b526e8 | 1815 | |
348eea5f | 1816 | new_reg = gen_reg_rtx (new_mode); |
8660aaae EC |
1817 | |
1818 | start_sequence (); | |
1819 | ||
1820 | /* In theory we could also check for an ashr. Ian Taylor knows | |
1821 | of one dsp where the cost of these two was not the same. But | |
1822 | this really is a rare case anyway. */ | |
1823 | target = expand_binop (new_mode, lshr_optab, new_reg, | |
1824 | GEN_INT (shift), new_reg, 1, OPTAB_DIRECT); | |
1825 | ||
c6f3019a RS |
1826 | shift_seq = get_insns (); |
1827 | end_sequence (); | |
8660aaae | 1828 | |
c6f3019a RS |
1829 | if (target != new_reg || shift_seq == NULL) |
1830 | continue; | |
1831 | ||
1832 | cost = 0; | |
1833 | for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn)) | |
1834 | if (INSN_P (insn)) | |
f40751dd | 1835 | cost += insn_rtx_cost (PATTERN (insn), speed); |
c6f3019a RS |
1836 | |
1837 | /* The computation up to here is essentially independent | |
1838 | of the arguments and could be precomputed. It may | |
1839 | not be worth doing so. We could precompute if | |
1840 | worthwhile or at least cache the results. The result | |
06acf7d0 RS |
1841 | technically depends on both SHIFT and ACCESS_SIZE, |
1842 | but in practice the answer will depend only on ACCESS_SIZE. */ | |
c6f3019a RS |
1843 | |
1844 | if (cost > COSTS_N_INSNS (1)) | |
1845 | continue; | |
1846 | ||
d898d29b JJ |
1847 | new_lhs = extract_low_bits (new_mode, store_mode, |
1848 | copy_rtx (store_info->rhs)); | |
1849 | if (new_lhs == NULL_RTX) | |
1850 | continue; | |
1851 | ||
c6f3019a RS |
1852 | /* We found an acceptable shift. Generate a move to |
1853 | take the value from the store and put it into the | |
1854 | shift pseudo, then shift it, then generate another | |
1855 | move to put in into the target of the read. */ | |
18b526e8 | 1856 | emit_move_insn (new_reg, new_lhs); |
c6f3019a | 1857 | emit_insn (shift_seq); |
18b526e8 | 1858 | read_reg = extract_low_bits (read_mode, new_mode, new_reg); |
c6f3019a | 1859 | break; |
8660aaae EC |
1860 | } |
1861 | ||
18b526e8 | 1862 | return read_reg; |
8660aaae EC |
1863 | } |
1864 | ||
1865 | ||
02b47899 KZ |
1866 | /* Call back for note_stores to find the hard regs set or clobbered by |
1867 | insn. Data is a bitmap of the hardregs set so far. */ | |
1868 | ||
1869 | static void | |
1870 | look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data) | |
1871 | { | |
1872 | bitmap regs_set = (bitmap) data; | |
1873 | ||
1874 | if (REG_P (x) | |
f773c2bd | 1875 | && HARD_REGISTER_P (x)) |
02b47899 | 1876 | { |
f773c2bd AS |
1877 | unsigned int regno = REGNO (x); |
1878 | bitmap_set_range (regs_set, regno, | |
1879 | hard_regno_nregs[regno][GET_MODE (x)]); | |
02b47899 KZ |
1880 | } |
1881 | } | |
1882 | ||
8dd5516b JJ |
1883 | /* Helper function for replace_read and record_store. |
1884 | Attempt to return a value stored in STORE_INFO, from READ_BEGIN | |
1885 | to one before READ_END bytes read in READ_MODE. Return NULL | |
1886 | if not successful. If REQUIRE_CST is true, return always constant. */ | |
1887 | ||
1888 | static rtx | |
ef4bddc2 | 1889 | get_stored_val (store_info_t store_info, machine_mode read_mode, |
8dd5516b JJ |
1890 | HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end, |
1891 | basic_block bb, bool require_cst) | |
1892 | { | |
ef4bddc2 | 1893 | machine_mode store_mode = GET_MODE (store_info->mem); |
8dd5516b JJ |
1894 | int shift; |
1895 | int access_size; /* In bytes. */ | |
1896 | rtx read_reg; | |
1897 | ||
1898 | /* To get here the read is within the boundaries of the write so | |
1899 | shift will never be negative. Start out with the shift being in | |
1900 | bytes. */ | |
1901 | if (store_mode == BLKmode) | |
1902 | shift = 0; | |
1903 | else if (BYTES_BIG_ENDIAN) | |
1904 | shift = store_info->end - read_end; | |
1905 | else | |
1906 | shift = read_begin - store_info->begin; | |
1907 | ||
1908 | access_size = shift + GET_MODE_SIZE (read_mode); | |
1909 | ||
1910 | /* From now on it is bits. */ | |
1911 | shift *= BITS_PER_UNIT; | |
1912 | ||
1913 | if (shift) | |
1914 | read_reg = find_shift_sequence (access_size, store_info, read_mode, shift, | |
1915 | optimize_bb_for_speed_p (bb), | |
1916 | require_cst); | |
1917 | else if (store_mode == BLKmode) | |
1918 | { | |
1919 | /* The store is a memset (addr, const_val, const_size). */ | |
1920 | gcc_assert (CONST_INT_P (store_info->rhs)); | |
1921 | store_mode = int_mode_for_mode (read_mode); | |
1922 | if (store_mode == BLKmode) | |
1923 | read_reg = NULL_RTX; | |
1924 | else if (store_info->rhs == const0_rtx) | |
1925 | read_reg = extract_low_bits (read_mode, store_mode, const0_rtx); | |
1926 | else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT | |
1927 | || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT) | |
1928 | read_reg = NULL_RTX; | |
1929 | else | |
1930 | { | |
1931 | unsigned HOST_WIDE_INT c | |
1932 | = INTVAL (store_info->rhs) | |
1933 | & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1); | |
1934 | int shift = BITS_PER_UNIT; | |
1935 | while (shift < HOST_BITS_PER_WIDE_INT) | |
1936 | { | |
1937 | c |= (c << shift); | |
1938 | shift <<= 1; | |
1939 | } | |
6d26322f | 1940 | read_reg = gen_int_mode (c, store_mode); |
8dd5516b JJ |
1941 | read_reg = extract_low_bits (read_mode, store_mode, read_reg); |
1942 | } | |
1943 | } | |
1944 | else if (store_info->const_rhs | |
1945 | && (require_cst | |
1946 | || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode))) | |
1947 | read_reg = extract_low_bits (read_mode, store_mode, | |
1948 | copy_rtx (store_info->const_rhs)); | |
1949 | else | |
1950 | read_reg = extract_low_bits (read_mode, store_mode, | |
1951 | copy_rtx (store_info->rhs)); | |
1952 | if (require_cst && read_reg && !CONSTANT_P (read_reg)) | |
1953 | read_reg = NULL_RTX; | |
1954 | return read_reg; | |
1955 | } | |
02b47899 | 1956 | |
6fb5fa3c DB |
1957 | /* Take a sequence of: |
1958 | A <- r1 | |
1959 | ... | |
1960 | ... <- A | |
1961 | ||
b8698a0f | 1962 | and change it into |
6fb5fa3c DB |
1963 | r2 <- r1 |
1964 | A <- r1 | |
1965 | ... | |
1966 | ... <- r2 | |
1967 | ||
8660aaae EC |
1968 | or |
1969 | ||
1970 | r3 <- extract (r1) | |
1971 | r3 <- r3 >> shift | |
1972 | r2 <- extract (r3) | |
1973 | ... <- r2 | |
1974 | ||
1975 | or | |
1976 | ||
1977 | r2 <- extract (r1) | |
1978 | ... <- r2 | |
1979 | ||
1980 | Depending on the alignment and the mode of the store and | |
1981 | subsequent load. | |
1982 | ||
1983 | ||
1984 | The STORE_INFO and STORE_INSN are for the store and READ_INFO | |
6fb5fa3c DB |
1985 | and READ_INSN are for the read. Return true if the replacement |
1986 | went ok. */ | |
1987 | ||
1988 | static bool | |
b8698a0f | 1989 | replace_read (store_info_t store_info, insn_info_t store_insn, |
8dd5516b JJ |
1990 | read_info_t read_info, insn_info_t read_insn, rtx *loc, |
1991 | bitmap regs_live) | |
6fb5fa3c | 1992 | { |
ef4bddc2 RS |
1993 | machine_mode store_mode = GET_MODE (store_info->mem); |
1994 | machine_mode read_mode = GET_MODE (read_info->mem); | |
dc01c3d1 DM |
1995 | rtx_insn *insns, *this_insn; |
1996 | rtx read_reg; | |
8dd5516b | 1997 | basic_block bb; |
8660aaae | 1998 | |
6fb5fa3c DB |
1999 | if (!dbg_cnt (dse)) |
2000 | return false; | |
2001 | ||
18b526e8 RS |
2002 | /* Create a sequence of instructions to set up the read register. |
2003 | This sequence goes immediately before the store and its result | |
2004 | is read by the load. | |
2005 | ||
2006 | We need to keep this in perspective. We are replacing a read | |
8660aaae EC |
2007 | with a sequence of insns, but the read will almost certainly be |
2008 | in cache, so it is not going to be an expensive one. Thus, we | |
2009 | are not willing to do a multi insn shift or worse a subroutine | |
2010 | call to get rid of the read. */ | |
456610d3 | 2011 | if (dump_file && (dump_flags & TDF_DETAILS)) |
18b526e8 RS |
2012 | fprintf (dump_file, "trying to replace %smode load in insn %d" |
2013 | " from %smode store in insn %d\n", | |
2014 | GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn), | |
2015 | GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn)); | |
2016 | start_sequence (); | |
8dd5516b JJ |
2017 | bb = BLOCK_FOR_INSN (read_insn->insn); |
2018 | read_reg = get_stored_val (store_info, | |
2019 | read_mode, read_info->begin, read_info->end, | |
2020 | bb, false); | |
18b526e8 | 2021 | if (read_reg == NULL_RTX) |
8660aaae | 2022 | { |
18b526e8 | 2023 | end_sequence (); |
456610d3 | 2024 | if (dump_file && (dump_flags & TDF_DETAILS)) |
18b526e8 RS |
2025 | fprintf (dump_file, " -- could not extract bits of stored value\n"); |
2026 | return false; | |
8660aaae | 2027 | } |
18b526e8 RS |
2028 | /* Force the value into a new register so that it won't be clobbered |
2029 | between the store and the load. */ | |
2030 | read_reg = copy_to_mode_reg (read_mode, read_reg); | |
2031 | insns = get_insns (); | |
2032 | end_sequence (); | |
8660aaae | 2033 | |
02b47899 KZ |
2034 | if (insns != NULL_RTX) |
2035 | { | |
2036 | /* Now we have to scan the set of new instructions to see if the | |
2037 | sequence contains and sets of hardregs that happened to be | |
2038 | live at this point. For instance, this can happen if one of | |
2039 | the insns sets the CC and the CC happened to be live at that | |
2040 | point. This does occasionally happen, see PR 37922. */ | |
3f9b14ff | 2041 | bitmap regs_set = BITMAP_ALLOC (®_obstack); |
02b47899 KZ |
2042 | |
2043 | for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn)) | |
2044 | note_stores (PATTERN (this_insn), look_for_hardregs, regs_set); | |
b8698a0f | 2045 | |
02b47899 KZ |
2046 | bitmap_and_into (regs_set, regs_live); |
2047 | if (!bitmap_empty_p (regs_set)) | |
2048 | { | |
456610d3 | 2049 | if (dump_file && (dump_flags & TDF_DETAILS)) |
02b47899 | 2050 | { |
b8698a0f | 2051 | fprintf (dump_file, |
02b47899 KZ |
2052 | "abandoning replacement because sequence clobbers live hardregs:"); |
2053 | df_print_regset (dump_file, regs_set); | |
2054 | } | |
b8698a0f | 2055 | |
02b47899 KZ |
2056 | BITMAP_FREE (regs_set); |
2057 | return false; | |
2058 | } | |
2059 | BITMAP_FREE (regs_set); | |
2060 | } | |
2061 | ||
8660aaae | 2062 | if (validate_change (read_insn->insn, loc, read_reg, 0)) |
6fb5fa3c | 2063 | { |
f883e0a7 KG |
2064 | deferred_change_t deferred_change = |
2065 | (deferred_change_t) pool_alloc (deferred_change_pool); | |
b8698a0f | 2066 | |
8660aaae EC |
2067 | /* Insert this right before the store insn where it will be safe |
2068 | from later insns that might change it before the read. */ | |
2069 | emit_insn_before (insns, store_insn->insn); | |
b8698a0f | 2070 | |
8660aaae EC |
2071 | /* And now for the kludge part: cselib croaks if you just |
2072 | return at this point. There are two reasons for this: | |
b8698a0f | 2073 | |
8660aaae EC |
2074 | 1) Cselib has an idea of how many pseudos there are and |
2075 | that does not include the new ones we just added. | |
b8698a0f | 2076 | |
8660aaae EC |
2077 | 2) Cselib does not know about the move insn we added |
2078 | above the store_info, and there is no way to tell it | |
2079 | about it, because it has "moved on". | |
b8698a0f | 2080 | |
8660aaae EC |
2081 | Problem (1) is fixable with a certain amount of engineering. |
2082 | Problem (2) is requires starting the bb from scratch. This | |
2083 | could be expensive. | |
b8698a0f | 2084 | |
8660aaae EC |
2085 | So we are just going to have to lie. The move/extraction |
2086 | insns are not really an issue, cselib did not see them. But | |
2087 | the use of the new pseudo read_insn is a real problem because | |
2088 | cselib has not scanned this insn. The way that we solve this | |
2089 | problem is that we are just going to put the mem back for now | |
2090 | and when we are finished with the block, we undo this. We | |
2091 | keep a table of mems to get rid of. At the end of the basic | |
2092 | block we can put them back. */ | |
b8698a0f | 2093 | |
8660aaae EC |
2094 | *loc = read_info->mem; |
2095 | deferred_change->next = deferred_change_list; | |
2096 | deferred_change_list = deferred_change; | |
2097 | deferred_change->loc = loc; | |
2098 | deferred_change->reg = read_reg; | |
b8698a0f | 2099 | |
8660aaae EC |
2100 | /* Get rid of the read_info, from the point of view of the |
2101 | rest of dse, play like this read never happened. */ | |
2102 | read_insn->read_rec = read_info->next; | |
2103 | pool_free (read_info_pool, read_info); | |
456610d3 | 2104 | if (dump_file && (dump_flags & TDF_DETAILS)) |
18b526e8 RS |
2105 | { |
2106 | fprintf (dump_file, " -- replaced the loaded MEM with "); | |
2107 | print_simple_rtl (dump_file, read_reg); | |
2108 | fprintf (dump_file, "\n"); | |
2109 | } | |
8660aaae | 2110 | return true; |
6fb5fa3c | 2111 | } |
b8698a0f | 2112 | else |
6fb5fa3c | 2113 | { |
456610d3 | 2114 | if (dump_file && (dump_flags & TDF_DETAILS)) |
18b526e8 RS |
2115 | { |
2116 | fprintf (dump_file, " -- replacing the loaded MEM with "); | |
2117 | print_simple_rtl (dump_file, read_reg); | |
2118 | fprintf (dump_file, " led to an invalid instruction\n"); | |
2119 | } | |
6fb5fa3c DB |
2120 | return false; |
2121 | } | |
2122 | } | |
2123 | ||
d7111da8 RS |
2124 | /* Check the address of MEM *LOC and kill any appropriate stores that may |
2125 | be active. */ | |
6fb5fa3c | 2126 | |
d7111da8 RS |
2127 | static void |
2128 | check_mem_read_rtx (rtx *loc, bb_info_t bb_info) | |
6fb5fa3c | 2129 | { |
6216f94e | 2130 | rtx mem = *loc, mem_addr; |
6fb5fa3c DB |
2131 | insn_info_t insn_info; |
2132 | HOST_WIDE_INT offset = 0; | |
2133 | HOST_WIDE_INT width = 0; | |
4862826d | 2134 | alias_set_type spill_alias_set = 0; |
b8698a0f | 2135 | cselib_val *base = NULL; |
6fb5fa3c DB |
2136 | int group_id; |
2137 | read_info_t read_info; | |
2138 | ||
6fb5fa3c DB |
2139 | insn_info = bb_info->last_insn; |
2140 | ||
2141 | if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER) | |
2142 | || (MEM_VOLATILE_P (mem))) | |
2143 | { | |
456610d3 | 2144 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2145 | fprintf (dump_file, " adding wild read, volatile or barrier.\n"); |
2146 | add_wild_read (bb_info); | |
2147 | insn_info->cannot_delete = true; | |
d7111da8 | 2148 | return; |
6fb5fa3c DB |
2149 | } |
2150 | ||
2151 | /* If it is reading readonly mem, then there can be no conflict with | |
2152 | another write. */ | |
2153 | if (MEM_READONLY_P (mem)) | |
d7111da8 | 2154 | return; |
6fb5fa3c DB |
2155 | |
2156 | if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base)) | |
2157 | { | |
456610d3 | 2158 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2159 | fprintf (dump_file, " adding wild read, canon_address failure.\n"); |
2160 | add_wild_read (bb_info); | |
d7111da8 | 2161 | return; |
6fb5fa3c DB |
2162 | } |
2163 | ||
2164 | if (GET_MODE (mem) == BLKmode) | |
2165 | width = -1; | |
2166 | else | |
2167 | width = GET_MODE_SIZE (GET_MODE (mem)); | |
2168 | ||
f883e0a7 | 2169 | read_info = (read_info_t) pool_alloc (read_info_pool); |
6fb5fa3c DB |
2170 | read_info->group_id = group_id; |
2171 | read_info->mem = mem; | |
2172 | read_info->alias_set = spill_alias_set; | |
2173 | read_info->begin = offset; | |
2174 | read_info->end = offset + width; | |
2175 | read_info->next = insn_info->read_rec; | |
2176 | insn_info->read_rec = read_info; | |
6216f94e JJ |
2177 | /* For alias_set != 0 canon_true_dependence should be never called. */ |
2178 | if (spill_alias_set) | |
2179 | mem_addr = NULL_RTX; | |
2180 | else | |
2181 | { | |
2182 | if (group_id < 0) | |
2183 | mem_addr = base->val_rtx; | |
2184 | else | |
2185 | { | |
2186 | group_info_t group | |
9771b263 | 2187 | = rtx_group_vec[group_id]; |
6216f94e JJ |
2188 | mem_addr = group->canon_base_addr; |
2189 | } | |
2190 | if (offset) | |
372d6395 | 2191 | mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset); |
6216f94e | 2192 | } |
6fb5fa3c | 2193 | |
0d52bcc1 | 2194 | /* We ignore the clobbers in store_info. The is mildly aggressive, |
6fb5fa3c DB |
2195 | but there really should not be a clobber followed by a read. */ |
2196 | ||
2197 | if (spill_alias_set) | |
2198 | { | |
2199 | insn_info_t i_ptr = active_local_stores; | |
2200 | insn_info_t last = NULL; | |
2201 | ||
456610d3 | 2202 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 2203 | fprintf (dump_file, " processing spill load %d\n", |
4862826d | 2204 | (int) spill_alias_set); |
6fb5fa3c DB |
2205 | |
2206 | while (i_ptr) | |
2207 | { | |
2208 | store_info_t store_info = i_ptr->store_rec; | |
2209 | ||
2210 | /* Skip the clobbers. */ | |
2211 | while (!store_info->is_set) | |
2212 | store_info = store_info->next; | |
b8698a0f | 2213 | |
6fb5fa3c DB |
2214 | if (store_info->alias_set == spill_alias_set) |
2215 | { | |
456610d3 | 2216 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2217 | dump_insn_info ("removing from active", i_ptr); |
2218 | ||
dabd47e7 | 2219 | active_local_stores_len--; |
6fb5fa3c DB |
2220 | if (last) |
2221 | last->next_local_store = i_ptr->next_local_store; | |
2222 | else | |
2223 | active_local_stores = i_ptr->next_local_store; | |
2224 | } | |
2225 | else | |
2226 | last = i_ptr; | |
2227 | i_ptr = i_ptr->next_local_store; | |
2228 | } | |
2229 | } | |
2230 | else if (group_id >= 0) | |
2231 | { | |
2232 | /* This is the restricted case where the base is a constant or | |
2233 | the frame pointer and offset is a constant. */ | |
2234 | insn_info_t i_ptr = active_local_stores; | |
2235 | insn_info_t last = NULL; | |
b8698a0f | 2236 | |
456610d3 | 2237 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2238 | { |
2239 | if (width == -1) | |
2240 | fprintf (dump_file, " processing const load gid=%d[BLK]\n", | |
2241 | group_id); | |
2242 | else | |
2243 | fprintf (dump_file, " processing const load gid=%d[%d..%d)\n", | |
2244 | group_id, (int)offset, (int)(offset+width)); | |
2245 | } | |
2246 | ||
2247 | while (i_ptr) | |
2248 | { | |
2249 | bool remove = false; | |
2250 | store_info_t store_info = i_ptr->store_rec; | |
b8698a0f | 2251 | |
6fb5fa3c DB |
2252 | /* Skip the clobbers. */ |
2253 | while (!store_info->is_set) | |
2254 | store_info = store_info->next; | |
b8698a0f | 2255 | |
6fb5fa3c DB |
2256 | /* There are three cases here. */ |
2257 | if (store_info->group_id < 0) | |
2258 | /* We have a cselib store followed by a read from a | |
2259 | const base. */ | |
b8698a0f L |
2260 | remove |
2261 | = canon_true_dependence (store_info->mem, | |
6fb5fa3c DB |
2262 | GET_MODE (store_info->mem), |
2263 | store_info->mem_addr, | |
53d9622b | 2264 | mem, mem_addr); |
b8698a0f | 2265 | |
6fb5fa3c DB |
2266 | else if (group_id == store_info->group_id) |
2267 | { | |
2268 | /* This is a block mode load. We may get lucky and | |
2269 | canon_true_dependence may save the day. */ | |
2270 | if (width == -1) | |
b8698a0f L |
2271 | remove |
2272 | = canon_true_dependence (store_info->mem, | |
6fb5fa3c DB |
2273 | GET_MODE (store_info->mem), |
2274 | store_info->mem_addr, | |
53d9622b | 2275 | mem, mem_addr); |
b8698a0f | 2276 | |
6fb5fa3c DB |
2277 | /* If this read is just reading back something that we just |
2278 | stored, rewrite the read. */ | |
b8698a0f | 2279 | else |
6fb5fa3c DB |
2280 | { |
2281 | if (store_info->rhs | |
8dd5516b JJ |
2282 | && offset >= store_info->begin |
2283 | && offset + width <= store_info->end | |
2284 | && all_positions_needed_p (store_info, | |
2285 | offset - store_info->begin, | |
2286 | width) | |
2287 | && replace_read (store_info, i_ptr, read_info, | |
2288 | insn_info, loc, bb_info->regs_live)) | |
d7111da8 | 2289 | return; |
8dd5516b | 2290 | |
6fb5fa3c DB |
2291 | /* The bases are the same, just see if the offsets |
2292 | overlap. */ | |
b8698a0f | 2293 | if ((offset < store_info->end) |
6fb5fa3c DB |
2294 | && (offset + width > store_info->begin)) |
2295 | remove = true; | |
2296 | } | |
2297 | } | |
b8698a0f L |
2298 | |
2299 | /* else | |
6fb5fa3c DB |
2300 | The else case that is missing here is that the |
2301 | bases are constant but different. There is nothing | |
2302 | to do here because there is no overlap. */ | |
b8698a0f | 2303 | |
6fb5fa3c DB |
2304 | if (remove) |
2305 | { | |
456610d3 | 2306 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2307 | dump_insn_info ("removing from active", i_ptr); |
2308 | ||
dabd47e7 | 2309 | active_local_stores_len--; |
6fb5fa3c DB |
2310 | if (last) |
2311 | last->next_local_store = i_ptr->next_local_store; | |
2312 | else | |
2313 | active_local_stores = i_ptr->next_local_store; | |
2314 | } | |
2315 | else | |
2316 | last = i_ptr; | |
2317 | i_ptr = i_ptr->next_local_store; | |
2318 | } | |
2319 | } | |
b8698a0f | 2320 | else |
6fb5fa3c DB |
2321 | { |
2322 | insn_info_t i_ptr = active_local_stores; | |
2323 | insn_info_t last = NULL; | |
456610d3 | 2324 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2325 | { |
2326 | fprintf (dump_file, " processing cselib load mem:"); | |
2327 | print_inline_rtx (dump_file, mem, 0); | |
2328 | fprintf (dump_file, "\n"); | |
2329 | } | |
2330 | ||
2331 | while (i_ptr) | |
2332 | { | |
2333 | bool remove = false; | |
2334 | store_info_t store_info = i_ptr->store_rec; | |
b8698a0f | 2335 | |
456610d3 | 2336 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2337 | fprintf (dump_file, " processing cselib load against insn %d\n", |
2338 | INSN_UID (i_ptr->insn)); | |
2339 | ||
2340 | /* Skip the clobbers. */ | |
2341 | while (!store_info->is_set) | |
2342 | store_info = store_info->next; | |
2343 | ||
2344 | /* If this read is just reading back something that we just | |
2345 | stored, rewrite the read. */ | |
2346 | if (store_info->rhs | |
2347 | && store_info->group_id == -1 | |
2348 | && store_info->cse_base == base | |
efc3527a | 2349 | && width != -1 |
8dd5516b JJ |
2350 | && offset >= store_info->begin |
2351 | && offset + width <= store_info->end | |
2352 | && all_positions_needed_p (store_info, | |
2353 | offset - store_info->begin, width) | |
2354 | && replace_read (store_info, i_ptr, read_info, insn_info, loc, | |
2355 | bb_info->regs_live)) | |
d7111da8 | 2356 | return; |
6fb5fa3c DB |
2357 | |
2358 | if (!store_info->alias_set) | |
b8698a0f | 2359 | remove = canon_true_dependence (store_info->mem, |
6fb5fa3c DB |
2360 | GET_MODE (store_info->mem), |
2361 | store_info->mem_addr, | |
53d9622b | 2362 | mem, mem_addr); |
b8698a0f | 2363 | |
6fb5fa3c DB |
2364 | if (remove) |
2365 | { | |
456610d3 | 2366 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 2367 | dump_insn_info ("removing from active", i_ptr); |
b8698a0f | 2368 | |
dabd47e7 | 2369 | active_local_stores_len--; |
6fb5fa3c DB |
2370 | if (last) |
2371 | last->next_local_store = i_ptr->next_local_store; | |
2372 | else | |
2373 | active_local_stores = i_ptr->next_local_store; | |
2374 | } | |
2375 | else | |
2376 | last = i_ptr; | |
2377 | i_ptr = i_ptr->next_local_store; | |
2378 | } | |
2379 | } | |
6fb5fa3c DB |
2380 | } |
2381 | ||
d7111da8 | 2382 | /* A note_uses callback in which DATA points the INSN_INFO for |
6fb5fa3c DB |
2383 | as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns |
2384 | true for any part of *LOC. */ | |
2385 | ||
2386 | static void | |
2387 | check_mem_read_use (rtx *loc, void *data) | |
2388 | { | |
d7111da8 RS |
2389 | subrtx_ptr_iterator::array_type array; |
2390 | FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST) | |
2391 | { | |
2392 | rtx *loc = *iter; | |
2393 | if (MEM_P (*loc)) | |
2394 | check_mem_read_rtx (loc, (bb_info_t) data); | |
2395 | } | |
6fb5fa3c DB |
2396 | } |
2397 | ||
8dd5516b JJ |
2398 | |
2399 | /* Get arguments passed to CALL_INSN. Return TRUE if successful. | |
2400 | So far it only handles arguments passed in registers. */ | |
2401 | ||
2402 | static bool | |
2403 | get_call_args (rtx call_insn, tree fn, rtx *args, int nargs) | |
2404 | { | |
d5cc9181 JR |
2405 | CUMULATIVE_ARGS args_so_far_v; |
2406 | cumulative_args_t args_so_far; | |
8dd5516b JJ |
2407 | tree arg; |
2408 | int idx; | |
2409 | ||
d5cc9181 JR |
2410 | INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3); |
2411 | args_so_far = pack_cumulative_args (&args_so_far_v); | |
8dd5516b JJ |
2412 | |
2413 | arg = TYPE_ARG_TYPES (TREE_TYPE (fn)); | |
2414 | for (idx = 0; | |
2415 | arg != void_list_node && idx < nargs; | |
2416 | arg = TREE_CHAIN (arg), idx++) | |
2417 | { | |
ef4bddc2 | 2418 | machine_mode mode = TYPE_MODE (TREE_VALUE (arg)); |
3c07301f | 2419 | rtx reg, link, tmp; |
d5cc9181 | 2420 | reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true); |
8dd5516b JJ |
2421 | if (!reg || !REG_P (reg) || GET_MODE (reg) != mode |
2422 | || GET_MODE_CLASS (mode) != MODE_INT) | |
2423 | return false; | |
2424 | ||
2425 | for (link = CALL_INSN_FUNCTION_USAGE (call_insn); | |
2426 | link; | |
2427 | link = XEXP (link, 1)) | |
2428 | if (GET_CODE (XEXP (link, 0)) == USE) | |
2429 | { | |
2430 | args[idx] = XEXP (XEXP (link, 0), 0); | |
2431 | if (REG_P (args[idx]) | |
2432 | && REGNO (args[idx]) == REGNO (reg) | |
2433 | && (GET_MODE (args[idx]) == mode | |
2434 | || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT | |
2435 | && (GET_MODE_SIZE (GET_MODE (args[idx])) | |
2436 | <= UNITS_PER_WORD) | |
2437 | && (GET_MODE_SIZE (GET_MODE (args[idx])) | |
2438 | > GET_MODE_SIZE (mode))))) | |
2439 | break; | |
2440 | } | |
2441 | if (!link) | |
2442 | return false; | |
2443 | ||
2444 | tmp = cselib_expand_value_rtx (args[idx], scratch, 5); | |
2445 | if (GET_MODE (args[idx]) != mode) | |
2446 | { | |
2447 | if (!tmp || !CONST_INT_P (tmp)) | |
2448 | return false; | |
6d26322f | 2449 | tmp = gen_int_mode (INTVAL (tmp), mode); |
8dd5516b JJ |
2450 | } |
2451 | if (tmp) | |
2452 | args[idx] = tmp; | |
2453 | ||
d5cc9181 | 2454 | targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true); |
8dd5516b JJ |
2455 | } |
2456 | if (arg != void_list_node || idx != nargs) | |
2457 | return false; | |
2458 | return true; | |
2459 | } | |
2460 | ||
9e582b1d JR |
2461 | /* Return a bitmap of the fixed registers contained in IN. */ |
2462 | ||
2463 | static bitmap | |
2464 | copy_fixed_regs (const_bitmap in) | |
2465 | { | |
2466 | bitmap ret; | |
2467 | ||
2468 | ret = ALLOC_REG_SET (NULL); | |
2469 | bitmap_and (ret, in, fixed_reg_set_regset); | |
2470 | return ret; | |
2471 | } | |
8dd5516b | 2472 | |
6fb5fa3c DB |
2473 | /* Apply record_store to all candidate stores in INSN. Mark INSN |
2474 | if some part of it is not a candidate store and assigns to a | |
2475 | non-register target. */ | |
2476 | ||
2477 | static void | |
dd60a84c | 2478 | scan_insn (bb_info_t bb_info, rtx_insn *insn) |
6fb5fa3c DB |
2479 | { |
2480 | rtx body; | |
f883e0a7 | 2481 | insn_info_t insn_info = (insn_info_t) pool_alloc (insn_info_pool); |
6fb5fa3c DB |
2482 | int mems_found = 0; |
2483 | memset (insn_info, 0, sizeof (struct insn_info)); | |
2484 | ||
456610d3 | 2485 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
2486 | fprintf (dump_file, "\n**scanning insn=%d\n", |
2487 | INSN_UID (insn)); | |
2488 | ||
2489 | insn_info->prev_insn = bb_info->last_insn; | |
2490 | insn_info->insn = insn; | |
2491 | bb_info->last_insn = insn_info; | |
b8698a0f | 2492 | |
b5b8b0ac AO |
2493 | if (DEBUG_INSN_P (insn)) |
2494 | { | |
2495 | insn_info->cannot_delete = true; | |
2496 | return; | |
2497 | } | |
6fb5fa3c | 2498 | |
6fb5fa3c DB |
2499 | /* Look at all of the uses in the insn. */ |
2500 | note_uses (&PATTERN (insn), check_mem_read_use, bb_info); | |
2501 | ||
2502 | if (CALL_P (insn)) | |
2503 | { | |
8dd5516b JJ |
2504 | bool const_call; |
2505 | tree memset_call = NULL_TREE; | |
2506 | ||
6fb5fa3c | 2507 | insn_info->cannot_delete = true; |
50f0f366 | 2508 | |
6fb5fa3c | 2509 | /* Const functions cannot do anything bad i.e. read memory, |
50f0f366 | 2510 | however, they can read their parameters which may have |
8dd5516b JJ |
2511 | been pushed onto the stack. |
2512 | memset and bzero don't read memory either. */ | |
2513 | const_call = RTL_CONST_CALL_P (insn); | |
2514 | if (!const_call) | |
2515 | { | |
da4fdf2d SB |
2516 | rtx call = get_call_rtx_from (insn); |
2517 | if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF) | |
8dd5516b JJ |
2518 | { |
2519 | rtx symbol = XEXP (XEXP (call, 0), 0); | |
2520 | if (SYMBOL_REF_DECL (symbol) | |
2521 | && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL) | |
2522 | { | |
2523 | if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol)) | |
2524 | == BUILT_IN_NORMAL | |
2525 | && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol)) | |
2526 | == BUILT_IN_MEMSET)) | |
2527 | || SYMBOL_REF_DECL (symbol) == block_clear_fn) | |
2528 | memset_call = SYMBOL_REF_DECL (symbol); | |
2529 | } | |
2530 | } | |
2531 | } | |
2532 | if (const_call || memset_call) | |
6fb5fa3c DB |
2533 | { |
2534 | insn_info_t i_ptr = active_local_stores; | |
2535 | insn_info_t last = NULL; | |
2536 | ||
456610d3 | 2537 | if (dump_file && (dump_flags & TDF_DETAILS)) |
8dd5516b JJ |
2538 | fprintf (dump_file, "%s call %d\n", |
2539 | const_call ? "const" : "memset", INSN_UID (insn)); | |
6fb5fa3c | 2540 | |
64520bdc | 2541 | /* See the head comment of the frame_read field. */ |
57534689 JJ |
2542 | if (reload_completed |
2543 | /* Tail calls are storing their arguments using | |
2544 | arg pointer. If it is a frame pointer on the target, | |
2545 | even before reload we need to kill frame pointer based | |
2546 | stores. */ | |
2547 | || (SIBLING_CALL_P (insn) | |
2548 | && HARD_FRAME_POINTER_IS_ARG_POINTER)) | |
64520bdc EB |
2549 | insn_info->frame_read = true; |
2550 | ||
2551 | /* Loop over the active stores and remove those which are | |
2552 | killed by the const function call. */ | |
6fb5fa3c DB |
2553 | while (i_ptr) |
2554 | { | |
64520bdc EB |
2555 | bool remove_store = false; |
2556 | ||
2557 | /* The stack pointer based stores are always killed. */ | |
50f0f366 | 2558 | if (i_ptr->stack_pointer_based) |
64520bdc EB |
2559 | remove_store = true; |
2560 | ||
2561 | /* If the frame is read, the frame related stores are killed. */ | |
2562 | else if (insn_info->frame_read) | |
2563 | { | |
2564 | store_info_t store_info = i_ptr->store_rec; | |
2565 | ||
2566 | /* Skip the clobbers. */ | |
2567 | while (!store_info->is_set) | |
2568 | store_info = store_info->next; | |
2569 | ||
2570 | if (store_info->group_id >= 0 | |
9771b263 | 2571 | && rtx_group_vec[store_info->group_id]->frame_related) |
64520bdc EB |
2572 | remove_store = true; |
2573 | } | |
2574 | ||
2575 | if (remove_store) | |
6fb5fa3c | 2576 | { |
456610d3 | 2577 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 2578 | dump_insn_info ("removing from active", i_ptr); |
b8698a0f | 2579 | |
dabd47e7 | 2580 | active_local_stores_len--; |
6fb5fa3c DB |
2581 | if (last) |
2582 | last->next_local_store = i_ptr->next_local_store; | |
2583 | else | |
2584 | active_local_stores = i_ptr->next_local_store; | |
2585 | } | |
2586 | else | |
2587 | last = i_ptr; | |
64520bdc | 2588 | |
6fb5fa3c DB |
2589 | i_ptr = i_ptr->next_local_store; |
2590 | } | |
8dd5516b JJ |
2591 | |
2592 | if (memset_call) | |
2593 | { | |
2594 | rtx args[3]; | |
2595 | if (get_call_args (insn, memset_call, args, 3) | |
2596 | && CONST_INT_P (args[1]) | |
2597 | && CONST_INT_P (args[2]) | |
2598 | && INTVAL (args[2]) > 0) | |
2599 | { | |
2600 | rtx mem = gen_rtx_MEM (BLKmode, args[0]); | |
f5541398 | 2601 | set_mem_size (mem, INTVAL (args[2])); |
8dd5516b JJ |
2602 | body = gen_rtx_SET (VOIDmode, mem, args[1]); |
2603 | mems_found += record_store (body, bb_info); | |
456610d3 | 2604 | if (dump_file && (dump_flags & TDF_DETAILS)) |
8dd5516b JJ |
2605 | fprintf (dump_file, "handling memset as BLKmode store\n"); |
2606 | if (mems_found == 1) | |
2607 | { | |
dabd47e7 JJ |
2608 | if (active_local_stores_len++ |
2609 | >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES)) | |
2610 | { | |
2611 | active_local_stores_len = 1; | |
2612 | active_local_stores = NULL; | |
2613 | } | |
9e582b1d JR |
2614 | insn_info->fixed_regs_live |
2615 | = copy_fixed_regs (bb_info->regs_live); | |
8dd5516b JJ |
2616 | insn_info->next_local_store = active_local_stores; |
2617 | active_local_stores = insn_info; | |
2618 | } | |
2619 | } | |
2620 | } | |
6fb5fa3c | 2621 | } |
57534689 JJ |
2622 | else if (SIBLING_CALL_P (insn) && reload_completed) |
2623 | /* Arguments for a sibling call that are pushed to memory are passed | |
2624 | using the incoming argument pointer of the current function. After | |
2625 | reload that might be (and likely is) frame pointer based. */ | |
2626 | add_wild_read (bb_info); | |
50f0f366 | 2627 | else |
d26c7090 ER |
2628 | /* Every other call, including pure functions, may read any memory |
2629 | that is not relative to the frame. */ | |
2630 | add_non_frame_wild_read (bb_info); | |
50f0f366 | 2631 | |
6fb5fa3c DB |
2632 | return; |
2633 | } | |
2634 | ||
2635 | /* Assuming that there are sets in these insns, we cannot delete | |
2636 | them. */ | |
2637 | if ((GET_CODE (PATTERN (insn)) == CLOBBER) | |
0a64eeca | 2638 | || volatile_refs_p (PATTERN (insn)) |
2da02156 | 2639 | || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn)) |
6fb5fa3c DB |
2640 | || (RTX_FRAME_RELATED_P (insn)) |
2641 | || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX)) | |
2642 | insn_info->cannot_delete = true; | |
b8698a0f | 2643 | |
6fb5fa3c DB |
2644 | body = PATTERN (insn); |
2645 | if (GET_CODE (body) == PARALLEL) | |
2646 | { | |
2647 | int i; | |
2648 | for (i = 0; i < XVECLEN (body, 0); i++) | |
2649 | mems_found += record_store (XVECEXP (body, 0, i), bb_info); | |
2650 | } | |
2651 | else | |
2652 | mems_found += record_store (body, bb_info); | |
2653 | ||
456610d3 | 2654 | if (dump_file && (dump_flags & TDF_DETAILS)) |
b8698a0f | 2655 | fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n", |
6fb5fa3c DB |
2656 | mems_found, insn_info->cannot_delete ? "true" : "false"); |
2657 | ||
8dd5516b JJ |
2658 | /* If we found some sets of mems, add it into the active_local_stores so |
2659 | that it can be locally deleted if found dead or used for | |
2660 | replace_read and redundant constant store elimination. Otherwise mark | |
2661 | it as cannot delete. This simplifies the processing later. */ | |
2662 | if (mems_found == 1) | |
6fb5fa3c | 2663 | { |
dabd47e7 JJ |
2664 | if (active_local_stores_len++ |
2665 | >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES)) | |
2666 | { | |
2667 | active_local_stores_len = 1; | |
2668 | active_local_stores = NULL; | |
2669 | } | |
9e582b1d | 2670 | insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live); |
6fb5fa3c DB |
2671 | insn_info->next_local_store = active_local_stores; |
2672 | active_local_stores = insn_info; | |
2673 | } | |
2674 | else | |
2675 | insn_info->cannot_delete = true; | |
2676 | } | |
2677 | ||
2678 | ||
2679 | /* Remove BASE from the set of active_local_stores. This is a | |
2680 | callback from cselib that is used to get rid of the stores in | |
2681 | active_local_stores. */ | |
2682 | ||
2683 | static void | |
2684 | remove_useless_values (cselib_val *base) | |
2685 | { | |
2686 | insn_info_t insn_info = active_local_stores; | |
2687 | insn_info_t last = NULL; | |
2688 | ||
2689 | while (insn_info) | |
2690 | { | |
2691 | store_info_t store_info = insn_info->store_rec; | |
60564289 | 2692 | bool del = false; |
6fb5fa3c DB |
2693 | |
2694 | /* If ANY of the store_infos match the cselib group that is | |
2695 | being deleted, then the insn can not be deleted. */ | |
2696 | while (store_info) | |
2697 | { | |
b8698a0f | 2698 | if ((store_info->group_id == -1) |
6fb5fa3c DB |
2699 | && (store_info->cse_base == base)) |
2700 | { | |
60564289 | 2701 | del = true; |
6fb5fa3c DB |
2702 | break; |
2703 | } | |
2704 | store_info = store_info->next; | |
2705 | } | |
2706 | ||
60564289 | 2707 | if (del) |
6fb5fa3c | 2708 | { |
dabd47e7 | 2709 | active_local_stores_len--; |
6fb5fa3c DB |
2710 | if (last) |
2711 | last->next_local_store = insn_info->next_local_store; | |
2712 | else | |
2713 | active_local_stores = insn_info->next_local_store; | |
2714 | free_store_info (insn_info); | |
2715 | } | |
2716 | else | |
2717 | last = insn_info; | |
b8698a0f | 2718 | |
6fb5fa3c DB |
2719 | insn_info = insn_info->next_local_store; |
2720 | } | |
2721 | } | |
2722 | ||
2723 | ||
2724 | /* Do all of step 1. */ | |
2725 | ||
2726 | static void | |
2727 | dse_step1 (void) | |
2728 | { | |
2729 | basic_block bb; | |
3f9b14ff | 2730 | bitmap regs_live = BITMAP_ALLOC (®_obstack); |
b8698a0f | 2731 | |
457eeaae | 2732 | cselib_init (0); |
6fb5fa3c DB |
2733 | all_blocks = BITMAP_ALLOC (NULL); |
2734 | bitmap_set_bit (all_blocks, ENTRY_BLOCK); | |
2735 | bitmap_set_bit (all_blocks, EXIT_BLOCK); | |
2736 | ||
04a90bec | 2737 | FOR_ALL_BB_FN (bb, cfun) |
6fb5fa3c DB |
2738 | { |
2739 | insn_info_t ptr; | |
f883e0a7 | 2740 | bb_info_t bb_info = (bb_info_t) pool_alloc (bb_info_pool); |
6fb5fa3c | 2741 | |
11478306 | 2742 | memset (bb_info, 0, sizeof (struct dse_bb_info)); |
6fb5fa3c | 2743 | bitmap_set_bit (all_blocks, bb->index); |
02b47899 KZ |
2744 | bb_info->regs_live = regs_live; |
2745 | ||
2746 | bitmap_copy (regs_live, DF_LR_IN (bb)); | |
2747 | df_simulate_initialize_forwards (bb, regs_live); | |
6fb5fa3c DB |
2748 | |
2749 | bb_table[bb->index] = bb_info; | |
2750 | cselib_discard_hook = remove_useless_values; | |
2751 | ||
2752 | if (bb->index >= NUM_FIXED_BLOCKS) | |
2753 | { | |
dd60a84c | 2754 | rtx_insn *insn; |
6fb5fa3c DB |
2755 | |
2756 | cse_store_info_pool | |
b8698a0f | 2757 | = create_alloc_pool ("cse_store_info_pool", |
6fb5fa3c DB |
2758 | sizeof (struct store_info), 100); |
2759 | active_local_stores = NULL; | |
dabd47e7 | 2760 | active_local_stores_len = 0; |
6fb5fa3c | 2761 | cselib_clear_table (); |
b8698a0f | 2762 | |
6fb5fa3c DB |
2763 | /* Scan the insns. */ |
2764 | FOR_BB_INSNS (bb, insn) | |
2765 | { | |
2766 | if (INSN_P (insn)) | |
2767 | scan_insn (bb_info, insn); | |
2768 | cselib_process_insn (insn); | |
02b47899 KZ |
2769 | if (INSN_P (insn)) |
2770 | df_simulate_one_insn_forwards (bb, insn, regs_live); | |
6fb5fa3c | 2771 | } |
b8698a0f | 2772 | |
6fb5fa3c DB |
2773 | /* This is something of a hack, because the global algorithm |
2774 | is supposed to take care of the case where stores go dead | |
2775 | at the end of the function. However, the global | |
2776 | algorithm must take a more conservative view of block | |
2777 | mode reads than the local alg does. So to get the case | |
2778 | where you have a store to the frame followed by a non | |
0d52bcc1 | 2779 | overlapping block more read, we look at the active local |
6fb5fa3c DB |
2780 | stores at the end of the function and delete all of the |
2781 | frame and spill based ones. */ | |
2782 | if (stores_off_frame_dead_at_return | |
2783 | && (EDGE_COUNT (bb->succs) == 0 | |
2784 | || (single_succ_p (bb) | |
fefa31b5 | 2785 | && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun) |
e3b5732b | 2786 | && ! crtl->calls_eh_return))) |
6fb5fa3c DB |
2787 | { |
2788 | insn_info_t i_ptr = active_local_stores; | |
2789 | while (i_ptr) | |
2790 | { | |
2791 | store_info_t store_info = i_ptr->store_rec; | |
2792 | ||
2793 | /* Skip the clobbers. */ | |
2794 | while (!store_info->is_set) | |
2795 | store_info = store_info->next; | |
8dd5516b | 2796 | if (store_info->alias_set && !i_ptr->cannot_delete) |
6fb5fa3c | 2797 | delete_dead_store_insn (i_ptr); |
b8698a0f | 2798 | else |
6fb5fa3c DB |
2799 | if (store_info->group_id >= 0) |
2800 | { | |
b8698a0f | 2801 | group_info_t group |
9771b263 | 2802 | = rtx_group_vec[store_info->group_id]; |
8dd5516b | 2803 | if (group->frame_related && !i_ptr->cannot_delete) |
6fb5fa3c DB |
2804 | delete_dead_store_insn (i_ptr); |
2805 | } | |
2806 | ||
2807 | i_ptr = i_ptr->next_local_store; | |
2808 | } | |
2809 | } | |
2810 | ||
2811 | /* Get rid of the loads that were discovered in | |
2812 | replace_read. Cselib is finished with this block. */ | |
2813 | while (deferred_change_list) | |
2814 | { | |
2815 | deferred_change_t next = deferred_change_list->next; | |
2816 | ||
2817 | /* There is no reason to validate this change. That was | |
2818 | done earlier. */ | |
2819 | *deferred_change_list->loc = deferred_change_list->reg; | |
2820 | pool_free (deferred_change_pool, deferred_change_list); | |
2821 | deferred_change_list = next; | |
2822 | } | |
2823 | ||
2824 | /* Get rid of all of the cselib based store_infos in this | |
2825 | block and mark the containing insns as not being | |
2826 | deletable. */ | |
2827 | ptr = bb_info->last_insn; | |
2828 | while (ptr) | |
2829 | { | |
2830 | if (ptr->contains_cselib_groups) | |
8dd5516b JJ |
2831 | { |
2832 | store_info_t s_info = ptr->store_rec; | |
2833 | while (s_info && !s_info->is_set) | |
2834 | s_info = s_info->next; | |
2835 | if (s_info | |
2836 | && s_info->redundant_reason | |
2837 | && s_info->redundant_reason->insn | |
2838 | && !ptr->cannot_delete) | |
2839 | { | |
456610d3 | 2840 | if (dump_file && (dump_flags & TDF_DETAILS)) |
8dd5516b JJ |
2841 | fprintf (dump_file, "Locally deleting insn %d " |
2842 | "because insn %d stores the " | |
2843 | "same value and couldn't be " | |
2844 | "eliminated\n", | |
2845 | INSN_UID (ptr->insn), | |
2846 | INSN_UID (s_info->redundant_reason->insn)); | |
2847 | delete_dead_store_insn (ptr); | |
2848 | } | |
8dd5516b JJ |
2849 | free_store_info (ptr); |
2850 | } | |
2851 | else | |
2852 | { | |
2853 | store_info_t s_info; | |
2854 | ||
2855 | /* Free at least positions_needed bitmaps. */ | |
2856 | for (s_info = ptr->store_rec; s_info; s_info = s_info->next) | |
2857 | if (s_info->is_large) | |
2858 | { | |
dc491a25 | 2859 | BITMAP_FREE (s_info->positions_needed.large.bmap); |
8dd5516b JJ |
2860 | s_info->is_large = false; |
2861 | } | |
2862 | } | |
6fb5fa3c DB |
2863 | ptr = ptr->prev_insn; |
2864 | } | |
2865 | ||
2866 | free_alloc_pool (cse_store_info_pool); | |
2867 | } | |
02b47899 | 2868 | bb_info->regs_live = NULL; |
6fb5fa3c DB |
2869 | } |
2870 | ||
02b47899 | 2871 | BITMAP_FREE (regs_live); |
6fb5fa3c | 2872 | cselib_finish (); |
c203e8a7 | 2873 | rtx_group_table->empty (); |
6fb5fa3c DB |
2874 | } |
2875 | ||
2876 | \f | |
2877 | /*---------------------------------------------------------------------------- | |
2878 | Second step. | |
2879 | ||
2880 | Assign each byte position in the stores that we are going to | |
2881 | analyze globally to a position in the bitmaps. Returns true if | |
6ed3da00 | 2882 | there are any bit positions assigned. |
6fb5fa3c DB |
2883 | ----------------------------------------------------------------------------*/ |
2884 | ||
2885 | static void | |
2886 | dse_step2_init (void) | |
2887 | { | |
2888 | unsigned int i; | |
2889 | group_info_t group; | |
2890 | ||
9771b263 | 2891 | FOR_EACH_VEC_ELT (rtx_group_vec, i, group) |
6fb5fa3c DB |
2892 | { |
2893 | /* For all non stack related bases, we only consider a store to | |
2894 | be deletable if there are two or more stores for that | |
2895 | position. This is because it takes one store to make the | |
2896 | other store redundant. However, for the stores that are | |
2897 | stack related, we consider them if there is only one store | |
2898 | for the position. We do this because the stack related | |
2899 | stores can be deleted if their is no read between them and | |
2900 | the end of the function. | |
b8698a0f | 2901 | |
6fb5fa3c DB |
2902 | To make this work in the current framework, we take the stack |
2903 | related bases add all of the bits from store1 into store2. | |
2904 | This has the effect of making the eligible even if there is | |
2905 | only one store. */ | |
2906 | ||
2907 | if (stores_off_frame_dead_at_return && group->frame_related) | |
2908 | { | |
2909 | bitmap_ior_into (group->store2_n, group->store1_n); | |
2910 | bitmap_ior_into (group->store2_p, group->store1_p); | |
456610d3 | 2911 | if (dump_file && (dump_flags & TDF_DETAILS)) |
b8698a0f | 2912 | fprintf (dump_file, "group %d is frame related ", i); |
6fb5fa3c DB |
2913 | } |
2914 | ||
2915 | group->offset_map_size_n++; | |
3f9b14ff SB |
2916 | group->offset_map_n = XOBNEWVEC (&dse_obstack, int, |
2917 | group->offset_map_size_n); | |
6fb5fa3c | 2918 | group->offset_map_size_p++; |
3f9b14ff SB |
2919 | group->offset_map_p = XOBNEWVEC (&dse_obstack, int, |
2920 | group->offset_map_size_p); | |
6fb5fa3c | 2921 | group->process_globally = false; |
456610d3 | 2922 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c | 2923 | { |
b8698a0f | 2924 | fprintf (dump_file, "group %d(%d+%d): ", i, |
6fb5fa3c DB |
2925 | (int)bitmap_count_bits (group->store2_n), |
2926 | (int)bitmap_count_bits (group->store2_p)); | |
2927 | bitmap_print (dump_file, group->store2_n, "n ", " "); | |
2928 | bitmap_print (dump_file, group->store2_p, "p ", "\n"); | |
2929 | } | |
2930 | } | |
2931 | } | |
2932 | ||
2933 | ||
2934 | /* Init the offset tables for the normal case. */ | |
2935 | ||
2936 | static bool | |
2937 | dse_step2_nospill (void) | |
2938 | { | |
2939 | unsigned int i; | |
2940 | group_info_t group; | |
2941 | /* Position 0 is unused because 0 is used in the maps to mean | |
2942 | unused. */ | |
2943 | current_position = 1; | |
9771b263 | 2944 | FOR_EACH_VEC_ELT (rtx_group_vec, i, group) |
6fb5fa3c DB |
2945 | { |
2946 | bitmap_iterator bi; | |
2947 | unsigned int j; | |
2948 | ||
2949 | if (group == clear_alias_group) | |
2950 | continue; | |
2951 | ||
c3284718 RS |
2952 | memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n); |
2953 | memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p); | |
6fb5fa3c DB |
2954 | bitmap_clear (group->group_kill); |
2955 | ||
2956 | EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi) | |
2957 | { | |
2958 | bitmap_set_bit (group->group_kill, current_position); | |
d26c7090 ER |
2959 | if (bitmap_bit_p (group->escaped_n, j)) |
2960 | bitmap_set_bit (kill_on_calls, current_position); | |
6fb5fa3c DB |
2961 | group->offset_map_n[j] = current_position++; |
2962 | group->process_globally = true; | |
2963 | } | |
2964 | EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi) | |
2965 | { | |
b8698a0f | 2966 | bitmap_set_bit (group->group_kill, current_position); |
d26c7090 ER |
2967 | if (bitmap_bit_p (group->escaped_p, j)) |
2968 | bitmap_set_bit (kill_on_calls, current_position); | |
6fb5fa3c DB |
2969 | group->offset_map_p[j] = current_position++; |
2970 | group->process_globally = true; | |
2971 | } | |
2972 | } | |
2973 | return current_position != 1; | |
2974 | } | |
2975 | ||
2976 | ||
6fb5fa3c DB |
2977 | \f |
2978 | /*---------------------------------------------------------------------------- | |
2979 | Third step. | |
b8698a0f | 2980 | |
6fb5fa3c DB |
2981 | Build the bit vectors for the transfer functions. |
2982 | ----------------------------------------------------------------------------*/ | |
2983 | ||
2984 | ||
6fb5fa3c DB |
2985 | /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not |
2986 | there, return 0. */ | |
2987 | ||
2988 | static int | |
2989 | get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset) | |
2990 | { | |
2991 | if (offset < 0) | |
2992 | { | |
2993 | HOST_WIDE_INT offset_p = -offset; | |
2994 | if (offset_p >= group_info->offset_map_size_n) | |
2995 | return 0; | |
2996 | return group_info->offset_map_n[offset_p]; | |
2997 | } | |
2998 | else | |
2999 | { | |
3000 | if (offset >= group_info->offset_map_size_p) | |
3001 | return 0; | |
3002 | return group_info->offset_map_p[offset]; | |
3003 | } | |
3004 | } | |
3005 | ||
3006 | ||
3007 | /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL | |
3008 | may be NULL. */ | |
3009 | ||
b8698a0f | 3010 | static void |
6fb5fa3c DB |
3011 | scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill) |
3012 | { | |
3013 | while (store_info) | |
3014 | { | |
3015 | HOST_WIDE_INT i; | |
b8698a0f | 3016 | group_info_t group_info |
9771b263 | 3017 | = rtx_group_vec[store_info->group_id]; |
6fb5fa3c DB |
3018 | if (group_info->process_globally) |
3019 | for (i = store_info->begin; i < store_info->end; i++) | |
3020 | { | |
3021 | int index = get_bitmap_index (group_info, i); | |
3022 | if (index != 0) | |
3023 | { | |
3024 | bitmap_set_bit (gen, index); | |
3025 | if (kill) | |
3026 | bitmap_clear_bit (kill, index); | |
3027 | } | |
3028 | } | |
3029 | store_info = store_info->next; | |
3030 | } | |
3031 | } | |
3032 | ||
3033 | ||
3034 | /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL | |
3035 | may be NULL. */ | |
3036 | ||
b8698a0f | 3037 | static void |
6fb5fa3c DB |
3038 | scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill) |
3039 | { | |
3040 | while (store_info) | |
3041 | { | |
3042 | if (store_info->alias_set) | |
3043 | { | |
b8698a0f | 3044 | int index = get_bitmap_index (clear_alias_group, |
6fb5fa3c DB |
3045 | store_info->alias_set); |
3046 | if (index != 0) | |
3047 | { | |
3048 | bitmap_set_bit (gen, index); | |
3049 | if (kill) | |
3050 | bitmap_clear_bit (kill, index); | |
3051 | } | |
3052 | } | |
3053 | store_info = store_info->next; | |
3054 | } | |
3055 | } | |
3056 | ||
3057 | ||
3058 | /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL | |
3059 | may be NULL. */ | |
3060 | ||
3061 | static void | |
3062 | scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill) | |
3063 | { | |
3064 | read_info_t read_info = insn_info->read_rec; | |
3065 | int i; | |
3066 | group_info_t group; | |
3067 | ||
64520bdc EB |
3068 | /* If this insn reads the frame, kill all the frame related stores. */ |
3069 | if (insn_info->frame_read) | |
3070 | { | |
9771b263 | 3071 | FOR_EACH_VEC_ELT (rtx_group_vec, i, group) |
64520bdc EB |
3072 | if (group->process_globally && group->frame_related) |
3073 | { | |
3074 | if (kill) | |
3075 | bitmap_ior_into (kill, group->group_kill); | |
b8698a0f | 3076 | bitmap_and_compl_into (gen, group->group_kill); |
64520bdc EB |
3077 | } |
3078 | } | |
d26c7090 ER |
3079 | if (insn_info->non_frame_wild_read) |
3080 | { | |
3081 | /* Kill all non-frame related stores. Kill all stores of variables that | |
3082 | escape. */ | |
3083 | if (kill) | |
3084 | bitmap_ior_into (kill, kill_on_calls); | |
3085 | bitmap_and_compl_into (gen, kill_on_calls); | |
9771b263 | 3086 | FOR_EACH_VEC_ELT (rtx_group_vec, i, group) |
d26c7090 ER |
3087 | if (group->process_globally && !group->frame_related) |
3088 | { | |
3089 | if (kill) | |
3090 | bitmap_ior_into (kill, group->group_kill); | |
3091 | bitmap_and_compl_into (gen, group->group_kill); | |
3092 | } | |
3093 | } | |
6fb5fa3c DB |
3094 | while (read_info) |
3095 | { | |
9771b263 | 3096 | FOR_EACH_VEC_ELT (rtx_group_vec, i, group) |
6fb5fa3c DB |
3097 | { |
3098 | if (group->process_globally) | |
3099 | { | |
3100 | if (i == read_info->group_id) | |
3101 | { | |
3102 | if (read_info->begin > read_info->end) | |
3103 | { | |
3104 | /* Begin > end for block mode reads. */ | |
3105 | if (kill) | |
3106 | bitmap_ior_into (kill, group->group_kill); | |
3107 | bitmap_and_compl_into (gen, group->group_kill); | |
3108 | } | |
3109 | else | |
3110 | { | |
3111 | /* The groups are the same, just process the | |
3112 | offsets. */ | |
3113 | HOST_WIDE_INT j; | |
3114 | for (j = read_info->begin; j < read_info->end; j++) | |
3115 | { | |
3116 | int index = get_bitmap_index (group, j); | |
3117 | if (index != 0) | |
3118 | { | |
3119 | if (kill) | |
3120 | bitmap_set_bit (kill, index); | |
3121 | bitmap_clear_bit (gen, index); | |
3122 | } | |
3123 | } | |
3124 | } | |
3125 | } | |
3126 | else | |
3127 | { | |
3128 | /* The groups are different, if the alias sets | |
3129 | conflict, clear the entire group. We only need | |
3130 | to apply this test if the read_info is a cselib | |
3131 | read. Anything with a constant base cannot alias | |
3132 | something else with a different constant | |
3133 | base. */ | |
3134 | if ((read_info->group_id < 0) | |
b8698a0f | 3135 | && canon_true_dependence (group->base_mem, |
d32f725a | 3136 | GET_MODE (group->base_mem), |
6216f94e | 3137 | group->canon_base_addr, |
53d9622b | 3138 | read_info->mem, NULL_RTX)) |
6fb5fa3c DB |
3139 | { |
3140 | if (kill) | |
3141 | bitmap_ior_into (kill, group->group_kill); | |
3142 | bitmap_and_compl_into (gen, group->group_kill); | |
3143 | } | |
3144 | } | |
3145 | } | |
3146 | } | |
b8698a0f | 3147 | |
6fb5fa3c DB |
3148 | read_info = read_info->next; |
3149 | } | |
3150 | } | |
3151 | ||
3152 | /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL | |
3153 | may be NULL. */ | |
3154 | ||
3155 | static void | |
3156 | scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill) | |
3157 | { | |
3158 | while (read_info) | |
3159 | { | |
3160 | if (read_info->alias_set) | |
3161 | { | |
b8698a0f | 3162 | int index = get_bitmap_index (clear_alias_group, |
6fb5fa3c DB |
3163 | read_info->alias_set); |
3164 | if (index != 0) | |
3165 | { | |
3166 | if (kill) | |
3167 | bitmap_set_bit (kill, index); | |
3168 | bitmap_clear_bit (gen, index); | |
3169 | } | |
3170 | } | |
b8698a0f | 3171 | |
6fb5fa3c DB |
3172 | read_info = read_info->next; |
3173 | } | |
3174 | } | |
3175 | ||
3176 | ||
3177 | /* Return the insn in BB_INFO before the first wild read or if there | |
3178 | are no wild reads in the block, return the last insn. */ | |
3179 | ||
3180 | static insn_info_t | |
3181 | find_insn_before_first_wild_read (bb_info_t bb_info) | |
3182 | { | |
3183 | insn_info_t insn_info = bb_info->last_insn; | |
3184 | insn_info_t last_wild_read = NULL; | |
3185 | ||
3186 | while (insn_info) | |
3187 | { | |
3188 | if (insn_info->wild_read) | |
3189 | { | |
3190 | last_wild_read = insn_info->prev_insn; | |
3191 | /* Block starts with wild read. */ | |
3192 | if (!last_wild_read) | |
3193 | return NULL; | |
3194 | } | |
3195 | ||
3196 | insn_info = insn_info->prev_insn; | |
3197 | } | |
3198 | ||
3199 | if (last_wild_read) | |
3200 | return last_wild_read; | |
3201 | else | |
3202 | return bb_info->last_insn; | |
3203 | } | |
3204 | ||
3205 | ||
3206 | /* Scan the insns in BB_INFO starting at PTR and going to the top of | |
3207 | the block in order to build the gen and kill sets for the block. | |
3208 | We start at ptr which may be the last insn in the block or may be | |
3209 | the first insn with a wild read. In the latter case we are able to | |
3210 | skip the rest of the block because it just does not matter: | |
3211 | anything that happens is hidden by the wild read. */ | |
3212 | ||
3213 | static void | |
3214 | dse_step3_scan (bool for_spills, basic_block bb) | |
3215 | { | |
3216 | bb_info_t bb_info = bb_table[bb->index]; | |
3217 | insn_info_t insn_info; | |
3218 | ||
3219 | if (for_spills) | |
3220 | /* There are no wild reads in the spill case. */ | |
3221 | insn_info = bb_info->last_insn; | |
3222 | else | |
3223 | insn_info = find_insn_before_first_wild_read (bb_info); | |
b8698a0f | 3224 | |
6fb5fa3c DB |
3225 | /* In the spill case or in the no_spill case if there is no wild |
3226 | read in the block, we will need a kill set. */ | |
3227 | if (insn_info == bb_info->last_insn) | |
3228 | { | |
3229 | if (bb_info->kill) | |
3230 | bitmap_clear (bb_info->kill); | |
3231 | else | |
3f9b14ff | 3232 | bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack); |
6fb5fa3c | 3233 | } |
b8698a0f | 3234 | else |
6fb5fa3c DB |
3235 | if (bb_info->kill) |
3236 | BITMAP_FREE (bb_info->kill); | |
3237 | ||
3238 | while (insn_info) | |
3239 | { | |
3240 | /* There may have been code deleted by the dce pass run before | |
3241 | this phase. */ | |
3242 | if (insn_info->insn && INSN_P (insn_info->insn)) | |
3243 | { | |
b8698a0f | 3244 | /* Process the read(s) last. */ |
6fb5fa3c DB |
3245 | if (for_spills) |
3246 | { | |
3247 | scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill); | |
3248 | scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill); | |
3249 | } | |
3250 | else | |
3251 | { | |
3252 | scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill); | |
3253 | scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill); | |
3254 | } | |
b8698a0f | 3255 | } |
6fb5fa3c DB |
3256 | |
3257 | insn_info = insn_info->prev_insn; | |
3258 | } | |
3259 | } | |
3260 | ||
3261 | ||
3262 | /* Set the gen set of the exit block, and also any block with no | |
3263 | successors that does not have a wild read. */ | |
3264 | ||
3265 | static void | |
3266 | dse_step3_exit_block_scan (bb_info_t bb_info) | |
3267 | { | |
3268 | /* The gen set is all 0's for the exit block except for the | |
3269 | frame_pointer_group. */ | |
b8698a0f | 3270 | |
6fb5fa3c DB |
3271 | if (stores_off_frame_dead_at_return) |
3272 | { | |
3273 | unsigned int i; | |
3274 | group_info_t group; | |
b8698a0f | 3275 | |
9771b263 | 3276 | FOR_EACH_VEC_ELT (rtx_group_vec, i, group) |
6fb5fa3c DB |
3277 | { |
3278 | if (group->process_globally && group->frame_related) | |
3279 | bitmap_ior_into (bb_info->gen, group->group_kill); | |
3280 | } | |
3281 | } | |
3282 | } | |
3283 | ||
3284 | ||
3285 | /* Find all of the blocks that are not backwards reachable from the | |
3286 | exit block or any block with no successors (BB). These are the | |
3287 | infinite loops or infinite self loops. These blocks will still | |
3288 | have their bits set in UNREACHABLE_BLOCKS. */ | |
3289 | ||
3290 | static void | |
3291 | mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb) | |
3292 | { | |
3293 | edge e; | |
3294 | edge_iterator ei; | |
3295 | ||
d7c028c0 | 3296 | if (bitmap_bit_p (unreachable_blocks, bb->index)) |
6fb5fa3c | 3297 | { |
d7c028c0 | 3298 | bitmap_clear_bit (unreachable_blocks, bb->index); |
6fb5fa3c | 3299 | FOR_EACH_EDGE (e, ei, bb->preds) |
b8698a0f | 3300 | { |
6fb5fa3c | 3301 | mark_reachable_blocks (unreachable_blocks, e->src); |
b8698a0f | 3302 | } |
6fb5fa3c DB |
3303 | } |
3304 | } | |
3305 | ||
3306 | /* Build the transfer functions for the function. */ | |
3307 | ||
3308 | static void | |
3309 | dse_step3 (bool for_spills) | |
3310 | { | |
3311 | basic_block bb; | |
8b1c6fd7 | 3312 | sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun)); |
6fb5fa3c DB |
3313 | sbitmap_iterator sbi; |
3314 | bitmap all_ones = NULL; | |
3315 | unsigned int i; | |
b8698a0f | 3316 | |
f61e445a | 3317 | bitmap_ones (unreachable_blocks); |
6fb5fa3c | 3318 | |
04a90bec | 3319 | FOR_ALL_BB_FN (bb, cfun) |
6fb5fa3c DB |
3320 | { |
3321 | bb_info_t bb_info = bb_table[bb->index]; | |
3322 | if (bb_info->gen) | |
3323 | bitmap_clear (bb_info->gen); | |
3324 | else | |
3f9b14ff | 3325 | bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack); |
6fb5fa3c DB |
3326 | |
3327 | if (bb->index == ENTRY_BLOCK) | |
3328 | ; | |
3329 | else if (bb->index == EXIT_BLOCK) | |
3330 | dse_step3_exit_block_scan (bb_info); | |
3331 | else | |
3332 | dse_step3_scan (for_spills, bb); | |
3333 | if (EDGE_COUNT (bb->succs) == 0) | |
3334 | mark_reachable_blocks (unreachable_blocks, bb); | |
3335 | ||
3336 | /* If this is the second time dataflow is run, delete the old | |
3337 | sets. */ | |
3338 | if (bb_info->in) | |
3339 | BITMAP_FREE (bb_info->in); | |
3340 | if (bb_info->out) | |
3341 | BITMAP_FREE (bb_info->out); | |
3342 | } | |
3343 | ||
3344 | /* For any block in an infinite loop, we must initialize the out set | |
3345 | to all ones. This could be expensive, but almost never occurs in | |
3346 | practice. However, it is common in regression tests. */ | |
d4ac4ce2 | 3347 | EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi) |
6fb5fa3c DB |
3348 | { |
3349 | if (bitmap_bit_p (all_blocks, i)) | |
3350 | { | |
3351 | bb_info_t bb_info = bb_table[i]; | |
3352 | if (!all_ones) | |
3353 | { | |
3354 | unsigned int j; | |
3355 | group_info_t group; | |
3356 | ||
3f9b14ff | 3357 | all_ones = BITMAP_ALLOC (&dse_bitmap_obstack); |
9771b263 | 3358 | FOR_EACH_VEC_ELT (rtx_group_vec, j, group) |
6fb5fa3c DB |
3359 | bitmap_ior_into (all_ones, group->group_kill); |
3360 | } | |
3361 | if (!bb_info->out) | |
3362 | { | |
3f9b14ff | 3363 | bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack); |
6fb5fa3c DB |
3364 | bitmap_copy (bb_info->out, all_ones); |
3365 | } | |
3366 | } | |
3367 | } | |
3368 | ||
3369 | if (all_ones) | |
3370 | BITMAP_FREE (all_ones); | |
3371 | sbitmap_free (unreachable_blocks); | |
3372 | } | |
3373 | ||
3374 | ||
3375 | \f | |
3376 | /*---------------------------------------------------------------------------- | |
3377 | Fourth step. | |
3378 | ||
3379 | Solve the bitvector equations. | |
3380 | ----------------------------------------------------------------------------*/ | |
3381 | ||
3382 | ||
3383 | /* Confluence function for blocks with no successors. Create an out | |
3384 | set from the gen set of the exit block. This block logically has | |
3385 | the exit block as a successor. */ | |
3386 | ||
3387 | ||
3388 | ||
3389 | static void | |
3390 | dse_confluence_0 (basic_block bb) | |
3391 | { | |
3392 | bb_info_t bb_info = bb_table[bb->index]; | |
3393 | ||
3394 | if (bb->index == EXIT_BLOCK) | |
3395 | return; | |
3396 | ||
3397 | if (!bb_info->out) | |
3398 | { | |
3f9b14ff | 3399 | bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack); |
6fb5fa3c DB |
3400 | bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen); |
3401 | } | |
3402 | } | |
3403 | ||
3404 | /* Propagate the information from the in set of the dest of E to the | |
3405 | out set of the src of E. If the various in or out sets are not | |
3406 | there, that means they are all ones. */ | |
3407 | ||
1a0f3fa1 | 3408 | static bool |
6fb5fa3c DB |
3409 | dse_confluence_n (edge e) |
3410 | { | |
3411 | bb_info_t src_info = bb_table[e->src->index]; | |
3412 | bb_info_t dest_info = bb_table[e->dest->index]; | |
3413 | ||
3414 | if (dest_info->in) | |
3415 | { | |
3416 | if (src_info->out) | |
3417 | bitmap_and_into (src_info->out, dest_info->in); | |
3418 | else | |
3419 | { | |
3f9b14ff | 3420 | src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack); |
6fb5fa3c DB |
3421 | bitmap_copy (src_info->out, dest_info->in); |
3422 | } | |
3423 | } | |
1a0f3fa1 | 3424 | return true; |
6fb5fa3c DB |
3425 | } |
3426 | ||
3427 | ||
3428 | /* Propagate the info from the out to the in set of BB_INDEX's basic | |
b8698a0f | 3429 | block. There are three cases: |
6fb5fa3c DB |
3430 | |
3431 | 1) The block has no kill set. In this case the kill set is all | |
3432 | ones. It does not matter what the out set of the block is, none of | |
3433 | the info can reach the top. The only thing that reaches the top is | |
3434 | the gen set and we just copy the set. | |
3435 | ||
3436 | 2) There is a kill set but no out set and bb has successors. In | |
3437 | this case we just return. Eventually an out set will be created and | |
3438 | it is better to wait than to create a set of ones. | |
3439 | ||
3440 | 3) There is both a kill and out set. We apply the obvious transfer | |
3441 | function. | |
3442 | */ | |
3443 | ||
3444 | static bool | |
3445 | dse_transfer_function (int bb_index) | |
3446 | { | |
3447 | bb_info_t bb_info = bb_table[bb_index]; | |
3448 | ||
3449 | if (bb_info->kill) | |
3450 | { | |
3451 | if (bb_info->out) | |
3452 | { | |
3453 | /* Case 3 above. */ | |
3454 | if (bb_info->in) | |
b8698a0f | 3455 | return bitmap_ior_and_compl (bb_info->in, bb_info->gen, |
6fb5fa3c DB |
3456 | bb_info->out, bb_info->kill); |
3457 | else | |
3458 | { | |
3f9b14ff | 3459 | bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack); |
b8698a0f | 3460 | bitmap_ior_and_compl (bb_info->in, bb_info->gen, |
6fb5fa3c DB |
3461 | bb_info->out, bb_info->kill); |
3462 | return true; | |
3463 | } | |
3464 | } | |
3465 | else | |
3466 | /* Case 2 above. */ | |
3467 | return false; | |
3468 | } | |
3469 | else | |
3470 | { | |
3471 | /* Case 1 above. If there is already an in set, nothing | |
3472 | happens. */ | |
3473 | if (bb_info->in) | |
3474 | return false; | |
3475 | else | |
3476 | { | |
3f9b14ff | 3477 | bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack); |
6fb5fa3c DB |
3478 | bitmap_copy (bb_info->in, bb_info->gen); |
3479 | return true; | |
3480 | } | |
3481 | } | |
3482 | } | |
3483 | ||
3484 | /* Solve the dataflow equations. */ | |
3485 | ||
3486 | static void | |
3487 | dse_step4 (void) | |
3488 | { | |
b8698a0f L |
3489 | df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0, |
3490 | dse_confluence_n, dse_transfer_function, | |
3491 | all_blocks, df_get_postorder (DF_BACKWARD), | |
6fb5fa3c | 3492 | df_get_n_blocks (DF_BACKWARD)); |
456610d3 | 3493 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
3494 | { |
3495 | basic_block bb; | |
3496 | ||
3497 | fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n"); | |
04a90bec | 3498 | FOR_ALL_BB_FN (bb, cfun) |
6fb5fa3c DB |
3499 | { |
3500 | bb_info_t bb_info = bb_table[bb->index]; | |
3501 | ||
3502 | df_print_bb_index (bb, dump_file); | |
3503 | if (bb_info->in) | |
3504 | bitmap_print (dump_file, bb_info->in, " in: ", "\n"); | |
3505 | else | |
3506 | fprintf (dump_file, " in: *MISSING*\n"); | |
3507 | if (bb_info->gen) | |
3508 | bitmap_print (dump_file, bb_info->gen, " gen: ", "\n"); | |
3509 | else | |
3510 | fprintf (dump_file, " gen: *MISSING*\n"); | |
3511 | if (bb_info->kill) | |
3512 | bitmap_print (dump_file, bb_info->kill, " kill: ", "\n"); | |
3513 | else | |
3514 | fprintf (dump_file, " kill: *MISSING*\n"); | |
3515 | if (bb_info->out) | |
3516 | bitmap_print (dump_file, bb_info->out, " out: ", "\n"); | |
3517 | else | |
3518 | fprintf (dump_file, " out: *MISSING*\n\n"); | |
3519 | } | |
3520 | } | |
3521 | } | |
3522 | ||
3523 | ||
3524 | \f | |
3525 | /*---------------------------------------------------------------------------- | |
3526 | Fifth step. | |
3527 | ||
0d52bcc1 | 3528 | Delete the stores that can only be deleted using the global information. |
6fb5fa3c DB |
3529 | ----------------------------------------------------------------------------*/ |
3530 | ||
3531 | ||
3532 | static void | |
3533 | dse_step5_nospill (void) | |
3534 | { | |
3535 | basic_block bb; | |
11cd3bed | 3536 | FOR_EACH_BB_FN (bb, cfun) |
6fb5fa3c DB |
3537 | { |
3538 | bb_info_t bb_info = bb_table[bb->index]; | |
3539 | insn_info_t insn_info = bb_info->last_insn; | |
3540 | bitmap v = bb_info->out; | |
3541 | ||
3542 | while (insn_info) | |
3543 | { | |
3544 | bool deleted = false; | |
3545 | if (dump_file && insn_info->insn) | |
3546 | { | |
3547 | fprintf (dump_file, "starting to process insn %d\n", | |
3548 | INSN_UID (insn_info->insn)); | |
3549 | bitmap_print (dump_file, v, " v: ", "\n"); | |
3550 | } | |
3551 | ||
3552 | /* There may have been code deleted by the dce pass run before | |
3553 | this phase. */ | |
b8698a0f | 3554 | if (insn_info->insn |
6fb5fa3c DB |
3555 | && INSN_P (insn_info->insn) |
3556 | && (!insn_info->cannot_delete) | |
3557 | && (!bitmap_empty_p (v))) | |
3558 | { | |
3559 | store_info_t store_info = insn_info->store_rec; | |
3560 | ||
3561 | /* Try to delete the current insn. */ | |
3562 | deleted = true; | |
b8698a0f | 3563 | |
6fb5fa3c DB |
3564 | /* Skip the clobbers. */ |
3565 | while (!store_info->is_set) | |
3566 | store_info = store_info->next; | |
3567 | ||
3568 | if (store_info->alias_set) | |
3569 | deleted = false; | |
3570 | else | |
3571 | { | |
3572 | HOST_WIDE_INT i; | |
b8698a0f | 3573 | group_info_t group_info |
9771b263 | 3574 | = rtx_group_vec[store_info->group_id]; |
b8698a0f | 3575 | |
6fb5fa3c DB |
3576 | for (i = store_info->begin; i < store_info->end; i++) |
3577 | { | |
3578 | int index = get_bitmap_index (group_info, i); | |
b8698a0f | 3579 | |
456610d3 | 3580 | if (dump_file && (dump_flags & TDF_DETAILS)) |
b8698a0f | 3581 | fprintf (dump_file, "i = %d, index = %d\n", (int)i, index); |
6fb5fa3c DB |
3582 | if (index == 0 || !bitmap_bit_p (v, index)) |
3583 | { | |
456610d3 | 3584 | if (dump_file && (dump_flags & TDF_DETAILS)) |
b8698a0f | 3585 | fprintf (dump_file, "failing at i = %d\n", (int)i); |
6fb5fa3c DB |
3586 | deleted = false; |
3587 | break; | |
3588 | } | |
3589 | } | |
3590 | } | |
3591 | if (deleted) | |
3592 | { | |
9e582b1d JR |
3593 | if (dbg_cnt (dse) |
3594 | && check_for_inc_dec_1 (insn_info)) | |
6fb5fa3c | 3595 | { |
6fb5fa3c DB |
3596 | delete_insn (insn_info->insn); |
3597 | insn_info->insn = NULL; | |
3598 | globally_deleted++; | |
3599 | } | |
3600 | } | |
3601 | } | |
3602 | /* We do want to process the local info if the insn was | |
6ed3da00 | 3603 | deleted. For instance, if the insn did a wild read, we |
6fb5fa3c | 3604 | no longer need to trash the info. */ |
b8698a0f | 3605 | if (insn_info->insn |
6fb5fa3c DB |
3606 | && INSN_P (insn_info->insn) |
3607 | && (!deleted)) | |
3608 | { | |
3609 | scan_stores_nospill (insn_info->store_rec, v, NULL); | |
3610 | if (insn_info->wild_read) | |
3611 | { | |
456610d3 | 3612 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
3613 | fprintf (dump_file, "wild read\n"); |
3614 | bitmap_clear (v); | |
3615 | } | |
d26c7090 ER |
3616 | else if (insn_info->read_rec |
3617 | || insn_info->non_frame_wild_read) | |
6fb5fa3c | 3618 | { |
d26c7090 | 3619 | if (dump_file && !insn_info->non_frame_wild_read) |
6fb5fa3c | 3620 | fprintf (dump_file, "regular read\n"); |
456610d3 | 3621 | else if (dump_file && (dump_flags & TDF_DETAILS)) |
d26c7090 | 3622 | fprintf (dump_file, "non-frame wild read\n"); |
6fb5fa3c DB |
3623 | scan_reads_nospill (insn_info, v, NULL); |
3624 | } | |
3625 | } | |
b8698a0f | 3626 | |
6fb5fa3c DB |
3627 | insn_info = insn_info->prev_insn; |
3628 | } | |
3629 | } | |
3630 | } | |
3631 | ||
3632 | ||
6fb5fa3c DB |
3633 | \f |
3634 | /*---------------------------------------------------------------------------- | |
3635 | Sixth step. | |
3636 | ||
8dd5516b JJ |
3637 | Delete stores made redundant by earlier stores (which store the same |
3638 | value) that couldn't be eliminated. | |
3639 | ----------------------------------------------------------------------------*/ | |
3640 | ||
3641 | static void | |
3642 | dse_step6 (void) | |
3643 | { | |
3644 | basic_block bb; | |
3645 | ||
04a90bec | 3646 | FOR_ALL_BB_FN (bb, cfun) |
8dd5516b JJ |
3647 | { |
3648 | bb_info_t bb_info = bb_table[bb->index]; | |
3649 | insn_info_t insn_info = bb_info->last_insn; | |
3650 | ||
3651 | while (insn_info) | |
3652 | { | |
3653 | /* There may have been code deleted by the dce pass run before | |
3654 | this phase. */ | |
3655 | if (insn_info->insn | |
3656 | && INSN_P (insn_info->insn) | |
3657 | && !insn_info->cannot_delete) | |
3658 | { | |
3659 | store_info_t s_info = insn_info->store_rec; | |
3660 | ||
3661 | while (s_info && !s_info->is_set) | |
3662 | s_info = s_info->next; | |
3663 | if (s_info | |
3664 | && s_info->redundant_reason | |
3665 | && s_info->redundant_reason->insn | |
3666 | && INSN_P (s_info->redundant_reason->insn)) | |
3667 | { | |
eb92d49a | 3668 | rtx_insn *rinsn = s_info->redundant_reason->insn; |
456610d3 | 3669 | if (dump_file && (dump_flags & TDF_DETAILS)) |
8dd5516b JJ |
3670 | fprintf (dump_file, "Locally deleting insn %d " |
3671 | "because insn %d stores the " | |
3672 | "same value and couldn't be " | |
3673 | "eliminated\n", | |
3674 | INSN_UID (insn_info->insn), | |
3675 | INSN_UID (rinsn)); | |
3676 | delete_dead_store_insn (insn_info); | |
3677 | } | |
3678 | } | |
3679 | insn_info = insn_info->prev_insn; | |
3680 | } | |
3681 | } | |
3682 | } | |
3683 | \f | |
3684 | /*---------------------------------------------------------------------------- | |
3685 | Seventh step. | |
3686 | ||
b8698a0f | 3687 | Destroy everything left standing. |
6fb5fa3c DB |
3688 | ----------------------------------------------------------------------------*/ |
3689 | ||
b8698a0f | 3690 | static void |
3f9b14ff | 3691 | dse_step7 (void) |
6fb5fa3c | 3692 | { |
3f9b14ff SB |
3693 | bitmap_obstack_release (&dse_bitmap_obstack); |
3694 | obstack_free (&dse_obstack, NULL); | |
370f38e8 | 3695 | |
6fb5fa3c DB |
3696 | end_alias_analysis (); |
3697 | free (bb_table); | |
c203e8a7 TS |
3698 | delete rtx_group_table; |
3699 | rtx_group_table = NULL; | |
9771b263 | 3700 | rtx_group_vec.release (); |
6fb5fa3c DB |
3701 | BITMAP_FREE (all_blocks); |
3702 | BITMAP_FREE (scratch); | |
3703 | ||
3704 | free_alloc_pool (rtx_store_info_pool); | |
3705 | free_alloc_pool (read_info_pool); | |
3706 | free_alloc_pool (insn_info_pool); | |
3707 | free_alloc_pool (bb_info_pool); | |
3708 | free_alloc_pool (rtx_group_info_pool); | |
3709 | free_alloc_pool (deferred_change_pool); | |
3710 | } | |
3711 | ||
3712 | ||
6fb5fa3c DB |
3713 | /* ------------------------------------------------------------------------- |
3714 | DSE | |
3715 | ------------------------------------------------------------------------- */ | |
3716 | ||
3717 | /* Callback for running pass_rtl_dse. */ | |
3718 | ||
3719 | static unsigned int | |
3720 | rest_of_handle_dse (void) | |
3721 | { | |
6fb5fa3c DB |
3722 | df_set_flags (DF_DEFER_INSN_RESCAN); |
3723 | ||
02b47899 KZ |
3724 | /* Need the notes since we must track live hardregs in the forwards |
3725 | direction. */ | |
3726 | df_note_add_problem (); | |
3727 | df_analyze (); | |
3728 | ||
6fb5fa3c DB |
3729 | dse_step0 (); |
3730 | dse_step1 (); | |
3731 | dse_step2_init (); | |
3732 | if (dse_step2_nospill ()) | |
3733 | { | |
3734 | df_set_flags (DF_LR_RUN_DCE); | |
3735 | df_analyze (); | |
456610d3 | 3736 | if (dump_file && (dump_flags & TDF_DETAILS)) |
6fb5fa3c DB |
3737 | fprintf (dump_file, "doing global processing\n"); |
3738 | dse_step3 (false); | |
3739 | dse_step4 (); | |
3740 | dse_step5_nospill (); | |
3741 | } | |
3742 | ||
8dd5516b | 3743 | dse_step6 (); |
3f9b14ff | 3744 | dse_step7 (); |
6fb5fa3c DB |
3745 | |
3746 | if (dump_file) | |
3747 | fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n", | |
3748 | locally_deleted, globally_deleted, spill_deleted); | |
3749 | return 0; | |
3750 | } | |
3751 | ||
27a4cd48 DM |
3752 | namespace { |
3753 | ||
3754 | const pass_data pass_data_rtl_dse1 = | |
3755 | { | |
3756 | RTL_PASS, /* type */ | |
3757 | "dse1", /* name */ | |
3758 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
3759 | TV_DSE1, /* tv_id */ |
3760 | 0, /* properties_required */ | |
3761 | 0, /* properties_provided */ | |
3762 | 0, /* properties_destroyed */ | |
3763 | 0, /* todo_flags_start */ | |
3bea341f | 3764 | TODO_df_finish, /* todo_flags_finish */ |
6fb5fa3c DB |
3765 | }; |
3766 | ||
27a4cd48 DM |
3767 | class pass_rtl_dse1 : public rtl_opt_pass |
3768 | { | |
3769 | public: | |
c3284718 RS |
3770 | pass_rtl_dse1 (gcc::context *ctxt) |
3771 | : rtl_opt_pass (pass_data_rtl_dse1, ctxt) | |
27a4cd48 DM |
3772 | {} |
3773 | ||
3774 | /* opt_pass methods: */ | |
1a3d085c TS |
3775 | virtual bool gate (function *) |
3776 | { | |
3777 | return optimize > 0 && flag_dse && dbg_cnt (dse1); | |
3778 | } | |
3779 | ||
be55bfe6 | 3780 | virtual unsigned int execute (function *) { return rest_of_handle_dse (); } |
27a4cd48 DM |
3781 | |
3782 | }; // class pass_rtl_dse1 | |
3783 | ||
3784 | } // anon namespace | |
3785 | ||
3786 | rtl_opt_pass * | |
3787 | make_pass_rtl_dse1 (gcc::context *ctxt) | |
3788 | { | |
3789 | return new pass_rtl_dse1 (ctxt); | |
3790 | } | |
3791 | ||
3792 | namespace { | |
3793 | ||
3794 | const pass_data pass_data_rtl_dse2 = | |
3795 | { | |
3796 | RTL_PASS, /* type */ | |
3797 | "dse2", /* name */ | |
3798 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
3799 | TV_DSE2, /* tv_id */ |
3800 | 0, /* properties_required */ | |
3801 | 0, /* properties_provided */ | |
3802 | 0, /* properties_destroyed */ | |
3803 | 0, /* todo_flags_start */ | |
3bea341f | 3804 | TODO_df_finish, /* todo_flags_finish */ |
6fb5fa3c | 3805 | }; |
27a4cd48 DM |
3806 | |
3807 | class pass_rtl_dse2 : public rtl_opt_pass | |
3808 | { | |
3809 | public: | |
c3284718 RS |
3810 | pass_rtl_dse2 (gcc::context *ctxt) |
3811 | : rtl_opt_pass (pass_data_rtl_dse2, ctxt) | |
27a4cd48 DM |
3812 | {} |
3813 | ||
3814 | /* opt_pass methods: */ | |
1a3d085c TS |
3815 | virtual bool gate (function *) |
3816 | { | |
3817 | return optimize > 0 && flag_dse && dbg_cnt (dse2); | |
3818 | } | |
3819 | ||
be55bfe6 | 3820 | virtual unsigned int execute (function *) { return rest_of_handle_dse (); } |
27a4cd48 DM |
3821 | |
3822 | }; // class pass_rtl_dse2 | |
3823 | ||
3824 | } // anon namespace | |
3825 | ||
3826 | rtl_opt_pass * | |
3827 | make_pass_rtl_dse2 (gcc::context *ctxt) | |
3828 | { | |
3829 | return new pass_rtl_dse2 (ctxt); | |
3830 | } |