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