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"
35 #include "hard-reg-set.h"
41 #include "tree-pass.h"
42 #include "alloc-pool.h"
44 #include "insn-config.h"
51 #include "tree-flow.h"
53 /* This file contains three techniques for performing Dead Store
56 * The first technique performs dse locally on any base address. It
57 is based on the cselib which is a local value numbering technique.
58 This technique is local to a basic block but deals with a fairly
61 * The second technique performs dse globally but is restricted to
62 base addresses that are either constant or are relative to the
65 * The third technique, (which is only done after register allocation)
66 processes the spill spill slots. This differs from the second
67 technique because it takes advantage of the fact that spilling is
68 completely free from the effects of aliasing.
70 Logically, dse is a backwards dataflow problem. A store can be
71 deleted if it if cannot be reached in the backward direction by any
72 use of the value being stored. However, the local technique uses a
73 forwards scan of the basic block because cselib requires that the
74 block be processed in that order.
76 The pass is logically broken into 7 steps:
80 1) The local algorithm, as well as scanning the insns for the two
83 2) Analysis to see if the global algs are necessary. In the case
84 of stores base on a constant address, there must be at least two
85 stores to that address, to make it possible to delete some of the
86 stores. In the case of stores off of the frame or spill related
87 stores, only one store to an address is necessary because those
88 stores die at the end of the function.
90 3) Set up the global dataflow equations based on processing the
91 info parsed in the first step.
93 4) Solve the dataflow equations.
95 5) Delete the insns that the global analysis has indicated are
98 6) Delete insns that store the same value as preceeding store
99 where the earlier store couldn't be eliminated.
103 This step uses cselib and canon_rtx to build the largest expression
104 possible for each address. This pass is a forwards pass through
105 each basic block. From the point of view of the global technique,
106 the first pass could examine a block in either direction. The
107 forwards ordering is to accommodate cselib.
109 We a simplifying assumption: addresses fall into four broad
112 1) base has rtx_varies_p == false, offset is constant.
113 2) base has rtx_varies_p == false, offset variable.
114 3) base has rtx_varies_p == true, offset constant.
115 4) base has rtx_varies_p == true, offset variable.
117 The local passes are able to process all 4 kinds of addresses. The
118 global pass only handles (1).
120 The global problem is formulated as follows:
122 A store, S1, to address A, where A is not relative to the stack
123 frame, can be eliminated if all paths from S1 to the end of the
124 of the function contain another store to A before a read to A.
126 If the address A is relative to the stack frame, a store S2 to A
127 can be eliminated if there are no paths from S1 that reach the
128 end of the function that read A before another store to A. In
129 this case S2 can be deleted if there are paths to from S2 to the
130 end of the function that have no reads or writes to A. This
131 second case allows stores to the stack frame to be deleted that
132 would otherwise die when the function returns. This cannot be
133 done if stores_off_frame_dead_at_return is not true. See the doc
134 for that variable for when this variable is false.
136 The global problem is formulated as a backwards set union
137 dataflow problem where the stores are the gens and reads are the
138 kills. Set union problems are rare and require some special
139 handling given our representation of bitmaps. A straightforward
140 implementation of requires a lot of bitmaps filled with 1s.
141 These are expensive and cumbersome in our bitmap formulation so
142 care has been taken to avoid large vectors filled with 1s. See
143 the comments in bb_info and in the dataflow confluence functions
146 There are two places for further enhancements to this algorithm:
148 1) The original dse which was embedded in a pass called flow also
149 did local address forwarding. For example in
154 flow would replace the right hand side of the second insn with a
155 reference to r100. Most of the information is available to add this
156 to this pass. It has not done it because it is a lot of work in
157 the case that either r100 is assigned to between the first and
158 second insn and/or the second insn is a load of part of the value
159 stored by the first insn.
161 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
162 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
163 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
164 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
166 2) The cleaning up of spill code is quite profitable. It currently
167 depends on reading tea leaves and chicken entrails left by reload.
168 This pass depends on reload creating a singleton alias set for each
169 spill slot and telling the next dse pass which of these alias sets
170 are the singletons. Rather than analyze the addresses of the
171 spills, dse's spill processing just does analysis of the loads and
172 stores that use those alias sets. There are three cases where this
175 a) Reload sometimes creates the slot for one mode of access, and
176 then inserts loads and/or stores for a smaller mode. In this
177 case, the current code just punts on the slot. The proper thing
178 to do is to back out and use one bit vector position for each
179 byte of the entity associated with the slot. This depends on
180 KNOWING that reload always generates the accesses for each of the
181 bytes in some canonical (read that easy to understand several
182 passes after reload happens) way.
184 b) Reload sometimes decides that spill slot it allocated was not
185 large enough for the mode and goes back and allocates more slots
186 with the same mode and alias set. The backout in this case is a
187 little more graceful than (a). In this case the slot is unmarked
188 as being a spill slot and if final address comes out to be based
189 off the frame pointer, the global algorithm handles this slot.
191 c) For any pass that may prespill, there is currently no
192 mechanism to tell the dse pass that the slot being used has the
193 special properties that reload uses. It may be that all that is
194 required is to have those passes make the same calls that reload
195 does, assuming that the alias sets can be manipulated in the same
198 /* There are limits to the size of constant offsets we model for the
199 global problem. There are certainly test cases, that exceed this
200 limit, however, it is unlikely that there are important programs
201 that really have constant offsets this size. */
202 #define MAX_OFFSET (64 * 1024)
205 static bitmap scratch
= NULL
;
208 /* This structure holds information about a candidate store. */
212 /* False means this is a clobber. */
215 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
218 /* The id of the mem group of the base address. If rtx_varies_p is
219 true, this is -1. Otherwise, it is the index into the group
223 /* This is the cselib value. */
224 cselib_val
*cse_base
;
226 /* This canonized mem. */
229 /* Canonized MEM address for use by canon_true_dependence. */
232 /* If this is non-zero, it is the alias set of a spill location. */
233 alias_set_type alias_set
;
235 /* The offset of the first and byte before the last byte associated
236 with the operation. */
237 HOST_WIDE_INT begin
, end
;
241 /* A bitmask as wide as the number of bytes in the word that
242 contains a 1 if the byte may be needed. The store is unused if
243 all of the bits are 0. This is used if IS_LARGE is false. */
244 unsigned HOST_WIDE_INT small_bitmask
;
248 /* A bitmap with one bit per byte. Cleared bit means the position
249 is needed. Used if IS_LARGE is false. */
252 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
253 equal to END - BEGIN, the whole store is unused. */
258 /* The next store info for this insn. */
259 struct store_info
*next
;
261 /* The right hand side of the store. This is used if there is a
262 subsequent reload of the mems address somewhere later in the
266 /* If rhs is or holds a constant, this contains that constant,
270 /* Set if this store stores the same constant value as REDUNDANT_REASON
271 insn stored. These aren't eliminated early, because doing that
272 might prevent the earlier larger store to be eliminated. */
273 struct insn_info
*redundant_reason
;
276 /* Return a bitmask with the first N low bits set. */
278 static unsigned HOST_WIDE_INT
279 lowpart_bitmask (int n
)
281 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
282 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
285 typedef struct store_info
*store_info_t
;
286 static alloc_pool cse_store_info_pool
;
287 static alloc_pool rtx_store_info_pool
;
289 /* This structure holds information about a load. These are only
290 built for rtx bases. */
293 /* The id of the mem group of the base address. */
296 /* If this is non-zero, it is the alias set of a spill location. */
297 alias_set_type alias_set
;
299 /* The offset of the first and byte after the last byte associated
300 with the operation. If begin == end == 0, the read did not have
301 a constant offset. */
304 /* The mem being read. */
307 /* The next read_info for this insn. */
308 struct read_info
*next
;
310 typedef struct read_info
*read_info_t
;
311 static alloc_pool read_info_pool
;
314 /* One of these records is created for each insn. */
318 /* Set true if the insn contains a store but the insn itself cannot
319 be deleted. This is set if the insn is a parallel and there is
320 more than one non dead output or if the insn is in some way
324 /* This field is only used by the global algorithm. It is set true
325 if the insn contains any read of mem except for a (1). This is
326 also set if the insn is a call or has a clobber mem. If the insn
327 contains a wild read, the use_rec will be null. */
330 /* This is true only for CALL instructions which could potentially read
331 any non-frame memory location. This field is used by the global
333 bool non_frame_wild_read
;
335 /* This field is only used for the processing of const functions.
336 These functions cannot read memory, but they can read the stack
337 because that is where they may get their parms. We need to be
338 this conservative because, like the store motion pass, we don't
339 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
340 Moreover, we need to distinguish two cases:
341 1. Before reload (register elimination), the stores related to
342 outgoing arguments are stack pointer based and thus deemed
343 of non-constant base in this pass. This requires special
344 handling but also means that the frame pointer based stores
345 need not be killed upon encountering a const function call.
346 2. After reload, the stores related to outgoing arguments can be
347 either stack pointer or hard frame pointer based. This means
348 that we have no other choice than also killing all the frame
349 pointer based stores upon encountering a const function call.
350 This field is set after reload for const function calls. Having
351 this set is less severe than a wild read, it just means that all
352 the frame related stores are killed rather than all the stores. */
355 /* This field is only used for the processing of const functions.
356 It is set if the insn may contain a stack pointer based store. */
357 bool stack_pointer_based
;
359 /* This is true if any of the sets within the store contains a
360 cselib base. Such stores can only be deleted by the local
362 bool contains_cselib_groups
;
367 /* The list of mem sets or mem clobbers that are contained in this
368 insn. If the insn is deletable, it contains only one mem set.
369 But it could also contain clobbers. Insns that contain more than
370 one mem set are not deletable, but each of those mems are here in
371 order to provide info to delete other insns. */
372 store_info_t store_rec
;
374 /* The linked list of mem uses in this insn. Only the reads from
375 rtx bases are listed here. The reads to cselib bases are
376 completely processed during the first scan and so are never
378 read_info_t read_rec
;
380 /* The live fixed registers. We assume only fixed registers can
381 cause trouble by being clobbered from an expanded pattern;
382 storing only the live fixed registers (rather than all registers)
383 means less memory needs to be allocated / copied for the individual
385 regset fixed_regs_live
;
387 /* The prev insn in the basic block. */
388 struct insn_info
* prev_insn
;
390 /* The linked list of insns that are in consideration for removal in
391 the forwards pass thru the basic block. This pointer may be
392 trash as it is not cleared when a wild read occurs. The only
393 time it is guaranteed to be correct is when the traversal starts
394 at active_local_stores. */
395 struct insn_info
* next_local_store
;
398 typedef struct insn_info
*insn_info_t
;
399 static alloc_pool insn_info_pool
;
401 /* The linked list of stores that are under consideration in this
403 static insn_info_t active_local_stores
;
404 static int active_local_stores_len
;
409 /* Pointer to the insn info for the last insn in the block. These
410 are linked so this is how all of the insns are reached. During
411 scanning this is the current insn being scanned. */
412 insn_info_t last_insn
;
414 /* The info for the global dataflow problem. */
417 /* This is set if the transfer function should and in the wild_read
418 bitmap before applying the kill and gen sets. That vector knocks
419 out most of the bits in the bitmap and thus speeds up the
421 bool apply_wild_read
;
423 /* The following 4 bitvectors hold information about which positions
424 of which stores are live or dead. They are indexed by
427 /* The set of store positions that exist in this block before a wild read. */
430 /* The set of load positions that exist in this block above the
431 same position of a store. */
434 /* The set of stores that reach the top of the block without being
437 Do not represent the in if it is all ones. Note that this is
438 what the bitvector should logically be initialized to for a set
439 intersection problem. However, like the kill set, this is too
440 expensive. So initially, the in set will only be created for the
441 exit block and any block that contains a wild read. */
444 /* The set of stores that reach the bottom of the block from it's
447 Do not represent the in if it is all ones. Note that this is
448 what the bitvector should logically be initialized to for a set
449 intersection problem. However, like the kill and in set, this is
450 too expensive. So what is done is that the confluence operator
451 just initializes the vector from one of the out sets of the
452 successors of the block. */
455 /* The following bitvector is indexed by the reg number. It
456 contains the set of regs that are live at the current instruction
457 being processed. While it contains info for all of the
458 registers, only the hard registers are actually examined. It is used
459 to assure that shift and/or add sequences that are inserted do not
460 accidently clobber live hard regs. */
464 typedef struct bb_info
*bb_info_t
;
465 static alloc_pool bb_info_pool
;
467 /* Table to hold all bb_infos. */
468 static bb_info_t
*bb_table
;
470 /* There is a group_info for each rtx base that is used to reference
471 memory. There are also not many of the rtx bases because they are
472 very limited in scope. */
476 /* The actual base of the address. */
479 /* The sequential id of the base. This allows us to have a
480 canonical ordering of these that is not based on addresses. */
483 /* True if there are any positions that are to be processed
485 bool process_globally
;
487 /* True if the base of this group is either the frame_pointer or
488 hard_frame_pointer. */
491 /* A mem wrapped around the base pointer for the group in order to do
492 read dependency. It must be given BLKmode in order to encompass all
493 the possible offsets from the base. */
496 /* Canonized version of base_mem's address. */
499 /* These two sets of two bitmaps are used to keep track of how many
500 stores are actually referencing that position from this base. We
501 only do this for rtx bases as this will be used to assign
502 positions in the bitmaps for the global problem. Bit N is set in
503 store1 on the first store for offset N. Bit N is set in store2
504 for the second store to offset N. This is all we need since we
505 only care about offsets that have two or more stores for them.
507 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
508 for 0 and greater offsets.
510 There is one special case here, for stores into the stack frame,
511 we will or store1 into store2 before deciding which stores look
512 at globally. This is because stores to the stack frame that have
513 no other reads before the end of the function can also be
515 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
517 /* These bitmaps keep track of offsets in this group escape this function.
518 An offset escapes if it corresponds to a named variable whose
519 addressable flag is set. */
520 bitmap escaped_n
, escaped_p
;
522 /* The positions in this bitmap have the same assignments as the in,
523 out, gen and kill bitmaps. This bitmap is all zeros except for
524 the positions that are occupied by stores for this group. */
527 /* The offset_map is used to map the offsets from this base into
528 positions in the global bitmaps. It is only created after all of
529 the all of stores have been scanned and we know which ones we
531 int *offset_map_n
, *offset_map_p
;
532 int offset_map_size_n
, offset_map_size_p
;
534 typedef struct group_info
*group_info_t
;
535 typedef const struct group_info
*const_group_info_t
;
536 static alloc_pool rtx_group_info_pool
;
538 /* Tables of group_info structures, hashed by base value. */
539 static htab_t rtx_group_table
;
541 /* Index into the rtx_group_vec. */
542 static int rtx_group_next_id
;
544 DEF_VEC_P(group_info_t
);
545 DEF_VEC_ALLOC_P(group_info_t
,heap
);
547 static VEC(group_info_t
,heap
) *rtx_group_vec
;
550 /* This structure holds the set of changes that are being deferred
551 when removing read operation. See replace_read. */
552 struct deferred_change
555 /* The mem that is being replaced. */
558 /* The reg it is being replaced with. */
561 struct deferred_change
*next
;
564 typedef struct deferred_change
*deferred_change_t
;
565 static alloc_pool deferred_change_pool
;
567 static deferred_change_t deferred_change_list
= NULL
;
569 /* This are used to hold the alias sets of spill variables. Since
570 these are never aliased and there may be a lot of them, it makes
571 sense to treat them specially. This bitvector is only allocated in
572 calls from dse_record_singleton_alias_set which currently is only
573 made during reload1. So when dse is called before reload this
574 mechanism does nothing. */
576 static bitmap clear_alias_sets
= NULL
;
578 /* The set of clear_alias_sets that have been disqualified because
579 there are loads or stores using a different mode than the alias set
580 was registered with. */
581 static bitmap disqualified_clear_alias_sets
= NULL
;
583 /* The group that holds all of the clear_alias_sets. */
584 static group_info_t clear_alias_group
;
586 /* The modes of the clear_alias_sets. */
587 static htab_t clear_alias_mode_table
;
589 /* Hash table element to look up the mode for an alias set. */
590 struct clear_alias_mode_holder
592 alias_set_type alias_set
;
593 enum machine_mode mode
;
596 static alloc_pool clear_alias_mode_pool
;
598 /* This is true except if cfun->stdarg -- i.e. we cannot do
599 this for vararg functions because they play games with the frame. */
600 static bool stores_off_frame_dead_at_return
;
602 /* Counter for stats. */
603 static int globally_deleted
;
604 static int locally_deleted
;
605 static int spill_deleted
;
607 static bitmap all_blocks
;
609 /* Locations that are killed by calls in the global phase. */
610 static bitmap kill_on_calls
;
612 /* The number of bits used in the global bitmaps. */
613 static unsigned int current_position
;
616 static bool gate_dse1 (void);
617 static bool gate_dse2 (void);
620 /*----------------------------------------------------------------------------
624 ----------------------------------------------------------------------------*/
627 /* Find the entry associated with ALIAS_SET. */
629 static struct clear_alias_mode_holder
*
630 clear_alias_set_lookup (alias_set_type alias_set
)
632 struct clear_alias_mode_holder tmp_holder
;
635 tmp_holder
.alias_set
= alias_set
;
636 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
639 return (struct clear_alias_mode_holder
*) *slot
;
643 /* Hashtable callbacks for maintaining the "bases" field of
644 store_group_info, given that the addresses are function invariants. */
647 invariant_group_base_eq (const void *p1
, const void *p2
)
649 const_group_info_t gi1
= (const_group_info_t
) p1
;
650 const_group_info_t gi2
= (const_group_info_t
) p2
;
651 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
656 invariant_group_base_hash (const void *p
)
658 const_group_info_t gi
= (const_group_info_t
) p
;
660 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
664 /* Get the GROUP for BASE. Add a new group if it is not there. */
667 get_group_info (rtx base
)
669 struct group_info tmp_gi
;
675 /* Find the store_base_info structure for BASE, creating a new one
677 tmp_gi
.rtx_base
= base
;
678 slot
= htab_find_slot (rtx_group_table
, &tmp_gi
, INSERT
);
679 gi
= (group_info_t
) *slot
;
683 if (!clear_alias_group
)
685 clear_alias_group
= gi
=
686 (group_info_t
) pool_alloc (rtx_group_info_pool
);
687 memset (gi
, 0, sizeof (struct group_info
));
688 gi
->id
= rtx_group_next_id
++;
689 gi
->store1_n
= BITMAP_ALLOC (NULL
);
690 gi
->store1_p
= BITMAP_ALLOC (NULL
);
691 gi
->store2_n
= BITMAP_ALLOC (NULL
);
692 gi
->store2_p
= BITMAP_ALLOC (NULL
);
693 gi
->escaped_p
= BITMAP_ALLOC (NULL
);
694 gi
->escaped_n
= BITMAP_ALLOC (NULL
);
695 gi
->group_kill
= BITMAP_ALLOC (NULL
);
696 gi
->process_globally
= false;
697 gi
->offset_map_size_n
= 0;
698 gi
->offset_map_size_p
= 0;
699 gi
->offset_map_n
= NULL
;
700 gi
->offset_map_p
= NULL
;
701 VEC_safe_push (group_info_t
, heap
, rtx_group_vec
, gi
);
703 return clear_alias_group
;
708 *slot
= gi
= (group_info_t
) pool_alloc (rtx_group_info_pool
);
710 gi
->id
= rtx_group_next_id
++;
711 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
712 gi
->canon_base_addr
= canon_rtx (base
);
713 gi
->store1_n
= BITMAP_ALLOC (NULL
);
714 gi
->store1_p
= BITMAP_ALLOC (NULL
);
715 gi
->store2_n
= BITMAP_ALLOC (NULL
);
716 gi
->store2_p
= BITMAP_ALLOC (NULL
);
717 gi
->escaped_p
= BITMAP_ALLOC (NULL
);
718 gi
->escaped_n
= BITMAP_ALLOC (NULL
);
719 gi
->group_kill
= BITMAP_ALLOC (NULL
);
720 gi
->process_globally
= false;
722 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
723 gi
->offset_map_size_n
= 0;
724 gi
->offset_map_size_p
= 0;
725 gi
->offset_map_n
= NULL
;
726 gi
->offset_map_p
= NULL
;
727 VEC_safe_push (group_info_t
, heap
, rtx_group_vec
, gi
);
734 /* Initialization of data structures. */
740 globally_deleted
= 0;
743 scratch
= BITMAP_ALLOC (NULL
);
744 kill_on_calls
= BITMAP_ALLOC (NULL
);
747 = create_alloc_pool ("rtx_store_info_pool",
748 sizeof (struct store_info
), 100);
750 = create_alloc_pool ("read_info_pool",
751 sizeof (struct read_info
), 100);
753 = create_alloc_pool ("insn_info_pool",
754 sizeof (struct insn_info
), 100);
756 = create_alloc_pool ("bb_info_pool",
757 sizeof (struct bb_info
), 100);
759 = create_alloc_pool ("rtx_group_info_pool",
760 sizeof (struct group_info
), 100);
762 = create_alloc_pool ("deferred_change_pool",
763 sizeof (struct deferred_change
), 10);
765 rtx_group_table
= htab_create (11, invariant_group_base_hash
,
766 invariant_group_base_eq
, NULL
);
768 bb_table
= XCNEWVEC (bb_info_t
, last_basic_block
);
769 rtx_group_next_id
= 0;
771 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
773 init_alias_analysis ();
775 if (clear_alias_sets
)
776 clear_alias_group
= get_group_info (NULL
);
778 clear_alias_group
= NULL
;
783 /*----------------------------------------------------------------------------
786 Scan all of the insns. Any random ordering of the blocks is fine.
787 Each block is scanned in forward order to accommodate cselib which
788 is used to remove stores with non-constant bases.
789 ----------------------------------------------------------------------------*/
791 /* Delete all of the store_info recs from INSN_INFO. */
794 free_store_info (insn_info_t insn_info
)
796 store_info_t store_info
= insn_info
->store_rec
;
799 store_info_t next
= store_info
->next
;
800 if (store_info
->is_large
)
801 BITMAP_FREE (store_info
->positions_needed
.large
.bmap
);
802 if (store_info
->cse_base
)
803 pool_free (cse_store_info_pool
, store_info
);
805 pool_free (rtx_store_info_pool
, store_info
);
809 insn_info
->cannot_delete
= true;
810 insn_info
->contains_cselib_groups
= false;
811 insn_info
->store_rec
= NULL
;
817 regset fixed_regs_live
;
819 } note_add_store_info
;
821 /* Callback for emit_inc_dec_insn_before via note_stores.
822 Check if a register is clobbered which is live afterwards. */
825 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
828 note_add_store_info
*info
= (note_add_store_info
*) data
;
834 /* If this register is referenced by the current or an earlier insn,
835 that's OK. E.g. this applies to the register that is being incremented
836 with this addition. */
837 for (insn
= info
->first
;
838 insn
!= NEXT_INSN (info
->current
);
839 insn
= NEXT_INSN (insn
))
840 if (reg_referenced_p (loc
, PATTERN (insn
)))
843 /* If we come here, we have a clobber of a register that's only OK
844 if that register is not live. If we don't have liveness information
845 available, fail now. */
846 if (!info
->fixed_regs_live
)
848 info
->failure
= true;
851 /* Now check if this is a live fixed register. */
853 n
= hard_regno_nregs
[r
][GET_MODE (loc
)];
855 if (REGNO_REG_SET_P (info
->fixed_regs_live
, r
+n
))
856 info
->failure
= true;
859 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
860 SRC + SRCOFF before insn ARG. */
863 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
864 rtx op ATTRIBUTE_UNUSED
,
865 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
867 insn_info_t insn_info
= (insn_info_t
) arg
;
868 rtx insn
= insn_info
->insn
, new_insn
, cur
;
869 note_add_store_info info
;
871 /* We can reuse all operands without copying, because we are about
872 to delete the insn that contained it. */
876 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
877 new_insn
= get_insns ();
881 new_insn
= gen_move_insn (dest
, src
);
882 info
.first
= new_insn
;
883 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
884 info
.failure
= false;
885 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
888 note_stores (PATTERN (cur
), note_add_store
, &info
);
891 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
892 return it immediately, communicating the failure to its caller. */
896 emit_insn_before (new_insn
, insn
);
901 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
902 is there, is split into a separate insn.
903 Return true on success (or if there was nothing to do), false on failure. */
906 check_for_inc_dec_1 (insn_info_t insn_info
)
908 rtx insn
= insn_info
->insn
;
909 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
911 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, insn_info
) == 0;
916 /* Entry point for postreload. If you work on reload_cse, or you need this
917 anywhere else, consider if you can provide register liveness information
918 and add a parameter to this function so that it can be passed down in
919 insn_info.fixed_regs_live. */
921 check_for_inc_dec (rtx insn
)
923 struct insn_info insn_info
;
926 insn_info
.insn
= insn
;
927 insn_info
.fixed_regs_live
= NULL
;
928 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
930 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, &insn_info
) == 0;
934 /* Delete the insn and free all of the fields inside INSN_INFO. */
937 delete_dead_store_insn (insn_info_t insn_info
)
939 read_info_t read_info
;
944 if (!check_for_inc_dec_1 (insn_info
))
948 fprintf (dump_file
, "Locally deleting insn %d ",
949 INSN_UID (insn_info
->insn
));
950 if (insn_info
->store_rec
->alias_set
)
951 fprintf (dump_file
, "alias set %d\n",
952 (int) insn_info
->store_rec
->alias_set
);
954 fprintf (dump_file
, "\n");
957 free_store_info (insn_info
);
958 read_info
= insn_info
->read_rec
;
962 read_info_t next
= read_info
->next
;
963 pool_free (read_info_pool
, read_info
);
966 insn_info
->read_rec
= NULL
;
968 delete_insn (insn_info
->insn
);
970 insn_info
->insn
= NULL
;
972 insn_info
->wild_read
= false;
975 /* Check if EXPR can possibly escape the current function scope. */
977 can_escape (tree expr
)
982 base
= get_base_address (expr
);
984 && !may_be_aliased (base
))
989 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
993 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
997 bool expr_escapes
= can_escape (expr
);
998 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
999 for (i
=offset
; i
<offset
+width
; i
++)
1007 store1
= group
->store1_n
;
1008 store2
= group
->store2_n
;
1009 escaped
= group
->escaped_n
;
1014 store1
= group
->store1_p
;
1015 store2
= group
->store2_p
;
1016 escaped
= group
->escaped_p
;
1020 if (!bitmap_set_bit (store1
, ai
))
1021 bitmap_set_bit (store2
, ai
);
1026 if (group
->offset_map_size_n
< ai
)
1027 group
->offset_map_size_n
= ai
;
1031 if (group
->offset_map_size_p
< ai
)
1032 group
->offset_map_size_p
= ai
;
1036 bitmap_set_bit (escaped
, ai
);
1041 reset_active_stores (void)
1043 active_local_stores
= NULL
;
1044 active_local_stores_len
= 0;
1047 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1050 free_read_records (bb_info_t bb_info
)
1052 insn_info_t insn_info
= bb_info
->last_insn
;
1053 read_info_t
*ptr
= &insn_info
->read_rec
;
1056 read_info_t next
= (*ptr
)->next
;
1057 if ((*ptr
)->alias_set
== 0)
1059 pool_free (read_info_pool
, *ptr
);
1063 ptr
= &(*ptr
)->next
;
1067 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1070 add_wild_read (bb_info_t bb_info
)
1072 insn_info_t insn_info
= bb_info
->last_insn
;
1073 insn_info
->wild_read
= true;
1074 free_read_records (bb_info
);
1075 reset_active_stores ();
1078 /* Set the BB_INFO so that the last insn is marked as a wild read of
1079 non-frame locations. */
1082 add_non_frame_wild_read (bb_info_t bb_info
)
1084 insn_info_t insn_info
= bb_info
->last_insn
;
1085 insn_info
->non_frame_wild_read
= true;
1086 free_read_records (bb_info
);
1087 reset_active_stores ();
1090 /* Return true if X is a constant or one of the registers that behave
1091 as a constant over the life of a function. This is equivalent to
1092 !rtx_varies_p for memory addresses. */
1095 const_or_frame_p (rtx x
)
1097 switch (GET_CODE (x
))
1108 /* Note that we have to test for the actual rtx used for the frame
1109 and arg pointers and not just the register number in case we have
1110 eliminated the frame and/or arg pointer and are using it
1112 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1113 /* The arg pointer varies if it is not a fixed register. */
1114 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1115 || x
== pic_offset_table_rtx
)
1124 /* Take all reasonable action to put the address of MEM into the form
1125 that we can do analysis on.
1127 The gold standard is to get the address into the form: address +
1128 OFFSET where address is something that rtx_varies_p considers a
1129 constant. When we can get the address in this form, we can do
1130 global analysis on it. Note that for constant bases, address is
1131 not actually returned, only the group_id. The address can be
1134 If that fails, we try cselib to get a value we can at least use
1135 locally. If that fails we return false.
1137 The GROUP_ID is set to -1 for cselib bases and the index of the
1138 group for non_varying bases.
1140 FOR_READ is true if this is a mem read and false if not. */
1143 canon_address (rtx mem
,
1144 alias_set_type
*alias_set_out
,
1146 HOST_WIDE_INT
*offset
,
1149 enum machine_mode address_mode
1150 = targetm
.addr_space
.address_mode (MEM_ADDR_SPACE (mem
));
1151 rtx mem_address
= XEXP (mem
, 0);
1152 rtx expanded_address
, address
;
1155 /* Make sure that cselib is has initialized all of the operands of
1156 the address before asking it to do the subst. */
1158 if (clear_alias_sets
)
1160 /* If this is a spill, do not do any further processing. */
1161 alias_set_type alias_set
= MEM_ALIAS_SET (mem
);
1163 fprintf (dump_file
, "found alias set %d\n", (int) alias_set
);
1164 if (bitmap_bit_p (clear_alias_sets
, alias_set
))
1166 struct clear_alias_mode_holder
*entry
1167 = clear_alias_set_lookup (alias_set
);
1169 /* If the modes do not match, we cannot process this set. */
1170 if (entry
->mode
!= GET_MODE (mem
))
1174 "disqualifying alias set %d, (%s) != (%s)\n",
1175 (int) alias_set
, GET_MODE_NAME (entry
->mode
),
1176 GET_MODE_NAME (GET_MODE (mem
)));
1178 bitmap_set_bit (disqualified_clear_alias_sets
, alias_set
);
1182 *alias_set_out
= alias_set
;
1183 *group_id
= clear_alias_group
->id
;
1190 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1194 fprintf (dump_file
, " mem: ");
1195 print_inline_rtx (dump_file
, mem_address
, 0);
1196 fprintf (dump_file
, "\n");
1199 /* First see if just canon_rtx (mem_address) is const or frame,
1200 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1202 for (expanded
= 0; expanded
< 2; expanded
++)
1206 /* Use cselib to replace all of the reg references with the full
1207 expression. This will take care of the case where we have
1209 r_x = base + offset;
1214 val = *(base + offset); */
1216 expanded_address
= cselib_expand_value_rtx (mem_address
,
1219 /* If this fails, just go with the address from first
1221 if (!expanded_address
)
1225 expanded_address
= mem_address
;
1227 /* Split the address into canonical BASE + OFFSET terms. */
1228 address
= canon_rtx (expanded_address
);
1236 fprintf (dump_file
, "\n after cselib_expand address: ");
1237 print_inline_rtx (dump_file
, expanded_address
, 0);
1238 fprintf (dump_file
, "\n");
1241 fprintf (dump_file
, "\n after canon_rtx address: ");
1242 print_inline_rtx (dump_file
, address
, 0);
1243 fprintf (dump_file
, "\n");
1246 if (GET_CODE (address
) == CONST
)
1247 address
= XEXP (address
, 0);
1249 if (GET_CODE (address
) == PLUS
1250 && CONST_INT_P (XEXP (address
, 1)))
1252 *offset
= INTVAL (XEXP (address
, 1));
1253 address
= XEXP (address
, 0);
1256 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1257 && const_or_frame_p (address
))
1259 group_info_t group
= get_group_info (address
);
1262 fprintf (dump_file
, " gid=%d offset=%d \n",
1263 group
->id
, (int)*offset
);
1265 *group_id
= group
->id
;
1270 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1276 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1280 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1281 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1286 /* Clear the rhs field from the active_local_stores array. */
1289 clear_rhs_from_active_local_stores (void)
1291 insn_info_t ptr
= active_local_stores
;
1295 store_info_t store_info
= ptr
->store_rec
;
1296 /* Skip the clobbers. */
1297 while (!store_info
->is_set
)
1298 store_info
= store_info
->next
;
1300 store_info
->rhs
= NULL
;
1301 store_info
->const_rhs
= NULL
;
1303 ptr
= ptr
->next_local_store
;
1308 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1311 set_position_unneeded (store_info_t s_info
, int pos
)
1313 if (__builtin_expect (s_info
->is_large
, false))
1315 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1316 s_info
->positions_needed
.large
.count
++;
1319 s_info
->positions_needed
.small_bitmask
1320 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1323 /* Mark the whole store S_INFO as unneeded. */
1326 set_all_positions_unneeded (store_info_t s_info
)
1328 if (__builtin_expect (s_info
->is_large
, false))
1330 int pos
, end
= s_info
->end
- s_info
->begin
;
1331 for (pos
= 0; pos
< end
; pos
++)
1332 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1333 s_info
->positions_needed
.large
.count
= end
;
1336 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1339 /* Return TRUE if any bytes from S_INFO store are needed. */
1342 any_positions_needed_p (store_info_t s_info
)
1344 if (__builtin_expect (s_info
->is_large
, false))
1345 return (s_info
->positions_needed
.large
.count
1346 < s_info
->end
- s_info
->begin
);
1348 return (s_info
->positions_needed
.small_bitmask
1349 != (unsigned HOST_WIDE_INT
) 0);
1352 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1353 store are needed. */
1356 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1358 if (__builtin_expect (s_info
->is_large
, false))
1360 int end
= start
+ width
;
1362 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1368 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1369 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1374 static rtx
get_stored_val (store_info_t
, enum machine_mode
, HOST_WIDE_INT
,
1375 HOST_WIDE_INT
, basic_block
, bool);
1378 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1379 there is a candidate store, after adding it to the appropriate
1380 local store group if so. */
1383 record_store (rtx body
, bb_info_t bb_info
)
1385 rtx mem
, rhs
, const_rhs
, mem_addr
;
1386 HOST_WIDE_INT offset
= 0;
1387 HOST_WIDE_INT width
= 0;
1388 alias_set_type spill_alias_set
;
1389 insn_info_t insn_info
= bb_info
->last_insn
;
1390 store_info_t store_info
= NULL
;
1392 cselib_val
*base
= NULL
;
1393 insn_info_t ptr
, last
, redundant_reason
;
1394 bool store_is_unused
;
1396 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1399 mem
= SET_DEST (body
);
1401 /* If this is not used, then this cannot be used to keep the insn
1402 from being deleted. On the other hand, it does provide something
1403 that can be used to prove that another store is dead. */
1405 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1407 /* Check whether that value is a suitable memory location. */
1410 /* If the set or clobber is unused, then it does not effect our
1411 ability to get rid of the entire insn. */
1412 if (!store_is_unused
)
1413 insn_info
->cannot_delete
= true;
1417 /* At this point we know mem is a mem. */
1418 if (GET_MODE (mem
) == BLKmode
)
1420 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1423 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1424 add_wild_read (bb_info
);
1425 insn_info
->cannot_delete
= true;
1428 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1429 as memset (addr, 0, 36); */
1430 else if (!MEM_SIZE_KNOWN_P (mem
)
1431 || MEM_SIZE (mem
) <= 0
1432 || MEM_SIZE (mem
) > MAX_OFFSET
1433 || GET_CODE (body
) != SET
1434 || !CONST_INT_P (SET_SRC (body
)))
1436 if (!store_is_unused
)
1438 /* If the set or clobber is unused, then it does not effect our
1439 ability to get rid of the entire insn. */
1440 insn_info
->cannot_delete
= true;
1441 clear_rhs_from_active_local_stores ();
1447 /* We can still process a volatile mem, we just cannot delete it. */
1448 if (MEM_VOLATILE_P (mem
))
1449 insn_info
->cannot_delete
= true;
1451 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1453 clear_rhs_from_active_local_stores ();
1457 if (GET_MODE (mem
) == BLKmode
)
1458 width
= MEM_SIZE (mem
);
1461 width
= GET_MODE_SIZE (GET_MODE (mem
));
1462 gcc_assert ((unsigned) width
<= HOST_BITS_PER_WIDE_INT
);
1465 if (spill_alias_set
)
1467 bitmap store1
= clear_alias_group
->store1_p
;
1468 bitmap store2
= clear_alias_group
->store2_p
;
1470 gcc_assert (GET_MODE (mem
) != BLKmode
);
1472 if (!bitmap_set_bit (store1
, spill_alias_set
))
1473 bitmap_set_bit (store2
, spill_alias_set
);
1475 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1476 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1478 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1481 fprintf (dump_file
, " processing spill store %d(%s)\n",
1482 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1484 else if (group_id
>= 0)
1486 /* In the restrictive case where the base is a constant or the
1487 frame pointer we can do global analysis. */
1490 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
1491 tree expr
= MEM_EXPR (mem
);
1493 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1494 set_usage_bits (group
, offset
, width
, expr
);
1497 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1498 group_id
, (int)offset
, (int)(offset
+width
));
1502 if (may_be_sp_based_p (XEXP (mem
, 0)))
1503 insn_info
->stack_pointer_based
= true;
1504 insn_info
->contains_cselib_groups
= true;
1506 store_info
= (store_info_t
) pool_alloc (cse_store_info_pool
);
1510 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1511 (int)offset
, (int)(offset
+width
));
1514 const_rhs
= rhs
= NULL_RTX
;
1515 if (GET_CODE (body
) == SET
1516 /* No place to keep the value after ra. */
1517 && !reload_completed
1518 && (REG_P (SET_SRC (body
))
1519 || GET_CODE (SET_SRC (body
)) == SUBREG
1520 || CONSTANT_P (SET_SRC (body
)))
1521 && !MEM_VOLATILE_P (mem
)
1522 /* Sometimes the store and reload is used for truncation and
1524 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1526 rhs
= SET_SRC (body
);
1527 if (CONSTANT_P (rhs
))
1529 else if (body
== PATTERN (insn_info
->insn
))
1531 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1532 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1533 const_rhs
= XEXP (tem
, 0);
1535 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1537 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1539 if (tem
&& CONSTANT_P (tem
))
1544 /* Check to see if this stores causes some other stores to be
1546 ptr
= active_local_stores
;
1548 redundant_reason
= NULL
;
1549 mem
= canon_rtx (mem
);
1550 /* For alias_set != 0 canon_true_dependence should be never called. */
1551 if (spill_alias_set
)
1552 mem_addr
= NULL_RTX
;
1556 mem_addr
= base
->val_rtx
;
1560 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
1561 mem_addr
= group
->canon_base_addr
;
1564 mem_addr
= plus_constant (mem_addr
, offset
);
1569 insn_info_t next
= ptr
->next_local_store
;
1570 store_info_t s_info
= ptr
->store_rec
;
1573 /* Skip the clobbers. We delete the active insn if this insn
1574 shadows the set. To have been put on the active list, it
1575 has exactly on set. */
1576 while (!s_info
->is_set
)
1577 s_info
= s_info
->next
;
1579 if (s_info
->alias_set
!= spill_alias_set
)
1581 else if (s_info
->alias_set
)
1583 struct clear_alias_mode_holder
*entry
1584 = clear_alias_set_lookup (s_info
->alias_set
);
1585 /* Generally, spills cannot be processed if and of the
1586 references to the slot have a different mode. But if
1587 we are in the same block and mode is exactly the same
1588 between this store and one before in the same block,
1589 we can still delete it. */
1590 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1591 && (GET_MODE (mem
) == entry
->mode
))
1594 set_all_positions_unneeded (s_info
);
1597 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1598 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1600 else if ((s_info
->group_id
== group_id
)
1601 && (s_info
->cse_base
== base
))
1605 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1606 INSN_UID (ptr
->insn
), s_info
->group_id
,
1607 (int)s_info
->begin
, (int)s_info
->end
);
1609 /* Even if PTR won't be eliminated as unneeded, if both
1610 PTR and this insn store the same constant value, we might
1611 eliminate this insn instead. */
1612 if (s_info
->const_rhs
1614 && offset
>= s_info
->begin
1615 && offset
+ width
<= s_info
->end
1616 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1619 if (GET_MODE (mem
) == BLKmode
)
1621 if (GET_MODE (s_info
->mem
) == BLKmode
1622 && s_info
->const_rhs
== const_rhs
)
1623 redundant_reason
= ptr
;
1625 else if (s_info
->const_rhs
== const0_rtx
1626 && const_rhs
== const0_rtx
)
1627 redundant_reason
= ptr
;
1632 val
= get_stored_val (s_info
, GET_MODE (mem
),
1633 offset
, offset
+ width
,
1634 BLOCK_FOR_INSN (insn_info
->insn
),
1636 if (get_insns () != NULL
)
1639 if (val
&& rtx_equal_p (val
, const_rhs
))
1640 redundant_reason
= ptr
;
1644 for (i
= MAX (offset
, s_info
->begin
);
1645 i
< offset
+ width
&& i
< s_info
->end
;
1647 set_position_unneeded (s_info
, i
- s_info
->begin
);
1649 else if (s_info
->rhs
)
1650 /* Need to see if it is possible for this store to overwrite
1651 the value of store_info. If it is, set the rhs to NULL to
1652 keep it from being used to remove a load. */
1654 if (canon_true_dependence (s_info
->mem
,
1655 GET_MODE (s_info
->mem
),
1660 s_info
->const_rhs
= NULL
;
1664 /* An insn can be deleted if every position of every one of
1665 its s_infos is zero. */
1666 if (any_positions_needed_p (s_info
))
1671 insn_info_t insn_to_delete
= ptr
;
1673 active_local_stores_len
--;
1675 last
->next_local_store
= ptr
->next_local_store
;
1677 active_local_stores
= ptr
->next_local_store
;
1679 if (!insn_to_delete
->cannot_delete
)
1680 delete_dead_store_insn (insn_to_delete
);
1688 /* Finish filling in the store_info. */
1689 store_info
->next
= insn_info
->store_rec
;
1690 insn_info
->store_rec
= store_info
;
1691 store_info
->mem
= mem
;
1692 store_info
->alias_set
= spill_alias_set
;
1693 store_info
->mem_addr
= mem_addr
;
1694 store_info
->cse_base
= base
;
1695 if (width
> HOST_BITS_PER_WIDE_INT
)
1697 store_info
->is_large
= true;
1698 store_info
->positions_needed
.large
.count
= 0;
1699 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (NULL
);
1703 store_info
->is_large
= false;
1704 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1706 store_info
->group_id
= group_id
;
1707 store_info
->begin
= offset
;
1708 store_info
->end
= offset
+ width
;
1709 store_info
->is_set
= GET_CODE (body
) == SET
;
1710 store_info
->rhs
= rhs
;
1711 store_info
->const_rhs
= const_rhs
;
1712 store_info
->redundant_reason
= redundant_reason
;
1714 /* If this is a clobber, we return 0. We will only be able to
1715 delete this insn if there is only one store USED store, but we
1716 can use the clobber to delete other stores earlier. */
1717 return store_info
->is_set
? 1 : 0;
1722 dump_insn_info (const char * start
, insn_info_t insn_info
)
1724 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1725 INSN_UID (insn_info
->insn
),
1726 insn_info
->store_rec
? "has store" : "naked");
1730 /* If the modes are different and the value's source and target do not
1731 line up, we need to extract the value from lower part of the rhs of
1732 the store, shift it, and then put it into a form that can be shoved
1733 into the read_insn. This function generates a right SHIFT of a
1734 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1735 shift sequence is returned or NULL if we failed to find a
1739 find_shift_sequence (int access_size
,
1740 store_info_t store_info
,
1741 enum machine_mode read_mode
,
1742 int shift
, bool speed
, bool require_cst
)
1744 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1745 enum machine_mode new_mode
;
1746 rtx read_reg
= NULL
;
1748 /* Some machines like the x86 have shift insns for each size of
1749 operand. Other machines like the ppc or the ia-64 may only have
1750 shift insns that shift values within 32 or 64 bit registers.
1751 This loop tries to find the smallest shift insn that will right
1752 justify the value we want to read but is available in one insn on
1755 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1757 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1758 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1760 rtx target
, new_reg
, shift_seq
, insn
, new_lhs
;
1763 /* If a constant was stored into memory, try to simplify it here,
1764 otherwise the cost of the shift might preclude this optimization
1765 e.g. at -Os, even when no actual shift will be needed. */
1766 if (store_info
->const_rhs
)
1768 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1769 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1771 if (ret
&& CONSTANT_P (ret
))
1773 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1774 ret
, GEN_INT (shift
));
1775 if (ret
&& CONSTANT_P (ret
))
1777 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1778 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1779 if (ret
&& CONSTANT_P (ret
)
1780 && set_src_cost (ret
, speed
) <= COSTS_N_INSNS (1))
1789 /* Try a wider mode if truncating the store mode to NEW_MODE
1790 requires a real instruction. */
1791 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1792 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1795 /* Also try a wider mode if the necessary punning is either not
1796 desirable or not possible. */
1797 if (!CONSTANT_P (store_info
->rhs
)
1798 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1801 new_reg
= gen_reg_rtx (new_mode
);
1805 /* In theory we could also check for an ashr. Ian Taylor knows
1806 of one dsp where the cost of these two was not the same. But
1807 this really is a rare case anyway. */
1808 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1809 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1811 shift_seq
= get_insns ();
1814 if (target
!= new_reg
|| shift_seq
== NULL
)
1818 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1820 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1822 /* The computation up to here is essentially independent
1823 of the arguments and could be precomputed. It may
1824 not be worth doing so. We could precompute if
1825 worthwhile or at least cache the results. The result
1826 technically depends on both SHIFT and ACCESS_SIZE,
1827 but in practice the answer will depend only on ACCESS_SIZE. */
1829 if (cost
> COSTS_N_INSNS (1))
1832 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1833 copy_rtx (store_info
->rhs
));
1834 if (new_lhs
== NULL_RTX
)
1837 /* We found an acceptable shift. Generate a move to
1838 take the value from the store and put it into the
1839 shift pseudo, then shift it, then generate another
1840 move to put in into the target of the read. */
1841 emit_move_insn (new_reg
, new_lhs
);
1842 emit_insn (shift_seq
);
1843 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1851 /* Call back for note_stores to find the hard regs set or clobbered by
1852 insn. Data is a bitmap of the hardregs set so far. */
1855 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1857 bitmap regs_set
= (bitmap
) data
;
1860 && HARD_REGISTER_P (x
))
1862 unsigned int regno
= REGNO (x
);
1863 bitmap_set_range (regs_set
, regno
,
1864 hard_regno_nregs
[regno
][GET_MODE (x
)]);
1868 /* Helper function for replace_read and record_store.
1869 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1870 to one before READ_END bytes read in READ_MODE. Return NULL
1871 if not successful. If REQUIRE_CST is true, return always constant. */
1874 get_stored_val (store_info_t store_info
, enum machine_mode read_mode
,
1875 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1876 basic_block bb
, bool require_cst
)
1878 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1880 int access_size
; /* In bytes. */
1883 /* To get here the read is within the boundaries of the write so
1884 shift will never be negative. Start out with the shift being in
1886 if (store_mode
== BLKmode
)
1888 else if (BYTES_BIG_ENDIAN
)
1889 shift
= store_info
->end
- read_end
;
1891 shift
= read_begin
- store_info
->begin
;
1893 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1895 /* From now on it is bits. */
1896 shift
*= BITS_PER_UNIT
;
1899 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1900 optimize_bb_for_speed_p (bb
),
1902 else if (store_mode
== BLKmode
)
1904 /* The store is a memset (addr, const_val, const_size). */
1905 gcc_assert (CONST_INT_P (store_info
->rhs
));
1906 store_mode
= int_mode_for_mode (read_mode
);
1907 if (store_mode
== BLKmode
)
1908 read_reg
= NULL_RTX
;
1909 else if (store_info
->rhs
== const0_rtx
)
1910 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1911 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1912 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1913 read_reg
= NULL_RTX
;
1916 unsigned HOST_WIDE_INT c
1917 = INTVAL (store_info
->rhs
)
1918 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1919 int shift
= BITS_PER_UNIT
;
1920 while (shift
< HOST_BITS_PER_WIDE_INT
)
1925 read_reg
= gen_int_mode (c
, store_mode
);
1926 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1929 else if (store_info
->const_rhs
1931 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1932 read_reg
= extract_low_bits (read_mode
, store_mode
,
1933 copy_rtx (store_info
->const_rhs
));
1935 read_reg
= extract_low_bits (read_mode
, store_mode
,
1936 copy_rtx (store_info
->rhs
));
1937 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1938 read_reg
= NULL_RTX
;
1942 /* Take a sequence of:
1965 Depending on the alignment and the mode of the store and
1969 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1970 and READ_INSN are for the read. Return true if the replacement
1974 replace_read (store_info_t store_info
, insn_info_t store_insn
,
1975 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
1978 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1979 enum machine_mode read_mode
= GET_MODE (read_info
->mem
);
1980 rtx insns
, this_insn
, read_reg
;
1986 /* Create a sequence of instructions to set up the read register.
1987 This sequence goes immediately before the store and its result
1988 is read by the load.
1990 We need to keep this in perspective. We are replacing a read
1991 with a sequence of insns, but the read will almost certainly be
1992 in cache, so it is not going to be an expensive one. Thus, we
1993 are not willing to do a multi insn shift or worse a subroutine
1994 call to get rid of the read. */
1996 fprintf (dump_file
, "trying to replace %smode load in insn %d"
1997 " from %smode store in insn %d\n",
1998 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
1999 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
2001 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
2002 read_reg
= get_stored_val (store_info
,
2003 read_mode
, read_info
->begin
, read_info
->end
,
2005 if (read_reg
== NULL_RTX
)
2009 fprintf (dump_file
, " -- could not extract bits of stored value\n");
2012 /* Force the value into a new register so that it won't be clobbered
2013 between the store and the load. */
2014 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
2015 insns
= get_insns ();
2018 if (insns
!= NULL_RTX
)
2020 /* Now we have to scan the set of new instructions to see if the
2021 sequence contains and sets of hardregs that happened to be
2022 live at this point. For instance, this can happen if one of
2023 the insns sets the CC and the CC happened to be live at that
2024 point. This does occasionally happen, see PR 37922. */
2025 bitmap regs_set
= BITMAP_ALLOC (NULL
);
2027 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2028 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2030 bitmap_and_into (regs_set
, regs_live
);
2031 if (!bitmap_empty_p (regs_set
))
2036 "abandoning replacement because sequence clobbers live hardregs:");
2037 df_print_regset (dump_file
, regs_set
);
2040 BITMAP_FREE (regs_set
);
2043 BITMAP_FREE (regs_set
);
2046 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2048 deferred_change_t deferred_change
=
2049 (deferred_change_t
) pool_alloc (deferred_change_pool
);
2051 /* Insert this right before the store insn where it will be safe
2052 from later insns that might change it before the read. */
2053 emit_insn_before (insns
, store_insn
->insn
);
2055 /* And now for the kludge part: cselib croaks if you just
2056 return at this point. There are two reasons for this:
2058 1) Cselib has an idea of how many pseudos there are and
2059 that does not include the new ones we just added.
2061 2) Cselib does not know about the move insn we added
2062 above the store_info, and there is no way to tell it
2063 about it, because it has "moved on".
2065 Problem (1) is fixable with a certain amount of engineering.
2066 Problem (2) is requires starting the bb from scratch. This
2069 So we are just going to have to lie. The move/extraction
2070 insns are not really an issue, cselib did not see them. But
2071 the use of the new pseudo read_insn is a real problem because
2072 cselib has not scanned this insn. The way that we solve this
2073 problem is that we are just going to put the mem back for now
2074 and when we are finished with the block, we undo this. We
2075 keep a table of mems to get rid of. At the end of the basic
2076 block we can put them back. */
2078 *loc
= read_info
->mem
;
2079 deferred_change
->next
= deferred_change_list
;
2080 deferred_change_list
= deferred_change
;
2081 deferred_change
->loc
= loc
;
2082 deferred_change
->reg
= read_reg
;
2084 /* Get rid of the read_info, from the point of view of the
2085 rest of dse, play like this read never happened. */
2086 read_insn
->read_rec
= read_info
->next
;
2087 pool_free (read_info_pool
, read_info
);
2090 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2091 print_simple_rtl (dump_file
, read_reg
);
2092 fprintf (dump_file
, "\n");
2100 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2101 print_simple_rtl (dump_file
, read_reg
);
2102 fprintf (dump_file
, " led to an invalid instruction\n");
2108 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2109 if LOC is a mem and if it is look at the address and kill any
2110 appropriate stores that may be active. */
2113 check_mem_read_rtx (rtx
*loc
, void *data
)
2115 rtx mem
= *loc
, mem_addr
;
2117 insn_info_t insn_info
;
2118 HOST_WIDE_INT offset
= 0;
2119 HOST_WIDE_INT width
= 0;
2120 alias_set_type spill_alias_set
= 0;
2121 cselib_val
*base
= NULL
;
2123 read_info_t read_info
;
2125 if (!mem
|| !MEM_P (mem
))
2128 bb_info
= (bb_info_t
) data
;
2129 insn_info
= bb_info
->last_insn
;
2131 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2132 || (MEM_VOLATILE_P (mem
)))
2135 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2136 add_wild_read (bb_info
);
2137 insn_info
->cannot_delete
= true;
2141 /* If it is reading readonly mem, then there can be no conflict with
2143 if (MEM_READONLY_P (mem
))
2146 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2149 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2150 add_wild_read (bb_info
);
2154 if (GET_MODE (mem
) == BLKmode
)
2157 width
= GET_MODE_SIZE (GET_MODE (mem
));
2159 read_info
= (read_info_t
) pool_alloc (read_info_pool
);
2160 read_info
->group_id
= group_id
;
2161 read_info
->mem
= mem
;
2162 read_info
->alias_set
= spill_alias_set
;
2163 read_info
->begin
= offset
;
2164 read_info
->end
= offset
+ width
;
2165 read_info
->next
= insn_info
->read_rec
;
2166 insn_info
->read_rec
= read_info
;
2167 /* For alias_set != 0 canon_true_dependence should be never called. */
2168 if (spill_alias_set
)
2169 mem_addr
= NULL_RTX
;
2173 mem_addr
= base
->val_rtx
;
2177 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
2178 mem_addr
= group
->canon_base_addr
;
2181 mem_addr
= plus_constant (mem_addr
, offset
);
2184 /* We ignore the clobbers in store_info. The is mildly aggressive,
2185 but there really should not be a clobber followed by a read. */
2187 if (spill_alias_set
)
2189 insn_info_t i_ptr
= active_local_stores
;
2190 insn_info_t last
= NULL
;
2193 fprintf (dump_file
, " processing spill load %d\n",
2194 (int) spill_alias_set
);
2198 store_info_t store_info
= i_ptr
->store_rec
;
2200 /* Skip the clobbers. */
2201 while (!store_info
->is_set
)
2202 store_info
= store_info
->next
;
2204 if (store_info
->alias_set
== spill_alias_set
)
2207 dump_insn_info ("removing from active", i_ptr
);
2209 active_local_stores_len
--;
2211 last
->next_local_store
= i_ptr
->next_local_store
;
2213 active_local_stores
= i_ptr
->next_local_store
;
2217 i_ptr
= i_ptr
->next_local_store
;
2220 else if (group_id
>= 0)
2222 /* This is the restricted case where the base is a constant or
2223 the frame pointer and offset is a constant. */
2224 insn_info_t i_ptr
= active_local_stores
;
2225 insn_info_t last
= NULL
;
2230 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2233 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2234 group_id
, (int)offset
, (int)(offset
+width
));
2239 bool remove
= false;
2240 store_info_t store_info
= i_ptr
->store_rec
;
2242 /* Skip the clobbers. */
2243 while (!store_info
->is_set
)
2244 store_info
= store_info
->next
;
2246 /* There are three cases here. */
2247 if (store_info
->group_id
< 0)
2248 /* We have a cselib store followed by a read from a
2251 = canon_true_dependence (store_info
->mem
,
2252 GET_MODE (store_info
->mem
),
2253 store_info
->mem_addr
,
2256 else if (group_id
== store_info
->group_id
)
2258 /* This is a block mode load. We may get lucky and
2259 canon_true_dependence may save the day. */
2262 = canon_true_dependence (store_info
->mem
,
2263 GET_MODE (store_info
->mem
),
2264 store_info
->mem_addr
,
2267 /* If this read is just reading back something that we just
2268 stored, rewrite the read. */
2272 && offset
>= store_info
->begin
2273 && offset
+ width
<= store_info
->end
2274 && all_positions_needed_p (store_info
,
2275 offset
- store_info
->begin
,
2277 && replace_read (store_info
, i_ptr
, read_info
,
2278 insn_info
, loc
, bb_info
->regs_live
))
2281 /* The bases are the same, just see if the offsets
2283 if ((offset
< store_info
->end
)
2284 && (offset
+ width
> store_info
->begin
))
2290 The else case that is missing here is that the
2291 bases are constant but different. There is nothing
2292 to do here because there is no overlap. */
2297 dump_insn_info ("removing from active", i_ptr
);
2299 active_local_stores_len
--;
2301 last
->next_local_store
= i_ptr
->next_local_store
;
2303 active_local_stores
= i_ptr
->next_local_store
;
2307 i_ptr
= i_ptr
->next_local_store
;
2312 insn_info_t i_ptr
= active_local_stores
;
2313 insn_info_t last
= NULL
;
2316 fprintf (dump_file
, " processing cselib load mem:");
2317 print_inline_rtx (dump_file
, mem
, 0);
2318 fprintf (dump_file
, "\n");
2323 bool remove
= false;
2324 store_info_t store_info
= i_ptr
->store_rec
;
2327 fprintf (dump_file
, " processing cselib load against insn %d\n",
2328 INSN_UID (i_ptr
->insn
));
2330 /* Skip the clobbers. */
2331 while (!store_info
->is_set
)
2332 store_info
= store_info
->next
;
2334 /* If this read is just reading back something that we just
2335 stored, rewrite the read. */
2337 && store_info
->group_id
== -1
2338 && store_info
->cse_base
== base
2340 && offset
>= store_info
->begin
2341 && offset
+ width
<= store_info
->end
2342 && all_positions_needed_p (store_info
,
2343 offset
- store_info
->begin
, width
)
2344 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2345 bb_info
->regs_live
))
2348 if (!store_info
->alias_set
)
2349 remove
= canon_true_dependence (store_info
->mem
,
2350 GET_MODE (store_info
->mem
),
2351 store_info
->mem_addr
,
2357 dump_insn_info ("removing from active", i_ptr
);
2359 active_local_stores_len
--;
2361 last
->next_local_store
= i_ptr
->next_local_store
;
2363 active_local_stores
= i_ptr
->next_local_store
;
2367 i_ptr
= i_ptr
->next_local_store
;
2373 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2374 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2375 true for any part of *LOC. */
2378 check_mem_read_use (rtx
*loc
, void *data
)
2380 for_each_rtx (loc
, check_mem_read_rtx
, data
);
2384 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2385 So far it only handles arguments passed in registers. */
2388 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2390 CUMULATIVE_ARGS args_so_far_v
;
2391 cumulative_args_t args_so_far
;
2395 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2396 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2398 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2400 arg
!= void_list_node
&& idx
< nargs
;
2401 arg
= TREE_CHAIN (arg
), idx
++)
2403 enum machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2405 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2406 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2407 || GET_MODE_CLASS (mode
) != MODE_INT
)
2410 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2412 link
= XEXP (link
, 1))
2413 if (GET_CODE (XEXP (link
, 0)) == USE
)
2415 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2416 if (REG_P (args
[idx
])
2417 && REGNO (args
[idx
]) == REGNO (reg
)
2418 && (GET_MODE (args
[idx
]) == mode
2419 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2420 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2422 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2423 > GET_MODE_SIZE (mode
)))))
2429 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2430 if (GET_MODE (args
[idx
]) != mode
)
2432 if (!tmp
|| !CONST_INT_P (tmp
))
2434 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2439 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2441 if (arg
!= void_list_node
|| idx
!= nargs
)
2446 /* Return a bitmap of the fixed registers contained in IN. */
2449 copy_fixed_regs (const_bitmap in
)
2453 ret
= ALLOC_REG_SET (NULL
);
2454 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2458 /* Apply record_store to all candidate stores in INSN. Mark INSN
2459 if some part of it is not a candidate store and assigns to a
2460 non-register target. */
2463 scan_insn (bb_info_t bb_info
, rtx insn
)
2466 insn_info_t insn_info
= (insn_info_t
) pool_alloc (insn_info_pool
);
2468 memset (insn_info
, 0, sizeof (struct insn_info
));
2471 fprintf (dump_file
, "\n**scanning insn=%d\n",
2474 insn_info
->prev_insn
= bb_info
->last_insn
;
2475 insn_info
->insn
= insn
;
2476 bb_info
->last_insn
= insn_info
;
2478 if (DEBUG_INSN_P (insn
))
2480 insn_info
->cannot_delete
= true;
2484 /* Cselib clears the table for this case, so we have to essentially
2486 if (NONJUMP_INSN_P (insn
)
2487 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2488 && MEM_VOLATILE_P (PATTERN (insn
)))
2490 add_wild_read (bb_info
);
2491 insn_info
->cannot_delete
= true;
2495 /* Look at all of the uses in the insn. */
2496 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2501 tree memset_call
= NULL_TREE
;
2503 insn_info
->cannot_delete
= true;
2505 /* Const functions cannot do anything bad i.e. read memory,
2506 however, they can read their parameters which may have
2507 been pushed onto the stack.
2508 memset and bzero don't read memory either. */
2509 const_call
= RTL_CONST_CALL_P (insn
);
2512 rtx call
= PATTERN (insn
);
2513 if (GET_CODE (call
) == PARALLEL
)
2514 call
= XVECEXP (call
, 0, 0);
2515 if (GET_CODE (call
) == SET
)
2516 call
= SET_SRC (call
);
2517 if (GET_CODE (call
) == CALL
2518 && MEM_P (XEXP (call
, 0))
2519 && GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2521 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2522 if (SYMBOL_REF_DECL (symbol
)
2523 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2525 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2527 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2528 == BUILT_IN_MEMSET
))
2529 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2530 memset_call
= SYMBOL_REF_DECL (symbol
);
2534 if (const_call
|| memset_call
)
2536 insn_info_t i_ptr
= active_local_stores
;
2537 insn_info_t last
= NULL
;
2540 fprintf (dump_file
, "%s call %d\n",
2541 const_call
? "const" : "memset", INSN_UID (insn
));
2543 /* See the head comment of the frame_read field. */
2544 if (reload_completed
)
2545 insn_info
->frame_read
= true;
2547 /* Loop over the active stores and remove those which are
2548 killed by the const function call. */
2551 bool remove_store
= false;
2553 /* The stack pointer based stores are always killed. */
2554 if (i_ptr
->stack_pointer_based
)
2555 remove_store
= true;
2557 /* If the frame is read, the frame related stores are killed. */
2558 else if (insn_info
->frame_read
)
2560 store_info_t store_info
= i_ptr
->store_rec
;
2562 /* Skip the clobbers. */
2563 while (!store_info
->is_set
)
2564 store_info
= store_info
->next
;
2566 if (store_info
->group_id
>= 0
2567 && VEC_index (group_info_t
, rtx_group_vec
,
2568 store_info
->group_id
)->frame_related
)
2569 remove_store
= true;
2575 dump_insn_info ("removing from active", i_ptr
);
2577 active_local_stores_len
--;
2579 last
->next_local_store
= i_ptr
->next_local_store
;
2581 active_local_stores
= i_ptr
->next_local_store
;
2586 i_ptr
= i_ptr
->next_local_store
;
2592 if (get_call_args (insn
, memset_call
, args
, 3)
2593 && CONST_INT_P (args
[1])
2594 && CONST_INT_P (args
[2])
2595 && INTVAL (args
[2]) > 0)
2597 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2598 set_mem_size (mem
, INTVAL (args
[2]));
2599 body
= gen_rtx_SET (VOIDmode
, mem
, args
[1]);
2600 mems_found
+= record_store (body
, bb_info
);
2602 fprintf (dump_file
, "handling memset as BLKmode store\n");
2603 if (mems_found
== 1)
2605 if (active_local_stores_len
++
2606 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2608 active_local_stores_len
= 1;
2609 active_local_stores
= NULL
;
2611 insn_info
->fixed_regs_live
2612 = copy_fixed_regs (bb_info
->regs_live
);
2613 insn_info
->next_local_store
= active_local_stores
;
2614 active_local_stores
= insn_info
;
2621 /* Every other call, including pure functions, may read any memory
2622 that is not relative to the frame. */
2623 add_non_frame_wild_read (bb_info
);
2628 /* Assuming that there are sets in these insns, we cannot delete
2630 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2631 || volatile_refs_p (PATTERN (insn
))
2632 || insn_could_throw_p (insn
)
2633 || (RTX_FRAME_RELATED_P (insn
))
2634 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2635 insn_info
->cannot_delete
= true;
2637 body
= PATTERN (insn
);
2638 if (GET_CODE (body
) == PARALLEL
)
2641 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2642 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2645 mems_found
+= record_store (body
, bb_info
);
2648 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2649 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2651 /* If we found some sets of mems, add it into the active_local_stores so
2652 that it can be locally deleted if found dead or used for
2653 replace_read and redundant constant store elimination. Otherwise mark
2654 it as cannot delete. This simplifies the processing later. */
2655 if (mems_found
== 1)
2657 if (active_local_stores_len
++
2658 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2660 active_local_stores_len
= 1;
2661 active_local_stores
= NULL
;
2663 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2664 insn_info
->next_local_store
= active_local_stores
;
2665 active_local_stores
= insn_info
;
2668 insn_info
->cannot_delete
= true;
2672 /* Remove BASE from the set of active_local_stores. This is a
2673 callback from cselib that is used to get rid of the stores in
2674 active_local_stores. */
2677 remove_useless_values (cselib_val
*base
)
2679 insn_info_t insn_info
= active_local_stores
;
2680 insn_info_t last
= NULL
;
2684 store_info_t store_info
= insn_info
->store_rec
;
2687 /* If ANY of the store_infos match the cselib group that is
2688 being deleted, then the insn can not be deleted. */
2691 if ((store_info
->group_id
== -1)
2692 && (store_info
->cse_base
== base
))
2697 store_info
= store_info
->next
;
2702 active_local_stores_len
--;
2704 last
->next_local_store
= insn_info
->next_local_store
;
2706 active_local_stores
= insn_info
->next_local_store
;
2707 free_store_info (insn_info
);
2712 insn_info
= insn_info
->next_local_store
;
2717 /* Do all of step 1. */
2723 bitmap regs_live
= BITMAP_ALLOC (NULL
);
2726 all_blocks
= BITMAP_ALLOC (NULL
);
2727 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2728 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2733 bb_info_t bb_info
= (bb_info_t
) pool_alloc (bb_info_pool
);
2735 memset (bb_info
, 0, sizeof (struct bb_info
));
2736 bitmap_set_bit (all_blocks
, bb
->index
);
2737 bb_info
->regs_live
= regs_live
;
2739 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2740 df_simulate_initialize_forwards (bb
, regs_live
);
2742 bb_table
[bb
->index
] = bb_info
;
2743 cselib_discard_hook
= remove_useless_values
;
2745 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2750 = create_alloc_pool ("cse_store_info_pool",
2751 sizeof (struct store_info
), 100);
2752 active_local_stores
= NULL
;
2753 active_local_stores_len
= 0;
2754 cselib_clear_table ();
2756 /* Scan the insns. */
2757 FOR_BB_INSNS (bb
, insn
)
2760 scan_insn (bb_info
, insn
);
2761 cselib_process_insn (insn
);
2763 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2766 /* This is something of a hack, because the global algorithm
2767 is supposed to take care of the case where stores go dead
2768 at the end of the function. However, the global
2769 algorithm must take a more conservative view of block
2770 mode reads than the local alg does. So to get the case
2771 where you have a store to the frame followed by a non
2772 overlapping block more read, we look at the active local
2773 stores at the end of the function and delete all of the
2774 frame and spill based ones. */
2775 if (stores_off_frame_dead_at_return
2776 && (EDGE_COUNT (bb
->succs
) == 0
2777 || (single_succ_p (bb
)
2778 && single_succ (bb
) == EXIT_BLOCK_PTR
2779 && ! crtl
->calls_eh_return
)))
2781 insn_info_t i_ptr
= active_local_stores
;
2784 store_info_t store_info
= i_ptr
->store_rec
;
2786 /* Skip the clobbers. */
2787 while (!store_info
->is_set
)
2788 store_info
= store_info
->next
;
2789 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2790 delete_dead_store_insn (i_ptr
);
2792 if (store_info
->group_id
>= 0)
2795 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
2796 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2797 delete_dead_store_insn (i_ptr
);
2800 i_ptr
= i_ptr
->next_local_store
;
2804 /* Get rid of the loads that were discovered in
2805 replace_read. Cselib is finished with this block. */
2806 while (deferred_change_list
)
2808 deferred_change_t next
= deferred_change_list
->next
;
2810 /* There is no reason to validate this change. That was
2812 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2813 pool_free (deferred_change_pool
, deferred_change_list
);
2814 deferred_change_list
= next
;
2817 /* Get rid of all of the cselib based store_infos in this
2818 block and mark the containing insns as not being
2820 ptr
= bb_info
->last_insn
;
2823 if (ptr
->contains_cselib_groups
)
2825 store_info_t s_info
= ptr
->store_rec
;
2826 while (s_info
&& !s_info
->is_set
)
2827 s_info
= s_info
->next
;
2829 && s_info
->redundant_reason
2830 && s_info
->redundant_reason
->insn
2831 && !ptr
->cannot_delete
)
2834 fprintf (dump_file
, "Locally deleting insn %d "
2835 "because insn %d stores the "
2836 "same value and couldn't be "
2838 INSN_UID (ptr
->insn
),
2839 INSN_UID (s_info
->redundant_reason
->insn
));
2840 delete_dead_store_insn (ptr
);
2843 s_info
->redundant_reason
= NULL
;
2844 free_store_info (ptr
);
2848 store_info_t s_info
;
2850 /* Free at least positions_needed bitmaps. */
2851 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2852 if (s_info
->is_large
)
2854 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2855 s_info
->is_large
= false;
2858 ptr
= ptr
->prev_insn
;
2861 free_alloc_pool (cse_store_info_pool
);
2863 bb_info
->regs_live
= NULL
;
2866 BITMAP_FREE (regs_live
);
2868 htab_empty (rtx_group_table
);
2872 /*----------------------------------------------------------------------------
2875 Assign each byte position in the stores that we are going to
2876 analyze globally to a position in the bitmaps. Returns true if
2877 there are any bit positions assigned.
2878 ----------------------------------------------------------------------------*/
2881 dse_step2_init (void)
2886 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
2888 /* For all non stack related bases, we only consider a store to
2889 be deletable if there are two or more stores for that
2890 position. This is because it takes one store to make the
2891 other store redundant. However, for the stores that are
2892 stack related, we consider them if there is only one store
2893 for the position. We do this because the stack related
2894 stores can be deleted if their is no read between them and
2895 the end of the function.
2897 To make this work in the current framework, we take the stack
2898 related bases add all of the bits from store1 into store2.
2899 This has the effect of making the eligible even if there is
2902 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2904 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2905 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2907 fprintf (dump_file
, "group %d is frame related ", i
);
2910 group
->offset_map_size_n
++;
2911 group
->offset_map_n
= XNEWVEC (int, group
->offset_map_size_n
);
2912 group
->offset_map_size_p
++;
2913 group
->offset_map_p
= XNEWVEC (int, group
->offset_map_size_p
);
2914 group
->process_globally
= false;
2917 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2918 (int)bitmap_count_bits (group
->store2_n
),
2919 (int)bitmap_count_bits (group
->store2_p
));
2920 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2921 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2927 /* Init the offset tables for the normal case. */
2930 dse_step2_nospill (void)
2934 /* Position 0 is unused because 0 is used in the maps to mean
2936 current_position
= 1;
2937 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
2942 if (group
== clear_alias_group
)
2945 memset (group
->offset_map_n
, 0, sizeof(int) * group
->offset_map_size_n
);
2946 memset (group
->offset_map_p
, 0, sizeof(int) * group
->offset_map_size_p
);
2947 bitmap_clear (group
->group_kill
);
2949 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2951 bitmap_set_bit (group
->group_kill
, current_position
);
2952 if (bitmap_bit_p (group
->escaped_n
, j
))
2953 bitmap_set_bit (kill_on_calls
, current_position
);
2954 group
->offset_map_n
[j
] = current_position
++;
2955 group
->process_globally
= true;
2957 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2959 bitmap_set_bit (group
->group_kill
, current_position
);
2960 if (bitmap_bit_p (group
->escaped_p
, j
))
2961 bitmap_set_bit (kill_on_calls
, current_position
);
2962 group
->offset_map_p
[j
] = current_position
++;
2963 group
->process_globally
= true;
2966 return current_position
!= 1;
2970 /* Init the offset tables for the spill case. */
2973 dse_step2_spill (void)
2976 group_info_t group
= clear_alias_group
;
2979 /* Position 0 is unused because 0 is used in the maps to mean
2981 current_position
= 1;
2985 bitmap_print (dump_file
, clear_alias_sets
,
2986 "clear alias sets ", "\n");
2987 bitmap_print (dump_file
, disqualified_clear_alias_sets
,
2988 "disqualified clear alias sets ", "\n");
2991 memset (group
->offset_map_n
, 0, sizeof(int) * group
->offset_map_size_n
);
2992 memset (group
->offset_map_p
, 0, sizeof(int) * group
->offset_map_size_p
);
2993 bitmap_clear (group
->group_kill
);
2995 /* Remove the disqualified positions from the store2_p set. */
2996 bitmap_and_compl_into (group
->store2_p
, disqualified_clear_alias_sets
);
2998 /* We do not need to process the store2_n set because
2999 alias_sets are always positive. */
3000 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
3002 bitmap_set_bit (group
->group_kill
, current_position
);
3003 group
->offset_map_p
[j
] = current_position
++;
3004 group
->process_globally
= true;
3007 return current_position
!= 1;
3012 /*----------------------------------------------------------------------------
3015 Build the bit vectors for the transfer functions.
3016 ----------------------------------------------------------------------------*/
3019 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
3023 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
3027 HOST_WIDE_INT offset_p
= -offset
;
3028 if (offset_p
>= group_info
->offset_map_size_n
)
3030 return group_info
->offset_map_n
[offset_p
];
3034 if (offset
>= group_info
->offset_map_size_p
)
3036 return group_info
->offset_map_p
[offset
];
3041 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3045 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3050 group_info_t group_info
3051 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
3052 if (group_info
->process_globally
)
3053 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3055 int index
= get_bitmap_index (group_info
, i
);
3058 bitmap_set_bit (gen
, index
);
3060 bitmap_clear_bit (kill
, index
);
3063 store_info
= store_info
->next
;
3068 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3072 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3076 if (store_info
->alias_set
)
3078 int index
= get_bitmap_index (clear_alias_group
,
3079 store_info
->alias_set
);
3082 bitmap_set_bit (gen
, index
);
3084 bitmap_clear_bit (kill
, index
);
3087 store_info
= store_info
->next
;
3092 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3096 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3098 read_info_t read_info
= insn_info
->read_rec
;
3102 /* If this insn reads the frame, kill all the frame related stores. */
3103 if (insn_info
->frame_read
)
3105 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3106 if (group
->process_globally
&& group
->frame_related
)
3109 bitmap_ior_into (kill
, group
->group_kill
);
3110 bitmap_and_compl_into (gen
, group
->group_kill
);
3113 if (insn_info
->non_frame_wild_read
)
3115 /* Kill all non-frame related stores. Kill all stores of variables that
3118 bitmap_ior_into (kill
, kill_on_calls
);
3119 bitmap_and_compl_into (gen
, kill_on_calls
);
3120 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3121 if (group
->process_globally
&& !group
->frame_related
)
3124 bitmap_ior_into (kill
, group
->group_kill
);
3125 bitmap_and_compl_into (gen
, group
->group_kill
);
3130 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3132 if (group
->process_globally
)
3134 if (i
== read_info
->group_id
)
3136 if (read_info
->begin
> read_info
->end
)
3138 /* Begin > end for block mode reads. */
3140 bitmap_ior_into (kill
, group
->group_kill
);
3141 bitmap_and_compl_into (gen
, group
->group_kill
);
3145 /* The groups are the same, just process the
3148 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3150 int index
= get_bitmap_index (group
, j
);
3154 bitmap_set_bit (kill
, index
);
3155 bitmap_clear_bit (gen
, index
);
3162 /* The groups are different, if the alias sets
3163 conflict, clear the entire group. We only need
3164 to apply this test if the read_info is a cselib
3165 read. Anything with a constant base cannot alias
3166 something else with a different constant
3168 if ((read_info
->group_id
< 0)
3169 && canon_true_dependence (group
->base_mem
,
3170 GET_MODE (group
->base_mem
),
3171 group
->canon_base_addr
,
3172 read_info
->mem
, NULL_RTX
))
3175 bitmap_ior_into (kill
, group
->group_kill
);
3176 bitmap_and_compl_into (gen
, group
->group_kill
);
3182 read_info
= read_info
->next
;
3186 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3190 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3194 if (read_info
->alias_set
)
3196 int index
= get_bitmap_index (clear_alias_group
,
3197 read_info
->alias_set
);
3201 bitmap_set_bit (kill
, index
);
3202 bitmap_clear_bit (gen
, index
);
3206 read_info
= read_info
->next
;
3211 /* Return the insn in BB_INFO before the first wild read or if there
3212 are no wild reads in the block, return the last insn. */
3215 find_insn_before_first_wild_read (bb_info_t bb_info
)
3217 insn_info_t insn_info
= bb_info
->last_insn
;
3218 insn_info_t last_wild_read
= NULL
;
3222 if (insn_info
->wild_read
)
3224 last_wild_read
= insn_info
->prev_insn
;
3225 /* Block starts with wild read. */
3226 if (!last_wild_read
)
3230 insn_info
= insn_info
->prev_insn
;
3234 return last_wild_read
;
3236 return bb_info
->last_insn
;
3240 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3241 the block in order to build the gen and kill sets for the block.
3242 We start at ptr which may be the last insn in the block or may be
3243 the first insn with a wild read. In the latter case we are able to
3244 skip the rest of the block because it just does not matter:
3245 anything that happens is hidden by the wild read. */
3248 dse_step3_scan (bool for_spills
, basic_block bb
)
3250 bb_info_t bb_info
= bb_table
[bb
->index
];
3251 insn_info_t insn_info
;
3254 /* There are no wild reads in the spill case. */
3255 insn_info
= bb_info
->last_insn
;
3257 insn_info
= find_insn_before_first_wild_read (bb_info
);
3259 /* In the spill case or in the no_spill case if there is no wild
3260 read in the block, we will need a kill set. */
3261 if (insn_info
== bb_info
->last_insn
)
3264 bitmap_clear (bb_info
->kill
);
3266 bb_info
->kill
= BITMAP_ALLOC (NULL
);
3270 BITMAP_FREE (bb_info
->kill
);
3274 /* There may have been code deleted by the dce pass run before
3276 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3278 /* Process the read(s) last. */
3281 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3282 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3286 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3287 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3291 insn_info
= insn_info
->prev_insn
;
3296 /* Set the gen set of the exit block, and also any block with no
3297 successors that does not have a wild read. */
3300 dse_step3_exit_block_scan (bb_info_t bb_info
)
3302 /* The gen set is all 0's for the exit block except for the
3303 frame_pointer_group. */
3305 if (stores_off_frame_dead_at_return
)
3310 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3312 if (group
->process_globally
&& group
->frame_related
)
3313 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3319 /* Find all of the blocks that are not backwards reachable from the
3320 exit block or any block with no successors (BB). These are the
3321 infinite loops or infinite self loops. These blocks will still
3322 have their bits set in UNREACHABLE_BLOCKS. */
3325 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3330 if (TEST_BIT (unreachable_blocks
, bb
->index
))
3332 RESET_BIT (unreachable_blocks
, bb
->index
);
3333 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3335 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3340 /* Build the transfer functions for the function. */
3343 dse_step3 (bool for_spills
)
3346 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block
);
3347 sbitmap_iterator sbi
;
3348 bitmap all_ones
= NULL
;
3351 sbitmap_ones (unreachable_blocks
);
3355 bb_info_t bb_info
= bb_table
[bb
->index
];
3357 bitmap_clear (bb_info
->gen
);
3359 bb_info
->gen
= BITMAP_ALLOC (NULL
);
3361 if (bb
->index
== ENTRY_BLOCK
)
3363 else if (bb
->index
== EXIT_BLOCK
)
3364 dse_step3_exit_block_scan (bb_info
);
3366 dse_step3_scan (for_spills
, bb
);
3367 if (EDGE_COUNT (bb
->succs
) == 0)
3368 mark_reachable_blocks (unreachable_blocks
, bb
);
3370 /* If this is the second time dataflow is run, delete the old
3373 BITMAP_FREE (bb_info
->in
);
3375 BITMAP_FREE (bb_info
->out
);
3378 /* For any block in an infinite loop, we must initialize the out set
3379 to all ones. This could be expensive, but almost never occurs in
3380 practice. However, it is common in regression tests. */
3381 EXECUTE_IF_SET_IN_SBITMAP (unreachable_blocks
, 0, i
, sbi
)
3383 if (bitmap_bit_p (all_blocks
, i
))
3385 bb_info_t bb_info
= bb_table
[i
];
3391 all_ones
= BITMAP_ALLOC (NULL
);
3392 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, j
, group
)
3393 bitmap_ior_into (all_ones
, group
->group_kill
);
3397 bb_info
->out
= BITMAP_ALLOC (NULL
);
3398 bitmap_copy (bb_info
->out
, all_ones
);
3404 BITMAP_FREE (all_ones
);
3405 sbitmap_free (unreachable_blocks
);
3410 /*----------------------------------------------------------------------------
3413 Solve the bitvector equations.
3414 ----------------------------------------------------------------------------*/
3417 /* Confluence function for blocks with no successors. Create an out
3418 set from the gen set of the exit block. This block logically has
3419 the exit block as a successor. */
3424 dse_confluence_0 (basic_block bb
)
3426 bb_info_t bb_info
= bb_table
[bb
->index
];
3428 if (bb
->index
== EXIT_BLOCK
)
3433 bb_info
->out
= BITMAP_ALLOC (NULL
);
3434 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3438 /* Propagate the information from the in set of the dest of E to the
3439 out set of the src of E. If the various in or out sets are not
3440 there, that means they are all ones. */
3443 dse_confluence_n (edge e
)
3445 bb_info_t src_info
= bb_table
[e
->src
->index
];
3446 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3451 bitmap_and_into (src_info
->out
, dest_info
->in
);
3454 src_info
->out
= BITMAP_ALLOC (NULL
);
3455 bitmap_copy (src_info
->out
, dest_info
->in
);
3462 /* Propagate the info from the out to the in set of BB_INDEX's basic
3463 block. There are three cases:
3465 1) The block has no kill set. In this case the kill set is all
3466 ones. It does not matter what the out set of the block is, none of
3467 the info can reach the top. The only thing that reaches the top is
3468 the gen set and we just copy the set.
3470 2) There is a kill set but no out set and bb has successors. In
3471 this case we just return. Eventually an out set will be created and
3472 it is better to wait than to create a set of ones.
3474 3) There is both a kill and out set. We apply the obvious transfer
3479 dse_transfer_function (int bb_index
)
3481 bb_info_t bb_info
= bb_table
[bb_index
];
3489 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3490 bb_info
->out
, bb_info
->kill
);
3493 bb_info
->in
= BITMAP_ALLOC (NULL
);
3494 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3495 bb_info
->out
, bb_info
->kill
);
3505 /* Case 1 above. If there is already an in set, nothing
3511 bb_info
->in
= BITMAP_ALLOC (NULL
);
3512 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3518 /* Solve the dataflow equations. */
3523 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3524 dse_confluence_n
, dse_transfer_function
,
3525 all_blocks
, df_get_postorder (DF_BACKWARD
),
3526 df_get_n_blocks (DF_BACKWARD
));
3531 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3534 bb_info_t bb_info
= bb_table
[bb
->index
];
3536 df_print_bb_index (bb
, dump_file
);
3538 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3540 fprintf (dump_file
, " in: *MISSING*\n");
3542 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3544 fprintf (dump_file
, " gen: *MISSING*\n");
3546 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3548 fprintf (dump_file
, " kill: *MISSING*\n");
3550 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3552 fprintf (dump_file
, " out: *MISSING*\n\n");
3559 /*----------------------------------------------------------------------------
3562 Delete the stores that can only be deleted using the global information.
3563 ----------------------------------------------------------------------------*/
3567 dse_step5_nospill (void)
3572 bb_info_t bb_info
= bb_table
[bb
->index
];
3573 insn_info_t insn_info
= bb_info
->last_insn
;
3574 bitmap v
= bb_info
->out
;
3578 bool deleted
= false;
3579 if (dump_file
&& insn_info
->insn
)
3581 fprintf (dump_file
, "starting to process insn %d\n",
3582 INSN_UID (insn_info
->insn
));
3583 bitmap_print (dump_file
, v
, " v: ", "\n");
3586 /* There may have been code deleted by the dce pass run before
3589 && INSN_P (insn_info
->insn
)
3590 && (!insn_info
->cannot_delete
)
3591 && (!bitmap_empty_p (v
)))
3593 store_info_t store_info
= insn_info
->store_rec
;
3595 /* Try to delete the current insn. */
3598 /* Skip the clobbers. */
3599 while (!store_info
->is_set
)
3600 store_info
= store_info
->next
;
3602 if (store_info
->alias_set
)
3607 group_info_t group_info
3608 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
3610 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3612 int index
= get_bitmap_index (group_info
, i
);
3615 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3616 if (index
== 0 || !bitmap_bit_p (v
, index
))
3619 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3628 && check_for_inc_dec_1 (insn_info
))
3630 delete_insn (insn_info
->insn
);
3631 insn_info
->insn
= NULL
;
3636 /* We do want to process the local info if the insn was
3637 deleted. For instance, if the insn did a wild read, we
3638 no longer need to trash the info. */
3640 && INSN_P (insn_info
->insn
)
3643 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3644 if (insn_info
->wild_read
)
3647 fprintf (dump_file
, "wild read\n");
3650 else if (insn_info
->read_rec
3651 || insn_info
->non_frame_wild_read
)
3653 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3654 fprintf (dump_file
, "regular read\n");
3656 fprintf (dump_file
, "non-frame wild read\n");
3657 scan_reads_nospill (insn_info
, v
, NULL
);
3661 insn_info
= insn_info
->prev_insn
;
3668 dse_step5_spill (void)
3673 bb_info_t bb_info
= bb_table
[bb
->index
];
3674 insn_info_t insn_info
= bb_info
->last_insn
;
3675 bitmap v
= bb_info
->out
;
3679 bool deleted
= false;
3680 /* There may have been code deleted by the dce pass run before
3683 && INSN_P (insn_info
->insn
)
3684 && (!insn_info
->cannot_delete
)
3685 && (!bitmap_empty_p (v
)))
3687 /* Try to delete the current insn. */
3688 store_info_t store_info
= insn_info
->store_rec
;
3693 if (store_info
->alias_set
)
3695 int index
= get_bitmap_index (clear_alias_group
,
3696 store_info
->alias_set
);
3697 if (index
== 0 || !bitmap_bit_p (v
, index
))
3705 store_info
= store_info
->next
;
3707 if (deleted
&& dbg_cnt (dse
)
3708 && check_for_inc_dec_1 (insn_info
))
3711 fprintf (dump_file
, "Spill deleting insn %d\n",
3712 INSN_UID (insn_info
->insn
));
3713 delete_insn (insn_info
->insn
);
3715 insn_info
->insn
= NULL
;
3720 && INSN_P (insn_info
->insn
)
3723 scan_stores_spill (insn_info
->store_rec
, v
, NULL
);
3724 scan_reads_spill (insn_info
->read_rec
, v
, NULL
);
3727 insn_info
= insn_info
->prev_insn
;
3734 /*----------------------------------------------------------------------------
3737 Delete stores made redundant by earlier stores (which store the same
3738 value) that couldn't be eliminated.
3739 ----------------------------------------------------------------------------*/
3748 bb_info_t bb_info
= bb_table
[bb
->index
];
3749 insn_info_t insn_info
= bb_info
->last_insn
;
3753 /* There may have been code deleted by the dce pass run before
3756 && INSN_P (insn_info
->insn
)
3757 && !insn_info
->cannot_delete
)
3759 store_info_t s_info
= insn_info
->store_rec
;
3761 while (s_info
&& !s_info
->is_set
)
3762 s_info
= s_info
->next
;
3764 && s_info
->redundant_reason
3765 && s_info
->redundant_reason
->insn
3766 && INSN_P (s_info
->redundant_reason
->insn
))
3768 rtx rinsn
= s_info
->redundant_reason
->insn
;
3770 fprintf (dump_file
, "Locally deleting insn %d "
3771 "because insn %d stores the "
3772 "same value and couldn't be "
3774 INSN_UID (insn_info
->insn
),
3776 delete_dead_store_insn (insn_info
);
3779 insn_info
= insn_info
->prev_insn
;
3784 /*----------------------------------------------------------------------------
3787 Destroy everything left standing.
3788 ----------------------------------------------------------------------------*/
3791 dse_step7 (bool global_done
)
3797 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3799 free (group
->offset_map_n
);
3800 free (group
->offset_map_p
);
3801 BITMAP_FREE (group
->store1_n
);
3802 BITMAP_FREE (group
->store1_p
);
3803 BITMAP_FREE (group
->store2_n
);
3804 BITMAP_FREE (group
->store2_p
);
3805 BITMAP_FREE (group
->escaped_n
);
3806 BITMAP_FREE (group
->escaped_p
);
3807 BITMAP_FREE (group
->group_kill
);
3813 bb_info_t bb_info
= bb_table
[bb
->index
];
3814 BITMAP_FREE (bb_info
->gen
);
3816 BITMAP_FREE (bb_info
->kill
);
3818 BITMAP_FREE (bb_info
->in
);
3820 BITMAP_FREE (bb_info
->out
);
3823 if (clear_alias_sets
)
3825 BITMAP_FREE (clear_alias_sets
);
3826 BITMAP_FREE (disqualified_clear_alias_sets
);
3827 free_alloc_pool (clear_alias_mode_pool
);
3828 htab_delete (clear_alias_mode_table
);
3831 end_alias_analysis ();
3833 htab_delete (rtx_group_table
);
3834 VEC_free (group_info_t
, heap
, rtx_group_vec
);
3835 BITMAP_FREE (all_blocks
);
3836 BITMAP_FREE (scratch
);
3837 BITMAP_FREE (kill_on_calls
);
3839 free_alloc_pool (rtx_store_info_pool
);
3840 free_alloc_pool (read_info_pool
);
3841 free_alloc_pool (insn_info_pool
);
3842 free_alloc_pool (bb_info_pool
);
3843 free_alloc_pool (rtx_group_info_pool
);
3844 free_alloc_pool (deferred_change_pool
);
3848 /* -------------------------------------------------------------------------
3850 ------------------------------------------------------------------------- */
3852 /* Callback for running pass_rtl_dse. */
3855 rest_of_handle_dse (void)
3857 bool did_global
= false;
3859 df_set_flags (DF_DEFER_INSN_RESCAN
);
3861 /* Need the notes since we must track live hardregs in the forwards
3863 df_note_add_problem ();
3869 if (dse_step2_nospill ())
3871 df_set_flags (DF_LR_RUN_DCE
);
3875 fprintf (dump_file
, "doing global processing\n");
3878 dse_step5_nospill ();
3881 /* For the instance of dse that runs after reload, we make a special
3882 pass to process the spills. These are special in that they are
3883 totally transparent, i.e, there is no aliasing issues that need
3884 to be considered. This means that the wild reads that kill
3885 everything else do not apply here. */
3886 if (clear_alias_sets
&& dse_step2_spill ())
3890 df_set_flags (DF_LR_RUN_DCE
);
3895 fprintf (dump_file
, "doing global spill processing\n");
3902 dse_step7 (did_global
);
3905 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3906 locally_deleted
, globally_deleted
, spill_deleted
);
3913 return optimize
> 0 && flag_dse
3920 return optimize
> 0 && flag_dse
3924 struct rtl_opt_pass pass_rtl_dse1
=
3929 gate_dse1
, /* gate */
3930 rest_of_handle_dse
, /* execute */
3933 0, /* static_pass_number */
3934 TV_DSE1
, /* tv_id */
3935 0, /* properties_required */
3936 0, /* properties_provided */
3937 0, /* properties_destroyed */
3938 0, /* todo_flags_start */
3939 TODO_df_finish
| TODO_verify_rtl_sharing
|
3940 TODO_ggc_collect
/* todo_flags_finish */
3944 struct rtl_opt_pass pass_rtl_dse2
=
3949 gate_dse2
, /* gate */
3950 rest_of_handle_dse
, /* execute */
3953 0, /* static_pass_number */
3954 TV_DSE2
, /* tv_id */
3955 0, /* properties_required */
3956 0, /* properties_provided */
3957 0, /* properties_destroyed */
3958 0, /* todo_flags_start */
3959 TODO_df_finish
| TODO_verify_rtl_sharing
|
3960 TODO_ggc_collect
/* todo_flags_finish */