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