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