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