1 /* RTL dead store elimination.
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
4 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
5 and Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
27 #include "coretypes.h"
28 #include "hash-table.h"
34 #include "hard-reg-set.h"
39 #include "tree-pass.h"
40 #include "alloc-pool.h"
42 #include "insn-config.h"
50 #include "gimple-ssa.h"
52 /* This file contains three techniques for performing Dead Store
55 * The first technique performs dse locally on any base address. It
56 is based on the cselib which is a local value numbering technique.
57 This technique is local to a basic block but deals with a fairly
60 * The second technique performs dse globally but is restricted to
61 base addresses that are either constant or are relative to the
64 * The third technique, (which is only done after register allocation)
65 processes the spill spill slots. This differs from the second
66 technique because it takes advantage of the fact that spilling is
67 completely free from the effects of aliasing.
69 Logically, dse is a backwards dataflow problem. A store can be
70 deleted if it if cannot be reached in the backward direction by any
71 use of the value being stored. However, the local technique uses a
72 forwards scan of the basic block because cselib requires that the
73 block be processed in that order.
75 The pass is logically broken into 7 steps:
79 1) The local algorithm, as well as scanning the insns for the two
82 2) Analysis to see if the global algs are necessary. In the case
83 of stores base on a constant address, there must be at least two
84 stores to that address, to make it possible to delete some of the
85 stores. In the case of stores off of the frame or spill related
86 stores, only one store to an address is necessary because those
87 stores die at the end of the function.
89 3) Set up the global dataflow equations based on processing the
90 info parsed in the first step.
92 4) Solve the dataflow equations.
94 5) Delete the insns that the global analysis has indicated are
97 6) Delete insns that store the same value as preceding store
98 where the earlier store couldn't be eliminated.
102 This step uses cselib and canon_rtx to build the largest expression
103 possible for each address. This pass is a forwards pass through
104 each basic block. From the point of view of the global technique,
105 the first pass could examine a block in either direction. The
106 forwards ordering is to accommodate cselib.
108 We make a simplifying assumption: addresses fall into four broad
111 1) base has rtx_varies_p == false, offset is constant.
112 2) base has rtx_varies_p == false, offset variable.
113 3) base has rtx_varies_p == true, offset constant.
114 4) base has rtx_varies_p == true, offset variable.
116 The local passes are able to process all 4 kinds of addresses. The
117 global pass only handles 1).
119 The global problem is formulated as follows:
121 A store, S1, to address A, where A is not relative to the stack
122 frame, can be eliminated if all paths from S1 to the end of the
123 function contain another store to A before a read to A.
125 If the address A is relative to the stack frame, a store S2 to A
126 can be eliminated if there are no paths from S2 that reach the
127 end of the function that read A before another store to A. In
128 this case S2 can be deleted if there are paths from S2 to the
129 end of the function that have no reads or writes to A. This
130 second case allows stores to the stack frame to be deleted that
131 would otherwise die when the function returns. This cannot be
132 done if stores_off_frame_dead_at_return is not true. See the doc
133 for that variable for when this variable is false.
135 The global problem is formulated as a backwards set union
136 dataflow problem where the stores are the gens and reads are the
137 kills. Set union problems are rare and require some special
138 handling given our representation of bitmaps. A straightforward
139 implementation requires a lot of bitmaps filled with 1s.
140 These are expensive and cumbersome in our bitmap formulation so
141 care has been taken to avoid large vectors filled with 1s. See
142 the comments in bb_info and in the dataflow confluence functions
145 There are two places for further enhancements to this algorithm:
147 1) The original dse which was embedded in a pass called flow also
148 did local address forwarding. For example in
153 flow would replace the right hand side of the second insn with a
154 reference to r100. Most of the information is available to add this
155 to this pass. It has not done it because it is a lot of work in
156 the case that either r100 is assigned to between the first and
157 second insn and/or the second insn is a load of part of the value
158 stored by the first insn.
160 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
161 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
162 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
163 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
165 2) The cleaning up of spill code is quite profitable. It currently
166 depends on reading tea leaves and chicken entrails left by reload.
167 This pass depends on reload creating a singleton alias set for each
168 spill slot and telling the next dse pass which of these alias sets
169 are the singletons. Rather than analyze the addresses of the
170 spills, dse's spill processing just does analysis of the loads and
171 stores that use those alias sets. There are three cases where this
174 a) Reload sometimes creates the slot for one mode of access, and
175 then inserts loads and/or stores for a smaller mode. In this
176 case, the current code just punts on the slot. The proper thing
177 to do is to back out and use one bit vector position for each
178 byte of the entity associated with the slot. This depends on
179 KNOWING that reload always generates the accesses for each of the
180 bytes in some canonical (read that easy to understand several
181 passes after reload happens) way.
183 b) Reload sometimes decides that spill slot it allocated was not
184 large enough for the mode and goes back and allocates more slots
185 with the same mode and alias set. The backout in this case is a
186 little more graceful than (a). In this case the slot is unmarked
187 as being a spill slot and if final address comes out to be based
188 off the frame pointer, the global algorithm handles this slot.
190 c) For any pass that may prespill, there is currently no
191 mechanism to tell the dse pass that the slot being used has the
192 special properties that reload uses. It may be that all that is
193 required is to have those passes make the same calls that reload
194 does, assuming that the alias sets can be manipulated in the same
197 /* There are limits to the size of constant offsets we model for the
198 global problem. There are certainly test cases, that exceed this
199 limit, however, it is unlikely that there are important programs
200 that really have constant offsets this size. */
201 #define MAX_OFFSET (64 * 1024)
203 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
204 on the default obstack because these bitmaps can grow quite large
205 (~2GB for the small (!) test case of PR54146) and we'll hold on to
206 all that memory until the end of the compiler run.
207 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
208 releasing the whole obstack. */
209 static bitmap_obstack dse_bitmap_obstack
;
211 /* Obstack for other data. As for above: Kinda nice to be able to
212 throw it all away at the end in one big sweep. */
213 static struct obstack dse_obstack
;
215 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
216 static bitmap scratch
= NULL
;
220 /* This structure holds information about a candidate store. */
224 /* False means this is a clobber. */
227 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
230 /* The id of the mem group of the base address. If rtx_varies_p is
231 true, this is -1. Otherwise, it is the index into the group
235 /* This is the cselib value. */
236 cselib_val
*cse_base
;
238 /* This canonized mem. */
241 /* Canonized MEM address for use by canon_true_dependence. */
244 /* If this is non-zero, it is the alias set of a spill location. */
245 alias_set_type alias_set
;
247 /* The offset of the first and byte before the last byte associated
248 with the operation. */
249 HOST_WIDE_INT begin
, end
;
253 /* A bitmask as wide as the number of bytes in the word that
254 contains a 1 if the byte may be needed. The store is unused if
255 all of the bits are 0. This is used if IS_LARGE is false. */
256 unsigned HOST_WIDE_INT small_bitmask
;
260 /* A bitmap with one bit per byte. Cleared bit means the position
261 is needed. Used if IS_LARGE is false. */
264 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
265 equal to END - BEGIN, the whole store is unused. */
270 /* The next store info for this insn. */
271 struct store_info
*next
;
273 /* The right hand side of the store. This is used if there is a
274 subsequent reload of the mems address somewhere later in the
278 /* If rhs is or holds a constant, this contains that constant,
282 /* Set if this store stores the same constant value as REDUNDANT_REASON
283 insn stored. These aren't eliminated early, because doing that
284 might prevent the earlier larger store to be eliminated. */
285 struct insn_info
*redundant_reason
;
288 /* Return a bitmask with the first N low bits set. */
290 static unsigned HOST_WIDE_INT
291 lowpart_bitmask (int n
)
293 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
294 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
297 typedef struct store_info
*store_info_t
;
298 static alloc_pool cse_store_info_pool
;
299 static alloc_pool rtx_store_info_pool
;
301 /* This structure holds information about a load. These are only
302 built for rtx bases. */
305 /* The id of the mem group of the base address. */
308 /* If this is non-zero, it is the alias set of a spill location. */
309 alias_set_type alias_set
;
311 /* The offset of the first and byte after the last byte associated
312 with the operation. If begin == end == 0, the read did not have
313 a constant offset. */
316 /* The mem being read. */
319 /* The next read_info for this insn. */
320 struct read_info
*next
;
322 typedef struct read_info
*read_info_t
;
323 static alloc_pool read_info_pool
;
326 /* One of these records is created for each insn. */
330 /* Set true if the insn contains a store but the insn itself cannot
331 be deleted. This is set if the insn is a parallel and there is
332 more than one non dead output or if the insn is in some way
336 /* This field is only used by the global algorithm. It is set true
337 if the insn contains any read of mem except for a (1). This is
338 also set if the insn is a call or has a clobber mem. If the insn
339 contains a wild read, the use_rec will be null. */
342 /* This is true only for CALL instructions which could potentially read
343 any non-frame memory location. This field is used by the global
345 bool non_frame_wild_read
;
347 /* This field is only used for the processing of const functions.
348 These functions cannot read memory, but they can read the stack
349 because that is where they may get their parms. We need to be
350 this conservative because, like the store motion pass, we don't
351 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
352 Moreover, we need to distinguish two cases:
353 1. Before reload (register elimination), the stores related to
354 outgoing arguments are stack pointer based and thus deemed
355 of non-constant base in this pass. This requires special
356 handling but also means that the frame pointer based stores
357 need not be killed upon encountering a const function call.
358 2. After reload, the stores related to outgoing arguments can be
359 either stack pointer or hard frame pointer based. This means
360 that we have no other choice than also killing all the frame
361 pointer based stores upon encountering a const function call.
362 This field is set after reload for const function calls. Having
363 this set is less severe than a wild read, it just means that all
364 the frame related stores are killed rather than all the stores. */
367 /* This field is only used for the processing of const functions.
368 It is set if the insn may contain a stack pointer based store. */
369 bool stack_pointer_based
;
371 /* This is true if any of the sets within the store contains a
372 cselib base. Such stores can only be deleted by the local
374 bool contains_cselib_groups
;
379 /* The list of mem sets or mem clobbers that are contained in this
380 insn. If the insn is deletable, it contains only one mem set.
381 But it could also contain clobbers. Insns that contain more than
382 one mem set are not deletable, but each of those mems are here in
383 order to provide info to delete other insns. */
384 store_info_t store_rec
;
386 /* The linked list of mem uses in this insn. Only the reads from
387 rtx bases are listed here. The reads to cselib bases are
388 completely processed during the first scan and so are never
390 read_info_t read_rec
;
392 /* The live fixed registers. We assume only fixed registers can
393 cause trouble by being clobbered from an expanded pattern;
394 storing only the live fixed registers (rather than all registers)
395 means less memory needs to be allocated / copied for the individual
397 regset fixed_regs_live
;
399 /* The prev insn in the basic block. */
400 struct insn_info
* prev_insn
;
402 /* The linked list of insns that are in consideration for removal in
403 the forwards pass through the basic block. This pointer may be
404 trash as it is not cleared when a wild read occurs. The only
405 time it is guaranteed to be correct is when the traversal starts
406 at active_local_stores. */
407 struct insn_info
* next_local_store
;
410 typedef struct insn_info
*insn_info_t
;
411 static alloc_pool insn_info_pool
;
413 /* The linked list of stores that are under consideration in this
415 static insn_info_t active_local_stores
;
416 static int active_local_stores_len
;
421 /* Pointer to the insn info for the last insn in the block. These
422 are linked so this is how all of the insns are reached. During
423 scanning this is the current insn being scanned. */
424 insn_info_t last_insn
;
426 /* The info for the global dataflow problem. */
429 /* This is set if the transfer function should and in the wild_read
430 bitmap before applying the kill and gen sets. That vector knocks
431 out most of the bits in the bitmap and thus speeds up the
433 bool apply_wild_read
;
435 /* The following 4 bitvectors hold information about which positions
436 of which stores are live or dead. They are indexed by
439 /* The set of store positions that exist in this block before a wild read. */
442 /* The set of load positions that exist in this block above the
443 same position of a store. */
446 /* The set of stores that reach the top of the block without being
449 Do not represent the in if it is all ones. Note that this is
450 what the bitvector should logically be initialized to for a set
451 intersection problem. However, like the kill set, this is too
452 expensive. So initially, the in set will only be created for the
453 exit block and any block that contains a wild read. */
456 /* The set of stores that reach the bottom of the block from it's
459 Do not represent the in if it is all ones. Note that this is
460 what the bitvector should logically be initialized to for a set
461 intersection problem. However, like the kill and in set, this is
462 too expensive. So what is done is that the confluence operator
463 just initializes the vector from one of the out sets of the
464 successors of the block. */
467 /* The following bitvector is indexed by the reg number. It
468 contains the set of regs that are live at the current instruction
469 being processed. While it contains info for all of the
470 registers, only the hard registers are actually examined. It is used
471 to assure that shift and/or add sequences that are inserted do not
472 accidentally clobber live hard regs. */
476 typedef struct bb_info
*bb_info_t
;
477 static alloc_pool bb_info_pool
;
479 /* Table to hold all bb_infos. */
480 static bb_info_t
*bb_table
;
482 /* There is a group_info for each rtx base that is used to reference
483 memory. There are also not many of the rtx bases because they are
484 very limited in scope. */
488 /* The actual base of the address. */
491 /* The sequential id of the base. This allows us to have a
492 canonical ordering of these that is not based on addresses. */
495 /* True if there are any positions that are to be processed
497 bool process_globally
;
499 /* True if the base of this group is either the frame_pointer or
500 hard_frame_pointer. */
503 /* A mem wrapped around the base pointer for the group in order to do
504 read dependency. It must be given BLKmode in order to encompass all
505 the possible offsets from the base. */
508 /* Canonized version of base_mem's address. */
511 /* These two sets of two bitmaps are used to keep track of how many
512 stores are actually referencing that position from this base. We
513 only do this for rtx bases as this will be used to assign
514 positions in the bitmaps for the global problem. Bit N is set in
515 store1 on the first store for offset N. Bit N is set in store2
516 for the second store to offset N. This is all we need since we
517 only care about offsets that have two or more stores for them.
519 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
520 for 0 and greater offsets.
522 There is one special case here, for stores into the stack frame,
523 we will or store1 into store2 before deciding which stores look
524 at globally. This is because stores to the stack frame that have
525 no other reads before the end of the function can also be
527 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
529 /* These bitmaps keep track of offsets in this group escape this function.
530 An offset escapes if it corresponds to a named variable whose
531 addressable flag is set. */
532 bitmap escaped_n
, escaped_p
;
534 /* The positions in this bitmap have the same assignments as the in,
535 out, gen and kill bitmaps. This bitmap is all zeros except for
536 the positions that are occupied by stores for this group. */
539 /* The offset_map is used to map the offsets from this base into
540 positions in the global bitmaps. It is only created after all of
541 the all of stores have been scanned and we know which ones we
543 int *offset_map_n
, *offset_map_p
;
544 int offset_map_size_n
, offset_map_size_p
;
546 typedef struct group_info
*group_info_t
;
547 typedef const struct group_info
*const_group_info_t
;
548 static alloc_pool rtx_group_info_pool
;
550 /* Index into the rtx_group_vec. */
551 static int rtx_group_next_id
;
554 static vec
<group_info_t
> rtx_group_vec
;
557 /* This structure holds the set of changes that are being deferred
558 when removing read operation. See replace_read. */
559 struct deferred_change
562 /* The mem that is being replaced. */
565 /* The reg it is being replaced with. */
568 struct deferred_change
*next
;
571 typedef struct deferred_change
*deferred_change_t
;
572 static alloc_pool deferred_change_pool
;
574 static deferred_change_t deferred_change_list
= NULL
;
576 /* The group that holds all of the clear_alias_sets. */
577 static group_info_t clear_alias_group
;
579 /* The modes of the clear_alias_sets. */
580 static htab_t clear_alias_mode_table
;
582 /* Hash table element to look up the mode for an alias set. */
583 struct clear_alias_mode_holder
585 alias_set_type alias_set
;
586 enum machine_mode mode
;
589 /* This is true except if cfun->stdarg -- i.e. we cannot do
590 this for vararg functions because they play games with the frame. */
591 static bool stores_off_frame_dead_at_return
;
593 /* Counter for stats. */
594 static int globally_deleted
;
595 static int locally_deleted
;
596 static int spill_deleted
;
598 static bitmap all_blocks
;
600 /* Locations that are killed by calls in the global phase. */
601 static bitmap kill_on_calls
;
603 /* The number of bits used in the global bitmaps. */
604 static unsigned int current_position
;
607 static bool gate_dse1 (void);
608 static bool gate_dse2 (void);
611 /*----------------------------------------------------------------------------
615 ----------------------------------------------------------------------------*/
618 /* Find the entry associated with ALIAS_SET. */
620 static struct clear_alias_mode_holder
*
621 clear_alias_set_lookup (alias_set_type alias_set
)
623 struct clear_alias_mode_holder tmp_holder
;
626 tmp_holder
.alias_set
= alias_set
;
627 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
630 return (struct clear_alias_mode_holder
*) *slot
;
634 /* Hashtable callbacks for maintaining the "bases" field of
635 store_group_info, given that the addresses are function invariants. */
637 struct invariant_group_base_hasher
: typed_noop_remove
<group_info
>
639 typedef group_info value_type
;
640 typedef group_info compare_type
;
641 static inline hashval_t
hash (const value_type
*);
642 static inline bool equal (const value_type
*, const compare_type
*);
646 invariant_group_base_hasher::equal (const value_type
*gi1
,
647 const compare_type
*gi2
)
649 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
653 invariant_group_base_hasher::hash (const value_type
*gi
)
656 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
659 /* Tables of group_info structures, hashed by base value. */
660 static hash_table
<invariant_group_base_hasher
> rtx_group_table
;
663 /* Get the GROUP for BASE. Add a new group if it is not there. */
666 get_group_info (rtx base
)
668 struct group_info tmp_gi
;
674 /* Find the store_base_info structure for BASE, creating a new one
676 tmp_gi
.rtx_base
= base
;
677 slot
= rtx_group_table
.find_slot (&tmp_gi
, INSERT
);
678 gi
= (group_info_t
) *slot
;
682 if (!clear_alias_group
)
684 clear_alias_group
= gi
=
685 (group_info_t
) pool_alloc (rtx_group_info_pool
);
686 memset (gi
, 0, sizeof (struct group_info
));
687 gi
->id
= rtx_group_next_id
++;
688 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
689 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
690 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
691 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
692 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
693 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
694 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
695 gi
->process_globally
= false;
696 gi
->offset_map_size_n
= 0;
697 gi
->offset_map_size_p
= 0;
698 gi
->offset_map_n
= NULL
;
699 gi
->offset_map_p
= NULL
;
700 rtx_group_vec
.safe_push (gi
);
702 return clear_alias_group
;
707 *slot
= gi
= (group_info_t
) pool_alloc (rtx_group_info_pool
);
709 gi
->id
= rtx_group_next_id
++;
710 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
711 gi
->canon_base_addr
= canon_rtx (base
);
712 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
713 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
714 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
715 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
716 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
717 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
718 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
719 gi
->process_globally
= false;
721 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
722 gi
->offset_map_size_n
= 0;
723 gi
->offset_map_size_p
= 0;
724 gi
->offset_map_n
= NULL
;
725 gi
->offset_map_p
= NULL
;
726 rtx_group_vec
.safe_push (gi
);
733 /* Initialization of data structures. */
739 globally_deleted
= 0;
742 bitmap_obstack_initialize (&dse_bitmap_obstack
);
743 gcc_obstack_init (&dse_obstack
);
745 scratch
= BITMAP_ALLOC (®_obstack
);
746 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
749 = create_alloc_pool ("rtx_store_info_pool",
750 sizeof (struct store_info
), 100);
752 = create_alloc_pool ("read_info_pool",
753 sizeof (struct read_info
), 100);
755 = create_alloc_pool ("insn_info_pool",
756 sizeof (struct insn_info
), 100);
758 = create_alloc_pool ("bb_info_pool",
759 sizeof (struct bb_info
), 100);
761 = create_alloc_pool ("rtx_group_info_pool",
762 sizeof (struct group_info
), 100);
764 = create_alloc_pool ("deferred_change_pool",
765 sizeof (struct deferred_change
), 10);
767 rtx_group_table
.create (11);
769 bb_table
= XNEWVEC (bb_info_t
, last_basic_block
);
770 rtx_group_next_id
= 0;
772 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
774 init_alias_analysis ();
776 clear_alias_group
= NULL
;
781 /*----------------------------------------------------------------------------
784 Scan all of the insns. Any random ordering of the blocks is fine.
785 Each block is scanned in forward order to accommodate cselib which
786 is used to remove stores with non-constant bases.
787 ----------------------------------------------------------------------------*/
789 /* Delete all of the store_info recs from INSN_INFO. */
792 free_store_info (insn_info_t insn_info
)
794 store_info_t store_info
= insn_info
->store_rec
;
797 store_info_t next
= store_info
->next
;
798 if (store_info
->is_large
)
799 BITMAP_FREE (store_info
->positions_needed
.large
.bmap
);
800 if (store_info
->cse_base
)
801 pool_free (cse_store_info_pool
, store_info
);
803 pool_free (rtx_store_info_pool
, store_info
);
807 insn_info
->cannot_delete
= true;
808 insn_info
->contains_cselib_groups
= false;
809 insn_info
->store_rec
= NULL
;
815 regset fixed_regs_live
;
817 } note_add_store_info
;
819 /* Callback for emit_inc_dec_insn_before via note_stores.
820 Check if a register is clobbered which is live afterwards. */
823 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
826 note_add_store_info
*info
= (note_add_store_info
*) data
;
832 /* If this register is referenced by the current or an earlier insn,
833 that's OK. E.g. this applies to the register that is being incremented
834 with this addition. */
835 for (insn
= info
->first
;
836 insn
!= NEXT_INSN (info
->current
);
837 insn
= NEXT_INSN (insn
))
838 if (reg_referenced_p (loc
, PATTERN (insn
)))
841 /* If we come here, we have a clobber of a register that's only OK
842 if that register is not live. If we don't have liveness information
843 available, fail now. */
844 if (!info
->fixed_regs_live
)
846 info
->failure
= true;
849 /* Now check if this is a live fixed register. */
851 n
= hard_regno_nregs
[r
][GET_MODE (loc
)];
853 if (REGNO_REG_SET_P (info
->fixed_regs_live
, r
+n
))
854 info
->failure
= true;
857 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
858 SRC + SRCOFF before insn ARG. */
861 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
862 rtx op ATTRIBUTE_UNUSED
,
863 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
865 insn_info_t insn_info
= (insn_info_t
) arg
;
866 rtx insn
= insn_info
->insn
, new_insn
, cur
;
867 note_add_store_info info
;
869 /* We can reuse all operands without copying, because we are about
870 to delete the insn that contained it. */
874 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
875 new_insn
= get_insns ();
879 new_insn
= gen_move_insn (dest
, src
);
880 info
.first
= new_insn
;
881 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
882 info
.failure
= false;
883 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
886 note_stores (PATTERN (cur
), note_add_store
, &info
);
889 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
890 return it immediately, communicating the failure to its caller. */
894 emit_insn_before (new_insn
, insn
);
899 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
900 is there, is split into a separate insn.
901 Return true on success (or if there was nothing to do), false on failure. */
904 check_for_inc_dec_1 (insn_info_t insn_info
)
906 rtx insn
= insn_info
->insn
;
907 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
909 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, insn_info
) == 0;
914 /* Entry point for postreload. If you work on reload_cse, or you need this
915 anywhere else, consider if you can provide register liveness information
916 and add a parameter to this function so that it can be passed down in
917 insn_info.fixed_regs_live. */
919 check_for_inc_dec (rtx insn
)
921 struct insn_info insn_info
;
924 insn_info
.insn
= insn
;
925 insn_info
.fixed_regs_live
= NULL
;
926 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
928 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, &insn_info
) == 0;
932 /* Delete the insn and free all of the fields inside INSN_INFO. */
935 delete_dead_store_insn (insn_info_t insn_info
)
937 read_info_t read_info
;
942 if (!check_for_inc_dec_1 (insn_info
))
944 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
946 fprintf (dump_file
, "Locally deleting insn %d ",
947 INSN_UID (insn_info
->insn
));
948 if (insn_info
->store_rec
->alias_set
)
949 fprintf (dump_file
, "alias set %d\n",
950 (int) insn_info
->store_rec
->alias_set
);
952 fprintf (dump_file
, "\n");
955 free_store_info (insn_info
);
956 read_info
= insn_info
->read_rec
;
960 read_info_t next
= read_info
->next
;
961 pool_free (read_info_pool
, read_info
);
964 insn_info
->read_rec
= NULL
;
966 delete_insn (insn_info
->insn
);
968 insn_info
->insn
= NULL
;
970 insn_info
->wild_read
= false;
973 /* Return whether DECL, a local variable, can possibly escape the current
977 local_variable_can_escape (tree decl
)
979 if (TREE_ADDRESSABLE (decl
))
982 /* If this is a partitioned variable, we need to consider all the variables
983 in the partition. This is necessary because a store into one of them can
984 be replaced with a store into another and this may not change the outcome
985 of the escape analysis. */
986 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
989 = pointer_map_contains (cfun
->gimple_df
->decls_to_pointers
, decl
);
991 return TREE_ADDRESSABLE (*(tree
*)namep
);
997 /* Return whether EXPR can possibly escape the current function scope. */
1000 can_escape (tree expr
)
1005 base
= get_base_address (expr
);
1007 && !may_be_aliased (base
)
1008 && !(TREE_CODE (base
) == VAR_DECL
1009 && !DECL_EXTERNAL (base
)
1010 && !TREE_STATIC (base
)
1011 && local_variable_can_escape (base
)))
1016 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1017 OFFSET and WIDTH. */
1020 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1024 bool expr_escapes
= can_escape (expr
);
1025 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1026 for (i
=offset
; i
<offset
+width
; i
++)
1034 store1
= group
->store1_n
;
1035 store2
= group
->store2_n
;
1036 escaped
= group
->escaped_n
;
1041 store1
= group
->store1_p
;
1042 store2
= group
->store2_p
;
1043 escaped
= group
->escaped_p
;
1047 if (!bitmap_set_bit (store1
, ai
))
1048 bitmap_set_bit (store2
, ai
);
1053 if (group
->offset_map_size_n
< ai
)
1054 group
->offset_map_size_n
= ai
;
1058 if (group
->offset_map_size_p
< ai
)
1059 group
->offset_map_size_p
= ai
;
1063 bitmap_set_bit (escaped
, ai
);
1068 reset_active_stores (void)
1070 active_local_stores
= NULL
;
1071 active_local_stores_len
= 0;
1074 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1077 free_read_records (bb_info_t bb_info
)
1079 insn_info_t insn_info
= bb_info
->last_insn
;
1080 read_info_t
*ptr
= &insn_info
->read_rec
;
1083 read_info_t next
= (*ptr
)->next
;
1084 if ((*ptr
)->alias_set
== 0)
1086 pool_free (read_info_pool
, *ptr
);
1090 ptr
= &(*ptr
)->next
;
1094 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1097 add_wild_read (bb_info_t bb_info
)
1099 insn_info_t insn_info
= bb_info
->last_insn
;
1100 insn_info
->wild_read
= true;
1101 free_read_records (bb_info
);
1102 reset_active_stores ();
1105 /* Set the BB_INFO so that the last insn is marked as a wild read of
1106 non-frame locations. */
1109 add_non_frame_wild_read (bb_info_t bb_info
)
1111 insn_info_t insn_info
= bb_info
->last_insn
;
1112 insn_info
->non_frame_wild_read
= true;
1113 free_read_records (bb_info
);
1114 reset_active_stores ();
1117 /* Return true if X is a constant or one of the registers that behave
1118 as a constant over the life of a function. This is equivalent to
1119 !rtx_varies_p for memory addresses. */
1122 const_or_frame_p (rtx x
)
1127 if (GET_CODE (x
) == REG
)
1129 /* Note that we have to test for the actual rtx used for the frame
1130 and arg pointers and not just the register number in case we have
1131 eliminated the frame and/or arg pointer and are using it
1133 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1134 /* The arg pointer varies if it is not a fixed register. */
1135 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1136 || x
== pic_offset_table_rtx
)
1144 /* Take all reasonable action to put the address of MEM into the form
1145 that we can do analysis on.
1147 The gold standard is to get the address into the form: address +
1148 OFFSET where address is something that rtx_varies_p considers a
1149 constant. When we can get the address in this form, we can do
1150 global analysis on it. Note that for constant bases, address is
1151 not actually returned, only the group_id. The address can be
1154 If that fails, we try cselib to get a value we can at least use
1155 locally. If that fails we return false.
1157 The GROUP_ID is set to -1 for cselib bases and the index of the
1158 group for non_varying bases.
1160 FOR_READ is true if this is a mem read and false if not. */
1163 canon_address (rtx mem
,
1164 alias_set_type
*alias_set_out
,
1166 HOST_WIDE_INT
*offset
,
1169 enum machine_mode address_mode
= get_address_mode (mem
);
1170 rtx mem_address
= XEXP (mem
, 0);
1171 rtx expanded_address
, address
;
1176 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1178 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1180 fprintf (dump_file
, " mem: ");
1181 print_inline_rtx (dump_file
, mem_address
, 0);
1182 fprintf (dump_file
, "\n");
1185 /* First see if just canon_rtx (mem_address) is const or frame,
1186 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1188 for (expanded
= 0; expanded
< 2; expanded
++)
1192 /* Use cselib to replace all of the reg references with the full
1193 expression. This will take care of the case where we have
1195 r_x = base + offset;
1200 val = *(base + offset); */
1202 expanded_address
= cselib_expand_value_rtx (mem_address
,
1205 /* If this fails, just go with the address from first
1207 if (!expanded_address
)
1211 expanded_address
= mem_address
;
1213 /* Split the address into canonical BASE + OFFSET terms. */
1214 address
= canon_rtx (expanded_address
);
1218 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1222 fprintf (dump_file
, "\n after cselib_expand address: ");
1223 print_inline_rtx (dump_file
, expanded_address
, 0);
1224 fprintf (dump_file
, "\n");
1227 fprintf (dump_file
, "\n after canon_rtx address: ");
1228 print_inline_rtx (dump_file
, address
, 0);
1229 fprintf (dump_file
, "\n");
1232 if (GET_CODE (address
) == CONST
)
1233 address
= XEXP (address
, 0);
1235 if (GET_CODE (address
) == PLUS
1236 && CONST_INT_P (XEXP (address
, 1)))
1238 *offset
= INTVAL (XEXP (address
, 1));
1239 address
= XEXP (address
, 0);
1242 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1243 && const_or_frame_p (address
))
1245 group_info_t group
= get_group_info (address
);
1247 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1248 fprintf (dump_file
, " gid=%d offset=%d \n",
1249 group
->id
, (int)*offset
);
1251 *group_id
= group
->id
;
1256 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1261 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1262 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1265 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1266 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1267 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1272 /* Clear the rhs field from the active_local_stores array. */
1275 clear_rhs_from_active_local_stores (void)
1277 insn_info_t ptr
= active_local_stores
;
1281 store_info_t store_info
= ptr
->store_rec
;
1282 /* Skip the clobbers. */
1283 while (!store_info
->is_set
)
1284 store_info
= store_info
->next
;
1286 store_info
->rhs
= NULL
;
1287 store_info
->const_rhs
= NULL
;
1289 ptr
= ptr
->next_local_store
;
1294 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1297 set_position_unneeded (store_info_t s_info
, int pos
)
1299 if (__builtin_expect (s_info
->is_large
, false))
1301 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1302 s_info
->positions_needed
.large
.count
++;
1305 s_info
->positions_needed
.small_bitmask
1306 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1309 /* Mark the whole store S_INFO as unneeded. */
1312 set_all_positions_unneeded (store_info_t s_info
)
1314 if (__builtin_expect (s_info
->is_large
, false))
1316 int pos
, end
= s_info
->end
- s_info
->begin
;
1317 for (pos
= 0; pos
< end
; pos
++)
1318 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1319 s_info
->positions_needed
.large
.count
= end
;
1322 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1325 /* Return TRUE if any bytes from S_INFO store are needed. */
1328 any_positions_needed_p (store_info_t s_info
)
1330 if (__builtin_expect (s_info
->is_large
, false))
1331 return (s_info
->positions_needed
.large
.count
1332 < s_info
->end
- s_info
->begin
);
1334 return (s_info
->positions_needed
.small_bitmask
1335 != (unsigned HOST_WIDE_INT
) 0);
1338 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1339 store are needed. */
1342 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1344 if (__builtin_expect (s_info
->is_large
, false))
1346 int end
= start
+ width
;
1348 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1354 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1355 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1360 static rtx
get_stored_val (store_info_t
, enum machine_mode
, HOST_WIDE_INT
,
1361 HOST_WIDE_INT
, basic_block
, bool);
1364 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1365 there is a candidate store, after adding it to the appropriate
1366 local store group if so. */
1369 record_store (rtx body
, bb_info_t bb_info
)
1371 rtx mem
, rhs
, const_rhs
, mem_addr
;
1372 HOST_WIDE_INT offset
= 0;
1373 HOST_WIDE_INT width
= 0;
1374 alias_set_type spill_alias_set
;
1375 insn_info_t insn_info
= bb_info
->last_insn
;
1376 store_info_t store_info
= NULL
;
1378 cselib_val
*base
= NULL
;
1379 insn_info_t ptr
, last
, redundant_reason
;
1380 bool store_is_unused
;
1382 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1385 mem
= SET_DEST (body
);
1387 /* If this is not used, then this cannot be used to keep the insn
1388 from being deleted. On the other hand, it does provide something
1389 that can be used to prove that another store is dead. */
1391 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1393 /* Check whether that value is a suitable memory location. */
1396 /* If the set or clobber is unused, then it does not effect our
1397 ability to get rid of the entire insn. */
1398 if (!store_is_unused
)
1399 insn_info
->cannot_delete
= true;
1403 /* At this point we know mem is a mem. */
1404 if (GET_MODE (mem
) == BLKmode
)
1406 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1408 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1409 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1410 add_wild_read (bb_info
);
1411 insn_info
->cannot_delete
= true;
1414 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1415 as memset (addr, 0, 36); */
1416 else if (!MEM_SIZE_KNOWN_P (mem
)
1417 || MEM_SIZE (mem
) <= 0
1418 || MEM_SIZE (mem
) > MAX_OFFSET
1419 || GET_CODE (body
) != SET
1420 || !CONST_INT_P (SET_SRC (body
)))
1422 if (!store_is_unused
)
1424 /* If the set or clobber is unused, then it does not effect our
1425 ability to get rid of the entire insn. */
1426 insn_info
->cannot_delete
= true;
1427 clear_rhs_from_active_local_stores ();
1433 /* We can still process a volatile mem, we just cannot delete it. */
1434 if (MEM_VOLATILE_P (mem
))
1435 insn_info
->cannot_delete
= true;
1437 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1439 clear_rhs_from_active_local_stores ();
1443 if (GET_MODE (mem
) == BLKmode
)
1444 width
= MEM_SIZE (mem
);
1446 width
= GET_MODE_SIZE (GET_MODE (mem
));
1448 if (spill_alias_set
)
1450 bitmap store1
= clear_alias_group
->store1_p
;
1451 bitmap store2
= clear_alias_group
->store2_p
;
1453 gcc_assert (GET_MODE (mem
) != BLKmode
);
1455 if (!bitmap_set_bit (store1
, spill_alias_set
))
1456 bitmap_set_bit (store2
, spill_alias_set
);
1458 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1459 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1461 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1463 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1464 fprintf (dump_file
, " processing spill store %d(%s)\n",
1465 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1467 else if (group_id
>= 0)
1469 /* In the restrictive case where the base is a constant or the
1470 frame pointer we can do global analysis. */
1473 = rtx_group_vec
[group_id
];
1474 tree expr
= MEM_EXPR (mem
);
1476 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1477 set_usage_bits (group
, offset
, width
, expr
);
1479 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1480 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1481 group_id
, (int)offset
, (int)(offset
+width
));
1485 if (may_be_sp_based_p (XEXP (mem
, 0)))
1486 insn_info
->stack_pointer_based
= true;
1487 insn_info
->contains_cselib_groups
= true;
1489 store_info
= (store_info_t
) pool_alloc (cse_store_info_pool
);
1492 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1493 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1494 (int)offset
, (int)(offset
+width
));
1497 const_rhs
= rhs
= NULL_RTX
;
1498 if (GET_CODE (body
) == SET
1499 /* No place to keep the value after ra. */
1500 && !reload_completed
1501 && (REG_P (SET_SRC (body
))
1502 || GET_CODE (SET_SRC (body
)) == SUBREG
1503 || CONSTANT_P (SET_SRC (body
)))
1504 && !MEM_VOLATILE_P (mem
)
1505 /* Sometimes the store and reload is used for truncation and
1507 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1509 rhs
= SET_SRC (body
);
1510 if (CONSTANT_P (rhs
))
1512 else if (body
== PATTERN (insn_info
->insn
))
1514 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1515 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1516 const_rhs
= XEXP (tem
, 0);
1518 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1520 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1522 if (tem
&& CONSTANT_P (tem
))
1527 /* Check to see if this stores causes some other stores to be
1529 ptr
= active_local_stores
;
1531 redundant_reason
= NULL
;
1532 mem
= canon_rtx (mem
);
1533 /* For alias_set != 0 canon_true_dependence should be never called. */
1534 if (spill_alias_set
)
1535 mem_addr
= NULL_RTX
;
1539 mem_addr
= base
->val_rtx
;
1543 = rtx_group_vec
[group_id
];
1544 mem_addr
= group
->canon_base_addr
;
1547 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1552 insn_info_t next
= ptr
->next_local_store
;
1553 store_info_t s_info
= ptr
->store_rec
;
1556 /* Skip the clobbers. We delete the active insn if this insn
1557 shadows the set. To have been put on the active list, it
1558 has exactly on set. */
1559 while (!s_info
->is_set
)
1560 s_info
= s_info
->next
;
1562 if (s_info
->alias_set
!= spill_alias_set
)
1564 else if (s_info
->alias_set
)
1566 struct clear_alias_mode_holder
*entry
1567 = clear_alias_set_lookup (s_info
->alias_set
);
1568 /* Generally, spills cannot be processed if and of the
1569 references to the slot have a different mode. But if
1570 we are in the same block and mode is exactly the same
1571 between this store and one before in the same block,
1572 we can still delete it. */
1573 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1574 && (GET_MODE (mem
) == entry
->mode
))
1577 set_all_positions_unneeded (s_info
);
1579 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1580 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1581 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1583 else if ((s_info
->group_id
== group_id
)
1584 && (s_info
->cse_base
== base
))
1587 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1588 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1589 INSN_UID (ptr
->insn
), s_info
->group_id
,
1590 (int)s_info
->begin
, (int)s_info
->end
);
1592 /* Even if PTR won't be eliminated as unneeded, if both
1593 PTR and this insn store the same constant value, we might
1594 eliminate this insn instead. */
1595 if (s_info
->const_rhs
1597 && offset
>= s_info
->begin
1598 && offset
+ width
<= s_info
->end
1599 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1602 if (GET_MODE (mem
) == BLKmode
)
1604 if (GET_MODE (s_info
->mem
) == BLKmode
1605 && s_info
->const_rhs
== const_rhs
)
1606 redundant_reason
= ptr
;
1608 else if (s_info
->const_rhs
== const0_rtx
1609 && const_rhs
== const0_rtx
)
1610 redundant_reason
= ptr
;
1615 val
= get_stored_val (s_info
, GET_MODE (mem
),
1616 offset
, offset
+ width
,
1617 BLOCK_FOR_INSN (insn_info
->insn
),
1619 if (get_insns () != NULL
)
1622 if (val
&& rtx_equal_p (val
, const_rhs
))
1623 redundant_reason
= ptr
;
1627 for (i
= MAX (offset
, s_info
->begin
);
1628 i
< offset
+ width
&& i
< s_info
->end
;
1630 set_position_unneeded (s_info
, i
- s_info
->begin
);
1632 else if (s_info
->rhs
)
1633 /* Need to see if it is possible for this store to overwrite
1634 the value of store_info. If it is, set the rhs to NULL to
1635 keep it from being used to remove a load. */
1637 if (canon_true_dependence (s_info
->mem
,
1638 GET_MODE (s_info
->mem
),
1643 s_info
->const_rhs
= NULL
;
1647 /* An insn can be deleted if every position of every one of
1648 its s_infos is zero. */
1649 if (any_positions_needed_p (s_info
))
1654 insn_info_t insn_to_delete
= ptr
;
1656 active_local_stores_len
--;
1658 last
->next_local_store
= ptr
->next_local_store
;
1660 active_local_stores
= ptr
->next_local_store
;
1662 if (!insn_to_delete
->cannot_delete
)
1663 delete_dead_store_insn (insn_to_delete
);
1671 /* Finish filling in the store_info. */
1672 store_info
->next
= insn_info
->store_rec
;
1673 insn_info
->store_rec
= store_info
;
1674 store_info
->mem
= mem
;
1675 store_info
->alias_set
= spill_alias_set
;
1676 store_info
->mem_addr
= mem_addr
;
1677 store_info
->cse_base
= base
;
1678 if (width
> HOST_BITS_PER_WIDE_INT
)
1680 store_info
->is_large
= true;
1681 store_info
->positions_needed
.large
.count
= 0;
1682 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1686 store_info
->is_large
= false;
1687 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1689 store_info
->group_id
= group_id
;
1690 store_info
->begin
= offset
;
1691 store_info
->end
= offset
+ width
;
1692 store_info
->is_set
= GET_CODE (body
) == SET
;
1693 store_info
->rhs
= rhs
;
1694 store_info
->const_rhs
= const_rhs
;
1695 store_info
->redundant_reason
= redundant_reason
;
1697 /* If this is a clobber, we return 0. We will only be able to
1698 delete this insn if there is only one store USED store, but we
1699 can use the clobber to delete other stores earlier. */
1700 return store_info
->is_set
? 1 : 0;
1705 dump_insn_info (const char * start
, insn_info_t insn_info
)
1707 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1708 INSN_UID (insn_info
->insn
),
1709 insn_info
->store_rec
? "has store" : "naked");
1713 /* If the modes are different and the value's source and target do not
1714 line up, we need to extract the value from lower part of the rhs of
1715 the store, shift it, and then put it into a form that can be shoved
1716 into the read_insn. This function generates a right SHIFT of a
1717 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1718 shift sequence is returned or NULL if we failed to find a
1722 find_shift_sequence (int access_size
,
1723 store_info_t store_info
,
1724 enum machine_mode read_mode
,
1725 int shift
, bool speed
, bool require_cst
)
1727 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1728 enum machine_mode new_mode
;
1729 rtx read_reg
= NULL
;
1731 /* Some machines like the x86 have shift insns for each size of
1732 operand. Other machines like the ppc or the ia-64 may only have
1733 shift insns that shift values within 32 or 64 bit registers.
1734 This loop tries to find the smallest shift insn that will right
1735 justify the value we want to read but is available in one insn on
1738 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1740 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1741 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1743 rtx target
, new_reg
, shift_seq
, insn
, new_lhs
;
1746 /* If a constant was stored into memory, try to simplify it here,
1747 otherwise the cost of the shift might preclude this optimization
1748 e.g. at -Os, even when no actual shift will be needed. */
1749 if (store_info
->const_rhs
)
1751 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1752 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1754 if (ret
&& CONSTANT_P (ret
))
1756 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1757 ret
, GEN_INT (shift
));
1758 if (ret
&& CONSTANT_P (ret
))
1760 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1761 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1762 if (ret
&& CONSTANT_P (ret
)
1763 && set_src_cost (ret
, speed
) <= COSTS_N_INSNS (1))
1772 /* Try a wider mode if truncating the store mode to NEW_MODE
1773 requires a real instruction. */
1774 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1775 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1778 /* Also try a wider mode if the necessary punning is either not
1779 desirable or not possible. */
1780 if (!CONSTANT_P (store_info
->rhs
)
1781 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1784 new_reg
= gen_reg_rtx (new_mode
);
1788 /* In theory we could also check for an ashr. Ian Taylor knows
1789 of one dsp where the cost of these two was not the same. But
1790 this really is a rare case anyway. */
1791 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1792 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1794 shift_seq
= get_insns ();
1797 if (target
!= new_reg
|| shift_seq
== NULL
)
1801 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1803 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1805 /* The computation up to here is essentially independent
1806 of the arguments and could be precomputed. It may
1807 not be worth doing so. We could precompute if
1808 worthwhile or at least cache the results. The result
1809 technically depends on both SHIFT and ACCESS_SIZE,
1810 but in practice the answer will depend only on ACCESS_SIZE. */
1812 if (cost
> COSTS_N_INSNS (1))
1815 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1816 copy_rtx (store_info
->rhs
));
1817 if (new_lhs
== NULL_RTX
)
1820 /* We found an acceptable shift. Generate a move to
1821 take the value from the store and put it into the
1822 shift pseudo, then shift it, then generate another
1823 move to put in into the target of the read. */
1824 emit_move_insn (new_reg
, new_lhs
);
1825 emit_insn (shift_seq
);
1826 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1834 /* Call back for note_stores to find the hard regs set or clobbered by
1835 insn. Data is a bitmap of the hardregs set so far. */
1838 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1840 bitmap regs_set
= (bitmap
) data
;
1843 && HARD_REGISTER_P (x
))
1845 unsigned int regno
= REGNO (x
);
1846 bitmap_set_range (regs_set
, regno
,
1847 hard_regno_nregs
[regno
][GET_MODE (x
)]);
1851 /* Helper function for replace_read and record_store.
1852 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1853 to one before READ_END bytes read in READ_MODE. Return NULL
1854 if not successful. If REQUIRE_CST is true, return always constant. */
1857 get_stored_val (store_info_t store_info
, enum machine_mode read_mode
,
1858 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1859 basic_block bb
, bool require_cst
)
1861 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1863 int access_size
; /* In bytes. */
1866 /* To get here the read is within the boundaries of the write so
1867 shift will never be negative. Start out with the shift being in
1869 if (store_mode
== BLKmode
)
1871 else if (BYTES_BIG_ENDIAN
)
1872 shift
= store_info
->end
- read_end
;
1874 shift
= read_begin
- store_info
->begin
;
1876 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1878 /* From now on it is bits. */
1879 shift
*= BITS_PER_UNIT
;
1882 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1883 optimize_bb_for_speed_p (bb
),
1885 else if (store_mode
== BLKmode
)
1887 /* The store is a memset (addr, const_val, const_size). */
1888 gcc_assert (CONST_INT_P (store_info
->rhs
));
1889 store_mode
= int_mode_for_mode (read_mode
);
1890 if (store_mode
== BLKmode
)
1891 read_reg
= NULL_RTX
;
1892 else if (store_info
->rhs
== const0_rtx
)
1893 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1894 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1895 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1896 read_reg
= NULL_RTX
;
1899 unsigned HOST_WIDE_INT c
1900 = INTVAL (store_info
->rhs
)
1901 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1902 int shift
= BITS_PER_UNIT
;
1903 while (shift
< HOST_BITS_PER_WIDE_INT
)
1908 read_reg
= gen_int_mode (c
, store_mode
);
1909 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1912 else if (store_info
->const_rhs
1914 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1915 read_reg
= extract_low_bits (read_mode
, store_mode
,
1916 copy_rtx (store_info
->const_rhs
));
1918 read_reg
= extract_low_bits (read_mode
, store_mode
,
1919 copy_rtx (store_info
->rhs
));
1920 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1921 read_reg
= NULL_RTX
;
1925 /* Take a sequence of:
1948 Depending on the alignment and the mode of the store and
1952 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1953 and READ_INSN are for the read. Return true if the replacement
1957 replace_read (store_info_t store_info
, insn_info_t store_insn
,
1958 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1961 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1962 enum machine_mode read_mode
= GET_MODE (read_info
->mem
);
1963 rtx insns
, this_insn
, read_reg
;
1969 /* Create a sequence of instructions to set up the read register.
1970 This sequence goes immediately before the store and its result
1971 is read by the load.
1973 We need to keep this in perspective. We are replacing a read
1974 with a sequence of insns, but the read will almost certainly be
1975 in cache, so it is not going to be an expensive one. Thus, we
1976 are not willing to do a multi insn shift or worse a subroutine
1977 call to get rid of the read. */
1978 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1979 fprintf (dump_file
, "trying to replace %smode load in insn %d"
1980 " from %smode store in insn %d\n",
1981 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
1982 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
1984 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
1985 read_reg
= get_stored_val (store_info
,
1986 read_mode
, read_info
->begin
, read_info
->end
,
1988 if (read_reg
== NULL_RTX
)
1991 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1992 fprintf (dump_file
, " -- could not extract bits of stored value\n");
1995 /* Force the value into a new register so that it won't be clobbered
1996 between the store and the load. */
1997 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
1998 insns
= get_insns ();
2001 if (insns
!= NULL_RTX
)
2003 /* Now we have to scan the set of new instructions to see if the
2004 sequence contains and sets of hardregs that happened to be
2005 live at this point. For instance, this can happen if one of
2006 the insns sets the CC and the CC happened to be live at that
2007 point. This does occasionally happen, see PR 37922. */
2008 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2010 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2011 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2013 bitmap_and_into (regs_set
, regs_live
);
2014 if (!bitmap_empty_p (regs_set
))
2016 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2019 "abandoning replacement because sequence clobbers live hardregs:");
2020 df_print_regset (dump_file
, regs_set
);
2023 BITMAP_FREE (regs_set
);
2026 BITMAP_FREE (regs_set
);
2029 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2031 deferred_change_t deferred_change
=
2032 (deferred_change_t
) pool_alloc (deferred_change_pool
);
2034 /* Insert this right before the store insn where it will be safe
2035 from later insns that might change it before the read. */
2036 emit_insn_before (insns
, store_insn
->insn
);
2038 /* And now for the kludge part: cselib croaks if you just
2039 return at this point. There are two reasons for this:
2041 1) Cselib has an idea of how many pseudos there are and
2042 that does not include the new ones we just added.
2044 2) Cselib does not know about the move insn we added
2045 above the store_info, and there is no way to tell it
2046 about it, because it has "moved on".
2048 Problem (1) is fixable with a certain amount of engineering.
2049 Problem (2) is requires starting the bb from scratch. This
2052 So we are just going to have to lie. The move/extraction
2053 insns are not really an issue, cselib did not see them. But
2054 the use of the new pseudo read_insn is a real problem because
2055 cselib has not scanned this insn. The way that we solve this
2056 problem is that we are just going to put the mem back for now
2057 and when we are finished with the block, we undo this. We
2058 keep a table of mems to get rid of. At the end of the basic
2059 block we can put them back. */
2061 *loc
= read_info
->mem
;
2062 deferred_change
->next
= deferred_change_list
;
2063 deferred_change_list
= deferred_change
;
2064 deferred_change
->loc
= loc
;
2065 deferred_change
->reg
= read_reg
;
2067 /* Get rid of the read_info, from the point of view of the
2068 rest of dse, play like this read never happened. */
2069 read_insn
->read_rec
= read_info
->next
;
2070 pool_free (read_info_pool
, read_info
);
2071 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2073 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2074 print_simple_rtl (dump_file
, read_reg
);
2075 fprintf (dump_file
, "\n");
2081 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2083 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2084 print_simple_rtl (dump_file
, read_reg
);
2085 fprintf (dump_file
, " led to an invalid instruction\n");
2091 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2092 if LOC is a mem and if it is look at the address and kill any
2093 appropriate stores that may be active. */
2096 check_mem_read_rtx (rtx
*loc
, void *data
)
2098 rtx mem
= *loc
, mem_addr
;
2100 insn_info_t insn_info
;
2101 HOST_WIDE_INT offset
= 0;
2102 HOST_WIDE_INT width
= 0;
2103 alias_set_type spill_alias_set
= 0;
2104 cselib_val
*base
= NULL
;
2106 read_info_t read_info
;
2108 if (!mem
|| !MEM_P (mem
))
2111 bb_info
= (bb_info_t
) data
;
2112 insn_info
= bb_info
->last_insn
;
2114 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2115 || (MEM_VOLATILE_P (mem
)))
2117 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2118 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2119 add_wild_read (bb_info
);
2120 insn_info
->cannot_delete
= true;
2124 /* If it is reading readonly mem, then there can be no conflict with
2126 if (MEM_READONLY_P (mem
))
2129 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2131 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2132 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2133 add_wild_read (bb_info
);
2137 if (GET_MODE (mem
) == BLKmode
)
2140 width
= GET_MODE_SIZE (GET_MODE (mem
));
2142 read_info
= (read_info_t
) pool_alloc (read_info_pool
);
2143 read_info
->group_id
= group_id
;
2144 read_info
->mem
= mem
;
2145 read_info
->alias_set
= spill_alias_set
;
2146 read_info
->begin
= offset
;
2147 read_info
->end
= offset
+ width
;
2148 read_info
->next
= insn_info
->read_rec
;
2149 insn_info
->read_rec
= read_info
;
2150 /* For alias_set != 0 canon_true_dependence should be never called. */
2151 if (spill_alias_set
)
2152 mem_addr
= NULL_RTX
;
2156 mem_addr
= base
->val_rtx
;
2160 = rtx_group_vec
[group_id
];
2161 mem_addr
= group
->canon_base_addr
;
2164 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2167 /* We ignore the clobbers in store_info. The is mildly aggressive,
2168 but there really should not be a clobber followed by a read. */
2170 if (spill_alias_set
)
2172 insn_info_t i_ptr
= active_local_stores
;
2173 insn_info_t last
= NULL
;
2175 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2176 fprintf (dump_file
, " processing spill load %d\n",
2177 (int) spill_alias_set
);
2181 store_info_t store_info
= i_ptr
->store_rec
;
2183 /* Skip the clobbers. */
2184 while (!store_info
->is_set
)
2185 store_info
= store_info
->next
;
2187 if (store_info
->alias_set
== spill_alias_set
)
2189 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2190 dump_insn_info ("removing from active", i_ptr
);
2192 active_local_stores_len
--;
2194 last
->next_local_store
= i_ptr
->next_local_store
;
2196 active_local_stores
= i_ptr
->next_local_store
;
2200 i_ptr
= i_ptr
->next_local_store
;
2203 else if (group_id
>= 0)
2205 /* This is the restricted case where the base is a constant or
2206 the frame pointer and offset is a constant. */
2207 insn_info_t i_ptr
= active_local_stores
;
2208 insn_info_t last
= NULL
;
2210 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2213 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2216 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2217 group_id
, (int)offset
, (int)(offset
+width
));
2222 bool remove
= false;
2223 store_info_t store_info
= i_ptr
->store_rec
;
2225 /* Skip the clobbers. */
2226 while (!store_info
->is_set
)
2227 store_info
= store_info
->next
;
2229 /* There are three cases here. */
2230 if (store_info
->group_id
< 0)
2231 /* We have a cselib store followed by a read from a
2234 = canon_true_dependence (store_info
->mem
,
2235 GET_MODE (store_info
->mem
),
2236 store_info
->mem_addr
,
2239 else if (group_id
== store_info
->group_id
)
2241 /* This is a block mode load. We may get lucky and
2242 canon_true_dependence may save the day. */
2245 = canon_true_dependence (store_info
->mem
,
2246 GET_MODE (store_info
->mem
),
2247 store_info
->mem_addr
,
2250 /* If this read is just reading back something that we just
2251 stored, rewrite the read. */
2255 && offset
>= store_info
->begin
2256 && offset
+ width
<= store_info
->end
2257 && all_positions_needed_p (store_info
,
2258 offset
- store_info
->begin
,
2260 && replace_read (store_info
, i_ptr
, read_info
,
2261 insn_info
, loc
, bb_info
->regs_live
))
2264 /* The bases are the same, just see if the offsets
2266 if ((offset
< store_info
->end
)
2267 && (offset
+ width
> store_info
->begin
))
2273 The else case that is missing here is that the
2274 bases are constant but different. There is nothing
2275 to do here because there is no overlap. */
2279 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2280 dump_insn_info ("removing from active", i_ptr
);
2282 active_local_stores_len
--;
2284 last
->next_local_store
= i_ptr
->next_local_store
;
2286 active_local_stores
= i_ptr
->next_local_store
;
2290 i_ptr
= i_ptr
->next_local_store
;
2295 insn_info_t i_ptr
= active_local_stores
;
2296 insn_info_t last
= NULL
;
2297 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2299 fprintf (dump_file
, " processing cselib load mem:");
2300 print_inline_rtx (dump_file
, mem
, 0);
2301 fprintf (dump_file
, "\n");
2306 bool remove
= false;
2307 store_info_t store_info
= i_ptr
->store_rec
;
2309 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2310 fprintf (dump_file
, " processing cselib load against insn %d\n",
2311 INSN_UID (i_ptr
->insn
));
2313 /* Skip the clobbers. */
2314 while (!store_info
->is_set
)
2315 store_info
= store_info
->next
;
2317 /* If this read is just reading back something that we just
2318 stored, rewrite the read. */
2320 && store_info
->group_id
== -1
2321 && store_info
->cse_base
== base
2323 && offset
>= store_info
->begin
2324 && offset
+ width
<= store_info
->end
2325 && all_positions_needed_p (store_info
,
2326 offset
- store_info
->begin
, width
)
2327 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2328 bb_info
->regs_live
))
2331 if (!store_info
->alias_set
)
2332 remove
= canon_true_dependence (store_info
->mem
,
2333 GET_MODE (store_info
->mem
),
2334 store_info
->mem_addr
,
2339 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2340 dump_insn_info ("removing from active", i_ptr
);
2342 active_local_stores_len
--;
2344 last
->next_local_store
= i_ptr
->next_local_store
;
2346 active_local_stores
= i_ptr
->next_local_store
;
2350 i_ptr
= i_ptr
->next_local_store
;
2356 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2357 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2358 true for any part of *LOC. */
2361 check_mem_read_use (rtx
*loc
, void *data
)
2363 for_each_rtx (loc
, check_mem_read_rtx
, data
);
2367 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2368 So far it only handles arguments passed in registers. */
2371 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2373 CUMULATIVE_ARGS args_so_far_v
;
2374 cumulative_args_t args_so_far
;
2378 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2379 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2381 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2383 arg
!= void_list_node
&& idx
< nargs
;
2384 arg
= TREE_CHAIN (arg
), idx
++)
2386 enum machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2388 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2389 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2390 || GET_MODE_CLASS (mode
) != MODE_INT
)
2393 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2395 link
= XEXP (link
, 1))
2396 if (GET_CODE (XEXP (link
, 0)) == USE
)
2398 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2399 if (REG_P (args
[idx
])
2400 && REGNO (args
[idx
]) == REGNO (reg
)
2401 && (GET_MODE (args
[idx
]) == mode
2402 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2403 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2405 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2406 > GET_MODE_SIZE (mode
)))))
2412 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2413 if (GET_MODE (args
[idx
]) != mode
)
2415 if (!tmp
|| !CONST_INT_P (tmp
))
2417 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2422 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2424 if (arg
!= void_list_node
|| idx
!= nargs
)
2429 /* Return a bitmap of the fixed registers contained in IN. */
2432 copy_fixed_regs (const_bitmap in
)
2436 ret
= ALLOC_REG_SET (NULL
);
2437 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2441 /* Apply record_store to all candidate stores in INSN. Mark INSN
2442 if some part of it is not a candidate store and assigns to a
2443 non-register target. */
2446 scan_insn (bb_info_t bb_info
, rtx insn
)
2449 insn_info_t insn_info
= (insn_info_t
) pool_alloc (insn_info_pool
);
2451 memset (insn_info
, 0, sizeof (struct insn_info
));
2453 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2454 fprintf (dump_file
, "\n**scanning insn=%d\n",
2457 insn_info
->prev_insn
= bb_info
->last_insn
;
2458 insn_info
->insn
= insn
;
2459 bb_info
->last_insn
= insn_info
;
2461 if (DEBUG_INSN_P (insn
))
2463 insn_info
->cannot_delete
= true;
2467 /* Cselib clears the table for this case, so we have to essentially
2469 if (NONJUMP_INSN_P (insn
)
2470 && volatile_insn_p (PATTERN (insn
)))
2472 add_wild_read (bb_info
);
2473 insn_info
->cannot_delete
= true;
2477 /* Look at all of the uses in the insn. */
2478 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2483 tree memset_call
= NULL_TREE
;
2485 insn_info
->cannot_delete
= true;
2487 /* Const functions cannot do anything bad i.e. read memory,
2488 however, they can read their parameters which may have
2489 been pushed onto the stack.
2490 memset and bzero don't read memory either. */
2491 const_call
= RTL_CONST_CALL_P (insn
);
2494 rtx call
= get_call_rtx_from (insn
);
2495 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2497 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2498 if (SYMBOL_REF_DECL (symbol
)
2499 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2501 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2503 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2504 == BUILT_IN_MEMSET
))
2505 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2506 memset_call
= SYMBOL_REF_DECL (symbol
);
2510 if (const_call
|| memset_call
)
2512 insn_info_t i_ptr
= active_local_stores
;
2513 insn_info_t last
= NULL
;
2515 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2516 fprintf (dump_file
, "%s call %d\n",
2517 const_call
? "const" : "memset", INSN_UID (insn
));
2519 /* See the head comment of the frame_read field. */
2520 if (reload_completed
)
2521 insn_info
->frame_read
= true;
2523 /* Loop over the active stores and remove those which are
2524 killed by the const function call. */
2527 bool remove_store
= false;
2529 /* The stack pointer based stores are always killed. */
2530 if (i_ptr
->stack_pointer_based
)
2531 remove_store
= true;
2533 /* If the frame is read, the frame related stores are killed. */
2534 else if (insn_info
->frame_read
)
2536 store_info_t store_info
= i_ptr
->store_rec
;
2538 /* Skip the clobbers. */
2539 while (!store_info
->is_set
)
2540 store_info
= store_info
->next
;
2542 if (store_info
->group_id
>= 0
2543 && rtx_group_vec
[store_info
->group_id
]->frame_related
)
2544 remove_store
= true;
2549 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2550 dump_insn_info ("removing from active", i_ptr
);
2552 active_local_stores_len
--;
2554 last
->next_local_store
= i_ptr
->next_local_store
;
2556 active_local_stores
= i_ptr
->next_local_store
;
2561 i_ptr
= i_ptr
->next_local_store
;
2567 if (get_call_args (insn
, memset_call
, args
, 3)
2568 && CONST_INT_P (args
[1])
2569 && CONST_INT_P (args
[2])
2570 && INTVAL (args
[2]) > 0)
2572 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2573 set_mem_size (mem
, INTVAL (args
[2]));
2574 body
= gen_rtx_SET (VOIDmode
, mem
, args
[1]);
2575 mems_found
+= record_store (body
, bb_info
);
2576 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2577 fprintf (dump_file
, "handling memset as BLKmode store\n");
2578 if (mems_found
== 1)
2580 if (active_local_stores_len
++
2581 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2583 active_local_stores_len
= 1;
2584 active_local_stores
= NULL
;
2586 insn_info
->fixed_regs_live
2587 = copy_fixed_regs (bb_info
->regs_live
);
2588 insn_info
->next_local_store
= active_local_stores
;
2589 active_local_stores
= insn_info
;
2596 /* Every other call, including pure functions, may read any memory
2597 that is not relative to the frame. */
2598 add_non_frame_wild_read (bb_info
);
2603 /* Assuming that there are sets in these insns, we cannot delete
2605 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2606 || volatile_refs_p (PATTERN (insn
))
2607 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2608 || (RTX_FRAME_RELATED_P (insn
))
2609 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2610 insn_info
->cannot_delete
= true;
2612 body
= PATTERN (insn
);
2613 if (GET_CODE (body
) == PARALLEL
)
2616 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2617 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2620 mems_found
+= record_store (body
, bb_info
);
2622 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2623 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2624 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2626 /* If we found some sets of mems, add it into the active_local_stores so
2627 that it can be locally deleted if found dead or used for
2628 replace_read and redundant constant store elimination. Otherwise mark
2629 it as cannot delete. This simplifies the processing later. */
2630 if (mems_found
== 1)
2632 if (active_local_stores_len
++
2633 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2635 active_local_stores_len
= 1;
2636 active_local_stores
= NULL
;
2638 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2639 insn_info
->next_local_store
= active_local_stores
;
2640 active_local_stores
= insn_info
;
2643 insn_info
->cannot_delete
= true;
2647 /* Remove BASE from the set of active_local_stores. This is a
2648 callback from cselib that is used to get rid of the stores in
2649 active_local_stores. */
2652 remove_useless_values (cselib_val
*base
)
2654 insn_info_t insn_info
= active_local_stores
;
2655 insn_info_t last
= NULL
;
2659 store_info_t store_info
= insn_info
->store_rec
;
2662 /* If ANY of the store_infos match the cselib group that is
2663 being deleted, then the insn can not be deleted. */
2666 if ((store_info
->group_id
== -1)
2667 && (store_info
->cse_base
== base
))
2672 store_info
= store_info
->next
;
2677 active_local_stores_len
--;
2679 last
->next_local_store
= insn_info
->next_local_store
;
2681 active_local_stores
= insn_info
->next_local_store
;
2682 free_store_info (insn_info
);
2687 insn_info
= insn_info
->next_local_store
;
2692 /* Do all of step 1. */
2698 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2701 all_blocks
= BITMAP_ALLOC (NULL
);
2702 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2703 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2708 bb_info_t bb_info
= (bb_info_t
) pool_alloc (bb_info_pool
);
2710 memset (bb_info
, 0, sizeof (struct bb_info
));
2711 bitmap_set_bit (all_blocks
, bb
->index
);
2712 bb_info
->regs_live
= regs_live
;
2714 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2715 df_simulate_initialize_forwards (bb
, regs_live
);
2717 bb_table
[bb
->index
] = bb_info
;
2718 cselib_discard_hook
= remove_useless_values
;
2720 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2725 = create_alloc_pool ("cse_store_info_pool",
2726 sizeof (struct store_info
), 100);
2727 active_local_stores
= NULL
;
2728 active_local_stores_len
= 0;
2729 cselib_clear_table ();
2731 /* Scan the insns. */
2732 FOR_BB_INSNS (bb
, insn
)
2735 scan_insn (bb_info
, insn
);
2736 cselib_process_insn (insn
);
2738 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2741 /* This is something of a hack, because the global algorithm
2742 is supposed to take care of the case where stores go dead
2743 at the end of the function. However, the global
2744 algorithm must take a more conservative view of block
2745 mode reads than the local alg does. So to get the case
2746 where you have a store to the frame followed by a non
2747 overlapping block more read, we look at the active local
2748 stores at the end of the function and delete all of the
2749 frame and spill based ones. */
2750 if (stores_off_frame_dead_at_return
2751 && (EDGE_COUNT (bb
->succs
) == 0
2752 || (single_succ_p (bb
)
2753 && single_succ (bb
) == EXIT_BLOCK_PTR
2754 && ! crtl
->calls_eh_return
)))
2756 insn_info_t i_ptr
= active_local_stores
;
2759 store_info_t store_info
= i_ptr
->store_rec
;
2761 /* Skip the clobbers. */
2762 while (!store_info
->is_set
)
2763 store_info
= store_info
->next
;
2764 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2765 delete_dead_store_insn (i_ptr
);
2767 if (store_info
->group_id
>= 0)
2770 = rtx_group_vec
[store_info
->group_id
];
2771 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2772 delete_dead_store_insn (i_ptr
);
2775 i_ptr
= i_ptr
->next_local_store
;
2779 /* Get rid of the loads that were discovered in
2780 replace_read. Cselib is finished with this block. */
2781 while (deferred_change_list
)
2783 deferred_change_t next
= deferred_change_list
->next
;
2785 /* There is no reason to validate this change. That was
2787 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2788 pool_free (deferred_change_pool
, deferred_change_list
);
2789 deferred_change_list
= next
;
2792 /* Get rid of all of the cselib based store_infos in this
2793 block and mark the containing insns as not being
2795 ptr
= bb_info
->last_insn
;
2798 if (ptr
->contains_cselib_groups
)
2800 store_info_t s_info
= ptr
->store_rec
;
2801 while (s_info
&& !s_info
->is_set
)
2802 s_info
= s_info
->next
;
2804 && s_info
->redundant_reason
2805 && s_info
->redundant_reason
->insn
2806 && !ptr
->cannot_delete
)
2808 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2809 fprintf (dump_file
, "Locally deleting insn %d "
2810 "because insn %d stores the "
2811 "same value and couldn't be "
2813 INSN_UID (ptr
->insn
),
2814 INSN_UID (s_info
->redundant_reason
->insn
));
2815 delete_dead_store_insn (ptr
);
2817 free_store_info (ptr
);
2821 store_info_t s_info
;
2823 /* Free at least positions_needed bitmaps. */
2824 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2825 if (s_info
->is_large
)
2827 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2828 s_info
->is_large
= false;
2831 ptr
= ptr
->prev_insn
;
2834 free_alloc_pool (cse_store_info_pool
);
2836 bb_info
->regs_live
= NULL
;
2839 BITMAP_FREE (regs_live
);
2841 rtx_group_table
.empty ();
2845 /*----------------------------------------------------------------------------
2848 Assign each byte position in the stores that we are going to
2849 analyze globally to a position in the bitmaps. Returns true if
2850 there are any bit positions assigned.
2851 ----------------------------------------------------------------------------*/
2854 dse_step2_init (void)
2859 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2861 /* For all non stack related bases, we only consider a store to
2862 be deletable if there are two or more stores for that
2863 position. This is because it takes one store to make the
2864 other store redundant. However, for the stores that are
2865 stack related, we consider them if there is only one store
2866 for the position. We do this because the stack related
2867 stores can be deleted if their is no read between them and
2868 the end of the function.
2870 To make this work in the current framework, we take the stack
2871 related bases add all of the bits from store1 into store2.
2872 This has the effect of making the eligible even if there is
2875 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2877 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2878 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2879 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2880 fprintf (dump_file
, "group %d is frame related ", i
);
2883 group
->offset_map_size_n
++;
2884 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2885 group
->offset_map_size_n
);
2886 group
->offset_map_size_p
++;
2887 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2888 group
->offset_map_size_p
);
2889 group
->process_globally
= false;
2890 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2892 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2893 (int)bitmap_count_bits (group
->store2_n
),
2894 (int)bitmap_count_bits (group
->store2_p
));
2895 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2896 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2902 /* Init the offset tables for the normal case. */
2905 dse_step2_nospill (void)
2909 /* Position 0 is unused because 0 is used in the maps to mean
2911 current_position
= 1;
2912 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
2917 if (group
== clear_alias_group
)
2920 memset (group
->offset_map_n
, 0, sizeof (int) * group
->offset_map_size_n
);
2921 memset (group
->offset_map_p
, 0, sizeof (int) * group
->offset_map_size_p
);
2922 bitmap_clear (group
->group_kill
);
2924 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2926 bitmap_set_bit (group
->group_kill
, current_position
);
2927 if (bitmap_bit_p (group
->escaped_n
, j
))
2928 bitmap_set_bit (kill_on_calls
, current_position
);
2929 group
->offset_map_n
[j
] = current_position
++;
2930 group
->process_globally
= true;
2932 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2934 bitmap_set_bit (group
->group_kill
, current_position
);
2935 if (bitmap_bit_p (group
->escaped_p
, j
))
2936 bitmap_set_bit (kill_on_calls
, current_position
);
2937 group
->offset_map_p
[j
] = current_position
++;
2938 group
->process_globally
= true;
2941 return current_position
!= 1;
2946 /*----------------------------------------------------------------------------
2949 Build the bit vectors for the transfer functions.
2950 ----------------------------------------------------------------------------*/
2953 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2957 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
2961 HOST_WIDE_INT offset_p
= -offset
;
2962 if (offset_p
>= group_info
->offset_map_size_n
)
2964 return group_info
->offset_map_n
[offset_p
];
2968 if (offset
>= group_info
->offset_map_size_p
)
2970 return group_info
->offset_map_p
[offset
];
2975 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2979 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
2984 group_info_t group_info
2985 = rtx_group_vec
[store_info
->group_id
];
2986 if (group_info
->process_globally
)
2987 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
2989 int index
= get_bitmap_index (group_info
, i
);
2992 bitmap_set_bit (gen
, index
);
2994 bitmap_clear_bit (kill
, index
);
2997 store_info
= store_info
->next
;
3002 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3006 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3010 if (store_info
->alias_set
)
3012 int index
= get_bitmap_index (clear_alias_group
,
3013 store_info
->alias_set
);
3016 bitmap_set_bit (gen
, index
);
3018 bitmap_clear_bit (kill
, index
);
3021 store_info
= store_info
->next
;
3026 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3030 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3032 read_info_t read_info
= insn_info
->read_rec
;
3036 /* If this insn reads the frame, kill all the frame related stores. */
3037 if (insn_info
->frame_read
)
3039 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3040 if (group
->process_globally
&& group
->frame_related
)
3043 bitmap_ior_into (kill
, group
->group_kill
);
3044 bitmap_and_compl_into (gen
, group
->group_kill
);
3047 if (insn_info
->non_frame_wild_read
)
3049 /* Kill all non-frame related stores. Kill all stores of variables that
3052 bitmap_ior_into (kill
, kill_on_calls
);
3053 bitmap_and_compl_into (gen
, kill_on_calls
);
3054 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3055 if (group
->process_globally
&& !group
->frame_related
)
3058 bitmap_ior_into (kill
, group
->group_kill
);
3059 bitmap_and_compl_into (gen
, group
->group_kill
);
3064 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3066 if (group
->process_globally
)
3068 if (i
== read_info
->group_id
)
3070 if (read_info
->begin
> read_info
->end
)
3072 /* Begin > end for block mode reads. */
3074 bitmap_ior_into (kill
, group
->group_kill
);
3075 bitmap_and_compl_into (gen
, group
->group_kill
);
3079 /* The groups are the same, just process the
3082 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3084 int index
= get_bitmap_index (group
, j
);
3088 bitmap_set_bit (kill
, index
);
3089 bitmap_clear_bit (gen
, index
);
3096 /* The groups are different, if the alias sets
3097 conflict, clear the entire group. We only need
3098 to apply this test if the read_info is a cselib
3099 read. Anything with a constant base cannot alias
3100 something else with a different constant
3102 if ((read_info
->group_id
< 0)
3103 && canon_true_dependence (group
->base_mem
,
3104 GET_MODE (group
->base_mem
),
3105 group
->canon_base_addr
,
3106 read_info
->mem
, NULL_RTX
))
3109 bitmap_ior_into (kill
, group
->group_kill
);
3110 bitmap_and_compl_into (gen
, group
->group_kill
);
3116 read_info
= read_info
->next
;
3120 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3124 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3128 if (read_info
->alias_set
)
3130 int index
= get_bitmap_index (clear_alias_group
,
3131 read_info
->alias_set
);
3135 bitmap_set_bit (kill
, index
);
3136 bitmap_clear_bit (gen
, index
);
3140 read_info
= read_info
->next
;
3145 /* Return the insn in BB_INFO before the first wild read or if there
3146 are no wild reads in the block, return the last insn. */
3149 find_insn_before_first_wild_read (bb_info_t bb_info
)
3151 insn_info_t insn_info
= bb_info
->last_insn
;
3152 insn_info_t last_wild_read
= NULL
;
3156 if (insn_info
->wild_read
)
3158 last_wild_read
= insn_info
->prev_insn
;
3159 /* Block starts with wild read. */
3160 if (!last_wild_read
)
3164 insn_info
= insn_info
->prev_insn
;
3168 return last_wild_read
;
3170 return bb_info
->last_insn
;
3174 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3175 the block in order to build the gen and kill sets for the block.
3176 We start at ptr which may be the last insn in the block or may be
3177 the first insn with a wild read. In the latter case we are able to
3178 skip the rest of the block because it just does not matter:
3179 anything that happens is hidden by the wild read. */
3182 dse_step3_scan (bool for_spills
, basic_block bb
)
3184 bb_info_t bb_info
= bb_table
[bb
->index
];
3185 insn_info_t insn_info
;
3188 /* There are no wild reads in the spill case. */
3189 insn_info
= bb_info
->last_insn
;
3191 insn_info
= find_insn_before_first_wild_read (bb_info
);
3193 /* In the spill case or in the no_spill case if there is no wild
3194 read in the block, we will need a kill set. */
3195 if (insn_info
== bb_info
->last_insn
)
3198 bitmap_clear (bb_info
->kill
);
3200 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3204 BITMAP_FREE (bb_info
->kill
);
3208 /* There may have been code deleted by the dce pass run before
3210 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3212 /* Process the read(s) last. */
3215 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3216 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3220 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3221 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3225 insn_info
= insn_info
->prev_insn
;
3230 /* Set the gen set of the exit block, and also any block with no
3231 successors that does not have a wild read. */
3234 dse_step3_exit_block_scan (bb_info_t bb_info
)
3236 /* The gen set is all 0's for the exit block except for the
3237 frame_pointer_group. */
3239 if (stores_off_frame_dead_at_return
)
3244 FOR_EACH_VEC_ELT (rtx_group_vec
, i
, group
)
3246 if (group
->process_globally
&& group
->frame_related
)
3247 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3253 /* Find all of the blocks that are not backwards reachable from the
3254 exit block or any block with no successors (BB). These are the
3255 infinite loops or infinite self loops. These blocks will still
3256 have their bits set in UNREACHABLE_BLOCKS. */
3259 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3264 if (bitmap_bit_p (unreachable_blocks
, bb
->index
))
3266 bitmap_clear_bit (unreachable_blocks
, bb
->index
);
3267 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3269 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3274 /* Build the transfer functions for the function. */
3277 dse_step3 (bool for_spills
)
3280 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block
);
3281 sbitmap_iterator sbi
;
3282 bitmap all_ones
= NULL
;
3285 bitmap_ones (unreachable_blocks
);
3289 bb_info_t bb_info
= bb_table
[bb
->index
];
3291 bitmap_clear (bb_info
->gen
);
3293 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3295 if (bb
->index
== ENTRY_BLOCK
)
3297 else if (bb
->index
== EXIT_BLOCK
)
3298 dse_step3_exit_block_scan (bb_info
);
3300 dse_step3_scan (for_spills
, bb
);
3301 if (EDGE_COUNT (bb
->succs
) == 0)
3302 mark_reachable_blocks (unreachable_blocks
, bb
);
3304 /* If this is the second time dataflow is run, delete the old
3307 BITMAP_FREE (bb_info
->in
);
3309 BITMAP_FREE (bb_info
->out
);
3312 /* For any block in an infinite loop, we must initialize the out set
3313 to all ones. This could be expensive, but almost never occurs in
3314 practice. However, it is common in regression tests. */
3315 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks
, 0, i
, sbi
)
3317 if (bitmap_bit_p (all_blocks
, i
))
3319 bb_info_t bb_info
= bb_table
[i
];
3325 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3326 FOR_EACH_VEC_ELT (rtx_group_vec
, j
, group
)
3327 bitmap_ior_into (all_ones
, group
->group_kill
);
3331 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3332 bitmap_copy (bb_info
->out
, all_ones
);
3338 BITMAP_FREE (all_ones
);
3339 sbitmap_free (unreachable_blocks
);
3344 /*----------------------------------------------------------------------------
3347 Solve the bitvector equations.
3348 ----------------------------------------------------------------------------*/
3351 /* Confluence function for blocks with no successors. Create an out
3352 set from the gen set of the exit block. This block logically has
3353 the exit block as a successor. */
3358 dse_confluence_0 (basic_block bb
)
3360 bb_info_t bb_info
= bb_table
[bb
->index
];
3362 if (bb
->index
== EXIT_BLOCK
)
3367 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3368 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3372 /* Propagate the information from the in set of the dest of E to the
3373 out set of the src of E. If the various in or out sets are not
3374 there, that means they are all ones. */
3377 dse_confluence_n (edge e
)
3379 bb_info_t src_info
= bb_table
[e
->src
->index
];
3380 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3385 bitmap_and_into (src_info
->out
, dest_info
->in
);
3388 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3389 bitmap_copy (src_info
->out
, dest_info
->in
);
3396 /* Propagate the info from the out to the in set of BB_INDEX's basic
3397 block. There are three cases:
3399 1) The block has no kill set. In this case the kill set is all
3400 ones. It does not matter what the out set of the block is, none of
3401 the info can reach the top. The only thing that reaches the top is
3402 the gen set and we just copy the set.
3404 2) There is a kill set but no out set and bb has successors. In
3405 this case we just return. Eventually an out set will be created and
3406 it is better to wait than to create a set of ones.
3408 3) There is both a kill and out set. We apply the obvious transfer
3413 dse_transfer_function (int bb_index
)
3415 bb_info_t bb_info
= bb_table
[bb_index
];
3423 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3424 bb_info
->out
, bb_info
->kill
);
3427 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3428 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3429 bb_info
->out
, bb_info
->kill
);
3439 /* Case 1 above. If there is already an in set, nothing
3445 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3446 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3452 /* Solve the dataflow equations. */
3457 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3458 dse_confluence_n
, dse_transfer_function
,
3459 all_blocks
, df_get_postorder (DF_BACKWARD
),
3460 df_get_n_blocks (DF_BACKWARD
));
3461 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3465 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3468 bb_info_t bb_info
= bb_table
[bb
->index
];
3470 df_print_bb_index (bb
, dump_file
);
3472 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3474 fprintf (dump_file
, " in: *MISSING*\n");
3476 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3478 fprintf (dump_file
, " gen: *MISSING*\n");
3480 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3482 fprintf (dump_file
, " kill: *MISSING*\n");
3484 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3486 fprintf (dump_file
, " out: *MISSING*\n\n");
3493 /*----------------------------------------------------------------------------
3496 Delete the stores that can only be deleted using the global information.
3497 ----------------------------------------------------------------------------*/
3501 dse_step5_nospill (void)
3506 bb_info_t bb_info
= bb_table
[bb
->index
];
3507 insn_info_t insn_info
= bb_info
->last_insn
;
3508 bitmap v
= bb_info
->out
;
3512 bool deleted
= false;
3513 if (dump_file
&& insn_info
->insn
)
3515 fprintf (dump_file
, "starting to process insn %d\n",
3516 INSN_UID (insn_info
->insn
));
3517 bitmap_print (dump_file
, v
, " v: ", "\n");
3520 /* There may have been code deleted by the dce pass run before
3523 && INSN_P (insn_info
->insn
)
3524 && (!insn_info
->cannot_delete
)
3525 && (!bitmap_empty_p (v
)))
3527 store_info_t store_info
= insn_info
->store_rec
;
3529 /* Try to delete the current insn. */
3532 /* Skip the clobbers. */
3533 while (!store_info
->is_set
)
3534 store_info
= store_info
->next
;
3536 if (store_info
->alias_set
)
3541 group_info_t group_info
3542 = rtx_group_vec
[store_info
->group_id
];
3544 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3546 int index
= get_bitmap_index (group_info
, i
);
3548 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3549 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3550 if (index
== 0 || !bitmap_bit_p (v
, index
))
3552 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3553 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3562 && check_for_inc_dec_1 (insn_info
))
3564 delete_insn (insn_info
->insn
);
3565 insn_info
->insn
= NULL
;
3570 /* We do want to process the local info if the insn was
3571 deleted. For instance, if the insn did a wild read, we
3572 no longer need to trash the info. */
3574 && INSN_P (insn_info
->insn
)
3577 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3578 if (insn_info
->wild_read
)
3580 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3581 fprintf (dump_file
, "wild read\n");
3584 else if (insn_info
->read_rec
3585 || insn_info
->non_frame_wild_read
)
3587 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3588 fprintf (dump_file
, "regular read\n");
3589 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3590 fprintf (dump_file
, "non-frame wild read\n");
3591 scan_reads_nospill (insn_info
, v
, NULL
);
3595 insn_info
= insn_info
->prev_insn
;
3602 /*----------------------------------------------------------------------------
3605 Delete stores made redundant by earlier stores (which store the same
3606 value) that couldn't be eliminated.
3607 ----------------------------------------------------------------------------*/
3616 bb_info_t bb_info
= bb_table
[bb
->index
];
3617 insn_info_t insn_info
= bb_info
->last_insn
;
3621 /* There may have been code deleted by the dce pass run before
3624 && INSN_P (insn_info
->insn
)
3625 && !insn_info
->cannot_delete
)
3627 store_info_t s_info
= insn_info
->store_rec
;
3629 while (s_info
&& !s_info
->is_set
)
3630 s_info
= s_info
->next
;
3632 && s_info
->redundant_reason
3633 && s_info
->redundant_reason
->insn
3634 && INSN_P (s_info
->redundant_reason
->insn
))
3636 rtx rinsn
= s_info
->redundant_reason
->insn
;
3637 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3638 fprintf (dump_file
, "Locally deleting insn %d "
3639 "because insn %d stores the "
3640 "same value and couldn't be "
3642 INSN_UID (insn_info
->insn
),
3644 delete_dead_store_insn (insn_info
);
3647 insn_info
= insn_info
->prev_insn
;
3652 /*----------------------------------------------------------------------------
3655 Destroy everything left standing.
3656 ----------------------------------------------------------------------------*/
3661 bitmap_obstack_release (&dse_bitmap_obstack
);
3662 obstack_free (&dse_obstack
, NULL
);
3664 end_alias_analysis ();
3666 rtx_group_table
.dispose ();
3667 rtx_group_vec
.release ();
3668 BITMAP_FREE (all_blocks
);
3669 BITMAP_FREE (scratch
);
3671 free_alloc_pool (rtx_store_info_pool
);
3672 free_alloc_pool (read_info_pool
);
3673 free_alloc_pool (insn_info_pool
);
3674 free_alloc_pool (bb_info_pool
);
3675 free_alloc_pool (rtx_group_info_pool
);
3676 free_alloc_pool (deferred_change_pool
);
3680 /* -------------------------------------------------------------------------
3682 ------------------------------------------------------------------------- */
3684 /* Callback for running pass_rtl_dse. */
3687 rest_of_handle_dse (void)
3689 df_set_flags (DF_DEFER_INSN_RESCAN
);
3691 /* Need the notes since we must track live hardregs in the forwards
3693 df_note_add_problem ();
3699 if (dse_step2_nospill ())
3701 df_set_flags (DF_LR_RUN_DCE
);
3703 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3704 fprintf (dump_file
, "doing global processing\n");
3707 dse_step5_nospill ();
3714 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3715 locally_deleted
, globally_deleted
, spill_deleted
);
3722 return optimize
> 0 && flag_dse
3729 return optimize
> 0 && flag_dse
3735 const pass_data pass_data_rtl_dse1
=
3737 RTL_PASS
, /* type */
3739 OPTGROUP_NONE
, /* optinfo_flags */
3740 true, /* has_gate */
3741 true, /* has_execute */
3742 TV_DSE1
, /* tv_id */
3743 0, /* properties_required */
3744 0, /* properties_provided */
3745 0, /* properties_destroyed */
3746 0, /* todo_flags_start */
3747 ( TODO_df_finish
| TODO_verify_rtl_sharing
), /* todo_flags_finish */
3750 class pass_rtl_dse1
: public rtl_opt_pass
3753 pass_rtl_dse1 (gcc::context
*ctxt
)
3754 : rtl_opt_pass (pass_data_rtl_dse1
, ctxt
)
3757 /* opt_pass methods: */
3758 bool gate () { return gate_dse1 (); }
3759 unsigned int execute () { return rest_of_handle_dse (); }
3761 }; // class pass_rtl_dse1
3766 make_pass_rtl_dse1 (gcc::context
*ctxt
)
3768 return new pass_rtl_dse1 (ctxt
);
3773 const pass_data pass_data_rtl_dse2
=
3775 RTL_PASS
, /* type */
3777 OPTGROUP_NONE
, /* optinfo_flags */
3778 true, /* has_gate */
3779 true, /* has_execute */
3780 TV_DSE2
, /* tv_id */
3781 0, /* properties_required */
3782 0, /* properties_provided */
3783 0, /* properties_destroyed */
3784 0, /* todo_flags_start */
3785 ( TODO_df_finish
| TODO_verify_rtl_sharing
), /* todo_flags_finish */
3788 class pass_rtl_dse2
: public rtl_opt_pass
3791 pass_rtl_dse2 (gcc::context
*ctxt
)
3792 : rtl_opt_pass (pass_data_rtl_dse2
, ctxt
)
3795 /* opt_pass methods: */
3796 bool gate () { return gate_dse2 (); }
3797 unsigned int execute () { return rest_of_handle_dse (); }
3799 }; // class pass_rtl_dse2
3804 make_pass_rtl_dse2 (gcc::context
*ctxt
)
3806 return new pass_rtl_dse2 (ctxt
);