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"
36 #include "gimple-ssa.h"
42 #include "stor-layout.h"
45 #include "tree-pass.h"
51 #include "cfgcleanup.h"
53 /* This file contains three techniques for performing Dead Store
56 * The first technique performs dse locally on any base address. It
57 is based on the cselib which is a local value numbering technique.
58 This technique is local to a basic block but deals with a fairly
61 * The second technique performs dse globally but is restricted to
62 base addresses that are either constant or are relative to the
65 * The third technique, (which is only done after register allocation)
66 processes the spill slots. This differs from the second
67 technique because it takes advantage of the fact that spilling is
68 completely free from the effects of aliasing.
70 Logically, dse is a backwards dataflow problem. A store can be
71 deleted if it if cannot be reached in the backward direction by any
72 use of the value being stored. However, the local technique uses a
73 forwards scan of the basic block because cselib requires that the
74 block be processed in that order.
76 The pass is logically broken into 7 steps:
80 1) The local algorithm, as well as scanning the insns for the two
83 2) Analysis to see if the global algs are necessary. In the case
84 of stores base on a constant address, there must be at least two
85 stores to that address, to make it possible to delete some of the
86 stores. In the case of stores off of the frame or spill related
87 stores, only one store to an address is necessary because those
88 stores die at the end of the function.
90 3) Set up the global dataflow equations based on processing the
91 info parsed in the first step.
93 4) Solve the dataflow equations.
95 5) Delete the insns that the global analysis has indicated are
98 6) Delete insns that store the same value as preceding store
99 where the earlier store couldn't be eliminated.
103 This step uses cselib and canon_rtx to build the largest expression
104 possible for each address. This pass is a forwards pass through
105 each basic block. From the point of view of the global technique,
106 the first pass could examine a block in either direction. The
107 forwards ordering is to accommodate cselib.
109 We make a simplifying assumption: addresses fall into four broad
112 1) base has rtx_varies_p == false, offset is constant.
113 2) base has rtx_varies_p == false, offset variable.
114 3) base has rtx_varies_p == true, offset constant.
115 4) base has rtx_varies_p == true, offset variable.
117 The local passes are able to process all 4 kinds of addresses. The
118 global pass only handles 1).
120 The global problem is formulated as follows:
122 A store, S1, to address A, where A is not relative to the stack
123 frame, can be eliminated if all paths from S1 to the end of the
124 function contain another store to A before a read to A.
126 If the address A is relative to the stack frame, a store S2 to A
127 can be eliminated if there are no paths from S2 that reach the
128 end of the function that read A before another store to A. In
129 this case S2 can be deleted if there are paths from S2 to the
130 end of the function that have no reads or writes to A. This
131 second case allows stores to the stack frame to be deleted that
132 would otherwise die when the function returns. This cannot be
133 done if stores_off_frame_dead_at_return is not true. See the doc
134 for that variable for when this variable is false.
136 The global problem is formulated as a backwards set union
137 dataflow problem where the stores are the gens and reads are the
138 kills. Set union problems are rare and require some special
139 handling given our representation of bitmaps. A straightforward
140 implementation requires a lot of bitmaps filled with 1s.
141 These are expensive and cumbersome in our bitmap formulation so
142 care has been taken to avoid large vectors filled with 1s. See
143 the comments in bb_info and in the dataflow confluence functions
146 There are two places for further enhancements to this algorithm:
148 1) The original dse which was embedded in a pass called flow also
149 did local address forwarding. For example in
154 flow would replace the right hand side of the second insn with a
155 reference to r100. Most of the information is available to add this
156 to this pass. It has not done it because it is a lot of work in
157 the case that either r100 is assigned to between the first and
158 second insn and/or the second insn is a load of part of the value
159 stored by the first insn.
161 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
162 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
163 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
164 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
166 2) The cleaning up of spill code is quite profitable. It currently
167 depends on reading tea leaves and chicken entrails left by reload.
168 This pass depends on reload creating a singleton alias set for each
169 spill slot and telling the next dse pass which of these alias sets
170 are the singletons. Rather than analyze the addresses of the
171 spills, dse's spill processing just does analysis of the loads and
172 stores that use those alias sets. There are three cases where this
175 a) Reload sometimes creates the slot for one mode of access, and
176 then inserts loads and/or stores for a smaller mode. In this
177 case, the current code just punts on the slot. The proper thing
178 to do is to back out and use one bit vector position for each
179 byte of the entity associated with the slot. This depends on
180 KNOWING that reload always generates the accesses for each of the
181 bytes in some canonical (read that easy to understand several
182 passes after reload happens) way.
184 b) Reload sometimes decides that spill slot it allocated was not
185 large enough for the mode and goes back and allocates more slots
186 with the same mode and alias set. The backout in this case is a
187 little more graceful than (a). In this case the slot is unmarked
188 as being a spill slot and if final address comes out to be based
189 off the frame pointer, the global algorithm handles this slot.
191 c) For any pass that may prespill, there is currently no
192 mechanism to tell the dse pass that the slot being used has the
193 special properties that reload uses. It may be that all that is
194 required is to have those passes make the same calls that reload
195 does, assuming that the alias sets can be manipulated in the same
198 /* There are limits to the size of constant offsets we model for the
199 global problem. There are certainly test cases, that exceed this
200 limit, however, it is unlikely that there are important programs
201 that really have constant offsets this size. */
202 #define MAX_OFFSET (64 * 1024)
204 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
205 on the default obstack because these bitmaps can grow quite large
206 (~2GB for the small (!) test case of PR54146) and we'll hold on to
207 all that memory until the end of the compiler run.
208 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
209 releasing the whole obstack. */
210 static bitmap_obstack dse_bitmap_obstack
;
212 /* Obstack for other data. As for above: Kinda nice to be able to
213 throw it all away at the end in one big sweep. */
214 static struct obstack dse_obstack
;
216 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
217 static bitmap scratch
= NULL
;
219 struct insn_info_type
;
221 /* This structure holds information about a candidate store. */
225 /* False means this is a clobber. */
228 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
231 /* The id of the mem group of the base address. If rtx_varies_p is
232 true, this is -1. Otherwise, it is the index into the group
236 /* This is the cselib value. */
237 cselib_val
*cse_base
;
239 /* This canonized mem. */
242 /* Canonized MEM address for use by canon_true_dependence. */
245 /* If this is non-zero, it is the alias set of a spill location. */
246 alias_set_type alias_set
;
248 /* The offset of the first and byte before the last byte associated
249 with the operation. */
250 HOST_WIDE_INT begin
, end
;
254 /* A bitmask as wide as the number of bytes in the word that
255 contains a 1 if the byte may be needed. The store is unused if
256 all of the bits are 0. This is used if IS_LARGE is false. */
257 unsigned HOST_WIDE_INT small_bitmask
;
261 /* A bitmap with one bit per byte. Cleared bit means the position
262 is needed. Used if IS_LARGE is false. */
265 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
266 equal to END - BEGIN, the whole store is unused. */
271 /* The next store info for this insn. */
272 struct store_info
*next
;
274 /* The right hand side of the store. This is used if there is a
275 subsequent reload of the mems address somewhere later in the
279 /* If rhs is or holds a constant, this contains that constant,
283 /* Set if this store stores the same constant value as REDUNDANT_REASON
284 insn stored. These aren't eliminated early, because doing that
285 might prevent the earlier larger store to be eliminated. */
286 struct insn_info_type
*redundant_reason
;
289 /* Return a bitmask with the first N low bits set. */
291 static unsigned HOST_WIDE_INT
292 lowpart_bitmask (int n
)
294 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
295 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
298 static object_allocator
<store_info
> cse_store_info_pool ("cse_store_info_pool");
300 static object_allocator
<store_info
> rtx_store_info_pool ("rtx_store_info_pool");
302 /* This structure holds information about a load. These are only
303 built for rtx bases. */
304 struct read_info_type
306 /* The id of the mem group of the base address. */
309 /* If this is non-zero, it is the alias set of a spill location. */
310 alias_set_type alias_set
;
312 /* The offset of the first and byte after the last byte associated
313 with the operation. If begin == end == 0, the read did not have
314 a constant offset. */
317 /* The mem being read. */
320 /* The next read_info for this insn. */
321 struct read_info_type
*next
;
323 typedef struct read_info_type
*read_info_t
;
325 static object_allocator
<read_info_type
> read_info_type_pool ("read_info_pool");
327 /* One of these records is created for each insn. */
329 struct insn_info_type
331 /* Set true if the insn contains a store but the insn itself cannot
332 be deleted. This is set if the insn is a parallel and there is
333 more than one non dead output or if the insn is in some way
337 /* This field is only used by the global algorithm. It is set true
338 if the insn contains any read of mem except for a (1). This is
339 also set if the insn is a call or has a clobber mem. If the insn
340 contains a wild read, the use_rec will be null. */
343 /* This is true only for CALL instructions which could potentially read
344 any non-frame memory location. This field is used by the global
346 bool non_frame_wild_read
;
348 /* This field is only used for the processing of const functions.
349 These functions cannot read memory, but they can read the stack
350 because that is where they may get their parms. We need to be
351 this conservative because, like the store motion pass, we don't
352 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
353 Moreover, we need to distinguish two cases:
354 1. Before reload (register elimination), the stores related to
355 outgoing arguments are stack pointer based and thus deemed
356 of non-constant base in this pass. This requires special
357 handling but also means that the frame pointer based stores
358 need not be killed upon encountering a const function call.
359 2. After reload, the stores related to outgoing arguments can be
360 either stack pointer or hard frame pointer based. This means
361 that we have no other choice than also killing all the frame
362 pointer based stores upon encountering a const function call.
363 This field is set after reload for const function calls and before
364 reload for const tail function calls on targets where arg pointer
365 is the frame pointer. Having this set is less severe than a wild
366 read, it just means that all the frame related stores are killed
367 rather than all the stores. */
370 /* This field is only used for the processing of const functions.
371 It is set if the insn may contain a stack pointer based store. */
372 bool stack_pointer_based
;
374 /* This is true if any of the sets within the store contains a
375 cselib base. Such stores can only be deleted by the local
377 bool contains_cselib_groups
;
382 /* The list of mem sets or mem clobbers that are contained in this
383 insn. If the insn is deletable, it contains only one mem set.
384 But it could also contain clobbers. Insns that contain more than
385 one mem set are not deletable, but each of those mems are here in
386 order to provide info to delete other insns. */
387 store_info
*store_rec
;
389 /* The linked list of mem uses in this insn. Only the reads from
390 rtx bases are listed here. The reads to cselib bases are
391 completely processed during the first scan and so are never
393 read_info_t read_rec
;
395 /* The live fixed registers. We assume only fixed registers can
396 cause trouble by being clobbered from an expanded pattern;
397 storing only the live fixed registers (rather than all registers)
398 means less memory needs to be allocated / copied for the individual
400 regset fixed_regs_live
;
402 /* The prev insn in the basic block. */
403 struct insn_info_type
* prev_insn
;
405 /* The linked list of insns that are in consideration for removal in
406 the forwards pass through the basic block. This pointer may be
407 trash as it is not cleared when a wild read occurs. The only
408 time it is guaranteed to be correct is when the traversal starts
409 at active_local_stores. */
410 struct insn_info_type
* next_local_store
;
412 typedef struct insn_info_type
*insn_info_t
;
414 static object_allocator
<insn_info_type
> insn_info_type_pool ("insn_info_pool");
416 /* The linked list of stores that are under consideration in this
418 static insn_info_t active_local_stores
;
419 static int active_local_stores_len
;
421 struct dse_bb_info_type
423 /* Pointer to the insn info for the last insn in the block. These
424 are linked so this is how all of the insns are reached. During
425 scanning this is the current insn being scanned. */
426 insn_info_t last_insn
;
428 /* The info for the global dataflow problem. */
431 /* This is set if the transfer function should and in the wild_read
432 bitmap before applying the kill and gen sets. That vector knocks
433 out most of the bits in the bitmap and thus speeds up the
435 bool apply_wild_read
;
437 /* The following 4 bitvectors hold information about which positions
438 of which stores are live or dead. They are indexed by
441 /* The set of store positions that exist in this block before a wild read. */
444 /* The set of load positions that exist in this block above the
445 same position of a store. */
448 /* The set of stores that reach the top of the block without being
451 Do not represent the in if it is all ones. Note that this is
452 what the bitvector should logically be initialized to for a set
453 intersection problem. However, like the kill set, this is too
454 expensive. So initially, the in set will only be created for the
455 exit block and any block that contains a wild read. */
458 /* The set of stores that reach the bottom of the block from it's
461 Do not represent the in if it is all ones. Note that this is
462 what the bitvector should logically be initialized to for a set
463 intersection problem. However, like the kill and in set, this is
464 too expensive. So what is done is that the confluence operator
465 just initializes the vector from one of the out sets of the
466 successors of the block. */
469 /* The following bitvector is indexed by the reg number. It
470 contains the set of regs that are live at the current instruction
471 being processed. While it contains info for all of the
472 registers, only the hard registers are actually examined. It is used
473 to assure that shift and/or add sequences that are inserted do not
474 accidentally clobber live hard regs. */
478 typedef struct dse_bb_info_type
*bb_info_t
;
480 static object_allocator
<dse_bb_info_type
> dse_bb_info_type_pool
483 /* Table to hold all bb_infos. */
484 static bb_info_t
*bb_table
;
486 /* There is a group_info for each rtx base that is used to reference
487 memory. There are also not many of the rtx bases because they are
488 very limited in scope. */
492 /* The actual base of the address. */
495 /* The sequential id of the base. This allows us to have a
496 canonical ordering of these that is not based on addresses. */
499 /* True if there are any positions that are to be processed
501 bool process_globally
;
503 /* True if the base of this group is either the frame_pointer or
504 hard_frame_pointer. */
507 /* A mem wrapped around the base pointer for the group in order to do
508 read dependency. It must be given BLKmode in order to encompass all
509 the possible offsets from the base. */
512 /* Canonized version of base_mem's address. */
515 /* These two sets of two bitmaps are used to keep track of how many
516 stores are actually referencing that position from this base. We
517 only do this for rtx bases as this will be used to assign
518 positions in the bitmaps for the global problem. Bit N is set in
519 store1 on the first store for offset N. Bit N is set in store2
520 for the second store to offset N. This is all we need since we
521 only care about offsets that have two or more stores for them.
523 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
524 for 0 and greater offsets.
526 There is one special case here, for stores into the stack frame,
527 we will or store1 into store2 before deciding which stores look
528 at globally. This is because stores to the stack frame that have
529 no other reads before the end of the function can also be
531 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
533 /* These bitmaps keep track of offsets in this group escape this function.
534 An offset escapes if it corresponds to a named variable whose
535 addressable flag is set. */
536 bitmap escaped_n
, escaped_p
;
538 /* The positions in this bitmap have the same assignments as the in,
539 out, gen and kill bitmaps. This bitmap is all zeros except for
540 the positions that are occupied by stores for this group. */
543 /* The offset_map is used to map the offsets from this base into
544 positions in the global bitmaps. It is only created after all of
545 the all of stores have been scanned and we know which ones we
547 int *offset_map_n
, *offset_map_p
;
548 int offset_map_size_n
, offset_map_size_p
;
551 static object_allocator
<group_info
> group_info_pool ("rtx_group_info_pool");
553 /* Index into the rtx_group_vec. */
554 static int rtx_group_next_id
;
557 static vec
<group_info
*> rtx_group_vec
;
560 /* This structure holds the set of changes that are being deferred
561 when removing read operation. See replace_read. */
562 struct deferred_change
565 /* The mem that is being replaced. */
568 /* The reg it is being replaced with. */
571 struct deferred_change
*next
;
574 static object_allocator
<deferred_change
> deferred_change_pool
575 ("deferred_change_pool");
577 static deferred_change
*deferred_change_list
= NULL
;
579 /* The group that holds all of the clear_alias_sets. */
580 static group_info
*clear_alias_group
;
582 /* The modes of the clear_alias_sets. */
583 static htab_t clear_alias_mode_table
;
585 /* Hash table element to look up the mode for an alias set. */
586 struct clear_alias_mode_holder
588 alias_set_type alias_set
;
592 /* This is true except if cfun->stdarg -- i.e. we cannot do
593 this for vararg functions because they play games with the frame. */
594 static bool stores_off_frame_dead_at_return
;
596 /* Counter for stats. */
597 static int globally_deleted
;
598 static int locally_deleted
;
599 static int spill_deleted
;
601 static bitmap all_blocks
;
603 /* Locations that are killed by calls in the global phase. */
604 static bitmap kill_on_calls
;
606 /* The number of bits used in the global bitmaps. */
607 static unsigned int current_position
;
609 /*----------------------------------------------------------------------------
613 ----------------------------------------------------------------------------*/
616 /* Find the entry associated with ALIAS_SET. */
618 static struct clear_alias_mode_holder
*
619 clear_alias_set_lookup (alias_set_type alias_set
)
621 struct clear_alias_mode_holder tmp_holder
;
624 tmp_holder
.alias_set
= alias_set
;
625 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
628 return (struct clear_alias_mode_holder
*) *slot
;
632 /* Hashtable callbacks for maintaining the "bases" field of
633 store_group_info, given that the addresses are function invariants. */
635 struct invariant_group_base_hasher
: nofree_ptr_hash
<group_info
>
637 static inline hashval_t
hash (const group_info
*);
638 static inline bool equal (const group_info
*, const group_info
*);
642 invariant_group_base_hasher::equal (const group_info
*gi1
,
643 const group_info
*gi2
)
645 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
649 invariant_group_base_hasher::hash (const group_info
*gi
)
652 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
655 /* Tables of group_info structures, hashed by base value. */
656 static hash_table
<invariant_group_base_hasher
> *rtx_group_table
;
659 /* Get the GROUP for BASE. Add a new group if it is not there. */
662 get_group_info (rtx base
)
664 struct group_info tmp_gi
;
670 /* Find the store_base_info structure for BASE, creating a new one
672 tmp_gi
.rtx_base
= base
;
673 slot
= rtx_group_table
->find_slot (&tmp_gi
, INSERT
);
678 if (!clear_alias_group
)
680 clear_alias_group
= gi
= group_info_pool
.allocate ();
681 memset (gi
, 0, sizeof (struct group_info
));
682 gi
->id
= rtx_group_next_id
++;
683 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
684 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
685 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
686 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
687 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
688 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
689 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
690 gi
->process_globally
= false;
691 gi
->offset_map_size_n
= 0;
692 gi
->offset_map_size_p
= 0;
693 gi
->offset_map_n
= NULL
;
694 gi
->offset_map_p
= NULL
;
695 rtx_group_vec
.safe_push (gi
);
697 return clear_alias_group
;
702 *slot
= gi
= group_info_pool
.allocate ();
704 gi
->id
= rtx_group_next_id
++;
705 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
706 gi
->canon_base_addr
= canon_rtx (base
);
707 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
708 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
709 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
710 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
711 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
712 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
713 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
714 gi
->process_globally
= false;
716 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
717 gi
->offset_map_size_n
= 0;
718 gi
->offset_map_size_p
= 0;
719 gi
->offset_map_n
= NULL
;
720 gi
->offset_map_p
= NULL
;
721 rtx_group_vec
.safe_push (gi
);
728 /* Initialization of data structures. */
734 globally_deleted
= 0;
737 bitmap_obstack_initialize (&dse_bitmap_obstack
);
738 gcc_obstack_init (&dse_obstack
);
740 scratch
= BITMAP_ALLOC (®_obstack
);
741 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
744 rtx_group_table
= new hash_table
<invariant_group_base_hasher
> (11);
746 bb_table
= XNEWVEC (bb_info_t
, last_basic_block_for_fn (cfun
));
747 rtx_group_next_id
= 0;
749 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
751 init_alias_analysis ();
753 clear_alias_group
= NULL
;
758 /*----------------------------------------------------------------------------
761 Scan all of the insns. Any random ordering of the blocks is fine.
762 Each block is scanned in forward order to accommodate cselib which
763 is used to remove stores with non-constant bases.
764 ----------------------------------------------------------------------------*/
766 /* Delete all of the store_info recs from INSN_INFO. */
769 free_store_info (insn_info_t insn_info
)
771 store_info
*cur
= insn_info
->store_rec
;
774 store_info
*next
= cur
->next
;
776 BITMAP_FREE (cur
->positions_needed
.large
.bmap
);
778 cse_store_info_pool
.remove (cur
);
780 rtx_store_info_pool
.remove (cur
);
784 insn_info
->cannot_delete
= true;
785 insn_info
->contains_cselib_groups
= false;
786 insn_info
->store_rec
= NULL
;
789 struct note_add_store_info
791 rtx_insn
*first
, *current
;
792 regset fixed_regs_live
;
796 /* Callback for emit_inc_dec_insn_before via note_stores.
797 Check if a register is clobbered which is live afterwards. */
800 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
803 note_add_store_info
*info
= (note_add_store_info
*) data
;
808 /* If this register is referenced by the current or an earlier insn,
809 that's OK. E.g. this applies to the register that is being incremented
810 with this addition. */
811 for (insn
= info
->first
;
812 insn
!= NEXT_INSN (info
->current
);
813 insn
= NEXT_INSN (insn
))
814 if (reg_referenced_p (loc
, PATTERN (insn
)))
817 /* If we come here, we have a clobber of a register that's only OK
818 if that register is not live. If we don't have liveness information
819 available, fail now. */
820 if (!info
->fixed_regs_live
)
822 info
->failure
= true;
825 /* Now check if this is a live fixed register. */
826 unsigned int end_regno
= END_REGNO (loc
);
827 for (unsigned int regno
= REGNO (loc
); regno
< end_regno
; ++regno
)
828 if (REGNO_REG_SET_P (info
->fixed_regs_live
, regno
))
829 info
->failure
= true;
832 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
833 SRC + SRCOFF before insn ARG. */
836 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
837 rtx op ATTRIBUTE_UNUSED
,
838 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
840 insn_info_t insn_info
= (insn_info_t
) arg
;
841 rtx_insn
*insn
= insn_info
->insn
, *new_insn
, *cur
;
842 note_add_store_info info
;
844 /* We can reuse all operands without copying, because we are about
845 to delete the insn that contained it. */
849 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
850 new_insn
= get_insns ();
854 new_insn
= gen_move_insn (dest
, src
);
855 info
.first
= new_insn
;
856 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
857 info
.failure
= false;
858 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
861 note_stores (PATTERN (cur
), note_add_store
, &info
);
864 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
865 return it immediately, communicating the failure to its caller. */
869 emit_insn_before (new_insn
, insn
);
874 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
875 is there, is split into a separate insn.
876 Return true on success (or if there was nothing to do), false on failure. */
879 check_for_inc_dec_1 (insn_info_t insn_info
)
881 rtx_insn
*insn
= insn_info
->insn
;
882 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
884 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
890 /* Entry point for postreload. If you work on reload_cse, or you need this
891 anywhere else, consider if you can provide register liveness information
892 and add a parameter to this function so that it can be passed down in
893 insn_info.fixed_regs_live. */
895 check_for_inc_dec (rtx_insn
*insn
)
897 insn_info_type insn_info
;
900 insn_info
.insn
= insn
;
901 insn_info
.fixed_regs_live
= NULL
;
902 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
904 return for_each_inc_dec (PATTERN (insn
), emit_inc_dec_insn_before
,
909 /* Delete the insn and free all of the fields inside INSN_INFO. */
912 delete_dead_store_insn (insn_info_t insn_info
)
914 read_info_t read_info
;
919 if (!check_for_inc_dec_1 (insn_info
))
921 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
923 fprintf (dump_file
, "Locally deleting insn %d ",
924 INSN_UID (insn_info
->insn
));
925 if (insn_info
->store_rec
->alias_set
)
926 fprintf (dump_file
, "alias set %d\n",
927 (int) insn_info
->store_rec
->alias_set
);
929 fprintf (dump_file
, "\n");
932 free_store_info (insn_info
);
933 read_info
= insn_info
->read_rec
;
937 read_info_t next
= read_info
->next
;
938 read_info_type_pool
.remove (read_info
);
941 insn_info
->read_rec
= NULL
;
943 delete_insn (insn_info
->insn
);
945 insn_info
->insn
= NULL
;
947 insn_info
->wild_read
= false;
950 /* Return whether DECL, a local variable, can possibly escape the current
954 local_variable_can_escape (tree decl
)
956 if (TREE_ADDRESSABLE (decl
))
959 /* If this is a partitioned variable, we need to consider all the variables
960 in the partition. This is necessary because a store into one of them can
961 be replaced with a store into another and this may not change the outcome
962 of the escape analysis. */
963 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
965 tree
*namep
= cfun
->gimple_df
->decls_to_pointers
->get (decl
);
967 return TREE_ADDRESSABLE (*namep
);
973 /* Return whether EXPR can possibly escape the current function scope. */
976 can_escape (tree expr
)
981 base
= get_base_address (expr
);
983 && !may_be_aliased (base
)
984 && !(TREE_CODE (base
) == VAR_DECL
985 && !DECL_EXTERNAL (base
)
986 && !TREE_STATIC (base
)
987 && local_variable_can_escape (base
)))
992 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
996 set_usage_bits (group_info
*group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1000 bool expr_escapes
= can_escape (expr
);
1001 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1002 for (i
=offset
; i
<offset
+width
; i
++)
1010 store1
= group
->store1_n
;
1011 store2
= group
->store2_n
;
1012 escaped
= group
->escaped_n
;
1017 store1
= group
->store1_p
;
1018 store2
= group
->store2_p
;
1019 escaped
= group
->escaped_p
;
1023 if (!bitmap_set_bit (store1
, ai
))
1024 bitmap_set_bit (store2
, ai
);
1029 if (group
->offset_map_size_n
< ai
)
1030 group
->offset_map_size_n
= ai
;
1034 if (group
->offset_map_size_p
< ai
)
1035 group
->offset_map_size_p
= ai
;
1039 bitmap_set_bit (escaped
, ai
);
1044 reset_active_stores (void)
1046 active_local_stores
= NULL
;
1047 active_local_stores_len
= 0;
1050 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1053 free_read_records (bb_info_t bb_info
)
1055 insn_info_t insn_info
= bb_info
->last_insn
;
1056 read_info_t
*ptr
= &insn_info
->read_rec
;
1059 read_info_t next
= (*ptr
)->next
;
1060 if ((*ptr
)->alias_set
== 0)
1062 read_info_type_pool
.remove (*ptr
);
1066 ptr
= &(*ptr
)->next
;
1070 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1073 add_wild_read (bb_info_t bb_info
)
1075 insn_info_t insn_info
= bb_info
->last_insn
;
1076 insn_info
->wild_read
= true;
1077 free_read_records (bb_info
);
1078 reset_active_stores ();
1081 /* Set the BB_INFO so that the last insn is marked as a wild read of
1082 non-frame locations. */
1085 add_non_frame_wild_read (bb_info_t bb_info
)
1087 insn_info_t insn_info
= bb_info
->last_insn
;
1088 insn_info
->non_frame_wild_read
= true;
1089 free_read_records (bb_info
);
1090 reset_active_stores ();
1093 /* Return true if X is a constant or one of the registers that behave
1094 as a constant over the life of a function. This is equivalent to
1095 !rtx_varies_p for memory addresses. */
1098 const_or_frame_p (rtx x
)
1103 if (GET_CODE (x
) == REG
)
1105 /* Note that we have to test for the actual rtx used for the frame
1106 and arg pointers and not just the register number in case we have
1107 eliminated the frame and/or arg pointer and are using it
1109 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1110 /* The arg pointer varies if it is not a fixed register. */
1111 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1112 || x
== pic_offset_table_rtx
)
1120 /* Take all reasonable action to put the address of MEM into the form
1121 that we can do analysis on.
1123 The gold standard is to get the address into the form: address +
1124 OFFSET where address is something that rtx_varies_p considers a
1125 constant. When we can get the address in this form, we can do
1126 global analysis on it. Note that for constant bases, address is
1127 not actually returned, only the group_id. The address can be
1130 If that fails, we try cselib to get a value we can at least use
1131 locally. If that fails we return false.
1133 The GROUP_ID is set to -1 for cselib bases and the index of the
1134 group for non_varying bases.
1136 FOR_READ is true if this is a mem read and false if not. */
1139 canon_address (rtx mem
,
1140 alias_set_type
*alias_set_out
,
1142 HOST_WIDE_INT
*offset
,
1145 machine_mode address_mode
= get_address_mode (mem
);
1146 rtx mem_address
= XEXP (mem
, 0);
1147 rtx expanded_address
, address
;
1152 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1154 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1156 fprintf (dump_file
, " mem: ");
1157 print_inline_rtx (dump_file
, mem_address
, 0);
1158 fprintf (dump_file
, "\n");
1161 /* First see if just canon_rtx (mem_address) is const or frame,
1162 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1164 for (expanded
= 0; expanded
< 2; expanded
++)
1168 /* Use cselib to replace all of the reg references with the full
1169 expression. This will take care of the case where we have
1171 r_x = base + offset;
1176 val = *(base + offset); */
1178 expanded_address
= cselib_expand_value_rtx (mem_address
,
1181 /* If this fails, just go with the address from first
1183 if (!expanded_address
)
1187 expanded_address
= mem_address
;
1189 /* Split the address into canonical BASE + OFFSET terms. */
1190 address
= canon_rtx (expanded_address
);
1194 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1198 fprintf (dump_file
, "\n after cselib_expand address: ");
1199 print_inline_rtx (dump_file
, expanded_address
, 0);
1200 fprintf (dump_file
, "\n");
1203 fprintf (dump_file
, "\n after canon_rtx address: ");
1204 print_inline_rtx (dump_file
, address
, 0);
1205 fprintf (dump_file
, "\n");
1208 if (GET_CODE (address
) == CONST
)
1209 address
= XEXP (address
, 0);
1211 if (GET_CODE (address
) == PLUS
1212 && CONST_INT_P (XEXP (address
, 1)))
1214 *offset
= INTVAL (XEXP (address
, 1));
1215 address
= XEXP (address
, 0);
1218 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1219 && const_or_frame_p (address
))
1221 group_info
*group
= get_group_info (address
);
1223 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1224 fprintf (dump_file
, " gid=%d offset=%d \n",
1225 group
->id
, (int)*offset
);
1227 *group_id
= group
->id
;
1232 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1237 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1238 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1241 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1242 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1243 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1248 /* Clear the rhs field from the active_local_stores array. */
1251 clear_rhs_from_active_local_stores (void)
1253 insn_info_t ptr
= active_local_stores
;
1257 store_info
*store_info
= ptr
->store_rec
;
1258 /* Skip the clobbers. */
1259 while (!store_info
->is_set
)
1260 store_info
= store_info
->next
;
1262 store_info
->rhs
= NULL
;
1263 store_info
->const_rhs
= NULL
;
1265 ptr
= ptr
->next_local_store
;
1270 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1273 set_position_unneeded (store_info
*s_info
, int pos
)
1275 if (__builtin_expect (s_info
->is_large
, false))
1277 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1278 s_info
->positions_needed
.large
.count
++;
1281 s_info
->positions_needed
.small_bitmask
1282 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1285 /* Mark the whole store S_INFO as unneeded. */
1288 set_all_positions_unneeded (store_info
*s_info
)
1290 if (__builtin_expect (s_info
->is_large
, false))
1292 int pos
, end
= s_info
->end
- s_info
->begin
;
1293 for (pos
= 0; pos
< end
; pos
++)
1294 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1295 s_info
->positions_needed
.large
.count
= end
;
1298 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1301 /* Return TRUE if any bytes from S_INFO store are needed. */
1304 any_positions_needed_p (store_info
*s_info
)
1306 if (__builtin_expect (s_info
->is_large
, false))
1307 return (s_info
->positions_needed
.large
.count
1308 < s_info
->end
- s_info
->begin
);
1310 return (s_info
->positions_needed
.small_bitmask
1311 != (unsigned HOST_WIDE_INT
) 0);
1314 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1315 store are needed. */
1318 all_positions_needed_p (store_info
*s_info
, int start
, int width
)
1320 if (__builtin_expect (s_info
->is_large
, false))
1322 int end
= start
+ width
;
1324 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1330 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1331 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1336 static rtx
get_stored_val (store_info
*, machine_mode
, HOST_WIDE_INT
,
1337 HOST_WIDE_INT
, basic_block
, bool);
1340 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1341 there is a candidate store, after adding it to the appropriate
1342 local store group if so. */
1345 record_store (rtx body
, bb_info_t bb_info
)
1347 rtx mem
, rhs
, const_rhs
, mem_addr
;
1348 HOST_WIDE_INT offset
= 0;
1349 HOST_WIDE_INT width
= 0;
1350 alias_set_type spill_alias_set
;
1351 insn_info_t insn_info
= bb_info
->last_insn
;
1352 store_info
*store_info
= NULL
;
1354 cselib_val
*base
= NULL
;
1355 insn_info_t ptr
, last
, redundant_reason
;
1356 bool store_is_unused
;
1358 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1361 mem
= SET_DEST (body
);
1363 /* If this is not used, then this cannot be used to keep the insn
1364 from being deleted. On the other hand, it does provide something
1365 that can be used to prove that another store is dead. */
1367 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1369 /* Check whether that value is a suitable memory location. */
1372 /* If the set or clobber is unused, then it does not effect our
1373 ability to get rid of the entire insn. */
1374 if (!store_is_unused
)
1375 insn_info
->cannot_delete
= true;
1379 /* At this point we know mem is a mem. */
1380 if (GET_MODE (mem
) == BLKmode
)
1382 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1384 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1385 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1386 add_wild_read (bb_info
);
1387 insn_info
->cannot_delete
= true;
1390 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1391 as memset (addr, 0, 36); */
1392 else if (!MEM_SIZE_KNOWN_P (mem
)
1393 || MEM_SIZE (mem
) <= 0
1394 || MEM_SIZE (mem
) > MAX_OFFSET
1395 || GET_CODE (body
) != SET
1396 || !CONST_INT_P (SET_SRC (body
)))
1398 if (!store_is_unused
)
1400 /* If the set or clobber is unused, then it does not effect our
1401 ability to get rid of the entire insn. */
1402 insn_info
->cannot_delete
= true;
1403 clear_rhs_from_active_local_stores ();
1409 /* We can still process a volatile mem, we just cannot delete it. */
1410 if (MEM_VOLATILE_P (mem
))
1411 insn_info
->cannot_delete
= true;
1413 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1415 clear_rhs_from_active_local_stores ();
1419 if (GET_MODE (mem
) == BLKmode
)
1420 width
= MEM_SIZE (mem
);
1422 width
= GET_MODE_SIZE (GET_MODE (mem
));
1424 if (spill_alias_set
)
1426 bitmap store1
= clear_alias_group
->store1_p
;
1427 bitmap store2
= clear_alias_group
->store2_p
;
1429 gcc_assert (GET_MODE (mem
) != BLKmode
);
1431 if (!bitmap_set_bit (store1
, spill_alias_set
))
1432 bitmap_set_bit (store2
, spill_alias_set
);
1434 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1435 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1437 store_info
= rtx_store_info_pool
.allocate ();
1439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1440 fprintf (dump_file
, " processing spill store %d(%s)\n",
1441 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1443 else if (group_id
>= 0)
1445 /* In the restrictive case where the base is a constant or the
1446 frame pointer we can do global analysis. */
1449 = rtx_group_vec
[group_id
];
1450 tree expr
= MEM_EXPR (mem
);
1452 store_info
= rtx_store_info_pool
.allocate ();
1453 set_usage_bits (group
, offset
, width
, expr
);
1455 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1456 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1457 group_id
, (int)offset
, (int)(offset
+width
));
1461 if (may_be_sp_based_p (XEXP (mem
, 0)))
1462 insn_info
->stack_pointer_based
= true;
1463 insn_info
->contains_cselib_groups
= true;
1465 store_info
= cse_store_info_pool
.allocate ();
1468 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1469 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1470 (int)offset
, (int)(offset
+width
));
1473 const_rhs
= rhs
= NULL_RTX
;
1474 if (GET_CODE (body
) == SET
1475 /* No place to keep the value after ra. */
1476 && !reload_completed
1477 && (REG_P (SET_SRC (body
))
1478 || GET_CODE (SET_SRC (body
)) == SUBREG
1479 || CONSTANT_P (SET_SRC (body
)))
1480 && !MEM_VOLATILE_P (mem
)
1481 /* Sometimes the store and reload is used for truncation and
1483 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1485 rhs
= SET_SRC (body
);
1486 if (CONSTANT_P (rhs
))
1488 else if (body
== PATTERN (insn_info
->insn
))
1490 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1491 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1492 const_rhs
= XEXP (tem
, 0);
1494 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1496 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1498 if (tem
&& CONSTANT_P (tem
))
1503 /* Check to see if this stores causes some other stores to be
1505 ptr
= active_local_stores
;
1507 redundant_reason
= NULL
;
1508 mem
= canon_rtx (mem
);
1509 /* For alias_set != 0 canon_true_dependence should be never called. */
1510 if (spill_alias_set
)
1511 mem_addr
= NULL_RTX
;
1515 mem_addr
= base
->val_rtx
;
1519 = rtx_group_vec
[group_id
];
1520 mem_addr
= group
->canon_base_addr
;
1522 /* get_addr can only handle VALUE but cannot handle expr like:
1523 VALUE + OFFSET, so call get_addr to get original addr for
1524 mem_addr before plus_constant. */
1525 mem_addr
= get_addr (mem_addr
);
1527 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1532 insn_info_t next
= ptr
->next_local_store
;
1533 struct store_info
*s_info
= ptr
->store_rec
;
1536 /* Skip the clobbers. We delete the active insn if this insn
1537 shadows the set. To have been put on the active list, it
1538 has exactly on set. */
1539 while (!s_info
->is_set
)
1540 s_info
= s_info
->next
;
1542 if (s_info
->alias_set
!= spill_alias_set
)
1544 else if (s_info
->alias_set
)
1546 struct clear_alias_mode_holder
*entry
1547 = clear_alias_set_lookup (s_info
->alias_set
);
1548 /* Generally, spills cannot be processed if and of the
1549 references to the slot have a different mode. But if
1550 we are in the same block and mode is exactly the same
1551 between this store and one before in the same block,
1552 we can still delete it. */
1553 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1554 && (GET_MODE (mem
) == entry
->mode
))
1557 set_all_positions_unneeded (s_info
);
1559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1560 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1561 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1563 else if ((s_info
->group_id
== group_id
)
1564 && (s_info
->cse_base
== base
))
1567 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1568 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1569 INSN_UID (ptr
->insn
), s_info
->group_id
,
1570 (int)s_info
->begin
, (int)s_info
->end
);
1572 /* Even if PTR won't be eliminated as unneeded, if both
1573 PTR and this insn store the same constant value, we might
1574 eliminate this insn instead. */
1575 if (s_info
->const_rhs
1577 && offset
>= s_info
->begin
1578 && offset
+ width
<= s_info
->end
1579 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1582 if (GET_MODE (mem
) == BLKmode
)
1584 if (GET_MODE (s_info
->mem
) == BLKmode
1585 && s_info
->const_rhs
== const_rhs
)
1586 redundant_reason
= ptr
;
1588 else if (s_info
->const_rhs
== const0_rtx
1589 && const_rhs
== const0_rtx
)
1590 redundant_reason
= ptr
;
1595 val
= get_stored_val (s_info
, GET_MODE (mem
),
1596 offset
, offset
+ width
,
1597 BLOCK_FOR_INSN (insn_info
->insn
),
1599 if (get_insns () != NULL
)
1602 if (val
&& rtx_equal_p (val
, const_rhs
))
1603 redundant_reason
= ptr
;
1607 for (i
= MAX (offset
, s_info
->begin
);
1608 i
< offset
+ width
&& i
< s_info
->end
;
1610 set_position_unneeded (s_info
, i
- s_info
->begin
);
1612 else if (s_info
->rhs
)
1613 /* Need to see if it is possible for this store to overwrite
1614 the value of store_info. If it is, set the rhs to NULL to
1615 keep it from being used to remove a load. */
1617 if (canon_true_dependence (s_info
->mem
,
1618 GET_MODE (s_info
->mem
),
1623 s_info
->const_rhs
= NULL
;
1627 /* An insn can be deleted if every position of every one of
1628 its s_infos is zero. */
1629 if (any_positions_needed_p (s_info
))
1634 insn_info_t insn_to_delete
= ptr
;
1636 active_local_stores_len
--;
1638 last
->next_local_store
= ptr
->next_local_store
;
1640 active_local_stores
= ptr
->next_local_store
;
1642 if (!insn_to_delete
->cannot_delete
)
1643 delete_dead_store_insn (insn_to_delete
);
1651 /* Finish filling in the store_info. */
1652 store_info
->next
= insn_info
->store_rec
;
1653 insn_info
->store_rec
= store_info
;
1654 store_info
->mem
= mem
;
1655 store_info
->alias_set
= spill_alias_set
;
1656 store_info
->mem_addr
= mem_addr
;
1657 store_info
->cse_base
= base
;
1658 if (width
> HOST_BITS_PER_WIDE_INT
)
1660 store_info
->is_large
= true;
1661 store_info
->positions_needed
.large
.count
= 0;
1662 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1666 store_info
->is_large
= false;
1667 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1669 store_info
->group_id
= group_id
;
1670 store_info
->begin
= offset
;
1671 store_info
->end
= offset
+ width
;
1672 store_info
->is_set
= GET_CODE (body
) == SET
;
1673 store_info
->rhs
= rhs
;
1674 store_info
->const_rhs
= const_rhs
;
1675 store_info
->redundant_reason
= redundant_reason
;
1677 /* If this is a clobber, we return 0. We will only be able to
1678 delete this insn if there is only one store USED store, but we
1679 can use the clobber to delete other stores earlier. */
1680 return store_info
->is_set
? 1 : 0;
1685 dump_insn_info (const char * start
, insn_info_t insn_info
)
1687 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1688 INSN_UID (insn_info
->insn
),
1689 insn_info
->store_rec
? "has store" : "naked");
1693 /* If the modes are different and the value's source and target do not
1694 line up, we need to extract the value from lower part of the rhs of
1695 the store, shift it, and then put it into a form that can be shoved
1696 into the read_insn. This function generates a right SHIFT of a
1697 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1698 shift sequence is returned or NULL if we failed to find a
1702 find_shift_sequence (int access_size
,
1703 store_info
*store_info
,
1704 machine_mode read_mode
,
1705 int shift
, bool speed
, bool require_cst
)
1707 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1708 machine_mode new_mode
;
1709 rtx read_reg
= NULL
;
1711 /* Some machines like the x86 have shift insns for each size of
1712 operand. Other machines like the ppc or the ia-64 may only have
1713 shift insns that shift values within 32 or 64 bit registers.
1714 This loop tries to find the smallest shift insn that will right
1715 justify the value we want to read but is available in one insn on
1718 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1720 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1721 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1723 rtx target
, new_reg
, new_lhs
;
1724 rtx_insn
*shift_seq
, *insn
;
1727 /* If a constant was stored into memory, try to simplify it here,
1728 otherwise the cost of the shift might preclude this optimization
1729 e.g. at -Os, even when no actual shift will be needed. */
1730 if (store_info
->const_rhs
)
1732 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1733 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1735 if (ret
&& CONSTANT_P (ret
))
1737 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1738 ret
, GEN_INT (shift
));
1739 if (ret
&& CONSTANT_P (ret
))
1741 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1742 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1743 if (ret
&& CONSTANT_P (ret
)
1744 && (set_src_cost (ret
, read_mode
, speed
)
1745 <= COSTS_N_INSNS (1)))
1754 /* Try a wider mode if truncating the store mode to NEW_MODE
1755 requires a real instruction. */
1756 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1757 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1760 /* Also try a wider mode if the necessary punning is either not
1761 desirable or not possible. */
1762 if (!CONSTANT_P (store_info
->rhs
)
1763 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1766 new_reg
= gen_reg_rtx (new_mode
);
1770 /* In theory we could also check for an ashr. Ian Taylor knows
1771 of one dsp where the cost of these two was not the same. But
1772 this really is a rare case anyway. */
1773 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1774 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1776 shift_seq
= get_insns ();
1779 if (target
!= new_reg
|| shift_seq
== NULL
)
1783 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1785 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1787 /* The computation up to here is essentially independent
1788 of the arguments and could be precomputed. It may
1789 not be worth doing so. We could precompute if
1790 worthwhile or at least cache the results. The result
1791 technically depends on both SHIFT and ACCESS_SIZE,
1792 but in practice the answer will depend only on ACCESS_SIZE. */
1794 if (cost
> COSTS_N_INSNS (1))
1797 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1798 copy_rtx (store_info
->rhs
));
1799 if (new_lhs
== NULL_RTX
)
1802 /* We found an acceptable shift. Generate a move to
1803 take the value from the store and put it into the
1804 shift pseudo, then shift it, then generate another
1805 move to put in into the target of the read. */
1806 emit_move_insn (new_reg
, new_lhs
);
1807 emit_insn (shift_seq
);
1808 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1816 /* Call back for note_stores to find the hard regs set or clobbered by
1817 insn. Data is a bitmap of the hardregs set so far. */
1820 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1822 bitmap regs_set
= (bitmap
) data
;
1825 && HARD_REGISTER_P (x
))
1826 bitmap_set_range (regs_set
, REGNO (x
), REG_NREGS (x
));
1829 /* Helper function for replace_read and record_store.
1830 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1831 to one before READ_END bytes read in READ_MODE. Return NULL
1832 if not successful. If REQUIRE_CST is true, return always constant. */
1835 get_stored_val (store_info
*store_info
, machine_mode read_mode
,
1836 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1837 basic_block bb
, bool require_cst
)
1839 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1841 int access_size
; /* In bytes. */
1844 /* To get here the read is within the boundaries of the write so
1845 shift will never be negative. Start out with the shift being in
1847 if (store_mode
== BLKmode
)
1849 else if (BYTES_BIG_ENDIAN
)
1850 shift
= store_info
->end
- read_end
;
1852 shift
= read_begin
- store_info
->begin
;
1854 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1856 /* From now on it is bits. */
1857 shift
*= BITS_PER_UNIT
;
1860 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1861 optimize_bb_for_speed_p (bb
),
1863 else if (store_mode
== BLKmode
)
1865 /* The store is a memset (addr, const_val, const_size). */
1866 gcc_assert (CONST_INT_P (store_info
->rhs
));
1867 store_mode
= int_mode_for_mode (read_mode
);
1868 if (store_mode
== BLKmode
)
1869 read_reg
= NULL_RTX
;
1870 else if (store_info
->rhs
== const0_rtx
)
1871 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1872 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1873 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1874 read_reg
= NULL_RTX
;
1877 unsigned HOST_WIDE_INT c
1878 = INTVAL (store_info
->rhs
)
1879 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1880 int shift
= BITS_PER_UNIT
;
1881 while (shift
< HOST_BITS_PER_WIDE_INT
)
1886 read_reg
= gen_int_mode (c
, store_mode
);
1887 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1890 else if (store_info
->const_rhs
1892 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1893 read_reg
= extract_low_bits (read_mode
, store_mode
,
1894 copy_rtx (store_info
->const_rhs
));
1896 read_reg
= extract_low_bits (read_mode
, store_mode
,
1897 copy_rtx (store_info
->rhs
));
1898 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1899 read_reg
= NULL_RTX
;
1903 /* Take a sequence of:
1926 Depending on the alignment and the mode of the store and
1930 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1931 and READ_INSN are for the read. Return true if the replacement
1935 replace_read (store_info
*store_info
, insn_info_t store_insn
,
1936 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1939 machine_mode store_mode
= GET_MODE (store_info
->mem
);
1940 machine_mode read_mode
= GET_MODE (read_info
->mem
);
1941 rtx_insn
*insns
, *this_insn
;
1948 /* Create a sequence of instructions to set up the read register.
1949 This sequence goes immediately before the store and its result
1950 is read by the load.
1952 We need to keep this in perspective. We are replacing a read
1953 with a sequence of insns, but the read will almost certainly be
1954 in cache, so it is not going to be an expensive one. Thus, we
1955 are not willing to do a multi insn shift or worse a subroutine
1956 call to get rid of the read. */
1957 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1958 fprintf (dump_file
, "trying to replace %smode load in insn %d"
1959 " from %smode store in insn %d\n",
1960 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
1961 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
1963 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
1964 read_reg
= get_stored_val (store_info
,
1965 read_mode
, read_info
->begin
, read_info
->end
,
1967 if (read_reg
== NULL_RTX
)
1970 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1971 fprintf (dump_file
, " -- could not extract bits of stored value\n");
1974 /* Force the value into a new register so that it won't be clobbered
1975 between the store and the load. */
1976 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
1977 insns
= get_insns ();
1980 if (insns
!= NULL_RTX
)
1982 /* Now we have to scan the set of new instructions to see if the
1983 sequence contains and sets of hardregs that happened to be
1984 live at this point. For instance, this can happen if one of
1985 the insns sets the CC and the CC happened to be live at that
1986 point. This does occasionally happen, see PR 37922. */
1987 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
1989 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
1990 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
1992 bitmap_and_into (regs_set
, regs_live
);
1993 if (!bitmap_empty_p (regs_set
))
1995 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1998 "abandoning replacement because sequence clobbers live hardregs:");
1999 df_print_regset (dump_file
, regs_set
);
2002 BITMAP_FREE (regs_set
);
2005 BITMAP_FREE (regs_set
);
2008 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2010 deferred_change
*change
= deferred_change_pool
.allocate ();
2012 /* Insert this right before the store insn where it will be safe
2013 from later insns that might change it before the read. */
2014 emit_insn_before (insns
, store_insn
->insn
);
2016 /* And now for the kludge part: cselib croaks if you just
2017 return at this point. There are two reasons for this:
2019 1) Cselib has an idea of how many pseudos there are and
2020 that does not include the new ones we just added.
2022 2) Cselib does not know about the move insn we added
2023 above the store_info, and there is no way to tell it
2024 about it, because it has "moved on".
2026 Problem (1) is fixable with a certain amount of engineering.
2027 Problem (2) is requires starting the bb from scratch. This
2030 So we are just going to have to lie. The move/extraction
2031 insns are not really an issue, cselib did not see them. But
2032 the use of the new pseudo read_insn is a real problem because
2033 cselib has not scanned this insn. The way that we solve this
2034 problem is that we are just going to put the mem back for now
2035 and when we are finished with the block, we undo this. We
2036 keep a table of mems to get rid of. At the end of the basic
2037 block we can put them back. */
2039 *loc
= read_info
->mem
;
2040 change
->next
= deferred_change_list
;
2041 deferred_change_list
= change
;
2043 change
->reg
= read_reg
;
2045 /* Get rid of the read_info, from the point of view of the
2046 rest of dse, play like this read never happened. */
2047 read_insn
->read_rec
= read_info
->next
;
2048 read_info_type_pool
.remove (read_info
);
2049 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2051 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2052 print_simple_rtl (dump_file
, read_reg
);
2053 fprintf (dump_file
, "\n");
2059 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2061 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2062 print_simple_rtl (dump_file
, read_reg
);
2063 fprintf (dump_file
, " led to an invalid instruction\n");
2069 /* Check the address of MEM *LOC and kill any appropriate stores that may
2073 check_mem_read_rtx (rtx
*loc
, bb_info_t bb_info
)
2075 rtx mem
= *loc
, mem_addr
;
2076 insn_info_t insn_info
;
2077 HOST_WIDE_INT offset
= 0;
2078 HOST_WIDE_INT width
= 0;
2079 alias_set_type spill_alias_set
= 0;
2080 cselib_val
*base
= NULL
;
2082 read_info_t read_info
;
2084 insn_info
= bb_info
->last_insn
;
2086 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2087 || (MEM_VOLATILE_P (mem
)))
2089 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2090 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2091 add_wild_read (bb_info
);
2092 insn_info
->cannot_delete
= true;
2096 /* If it is reading readonly mem, then there can be no conflict with
2098 if (MEM_READONLY_P (mem
))
2101 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2103 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2104 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2105 add_wild_read (bb_info
);
2109 if (GET_MODE (mem
) == BLKmode
)
2112 width
= GET_MODE_SIZE (GET_MODE (mem
));
2114 read_info
= read_info_type_pool
.allocate ();
2115 read_info
->group_id
= group_id
;
2116 read_info
->mem
= mem
;
2117 read_info
->alias_set
= spill_alias_set
;
2118 read_info
->begin
= offset
;
2119 read_info
->end
= offset
+ width
;
2120 read_info
->next
= insn_info
->read_rec
;
2121 insn_info
->read_rec
= read_info
;
2122 /* For alias_set != 0 canon_true_dependence should be never called. */
2123 if (spill_alias_set
)
2124 mem_addr
= NULL_RTX
;
2128 mem_addr
= base
->val_rtx
;
2132 = rtx_group_vec
[group_id
];
2133 mem_addr
= group
->canon_base_addr
;
2135 /* get_addr can only handle VALUE but cannot handle expr like:
2136 VALUE + OFFSET, so call get_addr to get original addr for
2137 mem_addr before plus_constant. */
2138 mem_addr
= get_addr (mem_addr
);
2140 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2143 /* We ignore the clobbers in store_info. The is mildly aggressive,
2144 but there really should not be a clobber followed by a read. */
2146 if (spill_alias_set
)
2148 insn_info_t i_ptr
= active_local_stores
;
2149 insn_info_t last
= NULL
;
2151 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2152 fprintf (dump_file
, " processing spill load %d\n",
2153 (int) spill_alias_set
);
2157 store_info
*store_info
= i_ptr
->store_rec
;
2159 /* Skip the clobbers. */
2160 while (!store_info
->is_set
)
2161 store_info
= store_info
->next
;
2163 if (store_info
->alias_set
== spill_alias_set
)
2165 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2166 dump_insn_info ("removing from active", i_ptr
);
2168 active_local_stores_len
--;
2170 last
->next_local_store
= i_ptr
->next_local_store
;
2172 active_local_stores
= i_ptr
->next_local_store
;
2176 i_ptr
= i_ptr
->next_local_store
;
2179 else if (group_id
>= 0)
2181 /* This is the restricted case where the base is a constant or
2182 the frame pointer and offset is a constant. */
2183 insn_info_t i_ptr
= active_local_stores
;
2184 insn_info_t last
= NULL
;
2186 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2189 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2192 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2193 group_id
, (int)offset
, (int)(offset
+width
));
2198 bool remove
= false;
2199 store_info
*store_info
= i_ptr
->store_rec
;
2201 /* Skip the clobbers. */
2202 while (!store_info
->is_set
)
2203 store_info
= store_info
->next
;
2205 /* There are three cases here. */
2206 if (store_info
->group_id
< 0)
2207 /* We have a cselib store followed by a read from a
2210 = canon_true_dependence (store_info
->mem
,
2211 GET_MODE (store_info
->mem
),
2212 store_info
->mem_addr
,
2215 else if (group_id
== store_info
->group_id
)
2217 /* This is a block mode load. We may get lucky and
2218 canon_true_dependence may save the day. */
2221 = canon_true_dependence (store_info
->mem
,
2222 GET_MODE (store_info
->mem
),
2223 store_info
->mem_addr
,
2226 /* If this read is just reading back something that we just
2227 stored, rewrite the read. */
2231 && offset
>= store_info
->begin
2232 && offset
+ width
<= store_info
->end
2233 && all_positions_needed_p (store_info
,
2234 offset
- store_info
->begin
,
2236 && replace_read (store_info
, i_ptr
, read_info
,
2237 insn_info
, loc
, bb_info
->regs_live
))
2240 /* The bases are the same, just see if the offsets
2242 if ((offset
< store_info
->end
)
2243 && (offset
+ width
> store_info
->begin
))
2249 The else case that is missing here is that the
2250 bases are constant but different. There is nothing
2251 to do here because there is no overlap. */
2255 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2256 dump_insn_info ("removing from active", i_ptr
);
2258 active_local_stores_len
--;
2260 last
->next_local_store
= i_ptr
->next_local_store
;
2262 active_local_stores
= i_ptr
->next_local_store
;
2266 i_ptr
= i_ptr
->next_local_store
;
2271 insn_info_t i_ptr
= active_local_stores
;
2272 insn_info_t last
= NULL
;
2273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2275 fprintf (dump_file
, " processing cselib load mem:");
2276 print_inline_rtx (dump_file
, mem
, 0);
2277 fprintf (dump_file
, "\n");
2282 bool remove
= false;
2283 store_info
*store_info
= i_ptr
->store_rec
;
2285 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2286 fprintf (dump_file
, " processing cselib load against insn %d\n",
2287 INSN_UID (i_ptr
->insn
));
2289 /* Skip the clobbers. */
2290 while (!store_info
->is_set
)
2291 store_info
= store_info
->next
;
2293 /* If this read is just reading back something that we just
2294 stored, rewrite the read. */
2296 && store_info
->group_id
== -1
2297 && store_info
->cse_base
== base
2299 && offset
>= store_info
->begin
2300 && offset
+ width
<= store_info
->end
2301 && all_positions_needed_p (store_info
,
2302 offset
- store_info
->begin
, width
)
2303 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2304 bb_info
->regs_live
))
2307 if (!store_info
->alias_set
)
2308 remove
= canon_true_dependence (store_info
->mem
,
2309 GET_MODE (store_info
->mem
),
2310 store_info
->mem_addr
,
2315 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2316 dump_insn_info ("removing from active", i_ptr
);
2318 active_local_stores_len
--;
2320 last
->next_local_store
= i_ptr
->next_local_store
;
2322 active_local_stores
= i_ptr
->next_local_store
;
2326 i_ptr
= i_ptr
->next_local_store
;
2331 /* A note_uses callback in which DATA points the INSN_INFO for
2332 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2333 true for any part of *LOC. */
2336 check_mem_read_use (rtx
*loc
, void *data
)
2338 subrtx_ptr_iterator::array_type array
;
2339 FOR_EACH_SUBRTX_PTR (iter
, array
, loc
, NONCONST
)
2343 check_mem_read_rtx (loc
, (bb_info_t
) data
);
2348 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2349 So far it only handles arguments passed in registers. */
2352 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2354 CUMULATIVE_ARGS args_so_far_v
;
2355 cumulative_args_t args_so_far
;
2359 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2360 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2362 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2364 arg
!= void_list_node
&& idx
< nargs
;
2365 arg
= TREE_CHAIN (arg
), idx
++)
2367 machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2369 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2370 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2371 || GET_MODE_CLASS (mode
) != MODE_INT
)
2374 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2376 link
= XEXP (link
, 1))
2377 if (GET_CODE (XEXP (link
, 0)) == USE
)
2379 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2380 if (REG_P (args
[idx
])
2381 && REGNO (args
[idx
]) == REGNO (reg
)
2382 && (GET_MODE (args
[idx
]) == mode
2383 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2384 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2386 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2387 > GET_MODE_SIZE (mode
)))))
2393 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2394 if (GET_MODE (args
[idx
]) != mode
)
2396 if (!tmp
|| !CONST_INT_P (tmp
))
2398 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2403 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2405 if (arg
!= void_list_node
|| idx
!= nargs
)
2410 /* Return a bitmap of the fixed registers contained in IN. */
2413 copy_fixed_regs (const_bitmap in
)
2417 ret
= ALLOC_REG_SET (NULL
);
2418 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2422 /* Apply record_store to all candidate stores in INSN. Mark INSN
2423 if some part of it is not a candidate store and assigns to a
2424 non-register target. */
2427 scan_insn (bb_info_t bb_info
, rtx_insn
*insn
)
2430 insn_info_type
*insn_info
= insn_info_type_pool
.allocate ();
2432 memset (insn_info
, 0, sizeof (struct insn_info_type
));
2434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2435 fprintf (dump_file
, "\n**scanning insn=%d\n",
2438 insn_info
->prev_insn
= bb_info
->last_insn
;
2439 insn_info
->insn
= insn
;
2440 bb_info
->last_insn
= insn_info
;
2442 if (DEBUG_INSN_P (insn
))
2444 insn_info
->cannot_delete
= true;
2448 /* Look at all of the uses in the insn. */
2449 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2454 tree memset_call
= NULL_TREE
;
2456 insn_info
->cannot_delete
= true;
2458 /* Const functions cannot do anything bad i.e. read memory,
2459 however, they can read their parameters which may have
2460 been pushed onto the stack.
2461 memset and bzero don't read memory either. */
2462 const_call
= RTL_CONST_CALL_P (insn
);
2465 rtx call
= get_call_rtx_from (insn
);
2466 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2468 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2469 if (SYMBOL_REF_DECL (symbol
)
2470 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2472 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2474 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2475 == BUILT_IN_MEMSET
))
2476 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2477 memset_call
= SYMBOL_REF_DECL (symbol
);
2481 if (const_call
|| memset_call
)
2483 insn_info_t i_ptr
= active_local_stores
;
2484 insn_info_t last
= NULL
;
2486 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2487 fprintf (dump_file
, "%s call %d\n",
2488 const_call
? "const" : "memset", INSN_UID (insn
));
2490 /* See the head comment of the frame_read field. */
2491 if (reload_completed
2492 /* Tail calls are storing their arguments using
2493 arg pointer. If it is a frame pointer on the target,
2494 even before reload we need to kill frame pointer based
2496 || (SIBLING_CALL_P (insn
)
2497 && HARD_FRAME_POINTER_IS_ARG_POINTER
))
2498 insn_info
->frame_read
= true;
2500 /* Loop over the active stores and remove those which are
2501 killed by the const function call. */
2504 bool remove_store
= false;
2506 /* The stack pointer based stores are always killed. */
2507 if (i_ptr
->stack_pointer_based
)
2508 remove_store
= true;
2510 /* If the frame is read, the frame related stores are killed. */
2511 else if (insn_info
->frame_read
)
2513 store_info
*store_info
= i_ptr
->store_rec
;
2515 /* Skip the clobbers. */
2516 while (!store_info
->is_set
)
2517 store_info
= store_info
->next
;
2519 if (store_info
->group_id
>= 0
2520 && rtx_group_vec
[store_info
->group_id
]->frame_related
)
2521 remove_store
= true;
2526 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2527 dump_insn_info ("removing from active", i_ptr
);
2529 active_local_stores_len
--;
2531 last
->next_local_store
= i_ptr
->next_local_store
;
2533 active_local_stores
= i_ptr
->next_local_store
;
2538 i_ptr
= i_ptr
->next_local_store
;
2544 if (get_call_args (insn
, memset_call
, args
, 3)
2545 && CONST_INT_P (args
[1])
2546 && CONST_INT_P (args
[2])
2547 && INTVAL (args
[2]) > 0)
2549 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2550 set_mem_size (mem
, INTVAL (args
[2]));
2551 body
= gen_rtx_SET (mem
, args
[1]);
2552 mems_found
+= record_store (body
, bb_info
);
2553 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2554 fprintf (dump_file
, "handling memset as BLKmode store\n");
2555 if (mems_found
== 1)
2557 if (active_local_stores_len
++
2558 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2560 active_local_stores_len
= 1;
2561 active_local_stores
= NULL
;
2563 insn_info
->fixed_regs_live
2564 = copy_fixed_regs (bb_info
->regs_live
);
2565 insn_info
->next_local_store
= active_local_stores
;
2566 active_local_stores
= insn_info
;
2571 else if (SIBLING_CALL_P (insn
) && reload_completed
)
2572 /* Arguments for a sibling call that are pushed to memory are passed
2573 using the incoming argument pointer of the current function. After
2574 reload that might be (and likely is) frame pointer based. */
2575 add_wild_read (bb_info
);
2577 /* Every other call, including pure functions, may read any memory
2578 that is not relative to the frame. */
2579 add_non_frame_wild_read (bb_info
);
2584 /* Assuming that there are sets in these insns, we cannot delete
2586 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2587 || volatile_refs_p (PATTERN (insn
))
2588 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2589 || (RTX_FRAME_RELATED_P (insn
))
2590 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2591 insn_info
->cannot_delete
= true;
2593 body
= PATTERN (insn
);
2594 if (GET_CODE (body
) == PARALLEL
)
2597 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2598 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2601 mems_found
+= record_store (body
, bb_info
);
2603 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2604 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2605 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2607 /* If we found some sets of mems, add it into the active_local_stores so
2608 that it can be locally deleted if found dead or used for
2609 replace_read and redundant constant store elimination. Otherwise mark
2610 it as cannot delete. This simplifies the processing later. */
2611 if (mems_found
== 1)
2613 if (active_local_stores_len
++
2614 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2616 active_local_stores_len
= 1;
2617 active_local_stores
= NULL
;
2619 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2620 insn_info
->next_local_store
= active_local_stores
;
2621 active_local_stores
= insn_info
;
2624 insn_info
->cannot_delete
= true;
2628 /* Remove BASE from the set of active_local_stores. This is a
2629 callback from cselib that is used to get rid of the stores in
2630 active_local_stores. */
2633 remove_useless_values (cselib_val
*base
)
2635 insn_info_t insn_info
= active_local_stores
;
2636 insn_info_t last
= NULL
;
2640 store_info
*store_info
= insn_info
->store_rec
;
2643 /* If ANY of the store_infos match the cselib group that is
2644 being deleted, then the insn can not be deleted. */
2647 if ((store_info
->group_id
== -1)
2648 && (store_info
->cse_base
== base
))
2653 store_info
= store_info
->next
;
2658 active_local_stores_len
--;
2660 last
->next_local_store
= insn_info
->next_local_store
;
2662 active_local_stores
= insn_info
->next_local_store
;
2663 free_store_info (insn_info
);
2668 insn_info
= insn_info
->next_local_store
;
2673 /* Do all of step 1. */
2679 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2682 all_blocks
= BITMAP_ALLOC (NULL
);
2683 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2684 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2686 FOR_ALL_BB_FN (bb
, cfun
)
2689 bb_info_t bb_info
= dse_bb_info_type_pool
.allocate ();
2691 memset (bb_info
, 0, sizeof (dse_bb_info_type
));
2692 bitmap_set_bit (all_blocks
, bb
->index
);
2693 bb_info
->regs_live
= regs_live
;
2695 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2696 df_simulate_initialize_forwards (bb
, regs_live
);
2698 bb_table
[bb
->index
] = bb_info
;
2699 cselib_discard_hook
= remove_useless_values
;
2701 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2705 active_local_stores
= NULL
;
2706 active_local_stores_len
= 0;
2707 cselib_clear_table ();
2709 /* Scan the insns. */
2710 FOR_BB_INSNS (bb
, insn
)
2713 scan_insn (bb_info
, insn
);
2714 cselib_process_insn (insn
);
2716 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2719 /* This is something of a hack, because the global algorithm
2720 is supposed to take care of the case where stores go dead
2721 at the end of the function. However, the global
2722 algorithm must take a more conservative view of block
2723 mode reads than the local alg does. So to get the case
2724 where you have a store to the frame followed by a non
2725 overlapping block more read, we look at the active local
2726 stores at the end of the function and delete all of the
2727 frame and spill based ones. */
2728 if (stores_off_frame_dead_at_return
2729 && (EDGE_COUNT (bb
->succs
) == 0
2730 || (single_succ_p (bb
)
2731 && single_succ (bb
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
2732 && ! crtl
->calls_eh_return
)))
2734 insn_info_t i_ptr
= active_local_stores
;
2737 store_info
*store_info
= i_ptr
->store_rec
;
2739 /* Skip the clobbers. */
2740 while (!store_info
->is_set
)
2741 store_info
= store_info
->next
;
2742 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2743 delete_dead_store_insn (i_ptr
);
2745 if (store_info
->group_id
>= 0)
2748 = rtx_group_vec
[store_info
->group_id
];
2749 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2750 delete_dead_store_insn (i_ptr
);
2753 i_ptr
= i_ptr
->next_local_store
;
2757 /* Get rid of the loads that were discovered in
2758 replace_read. Cselib is finished with this block. */
2759 while (deferred_change_list
)
2761 deferred_change
*next
= deferred_change_list
->next
;
2763 /* There is no reason to validate this change. That was
2765 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2766 deferred_change_pool
.remove (deferred_change_list
);
2767 deferred_change_list
= next
;
2770 /* Get rid of all of the cselib based store_infos in this
2771 block and mark the containing insns as not being
2773 ptr
= bb_info
->last_insn
;
2776 if (ptr
->contains_cselib_groups
)
2778 store_info
*s_info
= ptr
->store_rec
;
2779 while (s_info
&& !s_info
->is_set
)
2780 s_info
= s_info
->next
;
2782 && s_info
->redundant_reason
2783 && s_info
->redundant_reason
->insn
2784 && !ptr
->cannot_delete
)
2786 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2787 fprintf (dump_file
, "Locally deleting insn %d "
2788 "because insn %d stores the "
2789 "same value and couldn't be "
2791 INSN_UID (ptr
->insn
),
2792 INSN_UID (s_info
->redundant_reason
->insn
));
2793 delete_dead_store_insn (ptr
);
2795 free_store_info (ptr
);
2801 /* Free at least positions_needed bitmaps. */
2802 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2803 if (s_info
->is_large
)
2805 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2806 s_info
->is_large
= false;
2809 ptr
= ptr
->prev_insn
;
2812 cse_store_info_pool
.release ();
2814 bb_info
->regs_live
= NULL
;
2817 BITMAP_FREE (regs_live
);
2819 rtx_group_table
->empty ();
2823 /*----------------------------------------------------------------------------
2826 Assign each byte position in the stores that we are going to
2827 analyze globally to a position in the bitmaps. Returns true if
2828 there are any bit positions assigned.
2829 ----------------------------------------------------------------------------*/
2832 dse_step2_init (void)
2837 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2839 /* For all non stack related bases, we only consider a store to
2840 be deletable if there are two or more stores for that
2841 position. This is because it takes one store to make the
2842 other store redundant. However, for the stores that are
2843 stack related, we consider them if there is only one store
2844 for the position. We do this because the stack related
2845 stores can be deleted if their is no read between them and
2846 the end of the function.
2848 To make this work in the current framework, we take the stack
2849 related bases add all of the bits from store1 into store2.
2850 This has the effect of making the eligible even if there is
2853 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2855 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2856 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2857 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2858 fprintf (dump_file
, "group %d is frame related ", i
);
2861 group
->offset_map_size_n
++;
2862 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2863 group
->offset_map_size_n
);
2864 group
->offset_map_size_p
++;
2865 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2866 group
->offset_map_size_p
);
2867 group
->process_globally
= false;
2868 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2870 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2871 (int)bitmap_count_bits (group
->store2_n
),
2872 (int)bitmap_count_bits (group
->store2_p
));
2873 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2874 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2880 /* Init the offset tables for the normal case. */
2883 dse_step2_nospill (void)
2887 /* Position 0 is unused because 0 is used in the maps to mean
2889 current_position
= 1;
2890 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2895 if (group
== clear_alias_group
)
2898 memset (group
->offset_map_n
, 0, sizeof (int) * group
->offset_map_size_n
);
2899 memset (group
->offset_map_p
, 0, sizeof (int) * group
->offset_map_size_p
);
2900 bitmap_clear (group
->group_kill
);
2902 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2904 bitmap_set_bit (group
->group_kill
, current_position
);
2905 if (bitmap_bit_p (group
->escaped_n
, j
))
2906 bitmap_set_bit (kill_on_calls
, current_position
);
2907 group
->offset_map_n
[j
] = current_position
++;
2908 group
->process_globally
= true;
2910 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2912 bitmap_set_bit (group
->group_kill
, current_position
);
2913 if (bitmap_bit_p (group
->escaped_p
, j
))
2914 bitmap_set_bit (kill_on_calls
, current_position
);
2915 group
->offset_map_p
[j
] = current_position
++;
2916 group
->process_globally
= true;
2919 return current_position
!= 1;
2924 /*----------------------------------------------------------------------------
2927 Build the bit vectors for the transfer functions.
2928 ----------------------------------------------------------------------------*/
2931 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2935 get_bitmap_index (group_info
*group_info
, HOST_WIDE_INT offset
)
2939 HOST_WIDE_INT offset_p
= -offset
;
2940 if (offset_p
>= group_info
->offset_map_size_n
)
2942 return group_info
->offset_map_n
[offset_p
];
2946 if (offset
>= group_info
->offset_map_size_p
)
2948 return group_info
->offset_map_p
[offset
];
2953 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2957 scan_stores_nospill (store_info
*store_info
, bitmap gen
, bitmap kill
)
2962 group_info
*group_info
2963 = rtx_group_vec
[store_info
->group_id
];
2964 if (group_info
->process_globally
)
2965 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
2967 int index
= get_bitmap_index (group_info
, i
);
2970 bitmap_set_bit (gen
, index
);
2972 bitmap_clear_bit (kill
, index
);
2975 store_info
= store_info
->next
;
2980 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2984 scan_stores_spill (store_info
*store_info
, bitmap gen
, bitmap kill
)
2988 if (store_info
->alias_set
)
2990 int index
= get_bitmap_index (clear_alias_group
,
2991 store_info
->alias_set
);
2994 bitmap_set_bit (gen
, index
);
2996 bitmap_clear_bit (kill
, index
);
2999 store_info
= store_info
->next
;
3004 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3008 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3010 read_info_t read_info
= insn_info
->read_rec
;
3014 /* If this insn reads the frame, kill all the frame related stores. */
3015 if (insn_info
->frame_read
)
3017 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3018 if (group
->process_globally
&& group
->frame_related
)
3021 bitmap_ior_into (kill
, group
->group_kill
);
3022 bitmap_and_compl_into (gen
, group
->group_kill
);
3025 if (insn_info
->non_frame_wild_read
)
3027 /* Kill all non-frame related stores. Kill all stores of variables that
3030 bitmap_ior_into (kill
, kill_on_calls
);
3031 bitmap_and_compl_into (gen
, kill_on_calls
);
3032 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3033 if (group
->process_globally
&& !group
->frame_related
)
3036 bitmap_ior_into (kill
, group
->group_kill
);
3037 bitmap_and_compl_into (gen
, group
->group_kill
);
3042 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3044 if (group
->process_globally
)
3046 if (i
== read_info
->group_id
)
3048 if (read_info
->begin
> read_info
->end
)
3050 /* Begin > end for block mode reads. */
3052 bitmap_ior_into (kill
, group
->group_kill
);
3053 bitmap_and_compl_into (gen
, group
->group_kill
);
3057 /* The groups are the same, just process the
3060 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3062 int index
= get_bitmap_index (group
, j
);
3066 bitmap_set_bit (kill
, index
);
3067 bitmap_clear_bit (gen
, index
);
3074 /* The groups are different, if the alias sets
3075 conflict, clear the entire group. We only need
3076 to apply this test if the read_info is a cselib
3077 read. Anything with a constant base cannot alias
3078 something else with a different constant
3080 if ((read_info
->group_id
< 0)
3081 && canon_true_dependence (group
->base_mem
,
3082 GET_MODE (group
->base_mem
),
3083 group
->canon_base_addr
,
3084 read_info
->mem
, NULL_RTX
))
3087 bitmap_ior_into (kill
, group
->group_kill
);
3088 bitmap_and_compl_into (gen
, group
->group_kill
);
3094 read_info
= read_info
->next
;
3098 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3102 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3106 if (read_info
->alias_set
)
3108 int index
= get_bitmap_index (clear_alias_group
,
3109 read_info
->alias_set
);
3113 bitmap_set_bit (kill
, index
);
3114 bitmap_clear_bit (gen
, index
);
3118 read_info
= read_info
->next
;
3123 /* Return the insn in BB_INFO before the first wild read or if there
3124 are no wild reads in the block, return the last insn. */
3127 find_insn_before_first_wild_read (bb_info_t bb_info
)
3129 insn_info_t insn_info
= bb_info
->last_insn
;
3130 insn_info_t last_wild_read
= NULL
;
3134 if (insn_info
->wild_read
)
3136 last_wild_read
= insn_info
->prev_insn
;
3137 /* Block starts with wild read. */
3138 if (!last_wild_read
)
3142 insn_info
= insn_info
->prev_insn
;
3146 return last_wild_read
;
3148 return bb_info
->last_insn
;
3152 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3153 the block in order to build the gen and kill sets for the block.
3154 We start at ptr which may be the last insn in the block or may be
3155 the first insn with a wild read. In the latter case we are able to
3156 skip the rest of the block because it just does not matter:
3157 anything that happens is hidden by the wild read. */
3160 dse_step3_scan (bool for_spills
, basic_block bb
)
3162 bb_info_t bb_info
= bb_table
[bb
->index
];
3163 insn_info_t insn_info
;
3166 /* There are no wild reads in the spill case. */
3167 insn_info
= bb_info
->last_insn
;
3169 insn_info
= find_insn_before_first_wild_read (bb_info
);
3171 /* In the spill case or in the no_spill case if there is no wild
3172 read in the block, we will need a kill set. */
3173 if (insn_info
== bb_info
->last_insn
)
3176 bitmap_clear (bb_info
->kill
);
3178 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3182 BITMAP_FREE (bb_info
->kill
);
3186 /* There may have been code deleted by the dce pass run before
3188 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3190 /* Process the read(s) last. */
3193 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3194 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3198 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3199 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3203 insn_info
= insn_info
->prev_insn
;
3208 /* Set the gen set of the exit block, and also any block with no
3209 successors that does not have a wild read. */
3212 dse_step3_exit_block_scan (bb_info_t bb_info
)
3214 /* The gen set is all 0's for the exit block except for the
3215 frame_pointer_group. */
3217 if (stores_off_frame_dead_at_return
)
3222 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3224 if (group
->process_globally
&& group
->frame_related
)
3225 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3231 /* Find all of the blocks that are not backwards reachable from the
3232 exit block or any block with no successors (BB). These are the
3233 infinite loops or infinite self loops. These blocks will still
3234 have their bits set in UNREACHABLE_BLOCKS. */
3237 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3242 if (bitmap_bit_p (unreachable_blocks
, bb
->index
))
3244 bitmap_clear_bit (unreachable_blocks
, bb
->index
);
3245 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3247 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3252 /* Build the transfer functions for the function. */
3255 dse_step3 (bool for_spills
)
3258 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
3259 sbitmap_iterator sbi
;
3260 bitmap all_ones
= NULL
;
3263 bitmap_ones (unreachable_blocks
);
3265 FOR_ALL_BB_FN (bb
, cfun
)
3267 bb_info_t bb_info
= bb_table
[bb
->index
];
3269 bitmap_clear (bb_info
->gen
);
3271 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3273 if (bb
->index
== ENTRY_BLOCK
)
3275 else if (bb
->index
== EXIT_BLOCK
)
3276 dse_step3_exit_block_scan (bb_info
);
3278 dse_step3_scan (for_spills
, bb
);
3279 if (EDGE_COUNT (bb
->succs
) == 0)
3280 mark_reachable_blocks (unreachable_blocks
, bb
);
3282 /* If this is the second time dataflow is run, delete the old
3285 BITMAP_FREE (bb_info
->in
);
3287 BITMAP_FREE (bb_info
->out
);
3290 /* For any block in an infinite loop, we must initialize the out set
3291 to all ones. This could be expensive, but almost never occurs in
3292 practice. However, it is common in regression tests. */
3293 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks
, 0, i
, sbi
)
3295 if (bitmap_bit_p (all_blocks
, i
))
3297 bb_info_t bb_info
= bb_table
[i
];
3303 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3304 FOR_EACH_VEC_ELT (rtx_group_vec
, j
, group
)
3305 bitmap_ior_into (all_ones
, group
->group_kill
);
3309 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3310 bitmap_copy (bb_info
->out
, all_ones
);
3316 BITMAP_FREE (all_ones
);
3317 sbitmap_free (unreachable_blocks
);
3322 /*----------------------------------------------------------------------------
3325 Solve the bitvector equations.
3326 ----------------------------------------------------------------------------*/
3329 /* Confluence function for blocks with no successors. Create an out
3330 set from the gen set of the exit block. This block logically has
3331 the exit block as a successor. */
3336 dse_confluence_0 (basic_block bb
)
3338 bb_info_t bb_info
= bb_table
[bb
->index
];
3340 if (bb
->index
== EXIT_BLOCK
)
3345 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3346 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3350 /* Propagate the information from the in set of the dest of E to the
3351 out set of the src of E. If the various in or out sets are not
3352 there, that means they are all ones. */
3355 dse_confluence_n (edge e
)
3357 bb_info_t src_info
= bb_table
[e
->src
->index
];
3358 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3363 bitmap_and_into (src_info
->out
, dest_info
->in
);
3366 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3367 bitmap_copy (src_info
->out
, dest_info
->in
);
3374 /* Propagate the info from the out to the in set of BB_INDEX's basic
3375 block. There are three cases:
3377 1) The block has no kill set. In this case the kill set is all
3378 ones. It does not matter what the out set of the block is, none of
3379 the info can reach the top. The only thing that reaches the top is
3380 the gen set and we just copy the set.
3382 2) There is a kill set but no out set and bb has successors. In
3383 this case we just return. Eventually an out set will be created and
3384 it is better to wait than to create a set of ones.
3386 3) There is both a kill and out set. We apply the obvious transfer
3391 dse_transfer_function (int bb_index
)
3393 bb_info_t bb_info
= bb_table
[bb_index
];
3401 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3402 bb_info
->out
, bb_info
->kill
);
3405 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3406 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3407 bb_info
->out
, bb_info
->kill
);
3417 /* Case 1 above. If there is already an in set, nothing
3423 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3424 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3430 /* Solve the dataflow equations. */
3435 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3436 dse_confluence_n
, dse_transfer_function
,
3437 all_blocks
, df_get_postorder (DF_BACKWARD
),
3438 df_get_n_blocks (DF_BACKWARD
));
3439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3443 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3444 FOR_ALL_BB_FN (bb
, cfun
)
3446 bb_info_t bb_info
= bb_table
[bb
->index
];
3448 df_print_bb_index (bb
, dump_file
);
3450 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3452 fprintf (dump_file
, " in: *MISSING*\n");
3454 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3456 fprintf (dump_file
, " gen: *MISSING*\n");
3458 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3460 fprintf (dump_file
, " kill: *MISSING*\n");
3462 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3464 fprintf (dump_file
, " out: *MISSING*\n\n");
3471 /*----------------------------------------------------------------------------
3474 Delete the stores that can only be deleted using the global information.
3475 ----------------------------------------------------------------------------*/
3479 dse_step5_nospill (void)
3482 FOR_EACH_BB_FN (bb
, cfun
)
3484 bb_info_t bb_info
= bb_table
[bb
->index
];
3485 insn_info_t insn_info
= bb_info
->last_insn
;
3486 bitmap v
= bb_info
->out
;
3490 bool deleted
= false;
3491 if (dump_file
&& insn_info
->insn
)
3493 fprintf (dump_file
, "starting to process insn %d\n",
3494 INSN_UID (insn_info
->insn
));
3495 bitmap_print (dump_file
, v
, " v: ", "\n");
3498 /* There may have been code deleted by the dce pass run before
3501 && INSN_P (insn_info
->insn
)
3502 && (!insn_info
->cannot_delete
)
3503 && (!bitmap_empty_p (v
)))
3505 store_info
*store_info
= insn_info
->store_rec
;
3507 /* Try to delete the current insn. */
3510 /* Skip the clobbers. */
3511 while (!store_info
->is_set
)
3512 store_info
= store_info
->next
;
3514 if (store_info
->alias_set
)
3519 group_info
*group_info
3520 = rtx_group_vec
[store_info
->group_id
];
3522 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3524 int index
= get_bitmap_index (group_info
, i
);
3526 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3527 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3528 if (index
== 0 || !bitmap_bit_p (v
, index
))
3530 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3531 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3540 && check_for_inc_dec_1 (insn_info
))
3542 delete_insn (insn_info
->insn
);
3543 insn_info
->insn
= NULL
;
3548 /* We do want to process the local info if the insn was
3549 deleted. For instance, if the insn did a wild read, we
3550 no longer need to trash the info. */
3552 && INSN_P (insn_info
->insn
)
3555 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3556 if (insn_info
->wild_read
)
3558 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3559 fprintf (dump_file
, "wild read\n");
3562 else if (insn_info
->read_rec
3563 || insn_info
->non_frame_wild_read
)
3565 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3566 fprintf (dump_file
, "regular read\n");
3567 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3568 fprintf (dump_file
, "non-frame wild read\n");
3569 scan_reads_nospill (insn_info
, v
, NULL
);
3573 insn_info
= insn_info
->prev_insn
;
3580 /*----------------------------------------------------------------------------
3583 Delete stores made redundant by earlier stores (which store the same
3584 value) that couldn't be eliminated.
3585 ----------------------------------------------------------------------------*/
3592 FOR_ALL_BB_FN (bb
, cfun
)
3594 bb_info_t bb_info
= bb_table
[bb
->index
];
3595 insn_info_t insn_info
= bb_info
->last_insn
;
3599 /* There may have been code deleted by the dce pass run before
3602 && INSN_P (insn_info
->insn
)
3603 && !insn_info
->cannot_delete
)
3605 store_info
*s_info
= insn_info
->store_rec
;
3607 while (s_info
&& !s_info
->is_set
)
3608 s_info
= s_info
->next
;
3610 && s_info
->redundant_reason
3611 && s_info
->redundant_reason
->insn
3612 && INSN_P (s_info
->redundant_reason
->insn
))
3614 rtx_insn
*rinsn
= s_info
->redundant_reason
->insn
;
3615 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3616 fprintf (dump_file
, "Locally deleting insn %d "
3617 "because insn %d stores the "
3618 "same value and couldn't be "
3620 INSN_UID (insn_info
->insn
),
3622 delete_dead_store_insn (insn_info
);
3625 insn_info
= insn_info
->prev_insn
;
3630 /*----------------------------------------------------------------------------
3633 Destroy everything left standing.
3634 ----------------------------------------------------------------------------*/
3639 bitmap_obstack_release (&dse_bitmap_obstack
);
3640 obstack_free (&dse_obstack
, NULL
);
3642 end_alias_analysis ();
3644 delete rtx_group_table
;
3645 rtx_group_table
= NULL
;
3646 rtx_group_vec
.release ();
3647 BITMAP_FREE (all_blocks
);
3648 BITMAP_FREE (scratch
);
3650 rtx_store_info_pool
.release ();
3651 read_info_type_pool
.release ();
3652 insn_info_type_pool
.release ();
3653 dse_bb_info_type_pool
.release ();
3654 group_info_pool
.release ();
3655 deferred_change_pool
.release ();
3659 /* -------------------------------------------------------------------------
3661 ------------------------------------------------------------------------- */
3663 /* Callback for running pass_rtl_dse. */
3666 rest_of_handle_dse (void)
3668 df_set_flags (DF_DEFER_INSN_RESCAN
);
3670 /* Need the notes since we must track live hardregs in the forwards
3672 df_note_add_problem ();
3678 if (dse_step2_nospill ())
3680 df_set_flags (DF_LR_RUN_DCE
);
3682 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3683 fprintf (dump_file
, "doing global processing\n");
3686 dse_step5_nospill ();
3693 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3694 locally_deleted
, globally_deleted
, spill_deleted
);
3696 /* DSE can eliminate potentially-trapping MEMs.
3697 Remove any EH edges associated with them. */
3698 if ((locally_deleted
|| globally_deleted
)
3699 && cfun
->can_throw_non_call_exceptions
3700 && purge_all_dead_edges ())
3708 const pass_data pass_data_rtl_dse1
=
3710 RTL_PASS
, /* type */
3712 OPTGROUP_NONE
, /* optinfo_flags */
3713 TV_DSE1
, /* tv_id */
3714 0, /* properties_required */
3715 0, /* properties_provided */
3716 0, /* properties_destroyed */
3717 0, /* todo_flags_start */
3718 TODO_df_finish
, /* todo_flags_finish */
3721 class pass_rtl_dse1
: public rtl_opt_pass
3724 pass_rtl_dse1 (gcc::context
*ctxt
)
3725 : rtl_opt_pass (pass_data_rtl_dse1
, ctxt
)
3728 /* opt_pass methods: */
3729 virtual bool gate (function
*)
3731 return optimize
> 0 && flag_dse
&& dbg_cnt (dse1
);
3734 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3736 }; // class pass_rtl_dse1
3741 make_pass_rtl_dse1 (gcc::context
*ctxt
)
3743 return new pass_rtl_dse1 (ctxt
);
3748 const pass_data pass_data_rtl_dse2
=
3750 RTL_PASS
, /* type */
3752 OPTGROUP_NONE
, /* optinfo_flags */
3753 TV_DSE2
, /* tv_id */
3754 0, /* properties_required */
3755 0, /* properties_provided */
3756 0, /* properties_destroyed */
3757 0, /* todo_flags_start */
3758 TODO_df_finish
, /* todo_flags_finish */
3761 class pass_rtl_dse2
: public rtl_opt_pass
3764 pass_rtl_dse2 (gcc::context
*ctxt
)
3765 : rtl_opt_pass (pass_data_rtl_dse2
, ctxt
)
3768 /* opt_pass methods: */
3769 virtual bool gate (function
*)
3771 return optimize
> 0 && flag_dse
&& dbg_cnt (dse2
);
3774 virtual unsigned int execute (function
*) { return rest_of_handle_dse (); }
3776 }; // class pass_rtl_dse2
3781 make_pass_rtl_dse2 (gcc::context
*ctxt
)
3783 return new pass_rtl_dse2 (ctxt
);