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"
35 #include "fold-const.h"
36 #include "stor-layout.h"
43 #include "tree-pass.h"
44 #include "alloc-pool.h"
45 #include "insn-config.h"
55 #include "insn-codes.h"
60 #include "internal-fn.h"
61 #include "gimple-ssa.h"
63 #include "cfgcleanup.h"
65 /* This file contains three techniques for performing Dead Store
68 * The first technique performs dse locally on any base address. It
69 is based on the cselib which is a local value numbering technique.
70 This technique is local to a basic block but deals with a fairly
73 * The second technique performs dse globally but is restricted to
74 base addresses that are either constant or are relative to the
77 * The third technique, (which is only done after register allocation)
78 processes the spill slots. This differs from the second
79 technique because it takes advantage of the fact that spilling is
80 completely free from the effects of aliasing.
82 Logically, dse is a backwards dataflow problem. A store can be
83 deleted if it if cannot be reached in the backward direction by any
84 use of the value being stored. However, the local technique uses a
85 forwards scan of the basic block because cselib requires that the
86 block be processed in that order.
88 The pass is logically broken into 7 steps:
92 1) The local algorithm, as well as scanning the insns for the two
95 2) Analysis to see if the global algs are necessary. In the case
96 of stores base on a constant address, there must be at least two
97 stores to that address, to make it possible to delete some of the
98 stores. In the case of stores off of the frame or spill related
99 stores, only one store to an address is necessary because those
100 stores die at the end of the function.
102 3) Set up the global dataflow equations based on processing the
103 info parsed in the first step.
105 4) Solve the dataflow equations.
107 5) Delete the insns that the global analysis has indicated are
110 6) Delete insns that store the same value as preceding store
111 where the earlier store couldn't be eliminated.
115 This step uses cselib and canon_rtx to build the largest expression
116 possible for each address. This pass is a forwards pass through
117 each basic block. From the point of view of the global technique,
118 the first pass could examine a block in either direction. The
119 forwards ordering is to accommodate cselib.
121 We make a simplifying assumption: addresses fall into four broad
124 1) base has rtx_varies_p == false, offset is constant.
125 2) base has rtx_varies_p == false, offset variable.
126 3) base has rtx_varies_p == true, offset constant.
127 4) base has rtx_varies_p == true, offset variable.
129 The local passes are able to process all 4 kinds of addresses. The
130 global pass only handles 1).
132 The global problem is formulated as follows:
134 A store, S1, to address A, where A is not relative to the stack
135 frame, can be eliminated if all paths from S1 to the end of the
136 function contain another store to A before a read to A.
138 If the address A is relative to the stack frame, a store S2 to A
139 can be eliminated if there are no paths from S2 that reach the
140 end of the function that read A before another store to A. In
141 this case S2 can be deleted if there are paths from S2 to the
142 end of the function that have no reads or writes to A. This
143 second case allows stores to the stack frame to be deleted that
144 would otherwise die when the function returns. This cannot be
145 done if stores_off_frame_dead_at_return is not true. See the doc
146 for that variable for when this variable is false.
148 The global problem is formulated as a backwards set union
149 dataflow problem where the stores are the gens and reads are the
150 kills. Set union problems are rare and require some special
151 handling given our representation of bitmaps. A straightforward
152 implementation requires a lot of bitmaps filled with 1s.
153 These are expensive and cumbersome in our bitmap formulation so
154 care has been taken to avoid large vectors filled with 1s. See
155 the comments in bb_info and in the dataflow confluence functions
158 There are two places for further enhancements to this algorithm:
160 1) The original dse which was embedded in a pass called flow also
161 did local address forwarding. For example in
166 flow would replace the right hand side of the second insn with a
167 reference to r100. Most of the information is available to add this
168 to this pass. It has not done it because it is a lot of work in
169 the case that either r100 is assigned to between the first and
170 second insn and/or the second insn is a load of part of the value
171 stored by the first insn.
173 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
174 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
175 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
176 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
178 2) The cleaning up of spill code is quite profitable. It currently
179 depends on reading tea leaves and chicken entrails left by reload.
180 This pass depends on reload creating a singleton alias set for each
181 spill slot and telling the next dse pass which of these alias sets
182 are the singletons. Rather than analyze the addresses of the
183 spills, dse's spill processing just does analysis of the loads and
184 stores that use those alias sets. There are three cases where this
187 a) Reload sometimes creates the slot for one mode of access, and
188 then inserts loads and/or stores for a smaller mode. In this
189 case, the current code just punts on the slot. The proper thing
190 to do is to back out and use one bit vector position for each
191 byte of the entity associated with the slot. This depends on
192 KNOWING that reload always generates the accesses for each of the
193 bytes in some canonical (read that easy to understand several
194 passes after reload happens) way.
196 b) Reload sometimes decides that spill slot it allocated was not
197 large enough for the mode and goes back and allocates more slots
198 with the same mode and alias set. The backout in this case is a
199 little more graceful than (a). In this case the slot is unmarked
200 as being a spill slot and if final address comes out to be based
201 off the frame pointer, the global algorithm handles this slot.
203 c) For any pass that may prespill, there is currently no
204 mechanism to tell the dse pass that the slot being used has the
205 special properties that reload uses. It may be that all that is
206 required is to have those passes make the same calls that reload
207 does, assuming that the alias sets can be manipulated in the same
210 /* There are limits to the size of constant offsets we model for the
211 global problem. There are certainly test cases, that exceed this
212 limit, however, it is unlikely that there are important programs
213 that really have constant offsets this size. */
214 #define MAX_OFFSET (64 * 1024)
216 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
217 on the default obstack because these bitmaps can grow quite large
218 (~2GB for the small (!) test case of PR54146) and we'll hold on to
219 all that memory until the end of the compiler run.
220 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
221 releasing the whole obstack. */
222 static bitmap_obstack dse_bitmap_obstack
;
224 /* Obstack for other data. As for above: Kinda nice to be able to
225 throw it all away at the end in one big sweep. */
226 static struct obstack dse_obstack
;
228 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
229 static bitmap scratch
= NULL
;
231 struct insn_info_type
;
233 /* This structure holds information about a candidate store. */
237 /* False means this is a clobber. */
240 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
243 /* The id of the mem group of the base address. If rtx_varies_p is
244 true, this is -1. Otherwise, it is the index into the group
248 /* This is the cselib value. */
249 cselib_val
*cse_base
;
251 /* This canonized mem. */
254 /* Canonized MEM address for use by canon_true_dependence. */
257 /* If this is non-zero, it is the alias set of a spill location. */
258 alias_set_type alias_set
;
260 /* The offset of the first and byte before the last byte associated
261 with the operation. */
262 HOST_WIDE_INT begin
, end
;
266 /* A bitmask as wide as the number of bytes in the word that
267 contains a 1 if the byte may be needed. The store is unused if
268 all of the bits are 0. This is used if IS_LARGE is false. */
269 unsigned HOST_WIDE_INT small_bitmask
;
273 /* A bitmap with one bit per byte. Cleared bit means the position
274 is needed. Used if IS_LARGE is false. */
277 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
278 equal to END - BEGIN, the whole store is unused. */
283 /* The next store info for this insn. */
284 struct store_info
*next
;
286 /* The right hand side of the store. This is used if there is a
287 subsequent reload of the mems address somewhere later in the
291 /* If rhs is or holds a constant, this contains that constant,
295 /* Set if this store stores the same constant value as REDUNDANT_REASON
296 insn stored. These aren't eliminated early, because doing that
297 might prevent the earlier larger store to be eliminated. */
298 struct insn_info_type
*redundant_reason
;
301 /* Return a bitmask with the first N low bits set. */
303 static unsigned HOST_WIDE_INT
304 lowpart_bitmask (int n
)
306 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
307 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
310 static object_allocator
<store_info
> cse_store_info_pool ("cse_store_info_pool");
312 static object_allocator
<store_info
> rtx_store_info_pool ("rtx_store_info_pool");
314 /* This structure holds information about a load. These are only
315 built for rtx bases. */
316 struct read_info_type
318 /* The id of the mem group of the base address. */
321 /* If this is non-zero, it is the alias set of a spill location. */
322 alias_set_type alias_set
;
324 /* The offset of the first and byte after the last byte associated
325 with the operation. If begin == end == 0, the read did not have
326 a constant offset. */
329 /* The mem being read. */
332 /* The next read_info for this insn. */
333 struct read_info_type
*next
;
335 typedef struct read_info_type
*read_info_t
;
337 static object_allocator
<read_info_type
> read_info_type_pool ("read_info_pool");
339 /* One of these records is created for each insn. */
341 struct insn_info_type
343 /* Set true if the insn contains a store but the insn itself cannot
344 be deleted. This is set if the insn is a parallel and there is
345 more than one non dead output or if the insn is in some way
349 /* This field is only used by the global algorithm. It is set true
350 if the insn contains any read of mem except for a (1). This is
351 also set if the insn is a call or has a clobber mem. If the insn
352 contains a wild read, the use_rec will be null. */
355 /* This is true only for CALL instructions which could potentially read
356 any non-frame memory location. This field is used by the global
358 bool non_frame_wild_read
;
360 /* This field is only used for the processing of const functions.
361 These functions cannot read memory, but they can read the stack
362 because that is where they may get their parms. We need to be
363 this conservative because, like the store motion pass, we don't
364 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
365 Moreover, we need to distinguish two cases:
366 1. Before reload (register elimination), the stores related to
367 outgoing arguments are stack pointer based and thus deemed
368 of non-constant base in this pass. This requires special
369 handling but also means that the frame pointer based stores
370 need not be killed upon encountering a const function call.
371 2. After reload, the stores related to outgoing arguments can be
372 either stack pointer or hard frame pointer based. This means
373 that we have no other choice than also killing all the frame
374 pointer based stores upon encountering a const function call.
375 This field is set after reload for const function calls and before
376 reload for const tail function calls on targets where arg pointer
377 is the frame pointer. Having this set is less severe than a wild
378 read, it just means that all the frame related stores are killed
379 rather than all the stores. */
382 /* This field is only used for the processing of const functions.
383 It is set if the insn may contain a stack pointer based store. */
384 bool stack_pointer_based
;
386 /* This is true if any of the sets within the store contains a
387 cselib base. Such stores can only be deleted by the local
389 bool contains_cselib_groups
;
394 /* The list of mem sets or mem clobbers that are contained in this
395 insn. If the insn is deletable, it contains only one mem set.
396 But it could also contain clobbers. Insns that contain more than
397 one mem set are not deletable, but each of those mems are here in
398 order to provide info to delete other insns. */
399 store_info
*store_rec
;
401 /* The linked list of mem uses in this insn. Only the reads from
402 rtx bases are listed here. The reads to cselib bases are
403 completely processed during the first scan and so are never
405 read_info_t read_rec
;
407 /* The live fixed registers. We assume only fixed registers can
408 cause trouble by being clobbered from an expanded pattern;
409 storing only the live fixed registers (rather than all registers)
410 means less memory needs to be allocated / copied for the individual
412 regset fixed_regs_live
;
414 /* The prev insn in the basic block. */
415 struct insn_info_type
* prev_insn
;
417 /* The linked list of insns that are in consideration for removal in
418 the forwards pass through the basic block. This pointer may be
419 trash as it is not cleared when a wild read occurs. The only
420 time it is guaranteed to be correct is when the traversal starts
421 at active_local_stores. */
422 struct insn_info_type
* next_local_store
;
424 typedef struct insn_info_type
*insn_info_t
;
426 static object_allocator
<insn_info_type
> insn_info_type_pool ("insn_info_pool");
428 /* The linked list of stores that are under consideration in this
430 static insn_info_t active_local_stores
;
431 static int active_local_stores_len
;
433 struct dse_bb_info_type
435 /* Pointer to the insn info for the last insn in the block. These
436 are linked so this is how all of the insns are reached. During
437 scanning this is the current insn being scanned. */
438 insn_info_t last_insn
;
440 /* The info for the global dataflow problem. */
443 /* This is set if the transfer function should and in the wild_read
444 bitmap before applying the kill and gen sets. That vector knocks
445 out most of the bits in the bitmap and thus speeds up the
447 bool apply_wild_read
;
449 /* The following 4 bitvectors hold information about which positions
450 of which stores are live or dead. They are indexed by
453 /* The set of store positions that exist in this block before a wild read. */
456 /* The set of load positions that exist in this block above the
457 same position of a store. */
460 /* The set of stores that reach the top of the block without being
463 Do not represent the in if it is all ones. Note that this is
464 what the bitvector should logically be initialized to for a set
465 intersection problem. However, like the kill set, this is too
466 expensive. So initially, the in set will only be created for the
467 exit block and any block that contains a wild read. */
470 /* The set of stores that reach the bottom of the block from it's
473 Do not represent the in if it is all ones. Note that this is
474 what the bitvector should logically be initialized to for a set
475 intersection problem. However, like the kill and in set, this is
476 too expensive. So what is done is that the confluence operator
477 just initializes the vector from one of the out sets of the
478 successors of the block. */
481 /* The following bitvector is indexed by the reg number. It
482 contains the set of regs that are live at the current instruction
483 being processed. While it contains info for all of the
484 registers, only the hard registers are actually examined. It is used
485 to assure that shift and/or add sequences that are inserted do not
486 accidentally clobber live hard regs. */
490 typedef struct dse_bb_info_type
*bb_info_t
;
492 static object_allocator
<dse_bb_info_type
> dse_bb_info_type_pool
495 /* Table to hold all bb_infos. */
496 static bb_info_t
*bb_table
;
498 /* There is a group_info for each rtx base that is used to reference
499 memory. There are also not many of the rtx bases because they are
500 very limited in scope. */
504 /* The actual base of the address. */
507 /* The sequential id of the base. This allows us to have a
508 canonical ordering of these that is not based on addresses. */
511 /* True if there are any positions that are to be processed
513 bool process_globally
;
515 /* True if the base of this group is either the frame_pointer or
516 hard_frame_pointer. */
519 /* A mem wrapped around the base pointer for the group in order to do
520 read dependency. It must be given BLKmode in order to encompass all
521 the possible offsets from the base. */
524 /* Canonized version of base_mem's address. */
527 /* These two sets of two bitmaps are used to keep track of how many
528 stores are actually referencing that position from this base. We
529 only do this for rtx bases as this will be used to assign
530 positions in the bitmaps for the global problem. Bit N is set in
531 store1 on the first store for offset N. Bit N is set in store2
532 for the second store to offset N. This is all we need since we
533 only care about offsets that have two or more stores for them.
535 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
536 for 0 and greater offsets.
538 There is one special case here, for stores into the stack frame,
539 we will or store1 into store2 before deciding which stores look
540 at globally. This is because stores to the stack frame that have
541 no other reads before the end of the function can also be
543 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
545 /* These bitmaps keep track of offsets in this group escape this function.
546 An offset escapes if it corresponds to a named variable whose
547 addressable flag is set. */
548 bitmap escaped_n
, escaped_p
;
550 /* The positions in this bitmap have the same assignments as the in,
551 out, gen and kill bitmaps. This bitmap is all zeros except for
552 the positions that are occupied by stores for this group. */
555 /* The offset_map is used to map the offsets from this base into
556 positions in the global bitmaps. It is only created after all of
557 the all of stores have been scanned and we know which ones we
559 int *offset_map_n
, *offset_map_p
;
560 int offset_map_size_n
, offset_map_size_p
;
563 static object_allocator
<group_info
> group_info_pool ("rtx_group_info_pool");
565 /* Index into the rtx_group_vec. */
566 static int rtx_group_next_id
;
569 static vec
<group_info
*> rtx_group_vec
;
572 /* This structure holds the set of changes that are being deferred
573 when removing read operation. See replace_read. */
574 struct deferred_change
577 /* The mem that is being replaced. */
580 /* The reg it is being replaced with. */
583 struct deferred_change
*next
;
586 static object_allocator
<deferred_change
> deferred_change_pool
587 ("deferred_change_pool");
589 static deferred_change
*deferred_change_list
= NULL
;
591 /* The group that holds all of the clear_alias_sets. */
592 static group_info
*clear_alias_group
;
594 /* The modes of the clear_alias_sets. */
595 static htab_t clear_alias_mode_table
;
597 /* Hash table element to look up the mode for an alias set. */
598 struct clear_alias_mode_holder
600 alias_set_type alias_set
;
604 /* This is true except if cfun->stdarg -- i.e. we cannot do
605 this for vararg functions because they play games with the frame. */
606 static bool stores_off_frame_dead_at_return
;
608 /* Counter for stats. */
609 static int globally_deleted
;
610 static int locally_deleted
;
611 static int spill_deleted
;
613 static bitmap all_blocks
;
615 /* Locations that are killed by calls in the global phase. */
616 static bitmap kill_on_calls
;
618 /* The number of bits used in the global bitmaps. */
619 static unsigned int current_position
;
621 /*----------------------------------------------------------------------------
625 ----------------------------------------------------------------------------*/
628 /* Find the entry associated with ALIAS_SET. */
630 static struct clear_alias_mode_holder
*
631 clear_alias_set_lookup (alias_set_type alias_set
)
633 struct clear_alias_mode_holder tmp_holder
;
636 tmp_holder
.alias_set
= alias_set
;
637 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
640 return (struct clear_alias_mode_holder
*) *slot
;
644 /* Hashtable callbacks for maintaining the "bases" field of
645 store_group_info, given that the addresses are function invariants. */
647 struct invariant_group_base_hasher
: nofree_ptr_hash
<group_info
>
649 static inline hashval_t
hash (const group_info
*);
650 static inline bool equal (const group_info
*, const group_info
*);
654 invariant_group_base_hasher::equal (const group_info
*gi1
,
655 const group_info
*gi2
)
657 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
661 invariant_group_base_hasher::hash (const group_info
*gi
)
664 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
667 /* Tables of group_info structures, hashed by base value. */
668 static hash_table
<invariant_group_base_hasher
> *rtx_group_table
;
671 /* Get the GROUP for BASE. Add a new group if it is not there. */
674 get_group_info (rtx base
)
676 struct group_info tmp_gi
;
682 /* Find the store_base_info structure for BASE, creating a new one
684 tmp_gi
.rtx_base
= base
;
685 slot
= rtx_group_table
->find_slot (&tmp_gi
, INSERT
);
690 if (!clear_alias_group
)
692 clear_alias_group
= gi
= group_info_pool
.allocate ();
693 memset (gi
, 0, sizeof (struct group_info
));
694 gi
->id
= rtx_group_next_id
++;
695 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
696 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
697 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
698 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
699 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
700 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
701 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
702 gi
->process_globally
= false;
703 gi
->offset_map_size_n
= 0;
704 gi
->offset_map_size_p
= 0;
705 gi
->offset_map_n
= NULL
;
706 gi
->offset_map_p
= NULL
;
707 rtx_group_vec
.safe_push (gi
);
709 return clear_alias_group
;
714 *slot
= gi
= group_info_pool
.allocate ();
716 gi
->id
= rtx_group_next_id
++;
717 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
718 gi
->canon_base_addr
= canon_rtx (base
);
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;
728 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
729 gi
->offset_map_size_n
= 0;
730 gi
->offset_map_size_p
= 0;
731 gi
->offset_map_n
= NULL
;
732 gi
->offset_map_p
= NULL
;
733 rtx_group_vec
.safe_push (gi
);
740 /* Initialization of data structures. */
746 globally_deleted
= 0;
749 bitmap_obstack_initialize (&dse_bitmap_obstack
);
750 gcc_obstack_init (&dse_obstack
);
752 scratch
= BITMAP_ALLOC (®_obstack
);
753 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
756 rtx_group_table
= new hash_table
<invariant_group_base_hasher
> (11);
758 bb_table
= XNEWVEC (bb_info_t
, last_basic_block_for_fn (cfun
));
759 rtx_group_next_id
= 0;
761 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
763 init_alias_analysis ();
765 clear_alias_group
= NULL
;
770 /*----------------------------------------------------------------------------
773 Scan all of the insns. Any random ordering of the blocks is fine.
774 Each block is scanned in forward order to accommodate cselib which
775 is used to remove stores with non-constant bases.
776 ----------------------------------------------------------------------------*/
778 /* Delete all of the store_info recs from INSN_INFO. */
781 free_store_info (insn_info_t insn_info
)
783 store_info
*cur
= insn_info
->store_rec
;
786 store_info
*next
= cur
->next
;
788 BITMAP_FREE (cur
->positions_needed
.large
.bmap
);
790 cse_store_info_pool
.remove (cur
);
792 rtx_store_info_pool
.remove (cur
);
796 insn_info
->cannot_delete
= true;
797 insn_info
->contains_cselib_groups
= false;
798 insn_info
->store_rec
= NULL
;
801 struct note_add_store_info
803 rtx_insn
*first
, *current
;
804 regset fixed_regs_live
;
808 /* Callback for emit_inc_dec_insn_before via note_stores.
809 Check if a register is clobbered which is live afterwards. */
812 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
815 note_add_store_info
*info
= (note_add_store_info
*) data
;
820 /* If this register is referenced by the current or an earlier insn,
821 that's OK. E.g. this applies to the register that is being incremented
822 with this addition. */
823 for (insn
= info
->first
;
824 insn
!= NEXT_INSN (info
->current
);
825 insn
= NEXT_INSN (insn
))
826 if (reg_referenced_p (loc
, PATTERN (insn
)))
829 /* If we come here, we have a clobber of a register that's only OK
830 if that register is not live. If we don't have liveness information
831 available, fail now. */
832 if (!info
->fixed_regs_live
)
834 info
->failure
= true;
837 /* Now check if this is a live fixed register. */
838 unsigned int end_regno
= END_REGNO (loc
);
839 for (unsigned int regno
= REGNO (loc
); regno
< end_regno
; ++regno
)
840 if (REGNO_REG_SET_P (info
->fixed_regs_live
, regno
))
841 info
->failure
= true;
844 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
845 SRC + SRCOFF before insn ARG. */
848 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
849 rtx op ATTRIBUTE_UNUSED
,
850 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
852 insn_info_t insn_info
= (insn_info_t
) arg
;
853 rtx_insn
*insn
= insn_info
->insn
, *new_insn
, *cur
;
854 note_add_store_info info
;
856 /* We can reuse all operands without copying, because we are about
857 to delete the insn that contained it. */
861 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
862 new_insn
= get_insns ();
866 new_insn
= gen_move_insn (dest
, src
);
867 info
.first
= new_insn
;
868 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
869 info
.failure
= false;
870 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
873 note_stores (PATTERN (cur
), note_add_store
, &info
);
876 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
877 return it immediately, communicating the failure to its caller. */
881 emit_insn_before (new_insn
, insn
);
886 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
887 is there, is split into a separate insn.
888 Return true on success (or if there was nothing to do), false on failure. */
891 check_for_inc_dec_1 (insn_info_t insn_info
)
893 rtx_insn
*insn
= insn_info
->insn
;
894 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
896 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
902 /* Entry point for postreload. If you work on reload_cse, or you need this
903 anywhere else, consider if you can provide register liveness information
904 and add a parameter to this function so that it can be passed down in
905 insn_info.fixed_regs_live. */
907 check_for_inc_dec (rtx_insn
*insn
)
909 insn_info_type insn_info
;
912 insn_info
.insn
= insn
;
913 insn_info
.fixed_regs_live
= NULL
;
914 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
916 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
921 /* Delete the insn and free all of the fields inside INSN_INFO. */
924 delete_dead_store_insn (insn_info_t insn_info
)
926 read_info_t read_info
;
931 if (!check_for_inc_dec_1 (insn_info
))
933 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
935 fprintf (dump_file
, "Locally deleting insn %d ",
936 INSN_UID (insn_info
->insn
));
937 if (insn_info
->store_rec
->alias_set
)
938 fprintf (dump_file
, "alias set %d\n",
939 (int) insn_info
->store_rec
->alias_set
);
941 fprintf (dump_file
, "\n");
944 free_store_info (insn_info
);
945 read_info
= insn_info
->read_rec
;
949 read_info_t next
= read_info
->next
;
950 read_info_type_pool
.remove (read_info
);
953 insn_info
->read_rec
= NULL
;
955 delete_insn (insn_info
->insn
);
957 insn_info
->insn
= NULL
;
959 insn_info
->wild_read
= false;
962 /* Return whether DECL, a local variable, can possibly escape the current
966 local_variable_can_escape (tree decl
)
968 if (TREE_ADDRESSABLE (decl
))
971 /* If this is a partitioned variable, we need to consider all the variables
972 in the partition. This is necessary because a store into one of them can
973 be replaced with a store into another and this may not change the outcome
974 of the escape analysis. */
975 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
977 tree
*namep
= cfun
->gimple_df
->decls_to_pointers
->get (decl
);
979 return TREE_ADDRESSABLE (*namep
);
985 /* Return whether EXPR can possibly escape the current function scope. */
988 can_escape (tree expr
)
993 base
= get_base_address (expr
);
995 && !may_be_aliased (base
)
996 && !(TREE_CODE (base
) == VAR_DECL
997 && !DECL_EXTERNAL (base
)
998 && !TREE_STATIC (base
)
999 && local_variable_can_escape (base
)))
1004 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1005 OFFSET and WIDTH. */
1008 set_usage_bits (group_info
*group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1012 bool expr_escapes
= can_escape (expr
);
1013 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1014 for (i
=offset
; i
<offset
+width
; i
++)
1022 store1
= group
->store1_n
;
1023 store2
= group
->store2_n
;
1024 escaped
= group
->escaped_n
;
1029 store1
= group
->store1_p
;
1030 store2
= group
->store2_p
;
1031 escaped
= group
->escaped_p
;
1035 if (!bitmap_set_bit (store1
, ai
))
1036 bitmap_set_bit (store2
, ai
);
1041 if (group
->offset_map_size_n
< ai
)
1042 group
->offset_map_size_n
= ai
;
1046 if (group
->offset_map_size_p
< ai
)
1047 group
->offset_map_size_p
= ai
;
1051 bitmap_set_bit (escaped
, ai
);
1056 reset_active_stores (void)
1058 active_local_stores
= NULL
;
1059 active_local_stores_len
= 0;
1062 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1065 free_read_records (bb_info_t bb_info
)
1067 insn_info_t insn_info
= bb_info
->last_insn
;
1068 read_info_t
*ptr
= &insn_info
->read_rec
;
1071 read_info_t next
= (*ptr
)->next
;
1072 if ((*ptr
)->alias_set
== 0)
1074 read_info_type_pool
.remove (*ptr
);
1078 ptr
= &(*ptr
)->next
;
1082 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1085 add_wild_read (bb_info_t bb_info
)
1087 insn_info_t insn_info
= bb_info
->last_insn
;
1088 insn_info
->wild_read
= true;
1089 free_read_records (bb_info
);
1090 reset_active_stores ();
1093 /* Set the BB_INFO so that the last insn is marked as a wild read of
1094 non-frame locations. */
1097 add_non_frame_wild_read (bb_info_t bb_info
)
1099 insn_info_t insn_info
= bb_info
->last_insn
;
1100 insn_info
->non_frame_wild_read
= true;
1101 free_read_records (bb_info
);
1102 reset_active_stores ();
1105 /* Return true if X is a constant or one of the registers that behave
1106 as a constant over the life of a function. This is equivalent to
1107 !rtx_varies_p for memory addresses. */
1110 const_or_frame_p (rtx x
)
1115 if (GET_CODE (x
) == REG
)
1117 /* Note that we have to test for the actual rtx used for the frame
1118 and arg pointers and not just the register number in case we have
1119 eliminated the frame and/or arg pointer and are using it
1121 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1122 /* The arg pointer varies if it is not a fixed register. */
1123 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1124 || x
== pic_offset_table_rtx
)
1132 /* Take all reasonable action to put the address of MEM into the form
1133 that we can do analysis on.
1135 The gold standard is to get the address into the form: address +
1136 OFFSET where address is something that rtx_varies_p considers a
1137 constant. When we can get the address in this form, we can do
1138 global analysis on it. Note that for constant bases, address is
1139 not actually returned, only the group_id. The address can be
1142 If that fails, we try cselib to get a value we can at least use
1143 locally. If that fails we return false.
1145 The GROUP_ID is set to -1 for cselib bases and the index of the
1146 group for non_varying bases.
1148 FOR_READ is true if this is a mem read and false if not. */
1151 canon_address (rtx mem
,
1152 alias_set_type
*alias_set_out
,
1154 HOST_WIDE_INT
*offset
,
1157 machine_mode address_mode
= get_address_mode (mem
);
1158 rtx mem_address
= XEXP (mem
, 0);
1159 rtx expanded_address
, address
;
1164 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1166 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1168 fprintf (dump_file
, " mem: ");
1169 print_inline_rtx (dump_file
, mem_address
, 0);
1170 fprintf (dump_file
, "\n");
1173 /* First see if just canon_rtx (mem_address) is const or frame,
1174 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1176 for (expanded
= 0; expanded
< 2; expanded
++)
1180 /* Use cselib to replace all of the reg references with the full
1181 expression. This will take care of the case where we have
1183 r_x = base + offset;
1188 val = *(base + offset); */
1190 expanded_address
= cselib_expand_value_rtx (mem_address
,
1193 /* If this fails, just go with the address from first
1195 if (!expanded_address
)
1199 expanded_address
= mem_address
;
1201 /* Split the address into canonical BASE + OFFSET terms. */
1202 address
= canon_rtx (expanded_address
);
1206 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1210 fprintf (dump_file
, "\n after cselib_expand address: ");
1211 print_inline_rtx (dump_file
, expanded_address
, 0);
1212 fprintf (dump_file
, "\n");
1215 fprintf (dump_file
, "\n after canon_rtx address: ");
1216 print_inline_rtx (dump_file
, address
, 0);
1217 fprintf (dump_file
, "\n");
1220 if (GET_CODE (address
) == CONST
)
1221 address
= XEXP (address
, 0);
1223 if (GET_CODE (address
) == PLUS
1224 && CONST_INT_P (XEXP (address
, 1)))
1226 *offset
= INTVAL (XEXP (address
, 1));
1227 address
= XEXP (address
, 0);
1230 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1231 && const_or_frame_p (address
))
1233 group_info
*group
= get_group_info (address
);
1235 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1236 fprintf (dump_file
, " gid=%d offset=%d \n",
1237 group
->id
, (int)*offset
);
1239 *group_id
= group
->id
;
1244 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1249 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1250 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1253 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1254 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1255 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1260 /* Clear the rhs field from the active_local_stores array. */
1263 clear_rhs_from_active_local_stores (void)
1265 insn_info_t ptr
= active_local_stores
;
1269 store_info
*store_info
= ptr
->store_rec
;
1270 /* Skip the clobbers. */
1271 while (!store_info
->is_set
)
1272 store_info
= store_info
->next
;
1274 store_info
->rhs
= NULL
;
1275 store_info
->const_rhs
= NULL
;
1277 ptr
= ptr
->next_local_store
;
1282 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1285 set_position_unneeded (store_info
*s_info
, int pos
)
1287 if (__builtin_expect (s_info
->is_large
, false))
1289 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1290 s_info
->positions_needed
.large
.count
++;
1293 s_info
->positions_needed
.small_bitmask
1294 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1297 /* Mark the whole store S_INFO as unneeded. */
1300 set_all_positions_unneeded (store_info
*s_info
)
1302 if (__builtin_expect (s_info
->is_large
, false))
1304 int pos
, end
= s_info
->end
- s_info
->begin
;
1305 for (pos
= 0; pos
< end
; pos
++)
1306 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1307 s_info
->positions_needed
.large
.count
= end
;
1310 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1313 /* Return TRUE if any bytes from S_INFO store are needed. */
1316 any_positions_needed_p (store_info
*s_info
)
1318 if (__builtin_expect (s_info
->is_large
, false))
1319 return (s_info
->positions_needed
.large
.count
1320 < s_info
->end
- s_info
->begin
);
1322 return (s_info
->positions_needed
.small_bitmask
1323 != (unsigned HOST_WIDE_INT
) 0);
1326 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1327 store are needed. */
1330 all_positions_needed_p (store_info
*s_info
, int start
, int width
)
1332 if (__builtin_expect (s_info
->is_large
, false))
1334 int end
= start
+ width
;
1336 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1342 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1343 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1348 static rtx
get_stored_val (store_info
*, machine_mode
, HOST_WIDE_INT
,
1349 HOST_WIDE_INT
, basic_block
, bool);
1352 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1353 there is a candidate store, after adding it to the appropriate
1354 local store group if so. */
1357 record_store (rtx body
, bb_info_t bb_info
)
1359 rtx mem
, rhs
, const_rhs
, mem_addr
;
1360 HOST_WIDE_INT offset
= 0;
1361 HOST_WIDE_INT width
= 0;
1362 alias_set_type spill_alias_set
;
1363 insn_info_t insn_info
= bb_info
->last_insn
;
1364 store_info
*store_info
= NULL
;
1366 cselib_val
*base
= NULL
;
1367 insn_info_t ptr
, last
, redundant_reason
;
1368 bool store_is_unused
;
1370 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1373 mem
= SET_DEST (body
);
1375 /* If this is not used, then this cannot be used to keep the insn
1376 from being deleted. On the other hand, it does provide something
1377 that can be used to prove that another store is dead. */
1379 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1381 /* Check whether that value is a suitable memory location. */
1384 /* If the set or clobber is unused, then it does not effect our
1385 ability to get rid of the entire insn. */
1386 if (!store_is_unused
)
1387 insn_info
->cannot_delete
= true;
1391 /* At this point we know mem is a mem. */
1392 if (GET_MODE (mem
) == BLKmode
)
1394 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1396 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1397 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1398 add_wild_read (bb_info
);
1399 insn_info
->cannot_delete
= true;
1402 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1403 as memset (addr, 0, 36); */
1404 else if (!MEM_SIZE_KNOWN_P (mem
)
1405 || MEM_SIZE (mem
) <= 0
1406 || MEM_SIZE (mem
) > MAX_OFFSET
1407 || GET_CODE (body
) != SET
1408 || !CONST_INT_P (SET_SRC (body
)))
1410 if (!store_is_unused
)
1412 /* If the set or clobber is unused, then it does not effect our
1413 ability to get rid of the entire insn. */
1414 insn_info
->cannot_delete
= true;
1415 clear_rhs_from_active_local_stores ();
1421 /* We can still process a volatile mem, we just cannot delete it. */
1422 if (MEM_VOLATILE_P (mem
))
1423 insn_info
->cannot_delete
= true;
1425 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1427 clear_rhs_from_active_local_stores ();
1431 if (GET_MODE (mem
) == BLKmode
)
1432 width
= MEM_SIZE (mem
);
1434 width
= GET_MODE_SIZE (GET_MODE (mem
));
1436 if (spill_alias_set
)
1438 bitmap store1
= clear_alias_group
->store1_p
;
1439 bitmap store2
= clear_alias_group
->store2_p
;
1441 gcc_assert (GET_MODE (mem
) != BLKmode
);
1443 if (!bitmap_set_bit (store1
, spill_alias_set
))
1444 bitmap_set_bit (store2
, spill_alias_set
);
1446 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1447 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1449 store_info
= rtx_store_info_pool
.allocate ();
1451 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1452 fprintf (dump_file
, " processing spill store %d(%s)\n",
1453 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1455 else if (group_id
>= 0)
1457 /* In the restrictive case where the base is a constant or the
1458 frame pointer we can do global analysis. */
1461 = rtx_group_vec
[group_id
];
1462 tree expr
= MEM_EXPR (mem
);
1464 store_info
= rtx_store_info_pool
.allocate ();
1465 set_usage_bits (group
, offset
, width
, expr
);
1467 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1468 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1469 group_id
, (int)offset
, (int)(offset
+width
));
1473 if (may_be_sp_based_p (XEXP (mem
, 0)))
1474 insn_info
->stack_pointer_based
= true;
1475 insn_info
->contains_cselib_groups
= true;
1477 store_info
= cse_store_info_pool
.allocate ();
1480 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1481 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1482 (int)offset
, (int)(offset
+width
));
1485 const_rhs
= rhs
= NULL_RTX
;
1486 if (GET_CODE (body
) == SET
1487 /* No place to keep the value after ra. */
1488 && !reload_completed
1489 && (REG_P (SET_SRC (body
))
1490 || GET_CODE (SET_SRC (body
)) == SUBREG
1491 || CONSTANT_P (SET_SRC (body
)))
1492 && !MEM_VOLATILE_P (mem
)
1493 /* Sometimes the store and reload is used for truncation and
1495 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1497 rhs
= SET_SRC (body
);
1498 if (CONSTANT_P (rhs
))
1500 else if (body
== PATTERN (insn_info
->insn
))
1502 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1503 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1504 const_rhs
= XEXP (tem
, 0);
1506 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1508 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1510 if (tem
&& CONSTANT_P (tem
))
1515 /* Check to see if this stores causes some other stores to be
1517 ptr
= active_local_stores
;
1519 redundant_reason
= NULL
;
1520 mem
= canon_rtx (mem
);
1521 /* For alias_set != 0 canon_true_dependence should be never called. */
1522 if (spill_alias_set
)
1523 mem_addr
= NULL_RTX
;
1527 mem_addr
= base
->val_rtx
;
1531 = rtx_group_vec
[group_id
];
1532 mem_addr
= group
->canon_base_addr
;
1534 /* get_addr can only handle VALUE but cannot handle expr like:
1535 VALUE + OFFSET, so call get_addr to get original addr for
1536 mem_addr before plus_constant. */
1537 mem_addr
= get_addr (mem_addr
);
1539 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1544 insn_info_t next
= ptr
->next_local_store
;
1545 struct store_info
*s_info
= ptr
->store_rec
;
1548 /* Skip the clobbers. We delete the active insn if this insn
1549 shadows the set. To have been put on the active list, it
1550 has exactly on set. */
1551 while (!s_info
->is_set
)
1552 s_info
= s_info
->next
;
1554 if (s_info
->alias_set
!= spill_alias_set
)
1556 else if (s_info
->alias_set
)
1558 struct clear_alias_mode_holder
*entry
1559 = clear_alias_set_lookup (s_info
->alias_set
);
1560 /* Generally, spills cannot be processed if and of the
1561 references to the slot have a different mode. But if
1562 we are in the same block and mode is exactly the same
1563 between this store and one before in the same block,
1564 we can still delete it. */
1565 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1566 && (GET_MODE (mem
) == entry
->mode
))
1569 set_all_positions_unneeded (s_info
);
1571 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1572 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1573 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1575 else if ((s_info
->group_id
== group_id
)
1576 && (s_info
->cse_base
== base
))
1579 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1580 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1581 INSN_UID (ptr
->insn
), s_info
->group_id
,
1582 (int)s_info
->begin
, (int)s_info
->end
);
1584 /* Even if PTR won't be eliminated as unneeded, if both
1585 PTR and this insn store the same constant value, we might
1586 eliminate this insn instead. */
1587 if (s_info
->const_rhs
1589 && offset
>= s_info
->begin
1590 && offset
+ width
<= s_info
->end
1591 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1594 if (GET_MODE (mem
) == BLKmode
)
1596 if (GET_MODE (s_info
->mem
) == BLKmode
1597 && s_info
->const_rhs
== const_rhs
)
1598 redundant_reason
= ptr
;
1600 else if (s_info
->const_rhs
== const0_rtx
1601 && const_rhs
== const0_rtx
)
1602 redundant_reason
= ptr
;
1607 val
= get_stored_val (s_info
, GET_MODE (mem
),
1608 offset
, offset
+ width
,
1609 BLOCK_FOR_INSN (insn_info
->insn
),
1611 if (get_insns () != NULL
)
1614 if (val
&& rtx_equal_p (val
, const_rhs
))
1615 redundant_reason
= ptr
;
1619 for (i
= MAX (offset
, s_info
->begin
);
1620 i
< offset
+ width
&& i
< s_info
->end
;
1622 set_position_unneeded (s_info
, i
- s_info
->begin
);
1624 else if (s_info
->rhs
)
1625 /* Need to see if it is possible for this store to overwrite
1626 the value of store_info. If it is, set the rhs to NULL to
1627 keep it from being used to remove a load. */
1629 if (canon_true_dependence (s_info
->mem
,
1630 GET_MODE (s_info
->mem
),
1635 s_info
->const_rhs
= NULL
;
1639 /* An insn can be deleted if every position of every one of
1640 its s_infos is zero. */
1641 if (any_positions_needed_p (s_info
))
1646 insn_info_t insn_to_delete
= ptr
;
1648 active_local_stores_len
--;
1650 last
->next_local_store
= ptr
->next_local_store
;
1652 active_local_stores
= ptr
->next_local_store
;
1654 if (!insn_to_delete
->cannot_delete
)
1655 delete_dead_store_insn (insn_to_delete
);
1663 /* Finish filling in the store_info. */
1664 store_info
->next
= insn_info
->store_rec
;
1665 insn_info
->store_rec
= store_info
;
1666 store_info
->mem
= mem
;
1667 store_info
->alias_set
= spill_alias_set
;
1668 store_info
->mem_addr
= mem_addr
;
1669 store_info
->cse_base
= base
;
1670 if (width
> HOST_BITS_PER_WIDE_INT
)
1672 store_info
->is_large
= true;
1673 store_info
->positions_needed
.large
.count
= 0;
1674 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1678 store_info
->is_large
= false;
1679 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1681 store_info
->group_id
= group_id
;
1682 store_info
->begin
= offset
;
1683 store_info
->end
= offset
+ width
;
1684 store_info
->is_set
= GET_CODE (body
) == SET
;
1685 store_info
->rhs
= rhs
;
1686 store_info
->const_rhs
= const_rhs
;
1687 store_info
->redundant_reason
= redundant_reason
;
1689 /* If this is a clobber, we return 0. We will only be able to
1690 delete this insn if there is only one store USED store, but we
1691 can use the clobber to delete other stores earlier. */
1692 return store_info
->is_set
? 1 : 0;
1697 dump_insn_info (const char * start
, insn_info_t insn_info
)
1699 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1700 INSN_UID (insn_info
->insn
),
1701 insn_info
->store_rec
? "has store" : "naked");
1705 /* If the modes are different and the value's source and target do not
1706 line up, we need to extract the value from lower part of the rhs of
1707 the store, shift it, and then put it into a form that can be shoved
1708 into the read_insn. This function generates a right SHIFT of a
1709 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1710 shift sequence is returned or NULL if we failed to find a
1714 find_shift_sequence (int access_size
,
1715 store_info
*store_info
,
1716 machine_mode read_mode
,
1717 int shift
, bool speed
, bool require_cst
)
1719 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1720 machine_mode new_mode
;
1721 rtx read_reg
= NULL
;
1723 /* Some machines like the x86 have shift insns for each size of
1724 operand. Other machines like the ppc or the ia-64 may only have
1725 shift insns that shift values within 32 or 64 bit registers.
1726 This loop tries to find the smallest shift insn that will right
1727 justify the value we want to read but is available in one insn on
1730 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1732 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1733 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1735 rtx target
, new_reg
, new_lhs
;
1736 rtx_insn
*shift_seq
, *insn
;
1739 /* If a constant was stored into memory, try to simplify it here,
1740 otherwise the cost of the shift might preclude this optimization
1741 e.g. at -Os, even when no actual shift will be needed. */
1742 if (store_info
->const_rhs
)
1744 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1745 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1747 if (ret
&& CONSTANT_P (ret
))
1749 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1750 ret
, GEN_INT (shift
));
1751 if (ret
&& CONSTANT_P (ret
))
1753 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1754 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1755 if (ret
&& CONSTANT_P (ret
)
1756 && (set_src_cost (ret
, read_mode
, speed
)
1757 <= COSTS_N_INSNS (1)))
1766 /* Try a wider mode if truncating the store mode to NEW_MODE
1767 requires a real instruction. */
1768 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1769 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1772 /* Also try a wider mode if the necessary punning is either not
1773 desirable or not possible. */
1774 if (!CONSTANT_P (store_info
->rhs
)
1775 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1778 new_reg
= gen_reg_rtx (new_mode
);
1782 /* In theory we could also check for an ashr. Ian Taylor knows
1783 of one dsp where the cost of these two was not the same. But
1784 this really is a rare case anyway. */
1785 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1786 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1788 shift_seq
= get_insns ();
1791 if (target
!= new_reg
|| shift_seq
== NULL
)
1795 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1797 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1799 /* The computation up to here is essentially independent
1800 of the arguments and could be precomputed. It may
1801 not be worth doing so. We could precompute if
1802 worthwhile or at least cache the results. The result
1803 technically depends on both SHIFT and ACCESS_SIZE,
1804 but in practice the answer will depend only on ACCESS_SIZE. */
1806 if (cost
> COSTS_N_INSNS (1))
1809 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1810 copy_rtx (store_info
->rhs
));
1811 if (new_lhs
== NULL_RTX
)
1814 /* We found an acceptable shift. Generate a move to
1815 take the value from the store and put it into the
1816 shift pseudo, then shift it, then generate another
1817 move to put in into the target of the read. */
1818 emit_move_insn (new_reg
, new_lhs
);
1819 emit_insn (shift_seq
);
1820 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1828 /* Call back for note_stores to find the hard regs set or clobbered by
1829 insn. Data is a bitmap of the hardregs set so far. */
1832 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1834 bitmap regs_set
= (bitmap
) data
;
1837 && HARD_REGISTER_P (x
))
1838 bitmap_set_range (regs_set
, REGNO (x
), REG_NREGS (x
));
1841 /* Helper function for replace_read and record_store.
1842 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1843 to one before READ_END bytes read in READ_MODE. Return NULL
1844 if not successful. If REQUIRE_CST is true, return always constant. */
1847 get_stored_val (store_info
*store_info
, machine_mode read_mode
,
1848 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1849 basic_block bb
, bool require_cst
)
1851 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1853 int access_size
; /* In bytes. */
1856 /* To get here the read is within the boundaries of the write so
1857 shift will never be negative. Start out with the shift being in
1859 if (store_mode
== BLKmode
)
1861 else if (BYTES_BIG_ENDIAN
)
1862 shift
= store_info
->end
- read_end
;
1864 shift
= read_begin
- store_info
->begin
;
1866 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1868 /* From now on it is bits. */
1869 shift
*= BITS_PER_UNIT
;
1872 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1873 optimize_bb_for_speed_p (bb
),
1875 else if (store_mode
== BLKmode
)
1877 /* The store is a memset (addr, const_val, const_size). */
1878 gcc_assert (CONST_INT_P (store_info
->rhs
));
1879 store_mode
= int_mode_for_mode (read_mode
);
1880 if (store_mode
== BLKmode
)
1881 read_reg
= NULL_RTX
;
1882 else if (store_info
->rhs
== const0_rtx
)
1883 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1884 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1885 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1886 read_reg
= NULL_RTX
;
1889 unsigned HOST_WIDE_INT c
1890 = INTVAL (store_info
->rhs
)
1891 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1892 int shift
= BITS_PER_UNIT
;
1893 while (shift
< HOST_BITS_PER_WIDE_INT
)
1898 read_reg
= gen_int_mode (c
, store_mode
);
1899 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1902 else if (store_info
->const_rhs
1904 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1905 read_reg
= extract_low_bits (read_mode
, store_mode
,
1906 copy_rtx (store_info
->const_rhs
));
1908 read_reg
= extract_low_bits (read_mode
, store_mode
,
1909 copy_rtx (store_info
->rhs
));
1910 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1911 read_reg
= NULL_RTX
;
1915 /* Take a sequence of:
1938 Depending on the alignment and the mode of the store and
1942 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1943 and READ_INSN are for the read. Return true if the replacement
1947 replace_read (store_info
*store_info
, insn_info_t store_insn
,
1948 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1951 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1952 machine_mode read_mode
= GET_MODE (read_info
->mem
);
1953 rtx_insn
*insns
, *this_insn
;
1960 /* Create a sequence of instructions to set up the read register.
1961 This sequence goes immediately before the store and its result
1962 is read by the load.
1964 We need to keep this in perspective. We are replacing a read
1965 with a sequence of insns, but the read will almost certainly be
1966 in cache, so it is not going to be an expensive one. Thus, we
1967 are not willing to do a multi insn shift or worse a subroutine
1968 call to get rid of the read. */
1969 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1970 fprintf (dump_file
, "trying to replace %smode load in insn %d"
1971 " from %smode store in insn %d\n",
1972 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
1973 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
1975 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
1976 read_reg
= get_stored_val (store_info
,
1977 read_mode
, read_info
->begin
, read_info
->end
,
1979 if (read_reg
== NULL_RTX
)
1982 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1983 fprintf (dump_file
, " -- could not extract bits of stored value\n");
1986 /* Force the value into a new register so that it won't be clobbered
1987 between the store and the load. */
1988 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
1989 insns
= get_insns ();
1992 if (insns
!= NULL_RTX
)
1994 /* Now we have to scan the set of new instructions to see if the
1995 sequence contains and sets of hardregs that happened to be
1996 live at this point. For instance, this can happen if one of
1997 the insns sets the CC and the CC happened to be live at that
1998 point. This does occasionally happen, see PR 37922. */
1999 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2001 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2002 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2004 bitmap_and_into (regs_set
, regs_live
);
2005 if (!bitmap_empty_p (regs_set
))
2007 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2010 "abandoning replacement because sequence clobbers live hardregs:");
2011 df_print_regset (dump_file
, regs_set
);
2014 BITMAP_FREE (regs_set
);
2017 BITMAP_FREE (regs_set
);
2020 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2022 deferred_change
*change
= deferred_change_pool
.allocate ();
2024 /* Insert this right before the store insn where it will be safe
2025 from later insns that might change it before the read. */
2026 emit_insn_before (insns
, store_insn
->insn
);
2028 /* And now for the kludge part: cselib croaks if you just
2029 return at this point. There are two reasons for this:
2031 1) Cselib has an idea of how many pseudos there are and
2032 that does not include the new ones we just added.
2034 2) Cselib does not know about the move insn we added
2035 above the store_info, and there is no way to tell it
2036 about it, because it has "moved on".
2038 Problem (1) is fixable with a certain amount of engineering.
2039 Problem (2) is requires starting the bb from scratch. This
2042 So we are just going to have to lie. The move/extraction
2043 insns are not really an issue, cselib did not see them. But
2044 the use of the new pseudo read_insn is a real problem because
2045 cselib has not scanned this insn. The way that we solve this
2046 problem is that we are just going to put the mem back for now
2047 and when we are finished with the block, we undo this. We
2048 keep a table of mems to get rid of. At the end of the basic
2049 block we can put them back. */
2051 *loc
= read_info
->mem
;
2052 change
->next
= deferred_change_list
;
2053 deferred_change_list
= change
;
2055 change
->reg
= read_reg
;
2057 /* Get rid of the read_info, from the point of view of the
2058 rest of dse, play like this read never happened. */
2059 read_insn
->read_rec
= read_info
->next
;
2060 read_info_type_pool
.remove (read_info
);
2061 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2063 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2064 print_simple_rtl (dump_file
, read_reg
);
2065 fprintf (dump_file
, "\n");
2071 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2073 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2074 print_simple_rtl (dump_file
, read_reg
);
2075 fprintf (dump_file
, " led to an invalid instruction\n");
2081 /* Check the address of MEM *LOC and kill any appropriate stores that may
2085 check_mem_read_rtx (rtx
*loc
, bb_info_t bb_info
)
2087 rtx mem
= *loc
, mem_addr
;
2088 insn_info_t insn_info
;
2089 HOST_WIDE_INT offset
= 0;
2090 HOST_WIDE_INT width
= 0;
2091 alias_set_type spill_alias_set
= 0;
2092 cselib_val
*base
= NULL
;
2094 read_info_t read_info
;
2096 insn_info
= bb_info
->last_insn
;
2098 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2099 || (MEM_VOLATILE_P (mem
)))
2101 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2102 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2103 add_wild_read (bb_info
);
2104 insn_info
->cannot_delete
= true;
2108 /* If it is reading readonly mem, then there can be no conflict with
2110 if (MEM_READONLY_P (mem
))
2113 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2115 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2116 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2117 add_wild_read (bb_info
);
2121 if (GET_MODE (mem
) == BLKmode
)
2124 width
= GET_MODE_SIZE (GET_MODE (mem
));
2126 read_info
= read_info_type_pool
.allocate ();
2127 read_info
->group_id
= group_id
;
2128 read_info
->mem
= mem
;
2129 read_info
->alias_set
= spill_alias_set
;
2130 read_info
->begin
= offset
;
2131 read_info
->end
= offset
+ width
;
2132 read_info
->next
= insn_info
->read_rec
;
2133 insn_info
->read_rec
= read_info
;
2134 /* For alias_set != 0 canon_true_dependence should be never called. */
2135 if (spill_alias_set
)
2136 mem_addr
= NULL_RTX
;
2140 mem_addr
= base
->val_rtx
;
2144 = rtx_group_vec
[group_id
];
2145 mem_addr
= group
->canon_base_addr
;
2147 /* get_addr can only handle VALUE but cannot handle expr like:
2148 VALUE + OFFSET, so call get_addr to get original addr for
2149 mem_addr before plus_constant. */
2150 mem_addr
= get_addr (mem_addr
);
2152 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2155 /* We ignore the clobbers in store_info. The is mildly aggressive,
2156 but there really should not be a clobber followed by a read. */
2158 if (spill_alias_set
)
2160 insn_info_t i_ptr
= active_local_stores
;
2161 insn_info_t last
= NULL
;
2163 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2164 fprintf (dump_file
, " processing spill load %d\n",
2165 (int) spill_alias_set
);
2169 store_info
*store_info
= i_ptr
->store_rec
;
2171 /* Skip the clobbers. */
2172 while (!store_info
->is_set
)
2173 store_info
= store_info
->next
;
2175 if (store_info
->alias_set
== spill_alias_set
)
2177 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2178 dump_insn_info ("removing from active", i_ptr
);
2180 active_local_stores_len
--;
2182 last
->next_local_store
= i_ptr
->next_local_store
;
2184 active_local_stores
= i_ptr
->next_local_store
;
2188 i_ptr
= i_ptr
->next_local_store
;
2191 else if (group_id
>= 0)
2193 /* This is the restricted case where the base is a constant or
2194 the frame pointer and offset is a constant. */
2195 insn_info_t i_ptr
= active_local_stores
;
2196 insn_info_t last
= NULL
;
2198 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2201 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2204 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2205 group_id
, (int)offset
, (int)(offset
+width
));
2210 bool remove
= false;
2211 store_info
*store_info
= i_ptr
->store_rec
;
2213 /* Skip the clobbers. */
2214 while (!store_info
->is_set
)
2215 store_info
= store_info
->next
;
2217 /* There are three cases here. */
2218 if (store_info
->group_id
< 0)
2219 /* We have a cselib store followed by a read from a
2222 = canon_true_dependence (store_info
->mem
,
2223 GET_MODE (store_info
->mem
),
2224 store_info
->mem_addr
,
2227 else if (group_id
== store_info
->group_id
)
2229 /* This is a block mode load. We may get lucky and
2230 canon_true_dependence may save the day. */
2233 = canon_true_dependence (store_info
->mem
,
2234 GET_MODE (store_info
->mem
),
2235 store_info
->mem_addr
,
2238 /* If this read is just reading back something that we just
2239 stored, rewrite the read. */
2243 && offset
>= store_info
->begin
2244 && offset
+ width
<= store_info
->end
2245 && all_positions_needed_p (store_info
,
2246 offset
- store_info
->begin
,
2248 && replace_read (store_info
, i_ptr
, read_info
,
2249 insn_info
, loc
, bb_info
->regs_live
))
2252 /* The bases are the same, just see if the offsets
2254 if ((offset
< store_info
->end
)
2255 && (offset
+ width
> store_info
->begin
))
2261 The else case that is missing here is that the
2262 bases are constant but different. There is nothing
2263 to do here because there is no overlap. */
2267 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2268 dump_insn_info ("removing from active", i_ptr
);
2270 active_local_stores_len
--;
2272 last
->next_local_store
= i_ptr
->next_local_store
;
2274 active_local_stores
= i_ptr
->next_local_store
;
2278 i_ptr
= i_ptr
->next_local_store
;
2283 insn_info_t i_ptr
= active_local_stores
;
2284 insn_info_t last
= NULL
;
2285 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2287 fprintf (dump_file
, " processing cselib load mem:");
2288 print_inline_rtx (dump_file
, mem
, 0);
2289 fprintf (dump_file
, "\n");
2294 bool remove
= false;
2295 store_info
*store_info
= i_ptr
->store_rec
;
2297 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2298 fprintf (dump_file
, " processing cselib load against insn %d\n",
2299 INSN_UID (i_ptr
->insn
));
2301 /* Skip the clobbers. */
2302 while (!store_info
->is_set
)
2303 store_info
= store_info
->next
;
2305 /* If this read is just reading back something that we just
2306 stored, rewrite the read. */
2308 && store_info
->group_id
== -1
2309 && store_info
->cse_base
== base
2311 && offset
>= store_info
->begin
2312 && offset
+ width
<= store_info
->end
2313 && all_positions_needed_p (store_info
,
2314 offset
- store_info
->begin
, width
)
2315 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2316 bb_info
->regs_live
))
2319 if (!store_info
->alias_set
)
2320 remove
= canon_true_dependence (store_info
->mem
,
2321 GET_MODE (store_info
->mem
),
2322 store_info
->mem_addr
,
2327 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2328 dump_insn_info ("removing from active", i_ptr
);
2330 active_local_stores_len
--;
2332 last
->next_local_store
= i_ptr
->next_local_store
;
2334 active_local_stores
= i_ptr
->next_local_store
;
2338 i_ptr
= i_ptr
->next_local_store
;
2343 /* A note_uses callback in which DATA points the INSN_INFO for
2344 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2345 true for any part of *LOC. */
2348 check_mem_read_use (rtx
*loc
, void *data
)
2350 subrtx_ptr_iterator::array_type array
;
2351 FOR_EACH_SUBRTX_PTR (iter
, array
, loc
, NONCONST
)
2355 check_mem_read_rtx (loc
, (bb_info_t
) data
);
2360 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2361 So far it only handles arguments passed in registers. */
2364 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2366 CUMULATIVE_ARGS args_so_far_v
;
2367 cumulative_args_t args_so_far
;
2371 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2372 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2374 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2376 arg
!= void_list_node
&& idx
< nargs
;
2377 arg
= TREE_CHAIN (arg
), idx
++)
2379 machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2381 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2382 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2383 || GET_MODE_CLASS (mode
) != MODE_INT
)
2386 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2388 link
= XEXP (link
, 1))
2389 if (GET_CODE (XEXP (link
, 0)) == USE
)
2391 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2392 if (REG_P (args
[idx
])
2393 && REGNO (args
[idx
]) == REGNO (reg
)
2394 && (GET_MODE (args
[idx
]) == mode
2395 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2396 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2398 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2399 > GET_MODE_SIZE (mode
)))))
2405 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2406 if (GET_MODE (args
[idx
]) != mode
)
2408 if (!tmp
|| !CONST_INT_P (tmp
))
2410 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2415 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2417 if (arg
!= void_list_node
|| idx
!= nargs
)
2422 /* Return a bitmap of the fixed registers contained in IN. */
2425 copy_fixed_regs (const_bitmap in
)
2429 ret
= ALLOC_REG_SET (NULL
);
2430 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2434 /* Apply record_store to all candidate stores in INSN. Mark INSN
2435 if some part of it is not a candidate store and assigns to a
2436 non-register target. */
2439 scan_insn (bb_info_t bb_info
, rtx_insn
*insn
)
2442 insn_info_type
*insn_info
= insn_info_type_pool
.allocate ();
2444 memset (insn_info
, 0, sizeof (struct insn_info_type
));
2446 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2447 fprintf (dump_file
, "\n**scanning insn=%d\n",
2450 insn_info
->prev_insn
= bb_info
->last_insn
;
2451 insn_info
->insn
= insn
;
2452 bb_info
->last_insn
= insn_info
;
2454 if (DEBUG_INSN_P (insn
))
2456 insn_info
->cannot_delete
= true;
2460 /* Look at all of the uses in the insn. */
2461 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2466 tree memset_call
= NULL_TREE
;
2468 insn_info
->cannot_delete
= true;
2470 /* Const functions cannot do anything bad i.e. read memory,
2471 however, they can read their parameters which may have
2472 been pushed onto the stack.
2473 memset and bzero don't read memory either. */
2474 const_call
= RTL_CONST_CALL_P (insn
);
2477 rtx call
= get_call_rtx_from (insn
);
2478 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2480 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2481 if (SYMBOL_REF_DECL (symbol
)
2482 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2484 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2486 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2487 == BUILT_IN_MEMSET
))
2488 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2489 memset_call
= SYMBOL_REF_DECL (symbol
);
2493 if (const_call
|| memset_call
)
2495 insn_info_t i_ptr
= active_local_stores
;
2496 insn_info_t last
= NULL
;
2498 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2499 fprintf (dump_file
, "%s call %d\n",
2500 const_call
? "const" : "memset", INSN_UID (insn
));
2502 /* See the head comment of the frame_read field. */
2503 if (reload_completed
2504 /* Tail calls are storing their arguments using
2505 arg pointer. If it is a frame pointer on the target,
2506 even before reload we need to kill frame pointer based
2508 || (SIBLING_CALL_P (insn
)
2509 && HARD_FRAME_POINTER_IS_ARG_POINTER
))
2510 insn_info
->frame_read
= true;
2512 /* Loop over the active stores and remove those which are
2513 killed by the const function call. */
2516 bool remove_store
= false;
2518 /* The stack pointer based stores are always killed. */
2519 if (i_ptr
->stack_pointer_based
)
2520 remove_store
= true;
2522 /* If the frame is read, the frame related stores are killed. */
2523 else if (insn_info
->frame_read
)
2525 store_info
*store_info
= i_ptr
->store_rec
;
2527 /* Skip the clobbers. */
2528 while (!store_info
->is_set
)
2529 store_info
= store_info
->next
;
2531 if (store_info
->group_id
>= 0
2532 && rtx_group_vec
[store_info
->group_id
]->frame_related
)
2533 remove_store
= true;
2538 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2539 dump_insn_info ("removing from active", i_ptr
);
2541 active_local_stores_len
--;
2543 last
->next_local_store
= i_ptr
->next_local_store
;
2545 active_local_stores
= i_ptr
->next_local_store
;
2550 i_ptr
= i_ptr
->next_local_store
;
2556 if (get_call_args (insn
, memset_call
, args
, 3)
2557 && CONST_INT_P (args
[1])
2558 && CONST_INT_P (args
[2])
2559 && INTVAL (args
[2]) > 0)
2561 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2562 set_mem_size (mem
, INTVAL (args
[2]));
2563 body
= gen_rtx_SET (mem
, args
[1]);
2564 mems_found
+= record_store (body
, bb_info
);
2565 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2566 fprintf (dump_file
, "handling memset as BLKmode store\n");
2567 if (mems_found
== 1)
2569 if (active_local_stores_len
++
2570 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2572 active_local_stores_len
= 1;
2573 active_local_stores
= NULL
;
2575 insn_info
->fixed_regs_live
2576 = copy_fixed_regs (bb_info
->regs_live
);
2577 insn_info
->next_local_store
= active_local_stores
;
2578 active_local_stores
= insn_info
;
2583 else if (SIBLING_CALL_P (insn
) && reload_completed
)
2584 /* Arguments for a sibling call that are pushed to memory are passed
2585 using the incoming argument pointer of the current function. After
2586 reload that might be (and likely is) frame pointer based. */
2587 add_wild_read (bb_info
);
2589 /* Every other call, including pure functions, may read any memory
2590 that is not relative to the frame. */
2591 add_non_frame_wild_read (bb_info
);
2596 /* Assuming that there are sets in these insns, we cannot delete
2598 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2599 || volatile_refs_p (PATTERN (insn
))
2600 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2601 || (RTX_FRAME_RELATED_P (insn
))
2602 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2603 insn_info
->cannot_delete
= true;
2605 body
= PATTERN (insn
);
2606 if (GET_CODE (body
) == PARALLEL
)
2609 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2610 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2613 mems_found
+= record_store (body
, bb_info
);
2615 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2616 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2617 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2619 /* If we found some sets of mems, add it into the active_local_stores so
2620 that it can be locally deleted if found dead or used for
2621 replace_read and redundant constant store elimination. Otherwise mark
2622 it as cannot delete. This simplifies the processing later. */
2623 if (mems_found
== 1)
2625 if (active_local_stores_len
++
2626 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2628 active_local_stores_len
= 1;
2629 active_local_stores
= NULL
;
2631 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2632 insn_info
->next_local_store
= active_local_stores
;
2633 active_local_stores
= insn_info
;
2636 insn_info
->cannot_delete
= true;
2640 /* Remove BASE from the set of active_local_stores. This is a
2641 callback from cselib that is used to get rid of the stores in
2642 active_local_stores. */
2645 remove_useless_values (cselib_val
*base
)
2647 insn_info_t insn_info
= active_local_stores
;
2648 insn_info_t last
= NULL
;
2652 store_info
*store_info
= insn_info
->store_rec
;
2655 /* If ANY of the store_infos match the cselib group that is
2656 being deleted, then the insn can not be deleted. */
2659 if ((store_info
->group_id
== -1)
2660 && (store_info
->cse_base
== base
))
2665 store_info
= store_info
->next
;
2670 active_local_stores_len
--;
2672 last
->next_local_store
= insn_info
->next_local_store
;
2674 active_local_stores
= insn_info
->next_local_store
;
2675 free_store_info (insn_info
);
2680 insn_info
= insn_info
->next_local_store
;
2685 /* Do all of step 1. */
2691 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2694 all_blocks
= BITMAP_ALLOC (NULL
);
2695 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2696 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2698 FOR_ALL_BB_FN (bb
, cfun
)
2701 bb_info_t bb_info
= dse_bb_info_type_pool
.allocate ();
2703 memset (bb_info
, 0, sizeof (dse_bb_info_type
));
2704 bitmap_set_bit (all_blocks
, bb
->index
);
2705 bb_info
->regs_live
= regs_live
;
2707 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2708 df_simulate_initialize_forwards (bb
, regs_live
);
2710 bb_table
[bb
->index
] = bb_info
;
2711 cselib_discard_hook
= remove_useless_values
;
2713 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2717 active_local_stores
= NULL
;
2718 active_local_stores_len
= 0;
2719 cselib_clear_table ();
2721 /* Scan the insns. */
2722 FOR_BB_INSNS (bb
, insn
)
2725 scan_insn (bb_info
, insn
);
2726 cselib_process_insn (insn
);
2728 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2731 /* This is something of a hack, because the global algorithm
2732 is supposed to take care of the case where stores go dead
2733 at the end of the function. However, the global
2734 algorithm must take a more conservative view of block
2735 mode reads than the local alg does. So to get the case
2736 where you have a store to the frame followed by a non
2737 overlapping block more read, we look at the active local
2738 stores at the end of the function and delete all of the
2739 frame and spill based ones. */
2740 if (stores_off_frame_dead_at_return
2741 && (EDGE_COUNT (bb
->succs
) == 0
2742 || (single_succ_p (bb
)
2743 && single_succ (bb
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
2744 && ! crtl
->calls_eh_return
)))
2746 insn_info_t i_ptr
= active_local_stores
;
2749 store_info
*store_info
= i_ptr
->store_rec
;
2751 /* Skip the clobbers. */
2752 while (!store_info
->is_set
)
2753 store_info
= store_info
->next
;
2754 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2755 delete_dead_store_insn (i_ptr
);
2757 if (store_info
->group_id
>= 0)
2760 = rtx_group_vec
[store_info
->group_id
];
2761 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2762 delete_dead_store_insn (i_ptr
);
2765 i_ptr
= i_ptr
->next_local_store
;
2769 /* Get rid of the loads that were discovered in
2770 replace_read. Cselib is finished with this block. */
2771 while (deferred_change_list
)
2773 deferred_change
*next
= deferred_change_list
->next
;
2775 /* There is no reason to validate this change. That was
2777 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2778 deferred_change_pool
.remove (deferred_change_list
);
2779 deferred_change_list
= next
;
2782 /* Get rid of all of the cselib based store_infos in this
2783 block and mark the containing insns as not being
2785 ptr
= bb_info
->last_insn
;
2788 if (ptr
->contains_cselib_groups
)
2790 store_info
*s_info
= ptr
->store_rec
;
2791 while (s_info
&& !s_info
->is_set
)
2792 s_info
= s_info
->next
;
2794 && s_info
->redundant_reason
2795 && s_info
->redundant_reason
->insn
2796 && !ptr
->cannot_delete
)
2798 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2799 fprintf (dump_file
, "Locally deleting insn %d "
2800 "because insn %d stores the "
2801 "same value and couldn't be "
2803 INSN_UID (ptr
->insn
),
2804 INSN_UID (s_info
->redundant_reason
->insn
));
2805 delete_dead_store_insn (ptr
);
2807 free_store_info (ptr
);
2813 /* Free at least positions_needed bitmaps. */
2814 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2815 if (s_info
->is_large
)
2817 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2818 s_info
->is_large
= false;
2821 ptr
= ptr
->prev_insn
;
2824 cse_store_info_pool
.release ();
2826 bb_info
->regs_live
= NULL
;
2829 BITMAP_FREE (regs_live
);
2831 rtx_group_table
->empty ();
2835 /*----------------------------------------------------------------------------
2838 Assign each byte position in the stores that we are going to
2839 analyze globally to a position in the bitmaps. Returns true if
2840 there are any bit positions assigned.
2841 ----------------------------------------------------------------------------*/
2844 dse_step2_init (void)
2849 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2851 /* For all non stack related bases, we only consider a store to
2852 be deletable if there are two or more stores for that
2853 position. This is because it takes one store to make the
2854 other store redundant. However, for the stores that are
2855 stack related, we consider them if there is only one store
2856 for the position. We do this because the stack related
2857 stores can be deleted if their is no read between them and
2858 the end of the function.
2860 To make this work in the current framework, we take the stack
2861 related bases add all of the bits from store1 into store2.
2862 This has the effect of making the eligible even if there is
2865 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2867 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2868 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2869 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2870 fprintf (dump_file
, "group %d is frame related ", i
);
2873 group
->offset_map_size_n
++;
2874 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2875 group
->offset_map_size_n
);
2876 group
->offset_map_size_p
++;
2877 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2878 group
->offset_map_size_p
);
2879 group
->process_globally
= false;
2880 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2882 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2883 (int)bitmap_count_bits (group
->store2_n
),
2884 (int)bitmap_count_bits (group
->store2_p
));
2885 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2886 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2892 /* Init the offset tables for the normal case. */
2895 dse_step2_nospill (void)
2899 /* Position 0 is unused because 0 is used in the maps to mean
2901 current_position
= 1;
2902 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2907 if (group
== clear_alias_group
)
2910 memset (group
->offset_map_n
, 0, sizeof (int) * group
->offset_map_size_n
);
2911 memset (group
->offset_map_p
, 0, sizeof (int) * group
->offset_map_size_p
);
2912 bitmap_clear (group
->group_kill
);
2914 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2916 bitmap_set_bit (group
->group_kill
, current_position
);
2917 if (bitmap_bit_p (group
->escaped_n
, j
))
2918 bitmap_set_bit (kill_on_calls
, current_position
);
2919 group
->offset_map_n
[j
] = current_position
++;
2920 group
->process_globally
= true;
2922 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2924 bitmap_set_bit (group
->group_kill
, current_position
);
2925 if (bitmap_bit_p (group
->escaped_p
, j
))
2926 bitmap_set_bit (kill_on_calls
, current_position
);
2927 group
->offset_map_p
[j
] = current_position
++;
2928 group
->process_globally
= true;
2931 return current_position
!= 1;
2936 /*----------------------------------------------------------------------------
2939 Build the bit vectors for the transfer functions.
2940 ----------------------------------------------------------------------------*/
2943 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2947 get_bitmap_index (group_info
*group_info
, HOST_WIDE_INT offset
)
2951 HOST_WIDE_INT offset_p
= -offset
;
2952 if (offset_p
>= group_info
->offset_map_size_n
)
2954 return group_info
->offset_map_n
[offset_p
];
2958 if (offset
>= group_info
->offset_map_size_p
)
2960 return group_info
->offset_map_p
[offset
];
2965 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2969 scan_stores_nospill (store_info
*store_info
, bitmap gen
, bitmap kill
)
2974 group_info
*group_info
2975 = rtx_group_vec
[store_info
->group_id
];
2976 if (group_info
->process_globally
)
2977 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
2979 int index
= get_bitmap_index (group_info
, i
);
2982 bitmap_set_bit (gen
, index
);
2984 bitmap_clear_bit (kill
, index
);
2987 store_info
= store_info
->next
;
2992 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2996 scan_stores_spill (store_info
*store_info
, bitmap gen
, bitmap kill
)
3000 if (store_info
->alias_set
)
3002 int index
= get_bitmap_index (clear_alias_group
,
3003 store_info
->alias_set
);
3006 bitmap_set_bit (gen
, index
);
3008 bitmap_clear_bit (kill
, index
);
3011 store_info
= store_info
->next
;
3016 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3020 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3022 read_info_t read_info
= insn_info
->read_rec
;
3026 /* If this insn reads the frame, kill all the frame related stores. */
3027 if (insn_info
->frame_read
)
3029 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3030 if (group
->process_globally
&& group
->frame_related
)
3033 bitmap_ior_into (kill
, group
->group_kill
);
3034 bitmap_and_compl_into (gen
, group
->group_kill
);
3037 if (insn_info
->non_frame_wild_read
)
3039 /* Kill all non-frame related stores. Kill all stores of variables that
3042 bitmap_ior_into (kill
, kill_on_calls
);
3043 bitmap_and_compl_into (gen
, kill_on_calls
);
3044 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3045 if (group
->process_globally
&& !group
->frame_related
)
3048 bitmap_ior_into (kill
, group
->group_kill
);
3049 bitmap_and_compl_into (gen
, group
->group_kill
);
3054 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3056 if (group
->process_globally
)
3058 if (i
== read_info
->group_id
)
3060 if (read_info
->begin
> read_info
->end
)
3062 /* Begin > end for block mode reads. */
3064 bitmap_ior_into (kill
, group
->group_kill
);
3065 bitmap_and_compl_into (gen
, group
->group_kill
);
3069 /* The groups are the same, just process the
3072 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3074 int index
= get_bitmap_index (group
, j
);
3078 bitmap_set_bit (kill
, index
);
3079 bitmap_clear_bit (gen
, index
);
3086 /* The groups are different, if the alias sets
3087 conflict, clear the entire group. We only need
3088 to apply this test if the read_info is a cselib
3089 read. Anything with a constant base cannot alias
3090 something else with a different constant
3092 if ((read_info
->group_id
< 0)
3093 && canon_true_dependence (group
->base_mem
,
3094 GET_MODE (group
->base_mem
),
3095 group
->canon_base_addr
,
3096 read_info
->mem
, NULL_RTX
))
3099 bitmap_ior_into (kill
, group
->group_kill
);
3100 bitmap_and_compl_into (gen
, group
->group_kill
);
3106 read_info
= read_info
->next
;
3110 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3114 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3118 if (read_info
->alias_set
)
3120 int index
= get_bitmap_index (clear_alias_group
,
3121 read_info
->alias_set
);
3125 bitmap_set_bit (kill
, index
);
3126 bitmap_clear_bit (gen
, index
);
3130 read_info
= read_info
->next
;
3135 /* Return the insn in BB_INFO before the first wild read or if there
3136 are no wild reads in the block, return the last insn. */
3139 find_insn_before_first_wild_read (bb_info_t bb_info
)
3141 insn_info_t insn_info
= bb_info
->last_insn
;
3142 insn_info_t last_wild_read
= NULL
;
3146 if (insn_info
->wild_read
)
3148 last_wild_read
= insn_info
->prev_insn
;
3149 /* Block starts with wild read. */
3150 if (!last_wild_read
)
3154 insn_info
= insn_info
->prev_insn
;
3158 return last_wild_read
;
3160 return bb_info
->last_insn
;
3164 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3165 the block in order to build the gen and kill sets for the block.
3166 We start at ptr which may be the last insn in the block or may be
3167 the first insn with a wild read. In the latter case we are able to
3168 skip the rest of the block because it just does not matter:
3169 anything that happens is hidden by the wild read. */
3172 dse_step3_scan (bool for_spills
, basic_block bb
)
3174 bb_info_t bb_info
= bb_table
[bb
->index
];
3175 insn_info_t insn_info
;
3178 /* There are no wild reads in the spill case. */
3179 insn_info
= bb_info
->last_insn
;
3181 insn_info
= find_insn_before_first_wild_read (bb_info
);
3183 /* In the spill case or in the no_spill case if there is no wild
3184 read in the block, we will need a kill set. */
3185 if (insn_info
== bb_info
->last_insn
)
3188 bitmap_clear (bb_info
->kill
);
3190 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3194 BITMAP_FREE (bb_info
->kill
);
3198 /* There may have been code deleted by the dce pass run before
3200 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3202 /* Process the read(s) last. */
3205 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3206 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3210 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3211 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3215 insn_info
= insn_info
->prev_insn
;
3220 /* Set the gen set of the exit block, and also any block with no
3221 successors that does not have a wild read. */
3224 dse_step3_exit_block_scan (bb_info_t bb_info
)
3226 /* The gen set is all 0's for the exit block except for the
3227 frame_pointer_group. */
3229 if (stores_off_frame_dead_at_return
)
3234 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3236 if (group
->process_globally
&& group
->frame_related
)
3237 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3243 /* Find all of the blocks that are not backwards reachable from the
3244 exit block or any block with no successors (BB). These are the
3245 infinite loops or infinite self loops. These blocks will still
3246 have their bits set in UNREACHABLE_BLOCKS. */
3249 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3254 if (bitmap_bit_p (unreachable_blocks
, bb
->index
))
3256 bitmap_clear_bit (unreachable_blocks
, bb
->index
);
3257 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3259 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3264 /* Build the transfer functions for the function. */
3267 dse_step3 (bool for_spills
)
3270 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
3271 sbitmap_iterator sbi
;
3272 bitmap all_ones
= NULL
;
3275 bitmap_ones (unreachable_blocks
);
3277 FOR_ALL_BB_FN (bb
, cfun
)
3279 bb_info_t bb_info
= bb_table
[bb
->index
];
3281 bitmap_clear (bb_info
->gen
);
3283 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3285 if (bb
->index
== ENTRY_BLOCK
)
3287 else if (bb
->index
== EXIT_BLOCK
)
3288 dse_step3_exit_block_scan (bb_info
);
3290 dse_step3_scan (for_spills
, bb
);
3291 if (EDGE_COUNT (bb
->succs
) == 0)
3292 mark_reachable_blocks (unreachable_blocks
, bb
);
3294 /* If this is the second time dataflow is run, delete the old
3297 BITMAP_FREE (bb_info
->in
);
3299 BITMAP_FREE (bb_info
->out
);
3302 /* For any block in an infinite loop, we must initialize the out set
3303 to all ones. This could be expensive, but almost never occurs in
3304 practice. However, it is common in regression tests. */
3305 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks
, 0, i
, sbi
)
3307 if (bitmap_bit_p (all_blocks
, i
))
3309 bb_info_t bb_info
= bb_table
[i
];
3315 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3316 FOR_EACH_VEC_ELT (rtx_group_vec
, j
, group
)
3317 bitmap_ior_into (all_ones
, group
->group_kill
);
3321 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3322 bitmap_copy (bb_info
->out
, all_ones
);
3328 BITMAP_FREE (all_ones
);
3329 sbitmap_free (unreachable_blocks
);
3334 /*----------------------------------------------------------------------------
3337 Solve the bitvector equations.
3338 ----------------------------------------------------------------------------*/
3341 /* Confluence function for blocks with no successors. Create an out
3342 set from the gen set of the exit block. This block logically has
3343 the exit block as a successor. */
3348 dse_confluence_0 (basic_block bb
)
3350 bb_info_t bb_info
= bb_table
[bb
->index
];
3352 if (bb
->index
== EXIT_BLOCK
)
3357 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3358 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3362 /* Propagate the information from the in set of the dest of E to the
3363 out set of the src of E. If the various in or out sets are not
3364 there, that means they are all ones. */
3367 dse_confluence_n (edge e
)
3369 bb_info_t src_info
= bb_table
[e
->src
->index
];
3370 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3375 bitmap_and_into (src_info
->out
, dest_info
->in
);
3378 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3379 bitmap_copy (src_info
->out
, dest_info
->in
);
3386 /* Propagate the info from the out to the in set of BB_INDEX's basic
3387 block. There are three cases:
3389 1) The block has no kill set. In this case the kill set is all
3390 ones. It does not matter what the out set of the block is, none of
3391 the info can reach the top. The only thing that reaches the top is
3392 the gen set and we just copy the set.
3394 2) There is a kill set but no out set and bb has successors. In
3395 this case we just return. Eventually an out set will be created and
3396 it is better to wait than to create a set of ones.
3398 3) There is both a kill and out set. We apply the obvious transfer
3403 dse_transfer_function (int bb_index
)
3405 bb_info_t bb_info
= bb_table
[bb_index
];
3413 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3414 bb_info
->out
, bb_info
->kill
);
3417 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3418 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3419 bb_info
->out
, bb_info
->kill
);
3429 /* Case 1 above. If there is already an in set, nothing
3435 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3436 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3442 /* Solve the dataflow equations. */
3447 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3448 dse_confluence_n
, dse_transfer_function
,
3449 all_blocks
, df_get_postorder (DF_BACKWARD
),
3450 df_get_n_blocks (DF_BACKWARD
));
3451 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3455 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3456 FOR_ALL_BB_FN (bb
, cfun
)
3458 bb_info_t bb_info
= bb_table
[bb
->index
];
3460 df_print_bb_index (bb
, dump_file
);
3462 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3464 fprintf (dump_file
, " in: *MISSING*\n");
3466 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3468 fprintf (dump_file
, " gen: *MISSING*\n");
3470 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3472 fprintf (dump_file
, " kill: *MISSING*\n");
3474 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3476 fprintf (dump_file
, " out: *MISSING*\n\n");
3483 /*----------------------------------------------------------------------------
3486 Delete the stores that can only be deleted using the global information.
3487 ----------------------------------------------------------------------------*/
3491 dse_step5_nospill (void)
3494 FOR_EACH_BB_FN (bb
, cfun
)
3496 bb_info_t bb_info
= bb_table
[bb
->index
];
3497 insn_info_t insn_info
= bb_info
->last_insn
;
3498 bitmap v
= bb_info
->out
;
3502 bool deleted
= false;
3503 if (dump_file
&& insn_info
->insn
)
3505 fprintf (dump_file
, "starting to process insn %d\n",
3506 INSN_UID (insn_info
->insn
));
3507 bitmap_print (dump_file
, v
, " v: ", "\n");
3510 /* There may have been code deleted by the dce pass run before
3513 && INSN_P (insn_info
->insn
)
3514 && (!insn_info
->cannot_delete
)
3515 && (!bitmap_empty_p (v
)))
3517 store_info
*store_info
= insn_info
->store_rec
;
3519 /* Try to delete the current insn. */
3522 /* Skip the clobbers. */
3523 while (!store_info
->is_set
)
3524 store_info
= store_info
->next
;
3526 if (store_info
->alias_set
)
3531 group_info
*group_info
3532 = rtx_group_vec
[store_info
->group_id
];
3534 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3536 int index
= get_bitmap_index (group_info
, i
);
3538 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3539 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3540 if (index
== 0 || !bitmap_bit_p (v
, index
))
3542 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3543 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3552 && check_for_inc_dec_1 (insn_info
))
3554 delete_insn (insn_info
->insn
);
3555 insn_info
->insn
= NULL
;
3560 /* We do want to process the local info if the insn was
3561 deleted. For instance, if the insn did a wild read, we
3562 no longer need to trash the info. */
3564 && INSN_P (insn_info
->insn
)
3567 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3568 if (insn_info
->wild_read
)
3570 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3571 fprintf (dump_file
, "wild read\n");
3574 else if (insn_info
->read_rec
3575 || insn_info
->non_frame_wild_read
)
3577 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3578 fprintf (dump_file
, "regular read\n");
3579 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3580 fprintf (dump_file
, "non-frame wild read\n");
3581 scan_reads_nospill (insn_info
, v
, NULL
);
3585 insn_info
= insn_info
->prev_insn
;
3592 /*----------------------------------------------------------------------------
3595 Delete stores made redundant by earlier stores (which store the same
3596 value) that couldn't be eliminated.
3597 ----------------------------------------------------------------------------*/
3604 FOR_ALL_BB_FN (bb
, cfun
)
3606 bb_info_t bb_info
= bb_table
[bb
->index
];
3607 insn_info_t insn_info
= bb_info
->last_insn
;
3611 /* There may have been code deleted by the dce pass run before
3614 && INSN_P (insn_info
->insn
)
3615 && !insn_info
->cannot_delete
)
3617 store_info
*s_info
= insn_info
->store_rec
;
3619 while (s_info
&& !s_info
->is_set
)
3620 s_info
= s_info
->next
;
3622 && s_info
->redundant_reason
3623 && s_info
->redundant_reason
->insn
3624 && INSN_P (s_info
->redundant_reason
->insn
))
3626 rtx_insn
*rinsn
= s_info
->redundant_reason
->insn
;
3627 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3628 fprintf (dump_file
, "Locally deleting insn %d "
3629 "because insn %d stores the "
3630 "same value and couldn't be "
3632 INSN_UID (insn_info
->insn
),
3634 delete_dead_store_insn (insn_info
);
3637 insn_info
= insn_info
->prev_insn
;
3642 /*----------------------------------------------------------------------------
3645 Destroy everything left standing.
3646 ----------------------------------------------------------------------------*/
3651 bitmap_obstack_release (&dse_bitmap_obstack
);
3652 obstack_free (&dse_obstack
, NULL
);
3654 end_alias_analysis ();
3656 delete rtx_group_table
;
3657 rtx_group_table
= NULL
;
3658 rtx_group_vec
.release ();
3659 BITMAP_FREE (all_blocks
);
3660 BITMAP_FREE (scratch
);
3662 rtx_store_info_pool
.release ();
3663 read_info_type_pool
.release ();
3664 insn_info_type_pool
.release ();
3665 dse_bb_info_type_pool
.release ();
3666 group_info_pool
.release ();
3667 deferred_change_pool
.release ();
3671 /* -------------------------------------------------------------------------
3673 ------------------------------------------------------------------------- */
3675 /* Callback for running pass_rtl_dse. */
3678 rest_of_handle_dse (void)
3680 df_set_flags (DF_DEFER_INSN_RESCAN
);
3682 /* Need the notes since we must track live hardregs in the forwards
3684 df_note_add_problem ();
3690 if (dse_step2_nospill ())
3692 df_set_flags (DF_LR_RUN_DCE
);
3694 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3695 fprintf (dump_file
, "doing global processing\n");
3698 dse_step5_nospill ();
3705 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3706 locally_deleted
, globally_deleted
, spill_deleted
);
3708 /* DSE can eliminate potentially-trapping MEMs.
3709 Remove any EH edges associated with them. */
3710 if ((locally_deleted
|| globally_deleted
)
3711 && cfun
->can_throw_non_call_exceptions
3712 && purge_all_dead_edges ())
3720 const pass_data pass_data_rtl_dse1
=
3722 RTL_PASS
, /* type */
3724 OPTGROUP_NONE
, /* optinfo_flags */
3725 TV_DSE1
, /* tv_id */
3726 0, /* properties_required */
3727 0, /* properties_provided */
3728 0, /* properties_destroyed */
3729 0, /* todo_flags_start */
3730 TODO_df_finish
, /* todo_flags_finish */
3733 class pass_rtl_dse1
: public rtl_opt_pass
3736 pass_rtl_dse1 (gcc::context
*ctxt
)
3737 : rtl_opt_pass (pass_data_rtl_dse1
, ctxt
)
3740 /* opt_pass methods: */
3741 virtual bool gate (function
*)
3743 return optimize
> 0 && flag_dse
&& dbg_cnt (dse1
);
3746 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3748 }; // class pass_rtl_dse1
3753 make_pass_rtl_dse1 (gcc::context
*ctxt
)
3755 return new pass_rtl_dse1 (ctxt
);
3760 const pass_data pass_data_rtl_dse2
=
3762 RTL_PASS
, /* type */
3764 OPTGROUP_NONE
, /* optinfo_flags */
3765 TV_DSE2
, /* tv_id */
3766 0, /* properties_required */
3767 0, /* properties_provided */
3768 0, /* properties_destroyed */
3769 0, /* todo_flags_start */
3770 TODO_df_finish
, /* todo_flags_finish */
3773 class pass_rtl_dse2
: public rtl_opt_pass
3776 pass_rtl_dse2 (gcc::context
*ctxt
)
3777 : rtl_opt_pass (pass_data_rtl_dse2
, ctxt
)
3780 /* opt_pass methods: */
3781 virtual bool gate (function
*)
3783 return optimize
> 0 && flag_dse
&& dbg_cnt (dse2
);
3786 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3788 }; // class pass_rtl_dse2
3793 make_pass_rtl_dse2 (gcc::context
*ctxt
)
3795 return new pass_rtl_dse2 (ctxt
);