PR gcov-profile/51113
[official-gcc.git] / gcc / dse.c
blobddabd3de0c6a86dd9536595df8268184542696e4
1 /* RTL dead store elimination.
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
6 and Kenneth Zadeck <zadeck@naturalbridge.com>
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #undef BASELINE
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "hashtab.h"
30 #include "tm.h"
31 #include "rtl.h"
32 #include "tree.h"
33 #include "tm_p.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "regset.h"
37 #include "flags.h"
38 #include "df.h"
39 #include "cselib.h"
40 #include "timevar.h"
41 #include "tree-pass.h"
42 #include "alloc-pool.h"
43 #include "alias.h"
44 #include "insn-config.h"
45 #include "expr.h"
46 #include "recog.h"
47 #include "dse.h"
48 #include "optabs.h"
49 #include "dbgcnt.h"
50 #include "target.h"
51 #include "params.h"
52 #include "tree-flow.h"
54 /* This file contains three techniques for performing Dead Store
55 Elimination (dse).
57 * The first technique performs dse locally on any base address. It
58 is based on the cselib which is a local value numbering technique.
59 This technique is local to a basic block but deals with a fairly
60 general addresses.
62 * The second technique performs dse globally but is restricted to
63 base addresses that are either constant or are relative to the
64 frame_pointer.
66 * The third technique, (which is only done after register allocation)
67 processes the spill spill slots. This differs from the second
68 technique because it takes advantage of the fact that spilling is
69 completely free from the effects of aliasing.
71 Logically, dse is a backwards dataflow problem. A store can be
72 deleted if it if cannot be reached in the backward direction by any
73 use of the value being stored. However, the local technique uses a
74 forwards scan of the basic block because cselib requires that the
75 block be processed in that order.
77 The pass is logically broken into 7 steps:
79 0) Initialization.
81 1) The local algorithm, as well as scanning the insns for the two
82 global algorithms.
84 2) Analysis to see if the global algs are necessary. In the case
85 of stores base on a constant address, there must be at least two
86 stores to that address, to make it possible to delete some of the
87 stores. In the case of stores off of the frame or spill related
88 stores, only one store to an address is necessary because those
89 stores die at the end of the function.
91 3) Set up the global dataflow equations based on processing the
92 info parsed in the first step.
94 4) Solve the dataflow equations.
96 5) Delete the insns that the global analysis has indicated are
97 unnecessary.
99 6) Delete insns that store the same value as preceeding store
100 where the earlier store couldn't be eliminated.
102 7) Cleanup.
104 This step uses cselib and canon_rtx to build the largest expression
105 possible for each address. This pass is a forwards pass through
106 each basic block. From the point of view of the global technique,
107 the first pass could examine a block in either direction. The
108 forwards ordering is to accommodate cselib.
110 We a simplifying assumption: addresses fall into four broad
111 categories:
113 1) base has rtx_varies_p == false, offset is constant.
114 2) base has rtx_varies_p == false, offset variable.
115 3) base has rtx_varies_p == true, offset constant.
116 4) base has rtx_varies_p == true, offset variable.
118 The local passes are able to process all 4 kinds of addresses. The
119 global pass only handles (1).
121 The global problem is formulated as follows:
123 A store, S1, to address A, where A is not relative to the stack
124 frame, can be eliminated if all paths from S1 to the end of the
125 of the function contain another store to A before a read to A.
127 If the address A is relative to the stack frame, a store S2 to A
128 can be eliminated if there are no paths from S1 that reach the
129 end of the function that read A before another store to A. In
130 this case S2 can be deleted if there are paths to from S2 to the
131 end of the function that have no reads or writes to A. This
132 second case allows stores to the stack frame to be deleted that
133 would otherwise die when the function returns. This cannot be
134 done if stores_off_frame_dead_at_return is not true. See the doc
135 for that variable for when this variable is false.
137 The global problem is formulated as a backwards set union
138 dataflow problem where the stores are the gens and reads are the
139 kills. Set union problems are rare and require some special
140 handling given our representation of bitmaps. A straightforward
141 implementation of requires a lot of bitmaps filled with 1s.
142 These are expensive and cumbersome in our bitmap formulation so
143 care has been taken to avoid large vectors filled with 1s. See
144 the comments in bb_info and in the dataflow confluence functions
145 for details.
147 There are two places for further enhancements to this algorithm:
149 1) The original dse which was embedded in a pass called flow also
150 did local address forwarding. For example in
152 A <- r100
153 ... <- A
155 flow would replace the right hand side of the second insn with a
156 reference to r100. Most of the information is available to add this
157 to this pass. It has not done it because it is a lot of work in
158 the case that either r100 is assigned to between the first and
159 second insn and/or the second insn is a load of part of the value
160 stored by the first insn.
162 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
163 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
164 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
165 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
167 2) The cleaning up of spill code is quite profitable. It currently
168 depends on reading tea leaves and chicken entrails left by reload.
169 This pass depends on reload creating a singleton alias set for each
170 spill slot and telling the next dse pass which of these alias sets
171 are the singletons. Rather than analyze the addresses of the
172 spills, dse's spill processing just does analysis of the loads and
173 stores that use those alias sets. There are three cases where this
174 falls short:
176 a) Reload sometimes creates the slot for one mode of access, and
177 then inserts loads and/or stores for a smaller mode. In this
178 case, the current code just punts on the slot. The proper thing
179 to do is to back out and use one bit vector position for each
180 byte of the entity associated with the slot. This depends on
181 KNOWING that reload always generates the accesses for each of the
182 bytes in some canonical (read that easy to understand several
183 passes after reload happens) way.
185 b) Reload sometimes decides that spill slot it allocated was not
186 large enough for the mode and goes back and allocates more slots
187 with the same mode and alias set. The backout in this case is a
188 little more graceful than (a). In this case the slot is unmarked
189 as being a spill slot and if final address comes out to be based
190 off the frame pointer, the global algorithm handles this slot.
192 c) For any pass that may prespill, there is currently no
193 mechanism to tell the dse pass that the slot being used has the
194 special properties that reload uses. It may be that all that is
195 required is to have those passes make the same calls that reload
196 does, assuming that the alias sets can be manipulated in the same
197 way. */
199 /* There are limits to the size of constant offsets we model for the
200 global problem. There are certainly test cases, that exceed this
201 limit, however, it is unlikely that there are important programs
202 that really have constant offsets this size. */
203 #define MAX_OFFSET (64 * 1024)
206 static bitmap scratch = NULL;
207 struct insn_info;
209 /* This structure holds information about a candidate store. */
210 struct store_info
213 /* False means this is a clobber. */
214 bool is_set;
216 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
217 bool is_large;
219 /* The id of the mem group of the base address. If rtx_varies_p is
220 true, this is -1. Otherwise, it is the index into the group
221 table. */
222 int group_id;
224 /* This is the cselib value. */
225 cselib_val *cse_base;
227 /* This canonized mem. */
228 rtx mem;
230 /* Canonized MEM address for use by canon_true_dependence. */
231 rtx mem_addr;
233 /* If this is non-zero, it is the alias set of a spill location. */
234 alias_set_type alias_set;
236 /* The offset of the first and byte before the last byte associated
237 with the operation. */
238 HOST_WIDE_INT begin, end;
240 union
242 /* A bitmask as wide as the number of bytes in the word that
243 contains a 1 if the byte may be needed. The store is unused if
244 all of the bits are 0. This is used if IS_LARGE is false. */
245 unsigned HOST_WIDE_INT small_bitmask;
247 struct
249 /* A bitmap with one bit per byte. Cleared bit means the position
250 is needed. Used if IS_LARGE is false. */
251 bitmap bmap;
253 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
254 equal to END - BEGIN, the whole store is unused. */
255 int count;
256 } large;
257 } positions_needed;
259 /* The next store info for this insn. */
260 struct store_info *next;
262 /* The right hand side of the store. This is used if there is a
263 subsequent reload of the mems address somewhere later in the
264 basic block. */
265 rtx rhs;
267 /* If rhs is or holds a constant, this contains that constant,
268 otherwise NULL. */
269 rtx const_rhs;
271 /* Set if this store stores the same constant value as REDUNDANT_REASON
272 insn stored. These aren't eliminated early, because doing that
273 might prevent the earlier larger store to be eliminated. */
274 struct insn_info *redundant_reason;
277 /* Return a bitmask with the first N low bits set. */
279 static unsigned HOST_WIDE_INT
280 lowpart_bitmask (int n)
282 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
283 return mask >> (HOST_BITS_PER_WIDE_INT - n);
286 typedef struct store_info *store_info_t;
287 static alloc_pool cse_store_info_pool;
288 static alloc_pool rtx_store_info_pool;
290 /* This structure holds information about a load. These are only
291 built for rtx bases. */
292 struct read_info
294 /* The id of the mem group of the base address. */
295 int group_id;
297 /* If this is non-zero, it is the alias set of a spill location. */
298 alias_set_type alias_set;
300 /* The offset of the first and byte after the last byte associated
301 with the operation. If begin == end == 0, the read did not have
302 a constant offset. */
303 int begin, end;
305 /* The mem being read. */
306 rtx mem;
308 /* The next read_info for this insn. */
309 struct read_info *next;
311 typedef struct read_info *read_info_t;
312 static alloc_pool read_info_pool;
315 /* One of these records is created for each insn. */
317 struct insn_info
319 /* Set true if the insn contains a store but the insn itself cannot
320 be deleted. This is set if the insn is a parallel and there is
321 more than one non dead output or if the insn is in some way
322 volatile. */
323 bool cannot_delete;
325 /* This field is only used by the global algorithm. It is set true
326 if the insn contains any read of mem except for a (1). This is
327 also set if the insn is a call or has a clobber mem. If the insn
328 contains a wild read, the use_rec will be null. */
329 bool wild_read;
331 /* This is true only for CALL instructions which could potentially read
332 any non-frame memory location. This field is used by the global
333 algorithm. */
334 bool non_frame_wild_read;
336 /* This field is only used for the processing of const functions.
337 These functions cannot read memory, but they can read the stack
338 because that is where they may get their parms. We need to be
339 this conservative because, like the store motion pass, we don't
340 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
341 Moreover, we need to distinguish two cases:
342 1. Before reload (register elimination), the stores related to
343 outgoing arguments are stack pointer based and thus deemed
344 of non-constant base in this pass. This requires special
345 handling but also means that the frame pointer based stores
346 need not be killed upon encountering a const function call.
347 2. After reload, the stores related to outgoing arguments can be
348 either stack pointer or hard frame pointer based. This means
349 that we have no other choice than also killing all the frame
350 pointer based stores upon encountering a const function call.
351 This field is set after reload for const function calls. Having
352 this set is less severe than a wild read, it just means that all
353 the frame related stores are killed rather than all the stores. */
354 bool frame_read;
356 /* This field is only used for the processing of const functions.
357 It is set if the insn may contain a stack pointer based store. */
358 bool stack_pointer_based;
360 /* This is true if any of the sets within the store contains a
361 cselib base. Such stores can only be deleted by the local
362 algorithm. */
363 bool contains_cselib_groups;
365 /* The insn. */
366 rtx insn;
368 /* The list of mem sets or mem clobbers that are contained in this
369 insn. If the insn is deletable, it contains only one mem set.
370 But it could also contain clobbers. Insns that contain more than
371 one mem set are not deletable, but each of those mems are here in
372 order to provide info to delete other insns. */
373 store_info_t store_rec;
375 /* The linked list of mem uses in this insn. Only the reads from
376 rtx bases are listed here. The reads to cselib bases are
377 completely processed during the first scan and so are never
378 created. */
379 read_info_t read_rec;
381 /* The live fixed registers. We assume only fixed registers can
382 cause trouble by being clobbered from an expanded pattern;
383 storing only the live fixed registers (rather than all registers)
384 means less memory needs to be allocated / copied for the individual
385 stores. */
386 regset fixed_regs_live;
388 /* The prev insn in the basic block. */
389 struct insn_info * prev_insn;
391 /* The linked list of insns that are in consideration for removal in
392 the forwards pass thru the basic block. This pointer may be
393 trash as it is not cleared when a wild read occurs. The only
394 time it is guaranteed to be correct is when the traversal starts
395 at active_local_stores. */
396 struct insn_info * next_local_store;
399 typedef struct insn_info *insn_info_t;
400 static alloc_pool insn_info_pool;
402 /* The linked list of stores that are under consideration in this
403 basic block. */
404 static insn_info_t active_local_stores;
405 static int active_local_stores_len;
407 struct bb_info
410 /* Pointer to the insn info for the last insn in the block. These
411 are linked so this is how all of the insns are reached. During
412 scanning this is the current insn being scanned. */
413 insn_info_t last_insn;
415 /* The info for the global dataflow problem. */
418 /* This is set if the transfer function should and in the wild_read
419 bitmap before applying the kill and gen sets. That vector knocks
420 out most of the bits in the bitmap and thus speeds up the
421 operations. */
422 bool apply_wild_read;
424 /* The following 4 bitvectors hold information about which positions
425 of which stores are live or dead. They are indexed by
426 get_bitmap_index. */
428 /* The set of store positions that exist in this block before a wild read. */
429 bitmap gen;
431 /* The set of load positions that exist in this block above the
432 same position of a store. */
433 bitmap kill;
435 /* The set of stores that reach the top of the block without being
436 killed by a read.
438 Do not represent the in if it is all ones. Note that this is
439 what the bitvector should logically be initialized to for a set
440 intersection problem. However, like the kill set, this is too
441 expensive. So initially, the in set will only be created for the
442 exit block and any block that contains a wild read. */
443 bitmap in;
445 /* The set of stores that reach the bottom of the block from it's
446 successors.
448 Do not represent the in if it is all ones. Note that this is
449 what the bitvector should logically be initialized to for a set
450 intersection problem. However, like the kill and in set, this is
451 too expensive. So what is done is that the confluence operator
452 just initializes the vector from one of the out sets of the
453 successors of the block. */
454 bitmap out;
456 /* The following bitvector is indexed by the reg number. It
457 contains the set of regs that are live at the current instruction
458 being processed. While it contains info for all of the
459 registers, only the hard registers are actually examined. It is used
460 to assure that shift and/or add sequences that are inserted do not
461 accidently clobber live hard regs. */
462 bitmap regs_live;
465 typedef struct bb_info *bb_info_t;
466 static alloc_pool bb_info_pool;
468 /* Table to hold all bb_infos. */
469 static bb_info_t *bb_table;
471 /* There is a group_info for each rtx base that is used to reference
472 memory. There are also not many of the rtx bases because they are
473 very limited in scope. */
475 struct group_info
477 /* The actual base of the address. */
478 rtx rtx_base;
480 /* The sequential id of the base. This allows us to have a
481 canonical ordering of these that is not based on addresses. */
482 int id;
484 /* True if there are any positions that are to be processed
485 globally. */
486 bool process_globally;
488 /* True if the base of this group is either the frame_pointer or
489 hard_frame_pointer. */
490 bool frame_related;
492 /* A mem wrapped around the base pointer for the group in order to do
493 read dependency. It must be given BLKmode in order to encompass all
494 the possible offsets from the base. */
495 rtx base_mem;
497 /* Canonized version of base_mem's address. */
498 rtx canon_base_addr;
500 /* These two sets of two bitmaps are used to keep track of how many
501 stores are actually referencing that position from this base. We
502 only do this for rtx bases as this will be used to assign
503 positions in the bitmaps for the global problem. Bit N is set in
504 store1 on the first store for offset N. Bit N is set in store2
505 for the second store to offset N. This is all we need since we
506 only care about offsets that have two or more stores for them.
508 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
509 for 0 and greater offsets.
511 There is one special case here, for stores into the stack frame,
512 we will or store1 into store2 before deciding which stores look
513 at globally. This is because stores to the stack frame that have
514 no other reads before the end of the function can also be
515 deleted. */
516 bitmap store1_n, store1_p, store2_n, store2_p;
518 /* These bitmaps keep track of offsets in this group escape this function.
519 An offset escapes if it corresponds to a named variable whose
520 addressable flag is set. */
521 bitmap escaped_n, escaped_p;
523 /* The positions in this bitmap have the same assignments as the in,
524 out, gen and kill bitmaps. This bitmap is all zeros except for
525 the positions that are occupied by stores for this group. */
526 bitmap group_kill;
528 /* The offset_map is used to map the offsets from this base into
529 positions in the global bitmaps. It is only created after all of
530 the all of stores have been scanned and we know which ones we
531 care about. */
532 int *offset_map_n, *offset_map_p;
533 int offset_map_size_n, offset_map_size_p;
535 typedef struct group_info *group_info_t;
536 typedef const struct group_info *const_group_info_t;
537 static alloc_pool rtx_group_info_pool;
539 /* Tables of group_info structures, hashed by base value. */
540 static htab_t rtx_group_table;
542 /* Index into the rtx_group_vec. */
543 static int rtx_group_next_id;
545 DEF_VEC_P(group_info_t);
546 DEF_VEC_ALLOC_P(group_info_t,heap);
548 static VEC(group_info_t,heap) *rtx_group_vec;
551 /* This structure holds the set of changes that are being deferred
552 when removing read operation. See replace_read. */
553 struct deferred_change
556 /* The mem that is being replaced. */
557 rtx *loc;
559 /* The reg it is being replaced with. */
560 rtx reg;
562 struct deferred_change *next;
565 typedef struct deferred_change *deferred_change_t;
566 static alloc_pool deferred_change_pool;
568 static deferred_change_t deferred_change_list = NULL;
570 /* This are used to hold the alias sets of spill variables. Since
571 these are never aliased and there may be a lot of them, it makes
572 sense to treat them specially. This bitvector is only allocated in
573 calls from dse_record_singleton_alias_set which currently is only
574 made during reload1. So when dse is called before reload this
575 mechanism does nothing. */
577 static bitmap clear_alias_sets = NULL;
579 /* The set of clear_alias_sets that have been disqualified because
580 there are loads or stores using a different mode than the alias set
581 was registered with. */
582 static bitmap disqualified_clear_alias_sets = NULL;
584 /* The group that holds all of the clear_alias_sets. */
585 static group_info_t clear_alias_group;
587 /* The modes of the clear_alias_sets. */
588 static htab_t clear_alias_mode_table;
590 /* Hash table element to look up the mode for an alias set. */
591 struct clear_alias_mode_holder
593 alias_set_type alias_set;
594 enum machine_mode mode;
597 static alloc_pool clear_alias_mode_pool;
599 /* This is true except if cfun->stdarg -- i.e. we cannot do
600 this for vararg functions because they play games with the frame. */
601 static bool stores_off_frame_dead_at_return;
603 /* Counter for stats. */
604 static int globally_deleted;
605 static int locally_deleted;
606 static int spill_deleted;
608 static bitmap all_blocks;
610 /* Locations that are killed by calls in the global phase. */
611 static bitmap kill_on_calls;
613 /* The number of bits used in the global bitmaps. */
614 static unsigned int current_position;
617 static bool gate_dse (void);
618 static bool gate_dse1 (void);
619 static bool gate_dse2 (void);
622 /*----------------------------------------------------------------------------
623 Zeroth step.
625 Initialization.
626 ----------------------------------------------------------------------------*/
628 /* Hashtable callbacks for maintaining the "bases" field of
629 store_group_info, given that the addresses are function invariants. */
631 static int
632 clear_alias_mode_eq (const void *p1, const void *p2)
634 const struct clear_alias_mode_holder * h1
635 = (const struct clear_alias_mode_holder *) p1;
636 const struct clear_alias_mode_holder * h2
637 = (const struct clear_alias_mode_holder *) p2;
638 return h1->alias_set == h2->alias_set;
642 static hashval_t
643 clear_alias_mode_hash (const void *p)
645 const struct clear_alias_mode_holder *holder
646 = (const struct clear_alias_mode_holder *) p;
647 return holder->alias_set;
651 /* Find the entry associated with ALIAS_SET. */
653 static struct clear_alias_mode_holder *
654 clear_alias_set_lookup (alias_set_type alias_set)
656 struct clear_alias_mode_holder tmp_holder;
657 void **slot;
659 tmp_holder.alias_set = alias_set;
660 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
661 gcc_assert (*slot);
663 return (struct clear_alias_mode_holder *) *slot;
667 /* Hashtable callbacks for maintaining the "bases" field of
668 store_group_info, given that the addresses are function invariants. */
670 static int
671 invariant_group_base_eq (const void *p1, const void *p2)
673 const_group_info_t gi1 = (const_group_info_t) p1;
674 const_group_info_t gi2 = (const_group_info_t) p2;
675 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
679 static hashval_t
680 invariant_group_base_hash (const void *p)
682 const_group_info_t gi = (const_group_info_t) p;
683 int do_not_record;
684 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
688 /* Get the GROUP for BASE. Add a new group if it is not there. */
690 static group_info_t
691 get_group_info (rtx base)
693 struct group_info tmp_gi;
694 group_info_t gi;
695 void **slot;
697 if (base)
699 /* Find the store_base_info structure for BASE, creating a new one
700 if necessary. */
701 tmp_gi.rtx_base = base;
702 slot = htab_find_slot (rtx_group_table, &tmp_gi, INSERT);
703 gi = (group_info_t) *slot;
705 else
707 if (!clear_alias_group)
709 clear_alias_group = gi =
710 (group_info_t) pool_alloc (rtx_group_info_pool);
711 memset (gi, 0, sizeof (struct group_info));
712 gi->id = rtx_group_next_id++;
713 gi->store1_n = BITMAP_ALLOC (NULL);
714 gi->store1_p = BITMAP_ALLOC (NULL);
715 gi->store2_n = BITMAP_ALLOC (NULL);
716 gi->store2_p = BITMAP_ALLOC (NULL);
717 gi->escaped_p = BITMAP_ALLOC (NULL);
718 gi->escaped_n = BITMAP_ALLOC (NULL);
719 gi->group_kill = BITMAP_ALLOC (NULL);
720 gi->process_globally = false;
721 gi->offset_map_size_n = 0;
722 gi->offset_map_size_p = 0;
723 gi->offset_map_n = NULL;
724 gi->offset_map_p = NULL;
725 VEC_safe_push (group_info_t, heap, rtx_group_vec, gi);
727 return clear_alias_group;
730 if (gi == NULL)
732 *slot = gi = (group_info_t) pool_alloc (rtx_group_info_pool);
733 gi->rtx_base = base;
734 gi->id = rtx_group_next_id++;
735 gi->base_mem = gen_rtx_MEM (BLKmode, base);
736 gi->canon_base_addr = canon_rtx (base);
737 gi->store1_n = BITMAP_ALLOC (NULL);
738 gi->store1_p = BITMAP_ALLOC (NULL);
739 gi->store2_n = BITMAP_ALLOC (NULL);
740 gi->store2_p = BITMAP_ALLOC (NULL);
741 gi->escaped_p = BITMAP_ALLOC (NULL);
742 gi->escaped_n = BITMAP_ALLOC (NULL);
743 gi->group_kill = BITMAP_ALLOC (NULL);
744 gi->process_globally = false;
745 gi->frame_related =
746 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
747 gi->offset_map_size_n = 0;
748 gi->offset_map_size_p = 0;
749 gi->offset_map_n = NULL;
750 gi->offset_map_p = NULL;
751 VEC_safe_push (group_info_t, heap, rtx_group_vec, gi);
754 return gi;
758 /* Initialization of data structures. */
760 static void
761 dse_step0 (void)
763 locally_deleted = 0;
764 globally_deleted = 0;
765 spill_deleted = 0;
767 scratch = BITMAP_ALLOC (NULL);
768 kill_on_calls = BITMAP_ALLOC (NULL);
770 rtx_store_info_pool
771 = create_alloc_pool ("rtx_store_info_pool",
772 sizeof (struct store_info), 100);
773 read_info_pool
774 = create_alloc_pool ("read_info_pool",
775 sizeof (struct read_info), 100);
776 insn_info_pool
777 = create_alloc_pool ("insn_info_pool",
778 sizeof (struct insn_info), 100);
779 bb_info_pool
780 = create_alloc_pool ("bb_info_pool",
781 sizeof (struct bb_info), 100);
782 rtx_group_info_pool
783 = create_alloc_pool ("rtx_group_info_pool",
784 sizeof (struct group_info), 100);
785 deferred_change_pool
786 = create_alloc_pool ("deferred_change_pool",
787 sizeof (struct deferred_change), 10);
789 rtx_group_table = htab_create (11, invariant_group_base_hash,
790 invariant_group_base_eq, NULL);
792 bb_table = XCNEWVEC (bb_info_t, last_basic_block);
793 rtx_group_next_id = 0;
795 stores_off_frame_dead_at_return = !cfun->stdarg;
797 init_alias_analysis ();
799 if (clear_alias_sets)
800 clear_alias_group = get_group_info (NULL);
801 else
802 clear_alias_group = NULL;
807 /*----------------------------------------------------------------------------
808 First step.
810 Scan all of the insns. Any random ordering of the blocks is fine.
811 Each block is scanned in forward order to accommodate cselib which
812 is used to remove stores with non-constant bases.
813 ----------------------------------------------------------------------------*/
815 /* Delete all of the store_info recs from INSN_INFO. */
817 static void
818 free_store_info (insn_info_t insn_info)
820 store_info_t store_info = insn_info->store_rec;
821 while (store_info)
823 store_info_t next = store_info->next;
824 if (store_info->is_large)
825 BITMAP_FREE (store_info->positions_needed.large.bmap);
826 if (store_info->cse_base)
827 pool_free (cse_store_info_pool, store_info);
828 else
829 pool_free (rtx_store_info_pool, store_info);
830 store_info = next;
833 insn_info->cannot_delete = true;
834 insn_info->contains_cselib_groups = false;
835 insn_info->store_rec = NULL;
838 typedef struct
840 rtx first, current;
841 regset fixed_regs_live;
842 bool failure;
843 } note_add_store_info;
845 /* Callback for emit_inc_dec_insn_before via note_stores.
846 Check if a register is clobbered which is live afterwards. */
848 static void
849 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
851 rtx insn;
852 note_add_store_info *info = (note_add_store_info *) data;
853 int r, n;
855 if (!REG_P (loc))
856 return;
858 /* If this register is referenced by the current or an earlier insn,
859 that's OK. E.g. this applies to the register that is being incremented
860 with this addition. */
861 for (insn = info->first;
862 insn != NEXT_INSN (info->current);
863 insn = NEXT_INSN (insn))
864 if (reg_referenced_p (loc, PATTERN (insn)))
865 return;
867 /* If we come here, we have a clobber of a register that's only OK
868 if that register is not live. If we don't have liveness information
869 available, fail now. */
870 if (!info->fixed_regs_live)
872 info->failure = true;
873 return;
875 /* Now check if this is a live fixed register. */
876 r = REGNO (loc);
877 n = hard_regno_nregs[r][GET_MODE (loc)];
878 while (--n >= 0)
879 if (REGNO_REG_SET_P (info->fixed_regs_live, r+n))
880 info->failure = true;
883 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
884 SRC + SRCOFF before insn ARG. */
886 static int
887 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
888 rtx op ATTRIBUTE_UNUSED,
889 rtx dest, rtx src, rtx srcoff, void *arg)
891 insn_info_t insn_info = (insn_info_t) arg;
892 rtx insn = insn_info->insn, new_insn, cur;
893 note_add_store_info info;
895 /* We can reuse all operands without copying, because we are about
896 to delete the insn that contained it. */
897 if (srcoff)
898 new_insn = gen_add3_insn (dest, src, srcoff);
899 else
900 new_insn = gen_move_insn (dest, src);
901 info.first = new_insn;
902 info.fixed_regs_live = insn_info->fixed_regs_live;
903 info.failure = false;
904 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
906 info.current = cur;
907 note_stores (PATTERN (cur), note_add_store, &info);
910 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
911 return it immediately, communicating the failure to its caller. */
912 if (info.failure)
913 return 1;
915 emit_insn_before (new_insn, insn);
917 return -1;
920 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
921 is there, is split into a separate insn.
922 Return true on success (or if there was nothing to do), false on failure. */
924 static bool
925 check_for_inc_dec_1 (insn_info_t insn_info)
927 rtx insn = insn_info->insn;
928 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
929 if (note)
930 return for_each_inc_dec (&insn, emit_inc_dec_insn_before, insn_info) == 0;
931 return true;
935 /* Entry point for postreload. If you work on reload_cse, or you need this
936 anywhere else, consider if you can provide register liveness information
937 and add a parameter to this function so that it can be passed down in
938 insn_info.fixed_regs_live. */
939 bool
940 check_for_inc_dec (rtx insn)
942 struct insn_info insn_info;
943 rtx note;
945 insn_info.insn = insn;
946 insn_info.fixed_regs_live = NULL;
947 note = find_reg_note (insn, REG_INC, NULL_RTX);
948 if (note)
949 return for_each_inc_dec (&insn, emit_inc_dec_insn_before, &insn_info) == 0;
950 return true;
953 /* Delete the insn and free all of the fields inside INSN_INFO. */
955 static void
956 delete_dead_store_insn (insn_info_t insn_info)
958 read_info_t read_info;
960 if (!dbg_cnt (dse))
961 return;
963 if (!check_for_inc_dec_1 (insn_info))
964 return;
965 if (dump_file)
967 fprintf (dump_file, "Locally deleting insn %d ",
968 INSN_UID (insn_info->insn));
969 if (insn_info->store_rec->alias_set)
970 fprintf (dump_file, "alias set %d\n",
971 (int) insn_info->store_rec->alias_set);
972 else
973 fprintf (dump_file, "\n");
976 free_store_info (insn_info);
977 read_info = insn_info->read_rec;
979 while (read_info)
981 read_info_t next = read_info->next;
982 pool_free (read_info_pool, read_info);
983 read_info = next;
985 insn_info->read_rec = NULL;
987 delete_insn (insn_info->insn);
988 locally_deleted++;
989 insn_info->insn = NULL;
991 insn_info->wild_read = false;
994 /* Check if EXPR can possibly escape the current function scope. */
995 static bool
996 can_escape (tree expr)
998 tree base;
999 if (!expr)
1000 return true;
1001 base = get_base_address (expr);
1002 if (DECL_P (base)
1003 && !may_be_aliased (base))
1004 return false;
1005 return true;
1008 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1009 OFFSET and WIDTH. */
1011 static void
1012 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
1013 tree expr)
1015 HOST_WIDE_INT i;
1016 bool expr_escapes = can_escape (expr);
1017 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
1018 for (i=offset; i<offset+width; i++)
1020 bitmap store1;
1021 bitmap store2;
1022 bitmap escaped;
1023 int ai;
1024 if (i < 0)
1026 store1 = group->store1_n;
1027 store2 = group->store2_n;
1028 escaped = group->escaped_n;
1029 ai = -i;
1031 else
1033 store1 = group->store1_p;
1034 store2 = group->store2_p;
1035 escaped = group->escaped_p;
1036 ai = i;
1039 if (!bitmap_set_bit (store1, ai))
1040 bitmap_set_bit (store2, ai);
1041 else
1043 if (i < 0)
1045 if (group->offset_map_size_n < ai)
1046 group->offset_map_size_n = ai;
1048 else
1050 if (group->offset_map_size_p < ai)
1051 group->offset_map_size_p = ai;
1054 if (expr_escapes)
1055 bitmap_set_bit (escaped, ai);
1059 static void
1060 reset_active_stores (void)
1062 active_local_stores = NULL;
1063 active_local_stores_len = 0;
1066 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1068 static void
1069 free_read_records (bb_info_t bb_info)
1071 insn_info_t insn_info = bb_info->last_insn;
1072 read_info_t *ptr = &insn_info->read_rec;
1073 while (*ptr)
1075 read_info_t next = (*ptr)->next;
1076 if ((*ptr)->alias_set == 0)
1078 pool_free (read_info_pool, *ptr);
1079 *ptr = next;
1081 else
1082 ptr = &(*ptr)->next;
1086 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1088 static void
1089 add_wild_read (bb_info_t bb_info)
1091 insn_info_t insn_info = bb_info->last_insn;
1092 insn_info->wild_read = true;
1093 free_read_records (bb_info);
1094 reset_active_stores ();
1097 /* Set the BB_INFO so that the last insn is marked as a wild read of
1098 non-frame locations. */
1100 static void
1101 add_non_frame_wild_read (bb_info_t bb_info)
1103 insn_info_t insn_info = bb_info->last_insn;
1104 insn_info->non_frame_wild_read = true;
1105 free_read_records (bb_info);
1106 reset_active_stores ();
1109 /* Return true if X is a constant or one of the registers that behave
1110 as a constant over the life of a function. This is equivalent to
1111 !rtx_varies_p for memory addresses. */
1113 static bool
1114 const_or_frame_p (rtx x)
1116 switch (GET_CODE (x))
1118 case CONST:
1119 case CONST_INT:
1120 case CONST_DOUBLE:
1121 case CONST_VECTOR:
1122 case SYMBOL_REF:
1123 case LABEL_REF:
1124 return true;
1126 case REG:
1127 /* Note that we have to test for the actual rtx used for the frame
1128 and arg pointers and not just the register number in case we have
1129 eliminated the frame and/or arg pointer and are using it
1130 for pseudos. */
1131 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1132 /* The arg pointer varies if it is not a fixed register. */
1133 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1134 || x == pic_offset_table_rtx)
1135 return true;
1136 return false;
1138 default:
1139 return false;
1143 /* Take all reasonable action to put the address of MEM into the form
1144 that we can do analysis on.
1146 The gold standard is to get the address into the form: address +
1147 OFFSET where address is something that rtx_varies_p considers a
1148 constant. When we can get the address in this form, we can do
1149 global analysis on it. Note that for constant bases, address is
1150 not actually returned, only the group_id. The address can be
1151 obtained from that.
1153 If that fails, we try cselib to get a value we can at least use
1154 locally. If that fails we return false.
1156 The GROUP_ID is set to -1 for cselib bases and the index of the
1157 group for non_varying bases.
1159 FOR_READ is true if this is a mem read and false if not. */
1161 static bool
1162 canon_address (rtx mem,
1163 alias_set_type *alias_set_out,
1164 int *group_id,
1165 HOST_WIDE_INT *offset,
1166 cselib_val **base)
1168 enum machine_mode address_mode
1169 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
1170 rtx mem_address = XEXP (mem, 0);
1171 rtx expanded_address, address;
1172 int expanded;
1174 /* Make sure that cselib is has initialized all of the operands of
1175 the address before asking it to do the subst. */
1177 if (clear_alias_sets)
1179 /* If this is a spill, do not do any further processing. */
1180 alias_set_type alias_set = MEM_ALIAS_SET (mem);
1181 if (dump_file)
1182 fprintf (dump_file, "found alias set %d\n", (int) alias_set);
1183 if (bitmap_bit_p (clear_alias_sets, alias_set))
1185 struct clear_alias_mode_holder *entry
1186 = clear_alias_set_lookup (alias_set);
1188 /* If the modes do not match, we cannot process this set. */
1189 if (entry->mode != GET_MODE (mem))
1191 if (dump_file)
1192 fprintf (dump_file,
1193 "disqualifying alias set %d, (%s) != (%s)\n",
1194 (int) alias_set, GET_MODE_NAME (entry->mode),
1195 GET_MODE_NAME (GET_MODE (mem)));
1197 bitmap_set_bit (disqualified_clear_alias_sets, alias_set);
1198 return false;
1201 *alias_set_out = alias_set;
1202 *group_id = clear_alias_group->id;
1203 return true;
1207 *alias_set_out = 0;
1209 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1211 if (dump_file)
1213 fprintf (dump_file, " mem: ");
1214 print_inline_rtx (dump_file, mem_address, 0);
1215 fprintf (dump_file, "\n");
1218 /* First see if just canon_rtx (mem_address) is const or frame,
1219 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1220 address = NULL_RTX;
1221 for (expanded = 0; expanded < 2; expanded++)
1223 if (expanded)
1225 /* Use cselib to replace all of the reg references with the full
1226 expression. This will take care of the case where we have
1228 r_x = base + offset;
1229 val = *r_x;
1231 by making it into
1233 val = *(base + offset); */
1235 expanded_address = cselib_expand_value_rtx (mem_address,
1236 scratch, 5);
1238 /* If this fails, just go with the address from first
1239 iteration. */
1240 if (!expanded_address)
1241 break;
1243 else
1244 expanded_address = mem_address;
1246 /* Split the address into canonical BASE + OFFSET terms. */
1247 address = canon_rtx (expanded_address);
1249 *offset = 0;
1251 if (dump_file)
1253 if (expanded)
1255 fprintf (dump_file, "\n after cselib_expand address: ");
1256 print_inline_rtx (dump_file, expanded_address, 0);
1257 fprintf (dump_file, "\n");
1260 fprintf (dump_file, "\n after canon_rtx address: ");
1261 print_inline_rtx (dump_file, address, 0);
1262 fprintf (dump_file, "\n");
1265 if (GET_CODE (address) == CONST)
1266 address = XEXP (address, 0);
1268 if (GET_CODE (address) == PLUS
1269 && CONST_INT_P (XEXP (address, 1)))
1271 *offset = INTVAL (XEXP (address, 1));
1272 address = XEXP (address, 0);
1275 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1276 && const_or_frame_p (address))
1278 group_info_t group = get_group_info (address);
1280 if (dump_file)
1281 fprintf (dump_file, " gid=%d offset=%d \n",
1282 group->id, (int)*offset);
1283 *base = NULL;
1284 *group_id = group->id;
1285 return true;
1289 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1290 *group_id = -1;
1292 if (*base == NULL)
1294 if (dump_file)
1295 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1296 return false;
1298 if (dump_file)
1299 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1300 (*base)->uid, (*base)->hash, (int)*offset);
1301 return true;
1305 /* Clear the rhs field from the active_local_stores array. */
1307 static void
1308 clear_rhs_from_active_local_stores (void)
1310 insn_info_t ptr = active_local_stores;
1312 while (ptr)
1314 store_info_t store_info = ptr->store_rec;
1315 /* Skip the clobbers. */
1316 while (!store_info->is_set)
1317 store_info = store_info->next;
1319 store_info->rhs = NULL;
1320 store_info->const_rhs = NULL;
1322 ptr = ptr->next_local_store;
1327 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1329 static inline void
1330 set_position_unneeded (store_info_t s_info, int pos)
1332 if (__builtin_expect (s_info->is_large, false))
1334 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1335 s_info->positions_needed.large.count++;
1337 else
1338 s_info->positions_needed.small_bitmask
1339 &= ~(((unsigned HOST_WIDE_INT) 1) << pos);
1342 /* Mark the whole store S_INFO as unneeded. */
1344 static inline void
1345 set_all_positions_unneeded (store_info_t s_info)
1347 if (__builtin_expect (s_info->is_large, false))
1349 int pos, end = s_info->end - s_info->begin;
1350 for (pos = 0; pos < end; pos++)
1351 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1352 s_info->positions_needed.large.count = end;
1354 else
1355 s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0;
1358 /* Return TRUE if any bytes from S_INFO store are needed. */
1360 static inline bool
1361 any_positions_needed_p (store_info_t s_info)
1363 if (__builtin_expect (s_info->is_large, false))
1364 return (s_info->positions_needed.large.count
1365 < s_info->end - s_info->begin);
1366 else
1367 return (s_info->positions_needed.small_bitmask
1368 != (unsigned HOST_WIDE_INT) 0);
1371 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1372 store are needed. */
1374 static inline bool
1375 all_positions_needed_p (store_info_t s_info, int start, int width)
1377 if (__builtin_expect (s_info->is_large, false))
1379 int end = start + width;
1380 while (start < end)
1381 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1382 return false;
1383 return true;
1385 else
1387 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1388 return (s_info->positions_needed.small_bitmask & mask) == mask;
1393 static rtx get_stored_val (store_info_t, enum machine_mode, HOST_WIDE_INT,
1394 HOST_WIDE_INT, basic_block, bool);
1397 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1398 there is a candidate store, after adding it to the appropriate
1399 local store group if so. */
1401 static int
1402 record_store (rtx body, bb_info_t bb_info)
1404 rtx mem, rhs, const_rhs, mem_addr;
1405 HOST_WIDE_INT offset = 0;
1406 HOST_WIDE_INT width = 0;
1407 alias_set_type spill_alias_set;
1408 insn_info_t insn_info = bb_info->last_insn;
1409 store_info_t store_info = NULL;
1410 int group_id;
1411 cselib_val *base = NULL;
1412 insn_info_t ptr, last, redundant_reason;
1413 bool store_is_unused;
1415 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1416 return 0;
1418 mem = SET_DEST (body);
1420 /* If this is not used, then this cannot be used to keep the insn
1421 from being deleted. On the other hand, it does provide something
1422 that can be used to prove that another store is dead. */
1423 store_is_unused
1424 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1426 /* Check whether that value is a suitable memory location. */
1427 if (!MEM_P (mem))
1429 /* If the set or clobber is unused, then it does not effect our
1430 ability to get rid of the entire insn. */
1431 if (!store_is_unused)
1432 insn_info->cannot_delete = true;
1433 return 0;
1436 /* At this point we know mem is a mem. */
1437 if (GET_MODE (mem) == BLKmode)
1439 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1441 if (dump_file)
1442 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1443 add_wild_read (bb_info);
1444 insn_info->cannot_delete = true;
1445 return 0;
1447 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1448 as memset (addr, 0, 36); */
1449 else if (!MEM_SIZE_KNOWN_P (mem)
1450 || MEM_SIZE (mem) <= 0
1451 || MEM_SIZE (mem) > MAX_OFFSET
1452 || GET_CODE (body) != SET
1453 || !CONST_INT_P (SET_SRC (body)))
1455 if (!store_is_unused)
1457 /* If the set or clobber is unused, then it does not effect our
1458 ability to get rid of the entire insn. */
1459 insn_info->cannot_delete = true;
1460 clear_rhs_from_active_local_stores ();
1462 return 0;
1466 /* We can still process a volatile mem, we just cannot delete it. */
1467 if (MEM_VOLATILE_P (mem))
1468 insn_info->cannot_delete = true;
1470 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1472 clear_rhs_from_active_local_stores ();
1473 return 0;
1476 if (GET_MODE (mem) == BLKmode)
1477 width = MEM_SIZE (mem);
1478 else
1480 width = GET_MODE_SIZE (GET_MODE (mem));
1481 gcc_assert ((unsigned) width <= HOST_BITS_PER_WIDE_INT);
1484 if (spill_alias_set)
1486 bitmap store1 = clear_alias_group->store1_p;
1487 bitmap store2 = clear_alias_group->store2_p;
1489 gcc_assert (GET_MODE (mem) != BLKmode);
1491 if (!bitmap_set_bit (store1, spill_alias_set))
1492 bitmap_set_bit (store2, spill_alias_set);
1494 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1495 clear_alias_group->offset_map_size_p = spill_alias_set;
1497 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1499 if (dump_file)
1500 fprintf (dump_file, " processing spill store %d(%s)\n",
1501 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1503 else if (group_id >= 0)
1505 /* In the restrictive case where the base is a constant or the
1506 frame pointer we can do global analysis. */
1508 group_info_t group
1509 = VEC_index (group_info_t, rtx_group_vec, group_id);
1510 tree expr = MEM_EXPR (mem);
1512 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1513 set_usage_bits (group, offset, width, expr);
1515 if (dump_file)
1516 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1517 group_id, (int)offset, (int)(offset+width));
1519 else
1521 rtx base_term = find_base_term (XEXP (mem, 0));
1522 if (!base_term
1523 || (GET_CODE (base_term) == ADDRESS
1524 && GET_MODE (base_term) == Pmode
1525 && XEXP (base_term, 0) == stack_pointer_rtx))
1526 insn_info->stack_pointer_based = true;
1527 insn_info->contains_cselib_groups = true;
1529 store_info = (store_info_t) pool_alloc (cse_store_info_pool);
1530 group_id = -1;
1532 if (dump_file)
1533 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1534 (int)offset, (int)(offset+width));
1537 const_rhs = rhs = NULL_RTX;
1538 if (GET_CODE (body) == SET
1539 /* No place to keep the value after ra. */
1540 && !reload_completed
1541 && (REG_P (SET_SRC (body))
1542 || GET_CODE (SET_SRC (body)) == SUBREG
1543 || CONSTANT_P (SET_SRC (body)))
1544 && !MEM_VOLATILE_P (mem)
1545 /* Sometimes the store and reload is used for truncation and
1546 rounding. */
1547 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1549 rhs = SET_SRC (body);
1550 if (CONSTANT_P (rhs))
1551 const_rhs = rhs;
1552 else if (body == PATTERN (insn_info->insn))
1554 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1555 if (tem && CONSTANT_P (XEXP (tem, 0)))
1556 const_rhs = XEXP (tem, 0);
1558 if (const_rhs == NULL_RTX && REG_P (rhs))
1560 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1562 if (tem && CONSTANT_P (tem))
1563 const_rhs = tem;
1567 /* Check to see if this stores causes some other stores to be
1568 dead. */
1569 ptr = active_local_stores;
1570 last = NULL;
1571 redundant_reason = NULL;
1572 mem = canon_rtx (mem);
1573 /* For alias_set != 0 canon_true_dependence should be never called. */
1574 if (spill_alias_set)
1575 mem_addr = NULL_RTX;
1576 else
1578 if (group_id < 0)
1579 mem_addr = base->val_rtx;
1580 else
1582 group_info_t group
1583 = VEC_index (group_info_t, rtx_group_vec, group_id);
1584 mem_addr = group->canon_base_addr;
1586 if (offset)
1587 mem_addr = plus_constant (mem_addr, offset);
1590 while (ptr)
1592 insn_info_t next = ptr->next_local_store;
1593 store_info_t s_info = ptr->store_rec;
1594 bool del = true;
1596 /* Skip the clobbers. We delete the active insn if this insn
1597 shadows the set. To have been put on the active list, it
1598 has exactly on set. */
1599 while (!s_info->is_set)
1600 s_info = s_info->next;
1602 if (s_info->alias_set != spill_alias_set)
1603 del = false;
1604 else if (s_info->alias_set)
1606 struct clear_alias_mode_holder *entry
1607 = clear_alias_set_lookup (s_info->alias_set);
1608 /* Generally, spills cannot be processed if and of the
1609 references to the slot have a different mode. But if
1610 we are in the same block and mode is exactly the same
1611 between this store and one before in the same block,
1612 we can still delete it. */
1613 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1614 && (GET_MODE (mem) == entry->mode))
1616 del = true;
1617 set_all_positions_unneeded (s_info);
1619 if (dump_file)
1620 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1621 INSN_UID (ptr->insn), (int) s_info->alias_set);
1623 else if ((s_info->group_id == group_id)
1624 && (s_info->cse_base == base))
1626 HOST_WIDE_INT i;
1627 if (dump_file)
1628 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1629 INSN_UID (ptr->insn), s_info->group_id,
1630 (int)s_info->begin, (int)s_info->end);
1632 /* Even if PTR won't be eliminated as unneeded, if both
1633 PTR and this insn store the same constant value, we might
1634 eliminate this insn instead. */
1635 if (s_info->const_rhs
1636 && const_rhs
1637 && offset >= s_info->begin
1638 && offset + width <= s_info->end
1639 && all_positions_needed_p (s_info, offset - s_info->begin,
1640 width))
1642 if (GET_MODE (mem) == BLKmode)
1644 if (GET_MODE (s_info->mem) == BLKmode
1645 && s_info->const_rhs == const_rhs)
1646 redundant_reason = ptr;
1648 else if (s_info->const_rhs == const0_rtx
1649 && const_rhs == const0_rtx)
1650 redundant_reason = ptr;
1651 else
1653 rtx val;
1654 start_sequence ();
1655 val = get_stored_val (s_info, GET_MODE (mem),
1656 offset, offset + width,
1657 BLOCK_FOR_INSN (insn_info->insn),
1658 true);
1659 if (get_insns () != NULL)
1660 val = NULL_RTX;
1661 end_sequence ();
1662 if (val && rtx_equal_p (val, const_rhs))
1663 redundant_reason = ptr;
1667 for (i = MAX (offset, s_info->begin);
1668 i < offset + width && i < s_info->end;
1669 i++)
1670 set_position_unneeded (s_info, i - s_info->begin);
1672 else if (s_info->rhs)
1673 /* Need to see if it is possible for this store to overwrite
1674 the value of store_info. If it is, set the rhs to NULL to
1675 keep it from being used to remove a load. */
1677 if (canon_true_dependence (s_info->mem,
1678 GET_MODE (s_info->mem),
1679 s_info->mem_addr,
1680 mem, mem_addr, rtx_varies_p))
1682 s_info->rhs = NULL;
1683 s_info->const_rhs = NULL;
1687 /* An insn can be deleted if every position of every one of
1688 its s_infos is zero. */
1689 if (any_positions_needed_p (s_info))
1690 del = false;
1692 if (del)
1694 insn_info_t insn_to_delete = ptr;
1696 active_local_stores_len--;
1697 if (last)
1698 last->next_local_store = ptr->next_local_store;
1699 else
1700 active_local_stores = ptr->next_local_store;
1702 if (!insn_to_delete->cannot_delete)
1703 delete_dead_store_insn (insn_to_delete);
1705 else
1706 last = ptr;
1708 ptr = next;
1711 /* Finish filling in the store_info. */
1712 store_info->next = insn_info->store_rec;
1713 insn_info->store_rec = store_info;
1714 store_info->mem = mem;
1715 store_info->alias_set = spill_alias_set;
1716 store_info->mem_addr = mem_addr;
1717 store_info->cse_base = base;
1718 if (width > HOST_BITS_PER_WIDE_INT)
1720 store_info->is_large = true;
1721 store_info->positions_needed.large.count = 0;
1722 store_info->positions_needed.large.bmap = BITMAP_ALLOC (NULL);
1724 else
1726 store_info->is_large = false;
1727 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1729 store_info->group_id = group_id;
1730 store_info->begin = offset;
1731 store_info->end = offset + width;
1732 store_info->is_set = GET_CODE (body) == SET;
1733 store_info->rhs = rhs;
1734 store_info->const_rhs = const_rhs;
1735 store_info->redundant_reason = redundant_reason;
1737 /* If this is a clobber, we return 0. We will only be able to
1738 delete this insn if there is only one store USED store, but we
1739 can use the clobber to delete other stores earlier. */
1740 return store_info->is_set ? 1 : 0;
1744 static void
1745 dump_insn_info (const char * start, insn_info_t insn_info)
1747 fprintf (dump_file, "%s insn=%d %s\n", start,
1748 INSN_UID (insn_info->insn),
1749 insn_info->store_rec ? "has store" : "naked");
1753 /* If the modes are different and the value's source and target do not
1754 line up, we need to extract the value from lower part of the rhs of
1755 the store, shift it, and then put it into a form that can be shoved
1756 into the read_insn. This function generates a right SHIFT of a
1757 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1758 shift sequence is returned or NULL if we failed to find a
1759 shift. */
1761 static rtx
1762 find_shift_sequence (int access_size,
1763 store_info_t store_info,
1764 enum machine_mode read_mode,
1765 int shift, bool speed, bool require_cst)
1767 enum machine_mode store_mode = GET_MODE (store_info->mem);
1768 enum machine_mode new_mode;
1769 rtx read_reg = NULL;
1771 /* Some machines like the x86 have shift insns for each size of
1772 operand. Other machines like the ppc or the ia-64 may only have
1773 shift insns that shift values within 32 or 64 bit registers.
1774 This loop tries to find the smallest shift insn that will right
1775 justify the value we want to read but is available in one insn on
1776 the machine. */
1778 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1779 MODE_INT);
1780 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1781 new_mode = GET_MODE_WIDER_MODE (new_mode))
1783 rtx target, new_reg, shift_seq, insn, new_lhs;
1784 int cost;
1786 /* If a constant was stored into memory, try to simplify it here,
1787 otherwise the cost of the shift might preclude this optimization
1788 e.g. at -Os, even when no actual shift will be needed. */
1789 if (store_info->const_rhs)
1791 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1792 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1793 store_mode, byte);
1794 if (ret && CONSTANT_P (ret))
1796 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1797 ret, GEN_INT (shift));
1798 if (ret && CONSTANT_P (ret))
1800 byte = subreg_lowpart_offset (read_mode, new_mode);
1801 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1802 if (ret && CONSTANT_P (ret)
1803 && set_src_cost (ret, speed) <= COSTS_N_INSNS (1))
1804 return ret;
1809 if (require_cst)
1810 return NULL_RTX;
1812 /* Try a wider mode if truncating the store mode to NEW_MODE
1813 requires a real instruction. */
1814 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1815 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1816 continue;
1818 /* Also try a wider mode if the necessary punning is either not
1819 desirable or not possible. */
1820 if (!CONSTANT_P (store_info->rhs)
1821 && !MODES_TIEABLE_P (new_mode, store_mode))
1822 continue;
1824 new_reg = gen_reg_rtx (new_mode);
1826 start_sequence ();
1828 /* In theory we could also check for an ashr. Ian Taylor knows
1829 of one dsp where the cost of these two was not the same. But
1830 this really is a rare case anyway. */
1831 target = expand_binop (new_mode, lshr_optab, new_reg,
1832 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1834 shift_seq = get_insns ();
1835 end_sequence ();
1837 if (target != new_reg || shift_seq == NULL)
1838 continue;
1840 cost = 0;
1841 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1842 if (INSN_P (insn))
1843 cost += insn_rtx_cost (PATTERN (insn), speed);
1845 /* The computation up to here is essentially independent
1846 of the arguments and could be precomputed. It may
1847 not be worth doing so. We could precompute if
1848 worthwhile or at least cache the results. The result
1849 technically depends on both SHIFT and ACCESS_SIZE,
1850 but in practice the answer will depend only on ACCESS_SIZE. */
1852 if (cost > COSTS_N_INSNS (1))
1853 continue;
1855 new_lhs = extract_low_bits (new_mode, store_mode,
1856 copy_rtx (store_info->rhs));
1857 if (new_lhs == NULL_RTX)
1858 continue;
1860 /* We found an acceptable shift. Generate a move to
1861 take the value from the store and put it into the
1862 shift pseudo, then shift it, then generate another
1863 move to put in into the target of the read. */
1864 emit_move_insn (new_reg, new_lhs);
1865 emit_insn (shift_seq);
1866 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1867 break;
1870 return read_reg;
1874 /* Call back for note_stores to find the hard regs set or clobbered by
1875 insn. Data is a bitmap of the hardregs set so far. */
1877 static void
1878 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1880 bitmap regs_set = (bitmap) data;
1882 if (REG_P (x)
1883 && HARD_REGISTER_P (x))
1885 unsigned int regno = REGNO (x);
1886 bitmap_set_range (regs_set, regno,
1887 hard_regno_nregs[regno][GET_MODE (x)]);
1891 /* Helper function for replace_read and record_store.
1892 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1893 to one before READ_END bytes read in READ_MODE. Return NULL
1894 if not successful. If REQUIRE_CST is true, return always constant. */
1896 static rtx
1897 get_stored_val (store_info_t store_info, enum machine_mode read_mode,
1898 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1899 basic_block bb, bool require_cst)
1901 enum machine_mode store_mode = GET_MODE (store_info->mem);
1902 int shift;
1903 int access_size; /* In bytes. */
1904 rtx read_reg;
1906 /* To get here the read is within the boundaries of the write so
1907 shift will never be negative. Start out with the shift being in
1908 bytes. */
1909 if (store_mode == BLKmode)
1910 shift = 0;
1911 else if (BYTES_BIG_ENDIAN)
1912 shift = store_info->end - read_end;
1913 else
1914 shift = read_begin - store_info->begin;
1916 access_size = shift + GET_MODE_SIZE (read_mode);
1918 /* From now on it is bits. */
1919 shift *= BITS_PER_UNIT;
1921 if (shift)
1922 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1923 optimize_bb_for_speed_p (bb),
1924 require_cst);
1925 else if (store_mode == BLKmode)
1927 /* The store is a memset (addr, const_val, const_size). */
1928 gcc_assert (CONST_INT_P (store_info->rhs));
1929 store_mode = int_mode_for_mode (read_mode);
1930 if (store_mode == BLKmode)
1931 read_reg = NULL_RTX;
1932 else if (store_info->rhs == const0_rtx)
1933 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1934 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1935 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1936 read_reg = NULL_RTX;
1937 else
1939 unsigned HOST_WIDE_INT c
1940 = INTVAL (store_info->rhs)
1941 & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
1942 int shift = BITS_PER_UNIT;
1943 while (shift < HOST_BITS_PER_WIDE_INT)
1945 c |= (c << shift);
1946 shift <<= 1;
1948 read_reg = GEN_INT (trunc_int_for_mode (c, store_mode));
1949 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1952 else if (store_info->const_rhs
1953 && (require_cst
1954 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1955 read_reg = extract_low_bits (read_mode, store_mode,
1956 copy_rtx (store_info->const_rhs));
1957 else
1958 read_reg = extract_low_bits (read_mode, store_mode,
1959 copy_rtx (store_info->rhs));
1960 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1961 read_reg = NULL_RTX;
1962 return read_reg;
1965 /* Take a sequence of:
1966 A <- r1
1968 ... <- A
1970 and change it into
1971 r2 <- r1
1972 A <- r1
1974 ... <- r2
1978 r3 <- extract (r1)
1979 r3 <- r3 >> shift
1980 r2 <- extract (r3)
1981 ... <- r2
1985 r2 <- extract (r1)
1986 ... <- r2
1988 Depending on the alignment and the mode of the store and
1989 subsequent load.
1992 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1993 and READ_INSN are for the read. Return true if the replacement
1994 went ok. */
1996 static bool
1997 replace_read (store_info_t store_info, insn_info_t store_insn,
1998 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1999 bitmap regs_live)
2001 enum machine_mode store_mode = GET_MODE (store_info->mem);
2002 enum machine_mode read_mode = GET_MODE (read_info->mem);
2003 rtx insns, this_insn, read_reg;
2004 basic_block bb;
2006 if (!dbg_cnt (dse))
2007 return false;
2009 /* Create a sequence of instructions to set up the read register.
2010 This sequence goes immediately before the store and its result
2011 is read by the load.
2013 We need to keep this in perspective. We are replacing a read
2014 with a sequence of insns, but the read will almost certainly be
2015 in cache, so it is not going to be an expensive one. Thus, we
2016 are not willing to do a multi insn shift or worse a subroutine
2017 call to get rid of the read. */
2018 if (dump_file)
2019 fprintf (dump_file, "trying to replace %smode load in insn %d"
2020 " from %smode store in insn %d\n",
2021 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
2022 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
2023 start_sequence ();
2024 bb = BLOCK_FOR_INSN (read_insn->insn);
2025 read_reg = get_stored_val (store_info,
2026 read_mode, read_info->begin, read_info->end,
2027 bb, false);
2028 if (read_reg == NULL_RTX)
2030 end_sequence ();
2031 if (dump_file)
2032 fprintf (dump_file, " -- could not extract bits of stored value\n");
2033 return false;
2035 /* Force the value into a new register so that it won't be clobbered
2036 between the store and the load. */
2037 read_reg = copy_to_mode_reg (read_mode, read_reg);
2038 insns = get_insns ();
2039 end_sequence ();
2041 if (insns != NULL_RTX)
2043 /* Now we have to scan the set of new instructions to see if the
2044 sequence contains and sets of hardregs that happened to be
2045 live at this point. For instance, this can happen if one of
2046 the insns sets the CC and the CC happened to be live at that
2047 point. This does occasionally happen, see PR 37922. */
2048 bitmap regs_set = BITMAP_ALLOC (NULL);
2050 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
2051 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
2053 bitmap_and_into (regs_set, regs_live);
2054 if (!bitmap_empty_p (regs_set))
2056 if (dump_file)
2058 fprintf (dump_file,
2059 "abandoning replacement because sequence clobbers live hardregs:");
2060 df_print_regset (dump_file, regs_set);
2063 BITMAP_FREE (regs_set);
2064 return false;
2066 BITMAP_FREE (regs_set);
2069 if (validate_change (read_insn->insn, loc, read_reg, 0))
2071 deferred_change_t deferred_change =
2072 (deferred_change_t) pool_alloc (deferred_change_pool);
2074 /* Insert this right before the store insn where it will be safe
2075 from later insns that might change it before the read. */
2076 emit_insn_before (insns, store_insn->insn);
2078 /* And now for the kludge part: cselib croaks if you just
2079 return at this point. There are two reasons for this:
2081 1) Cselib has an idea of how many pseudos there are and
2082 that does not include the new ones we just added.
2084 2) Cselib does not know about the move insn we added
2085 above the store_info, and there is no way to tell it
2086 about it, because it has "moved on".
2088 Problem (1) is fixable with a certain amount of engineering.
2089 Problem (2) is requires starting the bb from scratch. This
2090 could be expensive.
2092 So we are just going to have to lie. The move/extraction
2093 insns are not really an issue, cselib did not see them. But
2094 the use of the new pseudo read_insn is a real problem because
2095 cselib has not scanned this insn. The way that we solve this
2096 problem is that we are just going to put the mem back for now
2097 and when we are finished with the block, we undo this. We
2098 keep a table of mems to get rid of. At the end of the basic
2099 block we can put them back. */
2101 *loc = read_info->mem;
2102 deferred_change->next = deferred_change_list;
2103 deferred_change_list = deferred_change;
2104 deferred_change->loc = loc;
2105 deferred_change->reg = read_reg;
2107 /* Get rid of the read_info, from the point of view of the
2108 rest of dse, play like this read never happened. */
2109 read_insn->read_rec = read_info->next;
2110 pool_free (read_info_pool, read_info);
2111 if (dump_file)
2113 fprintf (dump_file, " -- replaced the loaded MEM with ");
2114 print_simple_rtl (dump_file, read_reg);
2115 fprintf (dump_file, "\n");
2117 return true;
2119 else
2121 if (dump_file)
2123 fprintf (dump_file, " -- replacing the loaded MEM with ");
2124 print_simple_rtl (dump_file, read_reg);
2125 fprintf (dump_file, " led to an invalid instruction\n");
2127 return false;
2131 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2132 if LOC is a mem and if it is look at the address and kill any
2133 appropriate stores that may be active. */
2135 static int
2136 check_mem_read_rtx (rtx *loc, void *data)
2138 rtx mem = *loc, mem_addr;
2139 bb_info_t bb_info;
2140 insn_info_t insn_info;
2141 HOST_WIDE_INT offset = 0;
2142 HOST_WIDE_INT width = 0;
2143 alias_set_type spill_alias_set = 0;
2144 cselib_val *base = NULL;
2145 int group_id;
2146 read_info_t read_info;
2148 if (!mem || !MEM_P (mem))
2149 return 0;
2151 bb_info = (bb_info_t) data;
2152 insn_info = bb_info->last_insn;
2154 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2155 || (MEM_VOLATILE_P (mem)))
2157 if (dump_file)
2158 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
2159 add_wild_read (bb_info);
2160 insn_info->cannot_delete = true;
2161 return 0;
2164 /* If it is reading readonly mem, then there can be no conflict with
2165 another write. */
2166 if (MEM_READONLY_P (mem))
2167 return 0;
2169 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
2171 if (dump_file)
2172 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2173 add_wild_read (bb_info);
2174 return 0;
2177 if (GET_MODE (mem) == BLKmode)
2178 width = -1;
2179 else
2180 width = GET_MODE_SIZE (GET_MODE (mem));
2182 read_info = (read_info_t) pool_alloc (read_info_pool);
2183 read_info->group_id = group_id;
2184 read_info->mem = mem;
2185 read_info->alias_set = spill_alias_set;
2186 read_info->begin = offset;
2187 read_info->end = offset + width;
2188 read_info->next = insn_info->read_rec;
2189 insn_info->read_rec = read_info;
2190 /* For alias_set != 0 canon_true_dependence should be never called. */
2191 if (spill_alias_set)
2192 mem_addr = NULL_RTX;
2193 else
2195 if (group_id < 0)
2196 mem_addr = base->val_rtx;
2197 else
2199 group_info_t group
2200 = VEC_index (group_info_t, rtx_group_vec, group_id);
2201 mem_addr = group->canon_base_addr;
2203 if (offset)
2204 mem_addr = plus_constant (mem_addr, offset);
2207 /* We ignore the clobbers in store_info. The is mildly aggressive,
2208 but there really should not be a clobber followed by a read. */
2210 if (spill_alias_set)
2212 insn_info_t i_ptr = active_local_stores;
2213 insn_info_t last = NULL;
2215 if (dump_file)
2216 fprintf (dump_file, " processing spill load %d\n",
2217 (int) spill_alias_set);
2219 while (i_ptr)
2221 store_info_t store_info = i_ptr->store_rec;
2223 /* Skip the clobbers. */
2224 while (!store_info->is_set)
2225 store_info = store_info->next;
2227 if (store_info->alias_set == spill_alias_set)
2229 if (dump_file)
2230 dump_insn_info ("removing from active", i_ptr);
2232 active_local_stores_len--;
2233 if (last)
2234 last->next_local_store = i_ptr->next_local_store;
2235 else
2236 active_local_stores = i_ptr->next_local_store;
2238 else
2239 last = i_ptr;
2240 i_ptr = i_ptr->next_local_store;
2243 else if (group_id >= 0)
2245 /* This is the restricted case where the base is a constant or
2246 the frame pointer and offset is a constant. */
2247 insn_info_t i_ptr = active_local_stores;
2248 insn_info_t last = NULL;
2250 if (dump_file)
2252 if (width == -1)
2253 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2254 group_id);
2255 else
2256 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2257 group_id, (int)offset, (int)(offset+width));
2260 while (i_ptr)
2262 bool remove = false;
2263 store_info_t store_info = i_ptr->store_rec;
2265 /* Skip the clobbers. */
2266 while (!store_info->is_set)
2267 store_info = store_info->next;
2269 /* There are three cases here. */
2270 if (store_info->group_id < 0)
2271 /* We have a cselib store followed by a read from a
2272 const base. */
2273 remove
2274 = canon_true_dependence (store_info->mem,
2275 GET_MODE (store_info->mem),
2276 store_info->mem_addr,
2277 mem, mem_addr, rtx_varies_p);
2279 else if (group_id == store_info->group_id)
2281 /* This is a block mode load. We may get lucky and
2282 canon_true_dependence may save the day. */
2283 if (width == -1)
2284 remove
2285 = canon_true_dependence (store_info->mem,
2286 GET_MODE (store_info->mem),
2287 store_info->mem_addr,
2288 mem, mem_addr, rtx_varies_p);
2290 /* If this read is just reading back something that we just
2291 stored, rewrite the read. */
2292 else
2294 if (store_info->rhs
2295 && offset >= store_info->begin
2296 && offset + width <= store_info->end
2297 && all_positions_needed_p (store_info,
2298 offset - store_info->begin,
2299 width)
2300 && replace_read (store_info, i_ptr, read_info,
2301 insn_info, loc, bb_info->regs_live))
2302 return 0;
2304 /* The bases are the same, just see if the offsets
2305 overlap. */
2306 if ((offset < store_info->end)
2307 && (offset + width > store_info->begin))
2308 remove = true;
2312 /* else
2313 The else case that is missing here is that the
2314 bases are constant but different. There is nothing
2315 to do here because there is no overlap. */
2317 if (remove)
2319 if (dump_file)
2320 dump_insn_info ("removing from active", i_ptr);
2322 active_local_stores_len--;
2323 if (last)
2324 last->next_local_store = i_ptr->next_local_store;
2325 else
2326 active_local_stores = i_ptr->next_local_store;
2328 else
2329 last = i_ptr;
2330 i_ptr = i_ptr->next_local_store;
2333 else
2335 insn_info_t i_ptr = active_local_stores;
2336 insn_info_t last = NULL;
2337 if (dump_file)
2339 fprintf (dump_file, " processing cselib load mem:");
2340 print_inline_rtx (dump_file, mem, 0);
2341 fprintf (dump_file, "\n");
2344 while (i_ptr)
2346 bool remove = false;
2347 store_info_t store_info = i_ptr->store_rec;
2349 if (dump_file)
2350 fprintf (dump_file, " processing cselib load against insn %d\n",
2351 INSN_UID (i_ptr->insn));
2353 /* Skip the clobbers. */
2354 while (!store_info->is_set)
2355 store_info = store_info->next;
2357 /* If this read is just reading back something that we just
2358 stored, rewrite the read. */
2359 if (store_info->rhs
2360 && store_info->group_id == -1
2361 && store_info->cse_base == base
2362 && width != -1
2363 && offset >= store_info->begin
2364 && offset + width <= store_info->end
2365 && all_positions_needed_p (store_info,
2366 offset - store_info->begin, width)
2367 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2368 bb_info->regs_live))
2369 return 0;
2371 if (!store_info->alias_set)
2372 remove = canon_true_dependence (store_info->mem,
2373 GET_MODE (store_info->mem),
2374 store_info->mem_addr,
2375 mem, mem_addr, rtx_varies_p);
2377 if (remove)
2379 if (dump_file)
2380 dump_insn_info ("removing from active", i_ptr);
2382 active_local_stores_len--;
2383 if (last)
2384 last->next_local_store = i_ptr->next_local_store;
2385 else
2386 active_local_stores = i_ptr->next_local_store;
2388 else
2389 last = i_ptr;
2390 i_ptr = i_ptr->next_local_store;
2393 return 0;
2396 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2397 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2398 true for any part of *LOC. */
2400 static void
2401 check_mem_read_use (rtx *loc, void *data)
2403 for_each_rtx (loc, check_mem_read_rtx, data);
2407 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2408 So far it only handles arguments passed in registers. */
2410 static bool
2411 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2413 CUMULATIVE_ARGS args_so_far_v;
2414 cumulative_args_t args_so_far;
2415 tree arg;
2416 int idx;
2418 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2419 args_so_far = pack_cumulative_args (&args_so_far_v);
2421 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2422 for (idx = 0;
2423 arg != void_list_node && idx < nargs;
2424 arg = TREE_CHAIN (arg), idx++)
2426 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2427 rtx reg, link, tmp;
2428 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2429 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2430 || GET_MODE_CLASS (mode) != MODE_INT)
2431 return false;
2433 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2434 link;
2435 link = XEXP (link, 1))
2436 if (GET_CODE (XEXP (link, 0)) == USE)
2438 args[idx] = XEXP (XEXP (link, 0), 0);
2439 if (REG_P (args[idx])
2440 && REGNO (args[idx]) == REGNO (reg)
2441 && (GET_MODE (args[idx]) == mode
2442 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2443 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2444 <= UNITS_PER_WORD)
2445 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2446 > GET_MODE_SIZE (mode)))))
2447 break;
2449 if (!link)
2450 return false;
2452 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2453 if (GET_MODE (args[idx]) != mode)
2455 if (!tmp || !CONST_INT_P (tmp))
2456 return false;
2457 tmp = GEN_INT (trunc_int_for_mode (INTVAL (tmp), mode));
2459 if (tmp)
2460 args[idx] = tmp;
2462 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2464 if (arg != void_list_node || idx != nargs)
2465 return false;
2466 return true;
2469 /* Return a bitmap of the fixed registers contained in IN. */
2471 static bitmap
2472 copy_fixed_regs (const_bitmap in)
2474 bitmap ret;
2476 ret = ALLOC_REG_SET (NULL);
2477 bitmap_and (ret, in, fixed_reg_set_regset);
2478 return ret;
2481 /* Apply record_store to all candidate stores in INSN. Mark INSN
2482 if some part of it is not a candidate store and assigns to a
2483 non-register target. */
2485 static void
2486 scan_insn (bb_info_t bb_info, rtx insn)
2488 rtx body;
2489 insn_info_t insn_info = (insn_info_t) pool_alloc (insn_info_pool);
2490 int mems_found = 0;
2491 memset (insn_info, 0, sizeof (struct insn_info));
2493 if (dump_file)
2494 fprintf (dump_file, "\n**scanning insn=%d\n",
2495 INSN_UID (insn));
2497 insn_info->prev_insn = bb_info->last_insn;
2498 insn_info->insn = insn;
2499 bb_info->last_insn = insn_info;
2501 if (DEBUG_INSN_P (insn))
2503 insn_info->cannot_delete = true;
2504 return;
2507 /* Cselib clears the table for this case, so we have to essentially
2508 do the same. */
2509 if (NONJUMP_INSN_P (insn)
2510 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2511 && MEM_VOLATILE_P (PATTERN (insn)))
2513 add_wild_read (bb_info);
2514 insn_info->cannot_delete = true;
2515 return;
2518 /* Look at all of the uses in the insn. */
2519 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2521 if (CALL_P (insn))
2523 bool const_call;
2524 tree memset_call = NULL_TREE;
2526 insn_info->cannot_delete = true;
2528 /* Const functions cannot do anything bad i.e. read memory,
2529 however, they can read their parameters which may have
2530 been pushed onto the stack.
2531 memset and bzero don't read memory either. */
2532 const_call = RTL_CONST_CALL_P (insn);
2533 if (!const_call)
2535 rtx call = PATTERN (insn);
2536 if (GET_CODE (call) == PARALLEL)
2537 call = XVECEXP (call, 0, 0);
2538 if (GET_CODE (call) == SET)
2539 call = SET_SRC (call);
2540 if (GET_CODE (call) == CALL
2541 && MEM_P (XEXP (call, 0))
2542 && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
2544 rtx symbol = XEXP (XEXP (call, 0), 0);
2545 if (SYMBOL_REF_DECL (symbol)
2546 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
2548 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
2549 == BUILT_IN_NORMAL
2550 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
2551 == BUILT_IN_MEMSET))
2552 || SYMBOL_REF_DECL (symbol) == block_clear_fn)
2553 memset_call = SYMBOL_REF_DECL (symbol);
2557 if (const_call || memset_call)
2559 insn_info_t i_ptr = active_local_stores;
2560 insn_info_t last = NULL;
2562 if (dump_file)
2563 fprintf (dump_file, "%s call %d\n",
2564 const_call ? "const" : "memset", INSN_UID (insn));
2566 /* See the head comment of the frame_read field. */
2567 if (reload_completed)
2568 insn_info->frame_read = true;
2570 /* Loop over the active stores and remove those which are
2571 killed by the const function call. */
2572 while (i_ptr)
2574 bool remove_store = false;
2576 /* The stack pointer based stores are always killed. */
2577 if (i_ptr->stack_pointer_based)
2578 remove_store = true;
2580 /* If the frame is read, the frame related stores are killed. */
2581 else if (insn_info->frame_read)
2583 store_info_t store_info = i_ptr->store_rec;
2585 /* Skip the clobbers. */
2586 while (!store_info->is_set)
2587 store_info = store_info->next;
2589 if (store_info->group_id >= 0
2590 && VEC_index (group_info_t, rtx_group_vec,
2591 store_info->group_id)->frame_related)
2592 remove_store = true;
2595 if (remove_store)
2597 if (dump_file)
2598 dump_insn_info ("removing from active", i_ptr);
2600 active_local_stores_len--;
2601 if (last)
2602 last->next_local_store = i_ptr->next_local_store;
2603 else
2604 active_local_stores = i_ptr->next_local_store;
2606 else
2607 last = i_ptr;
2609 i_ptr = i_ptr->next_local_store;
2612 if (memset_call)
2614 rtx args[3];
2615 if (get_call_args (insn, memset_call, args, 3)
2616 && CONST_INT_P (args[1])
2617 && CONST_INT_P (args[2])
2618 && INTVAL (args[2]) > 0)
2620 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2621 set_mem_size (mem, INTVAL (args[2]));
2622 body = gen_rtx_SET (VOIDmode, mem, args[1]);
2623 mems_found += record_store (body, bb_info);
2624 if (dump_file)
2625 fprintf (dump_file, "handling memset as BLKmode store\n");
2626 if (mems_found == 1)
2628 if (active_local_stores_len++
2629 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2631 active_local_stores_len = 1;
2632 active_local_stores = NULL;
2634 insn_info->fixed_regs_live
2635 = copy_fixed_regs (bb_info->regs_live);
2636 insn_info->next_local_store = active_local_stores;
2637 active_local_stores = insn_info;
2643 else
2644 /* Every other call, including pure functions, may read any memory
2645 that is not relative to the frame. */
2646 add_non_frame_wild_read (bb_info);
2648 return;
2651 /* Assuming that there are sets in these insns, we cannot delete
2652 them. */
2653 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2654 || volatile_refs_p (PATTERN (insn))
2655 || insn_could_throw_p (insn)
2656 || (RTX_FRAME_RELATED_P (insn))
2657 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2658 insn_info->cannot_delete = true;
2660 body = PATTERN (insn);
2661 if (GET_CODE (body) == PARALLEL)
2663 int i;
2664 for (i = 0; i < XVECLEN (body, 0); i++)
2665 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2667 else
2668 mems_found += record_store (body, bb_info);
2670 if (dump_file)
2671 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2672 mems_found, insn_info->cannot_delete ? "true" : "false");
2674 /* If we found some sets of mems, add it into the active_local_stores so
2675 that it can be locally deleted if found dead or used for
2676 replace_read and redundant constant store elimination. Otherwise mark
2677 it as cannot delete. This simplifies the processing later. */
2678 if (mems_found == 1)
2680 if (active_local_stores_len++
2681 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2683 active_local_stores_len = 1;
2684 active_local_stores = NULL;
2686 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2687 insn_info->next_local_store = active_local_stores;
2688 active_local_stores = insn_info;
2690 else
2691 insn_info->cannot_delete = true;
2695 /* Remove BASE from the set of active_local_stores. This is a
2696 callback from cselib that is used to get rid of the stores in
2697 active_local_stores. */
2699 static void
2700 remove_useless_values (cselib_val *base)
2702 insn_info_t insn_info = active_local_stores;
2703 insn_info_t last = NULL;
2705 while (insn_info)
2707 store_info_t store_info = insn_info->store_rec;
2708 bool del = false;
2710 /* If ANY of the store_infos match the cselib group that is
2711 being deleted, then the insn can not be deleted. */
2712 while (store_info)
2714 if ((store_info->group_id == -1)
2715 && (store_info->cse_base == base))
2717 del = true;
2718 break;
2720 store_info = store_info->next;
2723 if (del)
2725 active_local_stores_len--;
2726 if (last)
2727 last->next_local_store = insn_info->next_local_store;
2728 else
2729 active_local_stores = insn_info->next_local_store;
2730 free_store_info (insn_info);
2732 else
2733 last = insn_info;
2735 insn_info = insn_info->next_local_store;
2740 /* Do all of step 1. */
2742 static void
2743 dse_step1 (void)
2745 basic_block bb;
2746 bitmap regs_live = BITMAP_ALLOC (NULL);
2748 cselib_init (0);
2749 all_blocks = BITMAP_ALLOC (NULL);
2750 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2751 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2753 FOR_ALL_BB (bb)
2755 insn_info_t ptr;
2756 bb_info_t bb_info = (bb_info_t) pool_alloc (bb_info_pool);
2758 memset (bb_info, 0, sizeof (struct bb_info));
2759 bitmap_set_bit (all_blocks, bb->index);
2760 bb_info->regs_live = regs_live;
2762 bitmap_copy (regs_live, DF_LR_IN (bb));
2763 df_simulate_initialize_forwards (bb, regs_live);
2765 bb_table[bb->index] = bb_info;
2766 cselib_discard_hook = remove_useless_values;
2768 if (bb->index >= NUM_FIXED_BLOCKS)
2770 rtx insn;
2772 cse_store_info_pool
2773 = create_alloc_pool ("cse_store_info_pool",
2774 sizeof (struct store_info), 100);
2775 active_local_stores = NULL;
2776 active_local_stores_len = 0;
2777 cselib_clear_table ();
2779 /* Scan the insns. */
2780 FOR_BB_INSNS (bb, insn)
2782 if (INSN_P (insn))
2783 scan_insn (bb_info, insn);
2784 cselib_process_insn (insn);
2785 if (INSN_P (insn))
2786 df_simulate_one_insn_forwards (bb, insn, regs_live);
2789 /* This is something of a hack, because the global algorithm
2790 is supposed to take care of the case where stores go dead
2791 at the end of the function. However, the global
2792 algorithm must take a more conservative view of block
2793 mode reads than the local alg does. So to get the case
2794 where you have a store to the frame followed by a non
2795 overlapping block more read, we look at the active local
2796 stores at the end of the function and delete all of the
2797 frame and spill based ones. */
2798 if (stores_off_frame_dead_at_return
2799 && (EDGE_COUNT (bb->succs) == 0
2800 || (single_succ_p (bb)
2801 && single_succ (bb) == EXIT_BLOCK_PTR
2802 && ! crtl->calls_eh_return)))
2804 insn_info_t i_ptr = active_local_stores;
2805 while (i_ptr)
2807 store_info_t store_info = i_ptr->store_rec;
2809 /* Skip the clobbers. */
2810 while (!store_info->is_set)
2811 store_info = store_info->next;
2812 if (store_info->alias_set && !i_ptr->cannot_delete)
2813 delete_dead_store_insn (i_ptr);
2814 else
2815 if (store_info->group_id >= 0)
2817 group_info_t group
2818 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
2819 if (group->frame_related && !i_ptr->cannot_delete)
2820 delete_dead_store_insn (i_ptr);
2823 i_ptr = i_ptr->next_local_store;
2827 /* Get rid of the loads that were discovered in
2828 replace_read. Cselib is finished with this block. */
2829 while (deferred_change_list)
2831 deferred_change_t next = deferred_change_list->next;
2833 /* There is no reason to validate this change. That was
2834 done earlier. */
2835 *deferred_change_list->loc = deferred_change_list->reg;
2836 pool_free (deferred_change_pool, deferred_change_list);
2837 deferred_change_list = next;
2840 /* Get rid of all of the cselib based store_infos in this
2841 block and mark the containing insns as not being
2842 deletable. */
2843 ptr = bb_info->last_insn;
2844 while (ptr)
2846 if (ptr->contains_cselib_groups)
2848 store_info_t s_info = ptr->store_rec;
2849 while (s_info && !s_info->is_set)
2850 s_info = s_info->next;
2851 if (s_info
2852 && s_info->redundant_reason
2853 && s_info->redundant_reason->insn
2854 && !ptr->cannot_delete)
2856 if (dump_file)
2857 fprintf (dump_file, "Locally deleting insn %d "
2858 "because insn %d stores the "
2859 "same value and couldn't be "
2860 "eliminated\n",
2861 INSN_UID (ptr->insn),
2862 INSN_UID (s_info->redundant_reason->insn));
2863 delete_dead_store_insn (ptr);
2865 if (s_info)
2866 s_info->redundant_reason = NULL;
2867 free_store_info (ptr);
2869 else
2871 store_info_t s_info;
2873 /* Free at least positions_needed bitmaps. */
2874 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2875 if (s_info->is_large)
2877 BITMAP_FREE (s_info->positions_needed.large.bmap);
2878 s_info->is_large = false;
2881 ptr = ptr->prev_insn;
2884 free_alloc_pool (cse_store_info_pool);
2886 bb_info->regs_live = NULL;
2889 BITMAP_FREE (regs_live);
2890 cselib_finish ();
2891 htab_empty (rtx_group_table);
2895 /*----------------------------------------------------------------------------
2896 Second step.
2898 Assign each byte position in the stores that we are going to
2899 analyze globally to a position in the bitmaps. Returns true if
2900 there are any bit positions assigned.
2901 ----------------------------------------------------------------------------*/
2903 static void
2904 dse_step2_init (void)
2906 unsigned int i;
2907 group_info_t group;
2909 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, i, group)
2911 /* For all non stack related bases, we only consider a store to
2912 be deletable if there are two or more stores for that
2913 position. This is because it takes one store to make the
2914 other store redundant. However, for the stores that are
2915 stack related, we consider them if there is only one store
2916 for the position. We do this because the stack related
2917 stores can be deleted if their is no read between them and
2918 the end of the function.
2920 To make this work in the current framework, we take the stack
2921 related bases add all of the bits from store1 into store2.
2922 This has the effect of making the eligible even if there is
2923 only one store. */
2925 if (stores_off_frame_dead_at_return && group->frame_related)
2927 bitmap_ior_into (group->store2_n, group->store1_n);
2928 bitmap_ior_into (group->store2_p, group->store1_p);
2929 if (dump_file)
2930 fprintf (dump_file, "group %d is frame related ", i);
2933 group->offset_map_size_n++;
2934 group->offset_map_n = XNEWVEC (int, group->offset_map_size_n);
2935 group->offset_map_size_p++;
2936 group->offset_map_p = XNEWVEC (int, group->offset_map_size_p);
2937 group->process_globally = false;
2938 if (dump_file)
2940 fprintf (dump_file, "group %d(%d+%d): ", i,
2941 (int)bitmap_count_bits (group->store2_n),
2942 (int)bitmap_count_bits (group->store2_p));
2943 bitmap_print (dump_file, group->store2_n, "n ", " ");
2944 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2950 /* Init the offset tables for the normal case. */
2952 static bool
2953 dse_step2_nospill (void)
2955 unsigned int i;
2956 group_info_t group;
2957 /* Position 0 is unused because 0 is used in the maps to mean
2958 unused. */
2959 current_position = 1;
2960 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, i, group)
2962 bitmap_iterator bi;
2963 unsigned int j;
2965 if (group == clear_alias_group)
2966 continue;
2968 memset (group->offset_map_n, 0, sizeof(int) * group->offset_map_size_n);
2969 memset (group->offset_map_p, 0, sizeof(int) * group->offset_map_size_p);
2970 bitmap_clear (group->group_kill);
2972 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2974 bitmap_set_bit (group->group_kill, current_position);
2975 if (bitmap_bit_p (group->escaped_n, j))
2976 bitmap_set_bit (kill_on_calls, current_position);
2977 group->offset_map_n[j] = current_position++;
2978 group->process_globally = true;
2980 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2982 bitmap_set_bit (group->group_kill, current_position);
2983 if (bitmap_bit_p (group->escaped_p, j))
2984 bitmap_set_bit (kill_on_calls, current_position);
2985 group->offset_map_p[j] = current_position++;
2986 group->process_globally = true;
2989 return current_position != 1;
2993 /* Init the offset tables for the spill case. */
2995 static bool
2996 dse_step2_spill (void)
2998 unsigned int j;
2999 group_info_t group = clear_alias_group;
3000 bitmap_iterator bi;
3002 /* Position 0 is unused because 0 is used in the maps to mean
3003 unused. */
3004 current_position = 1;
3006 if (dump_file)
3008 bitmap_print (dump_file, clear_alias_sets,
3009 "clear alias sets ", "\n");
3010 bitmap_print (dump_file, disqualified_clear_alias_sets,
3011 "disqualified clear alias sets ", "\n");
3014 memset (group->offset_map_n, 0, sizeof(int) * group->offset_map_size_n);
3015 memset (group->offset_map_p, 0, sizeof(int) * group->offset_map_size_p);
3016 bitmap_clear (group->group_kill);
3018 /* Remove the disqualified positions from the store2_p set. */
3019 bitmap_and_compl_into (group->store2_p, disqualified_clear_alias_sets);
3021 /* We do not need to process the store2_n set because
3022 alias_sets are always positive. */
3023 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
3025 bitmap_set_bit (group->group_kill, current_position);
3026 group->offset_map_p[j] = current_position++;
3027 group->process_globally = true;
3030 return current_position != 1;
3035 /*----------------------------------------------------------------------------
3036 Third step.
3038 Build the bit vectors for the transfer functions.
3039 ----------------------------------------------------------------------------*/
3042 /* Note that this is NOT a general purpose function. Any mem that has
3043 an alias set registered here expected to be COMPLETELY unaliased:
3044 i.e it's addresses are not and need not be examined.
3046 It is known that all references to this address will have this
3047 alias set and there are NO other references to this address in the
3048 function.
3050 Currently the only place that is known to be clean enough to use
3051 this interface is the code that assigns the spill locations.
3053 All of the mems that have alias_sets registered are subjected to a
3054 very powerful form of dse where function calls, volatile reads and
3055 writes, and reads from random location are not taken into account.
3057 It is also assumed that these locations go dead when the function
3058 returns. This assumption could be relaxed if there were found to
3059 be places that this assumption was not correct.
3061 The MODE is passed in and saved. The mode of each load or store to
3062 a mem with ALIAS_SET is checked against MEM. If the size of that
3063 load or store is different from MODE, processing is halted on this
3064 alias set. For the vast majority of aliases sets, all of the loads
3065 and stores will use the same mode. But vectors are treated
3066 differently: the alias set is established for the entire vector,
3067 but reload will insert loads and stores for individual elements and
3068 we do not necessarily have the information to track those separate
3069 elements. So when we see a mode mismatch, we just bail. */
3072 void
3073 dse_record_singleton_alias_set (alias_set_type alias_set,
3074 enum machine_mode mode)
3076 struct clear_alias_mode_holder tmp_holder;
3077 struct clear_alias_mode_holder *entry;
3078 void **slot;
3080 /* If we are not going to run dse, we need to return now or there
3081 will be problems with allocating the bitmaps. */
3082 if ((!gate_dse()) || !alias_set)
3083 return;
3085 if (!clear_alias_sets)
3087 clear_alias_sets = BITMAP_ALLOC (NULL);
3088 disqualified_clear_alias_sets = BITMAP_ALLOC (NULL);
3089 clear_alias_mode_table = htab_create (11, clear_alias_mode_hash,
3090 clear_alias_mode_eq, NULL);
3091 clear_alias_mode_pool = create_alloc_pool ("clear_alias_mode_pool",
3092 sizeof (struct clear_alias_mode_holder), 100);
3095 bitmap_set_bit (clear_alias_sets, alias_set);
3097 tmp_holder.alias_set = alias_set;
3099 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, INSERT);
3100 gcc_assert (*slot == NULL);
3102 *slot = entry =
3103 (struct clear_alias_mode_holder *) pool_alloc (clear_alias_mode_pool);
3104 entry->alias_set = alias_set;
3105 entry->mode = mode;
3109 /* Remove ALIAS_SET from the sets of stack slots being considered. */
3111 void
3112 dse_invalidate_singleton_alias_set (alias_set_type alias_set)
3114 if ((!gate_dse()) || !alias_set)
3115 return;
3117 bitmap_clear_bit (clear_alias_sets, alias_set);
3121 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
3122 there, return 0. */
3124 static int
3125 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
3127 if (offset < 0)
3129 HOST_WIDE_INT offset_p = -offset;
3130 if (offset_p >= group_info->offset_map_size_n)
3131 return 0;
3132 return group_info->offset_map_n[offset_p];
3134 else
3136 if (offset >= group_info->offset_map_size_p)
3137 return 0;
3138 return group_info->offset_map_p[offset];
3143 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3144 may be NULL. */
3146 static void
3147 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
3149 while (store_info)
3151 HOST_WIDE_INT i;
3152 group_info_t group_info
3153 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
3154 if (group_info->process_globally)
3155 for (i = store_info->begin; i < store_info->end; i++)
3157 int index = get_bitmap_index (group_info, i);
3158 if (index != 0)
3160 bitmap_set_bit (gen, index);
3161 if (kill)
3162 bitmap_clear_bit (kill, index);
3165 store_info = store_info->next;
3170 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3171 may be NULL. */
3173 static void
3174 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
3176 while (store_info)
3178 if (store_info->alias_set)
3180 int index = get_bitmap_index (clear_alias_group,
3181 store_info->alias_set);
3182 if (index != 0)
3184 bitmap_set_bit (gen, index);
3185 if (kill)
3186 bitmap_clear_bit (kill, index);
3189 store_info = store_info->next;
3194 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3195 may be NULL. */
3197 static void
3198 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
3200 read_info_t read_info = insn_info->read_rec;
3201 int i;
3202 group_info_t group;
3204 /* If this insn reads the frame, kill all the frame related stores. */
3205 if (insn_info->frame_read)
3207 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, i, group)
3208 if (group->process_globally && group->frame_related)
3210 if (kill)
3211 bitmap_ior_into (kill, group->group_kill);
3212 bitmap_and_compl_into (gen, group->group_kill);
3215 if (insn_info->non_frame_wild_read)
3217 /* Kill all non-frame related stores. Kill all stores of variables that
3218 escape. */
3219 if (kill)
3220 bitmap_ior_into (kill, kill_on_calls);
3221 bitmap_and_compl_into (gen, kill_on_calls);
3222 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, i, group)
3223 if (group->process_globally && !group->frame_related)
3225 if (kill)
3226 bitmap_ior_into (kill, group->group_kill);
3227 bitmap_and_compl_into (gen, group->group_kill);
3230 while (read_info)
3232 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, i, group)
3234 if (group->process_globally)
3236 if (i == read_info->group_id)
3238 if (read_info->begin > read_info->end)
3240 /* Begin > end for block mode reads. */
3241 if (kill)
3242 bitmap_ior_into (kill, group->group_kill);
3243 bitmap_and_compl_into (gen, group->group_kill);
3245 else
3247 /* The groups are the same, just process the
3248 offsets. */
3249 HOST_WIDE_INT j;
3250 for (j = read_info->begin; j < read_info->end; j++)
3252 int index = get_bitmap_index (group, j);
3253 if (index != 0)
3255 if (kill)
3256 bitmap_set_bit (kill, index);
3257 bitmap_clear_bit (gen, index);
3262 else
3264 /* The groups are different, if the alias sets
3265 conflict, clear the entire group. We only need
3266 to apply this test if the read_info is a cselib
3267 read. Anything with a constant base cannot alias
3268 something else with a different constant
3269 base. */
3270 if ((read_info->group_id < 0)
3271 && canon_true_dependence (group->base_mem,
3272 GET_MODE (group->base_mem),
3273 group->canon_base_addr,
3274 read_info->mem, NULL_RTX,
3275 rtx_varies_p))
3277 if (kill)
3278 bitmap_ior_into (kill, group->group_kill);
3279 bitmap_and_compl_into (gen, group->group_kill);
3285 read_info = read_info->next;
3289 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3290 may be NULL. */
3292 static void
3293 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
3295 while (read_info)
3297 if (read_info->alias_set)
3299 int index = get_bitmap_index (clear_alias_group,
3300 read_info->alias_set);
3301 if (index != 0)
3303 if (kill)
3304 bitmap_set_bit (kill, index);
3305 bitmap_clear_bit (gen, index);
3309 read_info = read_info->next;
3314 /* Return the insn in BB_INFO before the first wild read or if there
3315 are no wild reads in the block, return the last insn. */
3317 static insn_info_t
3318 find_insn_before_first_wild_read (bb_info_t bb_info)
3320 insn_info_t insn_info = bb_info->last_insn;
3321 insn_info_t last_wild_read = NULL;
3323 while (insn_info)
3325 if (insn_info->wild_read)
3327 last_wild_read = insn_info->prev_insn;
3328 /* Block starts with wild read. */
3329 if (!last_wild_read)
3330 return NULL;
3333 insn_info = insn_info->prev_insn;
3336 if (last_wild_read)
3337 return last_wild_read;
3338 else
3339 return bb_info->last_insn;
3343 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3344 the block in order to build the gen and kill sets for the block.
3345 We start at ptr which may be the last insn in the block or may be
3346 the first insn with a wild read. In the latter case we are able to
3347 skip the rest of the block because it just does not matter:
3348 anything that happens is hidden by the wild read. */
3350 static void
3351 dse_step3_scan (bool for_spills, basic_block bb)
3353 bb_info_t bb_info = bb_table[bb->index];
3354 insn_info_t insn_info;
3356 if (for_spills)
3357 /* There are no wild reads in the spill case. */
3358 insn_info = bb_info->last_insn;
3359 else
3360 insn_info = find_insn_before_first_wild_read (bb_info);
3362 /* In the spill case or in the no_spill case if there is no wild
3363 read in the block, we will need a kill set. */
3364 if (insn_info == bb_info->last_insn)
3366 if (bb_info->kill)
3367 bitmap_clear (bb_info->kill);
3368 else
3369 bb_info->kill = BITMAP_ALLOC (NULL);
3371 else
3372 if (bb_info->kill)
3373 BITMAP_FREE (bb_info->kill);
3375 while (insn_info)
3377 /* There may have been code deleted by the dce pass run before
3378 this phase. */
3379 if (insn_info->insn && INSN_P (insn_info->insn))
3381 /* Process the read(s) last. */
3382 if (for_spills)
3384 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3385 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
3387 else
3389 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3390 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
3394 insn_info = insn_info->prev_insn;
3399 /* Set the gen set of the exit block, and also any block with no
3400 successors that does not have a wild read. */
3402 static void
3403 dse_step3_exit_block_scan (bb_info_t bb_info)
3405 /* The gen set is all 0's for the exit block except for the
3406 frame_pointer_group. */
3408 if (stores_off_frame_dead_at_return)
3410 unsigned int i;
3411 group_info_t group;
3413 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, i, group)
3415 if (group->process_globally && group->frame_related)
3416 bitmap_ior_into (bb_info->gen, group->group_kill);
3422 /* Find all of the blocks that are not backwards reachable from the
3423 exit block or any block with no successors (BB). These are the
3424 infinite loops or infinite self loops. These blocks will still
3425 have their bits set in UNREACHABLE_BLOCKS. */
3427 static void
3428 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3430 edge e;
3431 edge_iterator ei;
3433 if (TEST_BIT (unreachable_blocks, bb->index))
3435 RESET_BIT (unreachable_blocks, bb->index);
3436 FOR_EACH_EDGE (e, ei, bb->preds)
3438 mark_reachable_blocks (unreachable_blocks, e->src);
3443 /* Build the transfer functions for the function. */
3445 static void
3446 dse_step3 (bool for_spills)
3448 basic_block bb;
3449 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block);
3450 sbitmap_iterator sbi;
3451 bitmap all_ones = NULL;
3452 unsigned int i;
3454 sbitmap_ones (unreachable_blocks);
3456 FOR_ALL_BB (bb)
3458 bb_info_t bb_info = bb_table[bb->index];
3459 if (bb_info->gen)
3460 bitmap_clear (bb_info->gen);
3461 else
3462 bb_info->gen = BITMAP_ALLOC (NULL);
3464 if (bb->index == ENTRY_BLOCK)
3466 else if (bb->index == EXIT_BLOCK)
3467 dse_step3_exit_block_scan (bb_info);
3468 else
3469 dse_step3_scan (for_spills, bb);
3470 if (EDGE_COUNT (bb->succs) == 0)
3471 mark_reachable_blocks (unreachable_blocks, bb);
3473 /* If this is the second time dataflow is run, delete the old
3474 sets. */
3475 if (bb_info->in)
3476 BITMAP_FREE (bb_info->in);
3477 if (bb_info->out)
3478 BITMAP_FREE (bb_info->out);
3481 /* For any block in an infinite loop, we must initialize the out set
3482 to all ones. This could be expensive, but almost never occurs in
3483 practice. However, it is common in regression tests. */
3484 EXECUTE_IF_SET_IN_SBITMAP (unreachable_blocks, 0, i, sbi)
3486 if (bitmap_bit_p (all_blocks, i))
3488 bb_info_t bb_info = bb_table[i];
3489 if (!all_ones)
3491 unsigned int j;
3492 group_info_t group;
3494 all_ones = BITMAP_ALLOC (NULL);
3495 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, j, group)
3496 bitmap_ior_into (all_ones, group->group_kill);
3498 if (!bb_info->out)
3500 bb_info->out = BITMAP_ALLOC (NULL);
3501 bitmap_copy (bb_info->out, all_ones);
3506 if (all_ones)
3507 BITMAP_FREE (all_ones);
3508 sbitmap_free (unreachable_blocks);
3513 /*----------------------------------------------------------------------------
3514 Fourth step.
3516 Solve the bitvector equations.
3517 ----------------------------------------------------------------------------*/
3520 /* Confluence function for blocks with no successors. Create an out
3521 set from the gen set of the exit block. This block logically has
3522 the exit block as a successor. */
3526 static void
3527 dse_confluence_0 (basic_block bb)
3529 bb_info_t bb_info = bb_table[bb->index];
3531 if (bb->index == EXIT_BLOCK)
3532 return;
3534 if (!bb_info->out)
3536 bb_info->out = BITMAP_ALLOC (NULL);
3537 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3541 /* Propagate the information from the in set of the dest of E to the
3542 out set of the src of E. If the various in or out sets are not
3543 there, that means they are all ones. */
3545 static bool
3546 dse_confluence_n (edge e)
3548 bb_info_t src_info = bb_table[e->src->index];
3549 bb_info_t dest_info = bb_table[e->dest->index];
3551 if (dest_info->in)
3553 if (src_info->out)
3554 bitmap_and_into (src_info->out, dest_info->in);
3555 else
3557 src_info->out = BITMAP_ALLOC (NULL);
3558 bitmap_copy (src_info->out, dest_info->in);
3561 return true;
3565 /* Propagate the info from the out to the in set of BB_INDEX's basic
3566 block. There are three cases:
3568 1) The block has no kill set. In this case the kill set is all
3569 ones. It does not matter what the out set of the block is, none of
3570 the info can reach the top. The only thing that reaches the top is
3571 the gen set and we just copy the set.
3573 2) There is a kill set but no out set and bb has successors. In
3574 this case we just return. Eventually an out set will be created and
3575 it is better to wait than to create a set of ones.
3577 3) There is both a kill and out set. We apply the obvious transfer
3578 function.
3581 static bool
3582 dse_transfer_function (int bb_index)
3584 bb_info_t bb_info = bb_table[bb_index];
3586 if (bb_info->kill)
3588 if (bb_info->out)
3590 /* Case 3 above. */
3591 if (bb_info->in)
3592 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3593 bb_info->out, bb_info->kill);
3594 else
3596 bb_info->in = BITMAP_ALLOC (NULL);
3597 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3598 bb_info->out, bb_info->kill);
3599 return true;
3602 else
3603 /* Case 2 above. */
3604 return false;
3606 else
3608 /* Case 1 above. If there is already an in set, nothing
3609 happens. */
3610 if (bb_info->in)
3611 return false;
3612 else
3614 bb_info->in = BITMAP_ALLOC (NULL);
3615 bitmap_copy (bb_info->in, bb_info->gen);
3616 return true;
3621 /* Solve the dataflow equations. */
3623 static void
3624 dse_step4 (void)
3626 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3627 dse_confluence_n, dse_transfer_function,
3628 all_blocks, df_get_postorder (DF_BACKWARD),
3629 df_get_n_blocks (DF_BACKWARD));
3630 if (dump_file)
3632 basic_block bb;
3634 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3635 FOR_ALL_BB (bb)
3637 bb_info_t bb_info = bb_table[bb->index];
3639 df_print_bb_index (bb, dump_file);
3640 if (bb_info->in)
3641 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3642 else
3643 fprintf (dump_file, " in: *MISSING*\n");
3644 if (bb_info->gen)
3645 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3646 else
3647 fprintf (dump_file, " gen: *MISSING*\n");
3648 if (bb_info->kill)
3649 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3650 else
3651 fprintf (dump_file, " kill: *MISSING*\n");
3652 if (bb_info->out)
3653 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3654 else
3655 fprintf (dump_file, " out: *MISSING*\n\n");
3662 /*----------------------------------------------------------------------------
3663 Fifth step.
3665 Delete the stores that can only be deleted using the global information.
3666 ----------------------------------------------------------------------------*/
3669 static void
3670 dse_step5_nospill (void)
3672 basic_block bb;
3673 FOR_EACH_BB (bb)
3675 bb_info_t bb_info = bb_table[bb->index];
3676 insn_info_t insn_info = bb_info->last_insn;
3677 bitmap v = bb_info->out;
3679 while (insn_info)
3681 bool deleted = false;
3682 if (dump_file && insn_info->insn)
3684 fprintf (dump_file, "starting to process insn %d\n",
3685 INSN_UID (insn_info->insn));
3686 bitmap_print (dump_file, v, " v: ", "\n");
3689 /* There may have been code deleted by the dce pass run before
3690 this phase. */
3691 if (insn_info->insn
3692 && INSN_P (insn_info->insn)
3693 && (!insn_info->cannot_delete)
3694 && (!bitmap_empty_p (v)))
3696 store_info_t store_info = insn_info->store_rec;
3698 /* Try to delete the current insn. */
3699 deleted = true;
3701 /* Skip the clobbers. */
3702 while (!store_info->is_set)
3703 store_info = store_info->next;
3705 if (store_info->alias_set)
3706 deleted = false;
3707 else
3709 HOST_WIDE_INT i;
3710 group_info_t group_info
3711 = VEC_index (group_info_t, rtx_group_vec, store_info->group_id);
3713 for (i = store_info->begin; i < store_info->end; i++)
3715 int index = get_bitmap_index (group_info, i);
3717 if (dump_file)
3718 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3719 if (index == 0 || !bitmap_bit_p (v, index))
3721 if (dump_file)
3722 fprintf (dump_file, "failing at i = %d\n", (int)i);
3723 deleted = false;
3724 break;
3728 if (deleted)
3730 if (dbg_cnt (dse)
3731 && check_for_inc_dec_1 (insn_info))
3733 delete_insn (insn_info->insn);
3734 insn_info->insn = NULL;
3735 globally_deleted++;
3739 /* We do want to process the local info if the insn was
3740 deleted. For instance, if the insn did a wild read, we
3741 no longer need to trash the info. */
3742 if (insn_info->insn
3743 && INSN_P (insn_info->insn)
3744 && (!deleted))
3746 scan_stores_nospill (insn_info->store_rec, v, NULL);
3747 if (insn_info->wild_read)
3749 if (dump_file)
3750 fprintf (dump_file, "wild read\n");
3751 bitmap_clear (v);
3753 else if (insn_info->read_rec
3754 || insn_info->non_frame_wild_read)
3756 if (dump_file && !insn_info->non_frame_wild_read)
3757 fprintf (dump_file, "regular read\n");
3758 else if (dump_file)
3759 fprintf (dump_file, "non-frame wild read\n");
3760 scan_reads_nospill (insn_info, v, NULL);
3764 insn_info = insn_info->prev_insn;
3770 static void
3771 dse_step5_spill (void)
3773 basic_block bb;
3774 FOR_EACH_BB (bb)
3776 bb_info_t bb_info = bb_table[bb->index];
3777 insn_info_t insn_info = bb_info->last_insn;
3778 bitmap v = bb_info->out;
3780 while (insn_info)
3782 bool deleted = false;
3783 /* There may have been code deleted by the dce pass run before
3784 this phase. */
3785 if (insn_info->insn
3786 && INSN_P (insn_info->insn)
3787 && (!insn_info->cannot_delete)
3788 && (!bitmap_empty_p (v)))
3790 /* Try to delete the current insn. */
3791 store_info_t store_info = insn_info->store_rec;
3792 deleted = true;
3794 while (store_info)
3796 if (store_info->alias_set)
3798 int index = get_bitmap_index (clear_alias_group,
3799 store_info->alias_set);
3800 if (index == 0 || !bitmap_bit_p (v, index))
3802 deleted = false;
3803 break;
3806 else
3807 deleted = false;
3808 store_info = store_info->next;
3810 if (deleted && dbg_cnt (dse)
3811 && check_for_inc_dec_1 (insn_info))
3813 if (dump_file)
3814 fprintf (dump_file, "Spill deleting insn %d\n",
3815 INSN_UID (insn_info->insn));
3816 delete_insn (insn_info->insn);
3817 spill_deleted++;
3818 insn_info->insn = NULL;
3822 if (insn_info->insn
3823 && INSN_P (insn_info->insn)
3824 && (!deleted))
3826 scan_stores_spill (insn_info->store_rec, v, NULL);
3827 scan_reads_spill (insn_info->read_rec, v, NULL);
3830 insn_info = insn_info->prev_insn;
3837 /*----------------------------------------------------------------------------
3838 Sixth step.
3840 Delete stores made redundant by earlier stores (which store the same
3841 value) that couldn't be eliminated.
3842 ----------------------------------------------------------------------------*/
3844 static void
3845 dse_step6 (void)
3847 basic_block bb;
3849 FOR_ALL_BB (bb)
3851 bb_info_t bb_info = bb_table[bb->index];
3852 insn_info_t insn_info = bb_info->last_insn;
3854 while (insn_info)
3856 /* There may have been code deleted by the dce pass run before
3857 this phase. */
3858 if (insn_info->insn
3859 && INSN_P (insn_info->insn)
3860 && !insn_info->cannot_delete)
3862 store_info_t s_info = insn_info->store_rec;
3864 while (s_info && !s_info->is_set)
3865 s_info = s_info->next;
3866 if (s_info
3867 && s_info->redundant_reason
3868 && s_info->redundant_reason->insn
3869 && INSN_P (s_info->redundant_reason->insn))
3871 rtx rinsn = s_info->redundant_reason->insn;
3872 if (dump_file)
3873 fprintf (dump_file, "Locally deleting insn %d "
3874 "because insn %d stores the "
3875 "same value and couldn't be "
3876 "eliminated\n",
3877 INSN_UID (insn_info->insn),
3878 INSN_UID (rinsn));
3879 delete_dead_store_insn (insn_info);
3882 insn_info = insn_info->prev_insn;
3887 /*----------------------------------------------------------------------------
3888 Seventh step.
3890 Destroy everything left standing.
3891 ----------------------------------------------------------------------------*/
3893 static void
3894 dse_step7 (bool global_done)
3896 unsigned int i;
3897 group_info_t group;
3898 basic_block bb;
3900 FOR_EACH_VEC_ELT (group_info_t, rtx_group_vec, i, group)
3902 free (group->offset_map_n);
3903 free (group->offset_map_p);
3904 BITMAP_FREE (group->store1_n);
3905 BITMAP_FREE (group->store1_p);
3906 BITMAP_FREE (group->store2_n);
3907 BITMAP_FREE (group->store2_p);
3908 BITMAP_FREE (group->escaped_n);
3909 BITMAP_FREE (group->escaped_p);
3910 BITMAP_FREE (group->group_kill);
3913 if (global_done)
3914 FOR_ALL_BB (bb)
3916 bb_info_t bb_info = bb_table[bb->index];
3917 BITMAP_FREE (bb_info->gen);
3918 if (bb_info->kill)
3919 BITMAP_FREE (bb_info->kill);
3920 if (bb_info->in)
3921 BITMAP_FREE (bb_info->in);
3922 if (bb_info->out)
3923 BITMAP_FREE (bb_info->out);
3926 if (clear_alias_sets)
3928 BITMAP_FREE (clear_alias_sets);
3929 BITMAP_FREE (disqualified_clear_alias_sets);
3930 free_alloc_pool (clear_alias_mode_pool);
3931 htab_delete (clear_alias_mode_table);
3934 end_alias_analysis ();
3935 free (bb_table);
3936 htab_delete (rtx_group_table);
3937 VEC_free (group_info_t, heap, rtx_group_vec);
3938 BITMAP_FREE (all_blocks);
3939 BITMAP_FREE (scratch);
3940 BITMAP_FREE (kill_on_calls);
3942 free_alloc_pool (rtx_store_info_pool);
3943 free_alloc_pool (read_info_pool);
3944 free_alloc_pool (insn_info_pool);
3945 free_alloc_pool (bb_info_pool);
3946 free_alloc_pool (rtx_group_info_pool);
3947 free_alloc_pool (deferred_change_pool);
3951 /* -------------------------------------------------------------------------
3953 ------------------------------------------------------------------------- */
3955 /* Callback for running pass_rtl_dse. */
3957 static unsigned int
3958 rest_of_handle_dse (void)
3960 bool did_global = false;
3962 df_set_flags (DF_DEFER_INSN_RESCAN);
3964 /* Need the notes since we must track live hardregs in the forwards
3965 direction. */
3966 df_note_add_problem ();
3967 df_analyze ();
3969 dse_step0 ();
3970 dse_step1 ();
3971 dse_step2_init ();
3972 if (dse_step2_nospill ())
3974 df_set_flags (DF_LR_RUN_DCE);
3975 df_analyze ();
3976 did_global = true;
3977 if (dump_file)
3978 fprintf (dump_file, "doing global processing\n");
3979 dse_step3 (false);
3980 dse_step4 ();
3981 dse_step5_nospill ();
3984 /* For the instance of dse that runs after reload, we make a special
3985 pass to process the spills. These are special in that they are
3986 totally transparent, i.e, there is no aliasing issues that need
3987 to be considered. This means that the wild reads that kill
3988 everything else do not apply here. */
3989 if (clear_alias_sets && dse_step2_spill ())
3991 if (!did_global)
3993 df_set_flags (DF_LR_RUN_DCE);
3994 df_analyze ();
3996 did_global = true;
3997 if (dump_file)
3998 fprintf (dump_file, "doing global spill processing\n");
3999 dse_step3 (true);
4000 dse_step4 ();
4001 dse_step5_spill ();
4004 dse_step6 ();
4005 dse_step7 (did_global);
4007 if (dump_file)
4008 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
4009 locally_deleted, globally_deleted, spill_deleted);
4010 return 0;
4013 static bool
4014 gate_dse (void)
4016 return gate_dse1 () || gate_dse2 ();
4019 static bool
4020 gate_dse1 (void)
4022 return optimize > 0 && flag_dse
4023 && dbg_cnt (dse1);
4026 static bool
4027 gate_dse2 (void)
4029 return optimize > 0 && flag_dse
4030 && dbg_cnt (dse2);
4033 struct rtl_opt_pass pass_rtl_dse1 =
4036 RTL_PASS,
4037 "dse1", /* name */
4038 gate_dse1, /* gate */
4039 rest_of_handle_dse, /* execute */
4040 NULL, /* sub */
4041 NULL, /* next */
4042 0, /* static_pass_number */
4043 TV_DSE1, /* tv_id */
4044 0, /* properties_required */
4045 0, /* properties_provided */
4046 0, /* properties_destroyed */
4047 0, /* todo_flags_start */
4048 TODO_df_finish | TODO_verify_rtl_sharing |
4049 TODO_ggc_collect /* todo_flags_finish */
4053 struct rtl_opt_pass pass_rtl_dse2 =
4056 RTL_PASS,
4057 "dse2", /* name */
4058 gate_dse2, /* gate */
4059 rest_of_handle_dse, /* execute */
4060 NULL, /* sub */
4061 NULL, /* next */
4062 0, /* static_pass_number */
4063 TV_DSE2, /* tv_id */
4064 0, /* properties_required */
4065 0, /* properties_provided */
4066 0, /* properties_destroyed */
4067 0, /* todo_flags_start */
4068 TODO_df_finish | TODO_verify_rtl_sharing |
4069 TODO_ggc_collect /* todo_flags_finish */