* config/i386/predicates.md (general_reg_operand): Use GENERAL_REGNO_P.
[official-gcc.git] / gcc / dse.c
blob89ba0c961a371c2e807592c222e07edda5097659
1 /* RTL dead store elimination.
2 Copyright (C) 2005-2015 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 "backend.h"
29 #include "tree.h"
30 #include "gimple.h"
31 #include "rtl.h"
32 #include "df.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "tm_p.h"
37 #include "regs.h"
38 #include "regset.h"
39 #include "flags.h"
40 #include "cfgrtl.h"
41 #include "cselib.h"
42 #include "tree-pass.h"
43 #include "alloc-pool.h"
44 #include "insn-config.h"
45 #include "expmed.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "emit-rtl.h"
50 #include "varasm.h"
51 #include "stmt.h"
52 #include "expr.h"
53 #include "recog.h"
54 #include "insn-codes.h"
55 #include "optabs.h"
56 #include "dbgcnt.h"
57 #include "target.h"
58 #include "params.h"
59 #include "internal-fn.h"
60 #include "gimple-ssa.h"
61 #include "rtl-iter.h"
62 #include "cfgcleanup.h"
64 /* This file contains three techniques for performing Dead Store
65 Elimination (dse).
67 * The first technique performs dse locally on any base address. It
68 is based on the cselib which is a local value numbering technique.
69 This technique is local to a basic block but deals with a fairly
70 general addresses.
72 * The second technique performs dse globally but is restricted to
73 base addresses that are either constant or are relative to the
74 frame_pointer.
76 * The third technique, (which is only done after register allocation)
77 processes the spill spill slots. This differs from the second
78 technique because it takes advantage of the fact that spilling is
79 completely free from the effects of aliasing.
81 Logically, dse is a backwards dataflow problem. A store can be
82 deleted if it if cannot be reached in the backward direction by any
83 use of the value being stored. However, the local technique uses a
84 forwards scan of the basic block because cselib requires that the
85 block be processed in that order.
87 The pass is logically broken into 7 steps:
89 0) Initialization.
91 1) The local algorithm, as well as scanning the insns for the two
92 global algorithms.
94 2) Analysis to see if the global algs are necessary. In the case
95 of stores base on a constant address, there must be at least two
96 stores to that address, to make it possible to delete some of the
97 stores. In the case of stores off of the frame or spill related
98 stores, only one store to an address is necessary because those
99 stores die at the end of the function.
101 3) Set up the global dataflow equations based on processing the
102 info parsed in the first step.
104 4) Solve the dataflow equations.
106 5) Delete the insns that the global analysis has indicated are
107 unnecessary.
109 6) Delete insns that store the same value as preceding store
110 where the earlier store couldn't be eliminated.
112 7) Cleanup.
114 This step uses cselib and canon_rtx to build the largest expression
115 possible for each address. This pass is a forwards pass through
116 each basic block. From the point of view of the global technique,
117 the first pass could examine a block in either direction. The
118 forwards ordering is to accommodate cselib.
120 We make a simplifying assumption: addresses fall into four broad
121 categories:
123 1) base has rtx_varies_p == false, offset is constant.
124 2) base has rtx_varies_p == false, offset variable.
125 3) base has rtx_varies_p == true, offset constant.
126 4) base has rtx_varies_p == true, offset variable.
128 The local passes are able to process all 4 kinds of addresses. The
129 global pass only handles 1).
131 The global problem is formulated as follows:
133 A store, S1, to address A, where A is not relative to the stack
134 frame, can be eliminated if all paths from S1 to the end of the
135 function contain another store to A before a read to A.
137 If the address A is relative to the stack frame, a store S2 to A
138 can be eliminated if there are no paths from S2 that reach the
139 end of the function that read A before another store to A. In
140 this case S2 can be deleted if there are paths from S2 to the
141 end of the function that have no reads or writes to A. This
142 second case allows stores to the stack frame to be deleted that
143 would otherwise die when the function returns. This cannot be
144 done if stores_off_frame_dead_at_return is not true. See the doc
145 for that variable for when this variable is false.
147 The global problem is formulated as a backwards set union
148 dataflow problem where the stores are the gens and reads are the
149 kills. Set union problems are rare and require some special
150 handling given our representation of bitmaps. A straightforward
151 implementation requires a lot of bitmaps filled with 1s.
152 These are expensive and cumbersome in our bitmap formulation so
153 care has been taken to avoid large vectors filled with 1s. See
154 the comments in bb_info and in the dataflow confluence functions
155 for details.
157 There are two places for further enhancements to this algorithm:
159 1) The original dse which was embedded in a pass called flow also
160 did local address forwarding. For example in
162 A <- r100
163 ... <- A
165 flow would replace the right hand side of the second insn with a
166 reference to r100. Most of the information is available to add this
167 to this pass. It has not done it because it is a lot of work in
168 the case that either r100 is assigned to between the first and
169 second insn and/or the second insn is a load of part of the value
170 stored by the first insn.
172 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
173 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
174 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
175 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
177 2) The cleaning up of spill code is quite profitable. It currently
178 depends on reading tea leaves and chicken entrails left by reload.
179 This pass depends on reload creating a singleton alias set for each
180 spill slot and telling the next dse pass which of these alias sets
181 are the singletons. Rather than analyze the addresses of the
182 spills, dse's spill processing just does analysis of the loads and
183 stores that use those alias sets. There are three cases where this
184 falls short:
186 a) Reload sometimes creates the slot for one mode of access, and
187 then inserts loads and/or stores for a smaller mode. In this
188 case, the current code just punts on the slot. The proper thing
189 to do is to back out and use one bit vector position for each
190 byte of the entity associated with the slot. This depends on
191 KNOWING that reload always generates the accesses for each of the
192 bytes in some canonical (read that easy to understand several
193 passes after reload happens) way.
195 b) Reload sometimes decides that spill slot it allocated was not
196 large enough for the mode and goes back and allocates more slots
197 with the same mode and alias set. The backout in this case is a
198 little more graceful than (a). In this case the slot is unmarked
199 as being a spill slot and if final address comes out to be based
200 off the frame pointer, the global algorithm handles this slot.
202 c) For any pass that may prespill, there is currently no
203 mechanism to tell the dse pass that the slot being used has the
204 special properties that reload uses. It may be that all that is
205 required is to have those passes make the same calls that reload
206 does, assuming that the alias sets can be manipulated in the same
207 way. */
209 /* There are limits to the size of constant offsets we model for the
210 global problem. There are certainly test cases, that exceed this
211 limit, however, it is unlikely that there are important programs
212 that really have constant offsets this size. */
213 #define MAX_OFFSET (64 * 1024)
215 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
216 on the default obstack because these bitmaps can grow quite large
217 (~2GB for the small (!) test case of PR54146) and we'll hold on to
218 all that memory until the end of the compiler run.
219 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
220 releasing the whole obstack. */
221 static bitmap_obstack dse_bitmap_obstack;
223 /* Obstack for other data. As for above: Kinda nice to be able to
224 throw it all away at the end in one big sweep. */
225 static struct obstack dse_obstack;
227 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
228 static bitmap scratch = NULL;
230 struct insn_info_type;
232 /* This structure holds information about a candidate store. */
233 struct store_info
236 /* False means this is a clobber. */
237 bool is_set;
239 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
240 bool is_large;
242 /* The id of the mem group of the base address. If rtx_varies_p is
243 true, this is -1. Otherwise, it is the index into the group
244 table. */
245 int group_id;
247 /* This is the cselib value. */
248 cselib_val *cse_base;
250 /* This canonized mem. */
251 rtx mem;
253 /* Canonized MEM address for use by canon_true_dependence. */
254 rtx mem_addr;
256 /* If this is non-zero, it is the alias set of a spill location. */
257 alias_set_type alias_set;
259 /* The offset of the first and byte before the last byte associated
260 with the operation. */
261 HOST_WIDE_INT begin, end;
263 union
265 /* A bitmask as wide as the number of bytes in the word that
266 contains a 1 if the byte may be needed. The store is unused if
267 all of the bits are 0. This is used if IS_LARGE is false. */
268 unsigned HOST_WIDE_INT small_bitmask;
270 struct
272 /* A bitmap with one bit per byte. Cleared bit means the position
273 is needed. Used if IS_LARGE is false. */
274 bitmap bmap;
276 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
277 equal to END - BEGIN, the whole store is unused. */
278 int count;
279 } large;
280 } positions_needed;
282 /* The next store info for this insn. */
283 struct store_info *next;
285 /* The right hand side of the store. This is used if there is a
286 subsequent reload of the mems address somewhere later in the
287 basic block. */
288 rtx rhs;
290 /* If rhs is or holds a constant, this contains that constant,
291 otherwise NULL. */
292 rtx const_rhs;
294 /* Set if this store stores the same constant value as REDUNDANT_REASON
295 insn stored. These aren't eliminated early, because doing that
296 might prevent the earlier larger store to be eliminated. */
297 struct insn_info_type *redundant_reason;
300 /* Return a bitmask with the first N low bits set. */
302 static unsigned HOST_WIDE_INT
303 lowpart_bitmask (int n)
305 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
306 return mask >> (HOST_BITS_PER_WIDE_INT - n);
309 typedef struct store_info *store_info_t;
310 static pool_allocator<store_info> cse_store_info_pool ("cse_store_info_pool",
311 100);
313 static pool_allocator<store_info> rtx_store_info_pool ("rtx_store_info_pool",
314 100);
316 /* This structure holds information about a load. These are only
317 built for rtx bases. */
318 struct read_info_type
320 /* The id of the mem group of the base address. */
321 int group_id;
323 /* If this is non-zero, it is the alias set of a spill location. */
324 alias_set_type alias_set;
326 /* The offset of the first and byte after the last byte associated
327 with the operation. If begin == end == 0, the read did not have
328 a constant offset. */
329 int begin, end;
331 /* The mem being read. */
332 rtx mem;
334 /* The next read_info for this insn. */
335 struct read_info_type *next;
337 /* Pool allocation new operator. */
338 inline void *operator new (size_t)
340 return pool.allocate ();
343 /* Delete operator utilizing pool allocation. */
344 inline void operator delete (void *ptr)
346 pool.remove ((read_info_type *) ptr);
349 /* Memory allocation pool. */
350 static pool_allocator<read_info_type> pool;
352 typedef struct read_info_type *read_info_t;
354 pool_allocator<read_info_type> read_info_type::pool ("read_info_pool", 100);
356 /* One of these records is created for each insn. */
358 struct insn_info_type
360 /* Set true if the insn contains a store but the insn itself cannot
361 be deleted. This is set if the insn is a parallel and there is
362 more than one non dead output or if the insn is in some way
363 volatile. */
364 bool cannot_delete;
366 /* This field is only used by the global algorithm. It is set true
367 if the insn contains any read of mem except for a (1). This is
368 also set if the insn is a call or has a clobber mem. If the insn
369 contains a wild read, the use_rec will be null. */
370 bool wild_read;
372 /* This is true only for CALL instructions which could potentially read
373 any non-frame memory location. This field is used by the global
374 algorithm. */
375 bool non_frame_wild_read;
377 /* This field is only used for the processing of const functions.
378 These functions cannot read memory, but they can read the stack
379 because that is where they may get their parms. We need to be
380 this conservative because, like the store motion pass, we don't
381 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
382 Moreover, we need to distinguish two cases:
383 1. Before reload (register elimination), the stores related to
384 outgoing arguments are stack pointer based and thus deemed
385 of non-constant base in this pass. This requires special
386 handling but also means that the frame pointer based stores
387 need not be killed upon encountering a const function call.
388 2. After reload, the stores related to outgoing arguments can be
389 either stack pointer or hard frame pointer based. This means
390 that we have no other choice than also killing all the frame
391 pointer based stores upon encountering a const function call.
392 This field is set after reload for const function calls and before
393 reload for const tail function calls on targets where arg pointer
394 is the frame pointer. Having this set is less severe than a wild
395 read, it just means that all the frame related stores are killed
396 rather than all the stores. */
397 bool frame_read;
399 /* This field is only used for the processing of const functions.
400 It is set if the insn may contain a stack pointer based store. */
401 bool stack_pointer_based;
403 /* This is true if any of the sets within the store contains a
404 cselib base. Such stores can only be deleted by the local
405 algorithm. */
406 bool contains_cselib_groups;
408 /* The insn. */
409 rtx_insn *insn;
411 /* The list of mem sets or mem clobbers that are contained in this
412 insn. If the insn is deletable, it contains only one mem set.
413 But it could also contain clobbers. Insns that contain more than
414 one mem set are not deletable, but each of those mems are here in
415 order to provide info to delete other insns. */
416 store_info_t store_rec;
418 /* The linked list of mem uses in this insn. Only the reads from
419 rtx bases are listed here. The reads to cselib bases are
420 completely processed during the first scan and so are never
421 created. */
422 read_info_t read_rec;
424 /* The live fixed registers. We assume only fixed registers can
425 cause trouble by being clobbered from an expanded pattern;
426 storing only the live fixed registers (rather than all registers)
427 means less memory needs to be allocated / copied for the individual
428 stores. */
429 regset fixed_regs_live;
431 /* The prev insn in the basic block. */
432 struct insn_info_type * prev_insn;
434 /* The linked list of insns that are in consideration for removal in
435 the forwards pass through the basic block. This pointer may be
436 trash as it is not cleared when a wild read occurs. The only
437 time it is guaranteed to be correct is when the traversal starts
438 at active_local_stores. */
439 struct insn_info_type * next_local_store;
441 /* Pool allocation new operator. */
442 inline void *operator new (size_t)
444 return pool.allocate ();
447 /* Delete operator utilizing pool allocation. */
448 inline void operator delete (void *ptr)
450 pool.remove ((insn_info_type *) ptr);
453 /* Memory allocation pool. */
454 static pool_allocator<insn_info_type> pool;
456 typedef struct insn_info_type *insn_info_t;
458 pool_allocator<insn_info_type> insn_info_type::pool ("insn_info_pool", 100);
460 /* The linked list of stores that are under consideration in this
461 basic block. */
462 static insn_info_t active_local_stores;
463 static int active_local_stores_len;
465 struct dse_bb_info_type
467 /* Pointer to the insn info for the last insn in the block. These
468 are linked so this is how all of the insns are reached. During
469 scanning this is the current insn being scanned. */
470 insn_info_t last_insn;
472 /* The info for the global dataflow problem. */
475 /* This is set if the transfer function should and in the wild_read
476 bitmap before applying the kill and gen sets. That vector knocks
477 out most of the bits in the bitmap and thus speeds up the
478 operations. */
479 bool apply_wild_read;
481 /* The following 4 bitvectors hold information about which positions
482 of which stores are live or dead. They are indexed by
483 get_bitmap_index. */
485 /* The set of store positions that exist in this block before a wild read. */
486 bitmap gen;
488 /* The set of load positions that exist in this block above the
489 same position of a store. */
490 bitmap kill;
492 /* The set of stores that reach the top of the block without being
493 killed by a read.
495 Do not represent the in if it is all ones. Note that this is
496 what the bitvector should logically be initialized to for a set
497 intersection problem. However, like the kill set, this is too
498 expensive. So initially, the in set will only be created for the
499 exit block and any block that contains a wild read. */
500 bitmap in;
502 /* The set of stores that reach the bottom of the block from it's
503 successors.
505 Do not represent the in if it is all ones. Note that this is
506 what the bitvector should logically be initialized to for a set
507 intersection problem. However, like the kill and in set, this is
508 too expensive. So what is done is that the confluence operator
509 just initializes the vector from one of the out sets of the
510 successors of the block. */
511 bitmap out;
513 /* The following bitvector is indexed by the reg number. It
514 contains the set of regs that are live at the current instruction
515 being processed. While it contains info for all of the
516 registers, only the hard registers are actually examined. It is used
517 to assure that shift and/or add sequences that are inserted do not
518 accidentally clobber live hard regs. */
519 bitmap regs_live;
521 /* Pool allocation new operator. */
522 inline void *operator new (size_t)
524 return pool.allocate ();
527 /* Delete operator utilizing pool allocation. */
528 inline void operator delete (void *ptr)
530 pool.remove ((dse_bb_info_type *) ptr);
533 /* Memory allocation pool. */
534 static pool_allocator<dse_bb_info_type> pool;
537 typedef struct dse_bb_info_type *bb_info_t;
538 pool_allocator<dse_bb_info_type> dse_bb_info_type::pool ("bb_info_pool", 100);
540 /* Table to hold all bb_infos. */
541 static bb_info_t *bb_table;
543 /* There is a group_info for each rtx base that is used to reference
544 memory. There are also not many of the rtx bases because they are
545 very limited in scope. */
547 struct group_info
549 /* The actual base of the address. */
550 rtx rtx_base;
552 /* The sequential id of the base. This allows us to have a
553 canonical ordering of these that is not based on addresses. */
554 int id;
556 /* True if there are any positions that are to be processed
557 globally. */
558 bool process_globally;
560 /* True if the base of this group is either the frame_pointer or
561 hard_frame_pointer. */
562 bool frame_related;
564 /* A mem wrapped around the base pointer for the group in order to do
565 read dependency. It must be given BLKmode in order to encompass all
566 the possible offsets from the base. */
567 rtx base_mem;
569 /* Canonized version of base_mem's address. */
570 rtx canon_base_addr;
572 /* These two sets of two bitmaps are used to keep track of how many
573 stores are actually referencing that position from this base. We
574 only do this for rtx bases as this will be used to assign
575 positions in the bitmaps for the global problem. Bit N is set in
576 store1 on the first store for offset N. Bit N is set in store2
577 for the second store to offset N. This is all we need since we
578 only care about offsets that have two or more stores for them.
580 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
581 for 0 and greater offsets.
583 There is one special case here, for stores into the stack frame,
584 we will or store1 into store2 before deciding which stores look
585 at globally. This is because stores to the stack frame that have
586 no other reads before the end of the function can also be
587 deleted. */
588 bitmap store1_n, store1_p, store2_n, store2_p;
590 /* These bitmaps keep track of offsets in this group escape this function.
591 An offset escapes if it corresponds to a named variable whose
592 addressable flag is set. */
593 bitmap escaped_n, escaped_p;
595 /* The positions in this bitmap have the same assignments as the in,
596 out, gen and kill bitmaps. This bitmap is all zeros except for
597 the positions that are occupied by stores for this group. */
598 bitmap group_kill;
600 /* The offset_map is used to map the offsets from this base into
601 positions in the global bitmaps. It is only created after all of
602 the all of stores have been scanned and we know which ones we
603 care about. */
604 int *offset_map_n, *offset_map_p;
605 int offset_map_size_n, offset_map_size_p;
607 /* Pool allocation new operator. */
608 inline void *operator new (size_t)
610 return pool.allocate ();
613 /* Delete operator utilizing pool allocation. */
614 inline void operator delete (void *ptr)
616 pool.remove ((group_info *) ptr);
619 /* Memory allocation pool. */
620 static pool_allocator<group_info> pool;
622 typedef struct group_info *group_info_t;
623 typedef const struct group_info *const_group_info_t;
625 pool_allocator<group_info> group_info::pool ("rtx_group_info_pool", 100);
627 /* Index into the rtx_group_vec. */
628 static int rtx_group_next_id;
631 static vec<group_info_t> rtx_group_vec;
634 /* This structure holds the set of changes that are being deferred
635 when removing read operation. See replace_read. */
636 struct deferred_change
639 /* The mem that is being replaced. */
640 rtx *loc;
642 /* The reg it is being replaced with. */
643 rtx reg;
645 struct deferred_change *next;
647 /* Pool allocation new operator. */
648 inline void *operator new (size_t)
650 return pool.allocate ();
653 /* Delete operator utilizing pool allocation. */
654 inline void operator delete (void *ptr)
656 pool.remove ((deferred_change *) ptr);
659 /* Memory allocation pool. */
660 static pool_allocator<deferred_change> pool;
663 typedef struct deferred_change *deferred_change_t;
665 pool_allocator<deferred_change> deferred_change::pool
666 ("deferred_change_pool", 10);
668 static deferred_change_t deferred_change_list = NULL;
670 /* The group that holds all of the clear_alias_sets. */
671 static group_info_t clear_alias_group;
673 /* The modes of the clear_alias_sets. */
674 static htab_t clear_alias_mode_table;
676 /* Hash table element to look up the mode for an alias set. */
677 struct clear_alias_mode_holder
679 alias_set_type alias_set;
680 machine_mode mode;
683 /* This is true except if cfun->stdarg -- i.e. we cannot do
684 this for vararg functions because they play games with the frame. */
685 static bool stores_off_frame_dead_at_return;
687 /* Counter for stats. */
688 static int globally_deleted;
689 static int locally_deleted;
690 static int spill_deleted;
692 static bitmap all_blocks;
694 /* Locations that are killed by calls in the global phase. */
695 static bitmap kill_on_calls;
697 /* The number of bits used in the global bitmaps. */
698 static unsigned int current_position;
700 /*----------------------------------------------------------------------------
701 Zeroth step.
703 Initialization.
704 ----------------------------------------------------------------------------*/
707 /* Find the entry associated with ALIAS_SET. */
709 static struct clear_alias_mode_holder *
710 clear_alias_set_lookup (alias_set_type alias_set)
712 struct clear_alias_mode_holder tmp_holder;
713 void **slot;
715 tmp_holder.alias_set = alias_set;
716 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
717 gcc_assert (*slot);
719 return (struct clear_alias_mode_holder *) *slot;
723 /* Hashtable callbacks for maintaining the "bases" field of
724 store_group_info, given that the addresses are function invariants. */
726 struct invariant_group_base_hasher : nofree_ptr_hash <group_info>
728 static inline hashval_t hash (const group_info *);
729 static inline bool equal (const group_info *, const group_info *);
732 inline bool
733 invariant_group_base_hasher::equal (const group_info *gi1,
734 const group_info *gi2)
736 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
739 inline hashval_t
740 invariant_group_base_hasher::hash (const group_info *gi)
742 int do_not_record;
743 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
746 /* Tables of group_info structures, hashed by base value. */
747 static hash_table<invariant_group_base_hasher> *rtx_group_table;
750 /* Get the GROUP for BASE. Add a new group if it is not there. */
752 static group_info_t
753 get_group_info (rtx base)
755 struct group_info tmp_gi;
756 group_info_t gi;
757 group_info **slot;
759 if (base)
761 /* Find the store_base_info structure for BASE, creating a new one
762 if necessary. */
763 tmp_gi.rtx_base = base;
764 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
765 gi = (group_info_t) *slot;
767 else
769 if (!clear_alias_group)
771 clear_alias_group = gi = new group_info;
772 memset (gi, 0, sizeof (struct group_info));
773 gi->id = rtx_group_next_id++;
774 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
775 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
776 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
777 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
778 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
779 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
780 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
781 gi->process_globally = false;
782 gi->offset_map_size_n = 0;
783 gi->offset_map_size_p = 0;
784 gi->offset_map_n = NULL;
785 gi->offset_map_p = NULL;
786 rtx_group_vec.safe_push (gi);
788 return clear_alias_group;
791 if (gi == NULL)
793 *slot = gi = new group_info;
794 gi->rtx_base = base;
795 gi->id = rtx_group_next_id++;
796 gi->base_mem = gen_rtx_MEM (BLKmode, base);
797 gi->canon_base_addr = canon_rtx (base);
798 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
799 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
800 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
801 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
802 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
803 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
804 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
805 gi->process_globally = false;
806 gi->frame_related =
807 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
808 gi->offset_map_size_n = 0;
809 gi->offset_map_size_p = 0;
810 gi->offset_map_n = NULL;
811 gi->offset_map_p = NULL;
812 rtx_group_vec.safe_push (gi);
815 return gi;
819 /* Initialization of data structures. */
821 static void
822 dse_step0 (void)
824 locally_deleted = 0;
825 globally_deleted = 0;
826 spill_deleted = 0;
828 bitmap_obstack_initialize (&dse_bitmap_obstack);
829 gcc_obstack_init (&dse_obstack);
831 scratch = BITMAP_ALLOC (&reg_obstack);
832 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
835 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
837 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
838 rtx_group_next_id = 0;
840 stores_off_frame_dead_at_return = !cfun->stdarg;
842 init_alias_analysis ();
844 clear_alias_group = NULL;
849 /*----------------------------------------------------------------------------
850 First step.
852 Scan all of the insns. Any random ordering of the blocks is fine.
853 Each block is scanned in forward order to accommodate cselib which
854 is used to remove stores with non-constant bases.
855 ----------------------------------------------------------------------------*/
857 /* Delete all of the store_info recs from INSN_INFO. */
859 static void
860 free_store_info (insn_info_t insn_info)
862 store_info_t store_info = insn_info->store_rec;
863 while (store_info)
865 store_info_t next = store_info->next;
866 if (store_info->is_large)
867 BITMAP_FREE (store_info->positions_needed.large.bmap);
868 if (store_info->cse_base)
869 cse_store_info_pool.remove (store_info);
870 else
871 rtx_store_info_pool.remove (store_info);
872 store_info = next;
875 insn_info->cannot_delete = true;
876 insn_info->contains_cselib_groups = false;
877 insn_info->store_rec = NULL;
880 typedef struct
882 rtx_insn *first, *current;
883 regset fixed_regs_live;
884 bool failure;
885 } note_add_store_info;
887 /* Callback for emit_inc_dec_insn_before via note_stores.
888 Check if a register is clobbered which is live afterwards. */
890 static void
891 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
893 rtx_insn *insn;
894 note_add_store_info *info = (note_add_store_info *) data;
896 if (!REG_P (loc))
897 return;
899 /* If this register is referenced by the current or an earlier insn,
900 that's OK. E.g. this applies to the register that is being incremented
901 with this addition. */
902 for (insn = info->first;
903 insn != NEXT_INSN (info->current);
904 insn = NEXT_INSN (insn))
905 if (reg_referenced_p (loc, PATTERN (insn)))
906 return;
908 /* If we come here, we have a clobber of a register that's only OK
909 if that register is not live. If we don't have liveness information
910 available, fail now. */
911 if (!info->fixed_regs_live)
913 info->failure = true;
914 return;
916 /* Now check if this is a live fixed register. */
917 unsigned int end_regno = END_REGNO (loc);
918 for (unsigned int regno = REGNO (loc); regno < end_regno; ++regno)
919 if (REGNO_REG_SET_P (info->fixed_regs_live, regno))
920 info->failure = true;
923 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
924 SRC + SRCOFF before insn ARG. */
926 static int
927 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
928 rtx op ATTRIBUTE_UNUSED,
929 rtx dest, rtx src, rtx srcoff, void *arg)
931 insn_info_t insn_info = (insn_info_t) arg;
932 rtx_insn *insn = insn_info->insn, *new_insn, *cur;
933 note_add_store_info info;
935 /* We can reuse all operands without copying, because we are about
936 to delete the insn that contained it. */
937 if (srcoff)
939 start_sequence ();
940 emit_insn (gen_add3_insn (dest, src, srcoff));
941 new_insn = get_insns ();
942 end_sequence ();
944 else
945 new_insn = gen_move_insn (dest, src);
946 info.first = new_insn;
947 info.fixed_regs_live = insn_info->fixed_regs_live;
948 info.failure = false;
949 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
951 info.current = cur;
952 note_stores (PATTERN (cur), note_add_store, &info);
955 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
956 return it immediately, communicating the failure to its caller. */
957 if (info.failure)
958 return 1;
960 emit_insn_before (new_insn, insn);
962 return 0;
965 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
966 is there, is split into a separate insn.
967 Return true on success (or if there was nothing to do), false on failure. */
969 static bool
970 check_for_inc_dec_1 (insn_info_t insn_info)
972 rtx_insn *insn = insn_info->insn;
973 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
974 if (note)
975 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
976 insn_info) == 0;
977 return true;
981 /* Entry point for postreload. If you work on reload_cse, or you need this
982 anywhere else, consider if you can provide register liveness information
983 and add a parameter to this function so that it can be passed down in
984 insn_info.fixed_regs_live. */
985 bool
986 check_for_inc_dec (rtx_insn *insn)
988 insn_info_type insn_info;
989 rtx note;
991 insn_info.insn = insn;
992 insn_info.fixed_regs_live = NULL;
993 note = find_reg_note (insn, REG_INC, NULL_RTX);
994 if (note)
995 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
996 &insn_info) == 0;
997 return true;
1000 /* Delete the insn and free all of the fields inside INSN_INFO. */
1002 static void
1003 delete_dead_store_insn (insn_info_t insn_info)
1005 read_info_t read_info;
1007 if (!dbg_cnt (dse))
1008 return;
1010 if (!check_for_inc_dec_1 (insn_info))
1011 return;
1012 if (dump_file && (dump_flags & TDF_DETAILS))
1014 fprintf (dump_file, "Locally deleting insn %d ",
1015 INSN_UID (insn_info->insn));
1016 if (insn_info->store_rec->alias_set)
1017 fprintf (dump_file, "alias set %d\n",
1018 (int) insn_info->store_rec->alias_set);
1019 else
1020 fprintf (dump_file, "\n");
1023 free_store_info (insn_info);
1024 read_info = insn_info->read_rec;
1026 while (read_info)
1028 read_info_t next = read_info->next;
1029 delete read_info;
1030 read_info = next;
1032 insn_info->read_rec = NULL;
1034 delete_insn (insn_info->insn);
1035 locally_deleted++;
1036 insn_info->insn = NULL;
1038 insn_info->wild_read = false;
1041 /* Return whether DECL, a local variable, can possibly escape the current
1042 function scope. */
1044 static bool
1045 local_variable_can_escape (tree decl)
1047 if (TREE_ADDRESSABLE (decl))
1048 return true;
1050 /* If this is a partitioned variable, we need to consider all the variables
1051 in the partition. This is necessary because a store into one of them can
1052 be replaced with a store into another and this may not change the outcome
1053 of the escape analysis. */
1054 if (cfun->gimple_df->decls_to_pointers != NULL)
1056 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
1057 if (namep)
1058 return TREE_ADDRESSABLE (*namep);
1061 return false;
1064 /* Return whether EXPR can possibly escape the current function scope. */
1066 static bool
1067 can_escape (tree expr)
1069 tree base;
1070 if (!expr)
1071 return true;
1072 base = get_base_address (expr);
1073 if (DECL_P (base)
1074 && !may_be_aliased (base)
1075 && !(TREE_CODE (base) == VAR_DECL
1076 && !DECL_EXTERNAL (base)
1077 && !TREE_STATIC (base)
1078 && local_variable_can_escape (base)))
1079 return false;
1080 return true;
1083 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1084 OFFSET and WIDTH. */
1086 static void
1087 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
1088 tree expr)
1090 HOST_WIDE_INT i;
1091 bool expr_escapes = can_escape (expr);
1092 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
1093 for (i=offset; i<offset+width; i++)
1095 bitmap store1;
1096 bitmap store2;
1097 bitmap escaped;
1098 int ai;
1099 if (i < 0)
1101 store1 = group->store1_n;
1102 store2 = group->store2_n;
1103 escaped = group->escaped_n;
1104 ai = -i;
1106 else
1108 store1 = group->store1_p;
1109 store2 = group->store2_p;
1110 escaped = group->escaped_p;
1111 ai = i;
1114 if (!bitmap_set_bit (store1, ai))
1115 bitmap_set_bit (store2, ai);
1116 else
1118 if (i < 0)
1120 if (group->offset_map_size_n < ai)
1121 group->offset_map_size_n = ai;
1123 else
1125 if (group->offset_map_size_p < ai)
1126 group->offset_map_size_p = ai;
1129 if (expr_escapes)
1130 bitmap_set_bit (escaped, ai);
1134 static void
1135 reset_active_stores (void)
1137 active_local_stores = NULL;
1138 active_local_stores_len = 0;
1141 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1143 static void
1144 free_read_records (bb_info_t bb_info)
1146 insn_info_t insn_info = bb_info->last_insn;
1147 read_info_t *ptr = &insn_info->read_rec;
1148 while (*ptr)
1150 read_info_t next = (*ptr)->next;
1151 if ((*ptr)->alias_set == 0)
1153 delete *ptr;
1154 *ptr = next;
1156 else
1157 ptr = &(*ptr)->next;
1161 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1163 static void
1164 add_wild_read (bb_info_t bb_info)
1166 insn_info_t insn_info = bb_info->last_insn;
1167 insn_info->wild_read = true;
1168 free_read_records (bb_info);
1169 reset_active_stores ();
1172 /* Set the BB_INFO so that the last insn is marked as a wild read of
1173 non-frame locations. */
1175 static void
1176 add_non_frame_wild_read (bb_info_t bb_info)
1178 insn_info_t insn_info = bb_info->last_insn;
1179 insn_info->non_frame_wild_read = true;
1180 free_read_records (bb_info);
1181 reset_active_stores ();
1184 /* Return true if X is a constant or one of the registers that behave
1185 as a constant over the life of a function. This is equivalent to
1186 !rtx_varies_p for memory addresses. */
1188 static bool
1189 const_or_frame_p (rtx x)
1191 if (CONSTANT_P (x))
1192 return true;
1194 if (GET_CODE (x) == REG)
1196 /* Note that we have to test for the actual rtx used for the frame
1197 and arg pointers and not just the register number in case we have
1198 eliminated the frame and/or arg pointer and are using it
1199 for pseudos. */
1200 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1201 /* The arg pointer varies if it is not a fixed register. */
1202 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1203 || x == pic_offset_table_rtx)
1204 return true;
1205 return false;
1208 return false;
1211 /* Take all reasonable action to put the address of MEM into the form
1212 that we can do analysis on.
1214 The gold standard is to get the address into the form: address +
1215 OFFSET where address is something that rtx_varies_p considers a
1216 constant. When we can get the address in this form, we can do
1217 global analysis on it. Note that for constant bases, address is
1218 not actually returned, only the group_id. The address can be
1219 obtained from that.
1221 If that fails, we try cselib to get a value we can at least use
1222 locally. If that fails we return false.
1224 The GROUP_ID is set to -1 for cselib bases and the index of the
1225 group for non_varying bases.
1227 FOR_READ is true if this is a mem read and false if not. */
1229 static bool
1230 canon_address (rtx mem,
1231 alias_set_type *alias_set_out,
1232 int *group_id,
1233 HOST_WIDE_INT *offset,
1234 cselib_val **base)
1236 machine_mode address_mode = get_address_mode (mem);
1237 rtx mem_address = XEXP (mem, 0);
1238 rtx expanded_address, address;
1239 int expanded;
1241 *alias_set_out = 0;
1243 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1245 if (dump_file && (dump_flags & TDF_DETAILS))
1247 fprintf (dump_file, " mem: ");
1248 print_inline_rtx (dump_file, mem_address, 0);
1249 fprintf (dump_file, "\n");
1252 /* First see if just canon_rtx (mem_address) is const or frame,
1253 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1254 address = NULL_RTX;
1255 for (expanded = 0; expanded < 2; expanded++)
1257 if (expanded)
1259 /* Use cselib to replace all of the reg references with the full
1260 expression. This will take care of the case where we have
1262 r_x = base + offset;
1263 val = *r_x;
1265 by making it into
1267 val = *(base + offset); */
1269 expanded_address = cselib_expand_value_rtx (mem_address,
1270 scratch, 5);
1272 /* If this fails, just go with the address from first
1273 iteration. */
1274 if (!expanded_address)
1275 break;
1277 else
1278 expanded_address = mem_address;
1280 /* Split the address into canonical BASE + OFFSET terms. */
1281 address = canon_rtx (expanded_address);
1283 *offset = 0;
1285 if (dump_file && (dump_flags & TDF_DETAILS))
1287 if (expanded)
1289 fprintf (dump_file, "\n after cselib_expand address: ");
1290 print_inline_rtx (dump_file, expanded_address, 0);
1291 fprintf (dump_file, "\n");
1294 fprintf (dump_file, "\n after canon_rtx address: ");
1295 print_inline_rtx (dump_file, address, 0);
1296 fprintf (dump_file, "\n");
1299 if (GET_CODE (address) == CONST)
1300 address = XEXP (address, 0);
1302 if (GET_CODE (address) == PLUS
1303 && CONST_INT_P (XEXP (address, 1)))
1305 *offset = INTVAL (XEXP (address, 1));
1306 address = XEXP (address, 0);
1309 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1310 && const_or_frame_p (address))
1312 group_info_t group = get_group_info (address);
1314 if (dump_file && (dump_flags & TDF_DETAILS))
1315 fprintf (dump_file, " gid=%d offset=%d \n",
1316 group->id, (int)*offset);
1317 *base = NULL;
1318 *group_id = group->id;
1319 return true;
1323 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1324 *group_id = -1;
1326 if (*base == NULL)
1328 if (dump_file && (dump_flags & TDF_DETAILS))
1329 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1330 return false;
1332 if (dump_file && (dump_flags & TDF_DETAILS))
1333 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1334 (*base)->uid, (*base)->hash, (int)*offset);
1335 return true;
1339 /* Clear the rhs field from the active_local_stores array. */
1341 static void
1342 clear_rhs_from_active_local_stores (void)
1344 insn_info_t ptr = active_local_stores;
1346 while (ptr)
1348 store_info_t store_info = ptr->store_rec;
1349 /* Skip the clobbers. */
1350 while (!store_info->is_set)
1351 store_info = store_info->next;
1353 store_info->rhs = NULL;
1354 store_info->const_rhs = NULL;
1356 ptr = ptr->next_local_store;
1361 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1363 static inline void
1364 set_position_unneeded (store_info_t s_info, int pos)
1366 if (__builtin_expect (s_info->is_large, false))
1368 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1369 s_info->positions_needed.large.count++;
1371 else
1372 s_info->positions_needed.small_bitmask
1373 &= ~(((unsigned HOST_WIDE_INT) 1) << pos);
1376 /* Mark the whole store S_INFO as unneeded. */
1378 static inline void
1379 set_all_positions_unneeded (store_info_t s_info)
1381 if (__builtin_expect (s_info->is_large, false))
1383 int pos, end = s_info->end - s_info->begin;
1384 for (pos = 0; pos < end; pos++)
1385 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1386 s_info->positions_needed.large.count = end;
1388 else
1389 s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0;
1392 /* Return TRUE if any bytes from S_INFO store are needed. */
1394 static inline bool
1395 any_positions_needed_p (store_info_t s_info)
1397 if (__builtin_expect (s_info->is_large, false))
1398 return (s_info->positions_needed.large.count
1399 < s_info->end - s_info->begin);
1400 else
1401 return (s_info->positions_needed.small_bitmask
1402 != (unsigned HOST_WIDE_INT) 0);
1405 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1406 store are needed. */
1408 static inline bool
1409 all_positions_needed_p (store_info_t s_info, int start, int width)
1411 if (__builtin_expect (s_info->is_large, false))
1413 int end = start + width;
1414 while (start < end)
1415 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1416 return false;
1417 return true;
1419 else
1421 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1422 return (s_info->positions_needed.small_bitmask & mask) == mask;
1427 static rtx get_stored_val (store_info_t, machine_mode, HOST_WIDE_INT,
1428 HOST_WIDE_INT, basic_block, bool);
1431 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1432 there is a candidate store, after adding it to the appropriate
1433 local store group if so. */
1435 static int
1436 record_store (rtx body, bb_info_t bb_info)
1438 rtx mem, rhs, const_rhs, mem_addr;
1439 HOST_WIDE_INT offset = 0;
1440 HOST_WIDE_INT width = 0;
1441 alias_set_type spill_alias_set;
1442 insn_info_t insn_info = bb_info->last_insn;
1443 store_info_t store_info = NULL;
1444 int group_id;
1445 cselib_val *base = NULL;
1446 insn_info_t ptr, last, redundant_reason;
1447 bool store_is_unused;
1449 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1450 return 0;
1452 mem = SET_DEST (body);
1454 /* If this is not used, then this cannot be used to keep the insn
1455 from being deleted. On the other hand, it does provide something
1456 that can be used to prove that another store is dead. */
1457 store_is_unused
1458 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1460 /* Check whether that value is a suitable memory location. */
1461 if (!MEM_P (mem))
1463 /* If the set or clobber is unused, then it does not effect our
1464 ability to get rid of the entire insn. */
1465 if (!store_is_unused)
1466 insn_info->cannot_delete = true;
1467 return 0;
1470 /* At this point we know mem is a mem. */
1471 if (GET_MODE (mem) == BLKmode)
1473 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1475 if (dump_file && (dump_flags & TDF_DETAILS))
1476 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1477 add_wild_read (bb_info);
1478 insn_info->cannot_delete = true;
1479 return 0;
1481 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1482 as memset (addr, 0, 36); */
1483 else if (!MEM_SIZE_KNOWN_P (mem)
1484 || MEM_SIZE (mem) <= 0
1485 || MEM_SIZE (mem) > MAX_OFFSET
1486 || GET_CODE (body) != SET
1487 || !CONST_INT_P (SET_SRC (body)))
1489 if (!store_is_unused)
1491 /* If the set or clobber is unused, then it does not effect our
1492 ability to get rid of the entire insn. */
1493 insn_info->cannot_delete = true;
1494 clear_rhs_from_active_local_stores ();
1496 return 0;
1500 /* We can still process a volatile mem, we just cannot delete it. */
1501 if (MEM_VOLATILE_P (mem))
1502 insn_info->cannot_delete = true;
1504 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1506 clear_rhs_from_active_local_stores ();
1507 return 0;
1510 if (GET_MODE (mem) == BLKmode)
1511 width = MEM_SIZE (mem);
1512 else
1513 width = GET_MODE_SIZE (GET_MODE (mem));
1515 if (spill_alias_set)
1517 bitmap store1 = clear_alias_group->store1_p;
1518 bitmap store2 = clear_alias_group->store2_p;
1520 gcc_assert (GET_MODE (mem) != BLKmode);
1522 if (!bitmap_set_bit (store1, spill_alias_set))
1523 bitmap_set_bit (store2, spill_alias_set);
1525 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1526 clear_alias_group->offset_map_size_p = spill_alias_set;
1528 store_info = rtx_store_info_pool.allocate ();
1530 if (dump_file && (dump_flags & TDF_DETAILS))
1531 fprintf (dump_file, " processing spill store %d(%s)\n",
1532 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1534 else if (group_id >= 0)
1536 /* In the restrictive case where the base is a constant or the
1537 frame pointer we can do global analysis. */
1539 group_info_t group
1540 = rtx_group_vec[group_id];
1541 tree expr = MEM_EXPR (mem);
1543 store_info = rtx_store_info_pool.allocate ();
1544 set_usage_bits (group, offset, width, expr);
1546 if (dump_file && (dump_flags & TDF_DETAILS))
1547 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1548 group_id, (int)offset, (int)(offset+width));
1550 else
1552 if (may_be_sp_based_p (XEXP (mem, 0)))
1553 insn_info->stack_pointer_based = true;
1554 insn_info->contains_cselib_groups = true;
1556 store_info = cse_store_info_pool.allocate ();
1557 group_id = -1;
1559 if (dump_file && (dump_flags & TDF_DETAILS))
1560 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1561 (int)offset, (int)(offset+width));
1564 const_rhs = rhs = NULL_RTX;
1565 if (GET_CODE (body) == SET
1566 /* No place to keep the value after ra. */
1567 && !reload_completed
1568 && (REG_P (SET_SRC (body))
1569 || GET_CODE (SET_SRC (body)) == SUBREG
1570 || CONSTANT_P (SET_SRC (body)))
1571 && !MEM_VOLATILE_P (mem)
1572 /* Sometimes the store and reload is used for truncation and
1573 rounding. */
1574 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1576 rhs = SET_SRC (body);
1577 if (CONSTANT_P (rhs))
1578 const_rhs = rhs;
1579 else if (body == PATTERN (insn_info->insn))
1581 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1582 if (tem && CONSTANT_P (XEXP (tem, 0)))
1583 const_rhs = XEXP (tem, 0);
1585 if (const_rhs == NULL_RTX && REG_P (rhs))
1587 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1589 if (tem && CONSTANT_P (tem))
1590 const_rhs = tem;
1594 /* Check to see if this stores causes some other stores to be
1595 dead. */
1596 ptr = active_local_stores;
1597 last = NULL;
1598 redundant_reason = NULL;
1599 mem = canon_rtx (mem);
1600 /* For alias_set != 0 canon_true_dependence should be never called. */
1601 if (spill_alias_set)
1602 mem_addr = NULL_RTX;
1603 else
1605 if (group_id < 0)
1606 mem_addr = base->val_rtx;
1607 else
1609 group_info_t group
1610 = rtx_group_vec[group_id];
1611 mem_addr = group->canon_base_addr;
1613 /* get_addr can only handle VALUE but cannot handle expr like:
1614 VALUE + OFFSET, so call get_addr to get original addr for
1615 mem_addr before plus_constant. */
1616 mem_addr = get_addr (mem_addr);
1617 if (offset)
1618 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1621 while (ptr)
1623 insn_info_t next = ptr->next_local_store;
1624 store_info_t s_info = ptr->store_rec;
1625 bool del = true;
1627 /* Skip the clobbers. We delete the active insn if this insn
1628 shadows the set. To have been put on the active list, it
1629 has exactly on set. */
1630 while (!s_info->is_set)
1631 s_info = s_info->next;
1633 if (s_info->alias_set != spill_alias_set)
1634 del = false;
1635 else if (s_info->alias_set)
1637 struct clear_alias_mode_holder *entry
1638 = clear_alias_set_lookup (s_info->alias_set);
1639 /* Generally, spills cannot be processed if and of the
1640 references to the slot have a different mode. But if
1641 we are in the same block and mode is exactly the same
1642 between this store and one before in the same block,
1643 we can still delete it. */
1644 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1645 && (GET_MODE (mem) == entry->mode))
1647 del = true;
1648 set_all_positions_unneeded (s_info);
1650 if (dump_file && (dump_flags & TDF_DETAILS))
1651 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1652 INSN_UID (ptr->insn), (int) s_info->alias_set);
1654 else if ((s_info->group_id == group_id)
1655 && (s_info->cse_base == base))
1657 HOST_WIDE_INT i;
1658 if (dump_file && (dump_flags & TDF_DETAILS))
1659 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1660 INSN_UID (ptr->insn), s_info->group_id,
1661 (int)s_info->begin, (int)s_info->end);
1663 /* Even if PTR won't be eliminated as unneeded, if both
1664 PTR and this insn store the same constant value, we might
1665 eliminate this insn instead. */
1666 if (s_info->const_rhs
1667 && const_rhs
1668 && offset >= s_info->begin
1669 && offset + width <= s_info->end
1670 && all_positions_needed_p (s_info, offset - s_info->begin,
1671 width))
1673 if (GET_MODE (mem) == BLKmode)
1675 if (GET_MODE (s_info->mem) == BLKmode
1676 && s_info->const_rhs == const_rhs)
1677 redundant_reason = ptr;
1679 else if (s_info->const_rhs == const0_rtx
1680 && const_rhs == const0_rtx)
1681 redundant_reason = ptr;
1682 else
1684 rtx val;
1685 start_sequence ();
1686 val = get_stored_val (s_info, GET_MODE (mem),
1687 offset, offset + width,
1688 BLOCK_FOR_INSN (insn_info->insn),
1689 true);
1690 if (get_insns () != NULL)
1691 val = NULL_RTX;
1692 end_sequence ();
1693 if (val && rtx_equal_p (val, const_rhs))
1694 redundant_reason = ptr;
1698 for (i = MAX (offset, s_info->begin);
1699 i < offset + width && i < s_info->end;
1700 i++)
1701 set_position_unneeded (s_info, i - s_info->begin);
1703 else if (s_info->rhs)
1704 /* Need to see if it is possible for this store to overwrite
1705 the value of store_info. If it is, set the rhs to NULL to
1706 keep it from being used to remove a load. */
1708 if (canon_true_dependence (s_info->mem,
1709 GET_MODE (s_info->mem),
1710 s_info->mem_addr,
1711 mem, mem_addr))
1713 s_info->rhs = NULL;
1714 s_info->const_rhs = NULL;
1718 /* An insn can be deleted if every position of every one of
1719 its s_infos is zero. */
1720 if (any_positions_needed_p (s_info))
1721 del = false;
1723 if (del)
1725 insn_info_t insn_to_delete = ptr;
1727 active_local_stores_len--;
1728 if (last)
1729 last->next_local_store = ptr->next_local_store;
1730 else
1731 active_local_stores = ptr->next_local_store;
1733 if (!insn_to_delete->cannot_delete)
1734 delete_dead_store_insn (insn_to_delete);
1736 else
1737 last = ptr;
1739 ptr = next;
1742 /* Finish filling in the store_info. */
1743 store_info->next = insn_info->store_rec;
1744 insn_info->store_rec = store_info;
1745 store_info->mem = mem;
1746 store_info->alias_set = spill_alias_set;
1747 store_info->mem_addr = mem_addr;
1748 store_info->cse_base = base;
1749 if (width > HOST_BITS_PER_WIDE_INT)
1751 store_info->is_large = true;
1752 store_info->positions_needed.large.count = 0;
1753 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1755 else
1757 store_info->is_large = false;
1758 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1760 store_info->group_id = group_id;
1761 store_info->begin = offset;
1762 store_info->end = offset + width;
1763 store_info->is_set = GET_CODE (body) == SET;
1764 store_info->rhs = rhs;
1765 store_info->const_rhs = const_rhs;
1766 store_info->redundant_reason = redundant_reason;
1768 /* If this is a clobber, we return 0. We will only be able to
1769 delete this insn if there is only one store USED store, but we
1770 can use the clobber to delete other stores earlier. */
1771 return store_info->is_set ? 1 : 0;
1775 static void
1776 dump_insn_info (const char * start, insn_info_t insn_info)
1778 fprintf (dump_file, "%s insn=%d %s\n", start,
1779 INSN_UID (insn_info->insn),
1780 insn_info->store_rec ? "has store" : "naked");
1784 /* If the modes are different and the value's source and target do not
1785 line up, we need to extract the value from lower part of the rhs of
1786 the store, shift it, and then put it into a form that can be shoved
1787 into the read_insn. This function generates a right SHIFT of a
1788 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1789 shift sequence is returned or NULL if we failed to find a
1790 shift. */
1792 static rtx
1793 find_shift_sequence (int access_size,
1794 store_info_t store_info,
1795 machine_mode read_mode,
1796 int shift, bool speed, bool require_cst)
1798 machine_mode store_mode = GET_MODE (store_info->mem);
1799 machine_mode new_mode;
1800 rtx read_reg = NULL;
1802 /* Some machines like the x86 have shift insns for each size of
1803 operand. Other machines like the ppc or the ia-64 may only have
1804 shift insns that shift values within 32 or 64 bit registers.
1805 This loop tries to find the smallest shift insn that will right
1806 justify the value we want to read but is available in one insn on
1807 the machine. */
1809 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1810 MODE_INT);
1811 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1812 new_mode = GET_MODE_WIDER_MODE (new_mode))
1814 rtx target, new_reg, new_lhs;
1815 rtx_insn *shift_seq, *insn;
1816 int cost;
1818 /* If a constant was stored into memory, try to simplify it here,
1819 otherwise the cost of the shift might preclude this optimization
1820 e.g. at -Os, even when no actual shift will be needed. */
1821 if (store_info->const_rhs)
1823 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1824 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1825 store_mode, byte);
1826 if (ret && CONSTANT_P (ret))
1828 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1829 ret, GEN_INT (shift));
1830 if (ret && CONSTANT_P (ret))
1832 byte = subreg_lowpart_offset (read_mode, new_mode);
1833 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1834 if (ret && CONSTANT_P (ret)
1835 && (set_src_cost (ret, read_mode, speed)
1836 <= COSTS_N_INSNS (1)))
1837 return ret;
1842 if (require_cst)
1843 return NULL_RTX;
1845 /* Try a wider mode if truncating the store mode to NEW_MODE
1846 requires a real instruction. */
1847 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1848 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1849 continue;
1851 /* Also try a wider mode if the necessary punning is either not
1852 desirable or not possible. */
1853 if (!CONSTANT_P (store_info->rhs)
1854 && !MODES_TIEABLE_P (new_mode, store_mode))
1855 continue;
1857 new_reg = gen_reg_rtx (new_mode);
1859 start_sequence ();
1861 /* In theory we could also check for an ashr. Ian Taylor knows
1862 of one dsp where the cost of these two was not the same. But
1863 this really is a rare case anyway. */
1864 target = expand_binop (new_mode, lshr_optab, new_reg,
1865 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1867 shift_seq = get_insns ();
1868 end_sequence ();
1870 if (target != new_reg || shift_seq == NULL)
1871 continue;
1873 cost = 0;
1874 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1875 if (INSN_P (insn))
1876 cost += insn_rtx_cost (PATTERN (insn), speed);
1878 /* The computation up to here is essentially independent
1879 of the arguments and could be precomputed. It may
1880 not be worth doing so. We could precompute if
1881 worthwhile or at least cache the results. The result
1882 technically depends on both SHIFT and ACCESS_SIZE,
1883 but in practice the answer will depend only on ACCESS_SIZE. */
1885 if (cost > COSTS_N_INSNS (1))
1886 continue;
1888 new_lhs = extract_low_bits (new_mode, store_mode,
1889 copy_rtx (store_info->rhs));
1890 if (new_lhs == NULL_RTX)
1891 continue;
1893 /* We found an acceptable shift. Generate a move to
1894 take the value from the store and put it into the
1895 shift pseudo, then shift it, then generate another
1896 move to put in into the target of the read. */
1897 emit_move_insn (new_reg, new_lhs);
1898 emit_insn (shift_seq);
1899 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1900 break;
1903 return read_reg;
1907 /* Call back for note_stores to find the hard regs set or clobbered by
1908 insn. Data is a bitmap of the hardregs set so far. */
1910 static void
1911 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1913 bitmap regs_set = (bitmap) data;
1915 if (REG_P (x)
1916 && HARD_REGISTER_P (x))
1917 bitmap_set_range (regs_set, REGNO (x), REG_NREGS (x));
1920 /* Helper function for replace_read and record_store.
1921 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1922 to one before READ_END bytes read in READ_MODE. Return NULL
1923 if not successful. If REQUIRE_CST is true, return always constant. */
1925 static rtx
1926 get_stored_val (store_info_t store_info, machine_mode read_mode,
1927 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1928 basic_block bb, bool require_cst)
1930 machine_mode store_mode = GET_MODE (store_info->mem);
1931 int shift;
1932 int access_size; /* In bytes. */
1933 rtx read_reg;
1935 /* To get here the read is within the boundaries of the write so
1936 shift will never be negative. Start out with the shift being in
1937 bytes. */
1938 if (store_mode == BLKmode)
1939 shift = 0;
1940 else if (BYTES_BIG_ENDIAN)
1941 shift = store_info->end - read_end;
1942 else
1943 shift = read_begin - store_info->begin;
1945 access_size = shift + GET_MODE_SIZE (read_mode);
1947 /* From now on it is bits. */
1948 shift *= BITS_PER_UNIT;
1950 if (shift)
1951 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1952 optimize_bb_for_speed_p (bb),
1953 require_cst);
1954 else if (store_mode == BLKmode)
1956 /* The store is a memset (addr, const_val, const_size). */
1957 gcc_assert (CONST_INT_P (store_info->rhs));
1958 store_mode = int_mode_for_mode (read_mode);
1959 if (store_mode == BLKmode)
1960 read_reg = NULL_RTX;
1961 else if (store_info->rhs == const0_rtx)
1962 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1963 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1964 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1965 read_reg = NULL_RTX;
1966 else
1968 unsigned HOST_WIDE_INT c
1969 = INTVAL (store_info->rhs)
1970 & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
1971 int shift = BITS_PER_UNIT;
1972 while (shift < HOST_BITS_PER_WIDE_INT)
1974 c |= (c << shift);
1975 shift <<= 1;
1977 read_reg = gen_int_mode (c, store_mode);
1978 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1981 else if (store_info->const_rhs
1982 && (require_cst
1983 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1984 read_reg = extract_low_bits (read_mode, store_mode,
1985 copy_rtx (store_info->const_rhs));
1986 else
1987 read_reg = extract_low_bits (read_mode, store_mode,
1988 copy_rtx (store_info->rhs));
1989 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1990 read_reg = NULL_RTX;
1991 return read_reg;
1994 /* Take a sequence of:
1995 A <- r1
1997 ... <- A
1999 and change it into
2000 r2 <- r1
2001 A <- r1
2003 ... <- r2
2007 r3 <- extract (r1)
2008 r3 <- r3 >> shift
2009 r2 <- extract (r3)
2010 ... <- r2
2014 r2 <- extract (r1)
2015 ... <- r2
2017 Depending on the alignment and the mode of the store and
2018 subsequent load.
2021 The STORE_INFO and STORE_INSN are for the store and READ_INFO
2022 and READ_INSN are for the read. Return true if the replacement
2023 went ok. */
2025 static bool
2026 replace_read (store_info_t store_info, insn_info_t store_insn,
2027 read_info_t read_info, insn_info_t read_insn, rtx *loc,
2028 bitmap regs_live)
2030 machine_mode store_mode = GET_MODE (store_info->mem);
2031 machine_mode read_mode = GET_MODE (read_info->mem);
2032 rtx_insn *insns, *this_insn;
2033 rtx read_reg;
2034 basic_block bb;
2036 if (!dbg_cnt (dse))
2037 return false;
2039 /* Create a sequence of instructions to set up the read register.
2040 This sequence goes immediately before the store and its result
2041 is read by the load.
2043 We need to keep this in perspective. We are replacing a read
2044 with a sequence of insns, but the read will almost certainly be
2045 in cache, so it is not going to be an expensive one. Thus, we
2046 are not willing to do a multi insn shift or worse a subroutine
2047 call to get rid of the read. */
2048 if (dump_file && (dump_flags & TDF_DETAILS))
2049 fprintf (dump_file, "trying to replace %smode load in insn %d"
2050 " from %smode store in insn %d\n",
2051 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
2052 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
2053 start_sequence ();
2054 bb = BLOCK_FOR_INSN (read_insn->insn);
2055 read_reg = get_stored_val (store_info,
2056 read_mode, read_info->begin, read_info->end,
2057 bb, false);
2058 if (read_reg == NULL_RTX)
2060 end_sequence ();
2061 if (dump_file && (dump_flags & TDF_DETAILS))
2062 fprintf (dump_file, " -- could not extract bits of stored value\n");
2063 return false;
2065 /* Force the value into a new register so that it won't be clobbered
2066 between the store and the load. */
2067 read_reg = copy_to_mode_reg (read_mode, read_reg);
2068 insns = get_insns ();
2069 end_sequence ();
2071 if (insns != NULL_RTX)
2073 /* Now we have to scan the set of new instructions to see if the
2074 sequence contains and sets of hardregs that happened to be
2075 live at this point. For instance, this can happen if one of
2076 the insns sets the CC and the CC happened to be live at that
2077 point. This does occasionally happen, see PR 37922. */
2078 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
2080 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
2081 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
2083 bitmap_and_into (regs_set, regs_live);
2084 if (!bitmap_empty_p (regs_set))
2086 if (dump_file && (dump_flags & TDF_DETAILS))
2088 fprintf (dump_file,
2089 "abandoning replacement because sequence clobbers live hardregs:");
2090 df_print_regset (dump_file, regs_set);
2093 BITMAP_FREE (regs_set);
2094 return false;
2096 BITMAP_FREE (regs_set);
2099 if (validate_change (read_insn->insn, loc, read_reg, 0))
2101 deferred_change_t change = new deferred_change;
2103 /* Insert this right before the store insn where it will be safe
2104 from later insns that might change it before the read. */
2105 emit_insn_before (insns, store_insn->insn);
2107 /* And now for the kludge part: cselib croaks if you just
2108 return at this point. There are two reasons for this:
2110 1) Cselib has an idea of how many pseudos there are and
2111 that does not include the new ones we just added.
2113 2) Cselib does not know about the move insn we added
2114 above the store_info, and there is no way to tell it
2115 about it, because it has "moved on".
2117 Problem (1) is fixable with a certain amount of engineering.
2118 Problem (2) is requires starting the bb from scratch. This
2119 could be expensive.
2121 So we are just going to have to lie. The move/extraction
2122 insns are not really an issue, cselib did not see them. But
2123 the use of the new pseudo read_insn is a real problem because
2124 cselib has not scanned this insn. The way that we solve this
2125 problem is that we are just going to put the mem back for now
2126 and when we are finished with the block, we undo this. We
2127 keep a table of mems to get rid of. At the end of the basic
2128 block we can put them back. */
2130 *loc = read_info->mem;
2131 change->next = deferred_change_list;
2132 deferred_change_list = change;
2133 change->loc = loc;
2134 change->reg = read_reg;
2136 /* Get rid of the read_info, from the point of view of the
2137 rest of dse, play like this read never happened. */
2138 read_insn->read_rec = read_info->next;
2139 delete read_info;
2140 if (dump_file && (dump_flags & TDF_DETAILS))
2142 fprintf (dump_file, " -- replaced the loaded MEM with ");
2143 print_simple_rtl (dump_file, read_reg);
2144 fprintf (dump_file, "\n");
2146 return true;
2148 else
2150 if (dump_file && (dump_flags & TDF_DETAILS))
2152 fprintf (dump_file, " -- replacing the loaded MEM with ");
2153 print_simple_rtl (dump_file, read_reg);
2154 fprintf (dump_file, " led to an invalid instruction\n");
2156 return false;
2160 /* Check the address of MEM *LOC and kill any appropriate stores that may
2161 be active. */
2163 static void
2164 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
2166 rtx mem = *loc, mem_addr;
2167 insn_info_t insn_info;
2168 HOST_WIDE_INT offset = 0;
2169 HOST_WIDE_INT width = 0;
2170 alias_set_type spill_alias_set = 0;
2171 cselib_val *base = NULL;
2172 int group_id;
2173 read_info_t read_info;
2175 insn_info = bb_info->last_insn;
2177 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2178 || (MEM_VOLATILE_P (mem)))
2180 if (dump_file && (dump_flags & TDF_DETAILS))
2181 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
2182 add_wild_read (bb_info);
2183 insn_info->cannot_delete = true;
2184 return;
2187 /* If it is reading readonly mem, then there can be no conflict with
2188 another write. */
2189 if (MEM_READONLY_P (mem))
2190 return;
2192 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
2194 if (dump_file && (dump_flags & TDF_DETAILS))
2195 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2196 add_wild_read (bb_info);
2197 return;
2200 if (GET_MODE (mem) == BLKmode)
2201 width = -1;
2202 else
2203 width = GET_MODE_SIZE (GET_MODE (mem));
2205 read_info = new read_info_type;
2206 read_info->group_id = group_id;
2207 read_info->mem = mem;
2208 read_info->alias_set = spill_alias_set;
2209 read_info->begin = offset;
2210 read_info->end = offset + width;
2211 read_info->next = insn_info->read_rec;
2212 insn_info->read_rec = read_info;
2213 /* For alias_set != 0 canon_true_dependence should be never called. */
2214 if (spill_alias_set)
2215 mem_addr = NULL_RTX;
2216 else
2218 if (group_id < 0)
2219 mem_addr = base->val_rtx;
2220 else
2222 group_info_t group
2223 = rtx_group_vec[group_id];
2224 mem_addr = group->canon_base_addr;
2226 /* get_addr can only handle VALUE but cannot handle expr like:
2227 VALUE + OFFSET, so call get_addr to get original addr for
2228 mem_addr before plus_constant. */
2229 mem_addr = get_addr (mem_addr);
2230 if (offset)
2231 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2234 /* We ignore the clobbers in store_info. The is mildly aggressive,
2235 but there really should not be a clobber followed by a read. */
2237 if (spill_alias_set)
2239 insn_info_t i_ptr = active_local_stores;
2240 insn_info_t last = NULL;
2242 if (dump_file && (dump_flags & TDF_DETAILS))
2243 fprintf (dump_file, " processing spill load %d\n",
2244 (int) spill_alias_set);
2246 while (i_ptr)
2248 store_info_t store_info = i_ptr->store_rec;
2250 /* Skip the clobbers. */
2251 while (!store_info->is_set)
2252 store_info = store_info->next;
2254 if (store_info->alias_set == spill_alias_set)
2256 if (dump_file && (dump_flags & TDF_DETAILS))
2257 dump_insn_info ("removing from active", i_ptr);
2259 active_local_stores_len--;
2260 if (last)
2261 last->next_local_store = i_ptr->next_local_store;
2262 else
2263 active_local_stores = i_ptr->next_local_store;
2265 else
2266 last = i_ptr;
2267 i_ptr = i_ptr->next_local_store;
2270 else if (group_id >= 0)
2272 /* This is the restricted case where the base is a constant or
2273 the frame pointer and offset is a constant. */
2274 insn_info_t i_ptr = active_local_stores;
2275 insn_info_t last = NULL;
2277 if (dump_file && (dump_flags & TDF_DETAILS))
2279 if (width == -1)
2280 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2281 group_id);
2282 else
2283 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2284 group_id, (int)offset, (int)(offset+width));
2287 while (i_ptr)
2289 bool remove = false;
2290 store_info_t store_info = i_ptr->store_rec;
2292 /* Skip the clobbers. */
2293 while (!store_info->is_set)
2294 store_info = store_info->next;
2296 /* There are three cases here. */
2297 if (store_info->group_id < 0)
2298 /* We have a cselib store followed by a read from a
2299 const base. */
2300 remove
2301 = canon_true_dependence (store_info->mem,
2302 GET_MODE (store_info->mem),
2303 store_info->mem_addr,
2304 mem, mem_addr);
2306 else if (group_id == store_info->group_id)
2308 /* This is a block mode load. We may get lucky and
2309 canon_true_dependence may save the day. */
2310 if (width == -1)
2311 remove
2312 = canon_true_dependence (store_info->mem,
2313 GET_MODE (store_info->mem),
2314 store_info->mem_addr,
2315 mem, mem_addr);
2317 /* If this read is just reading back something that we just
2318 stored, rewrite the read. */
2319 else
2321 if (store_info->rhs
2322 && offset >= store_info->begin
2323 && offset + width <= store_info->end
2324 && all_positions_needed_p (store_info,
2325 offset - store_info->begin,
2326 width)
2327 && replace_read (store_info, i_ptr, read_info,
2328 insn_info, loc, bb_info->regs_live))
2329 return;
2331 /* The bases are the same, just see if the offsets
2332 overlap. */
2333 if ((offset < store_info->end)
2334 && (offset + width > store_info->begin))
2335 remove = true;
2339 /* else
2340 The else case that is missing here is that the
2341 bases are constant but different. There is nothing
2342 to do here because there is no overlap. */
2344 if (remove)
2346 if (dump_file && (dump_flags & TDF_DETAILS))
2347 dump_insn_info ("removing from active", i_ptr);
2349 active_local_stores_len--;
2350 if (last)
2351 last->next_local_store = i_ptr->next_local_store;
2352 else
2353 active_local_stores = i_ptr->next_local_store;
2355 else
2356 last = i_ptr;
2357 i_ptr = i_ptr->next_local_store;
2360 else
2362 insn_info_t i_ptr = active_local_stores;
2363 insn_info_t last = NULL;
2364 if (dump_file && (dump_flags & TDF_DETAILS))
2366 fprintf (dump_file, " processing cselib load mem:");
2367 print_inline_rtx (dump_file, mem, 0);
2368 fprintf (dump_file, "\n");
2371 while (i_ptr)
2373 bool remove = false;
2374 store_info_t store_info = i_ptr->store_rec;
2376 if (dump_file && (dump_flags & TDF_DETAILS))
2377 fprintf (dump_file, " processing cselib load against insn %d\n",
2378 INSN_UID (i_ptr->insn));
2380 /* Skip the clobbers. */
2381 while (!store_info->is_set)
2382 store_info = store_info->next;
2384 /* If this read is just reading back something that we just
2385 stored, rewrite the read. */
2386 if (store_info->rhs
2387 && store_info->group_id == -1
2388 && store_info->cse_base == base
2389 && width != -1
2390 && offset >= store_info->begin
2391 && offset + width <= store_info->end
2392 && all_positions_needed_p (store_info,
2393 offset - store_info->begin, width)
2394 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2395 bb_info->regs_live))
2396 return;
2398 if (!store_info->alias_set)
2399 remove = canon_true_dependence (store_info->mem,
2400 GET_MODE (store_info->mem),
2401 store_info->mem_addr,
2402 mem, mem_addr);
2404 if (remove)
2406 if (dump_file && (dump_flags & TDF_DETAILS))
2407 dump_insn_info ("removing from active", i_ptr);
2409 active_local_stores_len--;
2410 if (last)
2411 last->next_local_store = i_ptr->next_local_store;
2412 else
2413 active_local_stores = i_ptr->next_local_store;
2415 else
2416 last = i_ptr;
2417 i_ptr = i_ptr->next_local_store;
2422 /* A note_uses callback in which DATA points the INSN_INFO for
2423 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2424 true for any part of *LOC. */
2426 static void
2427 check_mem_read_use (rtx *loc, void *data)
2429 subrtx_ptr_iterator::array_type array;
2430 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2432 rtx *loc = *iter;
2433 if (MEM_P (*loc))
2434 check_mem_read_rtx (loc, (bb_info_t) data);
2439 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2440 So far it only handles arguments passed in registers. */
2442 static bool
2443 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2445 CUMULATIVE_ARGS args_so_far_v;
2446 cumulative_args_t args_so_far;
2447 tree arg;
2448 int idx;
2450 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2451 args_so_far = pack_cumulative_args (&args_so_far_v);
2453 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2454 for (idx = 0;
2455 arg != void_list_node && idx < nargs;
2456 arg = TREE_CHAIN (arg), idx++)
2458 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2459 rtx reg, link, tmp;
2460 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2461 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2462 || GET_MODE_CLASS (mode) != MODE_INT)
2463 return false;
2465 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2466 link;
2467 link = XEXP (link, 1))
2468 if (GET_CODE (XEXP (link, 0)) == USE)
2470 args[idx] = XEXP (XEXP (link, 0), 0);
2471 if (REG_P (args[idx])
2472 && REGNO (args[idx]) == REGNO (reg)
2473 && (GET_MODE (args[idx]) == mode
2474 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2475 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2476 <= UNITS_PER_WORD)
2477 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2478 > GET_MODE_SIZE (mode)))))
2479 break;
2481 if (!link)
2482 return false;
2484 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2485 if (GET_MODE (args[idx]) != mode)
2487 if (!tmp || !CONST_INT_P (tmp))
2488 return false;
2489 tmp = gen_int_mode (INTVAL (tmp), mode);
2491 if (tmp)
2492 args[idx] = tmp;
2494 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2496 if (arg != void_list_node || idx != nargs)
2497 return false;
2498 return true;
2501 /* Return a bitmap of the fixed registers contained in IN. */
2503 static bitmap
2504 copy_fixed_regs (const_bitmap in)
2506 bitmap ret;
2508 ret = ALLOC_REG_SET (NULL);
2509 bitmap_and (ret, in, fixed_reg_set_regset);
2510 return ret;
2513 /* Apply record_store to all candidate stores in INSN. Mark INSN
2514 if some part of it is not a candidate store and assigns to a
2515 non-register target. */
2517 static void
2518 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2520 rtx body;
2521 insn_info_type *insn_info = new insn_info_type;
2522 int mems_found = 0;
2523 memset (insn_info, 0, sizeof (struct insn_info_type));
2525 if (dump_file && (dump_flags & TDF_DETAILS))
2526 fprintf (dump_file, "\n**scanning insn=%d\n",
2527 INSN_UID (insn));
2529 insn_info->prev_insn = bb_info->last_insn;
2530 insn_info->insn = insn;
2531 bb_info->last_insn = insn_info;
2533 if (DEBUG_INSN_P (insn))
2535 insn_info->cannot_delete = true;
2536 return;
2539 /* Look at all of the uses in the insn. */
2540 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2542 if (CALL_P (insn))
2544 bool const_call;
2545 tree memset_call = NULL_TREE;
2547 insn_info->cannot_delete = true;
2549 /* Const functions cannot do anything bad i.e. read memory,
2550 however, they can read their parameters which may have
2551 been pushed onto the stack.
2552 memset and bzero don't read memory either. */
2553 const_call = RTL_CONST_CALL_P (insn);
2554 if (!const_call)
2556 rtx call = get_call_rtx_from (insn);
2557 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
2559 rtx symbol = XEXP (XEXP (call, 0), 0);
2560 if (SYMBOL_REF_DECL (symbol)
2561 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
2563 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
2564 == BUILT_IN_NORMAL
2565 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
2566 == BUILT_IN_MEMSET))
2567 || SYMBOL_REF_DECL (symbol) == block_clear_fn)
2568 memset_call = SYMBOL_REF_DECL (symbol);
2572 if (const_call || memset_call)
2574 insn_info_t i_ptr = active_local_stores;
2575 insn_info_t last = NULL;
2577 if (dump_file && (dump_flags & TDF_DETAILS))
2578 fprintf (dump_file, "%s call %d\n",
2579 const_call ? "const" : "memset", INSN_UID (insn));
2581 /* See the head comment of the frame_read field. */
2582 if (reload_completed
2583 /* Tail calls are storing their arguments using
2584 arg pointer. If it is a frame pointer on the target,
2585 even before reload we need to kill frame pointer based
2586 stores. */
2587 || (SIBLING_CALL_P (insn)
2588 && HARD_FRAME_POINTER_IS_ARG_POINTER))
2589 insn_info->frame_read = true;
2591 /* Loop over the active stores and remove those which are
2592 killed by the const function call. */
2593 while (i_ptr)
2595 bool remove_store = false;
2597 /* The stack pointer based stores are always killed. */
2598 if (i_ptr->stack_pointer_based)
2599 remove_store = true;
2601 /* If the frame is read, the frame related stores are killed. */
2602 else if (insn_info->frame_read)
2604 store_info_t store_info = i_ptr->store_rec;
2606 /* Skip the clobbers. */
2607 while (!store_info->is_set)
2608 store_info = store_info->next;
2610 if (store_info->group_id >= 0
2611 && rtx_group_vec[store_info->group_id]->frame_related)
2612 remove_store = true;
2615 if (remove_store)
2617 if (dump_file && (dump_flags & TDF_DETAILS))
2618 dump_insn_info ("removing from active", i_ptr);
2620 active_local_stores_len--;
2621 if (last)
2622 last->next_local_store = i_ptr->next_local_store;
2623 else
2624 active_local_stores = i_ptr->next_local_store;
2626 else
2627 last = i_ptr;
2629 i_ptr = i_ptr->next_local_store;
2632 if (memset_call)
2634 rtx args[3];
2635 if (get_call_args (insn, memset_call, args, 3)
2636 && CONST_INT_P (args[1])
2637 && CONST_INT_P (args[2])
2638 && INTVAL (args[2]) > 0)
2640 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2641 set_mem_size (mem, INTVAL (args[2]));
2642 body = gen_rtx_SET (mem, args[1]);
2643 mems_found += record_store (body, bb_info);
2644 if (dump_file && (dump_flags & TDF_DETAILS))
2645 fprintf (dump_file, "handling memset as BLKmode store\n");
2646 if (mems_found == 1)
2648 if (active_local_stores_len++
2649 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2651 active_local_stores_len = 1;
2652 active_local_stores = NULL;
2654 insn_info->fixed_regs_live
2655 = copy_fixed_regs (bb_info->regs_live);
2656 insn_info->next_local_store = active_local_stores;
2657 active_local_stores = insn_info;
2662 else if (SIBLING_CALL_P (insn) && reload_completed)
2663 /* Arguments for a sibling call that are pushed to memory are passed
2664 using the incoming argument pointer of the current function. After
2665 reload that might be (and likely is) frame pointer based. */
2666 add_wild_read (bb_info);
2667 else
2668 /* Every other call, including pure functions, may read any memory
2669 that is not relative to the frame. */
2670 add_non_frame_wild_read (bb_info);
2672 return;
2675 /* Assuming that there are sets in these insns, we cannot delete
2676 them. */
2677 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2678 || volatile_refs_p (PATTERN (insn))
2679 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2680 || (RTX_FRAME_RELATED_P (insn))
2681 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2682 insn_info->cannot_delete = true;
2684 body = PATTERN (insn);
2685 if (GET_CODE (body) == PARALLEL)
2687 int i;
2688 for (i = 0; i < XVECLEN (body, 0); i++)
2689 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2691 else
2692 mems_found += record_store (body, bb_info);
2694 if (dump_file && (dump_flags & TDF_DETAILS))
2695 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2696 mems_found, insn_info->cannot_delete ? "true" : "false");
2698 /* If we found some sets of mems, add it into the active_local_stores so
2699 that it can be locally deleted if found dead or used for
2700 replace_read and redundant constant store elimination. Otherwise mark
2701 it as cannot delete. This simplifies the processing later. */
2702 if (mems_found == 1)
2704 if (active_local_stores_len++
2705 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2707 active_local_stores_len = 1;
2708 active_local_stores = NULL;
2710 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2711 insn_info->next_local_store = active_local_stores;
2712 active_local_stores = insn_info;
2714 else
2715 insn_info->cannot_delete = true;
2719 /* Remove BASE from the set of active_local_stores. This is a
2720 callback from cselib that is used to get rid of the stores in
2721 active_local_stores. */
2723 static void
2724 remove_useless_values (cselib_val *base)
2726 insn_info_t insn_info = active_local_stores;
2727 insn_info_t last = NULL;
2729 while (insn_info)
2731 store_info_t store_info = insn_info->store_rec;
2732 bool del = false;
2734 /* If ANY of the store_infos match the cselib group that is
2735 being deleted, then the insn can not be deleted. */
2736 while (store_info)
2738 if ((store_info->group_id == -1)
2739 && (store_info->cse_base == base))
2741 del = true;
2742 break;
2744 store_info = store_info->next;
2747 if (del)
2749 active_local_stores_len--;
2750 if (last)
2751 last->next_local_store = insn_info->next_local_store;
2752 else
2753 active_local_stores = insn_info->next_local_store;
2754 free_store_info (insn_info);
2756 else
2757 last = insn_info;
2759 insn_info = insn_info->next_local_store;
2764 /* Do all of step 1. */
2766 static void
2767 dse_step1 (void)
2769 basic_block bb;
2770 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2772 cselib_init (0);
2773 all_blocks = BITMAP_ALLOC (NULL);
2774 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2775 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2777 FOR_ALL_BB_FN (bb, cfun)
2779 insn_info_t ptr;
2780 bb_info_t bb_info = new dse_bb_info_type;
2782 memset (bb_info, 0, sizeof (dse_bb_info_type));
2783 bitmap_set_bit (all_blocks, bb->index);
2784 bb_info->regs_live = regs_live;
2786 bitmap_copy (regs_live, DF_LR_IN (bb));
2787 df_simulate_initialize_forwards (bb, regs_live);
2789 bb_table[bb->index] = bb_info;
2790 cselib_discard_hook = remove_useless_values;
2792 if (bb->index >= NUM_FIXED_BLOCKS)
2794 rtx_insn *insn;
2796 active_local_stores = NULL;
2797 active_local_stores_len = 0;
2798 cselib_clear_table ();
2800 /* Scan the insns. */
2801 FOR_BB_INSNS (bb, insn)
2803 if (INSN_P (insn))
2804 scan_insn (bb_info, insn);
2805 cselib_process_insn (insn);
2806 if (INSN_P (insn))
2807 df_simulate_one_insn_forwards (bb, insn, regs_live);
2810 /* This is something of a hack, because the global algorithm
2811 is supposed to take care of the case where stores go dead
2812 at the end of the function. However, the global
2813 algorithm must take a more conservative view of block
2814 mode reads than the local alg does. So to get the case
2815 where you have a store to the frame followed by a non
2816 overlapping block more read, we look at the active local
2817 stores at the end of the function and delete all of the
2818 frame and spill based ones. */
2819 if (stores_off_frame_dead_at_return
2820 && (EDGE_COUNT (bb->succs) == 0
2821 || (single_succ_p (bb)
2822 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2823 && ! crtl->calls_eh_return)))
2825 insn_info_t i_ptr = active_local_stores;
2826 while (i_ptr)
2828 store_info_t store_info = i_ptr->store_rec;
2830 /* Skip the clobbers. */
2831 while (!store_info->is_set)
2832 store_info = store_info->next;
2833 if (store_info->alias_set && !i_ptr->cannot_delete)
2834 delete_dead_store_insn (i_ptr);
2835 else
2836 if (store_info->group_id >= 0)
2838 group_info_t group
2839 = rtx_group_vec[store_info->group_id];
2840 if (group->frame_related && !i_ptr->cannot_delete)
2841 delete_dead_store_insn (i_ptr);
2844 i_ptr = i_ptr->next_local_store;
2848 /* Get rid of the loads that were discovered in
2849 replace_read. Cselib is finished with this block. */
2850 while (deferred_change_list)
2852 deferred_change_t next = deferred_change_list->next;
2854 /* There is no reason to validate this change. That was
2855 done earlier. */
2856 *deferred_change_list->loc = deferred_change_list->reg;
2857 delete deferred_change_list;
2858 deferred_change_list = next;
2861 /* Get rid of all of the cselib based store_infos in this
2862 block and mark the containing insns as not being
2863 deletable. */
2864 ptr = bb_info->last_insn;
2865 while (ptr)
2867 if (ptr->contains_cselib_groups)
2869 store_info_t s_info = ptr->store_rec;
2870 while (s_info && !s_info->is_set)
2871 s_info = s_info->next;
2872 if (s_info
2873 && s_info->redundant_reason
2874 && s_info->redundant_reason->insn
2875 && !ptr->cannot_delete)
2877 if (dump_file && (dump_flags & TDF_DETAILS))
2878 fprintf (dump_file, "Locally deleting insn %d "
2879 "because insn %d stores the "
2880 "same value and couldn't be "
2881 "eliminated\n",
2882 INSN_UID (ptr->insn),
2883 INSN_UID (s_info->redundant_reason->insn));
2884 delete_dead_store_insn (ptr);
2886 free_store_info (ptr);
2888 else
2890 store_info_t s_info;
2892 /* Free at least positions_needed bitmaps. */
2893 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2894 if (s_info->is_large)
2896 BITMAP_FREE (s_info->positions_needed.large.bmap);
2897 s_info->is_large = false;
2900 ptr = ptr->prev_insn;
2903 cse_store_info_pool.release ();
2905 bb_info->regs_live = NULL;
2908 BITMAP_FREE (regs_live);
2909 cselib_finish ();
2910 rtx_group_table->empty ();
2914 /*----------------------------------------------------------------------------
2915 Second step.
2917 Assign each byte position in the stores that we are going to
2918 analyze globally to a position in the bitmaps. Returns true if
2919 there are any bit positions assigned.
2920 ----------------------------------------------------------------------------*/
2922 static void
2923 dse_step2_init (void)
2925 unsigned int i;
2926 group_info_t group;
2928 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2930 /* For all non stack related bases, we only consider a store to
2931 be deletable if there are two or more stores for that
2932 position. This is because it takes one store to make the
2933 other store redundant. However, for the stores that are
2934 stack related, we consider them if there is only one store
2935 for the position. We do this because the stack related
2936 stores can be deleted if their is no read between them and
2937 the end of the function.
2939 To make this work in the current framework, we take the stack
2940 related bases add all of the bits from store1 into store2.
2941 This has the effect of making the eligible even if there is
2942 only one store. */
2944 if (stores_off_frame_dead_at_return && group->frame_related)
2946 bitmap_ior_into (group->store2_n, group->store1_n);
2947 bitmap_ior_into (group->store2_p, group->store1_p);
2948 if (dump_file && (dump_flags & TDF_DETAILS))
2949 fprintf (dump_file, "group %d is frame related ", i);
2952 group->offset_map_size_n++;
2953 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2954 group->offset_map_size_n);
2955 group->offset_map_size_p++;
2956 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2957 group->offset_map_size_p);
2958 group->process_globally = false;
2959 if (dump_file && (dump_flags & TDF_DETAILS))
2961 fprintf (dump_file, "group %d(%d+%d): ", i,
2962 (int)bitmap_count_bits (group->store2_n),
2963 (int)bitmap_count_bits (group->store2_p));
2964 bitmap_print (dump_file, group->store2_n, "n ", " ");
2965 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2971 /* Init the offset tables for the normal case. */
2973 static bool
2974 dse_step2_nospill (void)
2976 unsigned int i;
2977 group_info_t group;
2978 /* Position 0 is unused because 0 is used in the maps to mean
2979 unused. */
2980 current_position = 1;
2981 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2983 bitmap_iterator bi;
2984 unsigned int j;
2986 if (group == clear_alias_group)
2987 continue;
2989 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2990 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2991 bitmap_clear (group->group_kill);
2993 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2995 bitmap_set_bit (group->group_kill, current_position);
2996 if (bitmap_bit_p (group->escaped_n, j))
2997 bitmap_set_bit (kill_on_calls, current_position);
2998 group->offset_map_n[j] = current_position++;
2999 group->process_globally = true;
3001 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
3003 bitmap_set_bit (group->group_kill, current_position);
3004 if (bitmap_bit_p (group->escaped_p, j))
3005 bitmap_set_bit (kill_on_calls, current_position);
3006 group->offset_map_p[j] = current_position++;
3007 group->process_globally = true;
3010 return current_position != 1;
3015 /*----------------------------------------------------------------------------
3016 Third step.
3018 Build the bit vectors for the transfer functions.
3019 ----------------------------------------------------------------------------*/
3022 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
3023 there, return 0. */
3025 static int
3026 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
3028 if (offset < 0)
3030 HOST_WIDE_INT offset_p = -offset;
3031 if (offset_p >= group_info->offset_map_size_n)
3032 return 0;
3033 return group_info->offset_map_n[offset_p];
3035 else
3037 if (offset >= group_info->offset_map_size_p)
3038 return 0;
3039 return group_info->offset_map_p[offset];
3044 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3045 may be NULL. */
3047 static void
3048 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
3050 while (store_info)
3052 HOST_WIDE_INT i;
3053 group_info_t group_info
3054 = rtx_group_vec[store_info->group_id];
3055 if (group_info->process_globally)
3056 for (i = store_info->begin; i < store_info->end; i++)
3058 int index = get_bitmap_index (group_info, i);
3059 if (index != 0)
3061 bitmap_set_bit (gen, index);
3062 if (kill)
3063 bitmap_clear_bit (kill, index);
3066 store_info = store_info->next;
3071 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3072 may be NULL. */
3074 static void
3075 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
3077 while (store_info)
3079 if (store_info->alias_set)
3081 int index = get_bitmap_index (clear_alias_group,
3082 store_info->alias_set);
3083 if (index != 0)
3085 bitmap_set_bit (gen, index);
3086 if (kill)
3087 bitmap_clear_bit (kill, index);
3090 store_info = store_info->next;
3095 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3096 may be NULL. */
3098 static void
3099 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
3101 read_info_t read_info = insn_info->read_rec;
3102 int i;
3103 group_info_t group;
3105 /* If this insn reads the frame, kill all the frame related stores. */
3106 if (insn_info->frame_read)
3108 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3109 if (group->process_globally && group->frame_related)
3111 if (kill)
3112 bitmap_ior_into (kill, group->group_kill);
3113 bitmap_and_compl_into (gen, group->group_kill);
3116 if (insn_info->non_frame_wild_read)
3118 /* Kill all non-frame related stores. Kill all stores of variables that
3119 escape. */
3120 if (kill)
3121 bitmap_ior_into (kill, kill_on_calls);
3122 bitmap_and_compl_into (gen, kill_on_calls);
3123 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3124 if (group->process_globally && !group->frame_related)
3126 if (kill)
3127 bitmap_ior_into (kill, group->group_kill);
3128 bitmap_and_compl_into (gen, group->group_kill);
3131 while (read_info)
3133 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3135 if (group->process_globally)
3137 if (i == read_info->group_id)
3139 if (read_info->begin > read_info->end)
3141 /* Begin > end for block mode reads. */
3142 if (kill)
3143 bitmap_ior_into (kill, group->group_kill);
3144 bitmap_and_compl_into (gen, group->group_kill);
3146 else
3148 /* The groups are the same, just process the
3149 offsets. */
3150 HOST_WIDE_INT j;
3151 for (j = read_info->begin; j < read_info->end; j++)
3153 int index = get_bitmap_index (group, j);
3154 if (index != 0)
3156 if (kill)
3157 bitmap_set_bit (kill, index);
3158 bitmap_clear_bit (gen, index);
3163 else
3165 /* The groups are different, if the alias sets
3166 conflict, clear the entire group. We only need
3167 to apply this test if the read_info is a cselib
3168 read. Anything with a constant base cannot alias
3169 something else with a different constant
3170 base. */
3171 if ((read_info->group_id < 0)
3172 && canon_true_dependence (group->base_mem,
3173 GET_MODE (group->base_mem),
3174 group->canon_base_addr,
3175 read_info->mem, NULL_RTX))
3177 if (kill)
3178 bitmap_ior_into (kill, group->group_kill);
3179 bitmap_and_compl_into (gen, group->group_kill);
3185 read_info = read_info->next;
3189 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3190 may be NULL. */
3192 static void
3193 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
3195 while (read_info)
3197 if (read_info->alias_set)
3199 int index = get_bitmap_index (clear_alias_group,
3200 read_info->alias_set);
3201 if (index != 0)
3203 if (kill)
3204 bitmap_set_bit (kill, index);
3205 bitmap_clear_bit (gen, index);
3209 read_info = read_info->next;
3214 /* Return the insn in BB_INFO before the first wild read or if there
3215 are no wild reads in the block, return the last insn. */
3217 static insn_info_t
3218 find_insn_before_first_wild_read (bb_info_t bb_info)
3220 insn_info_t insn_info = bb_info->last_insn;
3221 insn_info_t last_wild_read = NULL;
3223 while (insn_info)
3225 if (insn_info->wild_read)
3227 last_wild_read = insn_info->prev_insn;
3228 /* Block starts with wild read. */
3229 if (!last_wild_read)
3230 return NULL;
3233 insn_info = insn_info->prev_insn;
3236 if (last_wild_read)
3237 return last_wild_read;
3238 else
3239 return bb_info->last_insn;
3243 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3244 the block in order to build the gen and kill sets for the block.
3245 We start at ptr which may be the last insn in the block or may be
3246 the first insn with a wild read. In the latter case we are able to
3247 skip the rest of the block because it just does not matter:
3248 anything that happens is hidden by the wild read. */
3250 static void
3251 dse_step3_scan (bool for_spills, basic_block bb)
3253 bb_info_t bb_info = bb_table[bb->index];
3254 insn_info_t insn_info;
3256 if (for_spills)
3257 /* There are no wild reads in the spill case. */
3258 insn_info = bb_info->last_insn;
3259 else
3260 insn_info = find_insn_before_first_wild_read (bb_info);
3262 /* In the spill case or in the no_spill case if there is no wild
3263 read in the block, we will need a kill set. */
3264 if (insn_info == bb_info->last_insn)
3266 if (bb_info->kill)
3267 bitmap_clear (bb_info->kill);
3268 else
3269 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
3271 else
3272 if (bb_info->kill)
3273 BITMAP_FREE (bb_info->kill);
3275 while (insn_info)
3277 /* There may have been code deleted by the dce pass run before
3278 this phase. */
3279 if (insn_info->insn && INSN_P (insn_info->insn))
3281 /* Process the read(s) last. */
3282 if (for_spills)
3284 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3285 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
3287 else
3289 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3290 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
3294 insn_info = insn_info->prev_insn;
3299 /* Set the gen set of the exit block, and also any block with no
3300 successors that does not have a wild read. */
3302 static void
3303 dse_step3_exit_block_scan (bb_info_t bb_info)
3305 /* The gen set is all 0's for the exit block except for the
3306 frame_pointer_group. */
3308 if (stores_off_frame_dead_at_return)
3310 unsigned int i;
3311 group_info_t group;
3313 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3315 if (group->process_globally && group->frame_related)
3316 bitmap_ior_into (bb_info->gen, group->group_kill);
3322 /* Find all of the blocks that are not backwards reachable from the
3323 exit block or any block with no successors (BB). These are the
3324 infinite loops or infinite self loops. These blocks will still
3325 have their bits set in UNREACHABLE_BLOCKS. */
3327 static void
3328 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3330 edge e;
3331 edge_iterator ei;
3333 if (bitmap_bit_p (unreachable_blocks, bb->index))
3335 bitmap_clear_bit (unreachable_blocks, bb->index);
3336 FOR_EACH_EDGE (e, ei, bb->preds)
3338 mark_reachable_blocks (unreachable_blocks, e->src);
3343 /* Build the transfer functions for the function. */
3345 static void
3346 dse_step3 (bool for_spills)
3348 basic_block bb;
3349 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
3350 sbitmap_iterator sbi;
3351 bitmap all_ones = NULL;
3352 unsigned int i;
3354 bitmap_ones (unreachable_blocks);
3356 FOR_ALL_BB_FN (bb, cfun)
3358 bb_info_t bb_info = bb_table[bb->index];
3359 if (bb_info->gen)
3360 bitmap_clear (bb_info->gen);
3361 else
3362 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3364 if (bb->index == ENTRY_BLOCK)
3366 else if (bb->index == EXIT_BLOCK)
3367 dse_step3_exit_block_scan (bb_info);
3368 else
3369 dse_step3_scan (for_spills, bb);
3370 if (EDGE_COUNT (bb->succs) == 0)
3371 mark_reachable_blocks (unreachable_blocks, bb);
3373 /* If this is the second time dataflow is run, delete the old
3374 sets. */
3375 if (bb_info->in)
3376 BITMAP_FREE (bb_info->in);
3377 if (bb_info->out)
3378 BITMAP_FREE (bb_info->out);
3381 /* For any block in an infinite loop, we must initialize the out set
3382 to all ones. This could be expensive, but almost never occurs in
3383 practice. However, it is common in regression tests. */
3384 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3386 if (bitmap_bit_p (all_blocks, i))
3388 bb_info_t bb_info = bb_table[i];
3389 if (!all_ones)
3391 unsigned int j;
3392 group_info_t group;
3394 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3395 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3396 bitmap_ior_into (all_ones, group->group_kill);
3398 if (!bb_info->out)
3400 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3401 bitmap_copy (bb_info->out, all_ones);
3406 if (all_ones)
3407 BITMAP_FREE (all_ones);
3408 sbitmap_free (unreachable_blocks);
3413 /*----------------------------------------------------------------------------
3414 Fourth step.
3416 Solve the bitvector equations.
3417 ----------------------------------------------------------------------------*/
3420 /* Confluence function for blocks with no successors. Create an out
3421 set from the gen set of the exit block. This block logically has
3422 the exit block as a successor. */
3426 static void
3427 dse_confluence_0 (basic_block bb)
3429 bb_info_t bb_info = bb_table[bb->index];
3431 if (bb->index == EXIT_BLOCK)
3432 return;
3434 if (!bb_info->out)
3436 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3437 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3441 /* Propagate the information from the in set of the dest of E to the
3442 out set of the src of E. If the various in or out sets are not
3443 there, that means they are all ones. */
3445 static bool
3446 dse_confluence_n (edge e)
3448 bb_info_t src_info = bb_table[e->src->index];
3449 bb_info_t dest_info = bb_table[e->dest->index];
3451 if (dest_info->in)
3453 if (src_info->out)
3454 bitmap_and_into (src_info->out, dest_info->in);
3455 else
3457 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3458 bitmap_copy (src_info->out, dest_info->in);
3461 return true;
3465 /* Propagate the info from the out to the in set of BB_INDEX's basic
3466 block. There are three cases:
3468 1) The block has no kill set. In this case the kill set is all
3469 ones. It does not matter what the out set of the block is, none of
3470 the info can reach the top. The only thing that reaches the top is
3471 the gen set and we just copy the set.
3473 2) There is a kill set but no out set and bb has successors. In
3474 this case we just return. Eventually an out set will be created and
3475 it is better to wait than to create a set of ones.
3477 3) There is both a kill and out set. We apply the obvious transfer
3478 function.
3481 static bool
3482 dse_transfer_function (int bb_index)
3484 bb_info_t bb_info = bb_table[bb_index];
3486 if (bb_info->kill)
3488 if (bb_info->out)
3490 /* Case 3 above. */
3491 if (bb_info->in)
3492 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3493 bb_info->out, bb_info->kill);
3494 else
3496 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3497 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3498 bb_info->out, bb_info->kill);
3499 return true;
3502 else
3503 /* Case 2 above. */
3504 return false;
3506 else
3508 /* Case 1 above. If there is already an in set, nothing
3509 happens. */
3510 if (bb_info->in)
3511 return false;
3512 else
3514 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3515 bitmap_copy (bb_info->in, bb_info->gen);
3516 return true;
3521 /* Solve the dataflow equations. */
3523 static void
3524 dse_step4 (void)
3526 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3527 dse_confluence_n, dse_transfer_function,
3528 all_blocks, df_get_postorder (DF_BACKWARD),
3529 df_get_n_blocks (DF_BACKWARD));
3530 if (dump_file && (dump_flags & TDF_DETAILS))
3532 basic_block bb;
3534 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3535 FOR_ALL_BB_FN (bb, cfun)
3537 bb_info_t bb_info = bb_table[bb->index];
3539 df_print_bb_index (bb, dump_file);
3540 if (bb_info->in)
3541 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3542 else
3543 fprintf (dump_file, " in: *MISSING*\n");
3544 if (bb_info->gen)
3545 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3546 else
3547 fprintf (dump_file, " gen: *MISSING*\n");
3548 if (bb_info->kill)
3549 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3550 else
3551 fprintf (dump_file, " kill: *MISSING*\n");
3552 if (bb_info->out)
3553 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3554 else
3555 fprintf (dump_file, " out: *MISSING*\n\n");
3562 /*----------------------------------------------------------------------------
3563 Fifth step.
3565 Delete the stores that can only be deleted using the global information.
3566 ----------------------------------------------------------------------------*/
3569 static void
3570 dse_step5_nospill (void)
3572 basic_block bb;
3573 FOR_EACH_BB_FN (bb, cfun)
3575 bb_info_t bb_info = bb_table[bb->index];
3576 insn_info_t insn_info = bb_info->last_insn;
3577 bitmap v = bb_info->out;
3579 while (insn_info)
3581 bool deleted = false;
3582 if (dump_file && insn_info->insn)
3584 fprintf (dump_file, "starting to process insn %d\n",
3585 INSN_UID (insn_info->insn));
3586 bitmap_print (dump_file, v, " v: ", "\n");
3589 /* There may have been code deleted by the dce pass run before
3590 this phase. */
3591 if (insn_info->insn
3592 && INSN_P (insn_info->insn)
3593 && (!insn_info->cannot_delete)
3594 && (!bitmap_empty_p (v)))
3596 store_info_t store_info = insn_info->store_rec;
3598 /* Try to delete the current insn. */
3599 deleted = true;
3601 /* Skip the clobbers. */
3602 while (!store_info->is_set)
3603 store_info = store_info->next;
3605 if (store_info->alias_set)
3606 deleted = false;
3607 else
3609 HOST_WIDE_INT i;
3610 group_info_t group_info
3611 = rtx_group_vec[store_info->group_id];
3613 for (i = store_info->begin; i < store_info->end; i++)
3615 int index = get_bitmap_index (group_info, i);
3617 if (dump_file && (dump_flags & TDF_DETAILS))
3618 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3619 if (index == 0 || !bitmap_bit_p (v, index))
3621 if (dump_file && (dump_flags & TDF_DETAILS))
3622 fprintf (dump_file, "failing at i = %d\n", (int)i);
3623 deleted = false;
3624 break;
3628 if (deleted)
3630 if (dbg_cnt (dse)
3631 && check_for_inc_dec_1 (insn_info))
3633 delete_insn (insn_info->insn);
3634 insn_info->insn = NULL;
3635 globally_deleted++;
3639 /* We do want to process the local info if the insn was
3640 deleted. For instance, if the insn did a wild read, we
3641 no longer need to trash the info. */
3642 if (insn_info->insn
3643 && INSN_P (insn_info->insn)
3644 && (!deleted))
3646 scan_stores_nospill (insn_info->store_rec, v, NULL);
3647 if (insn_info->wild_read)
3649 if (dump_file && (dump_flags & TDF_DETAILS))
3650 fprintf (dump_file, "wild read\n");
3651 bitmap_clear (v);
3653 else if (insn_info->read_rec
3654 || insn_info->non_frame_wild_read)
3656 if (dump_file && !insn_info->non_frame_wild_read)
3657 fprintf (dump_file, "regular read\n");
3658 else if (dump_file && (dump_flags & TDF_DETAILS))
3659 fprintf (dump_file, "non-frame wild read\n");
3660 scan_reads_nospill (insn_info, v, NULL);
3664 insn_info = insn_info->prev_insn;
3671 /*----------------------------------------------------------------------------
3672 Sixth step.
3674 Delete stores made redundant by earlier stores (which store the same
3675 value) that couldn't be eliminated.
3676 ----------------------------------------------------------------------------*/
3678 static void
3679 dse_step6 (void)
3681 basic_block bb;
3683 FOR_ALL_BB_FN (bb, cfun)
3685 bb_info_t bb_info = bb_table[bb->index];
3686 insn_info_t insn_info = bb_info->last_insn;
3688 while (insn_info)
3690 /* There may have been code deleted by the dce pass run before
3691 this phase. */
3692 if (insn_info->insn
3693 && INSN_P (insn_info->insn)
3694 && !insn_info->cannot_delete)
3696 store_info_t s_info = insn_info->store_rec;
3698 while (s_info && !s_info->is_set)
3699 s_info = s_info->next;
3700 if (s_info
3701 && s_info->redundant_reason
3702 && s_info->redundant_reason->insn
3703 && INSN_P (s_info->redundant_reason->insn))
3705 rtx_insn *rinsn = s_info->redundant_reason->insn;
3706 if (dump_file && (dump_flags & TDF_DETAILS))
3707 fprintf (dump_file, "Locally deleting insn %d "
3708 "because insn %d stores the "
3709 "same value and couldn't be "
3710 "eliminated\n",
3711 INSN_UID (insn_info->insn),
3712 INSN_UID (rinsn));
3713 delete_dead_store_insn (insn_info);
3716 insn_info = insn_info->prev_insn;
3721 /*----------------------------------------------------------------------------
3722 Seventh step.
3724 Destroy everything left standing.
3725 ----------------------------------------------------------------------------*/
3727 static void
3728 dse_step7 (void)
3730 bitmap_obstack_release (&dse_bitmap_obstack);
3731 obstack_free (&dse_obstack, NULL);
3733 end_alias_analysis ();
3734 free (bb_table);
3735 delete rtx_group_table;
3736 rtx_group_table = NULL;
3737 rtx_group_vec.release ();
3738 BITMAP_FREE (all_blocks);
3739 BITMAP_FREE (scratch);
3741 rtx_store_info_pool.release ();
3742 read_info_type::pool.release ();
3743 insn_info_type::pool.release ();
3744 dse_bb_info_type::pool.release ();
3745 group_info::pool.release ();
3746 deferred_change::pool.release ();
3750 /* -------------------------------------------------------------------------
3752 ------------------------------------------------------------------------- */
3754 /* Callback for running pass_rtl_dse. */
3756 static unsigned int
3757 rest_of_handle_dse (void)
3759 df_set_flags (DF_DEFER_INSN_RESCAN);
3761 /* Need the notes since we must track live hardregs in the forwards
3762 direction. */
3763 df_note_add_problem ();
3764 df_analyze ();
3766 dse_step0 ();
3767 dse_step1 ();
3768 dse_step2_init ();
3769 if (dse_step2_nospill ())
3771 df_set_flags (DF_LR_RUN_DCE);
3772 df_analyze ();
3773 if (dump_file && (dump_flags & TDF_DETAILS))
3774 fprintf (dump_file, "doing global processing\n");
3775 dse_step3 (false);
3776 dse_step4 ();
3777 dse_step5_nospill ();
3780 dse_step6 ();
3781 dse_step7 ();
3783 if (dump_file)
3784 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3785 locally_deleted, globally_deleted, spill_deleted);
3787 /* DSE can eliminate potentially-trapping MEMs.
3788 Remove any EH edges associated with them. */
3789 if ((locally_deleted || globally_deleted)
3790 && cfun->can_throw_non_call_exceptions
3791 && purge_all_dead_edges ())
3792 cleanup_cfg (0);
3794 return 0;
3797 namespace {
3799 const pass_data pass_data_rtl_dse1 =
3801 RTL_PASS, /* type */
3802 "dse1", /* name */
3803 OPTGROUP_NONE, /* optinfo_flags */
3804 TV_DSE1, /* tv_id */
3805 0, /* properties_required */
3806 0, /* properties_provided */
3807 0, /* properties_destroyed */
3808 0, /* todo_flags_start */
3809 TODO_df_finish, /* todo_flags_finish */
3812 class pass_rtl_dse1 : public rtl_opt_pass
3814 public:
3815 pass_rtl_dse1 (gcc::context *ctxt)
3816 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3819 /* opt_pass methods: */
3820 virtual bool gate (function *)
3822 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3825 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3827 }; // class pass_rtl_dse1
3829 } // anon namespace
3831 rtl_opt_pass *
3832 make_pass_rtl_dse1 (gcc::context *ctxt)
3834 return new pass_rtl_dse1 (ctxt);
3837 namespace {
3839 const pass_data pass_data_rtl_dse2 =
3841 RTL_PASS, /* type */
3842 "dse2", /* name */
3843 OPTGROUP_NONE, /* optinfo_flags */
3844 TV_DSE2, /* tv_id */
3845 0, /* properties_required */
3846 0, /* properties_provided */
3847 0, /* properties_destroyed */
3848 0, /* todo_flags_start */
3849 TODO_df_finish, /* todo_flags_finish */
3852 class pass_rtl_dse2 : public rtl_opt_pass
3854 public:
3855 pass_rtl_dse2 (gcc::context *ctxt)
3856 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3859 /* opt_pass methods: */
3860 virtual bool gate (function *)
3862 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3865 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3867 }; // class pass_rtl_dse2
3869 } // anon namespace
3871 rtl_opt_pass *
3872 make_pass_rtl_dse2 (gcc::context *ctxt)
3874 return new pass_rtl_dse2 (ctxt);