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