1 /* RTL dead store elimination.
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
4 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
5 and Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
27 #include "coretypes.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
42 #include "tree-pass.h"
43 #include "alloc-pool.h"
44 #include "insn-config.h"
54 #include "insn-codes.h"
59 #include "internal-fn.h"
60 #include "gimple-ssa.h"
62 #include "cfgcleanup.h"
64 /* This file contains three techniques for performing Dead Store
67 * The first technique performs dse locally on any base address. It
68 is based on the cselib which is a local value numbering technique.
69 This technique is local to a basic block but deals with a fairly
72 * The second technique performs dse globally but is restricted to
73 base addresses that are either constant or are relative to the
76 * The third technique, (which is only done after register allocation)
77 processes the spill spill slots. This differs from the second
78 technique because it takes advantage of the fact that spilling is
79 completely free from the effects of aliasing.
81 Logically, dse is a backwards dataflow problem. A store can be
82 deleted if it if cannot be reached in the backward direction by any
83 use of the value being stored. However, the local technique uses a
84 forwards scan of the basic block because cselib requires that the
85 block be processed in that order.
87 The pass is logically broken into 7 steps:
91 1) The local algorithm, as well as scanning the insns for the two
94 2) Analysis to see if the global algs are necessary. In the case
95 of stores base on a constant address, there must be at least two
96 stores to that address, to make it possible to delete some of the
97 stores. In the case of stores off of the frame or spill related
98 stores, only one store to an address is necessary because those
99 stores die at the end of the function.
101 3) Set up the global dataflow equations based on processing the
102 info parsed in the first step.
104 4) Solve the dataflow equations.
106 5) Delete the insns that the global analysis has indicated are
109 6) Delete insns that store the same value as preceding store
110 where the earlier store couldn't be eliminated.
114 This step uses cselib and canon_rtx to build the largest expression
115 possible for each address. This pass is a forwards pass through
116 each basic block. From the point of view of the global technique,
117 the first pass could examine a block in either direction. The
118 forwards ordering is to accommodate cselib.
120 We make a simplifying assumption: addresses fall into four broad
123 1) base has rtx_varies_p == false, offset is constant.
124 2) base has rtx_varies_p == false, offset variable.
125 3) base has rtx_varies_p == true, offset constant.
126 4) base has rtx_varies_p == true, offset variable.
128 The local passes are able to process all 4 kinds of addresses. The
129 global pass only handles 1).
131 The global problem is formulated as follows:
133 A store, S1, to address A, where A is not relative to the stack
134 frame, can be eliminated if all paths from S1 to the end of the
135 function contain another store to A before a read to A.
137 If the address A is relative to the stack frame, a store S2 to A
138 can be eliminated if there are no paths from S2 that reach the
139 end of the function that read A before another store to A. In
140 this case S2 can be deleted if there are paths from S2 to the
141 end of the function that have no reads or writes to A. This
142 second case allows stores to the stack frame to be deleted that
143 would otherwise die when the function returns. This cannot be
144 done if stores_off_frame_dead_at_return is not true. See the doc
145 for that variable for when this variable is false.
147 The global problem is formulated as a backwards set union
148 dataflow problem where the stores are the gens and reads are the
149 kills. Set union problems are rare and require some special
150 handling given our representation of bitmaps. A straightforward
151 implementation requires a lot of bitmaps filled with 1s.
152 These are expensive and cumbersome in our bitmap formulation so
153 care has been taken to avoid large vectors filled with 1s. See
154 the comments in bb_info and in the dataflow confluence functions
157 There are two places for further enhancements to this algorithm:
159 1) The original dse which was embedded in a pass called flow also
160 did local address forwarding. For example in
165 flow would replace the right hand side of the second insn with a
166 reference to r100. Most of the information is available to add this
167 to this pass. It has not done it because it is a lot of work in
168 the case that either r100 is assigned to between the first and
169 second insn and/or the second insn is a load of part of the value
170 stored by the first insn.
172 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
173 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
174 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
175 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
177 2) The cleaning up of spill code is quite profitable. It currently
178 depends on reading tea leaves and chicken entrails left by reload.
179 This pass depends on reload creating a singleton alias set for each
180 spill slot and telling the next dse pass which of these alias sets
181 are the singletons. Rather than analyze the addresses of the
182 spills, dse's spill processing just does analysis of the loads and
183 stores that use those alias sets. There are three cases where this
186 a) Reload sometimes creates the slot for one mode of access, and
187 then inserts loads and/or stores for a smaller mode. In this
188 case, the current code just punts on the slot. The proper thing
189 to do is to back out and use one bit vector position for each
190 byte of the entity associated with the slot. This depends on
191 KNOWING that reload always generates the accesses for each of the
192 bytes in some canonical (read that easy to understand several
193 passes after reload happens) way.
195 b) Reload sometimes decides that spill slot it allocated was not
196 large enough for the mode and goes back and allocates more slots
197 with the same mode and alias set. The backout in this case is a
198 little more graceful than (a). In this case the slot is unmarked
199 as being a spill slot and if final address comes out to be based
200 off the frame pointer, the global algorithm handles this slot.
202 c) For any pass that may prespill, there is currently no
203 mechanism to tell the dse pass that the slot being used has the
204 special properties that reload uses. It may be that all that is
205 required is to have those passes make the same calls that reload
206 does, assuming that the alias sets can be manipulated in the same
209 /* There are limits to the size of constant offsets we model for the
210 global problem. There are certainly test cases, that exceed this
211 limit, however, it is unlikely that there are important programs
212 that really have constant offsets this size. */
213 #define MAX_OFFSET (64 * 1024)
215 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
216 on the default obstack because these bitmaps can grow quite large
217 (~2GB for the small (!) test case of PR54146) and we'll hold on to
218 all that memory until the end of the compiler run.
219 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
220 releasing the whole obstack. */
221 static bitmap_obstack dse_bitmap_obstack
;
223 /* Obstack for other data. As for above: Kinda nice to be able to
224 throw it all away at the end in one big sweep. */
225 static struct obstack dse_obstack
;
227 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
228 static bitmap scratch
= NULL
;
230 struct insn_info_type
;
232 /* This structure holds information about a candidate store. */
236 /* False means this is a clobber. */
239 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
242 /* The id of the mem group of the base address. If rtx_varies_p is
243 true, this is -1. Otherwise, it is the index into the group
247 /* This is the cselib value. */
248 cselib_val
*cse_base
;
250 /* This canonized mem. */
253 /* Canonized MEM address for use by canon_true_dependence. */
256 /* If this is non-zero, it is the alias set of a spill location. */
257 alias_set_type alias_set
;
259 /* The offset of the first and byte before the last byte associated
260 with the operation. */
261 HOST_WIDE_INT begin
, end
;
265 /* A bitmask as wide as the number of bytes in the word that
266 contains a 1 if the byte may be needed. The store is unused if
267 all of the bits are 0. This is used if IS_LARGE is false. */
268 unsigned HOST_WIDE_INT small_bitmask
;
272 /* A bitmap with one bit per byte. Cleared bit means the position
273 is needed. Used if IS_LARGE is false. */
276 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
277 equal to END - BEGIN, the whole store is unused. */
282 /* The next store info for this insn. */
283 struct store_info
*next
;
285 /* The right hand side of the store. This is used if there is a
286 subsequent reload of the mems address somewhere later in the
290 /* If rhs is or holds a constant, this contains that constant,
294 /* Set if this store stores the same constant value as REDUNDANT_REASON
295 insn stored. These aren't eliminated early, because doing that
296 might prevent the earlier larger store to be eliminated. */
297 struct insn_info_type
*redundant_reason
;
300 /* Return a bitmask with the first N low bits set. */
302 static unsigned HOST_WIDE_INT
303 lowpart_bitmask (int n
)
305 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
306 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
309 typedef struct store_info
*store_info_t
;
310 static pool_allocator
<store_info
> cse_store_info_pool ("cse_store_info_pool",
313 static pool_allocator
<store_info
> rtx_store_info_pool ("rtx_store_info_pool",
316 /* This structure holds information about a load. These are only
317 built for rtx bases. */
318 struct read_info_type
320 /* The id of the mem group of the base address. */
323 /* If this is non-zero, it is the alias set of a spill location. */
324 alias_set_type alias_set
;
326 /* The offset of the first and byte after the last byte associated
327 with the operation. If begin == end == 0, the read did not have
328 a constant offset. */
331 /* The mem being read. */
334 /* The next read_info for this insn. */
335 struct read_info_type
*next
;
337 /* Pool allocation new operator. */
338 inline void *operator new (size_t)
340 return pool
.allocate ();
343 /* Delete operator utilizing pool allocation. */
344 inline void operator delete (void *ptr
)
346 pool
.remove ((read_info_type
*) ptr
);
349 /* Memory allocation pool. */
350 static pool_allocator
<read_info_type
> pool
;
352 typedef struct read_info_type
*read_info_t
;
354 pool_allocator
<read_info_type
> read_info_type::pool ("read_info_pool", 100);
356 /* One of these records is created for each insn. */
358 struct insn_info_type
360 /* Set true if the insn contains a store but the insn itself cannot
361 be deleted. This is set if the insn is a parallel and there is
362 more than one non dead output or if the insn is in some way
366 /* This field is only used by the global algorithm. It is set true
367 if the insn contains any read of mem except for a (1). This is
368 also set if the insn is a call or has a clobber mem. If the insn
369 contains a wild read, the use_rec will be null. */
372 /* This is true only for CALL instructions which could potentially read
373 any non-frame memory location. This field is used by the global
375 bool non_frame_wild_read
;
377 /* This field is only used for the processing of const functions.
378 These functions cannot read memory, but they can read the stack
379 because that is where they may get their parms. We need to be
380 this conservative because, like the store motion pass, we don't
381 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
382 Moreover, we need to distinguish two cases:
383 1. Before reload (register elimination), the stores related to
384 outgoing arguments are stack pointer based and thus deemed
385 of non-constant base in this pass. This requires special
386 handling but also means that the frame pointer based stores
387 need not be killed upon encountering a const function call.
388 2. After reload, the stores related to outgoing arguments can be
389 either stack pointer or hard frame pointer based. This means
390 that we have no other choice than also killing all the frame
391 pointer based stores upon encountering a const function call.
392 This field is set after reload for const function calls and before
393 reload for const tail function calls on targets where arg pointer
394 is the frame pointer. Having this set is less severe than a wild
395 read, it just means that all the frame related stores are killed
396 rather than all the stores. */
399 /* This field is only used for the processing of const functions.
400 It is set if the insn may contain a stack pointer based store. */
401 bool stack_pointer_based
;
403 /* This is true if any of the sets within the store contains a
404 cselib base. Such stores can only be deleted by the local
406 bool contains_cselib_groups
;
411 /* The list of mem sets or mem clobbers that are contained in this
412 insn. If the insn is deletable, it contains only one mem set.
413 But it could also contain clobbers. Insns that contain more than
414 one mem set are not deletable, but each of those mems are here in
415 order to provide info to delete other insns. */
416 store_info_t store_rec
;
418 /* The linked list of mem uses in this insn. Only the reads from
419 rtx bases are listed here. The reads to cselib bases are
420 completely processed during the first scan and so are never
422 read_info_t read_rec
;
424 /* The live fixed registers. We assume only fixed registers can
425 cause trouble by being clobbered from an expanded pattern;
426 storing only the live fixed registers (rather than all registers)
427 means less memory needs to be allocated / copied for the individual
429 regset fixed_regs_live
;
431 /* The prev insn in the basic block. */
432 struct insn_info_type
* prev_insn
;
434 /* The linked list of insns that are in consideration for removal in
435 the forwards pass through the basic block. This pointer may be
436 trash as it is not cleared when a wild read occurs. The only
437 time it is guaranteed to be correct is when the traversal starts
438 at active_local_stores. */
439 struct insn_info_type
* next_local_store
;
441 /* Pool allocation new operator. */
442 inline void *operator new (size_t)
444 return pool
.allocate ();
447 /* Delete operator utilizing pool allocation. */
448 inline void operator delete (void *ptr
)
450 pool
.remove ((insn_info_type
*) ptr
);
453 /* Memory allocation pool. */
454 static pool_allocator
<insn_info_type
> pool
;
456 typedef struct insn_info_type
*insn_info_t
;
458 pool_allocator
<insn_info_type
> insn_info_type::pool ("insn_info_pool", 100);
460 /* The linked list of stores that are under consideration in this
462 static insn_info_t active_local_stores
;
463 static int active_local_stores_len
;
465 struct dse_bb_info_type
467 /* Pointer to the insn info for the last insn in the block. These
468 are linked so this is how all of the insns are reached. During
469 scanning this is the current insn being scanned. */
470 insn_info_t last_insn
;
472 /* The info for the global dataflow problem. */
475 /* This is set if the transfer function should and in the wild_read
476 bitmap before applying the kill and gen sets. That vector knocks
477 out most of the bits in the bitmap and thus speeds up the
479 bool apply_wild_read
;
481 /* The following 4 bitvectors hold information about which positions
482 of which stores are live or dead. They are indexed by
485 /* The set of store positions that exist in this block before a wild read. */
488 /* The set of load positions that exist in this block above the
489 same position of a store. */
492 /* The set of stores that reach the top of the block without being
495 Do not represent the in if it is all ones. Note that this is
496 what the bitvector should logically be initialized to for a set
497 intersection problem. However, like the kill set, this is too
498 expensive. So initially, the in set will only be created for the
499 exit block and any block that contains a wild read. */
502 /* The set of stores that reach the bottom of the block from it's
505 Do not represent the in if it is all ones. Note that this is
506 what the bitvector should logically be initialized to for a set
507 intersection problem. However, like the kill and in set, this is
508 too expensive. So what is done is that the confluence operator
509 just initializes the vector from one of the out sets of the
510 successors of the block. */
513 /* The following bitvector is indexed by the reg number. It
514 contains the set of regs that are live at the current instruction
515 being processed. While it contains info for all of the
516 registers, only the hard registers are actually examined. It is used
517 to assure that shift and/or add sequences that are inserted do not
518 accidentally clobber live hard regs. */
521 /* Pool allocation new operator. */
522 inline void *operator new (size_t)
524 return pool
.allocate ();
527 /* Delete operator utilizing pool allocation. */
528 inline void operator delete (void *ptr
)
530 pool
.remove ((dse_bb_info_type
*) ptr
);
533 /* Memory allocation pool. */
534 static pool_allocator
<dse_bb_info_type
> pool
;
537 typedef struct dse_bb_info_type
*bb_info_t
;
538 pool_allocator
<dse_bb_info_type
> dse_bb_info_type::pool ("bb_info_pool", 100);
540 /* Table to hold all bb_infos. */
541 static bb_info_t
*bb_table
;
543 /* There is a group_info for each rtx base that is used to reference
544 memory. There are also not many of the rtx bases because they are
545 very limited in scope. */
549 /* The actual base of the address. */
552 /* The sequential id of the base. This allows us to have a
553 canonical ordering of these that is not based on addresses. */
556 /* True if there are any positions that are to be processed
558 bool process_globally
;
560 /* True if the base of this group is either the frame_pointer or
561 hard_frame_pointer. */
564 /* A mem wrapped around the base pointer for the group in order to do
565 read dependency. It must be given BLKmode in order to encompass all
566 the possible offsets from the base. */
569 /* Canonized version of base_mem's address. */
572 /* These two sets of two bitmaps are used to keep track of how many
573 stores are actually referencing that position from this base. We
574 only do this for rtx bases as this will be used to assign
575 positions in the bitmaps for the global problem. Bit N is set in
576 store1 on the first store for offset N. Bit N is set in store2
577 for the second store to offset N. This is all we need since we
578 only care about offsets that have two or more stores for them.
580 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
581 for 0 and greater offsets.
583 There is one special case here, for stores into the stack frame,
584 we will or store1 into store2 before deciding which stores look
585 at globally. This is because stores to the stack frame that have
586 no other reads before the end of the function can also be
588 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
590 /* These bitmaps keep track of offsets in this group escape this function.
591 An offset escapes if it corresponds to a named variable whose
592 addressable flag is set. */
593 bitmap escaped_n
, escaped_p
;
595 /* The positions in this bitmap have the same assignments as the in,
596 out, gen and kill bitmaps. This bitmap is all zeros except for
597 the positions that are occupied by stores for this group. */
600 /* The offset_map is used to map the offsets from this base into
601 positions in the global bitmaps. It is only created after all of
602 the all of stores have been scanned and we know which ones we
604 int *offset_map_n
, *offset_map_p
;
605 int offset_map_size_n
, offset_map_size_p
;
607 /* Pool allocation new operator. */
608 inline void *operator new (size_t)
610 return pool
.allocate ();
613 /* Delete operator utilizing pool allocation. */
614 inline void operator delete (void *ptr
)
616 pool
.remove ((group_info
*) ptr
);
619 /* Memory allocation pool. */
620 static pool_allocator
<group_info
> pool
;
622 typedef struct group_info
*group_info_t
;
623 typedef const struct group_info
*const_group_info_t
;
625 pool_allocator
<group_info
> group_info::pool ("rtx_group_info_pool", 100);
627 /* Index into the rtx_group_vec. */
628 static int rtx_group_next_id
;
631 static vec
<group_info_t
> rtx_group_vec
;
634 /* This structure holds the set of changes that are being deferred
635 when removing read operation. See replace_read. */
636 struct deferred_change
639 /* The mem that is being replaced. */
642 /* The reg it is being replaced with. */
645 struct deferred_change
*next
;
647 /* Pool allocation new operator. */
648 inline void *operator new (size_t)
650 return pool
.allocate ();
653 /* Delete operator utilizing pool allocation. */
654 inline void operator delete (void *ptr
)
656 pool
.remove ((deferred_change
*) ptr
);
659 /* Memory allocation pool. */
660 static pool_allocator
<deferred_change
> pool
;
663 typedef struct deferred_change
*deferred_change_t
;
665 pool_allocator
<deferred_change
> deferred_change::pool
666 ("deferred_change_pool", 10);
668 static deferred_change_t deferred_change_list
= NULL
;
670 /* The group that holds all of the clear_alias_sets. */
671 static group_info_t clear_alias_group
;
673 /* The modes of the clear_alias_sets. */
674 static htab_t clear_alias_mode_table
;
676 /* Hash table element to look up the mode for an alias set. */
677 struct clear_alias_mode_holder
679 alias_set_type alias_set
;
683 /* This is true except if cfun->stdarg -- i.e. we cannot do
684 this for vararg functions because they play games with the frame. */
685 static bool stores_off_frame_dead_at_return
;
687 /* Counter for stats. */
688 static int globally_deleted
;
689 static int locally_deleted
;
690 static int spill_deleted
;
692 static bitmap all_blocks
;
694 /* Locations that are killed by calls in the global phase. */
695 static bitmap kill_on_calls
;
697 /* The number of bits used in the global bitmaps. */
698 static unsigned int current_position
;
700 /*----------------------------------------------------------------------------
704 ----------------------------------------------------------------------------*/
707 /* Find the entry associated with ALIAS_SET. */
709 static struct clear_alias_mode_holder
*
710 clear_alias_set_lookup (alias_set_type alias_set
)
712 struct clear_alias_mode_holder tmp_holder
;
715 tmp_holder
.alias_set
= alias_set
;
716 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
719 return (struct clear_alias_mode_holder
*) *slot
;
723 /* Hashtable callbacks for maintaining the "bases" field of
724 store_group_info, given that the addresses are function invariants. */
726 struct invariant_group_base_hasher
: nofree_ptr_hash
<group_info
>
728 static inline hashval_t
hash (const group_info
*);
729 static inline bool equal (const group_info
*, const group_info
*);
733 invariant_group_base_hasher::equal (const group_info
*gi1
,
734 const group_info
*gi2
)
736 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
740 invariant_group_base_hasher::hash (const group_info
*gi
)
743 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
746 /* Tables of group_info structures, hashed by base value. */
747 static hash_table
<invariant_group_base_hasher
> *rtx_group_table
;
750 /* Get the GROUP for BASE. Add a new group if it is not there. */
753 get_group_info (rtx base
)
755 struct group_info tmp_gi
;
761 /* Find the store_base_info structure for BASE, creating a new one
763 tmp_gi
.rtx_base
= base
;
764 slot
= rtx_group_table
->find_slot (&tmp_gi
, INSERT
);
765 gi
= (group_info_t
) *slot
;
769 if (!clear_alias_group
)
771 clear_alias_group
= gi
= new group_info
;
772 memset (gi
, 0, sizeof (struct group_info
));
773 gi
->id
= rtx_group_next_id
++;
774 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
775 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
776 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
777 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
778 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
779 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
780 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
781 gi
->process_globally
= false;
782 gi
->offset_map_size_n
= 0;
783 gi
->offset_map_size_p
= 0;
784 gi
->offset_map_n
= NULL
;
785 gi
->offset_map_p
= NULL
;
786 rtx_group_vec
.safe_push (gi
);
788 return clear_alias_group
;
793 *slot
= gi
= new group_info
;
795 gi
->id
= rtx_group_next_id
++;
796 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
797 gi
->canon_base_addr
= canon_rtx (base
);
798 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
799 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
800 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
801 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
802 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
803 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
804 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
805 gi
->process_globally
= false;
807 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
808 gi
->offset_map_size_n
= 0;
809 gi
->offset_map_size_p
= 0;
810 gi
->offset_map_n
= NULL
;
811 gi
->offset_map_p
= NULL
;
812 rtx_group_vec
.safe_push (gi
);
819 /* Initialization of data structures. */
825 globally_deleted
= 0;
828 bitmap_obstack_initialize (&dse_bitmap_obstack
);
829 gcc_obstack_init (&dse_obstack
);
831 scratch
= BITMAP_ALLOC (®_obstack
);
832 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
835 rtx_group_table
= new hash_table
<invariant_group_base_hasher
> (11);
837 bb_table
= XNEWVEC (bb_info_t
, last_basic_block_for_fn (cfun
));
838 rtx_group_next_id
= 0;
840 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
842 init_alias_analysis ();
844 clear_alias_group
= NULL
;
849 /*----------------------------------------------------------------------------
852 Scan all of the insns. Any random ordering of the blocks is fine.
853 Each block is scanned in forward order to accommodate cselib which
854 is used to remove stores with non-constant bases.
855 ----------------------------------------------------------------------------*/
857 /* Delete all of the store_info recs from INSN_INFO. */
860 free_store_info (insn_info_t insn_info
)
862 store_info_t store_info
= insn_info
->store_rec
;
865 store_info_t next
= store_info
->next
;
866 if (store_info
->is_large
)
867 BITMAP_FREE (store_info
->positions_needed
.large
.bmap
);
868 if (store_info
->cse_base
)
869 cse_store_info_pool
.remove (store_info
);
871 rtx_store_info_pool
.remove (store_info
);
875 insn_info
->cannot_delete
= true;
876 insn_info
->contains_cselib_groups
= false;
877 insn_info
->store_rec
= NULL
;
882 rtx_insn
*first
, *current
;
883 regset fixed_regs_live
;
885 } note_add_store_info
;
887 /* Callback for emit_inc_dec_insn_before via note_stores.
888 Check if a register is clobbered which is live afterwards. */
891 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
894 note_add_store_info
*info
= (note_add_store_info
*) data
;
899 /* If this register is referenced by the current or an earlier insn,
900 that's OK. E.g. this applies to the register that is being incremented
901 with this addition. */
902 for (insn
= info
->first
;
903 insn
!= NEXT_INSN (info
->current
);
904 insn
= NEXT_INSN (insn
))
905 if (reg_referenced_p (loc
, PATTERN (insn
)))
908 /* If we come here, we have a clobber of a register that's only OK
909 if that register is not live. If we don't have liveness information
910 available, fail now. */
911 if (!info
->fixed_regs_live
)
913 info
->failure
= true;
916 /* Now check if this is a live fixed register. */
917 unsigned int end_regno
= END_REGNO (loc
);
918 for (unsigned int regno
= REGNO (loc
); regno
< end_regno
; ++regno
)
919 if (REGNO_REG_SET_P (info
->fixed_regs_live
, regno
))
920 info
->failure
= true;
923 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
924 SRC + SRCOFF before insn ARG. */
927 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
928 rtx op ATTRIBUTE_UNUSED
,
929 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
931 insn_info_t insn_info
= (insn_info_t
) arg
;
932 rtx_insn
*insn
= insn_info
->insn
, *new_insn
, *cur
;
933 note_add_store_info info
;
935 /* We can reuse all operands without copying, because we are about
936 to delete the insn that contained it. */
940 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
941 new_insn
= get_insns ();
945 new_insn
= gen_move_insn (dest
, src
);
946 info
.first
= new_insn
;
947 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
948 info
.failure
= false;
949 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
952 note_stores (PATTERN (cur
), note_add_store
, &info
);
955 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
956 return it immediately, communicating the failure to its caller. */
960 emit_insn_before (new_insn
, insn
);
965 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
966 is there, is split into a separate insn.
967 Return true on success (or if there was nothing to do), false on failure. */
970 check_for_inc_dec_1 (insn_info_t insn_info
)
972 rtx_insn
*insn
= insn_info
->insn
;
973 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
975 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
981 /* Entry point for postreload. If you work on reload_cse, or you need this
982 anywhere else, consider if you can provide register liveness information
983 and add a parameter to this function so that it can be passed down in
984 insn_info.fixed_regs_live. */
986 check_for_inc_dec (rtx_insn
*insn
)
988 insn_info_type insn_info
;
991 insn_info
.insn
= insn
;
992 insn_info
.fixed_regs_live
= NULL
;
993 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
995 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
1000 /* Delete the insn and free all of the fields inside INSN_INFO. */
1003 delete_dead_store_insn (insn_info_t insn_info
)
1005 read_info_t read_info
;
1010 if (!check_for_inc_dec_1 (insn_info
))
1012 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1014 fprintf (dump_file
, "Locally deleting insn %d ",
1015 INSN_UID (insn_info
->insn
));
1016 if (insn_info
->store_rec
->alias_set
)
1017 fprintf (dump_file
, "alias set %d\n",
1018 (int) insn_info
->store_rec
->alias_set
);
1020 fprintf (dump_file
, "\n");
1023 free_store_info (insn_info
);
1024 read_info
= insn_info
->read_rec
;
1028 read_info_t next
= read_info
->next
;
1032 insn_info
->read_rec
= NULL
;
1034 delete_insn (insn_info
->insn
);
1036 insn_info
->insn
= NULL
;
1038 insn_info
->wild_read
= false;
1041 /* Return whether DECL, a local variable, can possibly escape the current
1045 local_variable_can_escape (tree decl
)
1047 if (TREE_ADDRESSABLE (decl
))
1050 /* If this is a partitioned variable, we need to consider all the variables
1051 in the partition. This is necessary because a store into one of them can
1052 be replaced with a store into another and this may not change the outcome
1053 of the escape analysis. */
1054 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
1056 tree
*namep
= cfun
->gimple_df
->decls_to_pointers
->get (decl
);
1058 return TREE_ADDRESSABLE (*namep
);
1064 /* Return whether EXPR can possibly escape the current function scope. */
1067 can_escape (tree expr
)
1072 base
= get_base_address (expr
);
1074 && !may_be_aliased (base
)
1075 && !(TREE_CODE (base
) == VAR_DECL
1076 && !DECL_EXTERNAL (base
)
1077 && !TREE_STATIC (base
)
1078 && local_variable_can_escape (base
)))
1083 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1084 OFFSET and WIDTH. */
1087 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1091 bool expr_escapes
= can_escape (expr
);
1092 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1093 for (i
=offset
; i
<offset
+width
; i
++)
1101 store1
= group
->store1_n
;
1102 store2
= group
->store2_n
;
1103 escaped
= group
->escaped_n
;
1108 store1
= group
->store1_p
;
1109 store2
= group
->store2_p
;
1110 escaped
= group
->escaped_p
;
1114 if (!bitmap_set_bit (store1
, ai
))
1115 bitmap_set_bit (store2
, ai
);
1120 if (group
->offset_map_size_n
< ai
)
1121 group
->offset_map_size_n
= ai
;
1125 if (group
->offset_map_size_p
< ai
)
1126 group
->offset_map_size_p
= ai
;
1130 bitmap_set_bit (escaped
, ai
);
1135 reset_active_stores (void)
1137 active_local_stores
= NULL
;
1138 active_local_stores_len
= 0;
1141 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1144 free_read_records (bb_info_t bb_info
)
1146 insn_info_t insn_info
= bb_info
->last_insn
;
1147 read_info_t
*ptr
= &insn_info
->read_rec
;
1150 read_info_t next
= (*ptr
)->next
;
1151 if ((*ptr
)->alias_set
== 0)
1157 ptr
= &(*ptr
)->next
;
1161 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1164 add_wild_read (bb_info_t bb_info
)
1166 insn_info_t insn_info
= bb_info
->last_insn
;
1167 insn_info
->wild_read
= true;
1168 free_read_records (bb_info
);
1169 reset_active_stores ();
1172 /* Set the BB_INFO so that the last insn is marked as a wild read of
1173 non-frame locations. */
1176 add_non_frame_wild_read (bb_info_t bb_info
)
1178 insn_info_t insn_info
= bb_info
->last_insn
;
1179 insn_info
->non_frame_wild_read
= true;
1180 free_read_records (bb_info
);
1181 reset_active_stores ();
1184 /* Return true if X is a constant or one of the registers that behave
1185 as a constant over the life of a function. This is equivalent to
1186 !rtx_varies_p for memory addresses. */
1189 const_or_frame_p (rtx x
)
1194 if (GET_CODE (x
) == REG
)
1196 /* Note that we have to test for the actual rtx used for the frame
1197 and arg pointers and not just the register number in case we have
1198 eliminated the frame and/or arg pointer and are using it
1200 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1201 /* The arg pointer varies if it is not a fixed register. */
1202 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1203 || x
== pic_offset_table_rtx
)
1211 /* Take all reasonable action to put the address of MEM into the form
1212 that we can do analysis on.
1214 The gold standard is to get the address into the form: address +
1215 OFFSET where address is something that rtx_varies_p considers a
1216 constant. When we can get the address in this form, we can do
1217 global analysis on it. Note that for constant bases, address is
1218 not actually returned, only the group_id. The address can be
1221 If that fails, we try cselib to get a value we can at least use
1222 locally. If that fails we return false.
1224 The GROUP_ID is set to -1 for cselib bases and the index of the
1225 group for non_varying bases.
1227 FOR_READ is true if this is a mem read and false if not. */
1230 canon_address (rtx mem
,
1231 alias_set_type
*alias_set_out
,
1233 HOST_WIDE_INT
*offset
,
1236 machine_mode address_mode
= get_address_mode (mem
);
1237 rtx mem_address
= XEXP (mem
, 0);
1238 rtx expanded_address
, address
;
1243 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1245 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1247 fprintf (dump_file
, " mem: ");
1248 print_inline_rtx (dump_file
, mem_address
, 0);
1249 fprintf (dump_file
, "\n");
1252 /* First see if just canon_rtx (mem_address) is const or frame,
1253 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1255 for (expanded
= 0; expanded
< 2; expanded
++)
1259 /* Use cselib to replace all of the reg references with the full
1260 expression. This will take care of the case where we have
1262 r_x = base + offset;
1267 val = *(base + offset); */
1269 expanded_address
= cselib_expand_value_rtx (mem_address
,
1272 /* If this fails, just go with the address from first
1274 if (!expanded_address
)
1278 expanded_address
= mem_address
;
1280 /* Split the address into canonical BASE + OFFSET terms. */
1281 address
= canon_rtx (expanded_address
);
1285 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1289 fprintf (dump_file
, "\n after cselib_expand address: ");
1290 print_inline_rtx (dump_file
, expanded_address
, 0);
1291 fprintf (dump_file
, "\n");
1294 fprintf (dump_file
, "\n after canon_rtx address: ");
1295 print_inline_rtx (dump_file
, address
, 0);
1296 fprintf (dump_file
, "\n");
1299 if (GET_CODE (address
) == CONST
)
1300 address
= XEXP (address
, 0);
1302 if (GET_CODE (address
) == PLUS
1303 && CONST_INT_P (XEXP (address
, 1)))
1305 *offset
= INTVAL (XEXP (address
, 1));
1306 address
= XEXP (address
, 0);
1309 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1310 && const_or_frame_p (address
))
1312 group_info_t group
= get_group_info (address
);
1314 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1315 fprintf (dump_file
, " gid=%d offset=%d \n",
1316 group
->id
, (int)*offset
);
1318 *group_id
= group
->id
;
1323 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1328 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1329 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1332 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1333 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1334 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1339 /* Clear the rhs field from the active_local_stores array. */
1342 clear_rhs_from_active_local_stores (void)
1344 insn_info_t ptr
= active_local_stores
;
1348 store_info_t store_info
= ptr
->store_rec
;
1349 /* Skip the clobbers. */
1350 while (!store_info
->is_set
)
1351 store_info
= store_info
->next
;
1353 store_info
->rhs
= NULL
;
1354 store_info
->const_rhs
= NULL
;
1356 ptr
= ptr
->next_local_store
;
1361 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1364 set_position_unneeded (store_info_t s_info
, int pos
)
1366 if (__builtin_expect (s_info
->is_large
, false))
1368 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1369 s_info
->positions_needed
.large
.count
++;
1372 s_info
->positions_needed
.small_bitmask
1373 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1376 /* Mark the whole store S_INFO as unneeded. */
1379 set_all_positions_unneeded (store_info_t s_info
)
1381 if (__builtin_expect (s_info
->is_large
, false))
1383 int pos
, end
= s_info
->end
- s_info
->begin
;
1384 for (pos
= 0; pos
< end
; pos
++)
1385 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1386 s_info
->positions_needed
.large
.count
= end
;
1389 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1392 /* Return TRUE if any bytes from S_INFO store are needed. */
1395 any_positions_needed_p (store_info_t s_info
)
1397 if (__builtin_expect (s_info
->is_large
, false))
1398 return (s_info
->positions_needed
.large
.count
1399 < s_info
->end
- s_info
->begin
);
1401 return (s_info
->positions_needed
.small_bitmask
1402 != (unsigned HOST_WIDE_INT
) 0);
1405 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1406 store are needed. */
1409 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1411 if (__builtin_expect (s_info
->is_large
, false))
1413 int end
= start
+ width
;
1415 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1421 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1422 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1427 static rtx
get_stored_val (store_info_t
, machine_mode
, HOST_WIDE_INT
,
1428 HOST_WIDE_INT
, basic_block
, bool);
1431 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1432 there is a candidate store, after adding it to the appropriate
1433 local store group if so. */
1436 record_store (rtx body
, bb_info_t bb_info
)
1438 rtx mem
, rhs
, const_rhs
, mem_addr
;
1439 HOST_WIDE_INT offset
= 0;
1440 HOST_WIDE_INT width
= 0;
1441 alias_set_type spill_alias_set
;
1442 insn_info_t insn_info
= bb_info
->last_insn
;
1443 store_info_t store_info
= NULL
;
1445 cselib_val
*base
= NULL
;
1446 insn_info_t ptr
, last
, redundant_reason
;
1447 bool store_is_unused
;
1449 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1452 mem
= SET_DEST (body
);
1454 /* If this is not used, then this cannot be used to keep the insn
1455 from being deleted. On the other hand, it does provide something
1456 that can be used to prove that another store is dead. */
1458 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1460 /* Check whether that value is a suitable memory location. */
1463 /* If the set or clobber is unused, then it does not effect our
1464 ability to get rid of the entire insn. */
1465 if (!store_is_unused
)
1466 insn_info
->cannot_delete
= true;
1470 /* At this point we know mem is a mem. */
1471 if (GET_MODE (mem
) == BLKmode
)
1473 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1475 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1476 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1477 add_wild_read (bb_info
);
1478 insn_info
->cannot_delete
= true;
1481 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1482 as memset (addr, 0, 36); */
1483 else if (!MEM_SIZE_KNOWN_P (mem
)
1484 || MEM_SIZE (mem
) <= 0
1485 || MEM_SIZE (mem
) > MAX_OFFSET
1486 || GET_CODE (body
) != SET
1487 || !CONST_INT_P (SET_SRC (body
)))
1489 if (!store_is_unused
)
1491 /* If the set or clobber is unused, then it does not effect our
1492 ability to get rid of the entire insn. */
1493 insn_info
->cannot_delete
= true;
1494 clear_rhs_from_active_local_stores ();
1500 /* We can still process a volatile mem, we just cannot delete it. */
1501 if (MEM_VOLATILE_P (mem
))
1502 insn_info
->cannot_delete
= true;
1504 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1506 clear_rhs_from_active_local_stores ();
1510 if (GET_MODE (mem
) == BLKmode
)
1511 width
= MEM_SIZE (mem
);
1513 width
= GET_MODE_SIZE (GET_MODE (mem
));
1515 if (spill_alias_set
)
1517 bitmap store1
= clear_alias_group
->store1_p
;
1518 bitmap store2
= clear_alias_group
->store2_p
;
1520 gcc_assert (GET_MODE (mem
) != BLKmode
);
1522 if (!bitmap_set_bit (store1
, spill_alias_set
))
1523 bitmap_set_bit (store2
, spill_alias_set
);
1525 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1526 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1528 store_info
= rtx_store_info_pool
.allocate ();
1530 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1531 fprintf (dump_file
, " processing spill store %d(%s)\n",
1532 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1534 else if (group_id
>= 0)
1536 /* In the restrictive case where the base is a constant or the
1537 frame pointer we can do global analysis. */
1540 = rtx_group_vec
[group_id
];
1541 tree expr
= MEM_EXPR (mem
);
1543 store_info
= rtx_store_info_pool
.allocate ();
1544 set_usage_bits (group
, offset
, width
, expr
);
1546 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1547 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1548 group_id
, (int)offset
, (int)(offset
+width
));
1552 if (may_be_sp_based_p (XEXP (mem
, 0)))
1553 insn_info
->stack_pointer_based
= true;
1554 insn_info
->contains_cselib_groups
= true;
1556 store_info
= cse_store_info_pool
.allocate ();
1559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1560 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1561 (int)offset
, (int)(offset
+width
));
1564 const_rhs
= rhs
= NULL_RTX
;
1565 if (GET_CODE (body
) == SET
1566 /* No place to keep the value after ra. */
1567 && !reload_completed
1568 && (REG_P (SET_SRC (body
))
1569 || GET_CODE (SET_SRC (body
)) == SUBREG
1570 || CONSTANT_P (SET_SRC (body
)))
1571 && !MEM_VOLATILE_P (mem
)
1572 /* Sometimes the store and reload is used for truncation and
1574 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1576 rhs
= SET_SRC (body
);
1577 if (CONSTANT_P (rhs
))
1579 else if (body
== PATTERN (insn_info
->insn
))
1581 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1582 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1583 const_rhs
= XEXP (tem
, 0);
1585 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1587 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1589 if (tem
&& CONSTANT_P (tem
))
1594 /* Check to see if this stores causes some other stores to be
1596 ptr
= active_local_stores
;
1598 redundant_reason
= NULL
;
1599 mem
= canon_rtx (mem
);
1600 /* For alias_set != 0 canon_true_dependence should be never called. */
1601 if (spill_alias_set
)
1602 mem_addr
= NULL_RTX
;
1606 mem_addr
= base
->val_rtx
;
1610 = rtx_group_vec
[group_id
];
1611 mem_addr
= group
->canon_base_addr
;
1613 /* get_addr can only handle VALUE but cannot handle expr like:
1614 VALUE + OFFSET, so call get_addr to get original addr for
1615 mem_addr before plus_constant. */
1616 mem_addr
= get_addr (mem_addr
);
1618 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1623 insn_info_t next
= ptr
->next_local_store
;
1624 store_info_t s_info
= ptr
->store_rec
;
1627 /* Skip the clobbers. We delete the active insn if this insn
1628 shadows the set. To have been put on the active list, it
1629 has exactly on set. */
1630 while (!s_info
->is_set
)
1631 s_info
= s_info
->next
;
1633 if (s_info
->alias_set
!= spill_alias_set
)
1635 else if (s_info
->alias_set
)
1637 struct clear_alias_mode_holder
*entry
1638 = clear_alias_set_lookup (s_info
->alias_set
);
1639 /* Generally, spills cannot be processed if and of the
1640 references to the slot have a different mode. But if
1641 we are in the same block and mode is exactly the same
1642 between this store and one before in the same block,
1643 we can still delete it. */
1644 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1645 && (GET_MODE (mem
) == entry
->mode
))
1648 set_all_positions_unneeded (s_info
);
1650 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1651 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1652 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1654 else if ((s_info
->group_id
== group_id
)
1655 && (s_info
->cse_base
== base
))
1658 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1659 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1660 INSN_UID (ptr
->insn
), s_info
->group_id
,
1661 (int)s_info
->begin
, (int)s_info
->end
);
1663 /* Even if PTR won't be eliminated as unneeded, if both
1664 PTR and this insn store the same constant value, we might
1665 eliminate this insn instead. */
1666 if (s_info
->const_rhs
1668 && offset
>= s_info
->begin
1669 && offset
+ width
<= s_info
->end
1670 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1673 if (GET_MODE (mem
) == BLKmode
)
1675 if (GET_MODE (s_info
->mem
) == BLKmode
1676 && s_info
->const_rhs
== const_rhs
)
1677 redundant_reason
= ptr
;
1679 else if (s_info
->const_rhs
== const0_rtx
1680 && const_rhs
== const0_rtx
)
1681 redundant_reason
= ptr
;
1686 val
= get_stored_val (s_info
, GET_MODE (mem
),
1687 offset
, offset
+ width
,
1688 BLOCK_FOR_INSN (insn_info
->insn
),
1690 if (get_insns () != NULL
)
1693 if (val
&& rtx_equal_p (val
, const_rhs
))
1694 redundant_reason
= ptr
;
1698 for (i
= MAX (offset
, s_info
->begin
);
1699 i
< offset
+ width
&& i
< s_info
->end
;
1701 set_position_unneeded (s_info
, i
- s_info
->begin
);
1703 else if (s_info
->rhs
)
1704 /* Need to see if it is possible for this store to overwrite
1705 the value of store_info. If it is, set the rhs to NULL to
1706 keep it from being used to remove a load. */
1708 if (canon_true_dependence (s_info
->mem
,
1709 GET_MODE (s_info
->mem
),
1714 s_info
->const_rhs
= NULL
;
1718 /* An insn can be deleted if every position of every one of
1719 its s_infos is zero. */
1720 if (any_positions_needed_p (s_info
))
1725 insn_info_t insn_to_delete
= ptr
;
1727 active_local_stores_len
--;
1729 last
->next_local_store
= ptr
->next_local_store
;
1731 active_local_stores
= ptr
->next_local_store
;
1733 if (!insn_to_delete
->cannot_delete
)
1734 delete_dead_store_insn (insn_to_delete
);
1742 /* Finish filling in the store_info. */
1743 store_info
->next
= insn_info
->store_rec
;
1744 insn_info
->store_rec
= store_info
;
1745 store_info
->mem
= mem
;
1746 store_info
->alias_set
= spill_alias_set
;
1747 store_info
->mem_addr
= mem_addr
;
1748 store_info
->cse_base
= base
;
1749 if (width
> HOST_BITS_PER_WIDE_INT
)
1751 store_info
->is_large
= true;
1752 store_info
->positions_needed
.large
.count
= 0;
1753 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1757 store_info
->is_large
= false;
1758 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1760 store_info
->group_id
= group_id
;
1761 store_info
->begin
= offset
;
1762 store_info
->end
= offset
+ width
;
1763 store_info
->is_set
= GET_CODE (body
) == SET
;
1764 store_info
->rhs
= rhs
;
1765 store_info
->const_rhs
= const_rhs
;
1766 store_info
->redundant_reason
= redundant_reason
;
1768 /* If this is a clobber, we return 0. We will only be able to
1769 delete this insn if there is only one store USED store, but we
1770 can use the clobber to delete other stores earlier. */
1771 return store_info
->is_set
? 1 : 0;
1776 dump_insn_info (const char * start
, insn_info_t insn_info
)
1778 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1779 INSN_UID (insn_info
->insn
),
1780 insn_info
->store_rec
? "has store" : "naked");
1784 /* If the modes are different and the value's source and target do not
1785 line up, we need to extract the value from lower part of the rhs of
1786 the store, shift it, and then put it into a form that can be shoved
1787 into the read_insn. This function generates a right SHIFT of a
1788 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1789 shift sequence is returned or NULL if we failed to find a
1793 find_shift_sequence (int access_size
,
1794 store_info_t store_info
,
1795 machine_mode read_mode
,
1796 int shift
, bool speed
, bool require_cst
)
1798 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1799 machine_mode new_mode
;
1800 rtx read_reg
= NULL
;
1802 /* Some machines like the x86 have shift insns for each size of
1803 operand. Other machines like the ppc or the ia-64 may only have
1804 shift insns that shift values within 32 or 64 bit registers.
1805 This loop tries to find the smallest shift insn that will right
1806 justify the value we want to read but is available in one insn on
1809 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1811 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1812 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1814 rtx target
, new_reg
, new_lhs
;
1815 rtx_insn
*shift_seq
, *insn
;
1818 /* If a constant was stored into memory, try to simplify it here,
1819 otherwise the cost of the shift might preclude this optimization
1820 e.g. at -Os, even when no actual shift will be needed. */
1821 if (store_info
->const_rhs
)
1823 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1824 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1826 if (ret
&& CONSTANT_P (ret
))
1828 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1829 ret
, GEN_INT (shift
));
1830 if (ret
&& CONSTANT_P (ret
))
1832 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1833 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1834 if (ret
&& CONSTANT_P (ret
)
1835 && (set_src_cost (ret
, read_mode
, speed
)
1836 <= COSTS_N_INSNS (1)))
1845 /* Try a wider mode if truncating the store mode to NEW_MODE
1846 requires a real instruction. */
1847 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1848 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1851 /* Also try a wider mode if the necessary punning is either not
1852 desirable or not possible. */
1853 if (!CONSTANT_P (store_info
->rhs
)
1854 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1857 new_reg
= gen_reg_rtx (new_mode
);
1861 /* In theory we could also check for an ashr. Ian Taylor knows
1862 of one dsp where the cost of these two was not the same. But
1863 this really is a rare case anyway. */
1864 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1865 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1867 shift_seq
= get_insns ();
1870 if (target
!= new_reg
|| shift_seq
== NULL
)
1874 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1876 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1878 /* The computation up to here is essentially independent
1879 of the arguments and could be precomputed. It may
1880 not be worth doing so. We could precompute if
1881 worthwhile or at least cache the results. The result
1882 technically depends on both SHIFT and ACCESS_SIZE,
1883 but in practice the answer will depend only on ACCESS_SIZE. */
1885 if (cost
> COSTS_N_INSNS (1))
1888 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1889 copy_rtx (store_info
->rhs
));
1890 if (new_lhs
== NULL_RTX
)
1893 /* We found an acceptable shift. Generate a move to
1894 take the value from the store and put it into the
1895 shift pseudo, then shift it, then generate another
1896 move to put in into the target of the read. */
1897 emit_move_insn (new_reg
, new_lhs
);
1898 emit_insn (shift_seq
);
1899 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1907 /* Call back for note_stores to find the hard regs set or clobbered by
1908 insn. Data is a bitmap of the hardregs set so far. */
1911 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1913 bitmap regs_set
= (bitmap
) data
;
1916 && HARD_REGISTER_P (x
))
1917 bitmap_set_range (regs_set
, REGNO (x
), REG_NREGS (x
));
1920 /* Helper function for replace_read and record_store.
1921 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1922 to one before READ_END bytes read in READ_MODE. Return NULL
1923 if not successful. If REQUIRE_CST is true, return always constant. */
1926 get_stored_val (store_info_t store_info
, machine_mode read_mode
,
1927 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1928 basic_block bb
, bool require_cst
)
1930 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1932 int access_size
; /* In bytes. */
1935 /* To get here the read is within the boundaries of the write so
1936 shift will never be negative. Start out with the shift being in
1938 if (store_mode
== BLKmode
)
1940 else if (BYTES_BIG_ENDIAN
)
1941 shift
= store_info
->end
- read_end
;
1943 shift
= read_begin
- store_info
->begin
;
1945 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1947 /* From now on it is bits. */
1948 shift
*= BITS_PER_UNIT
;
1951 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1952 optimize_bb_for_speed_p (bb
),
1954 else if (store_mode
== BLKmode
)
1956 /* The store is a memset (addr, const_val, const_size). */
1957 gcc_assert (CONST_INT_P (store_info
->rhs
));
1958 store_mode
= int_mode_for_mode (read_mode
);
1959 if (store_mode
== BLKmode
)
1960 read_reg
= NULL_RTX
;
1961 else if (store_info
->rhs
== const0_rtx
)
1962 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1963 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1964 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1965 read_reg
= NULL_RTX
;
1968 unsigned HOST_WIDE_INT c
1969 = INTVAL (store_info
->rhs
)
1970 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1971 int shift
= BITS_PER_UNIT
;
1972 while (shift
< HOST_BITS_PER_WIDE_INT
)
1977 read_reg
= gen_int_mode (c
, store_mode
);
1978 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1981 else if (store_info
->const_rhs
1983 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1984 read_reg
= extract_low_bits (read_mode
, store_mode
,
1985 copy_rtx (store_info
->const_rhs
));
1987 read_reg
= extract_low_bits (read_mode
, store_mode
,
1988 copy_rtx (store_info
->rhs
));
1989 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1990 read_reg
= NULL_RTX
;
1994 /* Take a sequence of:
2017 Depending on the alignment and the mode of the store and
2021 The STORE_INFO and STORE_INSN are for the store and READ_INFO
2022 and READ_INSN are for the read. Return true if the replacement
2026 replace_read (store_info_t store_info
, insn_info_t store_insn
,
2027 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
2030 machine_mode store_mode
= GET_MODE (store_info
->mem
);
2031 machine_mode read_mode
= GET_MODE (read_info
->mem
);
2032 rtx_insn
*insns
, *this_insn
;
2039 /* Create a sequence of instructions to set up the read register.
2040 This sequence goes immediately before the store and its result
2041 is read by the load.
2043 We need to keep this in perspective. We are replacing a read
2044 with a sequence of insns, but the read will almost certainly be
2045 in cache, so it is not going to be an expensive one. Thus, we
2046 are not willing to do a multi insn shift or worse a subroutine
2047 call to get rid of the read. */
2048 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2049 fprintf (dump_file
, "trying to replace %smode load in insn %d"
2050 " from %smode store in insn %d\n",
2051 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
2052 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
2054 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
2055 read_reg
= get_stored_val (store_info
,
2056 read_mode
, read_info
->begin
, read_info
->end
,
2058 if (read_reg
== NULL_RTX
)
2061 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2062 fprintf (dump_file
, " -- could not extract bits of stored value\n");
2065 /* Force the value into a new register so that it won't be clobbered
2066 between the store and the load. */
2067 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
2068 insns
= get_insns ();
2071 if (insns
!= NULL_RTX
)
2073 /* Now we have to scan the set of new instructions to see if the
2074 sequence contains and sets of hardregs that happened to be
2075 live at this point. For instance, this can happen if one of
2076 the insns sets the CC and the CC happened to be live at that
2077 point. This does occasionally happen, see PR 37922. */
2078 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2080 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2081 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2083 bitmap_and_into (regs_set
, regs_live
);
2084 if (!bitmap_empty_p (regs_set
))
2086 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2089 "abandoning replacement because sequence clobbers live hardregs:");
2090 df_print_regset (dump_file
, regs_set
);
2093 BITMAP_FREE (regs_set
);
2096 BITMAP_FREE (regs_set
);
2099 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2101 deferred_change_t change
= new deferred_change
;
2103 /* Insert this right before the store insn where it will be safe
2104 from later insns that might change it before the read. */
2105 emit_insn_before (insns
, store_insn
->insn
);
2107 /* And now for the kludge part: cselib croaks if you just
2108 return at this point. There are two reasons for this:
2110 1) Cselib has an idea of how many pseudos there are and
2111 that does not include the new ones we just added.
2113 2) Cselib does not know about the move insn we added
2114 above the store_info, and there is no way to tell it
2115 about it, because it has "moved on".
2117 Problem (1) is fixable with a certain amount of engineering.
2118 Problem (2) is requires starting the bb from scratch. This
2121 So we are just going to have to lie. The move/extraction
2122 insns are not really an issue, cselib did not see them. But
2123 the use of the new pseudo read_insn is a real problem because
2124 cselib has not scanned this insn. The way that we solve this
2125 problem is that we are just going to put the mem back for now
2126 and when we are finished with the block, we undo this. We
2127 keep a table of mems to get rid of. At the end of the basic
2128 block we can put them back. */
2130 *loc
= read_info
->mem
;
2131 change
->next
= deferred_change_list
;
2132 deferred_change_list
= change
;
2134 change
->reg
= read_reg
;
2136 /* Get rid of the read_info, from the point of view of the
2137 rest of dse, play like this read never happened. */
2138 read_insn
->read_rec
= read_info
->next
;
2140 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2142 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2143 print_simple_rtl (dump_file
, read_reg
);
2144 fprintf (dump_file
, "\n");
2150 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2152 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2153 print_simple_rtl (dump_file
, read_reg
);
2154 fprintf (dump_file
, " led to an invalid instruction\n");
2160 /* Check the address of MEM *LOC and kill any appropriate stores that may
2164 check_mem_read_rtx (rtx
*loc
, bb_info_t bb_info
)
2166 rtx mem
= *loc
, mem_addr
;
2167 insn_info_t insn_info
;
2168 HOST_WIDE_INT offset
= 0;
2169 HOST_WIDE_INT width
= 0;
2170 alias_set_type spill_alias_set
= 0;
2171 cselib_val
*base
= NULL
;
2173 read_info_t read_info
;
2175 insn_info
= bb_info
->last_insn
;
2177 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2178 || (MEM_VOLATILE_P (mem
)))
2180 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2181 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2182 add_wild_read (bb_info
);
2183 insn_info
->cannot_delete
= true;
2187 /* If it is reading readonly mem, then there can be no conflict with
2189 if (MEM_READONLY_P (mem
))
2192 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2194 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2195 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2196 add_wild_read (bb_info
);
2200 if (GET_MODE (mem
) == BLKmode
)
2203 width
= GET_MODE_SIZE (GET_MODE (mem
));
2205 read_info
= new read_info_type
;
2206 read_info
->group_id
= group_id
;
2207 read_info
->mem
= mem
;
2208 read_info
->alias_set
= spill_alias_set
;
2209 read_info
->begin
= offset
;
2210 read_info
->end
= offset
+ width
;
2211 read_info
->next
= insn_info
->read_rec
;
2212 insn_info
->read_rec
= read_info
;
2213 /* For alias_set != 0 canon_true_dependence should be never called. */
2214 if (spill_alias_set
)
2215 mem_addr
= NULL_RTX
;
2219 mem_addr
= base
->val_rtx
;
2223 = rtx_group_vec
[group_id
];
2224 mem_addr
= group
->canon_base_addr
;
2226 /* get_addr can only handle VALUE but cannot handle expr like:
2227 VALUE + OFFSET, so call get_addr to get original addr for
2228 mem_addr before plus_constant. */
2229 mem_addr
= get_addr (mem_addr
);
2231 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2234 /* We ignore the clobbers in store_info. The is mildly aggressive,
2235 but there really should not be a clobber followed by a read. */
2237 if (spill_alias_set
)
2239 insn_info_t i_ptr
= active_local_stores
;
2240 insn_info_t last
= NULL
;
2242 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2243 fprintf (dump_file
, " processing spill load %d\n",
2244 (int) spill_alias_set
);
2248 store_info_t store_info
= i_ptr
->store_rec
;
2250 /* Skip the clobbers. */
2251 while (!store_info
->is_set
)
2252 store_info
= store_info
->next
;
2254 if (store_info
->alias_set
== spill_alias_set
)
2256 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2257 dump_insn_info ("removing from active", i_ptr
);
2259 active_local_stores_len
--;
2261 last
->next_local_store
= i_ptr
->next_local_store
;
2263 active_local_stores
= i_ptr
->next_local_store
;
2267 i_ptr
= i_ptr
->next_local_store
;
2270 else if (group_id
>= 0)
2272 /* This is the restricted case where the base is a constant or
2273 the frame pointer and offset is a constant. */
2274 insn_info_t i_ptr
= active_local_stores
;
2275 insn_info_t last
= NULL
;
2277 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2280 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2283 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2284 group_id
, (int)offset
, (int)(offset
+width
));
2289 bool remove
= false;
2290 store_info_t store_info
= i_ptr
->store_rec
;
2292 /* Skip the clobbers. */
2293 while (!store_info
->is_set
)
2294 store_info
= store_info
->next
;
2296 /* There are three cases here. */
2297 if (store_info
->group_id
< 0)
2298 /* We have a cselib store followed by a read from a
2301 = canon_true_dependence (store_info
->mem
,
2302 GET_MODE (store_info
->mem
),
2303 store_info
->mem_addr
,
2306 else if (group_id
== store_info
->group_id
)
2308 /* This is a block mode load. We may get lucky and
2309 canon_true_dependence may save the day. */
2312 = canon_true_dependence (store_info
->mem
,
2313 GET_MODE (store_info
->mem
),
2314 store_info
->mem_addr
,
2317 /* If this read is just reading back something that we just
2318 stored, rewrite the read. */
2322 && offset
>= store_info
->begin
2323 && offset
+ width
<= store_info
->end
2324 && all_positions_needed_p (store_info
,
2325 offset
- store_info
->begin
,
2327 && replace_read (store_info
, i_ptr
, read_info
,
2328 insn_info
, loc
, bb_info
->regs_live
))
2331 /* The bases are the same, just see if the offsets
2333 if ((offset
< store_info
->end
)
2334 && (offset
+ width
> store_info
->begin
))
2340 The else case that is missing here is that the
2341 bases are constant but different. There is nothing
2342 to do here because there is no overlap. */
2346 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2347 dump_insn_info ("removing from active", i_ptr
);
2349 active_local_stores_len
--;
2351 last
->next_local_store
= i_ptr
->next_local_store
;
2353 active_local_stores
= i_ptr
->next_local_store
;
2357 i_ptr
= i_ptr
->next_local_store
;
2362 insn_info_t i_ptr
= active_local_stores
;
2363 insn_info_t last
= NULL
;
2364 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2366 fprintf (dump_file
, " processing cselib load mem:");
2367 print_inline_rtx (dump_file
, mem
, 0);
2368 fprintf (dump_file
, "\n");
2373 bool remove
= false;
2374 store_info_t store_info
= i_ptr
->store_rec
;
2376 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2377 fprintf (dump_file
, " processing cselib load against insn %d\n",
2378 INSN_UID (i_ptr
->insn
));
2380 /* Skip the clobbers. */
2381 while (!store_info
->is_set
)
2382 store_info
= store_info
->next
;
2384 /* If this read is just reading back something that we just
2385 stored, rewrite the read. */
2387 && store_info
->group_id
== -1
2388 && store_info
->cse_base
== base
2390 && offset
>= store_info
->begin
2391 && offset
+ width
<= store_info
->end
2392 && all_positions_needed_p (store_info
,
2393 offset
- store_info
->begin
, width
)
2394 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2395 bb_info
->regs_live
))
2398 if (!store_info
->alias_set
)
2399 remove
= canon_true_dependence (store_info
->mem
,
2400 GET_MODE (store_info
->mem
),
2401 store_info
->mem_addr
,
2406 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2407 dump_insn_info ("removing from active", i_ptr
);
2409 active_local_stores_len
--;
2411 last
->next_local_store
= i_ptr
->next_local_store
;
2413 active_local_stores
= i_ptr
->next_local_store
;
2417 i_ptr
= i_ptr
->next_local_store
;
2422 /* A note_uses callback in which DATA points the INSN_INFO for
2423 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2424 true for any part of *LOC. */
2427 check_mem_read_use (rtx
*loc
, void *data
)
2429 subrtx_ptr_iterator::array_type array
;
2430 FOR_EACH_SUBRTX_PTR (iter
, array
, loc
, NONCONST
)
2434 check_mem_read_rtx (loc
, (bb_info_t
) data
);
2439 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2440 So far it only handles arguments passed in registers. */
2443 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2445 CUMULATIVE_ARGS args_so_far_v
;
2446 cumulative_args_t args_so_far
;
2450 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2451 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2453 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2455 arg
!= void_list_node
&& idx
< nargs
;
2456 arg
= TREE_CHAIN (arg
), idx
++)
2458 machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2460 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2461 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2462 || GET_MODE_CLASS (mode
) != MODE_INT
)
2465 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2467 link
= XEXP (link
, 1))
2468 if (GET_CODE (XEXP (link
, 0)) == USE
)
2470 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2471 if (REG_P (args
[idx
])
2472 && REGNO (args
[idx
]) == REGNO (reg
)
2473 && (GET_MODE (args
[idx
]) == mode
2474 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2475 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2477 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2478 > GET_MODE_SIZE (mode
)))))
2484 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2485 if (GET_MODE (args
[idx
]) != mode
)
2487 if (!tmp
|| !CONST_INT_P (tmp
))
2489 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2494 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2496 if (arg
!= void_list_node
|| idx
!= nargs
)
2501 /* Return a bitmap of the fixed registers contained in IN. */
2504 copy_fixed_regs (const_bitmap in
)
2508 ret
= ALLOC_REG_SET (NULL
);
2509 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2513 /* Apply record_store to all candidate stores in INSN. Mark INSN
2514 if some part of it is not a candidate store and assigns to a
2515 non-register target. */
2518 scan_insn (bb_info_t bb_info
, rtx_insn
*insn
)
2521 insn_info_type
*insn_info
= new insn_info_type
;
2523 memset (insn_info
, 0, sizeof (struct insn_info_type
));
2525 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2526 fprintf (dump_file
, "\n**scanning insn=%d\n",
2529 insn_info
->prev_insn
= bb_info
->last_insn
;
2530 insn_info
->insn
= insn
;
2531 bb_info
->last_insn
= insn_info
;
2533 if (DEBUG_INSN_P (insn
))
2535 insn_info
->cannot_delete
= true;
2539 /* Look at all of the uses in the insn. */
2540 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2545 tree memset_call
= NULL_TREE
;
2547 insn_info
->cannot_delete
= true;
2549 /* Const functions cannot do anything bad i.e. read memory,
2550 however, they can read their parameters which may have
2551 been pushed onto the stack.
2552 memset and bzero don't read memory either. */
2553 const_call
= RTL_CONST_CALL_P (insn
);
2556 rtx call
= get_call_rtx_from (insn
);
2557 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2559 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2560 if (SYMBOL_REF_DECL (symbol
)
2561 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2563 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2565 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2566 == BUILT_IN_MEMSET
))
2567 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2568 memset_call
= SYMBOL_REF_DECL (symbol
);
2572 if (const_call
|| memset_call
)
2574 insn_info_t i_ptr
= active_local_stores
;
2575 insn_info_t last
= NULL
;
2577 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2578 fprintf (dump_file
, "%s call %d\n",
2579 const_call
? "const" : "memset", INSN_UID (insn
));
2581 /* See the head comment of the frame_read field. */
2582 if (reload_completed
2583 /* Tail calls are storing their arguments using
2584 arg pointer. If it is a frame pointer on the target,
2585 even before reload we need to kill frame pointer based
2587 || (SIBLING_CALL_P (insn
)
2588 && HARD_FRAME_POINTER_IS_ARG_POINTER
))
2589 insn_info
->frame_read
= true;
2591 /* Loop over the active stores and remove those which are
2592 killed by the const function call. */
2595 bool remove_store
= false;
2597 /* The stack pointer based stores are always killed. */
2598 if (i_ptr
->stack_pointer_based
)
2599 remove_store
= true;
2601 /* If the frame is read, the frame related stores are killed. */
2602 else if (insn_info
->frame_read
)
2604 store_info_t store_info
= i_ptr
->store_rec
;
2606 /* Skip the clobbers. */
2607 while (!store_info
->is_set
)
2608 store_info
= store_info
->next
;
2610 if (store_info
->group_id
>= 0
2611 && rtx_group_vec
[store_info
->group_id
]->frame_related
)
2612 remove_store
= true;
2617 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2618 dump_insn_info ("removing from active", i_ptr
);
2620 active_local_stores_len
--;
2622 last
->next_local_store
= i_ptr
->next_local_store
;
2624 active_local_stores
= i_ptr
->next_local_store
;
2629 i_ptr
= i_ptr
->next_local_store
;
2635 if (get_call_args (insn
, memset_call
, args
, 3)
2636 && CONST_INT_P (args
[1])
2637 && CONST_INT_P (args
[2])
2638 && INTVAL (args
[2]) > 0)
2640 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2641 set_mem_size (mem
, INTVAL (args
[2]));
2642 body
= gen_rtx_SET (mem
, args
[1]);
2643 mems_found
+= record_store (body
, bb_info
);
2644 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2645 fprintf (dump_file
, "handling memset as BLKmode store\n");
2646 if (mems_found
== 1)
2648 if (active_local_stores_len
++
2649 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2651 active_local_stores_len
= 1;
2652 active_local_stores
= NULL
;
2654 insn_info
->fixed_regs_live
2655 = copy_fixed_regs (bb_info
->regs_live
);
2656 insn_info
->next_local_store
= active_local_stores
;
2657 active_local_stores
= insn_info
;
2662 else if (SIBLING_CALL_P (insn
) && reload_completed
)
2663 /* Arguments for a sibling call that are pushed to memory are passed
2664 using the incoming argument pointer of the current function. After
2665 reload that might be (and likely is) frame pointer based. */
2666 add_wild_read (bb_info
);
2668 /* Every other call, including pure functions, may read any memory
2669 that is not relative to the frame. */
2670 add_non_frame_wild_read (bb_info
);
2675 /* Assuming that there are sets in these insns, we cannot delete
2677 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2678 || volatile_refs_p (PATTERN (insn
))
2679 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2680 || (RTX_FRAME_RELATED_P (insn
))
2681 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2682 insn_info
->cannot_delete
= true;
2684 body
= PATTERN (insn
);
2685 if (GET_CODE (body
) == PARALLEL
)
2688 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2689 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2692 mems_found
+= record_store (body
, bb_info
);
2694 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2695 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2696 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2698 /* If we found some sets of mems, add it into the active_local_stores so
2699 that it can be locally deleted if found dead or used for
2700 replace_read and redundant constant store elimination. Otherwise mark
2701 it as cannot delete. This simplifies the processing later. */
2702 if (mems_found
== 1)
2704 if (active_local_stores_len
++
2705 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2707 active_local_stores_len
= 1;
2708 active_local_stores
= NULL
;
2710 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2711 insn_info
->next_local_store
= active_local_stores
;
2712 active_local_stores
= insn_info
;
2715 insn_info
->cannot_delete
= true;
2719 /* Remove BASE from the set of active_local_stores. This is a
2720 callback from cselib that is used to get rid of the stores in
2721 active_local_stores. */
2724 remove_useless_values (cselib_val
*base
)
2726 insn_info_t insn_info
= active_local_stores
;
2727 insn_info_t last
= NULL
;
2731 store_info_t store_info
= insn_info
->store_rec
;
2734 /* If ANY of the store_infos match the cselib group that is
2735 being deleted, then the insn can not be deleted. */
2738 if ((store_info
->group_id
== -1)
2739 && (store_info
->cse_base
== base
))
2744 store_info
= store_info
->next
;
2749 active_local_stores_len
--;
2751 last
->next_local_store
= insn_info
->next_local_store
;
2753 active_local_stores
= insn_info
->next_local_store
;
2754 free_store_info (insn_info
);
2759 insn_info
= insn_info
->next_local_store
;
2764 /* Do all of step 1. */
2770 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2773 all_blocks
= BITMAP_ALLOC (NULL
);
2774 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2775 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2777 FOR_ALL_BB_FN (bb
, cfun
)
2780 bb_info_t bb_info
= new dse_bb_info_type
;
2782 memset (bb_info
, 0, sizeof (dse_bb_info_type
));
2783 bitmap_set_bit (all_blocks
, bb
->index
);
2784 bb_info
->regs_live
= regs_live
;
2786 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2787 df_simulate_initialize_forwards (bb
, regs_live
);
2789 bb_table
[bb
->index
] = bb_info
;
2790 cselib_discard_hook
= remove_useless_values
;
2792 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2796 active_local_stores
= NULL
;
2797 active_local_stores_len
= 0;
2798 cselib_clear_table ();
2800 /* Scan the insns. */
2801 FOR_BB_INSNS (bb
, insn
)
2804 scan_insn (bb_info
, insn
);
2805 cselib_process_insn (insn
);
2807 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2810 /* This is something of a hack, because the global algorithm
2811 is supposed to take care of the case where stores go dead
2812 at the end of the function. However, the global
2813 algorithm must take a more conservative view of block
2814 mode reads than the local alg does. So to get the case
2815 where you have a store to the frame followed by a non
2816 overlapping block more read, we look at the active local
2817 stores at the end of the function and delete all of the
2818 frame and spill based ones. */
2819 if (stores_off_frame_dead_at_return
2820 && (EDGE_COUNT (bb
->succs
) == 0
2821 || (single_succ_p (bb
)
2822 && single_succ (bb
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
2823 && ! crtl
->calls_eh_return
)))
2825 insn_info_t i_ptr
= active_local_stores
;
2828 store_info_t store_info
= i_ptr
->store_rec
;
2830 /* Skip the clobbers. */
2831 while (!store_info
->is_set
)
2832 store_info
= store_info
->next
;
2833 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2834 delete_dead_store_insn (i_ptr
);
2836 if (store_info
->group_id
>= 0)
2839 = rtx_group_vec
[store_info
->group_id
];
2840 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2841 delete_dead_store_insn (i_ptr
);
2844 i_ptr
= i_ptr
->next_local_store
;
2848 /* Get rid of the loads that were discovered in
2849 replace_read. Cselib is finished with this block. */
2850 while (deferred_change_list
)
2852 deferred_change_t next
= deferred_change_list
->next
;
2854 /* There is no reason to validate this change. That was
2856 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2857 delete deferred_change_list
;
2858 deferred_change_list
= next
;
2861 /* Get rid of all of the cselib based store_infos in this
2862 block and mark the containing insns as not being
2864 ptr
= bb_info
->last_insn
;
2867 if (ptr
->contains_cselib_groups
)
2869 store_info_t s_info
= ptr
->store_rec
;
2870 while (s_info
&& !s_info
->is_set
)
2871 s_info
= s_info
->next
;
2873 && s_info
->redundant_reason
2874 && s_info
->redundant_reason
->insn
2875 && !ptr
->cannot_delete
)
2877 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2878 fprintf (dump_file
, "Locally deleting insn %d "
2879 "because insn %d stores the "
2880 "same value and couldn't be "
2882 INSN_UID (ptr
->insn
),
2883 INSN_UID (s_info
->redundant_reason
->insn
));
2884 delete_dead_store_insn (ptr
);
2886 free_store_info (ptr
);
2890 store_info_t s_info
;
2892 /* Free at least positions_needed bitmaps. */
2893 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2894 if (s_info
->is_large
)
2896 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2897 s_info
->is_large
= false;
2900 ptr
= ptr
->prev_insn
;
2903 cse_store_info_pool
.release ();
2905 bb_info
->regs_live
= NULL
;
2908 BITMAP_FREE (regs_live
);
2910 rtx_group_table
->empty ();
2914 /*----------------------------------------------------------------------------
2917 Assign each byte position in the stores that we are going to
2918 analyze globally to a position in the bitmaps. Returns true if
2919 there are any bit positions assigned.
2920 ----------------------------------------------------------------------------*/
2923 dse_step2_init (void)
2928 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2930 /* For all non stack related bases, we only consider a store to
2931 be deletable if there are two or more stores for that
2932 position. This is because it takes one store to make the
2933 other store redundant. However, for the stores that are
2934 stack related, we consider them if there is only one store
2935 for the position. We do this because the stack related
2936 stores can be deleted if their is no read between them and
2937 the end of the function.
2939 To make this work in the current framework, we take the stack
2940 related bases add all of the bits from store1 into store2.
2941 This has the effect of making the eligible even if there is
2944 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2946 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2947 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2948 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2949 fprintf (dump_file
, "group %d is frame related ", i
);
2952 group
->offset_map_size_n
++;
2953 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2954 group
->offset_map_size_n
);
2955 group
->offset_map_size_p
++;
2956 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2957 group
->offset_map_size_p
);
2958 group
->process_globally
= false;
2959 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2961 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2962 (int)bitmap_count_bits (group
->store2_n
),
2963 (int)bitmap_count_bits (group
->store2_p
));
2964 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2965 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2971 /* Init the offset tables for the normal case. */
2974 dse_step2_nospill (void)
2978 /* Position 0 is unused because 0 is used in the maps to mean
2980 current_position
= 1;
2981 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2986 if (group
== clear_alias_group
)
2989 memset (group
->offset_map_n
, 0, sizeof (int) * group
->offset_map_size_n
);
2990 memset (group
->offset_map_p
, 0, sizeof (int) * group
->offset_map_size_p
);
2991 bitmap_clear (group
->group_kill
);
2993 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2995 bitmap_set_bit (group
->group_kill
, current_position
);
2996 if (bitmap_bit_p (group
->escaped_n
, j
))
2997 bitmap_set_bit (kill_on_calls
, current_position
);
2998 group
->offset_map_n
[j
] = current_position
++;
2999 group
->process_globally
= true;
3001 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
3003 bitmap_set_bit (group
->group_kill
, current_position
);
3004 if (bitmap_bit_p (group
->escaped_p
, j
))
3005 bitmap_set_bit (kill_on_calls
, current_position
);
3006 group
->offset_map_p
[j
] = current_position
++;
3007 group
->process_globally
= true;
3010 return current_position
!= 1;
3015 /*----------------------------------------------------------------------------
3018 Build the bit vectors for the transfer functions.
3019 ----------------------------------------------------------------------------*/
3022 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
3026 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
3030 HOST_WIDE_INT offset_p
= -offset
;
3031 if (offset_p
>= group_info
->offset_map_size_n
)
3033 return group_info
->offset_map_n
[offset_p
];
3037 if (offset
>= group_info
->offset_map_size_p
)
3039 return group_info
->offset_map_p
[offset
];
3044 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3048 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3053 group_info_t group_info
3054 = rtx_group_vec
[store_info
->group_id
];
3055 if (group_info
->process_globally
)
3056 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3058 int index
= get_bitmap_index (group_info
, i
);
3061 bitmap_set_bit (gen
, index
);
3063 bitmap_clear_bit (kill
, index
);
3066 store_info
= store_info
->next
;
3071 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3075 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3079 if (store_info
->alias_set
)
3081 int index
= get_bitmap_index (clear_alias_group
,
3082 store_info
->alias_set
);
3085 bitmap_set_bit (gen
, index
);
3087 bitmap_clear_bit (kill
, index
);
3090 store_info
= store_info
->next
;
3095 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3099 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3101 read_info_t read_info
= insn_info
->read_rec
;
3105 /* If this insn reads the frame, kill all the frame related stores. */
3106 if (insn_info
->frame_read
)
3108 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3109 if (group
->process_globally
&& group
->frame_related
)
3112 bitmap_ior_into (kill
, group
->group_kill
);
3113 bitmap_and_compl_into (gen
, group
->group_kill
);
3116 if (insn_info
->non_frame_wild_read
)
3118 /* Kill all non-frame related stores. Kill all stores of variables that
3121 bitmap_ior_into (kill
, kill_on_calls
);
3122 bitmap_and_compl_into (gen
, kill_on_calls
);
3123 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3124 if (group
->process_globally
&& !group
->frame_related
)
3127 bitmap_ior_into (kill
, group
->group_kill
);
3128 bitmap_and_compl_into (gen
, group
->group_kill
);
3133 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3135 if (group
->process_globally
)
3137 if (i
== read_info
->group_id
)
3139 if (read_info
->begin
> read_info
->end
)
3141 /* Begin > end for block mode reads. */
3143 bitmap_ior_into (kill
, group
->group_kill
);
3144 bitmap_and_compl_into (gen
, group
->group_kill
);
3148 /* The groups are the same, just process the
3151 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3153 int index
= get_bitmap_index (group
, j
);
3157 bitmap_set_bit (kill
, index
);
3158 bitmap_clear_bit (gen
, index
);
3165 /* The groups are different, if the alias sets
3166 conflict, clear the entire group. We only need
3167 to apply this test if the read_info is a cselib
3168 read. Anything with a constant base cannot alias
3169 something else with a different constant
3171 if ((read_info
->group_id
< 0)
3172 && canon_true_dependence (group
->base_mem
,
3173 GET_MODE (group
->base_mem
),
3174 group
->canon_base_addr
,
3175 read_info
->mem
, NULL_RTX
))
3178 bitmap_ior_into (kill
, group
->group_kill
);
3179 bitmap_and_compl_into (gen
, group
->group_kill
);
3185 read_info
= read_info
->next
;
3189 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3193 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3197 if (read_info
->alias_set
)
3199 int index
= get_bitmap_index (clear_alias_group
,
3200 read_info
->alias_set
);
3204 bitmap_set_bit (kill
, index
);
3205 bitmap_clear_bit (gen
, index
);
3209 read_info
= read_info
->next
;
3214 /* Return the insn in BB_INFO before the first wild read or if there
3215 are no wild reads in the block, return the last insn. */
3218 find_insn_before_first_wild_read (bb_info_t bb_info
)
3220 insn_info_t insn_info
= bb_info
->last_insn
;
3221 insn_info_t last_wild_read
= NULL
;
3225 if (insn_info
->wild_read
)
3227 last_wild_read
= insn_info
->prev_insn
;
3228 /* Block starts with wild read. */
3229 if (!last_wild_read
)
3233 insn_info
= insn_info
->prev_insn
;
3237 return last_wild_read
;
3239 return bb_info
->last_insn
;
3243 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3244 the block in order to build the gen and kill sets for the block.
3245 We start at ptr which may be the last insn in the block or may be
3246 the first insn with a wild read. In the latter case we are able to
3247 skip the rest of the block because it just does not matter:
3248 anything that happens is hidden by the wild read. */
3251 dse_step3_scan (bool for_spills
, basic_block bb
)
3253 bb_info_t bb_info
= bb_table
[bb
->index
];
3254 insn_info_t insn_info
;
3257 /* There are no wild reads in the spill case. */
3258 insn_info
= bb_info
->last_insn
;
3260 insn_info
= find_insn_before_first_wild_read (bb_info
);
3262 /* In the spill case or in the no_spill case if there is no wild
3263 read in the block, we will need a kill set. */
3264 if (insn_info
== bb_info
->last_insn
)
3267 bitmap_clear (bb_info
->kill
);
3269 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3273 BITMAP_FREE (bb_info
->kill
);
3277 /* There may have been code deleted by the dce pass run before
3279 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3281 /* Process the read(s) last. */
3284 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3285 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3289 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3290 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3294 insn_info
= insn_info
->prev_insn
;
3299 /* Set the gen set of the exit block, and also any block with no
3300 successors that does not have a wild read. */
3303 dse_step3_exit_block_scan (bb_info_t bb_info
)
3305 /* The gen set is all 0's for the exit block except for the
3306 frame_pointer_group. */
3308 if (stores_off_frame_dead_at_return
)
3313 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3315 if (group
->process_globally
&& group
->frame_related
)
3316 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3322 /* Find all of the blocks that are not backwards reachable from the
3323 exit block or any block with no successors (BB). These are the
3324 infinite loops or infinite self loops. These blocks will still
3325 have their bits set in UNREACHABLE_BLOCKS. */
3328 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3333 if (bitmap_bit_p (unreachable_blocks
, bb
->index
))
3335 bitmap_clear_bit (unreachable_blocks
, bb
->index
);
3336 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3338 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3343 /* Build the transfer functions for the function. */
3346 dse_step3 (bool for_spills
)
3349 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
3350 sbitmap_iterator sbi
;
3351 bitmap all_ones
= NULL
;
3354 bitmap_ones (unreachable_blocks
);
3356 FOR_ALL_BB_FN (bb
, cfun
)
3358 bb_info_t bb_info
= bb_table
[bb
->index
];
3360 bitmap_clear (bb_info
->gen
);
3362 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3364 if (bb
->index
== ENTRY_BLOCK
)
3366 else if (bb
->index
== EXIT_BLOCK
)
3367 dse_step3_exit_block_scan (bb_info
);
3369 dse_step3_scan (for_spills
, bb
);
3370 if (EDGE_COUNT (bb
->succs
) == 0)
3371 mark_reachable_blocks (unreachable_blocks
, bb
);
3373 /* If this is the second time dataflow is run, delete the old
3376 BITMAP_FREE (bb_info
->in
);
3378 BITMAP_FREE (bb_info
->out
);
3381 /* For any block in an infinite loop, we must initialize the out set
3382 to all ones. This could be expensive, but almost never occurs in
3383 practice. However, it is common in regression tests. */
3384 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks
, 0, i
, sbi
)
3386 if (bitmap_bit_p (all_blocks
, i
))
3388 bb_info_t bb_info
= bb_table
[i
];
3394 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3395 FOR_EACH_VEC_ELT (rtx_group_vec
, j
, group
)
3396 bitmap_ior_into (all_ones
, group
->group_kill
);
3400 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3401 bitmap_copy (bb_info
->out
, all_ones
);
3407 BITMAP_FREE (all_ones
);
3408 sbitmap_free (unreachable_blocks
);
3413 /*----------------------------------------------------------------------------
3416 Solve the bitvector equations.
3417 ----------------------------------------------------------------------------*/
3420 /* Confluence function for blocks with no successors. Create an out
3421 set from the gen set of the exit block. This block logically has
3422 the exit block as a successor. */
3427 dse_confluence_0 (basic_block bb
)
3429 bb_info_t bb_info
= bb_table
[bb
->index
];
3431 if (bb
->index
== EXIT_BLOCK
)
3436 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3437 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3441 /* Propagate the information from the in set of the dest of E to the
3442 out set of the src of E. If the various in or out sets are not
3443 there, that means they are all ones. */
3446 dse_confluence_n (edge e
)
3448 bb_info_t src_info
= bb_table
[e
->src
->index
];
3449 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3454 bitmap_and_into (src_info
->out
, dest_info
->in
);
3457 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3458 bitmap_copy (src_info
->out
, dest_info
->in
);
3465 /* Propagate the info from the out to the in set of BB_INDEX's basic
3466 block. There are three cases:
3468 1) The block has no kill set. In this case the kill set is all
3469 ones. It does not matter what the out set of the block is, none of
3470 the info can reach the top. The only thing that reaches the top is
3471 the gen set and we just copy the set.
3473 2) There is a kill set but no out set and bb has successors. In
3474 this case we just return. Eventually an out set will be created and
3475 it is better to wait than to create a set of ones.
3477 3) There is both a kill and out set. We apply the obvious transfer
3482 dse_transfer_function (int bb_index
)
3484 bb_info_t bb_info
= bb_table
[bb_index
];
3492 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3493 bb_info
->out
, bb_info
->kill
);
3496 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3497 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3498 bb_info
->out
, bb_info
->kill
);
3508 /* Case 1 above. If there is already an in set, nothing
3514 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3515 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3521 /* Solve the dataflow equations. */
3526 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3527 dse_confluence_n
, dse_transfer_function
,
3528 all_blocks
, df_get_postorder (DF_BACKWARD
),
3529 df_get_n_blocks (DF_BACKWARD
));
3530 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3534 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3535 FOR_ALL_BB_FN (bb
, cfun
)
3537 bb_info_t bb_info
= bb_table
[bb
->index
];
3539 df_print_bb_index (bb
, dump_file
);
3541 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3543 fprintf (dump_file
, " in: *MISSING*\n");
3545 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3547 fprintf (dump_file
, " gen: *MISSING*\n");
3549 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3551 fprintf (dump_file
, " kill: *MISSING*\n");
3553 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3555 fprintf (dump_file
, " out: *MISSING*\n\n");
3562 /*----------------------------------------------------------------------------
3565 Delete the stores that can only be deleted using the global information.
3566 ----------------------------------------------------------------------------*/
3570 dse_step5_nospill (void)
3573 FOR_EACH_BB_FN (bb
, cfun
)
3575 bb_info_t bb_info
= bb_table
[bb
->index
];
3576 insn_info_t insn_info
= bb_info
->last_insn
;
3577 bitmap v
= bb_info
->out
;
3581 bool deleted
= false;
3582 if (dump_file
&& insn_info
->insn
)
3584 fprintf (dump_file
, "starting to process insn %d\n",
3585 INSN_UID (insn_info
->insn
));
3586 bitmap_print (dump_file
, v
, " v: ", "\n");
3589 /* There may have been code deleted by the dce pass run before
3592 && INSN_P (insn_info
->insn
)
3593 && (!insn_info
->cannot_delete
)
3594 && (!bitmap_empty_p (v
)))
3596 store_info_t store_info
= insn_info
->store_rec
;
3598 /* Try to delete the current insn. */
3601 /* Skip the clobbers. */
3602 while (!store_info
->is_set
)
3603 store_info
= store_info
->next
;
3605 if (store_info
->alias_set
)
3610 group_info_t group_info
3611 = rtx_group_vec
[store_info
->group_id
];
3613 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3615 int index
= get_bitmap_index (group_info
, i
);
3617 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3618 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3619 if (index
== 0 || !bitmap_bit_p (v
, index
))
3621 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3622 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3631 && check_for_inc_dec_1 (insn_info
))
3633 delete_insn (insn_info
->insn
);
3634 insn_info
->insn
= NULL
;
3639 /* We do want to process the local info if the insn was
3640 deleted. For instance, if the insn did a wild read, we
3641 no longer need to trash the info. */
3643 && INSN_P (insn_info
->insn
)
3646 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3647 if (insn_info
->wild_read
)
3649 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3650 fprintf (dump_file
, "wild read\n");
3653 else if (insn_info
->read_rec
3654 || insn_info
->non_frame_wild_read
)
3656 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3657 fprintf (dump_file
, "regular read\n");
3658 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3659 fprintf (dump_file
, "non-frame wild read\n");
3660 scan_reads_nospill (insn_info
, v
, NULL
);
3664 insn_info
= insn_info
->prev_insn
;
3671 /*----------------------------------------------------------------------------
3674 Delete stores made redundant by earlier stores (which store the same
3675 value) that couldn't be eliminated.
3676 ----------------------------------------------------------------------------*/
3683 FOR_ALL_BB_FN (bb
, cfun
)
3685 bb_info_t bb_info
= bb_table
[bb
->index
];
3686 insn_info_t insn_info
= bb_info
->last_insn
;
3690 /* There may have been code deleted by the dce pass run before
3693 && INSN_P (insn_info
->insn
)
3694 && !insn_info
->cannot_delete
)
3696 store_info_t s_info
= insn_info
->store_rec
;
3698 while (s_info
&& !s_info
->is_set
)
3699 s_info
= s_info
->next
;
3701 && s_info
->redundant_reason
3702 && s_info
->redundant_reason
->insn
3703 && INSN_P (s_info
->redundant_reason
->insn
))
3705 rtx_insn
*rinsn
= s_info
->redundant_reason
->insn
;
3706 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3707 fprintf (dump_file
, "Locally deleting insn %d "
3708 "because insn %d stores the "
3709 "same value and couldn't be "
3711 INSN_UID (insn_info
->insn
),
3713 delete_dead_store_insn (insn_info
);
3716 insn_info
= insn_info
->prev_insn
;
3721 /*----------------------------------------------------------------------------
3724 Destroy everything left standing.
3725 ----------------------------------------------------------------------------*/
3730 bitmap_obstack_release (&dse_bitmap_obstack
);
3731 obstack_free (&dse_obstack
, NULL
);
3733 end_alias_analysis ();
3735 delete rtx_group_table
;
3736 rtx_group_table
= NULL
;
3737 rtx_group_vec
.release ();
3738 BITMAP_FREE (all_blocks
);
3739 BITMAP_FREE (scratch
);
3741 rtx_store_info_pool
.release ();
3742 read_info_type::pool
.release ();
3743 insn_info_type::pool
.release ();
3744 dse_bb_info_type::pool
.release ();
3745 group_info::pool
.release ();
3746 deferred_change::pool
.release ();
3750 /* -------------------------------------------------------------------------
3752 ------------------------------------------------------------------------- */
3754 /* Callback for running pass_rtl_dse. */
3757 rest_of_handle_dse (void)
3759 df_set_flags (DF_DEFER_INSN_RESCAN
);
3761 /* Need the notes since we must track live hardregs in the forwards
3763 df_note_add_problem ();
3769 if (dse_step2_nospill ())
3771 df_set_flags (DF_LR_RUN_DCE
);
3773 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3774 fprintf (dump_file
, "doing global processing\n");
3777 dse_step5_nospill ();
3784 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3785 locally_deleted
, globally_deleted
, spill_deleted
);
3787 /* DSE can eliminate potentially-trapping MEMs.
3788 Remove any EH edges associated with them. */
3789 if ((locally_deleted
|| globally_deleted
)
3790 && cfun
->can_throw_non_call_exceptions
3791 && purge_all_dead_edges ())
3799 const pass_data pass_data_rtl_dse1
=
3801 RTL_PASS
, /* type */
3803 OPTGROUP_NONE
, /* optinfo_flags */
3804 TV_DSE1
, /* tv_id */
3805 0, /* properties_required */
3806 0, /* properties_provided */
3807 0, /* properties_destroyed */
3808 0, /* todo_flags_start */
3809 TODO_df_finish
, /* todo_flags_finish */
3812 class pass_rtl_dse1
: public rtl_opt_pass
3815 pass_rtl_dse1 (gcc::context
*ctxt
)
3816 : rtl_opt_pass (pass_data_rtl_dse1
, ctxt
)
3819 /* opt_pass methods: */
3820 virtual bool gate (function
*)
3822 return optimize
> 0 && flag_dse
&& dbg_cnt (dse1
);
3825 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3827 }; // class pass_rtl_dse1
3832 make_pass_rtl_dse1 (gcc::context
*ctxt
)
3834 return new pass_rtl_dse1 (ctxt
);
3839 const pass_data pass_data_rtl_dse2
=
3841 RTL_PASS
, /* type */
3843 OPTGROUP_NONE
, /* optinfo_flags */
3844 TV_DSE2
, /* tv_id */
3845 0, /* properties_required */
3846 0, /* properties_provided */
3847 0, /* properties_destroyed */
3848 0, /* todo_flags_start */
3849 TODO_df_finish
, /* todo_flags_finish */
3852 class pass_rtl_dse2
: public rtl_opt_pass
3855 pass_rtl_dse2 (gcc::context
*ctxt
)
3856 : rtl_opt_pass (pass_data_rtl_dse2
, ctxt
)
3859 /* opt_pass methods: */
3860 virtual bool gate (function
*)
3862 return optimize
> 0 && flag_dse
&& dbg_cnt (dse2
);
3865 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3867 }; // class pass_rtl_dse2
3872 make_pass_rtl_dse2 (gcc::context
*ctxt
)
3874 return new pass_rtl_dse2 (ctxt
);