2008-12-09 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / dse.c
blobd7fb2d8aa20cdbc0c96b02e11289761c34017b02
1 /* RTL dead store elimination.
2 Copyright (C) 2005, 2006, 2007, 2008 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
12 version.
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
17 for more details.
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/>. */
23 #undef BASELINE
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "hashtab.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "tm_p.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "flags.h"
36 #include "df.h"
37 #include "cselib.h"
38 #include "timevar.h"
39 #include "tree-pass.h"
40 #include "alloc-pool.h"
41 #include "alias.h"
42 #include "insn-config.h"
43 #include "expr.h"
44 #include "recog.h"
45 #include "dse.h"
46 #include "optabs.h"
47 #include "dbgcnt.h"
49 /* This file contains three techniques for performing Dead Store
50 Elimination (dse).
52 * The first technique performs dse locally on any base address. It
53 is based on the cselib which is a local value numbering technique.
54 This technique is local to a basic block but deals with a fairly
55 general addresses.
57 * The second technique performs dse globally but is restricted to
58 base addresses that are either constant or are relative to the
59 frame_pointer.
61 * The third technique, (which is only done after register allocation)
62 processes the spill spill slots. This differs from the second
63 technique because it takes advantage of the fact that spilling is
64 completely free from the effects of aliasing.
66 Logically, dse is a backwards dataflow problem. A store can be
67 deleted if it if cannot be reached in the backward direction by any
68 use of the value being stored. However, the local technique uses a
69 forwards scan of the basic block because cselib requires that the
70 block be processed in that order.
72 The pass is logically broken into 7 steps:
74 0) Initialization.
76 1) The local algorithm, as well as scanning the insns for the two
77 global algorithms.
79 2) Analysis to see if the global algs are necessary. In the case
80 of stores base on a constant address, there must be at least two
81 stores to that address, to make it possible to delete some of the
82 stores. In the case of stores off of the frame or spill related
83 stores, only one store to an address is necessary because those
84 stores die at the end of the function.
86 3) Set up the global dataflow equations based on processing the
87 info parsed in the first step.
89 4) Solve the dataflow equations.
91 5) Delete the insns that the global analysis has indicated are
92 unnecessary.
94 6) Cleanup.
96 This step uses cselib and canon_rtx to build the largest expression
97 possible for each address. This pass is a forwards pass through
98 each basic block. From the point of view of the global technique,
99 the first pass could examine a block in either direction. The
100 forwards ordering is to accommodate cselib.
102 We a simplifying assumption: addresses fall into four broad
103 categories:
105 1) base has rtx_varies_p == false, offset is constant.
106 2) base has rtx_varies_p == false, offset variable.
107 3) base has rtx_varies_p == true, offset constant.
108 4) base has rtx_varies_p == true, offset variable.
110 The local passes are able to process all 4 kinds of addresses. The
111 global pass only handles (1).
113 The global problem is formulated as follows:
115 A store, S1, to address A, where A is not relative to the stack
116 frame, can be eliminated if all paths from S1 to the end of the
117 of the function contain another store to A before a read to A.
119 If the address A is relative to the stack frame, a store S2 to A
120 can be eliminated if there are no paths from S1 that reach the
121 end of the function that read A before another store to A. In
122 this case S2 can be deleted if there are paths to from S2 to the
123 end of the function that have no reads or writes to A. This
124 second case allows stores to the stack frame to be deleted that
125 would otherwise die when the function returns. This cannot be
126 done if stores_off_frame_dead_at_return is not true. See the doc
127 for that variable for when this variable is false.
129 The global problem is formulated as a backwards set union
130 dataflow problem where the stores are the gens and reads are the
131 kills. Set union problems are rare and require some special
132 handling given our representation of bitmaps. A straightforward
133 implementation of requires a lot of bitmaps filled with 1s.
134 These are expensive and cumbersome in our bitmap formulation so
135 care has been taken to avoid large vectors filled with 1s. See
136 the comments in bb_info and in the dataflow confluence functions
137 for details.
139 There are two places for further enhancements to this algorithm:
141 1) The original dse which was embedded in a pass called flow also
142 did local address forwarding. For example in
144 A <- r100
145 ... <- A
147 flow would replace the right hand side of the second insn with a
148 reference to r100. Most of the information is available to add this
149 to this pass. It has not done it because it is a lot of work in
150 the case that either r100 is assigned to between the first and
151 second insn and/or the second insn is a load of part of the value
152 stored by the first insn.
154 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
155 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
156 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
157 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
159 2) The cleaning up of spill code is quite profitable. It currently
160 depends on reading tea leaves and chicken entrails left by reload.
161 This pass depends on reload creating a singleton alias set for each
162 spill slot and telling the next dse pass which of these alias sets
163 are the singletons. Rather than analyze the addresses of the
164 spills, dse's spill processing just does analysis of the loads and
165 stores that use those alias sets. There are three cases where this
166 falls short:
168 a) Reload sometimes creates the slot for one mode of access, and
169 then inserts loads and/or stores for a smaller mode. In this
170 case, the current code just punts on the slot. The proper thing
171 to do is to back out and use one bit vector position for each
172 byte of the entity associated with the slot. This depends on
173 KNOWING that reload always generates the accesses for each of the
174 bytes in some canonical (read that easy to understand several
175 passes after reload happens) way.
177 b) Reload sometimes decides that spill slot it allocated was not
178 large enough for the mode and goes back and allocates more slots
179 with the same mode and alias set. The backout in this case is a
180 little more graceful than (a). In this case the slot is unmarked
181 as being a spill slot and if final address comes out to be based
182 off the frame pointer, the global algorithm handles this slot.
184 c) For any pass that may prespill, there is currently no
185 mechanism to tell the dse pass that the slot being used has the
186 special properties that reload uses. It may be that all that is
187 required is to have those passes make the same calls that reload
188 does, assuming that the alias sets can be manipulated in the same
189 way. */
191 /* There are limits to the size of constant offsets we model for the
192 global problem. There are certainly test cases, that exceed this
193 limit, however, it is unlikely that there are important programs
194 that really have constant offsets this size. */
195 #define MAX_OFFSET (64 * 1024)
198 static bitmap scratch = NULL;
199 struct insn_info;
201 /* This structure holds information about a candidate store. */
202 struct store_info
205 /* False means this is a clobber. */
206 bool is_set;
208 /* The id of the mem group of the base address. If rtx_varies_p is
209 true, this is -1. Otherwise, it is the index into the group
210 table. */
211 int group_id;
213 /* This is the cselib value. */
214 cselib_val *cse_base;
216 /* This canonized mem. */
217 rtx mem;
219 /* The result of get_addr on mem. */
220 rtx mem_addr;
222 /* If this is non-zero, it is the alias set of a spill location. */
223 alias_set_type alias_set;
225 /* The offset of the first and byte before the last byte associated
226 with the operation. */
227 int begin, end;
229 /* An bitmask as wide as the number of bytes in the word that
230 contains a 1 if the byte may be needed. The store is unused if
231 all of the bits are 0. */
232 unsigned HOST_WIDE_INT positions_needed;
234 /* The next store info for this insn. */
235 struct store_info *next;
237 /* The right hand side of the store. This is used if there is a
238 subsequent reload of the mems address somewhere later in the
239 basic block. */
240 rtx rhs;
243 /* Return a bitmask with the first N low bits set. */
245 static unsigned HOST_WIDE_INT
246 lowpart_bitmask (int n)
248 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
249 return mask >> (HOST_BITS_PER_WIDE_INT - n);
252 typedef struct store_info *store_info_t;
253 static alloc_pool cse_store_info_pool;
254 static alloc_pool rtx_store_info_pool;
256 /* This structure holds information about a load. These are only
257 built for rtx bases. */
258 struct read_info
260 /* The id of the mem group of the base address. */
261 int group_id;
263 /* If this is non-zero, it is the alias set of a spill location. */
264 alias_set_type alias_set;
266 /* The offset of the first and byte after the last byte associated
267 with the operation. If begin == end == 0, the read did not have
268 a constant offset. */
269 int begin, end;
271 /* The mem being read. */
272 rtx mem;
274 /* The next read_info for this insn. */
275 struct read_info *next;
277 typedef struct read_info *read_info_t;
278 static alloc_pool read_info_pool;
281 /* One of these records is created for each insn. */
283 struct insn_info
285 /* Set true if the insn contains a store but the insn itself cannot
286 be deleted. This is set if the insn is a parallel and there is
287 more than one non dead output or if the insn is in some way
288 volatile. */
289 bool cannot_delete;
291 /* This field is only used by the global algorithm. It is set true
292 if the insn contains any read of mem except for a (1). This is
293 also set if the insn is a call or has a clobber mem. If the insn
294 contains a wild read, the use_rec will be null. */
295 bool wild_read;
297 /* This field is only used for the processing of const functions.
298 These functions cannot read memory, but they can read the stack
299 because that is where they may get their parms. We need to be
300 this conservative because, like the store motion pass, we don't
301 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
302 Moreover, we need to distinguish two cases:
303 1. Before reload (register elimination), the stores related to
304 outgoing arguments are stack pointer based and thus deemed
305 of non-constant base in this pass. This requires special
306 handling but also means that the frame pointer based stores
307 need not be killed upon encountering a const function call.
308 2. After reload, the stores related to outgoing arguments can be
309 either stack pointer or hard frame pointer based. This means
310 that we have no other choice than also killing all the frame
311 pointer based stores upon encountering a const function call.
312 This field is set after reload for const function calls. Having
313 this set is less severe than a wild read, it just means that all
314 the frame related stores are killed rather than all the stores. */
315 bool frame_read;
317 /* This field is only used for the processing of const functions.
318 It is set if the insn may contain a stack pointer based store. */
319 bool stack_pointer_based;
321 /* This is true if any of the sets within the store contains a
322 cselib base. Such stores can only be deleted by the local
323 algorithm. */
324 bool contains_cselib_groups;
326 /* The insn. */
327 rtx insn;
329 /* The list of mem sets or mem clobbers that are contained in this
330 insn. If the insn is deletable, it contains only one mem set.
331 But it could also contain clobbers. Insns that contain more than
332 one mem set are not deletable, but each of those mems are here in
333 order to provide info to delete other insns. */
334 store_info_t store_rec;
336 /* The linked list of mem uses in this insn. Only the reads from
337 rtx bases are listed here. The reads to cselib bases are
338 completely processed during the first scan and so are never
339 created. */
340 read_info_t read_rec;
342 /* The prev insn in the basic block. */
343 struct insn_info * prev_insn;
345 /* The linked list of insns that are in consideration for removal in
346 the forwards pass thru the basic block. This pointer may be
347 trash as it is not cleared when a wild read occurs. The only
348 time it is guaranteed to be correct is when the traversal starts
349 at active_local_stores. */
350 struct insn_info * next_local_store;
353 typedef struct insn_info *insn_info_t;
354 static alloc_pool insn_info_pool;
356 /* The linked list of stores that are under consideration in this
357 basic block. */
358 static insn_info_t active_local_stores;
360 struct bb_info
363 /* Pointer to the insn info for the last insn in the block. These
364 are linked so this is how all of the insns are reached. During
365 scanning this is the current insn being scanned. */
366 insn_info_t last_insn;
368 /* The info for the global dataflow problem. */
371 /* This is set if the transfer function should and in the wild_read
372 bitmap before applying the kill and gen sets. That vector knocks
373 out most of the bits in the bitmap and thus speeds up the
374 operations. */
375 bool apply_wild_read;
377 /* The set of store positions that exist in this block before a wild read. */
378 bitmap gen;
380 /* The set of load positions that exist in this block above the
381 same position of a store. */
382 bitmap kill;
384 /* The set of stores that reach the top of the block without being
385 killed by a read.
387 Do not represent the in if it is all ones. Note that this is
388 what the bitvector should logically be initialized to for a set
389 intersection problem. However, like the kill set, this is too
390 expensive. So initially, the in set will only be created for the
391 exit block and any block that contains a wild read. */
392 bitmap in;
394 /* The set of stores that reach the bottom of the block from it's
395 successors.
397 Do not represent the in if it is all ones. Note that this is
398 what the bitvector should logically be initialized to for a set
399 intersection problem. However, like the kill and in set, this is
400 too expensive. So what is done is that the confluence operator
401 just initializes the vector from one of the out sets of the
402 successors of the block. */
403 bitmap out;
406 typedef struct bb_info *bb_info_t;
407 static alloc_pool bb_info_pool;
409 /* Table to hold all bb_infos. */
410 static bb_info_t *bb_table;
412 /* There is a group_info for each rtx base that is used to reference
413 memory. There are also not many of the rtx bases because they are
414 very limited in scope. */
416 struct group_info
418 /* The actual base of the address. */
419 rtx rtx_base;
421 /* The sequential id of the base. This allows us to have a
422 canonical ordering of these that is not based on addresses. */
423 int id;
425 /* A mem wrapped around the base pointer for the group in order to
426 do read dependency. */
427 rtx base_mem;
429 /* Canonized version of base_mem, most likely the same thing. */
430 rtx canon_base_mem;
432 /* These two sets of two bitmaps are used to keep track of how many
433 stores are actually referencing that position from this base. We
434 only do this for rtx bases as this will be used to assign
435 positions in the bitmaps for the global problem. Bit N is set in
436 store1 on the first store for offset N. Bit N is set in store2
437 for the second store to offset N. This is all we need since we
438 only care about offsets that have two or more stores for them.
440 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
441 for 0 and greater offsets.
443 There is one special case here, for stores into the stack frame,
444 we will or store1 into store2 before deciding which stores look
445 at globally. This is because stores to the stack frame that have
446 no other reads before the end of the function can also be
447 deleted. */
448 bitmap store1_n, store1_p, store2_n, store2_p;
450 /* The positions in this bitmap have the same assignments as the in,
451 out, gen and kill bitmaps. This bitmap is all zeros except for
452 the positions that are occupied by stores for this group. */
453 bitmap group_kill;
455 /* True if there are any positions that are to be processed
456 globally. */
457 bool process_globally;
459 /* True if the base of this group is either the frame_pointer or
460 hard_frame_pointer. */
461 bool frame_related;
463 /* The offset_map is used to map the offsets from this base into
464 positions in the global bitmaps. It is only created after all of
465 the all of stores have been scanned and we know which ones we
466 care about. */
467 int *offset_map_n, *offset_map_p;
468 int offset_map_size_n, offset_map_size_p;
470 typedef struct group_info *group_info_t;
471 typedef const struct group_info *const_group_info_t;
472 static alloc_pool rtx_group_info_pool;
474 /* Tables of group_info structures, hashed by base value. */
475 static htab_t rtx_group_table;
477 /* Index into the rtx_group_vec. */
478 static int rtx_group_next_id;
480 DEF_VEC_P(group_info_t);
481 DEF_VEC_ALLOC_P(group_info_t,heap);
483 static VEC(group_info_t,heap) *rtx_group_vec;
486 /* This structure holds the set of changes that are being deferred
487 when removing read operation. See replace_read. */
488 struct deferred_change
491 /* The mem that is being replaced. */
492 rtx *loc;
494 /* The reg it is being replaced with. */
495 rtx reg;
497 struct deferred_change *next;
500 typedef struct deferred_change *deferred_change_t;
501 static alloc_pool deferred_change_pool;
503 static deferred_change_t deferred_change_list = NULL;
505 /* This are used to hold the alias sets of spill variables. Since
506 these are never aliased and there may be a lot of them, it makes
507 sense to treat them specially. This bitvector is only allocated in
508 calls from dse_record_singleton_alias_set which currently is only
509 made during reload1. So when dse is called before reload this
510 mechanism does nothing. */
512 static bitmap clear_alias_sets = NULL;
514 /* The set of clear_alias_sets that have been disqualified because
515 there are loads or stores using a different mode than the alias set
516 was registered with. */
517 static bitmap disqualified_clear_alias_sets = NULL;
519 /* The group that holds all of the clear_alias_sets. */
520 static group_info_t clear_alias_group;
522 /* The modes of the clear_alias_sets. */
523 static htab_t clear_alias_mode_table;
525 /* Hash table element to look up the mode for an alias set. */
526 struct clear_alias_mode_holder
528 alias_set_type alias_set;
529 enum machine_mode mode;
532 static alloc_pool clear_alias_mode_pool;
534 /* This is true except if cfun->stdarg -- i.e. we cannot do
535 this for vararg functions because they play games with the frame. */
536 static bool stores_off_frame_dead_at_return;
538 /* Counter for stats. */
539 static int globally_deleted;
540 static int locally_deleted;
541 static int spill_deleted;
543 static bitmap all_blocks;
545 /* The number of bits used in the global bitmaps. */
546 static unsigned int current_position;
549 static bool gate_dse (void);
550 static bool gate_dse1 (void);
551 static bool gate_dse2 (void);
554 /*----------------------------------------------------------------------------
555 Zeroth step.
557 Initialization.
558 ----------------------------------------------------------------------------*/
560 /* Hashtable callbacks for maintaining the "bases" field of
561 store_group_info, given that the addresses are function invariants. */
563 static int
564 clear_alias_mode_eq (const void *p1, const void *p2)
566 const struct clear_alias_mode_holder * h1
567 = (const struct clear_alias_mode_holder *) p1;
568 const struct clear_alias_mode_holder * h2
569 = (const struct clear_alias_mode_holder *) p2;
570 return h1->alias_set == h2->alias_set;
574 static hashval_t
575 clear_alias_mode_hash (const void *p)
577 const struct clear_alias_mode_holder *holder
578 = (const struct clear_alias_mode_holder *) p;
579 return holder->alias_set;
583 /* Find the entry associated with ALIAS_SET. */
585 static struct clear_alias_mode_holder *
586 clear_alias_set_lookup (alias_set_type alias_set)
588 struct clear_alias_mode_holder tmp_holder;
589 void **slot;
591 tmp_holder.alias_set = alias_set;
592 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
593 gcc_assert (*slot);
595 return (struct clear_alias_mode_holder *) *slot;
599 /* Hashtable callbacks for maintaining the "bases" field of
600 store_group_info, given that the addresses are function invariants. */
602 static int
603 invariant_group_base_eq (const void *p1, const void *p2)
605 const_group_info_t gi1 = (const_group_info_t) p1;
606 const_group_info_t gi2 = (const_group_info_t) p2;
607 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
611 static hashval_t
612 invariant_group_base_hash (const void *p)
614 const_group_info_t gi = (const_group_info_t) p;
615 int do_not_record;
616 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
620 /* Get the GROUP for BASE. Add a new group if it is not there. */
622 static group_info_t
623 get_group_info (rtx base)
625 struct group_info tmp_gi;
626 group_info_t gi;
627 void **slot;
629 if (base)
631 /* Find the store_base_info structure for BASE, creating a new one
632 if necessary. */
633 tmp_gi.rtx_base = base;
634 slot = htab_find_slot (rtx_group_table, &tmp_gi, INSERT);
635 gi = (group_info_t) *slot;
637 else
639 if (!clear_alias_group)
641 clear_alias_group = gi =
642 (group_info_t) pool_alloc (rtx_group_info_pool);
643 memset (gi, 0, sizeof (struct group_info));
644 gi->id = rtx_group_next_id++;
645 gi->store1_n = BITMAP_ALLOC (NULL);
646 gi->store1_p = BITMAP_ALLOC (NULL);
647 gi->store2_n = BITMAP_ALLOC (NULL);
648 gi->store2_p = BITMAP_ALLOC (NULL);
649 gi->group_kill = BITMAP_ALLOC (NULL);
650 gi->process_globally = false;
651 gi->offset_map_size_n = 0;
652 gi->offset_map_size_p = 0;
653 gi->offset_map_n = NULL;
654 gi->offset_map_p = NULL;
655 VEC_safe_push (group_info_t, heap, rtx_group_vec, gi);
657 return clear_alias_group;
660 if (gi == NULL)
662 *slot = gi = (group_info_t) pool_alloc (rtx_group_info_pool);
663 gi->rtx_base = base;
664 gi->id = rtx_group_next_id++;
665 gi->base_mem = gen_rtx_MEM (QImode, base);
666 gi->canon_base_mem = canon_rtx (gi->base_mem);
667 gi->store1_n = BITMAP_ALLOC (NULL);
668 gi->store1_p = BITMAP_ALLOC (NULL);
669 gi->store2_n = BITMAP_ALLOC (NULL);
670 gi->store2_p = BITMAP_ALLOC (NULL);
671 gi->group_kill = BITMAP_ALLOC (NULL);
672 gi->process_globally = false;
673 gi->frame_related =
674 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
675 gi->offset_map_size_n = 0;
676 gi->offset_map_size_p = 0;
677 gi->offset_map_n = NULL;
678 gi->offset_map_p = NULL;
679 VEC_safe_push (group_info_t, heap, rtx_group_vec, gi);
682 return gi;
686 /* Initialization of data structures. */
688 static void
689 dse_step0 (void)
691 locally_deleted = 0;
692 globally_deleted = 0;
693 spill_deleted = 0;
695 scratch = BITMAP_ALLOC (NULL);
697 rtx_store_info_pool
698 = create_alloc_pool ("rtx_store_info_pool",
699 sizeof (struct store_info), 100);
700 read_info_pool
701 = create_alloc_pool ("read_info_pool",
702 sizeof (struct read_info), 100);
703 insn_info_pool
704 = create_alloc_pool ("insn_info_pool",
705 sizeof (struct insn_info), 100);
706 bb_info_pool
707 = create_alloc_pool ("bb_info_pool",
708 sizeof (struct bb_info), 100);
709 rtx_group_info_pool
710 = create_alloc_pool ("rtx_group_info_pool",
711 sizeof (struct group_info), 100);
712 deferred_change_pool
713 = create_alloc_pool ("deferred_change_pool",
714 sizeof (struct deferred_change), 10);
716 rtx_group_table = htab_create (11, invariant_group_base_hash,
717 invariant_group_base_eq, NULL);
719 bb_table = XCNEWVEC (bb_info_t, last_basic_block);
720 rtx_group_next_id = 0;
722 stores_off_frame_dead_at_return = !cfun->stdarg;
724 init_alias_analysis ();
726 if (clear_alias_sets)
727 clear_alias_group = get_group_info (NULL);
728 else
729 clear_alias_group = NULL;
734 /*----------------------------------------------------------------------------
735 First step.
737 Scan all of the insns. Any random ordering of the blocks is fine.
738 Each block is scanned in forward order to accommodate cselib which
739 is used to remove stores with non-constant bases.
740 ----------------------------------------------------------------------------*/
742 /* Delete all of the store_info recs from INSN_INFO. */
744 static void
745 free_store_info (insn_info_t insn_info)
747 store_info_t store_info = insn_info->store_rec;
748 while (store_info)
750 store_info_t next = store_info->next;
751 if (store_info->cse_base)
752 pool_free (cse_store_info_pool, store_info);
753 else
754 pool_free (rtx_store_info_pool, store_info);
755 store_info = next;
758 insn_info->cannot_delete = true;
759 insn_info->contains_cselib_groups = false;
760 insn_info->store_rec = NULL;
764 struct insn_size {
765 int size;
766 rtx insn;
770 /* Add an insn to do the add inside a x if it is a
771 PRE/POST-INC/DEC/MODIFY. D is an structure containing the insn and
772 the size of the mode of the MEM that this is inside of. */
774 static int
775 replace_inc_dec (rtx *r, void *d)
777 rtx x = *r;
778 struct insn_size *data = (struct insn_size *)d;
779 switch (GET_CODE (x))
781 case PRE_INC:
782 case POST_INC:
784 rtx r1 = XEXP (x, 0);
785 rtx c = gen_int_mode (Pmode, data->size);
786 emit_insn_before (gen_rtx_SET (Pmode, r1,
787 gen_rtx_PLUS (Pmode, r1, c)),
788 data->insn);
789 return -1;
792 case PRE_DEC:
793 case POST_DEC:
795 rtx r1 = XEXP (x, 0);
796 rtx c = gen_int_mode (Pmode, -data->size);
797 emit_insn_before (gen_rtx_SET (Pmode, r1,
798 gen_rtx_PLUS (Pmode, r1, c)),
799 data->insn);
800 return -1;
803 case PRE_MODIFY:
804 case POST_MODIFY:
806 /* We can reuse the add because we are about to delete the
807 insn that contained it. */
808 rtx add = XEXP (x, 0);
809 rtx r1 = XEXP (add, 0);
810 emit_insn_before (gen_rtx_SET (Pmode, r1, add), data->insn);
811 return -1;
814 default:
815 return 0;
820 /* If X is a MEM, check the address to see if it is PRE/POST-INC/DEC/MODIFY
821 and generate an add to replace that. */
823 static int
824 replace_inc_dec_mem (rtx *r, void *d)
826 rtx x = *r;
827 if (x != NULL_RTX && MEM_P (x))
829 struct insn_size data;
831 data.size = GET_MODE_SIZE (GET_MODE (x));
832 data.insn = (rtx) d;
834 for_each_rtx (&XEXP (x, 0), replace_inc_dec, &data);
836 return -1;
838 return 0;
841 /* Before we delete INSN, make sure that the auto inc/dec, if it is
842 there, is split into a separate insn. */
844 static void
845 check_for_inc_dec (rtx insn)
847 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
848 if (note)
849 for_each_rtx (&insn, replace_inc_dec_mem, insn);
853 /* Delete the insn and free all of the fields inside INSN_INFO. */
855 static void
856 delete_dead_store_insn (insn_info_t insn_info)
858 read_info_t read_info;
860 if (!dbg_cnt (dse))
861 return;
863 check_for_inc_dec (insn_info->insn);
864 if (dump_file)
866 fprintf (dump_file, "Locally deleting insn %d ",
867 INSN_UID (insn_info->insn));
868 if (insn_info->store_rec->alias_set)
869 fprintf (dump_file, "alias set %d\n",
870 (int) insn_info->store_rec->alias_set);
871 else
872 fprintf (dump_file, "\n");
875 free_store_info (insn_info);
876 read_info = insn_info->read_rec;
878 while (read_info)
880 read_info_t next = read_info->next;
881 pool_free (read_info_pool, read_info);
882 read_info = next;
884 insn_info->read_rec = NULL;
886 delete_insn (insn_info->insn);
887 locally_deleted++;
888 insn_info->insn = NULL;
890 insn_info->wild_read = false;
894 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
895 OFFSET and WIDTH. */
897 static void
898 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width)
900 HOST_WIDE_INT i;
902 if ((offset > -MAX_OFFSET) && (offset < MAX_OFFSET))
903 for (i=offset; i<offset+width; i++)
905 bitmap store1;
906 bitmap store2;
907 int ai;
908 if (i < 0)
910 store1 = group->store1_n;
911 store2 = group->store2_n;
912 ai = -i;
914 else
916 store1 = group->store1_p;
917 store2 = group->store2_p;
918 ai = i;
921 if (bitmap_bit_p (store1, ai))
922 bitmap_set_bit (store2, ai);
923 else
925 bitmap_set_bit (store1, ai);
926 if (i < 0)
928 if (group->offset_map_size_n < ai)
929 group->offset_map_size_n = ai;
931 else
933 if (group->offset_map_size_p < ai)
934 group->offset_map_size_p = ai;
941 /* Set the BB_INFO so that the last insn is marked as a wild read. */
943 static void
944 add_wild_read (bb_info_t bb_info)
946 insn_info_t insn_info = bb_info->last_insn;
947 read_info_t *ptr = &insn_info->read_rec;
949 while (*ptr)
951 read_info_t next = (*ptr)->next;
952 if ((*ptr)->alias_set == 0)
954 pool_free (read_info_pool, *ptr);
955 *ptr = next;
957 else
958 ptr = &(*ptr)->next;
960 insn_info->wild_read = true;
961 active_local_stores = NULL;
965 /* Return true if X is a constant or one of the registers that behave
966 as a constant over the life of a function. This is equivalent to
967 !rtx_varies_p for memory addresses. */
969 static bool
970 const_or_frame_p (rtx x)
972 switch (GET_CODE (x))
974 case MEM:
975 return MEM_READONLY_P (x);
977 case CONST:
978 case CONST_INT:
979 case CONST_DOUBLE:
980 case CONST_VECTOR:
981 case SYMBOL_REF:
982 case LABEL_REF:
983 return true;
985 case REG:
986 /* Note that we have to test for the actual rtx used for the frame
987 and arg pointers and not just the register number in case we have
988 eliminated the frame and/or arg pointer and are using it
989 for pseudos. */
990 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
991 /* The arg pointer varies if it is not a fixed register. */
992 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
993 || x == pic_offset_table_rtx)
994 return true;
995 return false;
997 default:
998 return false;
1002 /* Take all reasonable action to put the address of MEM into the form
1003 that we can do analysis on.
1005 The gold standard is to get the address into the form: address +
1006 OFFSET where address is something that rtx_varies_p considers a
1007 constant. When we can get the address in this form, we can do
1008 global analysis on it. Note that for constant bases, address is
1009 not actually returned, only the group_id. The address can be
1010 obtained from that.
1012 If that fails, we try cselib to get a value we can at least use
1013 locally. If that fails we return false.
1015 The GROUP_ID is set to -1 for cselib bases and the index of the
1016 group for non_varying bases.
1018 FOR_READ is true if this is a mem read and false if not. */
1020 static bool
1021 canon_address (rtx mem,
1022 alias_set_type *alias_set_out,
1023 int *group_id,
1024 HOST_WIDE_INT *offset,
1025 cselib_val **base)
1027 rtx mem_address = XEXP (mem, 0);
1028 rtx expanded_address, address;
1029 /* Make sure that cselib is has initialized all of the operands of
1030 the address before asking it to do the subst. */
1032 if (clear_alias_sets)
1034 /* If this is a spill, do not do any further processing. */
1035 alias_set_type alias_set = MEM_ALIAS_SET (mem);
1036 if (dump_file)
1037 fprintf (dump_file, "found alias set %d\n", (int) alias_set);
1038 if (bitmap_bit_p (clear_alias_sets, alias_set))
1040 struct clear_alias_mode_holder *entry
1041 = clear_alias_set_lookup (alias_set);
1043 /* If the modes do not match, we cannot process this set. */
1044 if (entry->mode != GET_MODE (mem))
1046 if (dump_file)
1047 fprintf (dump_file,
1048 "disqualifying alias set %d, (%s) != (%s)\n",
1049 (int) alias_set, GET_MODE_NAME (entry->mode),
1050 GET_MODE_NAME (GET_MODE (mem)));
1052 bitmap_set_bit (disqualified_clear_alias_sets, alias_set);
1053 return false;
1056 *alias_set_out = alias_set;
1057 *group_id = clear_alias_group->id;
1058 return true;
1062 *alias_set_out = 0;
1064 cselib_lookup (mem_address, Pmode, 1);
1066 if (dump_file)
1068 fprintf (dump_file, " mem: ");
1069 print_inline_rtx (dump_file, mem_address, 0);
1070 fprintf (dump_file, "\n");
1073 /* Use cselib to replace all of the reg references with the full
1074 expression. This will take care of the case where we have
1076 r_x = base + offset;
1077 val = *r_x;
1079 by making it into
1081 val = *(base + offset);
1084 expanded_address = cselib_expand_value_rtx (mem_address, scratch, 5);
1086 /* If this fails, just go with the mem_address. */
1087 if (!expanded_address)
1088 expanded_address = mem_address;
1090 /* Split the address into canonical BASE + OFFSET terms. */
1091 address = canon_rtx (expanded_address);
1093 *offset = 0;
1095 if (dump_file)
1097 fprintf (dump_file, "\n after cselib_expand address: ");
1098 print_inline_rtx (dump_file, expanded_address, 0);
1099 fprintf (dump_file, "\n");
1101 fprintf (dump_file, "\n after canon_rtx address: ");
1102 print_inline_rtx (dump_file, address, 0);
1103 fprintf (dump_file, "\n");
1106 if (GET_CODE (address) == CONST)
1107 address = XEXP (address, 0);
1109 if (GET_CODE (address) == PLUS && GET_CODE (XEXP (address, 1)) == CONST_INT)
1111 *offset = INTVAL (XEXP (address, 1));
1112 address = XEXP (address, 0);
1115 if (const_or_frame_p (address))
1117 group_info_t group = get_group_info (address);
1119 if (dump_file)
1120 fprintf (dump_file, " gid=%d offset=%d \n", group->id, (int)*offset);
1121 *base = NULL;
1122 *group_id = group->id;
1124 else
1126 *base = cselib_lookup (address, Pmode, true);
1127 *group_id = -1;
1129 if (*base == NULL)
1131 if (dump_file)
1132 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1133 return false;
1135 if (dump_file)
1136 fprintf (dump_file, " varying cselib base=%d offset = %d\n",
1137 (*base)->value, (int)*offset);
1139 return true;
1143 /* Clear the rhs field from the active_local_stores array. */
1145 static void
1146 clear_rhs_from_active_local_stores (void)
1148 insn_info_t ptr = active_local_stores;
1150 while (ptr)
1152 store_info_t store_info = ptr->store_rec;
1153 /* Skip the clobbers. */
1154 while (!store_info->is_set)
1155 store_info = store_info->next;
1157 store_info->rhs = NULL;
1159 ptr = ptr->next_local_store;
1164 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1165 there is a candidate store, after adding it to the appropriate
1166 local store group if so. */
1168 static int
1169 record_store (rtx body, bb_info_t bb_info)
1171 rtx mem;
1172 HOST_WIDE_INT offset = 0;
1173 HOST_WIDE_INT width = 0;
1174 alias_set_type spill_alias_set;
1175 insn_info_t insn_info = bb_info->last_insn;
1176 store_info_t store_info = NULL;
1177 int group_id;
1178 cselib_val *base = NULL;
1179 insn_info_t ptr, last;
1180 bool store_is_unused;
1182 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1183 return 0;
1185 /* If this is not used, then this cannot be used to keep the insn
1186 from being deleted. On the other hand, it does provide something
1187 that can be used to prove that another store is dead. */
1188 store_is_unused
1189 = (find_reg_note (insn_info->insn, REG_UNUSED, body) != NULL);
1191 /* Check whether that value is a suitable memory location. */
1192 mem = SET_DEST (body);
1193 if (!MEM_P (mem))
1195 /* If the set or clobber is unused, then it does not effect our
1196 ability to get rid of the entire insn. */
1197 if (!store_is_unused)
1198 insn_info->cannot_delete = true;
1199 return 0;
1202 /* At this point we know mem is a mem. */
1203 if (GET_MODE (mem) == BLKmode)
1205 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1207 if (dump_file)
1208 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1209 add_wild_read (bb_info);
1210 insn_info->cannot_delete = true;
1212 else if (!store_is_unused)
1214 /* If the set or clobber is unused, then it does not effect our
1215 ability to get rid of the entire insn. */
1216 insn_info->cannot_delete = true;
1217 clear_rhs_from_active_local_stores ();
1219 return 0;
1222 /* We can still process a volatile mem, we just cannot delete it. */
1223 if (MEM_VOLATILE_P (mem))
1224 insn_info->cannot_delete = true;
1226 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1228 clear_rhs_from_active_local_stores ();
1229 return 0;
1232 width = GET_MODE_SIZE (GET_MODE (mem));
1234 if (spill_alias_set)
1236 bitmap store1 = clear_alias_group->store1_p;
1237 bitmap store2 = clear_alias_group->store2_p;
1239 if (bitmap_bit_p (store1, spill_alias_set))
1240 bitmap_set_bit (store2, spill_alias_set);
1241 else
1242 bitmap_set_bit (store1, spill_alias_set);
1244 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1245 clear_alias_group->offset_map_size_p = spill_alias_set;
1247 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1249 if (dump_file)
1250 fprintf (dump_file, " processing spill store %d(%s)\n",
1251 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1253 else if (group_id >= 0)
1255 /* In the restrictive case where the base is a constant or the
1256 frame pointer we can do global analysis. */
1258 group_info_t group
1259 = VEC_index (group_info_t, rtx_group_vec, group_id);
1261 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1262 set_usage_bits (group, offset, width);
1264 if (dump_file)
1265 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1266 group_id, (int)offset, (int)(offset+width));
1268 else
1270 rtx base_term = find_base_term (XEXP (mem, 0));
1271 if (!base_term
1272 || (GET_CODE (base_term) == ADDRESS
1273 && GET_MODE (base_term) == Pmode
1274 && XEXP (base_term, 0) == stack_pointer_rtx))
1275 insn_info->stack_pointer_based = true;
1276 insn_info->contains_cselib_groups = true;
1278 store_info = (store_info_t) pool_alloc (cse_store_info_pool);
1279 group_id = -1;
1281 if (dump_file)
1282 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1283 (int)offset, (int)(offset+width));
1286 /* Check to see if this stores causes some other stores to be
1287 dead. */
1288 ptr = active_local_stores;
1289 last = NULL;
1291 while (ptr)
1293 insn_info_t next = ptr->next_local_store;
1294 store_info_t s_info = ptr->store_rec;
1295 bool del = true;
1297 /* Skip the clobbers. We delete the active insn if this insn
1298 shadows the set. To have been put on the active list, it
1299 has exactly on set. */
1300 while (!s_info->is_set)
1301 s_info = s_info->next;
1303 if (s_info->alias_set != spill_alias_set)
1304 del = false;
1305 else if (s_info->alias_set)
1307 struct clear_alias_mode_holder *entry
1308 = clear_alias_set_lookup (s_info->alias_set);
1309 /* Generally, spills cannot be processed if and of the
1310 references to the slot have a different mode. But if
1311 we are in the same block and mode is exactly the same
1312 between this store and one before in the same block,
1313 we can still delete it. */
1314 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1315 && (GET_MODE (mem) == entry->mode))
1317 del = true;
1318 s_info->positions_needed = (unsigned HOST_WIDE_INT) 0;
1320 if (dump_file)
1321 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1322 INSN_UID (ptr->insn), (int) s_info->alias_set);
1324 else if ((s_info->group_id == group_id)
1325 && (s_info->cse_base == base))
1327 HOST_WIDE_INT i;
1328 if (dump_file)
1329 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1330 INSN_UID (ptr->insn), s_info->group_id,
1331 (int)s_info->begin, (int)s_info->end);
1332 for (i = offset; i < offset+width; i++)
1333 if (i >= s_info->begin && i < s_info->end)
1334 s_info->positions_needed
1335 &= ~(((unsigned HOST_WIDE_INT) 1) << (i - s_info->begin));
1337 else if (s_info->rhs)
1338 /* Need to see if it is possible for this store to overwrite
1339 the value of store_info. If it is, set the rhs to NULL to
1340 keep it from being used to remove a load. */
1342 if (canon_true_dependence (s_info->mem,
1343 GET_MODE (s_info->mem),
1344 s_info->mem_addr,
1345 mem, rtx_varies_p))
1346 s_info->rhs = NULL;
1349 /* An insn can be deleted if every position of every one of
1350 its s_infos is zero. */
1351 if (s_info->positions_needed != (unsigned HOST_WIDE_INT) 0)
1352 del = false;
1354 if (del)
1356 insn_info_t insn_to_delete = ptr;
1358 if (last)
1359 last->next_local_store = ptr->next_local_store;
1360 else
1361 active_local_stores = ptr->next_local_store;
1363 delete_dead_store_insn (insn_to_delete);
1365 else
1366 last = ptr;
1368 ptr = next;
1371 gcc_assert ((unsigned) width <= HOST_BITS_PER_WIDE_INT);
1373 /* Finish filling in the store_info. */
1374 store_info->next = insn_info->store_rec;
1375 insn_info->store_rec = store_info;
1376 store_info->mem = canon_rtx (mem);
1377 store_info->alias_set = spill_alias_set;
1378 store_info->mem_addr = get_addr (XEXP (mem, 0));
1379 store_info->cse_base = base;
1380 store_info->positions_needed = lowpart_bitmask (width);
1381 store_info->group_id = group_id;
1382 store_info->begin = offset;
1383 store_info->end = offset + width;
1384 store_info->is_set = GET_CODE (body) == SET;
1386 if (store_info->is_set
1387 /* No place to keep the value after ra. */
1388 && !reload_completed
1389 && (REG_P (SET_SRC (body))
1390 || GET_CODE (SET_SRC (body)) == SUBREG
1391 || CONSTANT_P (SET_SRC (body)))
1392 /* Sometimes the store and reload is used for truncation and
1393 rounding. */
1394 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1395 store_info->rhs = SET_SRC (body);
1396 else
1397 store_info->rhs = NULL;
1399 /* If this is a clobber, we return 0. We will only be able to
1400 delete this insn if there is only one store USED store, but we
1401 can use the clobber to delete other stores earlier. */
1402 return store_info->is_set ? 1 : 0;
1406 static void
1407 dump_insn_info (const char * start, insn_info_t insn_info)
1409 fprintf (dump_file, "%s insn=%d %s\n", start,
1410 INSN_UID (insn_info->insn),
1411 insn_info->store_rec ? "has store" : "naked");
1415 /* If the modes are different and the value's source and target do not
1416 line up, we need to extract the value from lower part of the rhs of
1417 the store, shift it, and then put it into a form that can be shoved
1418 into the read_insn. This function generates a right SHIFT of a
1419 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1420 shift sequence is returned or NULL if we failed to find a
1421 shift. */
1423 static rtx
1424 find_shift_sequence (int access_size,
1425 store_info_t store_info,
1426 read_info_t read_info,
1427 int shift,
1428 bool speed)
1430 enum machine_mode store_mode = GET_MODE (store_info->mem);
1431 enum machine_mode read_mode = GET_MODE (read_info->mem);
1432 enum machine_mode new_mode;
1433 rtx read_reg = NULL;
1435 /* Some machines like the x86 have shift insns for each size of
1436 operand. Other machines like the ppc or the ia-64 may only have
1437 shift insns that shift values within 32 or 64 bit registers.
1438 This loop tries to find the smallest shift insn that will right
1439 justify the value we want to read but is available in one insn on
1440 the machine. */
1442 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1443 MODE_INT);
1444 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1445 new_mode = GET_MODE_WIDER_MODE (new_mode))
1447 rtx target, new_reg, shift_seq, insn, new_lhs;
1448 int cost;
1450 /* If a constant was stored into memory, try to simplify it here,
1451 otherwise the cost of the shift might preclude this optimization
1452 e.g. at -Os, even when no actual shift will be needed. */
1453 if (CONSTANT_P (store_info->rhs))
1455 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1456 rtx ret = simplify_subreg (new_mode, store_info->rhs, store_mode,
1457 byte);
1458 if (ret && CONSTANT_P (ret))
1460 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1461 ret, GEN_INT (shift));
1462 if (ret && CONSTANT_P (ret))
1464 byte = subreg_lowpart_offset (read_mode, new_mode);
1465 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1466 if (ret && CONSTANT_P (ret)
1467 && rtx_cost (ret, SET, speed) <= COSTS_N_INSNS (1))
1468 return ret;
1473 /* Try a wider mode if truncating the store mode to NEW_MODE
1474 requires a real instruction. */
1475 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1476 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (new_mode),
1477 GET_MODE_BITSIZE (store_mode)))
1478 continue;
1480 /* Also try a wider mode if the necessary punning is either not
1481 desirable or not possible. */
1482 if (!CONSTANT_P (store_info->rhs)
1483 && !MODES_TIEABLE_P (new_mode, store_mode))
1484 continue;
1486 new_reg = gen_reg_rtx (new_mode);
1488 start_sequence ();
1490 /* In theory we could also check for an ashr. Ian Taylor knows
1491 of one dsp where the cost of these two was not the same. But
1492 this really is a rare case anyway. */
1493 target = expand_binop (new_mode, lshr_optab, new_reg,
1494 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1496 shift_seq = get_insns ();
1497 end_sequence ();
1499 if (target != new_reg || shift_seq == NULL)
1500 continue;
1502 cost = 0;
1503 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1504 if (INSN_P (insn))
1505 cost += insn_rtx_cost (PATTERN (insn), speed);
1507 /* The computation up to here is essentially independent
1508 of the arguments and could be precomputed. It may
1509 not be worth doing so. We could precompute if
1510 worthwhile or at least cache the results. The result
1511 technically depends on both SHIFT and ACCESS_SIZE,
1512 but in practice the answer will depend only on ACCESS_SIZE. */
1514 if (cost > COSTS_N_INSNS (1))
1515 continue;
1517 new_lhs = extract_low_bits (new_mode, store_mode,
1518 copy_rtx (store_info->rhs));
1519 if (new_lhs == NULL_RTX)
1520 continue;
1522 /* We found an acceptable shift. Generate a move to
1523 take the value from the store and put it into the
1524 shift pseudo, then shift it, then generate another
1525 move to put in into the target of the read. */
1526 emit_move_insn (new_reg, new_lhs);
1527 emit_insn (shift_seq);
1528 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1529 break;
1532 return read_reg;
1536 /* Take a sequence of:
1537 A <- r1
1539 ... <- A
1541 and change it into
1542 r2 <- r1
1543 A <- r1
1545 ... <- r2
1549 r3 <- extract (r1)
1550 r3 <- r3 >> shift
1551 r2 <- extract (r3)
1552 ... <- r2
1556 r2 <- extract (r1)
1557 ... <- r2
1559 Depending on the alignment and the mode of the store and
1560 subsequent load.
1563 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1564 and READ_INSN are for the read. Return true if the replacement
1565 went ok. */
1567 static bool
1568 replace_read (store_info_t store_info, insn_info_t store_insn,
1569 read_info_t read_info, insn_info_t read_insn, rtx *loc)
1571 enum machine_mode store_mode = GET_MODE (store_info->mem);
1572 enum machine_mode read_mode = GET_MODE (read_info->mem);
1573 int shift;
1574 int access_size; /* In bytes. */
1575 rtx insns, read_reg;
1577 if (!dbg_cnt (dse))
1578 return false;
1580 /* To get here the read is within the boundaries of the write so
1581 shift will never be negative. Start out with the shift being in
1582 bytes. */
1583 if (BYTES_BIG_ENDIAN)
1584 shift = store_info->end - read_info->end;
1585 else
1586 shift = read_info->begin - store_info->begin;
1588 access_size = shift + GET_MODE_SIZE (read_mode);
1590 /* From now on it is bits. */
1591 shift *= BITS_PER_UNIT;
1593 /* Create a sequence of instructions to set up the read register.
1594 This sequence goes immediately before the store and its result
1595 is read by the load.
1597 We need to keep this in perspective. We are replacing a read
1598 with a sequence of insns, but the read will almost certainly be
1599 in cache, so it is not going to be an expensive one. Thus, we
1600 are not willing to do a multi insn shift or worse a subroutine
1601 call to get rid of the read. */
1602 if (dump_file)
1603 fprintf (dump_file, "trying to replace %smode load in insn %d"
1604 " from %smode store in insn %d\n",
1605 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1606 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1607 start_sequence ();
1608 if (shift)
1609 read_reg = find_shift_sequence (access_size, store_info, read_info, shift,
1610 optimize_bb_for_speed_p (BLOCK_FOR_INSN (read_insn->insn)));
1611 else
1612 read_reg = extract_low_bits (read_mode, store_mode,
1613 copy_rtx (store_info->rhs));
1614 if (read_reg == NULL_RTX)
1616 end_sequence ();
1617 if (dump_file)
1618 fprintf (dump_file, " -- could not extract bits of stored value\n");
1619 return false;
1621 /* Force the value into a new register so that it won't be clobbered
1622 between the store and the load. */
1623 read_reg = copy_to_mode_reg (read_mode, read_reg);
1624 insns = get_insns ();
1625 end_sequence ();
1627 if (validate_change (read_insn->insn, loc, read_reg, 0))
1629 deferred_change_t deferred_change =
1630 (deferred_change_t) pool_alloc (deferred_change_pool);
1632 /* Insert this right before the store insn where it will be safe
1633 from later insns that might change it before the read. */
1634 emit_insn_before (insns, store_insn->insn);
1636 /* And now for the kludge part: cselib croaks if you just
1637 return at this point. There are two reasons for this:
1639 1) Cselib has an idea of how many pseudos there are and
1640 that does not include the new ones we just added.
1642 2) Cselib does not know about the move insn we added
1643 above the store_info, and there is no way to tell it
1644 about it, because it has "moved on".
1646 Problem (1) is fixable with a certain amount of engineering.
1647 Problem (2) is requires starting the bb from scratch. This
1648 could be expensive.
1650 So we are just going to have to lie. The move/extraction
1651 insns are not really an issue, cselib did not see them. But
1652 the use of the new pseudo read_insn is a real problem because
1653 cselib has not scanned this insn. The way that we solve this
1654 problem is that we are just going to put the mem back for now
1655 and when we are finished with the block, we undo this. We
1656 keep a table of mems to get rid of. At the end of the basic
1657 block we can put them back. */
1659 *loc = read_info->mem;
1660 deferred_change->next = deferred_change_list;
1661 deferred_change_list = deferred_change;
1662 deferred_change->loc = loc;
1663 deferred_change->reg = read_reg;
1665 /* Get rid of the read_info, from the point of view of the
1666 rest of dse, play like this read never happened. */
1667 read_insn->read_rec = read_info->next;
1668 pool_free (read_info_pool, read_info);
1669 if (dump_file)
1671 fprintf (dump_file, " -- replaced the loaded MEM with ");
1672 print_simple_rtl (dump_file, read_reg);
1673 fprintf (dump_file, "\n");
1675 return true;
1677 else
1679 if (dump_file)
1681 fprintf (dump_file, " -- replacing the loaded MEM with ");
1682 print_simple_rtl (dump_file, read_reg);
1683 fprintf (dump_file, " led to an invalid instruction\n");
1685 return false;
1689 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
1690 if LOC is a mem and if it is look at the address and kill any
1691 appropriate stores that may be active. */
1693 static int
1694 check_mem_read_rtx (rtx *loc, void *data)
1696 rtx mem = *loc;
1697 bb_info_t bb_info;
1698 insn_info_t insn_info;
1699 HOST_WIDE_INT offset = 0;
1700 HOST_WIDE_INT width = 0;
1701 alias_set_type spill_alias_set = 0;
1702 cselib_val *base = NULL;
1703 int group_id;
1704 read_info_t read_info;
1706 if (!mem || !MEM_P (mem))
1707 return 0;
1709 bb_info = (bb_info_t) data;
1710 insn_info = bb_info->last_insn;
1712 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
1713 || (MEM_VOLATILE_P (mem)))
1715 if (dump_file)
1716 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
1717 add_wild_read (bb_info);
1718 insn_info->cannot_delete = true;
1719 return 0;
1722 /* If it is reading readonly mem, then there can be no conflict with
1723 another write. */
1724 if (MEM_READONLY_P (mem))
1725 return 0;
1727 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1729 if (dump_file)
1730 fprintf (dump_file, " adding wild read, canon_address failure.\n");
1731 add_wild_read (bb_info);
1732 return 0;
1735 if (GET_MODE (mem) == BLKmode)
1736 width = -1;
1737 else
1738 width = GET_MODE_SIZE (GET_MODE (mem));
1740 read_info = (read_info_t) pool_alloc (read_info_pool);
1741 read_info->group_id = group_id;
1742 read_info->mem = mem;
1743 read_info->alias_set = spill_alias_set;
1744 read_info->begin = offset;
1745 read_info->end = offset + width;
1746 read_info->next = insn_info->read_rec;
1747 insn_info->read_rec = read_info;
1749 /* We ignore the clobbers in store_info. The is mildly aggressive,
1750 but there really should not be a clobber followed by a read. */
1752 if (spill_alias_set)
1754 insn_info_t i_ptr = active_local_stores;
1755 insn_info_t last = NULL;
1757 if (dump_file)
1758 fprintf (dump_file, " processing spill load %d\n",
1759 (int) spill_alias_set);
1761 while (i_ptr)
1763 store_info_t store_info = i_ptr->store_rec;
1765 /* Skip the clobbers. */
1766 while (!store_info->is_set)
1767 store_info = store_info->next;
1769 if (store_info->alias_set == spill_alias_set)
1771 if (dump_file)
1772 dump_insn_info ("removing from active", i_ptr);
1774 if (last)
1775 last->next_local_store = i_ptr->next_local_store;
1776 else
1777 active_local_stores = i_ptr->next_local_store;
1779 else
1780 last = i_ptr;
1781 i_ptr = i_ptr->next_local_store;
1784 else if (group_id >= 0)
1786 /* This is the restricted case where the base is a constant or
1787 the frame pointer and offset is a constant. */
1788 insn_info_t i_ptr = active_local_stores;
1789 insn_info_t last = NULL;
1791 if (dump_file)
1793 if (width == -1)
1794 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
1795 group_id);
1796 else
1797 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
1798 group_id, (int)offset, (int)(offset+width));
1801 while (i_ptr)
1803 bool remove = false;
1804 store_info_t store_info = i_ptr->store_rec;
1806 /* Skip the clobbers. */
1807 while (!store_info->is_set)
1808 store_info = store_info->next;
1810 /* There are three cases here. */
1811 if (store_info->group_id < 0)
1812 /* We have a cselib store followed by a read from a
1813 const base. */
1814 remove
1815 = canon_true_dependence (store_info->mem,
1816 GET_MODE (store_info->mem),
1817 store_info->mem_addr,
1818 mem, rtx_varies_p);
1820 else if (group_id == store_info->group_id)
1822 /* This is a block mode load. We may get lucky and
1823 canon_true_dependence may save the day. */
1824 if (width == -1)
1825 remove
1826 = canon_true_dependence (store_info->mem,
1827 GET_MODE (store_info->mem),
1828 store_info->mem_addr,
1829 mem, rtx_varies_p);
1831 /* If this read is just reading back something that we just
1832 stored, rewrite the read. */
1833 else
1835 if (store_info->rhs
1836 && (offset >= store_info->begin)
1837 && (offset + width <= store_info->end))
1839 unsigned HOST_WIDE_INT mask
1840 = (lowpart_bitmask (width)
1841 << (offset - store_info->begin));
1843 if ((store_info->positions_needed & mask) == mask
1844 && replace_read (store_info, i_ptr,
1845 read_info, insn_info, loc))
1846 return 0;
1848 /* The bases are the same, just see if the offsets
1849 overlap. */
1850 if ((offset < store_info->end)
1851 && (offset + width > store_info->begin))
1852 remove = true;
1856 /* else
1857 The else case that is missing here is that the
1858 bases are constant but different. There is nothing
1859 to do here because there is no overlap. */
1861 if (remove)
1863 if (dump_file)
1864 dump_insn_info ("removing from active", i_ptr);
1866 if (last)
1867 last->next_local_store = i_ptr->next_local_store;
1868 else
1869 active_local_stores = i_ptr->next_local_store;
1871 else
1872 last = i_ptr;
1873 i_ptr = i_ptr->next_local_store;
1876 else
1878 insn_info_t i_ptr = active_local_stores;
1879 insn_info_t last = NULL;
1880 if (dump_file)
1882 fprintf (dump_file, " processing cselib load mem:");
1883 print_inline_rtx (dump_file, mem, 0);
1884 fprintf (dump_file, "\n");
1887 while (i_ptr)
1889 bool remove = false;
1890 store_info_t store_info = i_ptr->store_rec;
1892 if (dump_file)
1893 fprintf (dump_file, " processing cselib load against insn %d\n",
1894 INSN_UID (i_ptr->insn));
1896 /* Skip the clobbers. */
1897 while (!store_info->is_set)
1898 store_info = store_info->next;
1900 /* If this read is just reading back something that we just
1901 stored, rewrite the read. */
1902 if (store_info->rhs
1903 && store_info->group_id == -1
1904 && store_info->cse_base == base
1905 && (offset >= store_info->begin)
1906 && (offset + width <= store_info->end))
1908 unsigned HOST_WIDE_INT mask
1909 = (lowpart_bitmask (width)
1910 << (offset - store_info->begin));
1912 if ((store_info->positions_needed & mask) == mask
1913 && replace_read (store_info, i_ptr,
1914 read_info, insn_info, loc))
1915 return 0;
1918 if (!store_info->alias_set)
1919 remove = canon_true_dependence (store_info->mem,
1920 GET_MODE (store_info->mem),
1921 store_info->mem_addr,
1922 mem, rtx_varies_p);
1924 if (remove)
1926 if (dump_file)
1927 dump_insn_info ("removing from active", i_ptr);
1929 if (last)
1930 last->next_local_store = i_ptr->next_local_store;
1931 else
1932 active_local_stores = i_ptr->next_local_store;
1934 else
1935 last = i_ptr;
1936 i_ptr = i_ptr->next_local_store;
1939 return 0;
1942 /* A for_each_rtx callback in which DATA points the INSN_INFO for
1943 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
1944 true for any part of *LOC. */
1946 static void
1947 check_mem_read_use (rtx *loc, void *data)
1949 for_each_rtx (loc, check_mem_read_rtx, data);
1952 /* Apply record_store to all candidate stores in INSN. Mark INSN
1953 if some part of it is not a candidate store and assigns to a
1954 non-register target. */
1956 static void
1957 scan_insn (bb_info_t bb_info, rtx insn)
1959 rtx body;
1960 insn_info_t insn_info = (insn_info_t) pool_alloc (insn_info_pool);
1961 int mems_found = 0;
1962 memset (insn_info, 0, sizeof (struct insn_info));
1964 if (dump_file)
1965 fprintf (dump_file, "\n**scanning insn=%d\n",
1966 INSN_UID (insn));
1968 insn_info->prev_insn = bb_info->last_insn;
1969 insn_info->insn = insn;
1970 bb_info->last_insn = insn_info;
1973 /* Cselib clears the table for this case, so we have to essentially
1974 do the same. */
1975 if (NONJUMP_INSN_P (insn)
1976 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
1977 && MEM_VOLATILE_P (PATTERN (insn)))
1979 add_wild_read (bb_info);
1980 insn_info->cannot_delete = true;
1981 return;
1984 /* Look at all of the uses in the insn. */
1985 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
1987 if (CALL_P (insn))
1989 insn_info->cannot_delete = true;
1991 /* Const functions cannot do anything bad i.e. read memory,
1992 however, they can read their parameters which may have
1993 been pushed onto the stack. */
1994 if (RTL_CONST_CALL_P (insn))
1996 insn_info_t i_ptr = active_local_stores;
1997 insn_info_t last = NULL;
1999 if (dump_file)
2000 fprintf (dump_file, "const call %d\n", INSN_UID (insn));
2002 /* See the head comment of the frame_read field. */
2003 if (reload_completed)
2004 insn_info->frame_read = true;
2006 /* Loop over the active stores and remove those which are
2007 killed by the const function call. */
2008 while (i_ptr)
2010 bool remove_store = false;
2012 /* The stack pointer based stores are always killed. */
2013 if (i_ptr->stack_pointer_based)
2014 remove_store = true;
2016 /* If the frame is read, the frame related stores are killed. */
2017 else if (insn_info->frame_read)
2019 store_info_t store_info = i_ptr->store_rec;
2021 /* Skip the clobbers. */
2022 while (!store_info->is_set)
2023 store_info = store_info->next;
2025 if (store_info->group_id >= 0
2026 && VEC_index (group_info_t, rtx_group_vec,
2027 store_info->group_id)->frame_related)
2028 remove_store = true;
2031 if (remove_store)
2033 if (dump_file)
2034 dump_insn_info ("removing from active", i_ptr);
2036 if (last)
2037 last->next_local_store = i_ptr->next_local_store;
2038 else
2039 active_local_stores = i_ptr->next_local_store;
2041 else
2042 last = i_ptr;
2044 i_ptr = i_ptr->next_local_store;
2048 else
2049 /* Every other call, including pure functions, may read memory. */
2050 add_wild_read (bb_info);
2052 return;
2055 /* Assuming that there are sets in these insns, we cannot delete
2056 them. */
2057 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2058 || volatile_refs_p (PATTERN (insn))
2059 || (flag_non_call_exceptions && may_trap_p (PATTERN (insn)))
2060 || (RTX_FRAME_RELATED_P (insn))
2061 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2062 insn_info->cannot_delete = true;
2064 body = PATTERN (insn);
2065 if (GET_CODE (body) == PARALLEL)
2067 int i;
2068 for (i = 0; i < XVECLEN (body, 0); i++)
2069 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2071 else
2072 mems_found += record_store (body, bb_info);
2074 if (dump_file)
2075 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2076 mems_found, insn_info->cannot_delete ? "true" : "false");
2078 /* If we found some sets of mems, and the insn has not been marked
2079 cannot delete, add it into the active_local_stores so that it can
2080 be locally deleted if found dead. Otherwise mark it as cannot
2081 delete. This simplifies the processing later. */
2082 if (mems_found == 1 && !insn_info->cannot_delete)
2084 insn_info->next_local_store = active_local_stores;
2085 active_local_stores = insn_info;
2087 else
2088 insn_info->cannot_delete = true;
2092 /* Remove BASE from the set of active_local_stores. This is a
2093 callback from cselib that is used to get rid of the stores in
2094 active_local_stores. */
2096 static void
2097 remove_useless_values (cselib_val *base)
2099 insn_info_t insn_info = active_local_stores;
2100 insn_info_t last = NULL;
2102 while (insn_info)
2104 store_info_t store_info = insn_info->store_rec;
2105 bool del = false;
2107 /* If ANY of the store_infos match the cselib group that is
2108 being deleted, then the insn can not be deleted. */
2109 while (store_info)
2111 if ((store_info->group_id == -1)
2112 && (store_info->cse_base == base))
2114 del = true;
2115 break;
2117 store_info = store_info->next;
2120 if (del)
2122 if (last)
2123 last->next_local_store = insn_info->next_local_store;
2124 else
2125 active_local_stores = insn_info->next_local_store;
2126 free_store_info (insn_info);
2128 else
2129 last = insn_info;
2131 insn_info = insn_info->next_local_store;
2136 /* Do all of step 1. */
2138 static void
2139 dse_step1 (void)
2141 basic_block bb;
2143 cselib_init (false);
2144 all_blocks = BITMAP_ALLOC (NULL);
2145 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2146 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2148 FOR_ALL_BB (bb)
2150 insn_info_t ptr;
2151 bb_info_t bb_info = (bb_info_t) pool_alloc (bb_info_pool);
2153 memset (bb_info, 0, sizeof (struct bb_info));
2154 bitmap_set_bit (all_blocks, bb->index);
2156 bb_table[bb->index] = bb_info;
2157 cselib_discard_hook = remove_useless_values;
2159 if (bb->index >= NUM_FIXED_BLOCKS)
2161 rtx insn;
2163 cse_store_info_pool
2164 = create_alloc_pool ("cse_store_info_pool",
2165 sizeof (struct store_info), 100);
2166 active_local_stores = NULL;
2167 cselib_clear_table ();
2169 /* Scan the insns. */
2170 FOR_BB_INSNS (bb, insn)
2172 if (INSN_P (insn))
2173 scan_insn (bb_info, insn);
2174 cselib_process_insn (insn);
2177 /* This is something of a hack, because the global algorithm
2178 is supposed to take care of the case where stores go dead
2179 at the end of the function. However, the global
2180 algorithm must take a more conservative view of block
2181 mode reads than the local alg does. So to get the case
2182 where you have a store to the frame followed by a non
2183 overlapping block more read, we look at the active local
2184 stores at the end of the function and delete all of the
2185 frame and spill based ones. */
2186 if (stores_off_frame_dead_at_return
2187 && (EDGE_COUNT (bb->succs) == 0
2188 || (single_succ_p (bb)
2189 && single_succ (bb) == EXIT_BLOCK_PTR
2190 && ! crtl->calls_eh_return)))
2192 insn_info_t i_ptr = active_local_stores;
2193 while (i_ptr)
2195 store_info_t store_info = i_ptr->store_rec;
2197 /* Skip the clobbers. */
2198 while (!store_info->is_set)
2199 store_info = store_info->next;
2200 if (store_info->alias_set)
2201 delete_dead_store_insn (i_ptr);
2202 else
2203 if (store_info->group_id >= 0)
2205 group_info_t group
2206 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
2207 if (group->frame_related)
2208 delete_dead_store_insn (i_ptr);
2211 i_ptr = i_ptr->next_local_store;
2215 /* Get rid of the loads that were discovered in
2216 replace_read. Cselib is finished with this block. */
2217 while (deferred_change_list)
2219 deferred_change_t next = deferred_change_list->next;
2221 /* There is no reason to validate this change. That was
2222 done earlier. */
2223 *deferred_change_list->loc = deferred_change_list->reg;
2224 pool_free (deferred_change_pool, deferred_change_list);
2225 deferred_change_list = next;
2228 /* Get rid of all of the cselib based store_infos in this
2229 block and mark the containing insns as not being
2230 deletable. */
2231 ptr = bb_info->last_insn;
2232 while (ptr)
2234 if (ptr->contains_cselib_groups)
2235 free_store_info (ptr);
2236 ptr = ptr->prev_insn;
2239 free_alloc_pool (cse_store_info_pool);
2243 cselib_finish ();
2244 htab_empty (rtx_group_table);
2248 /*----------------------------------------------------------------------------
2249 Second step.
2251 Assign each byte position in the stores that we are going to
2252 analyze globally to a position in the bitmaps. Returns true if
2253 there are any bit positions assigned.
2254 ----------------------------------------------------------------------------*/
2256 static void
2257 dse_step2_init (void)
2259 unsigned int i;
2260 group_info_t group;
2262 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
2264 /* For all non stack related bases, we only consider a store to
2265 be deletable if there are two or more stores for that
2266 position. This is because it takes one store to make the
2267 other store redundant. However, for the stores that are
2268 stack related, we consider them if there is only one store
2269 for the position. We do this because the stack related
2270 stores can be deleted if their is no read between them and
2271 the end of the function.
2273 To make this work in the current framework, we take the stack
2274 related bases add all of the bits from store1 into store2.
2275 This has the effect of making the eligible even if there is
2276 only one store. */
2278 if (stores_off_frame_dead_at_return && group->frame_related)
2280 bitmap_ior_into (group->store2_n, group->store1_n);
2281 bitmap_ior_into (group->store2_p, group->store1_p);
2282 if (dump_file)
2283 fprintf (dump_file, "group %d is frame related ", i);
2286 group->offset_map_size_n++;
2287 group->offset_map_n = XNEWVEC (int, group->offset_map_size_n);
2288 group->offset_map_size_p++;
2289 group->offset_map_p = XNEWVEC (int, group->offset_map_size_p);
2290 group->process_globally = false;
2291 if (dump_file)
2293 fprintf (dump_file, "group %d(%d+%d): ", i,
2294 (int)bitmap_count_bits (group->store2_n),
2295 (int)bitmap_count_bits (group->store2_p));
2296 bitmap_print (dump_file, group->store2_n, "n ", " ");
2297 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2303 /* Init the offset tables for the normal case. */
2305 static bool
2306 dse_step2_nospill (void)
2308 unsigned int i;
2309 group_info_t group;
2310 /* Position 0 is unused because 0 is used in the maps to mean
2311 unused. */
2312 current_position = 1;
2314 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
2316 bitmap_iterator bi;
2317 unsigned int j;
2319 if (group == clear_alias_group)
2320 continue;
2322 memset (group->offset_map_n, 0, sizeof(int) * group->offset_map_size_n);
2323 memset (group->offset_map_p, 0, sizeof(int) * group->offset_map_size_p);
2324 bitmap_clear (group->group_kill);
2326 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2328 bitmap_set_bit (group->group_kill, current_position);
2329 group->offset_map_n[j] = current_position++;
2330 group->process_globally = true;
2332 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2334 bitmap_set_bit (group->group_kill, current_position);
2335 group->offset_map_p[j] = current_position++;
2336 group->process_globally = true;
2339 return current_position != 1;
2343 /* Init the offset tables for the spill case. */
2345 static bool
2346 dse_step2_spill (void)
2348 unsigned int j;
2349 group_info_t group = clear_alias_group;
2350 bitmap_iterator bi;
2352 /* Position 0 is unused because 0 is used in the maps to mean
2353 unused. */
2354 current_position = 1;
2356 if (dump_file)
2358 bitmap_print (dump_file, clear_alias_sets,
2359 "clear alias sets ", "\n");
2360 bitmap_print (dump_file, disqualified_clear_alias_sets,
2361 "disqualified clear alias sets ", "\n");
2364 memset (group->offset_map_n, 0, sizeof(int) * group->offset_map_size_n);
2365 memset (group->offset_map_p, 0, sizeof(int) * group->offset_map_size_p);
2366 bitmap_clear (group->group_kill);
2368 /* Remove the disqualified positions from the store2_p set. */
2369 bitmap_and_compl_into (group->store2_p, disqualified_clear_alias_sets);
2371 /* We do not need to process the store2_n set because
2372 alias_sets are always positive. */
2373 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2375 bitmap_set_bit (group->group_kill, current_position);
2376 group->offset_map_p[j] = current_position++;
2377 group->process_globally = true;
2380 return current_position != 1;
2385 /*----------------------------------------------------------------------------
2386 Third step.
2388 Build the bit vectors for the transfer functions.
2389 ----------------------------------------------------------------------------*/
2392 /* Note that this is NOT a general purpose function. Any mem that has
2393 an alias set registered here expected to be COMPLETELY unaliased:
2394 i.e it's addresses are not and need not be examined.
2396 It is known that all references to this address will have this
2397 alias set and there are NO other references to this address in the
2398 function.
2400 Currently the only place that is known to be clean enough to use
2401 this interface is the code that assigns the spill locations.
2403 All of the mems that have alias_sets registered are subjected to a
2404 very powerful form of dse where function calls, volatile reads and
2405 writes, and reads from random location are not taken into account.
2407 It is also assumed that these locations go dead when the function
2408 returns. This assumption could be relaxed if there were found to
2409 be places that this assumption was not correct.
2411 The MODE is passed in and saved. The mode of each load or store to
2412 a mem with ALIAS_SET is checked against MEM. If the size of that
2413 load or store is different from MODE, processing is halted on this
2414 alias set. For the vast majority of aliases sets, all of the loads
2415 and stores will use the same mode. But vectors are treated
2416 differently: the alias set is established for the entire vector,
2417 but reload will insert loads and stores for individual elements and
2418 we do not necessarily have the information to track those separate
2419 elements. So when we see a mode mismatch, we just bail. */
2422 void
2423 dse_record_singleton_alias_set (alias_set_type alias_set,
2424 enum machine_mode mode)
2426 struct clear_alias_mode_holder tmp_holder;
2427 struct clear_alias_mode_holder *entry;
2428 void **slot;
2430 /* If we are not going to run dse, we need to return now or there
2431 will be problems with allocating the bitmaps. */
2432 if ((!gate_dse()) || !alias_set)
2433 return;
2435 if (!clear_alias_sets)
2437 clear_alias_sets = BITMAP_ALLOC (NULL);
2438 disqualified_clear_alias_sets = BITMAP_ALLOC (NULL);
2439 clear_alias_mode_table = htab_create (11, clear_alias_mode_hash,
2440 clear_alias_mode_eq, NULL);
2441 clear_alias_mode_pool = create_alloc_pool ("clear_alias_mode_pool",
2442 sizeof (struct clear_alias_mode_holder), 100);
2445 bitmap_set_bit (clear_alias_sets, alias_set);
2447 tmp_holder.alias_set = alias_set;
2449 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, INSERT);
2450 gcc_assert (*slot == NULL);
2452 *slot = entry =
2453 (struct clear_alias_mode_holder *) pool_alloc (clear_alias_mode_pool);
2454 entry->alias_set = alias_set;
2455 entry->mode = mode;
2459 /* Remove ALIAS_SET from the sets of stack slots being considered. */
2461 void
2462 dse_invalidate_singleton_alias_set (alias_set_type alias_set)
2464 if ((!gate_dse()) || !alias_set)
2465 return;
2467 bitmap_clear_bit (clear_alias_sets, alias_set);
2471 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2472 there, return 0. */
2474 static int
2475 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
2477 if (offset < 0)
2479 HOST_WIDE_INT offset_p = -offset;
2480 if (offset_p >= group_info->offset_map_size_n)
2481 return 0;
2482 return group_info->offset_map_n[offset_p];
2484 else
2486 if (offset >= group_info->offset_map_size_p)
2487 return 0;
2488 return group_info->offset_map_p[offset];
2493 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2494 may be NULL. */
2496 static void
2497 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
2499 while (store_info)
2501 HOST_WIDE_INT i;
2502 group_info_t group_info
2503 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
2504 if (group_info->process_globally)
2505 for (i = store_info->begin; i < store_info->end; i++)
2507 int index = get_bitmap_index (group_info, i);
2508 if (index != 0)
2510 bitmap_set_bit (gen, index);
2511 if (kill)
2512 bitmap_clear_bit (kill, index);
2515 store_info = store_info->next;
2520 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2521 may be NULL. */
2523 static void
2524 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
2526 while (store_info)
2528 if (store_info->alias_set)
2530 int index = get_bitmap_index (clear_alias_group,
2531 store_info->alias_set);
2532 if (index != 0)
2534 bitmap_set_bit (gen, index);
2535 if (kill)
2536 bitmap_clear_bit (kill, index);
2539 store_info = store_info->next;
2544 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
2545 may be NULL. */
2547 static void
2548 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
2550 read_info_t read_info = insn_info->read_rec;
2551 int i;
2552 group_info_t group;
2554 /* If this insn reads the frame, kill all the frame related stores. */
2555 if (insn_info->frame_read)
2557 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
2558 if (group->process_globally && group->frame_related)
2560 if (kill)
2561 bitmap_ior_into (kill, group->group_kill);
2562 bitmap_and_compl_into (gen, group->group_kill);
2566 while (read_info)
2568 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
2570 if (group->process_globally)
2572 if (i == read_info->group_id)
2574 if (read_info->begin > read_info->end)
2576 /* Begin > end for block mode reads. */
2577 if (kill)
2578 bitmap_ior_into (kill, group->group_kill);
2579 bitmap_and_compl_into (gen, group->group_kill);
2581 else
2583 /* The groups are the same, just process the
2584 offsets. */
2585 HOST_WIDE_INT j;
2586 for (j = read_info->begin; j < read_info->end; j++)
2588 int index = get_bitmap_index (group, j);
2589 if (index != 0)
2591 if (kill)
2592 bitmap_set_bit (kill, index);
2593 bitmap_clear_bit (gen, index);
2598 else
2600 /* The groups are different, if the alias sets
2601 conflict, clear the entire group. We only need
2602 to apply this test if the read_info is a cselib
2603 read. Anything with a constant base cannot alias
2604 something else with a different constant
2605 base. */
2606 if ((read_info->group_id < 0)
2607 && canon_true_dependence (group->base_mem,
2608 QImode,
2609 group->canon_base_mem,
2610 read_info->mem, rtx_varies_p))
2612 if (kill)
2613 bitmap_ior_into (kill, group->group_kill);
2614 bitmap_and_compl_into (gen, group->group_kill);
2620 read_info = read_info->next;
2624 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
2625 may be NULL. */
2627 static void
2628 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
2630 while (read_info)
2632 if (read_info->alias_set)
2634 int index = get_bitmap_index (clear_alias_group,
2635 read_info->alias_set);
2636 if (index != 0)
2638 if (kill)
2639 bitmap_set_bit (kill, index);
2640 bitmap_clear_bit (gen, index);
2644 read_info = read_info->next;
2649 /* Return the insn in BB_INFO before the first wild read or if there
2650 are no wild reads in the block, return the last insn. */
2652 static insn_info_t
2653 find_insn_before_first_wild_read (bb_info_t bb_info)
2655 insn_info_t insn_info = bb_info->last_insn;
2656 insn_info_t last_wild_read = NULL;
2658 while (insn_info)
2660 if (insn_info->wild_read)
2662 last_wild_read = insn_info->prev_insn;
2663 /* Block starts with wild read. */
2664 if (!last_wild_read)
2665 return NULL;
2668 insn_info = insn_info->prev_insn;
2671 if (last_wild_read)
2672 return last_wild_read;
2673 else
2674 return bb_info->last_insn;
2678 /* Scan the insns in BB_INFO starting at PTR and going to the top of
2679 the block in order to build the gen and kill sets for the block.
2680 We start at ptr which may be the last insn in the block or may be
2681 the first insn with a wild read. In the latter case we are able to
2682 skip the rest of the block because it just does not matter:
2683 anything that happens is hidden by the wild read. */
2685 static void
2686 dse_step3_scan (bool for_spills, basic_block bb)
2688 bb_info_t bb_info = bb_table[bb->index];
2689 insn_info_t insn_info;
2691 if (for_spills)
2692 /* There are no wild reads in the spill case. */
2693 insn_info = bb_info->last_insn;
2694 else
2695 insn_info = find_insn_before_first_wild_read (bb_info);
2697 /* In the spill case or in the no_spill case if there is no wild
2698 read in the block, we will need a kill set. */
2699 if (insn_info == bb_info->last_insn)
2701 if (bb_info->kill)
2702 bitmap_clear (bb_info->kill);
2703 else
2704 bb_info->kill = BITMAP_ALLOC (NULL);
2706 else
2707 if (bb_info->kill)
2708 BITMAP_FREE (bb_info->kill);
2710 while (insn_info)
2712 /* There may have been code deleted by the dce pass run before
2713 this phase. */
2714 if (insn_info->insn && INSN_P (insn_info->insn))
2716 /* Process the read(s) last. */
2717 if (for_spills)
2719 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
2720 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
2722 else
2724 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
2725 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
2729 insn_info = insn_info->prev_insn;
2734 /* Set the gen set of the exit block, and also any block with no
2735 successors that does not have a wild read. */
2737 static void
2738 dse_step3_exit_block_scan (bb_info_t bb_info)
2740 /* The gen set is all 0's for the exit block except for the
2741 frame_pointer_group. */
2743 if (stores_off_frame_dead_at_return)
2745 unsigned int i;
2746 group_info_t group;
2748 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
2750 if (group->process_globally && group->frame_related)
2751 bitmap_ior_into (bb_info->gen, group->group_kill);
2757 /* Find all of the blocks that are not backwards reachable from the
2758 exit block or any block with no successors (BB). These are the
2759 infinite loops or infinite self loops. These blocks will still
2760 have their bits set in UNREACHABLE_BLOCKS. */
2762 static void
2763 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
2765 edge e;
2766 edge_iterator ei;
2768 if (TEST_BIT (unreachable_blocks, bb->index))
2770 RESET_BIT (unreachable_blocks, bb->index);
2771 FOR_EACH_EDGE (e, ei, bb->preds)
2773 mark_reachable_blocks (unreachable_blocks, e->src);
2778 /* Build the transfer functions for the function. */
2780 static void
2781 dse_step3 (bool for_spills)
2783 basic_block bb;
2784 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block);
2785 sbitmap_iterator sbi;
2786 bitmap all_ones = NULL;
2787 unsigned int i;
2789 sbitmap_ones (unreachable_blocks);
2791 FOR_ALL_BB (bb)
2793 bb_info_t bb_info = bb_table[bb->index];
2794 if (bb_info->gen)
2795 bitmap_clear (bb_info->gen);
2796 else
2797 bb_info->gen = BITMAP_ALLOC (NULL);
2799 if (bb->index == ENTRY_BLOCK)
2801 else if (bb->index == EXIT_BLOCK)
2802 dse_step3_exit_block_scan (bb_info);
2803 else
2804 dse_step3_scan (for_spills, bb);
2805 if (EDGE_COUNT (bb->succs) == 0)
2806 mark_reachable_blocks (unreachable_blocks, bb);
2808 /* If this is the second time dataflow is run, delete the old
2809 sets. */
2810 if (bb_info->in)
2811 BITMAP_FREE (bb_info->in);
2812 if (bb_info->out)
2813 BITMAP_FREE (bb_info->out);
2816 /* For any block in an infinite loop, we must initialize the out set
2817 to all ones. This could be expensive, but almost never occurs in
2818 practice. However, it is common in regression tests. */
2819 EXECUTE_IF_SET_IN_SBITMAP (unreachable_blocks, 0, i, sbi)
2821 if (bitmap_bit_p (all_blocks, i))
2823 bb_info_t bb_info = bb_table[i];
2824 if (!all_ones)
2826 unsigned int j;
2827 group_info_t group;
2829 all_ones = BITMAP_ALLOC (NULL);
2830 for (j = 0; VEC_iterate (group_info_t, rtx_group_vec, j, group); j++)
2831 bitmap_ior_into (all_ones, group->group_kill);
2833 if (!bb_info->out)
2835 bb_info->out = BITMAP_ALLOC (NULL);
2836 bitmap_copy (bb_info->out, all_ones);
2841 if (all_ones)
2842 BITMAP_FREE (all_ones);
2843 sbitmap_free (unreachable_blocks);
2848 /*----------------------------------------------------------------------------
2849 Fourth step.
2851 Solve the bitvector equations.
2852 ----------------------------------------------------------------------------*/
2855 /* Confluence function for blocks with no successors. Create an out
2856 set from the gen set of the exit block. This block logically has
2857 the exit block as a successor. */
2861 static void
2862 dse_confluence_0 (basic_block bb)
2864 bb_info_t bb_info = bb_table[bb->index];
2866 if (bb->index == EXIT_BLOCK)
2867 return;
2869 if (!bb_info->out)
2871 bb_info->out = BITMAP_ALLOC (NULL);
2872 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
2876 /* Propagate the information from the in set of the dest of E to the
2877 out set of the src of E. If the various in or out sets are not
2878 there, that means they are all ones. */
2880 static void
2881 dse_confluence_n (edge e)
2883 bb_info_t src_info = bb_table[e->src->index];
2884 bb_info_t dest_info = bb_table[e->dest->index];
2886 if (dest_info->in)
2888 if (src_info->out)
2889 bitmap_and_into (src_info->out, dest_info->in);
2890 else
2892 src_info->out = BITMAP_ALLOC (NULL);
2893 bitmap_copy (src_info->out, dest_info->in);
2899 /* Propagate the info from the out to the in set of BB_INDEX's basic
2900 block. There are three cases:
2902 1) The block has no kill set. In this case the kill set is all
2903 ones. It does not matter what the out set of the block is, none of
2904 the info can reach the top. The only thing that reaches the top is
2905 the gen set and we just copy the set.
2907 2) There is a kill set but no out set and bb has successors. In
2908 this case we just return. Eventually an out set will be created and
2909 it is better to wait than to create a set of ones.
2911 3) There is both a kill and out set. We apply the obvious transfer
2912 function.
2915 static bool
2916 dse_transfer_function (int bb_index)
2918 bb_info_t bb_info = bb_table[bb_index];
2920 if (bb_info->kill)
2922 if (bb_info->out)
2924 /* Case 3 above. */
2925 if (bb_info->in)
2926 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
2927 bb_info->out, bb_info->kill);
2928 else
2930 bb_info->in = BITMAP_ALLOC (NULL);
2931 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
2932 bb_info->out, bb_info->kill);
2933 return true;
2936 else
2937 /* Case 2 above. */
2938 return false;
2940 else
2942 /* Case 1 above. If there is already an in set, nothing
2943 happens. */
2944 if (bb_info->in)
2945 return false;
2946 else
2948 bb_info->in = BITMAP_ALLOC (NULL);
2949 bitmap_copy (bb_info->in, bb_info->gen);
2950 return true;
2955 /* Solve the dataflow equations. */
2957 static void
2958 dse_step4 (void)
2960 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
2961 dse_confluence_n, dse_transfer_function,
2962 all_blocks, df_get_postorder (DF_BACKWARD),
2963 df_get_n_blocks (DF_BACKWARD));
2964 if (dump_file)
2966 basic_block bb;
2968 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
2969 FOR_ALL_BB (bb)
2971 bb_info_t bb_info = bb_table[bb->index];
2973 df_print_bb_index (bb, dump_file);
2974 if (bb_info->in)
2975 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
2976 else
2977 fprintf (dump_file, " in: *MISSING*\n");
2978 if (bb_info->gen)
2979 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
2980 else
2981 fprintf (dump_file, " gen: *MISSING*\n");
2982 if (bb_info->kill)
2983 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
2984 else
2985 fprintf (dump_file, " kill: *MISSING*\n");
2986 if (bb_info->out)
2987 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
2988 else
2989 fprintf (dump_file, " out: *MISSING*\n\n");
2996 /*----------------------------------------------------------------------------
2997 Fifth step.
2999 Delete the stores that can only be deleted using the global information.
3000 ----------------------------------------------------------------------------*/
3003 static void
3004 dse_step5_nospill (void)
3006 basic_block bb;
3007 FOR_EACH_BB (bb)
3009 bb_info_t bb_info = bb_table[bb->index];
3010 insn_info_t insn_info = bb_info->last_insn;
3011 bitmap v = bb_info->out;
3013 while (insn_info)
3015 bool deleted = false;
3016 if (dump_file && insn_info->insn)
3018 fprintf (dump_file, "starting to process insn %d\n",
3019 INSN_UID (insn_info->insn));
3020 bitmap_print (dump_file, v, " v: ", "\n");
3023 /* There may have been code deleted by the dce pass run before
3024 this phase. */
3025 if (insn_info->insn
3026 && INSN_P (insn_info->insn)
3027 && (!insn_info->cannot_delete)
3028 && (!bitmap_empty_p (v)))
3030 store_info_t store_info = insn_info->store_rec;
3032 /* Try to delete the current insn. */
3033 deleted = true;
3035 /* Skip the clobbers. */
3036 while (!store_info->is_set)
3037 store_info = store_info->next;
3039 if (store_info->alias_set)
3040 deleted = false;
3041 else
3043 HOST_WIDE_INT i;
3044 group_info_t group_info
3045 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
3047 for (i = store_info->begin; i < store_info->end; i++)
3049 int index = get_bitmap_index (group_info, i);
3051 if (dump_file)
3052 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3053 if (index == 0 || !bitmap_bit_p (v, index))
3055 if (dump_file)
3056 fprintf (dump_file, "failing at i = %d\n", (int)i);
3057 deleted = false;
3058 break;
3062 if (deleted)
3064 if (dbg_cnt (dse))
3066 check_for_inc_dec (insn_info->insn);
3067 delete_insn (insn_info->insn);
3068 insn_info->insn = NULL;
3069 globally_deleted++;
3073 /* We do want to process the local info if the insn was
3074 deleted. For instance, if the insn did a wild read, we
3075 no longer need to trash the info. */
3076 if (insn_info->insn
3077 && INSN_P (insn_info->insn)
3078 && (!deleted))
3080 scan_stores_nospill (insn_info->store_rec, v, NULL);
3081 if (insn_info->wild_read)
3083 if (dump_file)
3084 fprintf (dump_file, "wild read\n");
3085 bitmap_clear (v);
3087 else if (insn_info->read_rec)
3089 if (dump_file)
3090 fprintf (dump_file, "regular read\n");
3091 scan_reads_nospill (insn_info, v, NULL);
3095 insn_info = insn_info->prev_insn;
3101 static void
3102 dse_step5_spill (void)
3104 basic_block bb;
3105 FOR_EACH_BB (bb)
3107 bb_info_t bb_info = bb_table[bb->index];
3108 insn_info_t insn_info = bb_info->last_insn;
3109 bitmap v = bb_info->out;
3111 while (insn_info)
3113 bool deleted = false;
3114 /* There may have been code deleted by the dce pass run before
3115 this phase. */
3116 if (insn_info->insn
3117 && INSN_P (insn_info->insn)
3118 && (!insn_info->cannot_delete)
3119 && (!bitmap_empty_p (v)))
3121 /* Try to delete the current insn. */
3122 store_info_t store_info = insn_info->store_rec;
3123 deleted = true;
3125 while (store_info)
3127 if (store_info->alias_set)
3129 int index = get_bitmap_index (clear_alias_group,
3130 store_info->alias_set);
3131 if (index == 0 || !bitmap_bit_p (v, index))
3133 deleted = false;
3134 break;
3137 else
3138 deleted = false;
3139 store_info = store_info->next;
3141 if (deleted && dbg_cnt (dse))
3143 if (dump_file)
3144 fprintf (dump_file, "Spill deleting insn %d\n",
3145 INSN_UID (insn_info->insn));
3146 check_for_inc_dec (insn_info->insn);
3147 delete_insn (insn_info->insn);
3148 spill_deleted++;
3149 insn_info->insn = NULL;
3153 if (insn_info->insn
3154 && INSN_P (insn_info->insn)
3155 && (!deleted))
3157 scan_stores_spill (insn_info->store_rec, v, NULL);
3158 scan_reads_spill (insn_info->read_rec, v, NULL);
3161 insn_info = insn_info->prev_insn;
3168 /*----------------------------------------------------------------------------
3169 Sixth step.
3171 Destroy everything left standing.
3172 ----------------------------------------------------------------------------*/
3174 static void
3175 dse_step6 (bool global_done)
3177 unsigned int i;
3178 group_info_t group;
3179 basic_block bb;
3181 for (i = 0; VEC_iterate (group_info_t, rtx_group_vec, i, group); i++)
3183 free (group->offset_map_n);
3184 free (group->offset_map_p);
3185 BITMAP_FREE (group->store1_n);
3186 BITMAP_FREE (group->store1_p);
3187 BITMAP_FREE (group->store2_n);
3188 BITMAP_FREE (group->store2_p);
3189 BITMAP_FREE (group->group_kill);
3192 if (global_done)
3193 FOR_ALL_BB (bb)
3195 bb_info_t bb_info = bb_table[bb->index];
3196 BITMAP_FREE (bb_info->gen);
3197 if (bb_info->kill)
3198 BITMAP_FREE (bb_info->kill);
3199 if (bb_info->in)
3200 BITMAP_FREE (bb_info->in);
3201 if (bb_info->out)
3202 BITMAP_FREE (bb_info->out);
3205 if (clear_alias_sets)
3207 BITMAP_FREE (clear_alias_sets);
3208 BITMAP_FREE (disqualified_clear_alias_sets);
3209 free_alloc_pool (clear_alias_mode_pool);
3210 htab_delete (clear_alias_mode_table);
3213 end_alias_analysis ();
3214 free (bb_table);
3215 htab_delete (rtx_group_table);
3216 VEC_free (group_info_t, heap, rtx_group_vec);
3217 BITMAP_FREE (all_blocks);
3218 BITMAP_FREE (scratch);
3220 free_alloc_pool (rtx_store_info_pool);
3221 free_alloc_pool (read_info_pool);
3222 free_alloc_pool (insn_info_pool);
3223 free_alloc_pool (bb_info_pool);
3224 free_alloc_pool (rtx_group_info_pool);
3225 free_alloc_pool (deferred_change_pool);
3229 /* -------------------------------------------------------------------------
3231 ------------------------------------------------------------------------- */
3233 /* Callback for running pass_rtl_dse. */
3235 static unsigned int
3236 rest_of_handle_dse (void)
3238 bool did_global = false;
3240 df_set_flags (DF_DEFER_INSN_RESCAN);
3242 dse_step0 ();
3243 dse_step1 ();
3244 dse_step2_init ();
3245 if (dse_step2_nospill ())
3247 df_set_flags (DF_LR_RUN_DCE);
3248 df_analyze ();
3249 did_global = true;
3250 if (dump_file)
3251 fprintf (dump_file, "doing global processing\n");
3252 dse_step3 (false);
3253 dse_step4 ();
3254 dse_step5_nospill ();
3257 /* For the instance of dse that runs after reload, we make a special
3258 pass to process the spills. These are special in that they are
3259 totally transparent, i.e, there is no aliasing issues that need
3260 to be considered. This means that the wild reads that kill
3261 everything else do not apply here. */
3262 if (clear_alias_sets && dse_step2_spill ())
3264 if (!did_global)
3266 df_set_flags (DF_LR_RUN_DCE);
3267 df_analyze ();
3269 did_global = true;
3270 if (dump_file)
3271 fprintf (dump_file, "doing global spill processing\n");
3272 dse_step3 (true);
3273 dse_step4 ();
3274 dse_step5_spill ();
3277 dse_step6 (did_global);
3279 if (dump_file)
3280 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3281 locally_deleted, globally_deleted, spill_deleted);
3282 return 0;
3285 static bool
3286 gate_dse (void)
3288 return gate_dse1 () || gate_dse2 ();
3291 static bool
3292 gate_dse1 (void)
3294 return optimize > 0 && flag_dse
3295 && dbg_cnt (dse1);
3298 static bool
3299 gate_dse2 (void)
3301 return optimize > 0 && flag_dse
3302 && dbg_cnt (dse2);
3305 struct rtl_opt_pass pass_rtl_dse1 =
3308 RTL_PASS,
3309 "dse1", /* name */
3310 gate_dse1, /* gate */
3311 rest_of_handle_dse, /* execute */
3312 NULL, /* sub */
3313 NULL, /* next */
3314 0, /* static_pass_number */
3315 TV_DSE1, /* tv_id */
3316 0, /* properties_required */
3317 0, /* properties_provided */
3318 0, /* properties_destroyed */
3319 0, /* todo_flags_start */
3320 TODO_dump_func |
3321 TODO_df_finish | TODO_verify_rtl_sharing |
3322 TODO_ggc_collect /* todo_flags_finish */
3326 struct rtl_opt_pass pass_rtl_dse2 =
3329 RTL_PASS,
3330 "dse2", /* name */
3331 gate_dse2, /* gate */
3332 rest_of_handle_dse, /* execute */
3333 NULL, /* sub */
3334 NULL, /* next */
3335 0, /* static_pass_number */
3336 TV_DSE2, /* tv_id */
3337 0, /* properties_required */
3338 0, /* properties_provided */
3339 0, /* properties_destroyed */
3340 0, /* todo_flags_start */
3341 TODO_dump_func |
3342 TODO_df_finish | TODO_verify_rtl_sharing |
3343 TODO_ggc_collect /* todo_flags_finish */