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