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