1 /* RTL dead store elimination.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
5 and Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
27 #include "coretypes.h"
28 #include "hash-table.h"
32 #include "stor-layout.h"
35 #include "hard-reg-set.h"
40 #include "tree-pass.h"
41 #include "alloc-pool.h"
43 #include "insn-config.h"
50 #include "pointer-set.h"
51 #include "tree-ssa-alias.h"
52 #include "internal-fn.h"
53 #include "gimple-expr.h"
56 #include "gimple-ssa.h"
58 /* This file contains three techniques for performing Dead Store
61 * The first technique performs dse locally on any base address. It
62 is based on the cselib which is a local value numbering technique.
63 This technique is local to a basic block but deals with a fairly
66 * The second technique performs dse globally but is restricted to
67 base addresses that are either constant or are relative to the
70 * The third technique, (which is only done after register allocation)
71 processes the spill spill slots. This differs from the second
72 technique because it takes advantage of the fact that spilling is
73 completely free from the effects of aliasing.
75 Logically, dse is a backwards dataflow problem. A store can be
76 deleted if it if cannot be reached in the backward direction by any
77 use of the value being stored. However, the local technique uses a
78 forwards scan of the basic block because cselib requires that the
79 block be processed in that order.
81 The pass is logically broken into 7 steps:
85 1) The local algorithm, as well as scanning the insns for the two
88 2) Analysis to see if the global algs are necessary. In the case
89 of stores base on a constant address, there must be at least two
90 stores to that address, to make it possible to delete some of the
91 stores. In the case of stores off of the frame or spill related
92 stores, only one store to an address is necessary because those
93 stores die at the end of the function.
95 3) Set up the global dataflow equations based on processing the
96 info parsed in the first step.
98 4) Solve the dataflow equations.
100 5) Delete the insns that the global analysis has indicated are
103 6) Delete insns that store the same value as preceding store
104 where the earlier store couldn't be eliminated.
108 This step uses cselib and canon_rtx to build the largest expression
109 possible for each address. This pass is a forwards pass through
110 each basic block. From the point of view of the global technique,
111 the first pass could examine a block in either direction. The
112 forwards ordering is to accommodate cselib.
114 We make a simplifying assumption: addresses fall into four broad
117 1) base has rtx_varies_p == false, offset is constant.
118 2) base has rtx_varies_p == false, offset variable.
119 3) base has rtx_varies_p == true, offset constant.
120 4) base has rtx_varies_p == true, offset variable.
122 The local passes are able to process all 4 kinds of addresses. The
123 global pass only handles 1).
125 The global problem is formulated as follows:
127 A store, S1, to address A, where A is not relative to the stack
128 frame, can be eliminated if all paths from S1 to the end of the
129 function contain another store to A before a read to A.
131 If the address A is relative to the stack frame, a store S2 to A
132 can be eliminated if there are no paths from S2 that reach the
133 end of the function that read A before another store to A. In
134 this case S2 can be deleted if there are paths from S2 to the
135 end of the function that have no reads or writes to A. This
136 second case allows stores to the stack frame to be deleted that
137 would otherwise die when the function returns. This cannot be
138 done if stores_off_frame_dead_at_return is not true. See the doc
139 for that variable for when this variable is false.
141 The global problem is formulated as a backwards set union
142 dataflow problem where the stores are the gens and reads are the
143 kills. Set union problems are rare and require some special
144 handling given our representation of bitmaps. A straightforward
145 implementation requires a lot of bitmaps filled with 1s.
146 These are expensive and cumbersome in our bitmap formulation so
147 care has been taken to avoid large vectors filled with 1s. See
148 the comments in bb_info and in the dataflow confluence functions
151 There are two places for further enhancements to this algorithm:
153 1) The original dse which was embedded in a pass called flow also
154 did local address forwarding. For example in
159 flow would replace the right hand side of the second insn with a
160 reference to r100. Most of the information is available to add this
161 to this pass. It has not done it because it is a lot of work in
162 the case that either r100 is assigned to between the first and
163 second insn and/or the second insn is a load of part of the value
164 stored by the first insn.
166 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
167 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
168 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
169 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
171 2) The cleaning up of spill code is quite profitable. It currently
172 depends on reading tea leaves and chicken entrails left by reload.
173 This pass depends on reload creating a singleton alias set for each
174 spill slot and telling the next dse pass which of these alias sets
175 are the singletons. Rather than analyze the addresses of the
176 spills, dse's spill processing just does analysis of the loads and
177 stores that use those alias sets. There are three cases where this
180 a) Reload sometimes creates the slot for one mode of access, and
181 then inserts loads and/or stores for a smaller mode. In this
182 case, the current code just punts on the slot. The proper thing
183 to do is to back out and use one bit vector position for each
184 byte of the entity associated with the slot. This depends on
185 KNOWING that reload always generates the accesses for each of the
186 bytes in some canonical (read that easy to understand several
187 passes after reload happens) way.
189 b) Reload sometimes decides that spill slot it allocated was not
190 large enough for the mode and goes back and allocates more slots
191 with the same mode and alias set. The backout in this case is a
192 little more graceful than (a). In this case the slot is unmarked
193 as being a spill slot and if final address comes out to be based
194 off the frame pointer, the global algorithm handles this slot.
196 c) For any pass that may prespill, there is currently no
197 mechanism to tell the dse pass that the slot being used has the
198 special properties that reload uses. It may be that all that is
199 required is to have those passes make the same calls that reload
200 does, assuming that the alias sets can be manipulated in the same
203 /* There are limits to the size of constant offsets we model for the
204 global problem. There are certainly test cases, that exceed this
205 limit, however, it is unlikely that there are important programs
206 that really have constant offsets this size. */
207 #define MAX_OFFSET (64 * 1024)
209 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
210 on the default obstack because these bitmaps can grow quite large
211 (~2GB for the small (!) test case of PR54146) and we'll hold on to
212 all that memory until the end of the compiler run.
213 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
214 releasing the whole obstack. */
215 static bitmap_obstack dse_bitmap_obstack
;
217 /* Obstack for other data. As for above: Kinda nice to be able to
218 throw it all away at the end in one big sweep. */
219 static struct obstack dse_obstack
;
221 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
222 static bitmap scratch
= NULL
;
226 /* This structure holds information about a candidate store. */
230 /* False means this is a clobber. */
233 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
236 /* The id of the mem group of the base address. If rtx_varies_p is
237 true, this is -1. Otherwise, it is the index into the group
241 /* This is the cselib value. */
242 cselib_val
*cse_base
;
244 /* This canonized mem. */
247 /* Canonized MEM address for use by canon_true_dependence. */
250 /* If this is non-zero, it is the alias set of a spill location. */
251 alias_set_type alias_set
;
253 /* The offset of the first and byte before the last byte associated
254 with the operation. */
255 HOST_WIDE_INT begin
, end
;
259 /* A bitmask as wide as the number of bytes in the word that
260 contains a 1 if the byte may be needed. The store is unused if
261 all of the bits are 0. This is used if IS_LARGE is false. */
262 unsigned HOST_WIDE_INT small_bitmask
;
266 /* A bitmap with one bit per byte. Cleared bit means the position
267 is needed. Used if IS_LARGE is false. */
270 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
271 equal to END - BEGIN, the whole store is unused. */
276 /* The next store info for this insn. */
277 struct store_info
*next
;
279 /* The right hand side of the store. This is used if there is a
280 subsequent reload of the mems address somewhere later in the
284 /* If rhs is or holds a constant, this contains that constant,
288 /* Set if this store stores the same constant value as REDUNDANT_REASON
289 insn stored. These aren't eliminated early, because doing that
290 might prevent the earlier larger store to be eliminated. */
291 struct insn_info
*redundant_reason
;
294 /* Return a bitmask with the first N low bits set. */
296 static unsigned HOST_WIDE_INT
297 lowpart_bitmask (int n
)
299 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
300 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
303 typedef struct store_info
*store_info_t
;
304 static alloc_pool cse_store_info_pool
;
305 static alloc_pool rtx_store_info_pool
;
307 /* This structure holds information about a load. These are only
308 built for rtx bases. */
311 /* The id of the mem group of the base address. */
314 /* If this is non-zero, it is the alias set of a spill location. */
315 alias_set_type alias_set
;
317 /* The offset of the first and byte after the last byte associated
318 with the operation. If begin == end == 0, the read did not have
319 a constant offset. */
322 /* The mem being read. */
325 /* The next read_info for this insn. */
326 struct read_info
*next
;
328 typedef struct read_info
*read_info_t
;
329 static alloc_pool read_info_pool
;
332 /* One of these records is created for each insn. */
336 /* Set true if the insn contains a store but the insn itself cannot
337 be deleted. This is set if the insn is a parallel and there is
338 more than one non dead output or if the insn is in some way
342 /* This field is only used by the global algorithm. It is set true
343 if the insn contains any read of mem except for a (1). This is
344 also set if the insn is a call or has a clobber mem. If the insn
345 contains a wild read, the use_rec will be null. */
348 /* This is true only for CALL instructions which could potentially read
349 any non-frame memory location. This field is used by the global
351 bool non_frame_wild_read
;
353 /* This field is only used for the processing of const functions.
354 These functions cannot read memory, but they can read the stack
355 because that is where they may get their parms. We need to be
356 this conservative because, like the store motion pass, we don't
357 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
358 Moreover, we need to distinguish two cases:
359 1. Before reload (register elimination), the stores related to
360 outgoing arguments are stack pointer based and thus deemed
361 of non-constant base in this pass. This requires special
362 handling but also means that the frame pointer based stores
363 need not be killed upon encountering a const function call.
364 2. After reload, the stores related to outgoing arguments can be
365 either stack pointer or hard frame pointer based. This means
366 that we have no other choice than also killing all the frame
367 pointer based stores upon encountering a const function call.
368 This field is set after reload for const function calls. Having
369 this set is less severe than a wild read, it just means that all
370 the frame related stores are killed rather than all the stores. */
373 /* This field is only used for the processing of const functions.
374 It is set if the insn may contain a stack pointer based store. */
375 bool stack_pointer_based
;
377 /* This is true if any of the sets within the store contains a
378 cselib base. Such stores can only be deleted by the local
380 bool contains_cselib_groups
;
385 /* The list of mem sets or mem clobbers that are contained in this
386 insn. If the insn is deletable, it contains only one mem set.
387 But it could also contain clobbers. Insns that contain more than
388 one mem set are not deletable, but each of those mems are here in
389 order to provide info to delete other insns. */
390 store_info_t store_rec
;
392 /* The linked list of mem uses in this insn. Only the reads from
393 rtx bases are listed here. The reads to cselib bases are
394 completely processed during the first scan and so are never
396 read_info_t read_rec
;
398 /* The live fixed registers. We assume only fixed registers can
399 cause trouble by being clobbered from an expanded pattern;
400 storing only the live fixed registers (rather than all registers)
401 means less memory needs to be allocated / copied for the individual
403 regset fixed_regs_live
;
405 /* The prev insn in the basic block. */
406 struct insn_info
* prev_insn
;
408 /* The linked list of insns that are in consideration for removal in
409 the forwards pass through the basic block. This pointer may be
410 trash as it is not cleared when a wild read occurs. The only
411 time it is guaranteed to be correct is when the traversal starts
412 at active_local_stores. */
413 struct insn_info
* next_local_store
;
416 typedef struct insn_info
*insn_info_t
;
417 static alloc_pool insn_info_pool
;
419 /* The linked list of stores that are under consideration in this
421 static insn_info_t active_local_stores
;
422 static int active_local_stores_len
;
427 /* Pointer to the insn info for the last insn in the block. These
428 are linked so this is how all of the insns are reached. During
429 scanning this is the current insn being scanned. */
430 insn_info_t last_insn
;
432 /* The info for the global dataflow problem. */
435 /* This is set if the transfer function should and in the wild_read
436 bitmap before applying the kill and gen sets. That vector knocks
437 out most of the bits in the bitmap and thus speeds up the
439 bool apply_wild_read
;
441 /* The following 4 bitvectors hold information about which positions
442 of which stores are live or dead. They are indexed by
445 /* The set of store positions that exist in this block before a wild read. */
448 /* The set of load positions that exist in this block above the
449 same position of a store. */
452 /* The set of stores that reach the top of the block without being
455 Do not represent the in if it is all ones. Note that this is
456 what the bitvector should logically be initialized to for a set
457 intersection problem. However, like the kill set, this is too
458 expensive. So initially, the in set will only be created for the
459 exit block and any block that contains a wild read. */
462 /* The set of stores that reach the bottom of the block from it's
465 Do not represent the in if it is all ones. Note that this is
466 what the bitvector should logically be initialized to for a set
467 intersection problem. However, like the kill and in set, this is
468 too expensive. So what is done is that the confluence operator
469 just initializes the vector from one of the out sets of the
470 successors of the block. */
473 /* The following bitvector is indexed by the reg number. It
474 contains the set of regs that are live at the current instruction
475 being processed. While it contains info for all of the
476 registers, only the hard registers are actually examined. It is used
477 to assure that shift and/or add sequences that are inserted do not
478 accidentally clobber live hard regs. */
482 typedef struct bb_info
*bb_info_t
;
483 static alloc_pool bb_info_pool
;
485 /* Table to hold all bb_infos. */
486 static bb_info_t
*bb_table
;
488 /* There is a group_info for each rtx base that is used to reference
489 memory. There are also not many of the rtx bases because they are
490 very limited in scope. */
494 /* The actual base of the address. */
497 /* The sequential id of the base. This allows us to have a
498 canonical ordering of these that is not based on addresses. */
501 /* True if there are any positions that are to be processed
503 bool process_globally
;
505 /* True if the base of this group is either the frame_pointer or
506 hard_frame_pointer. */
509 /* A mem wrapped around the base pointer for the group in order to do
510 read dependency. It must be given BLKmode in order to encompass all
511 the possible offsets from the base. */
514 /* Canonized version of base_mem's address. */
517 /* These two sets of two bitmaps are used to keep track of how many
518 stores are actually referencing that position from this base. We
519 only do this for rtx bases as this will be used to assign
520 positions in the bitmaps for the global problem. Bit N is set in
521 store1 on the first store for offset N. Bit N is set in store2
522 for the second store to offset N. This is all we need since we
523 only care about offsets that have two or more stores for them.
525 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
526 for 0 and greater offsets.
528 There is one special case here, for stores into the stack frame,
529 we will or store1 into store2 before deciding which stores look
530 at globally. This is because stores to the stack frame that have
531 no other reads before the end of the function can also be
533 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
535 /* These bitmaps keep track of offsets in this group escape this function.
536 An offset escapes if it corresponds to a named variable whose
537 addressable flag is set. */
538 bitmap escaped_n
, escaped_p
;
540 /* The positions in this bitmap have the same assignments as the in,
541 out, gen and kill bitmaps. This bitmap is all zeros except for
542 the positions that are occupied by stores for this group. */
545 /* The offset_map is used to map the offsets from this base into
546 positions in the global bitmaps. It is only created after all of
547 the all of stores have been scanned and we know which ones we
549 int *offset_map_n
, *offset_map_p
;
550 int offset_map_size_n
, offset_map_size_p
;
552 typedef struct group_info
*group_info_t
;
553 typedef const struct group_info
*const_group_info_t
;
554 static alloc_pool rtx_group_info_pool
;
556 /* Index into the rtx_group_vec. */
557 static int rtx_group_next_id
;
560 static vec
<group_info_t
> rtx_group_vec
;
563 /* This structure holds the set of changes that are being deferred
564 when removing read operation. See replace_read. */
565 struct deferred_change
568 /* The mem that is being replaced. */
571 /* The reg it is being replaced with. */
574 struct deferred_change
*next
;
577 typedef struct deferred_change
*deferred_change_t
;
578 static alloc_pool deferred_change_pool
;
580 static deferred_change_t deferred_change_list
= NULL
;
582 /* The group that holds all of the clear_alias_sets. */
583 static group_info_t clear_alias_group
;
585 /* The modes of the clear_alias_sets. */
586 static htab_t clear_alias_mode_table
;
588 /* Hash table element to look up the mode for an alias set. */
589 struct clear_alias_mode_holder
591 alias_set_type alias_set
;
592 enum machine_mode mode
;
595 /* This is true except if cfun->stdarg -- i.e. we cannot do
596 this for vararg functions because they play games with the frame. */
597 static bool stores_off_frame_dead_at_return
;
599 /* Counter for stats. */
600 static int globally_deleted
;
601 static int locally_deleted
;
602 static int spill_deleted
;
604 static bitmap all_blocks
;
606 /* Locations that are killed by calls in the global phase. */
607 static bitmap kill_on_calls
;
609 /* The number of bits used in the global bitmaps. */
610 static unsigned int current_position
;
613 static bool gate_dse1 (void);
614 static bool gate_dse2 (void);
617 /*----------------------------------------------------------------------------
621 ----------------------------------------------------------------------------*/
624 /* Find the entry associated with ALIAS_SET. */
626 static struct clear_alias_mode_holder
*
627 clear_alias_set_lookup (alias_set_type alias_set
)
629 struct clear_alias_mode_holder tmp_holder
;
632 tmp_holder
.alias_set
= alias_set
;
633 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
636 return (struct clear_alias_mode_holder
*) *slot
;
640 /* Hashtable callbacks for maintaining the "bases" field of
641 store_group_info, given that the addresses are function invariants. */
643 struct invariant_group_base_hasher
: typed_noop_remove
<group_info
>
645 typedef group_info value_type
;
646 typedef group_info compare_type
;
647 static inline hashval_t
hash (const value_type
*);
648 static inline bool equal (const value_type
*, const compare_type
*);
652 invariant_group_base_hasher::equal (const value_type
*gi1
,
653 const compare_type
*gi2
)
655 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
659 invariant_group_base_hasher::hash (const value_type
*gi
)
662 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
665 /* Tables of group_info structures, hashed by base value. */
666 static hash_table
<invariant_group_base_hasher
> rtx_group_table
;
669 /* Get the GROUP for BASE. Add a new group if it is not there. */
672 get_group_info (rtx base
)
674 struct group_info tmp_gi
;
680 /* Find the store_base_info structure for BASE, creating a new one
682 tmp_gi
.rtx_base
= base
;
683 slot
= rtx_group_table
.find_slot (&tmp_gi
, INSERT
);
684 gi
= (group_info_t
) *slot
;
688 if (!clear_alias_group
)
690 clear_alias_group
= gi
=
691 (group_info_t
) pool_alloc (rtx_group_info_pool
);
692 memset (gi
, 0, sizeof (struct group_info
));
693 gi
->id
= rtx_group_next_id
++;
694 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
695 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
696 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
697 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
698 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
699 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
700 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
701 gi
->process_globally
= false;
702 gi
->offset_map_size_n
= 0;
703 gi
->offset_map_size_p
= 0;
704 gi
->offset_map_n
= NULL
;
705 gi
->offset_map_p
= NULL
;
706 rtx_group_vec
.safe_push (gi
);
708 return clear_alias_group
;
713 *slot
= gi
= (group_info_t
) pool_alloc (rtx_group_info_pool
);
715 gi
->id
= rtx_group_next_id
++;
716 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
717 gi
->canon_base_addr
= canon_rtx (base
);
718 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
719 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
720 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
721 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
722 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
723 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
724 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
725 gi
->process_globally
= false;
727 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
728 gi
->offset_map_size_n
= 0;
729 gi
->offset_map_size_p
= 0;
730 gi
->offset_map_n
= NULL
;
731 gi
->offset_map_p
= NULL
;
732 rtx_group_vec
.safe_push (gi
);
739 /* Initialization of data structures. */
745 globally_deleted
= 0;
748 bitmap_obstack_initialize (&dse_bitmap_obstack
);
749 gcc_obstack_init (&dse_obstack
);
751 scratch
= BITMAP_ALLOC (®_obstack
);
752 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
755 = create_alloc_pool ("rtx_store_info_pool",
756 sizeof (struct store_info
), 100);
758 = create_alloc_pool ("read_info_pool",
759 sizeof (struct read_info
), 100);
761 = create_alloc_pool ("insn_info_pool",
762 sizeof (struct insn_info
), 100);
764 = create_alloc_pool ("bb_info_pool",
765 sizeof (struct bb_info
), 100);
767 = create_alloc_pool ("rtx_group_info_pool",
768 sizeof (struct group_info
), 100);
770 = create_alloc_pool ("deferred_change_pool",
771 sizeof (struct deferred_change
), 10);
773 rtx_group_table
.create (11);
775 bb_table
= XNEWVEC (bb_info_t
, last_basic_block_for_fn (cfun
));
776 rtx_group_next_id
= 0;
778 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
780 init_alias_analysis ();
782 clear_alias_group
= NULL
;
787 /*----------------------------------------------------------------------------
790 Scan all of the insns. Any random ordering of the blocks is fine.
791 Each block is scanned in forward order to accommodate cselib which
792 is used to remove stores with non-constant bases.
793 ----------------------------------------------------------------------------*/
795 /* Delete all of the store_info recs from INSN_INFO. */
798 free_store_info (insn_info_t insn_info
)
800 store_info_t store_info
= insn_info
->store_rec
;
803 store_info_t next
= store_info
->next
;
804 if (store_info
->is_large
)
805 BITMAP_FREE (store_info
->positions_needed
.large
.bmap
);
806 if (store_info
->cse_base
)
807 pool_free (cse_store_info_pool
, store_info
);
809 pool_free (rtx_store_info_pool
, store_info
);
813 insn_info
->cannot_delete
= true;
814 insn_info
->contains_cselib_groups
= false;
815 insn_info
->store_rec
= NULL
;
821 regset fixed_regs_live
;
823 } note_add_store_info
;
825 /* Callback for emit_inc_dec_insn_before via note_stores.
826 Check if a register is clobbered which is live afterwards. */
829 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
832 note_add_store_info
*info
= (note_add_store_info
*) data
;
838 /* If this register is referenced by the current or an earlier insn,
839 that's OK. E.g. this applies to the register that is being incremented
840 with this addition. */
841 for (insn
= info
->first
;
842 insn
!= NEXT_INSN (info
->current
);
843 insn
= NEXT_INSN (insn
))
844 if (reg_referenced_p (loc
, PATTERN (insn
)))
847 /* If we come here, we have a clobber of a register that's only OK
848 if that register is not live. If we don't have liveness information
849 available, fail now. */
850 if (!info
->fixed_regs_live
)
852 info
->failure
= true;
855 /* Now check if this is a live fixed register. */
857 n
= hard_regno_nregs
[r
][GET_MODE (loc
)];
859 if (REGNO_REG_SET_P (info
->fixed_regs_live
, r
+n
))
860 info
->failure
= true;
863 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
864 SRC + SRCOFF before insn ARG. */
867 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
868 rtx op ATTRIBUTE_UNUSED
,
869 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
871 insn_info_t insn_info
= (insn_info_t
) arg
;
872 rtx insn
= insn_info
->insn
, new_insn
, cur
;
873 note_add_store_info info
;
875 /* We can reuse all operands without copying, because we are about
876 to delete the insn that contained it. */
880 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
881 new_insn
= get_insns ();
885 new_insn
= gen_move_insn (dest
, src
);
886 info
.first
= new_insn
;
887 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
888 info
.failure
= false;
889 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
892 note_stores (PATTERN (cur
), note_add_store
, &info
);
895 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
896 return it immediately, communicating the failure to its caller. */
900 emit_insn_before (new_insn
, insn
);
905 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
906 is there, is split into a separate insn.
907 Return true on success (or if there was nothing to do), false on failure. */
910 check_for_inc_dec_1 (insn_info_t insn_info
)
912 rtx insn
= insn_info
->insn
;
913 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
915 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, insn_info
) == 0;
920 /* Entry point for postreload. If you work on reload_cse, or you need this
921 anywhere else, consider if you can provide register liveness information
922 and add a parameter to this function so that it can be passed down in
923 insn_info.fixed_regs_live. */
925 check_for_inc_dec (rtx insn
)
927 struct insn_info insn_info
;
930 insn_info
.insn
= insn
;
931 insn_info
.fixed_regs_live
= NULL
;
932 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
934 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, &insn_info
) == 0;
938 /* Delete the insn and free all of the fields inside INSN_INFO. */
941 delete_dead_store_insn (insn_info_t insn_info
)
943 read_info_t read_info
;
948 if (!check_for_inc_dec_1 (insn_info
))
950 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
952 fprintf (dump_file
, "Locally deleting insn %d ",
953 INSN_UID (insn_info
->insn
));
954 if (insn_info
->store_rec
->alias_set
)
955 fprintf (dump_file
, "alias set %d\n",
956 (int) insn_info
->store_rec
->alias_set
);
958 fprintf (dump_file
, "\n");
961 free_store_info (insn_info
);
962 read_info
= insn_info
->read_rec
;
966 read_info_t next
= read_info
->next
;
967 pool_free (read_info_pool
, read_info
);
970 insn_info
->read_rec
= NULL
;
972 delete_insn (insn_info
->insn
);
974 insn_info
->insn
= NULL
;
976 insn_info
->wild_read
= false;
979 /* Return whether DECL, a local variable, can possibly escape the current
983 local_variable_can_escape (tree decl
)
985 if (TREE_ADDRESSABLE (decl
))
988 /* If this is a partitioned variable, we need to consider all the variables
989 in the partition. This is necessary because a store into one of them can
990 be replaced with a store into another and this may not change the outcome
991 of the escape analysis. */
992 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
995 = pointer_map_contains (cfun
->gimple_df
->decls_to_pointers
, decl
);
997 return TREE_ADDRESSABLE (*(tree
*)namep
);
1003 /* Return whether EXPR can possibly escape the current function scope. */
1006 can_escape (tree expr
)
1011 base
= get_base_address (expr
);
1013 && !may_be_aliased (base
)
1014 && !(TREE_CODE (base
) == VAR_DECL
1015 && !DECL_EXTERNAL (base
)
1016 && !TREE_STATIC (base
)
1017 && local_variable_can_escape (base
)))
1022 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1023 OFFSET and WIDTH. */
1026 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1030 bool expr_escapes
= can_escape (expr
);
1031 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1032 for (i
=offset
; i
<offset
+width
; i
++)
1040 store1
= group
->store1_n
;
1041 store2
= group
->store2_n
;
1042 escaped
= group
->escaped_n
;
1047 store1
= group
->store1_p
;
1048 store2
= group
->store2_p
;
1049 escaped
= group
->escaped_p
;
1053 if (!bitmap_set_bit (store1
, ai
))
1054 bitmap_set_bit (store2
, ai
);
1059 if (group
->offset_map_size_n
< ai
)
1060 group
->offset_map_size_n
= ai
;
1064 if (group
->offset_map_size_p
< ai
)
1065 group
->offset_map_size_p
= ai
;
1069 bitmap_set_bit (escaped
, ai
);
1074 reset_active_stores (void)
1076 active_local_stores
= NULL
;
1077 active_local_stores_len
= 0;
1080 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1083 free_read_records (bb_info_t bb_info
)
1085 insn_info_t insn_info
= bb_info
->last_insn
;
1086 read_info_t
*ptr
= &insn_info
->read_rec
;
1089 read_info_t next
= (*ptr
)->next
;
1090 if ((*ptr
)->alias_set
== 0)
1092 pool_free (read_info_pool
, *ptr
);
1096 ptr
= &(*ptr
)->next
;
1100 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1103 add_wild_read (bb_info_t bb_info
)
1105 insn_info_t insn_info
= bb_info
->last_insn
;
1106 insn_info
->wild_read
= true;
1107 free_read_records (bb_info
);
1108 reset_active_stores ();
1111 /* Set the BB_INFO so that the last insn is marked as a wild read of
1112 non-frame locations. */
1115 add_non_frame_wild_read (bb_info_t bb_info
)
1117 insn_info_t insn_info
= bb_info
->last_insn
;
1118 insn_info
->non_frame_wild_read
= true;
1119 free_read_records (bb_info
);
1120 reset_active_stores ();
1123 /* Return true if X is a constant or one of the registers that behave
1124 as a constant over the life of a function. This is equivalent to
1125 !rtx_varies_p for memory addresses. */
1128 const_or_frame_p (rtx x
)
1133 if (GET_CODE (x
) == REG
)
1135 /* Note that we have to test for the actual rtx used for the frame
1136 and arg pointers and not just the register number in case we have
1137 eliminated the frame and/or arg pointer and are using it
1139 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1140 /* The arg pointer varies if it is not a fixed register. */
1141 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1142 || x
== pic_offset_table_rtx
)
1150 /* Take all reasonable action to put the address of MEM into the form
1151 that we can do analysis on.
1153 The gold standard is to get the address into the form: address +
1154 OFFSET where address is something that rtx_varies_p considers a
1155 constant. When we can get the address in this form, we can do
1156 global analysis on it. Note that for constant bases, address is
1157 not actually returned, only the group_id. The address can be
1160 If that fails, we try cselib to get a value we can at least use
1161 locally. If that fails we return false.
1163 The GROUP_ID is set to -1 for cselib bases and the index of the
1164 group for non_varying bases.
1166 FOR_READ is true if this is a mem read and false if not. */
1169 canon_address (rtx mem
,
1170 alias_set_type
*alias_set_out
,
1172 HOST_WIDE_INT
*offset
,
1175 enum machine_mode address_mode
= get_address_mode (mem
);
1176 rtx mem_address
= XEXP (mem
, 0);
1177 rtx expanded_address
, address
;
1182 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1184 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1186 fprintf (dump_file
, " mem: ");
1187 print_inline_rtx (dump_file
, mem_address
, 0);
1188 fprintf (dump_file
, "\n");
1191 /* First see if just canon_rtx (mem_address) is const or frame,
1192 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1194 for (expanded
= 0; expanded
< 2; expanded
++)
1198 /* Use cselib to replace all of the reg references with the full
1199 expression. This will take care of the case where we have
1201 r_x = base + offset;
1206 val = *(base + offset); */
1208 expanded_address
= cselib_expand_value_rtx (mem_address
,
1211 /* If this fails, just go with the address from first
1213 if (!expanded_address
)
1217 expanded_address
= mem_address
;
1219 /* Split the address into canonical BASE + OFFSET terms. */
1220 address
= canon_rtx (expanded_address
);
1224 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1228 fprintf (dump_file
, "\n after cselib_expand address: ");
1229 print_inline_rtx (dump_file
, expanded_address
, 0);
1230 fprintf (dump_file
, "\n");
1233 fprintf (dump_file
, "\n after canon_rtx address: ");
1234 print_inline_rtx (dump_file
, address
, 0);
1235 fprintf (dump_file
, "\n");
1238 if (GET_CODE (address
) == CONST
)
1239 address
= XEXP (address
, 0);
1241 if (GET_CODE (address
) == PLUS
1242 && CONST_INT_P (XEXP (address
, 1)))
1244 *offset
= INTVAL (XEXP (address
, 1));
1245 address
= XEXP (address
, 0);
1248 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1249 && const_or_frame_p (address
))
1251 group_info_t group
= get_group_info (address
);
1253 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1254 fprintf (dump_file
, " gid=%d offset=%d \n",
1255 group
->id
, (int)*offset
);
1257 *group_id
= group
->id
;
1262 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1267 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1268 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1271 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1272 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1273 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1278 /* Clear the rhs field from the active_local_stores array. */
1281 clear_rhs_from_active_local_stores (void)
1283 insn_info_t ptr
= active_local_stores
;
1287 store_info_t store_info
= ptr
->store_rec
;
1288 /* Skip the clobbers. */
1289 while (!store_info
->is_set
)
1290 store_info
= store_info
->next
;
1292 store_info
->rhs
= NULL
;
1293 store_info
->const_rhs
= NULL
;
1295 ptr
= ptr
->next_local_store
;
1300 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1303 set_position_unneeded (store_info_t s_info
, int pos
)
1305 if (__builtin_expect (s_info
->is_large
, false))
1307 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1308 s_info
->positions_needed
.large
.count
++;
1311 s_info
->positions_needed
.small_bitmask
1312 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1315 /* Mark the whole store S_INFO as unneeded. */
1318 set_all_positions_unneeded (store_info_t s_info
)
1320 if (__builtin_expect (s_info
->is_large
, false))
1322 int pos
, end
= s_info
->end
- s_info
->begin
;
1323 for (pos
= 0; pos
< end
; pos
++)
1324 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1325 s_info
->positions_needed
.large
.count
= end
;
1328 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1331 /* Return TRUE if any bytes from S_INFO store are needed. */
1334 any_positions_needed_p (store_info_t s_info
)
1336 if (__builtin_expect (s_info
->is_large
, false))
1337 return (s_info
->positions_needed
.large
.count
1338 < s_info
->end
- s_info
->begin
);
1340 return (s_info
->positions_needed
.small_bitmask
1341 != (unsigned HOST_WIDE_INT
) 0);
1344 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1345 store are needed. */
1348 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1350 if (__builtin_expect (s_info
->is_large
, false))
1352 int end
= start
+ width
;
1354 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1360 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1361 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1366 static rtx
get_stored_val (store_info_t
, enum machine_mode
, HOST_WIDE_INT
,
1367 HOST_WIDE_INT
, basic_block
, bool);
1370 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1371 there is a candidate store, after adding it to the appropriate
1372 local store group if so. */
1375 record_store (rtx body
, bb_info_t bb_info
)
1377 rtx mem
, rhs
, const_rhs
, mem_addr
;
1378 HOST_WIDE_INT offset
= 0;
1379 HOST_WIDE_INT width
= 0;
1380 alias_set_type spill_alias_set
;
1381 insn_info_t insn_info
= bb_info
->last_insn
;
1382 store_info_t store_info
= NULL
;
1384 cselib_val
*base
= NULL
;
1385 insn_info_t ptr
, last
, redundant_reason
;
1386 bool store_is_unused
;
1388 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1391 mem
= SET_DEST (body
);
1393 /* If this is not used, then this cannot be used to keep the insn
1394 from being deleted. On the other hand, it does provide something
1395 that can be used to prove that another store is dead. */
1397 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1399 /* Check whether that value is a suitable memory location. */
1402 /* If the set or clobber is unused, then it does not effect our
1403 ability to get rid of the entire insn. */
1404 if (!store_is_unused
)
1405 insn_info
->cannot_delete
= true;
1409 /* At this point we know mem is a mem. */
1410 if (GET_MODE (mem
) == BLKmode
)
1412 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1414 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1415 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1416 add_wild_read (bb_info
);
1417 insn_info
->cannot_delete
= true;
1420 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1421 as memset (addr, 0, 36); */
1422 else if (!MEM_SIZE_KNOWN_P (mem
)
1423 || MEM_SIZE (mem
) <= 0
1424 || MEM_SIZE (mem
) > MAX_OFFSET
1425 || GET_CODE (body
) != SET
1426 || !CONST_INT_P (SET_SRC (body
)))
1428 if (!store_is_unused
)
1430 /* If the set or clobber is unused, then it does not effect our
1431 ability to get rid of the entire insn. */
1432 insn_info
->cannot_delete
= true;
1433 clear_rhs_from_active_local_stores ();
1439 /* We can still process a volatile mem, we just cannot delete it. */
1440 if (MEM_VOLATILE_P (mem
))
1441 insn_info
->cannot_delete
= true;
1443 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1445 clear_rhs_from_active_local_stores ();
1449 if (GET_MODE (mem
) == BLKmode
)
1450 width
= MEM_SIZE (mem
);
1452 width
= GET_MODE_SIZE (GET_MODE (mem
));
1454 if (spill_alias_set
)
1456 bitmap store1
= clear_alias_group
->store1_p
;
1457 bitmap store2
= clear_alias_group
->store2_p
;
1459 gcc_assert (GET_MODE (mem
) != BLKmode
);
1461 if (!bitmap_set_bit (store1
, spill_alias_set
))
1462 bitmap_set_bit (store2
, spill_alias_set
);
1464 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1465 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1467 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1469 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1470 fprintf (dump_file
, " processing spill store %d(%s)\n",
1471 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1473 else if (group_id
>= 0)
1475 /* In the restrictive case where the base is a constant or the
1476 frame pointer we can do global analysis. */
1479 = rtx_group_vec
[group_id
];
1480 tree expr
= MEM_EXPR (mem
);
1482 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1483 set_usage_bits (group
, offset
, width
, expr
);
1485 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1486 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1487 group_id
, (int)offset
, (int)(offset
+width
));
1491 if (may_be_sp_based_p (XEXP (mem
, 0)))
1492 insn_info
->stack_pointer_based
= true;
1493 insn_info
->contains_cselib_groups
= true;
1495 store_info
= (store_info_t
) pool_alloc (cse_store_info_pool
);
1498 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1499 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1500 (int)offset
, (int)(offset
+width
));
1503 const_rhs
= rhs
= NULL_RTX
;
1504 if (GET_CODE (body
) == SET
1505 /* No place to keep the value after ra. */
1506 && !reload_completed
1507 && (REG_P (SET_SRC (body
))
1508 || GET_CODE (SET_SRC (body
)) == SUBREG
1509 || CONSTANT_P (SET_SRC (body
)))
1510 && !MEM_VOLATILE_P (mem
)
1511 /* Sometimes the store and reload is used for truncation and
1513 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1515 rhs
= SET_SRC (body
);
1516 if (CONSTANT_P (rhs
))
1518 else if (body
== PATTERN (insn_info
->insn
))
1520 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1521 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1522 const_rhs
= XEXP (tem
, 0);
1524 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1526 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1528 if (tem
&& CONSTANT_P (tem
))
1533 /* Check to see if this stores causes some other stores to be
1535 ptr
= active_local_stores
;
1537 redundant_reason
= NULL
;
1538 mem
= canon_rtx (mem
);
1539 /* For alias_set != 0 canon_true_dependence should be never called. */
1540 if (spill_alias_set
)
1541 mem_addr
= NULL_RTX
;
1545 mem_addr
= base
->val_rtx
;
1549 = rtx_group_vec
[group_id
];
1550 mem_addr
= group
->canon_base_addr
;
1552 /* get_addr can only handle VALUE but cannot handle expr like:
1553 VALUE + OFFSET, so call get_addr to get original addr for
1554 mem_addr before plus_constant. */
1555 mem_addr
= get_addr (mem_addr
);
1557 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1562 insn_info_t next
= ptr
->next_local_store
;
1563 store_info_t s_info
= ptr
->store_rec
;
1566 /* Skip the clobbers. We delete the active insn if this insn
1567 shadows the set. To have been put on the active list, it
1568 has exactly on set. */
1569 while (!s_info
->is_set
)
1570 s_info
= s_info
->next
;
1572 if (s_info
->alias_set
!= spill_alias_set
)
1574 else if (s_info
->alias_set
)
1576 struct clear_alias_mode_holder
*entry
1577 = clear_alias_set_lookup (s_info
->alias_set
);
1578 /* Generally, spills cannot be processed if and of the
1579 references to the slot have a different mode. But if
1580 we are in the same block and mode is exactly the same
1581 between this store and one before in the same block,
1582 we can still delete it. */
1583 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1584 && (GET_MODE (mem
) == entry
->mode
))
1587 set_all_positions_unneeded (s_info
);
1589 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1590 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1591 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1593 else if ((s_info
->group_id
== group_id
)
1594 && (s_info
->cse_base
== base
))
1597 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1598 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1599 INSN_UID (ptr
->insn
), s_info
->group_id
,
1600 (int)s_info
->begin
, (int)s_info
->end
);
1602 /* Even if PTR won't be eliminated as unneeded, if both
1603 PTR and this insn store the same constant value, we might
1604 eliminate this insn instead. */
1605 if (s_info
->const_rhs
1607 && offset
>= s_info
->begin
1608 && offset
+ width
<= s_info
->end
1609 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1612 if (GET_MODE (mem
) == BLKmode
)
1614 if (GET_MODE (s_info
->mem
) == BLKmode
1615 && s_info
->const_rhs
== const_rhs
)
1616 redundant_reason
= ptr
;
1618 else if (s_info
->const_rhs
== const0_rtx
1619 && const_rhs
== const0_rtx
)
1620 redundant_reason
= ptr
;
1625 val
= get_stored_val (s_info
, GET_MODE (mem
),
1626 offset
, offset
+ width
,
1627 BLOCK_FOR_INSN (insn_info
->insn
),
1629 if (get_insns () != NULL
)
1632 if (val
&& rtx_equal_p (val
, const_rhs
))
1633 redundant_reason
= ptr
;
1637 for (i
= MAX (offset
, s_info
->begin
);
1638 i
< offset
+ width
&& i
< s_info
->end
;
1640 set_position_unneeded (s_info
, i
- s_info
->begin
);
1642 else if (s_info
->rhs
)
1643 /* Need to see if it is possible for this store to overwrite
1644 the value of store_info. If it is, set the rhs to NULL to
1645 keep it from being used to remove a load. */
1647 if (canon_true_dependence (s_info
->mem
,
1648 GET_MODE (s_info
->mem
),
1653 s_info
->const_rhs
= NULL
;
1657 /* An insn can be deleted if every position of every one of
1658 its s_infos is zero. */
1659 if (any_positions_needed_p (s_info
))
1664 insn_info_t insn_to_delete
= ptr
;
1666 active_local_stores_len
--;
1668 last
->next_local_store
= ptr
->next_local_store
;
1670 active_local_stores
= ptr
->next_local_store
;
1672 if (!insn_to_delete
->cannot_delete
)
1673 delete_dead_store_insn (insn_to_delete
);
1681 /* Finish filling in the store_info. */
1682 store_info
->next
= insn_info
->store_rec
;
1683 insn_info
->store_rec
= store_info
;
1684 store_info
->mem
= mem
;
1685 store_info
->alias_set
= spill_alias_set
;
1686 store_info
->mem_addr
= mem_addr
;
1687 store_info
->cse_base
= base
;
1688 if (width
> HOST_BITS_PER_WIDE_INT
)
1690 store_info
->is_large
= true;
1691 store_info
->positions_needed
.large
.count
= 0;
1692 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1696 store_info
->is_large
= false;
1697 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1699 store_info
->group_id
= group_id
;
1700 store_info
->begin
= offset
;
1701 store_info
->end
= offset
+ width
;
1702 store_info
->is_set
= GET_CODE (body
) == SET
;
1703 store_info
->rhs
= rhs
;
1704 store_info
->const_rhs
= const_rhs
;
1705 store_info
->redundant_reason
= redundant_reason
;
1707 /* If this is a clobber, we return 0. We will only be able to
1708 delete this insn if there is only one store USED store, but we
1709 can use the clobber to delete other stores earlier. */
1710 return store_info
->is_set
? 1 : 0;
1715 dump_insn_info (const char * start
, insn_info_t insn_info
)
1717 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1718 INSN_UID (insn_info
->insn
),
1719 insn_info
->store_rec
? "has store" : "naked");
1723 /* If the modes are different and the value's source and target do not
1724 line up, we need to extract the value from lower part of the rhs of
1725 the store, shift it, and then put it into a form that can be shoved
1726 into the read_insn. This function generates a right SHIFT of a
1727 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1728 shift sequence is returned or NULL if we failed to find a
1732 find_shift_sequence (int access_size
,
1733 store_info_t store_info
,
1734 enum machine_mode read_mode
,
1735 int shift
, bool speed
, bool require_cst
)
1737 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1738 enum machine_mode new_mode
;
1739 rtx read_reg
= NULL
;
1741 /* Some machines like the x86 have shift insns for each size of
1742 operand. Other machines like the ppc or the ia-64 may only have
1743 shift insns that shift values within 32 or 64 bit registers.
1744 This loop tries to find the smallest shift insn that will right
1745 justify the value we want to read but is available in one insn on
1748 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1750 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1751 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1753 rtx target
, new_reg
, shift_seq
, insn
, new_lhs
;
1756 /* If a constant was stored into memory, try to simplify it here,
1757 otherwise the cost of the shift might preclude this optimization
1758 e.g. at -Os, even when no actual shift will be needed. */
1759 if (store_info
->const_rhs
)
1761 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1762 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1764 if (ret
&& CONSTANT_P (ret
))
1766 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1767 ret
, GEN_INT (shift
));
1768 if (ret
&& CONSTANT_P (ret
))
1770 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1771 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1772 if (ret
&& CONSTANT_P (ret
)
1773 && set_src_cost (ret
, speed
) <= COSTS_N_INSNS (1))
1782 /* Try a wider mode if truncating the store mode to NEW_MODE
1783 requires a real instruction. */
1784 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1785 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1788 /* Also try a wider mode if the necessary punning is either not
1789 desirable or not possible. */
1790 if (!CONSTANT_P (store_info
->rhs
)
1791 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1794 new_reg
= gen_reg_rtx (new_mode
);
1798 /* In theory we could also check for an ashr. Ian Taylor knows
1799 of one dsp where the cost of these two was not the same. But
1800 this really is a rare case anyway. */
1801 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1802 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1804 shift_seq
= get_insns ();
1807 if (target
!= new_reg
|| shift_seq
== NULL
)
1811 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1813 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1815 /* The computation up to here is essentially independent
1816 of the arguments and could be precomputed. It may
1817 not be worth doing so. We could precompute if
1818 worthwhile or at least cache the results. The result
1819 technically depends on both SHIFT and ACCESS_SIZE,
1820 but in practice the answer will depend only on ACCESS_SIZE. */
1822 if (cost
> COSTS_N_INSNS (1))
1825 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1826 copy_rtx (store_info
->rhs
));
1827 if (new_lhs
== NULL_RTX
)
1830 /* We found an acceptable shift. Generate a move to
1831 take the value from the store and put it into the
1832 shift pseudo, then shift it, then generate another
1833 move to put in into the target of the read. */
1834 emit_move_insn (new_reg
, new_lhs
);
1835 emit_insn (shift_seq
);
1836 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1844 /* Call back for note_stores to find the hard regs set or clobbered by
1845 insn. Data is a bitmap of the hardregs set so far. */
1848 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1850 bitmap regs_set
= (bitmap
) data
;
1853 && HARD_REGISTER_P (x
))
1855 unsigned int regno
= REGNO (x
);
1856 bitmap_set_range (regs_set
, regno
,
1857 hard_regno_nregs
[regno
][GET_MODE (x
)]);
1861 /* Helper function for replace_read and record_store.
1862 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1863 to one before READ_END bytes read in READ_MODE. Return NULL
1864 if not successful. If REQUIRE_CST is true, return always constant. */
1867 get_stored_val (store_info_t store_info
, enum machine_mode read_mode
,
1868 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1869 basic_block bb
, bool require_cst
)
1871 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1873 int access_size
; /* In bytes. */
1876 /* To get here the read is within the boundaries of the write so
1877 shift will never be negative. Start out with the shift being in
1879 if (store_mode
== BLKmode
)
1881 else if (BYTES_BIG_ENDIAN
)
1882 shift
= store_info
->end
- read_end
;
1884 shift
= read_begin
- store_info
->begin
;
1886 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1888 /* From now on it is bits. */
1889 shift
*= BITS_PER_UNIT
;
1892 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1893 optimize_bb_for_speed_p (bb
),
1895 else if (store_mode
== BLKmode
)
1897 /* The store is a memset (addr, const_val, const_size). */
1898 gcc_assert (CONST_INT_P (store_info
->rhs
));
1899 store_mode
= int_mode_for_mode (read_mode
);
1900 if (store_mode
== BLKmode
)
1901 read_reg
= NULL_RTX
;
1902 else if (store_info
->rhs
== const0_rtx
)
1903 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1904 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1905 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1906 read_reg
= NULL_RTX
;
1909 unsigned HOST_WIDE_INT c
1910 = INTVAL (store_info
->rhs
)
1911 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1912 int shift
= BITS_PER_UNIT
;
1913 while (shift
< HOST_BITS_PER_WIDE_INT
)
1918 read_reg
= gen_int_mode (c
, store_mode
);
1919 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1922 else if (store_info
->const_rhs
1924 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1925 read_reg
= extract_low_bits (read_mode
, store_mode
,
1926 copy_rtx (store_info
->const_rhs
));
1928 read_reg
= extract_low_bits (read_mode
, store_mode
,
1929 copy_rtx (store_info
->rhs
));
1930 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1931 read_reg
= NULL_RTX
;
1935 /* Take a sequence of:
1958 Depending on the alignment and the mode of the store and
1962 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1963 and READ_INSN are for the read. Return true if the replacement
1967 replace_read (store_info_t store_info
, insn_info_t store_insn
,
1968 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1971 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1972 enum machine_mode read_mode
= GET_MODE (read_info
->mem
);
1973 rtx insns
, this_insn
, read_reg
;
1979 /* Create a sequence of instructions to set up the read register.
1980 This sequence goes immediately before the store and its result
1981 is read by the load.
1983 We need to keep this in perspective. We are replacing a read
1984 with a sequence of insns, but the read will almost certainly be
1985 in cache, so it is not going to be an expensive one. Thus, we
1986 are not willing to do a multi insn shift or worse a subroutine
1987 call to get rid of the read. */
1988 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1989 fprintf (dump_file
, "trying to replace %smode load in insn %d"
1990 " from %smode store in insn %d\n",
1991 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
1992 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
1994 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
1995 read_reg
= get_stored_val (store_info
,
1996 read_mode
, read_info
->begin
, read_info
->end
,
1998 if (read_reg
== NULL_RTX
)
2001 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2002 fprintf (dump_file
, " -- could not extract bits of stored value\n");
2005 /* Force the value into a new register so that it won't be clobbered
2006 between the store and the load. */
2007 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
2008 insns
= get_insns ();
2011 if (insns
!= NULL_RTX
)
2013 /* Now we have to scan the set of new instructions to see if the
2014 sequence contains and sets of hardregs that happened to be
2015 live at this point. For instance, this can happen if one of
2016 the insns sets the CC and the CC happened to be live at that
2017 point. This does occasionally happen, see PR 37922. */
2018 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2020 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2021 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2023 bitmap_and_into (regs_set
, regs_live
);
2024 if (!bitmap_empty_p (regs_set
))
2026 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2029 "abandoning replacement because sequence clobbers live hardregs:");
2030 df_print_regset (dump_file
, regs_set
);
2033 BITMAP_FREE (regs_set
);
2036 BITMAP_FREE (regs_set
);
2039 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2041 deferred_change_t deferred_change
=
2042 (deferred_change_t
) pool_alloc (deferred_change_pool
);
2044 /* Insert this right before the store insn where it will be safe
2045 from later insns that might change it before the read. */
2046 emit_insn_before (insns
, store_insn
->insn
);
2048 /* And now for the kludge part: cselib croaks if you just
2049 return at this point. There are two reasons for this:
2051 1) Cselib has an idea of how many pseudos there are and
2052 that does not include the new ones we just added.
2054 2) Cselib does not know about the move insn we added
2055 above the store_info, and there is no way to tell it
2056 about it, because it has "moved on".
2058 Problem (1) is fixable with a certain amount of engineering.
2059 Problem (2) is requires starting the bb from scratch. This
2062 So we are just going to have to lie. The move/extraction
2063 insns are not really an issue, cselib did not see them. But
2064 the use of the new pseudo read_insn is a real problem because
2065 cselib has not scanned this insn. The way that we solve this
2066 problem is that we are just going to put the mem back for now
2067 and when we are finished with the block, we undo this. We
2068 keep a table of mems to get rid of. At the end of the basic
2069 block we can put them back. */
2071 *loc
= read_info
->mem
;
2072 deferred_change
->next
= deferred_change_list
;
2073 deferred_change_list
= deferred_change
;
2074 deferred_change
->loc
= loc
;
2075 deferred_change
->reg
= read_reg
;
2077 /* Get rid of the read_info, from the point of view of the
2078 rest of dse, play like this read never happened. */
2079 read_insn
->read_rec
= read_info
->next
;
2080 pool_free (read_info_pool
, read_info
);
2081 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2083 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2084 print_simple_rtl (dump_file
, read_reg
);
2085 fprintf (dump_file
, "\n");
2091 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2093 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2094 print_simple_rtl (dump_file
, read_reg
);
2095 fprintf (dump_file
, " led to an invalid instruction\n");
2101 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2102 if LOC is a mem and if it is look at the address and kill any
2103 appropriate stores that may be active. */
2106 check_mem_read_rtx (rtx
*loc
, void *data
)
2108 rtx mem
= *loc
, mem_addr
;
2110 insn_info_t insn_info
;
2111 HOST_WIDE_INT offset
= 0;
2112 HOST_WIDE_INT width
= 0;
2113 alias_set_type spill_alias_set
= 0;
2114 cselib_val
*base
= NULL
;
2116 read_info_t read_info
;
2118 if (!mem
|| !MEM_P (mem
))
2121 bb_info
= (bb_info_t
) data
;
2122 insn_info
= bb_info
->last_insn
;
2124 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2125 || (MEM_VOLATILE_P (mem
)))
2127 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2128 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2129 add_wild_read (bb_info
);
2130 insn_info
->cannot_delete
= true;
2134 /* If it is reading readonly mem, then there can be no conflict with
2136 if (MEM_READONLY_P (mem
))
2139 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2141 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2142 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2143 add_wild_read (bb_info
);
2147 if (GET_MODE (mem
) == BLKmode
)
2150 width
= GET_MODE_SIZE (GET_MODE (mem
));
2152 read_info
= (read_info_t
) pool_alloc (read_info_pool
);
2153 read_info
->group_id
= group_id
;
2154 read_info
->mem
= mem
;
2155 read_info
->alias_set
= spill_alias_set
;
2156 read_info
->begin
= offset
;
2157 read_info
->end
= offset
+ width
;
2158 read_info
->next
= insn_info
->read_rec
;
2159 insn_info
->read_rec
= read_info
;
2160 /* For alias_set != 0 canon_true_dependence should be never called. */
2161 if (spill_alias_set
)
2162 mem_addr
= NULL_RTX
;
2166 mem_addr
= base
->val_rtx
;
2170 = rtx_group_vec
[group_id
];
2171 mem_addr
= group
->canon_base_addr
;
2173 /* get_addr can only handle VALUE but cannot handle expr like:
2174 VALUE + OFFSET, so call get_addr to get original addr for
2175 mem_addr before plus_constant. */
2176 mem_addr
= get_addr (mem_addr
);
2178 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2181 /* We ignore the clobbers in store_info. The is mildly aggressive,
2182 but there really should not be a clobber followed by a read. */
2184 if (spill_alias_set
)
2186 insn_info_t i_ptr
= active_local_stores
;
2187 insn_info_t last
= NULL
;
2189 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2190 fprintf (dump_file
, " processing spill load %d\n",
2191 (int) spill_alias_set
);
2195 store_info_t store_info
= i_ptr
->store_rec
;
2197 /* Skip the clobbers. */
2198 while (!store_info
->is_set
)
2199 store_info
= store_info
->next
;
2201 if (store_info
->alias_set
== spill_alias_set
)
2203 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2204 dump_insn_info ("removing from active", i_ptr
);
2206 active_local_stores_len
--;
2208 last
->next_local_store
= i_ptr
->next_local_store
;
2210 active_local_stores
= i_ptr
->next_local_store
;
2214 i_ptr
= i_ptr
->next_local_store
;
2217 else if (group_id
>= 0)
2219 /* This is the restricted case where the base is a constant or
2220 the frame pointer and offset is a constant. */
2221 insn_info_t i_ptr
= active_local_stores
;
2222 insn_info_t last
= NULL
;
2224 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2227 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2230 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2231 group_id
, (int)offset
, (int)(offset
+width
));
2236 bool remove
= false;
2237 store_info_t store_info
= i_ptr
->store_rec
;
2239 /* Skip the clobbers. */
2240 while (!store_info
->is_set
)
2241 store_info
= store_info
->next
;
2243 /* There are three cases here. */
2244 if (store_info
->group_id
< 0)
2245 /* We have a cselib store followed by a read from a
2248 = canon_true_dependence (store_info
->mem
,
2249 GET_MODE (store_info
->mem
),
2250 store_info
->mem_addr
,
2253 else if (group_id
== store_info
->group_id
)
2255 /* This is a block mode load. We may get lucky and
2256 canon_true_dependence may save the day. */
2259 = canon_true_dependence (store_info
->mem
,
2260 GET_MODE (store_info
->mem
),
2261 store_info
->mem_addr
,
2264 /* If this read is just reading back something that we just
2265 stored, rewrite the read. */
2269 && offset
>= store_info
->begin
2270 && offset
+ width
<= store_info
->end
2271 && all_positions_needed_p (store_info
,
2272 offset
- store_info
->begin
,
2274 && replace_read (store_info
, i_ptr
, read_info
,
2275 insn_info
, loc
, bb_info
->regs_live
))
2278 /* The bases are the same, just see if the offsets
2280 if ((offset
< store_info
->end
)
2281 && (offset
+ width
> store_info
->begin
))
2287 The else case that is missing here is that the
2288 bases are constant but different. There is nothing
2289 to do here because there is no overlap. */
2293 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2294 dump_insn_info ("removing from active", i_ptr
);
2296 active_local_stores_len
--;
2298 last
->next_local_store
= i_ptr
->next_local_store
;
2300 active_local_stores
= i_ptr
->next_local_store
;
2304 i_ptr
= i_ptr
->next_local_store
;
2309 insn_info_t i_ptr
= active_local_stores
;
2310 insn_info_t last
= NULL
;
2311 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2313 fprintf (dump_file
, " processing cselib load mem:");
2314 print_inline_rtx (dump_file
, mem
, 0);
2315 fprintf (dump_file
, "\n");
2320 bool remove
= false;
2321 store_info_t store_info
= i_ptr
->store_rec
;
2323 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2324 fprintf (dump_file
, " processing cselib load against insn %d\n",
2325 INSN_UID (i_ptr
->insn
));
2327 /* Skip the clobbers. */
2328 while (!store_info
->is_set
)
2329 store_info
= store_info
->next
;
2331 /* If this read is just reading back something that we just
2332 stored, rewrite the read. */
2334 && store_info
->group_id
== -1
2335 && store_info
->cse_base
== base
2337 && offset
>= store_info
->begin
2338 && offset
+ width
<= store_info
->end
2339 && all_positions_needed_p (store_info
,
2340 offset
- store_info
->begin
, width
)
2341 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2342 bb_info
->regs_live
))
2345 if (!store_info
->alias_set
)
2346 remove
= canon_true_dependence (store_info
->mem
,
2347 GET_MODE (store_info
->mem
),
2348 store_info
->mem_addr
,
2353 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2354 dump_insn_info ("removing from active", i_ptr
);
2356 active_local_stores_len
--;
2358 last
->next_local_store
= i_ptr
->next_local_store
;
2360 active_local_stores
= i_ptr
->next_local_store
;
2364 i_ptr
= i_ptr
->next_local_store
;
2370 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2371 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2372 true for any part of *LOC. */
2375 check_mem_read_use (rtx
*loc
, void *data
)
2377 for_each_rtx (loc
, check_mem_read_rtx
, data
);
2381 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2382 So far it only handles arguments passed in registers. */
2385 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2387 CUMULATIVE_ARGS args_so_far_v
;
2388 cumulative_args_t args_so_far
;
2392 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2393 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2395 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2397 arg
!= void_list_node
&& idx
< nargs
;
2398 arg
= TREE_CHAIN (arg
), idx
++)
2400 enum machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2402 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2403 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2404 || GET_MODE_CLASS (mode
) != MODE_INT
)
2407 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2409 link
= XEXP (link
, 1))
2410 if (GET_CODE (XEXP (link
, 0)) == USE
)
2412 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2413 if (REG_P (args
[idx
])
2414 && REGNO (args
[idx
]) == REGNO (reg
)
2415 && (GET_MODE (args
[idx
]) == mode
2416 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2417 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2419 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2420 > GET_MODE_SIZE (mode
)))))
2426 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2427 if (GET_MODE (args
[idx
]) != mode
)
2429 if (!tmp
|| !CONST_INT_P (tmp
))
2431 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2436 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2438 if (arg
!= void_list_node
|| idx
!= nargs
)
2443 /* Return a bitmap of the fixed registers contained in IN. */
2446 copy_fixed_regs (const_bitmap in
)
2450 ret
= ALLOC_REG_SET (NULL
);
2451 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2455 /* Apply record_store to all candidate stores in INSN. Mark INSN
2456 if some part of it is not a candidate store and assigns to a
2457 non-register target. */
2460 scan_insn (bb_info_t bb_info
, rtx insn
)
2463 insn_info_t insn_info
= (insn_info_t
) pool_alloc (insn_info_pool
);
2465 memset (insn_info
, 0, sizeof (struct insn_info
));
2467 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2468 fprintf (dump_file
, "\n**scanning insn=%d\n",
2471 insn_info
->prev_insn
= bb_info
->last_insn
;
2472 insn_info
->insn
= insn
;
2473 bb_info
->last_insn
= insn_info
;
2475 if (DEBUG_INSN_P (insn
))
2477 insn_info
->cannot_delete
= true;
2481 /* Look at all of the uses in the insn. */
2482 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2487 tree memset_call
= NULL_TREE
;
2489 insn_info
->cannot_delete
= true;
2491 /* Const functions cannot do anything bad i.e. read memory,
2492 however, they can read their parameters which may have
2493 been pushed onto the stack.
2494 memset and bzero don't read memory either. */
2495 const_call
= RTL_CONST_CALL_P (insn
);
2498 rtx call
= get_call_rtx_from (insn
);
2499 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2501 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2502 if (SYMBOL_REF_DECL (symbol
)
2503 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2505 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2507 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2508 == BUILT_IN_MEMSET
))
2509 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2510 memset_call
= SYMBOL_REF_DECL (symbol
);
2514 if (const_call
|| memset_call
)
2516 insn_info_t i_ptr
= active_local_stores
;
2517 insn_info_t last
= NULL
;
2519 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2520 fprintf (dump_file
, "%s call %d\n",
2521 const_call
? "const" : "memset", INSN_UID (insn
));
2523 /* See the head comment of the frame_read field. */
2524 if (reload_completed
)
2525 insn_info
->frame_read
= true;
2527 /* Loop over the active stores and remove those which are
2528 killed by the const function call. */
2531 bool remove_store
= false;
2533 /* The stack pointer based stores are always killed. */
2534 if (i_ptr
->stack_pointer_based
)
2535 remove_store
= true;
2537 /* If the frame is read, the frame related stores are killed. */
2538 else if (insn_info
->frame_read
)
2540 store_info_t store_info
= i_ptr
->store_rec
;
2542 /* Skip the clobbers. */
2543 while (!store_info
->is_set
)
2544 store_info
= store_info
->next
;
2546 if (store_info
->group_id
>= 0
2547 && rtx_group_vec
[store_info
->group_id
]->frame_related
)
2548 remove_store
= true;
2553 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2554 dump_insn_info ("removing from active", i_ptr
);
2556 active_local_stores_len
--;
2558 last
->next_local_store
= i_ptr
->next_local_store
;
2560 active_local_stores
= i_ptr
->next_local_store
;
2565 i_ptr
= i_ptr
->next_local_store
;
2571 if (get_call_args (insn
, memset_call
, args
, 3)
2572 && CONST_INT_P (args
[1])
2573 && CONST_INT_P (args
[2])
2574 && INTVAL (args
[2]) > 0)
2576 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2577 set_mem_size (mem
, INTVAL (args
[2]));
2578 body
= gen_rtx_SET (VOIDmode
, mem
, args
[1]);
2579 mems_found
+= record_store (body
, bb_info
);
2580 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2581 fprintf (dump_file
, "handling memset as BLKmode store\n");
2582 if (mems_found
== 1)
2584 if (active_local_stores_len
++
2585 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2587 active_local_stores_len
= 1;
2588 active_local_stores
= NULL
;
2590 insn_info
->fixed_regs_live
2591 = copy_fixed_regs (bb_info
->regs_live
);
2592 insn_info
->next_local_store
= active_local_stores
;
2593 active_local_stores
= insn_info
;
2600 /* Every other call, including pure functions, may read any memory
2601 that is not relative to the frame. */
2602 add_non_frame_wild_read (bb_info
);
2607 /* Assuming that there are sets in these insns, we cannot delete
2609 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2610 || volatile_refs_p (PATTERN (insn
))
2611 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2612 || (RTX_FRAME_RELATED_P (insn
))
2613 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2614 insn_info
->cannot_delete
= true;
2616 body
= PATTERN (insn
);
2617 if (GET_CODE (body
) == PARALLEL
)
2620 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2621 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2624 mems_found
+= record_store (body
, bb_info
);
2626 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2627 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2628 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2630 /* If we found some sets of mems, add it into the active_local_stores so
2631 that it can be locally deleted if found dead or used for
2632 replace_read and redundant constant store elimination. Otherwise mark
2633 it as cannot delete. This simplifies the processing later. */
2634 if (mems_found
== 1)
2636 if (active_local_stores_len
++
2637 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2639 active_local_stores_len
= 1;
2640 active_local_stores
= NULL
;
2642 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2643 insn_info
->next_local_store
= active_local_stores
;
2644 active_local_stores
= insn_info
;
2647 insn_info
->cannot_delete
= true;
2651 /* Remove BASE from the set of active_local_stores. This is a
2652 callback from cselib that is used to get rid of the stores in
2653 active_local_stores. */
2656 remove_useless_values (cselib_val
*base
)
2658 insn_info_t insn_info
= active_local_stores
;
2659 insn_info_t last
= NULL
;
2663 store_info_t store_info
= insn_info
->store_rec
;
2666 /* If ANY of the store_infos match the cselib group that is
2667 being deleted, then the insn can not be deleted. */
2670 if ((store_info
->group_id
== -1)
2671 && (store_info
->cse_base
== base
))
2676 store_info
= store_info
->next
;
2681 active_local_stores_len
--;
2683 last
->next_local_store
= insn_info
->next_local_store
;
2685 active_local_stores
= insn_info
->next_local_store
;
2686 free_store_info (insn_info
);
2691 insn_info
= insn_info
->next_local_store
;
2696 /* Do all of step 1. */
2702 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2705 all_blocks
= BITMAP_ALLOC (NULL
);
2706 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2707 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2709 FOR_ALL_BB_FN (bb
, cfun
)
2712 bb_info_t bb_info
= (bb_info_t
) pool_alloc (bb_info_pool
);
2714 memset (bb_info
, 0, sizeof (struct bb_info
));
2715 bitmap_set_bit (all_blocks
, bb
->index
);
2716 bb_info
->regs_live
= regs_live
;
2718 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2719 df_simulate_initialize_forwards (bb
, regs_live
);
2721 bb_table
[bb
->index
] = bb_info
;
2722 cselib_discard_hook
= remove_useless_values
;
2724 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2729 = create_alloc_pool ("cse_store_info_pool",
2730 sizeof (struct store_info
), 100);
2731 active_local_stores
= NULL
;
2732 active_local_stores_len
= 0;
2733 cselib_clear_table ();
2735 /* Scan the insns. */
2736 FOR_BB_INSNS (bb
, insn
)
2739 scan_insn (bb_info
, insn
);
2740 cselib_process_insn (insn
);
2742 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2745 /* This is something of a hack, because the global algorithm
2746 is supposed to take care of the case where stores go dead
2747 at the end of the function. However, the global
2748 algorithm must take a more conservative view of block
2749 mode reads than the local alg does. So to get the case
2750 where you have a store to the frame followed by a non
2751 overlapping block more read, we look at the active local
2752 stores at the end of the function and delete all of the
2753 frame and spill based ones. */
2754 if (stores_off_frame_dead_at_return
2755 && (EDGE_COUNT (bb
->succs
) == 0
2756 || (single_succ_p (bb
)
2757 && single_succ (bb
) == EXIT_BLOCK_PTR_FOR_FN (cfun
)
2758 && ! crtl
->calls_eh_return
)))
2760 insn_info_t i_ptr
= active_local_stores
;
2763 store_info_t store_info
= i_ptr
->store_rec
;
2765 /* Skip the clobbers. */
2766 while (!store_info
->is_set
)
2767 store_info
= store_info
->next
;
2768 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2769 delete_dead_store_insn (i_ptr
);
2771 if (store_info
->group_id
>= 0)
2774 = rtx_group_vec
[store_info
->group_id
];
2775 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2776 delete_dead_store_insn (i_ptr
);
2779 i_ptr
= i_ptr
->next_local_store
;
2783 /* Get rid of the loads that were discovered in
2784 replace_read. Cselib is finished with this block. */
2785 while (deferred_change_list
)
2787 deferred_change_t next
= deferred_change_list
->next
;
2789 /* There is no reason to validate this change. That was
2791 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2792 pool_free (deferred_change_pool
, deferred_change_list
);
2793 deferred_change_list
= next
;
2796 /* Get rid of all of the cselib based store_infos in this
2797 block and mark the containing insns as not being
2799 ptr
= bb_info
->last_insn
;
2802 if (ptr
->contains_cselib_groups
)
2804 store_info_t s_info
= ptr
->store_rec
;
2805 while (s_info
&& !s_info
->is_set
)
2806 s_info
= s_info
->next
;
2808 && s_info
->redundant_reason
2809 && s_info
->redundant_reason
->insn
2810 && !ptr
->cannot_delete
)
2812 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2813 fprintf (dump_file
, "Locally deleting insn %d "
2814 "because insn %d stores the "
2815 "same value and couldn't be "
2817 INSN_UID (ptr
->insn
),
2818 INSN_UID (s_info
->redundant_reason
->insn
));
2819 delete_dead_store_insn (ptr
);
2821 free_store_info (ptr
);
2825 store_info_t s_info
;
2827 /* Free at least positions_needed bitmaps. */
2828 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2829 if (s_info
->is_large
)
2831 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2832 s_info
->is_large
= false;
2835 ptr
= ptr
->prev_insn
;
2838 free_alloc_pool (cse_store_info_pool
);
2840 bb_info
->regs_live
= NULL
;
2843 BITMAP_FREE (regs_live
);
2845 rtx_group_table
.empty ();
2849 /*----------------------------------------------------------------------------
2852 Assign each byte position in the stores that we are going to
2853 analyze globally to a position in the bitmaps. Returns true if
2854 there are any bit positions assigned.
2855 ----------------------------------------------------------------------------*/
2858 dse_step2_init (void)
2863 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2865 /* For all non stack related bases, we only consider a store to
2866 be deletable if there are two or more stores for that
2867 position. This is because it takes one store to make the
2868 other store redundant. However, for the stores that are
2869 stack related, we consider them if there is only one store
2870 for the position. We do this because the stack related
2871 stores can be deleted if their is no read between them and
2872 the end of the function.
2874 To make this work in the current framework, we take the stack
2875 related bases add all of the bits from store1 into store2.
2876 This has the effect of making the eligible even if there is
2879 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2881 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2882 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2883 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2884 fprintf (dump_file
, "group %d is frame related ", i
);
2887 group
->offset_map_size_n
++;
2888 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2889 group
->offset_map_size_n
);
2890 group
->offset_map_size_p
++;
2891 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2892 group
->offset_map_size_p
);
2893 group
->process_globally
= false;
2894 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2896 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2897 (int)bitmap_count_bits (group
->store2_n
),
2898 (int)bitmap_count_bits (group
->store2_p
));
2899 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2900 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2906 /* Init the offset tables for the normal case. */
2909 dse_step2_nospill (void)
2913 /* Position 0 is unused because 0 is used in the maps to mean
2915 current_position
= 1;
2916 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2921 if (group
== clear_alias_group
)
2924 memset (group
->offset_map_n
, 0, sizeof (int) * group
->offset_map_size_n
);
2925 memset (group
->offset_map_p
, 0, sizeof (int) * group
->offset_map_size_p
);
2926 bitmap_clear (group
->group_kill
);
2928 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2930 bitmap_set_bit (group
->group_kill
, current_position
);
2931 if (bitmap_bit_p (group
->escaped_n
, j
))
2932 bitmap_set_bit (kill_on_calls
, current_position
);
2933 group
->offset_map_n
[j
] = current_position
++;
2934 group
->process_globally
= true;
2936 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2938 bitmap_set_bit (group
->group_kill
, current_position
);
2939 if (bitmap_bit_p (group
->escaped_p
, j
))
2940 bitmap_set_bit (kill_on_calls
, current_position
);
2941 group
->offset_map_p
[j
] = current_position
++;
2942 group
->process_globally
= true;
2945 return current_position
!= 1;
2950 /*----------------------------------------------------------------------------
2953 Build the bit vectors for the transfer functions.
2954 ----------------------------------------------------------------------------*/
2957 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2961 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
2965 HOST_WIDE_INT offset_p
= -offset
;
2966 if (offset_p
>= group_info
->offset_map_size_n
)
2968 return group_info
->offset_map_n
[offset_p
];
2972 if (offset
>= group_info
->offset_map_size_p
)
2974 return group_info
->offset_map_p
[offset
];
2979 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2983 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
2988 group_info_t group_info
2989 = rtx_group_vec
[store_info
->group_id
];
2990 if (group_info
->process_globally
)
2991 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
2993 int index
= get_bitmap_index (group_info
, i
);
2996 bitmap_set_bit (gen
, index
);
2998 bitmap_clear_bit (kill
, index
);
3001 store_info
= store_info
->next
;
3006 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3010 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3014 if (store_info
->alias_set
)
3016 int index
= get_bitmap_index (clear_alias_group
,
3017 store_info
->alias_set
);
3020 bitmap_set_bit (gen
, index
);
3022 bitmap_clear_bit (kill
, index
);
3025 store_info
= store_info
->next
;
3030 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3034 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3036 read_info_t read_info
= insn_info
->read_rec
;
3040 /* If this insn reads the frame, kill all the frame related stores. */
3041 if (insn_info
->frame_read
)
3043 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3044 if (group
->process_globally
&& group
->frame_related
)
3047 bitmap_ior_into (kill
, group
->group_kill
);
3048 bitmap_and_compl_into (gen
, group
->group_kill
);
3051 if (insn_info
->non_frame_wild_read
)
3053 /* Kill all non-frame related stores. Kill all stores of variables that
3056 bitmap_ior_into (kill
, kill_on_calls
);
3057 bitmap_and_compl_into (gen
, kill_on_calls
);
3058 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3059 if (group
->process_globally
&& !group
->frame_related
)
3062 bitmap_ior_into (kill
, group
->group_kill
);
3063 bitmap_and_compl_into (gen
, group
->group_kill
);
3068 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3070 if (group
->process_globally
)
3072 if (i
== read_info
->group_id
)
3074 if (read_info
->begin
> read_info
->end
)
3076 /* Begin > end for block mode reads. */
3078 bitmap_ior_into (kill
, group
->group_kill
);
3079 bitmap_and_compl_into (gen
, group
->group_kill
);
3083 /* The groups are the same, just process the
3086 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3088 int index
= get_bitmap_index (group
, j
);
3092 bitmap_set_bit (kill
, index
);
3093 bitmap_clear_bit (gen
, index
);
3100 /* The groups are different, if the alias sets
3101 conflict, clear the entire group. We only need
3102 to apply this test if the read_info is a cselib
3103 read. Anything with a constant base cannot alias
3104 something else with a different constant
3106 if ((read_info
->group_id
< 0)
3107 && canon_true_dependence (group
->base_mem
,
3108 GET_MODE (group
->base_mem
),
3109 group
->canon_base_addr
,
3110 read_info
->mem
, NULL_RTX
))
3113 bitmap_ior_into (kill
, group
->group_kill
);
3114 bitmap_and_compl_into (gen
, group
->group_kill
);
3120 read_info
= read_info
->next
;
3124 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3128 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3132 if (read_info
->alias_set
)
3134 int index
= get_bitmap_index (clear_alias_group
,
3135 read_info
->alias_set
);
3139 bitmap_set_bit (kill
, index
);
3140 bitmap_clear_bit (gen
, index
);
3144 read_info
= read_info
->next
;
3149 /* Return the insn in BB_INFO before the first wild read or if there
3150 are no wild reads in the block, return the last insn. */
3153 find_insn_before_first_wild_read (bb_info_t bb_info
)
3155 insn_info_t insn_info
= bb_info
->last_insn
;
3156 insn_info_t last_wild_read
= NULL
;
3160 if (insn_info
->wild_read
)
3162 last_wild_read
= insn_info
->prev_insn
;
3163 /* Block starts with wild read. */
3164 if (!last_wild_read
)
3168 insn_info
= insn_info
->prev_insn
;
3172 return last_wild_read
;
3174 return bb_info
->last_insn
;
3178 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3179 the block in order to build the gen and kill sets for the block.
3180 We start at ptr which may be the last insn in the block or may be
3181 the first insn with a wild read. In the latter case we are able to
3182 skip the rest of the block because it just does not matter:
3183 anything that happens is hidden by the wild read. */
3186 dse_step3_scan (bool for_spills
, basic_block bb
)
3188 bb_info_t bb_info
= bb_table
[bb
->index
];
3189 insn_info_t insn_info
;
3192 /* There are no wild reads in the spill case. */
3193 insn_info
= bb_info
->last_insn
;
3195 insn_info
= find_insn_before_first_wild_read (bb_info
);
3197 /* In the spill case or in the no_spill case if there is no wild
3198 read in the block, we will need a kill set. */
3199 if (insn_info
== bb_info
->last_insn
)
3202 bitmap_clear (bb_info
->kill
);
3204 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3208 BITMAP_FREE (bb_info
->kill
);
3212 /* There may have been code deleted by the dce pass run before
3214 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3216 /* Process the read(s) last. */
3219 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3220 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3224 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3225 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3229 insn_info
= insn_info
->prev_insn
;
3234 /* Set the gen set of the exit block, and also any block with no
3235 successors that does not have a wild read. */
3238 dse_step3_exit_block_scan (bb_info_t bb_info
)
3240 /* The gen set is all 0's for the exit block except for the
3241 frame_pointer_group. */
3243 if (stores_off_frame_dead_at_return
)
3248 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3250 if (group
->process_globally
&& group
->frame_related
)
3251 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3257 /* Find all of the blocks that are not backwards reachable from the
3258 exit block or any block with no successors (BB). These are the
3259 infinite loops or infinite self loops. These blocks will still
3260 have their bits set in UNREACHABLE_BLOCKS. */
3263 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3268 if (bitmap_bit_p (unreachable_blocks
, bb
->index
))
3270 bitmap_clear_bit (unreachable_blocks
, bb
->index
);
3271 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3273 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3278 /* Build the transfer functions for the function. */
3281 dse_step3 (bool for_spills
)
3284 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
3285 sbitmap_iterator sbi
;
3286 bitmap all_ones
= NULL
;
3289 bitmap_ones (unreachable_blocks
);
3291 FOR_ALL_BB_FN (bb
, cfun
)
3293 bb_info_t bb_info
= bb_table
[bb
->index
];
3295 bitmap_clear (bb_info
->gen
);
3297 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3299 if (bb
->index
== ENTRY_BLOCK
)
3301 else if (bb
->index
== EXIT_BLOCK
)
3302 dse_step3_exit_block_scan (bb_info
);
3304 dse_step3_scan (for_spills
, bb
);
3305 if (EDGE_COUNT (bb
->succs
) == 0)
3306 mark_reachable_blocks (unreachable_blocks
, bb
);
3308 /* If this is the second time dataflow is run, delete the old
3311 BITMAP_FREE (bb_info
->in
);
3313 BITMAP_FREE (bb_info
->out
);
3316 /* For any block in an infinite loop, we must initialize the out set
3317 to all ones. This could be expensive, but almost never occurs in
3318 practice. However, it is common in regression tests. */
3319 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks
, 0, i
, sbi
)
3321 if (bitmap_bit_p (all_blocks
, i
))
3323 bb_info_t bb_info
= bb_table
[i
];
3329 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3330 FOR_EACH_VEC_ELT (rtx_group_vec
, j
, group
)
3331 bitmap_ior_into (all_ones
, group
->group_kill
);
3335 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3336 bitmap_copy (bb_info
->out
, all_ones
);
3342 BITMAP_FREE (all_ones
);
3343 sbitmap_free (unreachable_blocks
);
3348 /*----------------------------------------------------------------------------
3351 Solve the bitvector equations.
3352 ----------------------------------------------------------------------------*/
3355 /* Confluence function for blocks with no successors. Create an out
3356 set from the gen set of the exit block. This block logically has
3357 the exit block as a successor. */
3362 dse_confluence_0 (basic_block bb
)
3364 bb_info_t bb_info
= bb_table
[bb
->index
];
3366 if (bb
->index
== EXIT_BLOCK
)
3371 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3372 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3376 /* Propagate the information from the in set of the dest of E to the
3377 out set of the src of E. If the various in or out sets are not
3378 there, that means they are all ones. */
3381 dse_confluence_n (edge e
)
3383 bb_info_t src_info
= bb_table
[e
->src
->index
];
3384 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3389 bitmap_and_into (src_info
->out
, dest_info
->in
);
3392 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3393 bitmap_copy (src_info
->out
, dest_info
->in
);
3400 /* Propagate the info from the out to the in set of BB_INDEX's basic
3401 block. There are three cases:
3403 1) The block has no kill set. In this case the kill set is all
3404 ones. It does not matter what the out set of the block is, none of
3405 the info can reach the top. The only thing that reaches the top is
3406 the gen set and we just copy the set.
3408 2) There is a kill set but no out set and bb has successors. In
3409 this case we just return. Eventually an out set will be created and
3410 it is better to wait than to create a set of ones.
3412 3) There is both a kill and out set. We apply the obvious transfer
3417 dse_transfer_function (int bb_index
)
3419 bb_info_t bb_info
= bb_table
[bb_index
];
3427 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3428 bb_info
->out
, bb_info
->kill
);
3431 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3432 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3433 bb_info
->out
, bb_info
->kill
);
3443 /* Case 1 above. If there is already an in set, nothing
3449 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3450 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3456 /* Solve the dataflow equations. */
3461 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3462 dse_confluence_n
, dse_transfer_function
,
3463 all_blocks
, df_get_postorder (DF_BACKWARD
),
3464 df_get_n_blocks (DF_BACKWARD
));
3465 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3469 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3470 FOR_ALL_BB_FN (bb
, cfun
)
3472 bb_info_t bb_info
= bb_table
[bb
->index
];
3474 df_print_bb_index (bb
, dump_file
);
3476 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3478 fprintf (dump_file
, " in: *MISSING*\n");
3480 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3482 fprintf (dump_file
, " gen: *MISSING*\n");
3484 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3486 fprintf (dump_file
, " kill: *MISSING*\n");
3488 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3490 fprintf (dump_file
, " out: *MISSING*\n\n");
3497 /*----------------------------------------------------------------------------
3500 Delete the stores that can only be deleted using the global information.
3501 ----------------------------------------------------------------------------*/
3505 dse_step5_nospill (void)
3508 FOR_EACH_BB_FN (bb
, cfun
)
3510 bb_info_t bb_info
= bb_table
[bb
->index
];
3511 insn_info_t insn_info
= bb_info
->last_insn
;
3512 bitmap v
= bb_info
->out
;
3516 bool deleted
= false;
3517 if (dump_file
&& insn_info
->insn
)
3519 fprintf (dump_file
, "starting to process insn %d\n",
3520 INSN_UID (insn_info
->insn
));
3521 bitmap_print (dump_file
, v
, " v: ", "\n");
3524 /* There may have been code deleted by the dce pass run before
3527 && INSN_P (insn_info
->insn
)
3528 && (!insn_info
->cannot_delete
)
3529 && (!bitmap_empty_p (v
)))
3531 store_info_t store_info
= insn_info
->store_rec
;
3533 /* Try to delete the current insn. */
3536 /* Skip the clobbers. */
3537 while (!store_info
->is_set
)
3538 store_info
= store_info
->next
;
3540 if (store_info
->alias_set
)
3545 group_info_t group_info
3546 = rtx_group_vec
[store_info
->group_id
];
3548 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3550 int index
= get_bitmap_index (group_info
, i
);
3552 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3553 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3554 if (index
== 0 || !bitmap_bit_p (v
, index
))
3556 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3557 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3566 && check_for_inc_dec_1 (insn_info
))
3568 delete_insn (insn_info
->insn
);
3569 insn_info
->insn
= NULL
;
3574 /* We do want to process the local info if the insn was
3575 deleted. For instance, if the insn did a wild read, we
3576 no longer need to trash the info. */
3578 && INSN_P (insn_info
->insn
)
3581 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3582 if (insn_info
->wild_read
)
3584 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3585 fprintf (dump_file
, "wild read\n");
3588 else if (insn_info
->read_rec
3589 || insn_info
->non_frame_wild_read
)
3591 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3592 fprintf (dump_file
, "regular read\n");
3593 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3594 fprintf (dump_file
, "non-frame wild read\n");
3595 scan_reads_nospill (insn_info
, v
, NULL
);
3599 insn_info
= insn_info
->prev_insn
;
3606 /*----------------------------------------------------------------------------
3609 Delete stores made redundant by earlier stores (which store the same
3610 value) that couldn't be eliminated.
3611 ----------------------------------------------------------------------------*/
3618 FOR_ALL_BB_FN (bb
, cfun
)
3620 bb_info_t bb_info
= bb_table
[bb
->index
];
3621 insn_info_t insn_info
= bb_info
->last_insn
;
3625 /* There may have been code deleted by the dce pass run before
3628 && INSN_P (insn_info
->insn
)
3629 && !insn_info
->cannot_delete
)
3631 store_info_t s_info
= insn_info
->store_rec
;
3633 while (s_info
&& !s_info
->is_set
)
3634 s_info
= s_info
->next
;
3636 && s_info
->redundant_reason
3637 && s_info
->redundant_reason
->insn
3638 && INSN_P (s_info
->redundant_reason
->insn
))
3640 rtx rinsn
= s_info
->redundant_reason
->insn
;
3641 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3642 fprintf (dump_file
, "Locally deleting insn %d "
3643 "because insn %d stores the "
3644 "same value and couldn't be "
3646 INSN_UID (insn_info
->insn
),
3648 delete_dead_store_insn (insn_info
);
3651 insn_info
= insn_info
->prev_insn
;
3656 /*----------------------------------------------------------------------------
3659 Destroy everything left standing.
3660 ----------------------------------------------------------------------------*/
3665 bitmap_obstack_release (&dse_bitmap_obstack
);
3666 obstack_free (&dse_obstack
, NULL
);
3668 end_alias_analysis ();
3670 rtx_group_table
.dispose ();
3671 rtx_group_vec
.release ();
3672 BITMAP_FREE (all_blocks
);
3673 BITMAP_FREE (scratch
);
3675 free_alloc_pool (rtx_store_info_pool
);
3676 free_alloc_pool (read_info_pool
);
3677 free_alloc_pool (insn_info_pool
);
3678 free_alloc_pool (bb_info_pool
);
3679 free_alloc_pool (rtx_group_info_pool
);
3680 free_alloc_pool (deferred_change_pool
);
3684 /* -------------------------------------------------------------------------
3686 ------------------------------------------------------------------------- */
3688 /* Callback for running pass_rtl_dse. */
3691 rest_of_handle_dse (void)
3693 df_set_flags (DF_DEFER_INSN_RESCAN
);
3695 /* Need the notes since we must track live hardregs in the forwards
3697 df_note_add_problem ();
3703 if (dse_step2_nospill ())
3705 df_set_flags (DF_LR_RUN_DCE
);
3707 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3708 fprintf (dump_file
, "doing global processing\n");
3711 dse_step5_nospill ();
3718 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3719 locally_deleted
, globally_deleted
, spill_deleted
);
3726 return optimize
> 0 && flag_dse
3733 return optimize
> 0 && flag_dse
3739 const pass_data pass_data_rtl_dse1
=
3741 RTL_PASS
, /* type */
3743 OPTGROUP_NONE
, /* optinfo_flags */
3744 true, /* has_gate */
3745 true, /* has_execute */
3746 TV_DSE1
, /* tv_id */
3747 0, /* properties_required */
3748 0, /* properties_provided */
3749 0, /* properties_destroyed */
3750 0, /* todo_flags_start */
3751 ( TODO_df_finish
| TODO_verify_rtl_sharing
), /* todo_flags_finish */
3754 class pass_rtl_dse1
: public rtl_opt_pass
3757 pass_rtl_dse1 (gcc::context
*ctxt
)
3758 : rtl_opt_pass (pass_data_rtl_dse1
, ctxt
)
3761 /* opt_pass methods: */
3762 bool gate () { return gate_dse1 (); }
3763 unsigned int execute () { return rest_of_handle_dse (); }
3765 }; // class pass_rtl_dse1
3770 make_pass_rtl_dse1 (gcc::context
*ctxt
)
3772 return new pass_rtl_dse1 (ctxt
);
3777 const pass_data pass_data_rtl_dse2
=
3779 RTL_PASS
, /* type */
3781 OPTGROUP_NONE
, /* optinfo_flags */
3782 true, /* has_gate */
3783 true, /* has_execute */
3784 TV_DSE2
, /* tv_id */
3785 0, /* properties_required */
3786 0, /* properties_provided */
3787 0, /* properties_destroyed */
3788 0, /* todo_flags_start */
3789 ( TODO_df_finish
| TODO_verify_rtl_sharing
), /* todo_flags_finish */
3792 class pass_rtl_dse2
: public rtl_opt_pass
3795 pass_rtl_dse2 (gcc::context
*ctxt
)
3796 : rtl_opt_pass (pass_data_rtl_dse2
, ctxt
)
3799 /* opt_pass methods: */
3800 bool gate () { return gate_dse2 (); }
3801 unsigned int execute () { return rest_of_handle_dse (); }
3803 }; // class pass_rtl_dse2
3808 make_pass_rtl_dse2 (gcc::context
*ctxt
)
3810 return new pass_rtl_dse2 (ctxt
);