1 /* RTL dead store elimination.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
5 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
6 and Kenneth Zadeck <zadeck@naturalbridge.com>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
28 #include "coretypes.h"
29 #include "hash-table.h"
35 #include "hard-reg-set.h"
40 #include "tree-pass.h"
41 #include "alloc-pool.h"
43 #include "insn-config.h"
50 #include "tree-flow.h" /* for may_be_aliased */
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
;
553 DEF_VEC_P(group_info_t
);
554 DEF_VEC_ALLOC_P(group_info_t
,heap
);
556 static VEC(group_info_t
,heap
) *rtx_group_vec
;
559 /* This structure holds the set of changes that are being deferred
560 when removing read operation. See replace_read. */
561 struct deferred_change
564 /* The mem that is being replaced. */
567 /* The reg it is being replaced with. */
570 struct deferred_change
*next
;
573 typedef struct deferred_change
*deferred_change_t
;
574 static alloc_pool deferred_change_pool
;
576 static deferred_change_t deferred_change_list
= NULL
;
578 /* This are used to hold the alias sets of spill variables. Since
579 these are never aliased and there may be a lot of them, it makes
580 sense to treat them specially. This bitvector is only allocated in
581 calls from dse_record_singleton_alias_set which currently is only
582 made during reload1. So when dse is called before reload this
583 mechanism does nothing. */
585 static bitmap clear_alias_sets
= NULL
;
587 /* The set of clear_alias_sets that have been disqualified because
588 there are loads or stores using a different mode than the alias set
589 was registered with. */
590 static bitmap disqualified_clear_alias_sets
= NULL
;
592 /* The group that holds all of the clear_alias_sets. */
593 static group_info_t clear_alias_group
;
595 /* The modes of the clear_alias_sets. */
596 static htab_t clear_alias_mode_table
;
598 /* Hash table element to look up the mode for an alias set. */
599 struct clear_alias_mode_holder
601 alias_set_type alias_set
;
602 enum machine_mode mode
;
605 static alloc_pool clear_alias_mode_pool
;
607 /* This is true except if cfun->stdarg -- i.e. we cannot do
608 this for vararg functions because they play games with the frame. */
609 static bool stores_off_frame_dead_at_return
;
611 /* Counter for stats. */
612 static int globally_deleted
;
613 static int locally_deleted
;
614 static int spill_deleted
;
616 static bitmap all_blocks
;
618 /* Locations that are killed by calls in the global phase. */
619 static bitmap kill_on_calls
;
621 /* The number of bits used in the global bitmaps. */
622 static unsigned int current_position
;
625 static bool gate_dse1 (void);
626 static bool gate_dse2 (void);
629 /*----------------------------------------------------------------------------
633 ----------------------------------------------------------------------------*/
636 /* Find the entry associated with ALIAS_SET. */
638 static struct clear_alias_mode_holder
*
639 clear_alias_set_lookup (alias_set_type alias_set
)
641 struct clear_alias_mode_holder tmp_holder
;
644 tmp_holder
.alias_set
= alias_set
;
645 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
648 return (struct clear_alias_mode_holder
*) *slot
;
652 /* Hashtable callbacks for maintaining the "bases" field of
653 store_group_info, given that the addresses are function invariants. */
655 struct invariant_group_base_hasher
: typed_noop_remove
<group_info
>
657 typedef group_info T
;
658 static inline hashval_t
hash (const T
*);
659 static inline bool equal (const T
*, const T
*);
663 invariant_group_base_hasher::equal (const T
*gi1
, const T
*gi2
)
665 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
669 invariant_group_base_hasher::hash (const T
*gi
)
672 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
675 /* Tables of group_info structures, hashed by base value. */
676 static hash_table
<invariant_group_base_hasher
> rtx_group_table
;
679 /* Get the GROUP for BASE. Add a new group if it is not there. */
682 get_group_info (rtx base
)
684 struct group_info tmp_gi
;
690 /* Find the store_base_info structure for BASE, creating a new one
692 tmp_gi
.rtx_base
= base
;
693 slot
= rtx_group_table
.find_slot (&tmp_gi
, INSERT
);
694 gi
= (group_info_t
) *slot
;
698 if (!clear_alias_group
)
700 clear_alias_group
= gi
=
701 (group_info_t
) pool_alloc (rtx_group_info_pool
);
702 memset (gi
, 0, sizeof (struct group_info
));
703 gi
->id
= rtx_group_next_id
++;
704 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
705 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
706 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
707 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
708 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
709 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
710 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
711 gi
->process_globally
= false;
712 gi
->offset_map_size_n
= 0;
713 gi
->offset_map_size_p
= 0;
714 gi
->offset_map_n
= NULL
;
715 gi
->offset_map_p
= NULL
;
716 VEC_safe_push (group_info_t
, heap
, rtx_group_vec
, gi
);
718 return clear_alias_group
;
723 *slot
= gi
= (group_info_t
) pool_alloc (rtx_group_info_pool
);
725 gi
->id
= rtx_group_next_id
++;
726 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
727 gi
->canon_base_addr
= canon_rtx (base
);
728 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
729 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
730 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
731 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
732 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
733 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
734 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
735 gi
->process_globally
= false;
737 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
738 gi
->offset_map_size_n
= 0;
739 gi
->offset_map_size_p
= 0;
740 gi
->offset_map_n
= NULL
;
741 gi
->offset_map_p
= NULL
;
742 VEC_safe_push (group_info_t
, heap
, rtx_group_vec
, gi
);
749 /* Initialization of data structures. */
755 globally_deleted
= 0;
758 bitmap_obstack_initialize (&dse_bitmap_obstack
);
759 gcc_obstack_init (&dse_obstack
);
761 scratch
= BITMAP_ALLOC (®_obstack
);
762 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
765 = create_alloc_pool ("rtx_store_info_pool",
766 sizeof (struct store_info
), 100);
768 = create_alloc_pool ("read_info_pool",
769 sizeof (struct read_info
), 100);
771 = create_alloc_pool ("insn_info_pool",
772 sizeof (struct insn_info
), 100);
774 = create_alloc_pool ("bb_info_pool",
775 sizeof (struct bb_info
), 100);
777 = create_alloc_pool ("rtx_group_info_pool",
778 sizeof (struct group_info
), 100);
780 = create_alloc_pool ("deferred_change_pool",
781 sizeof (struct deferred_change
), 10);
783 rtx_group_table
.create (11);
785 bb_table
= XNEWVEC (bb_info_t
, last_basic_block
);
786 rtx_group_next_id
= 0;
788 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
790 init_alias_analysis ();
792 if (clear_alias_sets
)
793 clear_alias_group
= get_group_info (NULL
);
795 clear_alias_group
= NULL
;
800 /*----------------------------------------------------------------------------
803 Scan all of the insns. Any random ordering of the blocks is fine.
804 Each block is scanned in forward order to accommodate cselib which
805 is used to remove stores with non-constant bases.
806 ----------------------------------------------------------------------------*/
808 /* Delete all of the store_info recs from INSN_INFO. */
811 free_store_info (insn_info_t insn_info
)
813 store_info_t store_info
= insn_info
->store_rec
;
816 store_info_t next
= store_info
->next
;
817 if (store_info
->is_large
)
818 BITMAP_FREE (store_info
->positions_needed
.large
.bmap
);
819 if (store_info
->cse_base
)
820 pool_free (cse_store_info_pool
, store_info
);
822 pool_free (rtx_store_info_pool
, store_info
);
826 insn_info
->cannot_delete
= true;
827 insn_info
->contains_cselib_groups
= false;
828 insn_info
->store_rec
= NULL
;
834 regset fixed_regs_live
;
836 } note_add_store_info
;
838 /* Callback for emit_inc_dec_insn_before via note_stores.
839 Check if a register is clobbered which is live afterwards. */
842 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
845 note_add_store_info
*info
= (note_add_store_info
*) data
;
851 /* If this register is referenced by the current or an earlier insn,
852 that's OK. E.g. this applies to the register that is being incremented
853 with this addition. */
854 for (insn
= info
->first
;
855 insn
!= NEXT_INSN (info
->current
);
856 insn
= NEXT_INSN (insn
))
857 if (reg_referenced_p (loc
, PATTERN (insn
)))
860 /* If we come here, we have a clobber of a register that's only OK
861 if that register is not live. If we don't have liveness information
862 available, fail now. */
863 if (!info
->fixed_regs_live
)
865 info
->failure
= true;
868 /* Now check if this is a live fixed register. */
870 n
= hard_regno_nregs
[r
][GET_MODE (loc
)];
872 if (REGNO_REG_SET_P (info
->fixed_regs_live
, r
+n
))
873 info
->failure
= true;
876 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
877 SRC + SRCOFF before insn ARG. */
880 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
881 rtx op ATTRIBUTE_UNUSED
,
882 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
884 insn_info_t insn_info
= (insn_info_t
) arg
;
885 rtx insn
= insn_info
->insn
, new_insn
, cur
;
886 note_add_store_info info
;
888 /* We can reuse all operands without copying, because we are about
889 to delete the insn that contained it. */
893 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
894 new_insn
= get_insns ();
898 new_insn
= gen_move_insn (dest
, src
);
899 info
.first
= new_insn
;
900 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
901 info
.failure
= false;
902 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
905 note_stores (PATTERN (cur
), note_add_store
, &info
);
908 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
909 return it immediately, communicating the failure to its caller. */
913 emit_insn_before (new_insn
, insn
);
918 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
919 is there, is split into a separate insn.
920 Return true on success (or if there was nothing to do), false on failure. */
923 check_for_inc_dec_1 (insn_info_t insn_info
)
925 rtx insn
= insn_info
->insn
;
926 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
928 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, insn_info
) == 0;
933 /* Entry point for postreload. If you work on reload_cse, or you need this
934 anywhere else, consider if you can provide register liveness information
935 and add a parameter to this function so that it can be passed down in
936 insn_info.fixed_regs_live. */
938 check_for_inc_dec (rtx insn
)
940 struct insn_info insn_info
;
943 insn_info
.insn
= insn
;
944 insn_info
.fixed_regs_live
= NULL
;
945 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
947 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, &insn_info
) == 0;
951 /* Delete the insn and free all of the fields inside INSN_INFO. */
954 delete_dead_store_insn (insn_info_t insn_info
)
956 read_info_t read_info
;
961 if (!check_for_inc_dec_1 (insn_info
))
965 fprintf (dump_file
, "Locally deleting insn %d ",
966 INSN_UID (insn_info
->insn
));
967 if (insn_info
->store_rec
->alias_set
)
968 fprintf (dump_file
, "alias set %d\n",
969 (int) insn_info
->store_rec
->alias_set
);
971 fprintf (dump_file
, "\n");
974 free_store_info (insn_info
);
975 read_info
= insn_info
->read_rec
;
979 read_info_t next
= read_info
->next
;
980 pool_free (read_info_pool
, read_info
);
983 insn_info
->read_rec
= NULL
;
985 delete_insn (insn_info
->insn
);
987 insn_info
->insn
= NULL
;
989 insn_info
->wild_read
= false;
992 /* Check if EXPR can possibly escape the current function scope. */
994 can_escape (tree expr
)
999 base
= get_base_address (expr
);
1001 && !may_be_aliased (base
))
1006 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1007 OFFSET and WIDTH. */
1010 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1014 bool expr_escapes
= can_escape (expr
);
1015 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1016 for (i
=offset
; i
<offset
+width
; i
++)
1024 store1
= group
->store1_n
;
1025 store2
= group
->store2_n
;
1026 escaped
= group
->escaped_n
;
1031 store1
= group
->store1_p
;
1032 store2
= group
->store2_p
;
1033 escaped
= group
->escaped_p
;
1037 if (!bitmap_set_bit (store1
, ai
))
1038 bitmap_set_bit (store2
, ai
);
1043 if (group
->offset_map_size_n
< ai
)
1044 group
->offset_map_size_n
= ai
;
1048 if (group
->offset_map_size_p
< ai
)
1049 group
->offset_map_size_p
= ai
;
1053 bitmap_set_bit (escaped
, ai
);
1058 reset_active_stores (void)
1060 active_local_stores
= NULL
;
1061 active_local_stores_len
= 0;
1064 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1067 free_read_records (bb_info_t bb_info
)
1069 insn_info_t insn_info
= bb_info
->last_insn
;
1070 read_info_t
*ptr
= &insn_info
->read_rec
;
1073 read_info_t next
= (*ptr
)->next
;
1074 if ((*ptr
)->alias_set
== 0)
1076 pool_free (read_info_pool
, *ptr
);
1080 ptr
= &(*ptr
)->next
;
1084 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1087 add_wild_read (bb_info_t bb_info
)
1089 insn_info_t insn_info
= bb_info
->last_insn
;
1090 insn_info
->wild_read
= true;
1091 free_read_records (bb_info
);
1092 reset_active_stores ();
1095 /* Set the BB_INFO so that the last insn is marked as a wild read of
1096 non-frame locations. */
1099 add_non_frame_wild_read (bb_info_t bb_info
)
1101 insn_info_t insn_info
= bb_info
->last_insn
;
1102 insn_info
->non_frame_wild_read
= true;
1103 free_read_records (bb_info
);
1104 reset_active_stores ();
1107 /* Return true if X is a constant or one of the registers that behave
1108 as a constant over the life of a function. This is equivalent to
1109 !rtx_varies_p for memory addresses. */
1112 const_or_frame_p (rtx x
)
1117 if (GET_CODE (x
) == REG
)
1119 /* Note that we have to test for the actual rtx used for the frame
1120 and arg pointers and not just the register number in case we have
1121 eliminated the frame and/or arg pointer and are using it
1123 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1124 /* The arg pointer varies if it is not a fixed register. */
1125 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1126 || x
== pic_offset_table_rtx
)
1134 /* Take all reasonable action to put the address of MEM into the form
1135 that we can do analysis on.
1137 The gold standard is to get the address into the form: address +
1138 OFFSET where address is something that rtx_varies_p considers a
1139 constant. When we can get the address in this form, we can do
1140 global analysis on it. Note that for constant bases, address is
1141 not actually returned, only the group_id. The address can be
1144 If that fails, we try cselib to get a value we can at least use
1145 locally. If that fails we return false.
1147 The GROUP_ID is set to -1 for cselib bases and the index of the
1148 group for non_varying bases.
1150 FOR_READ is true if this is a mem read and false if not. */
1153 canon_address (rtx mem
,
1154 alias_set_type
*alias_set_out
,
1156 HOST_WIDE_INT
*offset
,
1159 enum machine_mode address_mode
= get_address_mode (mem
);
1160 rtx mem_address
= XEXP (mem
, 0);
1161 rtx expanded_address
, address
;
1164 /* Make sure that cselib is has initialized all of the operands of
1165 the address before asking it to do the subst. */
1167 if (clear_alias_sets
)
1169 /* If this is a spill, do not do any further processing. */
1170 alias_set_type alias_set
= MEM_ALIAS_SET (mem
);
1172 fprintf (dump_file
, "found alias set %d\n", (int) alias_set
);
1173 if (bitmap_bit_p (clear_alias_sets
, alias_set
))
1175 struct clear_alias_mode_holder
*entry
1176 = clear_alias_set_lookup (alias_set
);
1178 /* If the modes do not match, we cannot process this set. */
1179 if (entry
->mode
!= GET_MODE (mem
))
1183 "disqualifying alias set %d, (%s) != (%s)\n",
1184 (int) alias_set
, GET_MODE_NAME (entry
->mode
),
1185 GET_MODE_NAME (GET_MODE (mem
)));
1187 bitmap_set_bit (disqualified_clear_alias_sets
, alias_set
);
1191 *alias_set_out
= alias_set
;
1192 *group_id
= clear_alias_group
->id
;
1199 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1203 fprintf (dump_file
, " mem: ");
1204 print_inline_rtx (dump_file
, mem_address
, 0);
1205 fprintf (dump_file
, "\n");
1208 /* First see if just canon_rtx (mem_address) is const or frame,
1209 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1211 for (expanded
= 0; expanded
< 2; expanded
++)
1215 /* Use cselib to replace all of the reg references with the full
1216 expression. This will take care of the case where we have
1218 r_x = base + offset;
1223 val = *(base + offset); */
1225 expanded_address
= cselib_expand_value_rtx (mem_address
,
1228 /* If this fails, just go with the address from first
1230 if (!expanded_address
)
1234 expanded_address
= mem_address
;
1236 /* Split the address into canonical BASE + OFFSET terms. */
1237 address
= canon_rtx (expanded_address
);
1245 fprintf (dump_file
, "\n after cselib_expand address: ");
1246 print_inline_rtx (dump_file
, expanded_address
, 0);
1247 fprintf (dump_file
, "\n");
1250 fprintf (dump_file
, "\n after canon_rtx address: ");
1251 print_inline_rtx (dump_file
, address
, 0);
1252 fprintf (dump_file
, "\n");
1255 if (GET_CODE (address
) == CONST
)
1256 address
= XEXP (address
, 0);
1258 if (GET_CODE (address
) == PLUS
1259 && CONST_INT_P (XEXP (address
, 1)))
1261 *offset
= INTVAL (XEXP (address
, 1));
1262 address
= XEXP (address
, 0);
1265 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1266 && const_or_frame_p (address
))
1268 group_info_t group
= get_group_info (address
);
1271 fprintf (dump_file
, " gid=%d offset=%d \n",
1272 group
->id
, (int)*offset
);
1274 *group_id
= group
->id
;
1279 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1285 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1289 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1290 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1295 /* Clear the rhs field from the active_local_stores array. */
1298 clear_rhs_from_active_local_stores (void)
1300 insn_info_t ptr
= active_local_stores
;
1304 store_info_t store_info
= ptr
->store_rec
;
1305 /* Skip the clobbers. */
1306 while (!store_info
->is_set
)
1307 store_info
= store_info
->next
;
1309 store_info
->rhs
= NULL
;
1310 store_info
->const_rhs
= NULL
;
1312 ptr
= ptr
->next_local_store
;
1317 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1320 set_position_unneeded (store_info_t s_info
, int pos
)
1322 if (__builtin_expect (s_info
->is_large
, false))
1324 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1325 s_info
->positions_needed
.large
.count
++;
1328 s_info
->positions_needed
.small_bitmask
1329 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1332 /* Mark the whole store S_INFO as unneeded. */
1335 set_all_positions_unneeded (store_info_t s_info
)
1337 if (__builtin_expect (s_info
->is_large
, false))
1339 int pos
, end
= s_info
->end
- s_info
->begin
;
1340 for (pos
= 0; pos
< end
; pos
++)
1341 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1342 s_info
->positions_needed
.large
.count
= end
;
1345 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1348 /* Return TRUE if any bytes from S_INFO store are needed. */
1351 any_positions_needed_p (store_info_t s_info
)
1353 if (__builtin_expect (s_info
->is_large
, false))
1354 return (s_info
->positions_needed
.large
.count
1355 < s_info
->end
- s_info
->begin
);
1357 return (s_info
->positions_needed
.small_bitmask
1358 != (unsigned HOST_WIDE_INT
) 0);
1361 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1362 store are needed. */
1365 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1367 if (__builtin_expect (s_info
->is_large
, false))
1369 int end
= start
+ width
;
1371 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1377 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1378 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1383 static rtx
get_stored_val (store_info_t
, enum machine_mode
, HOST_WIDE_INT
,
1384 HOST_WIDE_INT
, basic_block
, bool);
1387 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1388 there is a candidate store, after adding it to the appropriate
1389 local store group if so. */
1392 record_store (rtx body
, bb_info_t bb_info
)
1394 rtx mem
, rhs
, const_rhs
, mem_addr
;
1395 HOST_WIDE_INT offset
= 0;
1396 HOST_WIDE_INT width
= 0;
1397 alias_set_type spill_alias_set
;
1398 insn_info_t insn_info
= bb_info
->last_insn
;
1399 store_info_t store_info
= NULL
;
1401 cselib_val
*base
= NULL
;
1402 insn_info_t ptr
, last
, redundant_reason
;
1403 bool store_is_unused
;
1405 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1408 mem
= SET_DEST (body
);
1410 /* If this is not used, then this cannot be used to keep the insn
1411 from being deleted. On the other hand, it does provide something
1412 that can be used to prove that another store is dead. */
1414 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1416 /* Check whether that value is a suitable memory location. */
1419 /* If the set or clobber is unused, then it does not effect our
1420 ability to get rid of the entire insn. */
1421 if (!store_is_unused
)
1422 insn_info
->cannot_delete
= true;
1426 /* At this point we know mem is a mem. */
1427 if (GET_MODE (mem
) == BLKmode
)
1429 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1432 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1433 add_wild_read (bb_info
);
1434 insn_info
->cannot_delete
= true;
1437 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1438 as memset (addr, 0, 36); */
1439 else if (!MEM_SIZE_KNOWN_P (mem
)
1440 || MEM_SIZE (mem
) <= 0
1441 || MEM_SIZE (mem
) > MAX_OFFSET
1442 || GET_CODE (body
) != SET
1443 || !CONST_INT_P (SET_SRC (body
)))
1445 if (!store_is_unused
)
1447 /* If the set or clobber is unused, then it does not effect our
1448 ability to get rid of the entire insn. */
1449 insn_info
->cannot_delete
= true;
1450 clear_rhs_from_active_local_stores ();
1456 /* We can still process a volatile mem, we just cannot delete it. */
1457 if (MEM_VOLATILE_P (mem
))
1458 insn_info
->cannot_delete
= true;
1460 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1462 clear_rhs_from_active_local_stores ();
1466 if (GET_MODE (mem
) == BLKmode
)
1467 width
= MEM_SIZE (mem
);
1470 width
= GET_MODE_SIZE (GET_MODE (mem
));
1471 gcc_assert ((unsigned) width
<= HOST_BITS_PER_WIDE_INT
);
1474 if (spill_alias_set
)
1476 bitmap store1
= clear_alias_group
->store1_p
;
1477 bitmap store2
= clear_alias_group
->store2_p
;
1479 gcc_assert (GET_MODE (mem
) != BLKmode
);
1481 if (!bitmap_set_bit (store1
, spill_alias_set
))
1482 bitmap_set_bit (store2
, spill_alias_set
);
1484 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1485 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1487 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1490 fprintf (dump_file
, " processing spill store %d(%s)\n",
1491 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1493 else if (group_id
>= 0)
1495 /* In the restrictive case where the base is a constant or the
1496 frame pointer we can do global analysis. */
1499 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
1500 tree expr
= MEM_EXPR (mem
);
1502 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1503 set_usage_bits (group
, offset
, width
, expr
);
1506 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1507 group_id
, (int)offset
, (int)(offset
+width
));
1511 if (may_be_sp_based_p (XEXP (mem
, 0)))
1512 insn_info
->stack_pointer_based
= true;
1513 insn_info
->contains_cselib_groups
= true;
1515 store_info
= (store_info_t
) pool_alloc (cse_store_info_pool
);
1519 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1520 (int)offset
, (int)(offset
+width
));
1523 const_rhs
= rhs
= NULL_RTX
;
1524 if (GET_CODE (body
) == SET
1525 /* No place to keep the value after ra. */
1526 && !reload_completed
1527 && (REG_P (SET_SRC (body
))
1528 || GET_CODE (SET_SRC (body
)) == SUBREG
1529 || CONSTANT_P (SET_SRC (body
)))
1530 && !MEM_VOLATILE_P (mem
)
1531 /* Sometimes the store and reload is used for truncation and
1533 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1535 rhs
= SET_SRC (body
);
1536 if (CONSTANT_P (rhs
))
1538 else if (body
== PATTERN (insn_info
->insn
))
1540 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1541 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1542 const_rhs
= XEXP (tem
, 0);
1544 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1546 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1548 if (tem
&& CONSTANT_P (tem
))
1553 /* Check to see if this stores causes some other stores to be
1555 ptr
= active_local_stores
;
1557 redundant_reason
= NULL
;
1558 mem
= canon_rtx (mem
);
1559 /* For alias_set != 0 canon_true_dependence should be never called. */
1560 if (spill_alias_set
)
1561 mem_addr
= NULL_RTX
;
1565 mem_addr
= base
->val_rtx
;
1569 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
1570 mem_addr
= group
->canon_base_addr
;
1573 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1578 insn_info_t next
= ptr
->next_local_store
;
1579 store_info_t s_info
= ptr
->store_rec
;
1582 /* Skip the clobbers. We delete the active insn if this insn
1583 shadows the set. To have been put on the active list, it
1584 has exactly on set. */
1585 while (!s_info
->is_set
)
1586 s_info
= s_info
->next
;
1588 if (s_info
->alias_set
!= spill_alias_set
)
1590 else if (s_info
->alias_set
)
1592 struct clear_alias_mode_holder
*entry
1593 = clear_alias_set_lookup (s_info
->alias_set
);
1594 /* Generally, spills cannot be processed if and of the
1595 references to the slot have a different mode. But if
1596 we are in the same block and mode is exactly the same
1597 between this store and one before in the same block,
1598 we can still delete it. */
1599 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1600 && (GET_MODE (mem
) == entry
->mode
))
1603 set_all_positions_unneeded (s_info
);
1606 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1607 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1609 else if ((s_info
->group_id
== group_id
)
1610 && (s_info
->cse_base
== base
))
1614 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1615 INSN_UID (ptr
->insn
), s_info
->group_id
,
1616 (int)s_info
->begin
, (int)s_info
->end
);
1618 /* Even if PTR won't be eliminated as unneeded, if both
1619 PTR and this insn store the same constant value, we might
1620 eliminate this insn instead. */
1621 if (s_info
->const_rhs
1623 && offset
>= s_info
->begin
1624 && offset
+ width
<= s_info
->end
1625 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1628 if (GET_MODE (mem
) == BLKmode
)
1630 if (GET_MODE (s_info
->mem
) == BLKmode
1631 && s_info
->const_rhs
== const_rhs
)
1632 redundant_reason
= ptr
;
1634 else if (s_info
->const_rhs
== const0_rtx
1635 && const_rhs
== const0_rtx
)
1636 redundant_reason
= ptr
;
1641 val
= get_stored_val (s_info
, GET_MODE (mem
),
1642 offset
, offset
+ width
,
1643 BLOCK_FOR_INSN (insn_info
->insn
),
1645 if (get_insns () != NULL
)
1648 if (val
&& rtx_equal_p (val
, const_rhs
))
1649 redundant_reason
= ptr
;
1653 for (i
= MAX (offset
, s_info
->begin
);
1654 i
< offset
+ width
&& i
< s_info
->end
;
1656 set_position_unneeded (s_info
, i
- s_info
->begin
);
1658 else if (s_info
->rhs
)
1659 /* Need to see if it is possible for this store to overwrite
1660 the value of store_info. If it is, set the rhs to NULL to
1661 keep it from being used to remove a load. */
1663 if (canon_true_dependence (s_info
->mem
,
1664 GET_MODE (s_info
->mem
),
1669 s_info
->const_rhs
= NULL
;
1673 /* An insn can be deleted if every position of every one of
1674 its s_infos is zero. */
1675 if (any_positions_needed_p (s_info
))
1680 insn_info_t insn_to_delete
= ptr
;
1682 active_local_stores_len
--;
1684 last
->next_local_store
= ptr
->next_local_store
;
1686 active_local_stores
= ptr
->next_local_store
;
1688 if (!insn_to_delete
->cannot_delete
)
1689 delete_dead_store_insn (insn_to_delete
);
1697 /* Finish filling in the store_info. */
1698 store_info
->next
= insn_info
->store_rec
;
1699 insn_info
->store_rec
= store_info
;
1700 store_info
->mem
= mem
;
1701 store_info
->alias_set
= spill_alias_set
;
1702 store_info
->mem_addr
= mem_addr
;
1703 store_info
->cse_base
= base
;
1704 if (width
> HOST_BITS_PER_WIDE_INT
)
1706 store_info
->is_large
= true;
1707 store_info
->positions_needed
.large
.count
= 0;
1708 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1712 store_info
->is_large
= false;
1713 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1715 store_info
->group_id
= group_id
;
1716 store_info
->begin
= offset
;
1717 store_info
->end
= offset
+ width
;
1718 store_info
->is_set
= GET_CODE (body
) == SET
;
1719 store_info
->rhs
= rhs
;
1720 store_info
->const_rhs
= const_rhs
;
1721 store_info
->redundant_reason
= redundant_reason
;
1723 /* If this is a clobber, we return 0. We will only be able to
1724 delete this insn if there is only one store USED store, but we
1725 can use the clobber to delete other stores earlier. */
1726 return store_info
->is_set
? 1 : 0;
1731 dump_insn_info (const char * start
, insn_info_t insn_info
)
1733 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1734 INSN_UID (insn_info
->insn
),
1735 insn_info
->store_rec
? "has store" : "naked");
1739 /* If the modes are different and the value's source and target do not
1740 line up, we need to extract the value from lower part of the rhs of
1741 the store, shift it, and then put it into a form that can be shoved
1742 into the read_insn. This function generates a right SHIFT of a
1743 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1744 shift sequence is returned or NULL if we failed to find a
1748 find_shift_sequence (int access_size
,
1749 store_info_t store_info
,
1750 enum machine_mode read_mode
,
1751 int shift
, bool speed
, bool require_cst
)
1753 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1754 enum machine_mode new_mode
;
1755 rtx read_reg
= NULL
;
1757 /* Some machines like the x86 have shift insns for each size of
1758 operand. Other machines like the ppc or the ia-64 may only have
1759 shift insns that shift values within 32 or 64 bit registers.
1760 This loop tries to find the smallest shift insn that will right
1761 justify the value we want to read but is available in one insn on
1764 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1766 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1767 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1769 rtx target
, new_reg
, shift_seq
, insn
, new_lhs
;
1772 /* If a constant was stored into memory, try to simplify it here,
1773 otherwise the cost of the shift might preclude this optimization
1774 e.g. at -Os, even when no actual shift will be needed. */
1775 if (store_info
->const_rhs
)
1777 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1778 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1780 if (ret
&& CONSTANT_P (ret
))
1782 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1783 ret
, GEN_INT (shift
));
1784 if (ret
&& CONSTANT_P (ret
))
1786 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1787 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1788 if (ret
&& CONSTANT_P (ret
)
1789 && set_src_cost (ret
, speed
) <= COSTS_N_INSNS (1))
1798 /* Try a wider mode if truncating the store mode to NEW_MODE
1799 requires a real instruction. */
1800 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1801 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1804 /* Also try a wider mode if the necessary punning is either not
1805 desirable or not possible. */
1806 if (!CONSTANT_P (store_info
->rhs
)
1807 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1810 new_reg
= gen_reg_rtx (new_mode
);
1814 /* In theory we could also check for an ashr. Ian Taylor knows
1815 of one dsp where the cost of these two was not the same. But
1816 this really is a rare case anyway. */
1817 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1818 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1820 shift_seq
= get_insns ();
1823 if (target
!= new_reg
|| shift_seq
== NULL
)
1827 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1829 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1831 /* The computation up to here is essentially independent
1832 of the arguments and could be precomputed. It may
1833 not be worth doing so. We could precompute if
1834 worthwhile or at least cache the results. The result
1835 technically depends on both SHIFT and ACCESS_SIZE,
1836 but in practice the answer will depend only on ACCESS_SIZE. */
1838 if (cost
> COSTS_N_INSNS (1))
1841 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1842 copy_rtx (store_info
->rhs
));
1843 if (new_lhs
== NULL_RTX
)
1846 /* We found an acceptable shift. Generate a move to
1847 take the value from the store and put it into the
1848 shift pseudo, then shift it, then generate another
1849 move to put in into the target of the read. */
1850 emit_move_insn (new_reg
, new_lhs
);
1851 emit_insn (shift_seq
);
1852 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1860 /* Call back for note_stores to find the hard regs set or clobbered by
1861 insn. Data is a bitmap of the hardregs set so far. */
1864 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1866 bitmap regs_set
= (bitmap
) data
;
1869 && HARD_REGISTER_P (x
))
1871 unsigned int regno
= REGNO (x
);
1872 bitmap_set_range (regs_set
, regno
,
1873 hard_regno_nregs
[regno
][GET_MODE (x
)]);
1877 /* Helper function for replace_read and record_store.
1878 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1879 to one before READ_END bytes read in READ_MODE. Return NULL
1880 if not successful. If REQUIRE_CST is true, return always constant. */
1883 get_stored_val (store_info_t store_info
, enum machine_mode read_mode
,
1884 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1885 basic_block bb
, bool require_cst
)
1887 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1889 int access_size
; /* In bytes. */
1892 /* To get here the read is within the boundaries of the write so
1893 shift will never be negative. Start out with the shift being in
1895 if (store_mode
== BLKmode
)
1897 else if (BYTES_BIG_ENDIAN
)
1898 shift
= store_info
->end
- read_end
;
1900 shift
= read_begin
- store_info
->begin
;
1902 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1904 /* From now on it is bits. */
1905 shift
*= BITS_PER_UNIT
;
1908 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1909 optimize_bb_for_speed_p (bb
),
1911 else if (store_mode
== BLKmode
)
1913 /* The store is a memset (addr, const_val, const_size). */
1914 gcc_assert (CONST_INT_P (store_info
->rhs
));
1915 store_mode
= int_mode_for_mode (read_mode
);
1916 if (store_mode
== BLKmode
)
1917 read_reg
= NULL_RTX
;
1918 else if (store_info
->rhs
== const0_rtx
)
1919 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1920 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1921 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1922 read_reg
= NULL_RTX
;
1925 unsigned HOST_WIDE_INT c
1926 = INTVAL (store_info
->rhs
)
1927 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1928 int shift
= BITS_PER_UNIT
;
1929 while (shift
< HOST_BITS_PER_WIDE_INT
)
1934 read_reg
= gen_int_mode (c
, store_mode
);
1935 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1938 else if (store_info
->const_rhs
1940 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1941 read_reg
= extract_low_bits (read_mode
, store_mode
,
1942 copy_rtx (store_info
->const_rhs
));
1944 read_reg
= extract_low_bits (read_mode
, store_mode
,
1945 copy_rtx (store_info
->rhs
));
1946 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1947 read_reg
= NULL_RTX
;
1951 /* Take a sequence of:
1974 Depending on the alignment and the mode of the store and
1978 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1979 and READ_INSN are for the read. Return true if the replacement
1983 replace_read (store_info_t store_info
, insn_info_t store_insn
,
1984 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1987 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1988 enum machine_mode read_mode
= GET_MODE (read_info
->mem
);
1989 rtx insns
, this_insn
, read_reg
;
1995 /* Create a sequence of instructions to set up the read register.
1996 This sequence goes immediately before the store and its result
1997 is read by the load.
1999 We need to keep this in perspective. We are replacing a read
2000 with a sequence of insns, but the read will almost certainly be
2001 in cache, so it is not going to be an expensive one. Thus, we
2002 are not willing to do a multi insn shift or worse a subroutine
2003 call to get rid of the read. */
2005 fprintf (dump_file
, "trying to replace %smode load in insn %d"
2006 " from %smode store in insn %d\n",
2007 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
2008 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
2010 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
2011 read_reg
= get_stored_val (store_info
,
2012 read_mode
, read_info
->begin
, read_info
->end
,
2014 if (read_reg
== NULL_RTX
)
2018 fprintf (dump_file
, " -- could not extract bits of stored value\n");
2021 /* Force the value into a new register so that it won't be clobbered
2022 between the store and the load. */
2023 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
2024 insns
= get_insns ();
2027 if (insns
!= NULL_RTX
)
2029 /* Now we have to scan the set of new instructions to see if the
2030 sequence contains and sets of hardregs that happened to be
2031 live at this point. For instance, this can happen if one of
2032 the insns sets the CC and the CC happened to be live at that
2033 point. This does occasionally happen, see PR 37922. */
2034 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2036 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2037 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2039 bitmap_and_into (regs_set
, regs_live
);
2040 if (!bitmap_empty_p (regs_set
))
2045 "abandoning replacement because sequence clobbers live hardregs:");
2046 df_print_regset (dump_file
, regs_set
);
2049 BITMAP_FREE (regs_set
);
2052 BITMAP_FREE (regs_set
);
2055 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2057 deferred_change_t deferred_change
=
2058 (deferred_change_t
) pool_alloc (deferred_change_pool
);
2060 /* Insert this right before the store insn where it will be safe
2061 from later insns that might change it before the read. */
2062 emit_insn_before (insns
, store_insn
->insn
);
2064 /* And now for the kludge part: cselib croaks if you just
2065 return at this point. There are two reasons for this:
2067 1) Cselib has an idea of how many pseudos there are and
2068 that does not include the new ones we just added.
2070 2) Cselib does not know about the move insn we added
2071 above the store_info, and there is no way to tell it
2072 about it, because it has "moved on".
2074 Problem (1) is fixable with a certain amount of engineering.
2075 Problem (2) is requires starting the bb from scratch. This
2078 So we are just going to have to lie. The move/extraction
2079 insns are not really an issue, cselib did not see them. But
2080 the use of the new pseudo read_insn is a real problem because
2081 cselib has not scanned this insn. The way that we solve this
2082 problem is that we are just going to put the mem back for now
2083 and when we are finished with the block, we undo this. We
2084 keep a table of mems to get rid of. At the end of the basic
2085 block we can put them back. */
2087 *loc
= read_info
->mem
;
2088 deferred_change
->next
= deferred_change_list
;
2089 deferred_change_list
= deferred_change
;
2090 deferred_change
->loc
= loc
;
2091 deferred_change
->reg
= read_reg
;
2093 /* Get rid of the read_info, from the point of view of the
2094 rest of dse, play like this read never happened. */
2095 read_insn
->read_rec
= read_info
->next
;
2096 pool_free (read_info_pool
, read_info
);
2099 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2100 print_simple_rtl (dump_file
, read_reg
);
2101 fprintf (dump_file
, "\n");
2109 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2110 print_simple_rtl (dump_file
, read_reg
);
2111 fprintf (dump_file
, " led to an invalid instruction\n");
2117 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2118 if LOC is a mem and if it is look at the address and kill any
2119 appropriate stores that may be active. */
2122 check_mem_read_rtx (rtx
*loc
, void *data
)
2124 rtx mem
= *loc
, mem_addr
;
2126 insn_info_t insn_info
;
2127 HOST_WIDE_INT offset
= 0;
2128 HOST_WIDE_INT width
= 0;
2129 alias_set_type spill_alias_set
= 0;
2130 cselib_val
*base
= NULL
;
2132 read_info_t read_info
;
2134 if (!mem
|| !MEM_P (mem
))
2137 bb_info
= (bb_info_t
) data
;
2138 insn_info
= bb_info
->last_insn
;
2140 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2141 || (MEM_VOLATILE_P (mem
)))
2144 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2145 add_wild_read (bb_info
);
2146 insn_info
->cannot_delete
= true;
2150 /* If it is reading readonly mem, then there can be no conflict with
2152 if (MEM_READONLY_P (mem
))
2155 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2158 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2159 add_wild_read (bb_info
);
2163 if (GET_MODE (mem
) == BLKmode
)
2166 width
= GET_MODE_SIZE (GET_MODE (mem
));
2168 read_info
= (read_info_t
) pool_alloc (read_info_pool
);
2169 read_info
->group_id
= group_id
;
2170 read_info
->mem
= mem
;
2171 read_info
->alias_set
= spill_alias_set
;
2172 read_info
->begin
= offset
;
2173 read_info
->end
= offset
+ width
;
2174 read_info
->next
= insn_info
->read_rec
;
2175 insn_info
->read_rec
= read_info
;
2176 /* For alias_set != 0 canon_true_dependence should be never called. */
2177 if (spill_alias_set
)
2178 mem_addr
= NULL_RTX
;
2182 mem_addr
= base
->val_rtx
;
2186 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
2187 mem_addr
= group
->canon_base_addr
;
2190 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2193 /* We ignore the clobbers in store_info. The is mildly aggressive,
2194 but there really should not be a clobber followed by a read. */
2196 if (spill_alias_set
)
2198 insn_info_t i_ptr
= active_local_stores
;
2199 insn_info_t last
= NULL
;
2202 fprintf (dump_file
, " processing spill load %d\n",
2203 (int) spill_alias_set
);
2207 store_info_t store_info
= i_ptr
->store_rec
;
2209 /* Skip the clobbers. */
2210 while (!store_info
->is_set
)
2211 store_info
= store_info
->next
;
2213 if (store_info
->alias_set
== spill_alias_set
)
2216 dump_insn_info ("removing from active", i_ptr
);
2218 active_local_stores_len
--;
2220 last
->next_local_store
= i_ptr
->next_local_store
;
2222 active_local_stores
= i_ptr
->next_local_store
;
2226 i_ptr
= i_ptr
->next_local_store
;
2229 else if (group_id
>= 0)
2231 /* This is the restricted case where the base is a constant or
2232 the frame pointer and offset is a constant. */
2233 insn_info_t i_ptr
= active_local_stores
;
2234 insn_info_t last
= NULL
;
2239 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2242 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2243 group_id
, (int)offset
, (int)(offset
+width
));
2248 bool remove
= false;
2249 store_info_t store_info
= i_ptr
->store_rec
;
2251 /* Skip the clobbers. */
2252 while (!store_info
->is_set
)
2253 store_info
= store_info
->next
;
2255 /* There are three cases here. */
2256 if (store_info
->group_id
< 0)
2257 /* We have a cselib store followed by a read from a
2260 = canon_true_dependence (store_info
->mem
,
2261 GET_MODE (store_info
->mem
),
2262 store_info
->mem_addr
,
2265 else if (group_id
== store_info
->group_id
)
2267 /* This is a block mode load. We may get lucky and
2268 canon_true_dependence may save the day. */
2271 = canon_true_dependence (store_info
->mem
,
2272 GET_MODE (store_info
->mem
),
2273 store_info
->mem_addr
,
2276 /* If this read is just reading back something that we just
2277 stored, rewrite the read. */
2281 && offset
>= store_info
->begin
2282 && offset
+ width
<= store_info
->end
2283 && all_positions_needed_p (store_info
,
2284 offset
- store_info
->begin
,
2286 && replace_read (store_info
, i_ptr
, read_info
,
2287 insn_info
, loc
, bb_info
->regs_live
))
2290 /* The bases are the same, just see if the offsets
2292 if ((offset
< store_info
->end
)
2293 && (offset
+ width
> store_info
->begin
))
2299 The else case that is missing here is that the
2300 bases are constant but different. There is nothing
2301 to do here because there is no overlap. */
2306 dump_insn_info ("removing from active", i_ptr
);
2308 active_local_stores_len
--;
2310 last
->next_local_store
= i_ptr
->next_local_store
;
2312 active_local_stores
= i_ptr
->next_local_store
;
2316 i_ptr
= i_ptr
->next_local_store
;
2321 insn_info_t i_ptr
= active_local_stores
;
2322 insn_info_t last
= NULL
;
2325 fprintf (dump_file
, " processing cselib load mem:");
2326 print_inline_rtx (dump_file
, mem
, 0);
2327 fprintf (dump_file
, "\n");
2332 bool remove
= false;
2333 store_info_t store_info
= i_ptr
->store_rec
;
2336 fprintf (dump_file
, " processing cselib load against insn %d\n",
2337 INSN_UID (i_ptr
->insn
));
2339 /* Skip the clobbers. */
2340 while (!store_info
->is_set
)
2341 store_info
= store_info
->next
;
2343 /* If this read is just reading back something that we just
2344 stored, rewrite the read. */
2346 && store_info
->group_id
== -1
2347 && store_info
->cse_base
== base
2349 && offset
>= store_info
->begin
2350 && offset
+ width
<= store_info
->end
2351 && all_positions_needed_p (store_info
,
2352 offset
- store_info
->begin
, width
)
2353 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2354 bb_info
->regs_live
))
2357 if (!store_info
->alias_set
)
2358 remove
= canon_true_dependence (store_info
->mem
,
2359 GET_MODE (store_info
->mem
),
2360 store_info
->mem_addr
,
2366 dump_insn_info ("removing from active", i_ptr
);
2368 active_local_stores_len
--;
2370 last
->next_local_store
= i_ptr
->next_local_store
;
2372 active_local_stores
= i_ptr
->next_local_store
;
2376 i_ptr
= i_ptr
->next_local_store
;
2382 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2383 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2384 true for any part of *LOC. */
2387 check_mem_read_use (rtx
*loc
, void *data
)
2389 for_each_rtx (loc
, check_mem_read_rtx
, data
);
2393 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2394 So far it only handles arguments passed in registers. */
2397 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2399 CUMULATIVE_ARGS args_so_far_v
;
2400 cumulative_args_t args_so_far
;
2404 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2405 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2407 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2409 arg
!= void_list_node
&& idx
< nargs
;
2410 arg
= TREE_CHAIN (arg
), idx
++)
2412 enum machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2414 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2415 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2416 || GET_MODE_CLASS (mode
) != MODE_INT
)
2419 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2421 link
= XEXP (link
, 1))
2422 if (GET_CODE (XEXP (link
, 0)) == USE
)
2424 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2425 if (REG_P (args
[idx
])
2426 && REGNO (args
[idx
]) == REGNO (reg
)
2427 && (GET_MODE (args
[idx
]) == mode
2428 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2429 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2431 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2432 > GET_MODE_SIZE (mode
)))))
2438 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2439 if (GET_MODE (args
[idx
]) != mode
)
2441 if (!tmp
|| !CONST_INT_P (tmp
))
2443 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2448 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2450 if (arg
!= void_list_node
|| idx
!= nargs
)
2455 /* Return a bitmap of the fixed registers contained in IN. */
2458 copy_fixed_regs (const_bitmap in
)
2462 ret
= ALLOC_REG_SET (NULL
);
2463 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2467 /* Apply record_store to all candidate stores in INSN. Mark INSN
2468 if some part of it is not a candidate store and assigns to a
2469 non-register target. */
2472 scan_insn (bb_info_t bb_info
, rtx insn
)
2475 insn_info_t insn_info
= (insn_info_t
) pool_alloc (insn_info_pool
);
2477 memset (insn_info
, 0, sizeof (struct insn_info
));
2480 fprintf (dump_file
, "\n**scanning insn=%d\n",
2483 insn_info
->prev_insn
= bb_info
->last_insn
;
2484 insn_info
->insn
= insn
;
2485 bb_info
->last_insn
= insn_info
;
2487 if (DEBUG_INSN_P (insn
))
2489 insn_info
->cannot_delete
= true;
2493 /* Cselib clears the table for this case, so we have to essentially
2495 if (NONJUMP_INSN_P (insn
)
2496 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2497 && MEM_VOLATILE_P (PATTERN (insn
)))
2499 add_wild_read (bb_info
);
2500 insn_info
->cannot_delete
= true;
2504 /* Look at all of the uses in the insn. */
2505 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2510 tree memset_call
= NULL_TREE
;
2512 insn_info
->cannot_delete
= true;
2514 /* Const functions cannot do anything bad i.e. read memory,
2515 however, they can read their parameters which may have
2516 been pushed onto the stack.
2517 memset and bzero don't read memory either. */
2518 const_call
= RTL_CONST_CALL_P (insn
);
2521 rtx call
= PATTERN (insn
);
2522 if (GET_CODE (call
) == PARALLEL
)
2523 call
= XVECEXP (call
, 0, 0);
2524 if (GET_CODE (call
) == SET
)
2525 call
= SET_SRC (call
);
2526 if (GET_CODE (call
) == CALL
2527 && MEM_P (XEXP (call
, 0))
2528 && GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2530 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2531 if (SYMBOL_REF_DECL (symbol
)
2532 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2534 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2536 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2537 == BUILT_IN_MEMSET
))
2538 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2539 memset_call
= SYMBOL_REF_DECL (symbol
);
2543 if (const_call
|| memset_call
)
2545 insn_info_t i_ptr
= active_local_stores
;
2546 insn_info_t last
= NULL
;
2549 fprintf (dump_file
, "%s call %d\n",
2550 const_call
? "const" : "memset", INSN_UID (insn
));
2552 /* See the head comment of the frame_read field. */
2553 if (reload_completed
)
2554 insn_info
->frame_read
= true;
2556 /* Loop over the active stores and remove those which are
2557 killed by the const function call. */
2560 bool remove_store
= false;
2562 /* The stack pointer based stores are always killed. */
2563 if (i_ptr
->stack_pointer_based
)
2564 remove_store
= true;
2566 /* If the frame is read, the frame related stores are killed. */
2567 else if (insn_info
->frame_read
)
2569 store_info_t store_info
= i_ptr
->store_rec
;
2571 /* Skip the clobbers. */
2572 while (!store_info
->is_set
)
2573 store_info
= store_info
->next
;
2575 if (store_info
->group_id
>= 0
2576 && VEC_index (group_info_t
, rtx_group_vec
,
2577 store_info
->group_id
)->frame_related
)
2578 remove_store
= true;
2584 dump_insn_info ("removing from active", i_ptr
);
2586 active_local_stores_len
--;
2588 last
->next_local_store
= i_ptr
->next_local_store
;
2590 active_local_stores
= i_ptr
->next_local_store
;
2595 i_ptr
= i_ptr
->next_local_store
;
2601 if (get_call_args (insn
, memset_call
, args
, 3)
2602 && CONST_INT_P (args
[1])
2603 && CONST_INT_P (args
[2])
2604 && INTVAL (args
[2]) > 0)
2606 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2607 set_mem_size (mem
, INTVAL (args
[2]));
2608 body
= gen_rtx_SET (VOIDmode
, mem
, args
[1]);
2609 mems_found
+= record_store (body
, bb_info
);
2611 fprintf (dump_file
, "handling memset as BLKmode store\n");
2612 if (mems_found
== 1)
2614 if (active_local_stores_len
++
2615 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2617 active_local_stores_len
= 1;
2618 active_local_stores
= NULL
;
2620 insn_info
->fixed_regs_live
2621 = copy_fixed_regs (bb_info
->regs_live
);
2622 insn_info
->next_local_store
= active_local_stores
;
2623 active_local_stores
= insn_info
;
2630 /* Every other call, including pure functions, may read any memory
2631 that is not relative to the frame. */
2632 add_non_frame_wild_read (bb_info
);
2637 /* Assuming that there are sets in these insns, we cannot delete
2639 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2640 || volatile_refs_p (PATTERN (insn
))
2641 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2642 || (RTX_FRAME_RELATED_P (insn
))
2643 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2644 insn_info
->cannot_delete
= true;
2646 body
= PATTERN (insn
);
2647 if (GET_CODE (body
) == PARALLEL
)
2650 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2651 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2654 mems_found
+= record_store (body
, bb_info
);
2657 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2658 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2660 /* If we found some sets of mems, add it into the active_local_stores so
2661 that it can be locally deleted if found dead or used for
2662 replace_read and redundant constant store elimination. Otherwise mark
2663 it as cannot delete. This simplifies the processing later. */
2664 if (mems_found
== 1)
2666 if (active_local_stores_len
++
2667 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2669 active_local_stores_len
= 1;
2670 active_local_stores
= NULL
;
2672 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2673 insn_info
->next_local_store
= active_local_stores
;
2674 active_local_stores
= insn_info
;
2677 insn_info
->cannot_delete
= true;
2681 /* Remove BASE from the set of active_local_stores. This is a
2682 callback from cselib that is used to get rid of the stores in
2683 active_local_stores. */
2686 remove_useless_values (cselib_val
*base
)
2688 insn_info_t insn_info
= active_local_stores
;
2689 insn_info_t last
= NULL
;
2693 store_info_t store_info
= insn_info
->store_rec
;
2696 /* If ANY of the store_infos match the cselib group that is
2697 being deleted, then the insn can not be deleted. */
2700 if ((store_info
->group_id
== -1)
2701 && (store_info
->cse_base
== base
))
2706 store_info
= store_info
->next
;
2711 active_local_stores_len
--;
2713 last
->next_local_store
= insn_info
->next_local_store
;
2715 active_local_stores
= insn_info
->next_local_store
;
2716 free_store_info (insn_info
);
2721 insn_info
= insn_info
->next_local_store
;
2726 /* Do all of step 1. */
2732 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2735 all_blocks
= BITMAP_ALLOC (NULL
);
2736 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2737 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2742 bb_info_t bb_info
= (bb_info_t
) pool_alloc (bb_info_pool
);
2744 memset (bb_info
, 0, sizeof (struct bb_info
));
2745 bitmap_set_bit (all_blocks
, bb
->index
);
2746 bb_info
->regs_live
= regs_live
;
2748 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2749 df_simulate_initialize_forwards (bb
, regs_live
);
2751 bb_table
[bb
->index
] = bb_info
;
2752 cselib_discard_hook
= remove_useless_values
;
2754 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2759 = create_alloc_pool ("cse_store_info_pool",
2760 sizeof (struct store_info
), 100);
2761 active_local_stores
= NULL
;
2762 active_local_stores_len
= 0;
2763 cselib_clear_table ();
2765 /* Scan the insns. */
2766 FOR_BB_INSNS (bb
, insn
)
2769 scan_insn (bb_info
, insn
);
2770 cselib_process_insn (insn
);
2772 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2775 /* This is something of a hack, because the global algorithm
2776 is supposed to take care of the case where stores go dead
2777 at the end of the function. However, the global
2778 algorithm must take a more conservative view of block
2779 mode reads than the local alg does. So to get the case
2780 where you have a store to the frame followed by a non
2781 overlapping block more read, we look at the active local
2782 stores at the end of the function and delete all of the
2783 frame and spill based ones. */
2784 if (stores_off_frame_dead_at_return
2785 && (EDGE_COUNT (bb
->succs
) == 0
2786 || (single_succ_p (bb
)
2787 && single_succ (bb
) == EXIT_BLOCK_PTR
2788 && ! crtl
->calls_eh_return
)))
2790 insn_info_t i_ptr
= active_local_stores
;
2793 store_info_t store_info
= i_ptr
->store_rec
;
2795 /* Skip the clobbers. */
2796 while (!store_info
->is_set
)
2797 store_info
= store_info
->next
;
2798 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2799 delete_dead_store_insn (i_ptr
);
2801 if (store_info
->group_id
>= 0)
2804 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
2805 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2806 delete_dead_store_insn (i_ptr
);
2809 i_ptr
= i_ptr
->next_local_store
;
2813 /* Get rid of the loads that were discovered in
2814 replace_read. Cselib is finished with this block. */
2815 while (deferred_change_list
)
2817 deferred_change_t next
= deferred_change_list
->next
;
2819 /* There is no reason to validate this change. That was
2821 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2822 pool_free (deferred_change_pool
, deferred_change_list
);
2823 deferred_change_list
= next
;
2826 /* Get rid of all of the cselib based store_infos in this
2827 block and mark the containing insns as not being
2829 ptr
= bb_info
->last_insn
;
2832 if (ptr
->contains_cselib_groups
)
2834 store_info_t s_info
= ptr
->store_rec
;
2835 while (s_info
&& !s_info
->is_set
)
2836 s_info
= s_info
->next
;
2838 && s_info
->redundant_reason
2839 && s_info
->redundant_reason
->insn
2840 && !ptr
->cannot_delete
)
2843 fprintf (dump_file
, "Locally deleting insn %d "
2844 "because insn %d stores the "
2845 "same value and couldn't be "
2847 INSN_UID (ptr
->insn
),
2848 INSN_UID (s_info
->redundant_reason
->insn
));
2849 delete_dead_store_insn (ptr
);
2852 s_info
->redundant_reason
= NULL
;
2853 free_store_info (ptr
);
2857 store_info_t s_info
;
2859 /* Free at least positions_needed bitmaps. */
2860 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2861 if (s_info
->is_large
)
2863 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2864 s_info
->is_large
= false;
2867 ptr
= ptr
->prev_insn
;
2870 free_alloc_pool (cse_store_info_pool
);
2872 bb_info
->regs_live
= NULL
;
2875 BITMAP_FREE (regs_live
);
2877 rtx_group_table
.empty ();
2881 /*----------------------------------------------------------------------------
2884 Assign each byte position in the stores that we are going to
2885 analyze globally to a position in the bitmaps. Returns true if
2886 there are any bit positions assigned.
2887 ----------------------------------------------------------------------------*/
2890 dse_step2_init (void)
2895 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
2897 /* For all non stack related bases, we only consider a store to
2898 be deletable if there are two or more stores for that
2899 position. This is because it takes one store to make the
2900 other store redundant. However, for the stores that are
2901 stack related, we consider them if there is only one store
2902 for the position. We do this because the stack related
2903 stores can be deleted if their is no read between them and
2904 the end of the function.
2906 To make this work in the current framework, we take the stack
2907 related bases add all of the bits from store1 into store2.
2908 This has the effect of making the eligible even if there is
2911 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2913 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2914 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2916 fprintf (dump_file
, "group %d is frame related ", i
);
2919 group
->offset_map_size_n
++;
2920 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2921 group
->offset_map_size_n
);
2922 group
->offset_map_size_p
++;
2923 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2924 group
->offset_map_size_p
);
2925 group
->process_globally
= false;
2928 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2929 (int)bitmap_count_bits (group
->store2_n
),
2930 (int)bitmap_count_bits (group
->store2_p
));
2931 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2932 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2938 /* Init the offset tables for the normal case. */
2941 dse_step2_nospill (void)
2945 /* Position 0 is unused because 0 is used in the maps to mean
2947 current_position
= 1;
2948 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
2953 if (group
== clear_alias_group
)
2956 memset (group
->offset_map_n
, 0, sizeof(int) * group
->offset_map_size_n
);
2957 memset (group
->offset_map_p
, 0, sizeof(int) * group
->offset_map_size_p
);
2958 bitmap_clear (group
->group_kill
);
2960 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2962 bitmap_set_bit (group
->group_kill
, current_position
);
2963 if (bitmap_bit_p (group
->escaped_n
, j
))
2964 bitmap_set_bit (kill_on_calls
, current_position
);
2965 group
->offset_map_n
[j
] = current_position
++;
2966 group
->process_globally
= true;
2968 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2970 bitmap_set_bit (group
->group_kill
, current_position
);
2971 if (bitmap_bit_p (group
->escaped_p
, j
))
2972 bitmap_set_bit (kill_on_calls
, current_position
);
2973 group
->offset_map_p
[j
] = current_position
++;
2974 group
->process_globally
= true;
2977 return current_position
!= 1;
2981 /* Init the offset tables for the spill case. */
2984 dse_step2_spill (void)
2987 group_info_t group
= clear_alias_group
;
2990 /* Position 0 is unused because 0 is used in the maps to mean
2992 current_position
= 1;
2996 bitmap_print (dump_file
, clear_alias_sets
,
2997 "clear alias sets ", "\n");
2998 bitmap_print (dump_file
, disqualified_clear_alias_sets
,
2999 "disqualified clear alias sets ", "\n");
3002 memset (group
->offset_map_n
, 0, sizeof(int) * group
->offset_map_size_n
);
3003 memset (group
->offset_map_p
, 0, sizeof(int) * group
->offset_map_size_p
);
3004 bitmap_clear (group
->group_kill
);
3006 /* Remove the disqualified positions from the store2_p set. */
3007 bitmap_and_compl_into (group
->store2_p
, disqualified_clear_alias_sets
);
3009 /* We do not need to process the store2_n set because
3010 alias_sets are always positive. */
3011 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
3013 bitmap_set_bit (group
->group_kill
, current_position
);
3014 group
->offset_map_p
[j
] = current_position
++;
3015 group
->process_globally
= true;
3018 return current_position
!= 1;
3023 /*----------------------------------------------------------------------------
3026 Build the bit vectors for the transfer functions.
3027 ----------------------------------------------------------------------------*/
3030 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
3034 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
3038 HOST_WIDE_INT offset_p
= -offset
;
3039 if (offset_p
>= group_info
->offset_map_size_n
)
3041 return group_info
->offset_map_n
[offset_p
];
3045 if (offset
>= group_info
->offset_map_size_p
)
3047 return group_info
->offset_map_p
[offset
];
3052 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3056 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3061 group_info_t group_info
3062 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
3063 if (group_info
->process_globally
)
3064 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3066 int index
= get_bitmap_index (group_info
, i
);
3069 bitmap_set_bit (gen
, index
);
3071 bitmap_clear_bit (kill
, index
);
3074 store_info
= store_info
->next
;
3079 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3083 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3087 if (store_info
->alias_set
)
3089 int index
= get_bitmap_index (clear_alias_group
,
3090 store_info
->alias_set
);
3093 bitmap_set_bit (gen
, index
);
3095 bitmap_clear_bit (kill
, index
);
3098 store_info
= store_info
->next
;
3103 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3107 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3109 read_info_t read_info
= insn_info
->read_rec
;
3113 /* If this insn reads the frame, kill all the frame related stores. */
3114 if (insn_info
->frame_read
)
3116 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3117 if (group
->process_globally
&& group
->frame_related
)
3120 bitmap_ior_into (kill
, group
->group_kill
);
3121 bitmap_and_compl_into (gen
, group
->group_kill
);
3124 if (insn_info
->non_frame_wild_read
)
3126 /* Kill all non-frame related stores. Kill all stores of variables that
3129 bitmap_ior_into (kill
, kill_on_calls
);
3130 bitmap_and_compl_into (gen
, kill_on_calls
);
3131 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3132 if (group
->process_globally
&& !group
->frame_related
)
3135 bitmap_ior_into (kill
, group
->group_kill
);
3136 bitmap_and_compl_into (gen
, group
->group_kill
);
3141 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3143 if (group
->process_globally
)
3145 if (i
== read_info
->group_id
)
3147 if (read_info
->begin
> read_info
->end
)
3149 /* Begin > end for block mode reads. */
3151 bitmap_ior_into (kill
, group
->group_kill
);
3152 bitmap_and_compl_into (gen
, group
->group_kill
);
3156 /* The groups are the same, just process the
3159 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3161 int index
= get_bitmap_index (group
, j
);
3165 bitmap_set_bit (kill
, index
);
3166 bitmap_clear_bit (gen
, index
);
3173 /* The groups are different, if the alias sets
3174 conflict, clear the entire group. We only need
3175 to apply this test if the read_info is a cselib
3176 read. Anything with a constant base cannot alias
3177 something else with a different constant
3179 if ((read_info
->group_id
< 0)
3180 && canon_true_dependence (group
->base_mem
,
3181 GET_MODE (group
->base_mem
),
3182 group
->canon_base_addr
,
3183 read_info
->mem
, NULL_RTX
))
3186 bitmap_ior_into (kill
, group
->group_kill
);
3187 bitmap_and_compl_into (gen
, group
->group_kill
);
3193 read_info
= read_info
->next
;
3197 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3201 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3205 if (read_info
->alias_set
)
3207 int index
= get_bitmap_index (clear_alias_group
,
3208 read_info
->alias_set
);
3212 bitmap_set_bit (kill
, index
);
3213 bitmap_clear_bit (gen
, index
);
3217 read_info
= read_info
->next
;
3222 /* Return the insn in BB_INFO before the first wild read or if there
3223 are no wild reads in the block, return the last insn. */
3226 find_insn_before_first_wild_read (bb_info_t bb_info
)
3228 insn_info_t insn_info
= bb_info
->last_insn
;
3229 insn_info_t last_wild_read
= NULL
;
3233 if (insn_info
->wild_read
)
3235 last_wild_read
= insn_info
->prev_insn
;
3236 /* Block starts with wild read. */
3237 if (!last_wild_read
)
3241 insn_info
= insn_info
->prev_insn
;
3245 return last_wild_read
;
3247 return bb_info
->last_insn
;
3251 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3252 the block in order to build the gen and kill sets for the block.
3253 We start at ptr which may be the last insn in the block or may be
3254 the first insn with a wild read. In the latter case we are able to
3255 skip the rest of the block because it just does not matter:
3256 anything that happens is hidden by the wild read. */
3259 dse_step3_scan (bool for_spills
, basic_block bb
)
3261 bb_info_t bb_info
= bb_table
[bb
->index
];
3262 insn_info_t insn_info
;
3265 /* There are no wild reads in the spill case. */
3266 insn_info
= bb_info
->last_insn
;
3268 insn_info
= find_insn_before_first_wild_read (bb_info
);
3270 /* In the spill case or in the no_spill case if there is no wild
3271 read in the block, we will need a kill set. */
3272 if (insn_info
== bb_info
->last_insn
)
3275 bitmap_clear (bb_info
->kill
);
3277 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3281 BITMAP_FREE (bb_info
->kill
);
3285 /* There may have been code deleted by the dce pass run before
3287 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3289 /* Process the read(s) last. */
3292 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3293 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3297 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3298 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3302 insn_info
= insn_info
->prev_insn
;
3307 /* Set the gen set of the exit block, and also any block with no
3308 successors that does not have a wild read. */
3311 dse_step3_exit_block_scan (bb_info_t bb_info
)
3313 /* The gen set is all 0's for the exit block except for the
3314 frame_pointer_group. */
3316 if (stores_off_frame_dead_at_return
)
3321 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3323 if (group
->process_globally
&& group
->frame_related
)
3324 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3330 /* Find all of the blocks that are not backwards reachable from the
3331 exit block or any block with no successors (BB). These are the
3332 infinite loops or infinite self loops. These blocks will still
3333 have their bits set in UNREACHABLE_BLOCKS. */
3336 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3341 if (TEST_BIT (unreachable_blocks
, bb
->index
))
3343 RESET_BIT (unreachable_blocks
, bb
->index
);
3344 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3346 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3351 /* Build the transfer functions for the function. */
3354 dse_step3 (bool for_spills
)
3357 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block
);
3358 sbitmap_iterator sbi
;
3359 bitmap all_ones
= NULL
;
3362 sbitmap_ones (unreachable_blocks
);
3366 bb_info_t bb_info
= bb_table
[bb
->index
];
3368 bitmap_clear (bb_info
->gen
);
3370 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3372 if (bb
->index
== ENTRY_BLOCK
)
3374 else if (bb
->index
== EXIT_BLOCK
)
3375 dse_step3_exit_block_scan (bb_info
);
3377 dse_step3_scan (for_spills
, bb
);
3378 if (EDGE_COUNT (bb
->succs
) == 0)
3379 mark_reachable_blocks (unreachable_blocks
, bb
);
3381 /* If this is the second time dataflow is run, delete the old
3384 BITMAP_FREE (bb_info
->in
);
3386 BITMAP_FREE (bb_info
->out
);
3389 /* For any block in an infinite loop, we must initialize the out set
3390 to all ones. This could be expensive, but almost never occurs in
3391 practice. However, it is common in regression tests. */
3392 EXECUTE_IF_SET_IN_SBITMAP (unreachable_blocks
, 0, i
, sbi
)
3394 if (bitmap_bit_p (all_blocks
, i
))
3396 bb_info_t bb_info
= bb_table
[i
];
3402 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3403 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, j
, group
)
3404 bitmap_ior_into (all_ones
, group
->group_kill
);
3408 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3409 bitmap_copy (bb_info
->out
, all_ones
);
3415 BITMAP_FREE (all_ones
);
3416 sbitmap_free (unreachable_blocks
);
3421 /*----------------------------------------------------------------------------
3424 Solve the bitvector equations.
3425 ----------------------------------------------------------------------------*/
3428 /* Confluence function for blocks with no successors. Create an out
3429 set from the gen set of the exit block. This block logically has
3430 the exit block as a successor. */
3435 dse_confluence_0 (basic_block bb
)
3437 bb_info_t bb_info
= bb_table
[bb
->index
];
3439 if (bb
->index
== EXIT_BLOCK
)
3444 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3445 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3449 /* Propagate the information from the in set of the dest of E to the
3450 out set of the src of E. If the various in or out sets are not
3451 there, that means they are all ones. */
3454 dse_confluence_n (edge e
)
3456 bb_info_t src_info
= bb_table
[e
->src
->index
];
3457 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3462 bitmap_and_into (src_info
->out
, dest_info
->in
);
3465 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3466 bitmap_copy (src_info
->out
, dest_info
->in
);
3473 /* Propagate the info from the out to the in set of BB_INDEX's basic
3474 block. There are three cases:
3476 1) The block has no kill set. In this case the kill set is all
3477 ones. It does not matter what the out set of the block is, none of
3478 the info can reach the top. The only thing that reaches the top is
3479 the gen set and we just copy the set.
3481 2) There is a kill set but no out set and bb has successors. In
3482 this case we just return. Eventually an out set will be created and
3483 it is better to wait than to create a set of ones.
3485 3) There is both a kill and out set. We apply the obvious transfer
3490 dse_transfer_function (int bb_index
)
3492 bb_info_t bb_info
= bb_table
[bb_index
];
3500 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3501 bb_info
->out
, bb_info
->kill
);
3504 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3505 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3506 bb_info
->out
, bb_info
->kill
);
3516 /* Case 1 above. If there is already an in set, nothing
3522 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3523 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3529 /* Solve the dataflow equations. */
3534 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3535 dse_confluence_n
, dse_transfer_function
,
3536 all_blocks
, df_get_postorder (DF_BACKWARD
),
3537 df_get_n_blocks (DF_BACKWARD
));
3542 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3545 bb_info_t bb_info
= bb_table
[bb
->index
];
3547 df_print_bb_index (bb
, dump_file
);
3549 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3551 fprintf (dump_file
, " in: *MISSING*\n");
3553 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3555 fprintf (dump_file
, " gen: *MISSING*\n");
3557 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3559 fprintf (dump_file
, " kill: *MISSING*\n");
3561 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3563 fprintf (dump_file
, " out: *MISSING*\n\n");
3570 /*----------------------------------------------------------------------------
3573 Delete the stores that can only be deleted using the global information.
3574 ----------------------------------------------------------------------------*/
3578 dse_step5_nospill (void)
3583 bb_info_t bb_info
= bb_table
[bb
->index
];
3584 insn_info_t insn_info
= bb_info
->last_insn
;
3585 bitmap v
= bb_info
->out
;
3589 bool deleted
= false;
3590 if (dump_file
&& insn_info
->insn
)
3592 fprintf (dump_file
, "starting to process insn %d\n",
3593 INSN_UID (insn_info
->insn
));
3594 bitmap_print (dump_file
, v
, " v: ", "\n");
3597 /* There may have been code deleted by the dce pass run before
3600 && INSN_P (insn_info
->insn
)
3601 && (!insn_info
->cannot_delete
)
3602 && (!bitmap_empty_p (v
)))
3604 store_info_t store_info
= insn_info
->store_rec
;
3606 /* Try to delete the current insn. */
3609 /* Skip the clobbers. */
3610 while (!store_info
->is_set
)
3611 store_info
= store_info
->next
;
3613 if (store_info
->alias_set
)
3618 group_info_t group_info
3619 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
3621 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3623 int index
= get_bitmap_index (group_info
, i
);
3626 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3627 if (index
== 0 || !bitmap_bit_p (v
, index
))
3630 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3639 && check_for_inc_dec_1 (insn_info
))
3641 delete_insn (insn_info
->insn
);
3642 insn_info
->insn
= NULL
;
3647 /* We do want to process the local info if the insn was
3648 deleted. For instance, if the insn did a wild read, we
3649 no longer need to trash the info. */
3651 && INSN_P (insn_info
->insn
)
3654 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3655 if (insn_info
->wild_read
)
3658 fprintf (dump_file
, "wild read\n");
3661 else if (insn_info
->read_rec
3662 || insn_info
->non_frame_wild_read
)
3664 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3665 fprintf (dump_file
, "regular read\n");
3667 fprintf (dump_file
, "non-frame wild read\n");
3668 scan_reads_nospill (insn_info
, v
, NULL
);
3672 insn_info
= insn_info
->prev_insn
;
3679 dse_step5_spill (void)
3684 bb_info_t bb_info
= bb_table
[bb
->index
];
3685 insn_info_t insn_info
= bb_info
->last_insn
;
3686 bitmap v
= bb_info
->out
;
3690 bool deleted
= false;
3691 /* There may have been code deleted by the dce pass run before
3694 && INSN_P (insn_info
->insn
)
3695 && (!insn_info
->cannot_delete
)
3696 && (!bitmap_empty_p (v
)))
3698 /* Try to delete the current insn. */
3699 store_info_t store_info
= insn_info
->store_rec
;
3704 if (store_info
->alias_set
)
3706 int index
= get_bitmap_index (clear_alias_group
,
3707 store_info
->alias_set
);
3708 if (index
== 0 || !bitmap_bit_p (v
, index
))
3716 store_info
= store_info
->next
;
3718 if (deleted
&& dbg_cnt (dse
)
3719 && check_for_inc_dec_1 (insn_info
))
3722 fprintf (dump_file
, "Spill deleting insn %d\n",
3723 INSN_UID (insn_info
->insn
));
3724 delete_insn (insn_info
->insn
);
3726 insn_info
->insn
= NULL
;
3731 && INSN_P (insn_info
->insn
)
3734 scan_stores_spill (insn_info
->store_rec
, v
, NULL
);
3735 scan_reads_spill (insn_info
->read_rec
, v
, NULL
);
3738 insn_info
= insn_info
->prev_insn
;
3745 /*----------------------------------------------------------------------------
3748 Delete stores made redundant by earlier stores (which store the same
3749 value) that couldn't be eliminated.
3750 ----------------------------------------------------------------------------*/
3759 bb_info_t bb_info
= bb_table
[bb
->index
];
3760 insn_info_t insn_info
= bb_info
->last_insn
;
3764 /* There may have been code deleted by the dce pass run before
3767 && INSN_P (insn_info
->insn
)
3768 && !insn_info
->cannot_delete
)
3770 store_info_t s_info
= insn_info
->store_rec
;
3772 while (s_info
&& !s_info
->is_set
)
3773 s_info
= s_info
->next
;
3775 && s_info
->redundant_reason
3776 && s_info
->redundant_reason
->insn
3777 && INSN_P (s_info
->redundant_reason
->insn
))
3779 rtx rinsn
= s_info
->redundant_reason
->insn
;
3781 fprintf (dump_file
, "Locally deleting insn %d "
3782 "because insn %d stores the "
3783 "same value and couldn't be "
3785 INSN_UID (insn_info
->insn
),
3787 delete_dead_store_insn (insn_info
);
3790 insn_info
= insn_info
->prev_insn
;
3795 /*----------------------------------------------------------------------------
3798 Destroy everything left standing.
3799 ----------------------------------------------------------------------------*/
3804 bitmap_obstack_release (&dse_bitmap_obstack
);
3805 obstack_free (&dse_obstack
, NULL
);
3807 if (clear_alias_sets
)
3809 BITMAP_FREE (clear_alias_sets
);
3810 BITMAP_FREE (disqualified_clear_alias_sets
);
3811 free_alloc_pool (clear_alias_mode_pool
);
3812 htab_delete (clear_alias_mode_table
);
3815 end_alias_analysis ();
3817 rtx_group_table
.dispose ();
3818 VEC_free (group_info_t
, heap
, rtx_group_vec
);
3819 BITMAP_FREE (all_blocks
);
3820 BITMAP_FREE (scratch
);
3822 free_alloc_pool (rtx_store_info_pool
);
3823 free_alloc_pool (read_info_pool
);
3824 free_alloc_pool (insn_info_pool
);
3825 free_alloc_pool (bb_info_pool
);
3826 free_alloc_pool (rtx_group_info_pool
);
3827 free_alloc_pool (deferred_change_pool
);
3831 /* -------------------------------------------------------------------------
3833 ------------------------------------------------------------------------- */
3835 /* Callback for running pass_rtl_dse. */
3838 rest_of_handle_dse (void)
3840 bool did_global
= false;
3842 df_set_flags (DF_DEFER_INSN_RESCAN
);
3844 /* Need the notes since we must track live hardregs in the forwards
3846 df_note_add_problem ();
3852 if (dse_step2_nospill ())
3854 df_set_flags (DF_LR_RUN_DCE
);
3858 fprintf (dump_file
, "doing global processing\n");
3861 dse_step5_nospill ();
3864 /* For the instance of dse that runs after reload, we make a special
3865 pass to process the spills. These are special in that they are
3866 totally transparent, i.e, there is no aliasing issues that need
3867 to be considered. This means that the wild reads that kill
3868 everything else do not apply here. */
3869 if (clear_alias_sets
&& dse_step2_spill ())
3873 df_set_flags (DF_LR_RUN_DCE
);
3878 fprintf (dump_file
, "doing global spill processing\n");
3888 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3889 locally_deleted
, globally_deleted
, spill_deleted
);
3896 return optimize
> 0 && flag_dse
3903 return optimize
> 0 && flag_dse
3907 struct rtl_opt_pass pass_rtl_dse1
=
3912 gate_dse1
, /* gate */
3913 rest_of_handle_dse
, /* execute */
3916 0, /* static_pass_number */
3917 TV_DSE1
, /* tv_id */
3918 0, /* properties_required */
3919 0, /* properties_provided */
3920 0, /* properties_destroyed */
3921 0, /* todo_flags_start */
3922 TODO_df_finish
| TODO_verify_rtl_sharing
|
3923 TODO_ggc_collect
/* todo_flags_finish */
3927 struct rtl_opt_pass pass_rtl_dse2
=
3932 gate_dse2
, /* gate */
3933 rest_of_handle_dse
, /* execute */
3936 0, /* static_pass_number */
3937 TV_DSE2
, /* tv_id */
3938 0, /* properties_required */
3939 0, /* properties_provided */
3940 0, /* properties_destroyed */
3941 0, /* todo_flags_start */
3942 TODO_df_finish
| TODO_verify_rtl_sharing
|
3943 TODO_ggc_collect
/* todo_flags_finish */