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