PR 66861 Fix null pointer crash on mingw.
[official-gcc.git] / gcc / dse.c
blob339fb2df2736303158464c3f907ae953c805a77d
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 "predict.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "rtl.h"
33 #include "df.h"
34 #include "alias.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "tm_p.h"
38 #include "regs.h"
39 #include "regset.h"
40 #include "flags.h"
41 #include "cfgrtl.h"
42 #include "cselib.h"
43 #include "tree-pass.h"
44 #include "alloc-pool.h"
45 #include "insn-config.h"
46 #include "expmed.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "calls.h"
50 #include "emit-rtl.h"
51 #include "varasm.h"
52 #include "stmt.h"
53 #include "expr.h"
54 #include "recog.h"
55 #include "insn-codes.h"
56 #include "optabs.h"
57 #include "dbgcnt.h"
58 #include "target.h"
59 #include "params.h"
60 #include "internal-fn.h"
61 #include "gimple-ssa.h"
62 #include "rtl-iter.h"
63 #include "cfgcleanup.h"
65 /* This file contains three techniques for performing Dead Store
66 Elimination (dse).
68 * The first technique performs dse locally on any base address. It
69 is based on the cselib which is a local value numbering technique.
70 This technique is local to a basic block but deals with a fairly
71 general addresses.
73 * The second technique performs dse globally but is restricted to
74 base addresses that are either constant or are relative to the
75 frame_pointer.
77 * The third technique, (which is only done after register allocation)
78 processes the spill slots. This differs from the second
79 technique because it takes advantage of the fact that spilling is
80 completely free from the effects of aliasing.
82 Logically, dse is a backwards dataflow problem. A store can be
83 deleted if it if cannot be reached in the backward direction by any
84 use of the value being stored. However, the local technique uses a
85 forwards scan of the basic block because cselib requires that the
86 block be processed in that order.
88 The pass is logically broken into 7 steps:
90 0) Initialization.
92 1) The local algorithm, as well as scanning the insns for the two
93 global algorithms.
95 2) Analysis to see if the global algs are necessary. In the case
96 of stores base on a constant address, there must be at least two
97 stores to that address, to make it possible to delete some of the
98 stores. In the case of stores off of the frame or spill related
99 stores, only one store to an address is necessary because those
100 stores die at the end of the function.
102 3) Set up the global dataflow equations based on processing the
103 info parsed in the first step.
105 4) Solve the dataflow equations.
107 5) Delete the insns that the global analysis has indicated are
108 unnecessary.
110 6) Delete insns that store the same value as preceding store
111 where the earlier store couldn't be eliminated.
113 7) Cleanup.
115 This step uses cselib and canon_rtx to build the largest expression
116 possible for each address. This pass is a forwards pass through
117 each basic block. From the point of view of the global technique,
118 the first pass could examine a block in either direction. The
119 forwards ordering is to accommodate cselib.
121 We make a simplifying assumption: addresses fall into four broad
122 categories:
124 1) base has rtx_varies_p == false, offset is constant.
125 2) base has rtx_varies_p == false, offset variable.
126 3) base has rtx_varies_p == true, offset constant.
127 4) base has rtx_varies_p == true, offset variable.
129 The local passes are able to process all 4 kinds of addresses. The
130 global pass only handles 1).
132 The global problem is formulated as follows:
134 A store, S1, to address A, where A is not relative to the stack
135 frame, can be eliminated if all paths from S1 to the end of the
136 function contain another store to A before a read to A.
138 If the address A is relative to the stack frame, a store S2 to A
139 can be eliminated if there are no paths from S2 that reach the
140 end of the function that read A before another store to A. In
141 this case S2 can be deleted if there are paths from S2 to the
142 end of the function that have no reads or writes to A. This
143 second case allows stores to the stack frame to be deleted that
144 would otherwise die when the function returns. This cannot be
145 done if stores_off_frame_dead_at_return is not true. See the doc
146 for that variable for when this variable is false.
148 The global problem is formulated as a backwards set union
149 dataflow problem where the stores are the gens and reads are the
150 kills. Set union problems are rare and require some special
151 handling given our representation of bitmaps. A straightforward
152 implementation requires a lot of bitmaps filled with 1s.
153 These are expensive and cumbersome in our bitmap formulation so
154 care has been taken to avoid large vectors filled with 1s. See
155 the comments in bb_info and in the dataflow confluence functions
156 for details.
158 There are two places for further enhancements to this algorithm:
160 1) The original dse which was embedded in a pass called flow also
161 did local address forwarding. For example in
163 A <- r100
164 ... <- A
166 flow would replace the right hand side of the second insn with a
167 reference to r100. Most of the information is available to add this
168 to this pass. It has not done it because it is a lot of work in
169 the case that either r100 is assigned to between the first and
170 second insn and/or the second insn is a load of part of the value
171 stored by the first insn.
173 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
174 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
175 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
176 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
178 2) The cleaning up of spill code is quite profitable. It currently
179 depends on reading tea leaves and chicken entrails left by reload.
180 This pass depends on reload creating a singleton alias set for each
181 spill slot and telling the next dse pass which of these alias sets
182 are the singletons. Rather than analyze the addresses of the
183 spills, dse's spill processing just does analysis of the loads and
184 stores that use those alias sets. There are three cases where this
185 falls short:
187 a) Reload sometimes creates the slot for one mode of access, and
188 then inserts loads and/or stores for a smaller mode. In this
189 case, the current code just punts on the slot. The proper thing
190 to do is to back out and use one bit vector position for each
191 byte of the entity associated with the slot. This depends on
192 KNOWING that reload always generates the accesses for each of the
193 bytes in some canonical (read that easy to understand several
194 passes after reload happens) way.
196 b) Reload sometimes decides that spill slot it allocated was not
197 large enough for the mode and goes back and allocates more slots
198 with the same mode and alias set. The backout in this case is a
199 little more graceful than (a). In this case the slot is unmarked
200 as being a spill slot and if final address comes out to be based
201 off the frame pointer, the global algorithm handles this slot.
203 c) For any pass that may prespill, there is currently no
204 mechanism to tell the dse pass that the slot being used has the
205 special properties that reload uses. It may be that all that is
206 required is to have those passes make the same calls that reload
207 does, assuming that the alias sets can be manipulated in the same
208 way. */
210 /* There are limits to the size of constant offsets we model for the
211 global problem. There are certainly test cases, that exceed this
212 limit, however, it is unlikely that there are important programs
213 that really have constant offsets this size. */
214 #define MAX_OFFSET (64 * 1024)
216 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
217 on the default obstack because these bitmaps can grow quite large
218 (~2GB for the small (!) test case of PR54146) and we'll hold on to
219 all that memory until the end of the compiler run.
220 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
221 releasing the whole obstack. */
222 static bitmap_obstack dse_bitmap_obstack;
224 /* Obstack for other data. As for above: Kinda nice to be able to
225 throw it all away at the end in one big sweep. */
226 static struct obstack dse_obstack;
228 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
229 static bitmap scratch = NULL;
231 struct insn_info_type;
233 /* This structure holds information about a candidate store. */
234 struct store_info
237 /* False means this is a clobber. */
238 bool is_set;
240 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
241 bool is_large;
243 /* The id of the mem group of the base address. If rtx_varies_p is
244 true, this is -1. Otherwise, it is the index into the group
245 table. */
246 int group_id;
248 /* This is the cselib value. */
249 cselib_val *cse_base;
251 /* This canonized mem. */
252 rtx mem;
254 /* Canonized MEM address for use by canon_true_dependence. */
255 rtx mem_addr;
257 /* If this is non-zero, it is the alias set of a spill location. */
258 alias_set_type alias_set;
260 /* The offset of the first and byte before the last byte associated
261 with the operation. */
262 HOST_WIDE_INT begin, end;
264 union
266 /* A bitmask as wide as the number of bytes in the word that
267 contains a 1 if the byte may be needed. The store is unused if
268 all of the bits are 0. This is used if IS_LARGE is false. */
269 unsigned HOST_WIDE_INT small_bitmask;
271 struct
273 /* A bitmap with one bit per byte. Cleared bit means the position
274 is needed. Used if IS_LARGE is false. */
275 bitmap bmap;
277 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
278 equal to END - BEGIN, the whole store is unused. */
279 int count;
280 } large;
281 } positions_needed;
283 /* The next store info for this insn. */
284 struct store_info *next;
286 /* The right hand side of the store. This is used if there is a
287 subsequent reload of the mems address somewhere later in the
288 basic block. */
289 rtx rhs;
291 /* If rhs is or holds a constant, this contains that constant,
292 otherwise NULL. */
293 rtx const_rhs;
295 /* Set if this store stores the same constant value as REDUNDANT_REASON
296 insn stored. These aren't eliminated early, because doing that
297 might prevent the earlier larger store to be eliminated. */
298 struct insn_info_type *redundant_reason;
301 /* Return a bitmask with the first N low bits set. */
303 static unsigned HOST_WIDE_INT
304 lowpart_bitmask (int n)
306 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
307 return mask >> (HOST_BITS_PER_WIDE_INT - n);
310 typedef struct store_info *store_info_t;
311 static pool_allocator<store_info> cse_store_info_pool ("cse_store_info_pool",
312 100);
314 static pool_allocator<store_info> rtx_store_info_pool ("rtx_store_info_pool",
315 100);
317 /* This structure holds information about a load. These are only
318 built for rtx bases. */
319 struct read_info_type
321 /* The id of the mem group of the base address. */
322 int group_id;
324 /* If this is non-zero, it is the alias set of a spill location. */
325 alias_set_type alias_set;
327 /* The offset of the first and byte after the last byte associated
328 with the operation. If begin == end == 0, the read did not have
329 a constant offset. */
330 int begin, end;
332 /* The mem being read. */
333 rtx mem;
335 /* The next read_info for this insn. */
336 struct read_info_type *next;
338 /* Pool allocation new operator. */
339 inline void *operator new (size_t)
341 return pool.allocate ();
344 /* Delete operator utilizing pool allocation. */
345 inline void operator delete (void *ptr)
347 pool.remove ((read_info_type *) ptr);
350 /* Memory allocation pool. */
351 static pool_allocator<read_info_type> pool;
353 typedef struct read_info_type *read_info_t;
355 pool_allocator<read_info_type> read_info_type::pool ("read_info_pool", 100);
357 /* One of these records is created for each insn. */
359 struct insn_info_type
361 /* Set true if the insn contains a store but the insn itself cannot
362 be deleted. This is set if the insn is a parallel and there is
363 more than one non dead output or if the insn is in some way
364 volatile. */
365 bool cannot_delete;
367 /* This field is only used by the global algorithm. It is set true
368 if the insn contains any read of mem except for a (1). This is
369 also set if the insn is a call or has a clobber mem. If the insn
370 contains a wild read, the use_rec will be null. */
371 bool wild_read;
373 /* This is true only for CALL instructions which could potentially read
374 any non-frame memory location. This field is used by the global
375 algorithm. */
376 bool non_frame_wild_read;
378 /* This field is only used for the processing of const functions.
379 These functions cannot read memory, but they can read the stack
380 because that is where they may get their parms. We need to be
381 this conservative because, like the store motion pass, we don't
382 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
383 Moreover, we need to distinguish two cases:
384 1. Before reload (register elimination), the stores related to
385 outgoing arguments are stack pointer based and thus deemed
386 of non-constant base in this pass. This requires special
387 handling but also means that the frame pointer based stores
388 need not be killed upon encountering a const function call.
389 2. After reload, the stores related to outgoing arguments can be
390 either stack pointer or hard frame pointer based. This means
391 that we have no other choice than also killing all the frame
392 pointer based stores upon encountering a const function call.
393 This field is set after reload for const function calls and before
394 reload for const tail function calls on targets where arg pointer
395 is the frame pointer. Having this set is less severe than a wild
396 read, it just means that all the frame related stores are killed
397 rather than all the stores. */
398 bool frame_read;
400 /* This field is only used for the processing of const functions.
401 It is set if the insn may contain a stack pointer based store. */
402 bool stack_pointer_based;
404 /* This is true if any of the sets within the store contains a
405 cselib base. Such stores can only be deleted by the local
406 algorithm. */
407 bool contains_cselib_groups;
409 /* The insn. */
410 rtx_insn *insn;
412 /* The list of mem sets or mem clobbers that are contained in this
413 insn. If the insn is deletable, it contains only one mem set.
414 But it could also contain clobbers. Insns that contain more than
415 one mem set are not deletable, but each of those mems are here in
416 order to provide info to delete other insns. */
417 store_info_t store_rec;
419 /* The linked list of mem uses in this insn. Only the reads from
420 rtx bases are listed here. The reads to cselib bases are
421 completely processed during the first scan and so are never
422 created. */
423 read_info_t read_rec;
425 /* The live fixed registers. We assume only fixed registers can
426 cause trouble by being clobbered from an expanded pattern;
427 storing only the live fixed registers (rather than all registers)
428 means less memory needs to be allocated / copied for the individual
429 stores. */
430 regset fixed_regs_live;
432 /* The prev insn in the basic block. */
433 struct insn_info_type * prev_insn;
435 /* The linked list of insns that are in consideration for removal in
436 the forwards pass through the basic block. This pointer may be
437 trash as it is not cleared when a wild read occurs. The only
438 time it is guaranteed to be correct is when the traversal starts
439 at active_local_stores. */
440 struct insn_info_type * next_local_store;
442 /* Pool allocation new operator. */
443 inline void *operator new (size_t)
445 return pool.allocate ();
448 /* Delete operator utilizing pool allocation. */
449 inline void operator delete (void *ptr)
451 pool.remove ((insn_info_type *) ptr);
454 /* Memory allocation pool. */
455 static pool_allocator<insn_info_type> pool;
457 typedef struct insn_info_type *insn_info_t;
459 pool_allocator<insn_info_type> insn_info_type::pool ("insn_info_pool", 100);
461 /* The linked list of stores that are under consideration in this
462 basic block. */
463 static insn_info_t active_local_stores;
464 static int active_local_stores_len;
466 struct dse_bb_info_type
468 /* Pointer to the insn info for the last insn in the block. These
469 are linked so this is how all of the insns are reached. During
470 scanning this is the current insn being scanned. */
471 insn_info_t last_insn;
473 /* The info for the global dataflow problem. */
476 /* This is set if the transfer function should and in the wild_read
477 bitmap before applying the kill and gen sets. That vector knocks
478 out most of the bits in the bitmap and thus speeds up the
479 operations. */
480 bool apply_wild_read;
482 /* The following 4 bitvectors hold information about which positions
483 of which stores are live or dead. They are indexed by
484 get_bitmap_index. */
486 /* The set of store positions that exist in this block before a wild read. */
487 bitmap gen;
489 /* The set of load positions that exist in this block above the
490 same position of a store. */
491 bitmap kill;
493 /* The set of stores that reach the top of the block without being
494 killed by a read.
496 Do not represent the in if it is all ones. Note that this is
497 what the bitvector should logically be initialized to for a set
498 intersection problem. However, like the kill set, this is too
499 expensive. So initially, the in set will only be created for the
500 exit block and any block that contains a wild read. */
501 bitmap in;
503 /* The set of stores that reach the bottom of the block from it's
504 successors.
506 Do not represent the in if it is all ones. Note that this is
507 what the bitvector should logically be initialized to for a set
508 intersection problem. However, like the kill and in set, this is
509 too expensive. So what is done is that the confluence operator
510 just initializes the vector from one of the out sets of the
511 successors of the block. */
512 bitmap out;
514 /* The following bitvector is indexed by the reg number. It
515 contains the set of regs that are live at the current instruction
516 being processed. While it contains info for all of the
517 registers, only the hard registers are actually examined. It is used
518 to assure that shift and/or add sequences that are inserted do not
519 accidentally clobber live hard regs. */
520 bitmap regs_live;
522 /* Pool allocation new operator. */
523 inline void *operator new (size_t)
525 return pool.allocate ();
528 /* Delete operator utilizing pool allocation. */
529 inline void operator delete (void *ptr)
531 pool.remove ((dse_bb_info_type *) ptr);
534 /* Memory allocation pool. */
535 static pool_allocator<dse_bb_info_type> pool;
538 typedef struct dse_bb_info_type *bb_info_t;
539 pool_allocator<dse_bb_info_type> dse_bb_info_type::pool ("bb_info_pool", 100);
541 /* Table to hold all bb_infos. */
542 static bb_info_t *bb_table;
544 /* There is a group_info for each rtx base that is used to reference
545 memory. There are also not many of the rtx bases because they are
546 very limited in scope. */
548 struct group_info
550 /* The actual base of the address. */
551 rtx rtx_base;
553 /* The sequential id of the base. This allows us to have a
554 canonical ordering of these that is not based on addresses. */
555 int id;
557 /* True if there are any positions that are to be processed
558 globally. */
559 bool process_globally;
561 /* True if the base of this group is either the frame_pointer or
562 hard_frame_pointer. */
563 bool frame_related;
565 /* A mem wrapped around the base pointer for the group in order to do
566 read dependency. It must be given BLKmode in order to encompass all
567 the possible offsets from the base. */
568 rtx base_mem;
570 /* Canonized version of base_mem's address. */
571 rtx canon_base_addr;
573 /* These two sets of two bitmaps are used to keep track of how many
574 stores are actually referencing that position from this base. We
575 only do this for rtx bases as this will be used to assign
576 positions in the bitmaps for the global problem. Bit N is set in
577 store1 on the first store for offset N. Bit N is set in store2
578 for the second store to offset N. This is all we need since we
579 only care about offsets that have two or more stores for them.
581 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
582 for 0 and greater offsets.
584 There is one special case here, for stores into the stack frame,
585 we will or store1 into store2 before deciding which stores look
586 at globally. This is because stores to the stack frame that have
587 no other reads before the end of the function can also be
588 deleted. */
589 bitmap store1_n, store1_p, store2_n, store2_p;
591 /* These bitmaps keep track of offsets in this group escape this function.
592 An offset escapes if it corresponds to a named variable whose
593 addressable flag is set. */
594 bitmap escaped_n, escaped_p;
596 /* The positions in this bitmap have the same assignments as the in,
597 out, gen and kill bitmaps. This bitmap is all zeros except for
598 the positions that are occupied by stores for this group. */
599 bitmap group_kill;
601 /* The offset_map is used to map the offsets from this base into
602 positions in the global bitmaps. It is only created after all of
603 the all of stores have been scanned and we know which ones we
604 care about. */
605 int *offset_map_n, *offset_map_p;
606 int offset_map_size_n, offset_map_size_p;
608 /* Pool allocation new operator. */
609 inline void *operator new (size_t)
611 return pool.allocate ();
614 /* Delete operator utilizing pool allocation. */
615 inline void operator delete (void *ptr)
617 pool.remove ((group_info *) ptr);
620 /* Memory allocation pool. */
621 static pool_allocator<group_info> pool;
623 typedef struct group_info *group_info_t;
624 typedef const struct group_info *const_group_info_t;
626 pool_allocator<group_info> group_info::pool ("rtx_group_info_pool", 100);
628 /* Index into the rtx_group_vec. */
629 static int rtx_group_next_id;
632 static vec<group_info_t> rtx_group_vec;
635 /* This structure holds the set of changes that are being deferred
636 when removing read operation. See replace_read. */
637 struct deferred_change
640 /* The mem that is being replaced. */
641 rtx *loc;
643 /* The reg it is being replaced with. */
644 rtx reg;
646 struct deferred_change *next;
648 /* Pool allocation new operator. */
649 inline void *operator new (size_t)
651 return pool.allocate ();
654 /* Delete operator utilizing pool allocation. */
655 inline void operator delete (void *ptr)
657 pool.remove ((deferred_change *) ptr);
660 /* Memory allocation pool. */
661 static pool_allocator<deferred_change> pool;
664 typedef struct deferred_change *deferred_change_t;
666 pool_allocator<deferred_change> deferred_change::pool
667 ("deferred_change_pool", 10);
669 static deferred_change_t deferred_change_list = NULL;
671 /* The group that holds all of the clear_alias_sets. */
672 static group_info_t clear_alias_group;
674 /* The modes of the clear_alias_sets. */
675 static htab_t clear_alias_mode_table;
677 /* Hash table element to look up the mode for an alias set. */
678 struct clear_alias_mode_holder
680 alias_set_type alias_set;
681 machine_mode mode;
684 /* This is true except if cfun->stdarg -- i.e. we cannot do
685 this for vararg functions because they play games with the frame. */
686 static bool stores_off_frame_dead_at_return;
688 /* Counter for stats. */
689 static int globally_deleted;
690 static int locally_deleted;
691 static int spill_deleted;
693 static bitmap all_blocks;
695 /* Locations that are killed by calls in the global phase. */
696 static bitmap kill_on_calls;
698 /* The number of bits used in the global bitmaps. */
699 static unsigned int current_position;
701 /*----------------------------------------------------------------------------
702 Zeroth step.
704 Initialization.
705 ----------------------------------------------------------------------------*/
708 /* Find the entry associated with ALIAS_SET. */
710 static struct clear_alias_mode_holder *
711 clear_alias_set_lookup (alias_set_type alias_set)
713 struct clear_alias_mode_holder tmp_holder;
714 void **slot;
716 tmp_holder.alias_set = alias_set;
717 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
718 gcc_assert (*slot);
720 return (struct clear_alias_mode_holder *) *slot;
724 /* Hashtable callbacks for maintaining the "bases" field of
725 store_group_info, given that the addresses are function invariants. */
727 struct invariant_group_base_hasher : nofree_ptr_hash <group_info>
729 static inline hashval_t hash (const group_info *);
730 static inline bool equal (const group_info *, const group_info *);
733 inline bool
734 invariant_group_base_hasher::equal (const group_info *gi1,
735 const group_info *gi2)
737 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
740 inline hashval_t
741 invariant_group_base_hasher::hash (const group_info *gi)
743 int do_not_record;
744 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
747 /* Tables of group_info structures, hashed by base value. */
748 static hash_table<invariant_group_base_hasher> *rtx_group_table;
751 /* Get the GROUP for BASE. Add a new group if it is not there. */
753 static group_info_t
754 get_group_info (rtx base)
756 struct group_info tmp_gi;
757 group_info_t gi;
758 group_info **slot;
760 if (base)
762 /* Find the store_base_info structure for BASE, creating a new one
763 if necessary. */
764 tmp_gi.rtx_base = base;
765 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
766 gi = (group_info_t) *slot;
768 else
770 if (!clear_alias_group)
772 clear_alias_group = gi = new group_info;
773 memset (gi, 0, sizeof (struct group_info));
774 gi->id = rtx_group_next_id++;
775 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
776 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
777 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
778 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
779 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
780 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
781 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
782 gi->process_globally = false;
783 gi->offset_map_size_n = 0;
784 gi->offset_map_size_p = 0;
785 gi->offset_map_n = NULL;
786 gi->offset_map_p = NULL;
787 rtx_group_vec.safe_push (gi);
789 return clear_alias_group;
792 if (gi == NULL)
794 *slot = gi = new group_info;
795 gi->rtx_base = base;
796 gi->id = rtx_group_next_id++;
797 gi->base_mem = gen_rtx_MEM (BLKmode, base);
798 gi->canon_base_addr = canon_rtx (base);
799 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
800 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
801 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
802 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
803 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
804 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
805 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
806 gi->process_globally = false;
807 gi->frame_related =
808 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
809 gi->offset_map_size_n = 0;
810 gi->offset_map_size_p = 0;
811 gi->offset_map_n = NULL;
812 gi->offset_map_p = NULL;
813 rtx_group_vec.safe_push (gi);
816 return gi;
820 /* Initialization of data structures. */
822 static void
823 dse_step0 (void)
825 locally_deleted = 0;
826 globally_deleted = 0;
827 spill_deleted = 0;
829 bitmap_obstack_initialize (&dse_bitmap_obstack);
830 gcc_obstack_init (&dse_obstack);
832 scratch = BITMAP_ALLOC (&reg_obstack);
833 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
836 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
838 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
839 rtx_group_next_id = 0;
841 stores_off_frame_dead_at_return = !cfun->stdarg;
843 init_alias_analysis ();
845 clear_alias_group = NULL;
850 /*----------------------------------------------------------------------------
851 First step.
853 Scan all of the insns. Any random ordering of the blocks is fine.
854 Each block is scanned in forward order to accommodate cselib which
855 is used to remove stores with non-constant bases.
856 ----------------------------------------------------------------------------*/
858 /* Delete all of the store_info recs from INSN_INFO. */
860 static void
861 free_store_info (insn_info_t insn_info)
863 store_info_t store_info = insn_info->store_rec;
864 while (store_info)
866 store_info_t next = store_info->next;
867 if (store_info->is_large)
868 BITMAP_FREE (store_info->positions_needed.large.bmap);
869 if (store_info->cse_base)
870 cse_store_info_pool.remove (store_info);
871 else
872 rtx_store_info_pool.remove (store_info);
873 store_info = next;
876 insn_info->cannot_delete = true;
877 insn_info->contains_cselib_groups = false;
878 insn_info->store_rec = NULL;
881 typedef struct
883 rtx_insn *first, *current;
884 regset fixed_regs_live;
885 bool failure;
886 } note_add_store_info;
888 /* Callback for emit_inc_dec_insn_before via note_stores.
889 Check if a register is clobbered which is live afterwards. */
891 static void
892 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
894 rtx_insn *insn;
895 note_add_store_info *info = (note_add_store_info *) data;
897 if (!REG_P (loc))
898 return;
900 /* If this register is referenced by the current or an earlier insn,
901 that's OK. E.g. this applies to the register that is being incremented
902 with this addition. */
903 for (insn = info->first;
904 insn != NEXT_INSN (info->current);
905 insn = NEXT_INSN (insn))
906 if (reg_referenced_p (loc, PATTERN (insn)))
907 return;
909 /* If we come here, we have a clobber of a register that's only OK
910 if that register is not live. If we don't have liveness information
911 available, fail now. */
912 if (!info->fixed_regs_live)
914 info->failure = true;
915 return;
917 /* Now check if this is a live fixed register. */
918 unsigned int end_regno = END_REGNO (loc);
919 for (unsigned int regno = REGNO (loc); regno < end_regno; ++regno)
920 if (REGNO_REG_SET_P (info->fixed_regs_live, regno))
921 info->failure = true;
924 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
925 SRC + SRCOFF before insn ARG. */
927 static int
928 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
929 rtx op ATTRIBUTE_UNUSED,
930 rtx dest, rtx src, rtx srcoff, void *arg)
932 insn_info_t insn_info = (insn_info_t) arg;
933 rtx_insn *insn = insn_info->insn, *new_insn, *cur;
934 note_add_store_info info;
936 /* We can reuse all operands without copying, because we are about
937 to delete the insn that contained it. */
938 if (srcoff)
940 start_sequence ();
941 emit_insn (gen_add3_insn (dest, src, srcoff));
942 new_insn = get_insns ();
943 end_sequence ();
945 else
946 new_insn = gen_move_insn (dest, src);
947 info.first = new_insn;
948 info.fixed_regs_live = insn_info->fixed_regs_live;
949 info.failure = false;
950 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
952 info.current = cur;
953 note_stores (PATTERN (cur), note_add_store, &info);
956 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
957 return it immediately, communicating the failure to its caller. */
958 if (info.failure)
959 return 1;
961 emit_insn_before (new_insn, insn);
963 return 0;
966 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
967 is there, is split into a separate insn.
968 Return true on success (or if there was nothing to do), false on failure. */
970 static bool
971 check_for_inc_dec_1 (insn_info_t insn_info)
973 rtx_insn *insn = insn_info->insn;
974 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
975 if (note)
976 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
977 insn_info) == 0;
978 return true;
982 /* Entry point for postreload. If you work on reload_cse, or you need this
983 anywhere else, consider if you can provide register liveness information
984 and add a parameter to this function so that it can be passed down in
985 insn_info.fixed_regs_live. */
986 bool
987 check_for_inc_dec (rtx_insn *insn)
989 insn_info_type insn_info;
990 rtx note;
992 insn_info.insn = insn;
993 insn_info.fixed_regs_live = NULL;
994 note = find_reg_note (insn, REG_INC, NULL_RTX);
995 if (note)
996 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
997 &insn_info) == 0;
998 return true;
1001 /* Delete the insn and free all of the fields inside INSN_INFO. */
1003 static void
1004 delete_dead_store_insn (insn_info_t insn_info)
1006 read_info_t read_info;
1008 if (!dbg_cnt (dse))
1009 return;
1011 if (!check_for_inc_dec_1 (insn_info))
1012 return;
1013 if (dump_file && (dump_flags & TDF_DETAILS))
1015 fprintf (dump_file, "Locally deleting insn %d ",
1016 INSN_UID (insn_info->insn));
1017 if (insn_info->store_rec->alias_set)
1018 fprintf (dump_file, "alias set %d\n",
1019 (int) insn_info->store_rec->alias_set);
1020 else
1021 fprintf (dump_file, "\n");
1024 free_store_info (insn_info);
1025 read_info = insn_info->read_rec;
1027 while (read_info)
1029 read_info_t next = read_info->next;
1030 delete read_info;
1031 read_info = next;
1033 insn_info->read_rec = NULL;
1035 delete_insn (insn_info->insn);
1036 locally_deleted++;
1037 insn_info->insn = NULL;
1039 insn_info->wild_read = false;
1042 /* Return whether DECL, a local variable, can possibly escape the current
1043 function scope. */
1045 static bool
1046 local_variable_can_escape (tree decl)
1048 if (TREE_ADDRESSABLE (decl))
1049 return true;
1051 /* If this is a partitioned variable, we need to consider all the variables
1052 in the partition. This is necessary because a store into one of them can
1053 be replaced with a store into another and this may not change the outcome
1054 of the escape analysis. */
1055 if (cfun->gimple_df->decls_to_pointers != NULL)
1057 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
1058 if (namep)
1059 return TREE_ADDRESSABLE (*namep);
1062 return false;
1065 /* Return whether EXPR can possibly escape the current function scope. */
1067 static bool
1068 can_escape (tree expr)
1070 tree base;
1071 if (!expr)
1072 return true;
1073 base = get_base_address (expr);
1074 if (DECL_P (base)
1075 && !may_be_aliased (base)
1076 && !(TREE_CODE (base) == VAR_DECL
1077 && !DECL_EXTERNAL (base)
1078 && !TREE_STATIC (base)
1079 && local_variable_can_escape (base)))
1080 return false;
1081 return true;
1084 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1085 OFFSET and WIDTH. */
1087 static void
1088 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
1089 tree expr)
1091 HOST_WIDE_INT i;
1092 bool expr_escapes = can_escape (expr);
1093 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
1094 for (i=offset; i<offset+width; i++)
1096 bitmap store1;
1097 bitmap store2;
1098 bitmap escaped;
1099 int ai;
1100 if (i < 0)
1102 store1 = group->store1_n;
1103 store2 = group->store2_n;
1104 escaped = group->escaped_n;
1105 ai = -i;
1107 else
1109 store1 = group->store1_p;
1110 store2 = group->store2_p;
1111 escaped = group->escaped_p;
1112 ai = i;
1115 if (!bitmap_set_bit (store1, ai))
1116 bitmap_set_bit (store2, ai);
1117 else
1119 if (i < 0)
1121 if (group->offset_map_size_n < ai)
1122 group->offset_map_size_n = ai;
1124 else
1126 if (group->offset_map_size_p < ai)
1127 group->offset_map_size_p = ai;
1130 if (expr_escapes)
1131 bitmap_set_bit (escaped, ai);
1135 static void
1136 reset_active_stores (void)
1138 active_local_stores = NULL;
1139 active_local_stores_len = 0;
1142 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1144 static void
1145 free_read_records (bb_info_t bb_info)
1147 insn_info_t insn_info = bb_info->last_insn;
1148 read_info_t *ptr = &insn_info->read_rec;
1149 while (*ptr)
1151 read_info_t next = (*ptr)->next;
1152 if ((*ptr)->alias_set == 0)
1154 delete *ptr;
1155 *ptr = next;
1157 else
1158 ptr = &(*ptr)->next;
1162 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1164 static void
1165 add_wild_read (bb_info_t bb_info)
1167 insn_info_t insn_info = bb_info->last_insn;
1168 insn_info->wild_read = true;
1169 free_read_records (bb_info);
1170 reset_active_stores ();
1173 /* Set the BB_INFO so that the last insn is marked as a wild read of
1174 non-frame locations. */
1176 static void
1177 add_non_frame_wild_read (bb_info_t bb_info)
1179 insn_info_t insn_info = bb_info->last_insn;
1180 insn_info->non_frame_wild_read = true;
1181 free_read_records (bb_info);
1182 reset_active_stores ();
1185 /* Return true if X is a constant or one of the registers that behave
1186 as a constant over the life of a function. This is equivalent to
1187 !rtx_varies_p for memory addresses. */
1189 static bool
1190 const_or_frame_p (rtx x)
1192 if (CONSTANT_P (x))
1193 return true;
1195 if (GET_CODE (x) == REG)
1197 /* Note that we have to test for the actual rtx used for the frame
1198 and arg pointers and not just the register number in case we have
1199 eliminated the frame and/or arg pointer and are using it
1200 for pseudos. */
1201 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1202 /* The arg pointer varies if it is not a fixed register. */
1203 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1204 || x == pic_offset_table_rtx)
1205 return true;
1206 return false;
1209 return false;
1212 /* Take all reasonable action to put the address of MEM into the form
1213 that we can do analysis on.
1215 The gold standard is to get the address into the form: address +
1216 OFFSET where address is something that rtx_varies_p considers a
1217 constant. When we can get the address in this form, we can do
1218 global analysis on it. Note that for constant bases, address is
1219 not actually returned, only the group_id. The address can be
1220 obtained from that.
1222 If that fails, we try cselib to get a value we can at least use
1223 locally. If that fails we return false.
1225 The GROUP_ID is set to -1 for cselib bases and the index of the
1226 group for non_varying bases.
1228 FOR_READ is true if this is a mem read and false if not. */
1230 static bool
1231 canon_address (rtx mem,
1232 alias_set_type *alias_set_out,
1233 int *group_id,
1234 HOST_WIDE_INT *offset,
1235 cselib_val **base)
1237 machine_mode address_mode = get_address_mode (mem);
1238 rtx mem_address = XEXP (mem, 0);
1239 rtx expanded_address, address;
1240 int expanded;
1242 *alias_set_out = 0;
1244 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1246 if (dump_file && (dump_flags & TDF_DETAILS))
1248 fprintf (dump_file, " mem: ");
1249 print_inline_rtx (dump_file, mem_address, 0);
1250 fprintf (dump_file, "\n");
1253 /* First see if just canon_rtx (mem_address) is const or frame,
1254 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1255 address = NULL_RTX;
1256 for (expanded = 0; expanded < 2; expanded++)
1258 if (expanded)
1260 /* Use cselib to replace all of the reg references with the full
1261 expression. This will take care of the case where we have
1263 r_x = base + offset;
1264 val = *r_x;
1266 by making it into
1268 val = *(base + offset); */
1270 expanded_address = cselib_expand_value_rtx (mem_address,
1271 scratch, 5);
1273 /* If this fails, just go with the address from first
1274 iteration. */
1275 if (!expanded_address)
1276 break;
1278 else
1279 expanded_address = mem_address;
1281 /* Split the address into canonical BASE + OFFSET terms. */
1282 address = canon_rtx (expanded_address);
1284 *offset = 0;
1286 if (dump_file && (dump_flags & TDF_DETAILS))
1288 if (expanded)
1290 fprintf (dump_file, "\n after cselib_expand address: ");
1291 print_inline_rtx (dump_file, expanded_address, 0);
1292 fprintf (dump_file, "\n");
1295 fprintf (dump_file, "\n after canon_rtx address: ");
1296 print_inline_rtx (dump_file, address, 0);
1297 fprintf (dump_file, "\n");
1300 if (GET_CODE (address) == CONST)
1301 address = XEXP (address, 0);
1303 if (GET_CODE (address) == PLUS
1304 && CONST_INT_P (XEXP (address, 1)))
1306 *offset = INTVAL (XEXP (address, 1));
1307 address = XEXP (address, 0);
1310 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1311 && const_or_frame_p (address))
1313 group_info_t group = get_group_info (address);
1315 if (dump_file && (dump_flags & TDF_DETAILS))
1316 fprintf (dump_file, " gid=%d offset=%d \n",
1317 group->id, (int)*offset);
1318 *base = NULL;
1319 *group_id = group->id;
1320 return true;
1324 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1325 *group_id = -1;
1327 if (*base == NULL)
1329 if (dump_file && (dump_flags & TDF_DETAILS))
1330 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1331 return false;
1333 if (dump_file && (dump_flags & TDF_DETAILS))
1334 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1335 (*base)->uid, (*base)->hash, (int)*offset);
1336 return true;
1340 /* Clear the rhs field from the active_local_stores array. */
1342 static void
1343 clear_rhs_from_active_local_stores (void)
1345 insn_info_t ptr = active_local_stores;
1347 while (ptr)
1349 store_info_t store_info = ptr->store_rec;
1350 /* Skip the clobbers. */
1351 while (!store_info->is_set)
1352 store_info = store_info->next;
1354 store_info->rhs = NULL;
1355 store_info->const_rhs = NULL;
1357 ptr = ptr->next_local_store;
1362 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1364 static inline void
1365 set_position_unneeded (store_info_t s_info, int pos)
1367 if (__builtin_expect (s_info->is_large, false))
1369 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1370 s_info->positions_needed.large.count++;
1372 else
1373 s_info->positions_needed.small_bitmask
1374 &= ~(((unsigned HOST_WIDE_INT) 1) << pos);
1377 /* Mark the whole store S_INFO as unneeded. */
1379 static inline void
1380 set_all_positions_unneeded (store_info_t s_info)
1382 if (__builtin_expect (s_info->is_large, false))
1384 int pos, end = s_info->end - s_info->begin;
1385 for (pos = 0; pos < end; pos++)
1386 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1387 s_info->positions_needed.large.count = end;
1389 else
1390 s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0;
1393 /* Return TRUE if any bytes from S_INFO store are needed. */
1395 static inline bool
1396 any_positions_needed_p (store_info_t s_info)
1398 if (__builtin_expect (s_info->is_large, false))
1399 return (s_info->positions_needed.large.count
1400 < s_info->end - s_info->begin);
1401 else
1402 return (s_info->positions_needed.small_bitmask
1403 != (unsigned HOST_WIDE_INT) 0);
1406 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1407 store are needed. */
1409 static inline bool
1410 all_positions_needed_p (store_info_t s_info, int start, int width)
1412 if (__builtin_expect (s_info->is_large, false))
1414 int end = start + width;
1415 while (start < end)
1416 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1417 return false;
1418 return true;
1420 else
1422 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1423 return (s_info->positions_needed.small_bitmask & mask) == mask;
1428 static rtx get_stored_val (store_info_t, machine_mode, HOST_WIDE_INT,
1429 HOST_WIDE_INT, basic_block, bool);
1432 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1433 there is a candidate store, after adding it to the appropriate
1434 local store group if so. */
1436 static int
1437 record_store (rtx body, bb_info_t bb_info)
1439 rtx mem, rhs, const_rhs, mem_addr;
1440 HOST_WIDE_INT offset = 0;
1441 HOST_WIDE_INT width = 0;
1442 alias_set_type spill_alias_set;
1443 insn_info_t insn_info = bb_info->last_insn;
1444 store_info_t store_info = NULL;
1445 int group_id;
1446 cselib_val *base = NULL;
1447 insn_info_t ptr, last, redundant_reason;
1448 bool store_is_unused;
1450 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1451 return 0;
1453 mem = SET_DEST (body);
1455 /* If this is not used, then this cannot be used to keep the insn
1456 from being deleted. On the other hand, it does provide something
1457 that can be used to prove that another store is dead. */
1458 store_is_unused
1459 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1461 /* Check whether that value is a suitable memory location. */
1462 if (!MEM_P (mem))
1464 /* If the set or clobber is unused, then it does not effect our
1465 ability to get rid of the entire insn. */
1466 if (!store_is_unused)
1467 insn_info->cannot_delete = true;
1468 return 0;
1471 /* At this point we know mem is a mem. */
1472 if (GET_MODE (mem) == BLKmode)
1474 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1476 if (dump_file && (dump_flags & TDF_DETAILS))
1477 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1478 add_wild_read (bb_info);
1479 insn_info->cannot_delete = true;
1480 return 0;
1482 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1483 as memset (addr, 0, 36); */
1484 else if (!MEM_SIZE_KNOWN_P (mem)
1485 || MEM_SIZE (mem) <= 0
1486 || MEM_SIZE (mem) > MAX_OFFSET
1487 || GET_CODE (body) != SET
1488 || !CONST_INT_P (SET_SRC (body)))
1490 if (!store_is_unused)
1492 /* If the set or clobber is unused, then it does not effect our
1493 ability to get rid of the entire insn. */
1494 insn_info->cannot_delete = true;
1495 clear_rhs_from_active_local_stores ();
1497 return 0;
1501 /* We can still process a volatile mem, we just cannot delete it. */
1502 if (MEM_VOLATILE_P (mem))
1503 insn_info->cannot_delete = true;
1505 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1507 clear_rhs_from_active_local_stores ();
1508 return 0;
1511 if (GET_MODE (mem) == BLKmode)
1512 width = MEM_SIZE (mem);
1513 else
1514 width = GET_MODE_SIZE (GET_MODE (mem));
1516 if (spill_alias_set)
1518 bitmap store1 = clear_alias_group->store1_p;
1519 bitmap store2 = clear_alias_group->store2_p;
1521 gcc_assert (GET_MODE (mem) != BLKmode);
1523 if (!bitmap_set_bit (store1, spill_alias_set))
1524 bitmap_set_bit (store2, spill_alias_set);
1526 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1527 clear_alias_group->offset_map_size_p = spill_alias_set;
1529 store_info = rtx_store_info_pool.allocate ();
1531 if (dump_file && (dump_flags & TDF_DETAILS))
1532 fprintf (dump_file, " processing spill store %d(%s)\n",
1533 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1535 else if (group_id >= 0)
1537 /* In the restrictive case where the base is a constant or the
1538 frame pointer we can do global analysis. */
1540 group_info_t group
1541 = rtx_group_vec[group_id];
1542 tree expr = MEM_EXPR (mem);
1544 store_info = rtx_store_info_pool.allocate ();
1545 set_usage_bits (group, offset, width, expr);
1547 if (dump_file && (dump_flags & TDF_DETAILS))
1548 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1549 group_id, (int)offset, (int)(offset+width));
1551 else
1553 if (may_be_sp_based_p (XEXP (mem, 0)))
1554 insn_info->stack_pointer_based = true;
1555 insn_info->contains_cselib_groups = true;
1557 store_info = cse_store_info_pool.allocate ();
1558 group_id = -1;
1560 if (dump_file && (dump_flags & TDF_DETAILS))
1561 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1562 (int)offset, (int)(offset+width));
1565 const_rhs = rhs = NULL_RTX;
1566 if (GET_CODE (body) == SET
1567 /* No place to keep the value after ra. */
1568 && !reload_completed
1569 && (REG_P (SET_SRC (body))
1570 || GET_CODE (SET_SRC (body)) == SUBREG
1571 || CONSTANT_P (SET_SRC (body)))
1572 && !MEM_VOLATILE_P (mem)
1573 /* Sometimes the store and reload is used for truncation and
1574 rounding. */
1575 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1577 rhs = SET_SRC (body);
1578 if (CONSTANT_P (rhs))
1579 const_rhs = rhs;
1580 else if (body == PATTERN (insn_info->insn))
1582 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1583 if (tem && CONSTANT_P (XEXP (tem, 0)))
1584 const_rhs = XEXP (tem, 0);
1586 if (const_rhs == NULL_RTX && REG_P (rhs))
1588 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1590 if (tem && CONSTANT_P (tem))
1591 const_rhs = tem;
1595 /* Check to see if this stores causes some other stores to be
1596 dead. */
1597 ptr = active_local_stores;
1598 last = NULL;
1599 redundant_reason = NULL;
1600 mem = canon_rtx (mem);
1601 /* For alias_set != 0 canon_true_dependence should be never called. */
1602 if (spill_alias_set)
1603 mem_addr = NULL_RTX;
1604 else
1606 if (group_id < 0)
1607 mem_addr = base->val_rtx;
1608 else
1610 group_info_t group
1611 = rtx_group_vec[group_id];
1612 mem_addr = group->canon_base_addr;
1614 /* get_addr can only handle VALUE but cannot handle expr like:
1615 VALUE + OFFSET, so call get_addr to get original addr for
1616 mem_addr before plus_constant. */
1617 mem_addr = get_addr (mem_addr);
1618 if (offset)
1619 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1622 while (ptr)
1624 insn_info_t next = ptr->next_local_store;
1625 store_info_t s_info = ptr->store_rec;
1626 bool del = true;
1628 /* Skip the clobbers. We delete the active insn if this insn
1629 shadows the set. To have been put on the active list, it
1630 has exactly on set. */
1631 while (!s_info->is_set)
1632 s_info = s_info->next;
1634 if (s_info->alias_set != spill_alias_set)
1635 del = false;
1636 else if (s_info->alias_set)
1638 struct clear_alias_mode_holder *entry
1639 = clear_alias_set_lookup (s_info->alias_set);
1640 /* Generally, spills cannot be processed if and of the
1641 references to the slot have a different mode. But if
1642 we are in the same block and mode is exactly the same
1643 between this store and one before in the same block,
1644 we can still delete it. */
1645 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1646 && (GET_MODE (mem) == entry->mode))
1648 del = true;
1649 set_all_positions_unneeded (s_info);
1651 if (dump_file && (dump_flags & TDF_DETAILS))
1652 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1653 INSN_UID (ptr->insn), (int) s_info->alias_set);
1655 else if ((s_info->group_id == group_id)
1656 && (s_info->cse_base == base))
1658 HOST_WIDE_INT i;
1659 if (dump_file && (dump_flags & TDF_DETAILS))
1660 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1661 INSN_UID (ptr->insn), s_info->group_id,
1662 (int)s_info->begin, (int)s_info->end);
1664 /* Even if PTR won't be eliminated as unneeded, if both
1665 PTR and this insn store the same constant value, we might
1666 eliminate this insn instead. */
1667 if (s_info->const_rhs
1668 && const_rhs
1669 && offset >= s_info->begin
1670 && offset + width <= s_info->end
1671 && all_positions_needed_p (s_info, offset - s_info->begin,
1672 width))
1674 if (GET_MODE (mem) == BLKmode)
1676 if (GET_MODE (s_info->mem) == BLKmode
1677 && s_info->const_rhs == const_rhs)
1678 redundant_reason = ptr;
1680 else if (s_info->const_rhs == const0_rtx
1681 && const_rhs == const0_rtx)
1682 redundant_reason = ptr;
1683 else
1685 rtx val;
1686 start_sequence ();
1687 val = get_stored_val (s_info, GET_MODE (mem),
1688 offset, offset + width,
1689 BLOCK_FOR_INSN (insn_info->insn),
1690 true);
1691 if (get_insns () != NULL)
1692 val = NULL_RTX;
1693 end_sequence ();
1694 if (val && rtx_equal_p (val, const_rhs))
1695 redundant_reason = ptr;
1699 for (i = MAX (offset, s_info->begin);
1700 i < offset + width && i < s_info->end;
1701 i++)
1702 set_position_unneeded (s_info, i - s_info->begin);
1704 else if (s_info->rhs)
1705 /* Need to see if it is possible for this store to overwrite
1706 the value of store_info. If it is, set the rhs to NULL to
1707 keep it from being used to remove a load. */
1709 if (canon_true_dependence (s_info->mem,
1710 GET_MODE (s_info->mem),
1711 s_info->mem_addr,
1712 mem, mem_addr))
1714 s_info->rhs = NULL;
1715 s_info->const_rhs = NULL;
1719 /* An insn can be deleted if every position of every one of
1720 its s_infos is zero. */
1721 if (any_positions_needed_p (s_info))
1722 del = false;
1724 if (del)
1726 insn_info_t insn_to_delete = ptr;
1728 active_local_stores_len--;
1729 if (last)
1730 last->next_local_store = ptr->next_local_store;
1731 else
1732 active_local_stores = ptr->next_local_store;
1734 if (!insn_to_delete->cannot_delete)
1735 delete_dead_store_insn (insn_to_delete);
1737 else
1738 last = ptr;
1740 ptr = next;
1743 /* Finish filling in the store_info. */
1744 store_info->next = insn_info->store_rec;
1745 insn_info->store_rec = store_info;
1746 store_info->mem = mem;
1747 store_info->alias_set = spill_alias_set;
1748 store_info->mem_addr = mem_addr;
1749 store_info->cse_base = base;
1750 if (width > HOST_BITS_PER_WIDE_INT)
1752 store_info->is_large = true;
1753 store_info->positions_needed.large.count = 0;
1754 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1756 else
1758 store_info->is_large = false;
1759 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1761 store_info->group_id = group_id;
1762 store_info->begin = offset;
1763 store_info->end = offset + width;
1764 store_info->is_set = GET_CODE (body) == SET;
1765 store_info->rhs = rhs;
1766 store_info->const_rhs = const_rhs;
1767 store_info->redundant_reason = redundant_reason;
1769 /* If this is a clobber, we return 0. We will only be able to
1770 delete this insn if there is only one store USED store, but we
1771 can use the clobber to delete other stores earlier. */
1772 return store_info->is_set ? 1 : 0;
1776 static void
1777 dump_insn_info (const char * start, insn_info_t insn_info)
1779 fprintf (dump_file, "%s insn=%d %s\n", start,
1780 INSN_UID (insn_info->insn),
1781 insn_info->store_rec ? "has store" : "naked");
1785 /* If the modes are different and the value's source and target do not
1786 line up, we need to extract the value from lower part of the rhs of
1787 the store, shift it, and then put it into a form that can be shoved
1788 into the read_insn. This function generates a right SHIFT of a
1789 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1790 shift sequence is returned or NULL if we failed to find a
1791 shift. */
1793 static rtx
1794 find_shift_sequence (int access_size,
1795 store_info_t store_info,
1796 machine_mode read_mode,
1797 int shift, bool speed, bool require_cst)
1799 machine_mode store_mode = GET_MODE (store_info->mem);
1800 machine_mode new_mode;
1801 rtx read_reg = NULL;
1803 /* Some machines like the x86 have shift insns for each size of
1804 operand. Other machines like the ppc or the ia-64 may only have
1805 shift insns that shift values within 32 or 64 bit registers.
1806 This loop tries to find the smallest shift insn that will right
1807 justify the value we want to read but is available in one insn on
1808 the machine. */
1810 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1811 MODE_INT);
1812 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1813 new_mode = GET_MODE_WIDER_MODE (new_mode))
1815 rtx target, new_reg, new_lhs;
1816 rtx_insn *shift_seq, *insn;
1817 int cost;
1819 /* If a constant was stored into memory, try to simplify it here,
1820 otherwise the cost of the shift might preclude this optimization
1821 e.g. at -Os, even when no actual shift will be needed. */
1822 if (store_info->const_rhs)
1824 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1825 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1826 store_mode, byte);
1827 if (ret && CONSTANT_P (ret))
1829 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1830 ret, GEN_INT (shift));
1831 if (ret && CONSTANT_P (ret))
1833 byte = subreg_lowpart_offset (read_mode, new_mode);
1834 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1835 if (ret && CONSTANT_P (ret)
1836 && (set_src_cost (ret, read_mode, speed)
1837 <= COSTS_N_INSNS (1)))
1838 return ret;
1843 if (require_cst)
1844 return NULL_RTX;
1846 /* Try a wider mode if truncating the store mode to NEW_MODE
1847 requires a real instruction. */
1848 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1849 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1850 continue;
1852 /* Also try a wider mode if the necessary punning is either not
1853 desirable or not possible. */
1854 if (!CONSTANT_P (store_info->rhs)
1855 && !MODES_TIEABLE_P (new_mode, store_mode))
1856 continue;
1858 new_reg = gen_reg_rtx (new_mode);
1860 start_sequence ();
1862 /* In theory we could also check for an ashr. Ian Taylor knows
1863 of one dsp where the cost of these two was not the same. But
1864 this really is a rare case anyway. */
1865 target = expand_binop (new_mode, lshr_optab, new_reg,
1866 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1868 shift_seq = get_insns ();
1869 end_sequence ();
1871 if (target != new_reg || shift_seq == NULL)
1872 continue;
1874 cost = 0;
1875 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1876 if (INSN_P (insn))
1877 cost += insn_rtx_cost (PATTERN (insn), speed);
1879 /* The computation up to here is essentially independent
1880 of the arguments and could be precomputed. It may
1881 not be worth doing so. We could precompute if
1882 worthwhile or at least cache the results. The result
1883 technically depends on both SHIFT and ACCESS_SIZE,
1884 but in practice the answer will depend only on ACCESS_SIZE. */
1886 if (cost > COSTS_N_INSNS (1))
1887 continue;
1889 new_lhs = extract_low_bits (new_mode, store_mode,
1890 copy_rtx (store_info->rhs));
1891 if (new_lhs == NULL_RTX)
1892 continue;
1894 /* We found an acceptable shift. Generate a move to
1895 take the value from the store and put it into the
1896 shift pseudo, then shift it, then generate another
1897 move to put in into the target of the read. */
1898 emit_move_insn (new_reg, new_lhs);
1899 emit_insn (shift_seq);
1900 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1901 break;
1904 return read_reg;
1908 /* Call back for note_stores to find the hard regs set or clobbered by
1909 insn. Data is a bitmap of the hardregs set so far. */
1911 static void
1912 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1914 bitmap regs_set = (bitmap) data;
1916 if (REG_P (x)
1917 && HARD_REGISTER_P (x))
1918 bitmap_set_range (regs_set, REGNO (x), REG_NREGS (x));
1921 /* Helper function for replace_read and record_store.
1922 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1923 to one before READ_END bytes read in READ_MODE. Return NULL
1924 if not successful. If REQUIRE_CST is true, return always constant. */
1926 static rtx
1927 get_stored_val (store_info_t store_info, machine_mode read_mode,
1928 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1929 basic_block bb, bool require_cst)
1931 machine_mode store_mode = GET_MODE (store_info->mem);
1932 int shift;
1933 int access_size; /* In bytes. */
1934 rtx read_reg;
1936 /* To get here the read is within the boundaries of the write so
1937 shift will never be negative. Start out with the shift being in
1938 bytes. */
1939 if (store_mode == BLKmode)
1940 shift = 0;
1941 else if (BYTES_BIG_ENDIAN)
1942 shift = store_info->end - read_end;
1943 else
1944 shift = read_begin - store_info->begin;
1946 access_size = shift + GET_MODE_SIZE (read_mode);
1948 /* From now on it is bits. */
1949 shift *= BITS_PER_UNIT;
1951 if (shift)
1952 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1953 optimize_bb_for_speed_p (bb),
1954 require_cst);
1955 else if (store_mode == BLKmode)
1957 /* The store is a memset (addr, const_val, const_size). */
1958 gcc_assert (CONST_INT_P (store_info->rhs));
1959 store_mode = int_mode_for_mode (read_mode);
1960 if (store_mode == BLKmode)
1961 read_reg = NULL_RTX;
1962 else if (store_info->rhs == const0_rtx)
1963 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1964 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1965 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1966 read_reg = NULL_RTX;
1967 else
1969 unsigned HOST_WIDE_INT c
1970 = INTVAL (store_info->rhs)
1971 & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
1972 int shift = BITS_PER_UNIT;
1973 while (shift < HOST_BITS_PER_WIDE_INT)
1975 c |= (c << shift);
1976 shift <<= 1;
1978 read_reg = gen_int_mode (c, store_mode);
1979 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1982 else if (store_info->const_rhs
1983 && (require_cst
1984 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1985 read_reg = extract_low_bits (read_mode, store_mode,
1986 copy_rtx (store_info->const_rhs));
1987 else
1988 read_reg = extract_low_bits (read_mode, store_mode,
1989 copy_rtx (store_info->rhs));
1990 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1991 read_reg = NULL_RTX;
1992 return read_reg;
1995 /* Take a sequence of:
1996 A <- r1
1998 ... <- A
2000 and change it into
2001 r2 <- r1
2002 A <- r1
2004 ... <- r2
2008 r3 <- extract (r1)
2009 r3 <- r3 >> shift
2010 r2 <- extract (r3)
2011 ... <- r2
2015 r2 <- extract (r1)
2016 ... <- r2
2018 Depending on the alignment and the mode of the store and
2019 subsequent load.
2022 The STORE_INFO and STORE_INSN are for the store and READ_INFO
2023 and READ_INSN are for the read. Return true if the replacement
2024 went ok. */
2026 static bool
2027 replace_read (store_info_t store_info, insn_info_t store_insn,
2028 read_info_t read_info, insn_info_t read_insn, rtx *loc,
2029 bitmap regs_live)
2031 machine_mode store_mode = GET_MODE (store_info->mem);
2032 machine_mode read_mode = GET_MODE (read_info->mem);
2033 rtx_insn *insns, *this_insn;
2034 rtx read_reg;
2035 basic_block bb;
2037 if (!dbg_cnt (dse))
2038 return false;
2040 /* Create a sequence of instructions to set up the read register.
2041 This sequence goes immediately before the store and its result
2042 is read by the load.
2044 We need to keep this in perspective. We are replacing a read
2045 with a sequence of insns, but the read will almost certainly be
2046 in cache, so it is not going to be an expensive one. Thus, we
2047 are not willing to do a multi insn shift or worse a subroutine
2048 call to get rid of the read. */
2049 if (dump_file && (dump_flags & TDF_DETAILS))
2050 fprintf (dump_file, "trying to replace %smode load in insn %d"
2051 " from %smode store in insn %d\n",
2052 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
2053 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
2054 start_sequence ();
2055 bb = BLOCK_FOR_INSN (read_insn->insn);
2056 read_reg = get_stored_val (store_info,
2057 read_mode, read_info->begin, read_info->end,
2058 bb, false);
2059 if (read_reg == NULL_RTX)
2061 end_sequence ();
2062 if (dump_file && (dump_flags & TDF_DETAILS))
2063 fprintf (dump_file, " -- could not extract bits of stored value\n");
2064 return false;
2066 /* Force the value into a new register so that it won't be clobbered
2067 between the store and the load. */
2068 read_reg = copy_to_mode_reg (read_mode, read_reg);
2069 insns = get_insns ();
2070 end_sequence ();
2072 if (insns != NULL_RTX)
2074 /* Now we have to scan the set of new instructions to see if the
2075 sequence contains and sets of hardregs that happened to be
2076 live at this point. For instance, this can happen if one of
2077 the insns sets the CC and the CC happened to be live at that
2078 point. This does occasionally happen, see PR 37922. */
2079 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
2081 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
2082 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
2084 bitmap_and_into (regs_set, regs_live);
2085 if (!bitmap_empty_p (regs_set))
2087 if (dump_file && (dump_flags & TDF_DETAILS))
2089 fprintf (dump_file,
2090 "abandoning replacement because sequence clobbers live hardregs:");
2091 df_print_regset (dump_file, regs_set);
2094 BITMAP_FREE (regs_set);
2095 return false;
2097 BITMAP_FREE (regs_set);
2100 if (validate_change (read_insn->insn, loc, read_reg, 0))
2102 deferred_change_t change = new deferred_change;
2104 /* Insert this right before the store insn where it will be safe
2105 from later insns that might change it before the read. */
2106 emit_insn_before (insns, store_insn->insn);
2108 /* And now for the kludge part: cselib croaks if you just
2109 return at this point. There are two reasons for this:
2111 1) Cselib has an idea of how many pseudos there are and
2112 that does not include the new ones we just added.
2114 2) Cselib does not know about the move insn we added
2115 above the store_info, and there is no way to tell it
2116 about it, because it has "moved on".
2118 Problem (1) is fixable with a certain amount of engineering.
2119 Problem (2) is requires starting the bb from scratch. This
2120 could be expensive.
2122 So we are just going to have to lie. The move/extraction
2123 insns are not really an issue, cselib did not see them. But
2124 the use of the new pseudo read_insn is a real problem because
2125 cselib has not scanned this insn. The way that we solve this
2126 problem is that we are just going to put the mem back for now
2127 and when we are finished with the block, we undo this. We
2128 keep a table of mems to get rid of. At the end of the basic
2129 block we can put them back. */
2131 *loc = read_info->mem;
2132 change->next = deferred_change_list;
2133 deferred_change_list = change;
2134 change->loc = loc;
2135 change->reg = read_reg;
2137 /* Get rid of the read_info, from the point of view of the
2138 rest of dse, play like this read never happened. */
2139 read_insn->read_rec = read_info->next;
2140 delete read_info;
2141 if (dump_file && (dump_flags & TDF_DETAILS))
2143 fprintf (dump_file, " -- replaced the loaded MEM with ");
2144 print_simple_rtl (dump_file, read_reg);
2145 fprintf (dump_file, "\n");
2147 return true;
2149 else
2151 if (dump_file && (dump_flags & TDF_DETAILS))
2153 fprintf (dump_file, " -- replacing the loaded MEM with ");
2154 print_simple_rtl (dump_file, read_reg);
2155 fprintf (dump_file, " led to an invalid instruction\n");
2157 return false;
2161 /* Check the address of MEM *LOC and kill any appropriate stores that may
2162 be active. */
2164 static void
2165 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
2167 rtx mem = *loc, mem_addr;
2168 insn_info_t insn_info;
2169 HOST_WIDE_INT offset = 0;
2170 HOST_WIDE_INT width = 0;
2171 alias_set_type spill_alias_set = 0;
2172 cselib_val *base = NULL;
2173 int group_id;
2174 read_info_t read_info;
2176 insn_info = bb_info->last_insn;
2178 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2179 || (MEM_VOLATILE_P (mem)))
2181 if (dump_file && (dump_flags & TDF_DETAILS))
2182 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
2183 add_wild_read (bb_info);
2184 insn_info->cannot_delete = true;
2185 return;
2188 /* If it is reading readonly mem, then there can be no conflict with
2189 another write. */
2190 if (MEM_READONLY_P (mem))
2191 return;
2193 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
2195 if (dump_file && (dump_flags & TDF_DETAILS))
2196 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2197 add_wild_read (bb_info);
2198 return;
2201 if (GET_MODE (mem) == BLKmode)
2202 width = -1;
2203 else
2204 width = GET_MODE_SIZE (GET_MODE (mem));
2206 read_info = new read_info_type;
2207 read_info->group_id = group_id;
2208 read_info->mem = mem;
2209 read_info->alias_set = spill_alias_set;
2210 read_info->begin = offset;
2211 read_info->end = offset + width;
2212 read_info->next = insn_info->read_rec;
2213 insn_info->read_rec = read_info;
2214 /* For alias_set != 0 canon_true_dependence should be never called. */
2215 if (spill_alias_set)
2216 mem_addr = NULL_RTX;
2217 else
2219 if (group_id < 0)
2220 mem_addr = base->val_rtx;
2221 else
2223 group_info_t group
2224 = rtx_group_vec[group_id];
2225 mem_addr = group->canon_base_addr;
2227 /* get_addr can only handle VALUE but cannot handle expr like:
2228 VALUE + OFFSET, so call get_addr to get original addr for
2229 mem_addr before plus_constant. */
2230 mem_addr = get_addr (mem_addr);
2231 if (offset)
2232 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2235 /* We ignore the clobbers in store_info. The is mildly aggressive,
2236 but there really should not be a clobber followed by a read. */
2238 if (spill_alias_set)
2240 insn_info_t i_ptr = active_local_stores;
2241 insn_info_t last = NULL;
2243 if (dump_file && (dump_flags & TDF_DETAILS))
2244 fprintf (dump_file, " processing spill load %d\n",
2245 (int) spill_alias_set);
2247 while (i_ptr)
2249 store_info_t store_info = i_ptr->store_rec;
2251 /* Skip the clobbers. */
2252 while (!store_info->is_set)
2253 store_info = store_info->next;
2255 if (store_info->alias_set == spill_alias_set)
2257 if (dump_file && (dump_flags & TDF_DETAILS))
2258 dump_insn_info ("removing from active", i_ptr);
2260 active_local_stores_len--;
2261 if (last)
2262 last->next_local_store = i_ptr->next_local_store;
2263 else
2264 active_local_stores = i_ptr->next_local_store;
2266 else
2267 last = i_ptr;
2268 i_ptr = i_ptr->next_local_store;
2271 else if (group_id >= 0)
2273 /* This is the restricted case where the base is a constant or
2274 the frame pointer and offset is a constant. */
2275 insn_info_t i_ptr = active_local_stores;
2276 insn_info_t last = NULL;
2278 if (dump_file && (dump_flags & TDF_DETAILS))
2280 if (width == -1)
2281 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2282 group_id);
2283 else
2284 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2285 group_id, (int)offset, (int)(offset+width));
2288 while (i_ptr)
2290 bool remove = false;
2291 store_info_t store_info = i_ptr->store_rec;
2293 /* Skip the clobbers. */
2294 while (!store_info->is_set)
2295 store_info = store_info->next;
2297 /* There are three cases here. */
2298 if (store_info->group_id < 0)
2299 /* We have a cselib store followed by a read from a
2300 const base. */
2301 remove
2302 = canon_true_dependence (store_info->mem,
2303 GET_MODE (store_info->mem),
2304 store_info->mem_addr,
2305 mem, mem_addr);
2307 else if (group_id == store_info->group_id)
2309 /* This is a block mode load. We may get lucky and
2310 canon_true_dependence may save the day. */
2311 if (width == -1)
2312 remove
2313 = canon_true_dependence (store_info->mem,
2314 GET_MODE (store_info->mem),
2315 store_info->mem_addr,
2316 mem, mem_addr);
2318 /* If this read is just reading back something that we just
2319 stored, rewrite the read. */
2320 else
2322 if (store_info->rhs
2323 && offset >= store_info->begin
2324 && offset + width <= store_info->end
2325 && all_positions_needed_p (store_info,
2326 offset - store_info->begin,
2327 width)
2328 && replace_read (store_info, i_ptr, read_info,
2329 insn_info, loc, bb_info->regs_live))
2330 return;
2332 /* The bases are the same, just see if the offsets
2333 overlap. */
2334 if ((offset < store_info->end)
2335 && (offset + width > store_info->begin))
2336 remove = true;
2340 /* else
2341 The else case that is missing here is that the
2342 bases are constant but different. There is nothing
2343 to do here because there is no overlap. */
2345 if (remove)
2347 if (dump_file && (dump_flags & TDF_DETAILS))
2348 dump_insn_info ("removing from active", i_ptr);
2350 active_local_stores_len--;
2351 if (last)
2352 last->next_local_store = i_ptr->next_local_store;
2353 else
2354 active_local_stores = i_ptr->next_local_store;
2356 else
2357 last = i_ptr;
2358 i_ptr = i_ptr->next_local_store;
2361 else
2363 insn_info_t i_ptr = active_local_stores;
2364 insn_info_t last = NULL;
2365 if (dump_file && (dump_flags & TDF_DETAILS))
2367 fprintf (dump_file, " processing cselib load mem:");
2368 print_inline_rtx (dump_file, mem, 0);
2369 fprintf (dump_file, "\n");
2372 while (i_ptr)
2374 bool remove = false;
2375 store_info_t store_info = i_ptr->store_rec;
2377 if (dump_file && (dump_flags & TDF_DETAILS))
2378 fprintf (dump_file, " processing cselib load against insn %d\n",
2379 INSN_UID (i_ptr->insn));
2381 /* Skip the clobbers. */
2382 while (!store_info->is_set)
2383 store_info = store_info->next;
2385 /* If this read is just reading back something that we just
2386 stored, rewrite the read. */
2387 if (store_info->rhs
2388 && store_info->group_id == -1
2389 && store_info->cse_base == base
2390 && width != -1
2391 && offset >= store_info->begin
2392 && offset + width <= store_info->end
2393 && all_positions_needed_p (store_info,
2394 offset - store_info->begin, width)
2395 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2396 bb_info->regs_live))
2397 return;
2399 if (!store_info->alias_set)
2400 remove = canon_true_dependence (store_info->mem,
2401 GET_MODE (store_info->mem),
2402 store_info->mem_addr,
2403 mem, mem_addr);
2405 if (remove)
2407 if (dump_file && (dump_flags & TDF_DETAILS))
2408 dump_insn_info ("removing from active", i_ptr);
2410 active_local_stores_len--;
2411 if (last)
2412 last->next_local_store = i_ptr->next_local_store;
2413 else
2414 active_local_stores = i_ptr->next_local_store;
2416 else
2417 last = i_ptr;
2418 i_ptr = i_ptr->next_local_store;
2423 /* A note_uses callback in which DATA points the INSN_INFO for
2424 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2425 true for any part of *LOC. */
2427 static void
2428 check_mem_read_use (rtx *loc, void *data)
2430 subrtx_ptr_iterator::array_type array;
2431 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2433 rtx *loc = *iter;
2434 if (MEM_P (*loc))
2435 check_mem_read_rtx (loc, (bb_info_t) data);
2440 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2441 So far it only handles arguments passed in registers. */
2443 static bool
2444 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2446 CUMULATIVE_ARGS args_so_far_v;
2447 cumulative_args_t args_so_far;
2448 tree arg;
2449 int idx;
2451 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2452 args_so_far = pack_cumulative_args (&args_so_far_v);
2454 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2455 for (idx = 0;
2456 arg != void_list_node && idx < nargs;
2457 arg = TREE_CHAIN (arg), idx++)
2459 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2460 rtx reg, link, tmp;
2461 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2462 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2463 || GET_MODE_CLASS (mode) != MODE_INT)
2464 return false;
2466 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2467 link;
2468 link = XEXP (link, 1))
2469 if (GET_CODE (XEXP (link, 0)) == USE)
2471 args[idx] = XEXP (XEXP (link, 0), 0);
2472 if (REG_P (args[idx])
2473 && REGNO (args[idx]) == REGNO (reg)
2474 && (GET_MODE (args[idx]) == mode
2475 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2476 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2477 <= UNITS_PER_WORD)
2478 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2479 > GET_MODE_SIZE (mode)))))
2480 break;
2482 if (!link)
2483 return false;
2485 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2486 if (GET_MODE (args[idx]) != mode)
2488 if (!tmp || !CONST_INT_P (tmp))
2489 return false;
2490 tmp = gen_int_mode (INTVAL (tmp), mode);
2492 if (tmp)
2493 args[idx] = tmp;
2495 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2497 if (arg != void_list_node || idx != nargs)
2498 return false;
2499 return true;
2502 /* Return a bitmap of the fixed registers contained in IN. */
2504 static bitmap
2505 copy_fixed_regs (const_bitmap in)
2507 bitmap ret;
2509 ret = ALLOC_REG_SET (NULL);
2510 bitmap_and (ret, in, fixed_reg_set_regset);
2511 return ret;
2514 /* Apply record_store to all candidate stores in INSN. Mark INSN
2515 if some part of it is not a candidate store and assigns to a
2516 non-register target. */
2518 static void
2519 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2521 rtx body;
2522 insn_info_type *insn_info = new insn_info_type;
2523 int mems_found = 0;
2524 memset (insn_info, 0, sizeof (struct insn_info_type));
2526 if (dump_file && (dump_flags & TDF_DETAILS))
2527 fprintf (dump_file, "\n**scanning insn=%d\n",
2528 INSN_UID (insn));
2530 insn_info->prev_insn = bb_info->last_insn;
2531 insn_info->insn = insn;
2532 bb_info->last_insn = insn_info;
2534 if (DEBUG_INSN_P (insn))
2536 insn_info->cannot_delete = true;
2537 return;
2540 /* Look at all of the uses in the insn. */
2541 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2543 if (CALL_P (insn))
2545 bool const_call;
2546 tree memset_call = NULL_TREE;
2548 insn_info->cannot_delete = true;
2550 /* Const functions cannot do anything bad i.e. read memory,
2551 however, they can read their parameters which may have
2552 been pushed onto the stack.
2553 memset and bzero don't read memory either. */
2554 const_call = RTL_CONST_CALL_P (insn);
2555 if (!const_call)
2557 rtx call = get_call_rtx_from (insn);
2558 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
2560 rtx symbol = XEXP (XEXP (call, 0), 0);
2561 if (SYMBOL_REF_DECL (symbol)
2562 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
2564 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
2565 == BUILT_IN_NORMAL
2566 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
2567 == BUILT_IN_MEMSET))
2568 || SYMBOL_REF_DECL (symbol) == block_clear_fn)
2569 memset_call = SYMBOL_REF_DECL (symbol);
2573 if (const_call || memset_call)
2575 insn_info_t i_ptr = active_local_stores;
2576 insn_info_t last = NULL;
2578 if (dump_file && (dump_flags & TDF_DETAILS))
2579 fprintf (dump_file, "%s call %d\n",
2580 const_call ? "const" : "memset", INSN_UID (insn));
2582 /* See the head comment of the frame_read field. */
2583 if (reload_completed
2584 /* Tail calls are storing their arguments using
2585 arg pointer. If it is a frame pointer on the target,
2586 even before reload we need to kill frame pointer based
2587 stores. */
2588 || (SIBLING_CALL_P (insn)
2589 && HARD_FRAME_POINTER_IS_ARG_POINTER))
2590 insn_info->frame_read = true;
2592 /* Loop over the active stores and remove those which are
2593 killed by the const function call. */
2594 while (i_ptr)
2596 bool remove_store = false;
2598 /* The stack pointer based stores are always killed. */
2599 if (i_ptr->stack_pointer_based)
2600 remove_store = true;
2602 /* If the frame is read, the frame related stores are killed. */
2603 else if (insn_info->frame_read)
2605 store_info_t store_info = i_ptr->store_rec;
2607 /* Skip the clobbers. */
2608 while (!store_info->is_set)
2609 store_info = store_info->next;
2611 if (store_info->group_id >= 0
2612 && rtx_group_vec[store_info->group_id]->frame_related)
2613 remove_store = true;
2616 if (remove_store)
2618 if (dump_file && (dump_flags & TDF_DETAILS))
2619 dump_insn_info ("removing from active", i_ptr);
2621 active_local_stores_len--;
2622 if (last)
2623 last->next_local_store = i_ptr->next_local_store;
2624 else
2625 active_local_stores = i_ptr->next_local_store;
2627 else
2628 last = i_ptr;
2630 i_ptr = i_ptr->next_local_store;
2633 if (memset_call)
2635 rtx args[3];
2636 if (get_call_args (insn, memset_call, args, 3)
2637 && CONST_INT_P (args[1])
2638 && CONST_INT_P (args[2])
2639 && INTVAL (args[2]) > 0)
2641 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2642 set_mem_size (mem, INTVAL (args[2]));
2643 body = gen_rtx_SET (mem, args[1]);
2644 mems_found += record_store (body, bb_info);
2645 if (dump_file && (dump_flags & TDF_DETAILS))
2646 fprintf (dump_file, "handling memset as BLKmode store\n");
2647 if (mems_found == 1)
2649 if (active_local_stores_len++
2650 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2652 active_local_stores_len = 1;
2653 active_local_stores = NULL;
2655 insn_info->fixed_regs_live
2656 = copy_fixed_regs (bb_info->regs_live);
2657 insn_info->next_local_store = active_local_stores;
2658 active_local_stores = insn_info;
2663 else if (SIBLING_CALL_P (insn) && reload_completed)
2664 /* Arguments for a sibling call that are pushed to memory are passed
2665 using the incoming argument pointer of the current function. After
2666 reload that might be (and likely is) frame pointer based. */
2667 add_wild_read (bb_info);
2668 else
2669 /* Every other call, including pure functions, may read any memory
2670 that is not relative to the frame. */
2671 add_non_frame_wild_read (bb_info);
2673 return;
2676 /* Assuming that there are sets in these insns, we cannot delete
2677 them. */
2678 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2679 || volatile_refs_p (PATTERN (insn))
2680 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2681 || (RTX_FRAME_RELATED_P (insn))
2682 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2683 insn_info->cannot_delete = true;
2685 body = PATTERN (insn);
2686 if (GET_CODE (body) == PARALLEL)
2688 int i;
2689 for (i = 0; i < XVECLEN (body, 0); i++)
2690 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2692 else
2693 mems_found += record_store (body, bb_info);
2695 if (dump_file && (dump_flags & TDF_DETAILS))
2696 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2697 mems_found, insn_info->cannot_delete ? "true" : "false");
2699 /* If we found some sets of mems, add it into the active_local_stores so
2700 that it can be locally deleted if found dead or used for
2701 replace_read and redundant constant store elimination. Otherwise mark
2702 it as cannot delete. This simplifies the processing later. */
2703 if (mems_found == 1)
2705 if (active_local_stores_len++
2706 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2708 active_local_stores_len = 1;
2709 active_local_stores = NULL;
2711 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2712 insn_info->next_local_store = active_local_stores;
2713 active_local_stores = insn_info;
2715 else
2716 insn_info->cannot_delete = true;
2720 /* Remove BASE from the set of active_local_stores. This is a
2721 callback from cselib that is used to get rid of the stores in
2722 active_local_stores. */
2724 static void
2725 remove_useless_values (cselib_val *base)
2727 insn_info_t insn_info = active_local_stores;
2728 insn_info_t last = NULL;
2730 while (insn_info)
2732 store_info_t store_info = insn_info->store_rec;
2733 bool del = false;
2735 /* If ANY of the store_infos match the cselib group that is
2736 being deleted, then the insn can not be deleted. */
2737 while (store_info)
2739 if ((store_info->group_id == -1)
2740 && (store_info->cse_base == base))
2742 del = true;
2743 break;
2745 store_info = store_info->next;
2748 if (del)
2750 active_local_stores_len--;
2751 if (last)
2752 last->next_local_store = insn_info->next_local_store;
2753 else
2754 active_local_stores = insn_info->next_local_store;
2755 free_store_info (insn_info);
2757 else
2758 last = insn_info;
2760 insn_info = insn_info->next_local_store;
2765 /* Do all of step 1. */
2767 static void
2768 dse_step1 (void)
2770 basic_block bb;
2771 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2773 cselib_init (0);
2774 all_blocks = BITMAP_ALLOC (NULL);
2775 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2776 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2778 FOR_ALL_BB_FN (bb, cfun)
2780 insn_info_t ptr;
2781 bb_info_t bb_info = new dse_bb_info_type;
2783 memset (bb_info, 0, sizeof (dse_bb_info_type));
2784 bitmap_set_bit (all_blocks, bb->index);
2785 bb_info->regs_live = regs_live;
2787 bitmap_copy (regs_live, DF_LR_IN (bb));
2788 df_simulate_initialize_forwards (bb, regs_live);
2790 bb_table[bb->index] = bb_info;
2791 cselib_discard_hook = remove_useless_values;
2793 if (bb->index >= NUM_FIXED_BLOCKS)
2795 rtx_insn *insn;
2797 active_local_stores = NULL;
2798 active_local_stores_len = 0;
2799 cselib_clear_table ();
2801 /* Scan the insns. */
2802 FOR_BB_INSNS (bb, insn)
2804 if (INSN_P (insn))
2805 scan_insn (bb_info, insn);
2806 cselib_process_insn (insn);
2807 if (INSN_P (insn))
2808 df_simulate_one_insn_forwards (bb, insn, regs_live);
2811 /* This is something of a hack, because the global algorithm
2812 is supposed to take care of the case where stores go dead
2813 at the end of the function. However, the global
2814 algorithm must take a more conservative view of block
2815 mode reads than the local alg does. So to get the case
2816 where you have a store to the frame followed by a non
2817 overlapping block more read, we look at the active local
2818 stores at the end of the function and delete all of the
2819 frame and spill based ones. */
2820 if (stores_off_frame_dead_at_return
2821 && (EDGE_COUNT (bb->succs) == 0
2822 || (single_succ_p (bb)
2823 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2824 && ! crtl->calls_eh_return)))
2826 insn_info_t i_ptr = active_local_stores;
2827 while (i_ptr)
2829 store_info_t store_info = i_ptr->store_rec;
2831 /* Skip the clobbers. */
2832 while (!store_info->is_set)
2833 store_info = store_info->next;
2834 if (store_info->alias_set && !i_ptr->cannot_delete)
2835 delete_dead_store_insn (i_ptr);
2836 else
2837 if (store_info->group_id >= 0)
2839 group_info_t group
2840 = rtx_group_vec[store_info->group_id];
2841 if (group->frame_related && !i_ptr->cannot_delete)
2842 delete_dead_store_insn (i_ptr);
2845 i_ptr = i_ptr->next_local_store;
2849 /* Get rid of the loads that were discovered in
2850 replace_read. Cselib is finished with this block. */
2851 while (deferred_change_list)
2853 deferred_change_t next = deferred_change_list->next;
2855 /* There is no reason to validate this change. That was
2856 done earlier. */
2857 *deferred_change_list->loc = deferred_change_list->reg;
2858 delete deferred_change_list;
2859 deferred_change_list = next;
2862 /* Get rid of all of the cselib based store_infos in this
2863 block and mark the containing insns as not being
2864 deletable. */
2865 ptr = bb_info->last_insn;
2866 while (ptr)
2868 if (ptr->contains_cselib_groups)
2870 store_info_t s_info = ptr->store_rec;
2871 while (s_info && !s_info->is_set)
2872 s_info = s_info->next;
2873 if (s_info
2874 && s_info->redundant_reason
2875 && s_info->redundant_reason->insn
2876 && !ptr->cannot_delete)
2878 if (dump_file && (dump_flags & TDF_DETAILS))
2879 fprintf (dump_file, "Locally deleting insn %d "
2880 "because insn %d stores the "
2881 "same value and couldn't be "
2882 "eliminated\n",
2883 INSN_UID (ptr->insn),
2884 INSN_UID (s_info->redundant_reason->insn));
2885 delete_dead_store_insn (ptr);
2887 free_store_info (ptr);
2889 else
2891 store_info_t s_info;
2893 /* Free at least positions_needed bitmaps. */
2894 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2895 if (s_info->is_large)
2897 BITMAP_FREE (s_info->positions_needed.large.bmap);
2898 s_info->is_large = false;
2901 ptr = ptr->prev_insn;
2904 cse_store_info_pool.release ();
2906 bb_info->regs_live = NULL;
2909 BITMAP_FREE (regs_live);
2910 cselib_finish ();
2911 rtx_group_table->empty ();
2915 /*----------------------------------------------------------------------------
2916 Second step.
2918 Assign each byte position in the stores that we are going to
2919 analyze globally to a position in the bitmaps. Returns true if
2920 there are any bit positions assigned.
2921 ----------------------------------------------------------------------------*/
2923 static void
2924 dse_step2_init (void)
2926 unsigned int i;
2927 group_info_t group;
2929 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2931 /* For all non stack related bases, we only consider a store to
2932 be deletable if there are two or more stores for that
2933 position. This is because it takes one store to make the
2934 other store redundant. However, for the stores that are
2935 stack related, we consider them if there is only one store
2936 for the position. We do this because the stack related
2937 stores can be deleted if their is no read between them and
2938 the end of the function.
2940 To make this work in the current framework, we take the stack
2941 related bases add all of the bits from store1 into store2.
2942 This has the effect of making the eligible even if there is
2943 only one store. */
2945 if (stores_off_frame_dead_at_return && group->frame_related)
2947 bitmap_ior_into (group->store2_n, group->store1_n);
2948 bitmap_ior_into (group->store2_p, group->store1_p);
2949 if (dump_file && (dump_flags & TDF_DETAILS))
2950 fprintf (dump_file, "group %d is frame related ", i);
2953 group->offset_map_size_n++;
2954 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2955 group->offset_map_size_n);
2956 group->offset_map_size_p++;
2957 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2958 group->offset_map_size_p);
2959 group->process_globally = false;
2960 if (dump_file && (dump_flags & TDF_DETAILS))
2962 fprintf (dump_file, "group %d(%d+%d): ", i,
2963 (int)bitmap_count_bits (group->store2_n),
2964 (int)bitmap_count_bits (group->store2_p));
2965 bitmap_print (dump_file, group->store2_n, "n ", " ");
2966 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2972 /* Init the offset tables for the normal case. */
2974 static bool
2975 dse_step2_nospill (void)
2977 unsigned int i;
2978 group_info_t group;
2979 /* Position 0 is unused because 0 is used in the maps to mean
2980 unused. */
2981 current_position = 1;
2982 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2984 bitmap_iterator bi;
2985 unsigned int j;
2987 if (group == clear_alias_group)
2988 continue;
2990 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2991 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2992 bitmap_clear (group->group_kill);
2994 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2996 bitmap_set_bit (group->group_kill, current_position);
2997 if (bitmap_bit_p (group->escaped_n, j))
2998 bitmap_set_bit (kill_on_calls, current_position);
2999 group->offset_map_n[j] = current_position++;
3000 group->process_globally = true;
3002 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
3004 bitmap_set_bit (group->group_kill, current_position);
3005 if (bitmap_bit_p (group->escaped_p, j))
3006 bitmap_set_bit (kill_on_calls, current_position);
3007 group->offset_map_p[j] = current_position++;
3008 group->process_globally = true;
3011 return current_position != 1;
3016 /*----------------------------------------------------------------------------
3017 Third step.
3019 Build the bit vectors for the transfer functions.
3020 ----------------------------------------------------------------------------*/
3023 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
3024 there, return 0. */
3026 static int
3027 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
3029 if (offset < 0)
3031 HOST_WIDE_INT offset_p = -offset;
3032 if (offset_p >= group_info->offset_map_size_n)
3033 return 0;
3034 return group_info->offset_map_n[offset_p];
3036 else
3038 if (offset >= group_info->offset_map_size_p)
3039 return 0;
3040 return group_info->offset_map_p[offset];
3045 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3046 may be NULL. */
3048 static void
3049 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
3051 while (store_info)
3053 HOST_WIDE_INT i;
3054 group_info_t group_info
3055 = rtx_group_vec[store_info->group_id];
3056 if (group_info->process_globally)
3057 for (i = store_info->begin; i < store_info->end; i++)
3059 int index = get_bitmap_index (group_info, i);
3060 if (index != 0)
3062 bitmap_set_bit (gen, index);
3063 if (kill)
3064 bitmap_clear_bit (kill, index);
3067 store_info = store_info->next;
3072 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
3073 may be NULL. */
3075 static void
3076 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
3078 while (store_info)
3080 if (store_info->alias_set)
3082 int index = get_bitmap_index (clear_alias_group,
3083 store_info->alias_set);
3084 if (index != 0)
3086 bitmap_set_bit (gen, index);
3087 if (kill)
3088 bitmap_clear_bit (kill, index);
3091 store_info = store_info->next;
3096 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3097 may be NULL. */
3099 static void
3100 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
3102 read_info_t read_info = insn_info->read_rec;
3103 int i;
3104 group_info_t group;
3106 /* If this insn reads the frame, kill all the frame related stores. */
3107 if (insn_info->frame_read)
3109 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3110 if (group->process_globally && group->frame_related)
3112 if (kill)
3113 bitmap_ior_into (kill, group->group_kill);
3114 bitmap_and_compl_into (gen, group->group_kill);
3117 if (insn_info->non_frame_wild_read)
3119 /* Kill all non-frame related stores. Kill all stores of variables that
3120 escape. */
3121 if (kill)
3122 bitmap_ior_into (kill, kill_on_calls);
3123 bitmap_and_compl_into (gen, kill_on_calls);
3124 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3125 if (group->process_globally && !group->frame_related)
3127 if (kill)
3128 bitmap_ior_into (kill, group->group_kill);
3129 bitmap_and_compl_into (gen, group->group_kill);
3132 while (read_info)
3134 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3136 if (group->process_globally)
3138 if (i == read_info->group_id)
3140 if (read_info->begin > read_info->end)
3142 /* Begin > end for block mode reads. */
3143 if (kill)
3144 bitmap_ior_into (kill, group->group_kill);
3145 bitmap_and_compl_into (gen, group->group_kill);
3147 else
3149 /* The groups are the same, just process the
3150 offsets. */
3151 HOST_WIDE_INT j;
3152 for (j = read_info->begin; j < read_info->end; j++)
3154 int index = get_bitmap_index (group, j);
3155 if (index != 0)
3157 if (kill)
3158 bitmap_set_bit (kill, index);
3159 bitmap_clear_bit (gen, index);
3164 else
3166 /* The groups are different, if the alias sets
3167 conflict, clear the entire group. We only need
3168 to apply this test if the read_info is a cselib
3169 read. Anything with a constant base cannot alias
3170 something else with a different constant
3171 base. */
3172 if ((read_info->group_id < 0)
3173 && canon_true_dependence (group->base_mem,
3174 GET_MODE (group->base_mem),
3175 group->canon_base_addr,
3176 read_info->mem, NULL_RTX))
3178 if (kill)
3179 bitmap_ior_into (kill, group->group_kill);
3180 bitmap_and_compl_into (gen, group->group_kill);
3186 read_info = read_info->next;
3190 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3191 may be NULL. */
3193 static void
3194 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
3196 while (read_info)
3198 if (read_info->alias_set)
3200 int index = get_bitmap_index (clear_alias_group,
3201 read_info->alias_set);
3202 if (index != 0)
3204 if (kill)
3205 bitmap_set_bit (kill, index);
3206 bitmap_clear_bit (gen, index);
3210 read_info = read_info->next;
3215 /* Return the insn in BB_INFO before the first wild read or if there
3216 are no wild reads in the block, return the last insn. */
3218 static insn_info_t
3219 find_insn_before_first_wild_read (bb_info_t bb_info)
3221 insn_info_t insn_info = bb_info->last_insn;
3222 insn_info_t last_wild_read = NULL;
3224 while (insn_info)
3226 if (insn_info->wild_read)
3228 last_wild_read = insn_info->prev_insn;
3229 /* Block starts with wild read. */
3230 if (!last_wild_read)
3231 return NULL;
3234 insn_info = insn_info->prev_insn;
3237 if (last_wild_read)
3238 return last_wild_read;
3239 else
3240 return bb_info->last_insn;
3244 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3245 the block in order to build the gen and kill sets for the block.
3246 We start at ptr which may be the last insn in the block or may be
3247 the first insn with a wild read. In the latter case we are able to
3248 skip the rest of the block because it just does not matter:
3249 anything that happens is hidden by the wild read. */
3251 static void
3252 dse_step3_scan (bool for_spills, basic_block bb)
3254 bb_info_t bb_info = bb_table[bb->index];
3255 insn_info_t insn_info;
3257 if (for_spills)
3258 /* There are no wild reads in the spill case. */
3259 insn_info = bb_info->last_insn;
3260 else
3261 insn_info = find_insn_before_first_wild_read (bb_info);
3263 /* In the spill case or in the no_spill case if there is no wild
3264 read in the block, we will need a kill set. */
3265 if (insn_info == bb_info->last_insn)
3267 if (bb_info->kill)
3268 bitmap_clear (bb_info->kill);
3269 else
3270 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
3272 else
3273 if (bb_info->kill)
3274 BITMAP_FREE (bb_info->kill);
3276 while (insn_info)
3278 /* There may have been code deleted by the dce pass run before
3279 this phase. */
3280 if (insn_info->insn && INSN_P (insn_info->insn))
3282 /* Process the read(s) last. */
3283 if (for_spills)
3285 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3286 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
3288 else
3290 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3291 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
3295 insn_info = insn_info->prev_insn;
3300 /* Set the gen set of the exit block, and also any block with no
3301 successors that does not have a wild read. */
3303 static void
3304 dse_step3_exit_block_scan (bb_info_t bb_info)
3306 /* The gen set is all 0's for the exit block except for the
3307 frame_pointer_group. */
3309 if (stores_off_frame_dead_at_return)
3311 unsigned int i;
3312 group_info_t group;
3314 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3316 if (group->process_globally && group->frame_related)
3317 bitmap_ior_into (bb_info->gen, group->group_kill);
3323 /* Find all of the blocks that are not backwards reachable from the
3324 exit block or any block with no successors (BB). These are the
3325 infinite loops or infinite self loops. These blocks will still
3326 have their bits set in UNREACHABLE_BLOCKS. */
3328 static void
3329 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3331 edge e;
3332 edge_iterator ei;
3334 if (bitmap_bit_p (unreachable_blocks, bb->index))
3336 bitmap_clear_bit (unreachable_blocks, bb->index);
3337 FOR_EACH_EDGE (e, ei, bb->preds)
3339 mark_reachable_blocks (unreachable_blocks, e->src);
3344 /* Build the transfer functions for the function. */
3346 static void
3347 dse_step3 (bool for_spills)
3349 basic_block bb;
3350 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
3351 sbitmap_iterator sbi;
3352 bitmap all_ones = NULL;
3353 unsigned int i;
3355 bitmap_ones (unreachable_blocks);
3357 FOR_ALL_BB_FN (bb, cfun)
3359 bb_info_t bb_info = bb_table[bb->index];
3360 if (bb_info->gen)
3361 bitmap_clear (bb_info->gen);
3362 else
3363 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3365 if (bb->index == ENTRY_BLOCK)
3367 else if (bb->index == EXIT_BLOCK)
3368 dse_step3_exit_block_scan (bb_info);
3369 else
3370 dse_step3_scan (for_spills, bb);
3371 if (EDGE_COUNT (bb->succs) == 0)
3372 mark_reachable_blocks (unreachable_blocks, bb);
3374 /* If this is the second time dataflow is run, delete the old
3375 sets. */
3376 if (bb_info->in)
3377 BITMAP_FREE (bb_info->in);
3378 if (bb_info->out)
3379 BITMAP_FREE (bb_info->out);
3382 /* For any block in an infinite loop, we must initialize the out set
3383 to all ones. This could be expensive, but almost never occurs in
3384 practice. However, it is common in regression tests. */
3385 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3387 if (bitmap_bit_p (all_blocks, i))
3389 bb_info_t bb_info = bb_table[i];
3390 if (!all_ones)
3392 unsigned int j;
3393 group_info_t group;
3395 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3396 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3397 bitmap_ior_into (all_ones, group->group_kill);
3399 if (!bb_info->out)
3401 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3402 bitmap_copy (bb_info->out, all_ones);
3407 if (all_ones)
3408 BITMAP_FREE (all_ones);
3409 sbitmap_free (unreachable_blocks);
3414 /*----------------------------------------------------------------------------
3415 Fourth step.
3417 Solve the bitvector equations.
3418 ----------------------------------------------------------------------------*/
3421 /* Confluence function for blocks with no successors. Create an out
3422 set from the gen set of the exit block. This block logically has
3423 the exit block as a successor. */
3427 static void
3428 dse_confluence_0 (basic_block bb)
3430 bb_info_t bb_info = bb_table[bb->index];
3432 if (bb->index == EXIT_BLOCK)
3433 return;
3435 if (!bb_info->out)
3437 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3438 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3442 /* Propagate the information from the in set of the dest of E to the
3443 out set of the src of E. If the various in or out sets are not
3444 there, that means they are all ones. */
3446 static bool
3447 dse_confluence_n (edge e)
3449 bb_info_t src_info = bb_table[e->src->index];
3450 bb_info_t dest_info = bb_table[e->dest->index];
3452 if (dest_info->in)
3454 if (src_info->out)
3455 bitmap_and_into (src_info->out, dest_info->in);
3456 else
3458 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3459 bitmap_copy (src_info->out, dest_info->in);
3462 return true;
3466 /* Propagate the info from the out to the in set of BB_INDEX's basic
3467 block. There are three cases:
3469 1) The block has no kill set. In this case the kill set is all
3470 ones. It does not matter what the out set of the block is, none of
3471 the info can reach the top. The only thing that reaches the top is
3472 the gen set and we just copy the set.
3474 2) There is a kill set but no out set and bb has successors. In
3475 this case we just return. Eventually an out set will be created and
3476 it is better to wait than to create a set of ones.
3478 3) There is both a kill and out set. We apply the obvious transfer
3479 function.
3482 static bool
3483 dse_transfer_function (int bb_index)
3485 bb_info_t bb_info = bb_table[bb_index];
3487 if (bb_info->kill)
3489 if (bb_info->out)
3491 /* Case 3 above. */
3492 if (bb_info->in)
3493 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3494 bb_info->out, bb_info->kill);
3495 else
3497 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3498 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3499 bb_info->out, bb_info->kill);
3500 return true;
3503 else
3504 /* Case 2 above. */
3505 return false;
3507 else
3509 /* Case 1 above. If there is already an in set, nothing
3510 happens. */
3511 if (bb_info->in)
3512 return false;
3513 else
3515 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3516 bitmap_copy (bb_info->in, bb_info->gen);
3517 return true;
3522 /* Solve the dataflow equations. */
3524 static void
3525 dse_step4 (void)
3527 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3528 dse_confluence_n, dse_transfer_function,
3529 all_blocks, df_get_postorder (DF_BACKWARD),
3530 df_get_n_blocks (DF_BACKWARD));
3531 if (dump_file && (dump_flags & TDF_DETAILS))
3533 basic_block bb;
3535 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3536 FOR_ALL_BB_FN (bb, cfun)
3538 bb_info_t bb_info = bb_table[bb->index];
3540 df_print_bb_index (bb, dump_file);
3541 if (bb_info->in)
3542 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3543 else
3544 fprintf (dump_file, " in: *MISSING*\n");
3545 if (bb_info->gen)
3546 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3547 else
3548 fprintf (dump_file, " gen: *MISSING*\n");
3549 if (bb_info->kill)
3550 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3551 else
3552 fprintf (dump_file, " kill: *MISSING*\n");
3553 if (bb_info->out)
3554 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3555 else
3556 fprintf (dump_file, " out: *MISSING*\n\n");
3563 /*----------------------------------------------------------------------------
3564 Fifth step.
3566 Delete the stores that can only be deleted using the global information.
3567 ----------------------------------------------------------------------------*/
3570 static void
3571 dse_step5_nospill (void)
3573 basic_block bb;
3574 FOR_EACH_BB_FN (bb, cfun)
3576 bb_info_t bb_info = bb_table[bb->index];
3577 insn_info_t insn_info = bb_info->last_insn;
3578 bitmap v = bb_info->out;
3580 while (insn_info)
3582 bool deleted = false;
3583 if (dump_file && insn_info->insn)
3585 fprintf (dump_file, "starting to process insn %d\n",
3586 INSN_UID (insn_info->insn));
3587 bitmap_print (dump_file, v, " v: ", "\n");
3590 /* There may have been code deleted by the dce pass run before
3591 this phase. */
3592 if (insn_info->insn
3593 && INSN_P (insn_info->insn)
3594 && (!insn_info->cannot_delete)
3595 && (!bitmap_empty_p (v)))
3597 store_info_t store_info = insn_info->store_rec;
3599 /* Try to delete the current insn. */
3600 deleted = true;
3602 /* Skip the clobbers. */
3603 while (!store_info->is_set)
3604 store_info = store_info->next;
3606 if (store_info->alias_set)
3607 deleted = false;
3608 else
3610 HOST_WIDE_INT i;
3611 group_info_t group_info
3612 = rtx_group_vec[store_info->group_id];
3614 for (i = store_info->begin; i < store_info->end; i++)
3616 int index = get_bitmap_index (group_info, i);
3618 if (dump_file && (dump_flags & TDF_DETAILS))
3619 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3620 if (index == 0 || !bitmap_bit_p (v, index))
3622 if (dump_file && (dump_flags & TDF_DETAILS))
3623 fprintf (dump_file, "failing at i = %d\n", (int)i);
3624 deleted = false;
3625 break;
3629 if (deleted)
3631 if (dbg_cnt (dse)
3632 && check_for_inc_dec_1 (insn_info))
3634 delete_insn (insn_info->insn);
3635 insn_info->insn = NULL;
3636 globally_deleted++;
3640 /* We do want to process the local info if the insn was
3641 deleted. For instance, if the insn did a wild read, we
3642 no longer need to trash the info. */
3643 if (insn_info->insn
3644 && INSN_P (insn_info->insn)
3645 && (!deleted))
3647 scan_stores_nospill (insn_info->store_rec, v, NULL);
3648 if (insn_info->wild_read)
3650 if (dump_file && (dump_flags & TDF_DETAILS))
3651 fprintf (dump_file, "wild read\n");
3652 bitmap_clear (v);
3654 else if (insn_info->read_rec
3655 || insn_info->non_frame_wild_read)
3657 if (dump_file && !insn_info->non_frame_wild_read)
3658 fprintf (dump_file, "regular read\n");
3659 else if (dump_file && (dump_flags & TDF_DETAILS))
3660 fprintf (dump_file, "non-frame wild read\n");
3661 scan_reads_nospill (insn_info, v, NULL);
3665 insn_info = insn_info->prev_insn;
3672 /*----------------------------------------------------------------------------
3673 Sixth step.
3675 Delete stores made redundant by earlier stores (which store the same
3676 value) that couldn't be eliminated.
3677 ----------------------------------------------------------------------------*/
3679 static void
3680 dse_step6 (void)
3682 basic_block bb;
3684 FOR_ALL_BB_FN (bb, cfun)
3686 bb_info_t bb_info = bb_table[bb->index];
3687 insn_info_t insn_info = bb_info->last_insn;
3689 while (insn_info)
3691 /* There may have been code deleted by the dce pass run before
3692 this phase. */
3693 if (insn_info->insn
3694 && INSN_P (insn_info->insn)
3695 && !insn_info->cannot_delete)
3697 store_info_t s_info = insn_info->store_rec;
3699 while (s_info && !s_info->is_set)
3700 s_info = s_info->next;
3701 if (s_info
3702 && s_info->redundant_reason
3703 && s_info->redundant_reason->insn
3704 && INSN_P (s_info->redundant_reason->insn))
3706 rtx_insn *rinsn = s_info->redundant_reason->insn;
3707 if (dump_file && (dump_flags & TDF_DETAILS))
3708 fprintf (dump_file, "Locally deleting insn %d "
3709 "because insn %d stores the "
3710 "same value and couldn't be "
3711 "eliminated\n",
3712 INSN_UID (insn_info->insn),
3713 INSN_UID (rinsn));
3714 delete_dead_store_insn (insn_info);
3717 insn_info = insn_info->prev_insn;
3722 /*----------------------------------------------------------------------------
3723 Seventh step.
3725 Destroy everything left standing.
3726 ----------------------------------------------------------------------------*/
3728 static void
3729 dse_step7 (void)
3731 bitmap_obstack_release (&dse_bitmap_obstack);
3732 obstack_free (&dse_obstack, NULL);
3734 end_alias_analysis ();
3735 free (bb_table);
3736 delete rtx_group_table;
3737 rtx_group_table = NULL;
3738 rtx_group_vec.release ();
3739 BITMAP_FREE (all_blocks);
3740 BITMAP_FREE (scratch);
3742 rtx_store_info_pool.release ();
3743 read_info_type::pool.release ();
3744 insn_info_type::pool.release ();
3745 dse_bb_info_type::pool.release ();
3746 group_info::pool.release ();
3747 deferred_change::pool.release ();
3751 /* -------------------------------------------------------------------------
3753 ------------------------------------------------------------------------- */
3755 /* Callback for running pass_rtl_dse. */
3757 static unsigned int
3758 rest_of_handle_dse (void)
3760 df_set_flags (DF_DEFER_INSN_RESCAN);
3762 /* Need the notes since we must track live hardregs in the forwards
3763 direction. */
3764 df_note_add_problem ();
3765 df_analyze ();
3767 dse_step0 ();
3768 dse_step1 ();
3769 dse_step2_init ();
3770 if (dse_step2_nospill ())
3772 df_set_flags (DF_LR_RUN_DCE);
3773 df_analyze ();
3774 if (dump_file && (dump_flags & TDF_DETAILS))
3775 fprintf (dump_file, "doing global processing\n");
3776 dse_step3 (false);
3777 dse_step4 ();
3778 dse_step5_nospill ();
3781 dse_step6 ();
3782 dse_step7 ();
3784 if (dump_file)
3785 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3786 locally_deleted, globally_deleted, spill_deleted);
3788 /* DSE can eliminate potentially-trapping MEMs.
3789 Remove any EH edges associated with them. */
3790 if ((locally_deleted || globally_deleted)
3791 && cfun->can_throw_non_call_exceptions
3792 && purge_all_dead_edges ())
3793 cleanup_cfg (0);
3795 return 0;
3798 namespace {
3800 const pass_data pass_data_rtl_dse1 =
3802 RTL_PASS, /* type */
3803 "dse1", /* name */
3804 OPTGROUP_NONE, /* optinfo_flags */
3805 TV_DSE1, /* tv_id */
3806 0, /* properties_required */
3807 0, /* properties_provided */
3808 0, /* properties_destroyed */
3809 0, /* todo_flags_start */
3810 TODO_df_finish, /* todo_flags_finish */
3813 class pass_rtl_dse1 : public rtl_opt_pass
3815 public:
3816 pass_rtl_dse1 (gcc::context *ctxt)
3817 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3820 /* opt_pass methods: */
3821 virtual bool gate (function *)
3823 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3826 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3828 }; // class pass_rtl_dse1
3830 } // anon namespace
3832 rtl_opt_pass *
3833 make_pass_rtl_dse1 (gcc::context *ctxt)
3835 return new pass_rtl_dse1 (ctxt);
3838 namespace {
3840 const pass_data pass_data_rtl_dse2 =
3842 RTL_PASS, /* type */
3843 "dse2", /* name */
3844 OPTGROUP_NONE, /* optinfo_flags */
3845 TV_DSE2, /* tv_id */
3846 0, /* properties_required */
3847 0, /* properties_provided */
3848 0, /* properties_destroyed */
3849 0, /* todo_flags_start */
3850 TODO_df_finish, /* todo_flags_finish */
3853 class pass_rtl_dse2 : public rtl_opt_pass
3855 public:
3856 pass_rtl_dse2 (gcc::context *ctxt)
3857 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3860 /* opt_pass methods: */
3861 virtual bool gate (function *)
3863 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3866 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3868 }; // class pass_rtl_dse2
3870 } // anon namespace
3872 rtl_opt_pass *
3873 make_pass_rtl_dse2 (gcc::context *ctxt)
3875 return new pass_rtl_dse2 (ctxt);