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