1 /* RTL dead store elimination.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
5 and Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
27 #include "coretypes.h"
34 #include "hard-reg-set.h"
39 #include "tree-pass.h"
40 #include "alloc-pool.h"
42 #include "insn-config.h"
50 /* This file contains three techniques for performing Dead Store
53 * The first technique performs dse locally on any base address. It
54 is based on the cselib which is a local value numbering technique.
55 This technique is local to a basic block but deals with a fairly
58 * The second technique performs dse globally but is restricted to
59 base addresses that are either constant or are relative to the
62 * The third technique, (which is only done after register allocation)
63 processes the spill spill slots. This differs from the second
64 technique because it takes advantage of the fact that spilling is
65 completely free from the effects of aliasing.
67 Logically, dse is a backwards dataflow problem. A store can be
68 deleted if it if cannot be reached in the backward direction by any
69 use of the value being stored. However, the local technique uses a
70 forwards scan of the basic block because cselib requires that the
71 block be processed in that order.
73 The pass is logically broken into 7 steps:
77 1) The local algorithm, as well as scanning the insns for the two
80 2) Analysis to see if the global algs are necessary. In the case
81 of stores base on a constant address, there must be at least two
82 stores to that address, to make it possible to delete some of the
83 stores. In the case of stores off of the frame or spill related
84 stores, only one store to an address is necessary because those
85 stores die at the end of the function.
87 3) Set up the global dataflow equations based on processing the
88 info parsed in the first step.
90 4) Solve the dataflow equations.
92 5) Delete the insns that the global analysis has indicated are
95 6) Delete insns that store the same value as preceeding store
96 where the earlier store couldn't be eliminated.
100 This step uses cselib and canon_rtx to build the largest expression
101 possible for each address. This pass is a forwards pass through
102 each basic block. From the point of view of the global technique,
103 the first pass could examine a block in either direction. The
104 forwards ordering is to accommodate cselib.
106 We a simplifying assumption: addresses fall into four broad
109 1) base has rtx_varies_p == false, offset is constant.
110 2) base has rtx_varies_p == false, offset variable.
111 3) base has rtx_varies_p == true, offset constant.
112 4) base has rtx_varies_p == true, offset variable.
114 The local passes are able to process all 4 kinds of addresses. The
115 global pass only handles (1).
117 The global problem is formulated as follows:
119 A store, S1, to address A, where A is not relative to the stack
120 frame, can be eliminated if all paths from S1 to the end of the
121 of the function contain another store to A before a read to A.
123 If the address A is relative to the stack frame, a store S2 to A
124 can be eliminated if there are no paths from S1 that reach the
125 end of the function that read A before another store to A. In
126 this case S2 can be deleted if there are paths to from S2 to the
127 end of the function that have no reads or writes to A. This
128 second case allows stores to the stack frame to be deleted that
129 would otherwise die when the function returns. This cannot be
130 done if stores_off_frame_dead_at_return is not true. See the doc
131 for that variable for when this variable is false.
133 The global problem is formulated as a backwards set union
134 dataflow problem where the stores are the gens and reads are the
135 kills. Set union problems are rare and require some special
136 handling given our representation of bitmaps. A straightforward
137 implementation of requires a lot of bitmaps filled with 1s.
138 These are expensive and cumbersome in our bitmap formulation so
139 care has been taken to avoid large vectors filled with 1s. See
140 the comments in bb_info and in the dataflow confluence functions
143 There are two places for further enhancements to this algorithm:
145 1) The original dse which was embedded in a pass called flow also
146 did local address forwarding. For example in
151 flow would replace the right hand side of the second insn with a
152 reference to r100. Most of the information is available to add this
153 to this pass. It has not done it because it is a lot of work in
154 the case that either r100 is assigned to between the first and
155 second insn and/or the second insn is a load of part of the value
156 stored by the first insn.
158 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
159 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
160 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
161 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
163 2) The cleaning up of spill code is quite profitable. It currently
164 depends on reading tea leaves and chicken entrails left by reload.
165 This pass depends on reload creating a singleton alias set for each
166 spill slot and telling the next dse pass which of these alias sets
167 are the singletons. Rather than analyze the addresses of the
168 spills, dse's spill processing just does analysis of the loads and
169 stores that use those alias sets. There are three cases where this
172 a) Reload sometimes creates the slot for one mode of access, and
173 then inserts loads and/or stores for a smaller mode. In this
174 case, the current code just punts on the slot. The proper thing
175 to do is to back out and use one bit vector position for each
176 byte of the entity associated with the slot. This depends on
177 KNOWING that reload always generates the accesses for each of the
178 bytes in some canonical (read that easy to understand several
179 passes after reload happens) way.
181 b) Reload sometimes decides that spill slot it allocated was not
182 large enough for the mode and goes back and allocates more slots
183 with the same mode and alias set. The backout in this case is a
184 little more graceful than (a). In this case the slot is unmarked
185 as being a spill slot and if final address comes out to be based
186 off the frame pointer, the global algorithm handles this slot.
188 c) For any pass that may prespill, there is currently no
189 mechanism to tell the dse pass that the slot being used has the
190 special properties that reload uses. It may be that all that is
191 required is to have those passes make the same calls that reload
192 does, assuming that the alias sets can be manipulated in the same
195 /* There are limits to the size of constant offsets we model for the
196 global problem. There are certainly test cases, that exceed this
197 limit, however, it is unlikely that there are important programs
198 that really have constant offsets this size. */
199 #define MAX_OFFSET (64 * 1024)
202 static bitmap scratch = NULL;
205 /* This structure holds information about a candidate store. */
209 /* False means this is a clobber. */
212 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
215 /* The id of the mem group of the base address. If rtx_varies_p is
216 true, this is -1. Otherwise, it is the index into the group
220 /* This is the cselib value. */
221 cselib_val *cse_base;
223 /* This canonized mem. */
226 /* Canonized MEM address for use by canon_true_dependence. */
229 /* If this is non-zero, it is the alias set of a spill location. */
230 alias_set_type alias_set;
232 /* The offset of the first and byte before the last byte associated
233 with the operation. */
234 HOST_WIDE_INT begin, end;
238 /* A bitmask as wide as the number of bytes in the word that
239 contains a 1 if the byte may be needed. The store is unused if
240 all of the bits are 0. This is used if IS_LARGE is false. */
241 unsigned HOST_WIDE_INT small_bitmask;
245 /* A bitmap with one bit per byte. Cleared bit means the position
246 is needed. Used if IS_LARGE is false. */
249 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
250 equal to END - BEGIN, the whole store is unused. */
255 /* The next store info for this insn. */
256 struct store_info *next;
258 /* The right hand side of the store. This is used if there is a
259 subsequent reload of the mems address somewhere later in the
263 /* If rhs is or holds a constant, this contains that constant,
267 /* Set if this store stores the same constant value as REDUNDANT_REASON
268 insn stored. These aren't eliminated early, because doing that
269 might prevent the earlier larger store to be eliminated. */
270 struct insn_info *redundant_reason;
273 /* Return a bitmask with the first N low bits set. */
275 static unsigned HOST_WIDE_INT
276 lowpart_bitmask (int n)
278 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
279 return mask >> (HOST_BITS_PER_WIDE_INT - n);
282 typedef struct store_info *store_info_t;
283 static alloc_pool cse_store_info_pool;
284 static alloc_pool rtx_store_info_pool;
286 /* This structure holds information about a load. These are only
287 built for rtx bases. */
290 /* The id of the mem group of the base address. */
293 /* If this is non-zero, it is the alias set of a spill location. */
294 alias_set_type alias_set;
296 /* The offset of the first and byte after the last byte associated
297 with the operation. If begin == end == 0, the read did not have
298 a constant offset. */
301 /* The mem being read. */
304 /* The next read_info for this insn. */
305 struct read_info *next;
307 typedef struct read_info *read_info_t;
308 static alloc_pool read_info_pool;
311 /* One of these records is created for each insn. */
315 /* Set true if the insn contains a store but the insn itself cannot
316 be deleted. This is set if the insn is a parallel and there is
317 more than one non dead output or if the insn is in some way
321 /* This field is only used by the global algorithm. It is set true
322 if the insn contains any read of mem except for a (1). This is
323 also set if the insn is a call or has a clobber mem. If the insn
324 contains a wild read, the use_rec will be null. */
327 /* This field is only used for the processing of const functions.
328 These functions cannot read memory, but they can read the stack
329 because that is where they may get their parms. We need to be
330 this conservative because, like the store motion pass, we don't
331 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
332 Moreover, we need to distinguish two cases:
333 1. Before reload (register elimination), the stores related to
334 outgoing arguments are stack pointer based and thus deemed
335 of non-constant base in this pass. This requires special
336 handling but also means that the frame pointer based stores
337 need not be killed upon encountering a const function call.
338 2. After reload, the stores related to outgoing arguments can be
339 either stack pointer or hard frame pointer based. This means
340 that we have no other choice than also killing all the frame
341 pointer based stores upon encountering a const function call.
342 This field is set after reload for const function calls. Having
343 this set is less severe than a wild read, it just means that all
344 the frame related stores are killed rather than all the stores. */
347 /* This field is only used for the processing of const functions.
348 It is set if the insn may contain a stack pointer based store. */
349 bool stack_pointer_based;
351 /* This is true if any of the sets within the store contains a
352 cselib base. Such stores can only be deleted by the local
354 bool contains_cselib_groups;
359 /* The list of mem sets or mem clobbers that are contained in this
360 insn. If the insn is deletable, it contains only one mem set.
361 But it could also contain clobbers. Insns that contain more than
362 one mem set are not deletable, but each of those mems are here in
363 order to provide info to delete other insns. */
364 store_info_t store_rec;
366 /* The linked list of mem uses in this insn. Only the reads from
367 rtx bases are listed here. The reads to cselib bases are
368 completely processed during the first scan and so are never
370 read_info_t read_rec;
372 /* The prev insn in the basic block. */
373 struct insn_info * prev_insn;
375 /* The linked list of insns that are in consideration for removal in
376 the forwards pass thru the basic block. This pointer may be
377 trash as it is not cleared when a wild read occurs. The only
378 time it is guaranteed to be correct is when the traversal starts
379 at active_local_stores. */
380 struct insn_info * next_local_store;
383 typedef struct insn_info *insn_info_t;
384 static alloc_pool insn_info_pool;
386 /* The linked list of stores that are under consideration in this
388 static insn_info_t active_local_stores;
393 /* Pointer to the insn info for the last insn in the block. These
394 are linked so this is how all of the insns are reached. During
395 scanning this is the current insn being scanned. */
396 insn_info_t last_insn;
398 /* The info for the global dataflow problem. */
401 /* This is set if the transfer function should and in the wild_read
402 bitmap before applying the kill and gen sets. That vector knocks
403 out most of the bits in the bitmap and thus speeds up the
405 bool apply_wild_read;
407 /* The following 4 bitvectors hold information about which positions
408 of which stores are live or dead. They are indexed by
411 /* The set of store positions that exist in this block before a wild read. */
414 /* The set of load positions that exist in this block above the
415 same position of a store. */
418 /* The set of stores that reach the top of the block without being
421 Do not represent the in if it is all ones. Note that this is
422 what the bitvector should logically be initialized to for a set
423 intersection problem. However, like the kill set, this is too
424 expensive. So initially, the in set will only be created for the
425 exit block and any block that contains a wild read. */
428 /* The set of stores that reach the bottom of the block from it's
431 Do not represent the in if it is all ones. Note that this is
432 what the bitvector should logically be initialized to for a set
433 intersection problem. However, like the kill and in set, this is
434 too expensive. So what is done is that the confluence operator
435 just initializes the vector from one of the out sets of the
436 successors of the block. */
439 /* The following bitvector is indexed by the reg number. It
440 contains the set of regs that are live at the current instruction
441 being processed. While it contains info for all of the
442 registers, only the pseudos are actually examined. It is used to
443 assure that shift sequences that are inserted do not accidently
444 clobber live hard regs. */
448 typedef struct bb_info *bb_info_t;
449 static alloc_pool bb_info_pool;
451 /* Table to hold all bb_infos. */
452 static bb_info_t *bb_table;
454 /* There is a group_info for each rtx base that is used to reference
455 memory. There are also not many of the rtx bases because they are
456 very limited in scope. */
460 /* The actual base of the address. */
463 /* The sequential id of the base. This allows us to have a
464 canonical ordering of these that is not based on addresses. */
467 /* True if there are any positions that are to be processed
469 bool process_globally;
471 /* True if the base of this group is either the frame_pointer or
472 hard_frame_pointer. */
475 /* A mem wrapped around the base pointer for the group in order to
476 do read dependency. */
479 /* Canonized version of base_mem's address. */
482 /* These two sets of two bitmaps are used to keep track of how many
483 stores are actually referencing that position from this base. We
484 only do this for rtx bases as this will be used to assign
485 positions in the bitmaps for the global problem. Bit N is set in
486 store1 on the first store for offset N. Bit N is set in store2
487 for the second store to offset N. This is all we need since we
488 only care about offsets that have two or more stores for them.
490 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
491 for 0 and greater offsets.
493 There is one special case here, for stores into the stack frame,
494 we will or store1 into store2 before deciding which stores look
495 at globally. This is because stores to the stack frame that have
496 no other reads before the end of the function can also be
498 bitmap store1_n, store1_p, store2_n, store2_p;
500 /* The positions in this bitmap have the same assignments as the in,
501 out, gen and kill bitmaps. This bitmap is all zeros except for
502 the positions that are occupied by stores for this group. */
505 /* The offset_map is used to map the offsets from this base into
506 positions in the global bitmaps. It is only created after all of
507 the all of stores have been scanned and we know which ones we
509 int *offset_map_n, *offset_map_p;
510 int offset_map_size_n, offset_map_size_p;
512 typedef struct group_info *group_info_t;
513 typedef const struct group_info *const_group_info_t;
514 static alloc_pool rtx_group_info_pool;
516 /* Tables of group_info structures, hashed by base value. */
517 static htab_t rtx_group_table;
519 /* Index into the rtx_group_vec. */
520 static int rtx_group_next_id;
522 DEF_VEC_P(group_info_t);
523 DEF_VEC_ALLOC_P(group_info_t,heap);
525 static VEC(group_info_t,heap) *rtx_group_vec;
528 /* This structure holds the set of changes that are being deferred
529 when removing read operation. See replace_read. */
530 struct deferred_change
533 /* The mem that is being replaced. */
536 /* The reg it is being replaced with. */
539 struct deferred_change *next;
542 typedef struct deferred_change *deferred_change_t;
543 static alloc_pool deferred_change_pool;
545 static deferred_change_t deferred_change_list = NULL;
547 /* This are used to hold the alias sets of spill variables. Since
548 these are never aliased and there may be a lot of them, it makes
549 sense to treat them specially. This bitvector is only allocated in
550 calls from dse_record_singleton_alias_set which currently is only
551 made during reload1. So when dse is called before reload this
552 mechanism does nothing. */
554 static bitmap clear_alias_sets = NULL;
556 /* The set of clear_alias_sets that have been disqualified because
557 there are loads or stores using a different mode than the alias set
558 was registered with. */
559 static bitmap disqualified_clear_alias_sets = NULL;
561 /* The group that holds all of the clear_alias_sets. */
562 static group_info_t clear_alias_group;
564 /* The modes of the clear_alias_sets. */
565 static htab_t clear_alias_mode_table;
567 /* Hash table element to look up the mode for an alias set. */
568 struct clear_alias_mode_holder
570 alias_set_type alias_set;
571 enum machine_mode mode;
574 static alloc_pool clear_alias_mode_pool;
576 /* This is true except if cfun->stdarg -- i.e. we cannot do
577 this for vararg functions because they play games with the frame. */
578 static bool stores_off_frame_dead_at_return;
580 /* Counter for stats. */
581 static int globally_deleted;
582 static int locally_deleted;
583 static int spill_deleted;
585 static bitmap all_blocks;
587 /* The number of bits used in the global bitmaps. */
588 static unsigned int current_position;
591 static bool gate_dse (void);
592 static bool gate_dse1 (void);
593 static bool gate_dse2 (void);
596 /*----------------------------------------------------------------------------
600 ----------------------------------------------------------------------------*/
602 /* Hashtable callbacks for maintaining the "bases" field of
603 store_group_info, given that the addresses are function invariants. */
606 clear_alias_mode_eq (const void *p1, const void *p2)
608 const struct clear_alias_mode_holder * h1
609 = (const struct clear_alias_mode_holder *) p1;
610 const struct clear_alias_mode_holder * h2
611 = (const struct clear_alias_mode_holder *) p2;
612 return h1->alias_set == h2->alias_set;
617 clear_alias_mode_hash (const void *p)
619 const struct clear_alias_mode_holder *holder
620 = (const struct clear_alias_mode_holder *) p;
621 return holder->alias_set;
625 /* Find the entry associated with ALIAS_SET. */
627 static struct clear_alias_mode_holder *
628 clear_alias_set_lookup (alias_set_type alias_set)
630 struct clear_alias_mode_holder tmp_holder;
633 tmp_holder.alias_set = alias_set;
634 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
637 return (struct clear_alias_mode_holder *) *slot;
641 /* Hashtable callbacks for maintaining the "bases" field of
642 store_group_info, given that the addresses are function invariants. */
645 invariant_group_base_eq (const void *p1, const void *p2)
647 const_group_info_t gi1 = (const_group_info_t) p1;
648 const_group_info_t gi2 = (const_group_info_t) p2;
649 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
654 invariant_group_base_hash (const void *p)
656 const_group_info_t gi = (const_group_info_t) p;
658 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
662 /* Get the GROUP for BASE. Add a new group if it is not there. */
665 get_group_info (rtx base)
667 struct group_info tmp_gi;
673 /* Find the store_base_info structure for BASE, creating a new one
675 tmp_gi.rtx_base = base;
676 slot = htab_find_slot (rtx_group_table, &tmp_gi, INSERT);
677 gi = (group_info_t) *slot;
681 if (!clear_alias_group)
683 clear_alias_group = gi =
684 (group_info_t) pool_alloc (rtx_group_info_pool);
685 memset (gi, 0, sizeof (struct group_info));
686 gi->id = rtx_group_next_id++;
687 gi->store1_n = BITMAP_ALLOC (NULL);
688 gi->store1_p = BITMAP_ALLOC (NULL);
689 gi->store2_n = BITMAP_ALLOC (NULL);
690 gi->store2_p = BITMAP_ALLOC (NULL);
691 gi->group_kill = BITMAP_ALLOC (NULL);
692 gi->process_globally = false;
693 gi->offset_map_size_n = 0;
694 gi->offset_map_size_p = 0;
695 gi->offset_map_n = NULL;
696 gi->offset_map_p = NULL;
697 VEC_safe_push (group_info_t, heap, rtx_group_vec, gi);
699 return clear_alias_group;
704 *slot = gi = (group_info_t) pool_alloc (rtx_group_info_pool);
706 gi->id = rtx_group_next_id++;
707 gi->base_mem = gen_rtx_MEM (QImode, base);
708 gi->canon_base_addr = canon_rtx (base);
709 gi->store1_n = BITMAP_ALLOC (NULL);
710 gi->store1_p = BITMAP_ALLOC (NULL);
711 gi->store2_n = BITMAP_ALLOC (NULL);
712 gi->store2_p = BITMAP_ALLOC (NULL);
713 gi->group_kill = BITMAP_ALLOC (NULL);
714 gi->process_globally = false;
716 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
717 gi->offset_map_size_n = 0;
718 gi->offset_map_size_p = 0;
719 gi->offset_map_n = NULL;
720 gi->offset_map_p = NULL;
721 VEC_safe_push (group_info_t, heap, rtx_group_vec, gi);
728 /* Initialization of data structures. */
734 globally_deleted = 0;
737 scratch = BITMAP_ALLOC (NULL);
740 = create_alloc_pool ("rtx_store_info_pool",
741 sizeof (struct store_info), 100);
743 = create_alloc_pool ("read_info_pool",
744 sizeof (struct read_info), 100);
746 = create_alloc_pool ("insn_info_pool",
747 sizeof (struct insn_info), 100);
749 = create_alloc_pool ("bb_info_pool",
750 sizeof (struct bb_info), 100);
752 = create_alloc_pool ("rtx_group_info_pool",
753 sizeof (struct group_info), 100);
755 = create_alloc_pool ("deferred_change_pool",
756 sizeof (struct deferred_change), 10);
758 rtx_group_table = htab_create (11, invariant_group_base_hash,
759 invariant_group_base_eq, NULL);
761 bb_table = XCNEWVEC (bb_info_t, last_basic_block);
762 rtx_group_next_id = 0;
764 stores_off_frame_dead_at_return = !cfun->stdarg;
766 init_alias_analysis ();
768 if (clear_alias_sets)
769 clear_alias_group = get_group_info (NULL);
771 clear_alias_group = NULL;
776 /*----------------------------------------------------------------------------
779 Scan all of the insns. Any random ordering of the blocks is fine.
780 Each block is scanned in forward order to accommodate cselib which
781 is used to remove stores with non-constant bases.
782 ----------------------------------------------------------------------------*/
784 /* Delete all of the store_info recs from INSN_INFO. */
787 free_store_info (insn_info_t insn_info)
789 store_info_t store_info = insn_info->store_rec;
792 store_info_t next = store_info->next;
793 if (store_info->is_large)
794 BITMAP_FREE (store_info->positions_needed.large.bitmap);
795 if (store_info->cse_base)
796 pool_free (cse_store_info_pool, store_info);
798 pool_free (rtx_store_info_pool, store_info);
802 insn_info->cannot_delete = true;
803 insn_info->contains_cselib_groups = false;
804 insn_info->store_rec = NULL;
814 /* Add an insn to do the add inside a x if it is a
815 PRE/POST-INC/DEC/MODIFY. D is an structure containing the insn and
816 the size of the mode of the MEM that this is inside of. */
819 replace_inc_dec (rtx *r, void *d)
822 struct insn_size *data = (struct insn_size *)d;
823 switch (GET_CODE (x))
828 rtx r1 = XEXP (x, 0);
829 rtx c = gen_int_mode (data->size, Pmode);
830 emit_insn_before (gen_rtx_SET (Pmode, r1,
831 gen_rtx_PLUS (Pmode, r1, c)),
839 rtx r1 = XEXP (x, 0);
840 rtx c = gen_int_mode (-data->size, Pmode);
841 emit_insn_before (gen_rtx_SET (Pmode, r1,
842 gen_rtx_PLUS (Pmode, r1, c)),
850 /* We can reuse the add because we are about to delete the
851 insn that contained it. */
852 rtx add = XEXP (x, 0);
853 rtx r1 = XEXP (add, 0);
854 emit_insn_before (gen_rtx_SET (Pmode, r1, add), data->insn);
864 /* If X is a MEM, check the address to see if it is PRE/POST-INC/DEC/MODIFY
865 and generate an add to replace that. */
868 replace_inc_dec_mem (rtx *r, void *d)
871 if (x != NULL_RTX && MEM_P (x))
873 struct insn_size data;
875 data.size = GET_MODE_SIZE (GET_MODE (x));
878 for_each_rtx (&XEXP (x, 0), replace_inc_dec, &data);
885 /* Before we delete INSN, make sure that the auto inc/dec, if it is
886 there, is split into a separate insn. */
889 check_for_inc_dec (rtx insn)
891 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
893 for_each_rtx (&insn, replace_inc_dec_mem, insn);
897 /* Delete the insn and free all of the fields inside INSN_INFO. */
900 delete_dead_store_insn (insn_info_t insn_info)
902 read_info_t read_info;
907 check_for_inc_dec (insn_info->insn);
910 fprintf (dump_file, "Locally deleting insn %d ",
911 INSN_UID (insn_info->insn));
912 if (insn_info->store_rec->alias_set)
913 fprintf (dump_file, "alias set %d\n",
914 (int) insn_info->store_rec->alias_set);
916 fprintf (dump_file, "\n");
919 free_store_info (insn_info);
920 read_info = insn_info->read_rec;
924 read_info_t next = read_info->next;
925 pool_free (read_info_pool, read_info);
928 insn_info->read_rec = NULL;
930 delete_insn (insn_info->insn);
932 insn_info->insn = NULL;
934 insn_info->wild_read = false;
938 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
942 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width)
946 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
947 for (i=offset; i<offset+width; i++)
954 store1 = group->store1_n;
955 store2 = group->store2_n;
960 store1 = group->store1_p;
961 store2 = group->store2_p;
965 if (bitmap_bit_p (store1, ai))
966 bitmap_set_bit (store2, ai);
969 bitmap_set_bit (store1, ai);
972 if (group->offset_map_size_n < ai)
973 group->offset_map_size_n = ai;
977 if (group->offset_map_size_p < ai)
978 group->offset_map_size_p = ai;
985 /* Set the BB_INFO so that the last insn is marked as a wild read. */
988 add_wild_read (bb_info_t bb_info)
990 insn_info_t insn_info = bb_info->last_insn;
991 read_info_t *ptr = &insn_info->read_rec;
995 read_info_t next = (*ptr)->next;
996 if ((*ptr)->alias_set == 0)
998 pool_free (read_info_pool, *ptr);
1002 ptr = &(*ptr)->next;
1004 insn_info->wild_read = true;
1005 active_local_stores = NULL;
1009 /* Return true if X is a constant or one of the registers that behave
1010 as a constant over the life of a function. This is equivalent to
1011 !rtx_varies_p for memory addresses. */
1014 const_or_frame_p (rtx x)
1016 switch (GET_CODE (x))
1027 /* Note that we have to test for the actual rtx used for the frame
1028 and arg pointers and not just the register number in case we have
1029 eliminated the frame and/or arg pointer and are using it
1031 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1032 /* The arg pointer varies if it is not a fixed register. */
1033 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1034 || x == pic_offset_table_rtx)
1043 /* Take all reasonable action to put the address of MEM into the form
1044 that we can do analysis on.
1046 The gold standard is to get the address into the form: address +
1047 OFFSET where address is something that rtx_varies_p considers a
1048 constant. When we can get the address in this form, we can do
1049 global analysis on it. Note that for constant bases, address is
1050 not actually returned, only the group_id. The address can be
1053 If that fails, we try cselib to get a value we can at least use
1054 locally. If that fails we return false.
1056 The GROUP_ID is set to -1 for cselib bases and the index of the
1057 group for non_varying bases.
1059 FOR_READ is true if this is a mem read and false if not. */
1062 canon_address (rtx mem,
1063 alias_set_type *alias_set_out,
1065 HOST_WIDE_INT *offset,
1068 rtx mem_address = XEXP (mem, 0);
1069 rtx expanded_address, address;
1072 /* Make sure that cselib is has initialized all of the operands of
1073 the address before asking it to do the subst. */
1075 if (clear_alias_sets)
1077 /* If this is a spill, do not do any further processing. */
1078 alias_set_type alias_set = MEM_ALIAS_SET (mem);
1080 fprintf (dump_file, "found alias set %d\n", (int) alias_set);
1081 if (bitmap_bit_p (clear_alias_sets, alias_set))
1083 struct clear_alias_mode_holder *entry
1084 = clear_alias_set_lookup (alias_set);
1086 /* If the modes do not match, we cannot process this set. */
1087 if (entry->mode != GET_MODE (mem))
1091 "disqualifying alias set %d, (%s) != (%s)\n",
1092 (int) alias_set, GET_MODE_NAME (entry->mode),
1093 GET_MODE_NAME (GET_MODE (mem)));
1095 bitmap_set_bit (disqualified_clear_alias_sets, alias_set);
1099 *alias_set_out = alias_set;
1100 *group_id = clear_alias_group->id;
1107 cselib_lookup (mem_address, Pmode, 1);
1111 fprintf (dump_file, " mem: ");
1112 print_inline_rtx (dump_file, mem_address, 0);
1113 fprintf (dump_file, "\n");
1116 /* First see if just canon_rtx (mem_address) is const or frame,
1117 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1119 for (expanded = 0; expanded < 2; expanded++)
1123 /* Use cselib to replace all of the reg references with the full
1124 expression. This will take care of the case where we have
1126 r_x = base + offset;
1131 val = *(base + offset); */
1133 expanded_address = cselib_expand_value_rtx (mem_address,
1136 /* If this fails, just go with the address from first
1138 if (!expanded_address)
1142 expanded_address = mem_address;
1144 /* Split the address into canonical BASE + OFFSET terms. */
1145 address = canon_rtx (expanded_address);
1153 fprintf (dump_file, "\n after cselib_expand address: ");
1154 print_inline_rtx (dump_file, expanded_address, 0);
1155 fprintf (dump_file, "\n");
1158 fprintf (dump_file, "\n after canon_rtx address: ");
1159 print_inline_rtx (dump_file, address, 0);
1160 fprintf (dump_file, "\n");
1163 if (GET_CODE (address) == CONST)
1164 address = XEXP (address, 0);
1166 if (GET_CODE (address) == PLUS
1167 && GET_CODE (XEXP (address, 1)) == CONST_INT)
1169 *offset = INTVAL (XEXP (address, 1));
1170 address = XEXP (address, 0);
1173 if (const_or_frame_p (address))
1175 group_info_t group = get_group_info (address);
1178 fprintf (dump_file, " gid=%d offset=%d \n",
1179 group->id, (int)*offset);
1181 *group_id = group->id;
1186 *base = cselib_lookup (address, Pmode, true);
1192 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1196 fprintf (dump_file, " varying cselib base=%d offset = %d\n",
1197 (*base)->value, (int)*offset);
1202 /* Clear the rhs field from the active_local_stores array. */
1205 clear_rhs_from_active_local_stores (void)
1207 insn_info_t ptr = active_local_stores;
1211 store_info_t store_info = ptr->store_rec;
1212 /* Skip the clobbers. */
1213 while (!store_info->is_set)
1214 store_info = store_info->next;
1216 store_info->rhs = NULL;
1217 store_info->const_rhs = NULL;
1219 ptr = ptr->next_local_store;
1224 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1227 set_position_unneeded (store_info_t s_info, int pos)
1229 if (__builtin_expect (s_info->is_large, false))
1231 if (!bitmap_bit_p (s_info->positions_needed.large.bitmap, pos))
1233 s_info->positions_needed.large.count++;
1234 bitmap_set_bit (s_info->positions_needed.large.bitmap, pos);
1238 s_info->positions_needed.small_bitmask
1239 &= ~(((unsigned HOST_WIDE_INT) 1) << pos);
1242 /* Mark the whole store S_INFO as unneeded. */
1245 set_all_positions_unneeded (store_info_t s_info)
1247 if (__builtin_expect (s_info->is_large, false))
1249 int pos, end = s_info->end - s_info->begin;
1250 for (pos = 0; pos < end; pos++)
1251 bitmap_set_bit (s_info->positions_needed.large.bitmap, pos);
1252 s_info->positions_needed.large.count = end;
1255 s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0;
1258 /* Return TRUE if any bytes from S_INFO store are needed. */
1261 any_positions_needed_p (store_info_t s_info)
1263 if (__builtin_expect (s_info->is_large, false))
1264 return (s_info->positions_needed.large.count
1265 < s_info->end - s_info->begin);
1267 return (s_info->positions_needed.small_bitmask
1268 != (unsigned HOST_WIDE_INT) 0);
1271 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1272 store are needed. */
1275 all_positions_needed_p (store_info_t s_info, int start, int width)
1277 if (__builtin_expect (s_info->is_large, false))
1279 int end = start + width;
1281 if (bitmap_bit_p (s_info->positions_needed.large.bitmap, start++))
1287 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1288 return (s_info->positions_needed.small_bitmask & mask) == mask;
1293 static rtx get_stored_val (store_info_t, enum machine_mode, HOST_WIDE_INT,
1294 HOST_WIDE_INT, basic_block, bool);
1297 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1298 there is a candidate store, after adding it to the appropriate
1299 local store group if so. */
1302 record_store (rtx body, bb_info_t bb_info)
1304 rtx mem, rhs, const_rhs, mem_addr;
1305 HOST_WIDE_INT offset = 0;
1306 HOST_WIDE_INT width = 0;
1307 alias_set_type spill_alias_set;
1308 insn_info_t insn_info = bb_info->last_insn;
1309 store_info_t store_info = NULL;
1311 cselib_val *base = NULL;
1312 insn_info_t ptr, last, redundant_reason;
1313 bool store_is_unused;
1315 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1318 mem = SET_DEST (body);
1320 /* If this is not used, then this cannot be used to keep the insn
1321 from being deleted. On the other hand, it does provide something
1322 that can be used to prove that another store is dead. */
1324 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1326 /* Check whether that value is a suitable memory location. */
1329 /* If the set or clobber is unused, then it does not effect our
1330 ability to get rid of the entire insn. */
1331 if (!store_is_unused)
1332 insn_info->cannot_delete = true;
1336 /* At this point we know mem is a mem. */
1337 if (GET_MODE (mem) == BLKmode)
1339 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1342 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1343 add_wild_read (bb_info);
1344 insn_info->cannot_delete = true;
1347 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1348 as memset (addr, 0, 36); */
1349 else if (!MEM_SIZE (mem)
1350 || !CONST_INT_P (MEM_SIZE (mem))
1351 || GET_CODE (body) != SET
1352 || INTVAL (MEM_SIZE (mem)) <= 0
1353 || INTVAL (MEM_SIZE (mem)) > MAX_OFFSET
1354 || !CONST_INT_P (SET_SRC (body)))
1356 if (!store_is_unused)
1358 /* If the set or clobber is unused, then it does not effect our
1359 ability to get rid of the entire insn. */
1360 insn_info->cannot_delete = true;
1361 clear_rhs_from_active_local_stores ();
1367 /* We can still process a volatile mem, we just cannot delete it. */
1368 if (MEM_VOLATILE_P (mem))
1369 insn_info->cannot_delete = true;
1371 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1373 clear_rhs_from_active_local_stores ();
1377 if (GET_MODE (mem) == BLKmode)
1378 width = INTVAL (MEM_SIZE (mem));
1381 width = GET_MODE_SIZE (GET_MODE (mem));
1382 gcc_assert ((unsigned) width <= HOST_BITS_PER_WIDE_INT);
1385 if (spill_alias_set)
1387 bitmap store1 = clear_alias_group->store1_p;
1388 bitmap store2 = clear_alias_group->store2_p;
1390 gcc_assert (GET_MODE (mem) != BLKmode);
1392 if (bitmap_bit_p (store1, spill_alias_set))
1393 bitmap_set_bit (store2, spill_alias_set);
1395 bitmap_set_bit (store1, spill_alias_set);
1397 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1398 clear_alias_group->offset_map_size_p = spill_alias_set;
1400 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1403 fprintf (dump_file, " processing spill store %d(%s)\n",
1404 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1406 else if (group_id >= 0)
1408 /* In the restrictive case where the base is a constant or the
1409 frame pointer we can do global analysis. */
1412 = VEC_index (group_info_t, rtx_group_vec, group_id);
1414 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1415 set_usage_bits (group, offset, width);
1418 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1419 group_id, (int)offset, (int)(offset+width));
1423 rtx base_term = find_base_term (XEXP (mem, 0));
1425 || (GET_CODE (base_term) == ADDRESS
1426 && GET_MODE (base_term) == Pmode
1427 && XEXP (base_term, 0) == stack_pointer_rtx))
1428 insn_info->stack_pointer_based = true;
1429 insn_info->contains_cselib_groups = true;
1431 store_info = (store_info_t) pool_alloc (cse_store_info_pool);
1435 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1436 (int)offset, (int)(offset+width));
1439 const_rhs = rhs = NULL_RTX;
1440 if (GET_CODE (body) == SET
1441 /* No place to keep the value after ra. */
1442 && !reload_completed
1443 && (REG_P (SET_SRC (body))
1444 || GET_CODE (SET_SRC (body)) == SUBREG
1445 || CONSTANT_P (SET_SRC (body)))
1446 && !MEM_VOLATILE_P (mem)
1447 /* Sometimes the store and reload is used for truncation and
1449 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1451 rhs = SET_SRC (body);
1452 if (CONSTANT_P (rhs))
1454 else if (body == PATTERN (insn_info->insn))
1456 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1457 if (tem && CONSTANT_P (XEXP (tem, 0)))
1458 const_rhs = XEXP (tem, 0);
1460 if (const_rhs == NULL_RTX && REG_P (rhs))
1462 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1464 if (tem && CONSTANT_P (tem))
1469 /* Check to see if this stores causes some other stores to be
1471 ptr = active_local_stores;
1473 redundant_reason = NULL;
1474 mem = canon_rtx (mem);
1475 /* For alias_set != 0 canon_true_dependence should be never called. */
1476 if (spill_alias_set)
1477 mem_addr = NULL_RTX;
1481 mem_addr = base->val_rtx;
1485 = VEC_index (group_info_t, rtx_group_vec, group_id);
1486 mem_addr = group->canon_base_addr;
1489 mem_addr = plus_constant (mem_addr, offset);
1494 insn_info_t next = ptr->next_local_store;
1495 store_info_t s_info = ptr->store_rec;
1498 /* Skip the clobbers. We delete the active insn if this insn
1499 shadows the set. To have been put on the active list, it
1500 has exactly on set. */
1501 while (!s_info->is_set)
1502 s_info = s_info->next;
1504 if (s_info->alias_set != spill_alias_set)
1506 else if (s_info->alias_set)
1508 struct clear_alias_mode_holder *entry
1509 = clear_alias_set_lookup (s_info->alias_set);
1510 /* Generally, spills cannot be processed if and of the
1511 references to the slot have a different mode. But if
1512 we are in the same block and mode is exactly the same
1513 between this store and one before in the same block,
1514 we can still delete it. */
1515 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1516 && (GET_MODE (mem) == entry->mode))
1519 set_all_positions_unneeded (s_info);
1522 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1523 INSN_UID (ptr->insn), (int) s_info->alias_set);
1525 else if ((s_info->group_id == group_id)
1526 && (s_info->cse_base == base))
1530 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1531 INSN_UID (ptr->insn), s_info->group_id,
1532 (int)s_info->begin, (int)s_info->end);
1534 /* Even if PTR won't be eliminated as unneeded, if both
1535 PTR and this insn store the same constant value, we might
1536 eliminate this insn instead. */
1537 if (s_info->const_rhs
1539 && offset >= s_info->begin
1540 && offset + width <= s_info->end
1541 && all_positions_needed_p (s_info, offset - s_info->begin,
1544 if (GET_MODE (mem) == BLKmode)
1546 if (GET_MODE (s_info->mem) == BLKmode
1547 && s_info->const_rhs == const_rhs)
1548 redundant_reason = ptr;
1550 else if (s_info->const_rhs == const0_rtx
1551 && const_rhs == const0_rtx)
1552 redundant_reason = ptr;
1557 val = get_stored_val (s_info, GET_MODE (mem),
1558 offset, offset + width,
1559 BLOCK_FOR_INSN (insn_info->insn),
1561 if (get_insns () != NULL)
1564 if (val && rtx_equal_p (val, const_rhs))
1565 redundant_reason = ptr;
1569 for (i = MAX (offset, s_info->begin);
1570 i < offset + width && i < s_info->end;
1572 set_position_unneeded (s_info, i - s_info->begin);
1574 else if (s_info->rhs)
1575 /* Need to see if it is possible for this store to overwrite
1576 the value of store_info. If it is, set the rhs to NULL to
1577 keep it from being used to remove a load. */
1579 if (canon_true_dependence (s_info->mem,
1580 GET_MODE (s_info->mem),
1582 mem, mem_addr, rtx_varies_p))
1585 s_info->const_rhs = NULL;
1589 /* An insn can be deleted if every position of every one of
1590 its s_infos is zero. */
1591 if (any_positions_needed_p (s_info))
1596 insn_info_t insn_to_delete = ptr;
1599 last->next_local_store = ptr->next_local_store;
1601 active_local_stores = ptr->next_local_store;
1603 if (!insn_to_delete->cannot_delete)
1604 delete_dead_store_insn (insn_to_delete);
1612 /* Finish filling in the store_info. */
1613 store_info->next = insn_info->store_rec;
1614 insn_info->store_rec = store_info;
1615 store_info->mem = mem;
1616 store_info->alias_set = spill_alias_set;
1617 store_info->mem_addr = mem_addr;
1618 store_info->cse_base = base;
1619 if (width > HOST_BITS_PER_WIDE_INT)
1621 store_info->is_large = true;
1622 store_info->positions_needed.large.count = 0;
1623 store_info->positions_needed.large.bitmap = BITMAP_ALLOC (NULL);
1627 store_info->is_large = false;
1628 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1630 store_info->group_id = group_id;
1631 store_info->begin = offset;
1632 store_info->end = offset + width;
1633 store_info->is_set = GET_CODE (body) == SET;
1634 store_info->rhs = rhs;
1635 store_info->const_rhs = const_rhs;
1636 store_info->redundant_reason = redundant_reason;
1638 /* If this is a clobber, we return 0. We will only be able to
1639 delete this insn if there is only one store USED store, but we
1640 can use the clobber to delete other stores earlier. */
1641 return store_info->is_set ? 1 : 0;
1646 dump_insn_info (const char * start, insn_info_t insn_info)
1648 fprintf (dump_file, "%s insn=%d %s\n", start,
1649 INSN_UID (insn_info->insn),
1650 insn_info->store_rec ? "has store" : "naked");
1654 /* If the modes are different and the value's source and target do not
1655 line up, we need to extract the value from lower part of the rhs of
1656 the store, shift it, and then put it into a form that can be shoved
1657 into the read_insn. This function generates a right SHIFT of a
1658 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1659 shift sequence is returned or NULL if we failed to find a
1663 find_shift_sequence (int access_size,
1664 store_info_t store_info,
1665 enum machine_mode read_mode,
1666 int shift, bool speed, bool require_cst)
1668 enum machine_mode store_mode = GET_MODE (store_info->mem);
1669 enum machine_mode new_mode;
1670 rtx read_reg = NULL;
1672 /* Some machines like the x86 have shift insns for each size of
1673 operand. Other machines like the ppc or the ia-64 may only have
1674 shift insns that shift values within 32 or 64 bit registers.
1675 This loop tries to find the smallest shift insn that will right
1676 justify the value we want to read but is available in one insn on
1679 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1681 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1682 new_mode = GET_MODE_WIDER_MODE (new_mode))
1684 rtx target, new_reg, shift_seq, insn, new_lhs;
1687 /* If a constant was stored into memory, try to simplify it here,
1688 otherwise the cost of the shift might preclude this optimization
1689 e.g. at -Os, even when no actual shift will be needed. */
1690 if (store_info->const_rhs)
1692 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1693 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1695 if (ret && CONSTANT_P (ret))
1697 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1698 ret, GEN_INT (shift));
1699 if (ret && CONSTANT_P (ret))
1701 byte = subreg_lowpart_offset (read_mode, new_mode);
1702 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1703 if (ret && CONSTANT_P (ret)
1704 && rtx_cost (ret, SET, speed) <= COSTS_N_INSNS (1))
1713 /* Try a wider mode if truncating the store mode to NEW_MODE
1714 requires a real instruction. */
1715 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1716 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (new_mode),
1717 GET_MODE_BITSIZE (store_mode)))
1720 /* Also try a wider mode if the necessary punning is either not
1721 desirable or not possible. */
1722 if (!CONSTANT_P (store_info->rhs)
1723 && !MODES_TIEABLE_P (new_mode, store_mode))
1726 new_reg = gen_reg_rtx (new_mode);
1730 /* In theory we could also check for an ashr. Ian Taylor knows
1731 of one dsp where the cost of these two was not the same. But
1732 this really is a rare case anyway. */
1733 target = expand_binop (new_mode, lshr_optab, new_reg,
1734 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1736 shift_seq = get_insns ();
1739 if (target != new_reg || shift_seq == NULL)
1743 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1745 cost += insn_rtx_cost (PATTERN (insn), speed);
1747 /* The computation up to here is essentially independent
1748 of the arguments and could be precomputed. It may
1749 not be worth doing so. We could precompute if
1750 worthwhile or at least cache the results. The result
1751 technically depends on both SHIFT and ACCESS_SIZE,
1752 but in practice the answer will depend only on ACCESS_SIZE. */
1754 if (cost > COSTS_N_INSNS (1))
1757 new_lhs = extract_low_bits (new_mode, store_mode,
1758 copy_rtx (store_info->rhs));
1759 if (new_lhs == NULL_RTX)
1762 /* We found an acceptable shift. Generate a move to
1763 take the value from the store and put it into the
1764 shift pseudo, then shift it, then generate another
1765 move to put in into the target of the read. */
1766 emit_move_insn (new_reg, new_lhs);
1767 emit_insn (shift_seq);
1768 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1776 /* Call back for note_stores to find the hard regs set or clobbered by
1777 insn. Data is a bitmap of the hardregs set so far. */
1780 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1782 bitmap regs_set = (bitmap) data;
1785 && REGNO (x) < FIRST_PSEUDO_REGISTER)
1787 int regno = REGNO (x);
1788 int n = hard_regno_nregs[regno][GET_MODE (x)];
1790 bitmap_set_bit (regs_set, regno + n);
1794 /* Helper function for replace_read and record_store.
1795 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1796 to one before READ_END bytes read in READ_MODE. Return NULL
1797 if not successful. If REQUIRE_CST is true, return always constant. */
1800 get_stored_val (store_info_t store_info, enum machine_mode read_mode,
1801 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1802 basic_block bb, bool require_cst)
1804 enum machine_mode store_mode = GET_MODE (store_info->mem);
1806 int access_size; /* In bytes. */
1809 /* To get here the read is within the boundaries of the write so
1810 shift will never be negative. Start out with the shift being in
1812 if (store_mode == BLKmode)
1814 else if (BYTES_BIG_ENDIAN)
1815 shift = store_info->end - read_end;
1817 shift = read_begin - store_info->begin;
1819 access_size = shift + GET_MODE_SIZE (read_mode);
1821 /* From now on it is bits. */
1822 shift *= BITS_PER_UNIT;
1825 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1826 optimize_bb_for_speed_p (bb),
1828 else if (store_mode == BLKmode)
1830 /* The store is a memset (addr, const_val, const_size). */
1831 gcc_assert (CONST_INT_P (store_info->rhs));
1832 store_mode = int_mode_for_mode (read_mode);
1833 if (store_mode == BLKmode)
1834 read_reg = NULL_RTX;
1835 else if (store_info->rhs == const0_rtx)
1836 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1837 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1838 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1839 read_reg = NULL_RTX;
1842 unsigned HOST_WIDE_INT c
1843 = INTVAL (store_info->rhs)
1844 & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
1845 int shift = BITS_PER_UNIT;
1846 while (shift < HOST_BITS_PER_WIDE_INT)
1851 read_reg = GEN_INT (trunc_int_for_mode (c, store_mode));
1852 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1855 else if (store_info->const_rhs
1857 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1858 read_reg = extract_low_bits (read_mode, store_mode,
1859 copy_rtx (store_info->const_rhs));
1861 read_reg = extract_low_bits (read_mode, store_mode,
1862 copy_rtx (store_info->rhs));
1863 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1864 read_reg = NULL_RTX;
1868 /* Take a sequence of:
1891 Depending on the alignment and the mode of the store and
1895 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1896 and READ_INSN are for the read. Return true if the replacement
1900 replace_read (store_info_t store_info, insn_info_t store_insn,
1901 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1904 enum machine_mode store_mode = GET_MODE (store_info->mem);
1905 enum machine_mode read_mode = GET_MODE (read_info->mem);
1906 rtx insns, this_insn, read_reg;
1912 /* Create a sequence of instructions to set up the read register.
1913 This sequence goes immediately before the store and its result
1914 is read by the load.
1916 We need to keep this in perspective. We are replacing a read
1917 with a sequence of insns, but the read will almost certainly be
1918 in cache, so it is not going to be an expensive one. Thus, we
1919 are not willing to do a multi insn shift or worse a subroutine
1920 call to get rid of the read. */
1922 fprintf (dump_file, "trying to replace %smode load in insn %d"
1923 " from %smode store in insn %d\n",
1924 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1925 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1927 bb = BLOCK_FOR_INSN (read_insn->insn);
1928 read_reg = get_stored_val (store_info,
1929 read_mode, read_info->begin, read_info->end,
1931 if (read_reg == NULL_RTX)
1935 fprintf (dump_file, " -- could not extract bits of stored value\n");
1938 /* Force the value into a new register so that it won't be clobbered
1939 between the store and the load. */
1940 read_reg = copy_to_mode_reg (read_mode, read_reg);
1941 insns = get_insns ();
1944 if (insns != NULL_RTX)
1946 /* Now we have to scan the set of new instructions to see if the
1947 sequence contains and sets of hardregs that happened to be
1948 live at this point. For instance, this can happen if one of
1949 the insns sets the CC and the CC happened to be live at that
1950 point. This does occasionally happen, see PR 37922. */
1951 bitmap regs_set = BITMAP_ALLOC (NULL);
1953 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
1954 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
1956 bitmap_and_into (regs_set, regs_live);
1957 if (!bitmap_empty_p (regs_set))
1962 "abandoning replacement because sequence clobbers live hardregs:");
1963 df_print_regset (dump_file, regs_set);
1966 BITMAP_FREE (regs_set);
1969 BITMAP_FREE (regs_set);
1972 if (validate_change (read_insn->insn, loc, read_reg, 0))
1974 deferred_change_t deferred_change =
1975 (deferred_change_t) pool_alloc (deferred_change_pool);
1977 /* Insert this right before the store insn where it will be safe
1978 from later insns that might change it before the read. */
1979 emit_insn_before (insns, store_insn->insn);
1981 /* And now for the kludge part: cselib croaks if you just
1982 return at this point. There are two reasons for this:
1984 1) Cselib has an idea of how many pseudos there are and
1985 that does not include the new ones we just added.
1987 2) Cselib does not know about the move insn we added
1988 above the store_info, and there is no way to tell it
1989 about it, because it has "moved on".
1991 Problem (1) is fixable with a certain amount of engineering.
1992 Problem (2) is requires starting the bb from scratch. This
1995 So we are just going to have to lie. The move/extraction
1996 insns are not really an issue, cselib did not see them. But
1997 the use of the new pseudo read_insn is a real problem because
1998 cselib has not scanned this insn. The way that we solve this
1999 problem is that we are just going to put the mem back for now
2000 and when we are finished with the block, we undo this. We
2001 keep a table of mems to get rid of. At the end of the basic
2002 block we can put them back. */
2004 *loc = read_info->mem;
2005 deferred_change->next = deferred_change_list;
2006 deferred_change_list = deferred_change;
2007 deferred_change->loc = loc;
2008 deferred_change->reg = read_reg;
2010 /* Get rid of the read_info, from the point of view of the
2011 rest of dse, play like this read never happened. */
2012 read_insn->read_rec = read_info->next;
2013 pool_free (read_info_pool, read_info);
2016 fprintf (dump_file, " -- replaced the loaded MEM with ");
2017 print_simple_rtl (dump_file, read_reg);
2018 fprintf (dump_file, "\n");
2026 fprintf (dump_file, " -- replacing the loaded MEM with ");
2027 print_simple_rtl (dump_file, read_reg);
2028 fprintf (dump_file, " led to an invalid instruction\n");
2034 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2035 if LOC is a mem and if it is look at the address and kill any
2036 appropriate stores that may be active. */
2039 check_mem_read_rtx (rtx *loc, void *data)
2041 rtx mem = *loc, mem_addr;
2043 insn_info_t insn_info;
2044 HOST_WIDE_INT offset = 0;
2045 HOST_WIDE_INT width = 0;
2046 alias_set_type spill_alias_set = 0;
2047 cselib_val *base = NULL;
2049 read_info_t read_info;
2051 if (!mem || !MEM_P (mem))
2054 bb_info = (bb_info_t) data;
2055 insn_info = bb_info->last_insn;
2057 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2058 || (MEM_VOLATILE_P (mem)))
2061 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
2062 add_wild_read (bb_info);
2063 insn_info->cannot_delete = true;
2067 /* If it is reading readonly mem, then there can be no conflict with
2069 if (MEM_READONLY_P (mem))
2072 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
2075 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2076 add_wild_read (bb_info);
2080 if (GET_MODE (mem) == BLKmode)
2083 width = GET_MODE_SIZE (GET_MODE (mem));
2085 read_info = (read_info_t) pool_alloc (read_info_pool);
2086 read_info->group_id = group_id;
2087 read_info->mem = mem;
2088 read_info->alias_set = spill_alias_set;
2089 read_info->begin = offset;
2090 read_info->end = offset + width;
2091 read_info->next = insn_info->read_rec;
2092 insn_info->read_rec = read_info;
2093 /* For alias_set != 0 canon_true_dependence should be never called. */
2094 if (spill_alias_set)
2095 mem_addr = NULL_RTX;
2099 mem_addr = base->val_rtx;
2103 = VEC_index (group_info_t, rtx_group_vec, group_id);
2104 mem_addr = group->canon_base_addr;
2107 mem_addr = plus_constant (mem_addr, offset);
2110 /* We ignore the clobbers in store_info. The is mildly aggressive,
2111 but there really should not be a clobber followed by a read. */
2113 if (spill_alias_set)
2115 insn_info_t i_ptr = active_local_stores;
2116 insn_info_t last = NULL;
2119 fprintf (dump_file, " processing spill load %d\n",
2120 (int) spill_alias_set);
2124 store_info_t store_info = i_ptr->store_rec;
2126 /* Skip the clobbers. */
2127 while (!store_info->is_set)
2128 store_info = store_info->next;
2130 if (store_info->alias_set == spill_alias_set)
2133 dump_insn_info ("removing from active", i_ptr);
2136 last->next_local_store = i_ptr->next_local_store;
2138 active_local_stores = i_ptr->next_local_store;
2142 i_ptr = i_ptr->next_local_store;
2145 else if (group_id >= 0)
2147 /* This is the restricted case where the base is a constant or
2148 the frame pointer and offset is a constant. */
2149 insn_info_t i_ptr = active_local_stores;
2150 insn_info_t last = NULL;
2155 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2158 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2159 group_id, (int)offset, (int)(offset+width));
2164 bool remove = false;
2165 store_info_t store_info = i_ptr->store_rec;
2167 /* Skip the clobbers. */
2168 while (!store_info->is_set)
2169 store_info = store_info->next;
2171 /* There are three cases here. */
2172 if (store_info->group_id < 0)
2173 /* We have a cselib store followed by a read from a
2176 = canon_true_dependence (store_info->mem,
2177 GET_MODE (store_info->mem),
2178 store_info->mem_addr,
2179 mem, mem_addr, rtx_varies_p);
2181 else if (group_id == store_info->group_id)
2183 /* This is a block mode load. We may get lucky and
2184 canon_true_dependence may save the day. */
2187 = canon_true_dependence (store_info->mem,
2188 GET_MODE (store_info->mem),
2189 store_info->mem_addr,
2190 mem, mem_addr, rtx_varies_p);
2192 /* If this read is just reading back something that we just
2193 stored, rewrite the read. */
2197 && offset >= store_info->begin
2198 && offset + width <= store_info->end
2199 && all_positions_needed_p (store_info,
2200 offset - store_info->begin,
2202 && replace_read (store_info, i_ptr, read_info,
2203 insn_info, loc, bb_info->regs_live))
2206 /* The bases are the same, just see if the offsets
2208 if ((offset < store_info->end)
2209 && (offset + width > store_info->begin))
2215 The else case that is missing here is that the
2216 bases are constant but different. There is nothing
2217 to do here because there is no overlap. */
2222 dump_insn_info ("removing from active", i_ptr);
2225 last->next_local_store = i_ptr->next_local_store;
2227 active_local_stores = i_ptr->next_local_store;
2231 i_ptr = i_ptr->next_local_store;
2236 insn_info_t i_ptr = active_local_stores;
2237 insn_info_t last = NULL;
2240 fprintf (dump_file, " processing cselib load mem:");
2241 print_inline_rtx (dump_file, mem, 0);
2242 fprintf (dump_file, "\n");
2247 bool remove = false;
2248 store_info_t store_info = i_ptr->store_rec;
2251 fprintf (dump_file, " processing cselib load against insn %d\n",
2252 INSN_UID (i_ptr->insn));
2254 /* Skip the clobbers. */
2255 while (!store_info->is_set)
2256 store_info = store_info->next;
2258 /* If this read is just reading back something that we just
2259 stored, rewrite the read. */
2261 && store_info->group_id == -1
2262 && store_info->cse_base == base
2264 && offset >= store_info->begin
2265 && offset + width <= store_info->end
2266 && all_positions_needed_p (store_info,
2267 offset - store_info->begin, width)
2268 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2269 bb_info->regs_live))
2272 if (!store_info->alias_set)
2273 remove = canon_true_dependence (store_info->mem,
2274 GET_MODE (store_info->mem),
2275 store_info->mem_addr,
2276 mem, mem_addr, rtx_varies_p);
2281 dump_insn_info ("removing from active", i_ptr);
2284 last->next_local_store = i_ptr->next_local_store;
2286 active_local_stores = i_ptr->next_local_store;
2290 i_ptr = i_ptr->next_local_store;
2296 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2297 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2298 true for any part of *LOC. */
2301 check_mem_read_use (rtx *loc, void *data)
2303 for_each_rtx (loc, check_mem_read_rtx, data);
2307 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2308 So far it only handles arguments passed in registers. */
2311 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2313 CUMULATIVE_ARGS args_so_far;
2317 INIT_CUMULATIVE_ARGS (args_so_far, TREE_TYPE (fn), NULL_RTX, 0, 3);
2319 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2321 arg != void_list_node && idx < nargs;
2322 arg = TREE_CHAIN (arg), idx++)
2324 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2325 rtx reg = FUNCTION_ARG (args_so_far, mode, NULL_TREE, 1), link, tmp;
2326 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2327 || GET_MODE_CLASS (mode) != MODE_INT)
2330 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2332 link = XEXP (link, 1))
2333 if (GET_CODE (XEXP (link, 0)) == USE)
2335 args[idx] = XEXP (XEXP (link, 0), 0);
2336 if (REG_P (args[idx])
2337 && REGNO (args[idx]) == REGNO (reg)
2338 && (GET_MODE (args[idx]) == mode
2339 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2340 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2342 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2343 > GET_MODE_SIZE (mode)))))
2349 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2350 if (GET_MODE (args[idx]) != mode)
2352 if (!tmp || !CONST_INT_P (tmp))
2354 tmp = GEN_INT (trunc_int_for_mode (INTVAL (tmp), mode));
2359 FUNCTION_ARG_ADVANCE (args_so_far, mode, NULL_TREE, 1);
2361 if (arg != void_list_node || idx != nargs)
2367 /* Apply record_store to all candidate stores in INSN. Mark INSN
2368 if some part of it is not a candidate store and assigns to a
2369 non-register target. */
2372 scan_insn (bb_info_t bb_info, rtx insn)
2375 insn_info_t insn_info = (insn_info_t) pool_alloc (insn_info_pool);
2377 memset (insn_info, 0, sizeof (struct insn_info));
2380 fprintf (dump_file, "\n**scanning insn=%d\n",
2383 insn_info->prev_insn = bb_info->last_insn;
2384 insn_info->insn = insn;
2385 bb_info->last_insn = insn_info;
2388 /* Cselib clears the table for this case, so we have to essentially
2390 if (NONJUMP_INSN_P (insn)
2391 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2392 && MEM_VOLATILE_P (PATTERN (insn)))
2394 add_wild_read (bb_info);
2395 insn_info->cannot_delete = true;
2399 /* Look at all of the uses in the insn. */
2400 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2405 tree memset_call = NULL_TREE;
2407 insn_info->cannot_delete = true;
2409 /* Const functions cannot do anything bad i.e. read memory,
2410 however, they can read their parameters which may have
2411 been pushed onto the stack.
2412 memset and bzero don't read memory either. */
2413 const_call = RTL_CONST_CALL_P (insn);
2416 rtx call = PATTERN (insn);
2417 if (GET_CODE (call) == PARALLEL)
2418 call = XVECEXP (call, 0, 0);
2419 if (GET_CODE (call) == SET)
2420 call = SET_SRC (call);
2421 if (GET_CODE (call) == CALL
2422 && MEM_P (XEXP (call, 0))
2423 && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
2425 rtx symbol = XEXP (XEXP (call, 0), 0);
2426 if (SYMBOL_REF_DECL (symbol)
2427 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
2429 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
2431 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
2432 == BUILT_IN_MEMSET))
2433 || SYMBOL_REF_DECL (symbol) == block_clear_fn)
2434 memset_call = SYMBOL_REF_DECL (symbol);
2438 if (const_call || memset_call)
2440 insn_info_t i_ptr = active_local_stores;
2441 insn_info_t last = NULL;
2444 fprintf (dump_file, "%s call %d\n",
2445 const_call ? "const" : "memset", INSN_UID (insn));
2447 /* See the head comment of the frame_read field. */
2448 if (reload_completed)
2449 insn_info->frame_read = true;
2451 /* Loop over the active stores and remove those which are
2452 killed by the const function call. */
2455 bool remove_store = false;
2457 /* The stack pointer based stores are always killed. */
2458 if (i_ptr->stack_pointer_based)
2459 remove_store = true;
2461 /* If the frame is read, the frame related stores are killed. */
2462 else if (insn_info->frame_read)
2464 store_info_t store_info = i_ptr->store_rec;
2466 /* Skip the clobbers. */
2467 while (!store_info->is_set)
2468 store_info = store_info->next;
2470 if (store_info->group_id >= 0
2471 && VEC_index (group_info_t, rtx_group_vec,
2472 store_info->group_id)->frame_related)
2473 remove_store = true;
2479 dump_insn_info ("removing from active", i_ptr);
2482 last->next_local_store = i_ptr->next_local_store;
2484 active_local_stores = i_ptr->next_local_store;
2489 i_ptr = i_ptr->next_local_store;
2495 if (get_call_args (insn, memset_call, args, 3)
2496 && CONST_INT_P (args[1])
2497 && CONST_INT_P (args[2])
2498 && INTVAL (args[2]) > 0)
2500 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2501 set_mem_size (mem, args[2]);
2502 body = gen_rtx_SET (VOIDmode, mem, args[1]);
2503 mems_found += record_store (body, bb_info);
2505 fprintf (dump_file, "handling memset as BLKmode store\n");
2506 if (mems_found == 1)
2508 insn_info->next_local_store = active_local_stores;
2509 active_local_stores = insn_info;
2516 /* Every other call, including pure functions, may read memory. */
2517 add_wild_read (bb_info);
2522 /* Assuming that there are sets in these insns, we cannot delete
2524 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2525 || volatile_refs_p (PATTERN (insn))
2526 || (flag_non_call_exceptions && may_trap_p (PATTERN (insn)))
2527 || (RTX_FRAME_RELATED_P (insn))
2528 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2529 insn_info->cannot_delete = true;
2531 body = PATTERN (insn);
2532 if (GET_CODE (body) == PARALLEL)
2535 for (i = 0; i < XVECLEN (body, 0); i++)
2536 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2539 mems_found += record_store (body, bb_info);
2542 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2543 mems_found, insn_info->cannot_delete ? "true" : "false");
2545 /* If we found some sets of mems, add it into the active_local_stores so
2546 that it can be locally deleted if found dead or used for
2547 replace_read and redundant constant store elimination. Otherwise mark
2548 it as cannot delete. This simplifies the processing later. */
2549 if (mems_found == 1)
2551 insn_info->next_local_store = active_local_stores;
2552 active_local_stores = insn_info;
2555 insn_info->cannot_delete = true;
2559 /* Remove BASE from the set of active_local_stores. This is a
2560 callback from cselib that is used to get rid of the stores in
2561 active_local_stores. */
2564 remove_useless_values (cselib_val *base)
2566 insn_info_t insn_info = active_local_stores;
2567 insn_info_t last = NULL;
2571 store_info_t store_info = insn_info->store_rec;
2574 /* If ANY of the store_infos match the cselib group that is
2575 being deleted, then the insn can not be deleted. */
2578 if ((store_info->group_id == -1)
2579 && (store_info->cse_base == base))
2584 store_info = store_info->next;
2590 last->next_local_store = insn_info->next_local_store;
2592 active_local_stores = insn_info->next_local_store;
2593 free_store_info (insn_info);
2598 insn_info = insn_info->next_local_store;
2603 /* Do all of step 1. */
2609 bitmap regs_live = BITMAP_ALLOC (NULL);
2611 cselib_init (false);
2612 all_blocks = BITMAP_ALLOC (NULL);
2613 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2614 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2619 bb_info_t bb_info = (bb_info_t) pool_alloc (bb_info_pool);
2621 memset (bb_info, 0, sizeof (struct bb_info));
2622 bitmap_set_bit (all_blocks, bb->index);
2623 bb_info->regs_live = regs_live;
2625 bitmap_copy (regs_live, DF_LR_IN (bb));
2626 df_simulate_initialize_forwards (bb, regs_live);
2628 bb_table[bb->index] = bb_info;
2629 cselib_discard_hook = remove_useless_values;
2631 if (bb->index >= NUM_FIXED_BLOCKS)
2636 = create_alloc_pool ("cse_store_info_pool",
2637 sizeof (struct store_info), 100);
2638 active_local_stores = NULL;
2639 cselib_clear_table ();
2641 /* Scan the insns. */
2642 FOR_BB_INSNS (bb, insn)
2645 scan_insn (bb_info, insn);
2646 cselib_process_insn (insn);
2648 df_simulate_one_insn_forwards (bb, insn, regs_live);
2651 /* This is something of a hack, because the global algorithm
2652 is supposed to take care of the case where stores go dead
2653 at the end of the function. However, the global
2654 algorithm must take a more conservative view of block
2655 mode reads than the local alg does. So to get the case
2656 where you have a store to the frame followed by a non
2657 overlapping block more read, we look at the active local
2658 stores at the end of the function and delete all of the
2659 frame and spill based ones. */
2660 if (stores_off_frame_dead_at_return
2661 && (EDGE_COUNT (bb->succs) == 0
2662 || (single_succ_p (bb)
2663 && single_succ (bb) == EXIT_BLOCK_PTR
2664 && ! crtl->calls_eh_return)))
2666 insn_info_t i_ptr = active_local_stores;
2669 store_info_t store_info = i_ptr->store_rec;
2671 /* Skip the clobbers. */
2672 while (!store_info->is_set)
2673 store_info = store_info->next;
2674 if (store_info->alias_set && !i_ptr->cannot_delete)
2675 delete_dead_store_insn (i_ptr);
2677 if (store_info->group_id >= 0)
2680 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
2681 if (group->frame_related && !i_ptr->cannot_delete)
2682 delete_dead_store_insn (i_ptr);
2685 i_ptr = i_ptr->next_local_store;
2689 /* Get rid of the loads that were discovered in
2690 replace_read. Cselib is finished with this block. */
2691 while (deferred_change_list)
2693 deferred_change_t next = deferred_change_list->next;
2695 /* There is no reason to validate this change. That was
2697 *deferred_change_list->loc = deferred_change_list->reg;
2698 pool_free (deferred_change_pool, deferred_change_list);
2699 deferred_change_list = next;
2702 /* Get rid of all of the cselib based store_infos in this
2703 block and mark the containing insns as not being
2705 ptr = bb_info->last_insn;
2708 if (ptr->contains_cselib_groups)
2710 store_info_t s_info = ptr->store_rec;
2711 while (s_info && !s_info->is_set)
2712 s_info = s_info->next;
2714 && s_info->redundant_reason
2715 && s_info->redundant_reason->insn
2716 && !ptr->cannot_delete)
2719 fprintf (dump_file, "Locally deleting insn %d "
2720 "because insn %d stores the "
2721 "same value and couldn't be "
2723 INSN_UID (ptr->insn),
2724 INSN_UID (s_info->redundant_reason->insn));
2725 delete_dead_store_insn (ptr);
2728 s_info->redundant_reason = NULL;
2729 free_store_info (ptr);
2733 store_info_t s_info;
2735 /* Free at least positions_needed bitmaps. */
2736 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2737 if (s_info->is_large)
2739 BITMAP_FREE (s_info->positions_needed.large.bitmap);
2740 s_info->is_large = false;
2743 ptr = ptr->prev_insn;
2746 free_alloc_pool (cse_store_info_pool);
2748 bb_info->regs_live = NULL;
2751 BITMAP_FREE (regs_live);
2753 htab_empty (rtx_group_table);
2757 /*----------------------------------------------------------------------------
2760 Assign each byte position in the stores that we are going to
2761 analyze globally to a position in the bitmaps. Returns true if
2762 there are any bit positions assigned.
2763 ----------------------------------------------------------------------------*/
2766 dse_step2_init (void)
2771 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
2773 /* For all non stack related bases, we only consider a store to
2774 be deletable if there are two or more stores for that
2775 position. This is because it takes one store to make the
2776 other store redundant. However, for the stores that are
2777 stack related, we consider them if there is only one store
2778 for the position. We do this because the stack related
2779 stores can be deleted if their is no read between them and
2780 the end of the function.
2782 To make this work in the current framework, we take the stack
2783 related bases add all of the bits from store1 into store2.
2784 This has the effect of making the eligible even if there is
2787 if (stores_off_frame_dead_at_return && group->frame_related)
2789 bitmap_ior_into (group->store2_n, group->store1_n);
2790 bitmap_ior_into (group->store2_p, group->store1_p);
2792 fprintf (dump_file, "group %d is frame related ", i);
2795 group->offset_map_size_n++;
2796 group->offset_map_n = XNEWVEC (int, group->offset_map_size_n);
2797 group->offset_map_size_p++;
2798 group->offset_map_p = XNEWVEC (int, group->offset_map_size_p);
2799 group->process_globally = false;
2802 fprintf (dump_file, "group %d(%d+%d): ", i,
2803 (int)bitmap_count_bits (group->store2_n),
2804 (int)bitmap_count_bits (group->store2_p));
2805 bitmap_print (dump_file, group->store2_n, "n ", " ");
2806 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2812 /* Init the offset tables for the normal case. */
2815 dse_step2_nospill (void)
2819 /* Position 0 is unused because 0 is used in the maps to mean
2821 current_position = 1;
2823 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
2828 if (group == clear_alias_group)
2831 memset (group->offset_map_n, 0, sizeof(int) * group->offset_map_size_n);
2832 memset (group->offset_map_p, 0, sizeof(int) * group->offset_map_size_p);
2833 bitmap_clear (group->group_kill);
2835 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2837 bitmap_set_bit (group->group_kill, current_position);
2838 group->offset_map_n[j] = current_position++;
2839 group->process_globally = true;
2841 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2843 bitmap_set_bit (group->group_kill, current_position);
2844 group->offset_map_p[j] = current_position++;
2845 group->process_globally = true;
2848 return current_position != 1;
2852 /* Init the offset tables for the spill case. */
2855 dse_step2_spill (void)
2858 group_info_t group = clear_alias_group;
2861 /* Position 0 is unused because 0 is used in the maps to mean
2863 current_position = 1;
2867 bitmap_print (dump_file, clear_alias_sets,
2868 "clear alias sets ", "\n");
2869 bitmap_print (dump_file, disqualified_clear_alias_sets,
2870 "disqualified clear alias sets ", "\n");
2873 memset (group->offset_map_n, 0, sizeof(int) * group->offset_map_size_n);
2874 memset (group->offset_map_p, 0, sizeof(int) * group->offset_map_size_p);
2875 bitmap_clear (group->group_kill);
2877 /* Remove the disqualified positions from the store2_p set. */
2878 bitmap_and_compl_into (group->store2_p, disqualified_clear_alias_sets);
2880 /* We do not need to process the store2_n set because
2881 alias_sets are always positive. */
2882 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2884 bitmap_set_bit (group->group_kill, current_position);
2885 group->offset_map_p[j] = current_position++;
2886 group->process_globally = true;
2889 return current_position != 1;
2894 /*----------------------------------------------------------------------------
2897 Build the bit vectors for the transfer functions.
2898 ----------------------------------------------------------------------------*/
2901 /* Note that this is NOT a general purpose function. Any mem that has
2902 an alias set registered here expected to be COMPLETELY unaliased:
2903 i.e it's addresses are not and need not be examined.
2905 It is known that all references to this address will have this
2906 alias set and there are NO other references to this address in the
2909 Currently the only place that is known to be clean enough to use
2910 this interface is the code that assigns the spill locations.
2912 All of the mems that have alias_sets registered are subjected to a
2913 very powerful form of dse where function calls, volatile reads and
2914 writes, and reads from random location are not taken into account.
2916 It is also assumed that these locations go dead when the function
2917 returns. This assumption could be relaxed if there were found to
2918 be places that this assumption was not correct.
2920 The MODE is passed in and saved. The mode of each load or store to
2921 a mem with ALIAS_SET is checked against MEM. If the size of that
2922 load or store is different from MODE, processing is halted on this
2923 alias set. For the vast majority of aliases sets, all of the loads
2924 and stores will use the same mode. But vectors are treated
2925 differently: the alias set is established for the entire vector,
2926 but reload will insert loads and stores for individual elements and
2927 we do not necessarily have the information to track those separate
2928 elements. So when we see a mode mismatch, we just bail. */
2932 dse_record_singleton_alias_set (alias_set_type alias_set,
2933 enum machine_mode mode)
2935 struct clear_alias_mode_holder tmp_holder;
2936 struct clear_alias_mode_holder *entry;
2939 /* If we are not going to run dse, we need to return now or there
2940 will be problems with allocating the bitmaps. */
2941 if ((!gate_dse()) || !alias_set)
2944 if (!clear_alias_sets)
2946 clear_alias_sets = BITMAP_ALLOC (NULL);
2947 disqualified_clear_alias_sets = BITMAP_ALLOC (NULL);
2948 clear_alias_mode_table = htab_create (11, clear_alias_mode_hash,
2949 clear_alias_mode_eq, NULL);
2950 clear_alias_mode_pool = create_alloc_pool ("clear_alias_mode_pool",
2951 sizeof (struct clear_alias_mode_holder), 100);
2954 bitmap_set_bit (clear_alias_sets, alias_set);
2956 tmp_holder.alias_set = alias_set;
2958 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, INSERT);
2959 gcc_assert (*slot == NULL);
2962 (struct clear_alias_mode_holder *) pool_alloc (clear_alias_mode_pool);
2963 entry->alias_set = alias_set;
2968 /* Remove ALIAS_SET from the sets of stack slots being considered. */
2971 dse_invalidate_singleton_alias_set (alias_set_type alias_set)
2973 if ((!gate_dse()) || !alias_set)
2976 bitmap_clear_bit (clear_alias_sets, alias_set);
2980 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2984 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
2988 HOST_WIDE_INT offset_p = -offset;
2989 if (offset_p >= group_info->offset_map_size_n)
2991 return group_info->offset_map_n[offset_p];
2995 if (offset >= group_info->offset_map_size_p)
2997 return group_info->offset_map_p[offset];
3002 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3006 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
3011 group_info_t group_info
3012 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
3013 if (group_info->process_globally)
3014 for (i = store_info->begin; i < store_info->end; i++)
3016 int index = get_bitmap_index (group_info, i);
3019 bitmap_set_bit (gen, index);
3021 bitmap_clear_bit (kill, index);
3024 store_info = store_info->next;
3029 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3033 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
3037 if (store_info->alias_set)
3039 int index = get_bitmap_index (clear_alias_group,
3040 store_info->alias_set);
3043 bitmap_set_bit (gen, index);
3045 bitmap_clear_bit (kill, index);
3048 store_info = store_info->next;
3053 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3057 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
3059 read_info_t read_info = insn_info->read_rec;
3063 /* If this insn reads the frame, kill all the frame related stores. */
3064 if (insn_info->frame_read)
3066 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
3067 if (group->process_globally && group->frame_related)
3070 bitmap_ior_into (kill, group->group_kill);
3071 bitmap_and_compl_into (gen, group->group_kill);
3077 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
3079 if (group->process_globally)
3081 if (i == read_info->group_id)
3083 if (read_info->begin > read_info->end)
3085 /* Begin > end for block mode reads. */
3087 bitmap_ior_into (kill, group->group_kill);
3088 bitmap_and_compl_into (gen, group->group_kill);
3092 /* The groups are the same, just process the
3095 for (j = read_info->begin; j < read_info->end; j++)
3097 int index = get_bitmap_index (group, j);
3101 bitmap_set_bit (kill, index);
3102 bitmap_clear_bit (gen, index);
3109 /* The groups are different, if the alias sets
3110 conflict, clear the entire group. We only need
3111 to apply this test if the read_info is a cselib
3112 read. Anything with a constant base cannot alias
3113 something else with a different constant
3115 if ((read_info->group_id < 0)
3116 && canon_true_dependence (group->base_mem,
3118 group->canon_base_addr,
3119 read_info->mem, NULL_RTX,
3123 bitmap_ior_into (kill, group->group_kill);
3124 bitmap_and_compl_into (gen, group->group_kill);
3130 read_info = read_info->next;
3134 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3138 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
3142 if (read_info->alias_set)
3144 int index = get_bitmap_index (clear_alias_group,
3145 read_info->alias_set);
3149 bitmap_set_bit (kill, index);
3150 bitmap_clear_bit (gen, index);
3154 read_info = read_info->next;
3159 /* Return the insn in BB_INFO before the first wild read or if there
3160 are no wild reads in the block, return the last insn. */
3163 find_insn_before_first_wild_read (bb_info_t bb_info)
3165 insn_info_t insn_info = bb_info->last_insn;
3166 insn_info_t last_wild_read = NULL;
3170 if (insn_info->wild_read)
3172 last_wild_read = insn_info->prev_insn;
3173 /* Block starts with wild read. */
3174 if (!last_wild_read)
3178 insn_info = insn_info->prev_insn;
3182 return last_wild_read;
3184 return bb_info->last_insn;
3188 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3189 the block in order to build the gen and kill sets for the block.
3190 We start at ptr which may be the last insn in the block or may be
3191 the first insn with a wild read. In the latter case we are able to
3192 skip the rest of the block because it just does not matter:
3193 anything that happens is hidden by the wild read. */
3196 dse_step3_scan (bool for_spills, basic_block bb)
3198 bb_info_t bb_info = bb_table[bb->index];
3199 insn_info_t insn_info;
3202 /* There are no wild reads in the spill case. */
3203 insn_info = bb_info->last_insn;
3205 insn_info = find_insn_before_first_wild_read (bb_info);
3207 /* In the spill case or in the no_spill case if there is no wild
3208 read in the block, we will need a kill set. */
3209 if (insn_info == bb_info->last_insn)
3212 bitmap_clear (bb_info->kill);
3214 bb_info->kill = BITMAP_ALLOC (NULL);
3218 BITMAP_FREE (bb_info->kill);
3222 /* There may have been code deleted by the dce pass run before
3224 if (insn_info->insn && INSN_P (insn_info->insn))
3226 /* Process the read(s) last. */
3229 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3230 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
3234 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3235 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
3239 insn_info = insn_info->prev_insn;
3244 /* Set the gen set of the exit block, and also any block with no
3245 successors that does not have a wild read. */
3248 dse_step3_exit_block_scan (bb_info_t bb_info)
3250 /* The gen set is all 0's for the exit block except for the
3251 frame_pointer_group. */
3253 if (stores_off_frame_dead_at_return)
3258 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
3260 if (group->process_globally && group->frame_related)
3261 bitmap_ior_into (bb_info->gen, group->group_kill);
3267 /* Find all of the blocks that are not backwards reachable from the
3268 exit block or any block with no successors (BB). These are the
3269 infinite loops or infinite self loops. These blocks will still
3270 have their bits set in UNREACHABLE_BLOCKS. */
3273 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3278 if (TEST_BIT (unreachable_blocks, bb->index))
3280 RESET_BIT (unreachable_blocks, bb->index);
3281 FOR_EACH_EDGE (e, ei, bb->preds)
3283 mark_reachable_blocks (unreachable_blocks, e->src);
3288 /* Build the transfer functions for the function. */
3291 dse_step3 (bool for_spills)
3294 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block);
3295 sbitmap_iterator sbi;
3296 bitmap all_ones = NULL;
3299 sbitmap_ones (unreachable_blocks);
3303 bb_info_t bb_info = bb_table[bb->index];
3305 bitmap_clear (bb_info->gen);
3307 bb_info->gen = BITMAP_ALLOC (NULL);
3309 if (bb->index == ENTRY_BLOCK)
3311 else if (bb->index == EXIT_BLOCK)
3312 dse_step3_exit_block_scan (bb_info);
3314 dse_step3_scan (for_spills, bb);
3315 if (EDGE_COUNT (bb->succs) == 0)
3316 mark_reachable_blocks (unreachable_blocks, bb);
3318 /* If this is the second time dataflow is run, delete the old
3321 BITMAP_FREE (bb_info->in);
3323 BITMAP_FREE (bb_info->out);
3326 /* For any block in an infinite loop, we must initialize the out set
3327 to all ones. This could be expensive, but almost never occurs in
3328 practice. However, it is common in regression tests. */
3329 EXECUTE_IF_SET_IN_SBITMAP (unreachable_blocks, 0, i, sbi)
3331 if (bitmap_bit_p (all_blocks, i))
3333 bb_info_t bb_info = bb_table[i];
3339 all_ones = BITMAP_ALLOC (NULL);
3340 for (j = 0; VEC_iterate (group_info_t, rtx_group_vec, j, group); j++)
3341 bitmap_ior_into (all_ones, group->group_kill);
3345 bb_info->out = BITMAP_ALLOC (NULL);
3346 bitmap_copy (bb_info->out, all_ones);
3352 BITMAP_FREE (all_ones);
3353 sbitmap_free (unreachable_blocks);
3358 /*----------------------------------------------------------------------------
3361 Solve the bitvector equations.
3362 ----------------------------------------------------------------------------*/
3365 /* Confluence function for blocks with no successors. Create an out
3366 set from the gen set of the exit block. This block logically has
3367 the exit block as a successor. */
3372 dse_confluence_0 (basic_block bb)
3374 bb_info_t bb_info = bb_table[bb->index];
3376 if (bb->index == EXIT_BLOCK)
3381 bb_info->out = BITMAP_ALLOC (NULL);
3382 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3386 /* Propagate the information from the in set of the dest of E to the
3387 out set of the src of E. If the various in or out sets are not
3388 there, that means they are all ones. */
3391 dse_confluence_n (edge e)
3393 bb_info_t src_info = bb_table[e->src->index];
3394 bb_info_t dest_info = bb_table[e->dest->index];
3399 bitmap_and_into (src_info->out, dest_info->in);
3402 src_info->out = BITMAP_ALLOC (NULL);
3403 bitmap_copy (src_info->out, dest_info->in);
3409 /* Propagate the info from the out to the in set of BB_INDEX's basic
3410 block. There are three cases:
3412 1) The block has no kill set. In this case the kill set is all
3413 ones. It does not matter what the out set of the block is, none of
3414 the info can reach the top. The only thing that reaches the top is
3415 the gen set and we just copy the set.
3417 2) There is a kill set but no out set and bb has successors. In
3418 this case we just return. Eventually an out set will be created and
3419 it is better to wait than to create a set of ones.
3421 3) There is both a kill and out set. We apply the obvious transfer
3426 dse_transfer_function (int bb_index)
3428 bb_info_t bb_info = bb_table[bb_index];
3436 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3437 bb_info->out, bb_info->kill);
3440 bb_info->in = BITMAP_ALLOC (NULL);
3441 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3442 bb_info->out, bb_info->kill);
3452 /* Case 1 above. If there is already an in set, nothing
3458 bb_info->in = BITMAP_ALLOC (NULL);
3459 bitmap_copy (bb_info->in, bb_info->gen);
3465 /* Solve the dataflow equations. */
3470 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3471 dse_confluence_n, dse_transfer_function,
3472 all_blocks, df_get_postorder (DF_BACKWARD),
3473 df_get_n_blocks (DF_BACKWARD));
3478 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3481 bb_info_t bb_info = bb_table[bb->index];
3483 df_print_bb_index (bb, dump_file);
3485 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3487 fprintf (dump_file, " in: *MISSING*\n");
3489 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3491 fprintf (dump_file, " gen: *MISSING*\n");
3493 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3495 fprintf (dump_file, " kill: *MISSING*\n");
3497 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3499 fprintf (dump_file, " out: *MISSING*\n\n");
3506 /*----------------------------------------------------------------------------
3509 Delete the stores that can only be deleted using the global information.
3510 ----------------------------------------------------------------------------*/
3514 dse_step5_nospill (void)
3519 bb_info_t bb_info = bb_table[bb->index];
3520 insn_info_t insn_info = bb_info->last_insn;
3521 bitmap v = bb_info->out;
3525 bool deleted = false;
3526 if (dump_file && insn_info->insn)
3528 fprintf (dump_file, "starting to process insn %d\n",
3529 INSN_UID (insn_info->insn));
3530 bitmap_print (dump_file, v, " v: ", "\n");
3533 /* There may have been code deleted by the dce pass run before
3536 && INSN_P (insn_info->insn)
3537 && (!insn_info->cannot_delete)
3538 && (!bitmap_empty_p (v)))
3540 store_info_t store_info = insn_info->store_rec;
3542 /* Try to delete the current insn. */
3545 /* Skip the clobbers. */
3546 while (!store_info->is_set)
3547 store_info = store_info->next;
3549 if (store_info->alias_set)
3554 group_info_t group_info
3555 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
3557 for (i = store_info->begin; i < store_info->end; i++)
3559 int index = get_bitmap_index (group_info, i);
3562 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3563 if (index == 0 || !bitmap_bit_p (v, index))
3566 fprintf (dump_file, "failing at i = %d\n", (int)i);
3576 check_for_inc_dec (insn_info->insn);
3577 delete_insn (insn_info->insn);
3578 insn_info->insn = NULL;
3583 /* We do want to process the local info if the insn was
3584 deleted. For instance, if the insn did a wild read, we
3585 no longer need to trash the info. */
3587 && INSN_P (insn_info->insn)
3590 scan_stores_nospill (insn_info->store_rec, v, NULL);
3591 if (insn_info->wild_read)
3594 fprintf (dump_file, "wild read\n");
3597 else if (insn_info->read_rec)
3600 fprintf (dump_file, "regular read\n");
3601 scan_reads_nospill (insn_info, v, NULL);
3605 insn_info = insn_info->prev_insn;
3612 dse_step5_spill (void)
3617 bb_info_t bb_info = bb_table[bb->index];
3618 insn_info_t insn_info = bb_info->last_insn;
3619 bitmap v = bb_info->out;
3623 bool deleted = false;
3624 /* There may have been code deleted by the dce pass run before
3627 && INSN_P (insn_info->insn)
3628 && (!insn_info->cannot_delete)
3629 && (!bitmap_empty_p (v)))
3631 /* Try to delete the current insn. */
3632 store_info_t store_info = insn_info->store_rec;
3637 if (store_info->alias_set)
3639 int index = get_bitmap_index (clear_alias_group,
3640 store_info->alias_set);
3641 if (index == 0 || !bitmap_bit_p (v, index))
3649 store_info = store_info->next;
3651 if (deleted && dbg_cnt (dse))
3654 fprintf (dump_file, "Spill deleting insn %d\n",
3655 INSN_UID (insn_info->insn));
3656 check_for_inc_dec (insn_info->insn);
3657 delete_insn (insn_info->insn);
3659 insn_info->insn = NULL;
3664 && INSN_P (insn_info->insn)
3667 scan_stores_spill (insn_info->store_rec, v, NULL);
3668 scan_reads_spill (insn_info->read_rec, v, NULL);
3671 insn_info = insn_info->prev_insn;
3678 /*----------------------------------------------------------------------------
3681 Delete stores made redundant by earlier stores (which store the same
3682 value) that couldn't be eliminated.
3683 ----------------------------------------------------------------------------*/
3692 bb_info_t bb_info = bb_table[bb->index];
3693 insn_info_t insn_info = bb_info->last_insn;
3697 /* There may have been code deleted by the dce pass run before
3700 && INSN_P (insn_info->insn)
3701 && !insn_info->cannot_delete)
3703 store_info_t s_info = insn_info->store_rec;
3705 while (s_info && !s_info->is_set)
3706 s_info = s_info->next;
3708 && s_info->redundant_reason
3709 && s_info->redundant_reason->insn
3710 && INSN_P (s_info->redundant_reason->insn))
3712 rtx rinsn = s_info->redundant_reason->insn;
3714 fprintf (dump_file, "Locally deleting insn %d "
3715 "because insn %d stores the "
3716 "same value and couldn't be "
3718 INSN_UID (insn_info->insn),
3720 delete_dead_store_insn (insn_info);
3723 insn_info = insn_info->prev_insn;
3728 /*----------------------------------------------------------------------------
3731 Destroy everything left standing.
3732 ----------------------------------------------------------------------------*/
3735 dse_step7 (bool global_done)
3741 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
3743 free (group->offset_map_n);
3744 free (group->offset_map_p);
3745 BITMAP_FREE (group->store1_n);
3746 BITMAP_FREE (group->store1_p);
3747 BITMAP_FREE (group->store2_n);
3748 BITMAP_FREE (group->store2_p);
3749 BITMAP_FREE (group->group_kill);
3755 bb_info_t bb_info = bb_table[bb->index];
3756 BITMAP_FREE (bb_info->gen);
3758 BITMAP_FREE (bb_info->kill);
3760 BITMAP_FREE (bb_info->in);
3762 BITMAP_FREE (bb_info->out);
3765 if (clear_alias_sets)
3767 BITMAP_FREE (clear_alias_sets);
3768 BITMAP_FREE (disqualified_clear_alias_sets);
3769 free_alloc_pool (clear_alias_mode_pool);
3770 htab_delete (clear_alias_mode_table);
3773 end_alias_analysis ();
3775 htab_delete (rtx_group_table);
3776 VEC_free (group_info_t, heap, rtx_group_vec);
3777 BITMAP_FREE (all_blocks);
3778 BITMAP_FREE (scratch);
3780 free_alloc_pool (rtx_store_info_pool);
3781 free_alloc_pool (read_info_pool);
3782 free_alloc_pool (insn_info_pool);
3783 free_alloc_pool (bb_info_pool);
3784 free_alloc_pool (rtx_group_info_pool);
3785 free_alloc_pool (deferred_change_pool);
3789 /* -------------------------------------------------------------------------
3791 ------------------------------------------------------------------------- */
3793 /* Callback for running pass_rtl_dse. */
3796 rest_of_handle_dse (void)
3798 bool did_global = false;
3800 df_set_flags (DF_DEFER_INSN_RESCAN);
3802 /* Need the notes since we must track live hardregs in the forwards
3804 df_note_add_problem ();
3810 if (dse_step2_nospill ())
3812 df_set_flags (DF_LR_RUN_DCE);
3816 fprintf (dump_file, "doing global processing\n");
3819 dse_step5_nospill ();
3822 /* For the instance of dse that runs after reload, we make a special
3823 pass to process the spills. These are special in that they are
3824 totally transparent, i.e, there is no aliasing issues that need
3825 to be considered. This means that the wild reads that kill
3826 everything else do not apply here. */
3827 if (clear_alias_sets && dse_step2_spill ())
3831 df_set_flags (DF_LR_RUN_DCE);
3836 fprintf (dump_file, "doing global spill processing\n");
3843 dse_step7 (did_global);
3846 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3847 locally_deleted, globally_deleted, spill_deleted);
3854 return gate_dse1 () || gate_dse2 ();
3860 return optimize > 0 && flag_dse
3867 return optimize > 0 && flag_dse
3871 struct rtl_opt_pass pass_rtl_dse1 =
3876 gate_dse1, /* gate */
3877 rest_of_handle_dse, /* execute */
3880 0, /* static_pass_number */
3881 TV_DSE1, /* tv_id */
3882 0, /* properties_required */
3883 0, /* properties_provided */
3884 0, /* properties_destroyed */
3885 0, /* todo_flags_start */
3887 TODO_df_finish | TODO_verify_rtl_sharing |
3888 TODO_ggc_collect /* todo_flags_finish */
3892 struct rtl_opt_pass pass_rtl_dse2 =
3897 gate_dse2, /* gate */
3898 rest_of_handle_dse, /* execute */
3901 0, /* static_pass_number */
3902 TV_DSE2, /* tv_id */
3903 0, /* properties_required */
3904 0, /* properties_provided */
3905 0, /* properties_destroyed */
3906 0, /* todo_flags_start */
3908 TODO_df_finish | TODO_verify_rtl_sharing |
3909 TODO_ggc_collect /* todo_flags_finish */