svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / dse.c
blob9a8f3cf0328c2606427e88d5cad21f6174c8e2cc
1 /* RTL dead store elimination.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
5 and Kenneth Zadeck <zadeck@naturalbridge.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #undef BASELINE
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "hash-table.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "stor-layout.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 "dominance.h"
39 #include "cfg.h"
40 #include "cfgrtl.h"
41 #include "predict.h"
42 #include "basic-block.h"
43 #include "df.h"
44 #include "cselib.h"
45 #include "tree-pass.h"
46 #include "alloc-pool.h"
47 #include "alias.h"
48 #include "insn-config.h"
49 #include "expr.h"
50 #include "recog.h"
51 #include "optabs.h"
52 #include "dbgcnt.h"
53 #include "target.h"
54 #include "params.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-expr.h"
58 #include "is-a.h"
59 #include "gimple.h"
60 #include "gimple-ssa.h"
61 #include "rtl-iter.h"
63 /* This file contains three techniques for performing Dead Store
64 Elimination (dse).
66 * The first technique performs dse locally on any base address. It
67 is based on the cselib which is a local value numbering technique.
68 This technique is local to a basic block but deals with a fairly
69 general addresses.
71 * The second technique performs dse globally but is restricted to
72 base addresses that are either constant or are relative to the
73 frame_pointer.
75 * The third technique, (which is only done after register allocation)
76 processes the spill spill slots. This differs from the second
77 technique because it takes advantage of the fact that spilling is
78 completely free from the effects of aliasing.
80 Logically, dse is a backwards dataflow problem. A store can be
81 deleted if it if cannot be reached in the backward direction by any
82 use of the value being stored. However, the local technique uses a
83 forwards scan of the basic block because cselib requires that the
84 block be processed in that order.
86 The pass is logically broken into 7 steps:
88 0) Initialization.
90 1) The local algorithm, as well as scanning the insns for the two
91 global algorithms.
93 2) Analysis to see if the global algs are necessary. In the case
94 of stores base on a constant address, there must be at least two
95 stores to that address, to make it possible to delete some of the
96 stores. In the case of stores off of the frame or spill related
97 stores, only one store to an address is necessary because those
98 stores die at the end of the function.
100 3) Set up the global dataflow equations based on processing the
101 info parsed in the first step.
103 4) Solve the dataflow equations.
105 5) Delete the insns that the global analysis has indicated are
106 unnecessary.
108 6) Delete insns that store the same value as preceding store
109 where the earlier store couldn't be eliminated.
111 7) Cleanup.
113 This step uses cselib and canon_rtx to build the largest expression
114 possible for each address. This pass is a forwards pass through
115 each basic block. From the point of view of the global technique,
116 the first pass could examine a block in either direction. The
117 forwards ordering is to accommodate cselib.
119 We make a simplifying assumption: addresses fall into four broad
120 categories:
122 1) base has rtx_varies_p == false, offset is constant.
123 2) base has rtx_varies_p == false, offset variable.
124 3) base has rtx_varies_p == true, offset constant.
125 4) base has rtx_varies_p == true, offset variable.
127 The local passes are able to process all 4 kinds of addresses. The
128 global pass only handles 1).
130 The global problem is formulated as follows:
132 A store, S1, to address A, where A is not relative to the stack
133 frame, can be eliminated if all paths from S1 to the end of the
134 function contain another store to A before a read to A.
136 If the address A is relative to the stack frame, a store S2 to A
137 can be eliminated if there are no paths from S2 that reach the
138 end of the function that read A before another store to A. In
139 this case S2 can be deleted if there are paths from S2 to the
140 end of the function that have no reads or writes to A. This
141 second case allows stores to the stack frame to be deleted that
142 would otherwise die when the function returns. This cannot be
143 done if stores_off_frame_dead_at_return is not true. See the doc
144 for that variable for when this variable is false.
146 The global problem is formulated as a backwards set union
147 dataflow problem where the stores are the gens and reads are the
148 kills. Set union problems are rare and require some special
149 handling given our representation of bitmaps. A straightforward
150 implementation requires a lot of bitmaps filled with 1s.
151 These are expensive and cumbersome in our bitmap formulation so
152 care has been taken to avoid large vectors filled with 1s. See
153 the comments in bb_info and in the dataflow confluence functions
154 for details.
156 There are two places for further enhancements to this algorithm:
158 1) The original dse which was embedded in a pass called flow also
159 did local address forwarding. For example in
161 A <- r100
162 ... <- A
164 flow would replace the right hand side of the second insn with a
165 reference to r100. Most of the information is available to add this
166 to this pass. It has not done it because it is a lot of work in
167 the case that either r100 is assigned to between the first and
168 second insn and/or the second insn is a load of part of the value
169 stored by the first insn.
171 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
172 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
173 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
174 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
176 2) The cleaning up of spill code is quite profitable. It currently
177 depends on reading tea leaves and chicken entrails left by reload.
178 This pass depends on reload creating a singleton alias set for each
179 spill slot and telling the next dse pass which of these alias sets
180 are the singletons. Rather than analyze the addresses of the
181 spills, dse's spill processing just does analysis of the loads and
182 stores that use those alias sets. There are three cases where this
183 falls short:
185 a) Reload sometimes creates the slot for one mode of access, and
186 then inserts loads and/or stores for a smaller mode. In this
187 case, the current code just punts on the slot. The proper thing
188 to do is to back out and use one bit vector position for each
189 byte of the entity associated with the slot. This depends on
190 KNOWING that reload always generates the accesses for each of the
191 bytes in some canonical (read that easy to understand several
192 passes after reload happens) way.
194 b) Reload sometimes decides that spill slot it allocated was not
195 large enough for the mode and goes back and allocates more slots
196 with the same mode and alias set. The backout in this case is a
197 little more graceful than (a). In this case the slot is unmarked
198 as being a spill slot and if final address comes out to be based
199 off the frame pointer, the global algorithm handles this slot.
201 c) For any pass that may prespill, there is currently no
202 mechanism to tell the dse pass that the slot being used has the
203 special properties that reload uses. It may be that all that is
204 required is to have those passes make the same calls that reload
205 does, assuming that the alias sets can be manipulated in the same
206 way. */
208 /* There are limits to the size of constant offsets we model for the
209 global problem. There are certainly test cases, that exceed this
210 limit, however, it is unlikely that there are important programs
211 that really have constant offsets this size. */
212 #define MAX_OFFSET (64 * 1024)
214 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
215 on the default obstack because these bitmaps can grow quite large
216 (~2GB for the small (!) test case of PR54146) and we'll hold on to
217 all that memory until the end of the compiler run.
218 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
219 releasing the whole obstack. */
220 static bitmap_obstack dse_bitmap_obstack;
222 /* Obstack for other data. As for above: Kinda nice to be able to
223 throw it all away at the end in one big sweep. */
224 static struct obstack dse_obstack;
226 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
227 static bitmap scratch = NULL;
229 struct insn_info;
231 /* This structure holds information about a candidate store. */
232 struct store_info
235 /* False means this is a clobber. */
236 bool is_set;
238 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
239 bool is_large;
241 /* The id of the mem group of the base address. If rtx_varies_p is
242 true, this is -1. Otherwise, it is the index into the group
243 table. */
244 int group_id;
246 /* This is the cselib value. */
247 cselib_val *cse_base;
249 /* This canonized mem. */
250 rtx mem;
252 /* Canonized MEM address for use by canon_true_dependence. */
253 rtx mem_addr;
255 /* If this is non-zero, it is the alias set of a spill location. */
256 alias_set_type alias_set;
258 /* The offset of the first and byte before the last byte associated
259 with the operation. */
260 HOST_WIDE_INT begin, end;
262 union
264 /* A bitmask as wide as the number of bytes in the word that
265 contains a 1 if the byte may be needed. The store is unused if
266 all of the bits are 0. This is used if IS_LARGE is false. */
267 unsigned HOST_WIDE_INT small_bitmask;
269 struct
271 /* A bitmap with one bit per byte. Cleared bit means the position
272 is needed. Used if IS_LARGE is false. */
273 bitmap bmap;
275 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
276 equal to END - BEGIN, the whole store is unused. */
277 int count;
278 } large;
279 } positions_needed;
281 /* The next store info for this insn. */
282 struct store_info *next;
284 /* The right hand side of the store. This is used if there is a
285 subsequent reload of the mems address somewhere later in the
286 basic block. */
287 rtx rhs;
289 /* If rhs is or holds a constant, this contains that constant,
290 otherwise NULL. */
291 rtx const_rhs;
293 /* Set if this store stores the same constant value as REDUNDANT_REASON
294 insn stored. These aren't eliminated early, because doing that
295 might prevent the earlier larger store to be eliminated. */
296 struct insn_info *redundant_reason;
299 /* Return a bitmask with the first N low bits set. */
301 static unsigned HOST_WIDE_INT
302 lowpart_bitmask (int n)
304 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
305 return mask >> (HOST_BITS_PER_WIDE_INT - n);
308 typedef struct store_info *store_info_t;
309 static alloc_pool cse_store_info_pool;
310 static alloc_pool rtx_store_info_pool;
312 /* This structure holds information about a load. These are only
313 built for rtx bases. */
314 struct read_info
316 /* The id of the mem group of the base address. */
317 int group_id;
319 /* If this is non-zero, it is the alias set of a spill location. */
320 alias_set_type alias_set;
322 /* The offset of the first and byte after the last byte associated
323 with the operation. If begin == end == 0, the read did not have
324 a constant offset. */
325 int begin, end;
327 /* The mem being read. */
328 rtx mem;
330 /* The next read_info for this insn. */
331 struct read_info *next;
333 typedef struct read_info *read_info_t;
334 static alloc_pool read_info_pool;
337 /* One of these records is created for each insn. */
339 struct insn_info
341 /* Set true if the insn contains a store but the insn itself cannot
342 be deleted. This is set if the insn is a parallel and there is
343 more than one non dead output or if the insn is in some way
344 volatile. */
345 bool cannot_delete;
347 /* This field is only used by the global algorithm. It is set true
348 if the insn contains any read of mem except for a (1). This is
349 also set if the insn is a call or has a clobber mem. If the insn
350 contains a wild read, the use_rec will be null. */
351 bool wild_read;
353 /* This is true only for CALL instructions which could potentially read
354 any non-frame memory location. This field is used by the global
355 algorithm. */
356 bool non_frame_wild_read;
358 /* This field is only used for the processing of const functions.
359 These functions cannot read memory, but they can read the stack
360 because that is where they may get their parms. We need to be
361 this conservative because, like the store motion pass, we don't
362 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
363 Moreover, we need to distinguish two cases:
364 1. Before reload (register elimination), the stores related to
365 outgoing arguments are stack pointer based and thus deemed
366 of non-constant base in this pass. This requires special
367 handling but also means that the frame pointer based stores
368 need not be killed upon encountering a const function call.
369 2. After reload, the stores related to outgoing arguments can be
370 either stack pointer or hard frame pointer based. This means
371 that we have no other choice than also killing all the frame
372 pointer based stores upon encountering a const function call.
373 This field is set after reload for const function calls. Having
374 this set is less severe than a wild read, it just means that all
375 the frame related stores are killed rather than all the stores. */
376 bool frame_read;
378 /* This field is only used for the processing of const functions.
379 It is set if the insn may contain a stack pointer based store. */
380 bool stack_pointer_based;
382 /* This is true if any of the sets within the store contains a
383 cselib base. Such stores can only be deleted by the local
384 algorithm. */
385 bool contains_cselib_groups;
387 /* The insn. */
388 rtx_insn *insn;
390 /* The list of mem sets or mem clobbers that are contained in this
391 insn. If the insn is deletable, it contains only one mem set.
392 But it could also contain clobbers. Insns that contain more than
393 one mem set are not deletable, but each of those mems are here in
394 order to provide info to delete other insns. */
395 store_info_t store_rec;
397 /* The linked list of mem uses in this insn. Only the reads from
398 rtx bases are listed here. The reads to cselib bases are
399 completely processed during the first scan and so are never
400 created. */
401 read_info_t read_rec;
403 /* The live fixed registers. We assume only fixed registers can
404 cause trouble by being clobbered from an expanded pattern;
405 storing only the live fixed registers (rather than all registers)
406 means less memory needs to be allocated / copied for the individual
407 stores. */
408 regset fixed_regs_live;
410 /* The prev insn in the basic block. */
411 struct insn_info * prev_insn;
413 /* The linked list of insns that are in consideration for removal in
414 the forwards pass through the basic block. This pointer may be
415 trash as it is not cleared when a wild read occurs. The only
416 time it is guaranteed to be correct is when the traversal starts
417 at active_local_stores. */
418 struct insn_info * next_local_store;
421 typedef struct insn_info *insn_info_t;
422 static alloc_pool insn_info_pool;
424 /* The linked list of stores that are under consideration in this
425 basic block. */
426 static insn_info_t active_local_stores;
427 static int active_local_stores_len;
429 struct dse_bb_info
432 /* Pointer to the insn info for the last insn in the block. These
433 are linked so this is how all of the insns are reached. During
434 scanning this is the current insn being scanned. */
435 insn_info_t last_insn;
437 /* The info for the global dataflow problem. */
440 /* This is set if the transfer function should and in the wild_read
441 bitmap before applying the kill and gen sets. That vector knocks
442 out most of the bits in the bitmap and thus speeds up the
443 operations. */
444 bool apply_wild_read;
446 /* The following 4 bitvectors hold information about which positions
447 of which stores are live or dead. They are indexed by
448 get_bitmap_index. */
450 /* The set of store positions that exist in this block before a wild read. */
451 bitmap gen;
453 /* The set of load positions that exist in this block above the
454 same position of a store. */
455 bitmap kill;
457 /* The set of stores that reach the top of the block without being
458 killed by a read.
460 Do not represent the in if it is all ones. Note that this is
461 what the bitvector should logically be initialized to for a set
462 intersection problem. However, like the kill set, this is too
463 expensive. So initially, the in set will only be created for the
464 exit block and any block that contains a wild read. */
465 bitmap in;
467 /* The set of stores that reach the bottom of the block from it's
468 successors.
470 Do not represent the in if it is all ones. Note that this is
471 what the bitvector should logically be initialized to for a set
472 intersection problem. However, like the kill and in set, this is
473 too expensive. So what is done is that the confluence operator
474 just initializes the vector from one of the out sets of the
475 successors of the block. */
476 bitmap out;
478 /* The following bitvector is indexed by the reg number. It
479 contains the set of regs that are live at the current instruction
480 being processed. While it contains info for all of the
481 registers, only the hard registers are actually examined. It is used
482 to assure that shift and/or add sequences that are inserted do not
483 accidentally clobber live hard regs. */
484 bitmap regs_live;
487 typedef struct dse_bb_info *bb_info_t;
488 static alloc_pool bb_info_pool;
490 /* Table to hold all bb_infos. */
491 static bb_info_t *bb_table;
493 /* There is a group_info for each rtx base that is used to reference
494 memory. There are also not many of the rtx bases because they are
495 very limited in scope. */
497 struct group_info
499 /* The actual base of the address. */
500 rtx rtx_base;
502 /* The sequential id of the base. This allows us to have a
503 canonical ordering of these that is not based on addresses. */
504 int id;
506 /* True if there are any positions that are to be processed
507 globally. */
508 bool process_globally;
510 /* True if the base of this group is either the frame_pointer or
511 hard_frame_pointer. */
512 bool frame_related;
514 /* A mem wrapped around the base pointer for the group in order to do
515 read dependency. It must be given BLKmode in order to encompass all
516 the possible offsets from the base. */
517 rtx base_mem;
519 /* Canonized version of base_mem's address. */
520 rtx canon_base_addr;
522 /* These two sets of two bitmaps are used to keep track of how many
523 stores are actually referencing that position from this base. We
524 only do this for rtx bases as this will be used to assign
525 positions in the bitmaps for the global problem. Bit N is set in
526 store1 on the first store for offset N. Bit N is set in store2
527 for the second store to offset N. This is all we need since we
528 only care about offsets that have two or more stores for them.
530 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
531 for 0 and greater offsets.
533 There is one special case here, for stores into the stack frame,
534 we will or store1 into store2 before deciding which stores look
535 at globally. This is because stores to the stack frame that have
536 no other reads before the end of the function can also be
537 deleted. */
538 bitmap store1_n, store1_p, store2_n, store2_p;
540 /* These bitmaps keep track of offsets in this group escape this function.
541 An offset escapes if it corresponds to a named variable whose
542 addressable flag is set. */
543 bitmap escaped_n, escaped_p;
545 /* The positions in this bitmap have the same assignments as the in,
546 out, gen and kill bitmaps. This bitmap is all zeros except for
547 the positions that are occupied by stores for this group. */
548 bitmap group_kill;
550 /* The offset_map is used to map the offsets from this base into
551 positions in the global bitmaps. It is only created after all of
552 the all of stores have been scanned and we know which ones we
553 care about. */
554 int *offset_map_n, *offset_map_p;
555 int offset_map_size_n, offset_map_size_p;
557 typedef struct group_info *group_info_t;
558 typedef const struct group_info *const_group_info_t;
559 static alloc_pool rtx_group_info_pool;
561 /* Index into the rtx_group_vec. */
562 static int rtx_group_next_id;
565 static vec<group_info_t> rtx_group_vec;
568 /* This structure holds the set of changes that are being deferred
569 when removing read operation. See replace_read. */
570 struct deferred_change
573 /* The mem that is being replaced. */
574 rtx *loc;
576 /* The reg it is being replaced with. */
577 rtx reg;
579 struct deferred_change *next;
582 typedef struct deferred_change *deferred_change_t;
583 static alloc_pool deferred_change_pool;
585 static deferred_change_t deferred_change_list = NULL;
587 /* The group that holds all of the clear_alias_sets. */
588 static group_info_t clear_alias_group;
590 /* The modes of the clear_alias_sets. */
591 static htab_t clear_alias_mode_table;
593 /* Hash table element to look up the mode for an alias set. */
594 struct clear_alias_mode_holder
596 alias_set_type alias_set;
597 machine_mode mode;
600 /* This is true except if cfun->stdarg -- i.e. we cannot do
601 this for vararg functions because they play games with the frame. */
602 static bool stores_off_frame_dead_at_return;
604 /* Counter for stats. */
605 static int globally_deleted;
606 static int locally_deleted;
607 static int spill_deleted;
609 static bitmap all_blocks;
611 /* Locations that are killed by calls in the global phase. */
612 static bitmap kill_on_calls;
614 /* The number of bits used in the global bitmaps. */
615 static unsigned int current_position;
617 /*----------------------------------------------------------------------------
618 Zeroth step.
620 Initialization.
621 ----------------------------------------------------------------------------*/
624 /* Find the entry associated with ALIAS_SET. */
626 static struct clear_alias_mode_holder *
627 clear_alias_set_lookup (alias_set_type alias_set)
629 struct clear_alias_mode_holder tmp_holder;
630 void **slot;
632 tmp_holder.alias_set = alias_set;
633 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
634 gcc_assert (*slot);
636 return (struct clear_alias_mode_holder *) *slot;
640 /* Hashtable callbacks for maintaining the "bases" field of
641 store_group_info, given that the addresses are function invariants. */
643 struct invariant_group_base_hasher : typed_noop_remove <group_info>
645 typedef group_info value_type;
646 typedef group_info compare_type;
647 static inline hashval_t hash (const value_type *);
648 static inline bool equal (const value_type *, const compare_type *);
651 inline bool
652 invariant_group_base_hasher::equal (const value_type *gi1,
653 const compare_type *gi2)
655 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
658 inline hashval_t
659 invariant_group_base_hasher::hash (const value_type *gi)
661 int do_not_record;
662 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
665 /* Tables of group_info structures, hashed by base value. */
666 static hash_table<invariant_group_base_hasher> *rtx_group_table;
669 /* Get the GROUP for BASE. Add a new group if it is not there. */
671 static group_info_t
672 get_group_info (rtx base)
674 struct group_info tmp_gi;
675 group_info_t gi;
676 group_info **slot;
678 if (base)
680 /* Find the store_base_info structure for BASE, creating a new one
681 if necessary. */
682 tmp_gi.rtx_base = base;
683 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
684 gi = (group_info_t) *slot;
686 else
688 if (!clear_alias_group)
690 clear_alias_group = gi =
691 (group_info_t) pool_alloc (rtx_group_info_pool);
692 memset (gi, 0, sizeof (struct group_info));
693 gi->id = rtx_group_next_id++;
694 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
695 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
696 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
697 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
698 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
699 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
700 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
701 gi->process_globally = false;
702 gi->offset_map_size_n = 0;
703 gi->offset_map_size_p = 0;
704 gi->offset_map_n = NULL;
705 gi->offset_map_p = NULL;
706 rtx_group_vec.safe_push (gi);
708 return clear_alias_group;
711 if (gi == NULL)
713 *slot = gi = (group_info_t) pool_alloc (rtx_group_info_pool);
714 gi->rtx_base = base;
715 gi->id = rtx_group_next_id++;
716 gi->base_mem = gen_rtx_MEM (BLKmode, base);
717 gi->canon_base_addr = canon_rtx (base);
718 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
719 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
720 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
721 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
722 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
723 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
724 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
725 gi->process_globally = false;
726 gi->frame_related =
727 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
728 gi->offset_map_size_n = 0;
729 gi->offset_map_size_p = 0;
730 gi->offset_map_n = NULL;
731 gi->offset_map_p = NULL;
732 rtx_group_vec.safe_push (gi);
735 return gi;
739 /* Initialization of data structures. */
741 static void
742 dse_step0 (void)
744 locally_deleted = 0;
745 globally_deleted = 0;
746 spill_deleted = 0;
748 bitmap_obstack_initialize (&dse_bitmap_obstack);
749 gcc_obstack_init (&dse_obstack);
751 scratch = BITMAP_ALLOC (&reg_obstack);
752 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
754 rtx_store_info_pool
755 = create_alloc_pool ("rtx_store_info_pool",
756 sizeof (struct store_info), 100);
757 read_info_pool
758 = create_alloc_pool ("read_info_pool",
759 sizeof (struct read_info), 100);
760 insn_info_pool
761 = create_alloc_pool ("insn_info_pool",
762 sizeof (struct insn_info), 100);
763 bb_info_pool
764 = create_alloc_pool ("bb_info_pool",
765 sizeof (struct dse_bb_info), 100);
766 rtx_group_info_pool
767 = create_alloc_pool ("rtx_group_info_pool",
768 sizeof (struct group_info), 100);
769 deferred_change_pool
770 = create_alloc_pool ("deferred_change_pool",
771 sizeof (struct deferred_change), 10);
773 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
775 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
776 rtx_group_next_id = 0;
778 stores_off_frame_dead_at_return = !cfun->stdarg;
780 init_alias_analysis ();
782 clear_alias_group = NULL;
787 /*----------------------------------------------------------------------------
788 First step.
790 Scan all of the insns. Any random ordering of the blocks is fine.
791 Each block is scanned in forward order to accommodate cselib which
792 is used to remove stores with non-constant bases.
793 ----------------------------------------------------------------------------*/
795 /* Delete all of the store_info recs from INSN_INFO. */
797 static void
798 free_store_info (insn_info_t insn_info)
800 store_info_t store_info = insn_info->store_rec;
801 while (store_info)
803 store_info_t next = store_info->next;
804 if (store_info->is_large)
805 BITMAP_FREE (store_info->positions_needed.large.bmap);
806 if (store_info->cse_base)
807 pool_free (cse_store_info_pool, store_info);
808 else
809 pool_free (rtx_store_info_pool, store_info);
810 store_info = next;
813 insn_info->cannot_delete = true;
814 insn_info->contains_cselib_groups = false;
815 insn_info->store_rec = NULL;
818 typedef struct
820 rtx_insn *first, *current;
821 regset fixed_regs_live;
822 bool failure;
823 } note_add_store_info;
825 /* Callback for emit_inc_dec_insn_before via note_stores.
826 Check if a register is clobbered which is live afterwards. */
828 static void
829 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
831 rtx_insn *insn;
832 note_add_store_info *info = (note_add_store_info *) data;
833 int r, n;
835 if (!REG_P (loc))
836 return;
838 /* If this register is referenced by the current or an earlier insn,
839 that's OK. E.g. this applies to the register that is being incremented
840 with this addition. */
841 for (insn = info->first;
842 insn != NEXT_INSN (info->current);
843 insn = NEXT_INSN (insn))
844 if (reg_referenced_p (loc, PATTERN (insn)))
845 return;
847 /* If we come here, we have a clobber of a register that's only OK
848 if that register is not live. If we don't have liveness information
849 available, fail now. */
850 if (!info->fixed_regs_live)
852 info->failure = true;
853 return;
855 /* Now check if this is a live fixed register. */
856 r = REGNO (loc);
857 n = hard_regno_nregs[r][GET_MODE (loc)];
858 while (--n >= 0)
859 if (REGNO_REG_SET_P (info->fixed_regs_live, r+n))
860 info->failure = true;
863 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
864 SRC + SRCOFF before insn ARG. */
866 static int
867 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
868 rtx op ATTRIBUTE_UNUSED,
869 rtx dest, rtx src, rtx srcoff, void *arg)
871 insn_info_t insn_info = (insn_info_t) arg;
872 rtx_insn *insn = insn_info->insn, *new_insn, *cur;
873 note_add_store_info info;
875 /* We can reuse all operands without copying, because we are about
876 to delete the insn that contained it. */
877 if (srcoff)
879 start_sequence ();
880 emit_insn (gen_add3_insn (dest, src, srcoff));
881 new_insn = get_insns ();
882 end_sequence ();
884 else
885 new_insn = as_a <rtx_insn *> (gen_move_insn (dest, src));
886 info.first = new_insn;
887 info.fixed_regs_live = insn_info->fixed_regs_live;
888 info.failure = false;
889 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
891 info.current = cur;
892 note_stores (PATTERN (cur), note_add_store, &info);
895 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
896 return it immediately, communicating the failure to its caller. */
897 if (info.failure)
898 return 1;
900 emit_insn_before (new_insn, insn);
902 return 0;
905 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
906 is there, is split into a separate insn.
907 Return true on success (or if there was nothing to do), false on failure. */
909 static bool
910 check_for_inc_dec_1 (insn_info_t insn_info)
912 rtx_insn *insn = insn_info->insn;
913 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
914 if (note)
915 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
916 insn_info) == 0;
917 return true;
921 /* Entry point for postreload. If you work on reload_cse, or you need this
922 anywhere else, consider if you can provide register liveness information
923 and add a parameter to this function so that it can be passed down in
924 insn_info.fixed_regs_live. */
925 bool
926 check_for_inc_dec (rtx_insn *insn)
928 struct insn_info insn_info;
929 rtx note;
931 insn_info.insn = insn;
932 insn_info.fixed_regs_live = NULL;
933 note = find_reg_note (insn, REG_INC, NULL_RTX);
934 if (note)
935 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
936 &insn_info) == 0;
937 return true;
940 /* Delete the insn and free all of the fields inside INSN_INFO. */
942 static void
943 delete_dead_store_insn (insn_info_t insn_info)
945 read_info_t read_info;
947 if (!dbg_cnt (dse))
948 return;
950 if (!check_for_inc_dec_1 (insn_info))
951 return;
952 if (dump_file && (dump_flags & TDF_DETAILS))
954 fprintf (dump_file, "Locally deleting insn %d ",
955 INSN_UID (insn_info->insn));
956 if (insn_info->store_rec->alias_set)
957 fprintf (dump_file, "alias set %d\n",
958 (int) insn_info->store_rec->alias_set);
959 else
960 fprintf (dump_file, "\n");
963 free_store_info (insn_info);
964 read_info = insn_info->read_rec;
966 while (read_info)
968 read_info_t next = read_info->next;
969 pool_free (read_info_pool, read_info);
970 read_info = next;
972 insn_info->read_rec = NULL;
974 delete_insn (insn_info->insn);
975 locally_deleted++;
976 insn_info->insn = NULL;
978 insn_info->wild_read = false;
981 /* Return whether DECL, a local variable, can possibly escape the current
982 function scope. */
984 static bool
985 local_variable_can_escape (tree decl)
987 if (TREE_ADDRESSABLE (decl))
988 return true;
990 /* If this is a partitioned variable, we need to consider all the variables
991 in the partition. This is necessary because a store into one of them can
992 be replaced with a store into another and this may not change the outcome
993 of the escape analysis. */
994 if (cfun->gimple_df->decls_to_pointers != NULL)
996 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
997 if (namep)
998 return TREE_ADDRESSABLE (*namep);
1001 return false;
1004 /* Return whether EXPR can possibly escape the current function scope. */
1006 static bool
1007 can_escape (tree expr)
1009 tree base;
1010 if (!expr)
1011 return true;
1012 base = get_base_address (expr);
1013 if (DECL_P (base)
1014 && !may_be_aliased (base)
1015 && !(TREE_CODE (base) == VAR_DECL
1016 && !DECL_EXTERNAL (base)
1017 && !TREE_STATIC (base)
1018 && local_variable_can_escape (base)))
1019 return false;
1020 return true;
1023 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1024 OFFSET and WIDTH. */
1026 static void
1027 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
1028 tree expr)
1030 HOST_WIDE_INT i;
1031 bool expr_escapes = can_escape (expr);
1032 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
1033 for (i=offset; i<offset+width; i++)
1035 bitmap store1;
1036 bitmap store2;
1037 bitmap escaped;
1038 int ai;
1039 if (i < 0)
1041 store1 = group->store1_n;
1042 store2 = group->store2_n;
1043 escaped = group->escaped_n;
1044 ai = -i;
1046 else
1048 store1 = group->store1_p;
1049 store2 = group->store2_p;
1050 escaped = group->escaped_p;
1051 ai = i;
1054 if (!bitmap_set_bit (store1, ai))
1055 bitmap_set_bit (store2, ai);
1056 else
1058 if (i < 0)
1060 if (group->offset_map_size_n < ai)
1061 group->offset_map_size_n = ai;
1063 else
1065 if (group->offset_map_size_p < ai)
1066 group->offset_map_size_p = ai;
1069 if (expr_escapes)
1070 bitmap_set_bit (escaped, ai);
1074 static void
1075 reset_active_stores (void)
1077 active_local_stores = NULL;
1078 active_local_stores_len = 0;
1081 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1083 static void
1084 free_read_records (bb_info_t bb_info)
1086 insn_info_t insn_info = bb_info->last_insn;
1087 read_info_t *ptr = &insn_info->read_rec;
1088 while (*ptr)
1090 read_info_t next = (*ptr)->next;
1091 if ((*ptr)->alias_set == 0)
1093 pool_free (read_info_pool, *ptr);
1094 *ptr = next;
1096 else
1097 ptr = &(*ptr)->next;
1101 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1103 static void
1104 add_wild_read (bb_info_t bb_info)
1106 insn_info_t insn_info = bb_info->last_insn;
1107 insn_info->wild_read = true;
1108 free_read_records (bb_info);
1109 reset_active_stores ();
1112 /* Set the BB_INFO so that the last insn is marked as a wild read of
1113 non-frame locations. */
1115 static void
1116 add_non_frame_wild_read (bb_info_t bb_info)
1118 insn_info_t insn_info = bb_info->last_insn;
1119 insn_info->non_frame_wild_read = true;
1120 free_read_records (bb_info);
1121 reset_active_stores ();
1124 /* Return true if X is a constant or one of the registers that behave
1125 as a constant over the life of a function. This is equivalent to
1126 !rtx_varies_p for memory addresses. */
1128 static bool
1129 const_or_frame_p (rtx x)
1131 if (CONSTANT_P (x))
1132 return true;
1134 if (GET_CODE (x) == REG)
1136 /* Note that we have to test for the actual rtx used for the frame
1137 and arg pointers and not just the register number in case we have
1138 eliminated the frame and/or arg pointer and are using it
1139 for pseudos. */
1140 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1141 /* The arg pointer varies if it is not a fixed register. */
1142 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1143 || x == pic_offset_table_rtx)
1144 return true;
1145 return false;
1148 return false;
1151 /* Take all reasonable action to put the address of MEM into the form
1152 that we can do analysis on.
1154 The gold standard is to get the address into the form: address +
1155 OFFSET where address is something that rtx_varies_p considers a
1156 constant. When we can get the address in this form, we can do
1157 global analysis on it. Note that for constant bases, address is
1158 not actually returned, only the group_id. The address can be
1159 obtained from that.
1161 If that fails, we try cselib to get a value we can at least use
1162 locally. If that fails we return false.
1164 The GROUP_ID is set to -1 for cselib bases and the index of the
1165 group for non_varying bases.
1167 FOR_READ is true if this is a mem read and false if not. */
1169 static bool
1170 canon_address (rtx mem,
1171 alias_set_type *alias_set_out,
1172 int *group_id,
1173 HOST_WIDE_INT *offset,
1174 cselib_val **base)
1176 machine_mode address_mode = get_address_mode (mem);
1177 rtx mem_address = XEXP (mem, 0);
1178 rtx expanded_address, address;
1179 int expanded;
1181 *alias_set_out = 0;
1183 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1185 if (dump_file && (dump_flags & TDF_DETAILS))
1187 fprintf (dump_file, " mem: ");
1188 print_inline_rtx (dump_file, mem_address, 0);
1189 fprintf (dump_file, "\n");
1192 /* First see if just canon_rtx (mem_address) is const or frame,
1193 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1194 address = NULL_RTX;
1195 for (expanded = 0; expanded < 2; expanded++)
1197 if (expanded)
1199 /* Use cselib to replace all of the reg references with the full
1200 expression. This will take care of the case where we have
1202 r_x = base + offset;
1203 val = *r_x;
1205 by making it into
1207 val = *(base + offset); */
1209 expanded_address = cselib_expand_value_rtx (mem_address,
1210 scratch, 5);
1212 /* If this fails, just go with the address from first
1213 iteration. */
1214 if (!expanded_address)
1215 break;
1217 else
1218 expanded_address = mem_address;
1220 /* Split the address into canonical BASE + OFFSET terms. */
1221 address = canon_rtx (expanded_address);
1223 *offset = 0;
1225 if (dump_file && (dump_flags & TDF_DETAILS))
1227 if (expanded)
1229 fprintf (dump_file, "\n after cselib_expand address: ");
1230 print_inline_rtx (dump_file, expanded_address, 0);
1231 fprintf (dump_file, "\n");
1234 fprintf (dump_file, "\n after canon_rtx address: ");
1235 print_inline_rtx (dump_file, address, 0);
1236 fprintf (dump_file, "\n");
1239 if (GET_CODE (address) == CONST)
1240 address = XEXP (address, 0);
1242 if (GET_CODE (address) == PLUS
1243 && CONST_INT_P (XEXP (address, 1)))
1245 *offset = INTVAL (XEXP (address, 1));
1246 address = XEXP (address, 0);
1249 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1250 && const_or_frame_p (address))
1252 group_info_t group = get_group_info (address);
1254 if (dump_file && (dump_flags & TDF_DETAILS))
1255 fprintf (dump_file, " gid=%d offset=%d \n",
1256 group->id, (int)*offset);
1257 *base = NULL;
1258 *group_id = group->id;
1259 return true;
1263 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1264 *group_id = -1;
1266 if (*base == NULL)
1268 if (dump_file && (dump_flags & TDF_DETAILS))
1269 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1270 return false;
1272 if (dump_file && (dump_flags & TDF_DETAILS))
1273 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1274 (*base)->uid, (*base)->hash, (int)*offset);
1275 return true;
1279 /* Clear the rhs field from the active_local_stores array. */
1281 static void
1282 clear_rhs_from_active_local_stores (void)
1284 insn_info_t ptr = active_local_stores;
1286 while (ptr)
1288 store_info_t store_info = ptr->store_rec;
1289 /* Skip the clobbers. */
1290 while (!store_info->is_set)
1291 store_info = store_info->next;
1293 store_info->rhs = NULL;
1294 store_info->const_rhs = NULL;
1296 ptr = ptr->next_local_store;
1301 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1303 static inline void
1304 set_position_unneeded (store_info_t s_info, int pos)
1306 if (__builtin_expect (s_info->is_large, false))
1308 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1309 s_info->positions_needed.large.count++;
1311 else
1312 s_info->positions_needed.small_bitmask
1313 &= ~(((unsigned HOST_WIDE_INT) 1) << pos);
1316 /* Mark the whole store S_INFO as unneeded. */
1318 static inline void
1319 set_all_positions_unneeded (store_info_t s_info)
1321 if (__builtin_expect (s_info->is_large, false))
1323 int pos, end = s_info->end - s_info->begin;
1324 for (pos = 0; pos < end; pos++)
1325 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1326 s_info->positions_needed.large.count = end;
1328 else
1329 s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0;
1332 /* Return TRUE if any bytes from S_INFO store are needed. */
1334 static inline bool
1335 any_positions_needed_p (store_info_t s_info)
1337 if (__builtin_expect (s_info->is_large, false))
1338 return (s_info->positions_needed.large.count
1339 < s_info->end - s_info->begin);
1340 else
1341 return (s_info->positions_needed.small_bitmask
1342 != (unsigned HOST_WIDE_INT) 0);
1345 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1346 store are needed. */
1348 static inline bool
1349 all_positions_needed_p (store_info_t s_info, int start, int width)
1351 if (__builtin_expect (s_info->is_large, false))
1353 int end = start + width;
1354 while (start < end)
1355 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1356 return false;
1357 return true;
1359 else
1361 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1362 return (s_info->positions_needed.small_bitmask & mask) == mask;
1367 static rtx get_stored_val (store_info_t, machine_mode, HOST_WIDE_INT,
1368 HOST_WIDE_INT, basic_block, bool);
1371 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1372 there is a candidate store, after adding it to the appropriate
1373 local store group if so. */
1375 static int
1376 record_store (rtx body, bb_info_t bb_info)
1378 rtx mem, rhs, const_rhs, mem_addr;
1379 HOST_WIDE_INT offset = 0;
1380 HOST_WIDE_INT width = 0;
1381 alias_set_type spill_alias_set;
1382 insn_info_t insn_info = bb_info->last_insn;
1383 store_info_t store_info = NULL;
1384 int group_id;
1385 cselib_val *base = NULL;
1386 insn_info_t ptr, last, redundant_reason;
1387 bool store_is_unused;
1389 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1390 return 0;
1392 mem = SET_DEST (body);
1394 /* If this is not used, then this cannot be used to keep the insn
1395 from being deleted. On the other hand, it does provide something
1396 that can be used to prove that another store is dead. */
1397 store_is_unused
1398 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1400 /* Check whether that value is a suitable memory location. */
1401 if (!MEM_P (mem))
1403 /* If the set or clobber is unused, then it does not effect our
1404 ability to get rid of the entire insn. */
1405 if (!store_is_unused)
1406 insn_info->cannot_delete = true;
1407 return 0;
1410 /* At this point we know mem is a mem. */
1411 if (GET_MODE (mem) == BLKmode)
1413 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1415 if (dump_file && (dump_flags & TDF_DETAILS))
1416 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1417 add_wild_read (bb_info);
1418 insn_info->cannot_delete = true;
1419 return 0;
1421 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1422 as memset (addr, 0, 36); */
1423 else if (!MEM_SIZE_KNOWN_P (mem)
1424 || MEM_SIZE (mem) <= 0
1425 || MEM_SIZE (mem) > MAX_OFFSET
1426 || GET_CODE (body) != SET
1427 || !CONST_INT_P (SET_SRC (body)))
1429 if (!store_is_unused)
1431 /* If the set or clobber is unused, then it does not effect our
1432 ability to get rid of the entire insn. */
1433 insn_info->cannot_delete = true;
1434 clear_rhs_from_active_local_stores ();
1436 return 0;
1440 /* We can still process a volatile mem, we just cannot delete it. */
1441 if (MEM_VOLATILE_P (mem))
1442 insn_info->cannot_delete = true;
1444 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1446 clear_rhs_from_active_local_stores ();
1447 return 0;
1450 if (GET_MODE (mem) == BLKmode)
1451 width = MEM_SIZE (mem);
1452 else
1453 width = GET_MODE_SIZE (GET_MODE (mem));
1455 if (spill_alias_set)
1457 bitmap store1 = clear_alias_group->store1_p;
1458 bitmap store2 = clear_alias_group->store2_p;
1460 gcc_assert (GET_MODE (mem) != BLKmode);
1462 if (!bitmap_set_bit (store1, spill_alias_set))
1463 bitmap_set_bit (store2, spill_alias_set);
1465 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1466 clear_alias_group->offset_map_size_p = spill_alias_set;
1468 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1470 if (dump_file && (dump_flags & TDF_DETAILS))
1471 fprintf (dump_file, " processing spill store %d(%s)\n",
1472 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1474 else if (group_id >= 0)
1476 /* In the restrictive case where the base is a constant or the
1477 frame pointer we can do global analysis. */
1479 group_info_t group
1480 = rtx_group_vec[group_id];
1481 tree expr = MEM_EXPR (mem);
1483 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1484 set_usage_bits (group, offset, width, expr);
1486 if (dump_file && (dump_flags & TDF_DETAILS))
1487 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1488 group_id, (int)offset, (int)(offset+width));
1490 else
1492 if (may_be_sp_based_p (XEXP (mem, 0)))
1493 insn_info->stack_pointer_based = true;
1494 insn_info->contains_cselib_groups = true;
1496 store_info = (store_info_t) pool_alloc (cse_store_info_pool);
1497 group_id = -1;
1499 if (dump_file && (dump_flags & TDF_DETAILS))
1500 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1501 (int)offset, (int)(offset+width));
1504 const_rhs = rhs = NULL_RTX;
1505 if (GET_CODE (body) == SET
1506 /* No place to keep the value after ra. */
1507 && !reload_completed
1508 && (REG_P (SET_SRC (body))
1509 || GET_CODE (SET_SRC (body)) == SUBREG
1510 || CONSTANT_P (SET_SRC (body)))
1511 && !MEM_VOLATILE_P (mem)
1512 /* Sometimes the store and reload is used for truncation and
1513 rounding. */
1514 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1516 rhs = SET_SRC (body);
1517 if (CONSTANT_P (rhs))
1518 const_rhs = rhs;
1519 else if (body == PATTERN (insn_info->insn))
1521 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1522 if (tem && CONSTANT_P (XEXP (tem, 0)))
1523 const_rhs = XEXP (tem, 0);
1525 if (const_rhs == NULL_RTX && REG_P (rhs))
1527 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1529 if (tem && CONSTANT_P (tem))
1530 const_rhs = tem;
1534 /* Check to see if this stores causes some other stores to be
1535 dead. */
1536 ptr = active_local_stores;
1537 last = NULL;
1538 redundant_reason = NULL;
1539 mem = canon_rtx (mem);
1540 /* For alias_set != 0 canon_true_dependence should be never called. */
1541 if (spill_alias_set)
1542 mem_addr = NULL_RTX;
1543 else
1545 if (group_id < 0)
1546 mem_addr = base->val_rtx;
1547 else
1549 group_info_t group
1550 = rtx_group_vec[group_id];
1551 mem_addr = group->canon_base_addr;
1553 if (offset)
1554 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1557 while (ptr)
1559 insn_info_t next = ptr->next_local_store;
1560 store_info_t s_info = ptr->store_rec;
1561 bool del = true;
1563 /* Skip the clobbers. We delete the active insn if this insn
1564 shadows the set. To have been put on the active list, it
1565 has exactly on set. */
1566 while (!s_info->is_set)
1567 s_info = s_info->next;
1569 if (s_info->alias_set != spill_alias_set)
1570 del = false;
1571 else if (s_info->alias_set)
1573 struct clear_alias_mode_holder *entry
1574 = clear_alias_set_lookup (s_info->alias_set);
1575 /* Generally, spills cannot be processed if and of the
1576 references to the slot have a different mode. But if
1577 we are in the same block and mode is exactly the same
1578 between this store and one before in the same block,
1579 we can still delete it. */
1580 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1581 && (GET_MODE (mem) == entry->mode))
1583 del = true;
1584 set_all_positions_unneeded (s_info);
1586 if (dump_file && (dump_flags & TDF_DETAILS))
1587 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1588 INSN_UID (ptr->insn), (int) s_info->alias_set);
1590 else if ((s_info->group_id == group_id)
1591 && (s_info->cse_base == base))
1593 HOST_WIDE_INT i;
1594 if (dump_file && (dump_flags & TDF_DETAILS))
1595 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1596 INSN_UID (ptr->insn), s_info->group_id,
1597 (int)s_info->begin, (int)s_info->end);
1599 /* Even if PTR won't be eliminated as unneeded, if both
1600 PTR and this insn store the same constant value, we might
1601 eliminate this insn instead. */
1602 if (s_info->const_rhs
1603 && const_rhs
1604 && offset >= s_info->begin
1605 && offset + width <= s_info->end
1606 && all_positions_needed_p (s_info, offset - s_info->begin,
1607 width))
1609 if (GET_MODE (mem) == BLKmode)
1611 if (GET_MODE (s_info->mem) == BLKmode
1612 && s_info->const_rhs == const_rhs)
1613 redundant_reason = ptr;
1615 else if (s_info->const_rhs == const0_rtx
1616 && const_rhs == const0_rtx)
1617 redundant_reason = ptr;
1618 else
1620 rtx val;
1621 start_sequence ();
1622 val = get_stored_val (s_info, GET_MODE (mem),
1623 offset, offset + width,
1624 BLOCK_FOR_INSN (insn_info->insn),
1625 true);
1626 if (get_insns () != NULL)
1627 val = NULL_RTX;
1628 end_sequence ();
1629 if (val && rtx_equal_p (val, const_rhs))
1630 redundant_reason = ptr;
1634 for (i = MAX (offset, s_info->begin);
1635 i < offset + width && i < s_info->end;
1636 i++)
1637 set_position_unneeded (s_info, i - s_info->begin);
1639 else if (s_info->rhs)
1640 /* Need to see if it is possible for this store to overwrite
1641 the value of store_info. If it is, set the rhs to NULL to
1642 keep it from being used to remove a load. */
1644 if (canon_true_dependence (s_info->mem,
1645 GET_MODE (s_info->mem),
1646 s_info->mem_addr,
1647 mem, mem_addr))
1649 s_info->rhs = NULL;
1650 s_info->const_rhs = NULL;
1654 /* An insn can be deleted if every position of every one of
1655 its s_infos is zero. */
1656 if (any_positions_needed_p (s_info))
1657 del = false;
1659 if (del)
1661 insn_info_t insn_to_delete = ptr;
1663 active_local_stores_len--;
1664 if (last)
1665 last->next_local_store = ptr->next_local_store;
1666 else
1667 active_local_stores = ptr->next_local_store;
1669 if (!insn_to_delete->cannot_delete)
1670 delete_dead_store_insn (insn_to_delete);
1672 else
1673 last = ptr;
1675 ptr = next;
1678 /* Finish filling in the store_info. */
1679 store_info->next = insn_info->store_rec;
1680 insn_info->store_rec = store_info;
1681 store_info->mem = mem;
1682 store_info->alias_set = spill_alias_set;
1683 store_info->mem_addr = mem_addr;
1684 store_info->cse_base = base;
1685 if (width > HOST_BITS_PER_WIDE_INT)
1687 store_info->is_large = true;
1688 store_info->positions_needed.large.count = 0;
1689 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1691 else
1693 store_info->is_large = false;
1694 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1696 store_info->group_id = group_id;
1697 store_info->begin = offset;
1698 store_info->end = offset + width;
1699 store_info->is_set = GET_CODE (body) == SET;
1700 store_info->rhs = rhs;
1701 store_info->const_rhs = const_rhs;
1702 store_info->redundant_reason = redundant_reason;
1704 /* If this is a clobber, we return 0. We will only be able to
1705 delete this insn if there is only one store USED store, but we
1706 can use the clobber to delete other stores earlier. */
1707 return store_info->is_set ? 1 : 0;
1711 static void
1712 dump_insn_info (const char * start, insn_info_t insn_info)
1714 fprintf (dump_file, "%s insn=%d %s\n", start,
1715 INSN_UID (insn_info->insn),
1716 insn_info->store_rec ? "has store" : "naked");
1720 /* If the modes are different and the value's source and target do not
1721 line up, we need to extract the value from lower part of the rhs of
1722 the store, shift it, and then put it into a form that can be shoved
1723 into the read_insn. This function generates a right SHIFT of a
1724 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1725 shift sequence is returned or NULL if we failed to find a
1726 shift. */
1728 static rtx
1729 find_shift_sequence (int access_size,
1730 store_info_t store_info,
1731 machine_mode read_mode,
1732 int shift, bool speed, bool require_cst)
1734 machine_mode store_mode = GET_MODE (store_info->mem);
1735 machine_mode new_mode;
1736 rtx read_reg = NULL;
1738 /* Some machines like the x86 have shift insns for each size of
1739 operand. Other machines like the ppc or the ia-64 may only have
1740 shift insns that shift values within 32 or 64 bit registers.
1741 This loop tries to find the smallest shift insn that will right
1742 justify the value we want to read but is available in one insn on
1743 the machine. */
1745 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1746 MODE_INT);
1747 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1748 new_mode = GET_MODE_WIDER_MODE (new_mode))
1750 rtx target, new_reg, new_lhs;
1751 rtx_insn *shift_seq, *insn;
1752 int cost;
1754 /* If a constant was stored into memory, try to simplify it here,
1755 otherwise the cost of the shift might preclude this optimization
1756 e.g. at -Os, even when no actual shift will be needed. */
1757 if (store_info->const_rhs)
1759 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1760 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1761 store_mode, byte);
1762 if (ret && CONSTANT_P (ret))
1764 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1765 ret, GEN_INT (shift));
1766 if (ret && CONSTANT_P (ret))
1768 byte = subreg_lowpart_offset (read_mode, new_mode);
1769 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1770 if (ret && CONSTANT_P (ret)
1771 && set_src_cost (ret, speed) <= COSTS_N_INSNS (1))
1772 return ret;
1777 if (require_cst)
1778 return NULL_RTX;
1780 /* Try a wider mode if truncating the store mode to NEW_MODE
1781 requires a real instruction. */
1782 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1783 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1784 continue;
1786 /* Also try a wider mode if the necessary punning is either not
1787 desirable or not possible. */
1788 if (!CONSTANT_P (store_info->rhs)
1789 && !MODES_TIEABLE_P (new_mode, store_mode))
1790 continue;
1792 new_reg = gen_reg_rtx (new_mode);
1794 start_sequence ();
1796 /* In theory we could also check for an ashr. Ian Taylor knows
1797 of one dsp where the cost of these two was not the same. But
1798 this really is a rare case anyway. */
1799 target = expand_binop (new_mode, lshr_optab, new_reg,
1800 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1802 shift_seq = get_insns ();
1803 end_sequence ();
1805 if (target != new_reg || shift_seq == NULL)
1806 continue;
1808 cost = 0;
1809 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1810 if (INSN_P (insn))
1811 cost += insn_rtx_cost (PATTERN (insn), speed);
1813 /* The computation up to here is essentially independent
1814 of the arguments and could be precomputed. It may
1815 not be worth doing so. We could precompute if
1816 worthwhile or at least cache the results. The result
1817 technically depends on both SHIFT and ACCESS_SIZE,
1818 but in practice the answer will depend only on ACCESS_SIZE. */
1820 if (cost > COSTS_N_INSNS (1))
1821 continue;
1823 new_lhs = extract_low_bits (new_mode, store_mode,
1824 copy_rtx (store_info->rhs));
1825 if (new_lhs == NULL_RTX)
1826 continue;
1828 /* We found an acceptable shift. Generate a move to
1829 take the value from the store and put it into the
1830 shift pseudo, then shift it, then generate another
1831 move to put in into the target of the read. */
1832 emit_move_insn (new_reg, new_lhs);
1833 emit_insn (shift_seq);
1834 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1835 break;
1838 return read_reg;
1842 /* Call back for note_stores to find the hard regs set or clobbered by
1843 insn. Data is a bitmap of the hardregs set so far. */
1845 static void
1846 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1848 bitmap regs_set = (bitmap) data;
1850 if (REG_P (x)
1851 && HARD_REGISTER_P (x))
1853 unsigned int regno = REGNO (x);
1854 bitmap_set_range (regs_set, regno,
1855 hard_regno_nregs[regno][GET_MODE (x)]);
1859 /* Helper function for replace_read and record_store.
1860 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1861 to one before READ_END bytes read in READ_MODE. Return NULL
1862 if not successful. If REQUIRE_CST is true, return always constant. */
1864 static rtx
1865 get_stored_val (store_info_t store_info, machine_mode read_mode,
1866 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1867 basic_block bb, bool require_cst)
1869 machine_mode store_mode = GET_MODE (store_info->mem);
1870 int shift;
1871 int access_size; /* In bytes. */
1872 rtx read_reg;
1874 /* To get here the read is within the boundaries of the write so
1875 shift will never be negative. Start out with the shift being in
1876 bytes. */
1877 if (store_mode == BLKmode)
1878 shift = 0;
1879 else if (BYTES_BIG_ENDIAN)
1880 shift = store_info->end - read_end;
1881 else
1882 shift = read_begin - store_info->begin;
1884 access_size = shift + GET_MODE_SIZE (read_mode);
1886 /* From now on it is bits. */
1887 shift *= BITS_PER_UNIT;
1889 if (shift)
1890 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1891 optimize_bb_for_speed_p (bb),
1892 require_cst);
1893 else if (store_mode == BLKmode)
1895 /* The store is a memset (addr, const_val, const_size). */
1896 gcc_assert (CONST_INT_P (store_info->rhs));
1897 store_mode = int_mode_for_mode (read_mode);
1898 if (store_mode == BLKmode)
1899 read_reg = NULL_RTX;
1900 else if (store_info->rhs == const0_rtx)
1901 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1902 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1903 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1904 read_reg = NULL_RTX;
1905 else
1907 unsigned HOST_WIDE_INT c
1908 = INTVAL (store_info->rhs)
1909 & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
1910 int shift = BITS_PER_UNIT;
1911 while (shift < HOST_BITS_PER_WIDE_INT)
1913 c |= (c << shift);
1914 shift <<= 1;
1916 read_reg = gen_int_mode (c, store_mode);
1917 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1920 else if (store_info->const_rhs
1921 && (require_cst
1922 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1923 read_reg = extract_low_bits (read_mode, store_mode,
1924 copy_rtx (store_info->const_rhs));
1925 else
1926 read_reg = extract_low_bits (read_mode, store_mode,
1927 copy_rtx (store_info->rhs));
1928 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1929 read_reg = NULL_RTX;
1930 return read_reg;
1933 /* Take a sequence of:
1934 A <- r1
1936 ... <- A
1938 and change it into
1939 r2 <- r1
1940 A <- r1
1942 ... <- r2
1946 r3 <- extract (r1)
1947 r3 <- r3 >> shift
1948 r2 <- extract (r3)
1949 ... <- r2
1953 r2 <- extract (r1)
1954 ... <- r2
1956 Depending on the alignment and the mode of the store and
1957 subsequent load.
1960 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1961 and READ_INSN are for the read. Return true if the replacement
1962 went ok. */
1964 static bool
1965 replace_read (store_info_t store_info, insn_info_t store_insn,
1966 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1967 bitmap regs_live)
1969 machine_mode store_mode = GET_MODE (store_info->mem);
1970 machine_mode read_mode = GET_MODE (read_info->mem);
1971 rtx_insn *insns, *this_insn;
1972 rtx read_reg;
1973 basic_block bb;
1975 if (!dbg_cnt (dse))
1976 return false;
1978 /* Create a sequence of instructions to set up the read register.
1979 This sequence goes immediately before the store and its result
1980 is read by the load.
1982 We need to keep this in perspective. We are replacing a read
1983 with a sequence of insns, but the read will almost certainly be
1984 in cache, so it is not going to be an expensive one. Thus, we
1985 are not willing to do a multi insn shift or worse a subroutine
1986 call to get rid of the read. */
1987 if (dump_file && (dump_flags & TDF_DETAILS))
1988 fprintf (dump_file, "trying to replace %smode load in insn %d"
1989 " from %smode store in insn %d\n",
1990 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1991 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1992 start_sequence ();
1993 bb = BLOCK_FOR_INSN (read_insn->insn);
1994 read_reg = get_stored_val (store_info,
1995 read_mode, read_info->begin, read_info->end,
1996 bb, false);
1997 if (read_reg == NULL_RTX)
1999 end_sequence ();
2000 if (dump_file && (dump_flags & TDF_DETAILS))
2001 fprintf (dump_file, " -- could not extract bits of stored value\n");
2002 return false;
2004 /* Force the value into a new register so that it won't be clobbered
2005 between the store and the load. */
2006 read_reg = copy_to_mode_reg (read_mode, read_reg);
2007 insns = get_insns ();
2008 end_sequence ();
2010 if (insns != NULL_RTX)
2012 /* Now we have to scan the set of new instructions to see if the
2013 sequence contains and sets of hardregs that happened to be
2014 live at this point. For instance, this can happen if one of
2015 the insns sets the CC and the CC happened to be live at that
2016 point. This does occasionally happen, see PR 37922. */
2017 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
2019 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
2020 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
2022 bitmap_and_into (regs_set, regs_live);
2023 if (!bitmap_empty_p (regs_set))
2025 if (dump_file && (dump_flags & TDF_DETAILS))
2027 fprintf (dump_file,
2028 "abandoning replacement because sequence clobbers live hardregs:");
2029 df_print_regset (dump_file, regs_set);
2032 BITMAP_FREE (regs_set);
2033 return false;
2035 BITMAP_FREE (regs_set);
2038 if (validate_change (read_insn->insn, loc, read_reg, 0))
2040 deferred_change_t deferred_change =
2041 (deferred_change_t) pool_alloc (deferred_change_pool);
2043 /* Insert this right before the store insn where it will be safe
2044 from later insns that might change it before the read. */
2045 emit_insn_before (insns, store_insn->insn);
2047 /* And now for the kludge part: cselib croaks if you just
2048 return at this point. There are two reasons for this:
2050 1) Cselib has an idea of how many pseudos there are and
2051 that does not include the new ones we just added.
2053 2) Cselib does not know about the move insn we added
2054 above the store_info, and there is no way to tell it
2055 about it, because it has "moved on".
2057 Problem (1) is fixable with a certain amount of engineering.
2058 Problem (2) is requires starting the bb from scratch. This
2059 could be expensive.
2061 So we are just going to have to lie. The move/extraction
2062 insns are not really an issue, cselib did not see them. But
2063 the use of the new pseudo read_insn is a real problem because
2064 cselib has not scanned this insn. The way that we solve this
2065 problem is that we are just going to put the mem back for now
2066 and when we are finished with the block, we undo this. We
2067 keep a table of mems to get rid of. At the end of the basic
2068 block we can put them back. */
2070 *loc = read_info->mem;
2071 deferred_change->next = deferred_change_list;
2072 deferred_change_list = deferred_change;
2073 deferred_change->loc = loc;
2074 deferred_change->reg = read_reg;
2076 /* Get rid of the read_info, from the point of view of the
2077 rest of dse, play like this read never happened. */
2078 read_insn->read_rec = read_info->next;
2079 pool_free (read_info_pool, read_info);
2080 if (dump_file && (dump_flags & TDF_DETAILS))
2082 fprintf (dump_file, " -- replaced the loaded MEM with ");
2083 print_simple_rtl (dump_file, read_reg);
2084 fprintf (dump_file, "\n");
2086 return true;
2088 else
2090 if (dump_file && (dump_flags & TDF_DETAILS))
2092 fprintf (dump_file, " -- replacing the loaded MEM with ");
2093 print_simple_rtl (dump_file, read_reg);
2094 fprintf (dump_file, " led to an invalid instruction\n");
2096 return false;
2100 /* Check the address of MEM *LOC and kill any appropriate stores that may
2101 be active. */
2103 static void
2104 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
2106 rtx mem = *loc, mem_addr;
2107 insn_info_t insn_info;
2108 HOST_WIDE_INT offset = 0;
2109 HOST_WIDE_INT width = 0;
2110 alias_set_type spill_alias_set = 0;
2111 cselib_val *base = NULL;
2112 int group_id;
2113 read_info_t read_info;
2115 insn_info = bb_info->last_insn;
2117 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2118 || (MEM_VOLATILE_P (mem)))
2120 if (dump_file && (dump_flags & TDF_DETAILS))
2121 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
2122 add_wild_read (bb_info);
2123 insn_info->cannot_delete = true;
2124 return;
2127 /* If it is reading readonly mem, then there can be no conflict with
2128 another write. */
2129 if (MEM_READONLY_P (mem))
2130 return;
2132 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
2134 if (dump_file && (dump_flags & TDF_DETAILS))
2135 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2136 add_wild_read (bb_info);
2137 return;
2140 if (GET_MODE (mem) == BLKmode)
2141 width = -1;
2142 else
2143 width = GET_MODE_SIZE (GET_MODE (mem));
2145 read_info = (read_info_t) pool_alloc (read_info_pool);
2146 read_info->group_id = group_id;
2147 read_info->mem = mem;
2148 read_info->alias_set = spill_alias_set;
2149 read_info->begin = offset;
2150 read_info->end = offset + width;
2151 read_info->next = insn_info->read_rec;
2152 insn_info->read_rec = read_info;
2153 /* For alias_set != 0 canon_true_dependence should be never called. */
2154 if (spill_alias_set)
2155 mem_addr = NULL_RTX;
2156 else
2158 if (group_id < 0)
2159 mem_addr = base->val_rtx;
2160 else
2162 group_info_t group
2163 = rtx_group_vec[group_id];
2164 mem_addr = group->canon_base_addr;
2166 if (offset)
2167 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2170 /* We ignore the clobbers in store_info. The is mildly aggressive,
2171 but there really should not be a clobber followed by a read. */
2173 if (spill_alias_set)
2175 insn_info_t i_ptr = active_local_stores;
2176 insn_info_t last = NULL;
2178 if (dump_file && (dump_flags & TDF_DETAILS))
2179 fprintf (dump_file, " processing spill load %d\n",
2180 (int) spill_alias_set);
2182 while (i_ptr)
2184 store_info_t store_info = i_ptr->store_rec;
2186 /* Skip the clobbers. */
2187 while (!store_info->is_set)
2188 store_info = store_info->next;
2190 if (store_info->alias_set == spill_alias_set)
2192 if (dump_file && (dump_flags & TDF_DETAILS))
2193 dump_insn_info ("removing from active", i_ptr);
2195 active_local_stores_len--;
2196 if (last)
2197 last->next_local_store = i_ptr->next_local_store;
2198 else
2199 active_local_stores = i_ptr->next_local_store;
2201 else
2202 last = i_ptr;
2203 i_ptr = i_ptr->next_local_store;
2206 else if (group_id >= 0)
2208 /* This is the restricted case where the base is a constant or
2209 the frame pointer and offset is a constant. */
2210 insn_info_t i_ptr = active_local_stores;
2211 insn_info_t last = NULL;
2213 if (dump_file && (dump_flags & TDF_DETAILS))
2215 if (width == -1)
2216 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2217 group_id);
2218 else
2219 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2220 group_id, (int)offset, (int)(offset+width));
2223 while (i_ptr)
2225 bool remove = false;
2226 store_info_t store_info = i_ptr->store_rec;
2228 /* Skip the clobbers. */
2229 while (!store_info->is_set)
2230 store_info = store_info->next;
2232 /* There are three cases here. */
2233 if (store_info->group_id < 0)
2234 /* We have a cselib store followed by a read from a
2235 const base. */
2236 remove
2237 = canon_true_dependence (store_info->mem,
2238 GET_MODE (store_info->mem),
2239 store_info->mem_addr,
2240 mem, mem_addr);
2242 else if (group_id == store_info->group_id)
2244 /* This is a block mode load. We may get lucky and
2245 canon_true_dependence may save the day. */
2246 if (width == -1)
2247 remove
2248 = canon_true_dependence (store_info->mem,
2249 GET_MODE (store_info->mem),
2250 store_info->mem_addr,
2251 mem, mem_addr);
2253 /* If this read is just reading back something that we just
2254 stored, rewrite the read. */
2255 else
2257 if (store_info->rhs
2258 && offset >= store_info->begin
2259 && offset + width <= store_info->end
2260 && all_positions_needed_p (store_info,
2261 offset - store_info->begin,
2262 width)
2263 && replace_read (store_info, i_ptr, read_info,
2264 insn_info, loc, bb_info->regs_live))
2265 return;
2267 /* The bases are the same, just see if the offsets
2268 overlap. */
2269 if ((offset < store_info->end)
2270 && (offset + width > store_info->begin))
2271 remove = true;
2275 /* else
2276 The else case that is missing here is that the
2277 bases are constant but different. There is nothing
2278 to do here because there is no overlap. */
2280 if (remove)
2282 if (dump_file && (dump_flags & TDF_DETAILS))
2283 dump_insn_info ("removing from active", i_ptr);
2285 active_local_stores_len--;
2286 if (last)
2287 last->next_local_store = i_ptr->next_local_store;
2288 else
2289 active_local_stores = i_ptr->next_local_store;
2291 else
2292 last = i_ptr;
2293 i_ptr = i_ptr->next_local_store;
2296 else
2298 insn_info_t i_ptr = active_local_stores;
2299 insn_info_t last = NULL;
2300 if (dump_file && (dump_flags & TDF_DETAILS))
2302 fprintf (dump_file, " processing cselib load mem:");
2303 print_inline_rtx (dump_file, mem, 0);
2304 fprintf (dump_file, "\n");
2307 while (i_ptr)
2309 bool remove = false;
2310 store_info_t store_info = i_ptr->store_rec;
2312 if (dump_file && (dump_flags & TDF_DETAILS))
2313 fprintf (dump_file, " processing cselib load against insn %d\n",
2314 INSN_UID (i_ptr->insn));
2316 /* Skip the clobbers. */
2317 while (!store_info->is_set)
2318 store_info = store_info->next;
2320 /* If this read is just reading back something that we just
2321 stored, rewrite the read. */
2322 if (store_info->rhs
2323 && store_info->group_id == -1
2324 && store_info->cse_base == base
2325 && width != -1
2326 && offset >= store_info->begin
2327 && offset + width <= store_info->end
2328 && all_positions_needed_p (store_info,
2329 offset - store_info->begin, width)
2330 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2331 bb_info->regs_live))
2332 return;
2334 if (!store_info->alias_set)
2335 remove = canon_true_dependence (store_info->mem,
2336 GET_MODE (store_info->mem),
2337 store_info->mem_addr,
2338 mem, mem_addr);
2340 if (remove)
2342 if (dump_file && (dump_flags & TDF_DETAILS))
2343 dump_insn_info ("removing from active", i_ptr);
2345 active_local_stores_len--;
2346 if (last)
2347 last->next_local_store = i_ptr->next_local_store;
2348 else
2349 active_local_stores = i_ptr->next_local_store;
2351 else
2352 last = i_ptr;
2353 i_ptr = i_ptr->next_local_store;
2358 /* A note_uses callback in which DATA points the INSN_INFO for
2359 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2360 true for any part of *LOC. */
2362 static void
2363 check_mem_read_use (rtx *loc, void *data)
2365 subrtx_ptr_iterator::array_type array;
2366 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2368 rtx *loc = *iter;
2369 if (MEM_P (*loc))
2370 check_mem_read_rtx (loc, (bb_info_t) data);
2375 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2376 So far it only handles arguments passed in registers. */
2378 static bool
2379 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2381 CUMULATIVE_ARGS args_so_far_v;
2382 cumulative_args_t args_so_far;
2383 tree arg;
2384 int idx;
2386 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2387 args_so_far = pack_cumulative_args (&args_so_far_v);
2389 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2390 for (idx = 0;
2391 arg != void_list_node && idx < nargs;
2392 arg = TREE_CHAIN (arg), idx++)
2394 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2395 rtx reg, link, tmp;
2396 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2397 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2398 || GET_MODE_CLASS (mode) != MODE_INT)
2399 return false;
2401 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2402 link;
2403 link = XEXP (link, 1))
2404 if (GET_CODE (XEXP (link, 0)) == USE)
2406 args[idx] = XEXP (XEXP (link, 0), 0);
2407 if (REG_P (args[idx])
2408 && REGNO (args[idx]) == REGNO (reg)
2409 && (GET_MODE (args[idx]) == mode
2410 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2411 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2412 <= UNITS_PER_WORD)
2413 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2414 > GET_MODE_SIZE (mode)))))
2415 break;
2417 if (!link)
2418 return false;
2420 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2421 if (GET_MODE (args[idx]) != mode)
2423 if (!tmp || !CONST_INT_P (tmp))
2424 return false;
2425 tmp = gen_int_mode (INTVAL (tmp), mode);
2427 if (tmp)
2428 args[idx] = tmp;
2430 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2432 if (arg != void_list_node || idx != nargs)
2433 return false;
2434 return true;
2437 /* Return a bitmap of the fixed registers contained in IN. */
2439 static bitmap
2440 copy_fixed_regs (const_bitmap in)
2442 bitmap ret;
2444 ret = ALLOC_REG_SET (NULL);
2445 bitmap_and (ret, in, fixed_reg_set_regset);
2446 return ret;
2449 /* Apply record_store to all candidate stores in INSN. Mark INSN
2450 if some part of it is not a candidate store and assigns to a
2451 non-register target. */
2453 static void
2454 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2456 rtx body;
2457 insn_info_t insn_info = (insn_info_t) pool_alloc (insn_info_pool);
2458 int mems_found = 0;
2459 memset (insn_info, 0, sizeof (struct insn_info));
2461 if (dump_file && (dump_flags & TDF_DETAILS))
2462 fprintf (dump_file, "\n**scanning insn=%d\n",
2463 INSN_UID (insn));
2465 insn_info->prev_insn = bb_info->last_insn;
2466 insn_info->insn = insn;
2467 bb_info->last_insn = insn_info;
2469 if (DEBUG_INSN_P (insn))
2471 insn_info->cannot_delete = true;
2472 return;
2475 /* Look at all of the uses in the insn. */
2476 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2478 if (CALL_P (insn))
2480 bool const_call;
2481 tree memset_call = NULL_TREE;
2483 insn_info->cannot_delete = true;
2485 /* Const functions cannot do anything bad i.e. read memory,
2486 however, they can read their parameters which may have
2487 been pushed onto the stack.
2488 memset and bzero don't read memory either. */
2489 const_call = RTL_CONST_CALL_P (insn);
2490 if (!const_call)
2492 rtx call = get_call_rtx_from (insn);
2493 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
2495 rtx symbol = XEXP (XEXP (call, 0), 0);
2496 if (SYMBOL_REF_DECL (symbol)
2497 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
2499 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
2500 == BUILT_IN_NORMAL
2501 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
2502 == BUILT_IN_MEMSET))
2503 || SYMBOL_REF_DECL (symbol) == block_clear_fn)
2504 memset_call = SYMBOL_REF_DECL (symbol);
2508 if (const_call || memset_call)
2510 insn_info_t i_ptr = active_local_stores;
2511 insn_info_t last = NULL;
2513 if (dump_file && (dump_flags & TDF_DETAILS))
2514 fprintf (dump_file, "%s call %d\n",
2515 const_call ? "const" : "memset", INSN_UID (insn));
2517 /* See the head comment of the frame_read field. */
2518 if (reload_completed)
2519 insn_info->frame_read = true;
2521 /* Loop over the active stores and remove those which are
2522 killed by the const function call. */
2523 while (i_ptr)
2525 bool remove_store = false;
2527 /* The stack pointer based stores are always killed. */
2528 if (i_ptr->stack_pointer_based)
2529 remove_store = true;
2531 /* If the frame is read, the frame related stores are killed. */
2532 else if (insn_info->frame_read)
2534 store_info_t store_info = i_ptr->store_rec;
2536 /* Skip the clobbers. */
2537 while (!store_info->is_set)
2538 store_info = store_info->next;
2540 if (store_info->group_id >= 0
2541 && rtx_group_vec[store_info->group_id]->frame_related)
2542 remove_store = true;
2545 if (remove_store)
2547 if (dump_file && (dump_flags & TDF_DETAILS))
2548 dump_insn_info ("removing from active", i_ptr);
2550 active_local_stores_len--;
2551 if (last)
2552 last->next_local_store = i_ptr->next_local_store;
2553 else
2554 active_local_stores = i_ptr->next_local_store;
2556 else
2557 last = i_ptr;
2559 i_ptr = i_ptr->next_local_store;
2562 if (memset_call)
2564 rtx args[3];
2565 if (get_call_args (insn, memset_call, args, 3)
2566 && CONST_INT_P (args[1])
2567 && CONST_INT_P (args[2])
2568 && INTVAL (args[2]) > 0)
2570 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2571 set_mem_size (mem, INTVAL (args[2]));
2572 body = gen_rtx_SET (VOIDmode, mem, args[1]);
2573 mems_found += record_store (body, bb_info);
2574 if (dump_file && (dump_flags & TDF_DETAILS))
2575 fprintf (dump_file, "handling memset as BLKmode store\n");
2576 if (mems_found == 1)
2578 if (active_local_stores_len++
2579 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2581 active_local_stores_len = 1;
2582 active_local_stores = NULL;
2584 insn_info->fixed_regs_live
2585 = copy_fixed_regs (bb_info->regs_live);
2586 insn_info->next_local_store = active_local_stores;
2587 active_local_stores = insn_info;
2593 else
2594 /* Every other call, including pure functions, may read any memory
2595 that is not relative to the frame. */
2596 add_non_frame_wild_read (bb_info);
2598 return;
2601 /* Assuming that there are sets in these insns, we cannot delete
2602 them. */
2603 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2604 || volatile_refs_p (PATTERN (insn))
2605 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2606 || (RTX_FRAME_RELATED_P (insn))
2607 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2608 insn_info->cannot_delete = true;
2610 body = PATTERN (insn);
2611 if (GET_CODE (body) == PARALLEL)
2613 int i;
2614 for (i = 0; i < XVECLEN (body, 0); i++)
2615 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2617 else
2618 mems_found += record_store (body, bb_info);
2620 if (dump_file && (dump_flags & TDF_DETAILS))
2621 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2622 mems_found, insn_info->cannot_delete ? "true" : "false");
2624 /* If we found some sets of mems, add it into the active_local_stores so
2625 that it can be locally deleted if found dead or used for
2626 replace_read and redundant constant store elimination. Otherwise mark
2627 it as cannot delete. This simplifies the processing later. */
2628 if (mems_found == 1)
2630 if (active_local_stores_len++
2631 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2633 active_local_stores_len = 1;
2634 active_local_stores = NULL;
2636 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2637 insn_info->next_local_store = active_local_stores;
2638 active_local_stores = insn_info;
2640 else
2641 insn_info->cannot_delete = true;
2645 /* Remove BASE from the set of active_local_stores. This is a
2646 callback from cselib that is used to get rid of the stores in
2647 active_local_stores. */
2649 static void
2650 remove_useless_values (cselib_val *base)
2652 insn_info_t insn_info = active_local_stores;
2653 insn_info_t last = NULL;
2655 while (insn_info)
2657 store_info_t store_info = insn_info->store_rec;
2658 bool del = false;
2660 /* If ANY of the store_infos match the cselib group that is
2661 being deleted, then the insn can not be deleted. */
2662 while (store_info)
2664 if ((store_info->group_id == -1)
2665 && (store_info->cse_base == base))
2667 del = true;
2668 break;
2670 store_info = store_info->next;
2673 if (del)
2675 active_local_stores_len--;
2676 if (last)
2677 last->next_local_store = insn_info->next_local_store;
2678 else
2679 active_local_stores = insn_info->next_local_store;
2680 free_store_info (insn_info);
2682 else
2683 last = insn_info;
2685 insn_info = insn_info->next_local_store;
2690 /* Do all of step 1. */
2692 static void
2693 dse_step1 (void)
2695 basic_block bb;
2696 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2698 cselib_init (0);
2699 all_blocks = BITMAP_ALLOC (NULL);
2700 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2701 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2703 FOR_ALL_BB_FN (bb, cfun)
2705 insn_info_t ptr;
2706 bb_info_t bb_info = (bb_info_t) pool_alloc (bb_info_pool);
2708 memset (bb_info, 0, sizeof (struct dse_bb_info));
2709 bitmap_set_bit (all_blocks, bb->index);
2710 bb_info->regs_live = regs_live;
2712 bitmap_copy (regs_live, DF_LR_IN (bb));
2713 df_simulate_initialize_forwards (bb, regs_live);
2715 bb_table[bb->index] = bb_info;
2716 cselib_discard_hook = remove_useless_values;
2718 if (bb->index >= NUM_FIXED_BLOCKS)
2720 rtx_insn *insn;
2722 cse_store_info_pool
2723 = create_alloc_pool ("cse_store_info_pool",
2724 sizeof (struct store_info), 100);
2725 active_local_stores = NULL;
2726 active_local_stores_len = 0;
2727 cselib_clear_table ();
2729 /* Scan the insns. */
2730 FOR_BB_INSNS (bb, insn)
2732 if (INSN_P (insn))
2733 scan_insn (bb_info, insn);
2734 cselib_process_insn (insn);
2735 if (INSN_P (insn))
2736 df_simulate_one_insn_forwards (bb, insn, regs_live);
2739 /* This is something of a hack, because the global algorithm
2740 is supposed to take care of the case where stores go dead
2741 at the end of the function. However, the global
2742 algorithm must take a more conservative view of block
2743 mode reads than the local alg does. So to get the case
2744 where you have a store to the frame followed by a non
2745 overlapping block more read, we look at the active local
2746 stores at the end of the function and delete all of the
2747 frame and spill based ones. */
2748 if (stores_off_frame_dead_at_return
2749 && (EDGE_COUNT (bb->succs) == 0
2750 || (single_succ_p (bb)
2751 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2752 && ! crtl->calls_eh_return)))
2754 insn_info_t i_ptr = active_local_stores;
2755 while (i_ptr)
2757 store_info_t store_info = i_ptr->store_rec;
2759 /* Skip the clobbers. */
2760 while (!store_info->is_set)
2761 store_info = store_info->next;
2762 if (store_info->alias_set && !i_ptr->cannot_delete)
2763 delete_dead_store_insn (i_ptr);
2764 else
2765 if (store_info->group_id >= 0)
2767 group_info_t group
2768 = rtx_group_vec[store_info->group_id];
2769 if (group->frame_related && !i_ptr->cannot_delete)
2770 delete_dead_store_insn (i_ptr);
2773 i_ptr = i_ptr->next_local_store;
2777 /* Get rid of the loads that were discovered in
2778 replace_read. Cselib is finished with this block. */
2779 while (deferred_change_list)
2781 deferred_change_t next = deferred_change_list->next;
2783 /* There is no reason to validate this change. That was
2784 done earlier. */
2785 *deferred_change_list->loc = deferred_change_list->reg;
2786 pool_free (deferred_change_pool, deferred_change_list);
2787 deferred_change_list = next;
2790 /* Get rid of all of the cselib based store_infos in this
2791 block and mark the containing insns as not being
2792 deletable. */
2793 ptr = bb_info->last_insn;
2794 while (ptr)
2796 if (ptr->contains_cselib_groups)
2798 store_info_t s_info = ptr->store_rec;
2799 while (s_info && !s_info->is_set)
2800 s_info = s_info->next;
2801 if (s_info
2802 && s_info->redundant_reason
2803 && s_info->redundant_reason->insn
2804 && !ptr->cannot_delete)
2806 if (dump_file && (dump_flags & TDF_DETAILS))
2807 fprintf (dump_file, "Locally deleting insn %d "
2808 "because insn %d stores the "
2809 "same value and couldn't be "
2810 "eliminated\n",
2811 INSN_UID (ptr->insn),
2812 INSN_UID (s_info->redundant_reason->insn));
2813 delete_dead_store_insn (ptr);
2815 free_store_info (ptr);
2817 else
2819 store_info_t s_info;
2821 /* Free at least positions_needed bitmaps. */
2822 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2823 if (s_info->is_large)
2825 BITMAP_FREE (s_info->positions_needed.large.bmap);
2826 s_info->is_large = false;
2829 ptr = ptr->prev_insn;
2832 free_alloc_pool (cse_store_info_pool);
2834 bb_info->regs_live = NULL;
2837 BITMAP_FREE (regs_live);
2838 cselib_finish ();
2839 rtx_group_table->empty ();
2843 /*----------------------------------------------------------------------------
2844 Second step.
2846 Assign each byte position in the stores that we are going to
2847 analyze globally to a position in the bitmaps. Returns true if
2848 there are any bit positions assigned.
2849 ----------------------------------------------------------------------------*/
2851 static void
2852 dse_step2_init (void)
2854 unsigned int i;
2855 group_info_t group;
2857 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2859 /* For all non stack related bases, we only consider a store to
2860 be deletable if there are two or more stores for that
2861 position. This is because it takes one store to make the
2862 other store redundant. However, for the stores that are
2863 stack related, we consider them if there is only one store
2864 for the position. We do this because the stack related
2865 stores can be deleted if their is no read between them and
2866 the end of the function.
2868 To make this work in the current framework, we take the stack
2869 related bases add all of the bits from store1 into store2.
2870 This has the effect of making the eligible even if there is
2871 only one store. */
2873 if (stores_off_frame_dead_at_return && group->frame_related)
2875 bitmap_ior_into (group->store2_n, group->store1_n);
2876 bitmap_ior_into (group->store2_p, group->store1_p);
2877 if (dump_file && (dump_flags & TDF_DETAILS))
2878 fprintf (dump_file, "group %d is frame related ", i);
2881 group->offset_map_size_n++;
2882 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2883 group->offset_map_size_n);
2884 group->offset_map_size_p++;
2885 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2886 group->offset_map_size_p);
2887 group->process_globally = false;
2888 if (dump_file && (dump_flags & TDF_DETAILS))
2890 fprintf (dump_file, "group %d(%d+%d): ", i,
2891 (int)bitmap_count_bits (group->store2_n),
2892 (int)bitmap_count_bits (group->store2_p));
2893 bitmap_print (dump_file, group->store2_n, "n ", " ");
2894 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2900 /* Init the offset tables for the normal case. */
2902 static bool
2903 dse_step2_nospill (void)
2905 unsigned int i;
2906 group_info_t group;
2907 /* Position 0 is unused because 0 is used in the maps to mean
2908 unused. */
2909 current_position = 1;
2910 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2912 bitmap_iterator bi;
2913 unsigned int j;
2915 if (group == clear_alias_group)
2916 continue;
2918 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2919 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2920 bitmap_clear (group->group_kill);
2922 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2924 bitmap_set_bit (group->group_kill, current_position);
2925 if (bitmap_bit_p (group->escaped_n, j))
2926 bitmap_set_bit (kill_on_calls, current_position);
2927 group->offset_map_n[j] = current_position++;
2928 group->process_globally = true;
2930 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2932 bitmap_set_bit (group->group_kill, current_position);
2933 if (bitmap_bit_p (group->escaped_p, j))
2934 bitmap_set_bit (kill_on_calls, current_position);
2935 group->offset_map_p[j] = current_position++;
2936 group->process_globally = true;
2939 return current_position != 1;
2944 /*----------------------------------------------------------------------------
2945 Third step.
2947 Build the bit vectors for the transfer functions.
2948 ----------------------------------------------------------------------------*/
2951 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2952 there, return 0. */
2954 static int
2955 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
2957 if (offset < 0)
2959 HOST_WIDE_INT offset_p = -offset;
2960 if (offset_p >= group_info->offset_map_size_n)
2961 return 0;
2962 return group_info->offset_map_n[offset_p];
2964 else
2966 if (offset >= group_info->offset_map_size_p)
2967 return 0;
2968 return group_info->offset_map_p[offset];
2973 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2974 may be NULL. */
2976 static void
2977 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
2979 while (store_info)
2981 HOST_WIDE_INT i;
2982 group_info_t group_info
2983 = rtx_group_vec[store_info->group_id];
2984 if (group_info->process_globally)
2985 for (i = store_info->begin; i < store_info->end; i++)
2987 int index = get_bitmap_index (group_info, i);
2988 if (index != 0)
2990 bitmap_set_bit (gen, index);
2991 if (kill)
2992 bitmap_clear_bit (kill, index);
2995 store_info = store_info->next;
3000 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3001 may be NULL. */
3003 static void
3004 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
3006 while (store_info)
3008 if (store_info->alias_set)
3010 int index = get_bitmap_index (clear_alias_group,
3011 store_info->alias_set);
3012 if (index != 0)
3014 bitmap_set_bit (gen, index);
3015 if (kill)
3016 bitmap_clear_bit (kill, index);
3019 store_info = store_info->next;
3024 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3025 may be NULL. */
3027 static void
3028 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
3030 read_info_t read_info = insn_info->read_rec;
3031 int i;
3032 group_info_t group;
3034 /* If this insn reads the frame, kill all the frame related stores. */
3035 if (insn_info->frame_read)
3037 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3038 if (group->process_globally && group->frame_related)
3040 if (kill)
3041 bitmap_ior_into (kill, group->group_kill);
3042 bitmap_and_compl_into (gen, group->group_kill);
3045 if (insn_info->non_frame_wild_read)
3047 /* Kill all non-frame related stores. Kill all stores of variables that
3048 escape. */
3049 if (kill)
3050 bitmap_ior_into (kill, kill_on_calls);
3051 bitmap_and_compl_into (gen, kill_on_calls);
3052 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3053 if (group->process_globally && !group->frame_related)
3055 if (kill)
3056 bitmap_ior_into (kill, group->group_kill);
3057 bitmap_and_compl_into (gen, group->group_kill);
3060 while (read_info)
3062 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3064 if (group->process_globally)
3066 if (i == read_info->group_id)
3068 if (read_info->begin > read_info->end)
3070 /* Begin > end for block mode reads. */
3071 if (kill)
3072 bitmap_ior_into (kill, group->group_kill);
3073 bitmap_and_compl_into (gen, group->group_kill);
3075 else
3077 /* The groups are the same, just process the
3078 offsets. */
3079 HOST_WIDE_INT j;
3080 for (j = read_info->begin; j < read_info->end; j++)
3082 int index = get_bitmap_index (group, j);
3083 if (index != 0)
3085 if (kill)
3086 bitmap_set_bit (kill, index);
3087 bitmap_clear_bit (gen, index);
3092 else
3094 /* The groups are different, if the alias sets
3095 conflict, clear the entire group. We only need
3096 to apply this test if the read_info is a cselib
3097 read. Anything with a constant base cannot alias
3098 something else with a different constant
3099 base. */
3100 if ((read_info->group_id < 0)
3101 && canon_true_dependence (group->base_mem,
3102 GET_MODE (group->base_mem),
3103 group->canon_base_addr,
3104 read_info->mem, NULL_RTX))
3106 if (kill)
3107 bitmap_ior_into (kill, group->group_kill);
3108 bitmap_and_compl_into (gen, group->group_kill);
3114 read_info = read_info->next;
3118 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3119 may be NULL. */
3121 static void
3122 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
3124 while (read_info)
3126 if (read_info->alias_set)
3128 int index = get_bitmap_index (clear_alias_group,
3129 read_info->alias_set);
3130 if (index != 0)
3132 if (kill)
3133 bitmap_set_bit (kill, index);
3134 bitmap_clear_bit (gen, index);
3138 read_info = read_info->next;
3143 /* Return the insn in BB_INFO before the first wild read or if there
3144 are no wild reads in the block, return the last insn. */
3146 static insn_info_t
3147 find_insn_before_first_wild_read (bb_info_t bb_info)
3149 insn_info_t insn_info = bb_info->last_insn;
3150 insn_info_t last_wild_read = NULL;
3152 while (insn_info)
3154 if (insn_info->wild_read)
3156 last_wild_read = insn_info->prev_insn;
3157 /* Block starts with wild read. */
3158 if (!last_wild_read)
3159 return NULL;
3162 insn_info = insn_info->prev_insn;
3165 if (last_wild_read)
3166 return last_wild_read;
3167 else
3168 return bb_info->last_insn;
3172 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3173 the block in order to build the gen and kill sets for the block.
3174 We start at ptr which may be the last insn in the block or may be
3175 the first insn with a wild read. In the latter case we are able to
3176 skip the rest of the block because it just does not matter:
3177 anything that happens is hidden by the wild read. */
3179 static void
3180 dse_step3_scan (bool for_spills, basic_block bb)
3182 bb_info_t bb_info = bb_table[bb->index];
3183 insn_info_t insn_info;
3185 if (for_spills)
3186 /* There are no wild reads in the spill case. */
3187 insn_info = bb_info->last_insn;
3188 else
3189 insn_info = find_insn_before_first_wild_read (bb_info);
3191 /* In the spill case or in the no_spill case if there is no wild
3192 read in the block, we will need a kill set. */
3193 if (insn_info == bb_info->last_insn)
3195 if (bb_info->kill)
3196 bitmap_clear (bb_info->kill);
3197 else
3198 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
3200 else
3201 if (bb_info->kill)
3202 BITMAP_FREE (bb_info->kill);
3204 while (insn_info)
3206 /* There may have been code deleted by the dce pass run before
3207 this phase. */
3208 if (insn_info->insn && INSN_P (insn_info->insn))
3210 /* Process the read(s) last. */
3211 if (for_spills)
3213 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3214 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
3216 else
3218 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3219 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
3223 insn_info = insn_info->prev_insn;
3228 /* Set the gen set of the exit block, and also any block with no
3229 successors that does not have a wild read. */
3231 static void
3232 dse_step3_exit_block_scan (bb_info_t bb_info)
3234 /* The gen set is all 0's for the exit block except for the
3235 frame_pointer_group. */
3237 if (stores_off_frame_dead_at_return)
3239 unsigned int i;
3240 group_info_t group;
3242 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3244 if (group->process_globally && group->frame_related)
3245 bitmap_ior_into (bb_info->gen, group->group_kill);
3251 /* Find all of the blocks that are not backwards reachable from the
3252 exit block or any block with no successors (BB). These are the
3253 infinite loops or infinite self loops. These blocks will still
3254 have their bits set in UNREACHABLE_BLOCKS. */
3256 static void
3257 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3259 edge e;
3260 edge_iterator ei;
3262 if (bitmap_bit_p (unreachable_blocks, bb->index))
3264 bitmap_clear_bit (unreachable_blocks, bb->index);
3265 FOR_EACH_EDGE (e, ei, bb->preds)
3267 mark_reachable_blocks (unreachable_blocks, e->src);
3272 /* Build the transfer functions for the function. */
3274 static void
3275 dse_step3 (bool for_spills)
3277 basic_block bb;
3278 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
3279 sbitmap_iterator sbi;
3280 bitmap all_ones = NULL;
3281 unsigned int i;
3283 bitmap_ones (unreachable_blocks);
3285 FOR_ALL_BB_FN (bb, cfun)
3287 bb_info_t bb_info = bb_table[bb->index];
3288 if (bb_info->gen)
3289 bitmap_clear (bb_info->gen);
3290 else
3291 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3293 if (bb->index == ENTRY_BLOCK)
3295 else if (bb->index == EXIT_BLOCK)
3296 dse_step3_exit_block_scan (bb_info);
3297 else
3298 dse_step3_scan (for_spills, bb);
3299 if (EDGE_COUNT (bb->succs) == 0)
3300 mark_reachable_blocks (unreachable_blocks, bb);
3302 /* If this is the second time dataflow is run, delete the old
3303 sets. */
3304 if (bb_info->in)
3305 BITMAP_FREE (bb_info->in);
3306 if (bb_info->out)
3307 BITMAP_FREE (bb_info->out);
3310 /* For any block in an infinite loop, we must initialize the out set
3311 to all ones. This could be expensive, but almost never occurs in
3312 practice. However, it is common in regression tests. */
3313 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3315 if (bitmap_bit_p (all_blocks, i))
3317 bb_info_t bb_info = bb_table[i];
3318 if (!all_ones)
3320 unsigned int j;
3321 group_info_t group;
3323 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3324 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3325 bitmap_ior_into (all_ones, group->group_kill);
3327 if (!bb_info->out)
3329 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3330 bitmap_copy (bb_info->out, all_ones);
3335 if (all_ones)
3336 BITMAP_FREE (all_ones);
3337 sbitmap_free (unreachable_blocks);
3342 /*----------------------------------------------------------------------------
3343 Fourth step.
3345 Solve the bitvector equations.
3346 ----------------------------------------------------------------------------*/
3349 /* Confluence function for blocks with no successors. Create an out
3350 set from the gen set of the exit block. This block logically has
3351 the exit block as a successor. */
3355 static void
3356 dse_confluence_0 (basic_block bb)
3358 bb_info_t bb_info = bb_table[bb->index];
3360 if (bb->index == EXIT_BLOCK)
3361 return;
3363 if (!bb_info->out)
3365 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3366 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3370 /* Propagate the information from the in set of the dest of E to the
3371 out set of the src of E. If the various in or out sets are not
3372 there, that means they are all ones. */
3374 static bool
3375 dse_confluence_n (edge e)
3377 bb_info_t src_info = bb_table[e->src->index];
3378 bb_info_t dest_info = bb_table[e->dest->index];
3380 if (dest_info->in)
3382 if (src_info->out)
3383 bitmap_and_into (src_info->out, dest_info->in);
3384 else
3386 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3387 bitmap_copy (src_info->out, dest_info->in);
3390 return true;
3394 /* Propagate the info from the out to the in set of BB_INDEX's basic
3395 block. There are three cases:
3397 1) The block has no kill set. In this case the kill set is all
3398 ones. It does not matter what the out set of the block is, none of
3399 the info can reach the top. The only thing that reaches the top is
3400 the gen set and we just copy the set.
3402 2) There is a kill set but no out set and bb has successors. In
3403 this case we just return. Eventually an out set will be created and
3404 it is better to wait than to create a set of ones.
3406 3) There is both a kill and out set. We apply the obvious transfer
3407 function.
3410 static bool
3411 dse_transfer_function (int bb_index)
3413 bb_info_t bb_info = bb_table[bb_index];
3415 if (bb_info->kill)
3417 if (bb_info->out)
3419 /* Case 3 above. */
3420 if (bb_info->in)
3421 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3422 bb_info->out, bb_info->kill);
3423 else
3425 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3426 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3427 bb_info->out, bb_info->kill);
3428 return true;
3431 else
3432 /* Case 2 above. */
3433 return false;
3435 else
3437 /* Case 1 above. If there is already an in set, nothing
3438 happens. */
3439 if (bb_info->in)
3440 return false;
3441 else
3443 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3444 bitmap_copy (bb_info->in, bb_info->gen);
3445 return true;
3450 /* Solve the dataflow equations. */
3452 static void
3453 dse_step4 (void)
3455 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3456 dse_confluence_n, dse_transfer_function,
3457 all_blocks, df_get_postorder (DF_BACKWARD),
3458 df_get_n_blocks (DF_BACKWARD));
3459 if (dump_file && (dump_flags & TDF_DETAILS))
3461 basic_block bb;
3463 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3464 FOR_ALL_BB_FN (bb, cfun)
3466 bb_info_t bb_info = bb_table[bb->index];
3468 df_print_bb_index (bb, dump_file);
3469 if (bb_info->in)
3470 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3471 else
3472 fprintf (dump_file, " in: *MISSING*\n");
3473 if (bb_info->gen)
3474 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3475 else
3476 fprintf (dump_file, " gen: *MISSING*\n");
3477 if (bb_info->kill)
3478 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3479 else
3480 fprintf (dump_file, " kill: *MISSING*\n");
3481 if (bb_info->out)
3482 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3483 else
3484 fprintf (dump_file, " out: *MISSING*\n\n");
3491 /*----------------------------------------------------------------------------
3492 Fifth step.
3494 Delete the stores that can only be deleted using the global information.
3495 ----------------------------------------------------------------------------*/
3498 static void
3499 dse_step5_nospill (void)
3501 basic_block bb;
3502 FOR_EACH_BB_FN (bb, cfun)
3504 bb_info_t bb_info = bb_table[bb->index];
3505 insn_info_t insn_info = bb_info->last_insn;
3506 bitmap v = bb_info->out;
3508 while (insn_info)
3510 bool deleted = false;
3511 if (dump_file && insn_info->insn)
3513 fprintf (dump_file, "starting to process insn %d\n",
3514 INSN_UID (insn_info->insn));
3515 bitmap_print (dump_file, v, " v: ", "\n");
3518 /* There may have been code deleted by the dce pass run before
3519 this phase. */
3520 if (insn_info->insn
3521 && INSN_P (insn_info->insn)
3522 && (!insn_info->cannot_delete)
3523 && (!bitmap_empty_p (v)))
3525 store_info_t store_info = insn_info->store_rec;
3527 /* Try to delete the current insn. */
3528 deleted = true;
3530 /* Skip the clobbers. */
3531 while (!store_info->is_set)
3532 store_info = store_info->next;
3534 if (store_info->alias_set)
3535 deleted = false;
3536 else
3538 HOST_WIDE_INT i;
3539 group_info_t group_info
3540 = rtx_group_vec[store_info->group_id];
3542 for (i = store_info->begin; i < store_info->end; i++)
3544 int index = get_bitmap_index (group_info, i);
3546 if (dump_file && (dump_flags & TDF_DETAILS))
3547 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3548 if (index == 0 || !bitmap_bit_p (v, index))
3550 if (dump_file && (dump_flags & TDF_DETAILS))
3551 fprintf (dump_file, "failing at i = %d\n", (int)i);
3552 deleted = false;
3553 break;
3557 if (deleted)
3559 if (dbg_cnt (dse)
3560 && check_for_inc_dec_1 (insn_info))
3562 delete_insn (insn_info->insn);
3563 insn_info->insn = NULL;
3564 globally_deleted++;
3568 /* We do want to process the local info if the insn was
3569 deleted. For instance, if the insn did a wild read, we
3570 no longer need to trash the info. */
3571 if (insn_info->insn
3572 && INSN_P (insn_info->insn)
3573 && (!deleted))
3575 scan_stores_nospill (insn_info->store_rec, v, NULL);
3576 if (insn_info->wild_read)
3578 if (dump_file && (dump_flags & TDF_DETAILS))
3579 fprintf (dump_file, "wild read\n");
3580 bitmap_clear (v);
3582 else if (insn_info->read_rec
3583 || insn_info->non_frame_wild_read)
3585 if (dump_file && !insn_info->non_frame_wild_read)
3586 fprintf (dump_file, "regular read\n");
3587 else if (dump_file && (dump_flags & TDF_DETAILS))
3588 fprintf (dump_file, "non-frame wild read\n");
3589 scan_reads_nospill (insn_info, v, NULL);
3593 insn_info = insn_info->prev_insn;
3600 /*----------------------------------------------------------------------------
3601 Sixth step.
3603 Delete stores made redundant by earlier stores (which store the same
3604 value) that couldn't be eliminated.
3605 ----------------------------------------------------------------------------*/
3607 static void
3608 dse_step6 (void)
3610 basic_block bb;
3612 FOR_ALL_BB_FN (bb, cfun)
3614 bb_info_t bb_info = bb_table[bb->index];
3615 insn_info_t insn_info = bb_info->last_insn;
3617 while (insn_info)
3619 /* There may have been code deleted by the dce pass run before
3620 this phase. */
3621 if (insn_info->insn
3622 && INSN_P (insn_info->insn)
3623 && !insn_info->cannot_delete)
3625 store_info_t s_info = insn_info->store_rec;
3627 while (s_info && !s_info->is_set)
3628 s_info = s_info->next;
3629 if (s_info
3630 && s_info->redundant_reason
3631 && s_info->redundant_reason->insn
3632 && INSN_P (s_info->redundant_reason->insn))
3634 rtx_insn *rinsn = s_info->redundant_reason->insn;
3635 if (dump_file && (dump_flags & TDF_DETAILS))
3636 fprintf (dump_file, "Locally deleting insn %d "
3637 "because insn %d stores the "
3638 "same value and couldn't be "
3639 "eliminated\n",
3640 INSN_UID (insn_info->insn),
3641 INSN_UID (rinsn));
3642 delete_dead_store_insn (insn_info);
3645 insn_info = insn_info->prev_insn;
3650 /*----------------------------------------------------------------------------
3651 Seventh step.
3653 Destroy everything left standing.
3654 ----------------------------------------------------------------------------*/
3656 static void
3657 dse_step7 (void)
3659 bitmap_obstack_release (&dse_bitmap_obstack);
3660 obstack_free (&dse_obstack, NULL);
3662 end_alias_analysis ();
3663 free (bb_table);
3664 delete rtx_group_table;
3665 rtx_group_table = NULL;
3666 rtx_group_vec.release ();
3667 BITMAP_FREE (all_blocks);
3668 BITMAP_FREE (scratch);
3670 free_alloc_pool (rtx_store_info_pool);
3671 free_alloc_pool (read_info_pool);
3672 free_alloc_pool (insn_info_pool);
3673 free_alloc_pool (bb_info_pool);
3674 free_alloc_pool (rtx_group_info_pool);
3675 free_alloc_pool (deferred_change_pool);
3679 /* -------------------------------------------------------------------------
3681 ------------------------------------------------------------------------- */
3683 /* Callback for running pass_rtl_dse. */
3685 static unsigned int
3686 rest_of_handle_dse (void)
3688 df_set_flags (DF_DEFER_INSN_RESCAN);
3690 /* Need the notes since we must track live hardregs in the forwards
3691 direction. */
3692 df_note_add_problem ();
3693 df_analyze ();
3695 dse_step0 ();
3696 dse_step1 ();
3697 dse_step2_init ();
3698 if (dse_step2_nospill ())
3700 df_set_flags (DF_LR_RUN_DCE);
3701 df_analyze ();
3702 if (dump_file && (dump_flags & TDF_DETAILS))
3703 fprintf (dump_file, "doing global processing\n");
3704 dse_step3 (false);
3705 dse_step4 ();
3706 dse_step5_nospill ();
3709 dse_step6 ();
3710 dse_step7 ();
3712 if (dump_file)
3713 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3714 locally_deleted, globally_deleted, spill_deleted);
3715 return 0;
3718 namespace {
3720 const pass_data pass_data_rtl_dse1 =
3722 RTL_PASS, /* type */
3723 "dse1", /* name */
3724 OPTGROUP_NONE, /* optinfo_flags */
3725 TV_DSE1, /* tv_id */
3726 0, /* properties_required */
3727 0, /* properties_provided */
3728 0, /* properties_destroyed */
3729 0, /* todo_flags_start */
3730 TODO_df_finish, /* todo_flags_finish */
3733 class pass_rtl_dse1 : public rtl_opt_pass
3735 public:
3736 pass_rtl_dse1 (gcc::context *ctxt)
3737 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3740 /* opt_pass methods: */
3741 virtual bool gate (function *)
3743 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3746 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3748 }; // class pass_rtl_dse1
3750 } // anon namespace
3752 rtl_opt_pass *
3753 make_pass_rtl_dse1 (gcc::context *ctxt)
3755 return new pass_rtl_dse1 (ctxt);
3758 namespace {
3760 const pass_data pass_data_rtl_dse2 =
3762 RTL_PASS, /* type */
3763 "dse2", /* name */
3764 OPTGROUP_NONE, /* optinfo_flags */
3765 TV_DSE2, /* tv_id */
3766 0, /* properties_required */
3767 0, /* properties_provided */
3768 0, /* properties_destroyed */
3769 0, /* todo_flags_start */
3770 TODO_df_finish, /* todo_flags_finish */
3773 class pass_rtl_dse2 : public rtl_opt_pass
3775 public:
3776 pass_rtl_dse2 (gcc::context *ctxt)
3777 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3780 /* opt_pass methods: */
3781 virtual bool gate (function *)
3783 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3786 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3788 }; // class pass_rtl_dse2
3790 } // anon namespace
3792 rtl_opt_pass *
3793 make_pass_rtl_dse2 (gcc::context *ctxt)
3795 return new pass_rtl_dse2 (ctxt);