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