Fix bootstrap/PR63632
[official-gcc.git] / gcc / dse.c
blob4a2f3c7f2c7535213a78ef06e3013d921fb1f9e8
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 "df.h"
39 #include "cselib.h"
40 #include "tree-pass.h"
41 #include "alloc-pool.h"
42 #include "alias.h"
43 #include "insn-config.h"
44 #include "expr.h"
45 #include "recog.h"
46 #include "optabs.h"
47 #include "dbgcnt.h"
48 #include "target.h"
49 #include "params.h"
50 #include "tree-ssa-alias.h"
51 #include "internal-fn.h"
52 #include "gimple-expr.h"
53 #include "is-a.h"
54 #include "gimple.h"
55 #include "gimple-ssa.h"
56 #include "rtl-iter.h"
58 /* This file contains three techniques for performing Dead Store
59 Elimination (dse).
61 * The first technique performs dse locally on any base address. It
62 is based on the cselib which is a local value numbering technique.
63 This technique is local to a basic block but deals with a fairly
64 general addresses.
66 * The second technique performs dse globally but is restricted to
67 base addresses that are either constant or are relative to the
68 frame_pointer.
70 * The third technique, (which is only done after register allocation)
71 processes the spill spill slots. This differs from the second
72 technique because it takes advantage of the fact that spilling is
73 completely free from the effects of aliasing.
75 Logically, dse is a backwards dataflow problem. A store can be
76 deleted if it if cannot be reached in the backward direction by any
77 use of the value being stored. However, the local technique uses a
78 forwards scan of the basic block because cselib requires that the
79 block be processed in that order.
81 The pass is logically broken into 7 steps:
83 0) Initialization.
85 1) The local algorithm, as well as scanning the insns for the two
86 global algorithms.
88 2) Analysis to see if the global algs are necessary. In the case
89 of stores base on a constant address, there must be at least two
90 stores to that address, to make it possible to delete some of the
91 stores. In the case of stores off of the frame or spill related
92 stores, only one store to an address is necessary because those
93 stores die at the end of the function.
95 3) Set up the global dataflow equations based on processing the
96 info parsed in the first step.
98 4) Solve the dataflow equations.
100 5) Delete the insns that the global analysis has indicated are
101 unnecessary.
103 6) Delete insns that store the same value as preceding store
104 where the earlier store couldn't be eliminated.
106 7) Cleanup.
108 This step uses cselib and canon_rtx to build the largest expression
109 possible for each address. This pass is a forwards pass through
110 each basic block. From the point of view of the global technique,
111 the first pass could examine a block in either direction. The
112 forwards ordering is to accommodate cselib.
114 We make a simplifying assumption: addresses fall into four broad
115 categories:
117 1) base has rtx_varies_p == false, offset is constant.
118 2) base has rtx_varies_p == false, offset variable.
119 3) base has rtx_varies_p == true, offset constant.
120 4) base has rtx_varies_p == true, offset variable.
122 The local passes are able to process all 4 kinds of addresses. The
123 global pass only handles 1).
125 The global problem is formulated as follows:
127 A store, S1, to address A, where A is not relative to the stack
128 frame, can be eliminated if all paths from S1 to the end of the
129 function contain another store to A before a read to A.
131 If the address A is relative to the stack frame, a store S2 to A
132 can be eliminated if there are no paths from S2 that reach the
133 end of the function that read A before another store to A. In
134 this case S2 can be deleted if there are paths from S2 to the
135 end of the function that have no reads or writes to A. This
136 second case allows stores to the stack frame to be deleted that
137 would otherwise die when the function returns. This cannot be
138 done if stores_off_frame_dead_at_return is not true. See the doc
139 for that variable for when this variable is false.
141 The global problem is formulated as a backwards set union
142 dataflow problem where the stores are the gens and reads are the
143 kills. Set union problems are rare and require some special
144 handling given our representation of bitmaps. A straightforward
145 implementation requires a lot of bitmaps filled with 1s.
146 These are expensive and cumbersome in our bitmap formulation so
147 care has been taken to avoid large vectors filled with 1s. See
148 the comments in bb_info and in the dataflow confluence functions
149 for details.
151 There are two places for further enhancements to this algorithm:
153 1) The original dse which was embedded in a pass called flow also
154 did local address forwarding. For example in
156 A <- r100
157 ... <- A
159 flow would replace the right hand side of the second insn with a
160 reference to r100. Most of the information is available to add this
161 to this pass. It has not done it because it is a lot of work in
162 the case that either r100 is assigned to between the first and
163 second insn and/or the second insn is a load of part of the value
164 stored by the first insn.
166 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
167 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
168 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
169 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
171 2) The cleaning up of spill code is quite profitable. It currently
172 depends on reading tea leaves and chicken entrails left by reload.
173 This pass depends on reload creating a singleton alias set for each
174 spill slot and telling the next dse pass which of these alias sets
175 are the singletons. Rather than analyze the addresses of the
176 spills, dse's spill processing just does analysis of the loads and
177 stores that use those alias sets. There are three cases where this
178 falls short:
180 a) Reload sometimes creates the slot for one mode of access, and
181 then inserts loads and/or stores for a smaller mode. In this
182 case, the current code just punts on the slot. The proper thing
183 to do is to back out and use one bit vector position for each
184 byte of the entity associated with the slot. This depends on
185 KNOWING that reload always generates the accesses for each of the
186 bytes in some canonical (read that easy to understand several
187 passes after reload happens) way.
189 b) Reload sometimes decides that spill slot it allocated was not
190 large enough for the mode and goes back and allocates more slots
191 with the same mode and alias set. The backout in this case is a
192 little more graceful than (a). In this case the slot is unmarked
193 as being a spill slot and if final address comes out to be based
194 off the frame pointer, the global algorithm handles this slot.
196 c) For any pass that may prespill, there is currently no
197 mechanism to tell the dse pass that the slot being used has the
198 special properties that reload uses. It may be that all that is
199 required is to have those passes make the same calls that reload
200 does, assuming that the alias sets can be manipulated in the same
201 way. */
203 /* There are limits to the size of constant offsets we model for the
204 global problem. There are certainly test cases, that exceed this
205 limit, however, it is unlikely that there are important programs
206 that really have constant offsets this size. */
207 #define MAX_OFFSET (64 * 1024)
209 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
210 on the default obstack because these bitmaps can grow quite large
211 (~2GB for the small (!) test case of PR54146) and we'll hold on to
212 all that memory until the end of the compiler run.
213 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
214 releasing the whole obstack. */
215 static bitmap_obstack dse_bitmap_obstack;
217 /* Obstack for other data. As for above: Kinda nice to be able to
218 throw it all away at the end in one big sweep. */
219 static struct obstack dse_obstack;
221 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
222 static bitmap scratch = NULL;
224 struct insn_info;
226 /* This structure holds information about a candidate store. */
227 struct store_info
230 /* False means this is a clobber. */
231 bool is_set;
233 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
234 bool is_large;
236 /* The id of the mem group of the base address. If rtx_varies_p is
237 true, this is -1. Otherwise, it is the index into the group
238 table. */
239 int group_id;
241 /* This is the cselib value. */
242 cselib_val *cse_base;
244 /* This canonized mem. */
245 rtx mem;
247 /* Canonized MEM address for use by canon_true_dependence. */
248 rtx mem_addr;
250 /* If this is non-zero, it is the alias set of a spill location. */
251 alias_set_type alias_set;
253 /* The offset of the first and byte before the last byte associated
254 with the operation. */
255 HOST_WIDE_INT begin, end;
257 union
259 /* A bitmask as wide as the number of bytes in the word that
260 contains a 1 if the byte may be needed. The store is unused if
261 all of the bits are 0. This is used if IS_LARGE is false. */
262 unsigned HOST_WIDE_INT small_bitmask;
264 struct
266 /* A bitmap with one bit per byte. Cleared bit means the position
267 is needed. Used if IS_LARGE is false. */
268 bitmap bmap;
270 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
271 equal to END - BEGIN, the whole store is unused. */
272 int count;
273 } large;
274 } positions_needed;
276 /* The next store info for this insn. */
277 struct store_info *next;
279 /* The right hand side of the store. This is used if there is a
280 subsequent reload of the mems address somewhere later in the
281 basic block. */
282 rtx rhs;
284 /* If rhs is or holds a constant, this contains that constant,
285 otherwise NULL. */
286 rtx const_rhs;
288 /* Set if this store stores the same constant value as REDUNDANT_REASON
289 insn stored. These aren't eliminated early, because doing that
290 might prevent the earlier larger store to be eliminated. */
291 struct insn_info *redundant_reason;
294 /* Return a bitmask with the first N low bits set. */
296 static unsigned HOST_WIDE_INT
297 lowpart_bitmask (int n)
299 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
300 return mask >> (HOST_BITS_PER_WIDE_INT - n);
303 typedef struct store_info *store_info_t;
304 static alloc_pool cse_store_info_pool;
305 static alloc_pool rtx_store_info_pool;
307 /* This structure holds information about a load. These are only
308 built for rtx bases. */
309 struct read_info
311 /* The id of the mem group of the base address. */
312 int group_id;
314 /* If this is non-zero, it is the alias set of a spill location. */
315 alias_set_type alias_set;
317 /* The offset of the first and byte after the last byte associated
318 with the operation. If begin == end == 0, the read did not have
319 a constant offset. */
320 int begin, end;
322 /* The mem being read. */
323 rtx mem;
325 /* The next read_info for this insn. */
326 struct read_info *next;
328 typedef struct read_info *read_info_t;
329 static alloc_pool read_info_pool;
332 /* One of these records is created for each insn. */
334 struct insn_info
336 /* Set true if the insn contains a store but the insn itself cannot
337 be deleted. This is set if the insn is a parallel and there is
338 more than one non dead output or if the insn is in some way
339 volatile. */
340 bool cannot_delete;
342 /* This field is only used by the global algorithm. It is set true
343 if the insn contains any read of mem except for a (1). This is
344 also set if the insn is a call or has a clobber mem. If the insn
345 contains a wild read, the use_rec will be null. */
346 bool wild_read;
348 /* This is true only for CALL instructions which could potentially read
349 any non-frame memory location. This field is used by the global
350 algorithm. */
351 bool non_frame_wild_read;
353 /* This field is only used for the processing of const functions.
354 These functions cannot read memory, but they can read the stack
355 because that is where they may get their parms. We need to be
356 this conservative because, like the store motion pass, we don't
357 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
358 Moreover, we need to distinguish two cases:
359 1. Before reload (register elimination), the stores related to
360 outgoing arguments are stack pointer based and thus deemed
361 of non-constant base in this pass. This requires special
362 handling but also means that the frame pointer based stores
363 need not be killed upon encountering a const function call.
364 2. After reload, the stores related to outgoing arguments can be
365 either stack pointer or hard frame pointer based. This means
366 that we have no other choice than also killing all the frame
367 pointer based stores upon encountering a const function call.
368 This field is set after reload for const function calls. Having
369 this set is less severe than a wild read, it just means that all
370 the frame related stores are killed rather than all the stores. */
371 bool frame_read;
373 /* This field is only used for the processing of const functions.
374 It is set if the insn may contain a stack pointer based store. */
375 bool stack_pointer_based;
377 /* This is true if any of the sets within the store contains a
378 cselib base. Such stores can only be deleted by the local
379 algorithm. */
380 bool contains_cselib_groups;
382 /* The insn. */
383 rtx_insn *insn;
385 /* The list of mem sets or mem clobbers that are contained in this
386 insn. If the insn is deletable, it contains only one mem set.
387 But it could also contain clobbers. Insns that contain more than
388 one mem set are not deletable, but each of those mems are here in
389 order to provide info to delete other insns. */
390 store_info_t store_rec;
392 /* The linked list of mem uses in this insn. Only the reads from
393 rtx bases are listed here. The reads to cselib bases are
394 completely processed during the first scan and so are never
395 created. */
396 read_info_t read_rec;
398 /* The live fixed registers. We assume only fixed registers can
399 cause trouble by being clobbered from an expanded pattern;
400 storing only the live fixed registers (rather than all registers)
401 means less memory needs to be allocated / copied for the individual
402 stores. */
403 regset fixed_regs_live;
405 /* The prev insn in the basic block. */
406 struct insn_info * prev_insn;
408 /* The linked list of insns that are in consideration for removal in
409 the forwards pass through the basic block. This pointer may be
410 trash as it is not cleared when a wild read occurs. The only
411 time it is guaranteed to be correct is when the traversal starts
412 at active_local_stores. */
413 struct insn_info * next_local_store;
416 typedef struct insn_info *insn_info_t;
417 static alloc_pool insn_info_pool;
419 /* The linked list of stores that are under consideration in this
420 basic block. */
421 static insn_info_t active_local_stores;
422 static int active_local_stores_len;
424 struct dse_bb_info
427 /* Pointer to the insn info for the last insn in the block. These
428 are linked so this is how all of the insns are reached. During
429 scanning this is the current insn being scanned. */
430 insn_info_t last_insn;
432 /* The info for the global dataflow problem. */
435 /* This is set if the transfer function should and in the wild_read
436 bitmap before applying the kill and gen sets. That vector knocks
437 out most of the bits in the bitmap and thus speeds up the
438 operations. */
439 bool apply_wild_read;
441 /* The following 4 bitvectors hold information about which positions
442 of which stores are live or dead. They are indexed by
443 get_bitmap_index. */
445 /* The set of store positions that exist in this block before a wild read. */
446 bitmap gen;
448 /* The set of load positions that exist in this block above the
449 same position of a store. */
450 bitmap kill;
452 /* The set of stores that reach the top of the block without being
453 killed by a read.
455 Do not represent the in if it is all ones. Note that this is
456 what the bitvector should logically be initialized to for a set
457 intersection problem. However, like the kill set, this is too
458 expensive. So initially, the in set will only be created for the
459 exit block and any block that contains a wild read. */
460 bitmap in;
462 /* The set of stores that reach the bottom of the block from it's
463 successors.
465 Do not represent the in if it is all ones. Note that this is
466 what the bitvector should logically be initialized to for a set
467 intersection problem. However, like the kill and in set, this is
468 too expensive. So what is done is that the confluence operator
469 just initializes the vector from one of the out sets of the
470 successors of the block. */
471 bitmap out;
473 /* The following bitvector is indexed by the reg number. It
474 contains the set of regs that are live at the current instruction
475 being processed. While it contains info for all of the
476 registers, only the hard registers are actually examined. It is used
477 to assure that shift and/or add sequences that are inserted do not
478 accidentally clobber live hard regs. */
479 bitmap regs_live;
482 typedef struct dse_bb_info *bb_info_t;
483 static alloc_pool bb_info_pool;
485 /* Table to hold all bb_infos. */
486 static bb_info_t *bb_table;
488 /* There is a group_info for each rtx base that is used to reference
489 memory. There are also not many of the rtx bases because they are
490 very limited in scope. */
492 struct group_info
494 /* The actual base of the address. */
495 rtx rtx_base;
497 /* The sequential id of the base. This allows us to have a
498 canonical ordering of these that is not based on addresses. */
499 int id;
501 /* True if there are any positions that are to be processed
502 globally. */
503 bool process_globally;
505 /* True if the base of this group is either the frame_pointer or
506 hard_frame_pointer. */
507 bool frame_related;
509 /* A mem wrapped around the base pointer for the group in order to do
510 read dependency. It must be given BLKmode in order to encompass all
511 the possible offsets from the base. */
512 rtx base_mem;
514 /* Canonized version of base_mem's address. */
515 rtx canon_base_addr;
517 /* These two sets of two bitmaps are used to keep track of how many
518 stores are actually referencing that position from this base. We
519 only do this for rtx bases as this will be used to assign
520 positions in the bitmaps for the global problem. Bit N is set in
521 store1 on the first store for offset N. Bit N is set in store2
522 for the second store to offset N. This is all we need since we
523 only care about offsets that have two or more stores for them.
525 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
526 for 0 and greater offsets.
528 There is one special case here, for stores into the stack frame,
529 we will or store1 into store2 before deciding which stores look
530 at globally. This is because stores to the stack frame that have
531 no other reads before the end of the function can also be
532 deleted. */
533 bitmap store1_n, store1_p, store2_n, store2_p;
535 /* These bitmaps keep track of offsets in this group escape this function.
536 An offset escapes if it corresponds to a named variable whose
537 addressable flag is set. */
538 bitmap escaped_n, escaped_p;
540 /* The positions in this bitmap have the same assignments as the in,
541 out, gen and kill bitmaps. This bitmap is all zeros except for
542 the positions that are occupied by stores for this group. */
543 bitmap group_kill;
545 /* The offset_map is used to map the offsets from this base into
546 positions in the global bitmaps. It is only created after all of
547 the all of stores have been scanned and we know which ones we
548 care about. */
549 int *offset_map_n, *offset_map_p;
550 int offset_map_size_n, offset_map_size_p;
552 typedef struct group_info *group_info_t;
553 typedef const struct group_info *const_group_info_t;
554 static alloc_pool rtx_group_info_pool;
556 /* Index into the rtx_group_vec. */
557 static int rtx_group_next_id;
560 static vec<group_info_t> rtx_group_vec;
563 /* This structure holds the set of changes that are being deferred
564 when removing read operation. See replace_read. */
565 struct deferred_change
568 /* The mem that is being replaced. */
569 rtx *loc;
571 /* The reg it is being replaced with. */
572 rtx reg;
574 struct deferred_change *next;
577 typedef struct deferred_change *deferred_change_t;
578 static alloc_pool deferred_change_pool;
580 static deferred_change_t deferred_change_list = NULL;
582 /* The group that holds all of the clear_alias_sets. */
583 static group_info_t clear_alias_group;
585 /* The modes of the clear_alias_sets. */
586 static htab_t clear_alias_mode_table;
588 /* Hash table element to look up the mode for an alias set. */
589 struct clear_alias_mode_holder
591 alias_set_type alias_set;
592 enum machine_mode mode;
595 /* This is true except if cfun->stdarg -- i.e. we cannot do
596 this for vararg functions because they play games with the frame. */
597 static bool stores_off_frame_dead_at_return;
599 /* Counter for stats. */
600 static int globally_deleted;
601 static int locally_deleted;
602 static int spill_deleted;
604 static bitmap all_blocks;
606 /* Locations that are killed by calls in the global phase. */
607 static bitmap kill_on_calls;
609 /* The number of bits used in the global bitmaps. */
610 static unsigned int current_position;
612 /*----------------------------------------------------------------------------
613 Zeroth step.
615 Initialization.
616 ----------------------------------------------------------------------------*/
619 /* Find the entry associated with ALIAS_SET. */
621 static struct clear_alias_mode_holder *
622 clear_alias_set_lookup (alias_set_type alias_set)
624 struct clear_alias_mode_holder tmp_holder;
625 void **slot;
627 tmp_holder.alias_set = alias_set;
628 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
629 gcc_assert (*slot);
631 return (struct clear_alias_mode_holder *) *slot;
635 /* Hashtable callbacks for maintaining the "bases" field of
636 store_group_info, given that the addresses are function invariants. */
638 struct invariant_group_base_hasher : typed_noop_remove <group_info>
640 typedef group_info value_type;
641 typedef group_info compare_type;
642 static inline hashval_t hash (const value_type *);
643 static inline bool equal (const value_type *, const compare_type *);
646 inline bool
647 invariant_group_base_hasher::equal (const value_type *gi1,
648 const compare_type *gi2)
650 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
653 inline hashval_t
654 invariant_group_base_hasher::hash (const value_type *gi)
656 int do_not_record;
657 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
660 /* Tables of group_info structures, hashed by base value. */
661 static hash_table<invariant_group_base_hasher> *rtx_group_table;
664 /* Get the GROUP for BASE. Add a new group if it is not there. */
666 static group_info_t
667 get_group_info (rtx base)
669 struct group_info tmp_gi;
670 group_info_t gi;
671 group_info **slot;
673 if (base)
675 /* Find the store_base_info structure for BASE, creating a new one
676 if necessary. */
677 tmp_gi.rtx_base = base;
678 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
679 gi = (group_info_t) *slot;
681 else
683 if (!clear_alias_group)
685 clear_alias_group = gi =
686 (group_info_t) pool_alloc (rtx_group_info_pool);
687 memset (gi, 0, sizeof (struct group_info));
688 gi->id = rtx_group_next_id++;
689 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
690 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
691 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
692 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
693 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
694 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
695 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
696 gi->process_globally = false;
697 gi->offset_map_size_n = 0;
698 gi->offset_map_size_p = 0;
699 gi->offset_map_n = NULL;
700 gi->offset_map_p = NULL;
701 rtx_group_vec.safe_push (gi);
703 return clear_alias_group;
706 if (gi == NULL)
708 *slot = gi = (group_info_t) pool_alloc (rtx_group_info_pool);
709 gi->rtx_base = base;
710 gi->id = rtx_group_next_id++;
711 gi->base_mem = gen_rtx_MEM (BLKmode, base);
712 gi->canon_base_addr = canon_rtx (base);
713 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
714 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
715 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
716 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
717 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
718 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
719 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
720 gi->process_globally = false;
721 gi->frame_related =
722 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
723 gi->offset_map_size_n = 0;
724 gi->offset_map_size_p = 0;
725 gi->offset_map_n = NULL;
726 gi->offset_map_p = NULL;
727 rtx_group_vec.safe_push (gi);
730 return gi;
734 /* Initialization of data structures. */
736 static void
737 dse_step0 (void)
739 locally_deleted = 0;
740 globally_deleted = 0;
741 spill_deleted = 0;
743 bitmap_obstack_initialize (&dse_bitmap_obstack);
744 gcc_obstack_init (&dse_obstack);
746 scratch = BITMAP_ALLOC (&reg_obstack);
747 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
749 rtx_store_info_pool
750 = create_alloc_pool ("rtx_store_info_pool",
751 sizeof (struct store_info), 100);
752 read_info_pool
753 = create_alloc_pool ("read_info_pool",
754 sizeof (struct read_info), 100);
755 insn_info_pool
756 = create_alloc_pool ("insn_info_pool",
757 sizeof (struct insn_info), 100);
758 bb_info_pool
759 = create_alloc_pool ("bb_info_pool",
760 sizeof (struct dse_bb_info), 100);
761 rtx_group_info_pool
762 = create_alloc_pool ("rtx_group_info_pool",
763 sizeof (struct group_info), 100);
764 deferred_change_pool
765 = create_alloc_pool ("deferred_change_pool",
766 sizeof (struct deferred_change), 10);
768 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
770 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
771 rtx_group_next_id = 0;
773 stores_off_frame_dead_at_return = !cfun->stdarg;
775 init_alias_analysis ();
777 clear_alias_group = NULL;
782 /*----------------------------------------------------------------------------
783 First step.
785 Scan all of the insns. Any random ordering of the blocks is fine.
786 Each block is scanned in forward order to accommodate cselib which
787 is used to remove stores with non-constant bases.
788 ----------------------------------------------------------------------------*/
790 /* Delete all of the store_info recs from INSN_INFO. */
792 static void
793 free_store_info (insn_info_t insn_info)
795 store_info_t store_info = insn_info->store_rec;
796 while (store_info)
798 store_info_t next = store_info->next;
799 if (store_info->is_large)
800 BITMAP_FREE (store_info->positions_needed.large.bmap);
801 if (store_info->cse_base)
802 pool_free (cse_store_info_pool, store_info);
803 else
804 pool_free (rtx_store_info_pool, store_info);
805 store_info = next;
808 insn_info->cannot_delete = true;
809 insn_info->contains_cselib_groups = false;
810 insn_info->store_rec = NULL;
813 typedef struct
815 rtx_insn *first, *current;
816 regset fixed_regs_live;
817 bool failure;
818 } note_add_store_info;
820 /* Callback for emit_inc_dec_insn_before via note_stores.
821 Check if a register is clobbered which is live afterwards. */
823 static void
824 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
826 rtx_insn *insn;
827 note_add_store_info *info = (note_add_store_info *) data;
828 int r, n;
830 if (!REG_P (loc))
831 return;
833 /* If this register is referenced by the current or an earlier insn,
834 that's OK. E.g. this applies to the register that is being incremented
835 with this addition. */
836 for (insn = info->first;
837 insn != NEXT_INSN (info->current);
838 insn = NEXT_INSN (insn))
839 if (reg_referenced_p (loc, PATTERN (insn)))
840 return;
842 /* If we come here, we have a clobber of a register that's only OK
843 if that register is not live. If we don't have liveness information
844 available, fail now. */
845 if (!info->fixed_regs_live)
847 info->failure = true;
848 return;
850 /* Now check if this is a live fixed register. */
851 r = REGNO (loc);
852 n = hard_regno_nregs[r][GET_MODE (loc)];
853 while (--n >= 0)
854 if (REGNO_REG_SET_P (info->fixed_regs_live, r+n))
855 info->failure = true;
858 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
859 SRC + SRCOFF before insn ARG. */
861 static int
862 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
863 rtx op ATTRIBUTE_UNUSED,
864 rtx dest, rtx src, rtx srcoff, void *arg)
866 insn_info_t insn_info = (insn_info_t) arg;
867 rtx_insn *insn = insn_info->insn, *new_insn, *cur;
868 note_add_store_info info;
870 /* We can reuse all operands without copying, because we are about
871 to delete the insn that contained it. */
872 if (srcoff)
874 start_sequence ();
875 emit_insn (gen_add3_insn (dest, src, srcoff));
876 new_insn = get_insns ();
877 end_sequence ();
879 else
880 new_insn = as_a <rtx_insn *> (gen_move_insn (dest, src));
881 info.first = new_insn;
882 info.fixed_regs_live = insn_info->fixed_regs_live;
883 info.failure = false;
884 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
886 info.current = cur;
887 note_stores (PATTERN (cur), note_add_store, &info);
890 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
891 return it immediately, communicating the failure to its caller. */
892 if (info.failure)
893 return 1;
895 emit_insn_before (new_insn, insn);
897 return 0;
900 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
901 is there, is split into a separate insn.
902 Return true on success (or if there was nothing to do), false on failure. */
904 static bool
905 check_for_inc_dec_1 (insn_info_t insn_info)
907 rtx_insn *insn = insn_info->insn;
908 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
909 if (note)
910 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
911 insn_info) == 0;
912 return true;
916 /* Entry point for postreload. If you work on reload_cse, or you need this
917 anywhere else, consider if you can provide register liveness information
918 and add a parameter to this function so that it can be passed down in
919 insn_info.fixed_regs_live. */
920 bool
921 check_for_inc_dec (rtx_insn *insn)
923 struct insn_info insn_info;
924 rtx note;
926 insn_info.insn = insn;
927 insn_info.fixed_regs_live = NULL;
928 note = find_reg_note (insn, REG_INC, NULL_RTX);
929 if (note)
930 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
931 &insn_info) == 0;
932 return true;
935 /* Delete the insn and free all of the fields inside INSN_INFO. */
937 static void
938 delete_dead_store_insn (insn_info_t insn_info)
940 read_info_t read_info;
942 if (!dbg_cnt (dse))
943 return;
945 if (!check_for_inc_dec_1 (insn_info))
946 return;
947 if (dump_file && (dump_flags & TDF_DETAILS))
949 fprintf (dump_file, "Locally deleting insn %d ",
950 INSN_UID (insn_info->insn));
951 if (insn_info->store_rec->alias_set)
952 fprintf (dump_file, "alias set %d\n",
953 (int) insn_info->store_rec->alias_set);
954 else
955 fprintf (dump_file, "\n");
958 free_store_info (insn_info);
959 read_info = insn_info->read_rec;
961 while (read_info)
963 read_info_t next = read_info->next;
964 pool_free (read_info_pool, read_info);
965 read_info = next;
967 insn_info->read_rec = NULL;
969 delete_insn (insn_info->insn);
970 locally_deleted++;
971 insn_info->insn = NULL;
973 insn_info->wild_read = false;
976 /* Return whether DECL, a local variable, can possibly escape the current
977 function scope. */
979 static bool
980 local_variable_can_escape (tree decl)
982 if (TREE_ADDRESSABLE (decl))
983 return true;
985 /* If this is a partitioned variable, we need to consider all the variables
986 in the partition. This is necessary because a store into one of them can
987 be replaced with a store into another and this may not change the outcome
988 of the escape analysis. */
989 if (cfun->gimple_df->decls_to_pointers != NULL)
991 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
992 if (namep)
993 return TREE_ADDRESSABLE (*namep);
996 return false;
999 /* Return whether EXPR can possibly escape the current function scope. */
1001 static bool
1002 can_escape (tree expr)
1004 tree base;
1005 if (!expr)
1006 return true;
1007 base = get_base_address (expr);
1008 if (DECL_P (base)
1009 && !may_be_aliased (base)
1010 && !(TREE_CODE (base) == VAR_DECL
1011 && !DECL_EXTERNAL (base)
1012 && !TREE_STATIC (base)
1013 && local_variable_can_escape (base)))
1014 return false;
1015 return true;
1018 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1019 OFFSET and WIDTH. */
1021 static void
1022 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
1023 tree expr)
1025 HOST_WIDE_INT i;
1026 bool expr_escapes = can_escape (expr);
1027 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
1028 for (i=offset; i<offset+width; i++)
1030 bitmap store1;
1031 bitmap store2;
1032 bitmap escaped;
1033 int ai;
1034 if (i < 0)
1036 store1 = group->store1_n;
1037 store2 = group->store2_n;
1038 escaped = group->escaped_n;
1039 ai = -i;
1041 else
1043 store1 = group->store1_p;
1044 store2 = group->store2_p;
1045 escaped = group->escaped_p;
1046 ai = i;
1049 if (!bitmap_set_bit (store1, ai))
1050 bitmap_set_bit (store2, ai);
1051 else
1053 if (i < 0)
1055 if (group->offset_map_size_n < ai)
1056 group->offset_map_size_n = ai;
1058 else
1060 if (group->offset_map_size_p < ai)
1061 group->offset_map_size_p = ai;
1064 if (expr_escapes)
1065 bitmap_set_bit (escaped, ai);
1069 static void
1070 reset_active_stores (void)
1072 active_local_stores = NULL;
1073 active_local_stores_len = 0;
1076 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1078 static void
1079 free_read_records (bb_info_t bb_info)
1081 insn_info_t insn_info = bb_info->last_insn;
1082 read_info_t *ptr = &insn_info->read_rec;
1083 while (*ptr)
1085 read_info_t next = (*ptr)->next;
1086 if ((*ptr)->alias_set == 0)
1088 pool_free (read_info_pool, *ptr);
1089 *ptr = next;
1091 else
1092 ptr = &(*ptr)->next;
1096 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1098 static void
1099 add_wild_read (bb_info_t bb_info)
1101 insn_info_t insn_info = bb_info->last_insn;
1102 insn_info->wild_read = true;
1103 free_read_records (bb_info);
1104 reset_active_stores ();
1107 /* Set the BB_INFO so that the last insn is marked as a wild read of
1108 non-frame locations. */
1110 static void
1111 add_non_frame_wild_read (bb_info_t bb_info)
1113 insn_info_t insn_info = bb_info->last_insn;
1114 insn_info->non_frame_wild_read = true;
1115 free_read_records (bb_info);
1116 reset_active_stores ();
1119 /* Return true if X is a constant or one of the registers that behave
1120 as a constant over the life of a function. This is equivalent to
1121 !rtx_varies_p for memory addresses. */
1123 static bool
1124 const_or_frame_p (rtx x)
1126 if (CONSTANT_P (x))
1127 return true;
1129 if (GET_CODE (x) == REG)
1131 /* Note that we have to test for the actual rtx used for the frame
1132 and arg pointers and not just the register number in case we have
1133 eliminated the frame and/or arg pointer and are using it
1134 for pseudos. */
1135 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1136 /* The arg pointer varies if it is not a fixed register. */
1137 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1138 || x == pic_offset_table_rtx)
1139 return true;
1140 return false;
1143 return false;
1146 /* Take all reasonable action to put the address of MEM into the form
1147 that we can do analysis on.
1149 The gold standard is to get the address into the form: address +
1150 OFFSET where address is something that rtx_varies_p considers a
1151 constant. When we can get the address in this form, we can do
1152 global analysis on it. Note that for constant bases, address is
1153 not actually returned, only the group_id. The address can be
1154 obtained from that.
1156 If that fails, we try cselib to get a value we can at least use
1157 locally. If that fails we return false.
1159 The GROUP_ID is set to -1 for cselib bases and the index of the
1160 group for non_varying bases.
1162 FOR_READ is true if this is a mem read and false if not. */
1164 static bool
1165 canon_address (rtx mem,
1166 alias_set_type *alias_set_out,
1167 int *group_id,
1168 HOST_WIDE_INT *offset,
1169 cselib_val **base)
1171 enum machine_mode address_mode = get_address_mode (mem);
1172 rtx mem_address = XEXP (mem, 0);
1173 rtx expanded_address, address;
1174 int expanded;
1176 *alias_set_out = 0;
1178 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1180 if (dump_file && (dump_flags & TDF_DETAILS))
1182 fprintf (dump_file, " mem: ");
1183 print_inline_rtx (dump_file, mem_address, 0);
1184 fprintf (dump_file, "\n");
1187 /* First see if just canon_rtx (mem_address) is const or frame,
1188 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1189 address = NULL_RTX;
1190 for (expanded = 0; expanded < 2; expanded++)
1192 if (expanded)
1194 /* Use cselib to replace all of the reg references with the full
1195 expression. This will take care of the case where we have
1197 r_x = base + offset;
1198 val = *r_x;
1200 by making it into
1202 val = *(base + offset); */
1204 expanded_address = cselib_expand_value_rtx (mem_address,
1205 scratch, 5);
1207 /* If this fails, just go with the address from first
1208 iteration. */
1209 if (!expanded_address)
1210 break;
1212 else
1213 expanded_address = mem_address;
1215 /* Split the address into canonical BASE + OFFSET terms. */
1216 address = canon_rtx (expanded_address);
1218 *offset = 0;
1220 if (dump_file && (dump_flags & TDF_DETAILS))
1222 if (expanded)
1224 fprintf (dump_file, "\n after cselib_expand address: ");
1225 print_inline_rtx (dump_file, expanded_address, 0);
1226 fprintf (dump_file, "\n");
1229 fprintf (dump_file, "\n after canon_rtx address: ");
1230 print_inline_rtx (dump_file, address, 0);
1231 fprintf (dump_file, "\n");
1234 if (GET_CODE (address) == CONST)
1235 address = XEXP (address, 0);
1237 if (GET_CODE (address) == PLUS
1238 && CONST_INT_P (XEXP (address, 1)))
1240 *offset = INTVAL (XEXP (address, 1));
1241 address = XEXP (address, 0);
1244 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1245 && const_or_frame_p (address))
1247 group_info_t group = get_group_info (address);
1249 if (dump_file && (dump_flags & TDF_DETAILS))
1250 fprintf (dump_file, " gid=%d offset=%d \n",
1251 group->id, (int)*offset);
1252 *base = NULL;
1253 *group_id = group->id;
1254 return true;
1258 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1259 *group_id = -1;
1261 if (*base == NULL)
1263 if (dump_file && (dump_flags & TDF_DETAILS))
1264 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1265 return false;
1267 if (dump_file && (dump_flags & TDF_DETAILS))
1268 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1269 (*base)->uid, (*base)->hash, (int)*offset);
1270 return true;
1274 /* Clear the rhs field from the active_local_stores array. */
1276 static void
1277 clear_rhs_from_active_local_stores (void)
1279 insn_info_t ptr = active_local_stores;
1281 while (ptr)
1283 store_info_t store_info = ptr->store_rec;
1284 /* Skip the clobbers. */
1285 while (!store_info->is_set)
1286 store_info = store_info->next;
1288 store_info->rhs = NULL;
1289 store_info->const_rhs = NULL;
1291 ptr = ptr->next_local_store;
1296 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1298 static inline void
1299 set_position_unneeded (store_info_t s_info, int pos)
1301 if (__builtin_expect (s_info->is_large, false))
1303 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1304 s_info->positions_needed.large.count++;
1306 else
1307 s_info->positions_needed.small_bitmask
1308 &= ~(((unsigned HOST_WIDE_INT) 1) << pos);
1311 /* Mark the whole store S_INFO as unneeded. */
1313 static inline void
1314 set_all_positions_unneeded (store_info_t s_info)
1316 if (__builtin_expect (s_info->is_large, false))
1318 int pos, end = s_info->end - s_info->begin;
1319 for (pos = 0; pos < end; pos++)
1320 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1321 s_info->positions_needed.large.count = end;
1323 else
1324 s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0;
1327 /* Return TRUE if any bytes from S_INFO store are needed. */
1329 static inline bool
1330 any_positions_needed_p (store_info_t s_info)
1332 if (__builtin_expect (s_info->is_large, false))
1333 return (s_info->positions_needed.large.count
1334 < s_info->end - s_info->begin);
1335 else
1336 return (s_info->positions_needed.small_bitmask
1337 != (unsigned HOST_WIDE_INT) 0);
1340 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1341 store are needed. */
1343 static inline bool
1344 all_positions_needed_p (store_info_t s_info, int start, int width)
1346 if (__builtin_expect (s_info->is_large, false))
1348 int end = start + width;
1349 while (start < end)
1350 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1351 return false;
1352 return true;
1354 else
1356 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1357 return (s_info->positions_needed.small_bitmask & mask) == mask;
1362 static rtx get_stored_val (store_info_t, enum machine_mode, HOST_WIDE_INT,
1363 HOST_WIDE_INT, basic_block, bool);
1366 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1367 there is a candidate store, after adding it to the appropriate
1368 local store group if so. */
1370 static int
1371 record_store (rtx body, bb_info_t bb_info)
1373 rtx mem, rhs, const_rhs, mem_addr;
1374 HOST_WIDE_INT offset = 0;
1375 HOST_WIDE_INT width = 0;
1376 alias_set_type spill_alias_set;
1377 insn_info_t insn_info = bb_info->last_insn;
1378 store_info_t store_info = NULL;
1379 int group_id;
1380 cselib_val *base = NULL;
1381 insn_info_t ptr, last, redundant_reason;
1382 bool store_is_unused;
1384 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1385 return 0;
1387 mem = SET_DEST (body);
1389 /* If this is not used, then this cannot be used to keep the insn
1390 from being deleted. On the other hand, it does provide something
1391 that can be used to prove that another store is dead. */
1392 store_is_unused
1393 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1395 /* Check whether that value is a suitable memory location. */
1396 if (!MEM_P (mem))
1398 /* If the set or clobber is unused, then it does not effect our
1399 ability to get rid of the entire insn. */
1400 if (!store_is_unused)
1401 insn_info->cannot_delete = true;
1402 return 0;
1405 /* At this point we know mem is a mem. */
1406 if (GET_MODE (mem) == BLKmode)
1408 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1410 if (dump_file && (dump_flags & TDF_DETAILS))
1411 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1412 add_wild_read (bb_info);
1413 insn_info->cannot_delete = true;
1414 return 0;
1416 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1417 as memset (addr, 0, 36); */
1418 else if (!MEM_SIZE_KNOWN_P (mem)
1419 || MEM_SIZE (mem) <= 0
1420 || MEM_SIZE (mem) > MAX_OFFSET
1421 || GET_CODE (body) != SET
1422 || !CONST_INT_P (SET_SRC (body)))
1424 if (!store_is_unused)
1426 /* If the set or clobber is unused, then it does not effect our
1427 ability to get rid of the entire insn. */
1428 insn_info->cannot_delete = true;
1429 clear_rhs_from_active_local_stores ();
1431 return 0;
1435 /* We can still process a volatile mem, we just cannot delete it. */
1436 if (MEM_VOLATILE_P (mem))
1437 insn_info->cannot_delete = true;
1439 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1441 clear_rhs_from_active_local_stores ();
1442 return 0;
1445 if (GET_MODE (mem) == BLKmode)
1446 width = MEM_SIZE (mem);
1447 else
1448 width = GET_MODE_SIZE (GET_MODE (mem));
1450 if (spill_alias_set)
1452 bitmap store1 = clear_alias_group->store1_p;
1453 bitmap store2 = clear_alias_group->store2_p;
1455 gcc_assert (GET_MODE (mem) != BLKmode);
1457 if (!bitmap_set_bit (store1, spill_alias_set))
1458 bitmap_set_bit (store2, spill_alias_set);
1460 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1461 clear_alias_group->offset_map_size_p = spill_alias_set;
1463 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1465 if (dump_file && (dump_flags & TDF_DETAILS))
1466 fprintf (dump_file, " processing spill store %d(%s)\n",
1467 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1469 else if (group_id >= 0)
1471 /* In the restrictive case where the base is a constant or the
1472 frame pointer we can do global analysis. */
1474 group_info_t group
1475 = rtx_group_vec[group_id];
1476 tree expr = MEM_EXPR (mem);
1478 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1479 set_usage_bits (group, offset, width, expr);
1481 if (dump_file && (dump_flags & TDF_DETAILS))
1482 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1483 group_id, (int)offset, (int)(offset+width));
1485 else
1487 if (may_be_sp_based_p (XEXP (mem, 0)))
1488 insn_info->stack_pointer_based = true;
1489 insn_info->contains_cselib_groups = true;
1491 store_info = (store_info_t) pool_alloc (cse_store_info_pool);
1492 group_id = -1;
1494 if (dump_file && (dump_flags & TDF_DETAILS))
1495 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1496 (int)offset, (int)(offset+width));
1499 const_rhs = rhs = NULL_RTX;
1500 if (GET_CODE (body) == SET
1501 /* No place to keep the value after ra. */
1502 && !reload_completed
1503 && (REG_P (SET_SRC (body))
1504 || GET_CODE (SET_SRC (body)) == SUBREG
1505 || CONSTANT_P (SET_SRC (body)))
1506 && !MEM_VOLATILE_P (mem)
1507 /* Sometimes the store and reload is used for truncation and
1508 rounding. */
1509 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1511 rhs = SET_SRC (body);
1512 if (CONSTANT_P (rhs))
1513 const_rhs = rhs;
1514 else if (body == PATTERN (insn_info->insn))
1516 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1517 if (tem && CONSTANT_P (XEXP (tem, 0)))
1518 const_rhs = XEXP (tem, 0);
1520 if (const_rhs == NULL_RTX && REG_P (rhs))
1522 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1524 if (tem && CONSTANT_P (tem))
1525 const_rhs = tem;
1529 /* Check to see if this stores causes some other stores to be
1530 dead. */
1531 ptr = active_local_stores;
1532 last = NULL;
1533 redundant_reason = NULL;
1534 mem = canon_rtx (mem);
1535 /* For alias_set != 0 canon_true_dependence should be never called. */
1536 if (spill_alias_set)
1537 mem_addr = NULL_RTX;
1538 else
1540 if (group_id < 0)
1541 mem_addr = base->val_rtx;
1542 else
1544 group_info_t group
1545 = rtx_group_vec[group_id];
1546 mem_addr = group->canon_base_addr;
1548 if (offset)
1549 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1552 while (ptr)
1554 insn_info_t next = ptr->next_local_store;
1555 store_info_t s_info = ptr->store_rec;
1556 bool del = true;
1558 /* Skip the clobbers. We delete the active insn if this insn
1559 shadows the set. To have been put on the active list, it
1560 has exactly on set. */
1561 while (!s_info->is_set)
1562 s_info = s_info->next;
1564 if (s_info->alias_set != spill_alias_set)
1565 del = false;
1566 else if (s_info->alias_set)
1568 struct clear_alias_mode_holder *entry
1569 = clear_alias_set_lookup (s_info->alias_set);
1570 /* Generally, spills cannot be processed if and of the
1571 references to the slot have a different mode. But if
1572 we are in the same block and mode is exactly the same
1573 between this store and one before in the same block,
1574 we can still delete it. */
1575 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1576 && (GET_MODE (mem) == entry->mode))
1578 del = true;
1579 set_all_positions_unneeded (s_info);
1581 if (dump_file && (dump_flags & TDF_DETAILS))
1582 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1583 INSN_UID (ptr->insn), (int) s_info->alias_set);
1585 else if ((s_info->group_id == group_id)
1586 && (s_info->cse_base == base))
1588 HOST_WIDE_INT i;
1589 if (dump_file && (dump_flags & TDF_DETAILS))
1590 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1591 INSN_UID (ptr->insn), s_info->group_id,
1592 (int)s_info->begin, (int)s_info->end);
1594 /* Even if PTR won't be eliminated as unneeded, if both
1595 PTR and this insn store the same constant value, we might
1596 eliminate this insn instead. */
1597 if (s_info->const_rhs
1598 && const_rhs
1599 && offset >= s_info->begin
1600 && offset + width <= s_info->end
1601 && all_positions_needed_p (s_info, offset - s_info->begin,
1602 width))
1604 if (GET_MODE (mem) == BLKmode)
1606 if (GET_MODE (s_info->mem) == BLKmode
1607 && s_info->const_rhs == const_rhs)
1608 redundant_reason = ptr;
1610 else if (s_info->const_rhs == const0_rtx
1611 && const_rhs == const0_rtx)
1612 redundant_reason = ptr;
1613 else
1615 rtx val;
1616 start_sequence ();
1617 val = get_stored_val (s_info, GET_MODE (mem),
1618 offset, offset + width,
1619 BLOCK_FOR_INSN (insn_info->insn),
1620 true);
1621 if (get_insns () != NULL)
1622 val = NULL_RTX;
1623 end_sequence ();
1624 if (val && rtx_equal_p (val, const_rhs))
1625 redundant_reason = ptr;
1629 for (i = MAX (offset, s_info->begin);
1630 i < offset + width && i < s_info->end;
1631 i++)
1632 set_position_unneeded (s_info, i - s_info->begin);
1634 else if (s_info->rhs)
1635 /* Need to see if it is possible for this store to overwrite
1636 the value of store_info. If it is, set the rhs to NULL to
1637 keep it from being used to remove a load. */
1639 if (canon_true_dependence (s_info->mem,
1640 GET_MODE (s_info->mem),
1641 s_info->mem_addr,
1642 mem, mem_addr))
1644 s_info->rhs = NULL;
1645 s_info->const_rhs = NULL;
1649 /* An insn can be deleted if every position of every one of
1650 its s_infos is zero. */
1651 if (any_positions_needed_p (s_info))
1652 del = false;
1654 if (del)
1656 insn_info_t insn_to_delete = ptr;
1658 active_local_stores_len--;
1659 if (last)
1660 last->next_local_store = ptr->next_local_store;
1661 else
1662 active_local_stores = ptr->next_local_store;
1664 if (!insn_to_delete->cannot_delete)
1665 delete_dead_store_insn (insn_to_delete);
1667 else
1668 last = ptr;
1670 ptr = next;
1673 /* Finish filling in the store_info. */
1674 store_info->next = insn_info->store_rec;
1675 insn_info->store_rec = store_info;
1676 store_info->mem = mem;
1677 store_info->alias_set = spill_alias_set;
1678 store_info->mem_addr = mem_addr;
1679 store_info->cse_base = base;
1680 if (width > HOST_BITS_PER_WIDE_INT)
1682 store_info->is_large = true;
1683 store_info->positions_needed.large.count = 0;
1684 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1686 else
1688 store_info->is_large = false;
1689 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1691 store_info->group_id = group_id;
1692 store_info->begin = offset;
1693 store_info->end = offset + width;
1694 store_info->is_set = GET_CODE (body) == SET;
1695 store_info->rhs = rhs;
1696 store_info->const_rhs = const_rhs;
1697 store_info->redundant_reason = redundant_reason;
1699 /* If this is a clobber, we return 0. We will only be able to
1700 delete this insn if there is only one store USED store, but we
1701 can use the clobber to delete other stores earlier. */
1702 return store_info->is_set ? 1 : 0;
1706 static void
1707 dump_insn_info (const char * start, insn_info_t insn_info)
1709 fprintf (dump_file, "%s insn=%d %s\n", start,
1710 INSN_UID (insn_info->insn),
1711 insn_info->store_rec ? "has store" : "naked");
1715 /* If the modes are different and the value's source and target do not
1716 line up, we need to extract the value from lower part of the rhs of
1717 the store, shift it, and then put it into a form that can be shoved
1718 into the read_insn. This function generates a right SHIFT of a
1719 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1720 shift sequence is returned or NULL if we failed to find a
1721 shift. */
1723 static rtx
1724 find_shift_sequence (int access_size,
1725 store_info_t store_info,
1726 enum machine_mode read_mode,
1727 int shift, bool speed, bool require_cst)
1729 enum machine_mode store_mode = GET_MODE (store_info->mem);
1730 enum machine_mode new_mode;
1731 rtx read_reg = NULL;
1733 /* Some machines like the x86 have shift insns for each size of
1734 operand. Other machines like the ppc or the ia-64 may only have
1735 shift insns that shift values within 32 or 64 bit registers.
1736 This loop tries to find the smallest shift insn that will right
1737 justify the value we want to read but is available in one insn on
1738 the machine. */
1740 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1741 MODE_INT);
1742 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1743 new_mode = GET_MODE_WIDER_MODE (new_mode))
1745 rtx target, new_reg, new_lhs;
1746 rtx_insn *shift_seq, *insn;
1747 int cost;
1749 /* If a constant was stored into memory, try to simplify it here,
1750 otherwise the cost of the shift might preclude this optimization
1751 e.g. at -Os, even when no actual shift will be needed. */
1752 if (store_info->const_rhs)
1754 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1755 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1756 store_mode, byte);
1757 if (ret && CONSTANT_P (ret))
1759 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1760 ret, GEN_INT (shift));
1761 if (ret && CONSTANT_P (ret))
1763 byte = subreg_lowpart_offset (read_mode, new_mode);
1764 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1765 if (ret && CONSTANT_P (ret)
1766 && set_src_cost (ret, speed) <= COSTS_N_INSNS (1))
1767 return ret;
1772 if (require_cst)
1773 return NULL_RTX;
1775 /* Try a wider mode if truncating the store mode to NEW_MODE
1776 requires a real instruction. */
1777 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1778 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1779 continue;
1781 /* Also try a wider mode if the necessary punning is either not
1782 desirable or not possible. */
1783 if (!CONSTANT_P (store_info->rhs)
1784 && !MODES_TIEABLE_P (new_mode, store_mode))
1785 continue;
1787 new_reg = gen_reg_rtx (new_mode);
1789 start_sequence ();
1791 /* In theory we could also check for an ashr. Ian Taylor knows
1792 of one dsp where the cost of these two was not the same. But
1793 this really is a rare case anyway. */
1794 target = expand_binop (new_mode, lshr_optab, new_reg,
1795 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1797 shift_seq = get_insns ();
1798 end_sequence ();
1800 if (target != new_reg || shift_seq == NULL)
1801 continue;
1803 cost = 0;
1804 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1805 if (INSN_P (insn))
1806 cost += insn_rtx_cost (PATTERN (insn), speed);
1808 /* The computation up to here is essentially independent
1809 of the arguments and could be precomputed. It may
1810 not be worth doing so. We could precompute if
1811 worthwhile or at least cache the results. The result
1812 technically depends on both SHIFT and ACCESS_SIZE,
1813 but in practice the answer will depend only on ACCESS_SIZE. */
1815 if (cost > COSTS_N_INSNS (1))
1816 continue;
1818 new_lhs = extract_low_bits (new_mode, store_mode,
1819 copy_rtx (store_info->rhs));
1820 if (new_lhs == NULL_RTX)
1821 continue;
1823 /* We found an acceptable shift. Generate a move to
1824 take the value from the store and put it into the
1825 shift pseudo, then shift it, then generate another
1826 move to put in into the target of the read. */
1827 emit_move_insn (new_reg, new_lhs);
1828 emit_insn (shift_seq);
1829 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1830 break;
1833 return read_reg;
1837 /* Call back for note_stores to find the hard regs set or clobbered by
1838 insn. Data is a bitmap of the hardregs set so far. */
1840 static void
1841 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1843 bitmap regs_set = (bitmap) data;
1845 if (REG_P (x)
1846 && HARD_REGISTER_P (x))
1848 unsigned int regno = REGNO (x);
1849 bitmap_set_range (regs_set, regno,
1850 hard_regno_nregs[regno][GET_MODE (x)]);
1854 /* Helper function for replace_read and record_store.
1855 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1856 to one before READ_END bytes read in READ_MODE. Return NULL
1857 if not successful. If REQUIRE_CST is true, return always constant. */
1859 static rtx
1860 get_stored_val (store_info_t store_info, enum machine_mode read_mode,
1861 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1862 basic_block bb, bool require_cst)
1864 enum machine_mode store_mode = GET_MODE (store_info->mem);
1865 int shift;
1866 int access_size; /* In bytes. */
1867 rtx read_reg;
1869 /* To get here the read is within the boundaries of the write so
1870 shift will never be negative. Start out with the shift being in
1871 bytes. */
1872 if (store_mode == BLKmode)
1873 shift = 0;
1874 else if (BYTES_BIG_ENDIAN)
1875 shift = store_info->end - read_end;
1876 else
1877 shift = read_begin - store_info->begin;
1879 access_size = shift + GET_MODE_SIZE (read_mode);
1881 /* From now on it is bits. */
1882 shift *= BITS_PER_UNIT;
1884 if (shift)
1885 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1886 optimize_bb_for_speed_p (bb),
1887 require_cst);
1888 else if (store_mode == BLKmode)
1890 /* The store is a memset (addr, const_val, const_size). */
1891 gcc_assert (CONST_INT_P (store_info->rhs));
1892 store_mode = int_mode_for_mode (read_mode);
1893 if (store_mode == BLKmode)
1894 read_reg = NULL_RTX;
1895 else if (store_info->rhs == const0_rtx)
1896 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1897 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1898 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1899 read_reg = NULL_RTX;
1900 else
1902 unsigned HOST_WIDE_INT c
1903 = INTVAL (store_info->rhs)
1904 & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
1905 int shift = BITS_PER_UNIT;
1906 while (shift < HOST_BITS_PER_WIDE_INT)
1908 c |= (c << shift);
1909 shift <<= 1;
1911 read_reg = gen_int_mode (c, store_mode);
1912 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1915 else if (store_info->const_rhs
1916 && (require_cst
1917 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1918 read_reg = extract_low_bits (read_mode, store_mode,
1919 copy_rtx (store_info->const_rhs));
1920 else
1921 read_reg = extract_low_bits (read_mode, store_mode,
1922 copy_rtx (store_info->rhs));
1923 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1924 read_reg = NULL_RTX;
1925 return read_reg;
1928 /* Take a sequence of:
1929 A <- r1
1931 ... <- A
1933 and change it into
1934 r2 <- r1
1935 A <- r1
1937 ... <- r2
1941 r3 <- extract (r1)
1942 r3 <- r3 >> shift
1943 r2 <- extract (r3)
1944 ... <- r2
1948 r2 <- extract (r1)
1949 ... <- r2
1951 Depending on the alignment and the mode of the store and
1952 subsequent load.
1955 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1956 and READ_INSN are for the read. Return true if the replacement
1957 went ok. */
1959 static bool
1960 replace_read (store_info_t store_info, insn_info_t store_insn,
1961 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1962 bitmap regs_live)
1964 enum machine_mode store_mode = GET_MODE (store_info->mem);
1965 enum machine_mode read_mode = GET_MODE (read_info->mem);
1966 rtx_insn *insns, *this_insn;
1967 rtx read_reg;
1968 basic_block bb;
1970 if (!dbg_cnt (dse))
1971 return false;
1973 /* Create a sequence of instructions to set up the read register.
1974 This sequence goes immediately before the store and its result
1975 is read by the load.
1977 We need to keep this in perspective. We are replacing a read
1978 with a sequence of insns, but the read will almost certainly be
1979 in cache, so it is not going to be an expensive one. Thus, we
1980 are not willing to do a multi insn shift or worse a subroutine
1981 call to get rid of the read. */
1982 if (dump_file && (dump_flags & TDF_DETAILS))
1983 fprintf (dump_file, "trying to replace %smode load in insn %d"
1984 " from %smode store in insn %d\n",
1985 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1986 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1987 start_sequence ();
1988 bb = BLOCK_FOR_INSN (read_insn->insn);
1989 read_reg = get_stored_val (store_info,
1990 read_mode, read_info->begin, read_info->end,
1991 bb, false);
1992 if (read_reg == NULL_RTX)
1994 end_sequence ();
1995 if (dump_file && (dump_flags & TDF_DETAILS))
1996 fprintf (dump_file, " -- could not extract bits of stored value\n");
1997 return false;
1999 /* Force the value into a new register so that it won't be clobbered
2000 between the store and the load. */
2001 read_reg = copy_to_mode_reg (read_mode, read_reg);
2002 insns = get_insns ();
2003 end_sequence ();
2005 if (insns != NULL_RTX)
2007 /* Now we have to scan the set of new instructions to see if the
2008 sequence contains and sets of hardregs that happened to be
2009 live at this point. For instance, this can happen if one of
2010 the insns sets the CC and the CC happened to be live at that
2011 point. This does occasionally happen, see PR 37922. */
2012 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
2014 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
2015 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
2017 bitmap_and_into (regs_set, regs_live);
2018 if (!bitmap_empty_p (regs_set))
2020 if (dump_file && (dump_flags & TDF_DETAILS))
2022 fprintf (dump_file,
2023 "abandoning replacement because sequence clobbers live hardregs:");
2024 df_print_regset (dump_file, regs_set);
2027 BITMAP_FREE (regs_set);
2028 return false;
2030 BITMAP_FREE (regs_set);
2033 if (validate_change (read_insn->insn, loc, read_reg, 0))
2035 deferred_change_t deferred_change =
2036 (deferred_change_t) pool_alloc (deferred_change_pool);
2038 /* Insert this right before the store insn where it will be safe
2039 from later insns that might change it before the read. */
2040 emit_insn_before (insns, store_insn->insn);
2042 /* And now for the kludge part: cselib croaks if you just
2043 return at this point. There are two reasons for this:
2045 1) Cselib has an idea of how many pseudos there are and
2046 that does not include the new ones we just added.
2048 2) Cselib does not know about the move insn we added
2049 above the store_info, and there is no way to tell it
2050 about it, because it has "moved on".
2052 Problem (1) is fixable with a certain amount of engineering.
2053 Problem (2) is requires starting the bb from scratch. This
2054 could be expensive.
2056 So we are just going to have to lie. The move/extraction
2057 insns are not really an issue, cselib did not see them. But
2058 the use of the new pseudo read_insn is a real problem because
2059 cselib has not scanned this insn. The way that we solve this
2060 problem is that we are just going to put the mem back for now
2061 and when we are finished with the block, we undo this. We
2062 keep a table of mems to get rid of. At the end of the basic
2063 block we can put them back. */
2065 *loc = read_info->mem;
2066 deferred_change->next = deferred_change_list;
2067 deferred_change_list = deferred_change;
2068 deferred_change->loc = loc;
2069 deferred_change->reg = read_reg;
2071 /* Get rid of the read_info, from the point of view of the
2072 rest of dse, play like this read never happened. */
2073 read_insn->read_rec = read_info->next;
2074 pool_free (read_info_pool, read_info);
2075 if (dump_file && (dump_flags & TDF_DETAILS))
2077 fprintf (dump_file, " -- replaced the loaded MEM with ");
2078 print_simple_rtl (dump_file, read_reg);
2079 fprintf (dump_file, "\n");
2081 return true;
2083 else
2085 if (dump_file && (dump_flags & TDF_DETAILS))
2087 fprintf (dump_file, " -- replacing the loaded MEM with ");
2088 print_simple_rtl (dump_file, read_reg);
2089 fprintf (dump_file, " led to an invalid instruction\n");
2091 return false;
2095 /* Check the address of MEM *LOC and kill any appropriate stores that may
2096 be active. */
2098 static void
2099 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
2101 rtx mem = *loc, mem_addr;
2102 insn_info_t insn_info;
2103 HOST_WIDE_INT offset = 0;
2104 HOST_WIDE_INT width = 0;
2105 alias_set_type spill_alias_set = 0;
2106 cselib_val *base = NULL;
2107 int group_id;
2108 read_info_t read_info;
2110 insn_info = bb_info->last_insn;
2112 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2113 || (MEM_VOLATILE_P (mem)))
2115 if (dump_file && (dump_flags & TDF_DETAILS))
2116 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
2117 add_wild_read (bb_info);
2118 insn_info->cannot_delete = true;
2119 return;
2122 /* If it is reading readonly mem, then there can be no conflict with
2123 another write. */
2124 if (MEM_READONLY_P (mem))
2125 return;
2127 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
2129 if (dump_file && (dump_flags & TDF_DETAILS))
2130 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2131 add_wild_read (bb_info);
2132 return;
2135 if (GET_MODE (mem) == BLKmode)
2136 width = -1;
2137 else
2138 width = GET_MODE_SIZE (GET_MODE (mem));
2140 read_info = (read_info_t) pool_alloc (read_info_pool);
2141 read_info->group_id = group_id;
2142 read_info->mem = mem;
2143 read_info->alias_set = spill_alias_set;
2144 read_info->begin = offset;
2145 read_info->end = offset + width;
2146 read_info->next = insn_info->read_rec;
2147 insn_info->read_rec = read_info;
2148 /* For alias_set != 0 canon_true_dependence should be never called. */
2149 if (spill_alias_set)
2150 mem_addr = NULL_RTX;
2151 else
2153 if (group_id < 0)
2154 mem_addr = base->val_rtx;
2155 else
2157 group_info_t group
2158 = rtx_group_vec[group_id];
2159 mem_addr = group->canon_base_addr;
2161 if (offset)
2162 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2165 /* We ignore the clobbers in store_info. The is mildly aggressive,
2166 but there really should not be a clobber followed by a read. */
2168 if (spill_alias_set)
2170 insn_info_t i_ptr = active_local_stores;
2171 insn_info_t last = NULL;
2173 if (dump_file && (dump_flags & TDF_DETAILS))
2174 fprintf (dump_file, " processing spill load %d\n",
2175 (int) spill_alias_set);
2177 while (i_ptr)
2179 store_info_t store_info = i_ptr->store_rec;
2181 /* Skip the clobbers. */
2182 while (!store_info->is_set)
2183 store_info = store_info->next;
2185 if (store_info->alias_set == spill_alias_set)
2187 if (dump_file && (dump_flags & TDF_DETAILS))
2188 dump_insn_info ("removing from active", i_ptr);
2190 active_local_stores_len--;
2191 if (last)
2192 last->next_local_store = i_ptr->next_local_store;
2193 else
2194 active_local_stores = i_ptr->next_local_store;
2196 else
2197 last = i_ptr;
2198 i_ptr = i_ptr->next_local_store;
2201 else if (group_id >= 0)
2203 /* This is the restricted case where the base is a constant or
2204 the frame pointer and offset is a constant. */
2205 insn_info_t i_ptr = active_local_stores;
2206 insn_info_t last = NULL;
2208 if (dump_file && (dump_flags & TDF_DETAILS))
2210 if (width == -1)
2211 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2212 group_id);
2213 else
2214 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2215 group_id, (int)offset, (int)(offset+width));
2218 while (i_ptr)
2220 bool remove = false;
2221 store_info_t store_info = i_ptr->store_rec;
2223 /* Skip the clobbers. */
2224 while (!store_info->is_set)
2225 store_info = store_info->next;
2227 /* There are three cases here. */
2228 if (store_info->group_id < 0)
2229 /* We have a cselib store followed by a read from a
2230 const base. */
2231 remove
2232 = canon_true_dependence (store_info->mem,
2233 GET_MODE (store_info->mem),
2234 store_info->mem_addr,
2235 mem, mem_addr);
2237 else if (group_id == store_info->group_id)
2239 /* This is a block mode load. We may get lucky and
2240 canon_true_dependence may save the day. */
2241 if (width == -1)
2242 remove
2243 = canon_true_dependence (store_info->mem,
2244 GET_MODE (store_info->mem),
2245 store_info->mem_addr,
2246 mem, mem_addr);
2248 /* If this read is just reading back something that we just
2249 stored, rewrite the read. */
2250 else
2252 if (store_info->rhs
2253 && offset >= store_info->begin
2254 && offset + width <= store_info->end
2255 && all_positions_needed_p (store_info,
2256 offset - store_info->begin,
2257 width)
2258 && replace_read (store_info, i_ptr, read_info,
2259 insn_info, loc, bb_info->regs_live))
2260 return;
2262 /* The bases are the same, just see if the offsets
2263 overlap. */
2264 if ((offset < store_info->end)
2265 && (offset + width > store_info->begin))
2266 remove = true;
2270 /* else
2271 The else case that is missing here is that the
2272 bases are constant but different. There is nothing
2273 to do here because there is no overlap. */
2275 if (remove)
2277 if (dump_file && (dump_flags & TDF_DETAILS))
2278 dump_insn_info ("removing from active", i_ptr);
2280 active_local_stores_len--;
2281 if (last)
2282 last->next_local_store = i_ptr->next_local_store;
2283 else
2284 active_local_stores = i_ptr->next_local_store;
2286 else
2287 last = i_ptr;
2288 i_ptr = i_ptr->next_local_store;
2291 else
2293 insn_info_t i_ptr = active_local_stores;
2294 insn_info_t last = NULL;
2295 if (dump_file && (dump_flags & TDF_DETAILS))
2297 fprintf (dump_file, " processing cselib load mem:");
2298 print_inline_rtx (dump_file, mem, 0);
2299 fprintf (dump_file, "\n");
2302 while (i_ptr)
2304 bool remove = false;
2305 store_info_t store_info = i_ptr->store_rec;
2307 if (dump_file && (dump_flags & TDF_DETAILS))
2308 fprintf (dump_file, " processing cselib load against insn %d\n",
2309 INSN_UID (i_ptr->insn));
2311 /* Skip the clobbers. */
2312 while (!store_info->is_set)
2313 store_info = store_info->next;
2315 /* If this read is just reading back something that we just
2316 stored, rewrite the read. */
2317 if (store_info->rhs
2318 && store_info->group_id == -1
2319 && store_info->cse_base == base
2320 && width != -1
2321 && offset >= store_info->begin
2322 && offset + width <= store_info->end
2323 && all_positions_needed_p (store_info,
2324 offset - store_info->begin, width)
2325 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2326 bb_info->regs_live))
2327 return;
2329 if (!store_info->alias_set)
2330 remove = canon_true_dependence (store_info->mem,
2331 GET_MODE (store_info->mem),
2332 store_info->mem_addr,
2333 mem, mem_addr);
2335 if (remove)
2337 if (dump_file && (dump_flags & TDF_DETAILS))
2338 dump_insn_info ("removing from active", i_ptr);
2340 active_local_stores_len--;
2341 if (last)
2342 last->next_local_store = i_ptr->next_local_store;
2343 else
2344 active_local_stores = i_ptr->next_local_store;
2346 else
2347 last = i_ptr;
2348 i_ptr = i_ptr->next_local_store;
2353 /* A note_uses callback in which DATA points the INSN_INFO for
2354 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2355 true for any part of *LOC. */
2357 static void
2358 check_mem_read_use (rtx *loc, void *data)
2360 subrtx_ptr_iterator::array_type array;
2361 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2363 rtx *loc = *iter;
2364 if (MEM_P (*loc))
2365 check_mem_read_rtx (loc, (bb_info_t) data);
2370 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2371 So far it only handles arguments passed in registers. */
2373 static bool
2374 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2376 CUMULATIVE_ARGS args_so_far_v;
2377 cumulative_args_t args_so_far;
2378 tree arg;
2379 int idx;
2381 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2382 args_so_far = pack_cumulative_args (&args_so_far_v);
2384 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2385 for (idx = 0;
2386 arg != void_list_node && idx < nargs;
2387 arg = TREE_CHAIN (arg), idx++)
2389 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2390 rtx reg, link, tmp;
2391 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2392 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2393 || GET_MODE_CLASS (mode) != MODE_INT)
2394 return false;
2396 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2397 link;
2398 link = XEXP (link, 1))
2399 if (GET_CODE (XEXP (link, 0)) == USE)
2401 args[idx] = XEXP (XEXP (link, 0), 0);
2402 if (REG_P (args[idx])
2403 && REGNO (args[idx]) == REGNO (reg)
2404 && (GET_MODE (args[idx]) == mode
2405 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2406 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2407 <= UNITS_PER_WORD)
2408 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2409 > GET_MODE_SIZE (mode)))))
2410 break;
2412 if (!link)
2413 return false;
2415 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2416 if (GET_MODE (args[idx]) != mode)
2418 if (!tmp || !CONST_INT_P (tmp))
2419 return false;
2420 tmp = gen_int_mode (INTVAL (tmp), mode);
2422 if (tmp)
2423 args[idx] = tmp;
2425 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2427 if (arg != void_list_node || idx != nargs)
2428 return false;
2429 return true;
2432 /* Return a bitmap of the fixed registers contained in IN. */
2434 static bitmap
2435 copy_fixed_regs (const_bitmap in)
2437 bitmap ret;
2439 ret = ALLOC_REG_SET (NULL);
2440 bitmap_and (ret, in, fixed_reg_set_regset);
2441 return ret;
2444 /* Apply record_store to all candidate stores in INSN. Mark INSN
2445 if some part of it is not a candidate store and assigns to a
2446 non-register target. */
2448 static void
2449 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2451 rtx body;
2452 insn_info_t insn_info = (insn_info_t) pool_alloc (insn_info_pool);
2453 int mems_found = 0;
2454 memset (insn_info, 0, sizeof (struct insn_info));
2456 if (dump_file && (dump_flags & TDF_DETAILS))
2457 fprintf (dump_file, "\n**scanning insn=%d\n",
2458 INSN_UID (insn));
2460 insn_info->prev_insn = bb_info->last_insn;
2461 insn_info->insn = insn;
2462 bb_info->last_insn = insn_info;
2464 if (DEBUG_INSN_P (insn))
2466 insn_info->cannot_delete = true;
2467 return;
2470 /* Look at all of the uses in the insn. */
2471 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2473 if (CALL_P (insn))
2475 bool const_call;
2476 tree memset_call = NULL_TREE;
2478 insn_info->cannot_delete = true;
2480 /* Const functions cannot do anything bad i.e. read memory,
2481 however, they can read their parameters which may have
2482 been pushed onto the stack.
2483 memset and bzero don't read memory either. */
2484 const_call = RTL_CONST_CALL_P (insn);
2485 if (!const_call)
2487 rtx call = get_call_rtx_from (insn);
2488 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
2490 rtx symbol = XEXP (XEXP (call, 0), 0);
2491 if (SYMBOL_REF_DECL (symbol)
2492 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
2494 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
2495 == BUILT_IN_NORMAL
2496 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
2497 == BUILT_IN_MEMSET))
2498 || SYMBOL_REF_DECL (symbol) == block_clear_fn)
2499 memset_call = SYMBOL_REF_DECL (symbol);
2503 if (const_call || memset_call)
2505 insn_info_t i_ptr = active_local_stores;
2506 insn_info_t last = NULL;
2508 if (dump_file && (dump_flags & TDF_DETAILS))
2509 fprintf (dump_file, "%s call %d\n",
2510 const_call ? "const" : "memset", INSN_UID (insn));
2512 /* See the head comment of the frame_read field. */
2513 if (reload_completed)
2514 insn_info->frame_read = true;
2516 /* Loop over the active stores and remove those which are
2517 killed by the const function call. */
2518 while (i_ptr)
2520 bool remove_store = false;
2522 /* The stack pointer based stores are always killed. */
2523 if (i_ptr->stack_pointer_based)
2524 remove_store = true;
2526 /* If the frame is read, the frame related stores are killed. */
2527 else if (insn_info->frame_read)
2529 store_info_t store_info = i_ptr->store_rec;
2531 /* Skip the clobbers. */
2532 while (!store_info->is_set)
2533 store_info = store_info->next;
2535 if (store_info->group_id >= 0
2536 && rtx_group_vec[store_info->group_id]->frame_related)
2537 remove_store = true;
2540 if (remove_store)
2542 if (dump_file && (dump_flags & TDF_DETAILS))
2543 dump_insn_info ("removing from active", i_ptr);
2545 active_local_stores_len--;
2546 if (last)
2547 last->next_local_store = i_ptr->next_local_store;
2548 else
2549 active_local_stores = i_ptr->next_local_store;
2551 else
2552 last = i_ptr;
2554 i_ptr = i_ptr->next_local_store;
2557 if (memset_call)
2559 rtx args[3];
2560 if (get_call_args (insn, memset_call, args, 3)
2561 && CONST_INT_P (args[1])
2562 && CONST_INT_P (args[2])
2563 && INTVAL (args[2]) > 0)
2565 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2566 set_mem_size (mem, INTVAL (args[2]));
2567 body = gen_rtx_SET (VOIDmode, mem, args[1]);
2568 mems_found += record_store (body, bb_info);
2569 if (dump_file && (dump_flags & TDF_DETAILS))
2570 fprintf (dump_file, "handling memset as BLKmode store\n");
2571 if (mems_found == 1)
2573 if (active_local_stores_len++
2574 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2576 active_local_stores_len = 1;
2577 active_local_stores = NULL;
2579 insn_info->fixed_regs_live
2580 = copy_fixed_regs (bb_info->regs_live);
2581 insn_info->next_local_store = active_local_stores;
2582 active_local_stores = insn_info;
2588 else
2589 /* Every other call, including pure functions, may read any memory
2590 that is not relative to the frame. */
2591 add_non_frame_wild_read (bb_info);
2593 return;
2596 /* Assuming that there are sets in these insns, we cannot delete
2597 them. */
2598 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2599 || volatile_refs_p (PATTERN (insn))
2600 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2601 || (RTX_FRAME_RELATED_P (insn))
2602 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2603 insn_info->cannot_delete = true;
2605 body = PATTERN (insn);
2606 if (GET_CODE (body) == PARALLEL)
2608 int i;
2609 for (i = 0; i < XVECLEN (body, 0); i++)
2610 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2612 else
2613 mems_found += record_store (body, bb_info);
2615 if (dump_file && (dump_flags & TDF_DETAILS))
2616 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2617 mems_found, insn_info->cannot_delete ? "true" : "false");
2619 /* If we found some sets of mems, add it into the active_local_stores so
2620 that it can be locally deleted if found dead or used for
2621 replace_read and redundant constant store elimination. Otherwise mark
2622 it as cannot delete. This simplifies the processing later. */
2623 if (mems_found == 1)
2625 if (active_local_stores_len++
2626 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2628 active_local_stores_len = 1;
2629 active_local_stores = NULL;
2631 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2632 insn_info->next_local_store = active_local_stores;
2633 active_local_stores = insn_info;
2635 else
2636 insn_info->cannot_delete = true;
2640 /* Remove BASE from the set of active_local_stores. This is a
2641 callback from cselib that is used to get rid of the stores in
2642 active_local_stores. */
2644 static void
2645 remove_useless_values (cselib_val *base)
2647 insn_info_t insn_info = active_local_stores;
2648 insn_info_t last = NULL;
2650 while (insn_info)
2652 store_info_t store_info = insn_info->store_rec;
2653 bool del = false;
2655 /* If ANY of the store_infos match the cselib group that is
2656 being deleted, then the insn can not be deleted. */
2657 while (store_info)
2659 if ((store_info->group_id == -1)
2660 && (store_info->cse_base == base))
2662 del = true;
2663 break;
2665 store_info = store_info->next;
2668 if (del)
2670 active_local_stores_len--;
2671 if (last)
2672 last->next_local_store = insn_info->next_local_store;
2673 else
2674 active_local_stores = insn_info->next_local_store;
2675 free_store_info (insn_info);
2677 else
2678 last = insn_info;
2680 insn_info = insn_info->next_local_store;
2685 /* Do all of step 1. */
2687 static void
2688 dse_step1 (void)
2690 basic_block bb;
2691 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2693 cselib_init (0);
2694 all_blocks = BITMAP_ALLOC (NULL);
2695 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2696 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2698 FOR_ALL_BB_FN (bb, cfun)
2700 insn_info_t ptr;
2701 bb_info_t bb_info = (bb_info_t) pool_alloc (bb_info_pool);
2703 memset (bb_info, 0, sizeof (struct dse_bb_info));
2704 bitmap_set_bit (all_blocks, bb->index);
2705 bb_info->regs_live = regs_live;
2707 bitmap_copy (regs_live, DF_LR_IN (bb));
2708 df_simulate_initialize_forwards (bb, regs_live);
2710 bb_table[bb->index] = bb_info;
2711 cselib_discard_hook = remove_useless_values;
2713 if (bb->index >= NUM_FIXED_BLOCKS)
2715 rtx_insn *insn;
2717 cse_store_info_pool
2718 = create_alloc_pool ("cse_store_info_pool",
2719 sizeof (struct store_info), 100);
2720 active_local_stores = NULL;
2721 active_local_stores_len = 0;
2722 cselib_clear_table ();
2724 /* Scan the insns. */
2725 FOR_BB_INSNS (bb, insn)
2727 if (INSN_P (insn))
2728 scan_insn (bb_info, insn);
2729 cselib_process_insn (insn);
2730 if (INSN_P (insn))
2731 df_simulate_one_insn_forwards (bb, insn, regs_live);
2734 /* This is something of a hack, because the global algorithm
2735 is supposed to take care of the case where stores go dead
2736 at the end of the function. However, the global
2737 algorithm must take a more conservative view of block
2738 mode reads than the local alg does. So to get the case
2739 where you have a store to the frame followed by a non
2740 overlapping block more read, we look at the active local
2741 stores at the end of the function and delete all of the
2742 frame and spill based ones. */
2743 if (stores_off_frame_dead_at_return
2744 && (EDGE_COUNT (bb->succs) == 0
2745 || (single_succ_p (bb)
2746 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2747 && ! crtl->calls_eh_return)))
2749 insn_info_t i_ptr = active_local_stores;
2750 while (i_ptr)
2752 store_info_t store_info = i_ptr->store_rec;
2754 /* Skip the clobbers. */
2755 while (!store_info->is_set)
2756 store_info = store_info->next;
2757 if (store_info->alias_set && !i_ptr->cannot_delete)
2758 delete_dead_store_insn (i_ptr);
2759 else
2760 if (store_info->group_id >= 0)
2762 group_info_t group
2763 = rtx_group_vec[store_info->group_id];
2764 if (group->frame_related && !i_ptr->cannot_delete)
2765 delete_dead_store_insn (i_ptr);
2768 i_ptr = i_ptr->next_local_store;
2772 /* Get rid of the loads that were discovered in
2773 replace_read. Cselib is finished with this block. */
2774 while (deferred_change_list)
2776 deferred_change_t next = deferred_change_list->next;
2778 /* There is no reason to validate this change. That was
2779 done earlier. */
2780 *deferred_change_list->loc = deferred_change_list->reg;
2781 pool_free (deferred_change_pool, deferred_change_list);
2782 deferred_change_list = next;
2785 /* Get rid of all of the cselib based store_infos in this
2786 block and mark the containing insns as not being
2787 deletable. */
2788 ptr = bb_info->last_insn;
2789 while (ptr)
2791 if (ptr->contains_cselib_groups)
2793 store_info_t s_info = ptr->store_rec;
2794 while (s_info && !s_info->is_set)
2795 s_info = s_info->next;
2796 if (s_info
2797 && s_info->redundant_reason
2798 && s_info->redundant_reason->insn
2799 && !ptr->cannot_delete)
2801 if (dump_file && (dump_flags & TDF_DETAILS))
2802 fprintf (dump_file, "Locally deleting insn %d "
2803 "because insn %d stores the "
2804 "same value and couldn't be "
2805 "eliminated\n",
2806 INSN_UID (ptr->insn),
2807 INSN_UID (s_info->redundant_reason->insn));
2808 delete_dead_store_insn (ptr);
2810 free_store_info (ptr);
2812 else
2814 store_info_t s_info;
2816 /* Free at least positions_needed bitmaps. */
2817 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2818 if (s_info->is_large)
2820 BITMAP_FREE (s_info->positions_needed.large.bmap);
2821 s_info->is_large = false;
2824 ptr = ptr->prev_insn;
2827 free_alloc_pool (cse_store_info_pool);
2829 bb_info->regs_live = NULL;
2832 BITMAP_FREE (regs_live);
2833 cselib_finish ();
2834 rtx_group_table->empty ();
2838 /*----------------------------------------------------------------------------
2839 Second step.
2841 Assign each byte position in the stores that we are going to
2842 analyze globally to a position in the bitmaps. Returns true if
2843 there are any bit positions assigned.
2844 ----------------------------------------------------------------------------*/
2846 static void
2847 dse_step2_init (void)
2849 unsigned int i;
2850 group_info_t group;
2852 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2854 /* For all non stack related bases, we only consider a store to
2855 be deletable if there are two or more stores for that
2856 position. This is because it takes one store to make the
2857 other store redundant. However, for the stores that are
2858 stack related, we consider them if there is only one store
2859 for the position. We do this because the stack related
2860 stores can be deleted if their is no read between them and
2861 the end of the function.
2863 To make this work in the current framework, we take the stack
2864 related bases add all of the bits from store1 into store2.
2865 This has the effect of making the eligible even if there is
2866 only one store. */
2868 if (stores_off_frame_dead_at_return && group->frame_related)
2870 bitmap_ior_into (group->store2_n, group->store1_n);
2871 bitmap_ior_into (group->store2_p, group->store1_p);
2872 if (dump_file && (dump_flags & TDF_DETAILS))
2873 fprintf (dump_file, "group %d is frame related ", i);
2876 group->offset_map_size_n++;
2877 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2878 group->offset_map_size_n);
2879 group->offset_map_size_p++;
2880 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2881 group->offset_map_size_p);
2882 group->process_globally = false;
2883 if (dump_file && (dump_flags & TDF_DETAILS))
2885 fprintf (dump_file, "group %d(%d+%d): ", i,
2886 (int)bitmap_count_bits (group->store2_n),
2887 (int)bitmap_count_bits (group->store2_p));
2888 bitmap_print (dump_file, group->store2_n, "n ", " ");
2889 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2895 /* Init the offset tables for the normal case. */
2897 static bool
2898 dse_step2_nospill (void)
2900 unsigned int i;
2901 group_info_t group;
2902 /* Position 0 is unused because 0 is used in the maps to mean
2903 unused. */
2904 current_position = 1;
2905 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2907 bitmap_iterator bi;
2908 unsigned int j;
2910 if (group == clear_alias_group)
2911 continue;
2913 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2914 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2915 bitmap_clear (group->group_kill);
2917 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2919 bitmap_set_bit (group->group_kill, current_position);
2920 if (bitmap_bit_p (group->escaped_n, j))
2921 bitmap_set_bit (kill_on_calls, current_position);
2922 group->offset_map_n[j] = current_position++;
2923 group->process_globally = true;
2925 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2927 bitmap_set_bit (group->group_kill, current_position);
2928 if (bitmap_bit_p (group->escaped_p, j))
2929 bitmap_set_bit (kill_on_calls, current_position);
2930 group->offset_map_p[j] = current_position++;
2931 group->process_globally = true;
2934 return current_position != 1;
2939 /*----------------------------------------------------------------------------
2940 Third step.
2942 Build the bit vectors for the transfer functions.
2943 ----------------------------------------------------------------------------*/
2946 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2947 there, return 0. */
2949 static int
2950 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
2952 if (offset < 0)
2954 HOST_WIDE_INT offset_p = -offset;
2955 if (offset_p >= group_info->offset_map_size_n)
2956 return 0;
2957 return group_info->offset_map_n[offset_p];
2959 else
2961 if (offset >= group_info->offset_map_size_p)
2962 return 0;
2963 return group_info->offset_map_p[offset];
2968 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2969 may be NULL. */
2971 static void
2972 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
2974 while (store_info)
2976 HOST_WIDE_INT i;
2977 group_info_t group_info
2978 = rtx_group_vec[store_info->group_id];
2979 if (group_info->process_globally)
2980 for (i = store_info->begin; i < store_info->end; i++)
2982 int index = get_bitmap_index (group_info, i);
2983 if (index != 0)
2985 bitmap_set_bit (gen, index);
2986 if (kill)
2987 bitmap_clear_bit (kill, index);
2990 store_info = store_info->next;
2995 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2996 may be NULL. */
2998 static void
2999 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
3001 while (store_info)
3003 if (store_info->alias_set)
3005 int index = get_bitmap_index (clear_alias_group,
3006 store_info->alias_set);
3007 if (index != 0)
3009 bitmap_set_bit (gen, index);
3010 if (kill)
3011 bitmap_clear_bit (kill, index);
3014 store_info = store_info->next;
3019 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3020 may be NULL. */
3022 static void
3023 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
3025 read_info_t read_info = insn_info->read_rec;
3026 int i;
3027 group_info_t group;
3029 /* If this insn reads the frame, kill all the frame related stores. */
3030 if (insn_info->frame_read)
3032 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3033 if (group->process_globally && group->frame_related)
3035 if (kill)
3036 bitmap_ior_into (kill, group->group_kill);
3037 bitmap_and_compl_into (gen, group->group_kill);
3040 if (insn_info->non_frame_wild_read)
3042 /* Kill all non-frame related stores. Kill all stores of variables that
3043 escape. */
3044 if (kill)
3045 bitmap_ior_into (kill, kill_on_calls);
3046 bitmap_and_compl_into (gen, kill_on_calls);
3047 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3048 if (group->process_globally && !group->frame_related)
3050 if (kill)
3051 bitmap_ior_into (kill, group->group_kill);
3052 bitmap_and_compl_into (gen, group->group_kill);
3055 while (read_info)
3057 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3059 if (group->process_globally)
3061 if (i == read_info->group_id)
3063 if (read_info->begin > read_info->end)
3065 /* Begin > end for block mode reads. */
3066 if (kill)
3067 bitmap_ior_into (kill, group->group_kill);
3068 bitmap_and_compl_into (gen, group->group_kill);
3070 else
3072 /* The groups are the same, just process the
3073 offsets. */
3074 HOST_WIDE_INT j;
3075 for (j = read_info->begin; j < read_info->end; j++)
3077 int index = get_bitmap_index (group, j);
3078 if (index != 0)
3080 if (kill)
3081 bitmap_set_bit (kill, index);
3082 bitmap_clear_bit (gen, index);
3087 else
3089 /* The groups are different, if the alias sets
3090 conflict, clear the entire group. We only need
3091 to apply this test if the read_info is a cselib
3092 read. Anything with a constant base cannot alias
3093 something else with a different constant
3094 base. */
3095 if ((read_info->group_id < 0)
3096 && canon_true_dependence (group->base_mem,
3097 GET_MODE (group->base_mem),
3098 group->canon_base_addr,
3099 read_info->mem, NULL_RTX))
3101 if (kill)
3102 bitmap_ior_into (kill, group->group_kill);
3103 bitmap_and_compl_into (gen, group->group_kill);
3109 read_info = read_info->next;
3113 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3114 may be NULL. */
3116 static void
3117 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
3119 while (read_info)
3121 if (read_info->alias_set)
3123 int index = get_bitmap_index (clear_alias_group,
3124 read_info->alias_set);
3125 if (index != 0)
3127 if (kill)
3128 bitmap_set_bit (kill, index);
3129 bitmap_clear_bit (gen, index);
3133 read_info = read_info->next;
3138 /* Return the insn in BB_INFO before the first wild read or if there
3139 are no wild reads in the block, return the last insn. */
3141 static insn_info_t
3142 find_insn_before_first_wild_read (bb_info_t bb_info)
3144 insn_info_t insn_info = bb_info->last_insn;
3145 insn_info_t last_wild_read = NULL;
3147 while (insn_info)
3149 if (insn_info->wild_read)
3151 last_wild_read = insn_info->prev_insn;
3152 /* Block starts with wild read. */
3153 if (!last_wild_read)
3154 return NULL;
3157 insn_info = insn_info->prev_insn;
3160 if (last_wild_read)
3161 return last_wild_read;
3162 else
3163 return bb_info->last_insn;
3167 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3168 the block in order to build the gen and kill sets for the block.
3169 We start at ptr which may be the last insn in the block or may be
3170 the first insn with a wild read. In the latter case we are able to
3171 skip the rest of the block because it just does not matter:
3172 anything that happens is hidden by the wild read. */
3174 static void
3175 dse_step3_scan (bool for_spills, basic_block bb)
3177 bb_info_t bb_info = bb_table[bb->index];
3178 insn_info_t insn_info;
3180 if (for_spills)
3181 /* There are no wild reads in the spill case. */
3182 insn_info = bb_info->last_insn;
3183 else
3184 insn_info = find_insn_before_first_wild_read (bb_info);
3186 /* In the spill case or in the no_spill case if there is no wild
3187 read in the block, we will need a kill set. */
3188 if (insn_info == bb_info->last_insn)
3190 if (bb_info->kill)
3191 bitmap_clear (bb_info->kill);
3192 else
3193 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
3195 else
3196 if (bb_info->kill)
3197 BITMAP_FREE (bb_info->kill);
3199 while (insn_info)
3201 /* There may have been code deleted by the dce pass run before
3202 this phase. */
3203 if (insn_info->insn && INSN_P (insn_info->insn))
3205 /* Process the read(s) last. */
3206 if (for_spills)
3208 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3209 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
3211 else
3213 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3214 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
3218 insn_info = insn_info->prev_insn;
3223 /* Set the gen set of the exit block, and also any block with no
3224 successors that does not have a wild read. */
3226 static void
3227 dse_step3_exit_block_scan (bb_info_t bb_info)
3229 /* The gen set is all 0's for the exit block except for the
3230 frame_pointer_group. */
3232 if (stores_off_frame_dead_at_return)
3234 unsigned int i;
3235 group_info_t group;
3237 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3239 if (group->process_globally && group->frame_related)
3240 bitmap_ior_into (bb_info->gen, group->group_kill);
3246 /* Find all of the blocks that are not backwards reachable from the
3247 exit block or any block with no successors (BB). These are the
3248 infinite loops or infinite self loops. These blocks will still
3249 have their bits set in UNREACHABLE_BLOCKS. */
3251 static void
3252 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3254 edge e;
3255 edge_iterator ei;
3257 if (bitmap_bit_p (unreachable_blocks, bb->index))
3259 bitmap_clear_bit (unreachable_blocks, bb->index);
3260 FOR_EACH_EDGE (e, ei, bb->preds)
3262 mark_reachable_blocks (unreachable_blocks, e->src);
3267 /* Build the transfer functions for the function. */
3269 static void
3270 dse_step3 (bool for_spills)
3272 basic_block bb;
3273 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
3274 sbitmap_iterator sbi;
3275 bitmap all_ones = NULL;
3276 unsigned int i;
3278 bitmap_ones (unreachable_blocks);
3280 FOR_ALL_BB_FN (bb, cfun)
3282 bb_info_t bb_info = bb_table[bb->index];
3283 if (bb_info->gen)
3284 bitmap_clear (bb_info->gen);
3285 else
3286 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3288 if (bb->index == ENTRY_BLOCK)
3290 else if (bb->index == EXIT_BLOCK)
3291 dse_step3_exit_block_scan (bb_info);
3292 else
3293 dse_step3_scan (for_spills, bb);
3294 if (EDGE_COUNT (bb->succs) == 0)
3295 mark_reachable_blocks (unreachable_blocks, bb);
3297 /* If this is the second time dataflow is run, delete the old
3298 sets. */
3299 if (bb_info->in)
3300 BITMAP_FREE (bb_info->in);
3301 if (bb_info->out)
3302 BITMAP_FREE (bb_info->out);
3305 /* For any block in an infinite loop, we must initialize the out set
3306 to all ones. This could be expensive, but almost never occurs in
3307 practice. However, it is common in regression tests. */
3308 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3310 if (bitmap_bit_p (all_blocks, i))
3312 bb_info_t bb_info = bb_table[i];
3313 if (!all_ones)
3315 unsigned int j;
3316 group_info_t group;
3318 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3319 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3320 bitmap_ior_into (all_ones, group->group_kill);
3322 if (!bb_info->out)
3324 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3325 bitmap_copy (bb_info->out, all_ones);
3330 if (all_ones)
3331 BITMAP_FREE (all_ones);
3332 sbitmap_free (unreachable_blocks);
3337 /*----------------------------------------------------------------------------
3338 Fourth step.
3340 Solve the bitvector equations.
3341 ----------------------------------------------------------------------------*/
3344 /* Confluence function for blocks with no successors. Create an out
3345 set from the gen set of the exit block. This block logically has
3346 the exit block as a successor. */
3350 static void
3351 dse_confluence_0 (basic_block bb)
3353 bb_info_t bb_info = bb_table[bb->index];
3355 if (bb->index == EXIT_BLOCK)
3356 return;
3358 if (!bb_info->out)
3360 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3361 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3365 /* Propagate the information from the in set of the dest of E to the
3366 out set of the src of E. If the various in or out sets are not
3367 there, that means they are all ones. */
3369 static bool
3370 dse_confluence_n (edge e)
3372 bb_info_t src_info = bb_table[e->src->index];
3373 bb_info_t dest_info = bb_table[e->dest->index];
3375 if (dest_info->in)
3377 if (src_info->out)
3378 bitmap_and_into (src_info->out, dest_info->in);
3379 else
3381 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3382 bitmap_copy (src_info->out, dest_info->in);
3385 return true;
3389 /* Propagate the info from the out to the in set of BB_INDEX's basic
3390 block. There are three cases:
3392 1) The block has no kill set. In this case the kill set is all
3393 ones. It does not matter what the out set of the block is, none of
3394 the info can reach the top. The only thing that reaches the top is
3395 the gen set and we just copy the set.
3397 2) There is a kill set but no out set and bb has successors. In
3398 this case we just return. Eventually an out set will be created and
3399 it is better to wait than to create a set of ones.
3401 3) There is both a kill and out set. We apply the obvious transfer
3402 function.
3405 static bool
3406 dse_transfer_function (int bb_index)
3408 bb_info_t bb_info = bb_table[bb_index];
3410 if (bb_info->kill)
3412 if (bb_info->out)
3414 /* Case 3 above. */
3415 if (bb_info->in)
3416 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3417 bb_info->out, bb_info->kill);
3418 else
3420 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3421 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3422 bb_info->out, bb_info->kill);
3423 return true;
3426 else
3427 /* Case 2 above. */
3428 return false;
3430 else
3432 /* Case 1 above. If there is already an in set, nothing
3433 happens. */
3434 if (bb_info->in)
3435 return false;
3436 else
3438 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3439 bitmap_copy (bb_info->in, bb_info->gen);
3440 return true;
3445 /* Solve the dataflow equations. */
3447 static void
3448 dse_step4 (void)
3450 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3451 dse_confluence_n, dse_transfer_function,
3452 all_blocks, df_get_postorder (DF_BACKWARD),
3453 df_get_n_blocks (DF_BACKWARD));
3454 if (dump_file && (dump_flags & TDF_DETAILS))
3456 basic_block bb;
3458 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3459 FOR_ALL_BB_FN (bb, cfun)
3461 bb_info_t bb_info = bb_table[bb->index];
3463 df_print_bb_index (bb, dump_file);
3464 if (bb_info->in)
3465 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3466 else
3467 fprintf (dump_file, " in: *MISSING*\n");
3468 if (bb_info->gen)
3469 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3470 else
3471 fprintf (dump_file, " gen: *MISSING*\n");
3472 if (bb_info->kill)
3473 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3474 else
3475 fprintf (dump_file, " kill: *MISSING*\n");
3476 if (bb_info->out)
3477 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3478 else
3479 fprintf (dump_file, " out: *MISSING*\n\n");
3486 /*----------------------------------------------------------------------------
3487 Fifth step.
3489 Delete the stores that can only be deleted using the global information.
3490 ----------------------------------------------------------------------------*/
3493 static void
3494 dse_step5_nospill (void)
3496 basic_block bb;
3497 FOR_EACH_BB_FN (bb, cfun)
3499 bb_info_t bb_info = bb_table[bb->index];
3500 insn_info_t insn_info = bb_info->last_insn;
3501 bitmap v = bb_info->out;
3503 while (insn_info)
3505 bool deleted = false;
3506 if (dump_file && insn_info->insn)
3508 fprintf (dump_file, "starting to process insn %d\n",
3509 INSN_UID (insn_info->insn));
3510 bitmap_print (dump_file, v, " v: ", "\n");
3513 /* There may have been code deleted by the dce pass run before
3514 this phase. */
3515 if (insn_info->insn
3516 && INSN_P (insn_info->insn)
3517 && (!insn_info->cannot_delete)
3518 && (!bitmap_empty_p (v)))
3520 store_info_t store_info = insn_info->store_rec;
3522 /* Try to delete the current insn. */
3523 deleted = true;
3525 /* Skip the clobbers. */
3526 while (!store_info->is_set)
3527 store_info = store_info->next;
3529 if (store_info->alias_set)
3530 deleted = false;
3531 else
3533 HOST_WIDE_INT i;
3534 group_info_t group_info
3535 = rtx_group_vec[store_info->group_id];
3537 for (i = store_info->begin; i < store_info->end; i++)
3539 int index = get_bitmap_index (group_info, i);
3541 if (dump_file && (dump_flags & TDF_DETAILS))
3542 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3543 if (index == 0 || !bitmap_bit_p (v, index))
3545 if (dump_file && (dump_flags & TDF_DETAILS))
3546 fprintf (dump_file, "failing at i = %d\n", (int)i);
3547 deleted = false;
3548 break;
3552 if (deleted)
3554 if (dbg_cnt (dse)
3555 && check_for_inc_dec_1 (insn_info))
3557 delete_insn (insn_info->insn);
3558 insn_info->insn = NULL;
3559 globally_deleted++;
3563 /* We do want to process the local info if the insn was
3564 deleted. For instance, if the insn did a wild read, we
3565 no longer need to trash the info. */
3566 if (insn_info->insn
3567 && INSN_P (insn_info->insn)
3568 && (!deleted))
3570 scan_stores_nospill (insn_info->store_rec, v, NULL);
3571 if (insn_info->wild_read)
3573 if (dump_file && (dump_flags & TDF_DETAILS))
3574 fprintf (dump_file, "wild read\n");
3575 bitmap_clear (v);
3577 else if (insn_info->read_rec
3578 || insn_info->non_frame_wild_read)
3580 if (dump_file && !insn_info->non_frame_wild_read)
3581 fprintf (dump_file, "regular read\n");
3582 else if (dump_file && (dump_flags & TDF_DETAILS))
3583 fprintf (dump_file, "non-frame wild read\n");
3584 scan_reads_nospill (insn_info, v, NULL);
3588 insn_info = insn_info->prev_insn;
3595 /*----------------------------------------------------------------------------
3596 Sixth step.
3598 Delete stores made redundant by earlier stores (which store the same
3599 value) that couldn't be eliminated.
3600 ----------------------------------------------------------------------------*/
3602 static void
3603 dse_step6 (void)
3605 basic_block bb;
3607 FOR_ALL_BB_FN (bb, cfun)
3609 bb_info_t bb_info = bb_table[bb->index];
3610 insn_info_t insn_info = bb_info->last_insn;
3612 while (insn_info)
3614 /* There may have been code deleted by the dce pass run before
3615 this phase. */
3616 if (insn_info->insn
3617 && INSN_P (insn_info->insn)
3618 && !insn_info->cannot_delete)
3620 store_info_t s_info = insn_info->store_rec;
3622 while (s_info && !s_info->is_set)
3623 s_info = s_info->next;
3624 if (s_info
3625 && s_info->redundant_reason
3626 && s_info->redundant_reason->insn
3627 && INSN_P (s_info->redundant_reason->insn))
3629 rtx_insn *rinsn = s_info->redundant_reason->insn;
3630 if (dump_file && (dump_flags & TDF_DETAILS))
3631 fprintf (dump_file, "Locally deleting insn %d "
3632 "because insn %d stores the "
3633 "same value and couldn't be "
3634 "eliminated\n",
3635 INSN_UID (insn_info->insn),
3636 INSN_UID (rinsn));
3637 delete_dead_store_insn (insn_info);
3640 insn_info = insn_info->prev_insn;
3645 /*----------------------------------------------------------------------------
3646 Seventh step.
3648 Destroy everything left standing.
3649 ----------------------------------------------------------------------------*/
3651 static void
3652 dse_step7 (void)
3654 bitmap_obstack_release (&dse_bitmap_obstack);
3655 obstack_free (&dse_obstack, NULL);
3657 end_alias_analysis ();
3658 free (bb_table);
3659 delete rtx_group_table;
3660 rtx_group_table = NULL;
3661 rtx_group_vec.release ();
3662 BITMAP_FREE (all_blocks);
3663 BITMAP_FREE (scratch);
3665 free_alloc_pool (rtx_store_info_pool);
3666 free_alloc_pool (read_info_pool);
3667 free_alloc_pool (insn_info_pool);
3668 free_alloc_pool (bb_info_pool);
3669 free_alloc_pool (rtx_group_info_pool);
3670 free_alloc_pool (deferred_change_pool);
3674 /* -------------------------------------------------------------------------
3676 ------------------------------------------------------------------------- */
3678 /* Callback for running pass_rtl_dse. */
3680 static unsigned int
3681 rest_of_handle_dse (void)
3683 df_set_flags (DF_DEFER_INSN_RESCAN);
3685 /* Need the notes since we must track live hardregs in the forwards
3686 direction. */
3687 df_note_add_problem ();
3688 df_analyze ();
3690 dse_step0 ();
3691 dse_step1 ();
3692 dse_step2_init ();
3693 if (dse_step2_nospill ())
3695 df_set_flags (DF_LR_RUN_DCE);
3696 df_analyze ();
3697 if (dump_file && (dump_flags & TDF_DETAILS))
3698 fprintf (dump_file, "doing global processing\n");
3699 dse_step3 (false);
3700 dse_step4 ();
3701 dse_step5_nospill ();
3704 dse_step6 ();
3705 dse_step7 ();
3707 if (dump_file)
3708 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3709 locally_deleted, globally_deleted, spill_deleted);
3710 return 0;
3713 namespace {
3715 const pass_data pass_data_rtl_dse1 =
3717 RTL_PASS, /* type */
3718 "dse1", /* name */
3719 OPTGROUP_NONE, /* optinfo_flags */
3720 TV_DSE1, /* tv_id */
3721 0, /* properties_required */
3722 0, /* properties_provided */
3723 0, /* properties_destroyed */
3724 0, /* todo_flags_start */
3725 TODO_df_finish, /* todo_flags_finish */
3728 class pass_rtl_dse1 : public rtl_opt_pass
3730 public:
3731 pass_rtl_dse1 (gcc::context *ctxt)
3732 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3735 /* opt_pass methods: */
3736 virtual bool gate (function *)
3738 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3741 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3743 }; // class pass_rtl_dse1
3745 } // anon namespace
3747 rtl_opt_pass *
3748 make_pass_rtl_dse1 (gcc::context *ctxt)
3750 return new pass_rtl_dse1 (ctxt);
3753 namespace {
3755 const pass_data pass_data_rtl_dse2 =
3757 RTL_PASS, /* type */
3758 "dse2", /* name */
3759 OPTGROUP_NONE, /* optinfo_flags */
3760 TV_DSE2, /* tv_id */
3761 0, /* properties_required */
3762 0, /* properties_provided */
3763 0, /* properties_destroyed */
3764 0, /* todo_flags_start */
3765 TODO_df_finish, /* todo_flags_finish */
3768 class pass_rtl_dse2 : public rtl_opt_pass
3770 public:
3771 pass_rtl_dse2 (gcc::context *ctxt)
3772 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3775 /* opt_pass methods: */
3776 virtual bool gate (function *)
3778 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3781 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3783 }; // class pass_rtl_dse2
3785 } // anon namespace
3787 rtl_opt_pass *
3788 make_pass_rtl_dse2 (gcc::context *ctxt)
3790 return new pass_rtl_dse2 (ctxt);