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"
28 #include "hash-table.h"
34 #include "double-int.h"
42 #include "fold-const.h"
43 #include "stor-layout.h"
46 #include "hard-reg-set.h"
49 #include "dominance.h"
53 #include "basic-block.h"
56 #include "tree-pass.h"
57 #include "alloc-pool.h"
58 #include "insn-config.h"
61 #include "statistics.h"
62 #include "fixed-value.h"
72 #include "insn-codes.h"
77 #include "tree-ssa-alias.h"
78 #include "internal-fn.h"
79 #include "gimple-expr.h"
82 #include "gimple-ssa.h"
84 #include "cfgcleanup.h"
86 /* This file contains three techniques for performing Dead Store
89 * The first technique performs dse locally on any base address. It
90 is based on the cselib which is a local value numbering technique.
91 This technique is local to a basic block but deals with a fairly
94 * The second technique performs dse globally but is restricted to
95 base addresses that are either constant or are relative to the
98 * The third technique, (which is only done after register allocation)
99 processes the spill spill slots. This differs from the second
100 technique because it takes advantage of the fact that spilling is
101 completely free from the effects of aliasing.
103 Logically, dse is a backwards dataflow problem. A store can be
104 deleted if it if cannot be reached in the backward direction by any
105 use of the value being stored. However, the local technique uses a
106 forwards scan of the basic block because cselib requires that the
107 block be processed in that order.
109 The pass is logically broken into 7 steps:
113 1) The local algorithm, as well as scanning the insns for the two
116 2) Analysis to see if the global algs are necessary. In the case
117 of stores base on a constant address, there must be at least two
118 stores to that address, to make it possible to delete some of the
119 stores. In the case of stores off of the frame or spill related
120 stores, only one store to an address is necessary because those
121 stores die at the end of the function.
123 3) Set up the global dataflow equations based on processing the
124 info parsed in the first step.
126 4) Solve the dataflow equations.
128 5) Delete the insns that the global analysis has indicated are
131 6) Delete insns that store the same value as preceding store
132 where the earlier store couldn't be eliminated.
136 This step uses cselib and canon_rtx to build the largest expression
137 possible for each address. This pass is a forwards pass through
138 each basic block. From the point of view of the global technique,
139 the first pass could examine a block in either direction. The
140 forwards ordering is to accommodate cselib.
142 We make a simplifying assumption: addresses fall into four broad
145 1) base has rtx_varies_p == false, offset is constant.
146 2) base has rtx_varies_p == false, offset variable.
147 3) base has rtx_varies_p == true, offset constant.
148 4) base has rtx_varies_p == true, offset variable.
150 The local passes are able to process all 4 kinds of addresses. The
151 global pass only handles 1).
153 The global problem is formulated as follows:
155 A store, S1, to address A, where A is not relative to the stack
156 frame, can be eliminated if all paths from S1 to the end of the
157 function contain another store to A before a read to A.
159 If the address A is relative to the stack frame, a store S2 to A
160 can be eliminated if there are no paths from S2 that reach the
161 end of the function that read A before another store to A. In
162 this case S2 can be deleted if there are paths from S2 to the
163 end of the function that have no reads or writes to A. This
164 second case allows stores to the stack frame to be deleted that
165 would otherwise die when the function returns. This cannot be
166 done if stores_off_frame_dead_at_return is not true. See the doc
167 for that variable for when this variable is false.
169 The global problem is formulated as a backwards set union
170 dataflow problem where the stores are the gens and reads are the
171 kills. Set union problems are rare and require some special
172 handling given our representation of bitmaps. A straightforward
173 implementation requires a lot of bitmaps filled with 1s.
174 These are expensive and cumbersome in our bitmap formulation so
175 care has been taken to avoid large vectors filled with 1s. See
176 the comments in bb_info and in the dataflow confluence functions
179 There are two places for further enhancements to this algorithm:
181 1) The original dse which was embedded in a pass called flow also
182 did local address forwarding. For example in
187 flow would replace the right hand side of the second insn with a
188 reference to r100. Most of the information is available to add this
189 to this pass. It has not done it because it is a lot of work in
190 the case that either r100 is assigned to between the first and
191 second insn and/or the second insn is a load of part of the value
192 stored by the first insn.
194 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
195 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
196 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
197 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
199 2) The cleaning up of spill code is quite profitable. It currently
200 depends on reading tea leaves and chicken entrails left by reload.
201 This pass depends on reload creating a singleton alias set for each
202 spill slot and telling the next dse pass which of these alias sets
203 are the singletons. Rather than analyze the addresses of the
204 spills, dse's spill processing just does analysis of the loads and
205 stores that use those alias sets. There are three cases where this
208 a) Reload sometimes creates the slot for one mode of access, and
209 then inserts loads and/or stores for a smaller mode. In this
210 case, the current code just punts on the slot. The proper thing
211 to do is to back out and use one bit vector position for each
212 byte of the entity associated with the slot. This depends on
213 KNOWING that reload always generates the accesses for each of the
214 bytes in some canonical (read that easy to understand several
215 passes after reload happens) way.
217 b) Reload sometimes decides that spill slot it allocated was not
218 large enough for the mode and goes back and allocates more slots
219 with the same mode and alias set. The backout in this case is a
220 little more graceful than (a). In this case the slot is unmarked
221 as being a spill slot and if final address comes out to be based
222 off the frame pointer, the global algorithm handles this slot.
224 c) For any pass that may prespill, there is currently no
225 mechanism to tell the dse pass that the slot being used has the
226 special properties that reload uses. It may be that all that is
227 required is to have those passes make the same calls that reload
228 does, assuming that the alias sets can be manipulated in the same
231 /* There are limits to the size of constant offsets we model for the
232 global problem. There are certainly test cases, that exceed this
233 limit, however, it is unlikely that there are important programs
234 that really have constant offsets this size. */
235 #define MAX_OFFSET (64 * 1024)
237 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
238 on the default obstack because these bitmaps can grow quite large
239 (~2GB for the small (!) test case of PR54146) and we'll hold on to
240 all that memory until the end of the compiler run.
241 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
242 releasing the whole obstack. */
243 static bitmap_obstack dse_bitmap_obstack
;
245 /* Obstack for other data. As for above: Kinda nice to be able to
246 throw it all away at the end in one big sweep. */
247 static struct obstack dse_obstack
;
249 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
250 static bitmap scratch
= NULL
;
254 /* This structure holds information about a candidate store. */
258 /* False means this is a clobber. */
261 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
264 /* The id of the mem group of the base address. If rtx_varies_p is
265 true, this is -1. Otherwise, it is the index into the group
269 /* This is the cselib value. */
270 cselib_val
*cse_base
;
272 /* This canonized mem. */
275 /* Canonized MEM address for use by canon_true_dependence. */
278 /* If this is non-zero, it is the alias set of a spill location. */
279 alias_set_type alias_set
;
281 /* The offset of the first and byte before the last byte associated
282 with the operation. */
283 HOST_WIDE_INT begin
, end
;
287 /* A bitmask as wide as the number of bytes in the word that
288 contains a 1 if the byte may be needed. The store is unused if
289 all of the bits are 0. This is used if IS_LARGE is false. */
290 unsigned HOST_WIDE_INT small_bitmask
;
294 /* A bitmap with one bit per byte. Cleared bit means the position
295 is needed. Used if IS_LARGE is false. */
298 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
299 equal to END - BEGIN, the whole store is unused. */
304 /* The next store info for this insn. */
305 struct store_info
*next
;
307 /* The right hand side of the store. This is used if there is a
308 subsequent reload of the mems address somewhere later in the
312 /* If rhs is or holds a constant, this contains that constant,
316 /* Set if this store stores the same constant value as REDUNDANT_REASON
317 insn stored. These aren't eliminated early, because doing that
318 might prevent the earlier larger store to be eliminated. */
319 struct insn_info
*redundant_reason
;
322 /* Return a bitmask with the first N low bits set. */
324 static unsigned HOST_WIDE_INT
325 lowpart_bitmask (int n
)
327 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
328 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
331 typedef struct store_info
*store_info_t
;
332 static alloc_pool cse_store_info_pool
;
333 static alloc_pool rtx_store_info_pool
;
335 /* This structure holds information about a load. These are only
336 built for rtx bases. */
339 /* The id of the mem group of the base address. */
342 /* If this is non-zero, it is the alias set of a spill location. */
343 alias_set_type alias_set
;
345 /* The offset of the first and byte after the last byte associated
346 with the operation. If begin == end == 0, the read did not have
347 a constant offset. */
350 /* The mem being read. */
353 /* The next read_info for this insn. */
354 struct read_info
*next
;
356 typedef struct read_info
*read_info_t
;
357 static alloc_pool read_info_pool
;
360 /* One of these records is created for each insn. */
364 /* Set true if the insn contains a store but the insn itself cannot
365 be deleted. This is set if the insn is a parallel and there is
366 more than one non dead output or if the insn is in some way
370 /* This field is only used by the global algorithm. It is set true
371 if the insn contains any read of mem except for a (1). This is
372 also set if the insn is a call or has a clobber mem. If the insn
373 contains a wild read, the use_rec will be null. */
376 /* This is true only for CALL instructions which could potentially read
377 any non-frame memory location. This field is used by the global
379 bool non_frame_wild_read
;
381 /* This field is only used for the processing of const functions.
382 These functions cannot read memory, but they can read the stack
383 because that is where they may get their parms. We need to be
384 this conservative because, like the store motion pass, we don't
385 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
386 Moreover, we need to distinguish two cases:
387 1. Before reload (register elimination), the stores related to
388 outgoing arguments are stack pointer based and thus deemed
389 of non-constant base in this pass. This requires special
390 handling but also means that the frame pointer based stores
391 need not be killed upon encountering a const function call.
392 2. After reload, the stores related to outgoing arguments can be
393 either stack pointer or hard frame pointer based. This means
394 that we have no other choice than also killing all the frame
395 pointer based stores upon encountering a const function call.
396 This field is set after reload for const function calls and before
397 reload for const tail function calls on targets where arg pointer
398 is the frame pointer. Having this set is less severe than a wild
399 read, it just means that all the frame related stores are killed
400 rather than all the stores. */
403 /* This field is only used for the processing of const functions.
404 It is set if the insn may contain a stack pointer based store. */
405 bool stack_pointer_based
;
407 /* This is true if any of the sets within the store contains a
408 cselib base. Such stores can only be deleted by the local
410 bool contains_cselib_groups
;
415 /* The list of mem sets or mem clobbers that are contained in this
416 insn. If the insn is deletable, it contains only one mem set.
417 But it could also contain clobbers. Insns that contain more than
418 one mem set are not deletable, but each of those mems are here in
419 order to provide info to delete other insns. */
420 store_info_t store_rec
;
422 /* The linked list of mem uses in this insn. Only the reads from
423 rtx bases are listed here. The reads to cselib bases are
424 completely processed during the first scan and so are never
426 read_info_t read_rec
;
428 /* The live fixed registers. We assume only fixed registers can
429 cause trouble by being clobbered from an expanded pattern;
430 storing only the live fixed registers (rather than all registers)
431 means less memory needs to be allocated / copied for the individual
433 regset fixed_regs_live
;
435 /* The prev insn in the basic block. */
436 struct insn_info
* prev_insn
;
438 /* The linked list of insns that are in consideration for removal in
439 the forwards pass through the basic block. This pointer may be
440 trash as it is not cleared when a wild read occurs. The only
441 time it is guaranteed to be correct is when the traversal starts
442 at active_local_stores. */
443 struct insn_info
* next_local_store
;
446 typedef struct insn_info
*insn_info_t
;
447 static alloc_pool insn_info_pool
;
449 /* The linked list of stores that are under consideration in this
451 static insn_info_t active_local_stores
;
452 static int active_local_stores_len
;
457 /* Pointer to the insn info for the last insn in the block. These
458 are linked so this is how all of the insns are reached. During
459 scanning this is the current insn being scanned. */
460 insn_info_t last_insn
;
462 /* The info for the global dataflow problem. */
465 /* This is set if the transfer function should and in the wild_read
466 bitmap before applying the kill and gen sets. That vector knocks
467 out most of the bits in the bitmap and thus speeds up the
469 bool apply_wild_read
;
471 /* The following 4 bitvectors hold information about which positions
472 of which stores are live or dead. They are indexed by
475 /* The set of store positions that exist in this block before a wild read. */
478 /* The set of load positions that exist in this block above the
479 same position of a store. */
482 /* The set of stores that reach the top of the block without being
485 Do not represent the in if it is all ones. Note that this is
486 what the bitvector should logically be initialized to for a set
487 intersection problem. However, like the kill set, this is too
488 expensive. So initially, the in set will only be created for the
489 exit block and any block that contains a wild read. */
492 /* The set of stores that reach the bottom of the block from it's
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 and in set, this is
498 too expensive. So what is done is that the confluence operator
499 just initializes the vector from one of the out sets of the
500 successors of the block. */
503 /* The following bitvector is indexed by the reg number. It
504 contains the set of regs that are live at the current instruction
505 being processed. While it contains info for all of the
506 registers, only the hard registers are actually examined. It is used
507 to assure that shift and/or add sequences that are inserted do not
508 accidentally clobber live hard regs. */
512 typedef struct dse_bb_info
*bb_info_t
;
513 static alloc_pool bb_info_pool
;
515 /* Table to hold all bb_infos. */
516 static bb_info_t
*bb_table
;
518 /* There is a group_info for each rtx base that is used to reference
519 memory. There are also not many of the rtx bases because they are
520 very limited in scope. */
524 /* The actual base of the address. */
527 /* The sequential id of the base. This allows us to have a
528 canonical ordering of these that is not based on addresses. */
531 /* True if there are any positions that are to be processed
533 bool process_globally
;
535 /* True if the base of this group is either the frame_pointer or
536 hard_frame_pointer. */
539 /* A mem wrapped around the base pointer for the group in order to do
540 read dependency. It must be given BLKmode in order to encompass all
541 the possible offsets from the base. */
544 /* Canonized version of base_mem's address. */
547 /* These two sets of two bitmaps are used to keep track of how many
548 stores are actually referencing that position from this base. We
549 only do this for rtx bases as this will be used to assign
550 positions in the bitmaps for the global problem. Bit N is set in
551 store1 on the first store for offset N. Bit N is set in store2
552 for the second store to offset N. This is all we need since we
553 only care about offsets that have two or more stores for them.
555 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
556 for 0 and greater offsets.
558 There is one special case here, for stores into the stack frame,
559 we will or store1 into store2 before deciding which stores look
560 at globally. This is because stores to the stack frame that have
561 no other reads before the end of the function can also be
563 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
565 /* These bitmaps keep track of offsets in this group escape this function.
566 An offset escapes if it corresponds to a named variable whose
567 addressable flag is set. */
568 bitmap escaped_n
, escaped_p
;
570 /* The positions in this bitmap have the same assignments as the in,
571 out, gen and kill bitmaps. This bitmap is all zeros except for
572 the positions that are occupied by stores for this group. */
575 /* The offset_map is used to map the offsets from this base into
576 positions in the global bitmaps. It is only created after all of
577 the all of stores have been scanned and we know which ones we
579 int *offset_map_n
, *offset_map_p
;
580 int offset_map_size_n
, offset_map_size_p
;
582 typedef struct group_info
*group_info_t
;
583 typedef const struct group_info
*const_group_info_t
;
584 static alloc_pool rtx_group_info_pool
;
586 /* Index into the rtx_group_vec. */
587 static int rtx_group_next_id
;
590 static vec
<group_info_t
> rtx_group_vec
;
593 /* This structure holds the set of changes that are being deferred
594 when removing read operation. See replace_read. */
595 struct deferred_change
598 /* The mem that is being replaced. */
601 /* The reg it is being replaced with. */
604 struct deferred_change
*next
;
607 typedef struct deferred_change
*deferred_change_t
;
608 static alloc_pool deferred_change_pool
;
610 static deferred_change_t deferred_change_list
= NULL
;
612 /* The group that holds all of the clear_alias_sets. */
613 static group_info_t clear_alias_group
;
615 /* The modes of the clear_alias_sets. */
616 static htab_t clear_alias_mode_table
;
618 /* Hash table element to look up the mode for an alias set. */
619 struct clear_alias_mode_holder
621 alias_set_type alias_set
;
625 /* This is true except if cfun->stdarg -- i.e. we cannot do
626 this for vararg functions because they play games with the frame. */
627 static bool stores_off_frame_dead_at_return
;
629 /* Counter for stats. */
630 static int globally_deleted
;
631 static int locally_deleted
;
632 static int spill_deleted
;
634 static bitmap all_blocks
;
636 /* Locations that are killed by calls in the global phase. */
637 static bitmap kill_on_calls
;
639 /* The number of bits used in the global bitmaps. */
640 static unsigned int current_position
;
642 /*----------------------------------------------------------------------------
646 ----------------------------------------------------------------------------*/
649 /* Find the entry associated with ALIAS_SET. */
651 static struct clear_alias_mode_holder
*
652 clear_alias_set_lookup (alias_set_type alias_set
)
654 struct clear_alias_mode_holder tmp_holder
;
657 tmp_holder
.alias_set
= alias_set
;
658 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
661 return (struct clear_alias_mode_holder
*) *slot
;
665 /* Hashtable callbacks for maintaining the "bases" field of
666 store_group_info, given that the addresses are function invariants. */
668 struct invariant_group_base_hasher
: typed_noop_remove
<group_info
>
670 typedef group_info value_type
;
671 typedef group_info compare_type
;
672 static inline hashval_t
hash (const value_type
*);
673 static inline bool equal (const value_type
*, const compare_type
*);
677 invariant_group_base_hasher::equal (const value_type
*gi1
,
678 const compare_type
*gi2
)
680 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
684 invariant_group_base_hasher::hash (const value_type
*gi
)
687 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
690 /* Tables of group_info structures, hashed by base value. */
691 static hash_table
<invariant_group_base_hasher
> *rtx_group_table
;
694 /* Get the GROUP for BASE. Add a new group if it is not there. */
697 get_group_info (rtx base
)
699 struct group_info tmp_gi
;
705 /* Find the store_base_info structure for BASE, creating a new one
707 tmp_gi
.rtx_base
= base
;
708 slot
= rtx_group_table
->find_slot (&tmp_gi
, INSERT
);
709 gi
= (group_info_t
) *slot
;
713 if (!clear_alias_group
)
715 clear_alias_group
= gi
=
716 (group_info_t
) pool_alloc (rtx_group_info_pool
);
717 memset (gi
, 0, sizeof (struct group_info
));
718 gi
->id
= rtx_group_next_id
++;
719 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
720 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
721 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
722 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
723 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
724 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
725 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
726 gi
->process_globally
= false;
727 gi
->offset_map_size_n
= 0;
728 gi
->offset_map_size_p
= 0;
729 gi
->offset_map_n
= NULL
;
730 gi
->offset_map_p
= NULL
;
731 rtx_group_vec
.safe_push (gi
);
733 return clear_alias_group
;
738 *slot
= gi
= (group_info_t
) pool_alloc (rtx_group_info_pool
);
740 gi
->id
= rtx_group_next_id
++;
741 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
742 gi
->canon_base_addr
= canon_rtx (base
);
743 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
744 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
745 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
746 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
747 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
748 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
749 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
750 gi
->process_globally
= false;
752 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
753 gi
->offset_map_size_n
= 0;
754 gi
->offset_map_size_p
= 0;
755 gi
->offset_map_n
= NULL
;
756 gi
->offset_map_p
= NULL
;
757 rtx_group_vec
.safe_push (gi
);
764 /* Initialization of data structures. */
770 globally_deleted
= 0;
773 bitmap_obstack_initialize (&dse_bitmap_obstack
);
774 gcc_obstack_init (&dse_obstack
);
776 scratch
= BITMAP_ALLOC (®_obstack
);
777 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
780 = create_alloc_pool ("rtx_store_info_pool",
781 sizeof (struct store_info
), 100);
783 = create_alloc_pool ("read_info_pool",
784 sizeof (struct read_info
), 100);
786 = create_alloc_pool ("insn_info_pool",
787 sizeof (struct insn_info
), 100);
789 = create_alloc_pool ("bb_info_pool",
790 sizeof (struct dse_bb_info
), 100);
792 = create_alloc_pool ("rtx_group_info_pool",
793 sizeof (struct group_info
), 100);
795 = create_alloc_pool ("deferred_change_pool",
796 sizeof (struct deferred_change
), 10);
798 rtx_group_table
= new hash_table
<invariant_group_base_hasher
> (11);
800 bb_table
= XNEWVEC (bb_info_t
, last_basic_block_for_fn (cfun
));
801 rtx_group_next_id
= 0;
803 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
805 init_alias_analysis ();
807 clear_alias_group
= NULL
;
812 /*----------------------------------------------------------------------------
815 Scan all of the insns. Any random ordering of the blocks is fine.
816 Each block is scanned in forward order to accommodate cselib which
817 is used to remove stores with non-constant bases.
818 ----------------------------------------------------------------------------*/
820 /* Delete all of the store_info recs from INSN_INFO. */
823 free_store_info (insn_info_t insn_info
)
825 store_info_t store_info
= insn_info
->store_rec
;
828 store_info_t next
= store_info
->next
;
829 if (store_info
->is_large
)
830 BITMAP_FREE (store_info
->positions_needed
.large
.bmap
);
831 if (store_info
->cse_base
)
832 pool_free (cse_store_info_pool
, store_info
);
834 pool_free (rtx_store_info_pool
, store_info
);
838 insn_info
->cannot_delete
= true;
839 insn_info
->contains_cselib_groups
= false;
840 insn_info
->store_rec
= NULL
;
845 rtx_insn
*first
, *current
;
846 regset fixed_regs_live
;
848 } note_add_store_info
;
850 /* Callback for emit_inc_dec_insn_before via note_stores.
851 Check if a register is clobbered which is live afterwards. */
854 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
857 note_add_store_info
*info
= (note_add_store_info
*) data
;
863 /* If this register is referenced by the current or an earlier insn,
864 that's OK. E.g. this applies to the register that is being incremented
865 with this addition. */
866 for (insn
= info
->first
;
867 insn
!= NEXT_INSN (info
->current
);
868 insn
= NEXT_INSN (insn
))
869 if (reg_referenced_p (loc
, PATTERN (insn
)))
872 /* If we come here, we have a clobber of a register that's only OK
873 if that register is not live. If we don't have liveness information
874 available, fail now. */
875 if (!info
->fixed_regs_live
)
877 info
->failure
= true;
880 /* Now check if this is a live fixed register. */
882 n
= hard_regno_nregs
[r
][GET_MODE (loc
)];
884 if (REGNO_REG_SET_P (info
->fixed_regs_live
, r
+n
))
885 info
->failure
= true;
888 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
889 SRC + SRCOFF before insn ARG. */
892 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
893 rtx op ATTRIBUTE_UNUSED
,
894 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
896 insn_info_t insn_info
= (insn_info_t
) arg
;
897 rtx_insn
*insn
= insn_info
->insn
, *new_insn
, *cur
;
898 note_add_store_info info
;
900 /* We can reuse all operands without copying, because we are about
901 to delete the insn that contained it. */
905 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
906 new_insn
= get_insns ();
910 new_insn
= as_a
<rtx_insn
*> (gen_move_insn (dest
, src
));
911 info
.first
= new_insn
;
912 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
913 info
.failure
= false;
914 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
917 note_stores (PATTERN (cur
), note_add_store
, &info
);
920 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
921 return it immediately, communicating the failure to its caller. */
925 emit_insn_before (new_insn
, insn
);
930 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
931 is there, is split into a separate insn.
932 Return true on success (or if there was nothing to do), false on failure. */
935 check_for_inc_dec_1 (insn_info_t insn_info
)
937 rtx_insn
*insn
= insn_info
->insn
;
938 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
940 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
946 /* Entry point for postreload. If you work on reload_cse, or you need this
947 anywhere else, consider if you can provide register liveness information
948 and add a parameter to this function so that it can be passed down in
949 insn_info.fixed_regs_live. */
951 check_for_inc_dec (rtx_insn
*insn
)
953 struct insn_info insn_info
;
956 insn_info
.insn
= insn
;
957 insn_info
.fixed_regs_live
= NULL
;
958 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
960 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
965 /* Delete the insn and free all of the fields inside INSN_INFO. */
968 delete_dead_store_insn (insn_info_t insn_info
)
970 read_info_t read_info
;
975 if (!check_for_inc_dec_1 (insn_info
))
977 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
979 fprintf (dump_file
, "Locally deleting insn %d ",
980 INSN_UID (insn_info
->insn
));
981 if (insn_info
->store_rec
->alias_set
)
982 fprintf (dump_file
, "alias set %d\n",
983 (int) insn_info
->store_rec
->alias_set
);
985 fprintf (dump_file
, "\n");
988 free_store_info (insn_info
);
989 read_info
= insn_info
->read_rec
;
993 read_info_t next
= read_info
->next
;
994 pool_free (read_info_pool
, read_info
);
997 insn_info
->read_rec
= NULL
;
999 delete_insn (insn_info
->insn
);
1001 insn_info
->insn
= NULL
;
1003 insn_info
->wild_read
= false;
1006 /* Return whether DECL, a local variable, can possibly escape the current
1010 local_variable_can_escape (tree decl
)
1012 if (TREE_ADDRESSABLE (decl
))
1015 /* If this is a partitioned variable, we need to consider all the variables
1016 in the partition. This is necessary because a store into one of them can
1017 be replaced with a store into another and this may not change the outcome
1018 of the escape analysis. */
1019 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
1021 tree
*namep
= cfun
->gimple_df
->decls_to_pointers
->get (decl
);
1023 return TREE_ADDRESSABLE (*namep
);
1029 /* Return whether EXPR can possibly escape the current function scope. */
1032 can_escape (tree expr
)
1037 base
= get_base_address (expr
);
1039 && !may_be_aliased (base
)
1040 && !(TREE_CODE (base
) == VAR_DECL
1041 && !DECL_EXTERNAL (base
)
1042 && !TREE_STATIC (base
)
1043 && local_variable_can_escape (base
)))
1048 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1049 OFFSET and WIDTH. */
1052 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1056 bool expr_escapes
= can_escape (expr
);
1057 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1058 for (i
=offset
; i
<offset
+width
; i
++)
1066 store1
= group
->store1_n
;
1067 store2
= group
->store2_n
;
1068 escaped
= group
->escaped_n
;
1073 store1
= group
->store1_p
;
1074 store2
= group
->store2_p
;
1075 escaped
= group
->escaped_p
;
1079 if (!bitmap_set_bit (store1
, ai
))
1080 bitmap_set_bit (store2
, ai
);
1085 if (group
->offset_map_size_n
< ai
)
1086 group
->offset_map_size_n
= ai
;
1090 if (group
->offset_map_size_p
< ai
)
1091 group
->offset_map_size_p
= ai
;
1095 bitmap_set_bit (escaped
, ai
);
1100 reset_active_stores (void)
1102 active_local_stores
= NULL
;
1103 active_local_stores_len
= 0;
1106 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1109 free_read_records (bb_info_t bb_info
)
1111 insn_info_t insn_info
= bb_info
->last_insn
;
1112 read_info_t
*ptr
= &insn_info
->read_rec
;
1115 read_info_t next
= (*ptr
)->next
;
1116 if ((*ptr
)->alias_set
== 0)
1118 pool_free (read_info_pool
, *ptr
);
1122 ptr
= &(*ptr
)->next
;
1126 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1129 add_wild_read (bb_info_t bb_info
)
1131 insn_info_t insn_info
= bb_info
->last_insn
;
1132 insn_info
->wild_read
= true;
1133 free_read_records (bb_info
);
1134 reset_active_stores ();
1137 /* Set the BB_INFO so that the last insn is marked as a wild read of
1138 non-frame locations. */
1141 add_non_frame_wild_read (bb_info_t bb_info
)
1143 insn_info_t insn_info
= bb_info
->last_insn
;
1144 insn_info
->non_frame_wild_read
= true;
1145 free_read_records (bb_info
);
1146 reset_active_stores ();
1149 /* Return true if X is a constant or one of the registers that behave
1150 as a constant over the life of a function. This is equivalent to
1151 !rtx_varies_p for memory addresses. */
1154 const_or_frame_p (rtx x
)
1159 if (GET_CODE (x
) == REG
)
1161 /* Note that we have to test for the actual rtx used for the frame
1162 and arg pointers and not just the register number in case we have
1163 eliminated the frame and/or arg pointer and are using it
1165 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1166 /* The arg pointer varies if it is not a fixed register. */
1167 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1168 || x
== pic_offset_table_rtx
)
1176 /* Take all reasonable action to put the address of MEM into the form
1177 that we can do analysis on.
1179 The gold standard is to get the address into the form: address +
1180 OFFSET where address is something that rtx_varies_p considers a
1181 constant. When we can get the address in this form, we can do
1182 global analysis on it. Note that for constant bases, address is
1183 not actually returned, only the group_id. The address can be
1186 If that fails, we try cselib to get a value we can at least use
1187 locally. If that fails we return false.
1189 The GROUP_ID is set to -1 for cselib bases and the index of the
1190 group for non_varying bases.
1192 FOR_READ is true if this is a mem read and false if not. */
1195 canon_address (rtx mem
,
1196 alias_set_type
*alias_set_out
,
1198 HOST_WIDE_INT
*offset
,
1201 machine_mode address_mode
= get_address_mode (mem
);
1202 rtx mem_address
= XEXP (mem
, 0);
1203 rtx expanded_address
, address
;
1208 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1210 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1212 fprintf (dump_file
, " mem: ");
1213 print_inline_rtx (dump_file
, mem_address
, 0);
1214 fprintf (dump_file
, "\n");
1217 /* First see if just canon_rtx (mem_address) is const or frame,
1218 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1220 for (expanded
= 0; expanded
< 2; expanded
++)
1224 /* Use cselib to replace all of the reg references with the full
1225 expression. This will take care of the case where we have
1227 r_x = base + offset;
1232 val = *(base + offset); */
1234 expanded_address
= cselib_expand_value_rtx (mem_address
,
1237 /* If this fails, just go with the address from first
1239 if (!expanded_address
)
1243 expanded_address
= mem_address
;
1245 /* Split the address into canonical BASE + OFFSET terms. */
1246 address
= canon_rtx (expanded_address
);
1250 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1254 fprintf (dump_file
, "\n after cselib_expand address: ");
1255 print_inline_rtx (dump_file
, expanded_address
, 0);
1256 fprintf (dump_file
, "\n");
1259 fprintf (dump_file
, "\n after canon_rtx address: ");
1260 print_inline_rtx (dump_file
, address
, 0);
1261 fprintf (dump_file
, "\n");
1264 if (GET_CODE (address
) == CONST
)
1265 address
= XEXP (address
, 0);
1267 if (GET_CODE (address
) == PLUS
1268 && CONST_INT_P (XEXP (address
, 1)))
1270 *offset
= INTVAL (XEXP (address
, 1));
1271 address
= XEXP (address
, 0);
1274 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1275 && const_or_frame_p (address
))
1277 group_info_t group
= get_group_info (address
);
1279 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1280 fprintf (dump_file
, " gid=%d offset=%d \n",
1281 group
->id
, (int)*offset
);
1283 *group_id
= group
->id
;
1288 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1293 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1294 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1297 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1298 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1299 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1304 /* Clear the rhs field from the active_local_stores array. */
1307 clear_rhs_from_active_local_stores (void)
1309 insn_info_t ptr
= active_local_stores
;
1313 store_info_t store_info
= ptr
->store_rec
;
1314 /* Skip the clobbers. */
1315 while (!store_info
->is_set
)
1316 store_info
= store_info
->next
;
1318 store_info
->rhs
= NULL
;
1319 store_info
->const_rhs
= NULL
;
1321 ptr
= ptr
->next_local_store
;
1326 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1329 set_position_unneeded (store_info_t s_info
, int pos
)
1331 if (__builtin_expect (s_info
->is_large
, false))
1333 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1334 s_info
->positions_needed
.large
.count
++;
1337 s_info
->positions_needed
.small_bitmask
1338 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1341 /* Mark the whole store S_INFO as unneeded. */
1344 set_all_positions_unneeded (store_info_t s_info
)
1346 if (__builtin_expect (s_info
->is_large
, false))
1348 int pos
, end
= s_info
->end
- s_info
->begin
;
1349 for (pos
= 0; pos
< end
; pos
++)
1350 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1351 s_info
->positions_needed
.large
.count
= end
;
1354 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1357 /* Return TRUE if any bytes from S_INFO store are needed. */
1360 any_positions_needed_p (store_info_t s_info
)
1362 if (__builtin_expect (s_info
->is_large
, false))
1363 return (s_info
->positions_needed
.large
.count
1364 < s_info
->end
- s_info
->begin
);
1366 return (s_info
->positions_needed
.small_bitmask
1367 != (unsigned HOST_WIDE_INT
) 0);
1370 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1371 store are needed. */
1374 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1376 if (__builtin_expect (s_info
->is_large
, false))
1378 int end
= start
+ width
;
1380 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1386 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1387 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1392 static rtx
get_stored_val (store_info_t
, machine_mode
, HOST_WIDE_INT
,
1393 HOST_WIDE_INT
, basic_block
, bool);
1396 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1397 there is a candidate store, after adding it to the appropriate
1398 local store group if so. */
1401 record_store (rtx body
, bb_info_t bb_info
)
1403 rtx mem
, rhs
, const_rhs
, mem_addr
;
1404 HOST_WIDE_INT offset
= 0;
1405 HOST_WIDE_INT width
= 0;
1406 alias_set_type spill_alias_set
;
1407 insn_info_t insn_info
= bb_info
->last_insn
;
1408 store_info_t store_info
= NULL
;
1410 cselib_val
*base
= NULL
;
1411 insn_info_t ptr
, last
, redundant_reason
;
1412 bool store_is_unused
;
1414 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1417 mem
= SET_DEST (body
);
1419 /* If this is not used, then this cannot be used to keep the insn
1420 from being deleted. On the other hand, it does provide something
1421 that can be used to prove that another store is dead. */
1423 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1425 /* Check whether that value is a suitable memory location. */
1428 /* If the set or clobber is unused, then it does not effect our
1429 ability to get rid of the entire insn. */
1430 if (!store_is_unused
)
1431 insn_info
->cannot_delete
= true;
1435 /* At this point we know mem is a mem. */
1436 if (GET_MODE (mem
) == BLKmode
)
1438 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1440 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1441 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1442 add_wild_read (bb_info
);
1443 insn_info
->cannot_delete
= true;
1446 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1447 as memset (addr, 0, 36); */
1448 else if (!MEM_SIZE_KNOWN_P (mem
)
1449 || MEM_SIZE (mem
) <= 0
1450 || MEM_SIZE (mem
) > MAX_OFFSET
1451 || GET_CODE (body
) != SET
1452 || !CONST_INT_P (SET_SRC (body
)))
1454 if (!store_is_unused
)
1456 /* If the set or clobber is unused, then it does not effect our
1457 ability to get rid of the entire insn. */
1458 insn_info
->cannot_delete
= true;
1459 clear_rhs_from_active_local_stores ();
1465 /* We can still process a volatile mem, we just cannot delete it. */
1466 if (MEM_VOLATILE_P (mem
))
1467 insn_info
->cannot_delete
= true;
1469 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1471 clear_rhs_from_active_local_stores ();
1475 if (GET_MODE (mem
) == BLKmode
)
1476 width
= MEM_SIZE (mem
);
1478 width
= GET_MODE_SIZE (GET_MODE (mem
));
1480 if (spill_alias_set
)
1482 bitmap store1
= clear_alias_group
->store1_p
;
1483 bitmap store2
= clear_alias_group
->store2_p
;
1485 gcc_assert (GET_MODE (mem
) != BLKmode
);
1487 if (!bitmap_set_bit (store1
, spill_alias_set
))
1488 bitmap_set_bit (store2
, spill_alias_set
);
1490 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1491 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1493 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1495 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1496 fprintf (dump_file
, " processing spill store %d(%s)\n",
1497 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1499 else if (group_id
>= 0)
1501 /* In the restrictive case where the base is a constant or the
1502 frame pointer we can do global analysis. */
1505 = rtx_group_vec
[group_id
];
1506 tree expr
= MEM_EXPR (mem
);
1508 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1509 set_usage_bits (group
, offset
, width
, expr
);
1511 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1512 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1513 group_id
, (int)offset
, (int)(offset
+width
));
1517 if (may_be_sp_based_p (XEXP (mem
, 0)))
1518 insn_info
->stack_pointer_based
= true;
1519 insn_info
->contains_cselib_groups
= true;
1521 store_info
= (store_info_t
) pool_alloc (cse_store_info_pool
);
1524 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1525 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1526 (int)offset
, (int)(offset
+width
));
1529 const_rhs
= rhs
= NULL_RTX
;
1530 if (GET_CODE (body
) == SET
1531 /* No place to keep the value after ra. */
1532 && !reload_completed
1533 && (REG_P (SET_SRC (body
))
1534 || GET_CODE (SET_SRC (body
)) == SUBREG
1535 || CONSTANT_P (SET_SRC (body
)))
1536 && !MEM_VOLATILE_P (mem
)
1537 /* Sometimes the store and reload is used for truncation and
1539 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1541 rhs
= SET_SRC (body
);
1542 if (CONSTANT_P (rhs
))
1544 else if (body
== PATTERN (insn_info
->insn
))
1546 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1547 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1548 const_rhs
= XEXP (tem
, 0);
1550 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1552 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1554 if (tem
&& CONSTANT_P (tem
))
1559 /* Check to see if this stores causes some other stores to be
1561 ptr
= active_local_stores
;
1563 redundant_reason
= NULL
;
1564 mem
= canon_rtx (mem
);
1565 /* For alias_set != 0 canon_true_dependence should be never called. */
1566 if (spill_alias_set
)
1567 mem_addr
= NULL_RTX
;
1571 mem_addr
= base
->val_rtx
;
1575 = rtx_group_vec
[group_id
];
1576 mem_addr
= group
->canon_base_addr
;
1578 /* get_addr can only handle VALUE but cannot handle expr like:
1579 VALUE + OFFSET, so call get_addr to get original addr for
1580 mem_addr before plus_constant. */
1581 mem_addr
= get_addr (mem_addr
);
1583 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1588 insn_info_t next
= ptr
->next_local_store
;
1589 store_info_t s_info
= ptr
->store_rec
;
1592 /* Skip the clobbers. We delete the active insn if this insn
1593 shadows the set. To have been put on the active list, it
1594 has exactly on set. */
1595 while (!s_info
->is_set
)
1596 s_info
= s_info
->next
;
1598 if (s_info
->alias_set
!= spill_alias_set
)
1600 else if (s_info
->alias_set
)
1602 struct clear_alias_mode_holder
*entry
1603 = clear_alias_set_lookup (s_info
->alias_set
);
1604 /* Generally, spills cannot be processed if and of the
1605 references to the slot have a different mode. But if
1606 we are in the same block and mode is exactly the same
1607 between this store and one before in the same block,
1608 we can still delete it. */
1609 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1610 && (GET_MODE (mem
) == entry
->mode
))
1613 set_all_positions_unneeded (s_info
);
1615 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1616 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1617 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1619 else if ((s_info
->group_id
== group_id
)
1620 && (s_info
->cse_base
== base
))
1623 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1624 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1625 INSN_UID (ptr
->insn
), s_info
->group_id
,
1626 (int)s_info
->begin
, (int)s_info
->end
);
1628 /* Even if PTR won't be eliminated as unneeded, if both
1629 PTR and this insn store the same constant value, we might
1630 eliminate this insn instead. */
1631 if (s_info
->const_rhs
1633 && offset
>= s_info
->begin
1634 && offset
+ width
<= s_info
->end
1635 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1638 if (GET_MODE (mem
) == BLKmode
)
1640 if (GET_MODE (s_info
->mem
) == BLKmode
1641 && s_info
->const_rhs
== const_rhs
)
1642 redundant_reason
= ptr
;
1644 else if (s_info
->const_rhs
== const0_rtx
1645 && const_rhs
== const0_rtx
)
1646 redundant_reason
= ptr
;
1651 val
= get_stored_val (s_info
, GET_MODE (mem
),
1652 offset
, offset
+ width
,
1653 BLOCK_FOR_INSN (insn_info
->insn
),
1655 if (get_insns () != NULL
)
1658 if (val
&& rtx_equal_p (val
, const_rhs
))
1659 redundant_reason
= ptr
;
1663 for (i
= MAX (offset
, s_info
->begin
);
1664 i
< offset
+ width
&& i
< s_info
->end
;
1666 set_position_unneeded (s_info
, i
- s_info
->begin
);
1668 else if (s_info
->rhs
)
1669 /* Need to see if it is possible for this store to overwrite
1670 the value of store_info. If it is, set the rhs to NULL to
1671 keep it from being used to remove a load. */
1673 if (canon_true_dependence (s_info
->mem
,
1674 GET_MODE (s_info
->mem
),
1679 s_info
->const_rhs
= NULL
;
1683 /* An insn can be deleted if every position of every one of
1684 its s_infos is zero. */
1685 if (any_positions_needed_p (s_info
))
1690 insn_info_t insn_to_delete
= ptr
;
1692 active_local_stores_len
--;
1694 last
->next_local_store
= ptr
->next_local_store
;
1696 active_local_stores
= ptr
->next_local_store
;
1698 if (!insn_to_delete
->cannot_delete
)
1699 delete_dead_store_insn (insn_to_delete
);
1707 /* Finish filling in the store_info. */
1708 store_info
->next
= insn_info
->store_rec
;
1709 insn_info
->store_rec
= store_info
;
1710 store_info
->mem
= mem
;
1711 store_info
->alias_set
= spill_alias_set
;
1712 store_info
->mem_addr
= mem_addr
;
1713 store_info
->cse_base
= base
;
1714 if (width
> HOST_BITS_PER_WIDE_INT
)
1716 store_info
->is_large
= true;
1717 store_info
->positions_needed
.large
.count
= 0;
1718 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1722 store_info
->is_large
= false;
1723 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1725 store_info
->group_id
= group_id
;
1726 store_info
->begin
= offset
;
1727 store_info
->end
= offset
+ width
;
1728 store_info
->is_set
= GET_CODE (body
) == SET
;
1729 store_info
->rhs
= rhs
;
1730 store_info
->const_rhs
= const_rhs
;
1731 store_info
->redundant_reason
= redundant_reason
;
1733 /* If this is a clobber, we return 0. We will only be able to
1734 delete this insn if there is only one store USED store, but we
1735 can use the clobber to delete other stores earlier. */
1736 return store_info
->is_set
? 1 : 0;
1741 dump_insn_info (const char * start
, insn_info_t insn_info
)
1743 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1744 INSN_UID (insn_info
->insn
),
1745 insn_info
->store_rec
? "has store" : "naked");
1749 /* If the modes are different and the value's source and target do not
1750 line up, we need to extract the value from lower part of the rhs of
1751 the store, shift it, and then put it into a form that can be shoved
1752 into the read_insn. This function generates a right SHIFT of a
1753 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1754 shift sequence is returned or NULL if we failed to find a
1758 find_shift_sequence (int access_size
,
1759 store_info_t store_info
,
1760 machine_mode read_mode
,
1761 int shift
, bool speed
, bool require_cst
)
1763 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1764 machine_mode new_mode
;
1765 rtx read_reg
= NULL
;
1767 /* Some machines like the x86 have shift insns for each size of
1768 operand. Other machines like the ppc or the ia-64 may only have
1769 shift insns that shift values within 32 or 64 bit registers.
1770 This loop tries to find the smallest shift insn that will right
1771 justify the value we want to read but is available in one insn on
1774 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1776 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1777 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1779 rtx target
, new_reg
, new_lhs
;
1780 rtx_insn
*shift_seq
, *insn
;
1783 /* If a constant was stored into memory, try to simplify it here,
1784 otherwise the cost of the shift might preclude this optimization
1785 e.g. at -Os, even when no actual shift will be needed. */
1786 if (store_info
->const_rhs
)
1788 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1789 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1791 if (ret
&& CONSTANT_P (ret
))
1793 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1794 ret
, GEN_INT (shift
));
1795 if (ret
&& CONSTANT_P (ret
))
1797 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1798 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1799 if (ret
&& CONSTANT_P (ret
)
1800 && set_src_cost (ret
, speed
) <= COSTS_N_INSNS (1))
1809 /* Try a wider mode if truncating the store mode to NEW_MODE
1810 requires a real instruction. */
1811 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1812 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1815 /* Also try a wider mode if the necessary punning is either not
1816 desirable or not possible. */
1817 if (!CONSTANT_P (store_info
->rhs
)
1818 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1821 new_reg
= gen_reg_rtx (new_mode
);
1825 /* In theory we could also check for an ashr. Ian Taylor knows
1826 of one dsp where the cost of these two was not the same. But
1827 this really is a rare case anyway. */
1828 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1829 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1831 shift_seq
= get_insns ();
1834 if (target
!= new_reg
|| shift_seq
== NULL
)
1838 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1840 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1842 /* The computation up to here is essentially independent
1843 of the arguments and could be precomputed. It may
1844 not be worth doing so. We could precompute if
1845 worthwhile or at least cache the results. The result
1846 technically depends on both SHIFT and ACCESS_SIZE,
1847 but in practice the answer will depend only on ACCESS_SIZE. */
1849 if (cost
> COSTS_N_INSNS (1))
1852 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1853 copy_rtx (store_info
->rhs
));
1854 if (new_lhs
== NULL_RTX
)
1857 /* We found an acceptable shift. Generate a move to
1858 take the value from the store and put it into the
1859 shift pseudo, then shift it, then generate another
1860 move to put in into the target of the read. */
1861 emit_move_insn (new_reg
, new_lhs
);
1862 emit_insn (shift_seq
);
1863 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1871 /* Call back for note_stores to find the hard regs set or clobbered by
1872 insn. Data is a bitmap of the hardregs set so far. */
1875 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1877 bitmap regs_set
= (bitmap
) data
;
1880 && HARD_REGISTER_P (x
))
1882 unsigned int regno
= REGNO (x
);
1883 bitmap_set_range (regs_set
, regno
,
1884 hard_regno_nregs
[regno
][GET_MODE (x
)]);
1888 /* Helper function for replace_read and record_store.
1889 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1890 to one before READ_END bytes read in READ_MODE. Return NULL
1891 if not successful. If REQUIRE_CST is true, return always constant. */
1894 get_stored_val (store_info_t store_info
, machine_mode read_mode
,
1895 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1896 basic_block bb
, bool require_cst
)
1898 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1900 int access_size
; /* In bytes. */
1903 /* To get here the read is within the boundaries of the write so
1904 shift will never be negative. Start out with the shift being in
1906 if (store_mode
== BLKmode
)
1908 else if (BYTES_BIG_ENDIAN
)
1909 shift
= store_info
->end
- read_end
;
1911 shift
= read_begin
- store_info
->begin
;
1913 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1915 /* From now on it is bits. */
1916 shift
*= BITS_PER_UNIT
;
1919 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1920 optimize_bb_for_speed_p (bb
),
1922 else if (store_mode
== BLKmode
)
1924 /* The store is a memset (addr, const_val, const_size). */
1925 gcc_assert (CONST_INT_P (store_info
->rhs
));
1926 store_mode
= int_mode_for_mode (read_mode
);
1927 if (store_mode
== BLKmode
)
1928 read_reg
= NULL_RTX
;
1929 else if (store_info
->rhs
== const0_rtx
)
1930 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1931 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1932 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1933 read_reg
= NULL_RTX
;
1936 unsigned HOST_WIDE_INT c
1937 = INTVAL (store_info
->rhs
)
1938 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1939 int shift
= BITS_PER_UNIT
;
1940 while (shift
< HOST_BITS_PER_WIDE_INT
)
1945 read_reg
= gen_int_mode (c
, store_mode
);
1946 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1949 else if (store_info
->const_rhs
1951 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1952 read_reg
= extract_low_bits (read_mode
, store_mode
,
1953 copy_rtx (store_info
->const_rhs
));
1955 read_reg
= extract_low_bits (read_mode
, store_mode
,
1956 copy_rtx (store_info
->rhs
));
1957 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1958 read_reg
= NULL_RTX
;
1962 /* Take a sequence of:
1985 Depending on the alignment and the mode of the store and
1989 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1990 and READ_INSN are for the read. Return true if the replacement
1994 replace_read (store_info_t store_info
, insn_info_t store_insn
,
1995 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1998 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1999 machine_mode read_mode
= GET_MODE (read_info
->mem
);
2000 rtx_insn
*insns
, *this_insn
;
2007 /* Create a sequence of instructions to set up the read register.
2008 This sequence goes immediately before the store and its result
2009 is read by the load.
2011 We need to keep this in perspective. We are replacing a read
2012 with a sequence of insns, but the read will almost certainly be
2013 in cache, so it is not going to be an expensive one. Thus, we
2014 are not willing to do a multi insn shift or worse a subroutine
2015 call to get rid of the read. */
2016 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2017 fprintf (dump_file
, "trying to replace %smode load in insn %d"
2018 " from %smode store in insn %d\n",
2019 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
2020 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
2022 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
2023 read_reg
= get_stored_val (store_info
,
2024 read_mode
, read_info
->begin
, read_info
->end
,
2026 if (read_reg
== NULL_RTX
)
2029 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2030 fprintf (dump_file
, " -- could not extract bits of stored value\n");
2033 /* Force the value into a new register so that it won't be clobbered
2034 between the store and the load. */
2035 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
2036 insns
= get_insns ();
2039 if (insns
!= NULL_RTX
)
2041 /* Now we have to scan the set of new instructions to see if the
2042 sequence contains and sets of hardregs that happened to be
2043 live at this point. For instance, this can happen if one of
2044 the insns sets the CC and the CC happened to be live at that
2045 point. This does occasionally happen, see PR 37922. */
2046 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2048 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2049 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2051 bitmap_and_into (regs_set
, regs_live
);
2052 if (!bitmap_empty_p (regs_set
))
2054 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2057 "abandoning replacement because sequence clobbers live hardregs:");
2058 df_print_regset (dump_file
, regs_set
);
2061 BITMAP_FREE (regs_set
);
2064 BITMAP_FREE (regs_set
);
2067 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2069 deferred_change_t deferred_change
=
2070 (deferred_change_t
) pool_alloc (deferred_change_pool
);
2072 /* Insert this right before the store insn where it will be safe
2073 from later insns that might change it before the read. */
2074 emit_insn_before (insns
, store_insn
->insn
);
2076 /* And now for the kludge part: cselib croaks if you just
2077 return at this point. There are two reasons for this:
2079 1) Cselib has an idea of how many pseudos there are and
2080 that does not include the new ones we just added.
2082 2) Cselib does not know about the move insn we added
2083 above the store_info, and there is no way to tell it
2084 about it, because it has "moved on".
2086 Problem (1) is fixable with a certain amount of engineering.
2087 Problem (2) is requires starting the bb from scratch. This
2090 So we are just going to have to lie. The move/extraction
2091 insns are not really an issue, cselib did not see them. But
2092 the use of the new pseudo read_insn is a real problem because
2093 cselib has not scanned this insn. The way that we solve this
2094 problem is that we are just going to put the mem back for now
2095 and when we are finished with the block, we undo this. We
2096 keep a table of mems to get rid of. At the end of the basic
2097 block we can put them back. */
2099 *loc
= read_info
->mem
;
2100 deferred_change
->next
= deferred_change_list
;
2101 deferred_change_list
= deferred_change
;
2102 deferred_change
->loc
= loc
;
2103 deferred_change
->reg
= read_reg
;
2105 /* Get rid of the read_info, from the point of view of the
2106 rest of dse, play like this read never happened. */
2107 read_insn
->read_rec
= read_info
->next
;
2108 pool_free (read_info_pool
, read_info
);
2109 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2111 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2112 print_simple_rtl (dump_file
, read_reg
);
2113 fprintf (dump_file
, "\n");
2119 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2121 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2122 print_simple_rtl (dump_file
, read_reg
);
2123 fprintf (dump_file
, " led to an invalid instruction\n");
2129 /* Check the address of MEM *LOC and kill any appropriate stores that may
2133 check_mem_read_rtx (rtx
*loc
, bb_info_t bb_info
)
2135 rtx mem
= *loc
, mem_addr
;
2136 insn_info_t insn_info
;
2137 HOST_WIDE_INT offset
= 0;
2138 HOST_WIDE_INT width
= 0;
2139 alias_set_type spill_alias_set
= 0;
2140 cselib_val
*base
= NULL
;
2142 read_info_t read_info
;
2144 insn_info
= bb_info
->last_insn
;
2146 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2147 || (MEM_VOLATILE_P (mem
)))
2149 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2150 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2151 add_wild_read (bb_info
);
2152 insn_info
->cannot_delete
= true;
2156 /* If it is reading readonly mem, then there can be no conflict with
2158 if (MEM_READONLY_P (mem
))
2161 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2164 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2165 add_wild_read (bb_info
);
2169 if (GET_MODE (mem
) == BLKmode
)
2172 width
= GET_MODE_SIZE (GET_MODE (mem
));
2174 read_info
= (read_info_t
) pool_alloc (read_info_pool
);
2175 read_info
->group_id
= group_id
;
2176 read_info
->mem
= mem
;
2177 read_info
->alias_set
= spill_alias_set
;
2178 read_info
->begin
= offset
;
2179 read_info
->end
= offset
+ width
;
2180 read_info
->next
= insn_info
->read_rec
;
2181 insn_info
->read_rec
= read_info
;
2182 /* For alias_set != 0 canon_true_dependence should be never called. */
2183 if (spill_alias_set
)
2184 mem_addr
= NULL_RTX
;
2188 mem_addr
= base
->val_rtx
;
2192 = rtx_group_vec
[group_id
];
2193 mem_addr
= group
->canon_base_addr
;
2195 /* get_addr can only handle VALUE but cannot handle expr like:
2196 VALUE + OFFSET, so call get_addr to get original addr for
2197 mem_addr before plus_constant. */
2198 mem_addr
= get_addr (mem_addr
);
2200 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2203 /* We ignore the clobbers in store_info. The is mildly aggressive,
2204 but there really should not be a clobber followed by a read. */
2206 if (spill_alias_set
)
2208 insn_info_t i_ptr
= active_local_stores
;
2209 insn_info_t last
= NULL
;
2211 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2212 fprintf (dump_file
, " processing spill load %d\n",
2213 (int) spill_alias_set
);
2217 store_info_t store_info
= i_ptr
->store_rec
;
2219 /* Skip the clobbers. */
2220 while (!store_info
->is_set
)
2221 store_info
= store_info
->next
;
2223 if (store_info
->alias_set
== spill_alias_set
)
2225 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2226 dump_insn_info ("removing from active", i_ptr
);
2228 active_local_stores_len
--;
2230 last
->next_local_store
= i_ptr
->next_local_store
;
2232 active_local_stores
= i_ptr
->next_local_store
;
2236 i_ptr
= i_ptr
->next_local_store
;
2239 else if (group_id
>= 0)
2241 /* This is the restricted case where the base is a constant or
2242 the frame pointer and offset is a constant. */
2243 insn_info_t i_ptr
= active_local_stores
;
2244 insn_info_t last
= NULL
;
2246 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2249 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2252 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2253 group_id
, (int)offset
, (int)(offset
+width
));
2258 bool remove
= false;
2259 store_info_t store_info
= i_ptr
->store_rec
;
2261 /* Skip the clobbers. */
2262 while (!store_info
->is_set
)
2263 store_info
= store_info
->next
;
2265 /* There are three cases here. */
2266 if (store_info
->group_id
< 0)
2267 /* We have a cselib store followed by a read from a
2270 = canon_true_dependence (store_info
->mem
,
2271 GET_MODE (store_info
->mem
),
2272 store_info
->mem_addr
,
2275 else if (group_id
== store_info
->group_id
)
2277 /* This is a block mode load. We may get lucky and
2278 canon_true_dependence may save the day. */
2281 = canon_true_dependence (store_info
->mem
,
2282 GET_MODE (store_info
->mem
),
2283 store_info
->mem_addr
,
2286 /* If this read is just reading back something that we just
2287 stored, rewrite the read. */
2291 && offset
>= store_info
->begin
2292 && offset
+ width
<= store_info
->end
2293 && all_positions_needed_p (store_info
,
2294 offset
- store_info
->begin
,
2296 && replace_read (store_info
, i_ptr
, read_info
,
2297 insn_info
, loc
, bb_info
->regs_live
))
2300 /* The bases are the same, just see if the offsets
2302 if ((offset
< store_info
->end
)
2303 && (offset
+ width
> store_info
->begin
))
2309 The else case that is missing here is that the
2310 bases are constant but different. There is nothing
2311 to do here because there is no overlap. */
2315 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2316 dump_insn_info ("removing from active", i_ptr
);
2318 active_local_stores_len
--;
2320 last
->next_local_store
= i_ptr
->next_local_store
;
2322 active_local_stores
= i_ptr
->next_local_store
;
2326 i_ptr
= i_ptr
->next_local_store
;
2331 insn_info_t i_ptr
= active_local_stores
;
2332 insn_info_t last
= NULL
;
2333 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2335 fprintf (dump_file
, " processing cselib load mem:");
2336 print_inline_rtx (dump_file
, mem
, 0);
2337 fprintf (dump_file
, "\n");
2342 bool remove
= false;
2343 store_info_t store_info
= i_ptr
->store_rec
;
2345 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2346 fprintf (dump_file
, " processing cselib load against insn %d\n",
2347 INSN_UID (i_ptr
->insn
));
2349 /* Skip the clobbers. */
2350 while (!store_info
->is_set
)
2351 store_info
= store_info
->next
;
2353 /* If this read is just reading back something that we just
2354 stored, rewrite the read. */
2356 && store_info
->group_id
== -1
2357 && store_info
->cse_base
== base
2359 && offset
>= store_info
->begin
2360 && offset
+ width
<= store_info
->end
2361 && all_positions_needed_p (store_info
,
2362 offset
- store_info
->begin
, width
)
2363 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2364 bb_info
->regs_live
))
2367 if (!store_info
->alias_set
)
2368 remove
= canon_true_dependence (store_info
->mem
,
2369 GET_MODE (store_info
->mem
),
2370 store_info
->mem_addr
,
2375 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2376 dump_insn_info ("removing from active", i_ptr
);
2378 active_local_stores_len
--;
2380 last
->next_local_store
= i_ptr
->next_local_store
;
2382 active_local_stores
= i_ptr
->next_local_store
;
2386 i_ptr
= i_ptr
->next_local_store
;
2391 /* A note_uses callback in which DATA points the INSN_INFO for
2392 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2393 true for any part of *LOC. */
2396 check_mem_read_use (rtx
*loc
, void *data
)
2398 subrtx_ptr_iterator::array_type array
;
2399 FOR_EACH_SUBRTX_PTR (iter
, array
, loc
, NONCONST
)
2403 check_mem_read_rtx (loc
, (bb_info_t
) data
);
2408 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2409 So far it only handles arguments passed in registers. */
2412 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2414 CUMULATIVE_ARGS args_so_far_v
;
2415 cumulative_args_t args_so_far
;
2419 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2420 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2422 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2424 arg
!= void_list_node
&& idx
< nargs
;
2425 arg
= TREE_CHAIN (arg
), idx
++)
2427 machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2429 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2430 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2431 || GET_MODE_CLASS (mode
) != MODE_INT
)
2434 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2436 link
= XEXP (link
, 1))
2437 if (GET_CODE (XEXP (link
, 0)) == USE
)
2439 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2440 if (REG_P (args
[idx
])
2441 && REGNO (args
[idx
]) == REGNO (reg
)
2442 && (GET_MODE (args
[idx
]) == mode
2443 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2444 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2446 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2447 > GET_MODE_SIZE (mode
)))))
2453 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2454 if (GET_MODE (args
[idx
]) != mode
)
2456 if (!tmp
|| !CONST_INT_P (tmp
))
2458 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2463 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2465 if (arg
!= void_list_node
|| idx
!= nargs
)
2470 /* Return a bitmap of the fixed registers contained in IN. */
2473 copy_fixed_regs (const_bitmap in
)
2477 ret
= ALLOC_REG_SET (NULL
);
2478 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2482 /* Apply record_store to all candidate stores in INSN. Mark INSN
2483 if some part of it is not a candidate store and assigns to a
2484 non-register target. */
2487 scan_insn (bb_info_t bb_info
, rtx_insn
*insn
)
2490 insn_info_t insn_info
= (insn_info_t
) pool_alloc (insn_info_pool
);
2492 memset (insn_info
, 0, sizeof (struct insn_info
));
2494 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2495 fprintf (dump_file
, "\n**scanning insn=%d\n",
2498 insn_info
->prev_insn
= bb_info
->last_insn
;
2499 insn_info
->insn
= insn
;
2500 bb_info
->last_insn
= insn_info
;
2502 if (DEBUG_INSN_P (insn
))
2504 insn_info
->cannot_delete
= true;
2508 /* Look at all of the uses in the insn. */
2509 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2514 tree memset_call
= NULL_TREE
;
2516 insn_info
->cannot_delete
= true;
2518 /* Const functions cannot do anything bad i.e. read memory,
2519 however, they can read their parameters which may have
2520 been pushed onto the stack.
2521 memset and bzero don't read memory either. */
2522 const_call
= RTL_CONST_CALL_P (insn
);
2525 rtx call
= get_call_rtx_from (insn
);
2526 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2528 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2529 if (SYMBOL_REF_DECL (symbol
)
2530 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2532 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2534 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2535 == BUILT_IN_MEMSET
))
2536 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2537 memset_call
= SYMBOL_REF_DECL (symbol
);
2541 if (const_call
|| memset_call
)
2543 insn_info_t i_ptr
= active_local_stores
;
2544 insn_info_t last
= NULL
;
2546 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2547 fprintf (dump_file
, "%s call %d\n",
2548 const_call
? "const" : "memset", INSN_UID (insn
));
2550 /* See the head comment of the frame_read field. */
2551 if (reload_completed
2552 /* Tail calls are storing their arguments using
2553 arg pointer. If it is a frame pointer on the target,
2554 even before reload we need to kill frame pointer based
2556 || (SIBLING_CALL_P (insn
)
2557 && HARD_FRAME_POINTER_IS_ARG_POINTER
))
2558 insn_info
->frame_read
= true;
2560 /* Loop over the active stores and remove those which are
2561 killed by the const function call. */
2564 bool remove_store
= false;
2566 /* The stack pointer based stores are always killed. */
2567 if (i_ptr
->stack_pointer_based
)
2568 remove_store
= true;
2570 /* If the frame is read, the frame related stores are killed. */
2571 else if (insn_info
->frame_read
)
2573 store_info_t store_info
= i_ptr
->store_rec
;
2575 /* Skip the clobbers. */
2576 while (!store_info
->is_set
)
2577 store_info
= store_info
->next
;
2579 if (store_info
->group_id
>= 0
2580 && rtx_group_vec
[store_info
->group_id
]->frame_related
)
2581 remove_store
= true;
2586 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2587 dump_insn_info ("removing from active", i_ptr
);
2589 active_local_stores_len
--;
2591 last
->next_local_store
= i_ptr
->next_local_store
;
2593 active_local_stores
= i_ptr
->next_local_store
;
2598 i_ptr
= i_ptr
->next_local_store
;
2604 if (get_call_args (insn
, memset_call
, args
, 3)
2605 && CONST_INT_P (args
[1])
2606 && CONST_INT_P (args
[2])
2607 && INTVAL (args
[2]) > 0)
2609 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2610 set_mem_size (mem
, INTVAL (args
[2]));
2611 body
= gen_rtx_SET (VOIDmode
, mem
, args
[1]);
2612 mems_found
+= record_store (body
, bb_info
);
2613 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2614 fprintf (dump_file
, "handling memset as BLKmode store\n");
2615 if (mems_found
== 1)
2617 if (active_local_stores_len
++
2618 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2620 active_local_stores_len
= 1;
2621 active_local_stores
= NULL
;
2623 insn_info
->fixed_regs_live
2624 = copy_fixed_regs (bb_info
->regs_live
);
2625 insn_info
->next_local_store
= active_local_stores
;
2626 active_local_stores
= insn_info
;
2631 else if (SIBLING_CALL_P (insn
) && reload_completed
)
2632 /* Arguments for a sibling call that are pushed to memory are passed
2633 using the incoming argument pointer of the current function. After
2634 reload that might be (and likely is) frame pointer based. */
2635 add_wild_read (bb_info
);
2637 /* Every other call, including pure functions, may read any memory
2638 that is not relative to the frame. */
2639 add_non_frame_wild_read (bb_info
);
2644 /* Assuming that there are sets in these insns, we cannot delete
2646 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2647 || volatile_refs_p (PATTERN (insn
))
2648 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2649 || (RTX_FRAME_RELATED_P (insn
))
2650 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2651 insn_info
->cannot_delete
= true;
2653 body
= PATTERN (insn
);
2654 if (GET_CODE (body
) == PARALLEL
)
2657 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2658 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2661 mems_found
+= record_store (body
, bb_info
);
2663 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2664 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2665 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2667 /* If we found some sets of mems, add it into the active_local_stores so
2668 that it can be locally deleted if found dead or used for
2669 replace_read and redundant constant store elimination. Otherwise mark
2670 it as cannot delete. This simplifies the processing later. */
2671 if (mems_found
== 1)
2673 if (active_local_stores_len
++
2674 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2676 active_local_stores_len
= 1;
2677 active_local_stores
= NULL
;
2679 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2680 insn_info
->next_local_store
= active_local_stores
;
2681 active_local_stores
= insn_info
;
2684 insn_info
->cannot_delete
= true;
2688 /* Remove BASE from the set of active_local_stores. This is a
2689 callback from cselib that is used to get rid of the stores in
2690 active_local_stores. */
2693 remove_useless_values (cselib_val
*base
)
2695 insn_info_t insn_info
= active_local_stores
;
2696 insn_info_t last
= NULL
;
2700 store_info_t store_info
= insn_info
->store_rec
;
2703 /* If ANY of the store_infos match the cselib group that is
2704 being deleted, then the insn can not be deleted. */
2707 if ((store_info
->group_id
== -1)
2708 && (store_info
->cse_base
== base
))
2713 store_info
= store_info
->next
;
2718 active_local_stores_len
--;
2720 last
->next_local_store
= insn_info
->next_local_store
;
2722 active_local_stores
= insn_info
->next_local_store
;
2723 free_store_info (insn_info
);
2728 insn_info
= insn_info
->next_local_store
;
2733 /* Do all of step 1. */
2739 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2742 all_blocks
= BITMAP_ALLOC (NULL
);
2743 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2744 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2746 FOR_ALL_BB_FN (bb
, cfun
)
2749 bb_info_t bb_info
= (bb_info_t
) pool_alloc (bb_info_pool
);
2751 memset (bb_info
, 0, sizeof (struct dse_bb_info
));
2752 bitmap_set_bit (all_blocks
, bb
->index
);
2753 bb_info
->regs_live
= regs_live
;
2755 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2756 df_simulate_initialize_forwards (bb
, regs_live
);
2758 bb_table
[bb
->index
] = bb_info
;
2759 cselib_discard_hook
= remove_useless_values
;
2761 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2766 = create_alloc_pool ("cse_store_info_pool",
2767 sizeof (struct store_info
), 100);
2768 active_local_stores
= NULL
;
2769 active_local_stores_len
= 0;
2770 cselib_clear_table ();
2772 /* Scan the insns. */
2773 FOR_BB_INSNS (bb
, insn
)
2776 scan_insn (bb_info
, insn
);
2777 cselib_process_insn (insn
);
2779 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2782 /* This is something of a hack, because the global algorithm
2783 is supposed to take care of the case where stores go dead
2784 at the end of the function. However, the global
2785 algorithm must take a more conservative view of block
2786 mode reads than the local alg does. So to get the case
2787 where you have a store to the frame followed by a non
2788 overlapping block more read, we look at the active local
2789 stores at the end of the function and delete all of the
2790 frame and spill based ones. */
2791 if (stores_off_frame_dead_at_return
2792 && (EDGE_COUNT (bb
->succs
) == 0
2793 || (single_succ_p (bb
)
2794 && single_succ (bb
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
2795 && ! crtl
->calls_eh_return
)))
2797 insn_info_t i_ptr
= active_local_stores
;
2800 store_info_t store_info
= i_ptr
->store_rec
;
2802 /* Skip the clobbers. */
2803 while (!store_info
->is_set
)
2804 store_info
= store_info
->next
;
2805 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2806 delete_dead_store_insn (i_ptr
);
2808 if (store_info
->group_id
>= 0)
2811 = rtx_group_vec
[store_info
->group_id
];
2812 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2813 delete_dead_store_insn (i_ptr
);
2816 i_ptr
= i_ptr
->next_local_store
;
2820 /* Get rid of the loads that were discovered in
2821 replace_read. Cselib is finished with this block. */
2822 while (deferred_change_list
)
2824 deferred_change_t next
= deferred_change_list
->next
;
2826 /* There is no reason to validate this change. That was
2828 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2829 pool_free (deferred_change_pool
, deferred_change_list
);
2830 deferred_change_list
= next
;
2833 /* Get rid of all of the cselib based store_infos in this
2834 block and mark the containing insns as not being
2836 ptr
= bb_info
->last_insn
;
2839 if (ptr
->contains_cselib_groups
)
2841 store_info_t s_info
= ptr
->store_rec
;
2842 while (s_info
&& !s_info
->is_set
)
2843 s_info
= s_info
->next
;
2845 && s_info
->redundant_reason
2846 && s_info
->redundant_reason
->insn
2847 && !ptr
->cannot_delete
)
2849 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2850 fprintf (dump_file
, "Locally deleting insn %d "
2851 "because insn %d stores the "
2852 "same value and couldn't be "
2854 INSN_UID (ptr
->insn
),
2855 INSN_UID (s_info
->redundant_reason
->insn
));
2856 delete_dead_store_insn (ptr
);
2858 free_store_info (ptr
);
2862 store_info_t s_info
;
2864 /* Free at least positions_needed bitmaps. */
2865 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2866 if (s_info
->is_large
)
2868 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2869 s_info
->is_large
= false;
2872 ptr
= ptr
->prev_insn
;
2875 free_alloc_pool (cse_store_info_pool
);
2877 bb_info
->regs_live
= NULL
;
2880 BITMAP_FREE (regs_live
);
2882 rtx_group_table
->empty ();
2886 /*----------------------------------------------------------------------------
2889 Assign each byte position in the stores that we are going to
2890 analyze globally to a position in the bitmaps. Returns true if
2891 there are any bit positions assigned.
2892 ----------------------------------------------------------------------------*/
2895 dse_step2_init (void)
2900 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2902 /* For all non stack related bases, we only consider a store to
2903 be deletable if there are two or more stores for that
2904 position. This is because it takes one store to make the
2905 other store redundant. However, for the stores that are
2906 stack related, we consider them if there is only one store
2907 for the position. We do this because the stack related
2908 stores can be deleted if their is no read between them and
2909 the end of the function.
2911 To make this work in the current framework, we take the stack
2912 related bases add all of the bits from store1 into store2.
2913 This has the effect of making the eligible even if there is
2916 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2918 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2919 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2920 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2921 fprintf (dump_file
, "group %d is frame related ", i
);
2924 group
->offset_map_size_n
++;
2925 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2926 group
->offset_map_size_n
);
2927 group
->offset_map_size_p
++;
2928 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2929 group
->offset_map_size_p
);
2930 group
->process_globally
= false;
2931 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2933 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2934 (int)bitmap_count_bits (group
->store2_n
),
2935 (int)bitmap_count_bits (group
->store2_p
));
2936 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2937 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2943 /* Init the offset tables for the normal case. */
2946 dse_step2_nospill (void)
2950 /* Position 0 is unused because 0 is used in the maps to mean
2952 current_position
= 1;
2953 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2958 if (group
== clear_alias_group
)
2961 memset (group
->offset_map_n
, 0, sizeof (int) * group
->offset_map_size_n
);
2962 memset (group
->offset_map_p
, 0, sizeof (int) * group
->offset_map_size_p
);
2963 bitmap_clear (group
->group_kill
);
2965 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2967 bitmap_set_bit (group
->group_kill
, current_position
);
2968 if (bitmap_bit_p (group
->escaped_n
, j
))
2969 bitmap_set_bit (kill_on_calls
, current_position
);
2970 group
->offset_map_n
[j
] = current_position
++;
2971 group
->process_globally
= true;
2973 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2975 bitmap_set_bit (group
->group_kill
, current_position
);
2976 if (bitmap_bit_p (group
->escaped_p
, j
))
2977 bitmap_set_bit (kill_on_calls
, current_position
);
2978 group
->offset_map_p
[j
] = current_position
++;
2979 group
->process_globally
= true;
2982 return current_position
!= 1;
2987 /*----------------------------------------------------------------------------
2990 Build the bit vectors for the transfer functions.
2991 ----------------------------------------------------------------------------*/
2994 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2998 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
3002 HOST_WIDE_INT offset_p
= -offset
;
3003 if (offset_p
>= group_info
->offset_map_size_n
)
3005 return group_info
->offset_map_n
[offset_p
];
3009 if (offset
>= group_info
->offset_map_size_p
)
3011 return group_info
->offset_map_p
[offset
];
3016 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3020 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3025 group_info_t group_info
3026 = rtx_group_vec
[store_info
->group_id
];
3027 if (group_info
->process_globally
)
3028 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3030 int index
= get_bitmap_index (group_info
, i
);
3033 bitmap_set_bit (gen
, index
);
3035 bitmap_clear_bit (kill
, index
);
3038 store_info
= store_info
->next
;
3043 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3047 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3051 if (store_info
->alias_set
)
3053 int index
= get_bitmap_index (clear_alias_group
,
3054 store_info
->alias_set
);
3057 bitmap_set_bit (gen
, index
);
3059 bitmap_clear_bit (kill
, index
);
3062 store_info
= store_info
->next
;
3067 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3071 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3073 read_info_t read_info
= insn_info
->read_rec
;
3077 /* If this insn reads the frame, kill all the frame related stores. */
3078 if (insn_info
->frame_read
)
3080 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3081 if (group
->process_globally
&& group
->frame_related
)
3084 bitmap_ior_into (kill
, group
->group_kill
);
3085 bitmap_and_compl_into (gen
, group
->group_kill
);
3088 if (insn_info
->non_frame_wild_read
)
3090 /* Kill all non-frame related stores. Kill all stores of variables that
3093 bitmap_ior_into (kill
, kill_on_calls
);
3094 bitmap_and_compl_into (gen
, kill_on_calls
);
3095 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3096 if (group
->process_globally
&& !group
->frame_related
)
3099 bitmap_ior_into (kill
, group
->group_kill
);
3100 bitmap_and_compl_into (gen
, group
->group_kill
);
3105 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3107 if (group
->process_globally
)
3109 if (i
== read_info
->group_id
)
3111 if (read_info
->begin
> read_info
->end
)
3113 /* Begin > end for block mode reads. */
3115 bitmap_ior_into (kill
, group
->group_kill
);
3116 bitmap_and_compl_into (gen
, group
->group_kill
);
3120 /* The groups are the same, just process the
3123 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3125 int index
= get_bitmap_index (group
, j
);
3129 bitmap_set_bit (kill
, index
);
3130 bitmap_clear_bit (gen
, index
);
3137 /* The groups are different, if the alias sets
3138 conflict, clear the entire group. We only need
3139 to apply this test if the read_info is a cselib
3140 read. Anything with a constant base cannot alias
3141 something else with a different constant
3143 if ((read_info
->group_id
< 0)
3144 && canon_true_dependence (group
->base_mem
,
3145 GET_MODE (group
->base_mem
),
3146 group
->canon_base_addr
,
3147 read_info
->mem
, NULL_RTX
))
3150 bitmap_ior_into (kill
, group
->group_kill
);
3151 bitmap_and_compl_into (gen
, group
->group_kill
);
3157 read_info
= read_info
->next
;
3161 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3165 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3169 if (read_info
->alias_set
)
3171 int index
= get_bitmap_index (clear_alias_group
,
3172 read_info
->alias_set
);
3176 bitmap_set_bit (kill
, index
);
3177 bitmap_clear_bit (gen
, index
);
3181 read_info
= read_info
->next
;
3186 /* Return the insn in BB_INFO before the first wild read or if there
3187 are no wild reads in the block, return the last insn. */
3190 find_insn_before_first_wild_read (bb_info_t bb_info
)
3192 insn_info_t insn_info
= bb_info
->last_insn
;
3193 insn_info_t last_wild_read
= NULL
;
3197 if (insn_info
->wild_read
)
3199 last_wild_read
= insn_info
->prev_insn
;
3200 /* Block starts with wild read. */
3201 if (!last_wild_read
)
3205 insn_info
= insn_info
->prev_insn
;
3209 return last_wild_read
;
3211 return bb_info
->last_insn
;
3215 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3216 the block in order to build the gen and kill sets for the block.
3217 We start at ptr which may be the last insn in the block or may be
3218 the first insn with a wild read. In the latter case we are able to
3219 skip the rest of the block because it just does not matter:
3220 anything that happens is hidden by the wild read. */
3223 dse_step3_scan (bool for_spills
, basic_block bb
)
3225 bb_info_t bb_info
= bb_table
[bb
->index
];
3226 insn_info_t insn_info
;
3229 /* There are no wild reads in the spill case. */
3230 insn_info
= bb_info
->last_insn
;
3232 insn_info
= find_insn_before_first_wild_read (bb_info
);
3234 /* In the spill case or in the no_spill case if there is no wild
3235 read in the block, we will need a kill set. */
3236 if (insn_info
== bb_info
->last_insn
)
3239 bitmap_clear (bb_info
->kill
);
3241 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3245 BITMAP_FREE (bb_info
->kill
);
3249 /* There may have been code deleted by the dce pass run before
3251 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3253 /* Process the read(s) last. */
3256 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3257 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3261 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3262 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3266 insn_info
= insn_info
->prev_insn
;
3271 /* Set the gen set of the exit block, and also any block with no
3272 successors that does not have a wild read. */
3275 dse_step3_exit_block_scan (bb_info_t bb_info
)
3277 /* The gen set is all 0's for the exit block except for the
3278 frame_pointer_group. */
3280 if (stores_off_frame_dead_at_return
)
3285 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3287 if (group
->process_globally
&& group
->frame_related
)
3288 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3294 /* Find all of the blocks that are not backwards reachable from the
3295 exit block or any block with no successors (BB). These are the
3296 infinite loops or infinite self loops. These blocks will still
3297 have their bits set in UNREACHABLE_BLOCKS. */
3300 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3305 if (bitmap_bit_p (unreachable_blocks
, bb
->index
))
3307 bitmap_clear_bit (unreachable_blocks
, bb
->index
);
3308 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3310 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3315 /* Build the transfer functions for the function. */
3318 dse_step3 (bool for_spills
)
3321 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
3322 sbitmap_iterator sbi
;
3323 bitmap all_ones
= NULL
;
3326 bitmap_ones (unreachable_blocks
);
3328 FOR_ALL_BB_FN (bb
, cfun
)
3330 bb_info_t bb_info
= bb_table
[bb
->index
];
3332 bitmap_clear (bb_info
->gen
);
3334 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3336 if (bb
->index
== ENTRY_BLOCK
)
3338 else if (bb
->index
== EXIT_BLOCK
)
3339 dse_step3_exit_block_scan (bb_info
);
3341 dse_step3_scan (for_spills
, bb
);
3342 if (EDGE_COUNT (bb
->succs
) == 0)
3343 mark_reachable_blocks (unreachable_blocks
, bb
);
3345 /* If this is the second time dataflow is run, delete the old
3348 BITMAP_FREE (bb_info
->in
);
3350 BITMAP_FREE (bb_info
->out
);
3353 /* For any block in an infinite loop, we must initialize the out set
3354 to all ones. This could be expensive, but almost never occurs in
3355 practice. However, it is common in regression tests. */
3356 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks
, 0, i
, sbi
)
3358 if (bitmap_bit_p (all_blocks
, i
))
3360 bb_info_t bb_info
= bb_table
[i
];
3366 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3367 FOR_EACH_VEC_ELT (rtx_group_vec
, j
, group
)
3368 bitmap_ior_into (all_ones
, group
->group_kill
);
3372 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3373 bitmap_copy (bb_info
->out
, all_ones
);
3379 BITMAP_FREE (all_ones
);
3380 sbitmap_free (unreachable_blocks
);
3385 /*----------------------------------------------------------------------------
3388 Solve the bitvector equations.
3389 ----------------------------------------------------------------------------*/
3392 /* Confluence function for blocks with no successors. Create an out
3393 set from the gen set of the exit block. This block logically has
3394 the exit block as a successor. */
3399 dse_confluence_0 (basic_block bb
)
3401 bb_info_t bb_info
= bb_table
[bb
->index
];
3403 if (bb
->index
== EXIT_BLOCK
)
3408 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3409 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3413 /* Propagate the information from the in set of the dest of E to the
3414 out set of the src of E. If the various in or out sets are not
3415 there, that means they are all ones. */
3418 dse_confluence_n (edge e
)
3420 bb_info_t src_info
= bb_table
[e
->src
->index
];
3421 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3426 bitmap_and_into (src_info
->out
, dest_info
->in
);
3429 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3430 bitmap_copy (src_info
->out
, dest_info
->in
);
3437 /* Propagate the info from the out to the in set of BB_INDEX's basic
3438 block. There are three cases:
3440 1) The block has no kill set. In this case the kill set is all
3441 ones. It does not matter what the out set of the block is, none of
3442 the info can reach the top. The only thing that reaches the top is
3443 the gen set and we just copy the set.
3445 2) There is a kill set but no out set and bb has successors. In
3446 this case we just return. Eventually an out set will be created and
3447 it is better to wait than to create a set of ones.
3449 3) There is both a kill and out set. We apply the obvious transfer
3454 dse_transfer_function (int bb_index
)
3456 bb_info_t bb_info
= bb_table
[bb_index
];
3464 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3465 bb_info
->out
, bb_info
->kill
);
3468 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3469 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3470 bb_info
->out
, bb_info
->kill
);
3480 /* Case 1 above. If there is already an in set, nothing
3486 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3487 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3493 /* Solve the dataflow equations. */
3498 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3499 dse_confluence_n
, dse_transfer_function
,
3500 all_blocks
, df_get_postorder (DF_BACKWARD
),
3501 df_get_n_blocks (DF_BACKWARD
));
3502 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3506 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3507 FOR_ALL_BB_FN (bb
, cfun
)
3509 bb_info_t bb_info
= bb_table
[bb
->index
];
3511 df_print_bb_index (bb
, dump_file
);
3513 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3515 fprintf (dump_file
, " in: *MISSING*\n");
3517 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3519 fprintf (dump_file
, " gen: *MISSING*\n");
3521 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3523 fprintf (dump_file
, " kill: *MISSING*\n");
3525 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3527 fprintf (dump_file
, " out: *MISSING*\n\n");
3534 /*----------------------------------------------------------------------------
3537 Delete the stores that can only be deleted using the global information.
3538 ----------------------------------------------------------------------------*/
3542 dse_step5_nospill (void)
3545 FOR_EACH_BB_FN (bb
, cfun
)
3547 bb_info_t bb_info
= bb_table
[bb
->index
];
3548 insn_info_t insn_info
= bb_info
->last_insn
;
3549 bitmap v
= bb_info
->out
;
3553 bool deleted
= false;
3554 if (dump_file
&& insn_info
->insn
)
3556 fprintf (dump_file
, "starting to process insn %d\n",
3557 INSN_UID (insn_info
->insn
));
3558 bitmap_print (dump_file
, v
, " v: ", "\n");
3561 /* There may have been code deleted by the dce pass run before
3564 && INSN_P (insn_info
->insn
)
3565 && (!insn_info
->cannot_delete
)
3566 && (!bitmap_empty_p (v
)))
3568 store_info_t store_info
= insn_info
->store_rec
;
3570 /* Try to delete the current insn. */
3573 /* Skip the clobbers. */
3574 while (!store_info
->is_set
)
3575 store_info
= store_info
->next
;
3577 if (store_info
->alias_set
)
3582 group_info_t group_info
3583 = rtx_group_vec
[store_info
->group_id
];
3585 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3587 int index
= get_bitmap_index (group_info
, i
);
3589 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3590 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3591 if (index
== 0 || !bitmap_bit_p (v
, index
))
3593 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3594 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3603 && check_for_inc_dec_1 (insn_info
))
3605 delete_insn (insn_info
->insn
);
3606 insn_info
->insn
= NULL
;
3611 /* We do want to process the local info if the insn was
3612 deleted. For instance, if the insn did a wild read, we
3613 no longer need to trash the info. */
3615 && INSN_P (insn_info
->insn
)
3618 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3619 if (insn_info
->wild_read
)
3621 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3622 fprintf (dump_file
, "wild read\n");
3625 else if (insn_info
->read_rec
3626 || insn_info
->non_frame_wild_read
)
3628 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3629 fprintf (dump_file
, "regular read\n");
3630 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3631 fprintf (dump_file
, "non-frame wild read\n");
3632 scan_reads_nospill (insn_info
, v
, NULL
);
3636 insn_info
= insn_info
->prev_insn
;
3643 /*----------------------------------------------------------------------------
3646 Delete stores made redundant by earlier stores (which store the same
3647 value) that couldn't be eliminated.
3648 ----------------------------------------------------------------------------*/
3655 FOR_ALL_BB_FN (bb
, cfun
)
3657 bb_info_t bb_info
= bb_table
[bb
->index
];
3658 insn_info_t insn_info
= bb_info
->last_insn
;
3662 /* There may have been code deleted by the dce pass run before
3665 && INSN_P (insn_info
->insn
)
3666 && !insn_info
->cannot_delete
)
3668 store_info_t s_info
= insn_info
->store_rec
;
3670 while (s_info
&& !s_info
->is_set
)
3671 s_info
= s_info
->next
;
3673 && s_info
->redundant_reason
3674 && s_info
->redundant_reason
->insn
3675 && INSN_P (s_info
->redundant_reason
->insn
))
3677 rtx_insn
*rinsn
= s_info
->redundant_reason
->insn
;
3678 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3679 fprintf (dump_file
, "Locally deleting insn %d "
3680 "because insn %d stores the "
3681 "same value and couldn't be "
3683 INSN_UID (insn_info
->insn
),
3685 delete_dead_store_insn (insn_info
);
3688 insn_info
= insn_info
->prev_insn
;
3693 /*----------------------------------------------------------------------------
3696 Destroy everything left standing.
3697 ----------------------------------------------------------------------------*/
3702 bitmap_obstack_release (&dse_bitmap_obstack
);
3703 obstack_free (&dse_obstack
, NULL
);
3705 end_alias_analysis ();
3707 delete rtx_group_table
;
3708 rtx_group_table
= NULL
;
3709 rtx_group_vec
.release ();
3710 BITMAP_FREE (all_blocks
);
3711 BITMAP_FREE (scratch
);
3713 free_alloc_pool (rtx_store_info_pool
);
3714 free_alloc_pool (read_info_pool
);
3715 free_alloc_pool (insn_info_pool
);
3716 free_alloc_pool (bb_info_pool
);
3717 free_alloc_pool (rtx_group_info_pool
);
3718 free_alloc_pool (deferred_change_pool
);
3722 /* -------------------------------------------------------------------------
3724 ------------------------------------------------------------------------- */
3726 /* Callback for running pass_rtl_dse. */
3729 rest_of_handle_dse (void)
3731 df_set_flags (DF_DEFER_INSN_RESCAN
);
3733 /* Need the notes since we must track live hardregs in the forwards
3735 df_note_add_problem ();
3741 if (dse_step2_nospill ())
3743 df_set_flags (DF_LR_RUN_DCE
);
3745 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3746 fprintf (dump_file
, "doing global processing\n");
3749 dse_step5_nospill ();
3756 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3757 locally_deleted
, globally_deleted
, spill_deleted
);
3759 /* DSE can eliminate potentially-trapping MEMs.
3760 Remove any EH edges associated with them. */
3761 if ((locally_deleted
|| globally_deleted
)
3762 && cfun
->can_throw_non_call_exceptions
3763 && purge_all_dead_edges ())
3771 const pass_data pass_data_rtl_dse1
=
3773 RTL_PASS
, /* type */
3775 OPTGROUP_NONE
, /* optinfo_flags */
3776 TV_DSE1
, /* tv_id */
3777 0, /* properties_required */
3778 0, /* properties_provided */
3779 0, /* properties_destroyed */
3780 0, /* todo_flags_start */
3781 TODO_df_finish
, /* todo_flags_finish */
3784 class pass_rtl_dse1
: public rtl_opt_pass
3787 pass_rtl_dse1 (gcc::context
*ctxt
)
3788 : rtl_opt_pass (pass_data_rtl_dse1
, ctxt
)
3791 /* opt_pass methods: */
3792 virtual bool gate (function
*)
3794 return optimize
> 0 && flag_dse
&& dbg_cnt (dse1
);
3797 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3799 }; // class pass_rtl_dse1
3804 make_pass_rtl_dse1 (gcc::context
*ctxt
)
3806 return new pass_rtl_dse1 (ctxt
);
3811 const pass_data pass_data_rtl_dse2
=
3813 RTL_PASS
, /* type */
3815 OPTGROUP_NONE
, /* optinfo_flags */
3816 TV_DSE2
, /* tv_id */
3817 0, /* properties_required */
3818 0, /* properties_provided */
3819 0, /* properties_destroyed */
3820 0, /* todo_flags_start */
3821 TODO_df_finish
, /* todo_flags_finish */
3824 class pass_rtl_dse2
: public rtl_opt_pass
3827 pass_rtl_dse2 (gcc::context
*ctxt
)
3828 : rtl_opt_pass (pass_data_rtl_dse2
, ctxt
)
3831 /* opt_pass methods: */
3832 virtual bool gate (function
*)
3834 return optimize
> 0 && flag_dse
&& dbg_cnt (dse2
);
3837 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3839 }; // class pass_rtl_dse2
3844 make_pass_rtl_dse2 (gcc::context
*ctxt
)
3846 return new pass_rtl_dse2 (ctxt
);