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 group_info
*);
673 static inline bool equal (const group_info
*, const group_info
*);
677 invariant_group_base_hasher::equal (const group_info
*gi1
,
678 const group_info
*gi2
)
680 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
684 invariant_group_base_hasher::hash (const group_info
*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
;
862 /* If this register is referenced by the current or an earlier insn,
863 that's OK. E.g. this applies to the register that is being incremented
864 with this addition. */
865 for (insn
= info
->first
;
866 insn
!= NEXT_INSN (info
->current
);
867 insn
= NEXT_INSN (insn
))
868 if (reg_referenced_p (loc
, PATTERN (insn
)))
871 /* If we come here, we have a clobber of a register that's only OK
872 if that register is not live. If we don't have liveness information
873 available, fail now. */
874 if (!info
->fixed_regs_live
)
876 info
->failure
= true;
879 /* Now check if this is a live fixed register. */
880 unsigned int end_regno
= END_REGNO (loc
);
881 for (unsigned int regno
= REGNO (loc
); regno
< end_regno
; ++regno
)
882 if (REGNO_REG_SET_P (info
->fixed_regs_live
, regno
))
883 info
->failure
= true;
886 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
887 SRC + SRCOFF before insn ARG. */
890 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
891 rtx op ATTRIBUTE_UNUSED
,
892 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
894 insn_info_t insn_info
= (insn_info_t
) arg
;
895 rtx_insn
*insn
= insn_info
->insn
, *new_insn
, *cur
;
896 note_add_store_info info
;
898 /* We can reuse all operands without copying, because we are about
899 to delete the insn that contained it. */
903 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
904 new_insn
= get_insns ();
908 new_insn
= as_a
<rtx_insn
*> (gen_move_insn (dest
, src
));
909 info
.first
= new_insn
;
910 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
911 info
.failure
= false;
912 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
915 note_stores (PATTERN (cur
), note_add_store
, &info
);
918 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
919 return it immediately, communicating the failure to its caller. */
923 emit_insn_before (new_insn
, insn
);
928 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
929 is there, is split into a separate insn.
930 Return true on success (or if there was nothing to do), false on failure. */
933 check_for_inc_dec_1 (insn_info_t insn_info
)
935 rtx_insn
*insn
= insn_info
->insn
;
936 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
938 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
944 /* Entry point for postreload. If you work on reload_cse, or you need this
945 anywhere else, consider if you can provide register liveness information
946 and add a parameter to this function so that it can be passed down in
947 insn_info.fixed_regs_live. */
949 check_for_inc_dec (rtx_insn
*insn
)
951 struct insn_info insn_info
;
954 insn_info
.insn
= insn
;
955 insn_info
.fixed_regs_live
= NULL
;
956 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
958 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
963 /* Delete the insn and free all of the fields inside INSN_INFO. */
966 delete_dead_store_insn (insn_info_t insn_info
)
968 read_info_t read_info
;
973 if (!check_for_inc_dec_1 (insn_info
))
975 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
977 fprintf (dump_file
, "Locally deleting insn %d ",
978 INSN_UID (insn_info
->insn
));
979 if (insn_info
->store_rec
->alias_set
)
980 fprintf (dump_file
, "alias set %d\n",
981 (int) insn_info
->store_rec
->alias_set
);
983 fprintf (dump_file
, "\n");
986 free_store_info (insn_info
);
987 read_info
= insn_info
->read_rec
;
991 read_info_t next
= read_info
->next
;
992 pool_free (read_info_pool
, read_info
);
995 insn_info
->read_rec
= NULL
;
997 delete_insn (insn_info
->insn
);
999 insn_info
->insn
= NULL
;
1001 insn_info
->wild_read
= false;
1004 /* Return whether DECL, a local variable, can possibly escape the current
1008 local_variable_can_escape (tree decl
)
1010 if (TREE_ADDRESSABLE (decl
))
1013 /* If this is a partitioned variable, we need to consider all the variables
1014 in the partition. This is necessary because a store into one of them can
1015 be replaced with a store into another and this may not change the outcome
1016 of the escape analysis. */
1017 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
1019 tree
*namep
= cfun
->gimple_df
->decls_to_pointers
->get (decl
);
1021 return TREE_ADDRESSABLE (*namep
);
1027 /* Return whether EXPR can possibly escape the current function scope. */
1030 can_escape (tree expr
)
1035 base
= get_base_address (expr
);
1037 && !may_be_aliased (base
)
1038 && !(TREE_CODE (base
) == VAR_DECL
1039 && !DECL_EXTERNAL (base
)
1040 && !TREE_STATIC (base
)
1041 && local_variable_can_escape (base
)))
1046 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1047 OFFSET and WIDTH. */
1050 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1054 bool expr_escapes
= can_escape (expr
);
1055 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1056 for (i
=offset
; i
<offset
+width
; i
++)
1064 store1
= group
->store1_n
;
1065 store2
= group
->store2_n
;
1066 escaped
= group
->escaped_n
;
1071 store1
= group
->store1_p
;
1072 store2
= group
->store2_p
;
1073 escaped
= group
->escaped_p
;
1077 if (!bitmap_set_bit (store1
, ai
))
1078 bitmap_set_bit (store2
, ai
);
1083 if (group
->offset_map_size_n
< ai
)
1084 group
->offset_map_size_n
= ai
;
1088 if (group
->offset_map_size_p
< ai
)
1089 group
->offset_map_size_p
= ai
;
1093 bitmap_set_bit (escaped
, ai
);
1098 reset_active_stores (void)
1100 active_local_stores
= NULL
;
1101 active_local_stores_len
= 0;
1104 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1107 free_read_records (bb_info_t bb_info
)
1109 insn_info_t insn_info
= bb_info
->last_insn
;
1110 read_info_t
*ptr
= &insn_info
->read_rec
;
1113 read_info_t next
= (*ptr
)->next
;
1114 if ((*ptr
)->alias_set
== 0)
1116 pool_free (read_info_pool
, *ptr
);
1120 ptr
= &(*ptr
)->next
;
1124 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1127 add_wild_read (bb_info_t bb_info
)
1129 insn_info_t insn_info
= bb_info
->last_insn
;
1130 insn_info
->wild_read
= true;
1131 free_read_records (bb_info
);
1132 reset_active_stores ();
1135 /* Set the BB_INFO so that the last insn is marked as a wild read of
1136 non-frame locations. */
1139 add_non_frame_wild_read (bb_info_t bb_info
)
1141 insn_info_t insn_info
= bb_info
->last_insn
;
1142 insn_info
->non_frame_wild_read
= true;
1143 free_read_records (bb_info
);
1144 reset_active_stores ();
1147 /* Return true if X is a constant or one of the registers that behave
1148 as a constant over the life of a function. This is equivalent to
1149 !rtx_varies_p for memory addresses. */
1152 const_or_frame_p (rtx x
)
1157 if (GET_CODE (x
) == REG
)
1159 /* Note that we have to test for the actual rtx used for the frame
1160 and arg pointers and not just the register number in case we have
1161 eliminated the frame and/or arg pointer and are using it
1163 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1164 /* The arg pointer varies if it is not a fixed register. */
1165 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1166 || x
== pic_offset_table_rtx
)
1174 /* Take all reasonable action to put the address of MEM into the form
1175 that we can do analysis on.
1177 The gold standard is to get the address into the form: address +
1178 OFFSET where address is something that rtx_varies_p considers a
1179 constant. When we can get the address in this form, we can do
1180 global analysis on it. Note that for constant bases, address is
1181 not actually returned, only the group_id. The address can be
1184 If that fails, we try cselib to get a value we can at least use
1185 locally. If that fails we return false.
1187 The GROUP_ID is set to -1 for cselib bases and the index of the
1188 group for non_varying bases.
1190 FOR_READ is true if this is a mem read and false if not. */
1193 canon_address (rtx mem
,
1194 alias_set_type
*alias_set_out
,
1196 HOST_WIDE_INT
*offset
,
1199 machine_mode address_mode
= get_address_mode (mem
);
1200 rtx mem_address
= XEXP (mem
, 0);
1201 rtx expanded_address
, address
;
1206 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1208 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1210 fprintf (dump_file
, " mem: ");
1211 print_inline_rtx (dump_file
, mem_address
, 0);
1212 fprintf (dump_file
, "\n");
1215 /* First see if just canon_rtx (mem_address) is const or frame,
1216 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1218 for (expanded
= 0; expanded
< 2; expanded
++)
1222 /* Use cselib to replace all of the reg references with the full
1223 expression. This will take care of the case where we have
1225 r_x = base + offset;
1230 val = *(base + offset); */
1232 expanded_address
= cselib_expand_value_rtx (mem_address
,
1235 /* If this fails, just go with the address from first
1237 if (!expanded_address
)
1241 expanded_address
= mem_address
;
1243 /* Split the address into canonical BASE + OFFSET terms. */
1244 address
= canon_rtx (expanded_address
);
1248 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1252 fprintf (dump_file
, "\n after cselib_expand address: ");
1253 print_inline_rtx (dump_file
, expanded_address
, 0);
1254 fprintf (dump_file
, "\n");
1257 fprintf (dump_file
, "\n after canon_rtx address: ");
1258 print_inline_rtx (dump_file
, address
, 0);
1259 fprintf (dump_file
, "\n");
1262 if (GET_CODE (address
) == CONST
)
1263 address
= XEXP (address
, 0);
1265 if (GET_CODE (address
) == PLUS
1266 && CONST_INT_P (XEXP (address
, 1)))
1268 *offset
= INTVAL (XEXP (address
, 1));
1269 address
= XEXP (address
, 0);
1272 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1273 && const_or_frame_p (address
))
1275 group_info_t group
= get_group_info (address
);
1277 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1278 fprintf (dump_file
, " gid=%d offset=%d \n",
1279 group
->id
, (int)*offset
);
1281 *group_id
= group
->id
;
1286 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1291 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1292 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1295 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1296 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1297 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1302 /* Clear the rhs field from the active_local_stores array. */
1305 clear_rhs_from_active_local_stores (void)
1307 insn_info_t ptr
= active_local_stores
;
1311 store_info_t store_info
= ptr
->store_rec
;
1312 /* Skip the clobbers. */
1313 while (!store_info
->is_set
)
1314 store_info
= store_info
->next
;
1316 store_info
->rhs
= NULL
;
1317 store_info
->const_rhs
= NULL
;
1319 ptr
= ptr
->next_local_store
;
1324 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1327 set_position_unneeded (store_info_t s_info
, int pos
)
1329 if (__builtin_expect (s_info
->is_large
, false))
1331 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1332 s_info
->positions_needed
.large
.count
++;
1335 s_info
->positions_needed
.small_bitmask
1336 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1339 /* Mark the whole store S_INFO as unneeded. */
1342 set_all_positions_unneeded (store_info_t s_info
)
1344 if (__builtin_expect (s_info
->is_large
, false))
1346 int pos
, end
= s_info
->end
- s_info
->begin
;
1347 for (pos
= 0; pos
< end
; pos
++)
1348 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1349 s_info
->positions_needed
.large
.count
= end
;
1352 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1355 /* Return TRUE if any bytes from S_INFO store are needed. */
1358 any_positions_needed_p (store_info_t s_info
)
1360 if (__builtin_expect (s_info
->is_large
, false))
1361 return (s_info
->positions_needed
.large
.count
1362 < s_info
->end
- s_info
->begin
);
1364 return (s_info
->positions_needed
.small_bitmask
1365 != (unsigned HOST_WIDE_INT
) 0);
1368 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1369 store are needed. */
1372 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1374 if (__builtin_expect (s_info
->is_large
, false))
1376 int end
= start
+ width
;
1378 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1384 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1385 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1390 static rtx
get_stored_val (store_info_t
, machine_mode
, HOST_WIDE_INT
,
1391 HOST_WIDE_INT
, basic_block
, bool);
1394 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1395 there is a candidate store, after adding it to the appropriate
1396 local store group if so. */
1399 record_store (rtx body
, bb_info_t bb_info
)
1401 rtx mem
, rhs
, const_rhs
, mem_addr
;
1402 HOST_WIDE_INT offset
= 0;
1403 HOST_WIDE_INT width
= 0;
1404 alias_set_type spill_alias_set
;
1405 insn_info_t insn_info
= bb_info
->last_insn
;
1406 store_info_t store_info
= NULL
;
1408 cselib_val
*base
= NULL
;
1409 insn_info_t ptr
, last
, redundant_reason
;
1410 bool store_is_unused
;
1412 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1415 mem
= SET_DEST (body
);
1417 /* If this is not used, then this cannot be used to keep the insn
1418 from being deleted. On the other hand, it does provide something
1419 that can be used to prove that another store is dead. */
1421 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1423 /* Check whether that value is a suitable memory location. */
1426 /* If the set or clobber is unused, then it does not effect our
1427 ability to get rid of the entire insn. */
1428 if (!store_is_unused
)
1429 insn_info
->cannot_delete
= true;
1433 /* At this point we know mem is a mem. */
1434 if (GET_MODE (mem
) == BLKmode
)
1436 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1438 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1439 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1440 add_wild_read (bb_info
);
1441 insn_info
->cannot_delete
= true;
1444 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1445 as memset (addr, 0, 36); */
1446 else if (!MEM_SIZE_KNOWN_P (mem
)
1447 || MEM_SIZE (mem
) <= 0
1448 || MEM_SIZE (mem
) > MAX_OFFSET
1449 || GET_CODE (body
) != SET
1450 || !CONST_INT_P (SET_SRC (body
)))
1452 if (!store_is_unused
)
1454 /* If the set or clobber is unused, then it does not effect our
1455 ability to get rid of the entire insn. */
1456 insn_info
->cannot_delete
= true;
1457 clear_rhs_from_active_local_stores ();
1463 /* We can still process a volatile mem, we just cannot delete it. */
1464 if (MEM_VOLATILE_P (mem
))
1465 insn_info
->cannot_delete
= true;
1467 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1469 clear_rhs_from_active_local_stores ();
1473 if (GET_MODE (mem
) == BLKmode
)
1474 width
= MEM_SIZE (mem
);
1476 width
= GET_MODE_SIZE (GET_MODE (mem
));
1478 if (spill_alias_set
)
1480 bitmap store1
= clear_alias_group
->store1_p
;
1481 bitmap store2
= clear_alias_group
->store2_p
;
1483 gcc_assert (GET_MODE (mem
) != BLKmode
);
1485 if (!bitmap_set_bit (store1
, spill_alias_set
))
1486 bitmap_set_bit (store2
, spill_alias_set
);
1488 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1489 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1491 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1493 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1494 fprintf (dump_file
, " processing spill store %d(%s)\n",
1495 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1497 else if (group_id
>= 0)
1499 /* In the restrictive case where the base is a constant or the
1500 frame pointer we can do global analysis. */
1503 = rtx_group_vec
[group_id
];
1504 tree expr
= MEM_EXPR (mem
);
1506 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1507 set_usage_bits (group
, offset
, width
, expr
);
1509 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1510 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1511 group_id
, (int)offset
, (int)(offset
+width
));
1515 if (may_be_sp_based_p (XEXP (mem
, 0)))
1516 insn_info
->stack_pointer_based
= true;
1517 insn_info
->contains_cselib_groups
= true;
1519 store_info
= (store_info_t
) pool_alloc (cse_store_info_pool
);
1522 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1523 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1524 (int)offset
, (int)(offset
+width
));
1527 const_rhs
= rhs
= NULL_RTX
;
1528 if (GET_CODE (body
) == SET
1529 /* No place to keep the value after ra. */
1530 && !reload_completed
1531 && (REG_P (SET_SRC (body
))
1532 || GET_CODE (SET_SRC (body
)) == SUBREG
1533 || CONSTANT_P (SET_SRC (body
)))
1534 && !MEM_VOLATILE_P (mem
)
1535 /* Sometimes the store and reload is used for truncation and
1537 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1539 rhs
= SET_SRC (body
);
1540 if (CONSTANT_P (rhs
))
1542 else if (body
== PATTERN (insn_info
->insn
))
1544 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1545 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1546 const_rhs
= XEXP (tem
, 0);
1548 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1550 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1552 if (tem
&& CONSTANT_P (tem
))
1557 /* Check to see if this stores causes some other stores to be
1559 ptr
= active_local_stores
;
1561 redundant_reason
= NULL
;
1562 mem
= canon_rtx (mem
);
1563 /* For alias_set != 0 canon_true_dependence should be never called. */
1564 if (spill_alias_set
)
1565 mem_addr
= NULL_RTX
;
1569 mem_addr
= base
->val_rtx
;
1573 = rtx_group_vec
[group_id
];
1574 mem_addr
= group
->canon_base_addr
;
1576 /* get_addr can only handle VALUE but cannot handle expr like:
1577 VALUE + OFFSET, so call get_addr to get original addr for
1578 mem_addr before plus_constant. */
1579 mem_addr
= get_addr (mem_addr
);
1581 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1586 insn_info_t next
= ptr
->next_local_store
;
1587 store_info_t s_info
= ptr
->store_rec
;
1590 /* Skip the clobbers. We delete the active insn if this insn
1591 shadows the set. To have been put on the active list, it
1592 has exactly on set. */
1593 while (!s_info
->is_set
)
1594 s_info
= s_info
->next
;
1596 if (s_info
->alias_set
!= spill_alias_set
)
1598 else if (s_info
->alias_set
)
1600 struct clear_alias_mode_holder
*entry
1601 = clear_alias_set_lookup (s_info
->alias_set
);
1602 /* Generally, spills cannot be processed if and of the
1603 references to the slot have a different mode. But if
1604 we are in the same block and mode is exactly the same
1605 between this store and one before in the same block,
1606 we can still delete it. */
1607 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1608 && (GET_MODE (mem
) == entry
->mode
))
1611 set_all_positions_unneeded (s_info
);
1613 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1614 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1615 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1617 else if ((s_info
->group_id
== group_id
)
1618 && (s_info
->cse_base
== base
))
1621 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1622 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1623 INSN_UID (ptr
->insn
), s_info
->group_id
,
1624 (int)s_info
->begin
, (int)s_info
->end
);
1626 /* Even if PTR won't be eliminated as unneeded, if both
1627 PTR and this insn store the same constant value, we might
1628 eliminate this insn instead. */
1629 if (s_info
->const_rhs
1631 && offset
>= s_info
->begin
1632 && offset
+ width
<= s_info
->end
1633 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1636 if (GET_MODE (mem
) == BLKmode
)
1638 if (GET_MODE (s_info
->mem
) == BLKmode
1639 && s_info
->const_rhs
== const_rhs
)
1640 redundant_reason
= ptr
;
1642 else if (s_info
->const_rhs
== const0_rtx
1643 && const_rhs
== const0_rtx
)
1644 redundant_reason
= ptr
;
1649 val
= get_stored_val (s_info
, GET_MODE (mem
),
1650 offset
, offset
+ width
,
1651 BLOCK_FOR_INSN (insn_info
->insn
),
1653 if (get_insns () != NULL
)
1656 if (val
&& rtx_equal_p (val
, const_rhs
))
1657 redundant_reason
= ptr
;
1661 for (i
= MAX (offset
, s_info
->begin
);
1662 i
< offset
+ width
&& i
< s_info
->end
;
1664 set_position_unneeded (s_info
, i
- s_info
->begin
);
1666 else if (s_info
->rhs
)
1667 /* Need to see if it is possible for this store to overwrite
1668 the value of store_info. If it is, set the rhs to NULL to
1669 keep it from being used to remove a load. */
1671 if (canon_true_dependence (s_info
->mem
,
1672 GET_MODE (s_info
->mem
),
1677 s_info
->const_rhs
= NULL
;
1681 /* An insn can be deleted if every position of every one of
1682 its s_infos is zero. */
1683 if (any_positions_needed_p (s_info
))
1688 insn_info_t insn_to_delete
= ptr
;
1690 active_local_stores_len
--;
1692 last
->next_local_store
= ptr
->next_local_store
;
1694 active_local_stores
= ptr
->next_local_store
;
1696 if (!insn_to_delete
->cannot_delete
)
1697 delete_dead_store_insn (insn_to_delete
);
1705 /* Finish filling in the store_info. */
1706 store_info
->next
= insn_info
->store_rec
;
1707 insn_info
->store_rec
= store_info
;
1708 store_info
->mem
= mem
;
1709 store_info
->alias_set
= spill_alias_set
;
1710 store_info
->mem_addr
= mem_addr
;
1711 store_info
->cse_base
= base
;
1712 if (width
> HOST_BITS_PER_WIDE_INT
)
1714 store_info
->is_large
= true;
1715 store_info
->positions_needed
.large
.count
= 0;
1716 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1720 store_info
->is_large
= false;
1721 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1723 store_info
->group_id
= group_id
;
1724 store_info
->begin
= offset
;
1725 store_info
->end
= offset
+ width
;
1726 store_info
->is_set
= GET_CODE (body
) == SET
;
1727 store_info
->rhs
= rhs
;
1728 store_info
->const_rhs
= const_rhs
;
1729 store_info
->redundant_reason
= redundant_reason
;
1731 /* If this is a clobber, we return 0. We will only be able to
1732 delete this insn if there is only one store USED store, but we
1733 can use the clobber to delete other stores earlier. */
1734 return store_info
->is_set
? 1 : 0;
1739 dump_insn_info (const char * start
, insn_info_t insn_info
)
1741 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1742 INSN_UID (insn_info
->insn
),
1743 insn_info
->store_rec
? "has store" : "naked");
1747 /* If the modes are different and the value's source and target do not
1748 line up, we need to extract the value from lower part of the rhs of
1749 the store, shift it, and then put it into a form that can be shoved
1750 into the read_insn. This function generates a right SHIFT of a
1751 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1752 shift sequence is returned or NULL if we failed to find a
1756 find_shift_sequence (int access_size
,
1757 store_info_t store_info
,
1758 machine_mode read_mode
,
1759 int shift
, bool speed
, bool require_cst
)
1761 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1762 machine_mode new_mode
;
1763 rtx read_reg
= NULL
;
1765 /* Some machines like the x86 have shift insns for each size of
1766 operand. Other machines like the ppc or the ia-64 may only have
1767 shift insns that shift values within 32 or 64 bit registers.
1768 This loop tries to find the smallest shift insn that will right
1769 justify the value we want to read but is available in one insn on
1772 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1774 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1775 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1777 rtx target
, new_reg
, new_lhs
;
1778 rtx_insn
*shift_seq
, *insn
;
1781 /* If a constant was stored into memory, try to simplify it here,
1782 otherwise the cost of the shift might preclude this optimization
1783 e.g. at -Os, even when no actual shift will be needed. */
1784 if (store_info
->const_rhs
)
1786 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1787 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1789 if (ret
&& CONSTANT_P (ret
))
1791 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1792 ret
, GEN_INT (shift
));
1793 if (ret
&& CONSTANT_P (ret
))
1795 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1796 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1797 if (ret
&& CONSTANT_P (ret
)
1798 && set_src_cost (ret
, speed
) <= COSTS_N_INSNS (1))
1807 /* Try a wider mode if truncating the store mode to NEW_MODE
1808 requires a real instruction. */
1809 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1810 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1813 /* Also try a wider mode if the necessary punning is either not
1814 desirable or not possible. */
1815 if (!CONSTANT_P (store_info
->rhs
)
1816 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1819 new_reg
= gen_reg_rtx (new_mode
);
1823 /* In theory we could also check for an ashr. Ian Taylor knows
1824 of one dsp where the cost of these two was not the same. But
1825 this really is a rare case anyway. */
1826 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1827 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1829 shift_seq
= get_insns ();
1832 if (target
!= new_reg
|| shift_seq
== NULL
)
1836 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1838 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1840 /* The computation up to here is essentially independent
1841 of the arguments and could be precomputed. It may
1842 not be worth doing so. We could precompute if
1843 worthwhile or at least cache the results. The result
1844 technically depends on both SHIFT and ACCESS_SIZE,
1845 but in practice the answer will depend only on ACCESS_SIZE. */
1847 if (cost
> COSTS_N_INSNS (1))
1850 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1851 copy_rtx (store_info
->rhs
));
1852 if (new_lhs
== NULL_RTX
)
1855 /* We found an acceptable shift. Generate a move to
1856 take the value from the store and put it into the
1857 shift pseudo, then shift it, then generate another
1858 move to put in into the target of the read. */
1859 emit_move_insn (new_reg
, new_lhs
);
1860 emit_insn (shift_seq
);
1861 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1869 /* Call back for note_stores to find the hard regs set or clobbered by
1870 insn. Data is a bitmap of the hardregs set so far. */
1873 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1875 bitmap regs_set
= (bitmap
) data
;
1878 && HARD_REGISTER_P (x
))
1880 unsigned int regno
= REGNO (x
);
1881 bitmap_set_range (regs_set
, regno
,
1882 hard_regno_nregs
[regno
][GET_MODE (x
)]);
1886 /* Helper function for replace_read and record_store.
1887 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1888 to one before READ_END bytes read in READ_MODE. Return NULL
1889 if not successful. If REQUIRE_CST is true, return always constant. */
1892 get_stored_val (store_info_t store_info
, machine_mode read_mode
,
1893 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1894 basic_block bb
, bool require_cst
)
1896 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1898 int access_size
; /* In bytes. */
1901 /* To get here the read is within the boundaries of the write so
1902 shift will never be negative. Start out with the shift being in
1904 if (store_mode
== BLKmode
)
1906 else if (BYTES_BIG_ENDIAN
)
1907 shift
= store_info
->end
- read_end
;
1909 shift
= read_begin
- store_info
->begin
;
1911 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1913 /* From now on it is bits. */
1914 shift
*= BITS_PER_UNIT
;
1917 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1918 optimize_bb_for_speed_p (bb
),
1920 else if (store_mode
== BLKmode
)
1922 /* The store is a memset (addr, const_val, const_size). */
1923 gcc_assert (CONST_INT_P (store_info
->rhs
));
1924 store_mode
= int_mode_for_mode (read_mode
);
1925 if (store_mode
== BLKmode
)
1926 read_reg
= NULL_RTX
;
1927 else if (store_info
->rhs
== const0_rtx
)
1928 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1929 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1930 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1931 read_reg
= NULL_RTX
;
1934 unsigned HOST_WIDE_INT c
1935 = INTVAL (store_info
->rhs
)
1936 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1937 int shift
= BITS_PER_UNIT
;
1938 while (shift
< HOST_BITS_PER_WIDE_INT
)
1943 read_reg
= gen_int_mode (c
, store_mode
);
1944 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1947 else if (store_info
->const_rhs
1949 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1950 read_reg
= extract_low_bits (read_mode
, store_mode
,
1951 copy_rtx (store_info
->const_rhs
));
1953 read_reg
= extract_low_bits (read_mode
, store_mode
,
1954 copy_rtx (store_info
->rhs
));
1955 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1956 read_reg
= NULL_RTX
;
1960 /* Take a sequence of:
1983 Depending on the alignment and the mode of the store and
1987 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1988 and READ_INSN are for the read. Return true if the replacement
1992 replace_read (store_info_t store_info
, insn_info_t store_insn
,
1993 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1996 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1997 machine_mode read_mode
= GET_MODE (read_info
->mem
);
1998 rtx_insn
*insns
, *this_insn
;
2005 /* Create a sequence of instructions to set up the read register.
2006 This sequence goes immediately before the store and its result
2007 is read by the load.
2009 We need to keep this in perspective. We are replacing a read
2010 with a sequence of insns, but the read will almost certainly be
2011 in cache, so it is not going to be an expensive one. Thus, we
2012 are not willing to do a multi insn shift or worse a subroutine
2013 call to get rid of the read. */
2014 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2015 fprintf (dump_file
, "trying to replace %smode load in insn %d"
2016 " from %smode store in insn %d\n",
2017 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
2018 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
2020 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
2021 read_reg
= get_stored_val (store_info
,
2022 read_mode
, read_info
->begin
, read_info
->end
,
2024 if (read_reg
== NULL_RTX
)
2027 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2028 fprintf (dump_file
, " -- could not extract bits of stored value\n");
2031 /* Force the value into a new register so that it won't be clobbered
2032 between the store and the load. */
2033 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
2034 insns
= get_insns ();
2037 if (insns
!= NULL_RTX
)
2039 /* Now we have to scan the set of new instructions to see if the
2040 sequence contains and sets of hardregs that happened to be
2041 live at this point. For instance, this can happen if one of
2042 the insns sets the CC and the CC happened to be live at that
2043 point. This does occasionally happen, see PR 37922. */
2044 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2046 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2047 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2049 bitmap_and_into (regs_set
, regs_live
);
2050 if (!bitmap_empty_p (regs_set
))
2052 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2055 "abandoning replacement because sequence clobbers live hardregs:");
2056 df_print_regset (dump_file
, regs_set
);
2059 BITMAP_FREE (regs_set
);
2062 BITMAP_FREE (regs_set
);
2065 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2067 deferred_change_t deferred_change
=
2068 (deferred_change_t
) pool_alloc (deferred_change_pool
);
2070 /* Insert this right before the store insn where it will be safe
2071 from later insns that might change it before the read. */
2072 emit_insn_before (insns
, store_insn
->insn
);
2074 /* And now for the kludge part: cselib croaks if you just
2075 return at this point. There are two reasons for this:
2077 1) Cselib has an idea of how many pseudos there are and
2078 that does not include the new ones we just added.
2080 2) Cselib does not know about the move insn we added
2081 above the store_info, and there is no way to tell it
2082 about it, because it has "moved on".
2084 Problem (1) is fixable with a certain amount of engineering.
2085 Problem (2) is requires starting the bb from scratch. This
2088 So we are just going to have to lie. The move/extraction
2089 insns are not really an issue, cselib did not see them. But
2090 the use of the new pseudo read_insn is a real problem because
2091 cselib has not scanned this insn. The way that we solve this
2092 problem is that we are just going to put the mem back for now
2093 and when we are finished with the block, we undo this. We
2094 keep a table of mems to get rid of. At the end of the basic
2095 block we can put them back. */
2097 *loc
= read_info
->mem
;
2098 deferred_change
->next
= deferred_change_list
;
2099 deferred_change_list
= deferred_change
;
2100 deferred_change
->loc
= loc
;
2101 deferred_change
->reg
= read_reg
;
2103 /* Get rid of the read_info, from the point of view of the
2104 rest of dse, play like this read never happened. */
2105 read_insn
->read_rec
= read_info
->next
;
2106 pool_free (read_info_pool
, read_info
);
2107 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2109 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2110 print_simple_rtl (dump_file
, read_reg
);
2111 fprintf (dump_file
, "\n");
2117 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2119 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2120 print_simple_rtl (dump_file
, read_reg
);
2121 fprintf (dump_file
, " led to an invalid instruction\n");
2127 /* Check the address of MEM *LOC and kill any appropriate stores that may
2131 check_mem_read_rtx (rtx
*loc
, bb_info_t bb_info
)
2133 rtx mem
= *loc
, mem_addr
;
2134 insn_info_t insn_info
;
2135 HOST_WIDE_INT offset
= 0;
2136 HOST_WIDE_INT width
= 0;
2137 alias_set_type spill_alias_set
= 0;
2138 cselib_val
*base
= NULL
;
2140 read_info_t read_info
;
2142 insn_info
= bb_info
->last_insn
;
2144 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2145 || (MEM_VOLATILE_P (mem
)))
2147 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2148 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2149 add_wild_read (bb_info
);
2150 insn_info
->cannot_delete
= true;
2154 /* If it is reading readonly mem, then there can be no conflict with
2156 if (MEM_READONLY_P (mem
))
2159 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2161 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2162 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2163 add_wild_read (bb_info
);
2167 if (GET_MODE (mem
) == BLKmode
)
2170 width
= GET_MODE_SIZE (GET_MODE (mem
));
2172 read_info
= (read_info_t
) pool_alloc (read_info_pool
);
2173 read_info
->group_id
= group_id
;
2174 read_info
->mem
= mem
;
2175 read_info
->alias_set
= spill_alias_set
;
2176 read_info
->begin
= offset
;
2177 read_info
->end
= offset
+ width
;
2178 read_info
->next
= insn_info
->read_rec
;
2179 insn_info
->read_rec
= read_info
;
2180 /* For alias_set != 0 canon_true_dependence should be never called. */
2181 if (spill_alias_set
)
2182 mem_addr
= NULL_RTX
;
2186 mem_addr
= base
->val_rtx
;
2190 = rtx_group_vec
[group_id
];
2191 mem_addr
= group
->canon_base_addr
;
2193 /* get_addr can only handle VALUE but cannot handle expr like:
2194 VALUE + OFFSET, so call get_addr to get original addr for
2195 mem_addr before plus_constant. */
2196 mem_addr
= get_addr (mem_addr
);
2198 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2201 /* We ignore the clobbers in store_info. The is mildly aggressive,
2202 but there really should not be a clobber followed by a read. */
2204 if (spill_alias_set
)
2206 insn_info_t i_ptr
= active_local_stores
;
2207 insn_info_t last
= NULL
;
2209 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2210 fprintf (dump_file
, " processing spill load %d\n",
2211 (int) spill_alias_set
);
2215 store_info_t store_info
= i_ptr
->store_rec
;
2217 /* Skip the clobbers. */
2218 while (!store_info
->is_set
)
2219 store_info
= store_info
->next
;
2221 if (store_info
->alias_set
== spill_alias_set
)
2223 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2224 dump_insn_info ("removing from active", i_ptr
);
2226 active_local_stores_len
--;
2228 last
->next_local_store
= i_ptr
->next_local_store
;
2230 active_local_stores
= i_ptr
->next_local_store
;
2234 i_ptr
= i_ptr
->next_local_store
;
2237 else if (group_id
>= 0)
2239 /* This is the restricted case where the base is a constant or
2240 the frame pointer and offset is a constant. */
2241 insn_info_t i_ptr
= active_local_stores
;
2242 insn_info_t last
= NULL
;
2244 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2247 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2250 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2251 group_id
, (int)offset
, (int)(offset
+width
));
2256 bool remove
= false;
2257 store_info_t store_info
= i_ptr
->store_rec
;
2259 /* Skip the clobbers. */
2260 while (!store_info
->is_set
)
2261 store_info
= store_info
->next
;
2263 /* There are three cases here. */
2264 if (store_info
->group_id
< 0)
2265 /* We have a cselib store followed by a read from a
2268 = canon_true_dependence (store_info
->mem
,
2269 GET_MODE (store_info
->mem
),
2270 store_info
->mem_addr
,
2273 else if (group_id
== store_info
->group_id
)
2275 /* This is a block mode load. We may get lucky and
2276 canon_true_dependence may save the day. */
2279 = canon_true_dependence (store_info
->mem
,
2280 GET_MODE (store_info
->mem
),
2281 store_info
->mem_addr
,
2284 /* If this read is just reading back something that we just
2285 stored, rewrite the read. */
2289 && offset
>= store_info
->begin
2290 && offset
+ width
<= store_info
->end
2291 && all_positions_needed_p (store_info
,
2292 offset
- store_info
->begin
,
2294 && replace_read (store_info
, i_ptr
, read_info
,
2295 insn_info
, loc
, bb_info
->regs_live
))
2298 /* The bases are the same, just see if the offsets
2300 if ((offset
< store_info
->end
)
2301 && (offset
+ width
> store_info
->begin
))
2307 The else case that is missing here is that the
2308 bases are constant but different. There is nothing
2309 to do here because there is no overlap. */
2313 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2314 dump_insn_info ("removing from active", i_ptr
);
2316 active_local_stores_len
--;
2318 last
->next_local_store
= i_ptr
->next_local_store
;
2320 active_local_stores
= i_ptr
->next_local_store
;
2324 i_ptr
= i_ptr
->next_local_store
;
2329 insn_info_t i_ptr
= active_local_stores
;
2330 insn_info_t last
= NULL
;
2331 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2333 fprintf (dump_file
, " processing cselib load mem:");
2334 print_inline_rtx (dump_file
, mem
, 0);
2335 fprintf (dump_file
, "\n");
2340 bool remove
= false;
2341 store_info_t store_info
= i_ptr
->store_rec
;
2343 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2344 fprintf (dump_file
, " processing cselib load against insn %d\n",
2345 INSN_UID (i_ptr
->insn
));
2347 /* Skip the clobbers. */
2348 while (!store_info
->is_set
)
2349 store_info
= store_info
->next
;
2351 /* If this read is just reading back something that we just
2352 stored, rewrite the read. */
2354 && store_info
->group_id
== -1
2355 && store_info
->cse_base
== base
2357 && offset
>= store_info
->begin
2358 && offset
+ width
<= store_info
->end
2359 && all_positions_needed_p (store_info
,
2360 offset
- store_info
->begin
, width
)
2361 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2362 bb_info
->regs_live
))
2365 if (!store_info
->alias_set
)
2366 remove
= canon_true_dependence (store_info
->mem
,
2367 GET_MODE (store_info
->mem
),
2368 store_info
->mem_addr
,
2373 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2374 dump_insn_info ("removing from active", i_ptr
);
2376 active_local_stores_len
--;
2378 last
->next_local_store
= i_ptr
->next_local_store
;
2380 active_local_stores
= i_ptr
->next_local_store
;
2384 i_ptr
= i_ptr
->next_local_store
;
2389 /* A note_uses callback in which DATA points the INSN_INFO for
2390 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2391 true for any part of *LOC. */
2394 check_mem_read_use (rtx
*loc
, void *data
)
2396 subrtx_ptr_iterator::array_type array
;
2397 FOR_EACH_SUBRTX_PTR (iter
, array
, loc
, NONCONST
)
2401 check_mem_read_rtx (loc
, (bb_info_t
) data
);
2406 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2407 So far it only handles arguments passed in registers. */
2410 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2412 CUMULATIVE_ARGS args_so_far_v
;
2413 cumulative_args_t args_so_far
;
2417 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2418 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2420 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2422 arg
!= void_list_node
&& idx
< nargs
;
2423 arg
= TREE_CHAIN (arg
), idx
++)
2425 machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2427 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2428 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2429 || GET_MODE_CLASS (mode
) != MODE_INT
)
2432 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2434 link
= XEXP (link
, 1))
2435 if (GET_CODE (XEXP (link
, 0)) == USE
)
2437 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2438 if (REG_P (args
[idx
])
2439 && REGNO (args
[idx
]) == REGNO (reg
)
2440 && (GET_MODE (args
[idx
]) == mode
2441 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2442 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2444 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2445 > GET_MODE_SIZE (mode
)))))
2451 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2452 if (GET_MODE (args
[idx
]) != mode
)
2454 if (!tmp
|| !CONST_INT_P (tmp
))
2456 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2461 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2463 if (arg
!= void_list_node
|| idx
!= nargs
)
2468 /* Return a bitmap of the fixed registers contained in IN. */
2471 copy_fixed_regs (const_bitmap in
)
2475 ret
= ALLOC_REG_SET (NULL
);
2476 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2480 /* Apply record_store to all candidate stores in INSN. Mark INSN
2481 if some part of it is not a candidate store and assigns to a
2482 non-register target. */
2485 scan_insn (bb_info_t bb_info
, rtx_insn
*insn
)
2488 insn_info_t insn_info
= (insn_info_t
) pool_alloc (insn_info_pool
);
2490 memset (insn_info
, 0, sizeof (struct insn_info
));
2492 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2493 fprintf (dump_file
, "\n**scanning insn=%d\n",
2496 insn_info
->prev_insn
= bb_info
->last_insn
;
2497 insn_info
->insn
= insn
;
2498 bb_info
->last_insn
= insn_info
;
2500 if (DEBUG_INSN_P (insn
))
2502 insn_info
->cannot_delete
= true;
2506 /* Look at all of the uses in the insn. */
2507 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2512 tree memset_call
= NULL_TREE
;
2514 insn_info
->cannot_delete
= true;
2516 /* Const functions cannot do anything bad i.e. read memory,
2517 however, they can read their parameters which may have
2518 been pushed onto the stack.
2519 memset and bzero don't read memory either. */
2520 const_call
= RTL_CONST_CALL_P (insn
);
2523 rtx call
= get_call_rtx_from (insn
);
2524 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2526 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2527 if (SYMBOL_REF_DECL (symbol
)
2528 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2530 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2532 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2533 == BUILT_IN_MEMSET
))
2534 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2535 memset_call
= SYMBOL_REF_DECL (symbol
);
2539 if (const_call
|| memset_call
)
2541 insn_info_t i_ptr
= active_local_stores
;
2542 insn_info_t last
= NULL
;
2544 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2545 fprintf (dump_file
, "%s call %d\n",
2546 const_call
? "const" : "memset", INSN_UID (insn
));
2548 /* See the head comment of the frame_read field. */
2549 if (reload_completed
2550 /* Tail calls are storing their arguments using
2551 arg pointer. If it is a frame pointer on the target,
2552 even before reload we need to kill frame pointer based
2554 || (SIBLING_CALL_P (insn
)
2555 && HARD_FRAME_POINTER_IS_ARG_POINTER
))
2556 insn_info
->frame_read
= true;
2558 /* Loop over the active stores and remove those which are
2559 killed by the const function call. */
2562 bool remove_store
= false;
2564 /* The stack pointer based stores are always killed. */
2565 if (i_ptr
->stack_pointer_based
)
2566 remove_store
= true;
2568 /* If the frame is read, the frame related stores are killed. */
2569 else if (insn_info
->frame_read
)
2571 store_info_t store_info
= i_ptr
->store_rec
;
2573 /* Skip the clobbers. */
2574 while (!store_info
->is_set
)
2575 store_info
= store_info
->next
;
2577 if (store_info
->group_id
>= 0
2578 && rtx_group_vec
[store_info
->group_id
]->frame_related
)
2579 remove_store
= true;
2584 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2585 dump_insn_info ("removing from active", i_ptr
);
2587 active_local_stores_len
--;
2589 last
->next_local_store
= i_ptr
->next_local_store
;
2591 active_local_stores
= i_ptr
->next_local_store
;
2596 i_ptr
= i_ptr
->next_local_store
;
2602 if (get_call_args (insn
, memset_call
, args
, 3)
2603 && CONST_INT_P (args
[1])
2604 && CONST_INT_P (args
[2])
2605 && INTVAL (args
[2]) > 0)
2607 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2608 set_mem_size (mem
, INTVAL (args
[2]));
2609 body
= gen_rtx_SET (mem
, args
[1]);
2610 mems_found
+= record_store (body
, bb_info
);
2611 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2612 fprintf (dump_file
, "handling memset as BLKmode store\n");
2613 if (mems_found
== 1)
2615 if (active_local_stores_len
++
2616 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2618 active_local_stores_len
= 1;
2619 active_local_stores
= NULL
;
2621 insn_info
->fixed_regs_live
2622 = copy_fixed_regs (bb_info
->regs_live
);
2623 insn_info
->next_local_store
= active_local_stores
;
2624 active_local_stores
= insn_info
;
2629 else if (SIBLING_CALL_P (insn
) && reload_completed
)
2630 /* Arguments for a sibling call that are pushed to memory are passed
2631 using the incoming argument pointer of the current function. After
2632 reload that might be (and likely is) frame pointer based. */
2633 add_wild_read (bb_info
);
2635 /* Every other call, including pure functions, may read any memory
2636 that is not relative to the frame. */
2637 add_non_frame_wild_read (bb_info
);
2642 /* Assuming that there are sets in these insns, we cannot delete
2644 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2645 || volatile_refs_p (PATTERN (insn
))
2646 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2647 || (RTX_FRAME_RELATED_P (insn
))
2648 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2649 insn_info
->cannot_delete
= true;
2651 body
= PATTERN (insn
);
2652 if (GET_CODE (body
) == PARALLEL
)
2655 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2656 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2659 mems_found
+= record_store (body
, bb_info
);
2661 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2662 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2663 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2665 /* If we found some sets of mems, add it into the active_local_stores so
2666 that it can be locally deleted if found dead or used for
2667 replace_read and redundant constant store elimination. Otherwise mark
2668 it as cannot delete. This simplifies the processing later. */
2669 if (mems_found
== 1)
2671 if (active_local_stores_len
++
2672 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2674 active_local_stores_len
= 1;
2675 active_local_stores
= NULL
;
2677 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2678 insn_info
->next_local_store
= active_local_stores
;
2679 active_local_stores
= insn_info
;
2682 insn_info
->cannot_delete
= true;
2686 /* Remove BASE from the set of active_local_stores. This is a
2687 callback from cselib that is used to get rid of the stores in
2688 active_local_stores. */
2691 remove_useless_values (cselib_val
*base
)
2693 insn_info_t insn_info
= active_local_stores
;
2694 insn_info_t last
= NULL
;
2698 store_info_t store_info
= insn_info
->store_rec
;
2701 /* If ANY of the store_infos match the cselib group that is
2702 being deleted, then the insn can not be deleted. */
2705 if ((store_info
->group_id
== -1)
2706 && (store_info
->cse_base
== base
))
2711 store_info
= store_info
->next
;
2716 active_local_stores_len
--;
2718 last
->next_local_store
= insn_info
->next_local_store
;
2720 active_local_stores
= insn_info
->next_local_store
;
2721 free_store_info (insn_info
);
2726 insn_info
= insn_info
->next_local_store
;
2731 /* Do all of step 1. */
2737 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2740 all_blocks
= BITMAP_ALLOC (NULL
);
2741 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2742 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2744 FOR_ALL_BB_FN (bb
, cfun
)
2747 bb_info_t bb_info
= (bb_info_t
) pool_alloc (bb_info_pool
);
2749 memset (bb_info
, 0, sizeof (struct dse_bb_info
));
2750 bitmap_set_bit (all_blocks
, bb
->index
);
2751 bb_info
->regs_live
= regs_live
;
2753 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2754 df_simulate_initialize_forwards (bb
, regs_live
);
2756 bb_table
[bb
->index
] = bb_info
;
2757 cselib_discard_hook
= remove_useless_values
;
2759 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2764 = create_alloc_pool ("cse_store_info_pool",
2765 sizeof (struct store_info
), 100);
2766 active_local_stores
= NULL
;
2767 active_local_stores_len
= 0;
2768 cselib_clear_table ();
2770 /* Scan the insns. */
2771 FOR_BB_INSNS (bb
, insn
)
2774 scan_insn (bb_info
, insn
);
2775 cselib_process_insn (insn
);
2777 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2780 /* This is something of a hack, because the global algorithm
2781 is supposed to take care of the case where stores go dead
2782 at the end of the function. However, the global
2783 algorithm must take a more conservative view of block
2784 mode reads than the local alg does. So to get the case
2785 where you have a store to the frame followed by a non
2786 overlapping block more read, we look at the active local
2787 stores at the end of the function and delete all of the
2788 frame and spill based ones. */
2789 if (stores_off_frame_dead_at_return
2790 && (EDGE_COUNT (bb
->succs
) == 0
2791 || (single_succ_p (bb
)
2792 && single_succ (bb
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
2793 && ! crtl
->calls_eh_return
)))
2795 insn_info_t i_ptr
= active_local_stores
;
2798 store_info_t store_info
= i_ptr
->store_rec
;
2800 /* Skip the clobbers. */
2801 while (!store_info
->is_set
)
2802 store_info
= store_info
->next
;
2803 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2804 delete_dead_store_insn (i_ptr
);
2806 if (store_info
->group_id
>= 0)
2809 = rtx_group_vec
[store_info
->group_id
];
2810 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2811 delete_dead_store_insn (i_ptr
);
2814 i_ptr
= i_ptr
->next_local_store
;
2818 /* Get rid of the loads that were discovered in
2819 replace_read. Cselib is finished with this block. */
2820 while (deferred_change_list
)
2822 deferred_change_t next
= deferred_change_list
->next
;
2824 /* There is no reason to validate this change. That was
2826 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2827 pool_free (deferred_change_pool
, deferred_change_list
);
2828 deferred_change_list
= next
;
2831 /* Get rid of all of the cselib based store_infos in this
2832 block and mark the containing insns as not being
2834 ptr
= bb_info
->last_insn
;
2837 if (ptr
->contains_cselib_groups
)
2839 store_info_t s_info
= ptr
->store_rec
;
2840 while (s_info
&& !s_info
->is_set
)
2841 s_info
= s_info
->next
;
2843 && s_info
->redundant_reason
2844 && s_info
->redundant_reason
->insn
2845 && !ptr
->cannot_delete
)
2847 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2848 fprintf (dump_file
, "Locally deleting insn %d "
2849 "because insn %d stores the "
2850 "same value and couldn't be "
2852 INSN_UID (ptr
->insn
),
2853 INSN_UID (s_info
->redundant_reason
->insn
));
2854 delete_dead_store_insn (ptr
);
2856 free_store_info (ptr
);
2860 store_info_t s_info
;
2862 /* Free at least positions_needed bitmaps. */
2863 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2864 if (s_info
->is_large
)
2866 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2867 s_info
->is_large
= false;
2870 ptr
= ptr
->prev_insn
;
2873 free_alloc_pool (cse_store_info_pool
);
2875 bb_info
->regs_live
= NULL
;
2878 BITMAP_FREE (regs_live
);
2880 rtx_group_table
->empty ();
2884 /*----------------------------------------------------------------------------
2887 Assign each byte position in the stores that we are going to
2888 analyze globally to a position in the bitmaps. Returns true if
2889 there are any bit positions assigned.
2890 ----------------------------------------------------------------------------*/
2893 dse_step2_init (void)
2898 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2900 /* For all non stack related bases, we only consider a store to
2901 be deletable if there are two or more stores for that
2902 position. This is because it takes one store to make the
2903 other store redundant. However, for the stores that are
2904 stack related, we consider them if there is only one store
2905 for the position. We do this because the stack related
2906 stores can be deleted if their is no read between them and
2907 the end of the function.
2909 To make this work in the current framework, we take the stack
2910 related bases add all of the bits from store1 into store2.
2911 This has the effect of making the eligible even if there is
2914 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2916 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2917 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2918 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2919 fprintf (dump_file
, "group %d is frame related ", i
);
2922 group
->offset_map_size_n
++;
2923 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2924 group
->offset_map_size_n
);
2925 group
->offset_map_size_p
++;
2926 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2927 group
->offset_map_size_p
);
2928 group
->process_globally
= false;
2929 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2931 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2932 (int)bitmap_count_bits (group
->store2_n
),
2933 (int)bitmap_count_bits (group
->store2_p
));
2934 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2935 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2941 /* Init the offset tables for the normal case. */
2944 dse_step2_nospill (void)
2948 /* Position 0 is unused because 0 is used in the maps to mean
2950 current_position
= 1;
2951 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2956 if (group
== clear_alias_group
)
2959 memset (group
->offset_map_n
, 0, sizeof (int) * group
->offset_map_size_n
);
2960 memset (group
->offset_map_p
, 0, sizeof (int) * group
->offset_map_size_p
);
2961 bitmap_clear (group
->group_kill
);
2963 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2965 bitmap_set_bit (group
->group_kill
, current_position
);
2966 if (bitmap_bit_p (group
->escaped_n
, j
))
2967 bitmap_set_bit (kill_on_calls
, current_position
);
2968 group
->offset_map_n
[j
] = current_position
++;
2969 group
->process_globally
= true;
2971 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2973 bitmap_set_bit (group
->group_kill
, current_position
);
2974 if (bitmap_bit_p (group
->escaped_p
, j
))
2975 bitmap_set_bit (kill_on_calls
, current_position
);
2976 group
->offset_map_p
[j
] = current_position
++;
2977 group
->process_globally
= true;
2980 return current_position
!= 1;
2985 /*----------------------------------------------------------------------------
2988 Build the bit vectors for the transfer functions.
2989 ----------------------------------------------------------------------------*/
2992 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2996 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
3000 HOST_WIDE_INT offset_p
= -offset
;
3001 if (offset_p
>= group_info
->offset_map_size_n
)
3003 return group_info
->offset_map_n
[offset_p
];
3007 if (offset
>= group_info
->offset_map_size_p
)
3009 return group_info
->offset_map_p
[offset
];
3014 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3018 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3023 group_info_t group_info
3024 = rtx_group_vec
[store_info
->group_id
];
3025 if (group_info
->process_globally
)
3026 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3028 int index
= get_bitmap_index (group_info
, i
);
3031 bitmap_set_bit (gen
, index
);
3033 bitmap_clear_bit (kill
, index
);
3036 store_info
= store_info
->next
;
3041 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3045 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3049 if (store_info
->alias_set
)
3051 int index
= get_bitmap_index (clear_alias_group
,
3052 store_info
->alias_set
);
3055 bitmap_set_bit (gen
, index
);
3057 bitmap_clear_bit (kill
, index
);
3060 store_info
= store_info
->next
;
3065 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3069 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3071 read_info_t read_info
= insn_info
->read_rec
;
3075 /* If this insn reads the frame, kill all the frame related stores. */
3076 if (insn_info
->frame_read
)
3078 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3079 if (group
->process_globally
&& group
->frame_related
)
3082 bitmap_ior_into (kill
, group
->group_kill
);
3083 bitmap_and_compl_into (gen
, group
->group_kill
);
3086 if (insn_info
->non_frame_wild_read
)
3088 /* Kill all non-frame related stores. Kill all stores of variables that
3091 bitmap_ior_into (kill
, kill_on_calls
);
3092 bitmap_and_compl_into (gen
, kill_on_calls
);
3093 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3094 if (group
->process_globally
&& !group
->frame_related
)
3097 bitmap_ior_into (kill
, group
->group_kill
);
3098 bitmap_and_compl_into (gen
, group
->group_kill
);
3103 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3105 if (group
->process_globally
)
3107 if (i
== read_info
->group_id
)
3109 if (read_info
->begin
> read_info
->end
)
3111 /* Begin > end for block mode reads. */
3113 bitmap_ior_into (kill
, group
->group_kill
);
3114 bitmap_and_compl_into (gen
, group
->group_kill
);
3118 /* The groups are the same, just process the
3121 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3123 int index
= get_bitmap_index (group
, j
);
3127 bitmap_set_bit (kill
, index
);
3128 bitmap_clear_bit (gen
, index
);
3135 /* The groups are different, if the alias sets
3136 conflict, clear the entire group. We only need
3137 to apply this test if the read_info is a cselib
3138 read. Anything with a constant base cannot alias
3139 something else with a different constant
3141 if ((read_info
->group_id
< 0)
3142 && canon_true_dependence (group
->base_mem
,
3143 GET_MODE (group
->base_mem
),
3144 group
->canon_base_addr
,
3145 read_info
->mem
, NULL_RTX
))
3148 bitmap_ior_into (kill
, group
->group_kill
);
3149 bitmap_and_compl_into (gen
, group
->group_kill
);
3155 read_info
= read_info
->next
;
3159 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3163 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3167 if (read_info
->alias_set
)
3169 int index
= get_bitmap_index (clear_alias_group
,
3170 read_info
->alias_set
);
3174 bitmap_set_bit (kill
, index
);
3175 bitmap_clear_bit (gen
, index
);
3179 read_info
= read_info
->next
;
3184 /* Return the insn in BB_INFO before the first wild read or if there
3185 are no wild reads in the block, return the last insn. */
3188 find_insn_before_first_wild_read (bb_info_t bb_info
)
3190 insn_info_t insn_info
= bb_info
->last_insn
;
3191 insn_info_t last_wild_read
= NULL
;
3195 if (insn_info
->wild_read
)
3197 last_wild_read
= insn_info
->prev_insn
;
3198 /* Block starts with wild read. */
3199 if (!last_wild_read
)
3203 insn_info
= insn_info
->prev_insn
;
3207 return last_wild_read
;
3209 return bb_info
->last_insn
;
3213 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3214 the block in order to build the gen and kill sets for the block.
3215 We start at ptr which may be the last insn in the block or may be
3216 the first insn with a wild read. In the latter case we are able to
3217 skip the rest of the block because it just does not matter:
3218 anything that happens is hidden by the wild read. */
3221 dse_step3_scan (bool for_spills
, basic_block bb
)
3223 bb_info_t bb_info
= bb_table
[bb
->index
];
3224 insn_info_t insn_info
;
3227 /* There are no wild reads in the spill case. */
3228 insn_info
= bb_info
->last_insn
;
3230 insn_info
= find_insn_before_first_wild_read (bb_info
);
3232 /* In the spill case or in the no_spill case if there is no wild
3233 read in the block, we will need a kill set. */
3234 if (insn_info
== bb_info
->last_insn
)
3237 bitmap_clear (bb_info
->kill
);
3239 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3243 BITMAP_FREE (bb_info
->kill
);
3247 /* There may have been code deleted by the dce pass run before
3249 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3251 /* Process the read(s) last. */
3254 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3255 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3259 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3260 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3264 insn_info
= insn_info
->prev_insn
;
3269 /* Set the gen set of the exit block, and also any block with no
3270 successors that does not have a wild read. */
3273 dse_step3_exit_block_scan (bb_info_t bb_info
)
3275 /* The gen set is all 0's for the exit block except for the
3276 frame_pointer_group. */
3278 if (stores_off_frame_dead_at_return
)
3283 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3285 if (group
->process_globally
&& group
->frame_related
)
3286 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3292 /* Find all of the blocks that are not backwards reachable from the
3293 exit block or any block with no successors (BB). These are the
3294 infinite loops or infinite self loops. These blocks will still
3295 have their bits set in UNREACHABLE_BLOCKS. */
3298 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3303 if (bitmap_bit_p (unreachable_blocks
, bb
->index
))
3305 bitmap_clear_bit (unreachable_blocks
, bb
->index
);
3306 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3308 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3313 /* Build the transfer functions for the function. */
3316 dse_step3 (bool for_spills
)
3319 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
3320 sbitmap_iterator sbi
;
3321 bitmap all_ones
= NULL
;
3324 bitmap_ones (unreachable_blocks
);
3326 FOR_ALL_BB_FN (bb
, cfun
)
3328 bb_info_t bb_info
= bb_table
[bb
->index
];
3330 bitmap_clear (bb_info
->gen
);
3332 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3334 if (bb
->index
== ENTRY_BLOCK
)
3336 else if (bb
->index
== EXIT_BLOCK
)
3337 dse_step3_exit_block_scan (bb_info
);
3339 dse_step3_scan (for_spills
, bb
);
3340 if (EDGE_COUNT (bb
->succs
) == 0)
3341 mark_reachable_blocks (unreachable_blocks
, bb
);
3343 /* If this is the second time dataflow is run, delete the old
3346 BITMAP_FREE (bb_info
->in
);
3348 BITMAP_FREE (bb_info
->out
);
3351 /* For any block in an infinite loop, we must initialize the out set
3352 to all ones. This could be expensive, but almost never occurs in
3353 practice. However, it is common in regression tests. */
3354 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks
, 0, i
, sbi
)
3356 if (bitmap_bit_p (all_blocks
, i
))
3358 bb_info_t bb_info
= bb_table
[i
];
3364 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3365 FOR_EACH_VEC_ELT (rtx_group_vec
, j
, group
)
3366 bitmap_ior_into (all_ones
, group
->group_kill
);
3370 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3371 bitmap_copy (bb_info
->out
, all_ones
);
3377 BITMAP_FREE (all_ones
);
3378 sbitmap_free (unreachable_blocks
);
3383 /*----------------------------------------------------------------------------
3386 Solve the bitvector equations.
3387 ----------------------------------------------------------------------------*/
3390 /* Confluence function for blocks with no successors. Create an out
3391 set from the gen set of the exit block. This block logically has
3392 the exit block as a successor. */
3397 dse_confluence_0 (basic_block bb
)
3399 bb_info_t bb_info
= bb_table
[bb
->index
];
3401 if (bb
->index
== EXIT_BLOCK
)
3406 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3407 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3411 /* Propagate the information from the in set of the dest of E to the
3412 out set of the src of E. If the various in or out sets are not
3413 there, that means they are all ones. */
3416 dse_confluence_n (edge e
)
3418 bb_info_t src_info
= bb_table
[e
->src
->index
];
3419 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3424 bitmap_and_into (src_info
->out
, dest_info
->in
);
3427 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3428 bitmap_copy (src_info
->out
, dest_info
->in
);
3435 /* Propagate the info from the out to the in set of BB_INDEX's basic
3436 block. There are three cases:
3438 1) The block has no kill set. In this case the kill set is all
3439 ones. It does not matter what the out set of the block is, none of
3440 the info can reach the top. The only thing that reaches the top is
3441 the gen set and we just copy the set.
3443 2) There is a kill set but no out set and bb has successors. In
3444 this case we just return. Eventually an out set will be created and
3445 it is better to wait than to create a set of ones.
3447 3) There is both a kill and out set. We apply the obvious transfer
3452 dse_transfer_function (int bb_index
)
3454 bb_info_t bb_info
= bb_table
[bb_index
];
3462 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3463 bb_info
->out
, bb_info
->kill
);
3466 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3467 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3468 bb_info
->out
, bb_info
->kill
);
3478 /* Case 1 above. If there is already an in set, nothing
3484 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3485 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3491 /* Solve the dataflow equations. */
3496 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3497 dse_confluence_n
, dse_transfer_function
,
3498 all_blocks
, df_get_postorder (DF_BACKWARD
),
3499 df_get_n_blocks (DF_BACKWARD
));
3500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3504 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3505 FOR_ALL_BB_FN (bb
, cfun
)
3507 bb_info_t bb_info
= bb_table
[bb
->index
];
3509 df_print_bb_index (bb
, dump_file
);
3511 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3513 fprintf (dump_file
, " in: *MISSING*\n");
3515 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3517 fprintf (dump_file
, " gen: *MISSING*\n");
3519 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3521 fprintf (dump_file
, " kill: *MISSING*\n");
3523 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3525 fprintf (dump_file
, " out: *MISSING*\n\n");
3532 /*----------------------------------------------------------------------------
3535 Delete the stores that can only be deleted using the global information.
3536 ----------------------------------------------------------------------------*/
3540 dse_step5_nospill (void)
3543 FOR_EACH_BB_FN (bb
, cfun
)
3545 bb_info_t bb_info
= bb_table
[bb
->index
];
3546 insn_info_t insn_info
= bb_info
->last_insn
;
3547 bitmap v
= bb_info
->out
;
3551 bool deleted
= false;
3552 if (dump_file
&& insn_info
->insn
)
3554 fprintf (dump_file
, "starting to process insn %d\n",
3555 INSN_UID (insn_info
->insn
));
3556 bitmap_print (dump_file
, v
, " v: ", "\n");
3559 /* There may have been code deleted by the dce pass run before
3562 && INSN_P (insn_info
->insn
)
3563 && (!insn_info
->cannot_delete
)
3564 && (!bitmap_empty_p (v
)))
3566 store_info_t store_info
= insn_info
->store_rec
;
3568 /* Try to delete the current insn. */
3571 /* Skip the clobbers. */
3572 while (!store_info
->is_set
)
3573 store_info
= store_info
->next
;
3575 if (store_info
->alias_set
)
3580 group_info_t group_info
3581 = rtx_group_vec
[store_info
->group_id
];
3583 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3585 int index
= get_bitmap_index (group_info
, i
);
3587 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3588 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3589 if (index
== 0 || !bitmap_bit_p (v
, index
))
3591 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3592 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3601 && check_for_inc_dec_1 (insn_info
))
3603 delete_insn (insn_info
->insn
);
3604 insn_info
->insn
= NULL
;
3609 /* We do want to process the local info if the insn was
3610 deleted. For instance, if the insn did a wild read, we
3611 no longer need to trash the info. */
3613 && INSN_P (insn_info
->insn
)
3616 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3617 if (insn_info
->wild_read
)
3619 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3620 fprintf (dump_file
, "wild read\n");
3623 else if (insn_info
->read_rec
3624 || insn_info
->non_frame_wild_read
)
3626 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3627 fprintf (dump_file
, "regular read\n");
3628 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3629 fprintf (dump_file
, "non-frame wild read\n");
3630 scan_reads_nospill (insn_info
, v
, NULL
);
3634 insn_info
= insn_info
->prev_insn
;
3641 /*----------------------------------------------------------------------------
3644 Delete stores made redundant by earlier stores (which store the same
3645 value) that couldn't be eliminated.
3646 ----------------------------------------------------------------------------*/
3653 FOR_ALL_BB_FN (bb
, cfun
)
3655 bb_info_t bb_info
= bb_table
[bb
->index
];
3656 insn_info_t insn_info
= bb_info
->last_insn
;
3660 /* There may have been code deleted by the dce pass run before
3663 && INSN_P (insn_info
->insn
)
3664 && !insn_info
->cannot_delete
)
3666 store_info_t s_info
= insn_info
->store_rec
;
3668 while (s_info
&& !s_info
->is_set
)
3669 s_info
= s_info
->next
;
3671 && s_info
->redundant_reason
3672 && s_info
->redundant_reason
->insn
3673 && INSN_P (s_info
->redundant_reason
->insn
))
3675 rtx_insn
*rinsn
= s_info
->redundant_reason
->insn
;
3676 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3677 fprintf (dump_file
, "Locally deleting insn %d "
3678 "because insn %d stores the "
3679 "same value and couldn't be "
3681 INSN_UID (insn_info
->insn
),
3683 delete_dead_store_insn (insn_info
);
3686 insn_info
= insn_info
->prev_insn
;
3691 /*----------------------------------------------------------------------------
3694 Destroy everything left standing.
3695 ----------------------------------------------------------------------------*/
3700 bitmap_obstack_release (&dse_bitmap_obstack
);
3701 obstack_free (&dse_obstack
, NULL
);
3703 end_alias_analysis ();
3705 delete rtx_group_table
;
3706 rtx_group_table
= NULL
;
3707 rtx_group_vec
.release ();
3708 BITMAP_FREE (all_blocks
);
3709 BITMAP_FREE (scratch
);
3711 free_alloc_pool (rtx_store_info_pool
);
3712 free_alloc_pool (read_info_pool
);
3713 free_alloc_pool (insn_info_pool
);
3714 free_alloc_pool (bb_info_pool
);
3715 free_alloc_pool (rtx_group_info_pool
);
3716 free_alloc_pool (deferred_change_pool
);
3720 /* -------------------------------------------------------------------------
3722 ------------------------------------------------------------------------- */
3724 /* Callback for running pass_rtl_dse. */
3727 rest_of_handle_dse (void)
3729 df_set_flags (DF_DEFER_INSN_RESCAN
);
3731 /* Need the notes since we must track live hardregs in the forwards
3733 df_note_add_problem ();
3739 if (dse_step2_nospill ())
3741 df_set_flags (DF_LR_RUN_DCE
);
3743 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3744 fprintf (dump_file
, "doing global processing\n");
3747 dse_step5_nospill ();
3754 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3755 locally_deleted
, globally_deleted
, spill_deleted
);
3757 /* DSE can eliminate potentially-trapping MEMs.
3758 Remove any EH edges associated with them. */
3759 if ((locally_deleted
|| globally_deleted
)
3760 && cfun
->can_throw_non_call_exceptions
3761 && purge_all_dead_edges ())
3769 const pass_data pass_data_rtl_dse1
=
3771 RTL_PASS
, /* type */
3773 OPTGROUP_NONE
, /* optinfo_flags */
3774 TV_DSE1
, /* tv_id */
3775 0, /* properties_required */
3776 0, /* properties_provided */
3777 0, /* properties_destroyed */
3778 0, /* todo_flags_start */
3779 TODO_df_finish
, /* todo_flags_finish */
3782 class pass_rtl_dse1
: public rtl_opt_pass
3785 pass_rtl_dse1 (gcc::context
*ctxt
)
3786 : rtl_opt_pass (pass_data_rtl_dse1
, ctxt
)
3789 /* opt_pass methods: */
3790 virtual bool gate (function
*)
3792 return optimize
> 0 && flag_dse
&& dbg_cnt (dse1
);
3795 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3797 }; // class pass_rtl_dse1
3802 make_pass_rtl_dse1 (gcc::context
*ctxt
)
3804 return new pass_rtl_dse1 (ctxt
);
3809 const pass_data pass_data_rtl_dse2
=
3811 RTL_PASS
, /* type */
3813 OPTGROUP_NONE
, /* optinfo_flags */
3814 TV_DSE2
, /* tv_id */
3815 0, /* properties_required */
3816 0, /* properties_provided */
3817 0, /* properties_destroyed */
3818 0, /* todo_flags_start */
3819 TODO_df_finish
, /* todo_flags_finish */
3822 class pass_rtl_dse2
: public rtl_opt_pass
3825 pass_rtl_dse2 (gcc::context
*ctxt
)
3826 : rtl_opt_pass (pass_data_rtl_dse2
, ctxt
)
3829 /* opt_pass methods: */
3830 virtual bool gate (function
*)
3832 return optimize
> 0 && flag_dse
&& dbg_cnt (dse2
);
3835 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3837 }; // class pass_rtl_dse2
3842 make_pass_rtl_dse2 (gcc::context
*ctxt
)
3844 return new pass_rtl_dse2 (ctxt
);