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