1 /* RTL dead store elimination.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
5 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
6 and Kenneth Zadeck <zadeck@naturalbridge.com>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
28 #include "coretypes.h"
29 #include "hash-table.h"
35 #include "hard-reg-set.h"
40 #include "tree-pass.h"
41 #include "alloc-pool.h"
43 #include "insn-config.h"
50 #include "tree-flow.h" /* for may_be_aliased */
52 /* This file contains three techniques for performing Dead Store
55 * The first technique performs dse locally on any base address. It
56 is based on the cselib which is a local value numbering technique.
57 This technique is local to a basic block but deals with a fairly
60 * The second technique performs dse globally but is restricted to
61 base addresses that are either constant or are relative to the
64 * The third technique, (which is only done after register allocation)
65 processes the spill spill slots. This differs from the second
66 technique because it takes advantage of the fact that spilling is
67 completely free from the effects of aliasing.
69 Logically, dse is a backwards dataflow problem. A store can be
70 deleted if it if cannot be reached in the backward direction by any
71 use of the value being stored. However, the local technique uses a
72 forwards scan of the basic block because cselib requires that the
73 block be processed in that order.
75 The pass is logically broken into 7 steps:
79 1) The local algorithm, as well as scanning the insns for the two
82 2) Analysis to see if the global algs are necessary. In the case
83 of stores base on a constant address, there must be at least two
84 stores to that address, to make it possible to delete some of the
85 stores. In the case of stores off of the frame or spill related
86 stores, only one store to an address is necessary because those
87 stores die at the end of the function.
89 3) Set up the global dataflow equations based on processing the
90 info parsed in the first step.
92 4) Solve the dataflow equations.
94 5) Delete the insns that the global analysis has indicated are
97 6) Delete insns that store the same value as preceding store
98 where the earlier store couldn't be eliminated.
102 This step uses cselib and canon_rtx to build the largest expression
103 possible for each address. This pass is a forwards pass through
104 each basic block. From the point of view of the global technique,
105 the first pass could examine a block in either direction. The
106 forwards ordering is to accommodate cselib.
108 We make a simplifying assumption: addresses fall into four broad
111 1) base has rtx_varies_p == false, offset is constant.
112 2) base has rtx_varies_p == false, offset variable.
113 3) base has rtx_varies_p == true, offset constant.
114 4) base has rtx_varies_p == true, offset variable.
116 The local passes are able to process all 4 kinds of addresses. The
117 global pass only handles 1).
119 The global problem is formulated as follows:
121 A store, S1, to address A, where A is not relative to the stack
122 frame, can be eliminated if all paths from S1 to the end of the
123 function contain another store to A before a read to A.
125 If the address A is relative to the stack frame, a store S2 to A
126 can be eliminated if there are no paths from S2 that reach the
127 end of the function that read A before another store to A. In
128 this case S2 can be deleted if there are paths from S2 to the
129 end of the function that have no reads or writes to A. This
130 second case allows stores to the stack frame to be deleted that
131 would otherwise die when the function returns. This cannot be
132 done if stores_off_frame_dead_at_return is not true. See the doc
133 for that variable for when this variable is false.
135 The global problem is formulated as a backwards set union
136 dataflow problem where the stores are the gens and reads are the
137 kills. Set union problems are rare and require some special
138 handling given our representation of bitmaps. A straightforward
139 implementation requires a lot of bitmaps filled with 1s.
140 These are expensive and cumbersome in our bitmap formulation so
141 care has been taken to avoid large vectors filled with 1s. See
142 the comments in bb_info and in the dataflow confluence functions
145 There are two places for further enhancements to this algorithm:
147 1) The original dse which was embedded in a pass called flow also
148 did local address forwarding. For example in
153 flow would replace the right hand side of the second insn with a
154 reference to r100. Most of the information is available to add this
155 to this pass. It has not done it because it is a lot of work in
156 the case that either r100 is assigned to between the first and
157 second insn and/or the second insn is a load of part of the value
158 stored by the first insn.
160 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
161 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
162 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
163 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
165 2) The cleaning up of spill code is quite profitable. It currently
166 depends on reading tea leaves and chicken entrails left by reload.
167 This pass depends on reload creating a singleton alias set for each
168 spill slot and telling the next dse pass which of these alias sets
169 are the singletons. Rather than analyze the addresses of the
170 spills, dse's spill processing just does analysis of the loads and
171 stores that use those alias sets. There are three cases where this
174 a) Reload sometimes creates the slot for one mode of access, and
175 then inserts loads and/or stores for a smaller mode. In this
176 case, the current code just punts on the slot. The proper thing
177 to do is to back out and use one bit vector position for each
178 byte of the entity associated with the slot. This depends on
179 KNOWING that reload always generates the accesses for each of the
180 bytes in some canonical (read that easy to understand several
181 passes after reload happens) way.
183 b) Reload sometimes decides that spill slot it allocated was not
184 large enough for the mode and goes back and allocates more slots
185 with the same mode and alias set. The backout in this case is a
186 little more graceful than (a). In this case the slot is unmarked
187 as being a spill slot and if final address comes out to be based
188 off the frame pointer, the global algorithm handles this slot.
190 c) For any pass that may prespill, there is currently no
191 mechanism to tell the dse pass that the slot being used has the
192 special properties that reload uses. It may be that all that is
193 required is to have those passes make the same calls that reload
194 does, assuming that the alias sets can be manipulated in the same
197 /* There are limits to the size of constant offsets we model for the
198 global problem. There are certainly test cases, that exceed this
199 limit, however, it is unlikely that there are important programs
200 that really have constant offsets this size. */
201 #define MAX_OFFSET (64 * 1024)
203 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
204 on the default obstack because these bitmaps can grow quite large
205 (~2GB for the small (!) test case of PR54146) and we'll hold on to
206 all that memory until the end of the compiler run.
207 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
208 releasing the whole obstack. */
209 static bitmap_obstack dse_bitmap_obstack
;
211 /* Obstack for other data. As for above: Kinda nice to be able to
212 throw it all away at the end in one big sweep. */
213 static struct obstack dse_obstack
;
215 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
216 static bitmap scratch
= NULL
;
220 /* This structure holds information about a candidate store. */
224 /* False means this is a clobber. */
227 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
230 /* The id of the mem group of the base address. If rtx_varies_p is
231 true, this is -1. Otherwise, it is the index into the group
235 /* This is the cselib value. */
236 cselib_val
*cse_base
;
238 /* This canonized mem. */
241 /* Canonized MEM address for use by canon_true_dependence. */
244 /* If this is non-zero, it is the alias set of a spill location. */
245 alias_set_type alias_set
;
247 /* The offset of the first and byte before the last byte associated
248 with the operation. */
249 HOST_WIDE_INT begin
, end
;
253 /* A bitmask as wide as the number of bytes in the word that
254 contains a 1 if the byte may be needed. The store is unused if
255 all of the bits are 0. This is used if IS_LARGE is false. */
256 unsigned HOST_WIDE_INT small_bitmask
;
260 /* A bitmap with one bit per byte. Cleared bit means the position
261 is needed. Used if IS_LARGE is false. */
264 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
265 equal to END - BEGIN, the whole store is unused. */
270 /* The next store info for this insn. */
271 struct store_info
*next
;
273 /* The right hand side of the store. This is used if there is a
274 subsequent reload of the mems address somewhere later in the
278 /* If rhs is or holds a constant, this contains that constant,
282 /* Set if this store stores the same constant value as REDUNDANT_REASON
283 insn stored. These aren't eliminated early, because doing that
284 might prevent the earlier larger store to be eliminated. */
285 struct insn_info
*redundant_reason
;
288 /* Return a bitmask with the first N low bits set. */
290 static unsigned HOST_WIDE_INT
291 lowpart_bitmask (int n
)
293 unsigned HOST_WIDE_INT mask
= ~(unsigned HOST_WIDE_INT
) 0;
294 return mask
>> (HOST_BITS_PER_WIDE_INT
- n
);
297 typedef struct store_info
*store_info_t
;
298 static alloc_pool cse_store_info_pool
;
299 static alloc_pool rtx_store_info_pool
;
301 /* This structure holds information about a load. These are only
302 built for rtx bases. */
305 /* The id of the mem group of the base address. */
308 /* If this is non-zero, it is the alias set of a spill location. */
309 alias_set_type alias_set
;
311 /* The offset of the first and byte after the last byte associated
312 with the operation. If begin == end == 0, the read did not have
313 a constant offset. */
316 /* The mem being read. */
319 /* The next read_info for this insn. */
320 struct read_info
*next
;
322 typedef struct read_info
*read_info_t
;
323 static alloc_pool read_info_pool
;
326 /* One of these records is created for each insn. */
330 /* Set true if the insn contains a store but the insn itself cannot
331 be deleted. This is set if the insn is a parallel and there is
332 more than one non dead output or if the insn is in some way
336 /* This field is only used by the global algorithm. It is set true
337 if the insn contains any read of mem except for a (1). This is
338 also set if the insn is a call or has a clobber mem. If the insn
339 contains a wild read, the use_rec will be null. */
342 /* This is true only for CALL instructions which could potentially read
343 any non-frame memory location. This field is used by the global
345 bool non_frame_wild_read
;
347 /* This field is only used for the processing of const functions.
348 These functions cannot read memory, but they can read the stack
349 because that is where they may get their parms. We need to be
350 this conservative because, like the store motion pass, we don't
351 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
352 Moreover, we need to distinguish two cases:
353 1. Before reload (register elimination), the stores related to
354 outgoing arguments are stack pointer based and thus deemed
355 of non-constant base in this pass. This requires special
356 handling but also means that the frame pointer based stores
357 need not be killed upon encountering a const function call.
358 2. After reload, the stores related to outgoing arguments can be
359 either stack pointer or hard frame pointer based. This means
360 that we have no other choice than also killing all the frame
361 pointer based stores upon encountering a const function call.
362 This field is set after reload for const function calls. Having
363 this set is less severe than a wild read, it just means that all
364 the frame related stores are killed rather than all the stores. */
367 /* This field is only used for the processing of const functions.
368 It is set if the insn may contain a stack pointer based store. */
369 bool stack_pointer_based
;
371 /* This is true if any of the sets within the store contains a
372 cselib base. Such stores can only be deleted by the local
374 bool contains_cselib_groups
;
379 /* The list of mem sets or mem clobbers that are contained in this
380 insn. If the insn is deletable, it contains only one mem set.
381 But it could also contain clobbers. Insns that contain more than
382 one mem set are not deletable, but each of those mems are here in
383 order to provide info to delete other insns. */
384 store_info_t store_rec
;
386 /* The linked list of mem uses in this insn. Only the reads from
387 rtx bases are listed here. The reads to cselib bases are
388 completely processed during the first scan and so are never
390 read_info_t read_rec
;
392 /* The live fixed registers. We assume only fixed registers can
393 cause trouble by being clobbered from an expanded pattern;
394 storing only the live fixed registers (rather than all registers)
395 means less memory needs to be allocated / copied for the individual
397 regset fixed_regs_live
;
399 /* The prev insn in the basic block. */
400 struct insn_info
* prev_insn
;
402 /* The linked list of insns that are in consideration for removal in
403 the forwards pass through the basic block. This pointer may be
404 trash as it is not cleared when a wild read occurs. The only
405 time it is guaranteed to be correct is when the traversal starts
406 at active_local_stores. */
407 struct insn_info
* next_local_store
;
410 typedef struct insn_info
*insn_info_t
;
411 static alloc_pool insn_info_pool
;
413 /* The linked list of stores that are under consideration in this
415 static insn_info_t active_local_stores
;
416 static int active_local_stores_len
;
421 /* Pointer to the insn info for the last insn in the block. These
422 are linked so this is how all of the insns are reached. During
423 scanning this is the current insn being scanned. */
424 insn_info_t last_insn
;
426 /* The info for the global dataflow problem. */
429 /* This is set if the transfer function should and in the wild_read
430 bitmap before applying the kill and gen sets. That vector knocks
431 out most of the bits in the bitmap and thus speeds up the
433 bool apply_wild_read
;
435 /* The following 4 bitvectors hold information about which positions
436 of which stores are live or dead. They are indexed by
439 /* The set of store positions that exist in this block before a wild read. */
442 /* The set of load positions that exist in this block above the
443 same position of a store. */
446 /* The set of stores that reach the top of the block without being
449 Do not represent the in if it is all ones. Note that this is
450 what the bitvector should logically be initialized to for a set
451 intersection problem. However, like the kill set, this is too
452 expensive. So initially, the in set will only be created for the
453 exit block and any block that contains a wild read. */
456 /* The set of stores that reach the bottom of the block from it's
459 Do not represent the in if it is all ones. Note that this is
460 what the bitvector should logically be initialized to for a set
461 intersection problem. However, like the kill and in set, this is
462 too expensive. So what is done is that the confluence operator
463 just initializes the vector from one of the out sets of the
464 successors of the block. */
467 /* The following bitvector is indexed by the reg number. It
468 contains the set of regs that are live at the current instruction
469 being processed. While it contains info for all of the
470 registers, only the hard registers are actually examined. It is used
471 to assure that shift and/or add sequences that are inserted do not
472 accidentally clobber live hard regs. */
476 typedef struct bb_info
*bb_info_t
;
477 static alloc_pool bb_info_pool
;
479 /* Table to hold all bb_infos. */
480 static bb_info_t
*bb_table
;
482 /* There is a group_info for each rtx base that is used to reference
483 memory. There are also not many of the rtx bases because they are
484 very limited in scope. */
488 /* The actual base of the address. */
491 /* The sequential id of the base. This allows us to have a
492 canonical ordering of these that is not based on addresses. */
495 /* True if there are any positions that are to be processed
497 bool process_globally
;
499 /* True if the base of this group is either the frame_pointer or
500 hard_frame_pointer. */
503 /* A mem wrapped around the base pointer for the group in order to do
504 read dependency. It must be given BLKmode in order to encompass all
505 the possible offsets from the base. */
508 /* Canonized version of base_mem's address. */
511 /* These two sets of two bitmaps are used to keep track of how many
512 stores are actually referencing that position from this base. We
513 only do this for rtx bases as this will be used to assign
514 positions in the bitmaps for the global problem. Bit N is set in
515 store1 on the first store for offset N. Bit N is set in store2
516 for the second store to offset N. This is all we need since we
517 only care about offsets that have two or more stores for them.
519 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
520 for 0 and greater offsets.
522 There is one special case here, for stores into the stack frame,
523 we will or store1 into store2 before deciding which stores look
524 at globally. This is because stores to the stack frame that have
525 no other reads before the end of the function can also be
527 bitmap store1_n
, store1_p
, store2_n
, store2_p
;
529 /* These bitmaps keep track of offsets in this group escape this function.
530 An offset escapes if it corresponds to a named variable whose
531 addressable flag is set. */
532 bitmap escaped_n
, escaped_p
;
534 /* The positions in this bitmap have the same assignments as the in,
535 out, gen and kill bitmaps. This bitmap is all zeros except for
536 the positions that are occupied by stores for this group. */
539 /* The offset_map is used to map the offsets from this base into
540 positions in the global bitmaps. It is only created after all of
541 the all of stores have been scanned and we know which ones we
543 int *offset_map_n
, *offset_map_p
;
544 int offset_map_size_n
, offset_map_size_p
;
546 typedef struct group_info
*group_info_t
;
547 typedef const struct group_info
*const_group_info_t
;
548 static alloc_pool rtx_group_info_pool
;
550 /* Index into the rtx_group_vec. */
551 static int rtx_group_next_id
;
553 DEF_VEC_P(group_info_t
);
554 DEF_VEC_ALLOC_P(group_info_t
,heap
);
556 static VEC(group_info_t
,heap
) *rtx_group_vec
;
559 /* This structure holds the set of changes that are being deferred
560 when removing read operation. See replace_read. */
561 struct deferred_change
564 /* The mem that is being replaced. */
567 /* The reg it is being replaced with. */
570 struct deferred_change
*next
;
573 typedef struct deferred_change
*deferred_change_t
;
574 static alloc_pool deferred_change_pool
;
576 static deferred_change_t deferred_change_list
= NULL
;
578 /* This are used to hold the alias sets of spill variables. Since
579 these are never aliased and there may be a lot of them, it makes
580 sense to treat them specially. This bitvector is only allocated in
581 calls from dse_record_singleton_alias_set which currently is only
582 made during reload1. So when dse is called before reload this
583 mechanism does nothing. */
585 static bitmap clear_alias_sets
= NULL
;
587 /* The set of clear_alias_sets that have been disqualified because
588 there are loads or stores using a different mode than the alias set
589 was registered with. */
590 static bitmap disqualified_clear_alias_sets
= NULL
;
592 /* The group that holds all of the clear_alias_sets. */
593 static group_info_t clear_alias_group
;
595 /* The modes of the clear_alias_sets. */
596 static htab_t clear_alias_mode_table
;
598 /* Hash table element to look up the mode for an alias set. */
599 struct clear_alias_mode_holder
601 alias_set_type alias_set
;
602 enum machine_mode mode
;
605 static alloc_pool clear_alias_mode_pool
;
607 /* This is true except if cfun->stdarg -- i.e. we cannot do
608 this for vararg functions because they play games with the frame. */
609 static bool stores_off_frame_dead_at_return
;
611 /* Counter for stats. */
612 static int globally_deleted
;
613 static int locally_deleted
;
614 static int spill_deleted
;
616 static bitmap all_blocks
;
618 /* Locations that are killed by calls in the global phase. */
619 static bitmap kill_on_calls
;
621 /* The number of bits used in the global bitmaps. */
622 static unsigned int current_position
;
625 static bool gate_dse1 (void);
626 static bool gate_dse2 (void);
629 /*----------------------------------------------------------------------------
633 ----------------------------------------------------------------------------*/
636 /* Find the entry associated with ALIAS_SET. */
638 static struct clear_alias_mode_holder
*
639 clear_alias_set_lookup (alias_set_type alias_set
)
641 struct clear_alias_mode_holder tmp_holder
;
644 tmp_holder
.alias_set
= alias_set
;
645 slot
= htab_find_slot (clear_alias_mode_table
, &tmp_holder
, NO_INSERT
);
648 return (struct clear_alias_mode_holder
*) *slot
;
652 /* Hashtable callbacks for maintaining the "bases" field of
653 store_group_info, given that the addresses are function invariants. */
655 struct invariant_group_base_hasher
: typed_noop_remove
<group_info
>
657 typedef group_info value_type
;
658 typedef group_info compare_type
;
659 static inline hashval_t
hash (const value_type
*);
660 static inline bool equal (const value_type
*, const compare_type
*);
664 invariant_group_base_hasher::equal (const value_type
*gi1
,
665 const compare_type
*gi2
)
667 return rtx_equal_p (gi1
->rtx_base
, gi2
->rtx_base
);
671 invariant_group_base_hasher::hash (const value_type
*gi
)
674 return hash_rtx (gi
->rtx_base
, Pmode
, &do_not_record
, NULL
, false);
677 /* Tables of group_info structures, hashed by base value. */
678 static hash_table
<invariant_group_base_hasher
> rtx_group_table
;
681 /* Get the GROUP for BASE. Add a new group if it is not there. */
684 get_group_info (rtx base
)
686 struct group_info tmp_gi
;
692 /* Find the store_base_info structure for BASE, creating a new one
694 tmp_gi
.rtx_base
= base
;
695 slot
= rtx_group_table
.find_slot (&tmp_gi
, INSERT
);
696 gi
= (group_info_t
) *slot
;
700 if (!clear_alias_group
)
702 clear_alias_group
= gi
=
703 (group_info_t
) pool_alloc (rtx_group_info_pool
);
704 memset (gi
, 0, sizeof (struct group_info
));
705 gi
->id
= rtx_group_next_id
++;
706 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
707 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
708 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
709 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
710 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
711 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
712 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
713 gi
->process_globally
= false;
714 gi
->offset_map_size_n
= 0;
715 gi
->offset_map_size_p
= 0;
716 gi
->offset_map_n
= NULL
;
717 gi
->offset_map_p
= NULL
;
718 VEC_safe_push (group_info_t
, heap
, rtx_group_vec
, gi
);
720 return clear_alias_group
;
725 *slot
= gi
= (group_info_t
) pool_alloc (rtx_group_info_pool
);
727 gi
->id
= rtx_group_next_id
++;
728 gi
->base_mem
= gen_rtx_MEM (BLKmode
, base
);
729 gi
->canon_base_addr
= canon_rtx (base
);
730 gi
->store1_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
731 gi
->store1_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
732 gi
->store2_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
733 gi
->store2_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
734 gi
->escaped_p
= BITMAP_ALLOC (&dse_bitmap_obstack
);
735 gi
->escaped_n
= BITMAP_ALLOC (&dse_bitmap_obstack
);
736 gi
->group_kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
737 gi
->process_globally
= false;
739 (base
== frame_pointer_rtx
) || (base
== hard_frame_pointer_rtx
);
740 gi
->offset_map_size_n
= 0;
741 gi
->offset_map_size_p
= 0;
742 gi
->offset_map_n
= NULL
;
743 gi
->offset_map_p
= NULL
;
744 VEC_safe_push (group_info_t
, heap
, rtx_group_vec
, gi
);
751 /* Initialization of data structures. */
757 globally_deleted
= 0;
760 bitmap_obstack_initialize (&dse_bitmap_obstack
);
761 gcc_obstack_init (&dse_obstack
);
763 scratch
= BITMAP_ALLOC (®_obstack
);
764 kill_on_calls
= BITMAP_ALLOC (&dse_bitmap_obstack
);
767 = create_alloc_pool ("rtx_store_info_pool",
768 sizeof (struct store_info
), 100);
770 = create_alloc_pool ("read_info_pool",
771 sizeof (struct read_info
), 100);
773 = create_alloc_pool ("insn_info_pool",
774 sizeof (struct insn_info
), 100);
776 = create_alloc_pool ("bb_info_pool",
777 sizeof (struct bb_info
), 100);
779 = create_alloc_pool ("rtx_group_info_pool",
780 sizeof (struct group_info
), 100);
782 = create_alloc_pool ("deferred_change_pool",
783 sizeof (struct deferred_change
), 10);
785 rtx_group_table
.create (11);
787 bb_table
= XNEWVEC (bb_info_t
, last_basic_block
);
788 rtx_group_next_id
= 0;
790 stores_off_frame_dead_at_return
= !cfun
->stdarg
;
792 init_alias_analysis ();
794 if (clear_alias_sets
)
795 clear_alias_group
= get_group_info (NULL
);
797 clear_alias_group
= NULL
;
802 /*----------------------------------------------------------------------------
805 Scan all of the insns. Any random ordering of the blocks is fine.
806 Each block is scanned in forward order to accommodate cselib which
807 is used to remove stores with non-constant bases.
808 ----------------------------------------------------------------------------*/
810 /* Delete all of the store_info recs from INSN_INFO. */
813 free_store_info (insn_info_t insn_info
)
815 store_info_t store_info
= insn_info
->store_rec
;
818 store_info_t next
= store_info
->next
;
819 if (store_info
->is_large
)
820 BITMAP_FREE (store_info
->positions_needed
.large
.bmap
);
821 if (store_info
->cse_base
)
822 pool_free (cse_store_info_pool
, store_info
);
824 pool_free (rtx_store_info_pool
, store_info
);
828 insn_info
->cannot_delete
= true;
829 insn_info
->contains_cselib_groups
= false;
830 insn_info
->store_rec
= NULL
;
836 regset fixed_regs_live
;
838 } note_add_store_info
;
840 /* Callback for emit_inc_dec_insn_before via note_stores.
841 Check if a register is clobbered which is live afterwards. */
844 note_add_store (rtx loc
, const_rtx expr ATTRIBUTE_UNUSED
, void *data
)
847 note_add_store_info
*info
= (note_add_store_info
*) data
;
853 /* If this register is referenced by the current or an earlier insn,
854 that's OK. E.g. this applies to the register that is being incremented
855 with this addition. */
856 for (insn
= info
->first
;
857 insn
!= NEXT_INSN (info
->current
);
858 insn
= NEXT_INSN (insn
))
859 if (reg_referenced_p (loc
, PATTERN (insn
)))
862 /* If we come here, we have a clobber of a register that's only OK
863 if that register is not live. If we don't have liveness information
864 available, fail now. */
865 if (!info
->fixed_regs_live
)
867 info
->failure
= true;
870 /* Now check if this is a live fixed register. */
872 n
= hard_regno_nregs
[r
][GET_MODE (loc
)];
874 if (REGNO_REG_SET_P (info
->fixed_regs_live
, r
+n
))
875 info
->failure
= true;
878 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
879 SRC + SRCOFF before insn ARG. */
882 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED
,
883 rtx op ATTRIBUTE_UNUSED
,
884 rtx dest
, rtx src
, rtx srcoff
, void *arg
)
886 insn_info_t insn_info
= (insn_info_t
) arg
;
887 rtx insn
= insn_info
->insn
, new_insn
, cur
;
888 note_add_store_info info
;
890 /* We can reuse all operands without copying, because we are about
891 to delete the insn that contained it. */
895 emit_insn (gen_add3_insn (dest
, src
, srcoff
));
896 new_insn
= get_insns ();
900 new_insn
= gen_move_insn (dest
, src
);
901 info
.first
= new_insn
;
902 info
.fixed_regs_live
= insn_info
->fixed_regs_live
;
903 info
.failure
= false;
904 for (cur
= new_insn
; cur
; cur
= NEXT_INSN (cur
))
907 note_stores (PATTERN (cur
), note_add_store
, &info
);
910 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
911 return it immediately, communicating the failure to its caller. */
915 emit_insn_before (new_insn
, insn
);
920 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
921 is there, is split into a separate insn.
922 Return true on success (or if there was nothing to do), false on failure. */
925 check_for_inc_dec_1 (insn_info_t insn_info
)
927 rtx insn
= insn_info
->insn
;
928 rtx note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
930 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, insn_info
) == 0;
935 /* Entry point for postreload. If you work on reload_cse, or you need this
936 anywhere else, consider if you can provide register liveness information
937 and add a parameter to this function so that it can be passed down in
938 insn_info.fixed_regs_live. */
940 check_for_inc_dec (rtx insn
)
942 struct insn_info insn_info
;
945 insn_info
.insn
= insn
;
946 insn_info
.fixed_regs_live
= NULL
;
947 note
= find_reg_note (insn
, REG_INC
, NULL_RTX
);
949 return for_each_inc_dec (&insn
, emit_inc_dec_insn_before
, &insn_info
) == 0;
953 /* Delete the insn and free all of the fields inside INSN_INFO. */
956 delete_dead_store_insn (insn_info_t insn_info
)
958 read_info_t read_info
;
963 if (!check_for_inc_dec_1 (insn_info
))
967 fprintf (dump_file
, "Locally deleting insn %d ",
968 INSN_UID (insn_info
->insn
));
969 if (insn_info
->store_rec
->alias_set
)
970 fprintf (dump_file
, "alias set %d\n",
971 (int) insn_info
->store_rec
->alias_set
);
973 fprintf (dump_file
, "\n");
976 free_store_info (insn_info
);
977 read_info
= insn_info
->read_rec
;
981 read_info_t next
= read_info
->next
;
982 pool_free (read_info_pool
, read_info
);
985 insn_info
->read_rec
= NULL
;
987 delete_insn (insn_info
->insn
);
989 insn_info
->insn
= NULL
;
991 insn_info
->wild_read
= false;
994 /* Return whether DECL, a local variable, can possibly escape the current
998 local_variable_can_escape (tree decl
)
1000 if (TREE_ADDRESSABLE (decl
))
1003 /* If this is a partitioned variable, we need to consider all the variables
1004 in the partition. This is necessary because a store into one of them can
1005 be replaced with a store into another and this may not change the outcome
1006 of the escape analysis. */
1007 if (cfun
->gimple_df
->decls_to_pointers
!= NULL
)
1010 = pointer_map_contains (cfun
->gimple_df
->decls_to_pointers
, decl
);
1012 return TREE_ADDRESSABLE (*(tree
*)namep
);
1018 /* Return whether EXPR can possibly escape the current function scope. */
1021 can_escape (tree expr
)
1026 base
= get_base_address (expr
);
1028 && !may_be_aliased (base
)
1029 && !(TREE_CODE (base
) == VAR_DECL
1030 && !DECL_EXTERNAL (base
)
1031 && !TREE_STATIC (base
)
1032 && local_variable_can_escape (base
)))
1037 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1038 OFFSET and WIDTH. */
1041 set_usage_bits (group_info_t group
, HOST_WIDE_INT offset
, HOST_WIDE_INT width
,
1045 bool expr_escapes
= can_escape (expr
);
1046 if (offset
> -MAX_OFFSET
&& offset
+ width
< MAX_OFFSET
)
1047 for (i
=offset
; i
<offset
+width
; i
++)
1055 store1
= group
->store1_n
;
1056 store2
= group
->store2_n
;
1057 escaped
= group
->escaped_n
;
1062 store1
= group
->store1_p
;
1063 store2
= group
->store2_p
;
1064 escaped
= group
->escaped_p
;
1068 if (!bitmap_set_bit (store1
, ai
))
1069 bitmap_set_bit (store2
, ai
);
1074 if (group
->offset_map_size_n
< ai
)
1075 group
->offset_map_size_n
= ai
;
1079 if (group
->offset_map_size_p
< ai
)
1080 group
->offset_map_size_p
= ai
;
1084 bitmap_set_bit (escaped
, ai
);
1089 reset_active_stores (void)
1091 active_local_stores
= NULL
;
1092 active_local_stores_len
= 0;
1095 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1098 free_read_records (bb_info_t bb_info
)
1100 insn_info_t insn_info
= bb_info
->last_insn
;
1101 read_info_t
*ptr
= &insn_info
->read_rec
;
1104 read_info_t next
= (*ptr
)->next
;
1105 if ((*ptr
)->alias_set
== 0)
1107 pool_free (read_info_pool
, *ptr
);
1111 ptr
= &(*ptr
)->next
;
1115 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1118 add_wild_read (bb_info_t bb_info
)
1120 insn_info_t insn_info
= bb_info
->last_insn
;
1121 insn_info
->wild_read
= true;
1122 free_read_records (bb_info
);
1123 reset_active_stores ();
1126 /* Set the BB_INFO so that the last insn is marked as a wild read of
1127 non-frame locations. */
1130 add_non_frame_wild_read (bb_info_t bb_info
)
1132 insn_info_t insn_info
= bb_info
->last_insn
;
1133 insn_info
->non_frame_wild_read
= true;
1134 free_read_records (bb_info
);
1135 reset_active_stores ();
1138 /* Return true if X is a constant or one of the registers that behave
1139 as a constant over the life of a function. This is equivalent to
1140 !rtx_varies_p for memory addresses. */
1143 const_or_frame_p (rtx x
)
1148 if (GET_CODE (x
) == REG
)
1150 /* Note that we have to test for the actual rtx used for the frame
1151 and arg pointers and not just the register number in case we have
1152 eliminated the frame and/or arg pointer and are using it
1154 if (x
== frame_pointer_rtx
|| x
== hard_frame_pointer_rtx
1155 /* The arg pointer varies if it is not a fixed register. */
1156 || (x
== arg_pointer_rtx
&& fixed_regs
[ARG_POINTER_REGNUM
])
1157 || x
== pic_offset_table_rtx
)
1165 /* Take all reasonable action to put the address of MEM into the form
1166 that we can do analysis on.
1168 The gold standard is to get the address into the form: address +
1169 OFFSET where address is something that rtx_varies_p considers a
1170 constant. When we can get the address in this form, we can do
1171 global analysis on it. Note that for constant bases, address is
1172 not actually returned, only the group_id. The address can be
1175 If that fails, we try cselib to get a value we can at least use
1176 locally. If that fails we return false.
1178 The GROUP_ID is set to -1 for cselib bases and the index of the
1179 group for non_varying bases.
1181 FOR_READ is true if this is a mem read and false if not. */
1184 canon_address (rtx mem
,
1185 alias_set_type
*alias_set_out
,
1187 HOST_WIDE_INT
*offset
,
1190 enum machine_mode address_mode
= get_address_mode (mem
);
1191 rtx mem_address
= XEXP (mem
, 0);
1192 rtx expanded_address
, address
;
1195 /* Make sure that cselib is has initialized all of the operands of
1196 the address before asking it to do the subst. */
1198 if (clear_alias_sets
)
1200 /* If this is a spill, do not do any further processing. */
1201 alias_set_type alias_set
= MEM_ALIAS_SET (mem
);
1203 fprintf (dump_file
, "found alias set %d\n", (int) alias_set
);
1204 if (bitmap_bit_p (clear_alias_sets
, alias_set
))
1206 struct clear_alias_mode_holder
*entry
1207 = clear_alias_set_lookup (alias_set
);
1209 /* If the modes do not match, we cannot process this set. */
1210 if (entry
->mode
!= GET_MODE (mem
))
1214 "disqualifying alias set %d, (%s) != (%s)\n",
1215 (int) alias_set
, GET_MODE_NAME (entry
->mode
),
1216 GET_MODE_NAME (GET_MODE (mem
)));
1218 bitmap_set_bit (disqualified_clear_alias_sets
, alias_set
);
1222 *alias_set_out
= alias_set
;
1223 *group_id
= clear_alias_group
->id
;
1230 cselib_lookup (mem_address
, address_mode
, 1, GET_MODE (mem
));
1234 fprintf (dump_file
, " mem: ");
1235 print_inline_rtx (dump_file
, mem_address
, 0);
1236 fprintf (dump_file
, "\n");
1239 /* First see if just canon_rtx (mem_address) is const or frame,
1240 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1242 for (expanded
= 0; expanded
< 2; expanded
++)
1246 /* Use cselib to replace all of the reg references with the full
1247 expression. This will take care of the case where we have
1249 r_x = base + offset;
1254 val = *(base + offset); */
1256 expanded_address
= cselib_expand_value_rtx (mem_address
,
1259 /* If this fails, just go with the address from first
1261 if (!expanded_address
)
1265 expanded_address
= mem_address
;
1267 /* Split the address into canonical BASE + OFFSET terms. */
1268 address
= canon_rtx (expanded_address
);
1276 fprintf (dump_file
, "\n after cselib_expand address: ");
1277 print_inline_rtx (dump_file
, expanded_address
, 0);
1278 fprintf (dump_file
, "\n");
1281 fprintf (dump_file
, "\n after canon_rtx address: ");
1282 print_inline_rtx (dump_file
, address
, 0);
1283 fprintf (dump_file
, "\n");
1286 if (GET_CODE (address
) == CONST
)
1287 address
= XEXP (address
, 0);
1289 if (GET_CODE (address
) == PLUS
1290 && CONST_INT_P (XEXP (address
, 1)))
1292 *offset
= INTVAL (XEXP (address
, 1));
1293 address
= XEXP (address
, 0);
1296 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem
))
1297 && const_or_frame_p (address
))
1299 group_info_t group
= get_group_info (address
);
1302 fprintf (dump_file
, " gid=%d offset=%d \n",
1303 group
->id
, (int)*offset
);
1305 *group_id
= group
->id
;
1310 *base
= cselib_lookup (address
, address_mode
, true, GET_MODE (mem
));
1316 fprintf (dump_file
, " no cselib val - should be a wild read.\n");
1320 fprintf (dump_file
, " varying cselib base=%u:%u offset = %d\n",
1321 (*base
)->uid
, (*base
)->hash
, (int)*offset
);
1326 /* Clear the rhs field from the active_local_stores array. */
1329 clear_rhs_from_active_local_stores (void)
1331 insn_info_t ptr
= active_local_stores
;
1335 store_info_t store_info
= ptr
->store_rec
;
1336 /* Skip the clobbers. */
1337 while (!store_info
->is_set
)
1338 store_info
= store_info
->next
;
1340 store_info
->rhs
= NULL
;
1341 store_info
->const_rhs
= NULL
;
1343 ptr
= ptr
->next_local_store
;
1348 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1351 set_position_unneeded (store_info_t s_info
, int pos
)
1353 if (__builtin_expect (s_info
->is_large
, false))
1355 if (bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
))
1356 s_info
->positions_needed
.large
.count
++;
1359 s_info
->positions_needed
.small_bitmask
1360 &= ~(((unsigned HOST_WIDE_INT
) 1) << pos
);
1363 /* Mark the whole store S_INFO as unneeded. */
1366 set_all_positions_unneeded (store_info_t s_info
)
1368 if (__builtin_expect (s_info
->is_large
, false))
1370 int pos
, end
= s_info
->end
- s_info
->begin
;
1371 for (pos
= 0; pos
< end
; pos
++)
1372 bitmap_set_bit (s_info
->positions_needed
.large
.bmap
, pos
);
1373 s_info
->positions_needed
.large
.count
= end
;
1376 s_info
->positions_needed
.small_bitmask
= (unsigned HOST_WIDE_INT
) 0;
1379 /* Return TRUE if any bytes from S_INFO store are needed. */
1382 any_positions_needed_p (store_info_t s_info
)
1384 if (__builtin_expect (s_info
->is_large
, false))
1385 return (s_info
->positions_needed
.large
.count
1386 < s_info
->end
- s_info
->begin
);
1388 return (s_info
->positions_needed
.small_bitmask
1389 != (unsigned HOST_WIDE_INT
) 0);
1392 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1393 store are needed. */
1396 all_positions_needed_p (store_info_t s_info
, int start
, int width
)
1398 if (__builtin_expect (s_info
->is_large
, false))
1400 int end
= start
+ width
;
1402 if (bitmap_bit_p (s_info
->positions_needed
.large
.bmap
, start
++))
1408 unsigned HOST_WIDE_INT mask
= lowpart_bitmask (width
) << start
;
1409 return (s_info
->positions_needed
.small_bitmask
& mask
) == mask
;
1414 static rtx
get_stored_val (store_info_t
, enum machine_mode
, HOST_WIDE_INT
,
1415 HOST_WIDE_INT
, basic_block
, bool);
1418 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1419 there is a candidate store, after adding it to the appropriate
1420 local store group if so. */
1423 record_store (rtx body
, bb_info_t bb_info
)
1425 rtx mem
, rhs
, const_rhs
, mem_addr
;
1426 HOST_WIDE_INT offset
= 0;
1427 HOST_WIDE_INT width
= 0;
1428 alias_set_type spill_alias_set
;
1429 insn_info_t insn_info
= bb_info
->last_insn
;
1430 store_info_t store_info
= NULL
;
1432 cselib_val
*base
= NULL
;
1433 insn_info_t ptr
, last
, redundant_reason
;
1434 bool store_is_unused
;
1436 if (GET_CODE (body
) != SET
&& GET_CODE (body
) != CLOBBER
)
1439 mem
= SET_DEST (body
);
1441 /* If this is not used, then this cannot be used to keep the insn
1442 from being deleted. On the other hand, it does provide something
1443 that can be used to prove that another store is dead. */
1445 = (find_reg_note (insn_info
->insn
, REG_UNUSED
, mem
) != NULL
);
1447 /* Check whether that value is a suitable memory location. */
1450 /* If the set or clobber is unused, then it does not effect our
1451 ability to get rid of the entire insn. */
1452 if (!store_is_unused
)
1453 insn_info
->cannot_delete
= true;
1457 /* At this point we know mem is a mem. */
1458 if (GET_MODE (mem
) == BLKmode
)
1460 if (GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
1463 fprintf (dump_file
, " adding wild read for (clobber (mem:BLK (scratch))\n");
1464 add_wild_read (bb_info
);
1465 insn_info
->cannot_delete
= true;
1468 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1469 as memset (addr, 0, 36); */
1470 else if (!MEM_SIZE_KNOWN_P (mem
)
1471 || MEM_SIZE (mem
) <= 0
1472 || MEM_SIZE (mem
) > MAX_OFFSET
1473 || GET_CODE (body
) != SET
1474 || !CONST_INT_P (SET_SRC (body
)))
1476 if (!store_is_unused
)
1478 /* If the set or clobber is unused, then it does not effect our
1479 ability to get rid of the entire insn. */
1480 insn_info
->cannot_delete
= true;
1481 clear_rhs_from_active_local_stores ();
1487 /* We can still process a volatile mem, we just cannot delete it. */
1488 if (MEM_VOLATILE_P (mem
))
1489 insn_info
->cannot_delete
= true;
1491 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
1493 clear_rhs_from_active_local_stores ();
1497 if (GET_MODE (mem
) == BLKmode
)
1498 width
= MEM_SIZE (mem
);
1501 width
= GET_MODE_SIZE (GET_MODE (mem
));
1502 gcc_assert ((unsigned) width
<= HOST_BITS_PER_WIDE_INT
);
1505 if (spill_alias_set
)
1507 bitmap store1
= clear_alias_group
->store1_p
;
1508 bitmap store2
= clear_alias_group
->store2_p
;
1510 gcc_assert (GET_MODE (mem
) != BLKmode
);
1512 if (!bitmap_set_bit (store1
, spill_alias_set
))
1513 bitmap_set_bit (store2
, spill_alias_set
);
1515 if (clear_alias_group
->offset_map_size_p
< spill_alias_set
)
1516 clear_alias_group
->offset_map_size_p
= spill_alias_set
;
1518 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1521 fprintf (dump_file
, " processing spill store %d(%s)\n",
1522 (int) spill_alias_set
, GET_MODE_NAME (GET_MODE (mem
)));
1524 else if (group_id
>= 0)
1526 /* In the restrictive case where the base is a constant or the
1527 frame pointer we can do global analysis. */
1530 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
1531 tree expr
= MEM_EXPR (mem
);
1533 store_info
= (store_info_t
) pool_alloc (rtx_store_info_pool
);
1534 set_usage_bits (group
, offset
, width
, expr
);
1537 fprintf (dump_file
, " processing const base store gid=%d[%d..%d)\n",
1538 group_id
, (int)offset
, (int)(offset
+width
));
1542 if (may_be_sp_based_p (XEXP (mem
, 0)))
1543 insn_info
->stack_pointer_based
= true;
1544 insn_info
->contains_cselib_groups
= true;
1546 store_info
= (store_info_t
) pool_alloc (cse_store_info_pool
);
1550 fprintf (dump_file
, " processing cselib store [%d..%d)\n",
1551 (int)offset
, (int)(offset
+width
));
1554 const_rhs
= rhs
= NULL_RTX
;
1555 if (GET_CODE (body
) == SET
1556 /* No place to keep the value after ra. */
1557 && !reload_completed
1558 && (REG_P (SET_SRC (body
))
1559 || GET_CODE (SET_SRC (body
)) == SUBREG
1560 || CONSTANT_P (SET_SRC (body
)))
1561 && !MEM_VOLATILE_P (mem
)
1562 /* Sometimes the store and reload is used for truncation and
1564 && !(FLOAT_MODE_P (GET_MODE (mem
)) && (flag_float_store
)))
1566 rhs
= SET_SRC (body
);
1567 if (CONSTANT_P (rhs
))
1569 else if (body
== PATTERN (insn_info
->insn
))
1571 rtx tem
= find_reg_note (insn_info
->insn
, REG_EQUAL
, NULL_RTX
);
1572 if (tem
&& CONSTANT_P (XEXP (tem
, 0)))
1573 const_rhs
= XEXP (tem
, 0);
1575 if (const_rhs
== NULL_RTX
&& REG_P (rhs
))
1577 rtx tem
= cselib_expand_value_rtx (rhs
, scratch
, 5);
1579 if (tem
&& CONSTANT_P (tem
))
1584 /* Check to see if this stores causes some other stores to be
1586 ptr
= active_local_stores
;
1588 redundant_reason
= NULL
;
1589 mem
= canon_rtx (mem
);
1590 /* For alias_set != 0 canon_true_dependence should be never called. */
1591 if (spill_alias_set
)
1592 mem_addr
= NULL_RTX
;
1596 mem_addr
= base
->val_rtx
;
1600 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
1601 mem_addr
= group
->canon_base_addr
;
1604 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
1609 insn_info_t next
= ptr
->next_local_store
;
1610 store_info_t s_info
= ptr
->store_rec
;
1613 /* Skip the clobbers. We delete the active insn if this insn
1614 shadows the set. To have been put on the active list, it
1615 has exactly on set. */
1616 while (!s_info
->is_set
)
1617 s_info
= s_info
->next
;
1619 if (s_info
->alias_set
!= spill_alias_set
)
1621 else if (s_info
->alias_set
)
1623 struct clear_alias_mode_holder
*entry
1624 = clear_alias_set_lookup (s_info
->alias_set
);
1625 /* Generally, spills cannot be processed if and of the
1626 references to the slot have a different mode. But if
1627 we are in the same block and mode is exactly the same
1628 between this store and one before in the same block,
1629 we can still delete it. */
1630 if ((GET_MODE (mem
) == GET_MODE (s_info
->mem
))
1631 && (GET_MODE (mem
) == entry
->mode
))
1634 set_all_positions_unneeded (s_info
);
1637 fprintf (dump_file
, " trying spill store in insn=%d alias_set=%d\n",
1638 INSN_UID (ptr
->insn
), (int) s_info
->alias_set
);
1640 else if ((s_info
->group_id
== group_id
)
1641 && (s_info
->cse_base
== base
))
1645 fprintf (dump_file
, " trying store in insn=%d gid=%d[%d..%d)\n",
1646 INSN_UID (ptr
->insn
), s_info
->group_id
,
1647 (int)s_info
->begin
, (int)s_info
->end
);
1649 /* Even if PTR won't be eliminated as unneeded, if both
1650 PTR and this insn store the same constant value, we might
1651 eliminate this insn instead. */
1652 if (s_info
->const_rhs
1654 && offset
>= s_info
->begin
1655 && offset
+ width
<= s_info
->end
1656 && all_positions_needed_p (s_info
, offset
- s_info
->begin
,
1659 if (GET_MODE (mem
) == BLKmode
)
1661 if (GET_MODE (s_info
->mem
) == BLKmode
1662 && s_info
->const_rhs
== const_rhs
)
1663 redundant_reason
= ptr
;
1665 else if (s_info
->const_rhs
== const0_rtx
1666 && const_rhs
== const0_rtx
)
1667 redundant_reason
= ptr
;
1672 val
= get_stored_val (s_info
, GET_MODE (mem
),
1673 offset
, offset
+ width
,
1674 BLOCK_FOR_INSN (insn_info
->insn
),
1676 if (get_insns () != NULL
)
1679 if (val
&& rtx_equal_p (val
, const_rhs
))
1680 redundant_reason
= ptr
;
1684 for (i
= MAX (offset
, s_info
->begin
);
1685 i
< offset
+ width
&& i
< s_info
->end
;
1687 set_position_unneeded (s_info
, i
- s_info
->begin
);
1689 else if (s_info
->rhs
)
1690 /* Need to see if it is possible for this store to overwrite
1691 the value of store_info. If it is, set the rhs to NULL to
1692 keep it from being used to remove a load. */
1694 if (canon_true_dependence (s_info
->mem
,
1695 GET_MODE (s_info
->mem
),
1700 s_info
->const_rhs
= NULL
;
1704 /* An insn can be deleted if every position of every one of
1705 its s_infos is zero. */
1706 if (any_positions_needed_p (s_info
))
1711 insn_info_t insn_to_delete
= ptr
;
1713 active_local_stores_len
--;
1715 last
->next_local_store
= ptr
->next_local_store
;
1717 active_local_stores
= ptr
->next_local_store
;
1719 if (!insn_to_delete
->cannot_delete
)
1720 delete_dead_store_insn (insn_to_delete
);
1728 /* Finish filling in the store_info. */
1729 store_info
->next
= insn_info
->store_rec
;
1730 insn_info
->store_rec
= store_info
;
1731 store_info
->mem
= mem
;
1732 store_info
->alias_set
= spill_alias_set
;
1733 store_info
->mem_addr
= mem_addr
;
1734 store_info
->cse_base
= base
;
1735 if (width
> HOST_BITS_PER_WIDE_INT
)
1737 store_info
->is_large
= true;
1738 store_info
->positions_needed
.large
.count
= 0;
1739 store_info
->positions_needed
.large
.bmap
= BITMAP_ALLOC (&dse_bitmap_obstack
);
1743 store_info
->is_large
= false;
1744 store_info
->positions_needed
.small_bitmask
= lowpart_bitmask (width
);
1746 store_info
->group_id
= group_id
;
1747 store_info
->begin
= offset
;
1748 store_info
->end
= offset
+ width
;
1749 store_info
->is_set
= GET_CODE (body
) == SET
;
1750 store_info
->rhs
= rhs
;
1751 store_info
->const_rhs
= const_rhs
;
1752 store_info
->redundant_reason
= redundant_reason
;
1754 /* If this is a clobber, we return 0. We will only be able to
1755 delete this insn if there is only one store USED store, but we
1756 can use the clobber to delete other stores earlier. */
1757 return store_info
->is_set
? 1 : 0;
1762 dump_insn_info (const char * start
, insn_info_t insn_info
)
1764 fprintf (dump_file
, "%s insn=%d %s\n", start
,
1765 INSN_UID (insn_info
->insn
),
1766 insn_info
->store_rec
? "has store" : "naked");
1770 /* If the modes are different and the value's source and target do not
1771 line up, we need to extract the value from lower part of the rhs of
1772 the store, shift it, and then put it into a form that can be shoved
1773 into the read_insn. This function generates a right SHIFT of a
1774 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1775 shift sequence is returned or NULL if we failed to find a
1779 find_shift_sequence (int access_size
,
1780 store_info_t store_info
,
1781 enum machine_mode read_mode
,
1782 int shift
, bool speed
, bool require_cst
)
1784 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1785 enum machine_mode new_mode
;
1786 rtx read_reg
= NULL
;
1788 /* Some machines like the x86 have shift insns for each size of
1789 operand. Other machines like the ppc or the ia-64 may only have
1790 shift insns that shift values within 32 or 64 bit registers.
1791 This loop tries to find the smallest shift insn that will right
1792 justify the value we want to read but is available in one insn on
1795 for (new_mode
= smallest_mode_for_size (access_size
* BITS_PER_UNIT
,
1797 GET_MODE_BITSIZE (new_mode
) <= BITS_PER_WORD
;
1798 new_mode
= GET_MODE_WIDER_MODE (new_mode
))
1800 rtx target
, new_reg
, shift_seq
, insn
, new_lhs
;
1803 /* If a constant was stored into memory, try to simplify it here,
1804 otherwise the cost of the shift might preclude this optimization
1805 e.g. at -Os, even when no actual shift will be needed. */
1806 if (store_info
->const_rhs
)
1808 unsigned int byte
= subreg_lowpart_offset (new_mode
, store_mode
);
1809 rtx ret
= simplify_subreg (new_mode
, store_info
->const_rhs
,
1811 if (ret
&& CONSTANT_P (ret
))
1813 ret
= simplify_const_binary_operation (LSHIFTRT
, new_mode
,
1814 ret
, GEN_INT (shift
));
1815 if (ret
&& CONSTANT_P (ret
))
1817 byte
= subreg_lowpart_offset (read_mode
, new_mode
);
1818 ret
= simplify_subreg (read_mode
, ret
, new_mode
, byte
);
1819 if (ret
&& CONSTANT_P (ret
)
1820 && set_src_cost (ret
, speed
) <= COSTS_N_INSNS (1))
1829 /* Try a wider mode if truncating the store mode to NEW_MODE
1830 requires a real instruction. */
1831 if (GET_MODE_BITSIZE (new_mode
) < GET_MODE_BITSIZE (store_mode
)
1832 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode
, store_mode
))
1835 /* Also try a wider mode if the necessary punning is either not
1836 desirable or not possible. */
1837 if (!CONSTANT_P (store_info
->rhs
)
1838 && !MODES_TIEABLE_P (new_mode
, store_mode
))
1841 new_reg
= gen_reg_rtx (new_mode
);
1845 /* In theory we could also check for an ashr. Ian Taylor knows
1846 of one dsp where the cost of these two was not the same. But
1847 this really is a rare case anyway. */
1848 target
= expand_binop (new_mode
, lshr_optab
, new_reg
,
1849 GEN_INT (shift
), new_reg
, 1, OPTAB_DIRECT
);
1851 shift_seq
= get_insns ();
1854 if (target
!= new_reg
|| shift_seq
== NULL
)
1858 for (insn
= shift_seq
; insn
!= NULL_RTX
; insn
= NEXT_INSN (insn
))
1860 cost
+= insn_rtx_cost (PATTERN (insn
), speed
);
1862 /* The computation up to here is essentially independent
1863 of the arguments and could be precomputed. It may
1864 not be worth doing so. We could precompute if
1865 worthwhile or at least cache the results. The result
1866 technically depends on both SHIFT and ACCESS_SIZE,
1867 but in practice the answer will depend only on ACCESS_SIZE. */
1869 if (cost
> COSTS_N_INSNS (1))
1872 new_lhs
= extract_low_bits (new_mode
, store_mode
,
1873 copy_rtx (store_info
->rhs
));
1874 if (new_lhs
== NULL_RTX
)
1877 /* We found an acceptable shift. Generate a move to
1878 take the value from the store and put it into the
1879 shift pseudo, then shift it, then generate another
1880 move to put in into the target of the read. */
1881 emit_move_insn (new_reg
, new_lhs
);
1882 emit_insn (shift_seq
);
1883 read_reg
= extract_low_bits (read_mode
, new_mode
, new_reg
);
1891 /* Call back for note_stores to find the hard regs set or clobbered by
1892 insn. Data is a bitmap of the hardregs set so far. */
1895 look_for_hardregs (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
1897 bitmap regs_set
= (bitmap
) data
;
1900 && HARD_REGISTER_P (x
))
1902 unsigned int regno
= REGNO (x
);
1903 bitmap_set_range (regs_set
, regno
,
1904 hard_regno_nregs
[regno
][GET_MODE (x
)]);
1908 /* Helper function for replace_read and record_store.
1909 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1910 to one before READ_END bytes read in READ_MODE. Return NULL
1911 if not successful. If REQUIRE_CST is true, return always constant. */
1914 get_stored_val (store_info_t store_info
, enum machine_mode read_mode
,
1915 HOST_WIDE_INT read_begin
, HOST_WIDE_INT read_end
,
1916 basic_block bb
, bool require_cst
)
1918 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
1920 int access_size
; /* In bytes. */
1923 /* To get here the read is within the boundaries of the write so
1924 shift will never be negative. Start out with the shift being in
1926 if (store_mode
== BLKmode
)
1928 else if (BYTES_BIG_ENDIAN
)
1929 shift
= store_info
->end
- read_end
;
1931 shift
= read_begin
- store_info
->begin
;
1933 access_size
= shift
+ GET_MODE_SIZE (read_mode
);
1935 /* From now on it is bits. */
1936 shift
*= BITS_PER_UNIT
;
1939 read_reg
= find_shift_sequence (access_size
, store_info
, read_mode
, shift
,
1940 optimize_bb_for_speed_p (bb
),
1942 else if (store_mode
== BLKmode
)
1944 /* The store is a memset (addr, const_val, const_size). */
1945 gcc_assert (CONST_INT_P (store_info
->rhs
));
1946 store_mode
= int_mode_for_mode (read_mode
);
1947 if (store_mode
== BLKmode
)
1948 read_reg
= NULL_RTX
;
1949 else if (store_info
->rhs
== const0_rtx
)
1950 read_reg
= extract_low_bits (read_mode
, store_mode
, const0_rtx
);
1951 else if (GET_MODE_BITSIZE (store_mode
) > HOST_BITS_PER_WIDE_INT
1952 || BITS_PER_UNIT
>= HOST_BITS_PER_WIDE_INT
)
1953 read_reg
= NULL_RTX
;
1956 unsigned HOST_WIDE_INT c
1957 = INTVAL (store_info
->rhs
)
1958 & (((HOST_WIDE_INT
) 1 << BITS_PER_UNIT
) - 1);
1959 int shift
= BITS_PER_UNIT
;
1960 while (shift
< HOST_BITS_PER_WIDE_INT
)
1965 read_reg
= gen_int_mode (c
, store_mode
);
1966 read_reg
= extract_low_bits (read_mode
, store_mode
, read_reg
);
1969 else if (store_info
->const_rhs
1971 || GET_MODE_CLASS (read_mode
) != GET_MODE_CLASS (store_mode
)))
1972 read_reg
= extract_low_bits (read_mode
, store_mode
,
1973 copy_rtx (store_info
->const_rhs
));
1975 read_reg
= extract_low_bits (read_mode
, store_mode
,
1976 copy_rtx (store_info
->rhs
));
1977 if (require_cst
&& read_reg
&& !CONSTANT_P (read_reg
))
1978 read_reg
= NULL_RTX
;
1982 /* Take a sequence of:
2005 Depending on the alignment and the mode of the store and
2009 The STORE_INFO and STORE_INSN are for the store and READ_INFO
2010 and READ_INSN are for the read. Return true if the replacement
2014 replace_read (store_info_t store_info
, insn_info_t store_insn
,
2015 read_info_t read_info
, insn_info_t read_insn
, rtx
*loc
,
2018 enum machine_mode store_mode
= GET_MODE (store_info
->mem
);
2019 enum machine_mode read_mode
= GET_MODE (read_info
->mem
);
2020 rtx insns
, this_insn
, read_reg
;
2026 /* Create a sequence of instructions to set up the read register.
2027 This sequence goes immediately before the store and its result
2028 is read by the load.
2030 We need to keep this in perspective. We are replacing a read
2031 with a sequence of insns, but the read will almost certainly be
2032 in cache, so it is not going to be an expensive one. Thus, we
2033 are not willing to do a multi insn shift or worse a subroutine
2034 call to get rid of the read. */
2036 fprintf (dump_file
, "trying to replace %smode load in insn %d"
2037 " from %smode store in insn %d\n",
2038 GET_MODE_NAME (read_mode
), INSN_UID (read_insn
->insn
),
2039 GET_MODE_NAME (store_mode
), INSN_UID (store_insn
->insn
));
2041 bb
= BLOCK_FOR_INSN (read_insn
->insn
);
2042 read_reg
= get_stored_val (store_info
,
2043 read_mode
, read_info
->begin
, read_info
->end
,
2045 if (read_reg
== NULL_RTX
)
2049 fprintf (dump_file
, " -- could not extract bits of stored value\n");
2052 /* Force the value into a new register so that it won't be clobbered
2053 between the store and the load. */
2054 read_reg
= copy_to_mode_reg (read_mode
, read_reg
);
2055 insns
= get_insns ();
2058 if (insns
!= NULL_RTX
)
2060 /* Now we have to scan the set of new instructions to see if the
2061 sequence contains and sets of hardregs that happened to be
2062 live at this point. For instance, this can happen if one of
2063 the insns sets the CC and the CC happened to be live at that
2064 point. This does occasionally happen, see PR 37922. */
2065 bitmap regs_set
= BITMAP_ALLOC (®_obstack
);
2067 for (this_insn
= insns
; this_insn
!= NULL_RTX
; this_insn
= NEXT_INSN (this_insn
))
2068 note_stores (PATTERN (this_insn
), look_for_hardregs
, regs_set
);
2070 bitmap_and_into (regs_set
, regs_live
);
2071 if (!bitmap_empty_p (regs_set
))
2076 "abandoning replacement because sequence clobbers live hardregs:");
2077 df_print_regset (dump_file
, regs_set
);
2080 BITMAP_FREE (regs_set
);
2083 BITMAP_FREE (regs_set
);
2086 if (validate_change (read_insn
->insn
, loc
, read_reg
, 0))
2088 deferred_change_t deferred_change
=
2089 (deferred_change_t
) pool_alloc (deferred_change_pool
);
2091 /* Insert this right before the store insn where it will be safe
2092 from later insns that might change it before the read. */
2093 emit_insn_before (insns
, store_insn
->insn
);
2095 /* And now for the kludge part: cselib croaks if you just
2096 return at this point. There are two reasons for this:
2098 1) Cselib has an idea of how many pseudos there are and
2099 that does not include the new ones we just added.
2101 2) Cselib does not know about the move insn we added
2102 above the store_info, and there is no way to tell it
2103 about it, because it has "moved on".
2105 Problem (1) is fixable with a certain amount of engineering.
2106 Problem (2) is requires starting the bb from scratch. This
2109 So we are just going to have to lie. The move/extraction
2110 insns are not really an issue, cselib did not see them. But
2111 the use of the new pseudo read_insn is a real problem because
2112 cselib has not scanned this insn. The way that we solve this
2113 problem is that we are just going to put the mem back for now
2114 and when we are finished with the block, we undo this. We
2115 keep a table of mems to get rid of. At the end of the basic
2116 block we can put them back. */
2118 *loc
= read_info
->mem
;
2119 deferred_change
->next
= deferred_change_list
;
2120 deferred_change_list
= deferred_change
;
2121 deferred_change
->loc
= loc
;
2122 deferred_change
->reg
= read_reg
;
2124 /* Get rid of the read_info, from the point of view of the
2125 rest of dse, play like this read never happened. */
2126 read_insn
->read_rec
= read_info
->next
;
2127 pool_free (read_info_pool
, read_info
);
2130 fprintf (dump_file
, " -- replaced the loaded MEM with ");
2131 print_simple_rtl (dump_file
, read_reg
);
2132 fprintf (dump_file
, "\n");
2140 fprintf (dump_file
, " -- replacing the loaded MEM with ");
2141 print_simple_rtl (dump_file
, read_reg
);
2142 fprintf (dump_file
, " led to an invalid instruction\n");
2148 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2149 if LOC is a mem and if it is look at the address and kill any
2150 appropriate stores that may be active. */
2153 check_mem_read_rtx (rtx
*loc
, void *data
)
2155 rtx mem
= *loc
, mem_addr
;
2157 insn_info_t insn_info
;
2158 HOST_WIDE_INT offset
= 0;
2159 HOST_WIDE_INT width
= 0;
2160 alias_set_type spill_alias_set
= 0;
2161 cselib_val
*base
= NULL
;
2163 read_info_t read_info
;
2165 if (!mem
|| !MEM_P (mem
))
2168 bb_info
= (bb_info_t
) data
;
2169 insn_info
= bb_info
->last_insn
;
2171 if ((MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2172 || (MEM_VOLATILE_P (mem
)))
2175 fprintf (dump_file
, " adding wild read, volatile or barrier.\n");
2176 add_wild_read (bb_info
);
2177 insn_info
->cannot_delete
= true;
2181 /* If it is reading readonly mem, then there can be no conflict with
2183 if (MEM_READONLY_P (mem
))
2186 if (!canon_address (mem
, &spill_alias_set
, &group_id
, &offset
, &base
))
2189 fprintf (dump_file
, " adding wild read, canon_address failure.\n");
2190 add_wild_read (bb_info
);
2194 if (GET_MODE (mem
) == BLKmode
)
2197 width
= GET_MODE_SIZE (GET_MODE (mem
));
2199 read_info
= (read_info_t
) pool_alloc (read_info_pool
);
2200 read_info
->group_id
= group_id
;
2201 read_info
->mem
= mem
;
2202 read_info
->alias_set
= spill_alias_set
;
2203 read_info
->begin
= offset
;
2204 read_info
->end
= offset
+ width
;
2205 read_info
->next
= insn_info
->read_rec
;
2206 insn_info
->read_rec
= read_info
;
2207 /* For alias_set != 0 canon_true_dependence should be never called. */
2208 if (spill_alias_set
)
2209 mem_addr
= NULL_RTX
;
2213 mem_addr
= base
->val_rtx
;
2217 = VEC_index (group_info_t
, rtx_group_vec
, group_id
);
2218 mem_addr
= group
->canon_base_addr
;
2221 mem_addr
= plus_constant (get_address_mode (mem
), mem_addr
, offset
);
2224 /* We ignore the clobbers in store_info. The is mildly aggressive,
2225 but there really should not be a clobber followed by a read. */
2227 if (spill_alias_set
)
2229 insn_info_t i_ptr
= active_local_stores
;
2230 insn_info_t last
= NULL
;
2233 fprintf (dump_file
, " processing spill load %d\n",
2234 (int) spill_alias_set
);
2238 store_info_t store_info
= i_ptr
->store_rec
;
2240 /* Skip the clobbers. */
2241 while (!store_info
->is_set
)
2242 store_info
= store_info
->next
;
2244 if (store_info
->alias_set
== spill_alias_set
)
2247 dump_insn_info ("removing from active", i_ptr
);
2249 active_local_stores_len
--;
2251 last
->next_local_store
= i_ptr
->next_local_store
;
2253 active_local_stores
= i_ptr
->next_local_store
;
2257 i_ptr
= i_ptr
->next_local_store
;
2260 else if (group_id
>= 0)
2262 /* This is the restricted case where the base is a constant or
2263 the frame pointer and offset is a constant. */
2264 insn_info_t i_ptr
= active_local_stores
;
2265 insn_info_t last
= NULL
;
2270 fprintf (dump_file
, " processing const load gid=%d[BLK]\n",
2273 fprintf (dump_file
, " processing const load gid=%d[%d..%d)\n",
2274 group_id
, (int)offset
, (int)(offset
+width
));
2279 bool remove
= false;
2280 store_info_t store_info
= i_ptr
->store_rec
;
2282 /* Skip the clobbers. */
2283 while (!store_info
->is_set
)
2284 store_info
= store_info
->next
;
2286 /* There are three cases here. */
2287 if (store_info
->group_id
< 0)
2288 /* We have a cselib store followed by a read from a
2291 = canon_true_dependence (store_info
->mem
,
2292 GET_MODE (store_info
->mem
),
2293 store_info
->mem_addr
,
2296 else if (group_id
== store_info
->group_id
)
2298 /* This is a block mode load. We may get lucky and
2299 canon_true_dependence may save the day. */
2302 = canon_true_dependence (store_info
->mem
,
2303 GET_MODE (store_info
->mem
),
2304 store_info
->mem_addr
,
2307 /* If this read is just reading back something that we just
2308 stored, rewrite the read. */
2312 && offset
>= store_info
->begin
2313 && offset
+ width
<= store_info
->end
2314 && all_positions_needed_p (store_info
,
2315 offset
- store_info
->begin
,
2317 && replace_read (store_info
, i_ptr
, read_info
,
2318 insn_info
, loc
, bb_info
->regs_live
))
2321 /* The bases are the same, just see if the offsets
2323 if ((offset
< store_info
->end
)
2324 && (offset
+ width
> store_info
->begin
))
2330 The else case that is missing here is that the
2331 bases are constant but different. There is nothing
2332 to do here because there is no overlap. */
2337 dump_insn_info ("removing from active", i_ptr
);
2339 active_local_stores_len
--;
2341 last
->next_local_store
= i_ptr
->next_local_store
;
2343 active_local_stores
= i_ptr
->next_local_store
;
2347 i_ptr
= i_ptr
->next_local_store
;
2352 insn_info_t i_ptr
= active_local_stores
;
2353 insn_info_t last
= NULL
;
2356 fprintf (dump_file
, " processing cselib load mem:");
2357 print_inline_rtx (dump_file
, mem
, 0);
2358 fprintf (dump_file
, "\n");
2363 bool remove
= false;
2364 store_info_t store_info
= i_ptr
->store_rec
;
2367 fprintf (dump_file
, " processing cselib load against insn %d\n",
2368 INSN_UID (i_ptr
->insn
));
2370 /* Skip the clobbers. */
2371 while (!store_info
->is_set
)
2372 store_info
= store_info
->next
;
2374 /* If this read is just reading back something that we just
2375 stored, rewrite the read. */
2377 && store_info
->group_id
== -1
2378 && store_info
->cse_base
== base
2380 && offset
>= store_info
->begin
2381 && offset
+ width
<= store_info
->end
2382 && all_positions_needed_p (store_info
,
2383 offset
- store_info
->begin
, width
)
2384 && replace_read (store_info
, i_ptr
, read_info
, insn_info
, loc
,
2385 bb_info
->regs_live
))
2388 if (!store_info
->alias_set
)
2389 remove
= canon_true_dependence (store_info
->mem
,
2390 GET_MODE (store_info
->mem
),
2391 store_info
->mem_addr
,
2397 dump_insn_info ("removing from active", i_ptr
);
2399 active_local_stores_len
--;
2401 last
->next_local_store
= i_ptr
->next_local_store
;
2403 active_local_stores
= i_ptr
->next_local_store
;
2407 i_ptr
= i_ptr
->next_local_store
;
2413 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2414 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2415 true for any part of *LOC. */
2418 check_mem_read_use (rtx
*loc
, void *data
)
2420 for_each_rtx (loc
, check_mem_read_rtx
, data
);
2424 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2425 So far it only handles arguments passed in registers. */
2428 get_call_args (rtx call_insn
, tree fn
, rtx
*args
, int nargs
)
2430 CUMULATIVE_ARGS args_so_far_v
;
2431 cumulative_args_t args_so_far
;
2435 INIT_CUMULATIVE_ARGS (args_so_far_v
, TREE_TYPE (fn
), NULL_RTX
, 0, 3);
2436 args_so_far
= pack_cumulative_args (&args_so_far_v
);
2438 arg
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
2440 arg
!= void_list_node
&& idx
< nargs
;
2441 arg
= TREE_CHAIN (arg
), idx
++)
2443 enum machine_mode mode
= TYPE_MODE (TREE_VALUE (arg
));
2445 reg
= targetm
.calls
.function_arg (args_so_far
, mode
, NULL_TREE
, true);
2446 if (!reg
|| !REG_P (reg
) || GET_MODE (reg
) != mode
2447 || GET_MODE_CLASS (mode
) != MODE_INT
)
2450 for (link
= CALL_INSN_FUNCTION_USAGE (call_insn
);
2452 link
= XEXP (link
, 1))
2453 if (GET_CODE (XEXP (link
, 0)) == USE
)
2455 args
[idx
] = XEXP (XEXP (link
, 0), 0);
2456 if (REG_P (args
[idx
])
2457 && REGNO (args
[idx
]) == REGNO (reg
)
2458 && (GET_MODE (args
[idx
]) == mode
2459 || (GET_MODE_CLASS (GET_MODE (args
[idx
])) == MODE_INT
2460 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2462 && (GET_MODE_SIZE (GET_MODE (args
[idx
]))
2463 > GET_MODE_SIZE (mode
)))))
2469 tmp
= cselib_expand_value_rtx (args
[idx
], scratch
, 5);
2470 if (GET_MODE (args
[idx
]) != mode
)
2472 if (!tmp
|| !CONST_INT_P (tmp
))
2474 tmp
= gen_int_mode (INTVAL (tmp
), mode
);
2479 targetm
.calls
.function_arg_advance (args_so_far
, mode
, NULL_TREE
, true);
2481 if (arg
!= void_list_node
|| idx
!= nargs
)
2486 /* Return a bitmap of the fixed registers contained in IN. */
2489 copy_fixed_regs (const_bitmap in
)
2493 ret
= ALLOC_REG_SET (NULL
);
2494 bitmap_and (ret
, in
, fixed_reg_set_regset
);
2498 /* Apply record_store to all candidate stores in INSN. Mark INSN
2499 if some part of it is not a candidate store and assigns to a
2500 non-register target. */
2503 scan_insn (bb_info_t bb_info
, rtx insn
)
2506 insn_info_t insn_info
= (insn_info_t
) pool_alloc (insn_info_pool
);
2508 memset (insn_info
, 0, sizeof (struct insn_info
));
2511 fprintf (dump_file
, "\n**scanning insn=%d\n",
2514 insn_info
->prev_insn
= bb_info
->last_insn
;
2515 insn_info
->insn
= insn
;
2516 bb_info
->last_insn
= insn_info
;
2518 if (DEBUG_INSN_P (insn
))
2520 insn_info
->cannot_delete
= true;
2524 /* Cselib clears the table for this case, so we have to essentially
2526 if (NONJUMP_INSN_P (insn
)
2527 && GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
2528 && MEM_VOLATILE_P (PATTERN (insn
)))
2530 add_wild_read (bb_info
);
2531 insn_info
->cannot_delete
= true;
2535 /* Look at all of the uses in the insn. */
2536 note_uses (&PATTERN (insn
), check_mem_read_use
, bb_info
);
2541 tree memset_call
= NULL_TREE
;
2543 insn_info
->cannot_delete
= true;
2545 /* Const functions cannot do anything bad i.e. read memory,
2546 however, they can read their parameters which may have
2547 been pushed onto the stack.
2548 memset and bzero don't read memory either. */
2549 const_call
= RTL_CONST_CALL_P (insn
);
2552 rtx call
= get_call_rtx_from (insn
);
2553 if (call
&& GET_CODE (XEXP (XEXP (call
, 0), 0)) == SYMBOL_REF
)
2555 rtx symbol
= XEXP (XEXP (call
, 0), 0);
2556 if (SYMBOL_REF_DECL (symbol
)
2557 && TREE_CODE (SYMBOL_REF_DECL (symbol
)) == FUNCTION_DECL
)
2559 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol
))
2561 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol
))
2562 == BUILT_IN_MEMSET
))
2563 || SYMBOL_REF_DECL (symbol
) == block_clear_fn
)
2564 memset_call
= SYMBOL_REF_DECL (symbol
);
2568 if (const_call
|| memset_call
)
2570 insn_info_t i_ptr
= active_local_stores
;
2571 insn_info_t last
= NULL
;
2574 fprintf (dump_file
, "%s call %d\n",
2575 const_call
? "const" : "memset", INSN_UID (insn
));
2577 /* See the head comment of the frame_read field. */
2578 if (reload_completed
)
2579 insn_info
->frame_read
= true;
2581 /* Loop over the active stores and remove those which are
2582 killed by the const function call. */
2585 bool remove_store
= false;
2587 /* The stack pointer based stores are always killed. */
2588 if (i_ptr
->stack_pointer_based
)
2589 remove_store
= true;
2591 /* If the frame is read, the frame related stores are killed. */
2592 else if (insn_info
->frame_read
)
2594 store_info_t store_info
= i_ptr
->store_rec
;
2596 /* Skip the clobbers. */
2597 while (!store_info
->is_set
)
2598 store_info
= store_info
->next
;
2600 if (store_info
->group_id
>= 0
2601 && VEC_index (group_info_t
, rtx_group_vec
,
2602 store_info
->group_id
)->frame_related
)
2603 remove_store
= true;
2609 dump_insn_info ("removing from active", i_ptr
);
2611 active_local_stores_len
--;
2613 last
->next_local_store
= i_ptr
->next_local_store
;
2615 active_local_stores
= i_ptr
->next_local_store
;
2620 i_ptr
= i_ptr
->next_local_store
;
2626 if (get_call_args (insn
, memset_call
, args
, 3)
2627 && CONST_INT_P (args
[1])
2628 && CONST_INT_P (args
[2])
2629 && INTVAL (args
[2]) > 0)
2631 rtx mem
= gen_rtx_MEM (BLKmode
, args
[0]);
2632 set_mem_size (mem
, INTVAL (args
[2]));
2633 body
= gen_rtx_SET (VOIDmode
, mem
, args
[1]);
2634 mems_found
+= record_store (body
, bb_info
);
2636 fprintf (dump_file
, "handling memset as BLKmode store\n");
2637 if (mems_found
== 1)
2639 if (active_local_stores_len
++
2640 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2642 active_local_stores_len
= 1;
2643 active_local_stores
= NULL
;
2645 insn_info
->fixed_regs_live
2646 = copy_fixed_regs (bb_info
->regs_live
);
2647 insn_info
->next_local_store
= active_local_stores
;
2648 active_local_stores
= insn_info
;
2655 /* Every other call, including pure functions, may read any memory
2656 that is not relative to the frame. */
2657 add_non_frame_wild_read (bb_info
);
2662 /* Assuming that there are sets in these insns, we cannot delete
2664 if ((GET_CODE (PATTERN (insn
)) == CLOBBER
)
2665 || volatile_refs_p (PATTERN (insn
))
2666 || (!cfun
->can_delete_dead_exceptions
&& !insn_nothrow_p (insn
))
2667 || (RTX_FRAME_RELATED_P (insn
))
2668 || find_reg_note (insn
, REG_FRAME_RELATED_EXPR
, NULL_RTX
))
2669 insn_info
->cannot_delete
= true;
2671 body
= PATTERN (insn
);
2672 if (GET_CODE (body
) == PARALLEL
)
2675 for (i
= 0; i
< XVECLEN (body
, 0); i
++)
2676 mems_found
+= record_store (XVECEXP (body
, 0, i
), bb_info
);
2679 mems_found
+= record_store (body
, bb_info
);
2682 fprintf (dump_file
, "mems_found = %d, cannot_delete = %s\n",
2683 mems_found
, insn_info
->cannot_delete
? "true" : "false");
2685 /* If we found some sets of mems, add it into the active_local_stores so
2686 that it can be locally deleted if found dead or used for
2687 replace_read and redundant constant store elimination. Otherwise mark
2688 it as cannot delete. This simplifies the processing later. */
2689 if (mems_found
== 1)
2691 if (active_local_stores_len
++
2692 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES
))
2694 active_local_stores_len
= 1;
2695 active_local_stores
= NULL
;
2697 insn_info
->fixed_regs_live
= copy_fixed_regs (bb_info
->regs_live
);
2698 insn_info
->next_local_store
= active_local_stores
;
2699 active_local_stores
= insn_info
;
2702 insn_info
->cannot_delete
= true;
2706 /* Remove BASE from the set of active_local_stores. This is a
2707 callback from cselib that is used to get rid of the stores in
2708 active_local_stores. */
2711 remove_useless_values (cselib_val
*base
)
2713 insn_info_t insn_info
= active_local_stores
;
2714 insn_info_t last
= NULL
;
2718 store_info_t store_info
= insn_info
->store_rec
;
2721 /* If ANY of the store_infos match the cselib group that is
2722 being deleted, then the insn can not be deleted. */
2725 if ((store_info
->group_id
== -1)
2726 && (store_info
->cse_base
== base
))
2731 store_info
= store_info
->next
;
2736 active_local_stores_len
--;
2738 last
->next_local_store
= insn_info
->next_local_store
;
2740 active_local_stores
= insn_info
->next_local_store
;
2741 free_store_info (insn_info
);
2746 insn_info
= insn_info
->next_local_store
;
2751 /* Do all of step 1. */
2757 bitmap regs_live
= BITMAP_ALLOC (®_obstack
);
2760 all_blocks
= BITMAP_ALLOC (NULL
);
2761 bitmap_set_bit (all_blocks
, ENTRY_BLOCK
);
2762 bitmap_set_bit (all_blocks
, EXIT_BLOCK
);
2767 bb_info_t bb_info
= (bb_info_t
) pool_alloc (bb_info_pool
);
2769 memset (bb_info
, 0, sizeof (struct bb_info
));
2770 bitmap_set_bit (all_blocks
, bb
->index
);
2771 bb_info
->regs_live
= regs_live
;
2773 bitmap_copy (regs_live
, DF_LR_IN (bb
));
2774 df_simulate_initialize_forwards (bb
, regs_live
);
2776 bb_table
[bb
->index
] = bb_info
;
2777 cselib_discard_hook
= remove_useless_values
;
2779 if (bb
->index
>= NUM_FIXED_BLOCKS
)
2784 = create_alloc_pool ("cse_store_info_pool",
2785 sizeof (struct store_info
), 100);
2786 active_local_stores
= NULL
;
2787 active_local_stores_len
= 0;
2788 cselib_clear_table ();
2790 /* Scan the insns. */
2791 FOR_BB_INSNS (bb
, insn
)
2794 scan_insn (bb_info
, insn
);
2795 cselib_process_insn (insn
);
2797 df_simulate_one_insn_forwards (bb
, insn
, regs_live
);
2800 /* This is something of a hack, because the global algorithm
2801 is supposed to take care of the case where stores go dead
2802 at the end of the function. However, the global
2803 algorithm must take a more conservative view of block
2804 mode reads than the local alg does. So to get the case
2805 where you have a store to the frame followed by a non
2806 overlapping block more read, we look at the active local
2807 stores at the end of the function and delete all of the
2808 frame and spill based ones. */
2809 if (stores_off_frame_dead_at_return
2810 && (EDGE_COUNT (bb
->succs
) == 0
2811 || (single_succ_p (bb
)
2812 && single_succ (bb
) == EXIT_BLOCK_PTR
2813 && ! crtl
->calls_eh_return
)))
2815 insn_info_t i_ptr
= active_local_stores
;
2818 store_info_t store_info
= i_ptr
->store_rec
;
2820 /* Skip the clobbers. */
2821 while (!store_info
->is_set
)
2822 store_info
= store_info
->next
;
2823 if (store_info
->alias_set
&& !i_ptr
->cannot_delete
)
2824 delete_dead_store_insn (i_ptr
);
2826 if (store_info
->group_id
>= 0)
2829 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
2830 if (group
->frame_related
&& !i_ptr
->cannot_delete
)
2831 delete_dead_store_insn (i_ptr
);
2834 i_ptr
= i_ptr
->next_local_store
;
2838 /* Get rid of the loads that were discovered in
2839 replace_read. Cselib is finished with this block. */
2840 while (deferred_change_list
)
2842 deferred_change_t next
= deferred_change_list
->next
;
2844 /* There is no reason to validate this change. That was
2846 *deferred_change_list
->loc
= deferred_change_list
->reg
;
2847 pool_free (deferred_change_pool
, deferred_change_list
);
2848 deferred_change_list
= next
;
2851 /* Get rid of all of the cselib based store_infos in this
2852 block and mark the containing insns as not being
2854 ptr
= bb_info
->last_insn
;
2857 if (ptr
->contains_cselib_groups
)
2859 store_info_t s_info
= ptr
->store_rec
;
2860 while (s_info
&& !s_info
->is_set
)
2861 s_info
= s_info
->next
;
2863 && s_info
->redundant_reason
2864 && s_info
->redundant_reason
->insn
2865 && !ptr
->cannot_delete
)
2868 fprintf (dump_file
, "Locally deleting insn %d "
2869 "because insn %d stores the "
2870 "same value and couldn't be "
2872 INSN_UID (ptr
->insn
),
2873 INSN_UID (s_info
->redundant_reason
->insn
));
2874 delete_dead_store_insn (ptr
);
2877 s_info
->redundant_reason
= NULL
;
2878 free_store_info (ptr
);
2882 store_info_t s_info
;
2884 /* Free at least positions_needed bitmaps. */
2885 for (s_info
= ptr
->store_rec
; s_info
; s_info
= s_info
->next
)
2886 if (s_info
->is_large
)
2888 BITMAP_FREE (s_info
->positions_needed
.large
.bmap
);
2889 s_info
->is_large
= false;
2892 ptr
= ptr
->prev_insn
;
2895 free_alloc_pool (cse_store_info_pool
);
2897 bb_info
->regs_live
= NULL
;
2900 BITMAP_FREE (regs_live
);
2902 rtx_group_table
.empty ();
2906 /*----------------------------------------------------------------------------
2909 Assign each byte position in the stores that we are going to
2910 analyze globally to a position in the bitmaps. Returns true if
2911 there are any bit positions assigned.
2912 ----------------------------------------------------------------------------*/
2915 dse_step2_init (void)
2920 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
2922 /* For all non stack related bases, we only consider a store to
2923 be deletable if there are two or more stores for that
2924 position. This is because it takes one store to make the
2925 other store redundant. However, for the stores that are
2926 stack related, we consider them if there is only one store
2927 for the position. We do this because the stack related
2928 stores can be deleted if their is no read between them and
2929 the end of the function.
2931 To make this work in the current framework, we take the stack
2932 related bases add all of the bits from store1 into store2.
2933 This has the effect of making the eligible even if there is
2936 if (stores_off_frame_dead_at_return
&& group
->frame_related
)
2938 bitmap_ior_into (group
->store2_n
, group
->store1_n
);
2939 bitmap_ior_into (group
->store2_p
, group
->store1_p
);
2941 fprintf (dump_file
, "group %d is frame related ", i
);
2944 group
->offset_map_size_n
++;
2945 group
->offset_map_n
= XOBNEWVEC (&dse_obstack
, int,
2946 group
->offset_map_size_n
);
2947 group
->offset_map_size_p
++;
2948 group
->offset_map_p
= XOBNEWVEC (&dse_obstack
, int,
2949 group
->offset_map_size_p
);
2950 group
->process_globally
= false;
2953 fprintf (dump_file
, "group %d(%d+%d): ", i
,
2954 (int)bitmap_count_bits (group
->store2_n
),
2955 (int)bitmap_count_bits (group
->store2_p
));
2956 bitmap_print (dump_file
, group
->store2_n
, "n ", " ");
2957 bitmap_print (dump_file
, group
->store2_p
, "p ", "\n");
2963 /* Init the offset tables for the normal case. */
2966 dse_step2_nospill (void)
2970 /* Position 0 is unused because 0 is used in the maps to mean
2972 current_position
= 1;
2973 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
2978 if (group
== clear_alias_group
)
2981 memset (group
->offset_map_n
, 0, sizeof(int) * group
->offset_map_size_n
);
2982 memset (group
->offset_map_p
, 0, sizeof(int) * group
->offset_map_size_p
);
2983 bitmap_clear (group
->group_kill
);
2985 EXECUTE_IF_SET_IN_BITMAP (group
->store2_n
, 0, j
, bi
)
2987 bitmap_set_bit (group
->group_kill
, current_position
);
2988 if (bitmap_bit_p (group
->escaped_n
, j
))
2989 bitmap_set_bit (kill_on_calls
, current_position
);
2990 group
->offset_map_n
[j
] = current_position
++;
2991 group
->process_globally
= true;
2993 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
2995 bitmap_set_bit (group
->group_kill
, current_position
);
2996 if (bitmap_bit_p (group
->escaped_p
, j
))
2997 bitmap_set_bit (kill_on_calls
, current_position
);
2998 group
->offset_map_p
[j
] = current_position
++;
2999 group
->process_globally
= true;
3002 return current_position
!= 1;
3006 /* Init the offset tables for the spill case. */
3009 dse_step2_spill (void)
3012 group_info_t group
= clear_alias_group
;
3015 /* Position 0 is unused because 0 is used in the maps to mean
3017 current_position
= 1;
3021 bitmap_print (dump_file
, clear_alias_sets
,
3022 "clear alias sets ", "\n");
3023 bitmap_print (dump_file
, disqualified_clear_alias_sets
,
3024 "disqualified clear alias sets ", "\n");
3027 memset (group
->offset_map_n
, 0, sizeof(int) * group
->offset_map_size_n
);
3028 memset (group
->offset_map_p
, 0, sizeof(int) * group
->offset_map_size_p
);
3029 bitmap_clear (group
->group_kill
);
3031 /* Remove the disqualified positions from the store2_p set. */
3032 bitmap_and_compl_into (group
->store2_p
, disqualified_clear_alias_sets
);
3034 /* We do not need to process the store2_n set because
3035 alias_sets are always positive. */
3036 EXECUTE_IF_SET_IN_BITMAP (group
->store2_p
, 0, j
, bi
)
3038 bitmap_set_bit (group
->group_kill
, current_position
);
3039 group
->offset_map_p
[j
] = current_position
++;
3040 group
->process_globally
= true;
3043 return current_position
!= 1;
3048 /*----------------------------------------------------------------------------
3051 Build the bit vectors for the transfer functions.
3052 ----------------------------------------------------------------------------*/
3055 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
3059 get_bitmap_index (group_info_t group_info
, HOST_WIDE_INT offset
)
3063 HOST_WIDE_INT offset_p
= -offset
;
3064 if (offset_p
>= group_info
->offset_map_size_n
)
3066 return group_info
->offset_map_n
[offset_p
];
3070 if (offset
>= group_info
->offset_map_size_p
)
3072 return group_info
->offset_map_p
[offset
];
3077 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3081 scan_stores_nospill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3086 group_info_t group_info
3087 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
3088 if (group_info
->process_globally
)
3089 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3091 int index
= get_bitmap_index (group_info
, i
);
3094 bitmap_set_bit (gen
, index
);
3096 bitmap_clear_bit (kill
, index
);
3099 store_info
= store_info
->next
;
3104 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3108 scan_stores_spill (store_info_t store_info
, bitmap gen
, bitmap kill
)
3112 if (store_info
->alias_set
)
3114 int index
= get_bitmap_index (clear_alias_group
,
3115 store_info
->alias_set
);
3118 bitmap_set_bit (gen
, index
);
3120 bitmap_clear_bit (kill
, index
);
3123 store_info
= store_info
->next
;
3128 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3132 scan_reads_nospill (insn_info_t insn_info
, bitmap gen
, bitmap kill
)
3134 read_info_t read_info
= insn_info
->read_rec
;
3138 /* If this insn reads the frame, kill all the frame related stores. */
3139 if (insn_info
->frame_read
)
3141 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3142 if (group
->process_globally
&& group
->frame_related
)
3145 bitmap_ior_into (kill
, group
->group_kill
);
3146 bitmap_and_compl_into (gen
, group
->group_kill
);
3149 if (insn_info
->non_frame_wild_read
)
3151 /* Kill all non-frame related stores. Kill all stores of variables that
3154 bitmap_ior_into (kill
, kill_on_calls
);
3155 bitmap_and_compl_into (gen
, kill_on_calls
);
3156 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3157 if (group
->process_globally
&& !group
->frame_related
)
3160 bitmap_ior_into (kill
, group
->group_kill
);
3161 bitmap_and_compl_into (gen
, group
->group_kill
);
3166 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3168 if (group
->process_globally
)
3170 if (i
== read_info
->group_id
)
3172 if (read_info
->begin
> read_info
->end
)
3174 /* Begin > end for block mode reads. */
3176 bitmap_ior_into (kill
, group
->group_kill
);
3177 bitmap_and_compl_into (gen
, group
->group_kill
);
3181 /* The groups are the same, just process the
3184 for (j
= read_info
->begin
; j
< read_info
->end
; j
++)
3186 int index
= get_bitmap_index (group
, j
);
3190 bitmap_set_bit (kill
, index
);
3191 bitmap_clear_bit (gen
, index
);
3198 /* The groups are different, if the alias sets
3199 conflict, clear the entire group. We only need
3200 to apply this test if the read_info is a cselib
3201 read. Anything with a constant base cannot alias
3202 something else with a different constant
3204 if ((read_info
->group_id
< 0)
3205 && canon_true_dependence (group
->base_mem
,
3206 GET_MODE (group
->base_mem
),
3207 group
->canon_base_addr
,
3208 read_info
->mem
, NULL_RTX
))
3211 bitmap_ior_into (kill
, group
->group_kill
);
3212 bitmap_and_compl_into (gen
, group
->group_kill
);
3218 read_info
= read_info
->next
;
3222 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3226 scan_reads_spill (read_info_t read_info
, bitmap gen
, bitmap kill
)
3230 if (read_info
->alias_set
)
3232 int index
= get_bitmap_index (clear_alias_group
,
3233 read_info
->alias_set
);
3237 bitmap_set_bit (kill
, index
);
3238 bitmap_clear_bit (gen
, index
);
3242 read_info
= read_info
->next
;
3247 /* Return the insn in BB_INFO before the first wild read or if there
3248 are no wild reads in the block, return the last insn. */
3251 find_insn_before_first_wild_read (bb_info_t bb_info
)
3253 insn_info_t insn_info
= bb_info
->last_insn
;
3254 insn_info_t last_wild_read
= NULL
;
3258 if (insn_info
->wild_read
)
3260 last_wild_read
= insn_info
->prev_insn
;
3261 /* Block starts with wild read. */
3262 if (!last_wild_read
)
3266 insn_info
= insn_info
->prev_insn
;
3270 return last_wild_read
;
3272 return bb_info
->last_insn
;
3276 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3277 the block in order to build the gen and kill sets for the block.
3278 We start at ptr which may be the last insn in the block or may be
3279 the first insn with a wild read. In the latter case we are able to
3280 skip the rest of the block because it just does not matter:
3281 anything that happens is hidden by the wild read. */
3284 dse_step3_scan (bool for_spills
, basic_block bb
)
3286 bb_info_t bb_info
= bb_table
[bb
->index
];
3287 insn_info_t insn_info
;
3290 /* There are no wild reads in the spill case. */
3291 insn_info
= bb_info
->last_insn
;
3293 insn_info
= find_insn_before_first_wild_read (bb_info
);
3295 /* In the spill case or in the no_spill case if there is no wild
3296 read in the block, we will need a kill set. */
3297 if (insn_info
== bb_info
->last_insn
)
3300 bitmap_clear (bb_info
->kill
);
3302 bb_info
->kill
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3306 BITMAP_FREE (bb_info
->kill
);
3310 /* There may have been code deleted by the dce pass run before
3312 if (insn_info
->insn
&& INSN_P (insn_info
->insn
))
3314 /* Process the read(s) last. */
3317 scan_stores_spill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3318 scan_reads_spill (insn_info
->read_rec
, bb_info
->gen
, bb_info
->kill
);
3322 scan_stores_nospill (insn_info
->store_rec
, bb_info
->gen
, bb_info
->kill
);
3323 scan_reads_nospill (insn_info
, bb_info
->gen
, bb_info
->kill
);
3327 insn_info
= insn_info
->prev_insn
;
3332 /* Set the gen set of the exit block, and also any block with no
3333 successors that does not have a wild read. */
3336 dse_step3_exit_block_scan (bb_info_t bb_info
)
3338 /* The gen set is all 0's for the exit block except for the
3339 frame_pointer_group. */
3341 if (stores_off_frame_dead_at_return
)
3346 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, i
, group
)
3348 if (group
->process_globally
&& group
->frame_related
)
3349 bitmap_ior_into (bb_info
->gen
, group
->group_kill
);
3355 /* Find all of the blocks that are not backwards reachable from the
3356 exit block or any block with no successors (BB). These are the
3357 infinite loops or infinite self loops. These blocks will still
3358 have their bits set in UNREACHABLE_BLOCKS. */
3361 mark_reachable_blocks (sbitmap unreachable_blocks
, basic_block bb
)
3366 if (TEST_BIT (unreachable_blocks
, bb
->index
))
3368 RESET_BIT (unreachable_blocks
, bb
->index
);
3369 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3371 mark_reachable_blocks (unreachable_blocks
, e
->src
);
3376 /* Build the transfer functions for the function. */
3379 dse_step3 (bool for_spills
)
3382 sbitmap unreachable_blocks
= sbitmap_alloc (last_basic_block
);
3383 sbitmap_iterator sbi
;
3384 bitmap all_ones
= NULL
;
3387 sbitmap_ones (unreachable_blocks
);
3391 bb_info_t bb_info
= bb_table
[bb
->index
];
3393 bitmap_clear (bb_info
->gen
);
3395 bb_info
->gen
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3397 if (bb
->index
== ENTRY_BLOCK
)
3399 else if (bb
->index
== EXIT_BLOCK
)
3400 dse_step3_exit_block_scan (bb_info
);
3402 dse_step3_scan (for_spills
, bb
);
3403 if (EDGE_COUNT (bb
->succs
) == 0)
3404 mark_reachable_blocks (unreachable_blocks
, bb
);
3406 /* If this is the second time dataflow is run, delete the old
3409 BITMAP_FREE (bb_info
->in
);
3411 BITMAP_FREE (bb_info
->out
);
3414 /* For any block in an infinite loop, we must initialize the out set
3415 to all ones. This could be expensive, but almost never occurs in
3416 practice. However, it is common in regression tests. */
3417 EXECUTE_IF_SET_IN_SBITMAP (unreachable_blocks
, 0, i
, sbi
)
3419 if (bitmap_bit_p (all_blocks
, i
))
3421 bb_info_t bb_info
= bb_table
[i
];
3427 all_ones
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3428 FOR_EACH_VEC_ELT (group_info_t
, rtx_group_vec
, j
, group
)
3429 bitmap_ior_into (all_ones
, group
->group_kill
);
3433 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3434 bitmap_copy (bb_info
->out
, all_ones
);
3440 BITMAP_FREE (all_ones
);
3441 sbitmap_free (unreachable_blocks
);
3446 /*----------------------------------------------------------------------------
3449 Solve the bitvector equations.
3450 ----------------------------------------------------------------------------*/
3453 /* Confluence function for blocks with no successors. Create an out
3454 set from the gen set of the exit block. This block logically has
3455 the exit block as a successor. */
3460 dse_confluence_0 (basic_block bb
)
3462 bb_info_t bb_info
= bb_table
[bb
->index
];
3464 if (bb
->index
== EXIT_BLOCK
)
3469 bb_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3470 bitmap_copy (bb_info
->out
, bb_table
[EXIT_BLOCK
]->gen
);
3474 /* Propagate the information from the in set of the dest of E to the
3475 out set of the src of E. If the various in or out sets are not
3476 there, that means they are all ones. */
3479 dse_confluence_n (edge e
)
3481 bb_info_t src_info
= bb_table
[e
->src
->index
];
3482 bb_info_t dest_info
= bb_table
[e
->dest
->index
];
3487 bitmap_and_into (src_info
->out
, dest_info
->in
);
3490 src_info
->out
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3491 bitmap_copy (src_info
->out
, dest_info
->in
);
3498 /* Propagate the info from the out to the in set of BB_INDEX's basic
3499 block. There are three cases:
3501 1) The block has no kill set. In this case the kill set is all
3502 ones. It does not matter what the out set of the block is, none of
3503 the info can reach the top. The only thing that reaches the top is
3504 the gen set and we just copy the set.
3506 2) There is a kill set but no out set and bb has successors. In
3507 this case we just return. Eventually an out set will be created and
3508 it is better to wait than to create a set of ones.
3510 3) There is both a kill and out set. We apply the obvious transfer
3515 dse_transfer_function (int bb_index
)
3517 bb_info_t bb_info
= bb_table
[bb_index
];
3525 return bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3526 bb_info
->out
, bb_info
->kill
);
3529 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3530 bitmap_ior_and_compl (bb_info
->in
, bb_info
->gen
,
3531 bb_info
->out
, bb_info
->kill
);
3541 /* Case 1 above. If there is already an in set, nothing
3547 bb_info
->in
= BITMAP_ALLOC (&dse_bitmap_obstack
);
3548 bitmap_copy (bb_info
->in
, bb_info
->gen
);
3554 /* Solve the dataflow equations. */
3559 df_simple_dataflow (DF_BACKWARD
, NULL
, dse_confluence_0
,
3560 dse_confluence_n
, dse_transfer_function
,
3561 all_blocks
, df_get_postorder (DF_BACKWARD
),
3562 df_get_n_blocks (DF_BACKWARD
));
3567 fprintf (dump_file
, "\n\n*** Global dataflow info after analysis.\n");
3570 bb_info_t bb_info
= bb_table
[bb
->index
];
3572 df_print_bb_index (bb
, dump_file
);
3574 bitmap_print (dump_file
, bb_info
->in
, " in: ", "\n");
3576 fprintf (dump_file
, " in: *MISSING*\n");
3578 bitmap_print (dump_file
, bb_info
->gen
, " gen: ", "\n");
3580 fprintf (dump_file
, " gen: *MISSING*\n");
3582 bitmap_print (dump_file
, bb_info
->kill
, " kill: ", "\n");
3584 fprintf (dump_file
, " kill: *MISSING*\n");
3586 bitmap_print (dump_file
, bb_info
->out
, " out: ", "\n");
3588 fprintf (dump_file
, " out: *MISSING*\n\n");
3595 /*----------------------------------------------------------------------------
3598 Delete the stores that can only be deleted using the global information.
3599 ----------------------------------------------------------------------------*/
3603 dse_step5_nospill (void)
3608 bb_info_t bb_info
= bb_table
[bb
->index
];
3609 insn_info_t insn_info
= bb_info
->last_insn
;
3610 bitmap v
= bb_info
->out
;
3614 bool deleted
= false;
3615 if (dump_file
&& insn_info
->insn
)
3617 fprintf (dump_file
, "starting to process insn %d\n",
3618 INSN_UID (insn_info
->insn
));
3619 bitmap_print (dump_file
, v
, " v: ", "\n");
3622 /* There may have been code deleted by the dce pass run before
3625 && INSN_P (insn_info
->insn
)
3626 && (!insn_info
->cannot_delete
)
3627 && (!bitmap_empty_p (v
)))
3629 store_info_t store_info
= insn_info
->store_rec
;
3631 /* Try to delete the current insn. */
3634 /* Skip the clobbers. */
3635 while (!store_info
->is_set
)
3636 store_info
= store_info
->next
;
3638 if (store_info
->alias_set
)
3643 group_info_t group_info
3644 = VEC_index (group_info_t
, rtx_group_vec
, store_info
->group_id
);
3646 for (i
= store_info
->begin
; i
< store_info
->end
; i
++)
3648 int index
= get_bitmap_index (group_info
, i
);
3651 fprintf (dump_file
, "i = %d, index = %d\n", (int)i
, index
);
3652 if (index
== 0 || !bitmap_bit_p (v
, index
))
3655 fprintf (dump_file
, "failing at i = %d\n", (int)i
);
3664 && check_for_inc_dec_1 (insn_info
))
3666 delete_insn (insn_info
->insn
);
3667 insn_info
->insn
= NULL
;
3672 /* We do want to process the local info if the insn was
3673 deleted. For instance, if the insn did a wild read, we
3674 no longer need to trash the info. */
3676 && INSN_P (insn_info
->insn
)
3679 scan_stores_nospill (insn_info
->store_rec
, v
, NULL
);
3680 if (insn_info
->wild_read
)
3683 fprintf (dump_file
, "wild read\n");
3686 else if (insn_info
->read_rec
3687 || insn_info
->non_frame_wild_read
)
3689 if (dump_file
&& !insn_info
->non_frame_wild_read
)
3690 fprintf (dump_file
, "regular read\n");
3692 fprintf (dump_file
, "non-frame wild read\n");
3693 scan_reads_nospill (insn_info
, v
, NULL
);
3697 insn_info
= insn_info
->prev_insn
;
3704 dse_step5_spill (void)
3709 bb_info_t bb_info
= bb_table
[bb
->index
];
3710 insn_info_t insn_info
= bb_info
->last_insn
;
3711 bitmap v
= bb_info
->out
;
3715 bool deleted
= false;
3716 /* There may have been code deleted by the dce pass run before
3719 && INSN_P (insn_info
->insn
)
3720 && (!insn_info
->cannot_delete
)
3721 && (!bitmap_empty_p (v
)))
3723 /* Try to delete the current insn. */
3724 store_info_t store_info
= insn_info
->store_rec
;
3729 if (store_info
->alias_set
)
3731 int index
= get_bitmap_index (clear_alias_group
,
3732 store_info
->alias_set
);
3733 if (index
== 0 || !bitmap_bit_p (v
, index
))
3741 store_info
= store_info
->next
;
3743 if (deleted
&& dbg_cnt (dse
)
3744 && check_for_inc_dec_1 (insn_info
))
3747 fprintf (dump_file
, "Spill deleting insn %d\n",
3748 INSN_UID (insn_info
->insn
));
3749 delete_insn (insn_info
->insn
);
3751 insn_info
->insn
= NULL
;
3756 && INSN_P (insn_info
->insn
)
3759 scan_stores_spill (insn_info
->store_rec
, v
, NULL
);
3760 scan_reads_spill (insn_info
->read_rec
, v
, NULL
);
3763 insn_info
= insn_info
->prev_insn
;
3770 /*----------------------------------------------------------------------------
3773 Delete stores made redundant by earlier stores (which store the same
3774 value) that couldn't be eliminated.
3775 ----------------------------------------------------------------------------*/
3784 bb_info_t bb_info
= bb_table
[bb
->index
];
3785 insn_info_t insn_info
= bb_info
->last_insn
;
3789 /* There may have been code deleted by the dce pass run before
3792 && INSN_P (insn_info
->insn
)
3793 && !insn_info
->cannot_delete
)
3795 store_info_t s_info
= insn_info
->store_rec
;
3797 while (s_info
&& !s_info
->is_set
)
3798 s_info
= s_info
->next
;
3800 && s_info
->redundant_reason
3801 && s_info
->redundant_reason
->insn
3802 && INSN_P (s_info
->redundant_reason
->insn
))
3804 rtx rinsn
= s_info
->redundant_reason
->insn
;
3806 fprintf (dump_file
, "Locally deleting insn %d "
3807 "because insn %d stores the "
3808 "same value and couldn't be "
3810 INSN_UID (insn_info
->insn
),
3812 delete_dead_store_insn (insn_info
);
3815 insn_info
= insn_info
->prev_insn
;
3820 /*----------------------------------------------------------------------------
3823 Destroy everything left standing.
3824 ----------------------------------------------------------------------------*/
3829 bitmap_obstack_release (&dse_bitmap_obstack
);
3830 obstack_free (&dse_obstack
, NULL
);
3832 if (clear_alias_sets
)
3834 BITMAP_FREE (clear_alias_sets
);
3835 BITMAP_FREE (disqualified_clear_alias_sets
);
3836 free_alloc_pool (clear_alias_mode_pool
);
3837 htab_delete (clear_alias_mode_table
);
3840 end_alias_analysis ();
3842 rtx_group_table
.dispose ();
3843 VEC_free (group_info_t
, heap
, rtx_group_vec
);
3844 BITMAP_FREE (all_blocks
);
3845 BITMAP_FREE (scratch
);
3847 free_alloc_pool (rtx_store_info_pool
);
3848 free_alloc_pool (read_info_pool
);
3849 free_alloc_pool (insn_info_pool
);
3850 free_alloc_pool (bb_info_pool
);
3851 free_alloc_pool (rtx_group_info_pool
);
3852 free_alloc_pool (deferred_change_pool
);
3856 /* -------------------------------------------------------------------------
3858 ------------------------------------------------------------------------- */
3860 /* Callback for running pass_rtl_dse. */
3863 rest_of_handle_dse (void)
3865 bool did_global
= false;
3867 df_set_flags (DF_DEFER_INSN_RESCAN
);
3869 /* Need the notes since we must track live hardregs in the forwards
3871 df_note_add_problem ();
3877 if (dse_step2_nospill ())
3879 df_set_flags (DF_LR_RUN_DCE
);
3883 fprintf (dump_file
, "doing global processing\n");
3886 dse_step5_nospill ();
3889 /* For the instance of dse that runs after reload, we make a special
3890 pass to process the spills. These are special in that they are
3891 totally transparent, i.e, there is no aliasing issues that need
3892 to be considered. This means that the wild reads that kill
3893 everything else do not apply here. */
3894 if (clear_alias_sets
&& dse_step2_spill ())
3898 df_set_flags (DF_LR_RUN_DCE
);
3903 fprintf (dump_file
, "doing global spill processing\n");
3913 fprintf (dump_file
, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3914 locally_deleted
, globally_deleted
, spill_deleted
);
3921 return optimize
> 0 && flag_dse
3928 return optimize
> 0 && flag_dse
3932 struct rtl_opt_pass pass_rtl_dse1
=
3937 gate_dse1
, /* gate */
3938 rest_of_handle_dse
, /* execute */
3941 0, /* static_pass_number */
3942 TV_DSE1
, /* tv_id */
3943 0, /* properties_required */
3944 0, /* properties_provided */
3945 0, /* properties_destroyed */
3946 0, /* todo_flags_start */
3947 TODO_df_finish
| TODO_verify_rtl_sharing
|
3948 TODO_ggc_collect
/* todo_flags_finish */
3952 struct rtl_opt_pass pass_rtl_dse2
=
3957 gate_dse2
, /* gate */
3958 rest_of_handle_dse
, /* execute */
3961 0, /* static_pass_number */
3962 TV_DSE2
, /* tv_id */
3963 0, /* properties_required */
3964 0, /* properties_provided */
3965 0, /* properties_destroyed */
3966 0, /* todo_flags_start */
3967 TODO_df_finish
| TODO_verify_rtl_sharing
|
3968 TODO_ggc_collect
/* todo_flags_finish */