2016-09-25 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / dse.c
blob17312c5a5872198705fe7ffb545c1901f565978a
1 /* RTL dead store elimination.
2 Copyright (C) 2005-2016 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 "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "predict.h"
34 #include "df.h"
35 #include "tm_p.h"
36 #include "gimple-ssa.h"
37 #include "expmed.h"
38 #include "optabs.h"
39 #include "emit-rtl.h"
40 #include "recog.h"
41 #include "alias.h"
42 #include "stor-layout.h"
43 #include "cfgrtl.h"
44 #include "cselib.h"
45 #include "tree-pass.h"
46 #include "explow.h"
47 #include "expr.h"
48 #include "dbgcnt.h"
49 #include "params.h"
50 #include "rtl-iter.h"
51 #include "cfgcleanup.h"
53 /* This file contains three techniques for performing Dead Store
54 Elimination (dse).
56 * The first technique performs dse locally on any base address. It
57 is based on the cselib which is a local value numbering technique.
58 This technique is local to a basic block but deals with a fairly
59 general addresses.
61 * The second technique performs dse globally but is restricted to
62 base addresses that are either constant or are relative to the
63 frame_pointer.
65 * The third technique, (which is only done after register allocation)
66 processes the spill slots. This differs from the second
67 technique because it takes advantage of the fact that spilling is
68 completely free from the effects of aliasing.
70 Logically, dse is a backwards dataflow problem. A store can be
71 deleted if it if cannot be reached in the backward direction by any
72 use of the value being stored. However, the local technique uses a
73 forwards scan of the basic block because cselib requires that the
74 block be processed in that order.
76 The pass is logically broken into 7 steps:
78 0) Initialization.
80 1) The local algorithm, as well as scanning the insns for the two
81 global algorithms.
83 2) Analysis to see if the global algs are necessary. In the case
84 of stores base on a constant address, there must be at least two
85 stores to that address, to make it possible to delete some of the
86 stores. In the case of stores off of the frame or spill related
87 stores, only one store to an address is necessary because those
88 stores die at the end of the function.
90 3) Set up the global dataflow equations based on processing the
91 info parsed in the first step.
93 4) Solve the dataflow equations.
95 5) Delete the insns that the global analysis has indicated are
96 unnecessary.
98 6) Delete insns that store the same value as preceding store
99 where the earlier store couldn't be eliminated.
101 7) Cleanup.
103 This step uses cselib and canon_rtx to build the largest expression
104 possible for each address. This pass is a forwards pass through
105 each basic block. From the point of view of the global technique,
106 the first pass could examine a block in either direction. The
107 forwards ordering is to accommodate cselib.
109 We make a simplifying assumption: addresses fall into four broad
110 categories:
112 1) base has rtx_varies_p == false, offset is constant.
113 2) base has rtx_varies_p == false, offset variable.
114 3) base has rtx_varies_p == true, offset constant.
115 4) base has rtx_varies_p == true, offset variable.
117 The local passes are able to process all 4 kinds of addresses. The
118 global pass only handles 1).
120 The global problem is formulated as follows:
122 A store, S1, to address A, where A is not relative to the stack
123 frame, can be eliminated if all paths from S1 to the end of the
124 function contain another store to A before a read to A.
126 If the address A is relative to the stack frame, a store S2 to A
127 can be eliminated if there are no paths from S2 that reach the
128 end of the function that read A before another store to A. In
129 this case S2 can be deleted if there are paths from S2 to the
130 end of the function that have no reads or writes to A. This
131 second case allows stores to the stack frame to be deleted that
132 would otherwise die when the function returns. This cannot be
133 done if stores_off_frame_dead_at_return is not true. See the doc
134 for that variable for when this variable is false.
136 The global problem is formulated as a backwards set union
137 dataflow problem where the stores are the gens and reads are the
138 kills. Set union problems are rare and require some special
139 handling given our representation of bitmaps. A straightforward
140 implementation requires a lot of bitmaps filled with 1s.
141 These are expensive and cumbersome in our bitmap formulation so
142 care has been taken to avoid large vectors filled with 1s. See
143 the comments in bb_info and in the dataflow confluence functions
144 for details.
146 There are two places for further enhancements to this algorithm:
148 1) The original dse which was embedded in a pass called flow also
149 did local address forwarding. For example in
151 A <- r100
152 ... <- A
154 flow would replace the right hand side of the second insn with a
155 reference to r100. Most of the information is available to add this
156 to this pass. It has not done it because it is a lot of work in
157 the case that either r100 is assigned to between the first and
158 second insn and/or the second insn is a load of part of the value
159 stored by the first insn.
161 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
162 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
163 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
164 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
166 2) The cleaning up of spill code is quite profitable. It currently
167 depends on reading tea leaves and chicken entrails left by reload.
168 This pass depends on reload creating a singleton alias set for each
169 spill slot and telling the next dse pass which of these alias sets
170 are the singletons. Rather than analyze the addresses of the
171 spills, dse's spill processing just does analysis of the loads and
172 stores that use those alias sets. There are three cases where this
173 falls short:
175 a) Reload sometimes creates the slot for one mode of access, and
176 then inserts loads and/or stores for a smaller mode. In this
177 case, the current code just punts on the slot. The proper thing
178 to do is to back out and use one bit vector position for each
179 byte of the entity associated with the slot. This depends on
180 KNOWING that reload always generates the accesses for each of the
181 bytes in some canonical (read that easy to understand several
182 passes after reload happens) way.
184 b) Reload sometimes decides that spill slot it allocated was not
185 large enough for the mode and goes back and allocates more slots
186 with the same mode and alias set. The backout in this case is a
187 little more graceful than (a). In this case the slot is unmarked
188 as being a spill slot and if final address comes out to be based
189 off the frame pointer, the global algorithm handles this slot.
191 c) For any pass that may prespill, there is currently no
192 mechanism to tell the dse pass that the slot being used has the
193 special properties that reload uses. It may be that all that is
194 required is to have those passes make the same calls that reload
195 does, assuming that the alias sets can be manipulated in the same
196 way. */
198 /* There are limits to the size of constant offsets we model for the
199 global problem. There are certainly test cases, that exceed this
200 limit, however, it is unlikely that there are important programs
201 that really have constant offsets this size. */
202 #define MAX_OFFSET (64 * 1024)
204 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
205 on the default obstack because these bitmaps can grow quite large
206 (~2GB for the small (!) test case of PR54146) and we'll hold on to
207 all that memory until the end of the compiler run.
208 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
209 releasing the whole obstack. */
210 static bitmap_obstack dse_bitmap_obstack;
212 /* Obstack for other data. As for above: Kinda nice to be able to
213 throw it all away at the end in one big sweep. */
214 static struct obstack dse_obstack;
216 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
217 static bitmap scratch = NULL;
219 struct insn_info_type;
221 /* This structure holds information about a candidate store. */
222 struct store_info
225 /* False means this is a clobber. */
226 bool is_set;
228 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
229 bool is_large;
231 /* The id of the mem group of the base address. If rtx_varies_p is
232 true, this is -1. Otherwise, it is the index into the group
233 table. */
234 int group_id;
236 /* This is the cselib value. */
237 cselib_val *cse_base;
239 /* This canonized mem. */
240 rtx mem;
242 /* Canonized MEM address for use by canon_true_dependence. */
243 rtx mem_addr;
245 /* The offset of the first and byte before the last byte associated
246 with the operation. */
247 HOST_WIDE_INT begin, end;
249 union
251 /* A bitmask as wide as the number of bytes in the word that
252 contains a 1 if the byte may be needed. The store is unused if
253 all of the bits are 0. This is used if IS_LARGE is false. */
254 unsigned HOST_WIDE_INT small_bitmask;
256 struct
258 /* A bitmap with one bit per byte. Cleared bit means the position
259 is needed. Used if IS_LARGE is false. */
260 bitmap bmap;
262 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
263 equal to END - BEGIN, the whole store is unused. */
264 int count;
265 } large;
266 } positions_needed;
268 /* The next store info for this insn. */
269 struct store_info *next;
271 /* The right hand side of the store. This is used if there is a
272 subsequent reload of the mems address somewhere later in the
273 basic block. */
274 rtx rhs;
276 /* If rhs is or holds a constant, this contains that constant,
277 otherwise NULL. */
278 rtx const_rhs;
280 /* Set if this store stores the same constant value as REDUNDANT_REASON
281 insn stored. These aren't eliminated early, because doing that
282 might prevent the earlier larger store to be eliminated. */
283 struct insn_info_type *redundant_reason;
286 /* Return a bitmask with the first N low bits set. */
288 static unsigned HOST_WIDE_INT
289 lowpart_bitmask (int n)
291 unsigned HOST_WIDE_INT mask = HOST_WIDE_INT_M1U;
292 return mask >> (HOST_BITS_PER_WIDE_INT - n);
295 static object_allocator<store_info> cse_store_info_pool ("cse_store_info_pool");
297 static object_allocator<store_info> rtx_store_info_pool ("rtx_store_info_pool");
299 /* This structure holds information about a load. These are only
300 built for rtx bases. */
301 struct read_info_type
303 /* The id of the mem group of the base address. */
304 int group_id;
306 /* The offset of the first and byte after the last byte associated
307 with the operation. If begin == end == 0, the read did not have
308 a constant offset. */
309 int begin, end;
311 /* The mem being read. */
312 rtx mem;
314 /* The next read_info for this insn. */
315 struct read_info_type *next;
317 typedef struct read_info_type *read_info_t;
319 static object_allocator<read_info_type> read_info_type_pool ("read_info_pool");
321 /* One of these records is created for each insn. */
323 struct insn_info_type
325 /* Set true if the insn contains a store but the insn itself cannot
326 be deleted. This is set if the insn is a parallel and there is
327 more than one non dead output or if the insn is in some way
328 volatile. */
329 bool cannot_delete;
331 /* This field is only used by the global algorithm. It is set true
332 if the insn contains any read of mem except for a (1). This is
333 also set if the insn is a call or has a clobber mem. If the insn
334 contains a wild read, the use_rec will be null. */
335 bool wild_read;
337 /* This is true only for CALL instructions which could potentially read
338 any non-frame memory location. This field is used by the global
339 algorithm. */
340 bool non_frame_wild_read;
342 /* This field is only used for the processing of const functions.
343 These functions cannot read memory, but they can read the stack
344 because that is where they may get their parms. We need to be
345 this conservative because, like the store motion pass, we don't
346 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
347 Moreover, we need to distinguish two cases:
348 1. Before reload (register elimination), the stores related to
349 outgoing arguments are stack pointer based and thus deemed
350 of non-constant base in this pass. This requires special
351 handling but also means that the frame pointer based stores
352 need not be killed upon encountering a const function call.
353 2. After reload, the stores related to outgoing arguments can be
354 either stack pointer or hard frame pointer based. This means
355 that we have no other choice than also killing all the frame
356 pointer based stores upon encountering a const function call.
357 This field is set after reload for const function calls and before
358 reload for const tail function calls on targets where arg pointer
359 is the frame pointer. Having this set is less severe than a wild
360 read, it just means that all the frame related stores are killed
361 rather than all the stores. */
362 bool frame_read;
364 /* This field is only used for the processing of const functions.
365 It is set if the insn may contain a stack pointer based store. */
366 bool stack_pointer_based;
368 /* This is true if any of the sets within the store contains a
369 cselib base. Such stores can only be deleted by the local
370 algorithm. */
371 bool contains_cselib_groups;
373 /* The insn. */
374 rtx_insn *insn;
376 /* The list of mem sets or mem clobbers that are contained in this
377 insn. If the insn is deletable, it contains only one mem set.
378 But it could also contain clobbers. Insns that contain more than
379 one mem set are not deletable, but each of those mems are here in
380 order to provide info to delete other insns. */
381 store_info *store_rec;
383 /* The linked list of mem uses in this insn. Only the reads from
384 rtx bases are listed here. The reads to cselib bases are
385 completely processed during the first scan and so are never
386 created. */
387 read_info_t read_rec;
389 /* The live fixed registers. We assume only fixed registers can
390 cause trouble by being clobbered from an expanded pattern;
391 storing only the live fixed registers (rather than all registers)
392 means less memory needs to be allocated / copied for the individual
393 stores. */
394 regset fixed_regs_live;
396 /* The prev insn in the basic block. */
397 struct insn_info_type * prev_insn;
399 /* The linked list of insns that are in consideration for removal in
400 the forwards pass through the basic block. This pointer may be
401 trash as it is not cleared when a wild read occurs. The only
402 time it is guaranteed to be correct is when the traversal starts
403 at active_local_stores. */
404 struct insn_info_type * next_local_store;
406 typedef struct insn_info_type *insn_info_t;
408 static object_allocator<insn_info_type> insn_info_type_pool ("insn_info_pool");
410 /* The linked list of stores that are under consideration in this
411 basic block. */
412 static insn_info_t active_local_stores;
413 static int active_local_stores_len;
415 struct dse_bb_info_type
417 /* Pointer to the insn info for the last insn in the block. These
418 are linked so this is how all of the insns are reached. During
419 scanning this is the current insn being scanned. */
420 insn_info_t last_insn;
422 /* The info for the global dataflow problem. */
425 /* This is set if the transfer function should and in the wild_read
426 bitmap before applying the kill and gen sets. That vector knocks
427 out most of the bits in the bitmap and thus speeds up the
428 operations. */
429 bool apply_wild_read;
431 /* The following 4 bitvectors hold information about which positions
432 of which stores are live or dead. They are indexed by
433 get_bitmap_index. */
435 /* The set of store positions that exist in this block before a wild read. */
436 bitmap gen;
438 /* The set of load positions that exist in this block above the
439 same position of a store. */
440 bitmap kill;
442 /* The set of stores that reach the top of the block without being
443 killed by a read.
445 Do not represent the in if it is all ones. Note that this is
446 what the bitvector should logically be initialized to for a set
447 intersection problem. However, like the kill set, this is too
448 expensive. So initially, the in set will only be created for the
449 exit block and any block that contains a wild read. */
450 bitmap in;
452 /* The set of stores that reach the bottom of the block from it's
453 successors.
455 Do not represent the in if it is all ones. Note that this is
456 what the bitvector should logically be initialized to for a set
457 intersection problem. However, like the kill and in set, this is
458 too expensive. So what is done is that the confluence operator
459 just initializes the vector from one of the out sets of the
460 successors of the block. */
461 bitmap out;
463 /* The following bitvector is indexed by the reg number. It
464 contains the set of regs that are live at the current instruction
465 being processed. While it contains info for all of the
466 registers, only the hard registers are actually examined. It is used
467 to assure that shift and/or add sequences that are inserted do not
468 accidentally clobber live hard regs. */
469 bitmap regs_live;
472 typedef struct dse_bb_info_type *bb_info_t;
474 static object_allocator<dse_bb_info_type> dse_bb_info_type_pool
475 ("bb_info_pool");
477 /* Table to hold all bb_infos. */
478 static bb_info_t *bb_table;
480 /* There is a group_info for each rtx base that is used to reference
481 memory. There are also not many of the rtx bases because they are
482 very limited in scope. */
484 struct group_info
486 /* The actual base of the address. */
487 rtx rtx_base;
489 /* The sequential id of the base. This allows us to have a
490 canonical ordering of these that is not based on addresses. */
491 int id;
493 /* True if there are any positions that are to be processed
494 globally. */
495 bool process_globally;
497 /* True if the base of this group is either the frame_pointer or
498 hard_frame_pointer. */
499 bool frame_related;
501 /* A mem wrapped around the base pointer for the group in order to do
502 read dependency. It must be given BLKmode in order to encompass all
503 the possible offsets from the base. */
504 rtx base_mem;
506 /* Canonized version of base_mem's address. */
507 rtx canon_base_addr;
509 /* These two sets of two bitmaps are used to keep track of how many
510 stores are actually referencing that position from this base. We
511 only do this for rtx bases as this will be used to assign
512 positions in the bitmaps for the global problem. Bit N is set in
513 store1 on the first store for offset N. Bit N is set in store2
514 for the second store to offset N. This is all we need since we
515 only care about offsets that have two or more stores for them.
517 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
518 for 0 and greater offsets.
520 There is one special case here, for stores into the stack frame,
521 we will or store1 into store2 before deciding which stores look
522 at globally. This is because stores to the stack frame that have
523 no other reads before the end of the function can also be
524 deleted. */
525 bitmap store1_n, store1_p, store2_n, store2_p;
527 /* These bitmaps keep track of offsets in this group escape this function.
528 An offset escapes if it corresponds to a named variable whose
529 addressable flag is set. */
530 bitmap escaped_n, escaped_p;
532 /* The positions in this bitmap have the same assignments as the in,
533 out, gen and kill bitmaps. This bitmap is all zeros except for
534 the positions that are occupied by stores for this group. */
535 bitmap group_kill;
537 /* The offset_map is used to map the offsets from this base into
538 positions in the global bitmaps. It is only created after all of
539 the all of stores have been scanned and we know which ones we
540 care about. */
541 int *offset_map_n, *offset_map_p;
542 int offset_map_size_n, offset_map_size_p;
545 static object_allocator<group_info> group_info_pool ("rtx_group_info_pool");
547 /* Index into the rtx_group_vec. */
548 static int rtx_group_next_id;
551 static vec<group_info *> rtx_group_vec;
554 /* This structure holds the set of changes that are being deferred
555 when removing read operation. See replace_read. */
556 struct deferred_change
559 /* The mem that is being replaced. */
560 rtx *loc;
562 /* The reg it is being replaced with. */
563 rtx reg;
565 struct deferred_change *next;
568 static object_allocator<deferred_change> deferred_change_pool
569 ("deferred_change_pool");
571 static deferred_change *deferred_change_list = NULL;
573 /* This is true except if cfun->stdarg -- i.e. we cannot do
574 this for vararg functions because they play games with the frame. */
575 static bool stores_off_frame_dead_at_return;
577 /* Counter for stats. */
578 static int globally_deleted;
579 static int locally_deleted;
581 static bitmap all_blocks;
583 /* Locations that are killed by calls in the global phase. */
584 static bitmap kill_on_calls;
586 /* The number of bits used in the global bitmaps. */
587 static unsigned int current_position;
589 /*----------------------------------------------------------------------------
590 Zeroth step.
592 Initialization.
593 ----------------------------------------------------------------------------*/
596 /* Hashtable callbacks for maintaining the "bases" field of
597 store_group_info, given that the addresses are function invariants. */
599 struct invariant_group_base_hasher : nofree_ptr_hash <group_info>
601 static inline hashval_t hash (const group_info *);
602 static inline bool equal (const group_info *, const group_info *);
605 inline bool
606 invariant_group_base_hasher::equal (const group_info *gi1,
607 const group_info *gi2)
609 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
612 inline hashval_t
613 invariant_group_base_hasher::hash (const group_info *gi)
615 int do_not_record;
616 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
619 /* Tables of group_info structures, hashed by base value. */
620 static hash_table<invariant_group_base_hasher> *rtx_group_table;
623 /* Get the GROUP for BASE. Add a new group if it is not there. */
625 static group_info *
626 get_group_info (rtx base)
628 struct group_info tmp_gi;
629 group_info *gi;
630 group_info **slot;
632 gcc_assert (base != NULL_RTX);
634 /* Find the store_base_info structure for BASE, creating a new one
635 if necessary. */
636 tmp_gi.rtx_base = base;
637 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
638 gi = *slot;
640 if (gi == NULL)
642 *slot = gi = group_info_pool.allocate ();
643 gi->rtx_base = base;
644 gi->id = rtx_group_next_id++;
645 gi->base_mem = gen_rtx_MEM (BLKmode, base);
646 gi->canon_base_addr = canon_rtx (base);
647 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
648 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
649 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
650 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
651 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
652 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
653 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
654 gi->process_globally = false;
655 gi->frame_related =
656 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
657 gi->offset_map_size_n = 0;
658 gi->offset_map_size_p = 0;
659 gi->offset_map_n = NULL;
660 gi->offset_map_p = NULL;
661 rtx_group_vec.safe_push (gi);
664 return gi;
668 /* Initialization of data structures. */
670 static void
671 dse_step0 (void)
673 locally_deleted = 0;
674 globally_deleted = 0;
676 bitmap_obstack_initialize (&dse_bitmap_obstack);
677 gcc_obstack_init (&dse_obstack);
679 scratch = BITMAP_ALLOC (&reg_obstack);
680 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
683 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
685 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
686 rtx_group_next_id = 0;
688 stores_off_frame_dead_at_return = !cfun->stdarg;
690 init_alias_analysis ();
695 /*----------------------------------------------------------------------------
696 First step.
698 Scan all of the insns. Any random ordering of the blocks is fine.
699 Each block is scanned in forward order to accommodate cselib which
700 is used to remove stores with non-constant bases.
701 ----------------------------------------------------------------------------*/
703 /* Delete all of the store_info recs from INSN_INFO. */
705 static void
706 free_store_info (insn_info_t insn_info)
708 store_info *cur = insn_info->store_rec;
709 while (cur)
711 store_info *next = cur->next;
712 if (cur->is_large)
713 BITMAP_FREE (cur->positions_needed.large.bmap);
714 if (cur->cse_base)
715 cse_store_info_pool.remove (cur);
716 else
717 rtx_store_info_pool.remove (cur);
718 cur = next;
721 insn_info->cannot_delete = true;
722 insn_info->contains_cselib_groups = false;
723 insn_info->store_rec = NULL;
726 struct note_add_store_info
728 rtx_insn *first, *current;
729 regset fixed_regs_live;
730 bool failure;
733 /* Callback for emit_inc_dec_insn_before via note_stores.
734 Check if a register is clobbered which is live afterwards. */
736 static void
737 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
739 rtx_insn *insn;
740 note_add_store_info *info = (note_add_store_info *) data;
742 if (!REG_P (loc))
743 return;
745 /* If this register is referenced by the current or an earlier insn,
746 that's OK. E.g. this applies to the register that is being incremented
747 with this addition. */
748 for (insn = info->first;
749 insn != NEXT_INSN (info->current);
750 insn = NEXT_INSN (insn))
751 if (reg_referenced_p (loc, PATTERN (insn)))
752 return;
754 /* If we come here, we have a clobber of a register that's only OK
755 if that register is not live. If we don't have liveness information
756 available, fail now. */
757 if (!info->fixed_regs_live)
759 info->failure = true;
760 return;
762 /* Now check if this is a live fixed register. */
763 unsigned int end_regno = END_REGNO (loc);
764 for (unsigned int regno = REGNO (loc); regno < end_regno; ++regno)
765 if (REGNO_REG_SET_P (info->fixed_regs_live, regno))
766 info->failure = true;
769 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
770 SRC + SRCOFF before insn ARG. */
772 static int
773 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
774 rtx op ATTRIBUTE_UNUSED,
775 rtx dest, rtx src, rtx srcoff, void *arg)
777 insn_info_t insn_info = (insn_info_t) arg;
778 rtx_insn *insn = insn_info->insn, *new_insn, *cur;
779 note_add_store_info info;
781 /* We can reuse all operands without copying, because we are about
782 to delete the insn that contained it. */
783 if (srcoff)
785 start_sequence ();
786 emit_insn (gen_add3_insn (dest, src, srcoff));
787 new_insn = get_insns ();
788 end_sequence ();
790 else
791 new_insn = gen_move_insn (dest, src);
792 info.first = new_insn;
793 info.fixed_regs_live = insn_info->fixed_regs_live;
794 info.failure = false;
795 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
797 info.current = cur;
798 note_stores (PATTERN (cur), note_add_store, &info);
801 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
802 return it immediately, communicating the failure to its caller. */
803 if (info.failure)
804 return 1;
806 emit_insn_before (new_insn, insn);
808 return 0;
811 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
812 is there, is split into a separate insn.
813 Return true on success (or if there was nothing to do), false on failure. */
815 static bool
816 check_for_inc_dec_1 (insn_info_t insn_info)
818 rtx_insn *insn = insn_info->insn;
819 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
820 if (note)
821 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
822 insn_info) == 0;
823 return true;
827 /* Entry point for postreload. If you work on reload_cse, or you need this
828 anywhere else, consider if you can provide register liveness information
829 and add a parameter to this function so that it can be passed down in
830 insn_info.fixed_regs_live. */
831 bool
832 check_for_inc_dec (rtx_insn *insn)
834 insn_info_type insn_info;
835 rtx note;
837 insn_info.insn = insn;
838 insn_info.fixed_regs_live = NULL;
839 note = find_reg_note (insn, REG_INC, NULL_RTX);
840 if (note)
841 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
842 &insn_info) == 0;
843 return true;
846 /* Delete the insn and free all of the fields inside INSN_INFO. */
848 static void
849 delete_dead_store_insn (insn_info_t insn_info)
851 read_info_t read_info;
853 if (!dbg_cnt (dse))
854 return;
856 if (!check_for_inc_dec_1 (insn_info))
857 return;
858 if (dump_file && (dump_flags & TDF_DETAILS))
859 fprintf (dump_file, "Locally deleting insn %d\n",
860 INSN_UID (insn_info->insn));
862 free_store_info (insn_info);
863 read_info = insn_info->read_rec;
865 while (read_info)
867 read_info_t next = read_info->next;
868 read_info_type_pool.remove (read_info);
869 read_info = next;
871 insn_info->read_rec = NULL;
873 delete_insn (insn_info->insn);
874 locally_deleted++;
875 insn_info->insn = NULL;
877 insn_info->wild_read = false;
880 /* Return whether DECL, a local variable, can possibly escape the current
881 function scope. */
883 static bool
884 local_variable_can_escape (tree decl)
886 if (TREE_ADDRESSABLE (decl))
887 return true;
889 /* If this is a partitioned variable, we need to consider all the variables
890 in the partition. This is necessary because a store into one of them can
891 be replaced with a store into another and this may not change the outcome
892 of the escape analysis. */
893 if (cfun->gimple_df->decls_to_pointers != NULL)
895 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
896 if (namep)
897 return TREE_ADDRESSABLE (*namep);
900 return false;
903 /* Return whether EXPR can possibly escape the current function scope. */
905 static bool
906 can_escape (tree expr)
908 tree base;
909 if (!expr)
910 return true;
911 base = get_base_address (expr);
912 if (DECL_P (base)
913 && !may_be_aliased (base)
914 && !(TREE_CODE (base) == VAR_DECL
915 && !DECL_EXTERNAL (base)
916 && !TREE_STATIC (base)
917 && local_variable_can_escape (base)))
918 return false;
919 return true;
922 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
923 OFFSET and WIDTH. */
925 static void
926 set_usage_bits (group_info *group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
927 tree expr)
929 HOST_WIDE_INT i;
930 bool expr_escapes = can_escape (expr);
931 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
932 for (i=offset; i<offset+width; i++)
934 bitmap store1;
935 bitmap store2;
936 bitmap escaped;
937 int ai;
938 if (i < 0)
940 store1 = group->store1_n;
941 store2 = group->store2_n;
942 escaped = group->escaped_n;
943 ai = -i;
945 else
947 store1 = group->store1_p;
948 store2 = group->store2_p;
949 escaped = group->escaped_p;
950 ai = i;
953 if (!bitmap_set_bit (store1, ai))
954 bitmap_set_bit (store2, ai);
955 else
957 if (i < 0)
959 if (group->offset_map_size_n < ai)
960 group->offset_map_size_n = ai;
962 else
964 if (group->offset_map_size_p < ai)
965 group->offset_map_size_p = ai;
968 if (expr_escapes)
969 bitmap_set_bit (escaped, ai);
973 static void
974 reset_active_stores (void)
976 active_local_stores = NULL;
977 active_local_stores_len = 0;
980 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
982 static void
983 free_read_records (bb_info_t bb_info)
985 insn_info_t insn_info = bb_info->last_insn;
986 read_info_t *ptr = &insn_info->read_rec;
987 while (*ptr)
989 read_info_t next = (*ptr)->next;
990 read_info_type_pool.remove (*ptr);
991 *ptr = next;
995 /* Set the BB_INFO so that the last insn is marked as a wild read. */
997 static void
998 add_wild_read (bb_info_t bb_info)
1000 insn_info_t insn_info = bb_info->last_insn;
1001 insn_info->wild_read = true;
1002 free_read_records (bb_info);
1003 reset_active_stores ();
1006 /* Set the BB_INFO so that the last insn is marked as a wild read of
1007 non-frame locations. */
1009 static void
1010 add_non_frame_wild_read (bb_info_t bb_info)
1012 insn_info_t insn_info = bb_info->last_insn;
1013 insn_info->non_frame_wild_read = true;
1014 free_read_records (bb_info);
1015 reset_active_stores ();
1018 /* Return true if X is a constant or one of the registers that behave
1019 as a constant over the life of a function. This is equivalent to
1020 !rtx_varies_p for memory addresses. */
1022 static bool
1023 const_or_frame_p (rtx x)
1025 if (CONSTANT_P (x))
1026 return true;
1028 if (GET_CODE (x) == REG)
1030 /* Note that we have to test for the actual rtx used for the frame
1031 and arg pointers and not just the register number in case we have
1032 eliminated the frame and/or arg pointer and are using it
1033 for pseudos. */
1034 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1035 /* The arg pointer varies if it is not a fixed register. */
1036 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1037 || x == pic_offset_table_rtx)
1038 return true;
1039 return false;
1042 return false;
1045 /* Take all reasonable action to put the address of MEM into the form
1046 that we can do analysis on.
1048 The gold standard is to get the address into the form: address +
1049 OFFSET where address is something that rtx_varies_p considers a
1050 constant. When we can get the address in this form, we can do
1051 global analysis on it. Note that for constant bases, address is
1052 not actually returned, only the group_id. The address can be
1053 obtained from that.
1055 If that fails, we try cselib to get a value we can at least use
1056 locally. If that fails we return false.
1058 The GROUP_ID is set to -1 for cselib bases and the index of the
1059 group for non_varying bases.
1061 FOR_READ is true if this is a mem read and false if not. */
1063 static bool
1064 canon_address (rtx mem,
1065 int *group_id,
1066 HOST_WIDE_INT *offset,
1067 cselib_val **base)
1069 machine_mode address_mode = get_address_mode (mem);
1070 rtx mem_address = XEXP (mem, 0);
1071 rtx expanded_address, address;
1072 int expanded;
1074 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1076 if (dump_file && (dump_flags & TDF_DETAILS))
1078 fprintf (dump_file, " mem: ");
1079 print_inline_rtx (dump_file, mem_address, 0);
1080 fprintf (dump_file, "\n");
1083 /* First see if just canon_rtx (mem_address) is const or frame,
1084 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1085 address = NULL_RTX;
1086 for (expanded = 0; expanded < 2; expanded++)
1088 if (expanded)
1090 /* Use cselib to replace all of the reg references with the full
1091 expression. This will take care of the case where we have
1093 r_x = base + offset;
1094 val = *r_x;
1096 by making it into
1098 val = *(base + offset); */
1100 expanded_address = cselib_expand_value_rtx (mem_address,
1101 scratch, 5);
1103 /* If this fails, just go with the address from first
1104 iteration. */
1105 if (!expanded_address)
1106 break;
1108 else
1109 expanded_address = mem_address;
1111 /* Split the address into canonical BASE + OFFSET terms. */
1112 address = canon_rtx (expanded_address);
1114 *offset = 0;
1116 if (dump_file && (dump_flags & TDF_DETAILS))
1118 if (expanded)
1120 fprintf (dump_file, "\n after cselib_expand address: ");
1121 print_inline_rtx (dump_file, expanded_address, 0);
1122 fprintf (dump_file, "\n");
1125 fprintf (dump_file, "\n after canon_rtx address: ");
1126 print_inline_rtx (dump_file, address, 0);
1127 fprintf (dump_file, "\n");
1130 if (GET_CODE (address) == CONST)
1131 address = XEXP (address, 0);
1133 if (GET_CODE (address) == PLUS
1134 && CONST_INT_P (XEXP (address, 1)))
1136 *offset = INTVAL (XEXP (address, 1));
1137 address = XEXP (address, 0);
1140 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1141 && const_or_frame_p (address))
1143 group_info *group = get_group_info (address);
1145 if (dump_file && (dump_flags & TDF_DETAILS))
1146 fprintf (dump_file, " gid=%d offset=%d \n",
1147 group->id, (int)*offset);
1148 *base = NULL;
1149 *group_id = group->id;
1150 return true;
1154 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1155 *group_id = -1;
1157 if (*base == NULL)
1159 if (dump_file && (dump_flags & TDF_DETAILS))
1160 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1161 return false;
1163 if (dump_file && (dump_flags & TDF_DETAILS))
1164 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1165 (*base)->uid, (*base)->hash, (int)*offset);
1166 return true;
1170 /* Clear the rhs field from the active_local_stores array. */
1172 static void
1173 clear_rhs_from_active_local_stores (void)
1175 insn_info_t ptr = active_local_stores;
1177 while (ptr)
1179 store_info *store_info = ptr->store_rec;
1180 /* Skip the clobbers. */
1181 while (!store_info->is_set)
1182 store_info = store_info->next;
1184 store_info->rhs = NULL;
1185 store_info->const_rhs = NULL;
1187 ptr = ptr->next_local_store;
1192 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1194 static inline void
1195 set_position_unneeded (store_info *s_info, int pos)
1197 if (__builtin_expect (s_info->is_large, false))
1199 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1200 s_info->positions_needed.large.count++;
1202 else
1203 s_info->positions_needed.small_bitmask
1204 &= ~(HOST_WIDE_INT_1U << pos);
1207 /* Mark the whole store S_INFO as unneeded. */
1209 static inline void
1210 set_all_positions_unneeded (store_info *s_info)
1212 if (__builtin_expect (s_info->is_large, false))
1214 int pos, end = s_info->end - s_info->begin;
1215 for (pos = 0; pos < end; pos++)
1216 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1217 s_info->positions_needed.large.count = end;
1219 else
1220 s_info->positions_needed.small_bitmask = HOST_WIDE_INT_0U;
1223 /* Return TRUE if any bytes from S_INFO store are needed. */
1225 static inline bool
1226 any_positions_needed_p (store_info *s_info)
1228 if (__builtin_expect (s_info->is_large, false))
1229 return (s_info->positions_needed.large.count
1230 < s_info->end - s_info->begin);
1231 else
1232 return (s_info->positions_needed.small_bitmask != HOST_WIDE_INT_0U);
1235 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1236 store are needed. */
1238 static inline bool
1239 all_positions_needed_p (store_info *s_info, int start, int width)
1241 if (__builtin_expect (s_info->is_large, false))
1243 int end = start + width;
1244 while (start < end)
1245 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1246 return false;
1247 return true;
1249 else
1251 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1252 return (s_info->positions_needed.small_bitmask & mask) == mask;
1257 static rtx get_stored_val (store_info *, machine_mode, HOST_WIDE_INT,
1258 HOST_WIDE_INT, basic_block, bool);
1261 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1262 there is a candidate store, after adding it to the appropriate
1263 local store group if so. */
1265 static int
1266 record_store (rtx body, bb_info_t bb_info)
1268 rtx mem, rhs, const_rhs, mem_addr;
1269 HOST_WIDE_INT offset = 0;
1270 HOST_WIDE_INT width = 0;
1271 insn_info_t insn_info = bb_info->last_insn;
1272 store_info *store_info = NULL;
1273 int group_id;
1274 cselib_val *base = NULL;
1275 insn_info_t ptr, last, redundant_reason;
1276 bool store_is_unused;
1278 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1279 return 0;
1281 mem = SET_DEST (body);
1283 /* If this is not used, then this cannot be used to keep the insn
1284 from being deleted. On the other hand, it does provide something
1285 that can be used to prove that another store is dead. */
1286 store_is_unused
1287 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1289 /* Check whether that value is a suitable memory location. */
1290 if (!MEM_P (mem))
1292 /* If the set or clobber is unused, then it does not effect our
1293 ability to get rid of the entire insn. */
1294 if (!store_is_unused)
1295 insn_info->cannot_delete = true;
1296 return 0;
1299 /* At this point we know mem is a mem. */
1300 if (GET_MODE (mem) == BLKmode)
1302 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1304 if (dump_file && (dump_flags & TDF_DETAILS))
1305 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1306 add_wild_read (bb_info);
1307 insn_info->cannot_delete = true;
1308 return 0;
1310 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1311 as memset (addr, 0, 36); */
1312 else if (!MEM_SIZE_KNOWN_P (mem)
1313 || MEM_SIZE (mem) <= 0
1314 || MEM_SIZE (mem) > MAX_OFFSET
1315 || GET_CODE (body) != SET
1316 || !CONST_INT_P (SET_SRC (body)))
1318 if (!store_is_unused)
1320 /* If the set or clobber is unused, then it does not effect our
1321 ability to get rid of the entire insn. */
1322 insn_info->cannot_delete = true;
1323 clear_rhs_from_active_local_stores ();
1325 return 0;
1329 /* We can still process a volatile mem, we just cannot delete it. */
1330 if (MEM_VOLATILE_P (mem))
1331 insn_info->cannot_delete = true;
1333 if (!canon_address (mem, &group_id, &offset, &base))
1335 clear_rhs_from_active_local_stores ();
1336 return 0;
1339 if (GET_MODE (mem) == BLKmode)
1340 width = MEM_SIZE (mem);
1341 else
1342 width = GET_MODE_SIZE (GET_MODE (mem));
1344 if (group_id >= 0)
1346 /* In the restrictive case where the base is a constant or the
1347 frame pointer we can do global analysis. */
1349 group_info *group
1350 = rtx_group_vec[group_id];
1351 tree expr = MEM_EXPR (mem);
1353 store_info = rtx_store_info_pool.allocate ();
1354 set_usage_bits (group, offset, width, expr);
1356 if (dump_file && (dump_flags & TDF_DETAILS))
1357 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1358 group_id, (int)offset, (int)(offset+width));
1360 else
1362 if (may_be_sp_based_p (XEXP (mem, 0)))
1363 insn_info->stack_pointer_based = true;
1364 insn_info->contains_cselib_groups = true;
1366 store_info = cse_store_info_pool.allocate ();
1367 group_id = -1;
1369 if (dump_file && (dump_flags & TDF_DETAILS))
1370 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1371 (int)offset, (int)(offset+width));
1374 const_rhs = rhs = NULL_RTX;
1375 if (GET_CODE (body) == SET
1376 /* No place to keep the value after ra. */
1377 && !reload_completed
1378 && (REG_P (SET_SRC (body))
1379 || GET_CODE (SET_SRC (body)) == SUBREG
1380 || CONSTANT_P (SET_SRC (body)))
1381 && !MEM_VOLATILE_P (mem)
1382 /* Sometimes the store and reload is used for truncation and
1383 rounding. */
1384 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1386 rhs = SET_SRC (body);
1387 if (CONSTANT_P (rhs))
1388 const_rhs = rhs;
1389 else if (body == PATTERN (insn_info->insn))
1391 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1392 if (tem && CONSTANT_P (XEXP (tem, 0)))
1393 const_rhs = XEXP (tem, 0);
1395 if (const_rhs == NULL_RTX && REG_P (rhs))
1397 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1399 if (tem && CONSTANT_P (tem))
1400 const_rhs = tem;
1404 /* Check to see if this stores causes some other stores to be
1405 dead. */
1406 ptr = active_local_stores;
1407 last = NULL;
1408 redundant_reason = NULL;
1409 mem = canon_rtx (mem);
1411 if (group_id < 0)
1412 mem_addr = base->val_rtx;
1413 else
1415 group_info *group = rtx_group_vec[group_id];
1416 mem_addr = group->canon_base_addr;
1418 if (offset)
1419 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1421 while (ptr)
1423 insn_info_t next = ptr->next_local_store;
1424 struct store_info *s_info = ptr->store_rec;
1425 bool del = true;
1427 /* Skip the clobbers. We delete the active insn if this insn
1428 shadows the set. To have been put on the active list, it
1429 has exactly on set. */
1430 while (!s_info->is_set)
1431 s_info = s_info->next;
1433 if (s_info->group_id == group_id && s_info->cse_base == base)
1435 HOST_WIDE_INT i;
1436 if (dump_file && (dump_flags & TDF_DETAILS))
1437 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1438 INSN_UID (ptr->insn), s_info->group_id,
1439 (int)s_info->begin, (int)s_info->end);
1441 /* Even if PTR won't be eliminated as unneeded, if both
1442 PTR and this insn store the same constant value, we might
1443 eliminate this insn instead. */
1444 if (s_info->const_rhs
1445 && const_rhs
1446 && offset >= s_info->begin
1447 && offset + width <= s_info->end
1448 && all_positions_needed_p (s_info, offset - s_info->begin,
1449 width))
1451 if (GET_MODE (mem) == BLKmode)
1453 if (GET_MODE (s_info->mem) == BLKmode
1454 && s_info->const_rhs == const_rhs)
1455 redundant_reason = ptr;
1457 else if (s_info->const_rhs == const0_rtx
1458 && const_rhs == const0_rtx)
1459 redundant_reason = ptr;
1460 else
1462 rtx val;
1463 start_sequence ();
1464 val = get_stored_val (s_info, GET_MODE (mem),
1465 offset, offset + width,
1466 BLOCK_FOR_INSN (insn_info->insn),
1467 true);
1468 if (get_insns () != NULL)
1469 val = NULL_RTX;
1470 end_sequence ();
1471 if (val && rtx_equal_p (val, const_rhs))
1472 redundant_reason = ptr;
1476 for (i = MAX (offset, s_info->begin);
1477 i < offset + width && i < s_info->end;
1478 i++)
1479 set_position_unneeded (s_info, i - s_info->begin);
1481 else if (s_info->rhs)
1482 /* Need to see if it is possible for this store to overwrite
1483 the value of store_info. If it is, set the rhs to NULL to
1484 keep it from being used to remove a load. */
1486 if (canon_output_dependence (s_info->mem, true,
1487 mem, GET_MODE (mem),
1488 mem_addr))
1490 s_info->rhs = NULL;
1491 s_info->const_rhs = NULL;
1495 /* An insn can be deleted if every position of every one of
1496 its s_infos is zero. */
1497 if (any_positions_needed_p (s_info))
1498 del = false;
1500 if (del)
1502 insn_info_t insn_to_delete = ptr;
1504 active_local_stores_len--;
1505 if (last)
1506 last->next_local_store = ptr->next_local_store;
1507 else
1508 active_local_stores = ptr->next_local_store;
1510 if (!insn_to_delete->cannot_delete)
1511 delete_dead_store_insn (insn_to_delete);
1513 else
1514 last = ptr;
1516 ptr = next;
1519 /* Finish filling in the store_info. */
1520 store_info->next = insn_info->store_rec;
1521 insn_info->store_rec = store_info;
1522 store_info->mem = mem;
1523 store_info->mem_addr = mem_addr;
1524 store_info->cse_base = base;
1525 if (width > HOST_BITS_PER_WIDE_INT)
1527 store_info->is_large = true;
1528 store_info->positions_needed.large.count = 0;
1529 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1531 else
1533 store_info->is_large = false;
1534 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1536 store_info->group_id = group_id;
1537 store_info->begin = offset;
1538 store_info->end = offset + width;
1539 store_info->is_set = GET_CODE (body) == SET;
1540 store_info->rhs = rhs;
1541 store_info->const_rhs = const_rhs;
1542 store_info->redundant_reason = redundant_reason;
1544 /* If this is a clobber, we return 0. We will only be able to
1545 delete this insn if there is only one store USED store, but we
1546 can use the clobber to delete other stores earlier. */
1547 return store_info->is_set ? 1 : 0;
1551 static void
1552 dump_insn_info (const char * start, insn_info_t insn_info)
1554 fprintf (dump_file, "%s insn=%d %s\n", start,
1555 INSN_UID (insn_info->insn),
1556 insn_info->store_rec ? "has store" : "naked");
1560 /* If the modes are different and the value's source and target do not
1561 line up, we need to extract the value from lower part of the rhs of
1562 the store, shift it, and then put it into a form that can be shoved
1563 into the read_insn. This function generates a right SHIFT of a
1564 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1565 shift sequence is returned or NULL if we failed to find a
1566 shift. */
1568 static rtx
1569 find_shift_sequence (int access_size,
1570 store_info *store_info,
1571 machine_mode read_mode,
1572 int shift, bool speed, bool require_cst)
1574 machine_mode store_mode = GET_MODE (store_info->mem);
1575 machine_mode new_mode;
1576 rtx read_reg = NULL;
1578 /* Some machines like the x86 have shift insns for each size of
1579 operand. Other machines like the ppc or the ia-64 may only have
1580 shift insns that shift values within 32 or 64 bit registers.
1581 This loop tries to find the smallest shift insn that will right
1582 justify the value we want to read but is available in one insn on
1583 the machine. */
1585 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1586 MODE_INT);
1587 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1588 new_mode = GET_MODE_WIDER_MODE (new_mode))
1590 rtx target, new_reg, new_lhs;
1591 rtx_insn *shift_seq, *insn;
1592 int cost;
1594 /* If a constant was stored into memory, try to simplify it here,
1595 otherwise the cost of the shift might preclude this optimization
1596 e.g. at -Os, even when no actual shift will be needed. */
1597 if (store_info->const_rhs)
1599 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1600 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1601 store_mode, byte);
1602 if (ret && CONSTANT_P (ret))
1604 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1605 ret, GEN_INT (shift));
1606 if (ret && CONSTANT_P (ret))
1608 byte = subreg_lowpart_offset (read_mode, new_mode);
1609 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1610 if (ret && CONSTANT_P (ret)
1611 && (set_src_cost (ret, read_mode, speed)
1612 <= COSTS_N_INSNS (1)))
1613 return ret;
1618 if (require_cst)
1619 return NULL_RTX;
1621 /* Try a wider mode if truncating the store mode to NEW_MODE
1622 requires a real instruction. */
1623 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1624 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1625 continue;
1627 /* Also try a wider mode if the necessary punning is either not
1628 desirable or not possible. */
1629 if (!CONSTANT_P (store_info->rhs)
1630 && !MODES_TIEABLE_P (new_mode, store_mode))
1631 continue;
1633 new_reg = gen_reg_rtx (new_mode);
1635 start_sequence ();
1637 /* In theory we could also check for an ashr. Ian Taylor knows
1638 of one dsp where the cost of these two was not the same. But
1639 this really is a rare case anyway. */
1640 target = expand_binop (new_mode, lshr_optab, new_reg,
1641 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1643 shift_seq = get_insns ();
1644 end_sequence ();
1646 if (target != new_reg || shift_seq == NULL)
1647 continue;
1649 cost = 0;
1650 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1651 if (INSN_P (insn))
1652 cost += insn_rtx_cost (PATTERN (insn), speed);
1654 /* The computation up to here is essentially independent
1655 of the arguments and could be precomputed. It may
1656 not be worth doing so. We could precompute if
1657 worthwhile or at least cache the results. The result
1658 technically depends on both SHIFT and ACCESS_SIZE,
1659 but in practice the answer will depend only on ACCESS_SIZE. */
1661 if (cost > COSTS_N_INSNS (1))
1662 continue;
1664 new_lhs = extract_low_bits (new_mode, store_mode,
1665 copy_rtx (store_info->rhs));
1666 if (new_lhs == NULL_RTX)
1667 continue;
1669 /* We found an acceptable shift. Generate a move to
1670 take the value from the store and put it into the
1671 shift pseudo, then shift it, then generate another
1672 move to put in into the target of the read. */
1673 emit_move_insn (new_reg, new_lhs);
1674 emit_insn (shift_seq);
1675 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1676 break;
1679 return read_reg;
1683 /* Call back for note_stores to find the hard regs set or clobbered by
1684 insn. Data is a bitmap of the hardregs set so far. */
1686 static void
1687 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1689 bitmap regs_set = (bitmap) data;
1691 if (REG_P (x)
1692 && HARD_REGISTER_P (x))
1693 bitmap_set_range (regs_set, REGNO (x), REG_NREGS (x));
1696 /* Helper function for replace_read and record_store.
1697 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1698 to one before READ_END bytes read in READ_MODE. Return NULL
1699 if not successful. If REQUIRE_CST is true, return always constant. */
1701 static rtx
1702 get_stored_val (store_info *store_info, machine_mode read_mode,
1703 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1704 basic_block bb, bool require_cst)
1706 machine_mode store_mode = GET_MODE (store_info->mem);
1707 int shift;
1708 int access_size; /* In bytes. */
1709 rtx read_reg;
1711 /* To get here the read is within the boundaries of the write so
1712 shift will never be negative. Start out with the shift being in
1713 bytes. */
1714 if (store_mode == BLKmode)
1715 shift = 0;
1716 else if (BYTES_BIG_ENDIAN)
1717 shift = store_info->end - read_end;
1718 else
1719 shift = read_begin - store_info->begin;
1721 access_size = shift + GET_MODE_SIZE (read_mode);
1723 /* From now on it is bits. */
1724 shift *= BITS_PER_UNIT;
1726 if (shift)
1727 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1728 optimize_bb_for_speed_p (bb),
1729 require_cst);
1730 else if (store_mode == BLKmode)
1732 /* The store is a memset (addr, const_val, const_size). */
1733 gcc_assert (CONST_INT_P (store_info->rhs));
1734 store_mode = int_mode_for_mode (read_mode);
1735 if (store_mode == BLKmode)
1736 read_reg = NULL_RTX;
1737 else if (store_info->rhs == const0_rtx)
1738 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1739 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1740 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1741 read_reg = NULL_RTX;
1742 else
1744 unsigned HOST_WIDE_INT c
1745 = INTVAL (store_info->rhs)
1746 & ((HOST_WIDE_INT_1 << BITS_PER_UNIT) - 1);
1747 int shift = BITS_PER_UNIT;
1748 while (shift < HOST_BITS_PER_WIDE_INT)
1750 c |= (c << shift);
1751 shift <<= 1;
1753 read_reg = gen_int_mode (c, store_mode);
1754 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1757 else if (store_info->const_rhs
1758 && (require_cst
1759 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1760 read_reg = extract_low_bits (read_mode, store_mode,
1761 copy_rtx (store_info->const_rhs));
1762 else
1763 read_reg = extract_low_bits (read_mode, store_mode,
1764 copy_rtx (store_info->rhs));
1765 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1766 read_reg = NULL_RTX;
1767 return read_reg;
1770 /* Take a sequence of:
1771 A <- r1
1773 ... <- A
1775 and change it into
1776 r2 <- r1
1777 A <- r1
1779 ... <- r2
1783 r3 <- extract (r1)
1784 r3 <- r3 >> shift
1785 r2 <- extract (r3)
1786 ... <- r2
1790 r2 <- extract (r1)
1791 ... <- r2
1793 Depending on the alignment and the mode of the store and
1794 subsequent load.
1797 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1798 and READ_INSN are for the read. Return true if the replacement
1799 went ok. */
1801 static bool
1802 replace_read (store_info *store_info, insn_info_t store_insn,
1803 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1804 bitmap regs_live)
1806 machine_mode store_mode = GET_MODE (store_info->mem);
1807 machine_mode read_mode = GET_MODE (read_info->mem);
1808 rtx_insn *insns, *this_insn;
1809 rtx read_reg;
1810 basic_block bb;
1812 if (!dbg_cnt (dse))
1813 return false;
1815 /* Create a sequence of instructions to set up the read register.
1816 This sequence goes immediately before the store and its result
1817 is read by the load.
1819 We need to keep this in perspective. We are replacing a read
1820 with a sequence of insns, but the read will almost certainly be
1821 in cache, so it is not going to be an expensive one. Thus, we
1822 are not willing to do a multi insn shift or worse a subroutine
1823 call to get rid of the read. */
1824 if (dump_file && (dump_flags & TDF_DETAILS))
1825 fprintf (dump_file, "trying to replace %smode load in insn %d"
1826 " from %smode store in insn %d\n",
1827 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1828 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1829 start_sequence ();
1830 bb = BLOCK_FOR_INSN (read_insn->insn);
1831 read_reg = get_stored_val (store_info,
1832 read_mode, read_info->begin, read_info->end,
1833 bb, false);
1834 if (read_reg == NULL_RTX)
1836 end_sequence ();
1837 if (dump_file && (dump_flags & TDF_DETAILS))
1838 fprintf (dump_file, " -- could not extract bits of stored value\n");
1839 return false;
1841 /* Force the value into a new register so that it won't be clobbered
1842 between the store and the load. */
1843 read_reg = copy_to_mode_reg (read_mode, read_reg);
1844 insns = get_insns ();
1845 end_sequence ();
1847 if (insns != NULL_RTX)
1849 /* Now we have to scan the set of new instructions to see if the
1850 sequence contains and sets of hardregs that happened to be
1851 live at this point. For instance, this can happen if one of
1852 the insns sets the CC and the CC happened to be live at that
1853 point. This does occasionally happen, see PR 37922. */
1854 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
1856 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
1857 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
1859 bitmap_and_into (regs_set, regs_live);
1860 if (!bitmap_empty_p (regs_set))
1862 if (dump_file && (dump_flags & TDF_DETAILS))
1864 fprintf (dump_file,
1865 "abandoning replacement because sequence clobbers live hardregs:");
1866 df_print_regset (dump_file, regs_set);
1869 BITMAP_FREE (regs_set);
1870 return false;
1872 BITMAP_FREE (regs_set);
1875 if (validate_change (read_insn->insn, loc, read_reg, 0))
1877 deferred_change *change = deferred_change_pool.allocate ();
1879 /* Insert this right before the store insn where it will be safe
1880 from later insns that might change it before the read. */
1881 emit_insn_before (insns, store_insn->insn);
1883 /* And now for the kludge part: cselib croaks if you just
1884 return at this point. There are two reasons for this:
1886 1) Cselib has an idea of how many pseudos there are and
1887 that does not include the new ones we just added.
1889 2) Cselib does not know about the move insn we added
1890 above the store_info, and there is no way to tell it
1891 about it, because it has "moved on".
1893 Problem (1) is fixable with a certain amount of engineering.
1894 Problem (2) is requires starting the bb from scratch. This
1895 could be expensive.
1897 So we are just going to have to lie. The move/extraction
1898 insns are not really an issue, cselib did not see them. But
1899 the use of the new pseudo read_insn is a real problem because
1900 cselib has not scanned this insn. The way that we solve this
1901 problem is that we are just going to put the mem back for now
1902 and when we are finished with the block, we undo this. We
1903 keep a table of mems to get rid of. At the end of the basic
1904 block we can put them back. */
1906 *loc = read_info->mem;
1907 change->next = deferred_change_list;
1908 deferred_change_list = change;
1909 change->loc = loc;
1910 change->reg = read_reg;
1912 /* Get rid of the read_info, from the point of view of the
1913 rest of dse, play like this read never happened. */
1914 read_insn->read_rec = read_info->next;
1915 read_info_type_pool.remove (read_info);
1916 if (dump_file && (dump_flags & TDF_DETAILS))
1918 fprintf (dump_file, " -- replaced the loaded MEM with ");
1919 print_simple_rtl (dump_file, read_reg);
1920 fprintf (dump_file, "\n");
1922 return true;
1924 else
1926 if (dump_file && (dump_flags & TDF_DETAILS))
1928 fprintf (dump_file, " -- replacing the loaded MEM with ");
1929 print_simple_rtl (dump_file, read_reg);
1930 fprintf (dump_file, " led to an invalid instruction\n");
1932 return false;
1936 /* Check the address of MEM *LOC and kill any appropriate stores that may
1937 be active. */
1939 static void
1940 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
1942 rtx mem = *loc, mem_addr;
1943 insn_info_t insn_info;
1944 HOST_WIDE_INT offset = 0;
1945 HOST_WIDE_INT width = 0;
1946 cselib_val *base = NULL;
1947 int group_id;
1948 read_info_t read_info;
1950 insn_info = bb_info->last_insn;
1952 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
1953 || (MEM_VOLATILE_P (mem)))
1955 if (dump_file && (dump_flags & TDF_DETAILS))
1956 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
1957 add_wild_read (bb_info);
1958 insn_info->cannot_delete = true;
1959 return;
1962 /* If it is reading readonly mem, then there can be no conflict with
1963 another write. */
1964 if (MEM_READONLY_P (mem))
1965 return;
1967 if (!canon_address (mem, &group_id, &offset, &base))
1969 if (dump_file && (dump_flags & TDF_DETAILS))
1970 fprintf (dump_file, " adding wild read, canon_address failure.\n");
1971 add_wild_read (bb_info);
1972 return;
1975 if (GET_MODE (mem) == BLKmode)
1976 width = -1;
1977 else
1978 width = GET_MODE_SIZE (GET_MODE (mem));
1980 read_info = read_info_type_pool.allocate ();
1981 read_info->group_id = group_id;
1982 read_info->mem = mem;
1983 read_info->begin = offset;
1984 read_info->end = offset + width;
1985 read_info->next = insn_info->read_rec;
1986 insn_info->read_rec = read_info;
1987 if (group_id < 0)
1988 mem_addr = base->val_rtx;
1989 else
1991 group_info *group = rtx_group_vec[group_id];
1992 mem_addr = group->canon_base_addr;
1994 if (offset)
1995 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1997 if (group_id >= 0)
1999 /* This is the restricted case where the base is a constant or
2000 the frame pointer and offset is a constant. */
2001 insn_info_t i_ptr = active_local_stores;
2002 insn_info_t last = NULL;
2004 if (dump_file && (dump_flags & TDF_DETAILS))
2006 if (width == -1)
2007 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2008 group_id);
2009 else
2010 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2011 group_id, (int)offset, (int)(offset+width));
2014 while (i_ptr)
2016 bool remove = false;
2017 store_info *store_info = i_ptr->store_rec;
2019 /* Skip the clobbers. */
2020 while (!store_info->is_set)
2021 store_info = store_info->next;
2023 /* There are three cases here. */
2024 if (store_info->group_id < 0)
2025 /* We have a cselib store followed by a read from a
2026 const base. */
2027 remove
2028 = canon_true_dependence (store_info->mem,
2029 GET_MODE (store_info->mem),
2030 store_info->mem_addr,
2031 mem, mem_addr);
2033 else if (group_id == store_info->group_id)
2035 /* This is a block mode load. We may get lucky and
2036 canon_true_dependence may save the day. */
2037 if (width == -1)
2038 remove
2039 = canon_true_dependence (store_info->mem,
2040 GET_MODE (store_info->mem),
2041 store_info->mem_addr,
2042 mem, mem_addr);
2044 /* If this read is just reading back something that we just
2045 stored, rewrite the read. */
2046 else
2048 if (store_info->rhs
2049 && offset >= store_info->begin
2050 && offset + width <= store_info->end
2051 && all_positions_needed_p (store_info,
2052 offset - store_info->begin,
2053 width)
2054 && replace_read (store_info, i_ptr, read_info,
2055 insn_info, loc, bb_info->regs_live))
2056 return;
2058 /* The bases are the same, just see if the offsets
2059 overlap. */
2060 if ((offset < store_info->end)
2061 && (offset + width > store_info->begin))
2062 remove = true;
2066 /* else
2067 The else case that is missing here is that the
2068 bases are constant but different. There is nothing
2069 to do here because there is no overlap. */
2071 if (remove)
2073 if (dump_file && (dump_flags & TDF_DETAILS))
2074 dump_insn_info ("removing from active", i_ptr);
2076 active_local_stores_len--;
2077 if (last)
2078 last->next_local_store = i_ptr->next_local_store;
2079 else
2080 active_local_stores = i_ptr->next_local_store;
2082 else
2083 last = i_ptr;
2084 i_ptr = i_ptr->next_local_store;
2087 else
2089 insn_info_t i_ptr = active_local_stores;
2090 insn_info_t last = NULL;
2091 if (dump_file && (dump_flags & TDF_DETAILS))
2093 fprintf (dump_file, " processing cselib load mem:");
2094 print_inline_rtx (dump_file, mem, 0);
2095 fprintf (dump_file, "\n");
2098 while (i_ptr)
2100 bool remove = false;
2101 store_info *store_info = i_ptr->store_rec;
2103 if (dump_file && (dump_flags & TDF_DETAILS))
2104 fprintf (dump_file, " processing cselib load against insn %d\n",
2105 INSN_UID (i_ptr->insn));
2107 /* Skip the clobbers. */
2108 while (!store_info->is_set)
2109 store_info = store_info->next;
2111 /* If this read is just reading back something that we just
2112 stored, rewrite the read. */
2113 if (store_info->rhs
2114 && store_info->group_id == -1
2115 && store_info->cse_base == base
2116 && width != -1
2117 && offset >= store_info->begin
2118 && offset + width <= store_info->end
2119 && all_positions_needed_p (store_info,
2120 offset - store_info->begin, width)
2121 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2122 bb_info->regs_live))
2123 return;
2125 remove = canon_true_dependence (store_info->mem,
2126 GET_MODE (store_info->mem),
2127 store_info->mem_addr,
2128 mem, mem_addr);
2130 if (remove)
2132 if (dump_file && (dump_flags & TDF_DETAILS))
2133 dump_insn_info ("removing from active", i_ptr);
2135 active_local_stores_len--;
2136 if (last)
2137 last->next_local_store = i_ptr->next_local_store;
2138 else
2139 active_local_stores = i_ptr->next_local_store;
2141 else
2142 last = i_ptr;
2143 i_ptr = i_ptr->next_local_store;
2148 /* A note_uses callback in which DATA points the INSN_INFO for
2149 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2150 true for any part of *LOC. */
2152 static void
2153 check_mem_read_use (rtx *loc, void *data)
2155 subrtx_ptr_iterator::array_type array;
2156 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2158 rtx *loc = *iter;
2159 if (MEM_P (*loc))
2160 check_mem_read_rtx (loc, (bb_info_t) data);
2165 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2166 So far it only handles arguments passed in registers. */
2168 static bool
2169 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2171 CUMULATIVE_ARGS args_so_far_v;
2172 cumulative_args_t args_so_far;
2173 tree arg;
2174 int idx;
2176 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2177 args_so_far = pack_cumulative_args (&args_so_far_v);
2179 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2180 for (idx = 0;
2181 arg != void_list_node && idx < nargs;
2182 arg = TREE_CHAIN (arg), idx++)
2184 machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2185 rtx reg, link, tmp;
2186 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2187 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2188 || GET_MODE_CLASS (mode) != MODE_INT)
2189 return false;
2191 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2192 link;
2193 link = XEXP (link, 1))
2194 if (GET_CODE (XEXP (link, 0)) == USE)
2196 args[idx] = XEXP (XEXP (link, 0), 0);
2197 if (REG_P (args[idx])
2198 && REGNO (args[idx]) == REGNO (reg)
2199 && (GET_MODE (args[idx]) == mode
2200 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2201 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2202 <= UNITS_PER_WORD)
2203 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2204 > GET_MODE_SIZE (mode)))))
2205 break;
2207 if (!link)
2208 return false;
2210 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2211 if (GET_MODE (args[idx]) != mode)
2213 if (!tmp || !CONST_INT_P (tmp))
2214 return false;
2215 tmp = gen_int_mode (INTVAL (tmp), mode);
2217 if (tmp)
2218 args[idx] = tmp;
2220 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2222 if (arg != void_list_node || idx != nargs)
2223 return false;
2224 return true;
2227 /* Return a bitmap of the fixed registers contained in IN. */
2229 static bitmap
2230 copy_fixed_regs (const_bitmap in)
2232 bitmap ret;
2234 ret = ALLOC_REG_SET (NULL);
2235 bitmap_and (ret, in, fixed_reg_set_regset);
2236 return ret;
2239 /* Apply record_store to all candidate stores in INSN. Mark INSN
2240 if some part of it is not a candidate store and assigns to a
2241 non-register target. */
2243 static void
2244 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2246 rtx body;
2247 insn_info_type *insn_info = insn_info_type_pool.allocate ();
2248 int mems_found = 0;
2249 memset (insn_info, 0, sizeof (struct insn_info_type));
2251 if (dump_file && (dump_flags & TDF_DETAILS))
2252 fprintf (dump_file, "\n**scanning insn=%d\n",
2253 INSN_UID (insn));
2255 insn_info->prev_insn = bb_info->last_insn;
2256 insn_info->insn = insn;
2257 bb_info->last_insn = insn_info;
2259 if (DEBUG_INSN_P (insn))
2261 insn_info->cannot_delete = true;
2262 return;
2265 /* Look at all of the uses in the insn. */
2266 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2268 if (CALL_P (insn))
2270 bool const_call;
2271 rtx call, sym;
2272 tree memset_call = NULL_TREE;
2274 insn_info->cannot_delete = true;
2276 /* Const functions cannot do anything bad i.e. read memory,
2277 however, they can read their parameters which may have
2278 been pushed onto the stack.
2279 memset and bzero don't read memory either. */
2280 const_call = RTL_CONST_CALL_P (insn);
2281 if (!const_call
2282 && (call = get_call_rtx_from (insn))
2283 && (sym = XEXP (XEXP (call, 0), 0))
2284 && GET_CODE (sym) == SYMBOL_REF
2285 && SYMBOL_REF_DECL (sym)
2286 && TREE_CODE (SYMBOL_REF_DECL (sym)) == FUNCTION_DECL
2287 && DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (sym)) == BUILT_IN_NORMAL
2288 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (sym)) == BUILT_IN_MEMSET)
2289 memset_call = SYMBOL_REF_DECL (sym);
2291 if (const_call || memset_call)
2293 insn_info_t i_ptr = active_local_stores;
2294 insn_info_t last = NULL;
2296 if (dump_file && (dump_flags & TDF_DETAILS))
2297 fprintf (dump_file, "%s call %d\n",
2298 const_call ? "const" : "memset", INSN_UID (insn));
2300 /* See the head comment of the frame_read field. */
2301 if (reload_completed
2302 /* Tail calls are storing their arguments using
2303 arg pointer. If it is a frame pointer on the target,
2304 even before reload we need to kill frame pointer based
2305 stores. */
2306 || (SIBLING_CALL_P (insn)
2307 && HARD_FRAME_POINTER_IS_ARG_POINTER))
2308 insn_info->frame_read = true;
2310 /* Loop over the active stores and remove those which are
2311 killed by the const function call. */
2312 while (i_ptr)
2314 bool remove_store = false;
2316 /* The stack pointer based stores are always killed. */
2317 if (i_ptr->stack_pointer_based)
2318 remove_store = true;
2320 /* If the frame is read, the frame related stores are killed. */
2321 else if (insn_info->frame_read)
2323 store_info *store_info = i_ptr->store_rec;
2325 /* Skip the clobbers. */
2326 while (!store_info->is_set)
2327 store_info = store_info->next;
2329 if (store_info->group_id >= 0
2330 && rtx_group_vec[store_info->group_id]->frame_related)
2331 remove_store = true;
2334 if (remove_store)
2336 if (dump_file && (dump_flags & TDF_DETAILS))
2337 dump_insn_info ("removing from active", i_ptr);
2339 active_local_stores_len--;
2340 if (last)
2341 last->next_local_store = i_ptr->next_local_store;
2342 else
2343 active_local_stores = i_ptr->next_local_store;
2345 else
2346 last = i_ptr;
2348 i_ptr = i_ptr->next_local_store;
2351 if (memset_call)
2353 rtx args[3];
2354 if (get_call_args (insn, memset_call, args, 3)
2355 && CONST_INT_P (args[1])
2356 && CONST_INT_P (args[2])
2357 && INTVAL (args[2]) > 0)
2359 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2360 set_mem_size (mem, INTVAL (args[2]));
2361 body = gen_rtx_SET (mem, args[1]);
2362 mems_found += record_store (body, bb_info);
2363 if (dump_file && (dump_flags & TDF_DETAILS))
2364 fprintf (dump_file, "handling memset as BLKmode store\n");
2365 if (mems_found == 1)
2367 if (active_local_stores_len++
2368 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2370 active_local_stores_len = 1;
2371 active_local_stores = NULL;
2373 insn_info->fixed_regs_live
2374 = copy_fixed_regs (bb_info->regs_live);
2375 insn_info->next_local_store = active_local_stores;
2376 active_local_stores = insn_info;
2379 else
2380 clear_rhs_from_active_local_stores ();
2383 else if (SIBLING_CALL_P (insn) && reload_completed)
2384 /* Arguments for a sibling call that are pushed to memory are passed
2385 using the incoming argument pointer of the current function. After
2386 reload that might be (and likely is) frame pointer based. */
2387 add_wild_read (bb_info);
2388 else
2389 /* Every other call, including pure functions, may read any memory
2390 that is not relative to the frame. */
2391 add_non_frame_wild_read (bb_info);
2393 return;
2396 /* Assuming that there are sets in these insns, we cannot delete
2397 them. */
2398 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2399 || volatile_refs_p (PATTERN (insn))
2400 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2401 || (RTX_FRAME_RELATED_P (insn))
2402 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2403 insn_info->cannot_delete = true;
2405 body = PATTERN (insn);
2406 if (GET_CODE (body) == PARALLEL)
2408 int i;
2409 for (i = 0; i < XVECLEN (body, 0); i++)
2410 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2412 else
2413 mems_found += record_store (body, bb_info);
2415 if (dump_file && (dump_flags & TDF_DETAILS))
2416 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2417 mems_found, insn_info->cannot_delete ? "true" : "false");
2419 /* If we found some sets of mems, add it into the active_local_stores so
2420 that it can be locally deleted if found dead or used for
2421 replace_read and redundant constant store elimination. Otherwise mark
2422 it as cannot delete. This simplifies the processing later. */
2423 if (mems_found == 1)
2425 if (active_local_stores_len++
2426 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2428 active_local_stores_len = 1;
2429 active_local_stores = NULL;
2431 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2432 insn_info->next_local_store = active_local_stores;
2433 active_local_stores = insn_info;
2435 else
2436 insn_info->cannot_delete = true;
2440 /* Remove BASE from the set of active_local_stores. This is a
2441 callback from cselib that is used to get rid of the stores in
2442 active_local_stores. */
2444 static void
2445 remove_useless_values (cselib_val *base)
2447 insn_info_t insn_info = active_local_stores;
2448 insn_info_t last = NULL;
2450 while (insn_info)
2452 store_info *store_info = insn_info->store_rec;
2453 bool del = false;
2455 /* If ANY of the store_infos match the cselib group that is
2456 being deleted, then the insn can not be deleted. */
2457 while (store_info)
2459 if ((store_info->group_id == -1)
2460 && (store_info->cse_base == base))
2462 del = true;
2463 break;
2465 store_info = store_info->next;
2468 if (del)
2470 active_local_stores_len--;
2471 if (last)
2472 last->next_local_store = insn_info->next_local_store;
2473 else
2474 active_local_stores = insn_info->next_local_store;
2475 free_store_info (insn_info);
2477 else
2478 last = insn_info;
2480 insn_info = insn_info->next_local_store;
2485 /* Do all of step 1. */
2487 static void
2488 dse_step1 (void)
2490 basic_block bb;
2491 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2493 cselib_init (0);
2494 all_blocks = BITMAP_ALLOC (NULL);
2495 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2496 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2498 FOR_ALL_BB_FN (bb, cfun)
2500 insn_info_t ptr;
2501 bb_info_t bb_info = dse_bb_info_type_pool.allocate ();
2503 memset (bb_info, 0, sizeof (dse_bb_info_type));
2504 bitmap_set_bit (all_blocks, bb->index);
2505 bb_info->regs_live = regs_live;
2507 bitmap_copy (regs_live, DF_LR_IN (bb));
2508 df_simulate_initialize_forwards (bb, regs_live);
2510 bb_table[bb->index] = bb_info;
2511 cselib_discard_hook = remove_useless_values;
2513 if (bb->index >= NUM_FIXED_BLOCKS)
2515 rtx_insn *insn;
2517 active_local_stores = NULL;
2518 active_local_stores_len = 0;
2519 cselib_clear_table ();
2521 /* Scan the insns. */
2522 FOR_BB_INSNS (bb, insn)
2524 if (INSN_P (insn))
2525 scan_insn (bb_info, insn);
2526 cselib_process_insn (insn);
2527 if (INSN_P (insn))
2528 df_simulate_one_insn_forwards (bb, insn, regs_live);
2531 /* This is something of a hack, because the global algorithm
2532 is supposed to take care of the case where stores go dead
2533 at the end of the function. However, the global
2534 algorithm must take a more conservative view of block
2535 mode reads than the local alg does. So to get the case
2536 where you have a store to the frame followed by a non
2537 overlapping block more read, we look at the active local
2538 stores at the end of the function and delete all of the
2539 frame and spill based ones. */
2540 if (stores_off_frame_dead_at_return
2541 && (EDGE_COUNT (bb->succs) == 0
2542 || (single_succ_p (bb)
2543 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2544 && ! crtl->calls_eh_return)))
2546 insn_info_t i_ptr = active_local_stores;
2547 while (i_ptr)
2549 store_info *store_info = i_ptr->store_rec;
2551 /* Skip the clobbers. */
2552 while (!store_info->is_set)
2553 store_info = store_info->next;
2554 if (store_info->group_id >= 0)
2556 group_info *group = rtx_group_vec[store_info->group_id];
2557 if (group->frame_related && !i_ptr->cannot_delete)
2558 delete_dead_store_insn (i_ptr);
2561 i_ptr = i_ptr->next_local_store;
2565 /* Get rid of the loads that were discovered in
2566 replace_read. Cselib is finished with this block. */
2567 while (deferred_change_list)
2569 deferred_change *next = deferred_change_list->next;
2571 /* There is no reason to validate this change. That was
2572 done earlier. */
2573 *deferred_change_list->loc = deferred_change_list->reg;
2574 deferred_change_pool.remove (deferred_change_list);
2575 deferred_change_list = next;
2578 /* Get rid of all of the cselib based store_infos in this
2579 block and mark the containing insns as not being
2580 deletable. */
2581 ptr = bb_info->last_insn;
2582 while (ptr)
2584 if (ptr->contains_cselib_groups)
2586 store_info *s_info = ptr->store_rec;
2587 while (s_info && !s_info->is_set)
2588 s_info = s_info->next;
2589 if (s_info
2590 && s_info->redundant_reason
2591 && s_info->redundant_reason->insn
2592 && !ptr->cannot_delete)
2594 if (dump_file && (dump_flags & TDF_DETAILS))
2595 fprintf (dump_file, "Locally deleting insn %d "
2596 "because insn %d stores the "
2597 "same value and couldn't be "
2598 "eliminated\n",
2599 INSN_UID (ptr->insn),
2600 INSN_UID (s_info->redundant_reason->insn));
2601 delete_dead_store_insn (ptr);
2603 free_store_info (ptr);
2605 else
2607 store_info *s_info;
2609 /* Free at least positions_needed bitmaps. */
2610 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2611 if (s_info->is_large)
2613 BITMAP_FREE (s_info->positions_needed.large.bmap);
2614 s_info->is_large = false;
2617 ptr = ptr->prev_insn;
2620 cse_store_info_pool.release ();
2622 bb_info->regs_live = NULL;
2625 BITMAP_FREE (regs_live);
2626 cselib_finish ();
2627 rtx_group_table->empty ();
2631 /*----------------------------------------------------------------------------
2632 Second step.
2634 Assign each byte position in the stores that we are going to
2635 analyze globally to a position in the bitmaps. Returns true if
2636 there are any bit positions assigned.
2637 ----------------------------------------------------------------------------*/
2639 static void
2640 dse_step2_init (void)
2642 unsigned int i;
2643 group_info *group;
2645 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2647 /* For all non stack related bases, we only consider a store to
2648 be deletable if there are two or more stores for that
2649 position. This is because it takes one store to make the
2650 other store redundant. However, for the stores that are
2651 stack related, we consider them if there is only one store
2652 for the position. We do this because the stack related
2653 stores can be deleted if their is no read between them and
2654 the end of the function.
2656 To make this work in the current framework, we take the stack
2657 related bases add all of the bits from store1 into store2.
2658 This has the effect of making the eligible even if there is
2659 only one store. */
2661 if (stores_off_frame_dead_at_return && group->frame_related)
2663 bitmap_ior_into (group->store2_n, group->store1_n);
2664 bitmap_ior_into (group->store2_p, group->store1_p);
2665 if (dump_file && (dump_flags & TDF_DETAILS))
2666 fprintf (dump_file, "group %d is frame related ", i);
2669 group->offset_map_size_n++;
2670 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2671 group->offset_map_size_n);
2672 group->offset_map_size_p++;
2673 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2674 group->offset_map_size_p);
2675 group->process_globally = false;
2676 if (dump_file && (dump_flags & TDF_DETAILS))
2678 fprintf (dump_file, "group %d(%d+%d): ", i,
2679 (int)bitmap_count_bits (group->store2_n),
2680 (int)bitmap_count_bits (group->store2_p));
2681 bitmap_print (dump_file, group->store2_n, "n ", " ");
2682 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2688 /* Init the offset tables. */
2690 static bool
2691 dse_step2 (void)
2693 unsigned int i;
2694 group_info *group;
2695 /* Position 0 is unused because 0 is used in the maps to mean
2696 unused. */
2697 current_position = 1;
2698 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2700 bitmap_iterator bi;
2701 unsigned int j;
2703 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2704 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2705 bitmap_clear (group->group_kill);
2707 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2709 bitmap_set_bit (group->group_kill, current_position);
2710 if (bitmap_bit_p (group->escaped_n, j))
2711 bitmap_set_bit (kill_on_calls, current_position);
2712 group->offset_map_n[j] = current_position++;
2713 group->process_globally = true;
2715 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2717 bitmap_set_bit (group->group_kill, current_position);
2718 if (bitmap_bit_p (group->escaped_p, j))
2719 bitmap_set_bit (kill_on_calls, current_position);
2720 group->offset_map_p[j] = current_position++;
2721 group->process_globally = true;
2724 return current_position != 1;
2729 /*----------------------------------------------------------------------------
2730 Third step.
2732 Build the bit vectors for the transfer functions.
2733 ----------------------------------------------------------------------------*/
2736 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2737 there, return 0. */
2739 static int
2740 get_bitmap_index (group_info *group_info, HOST_WIDE_INT offset)
2742 if (offset < 0)
2744 HOST_WIDE_INT offset_p = -offset;
2745 if (offset_p >= group_info->offset_map_size_n)
2746 return 0;
2747 return group_info->offset_map_n[offset_p];
2749 else
2751 if (offset >= group_info->offset_map_size_p)
2752 return 0;
2753 return group_info->offset_map_p[offset];
2758 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2759 may be NULL. */
2761 static void
2762 scan_stores (store_info *store_info, bitmap gen, bitmap kill)
2764 while (store_info)
2766 HOST_WIDE_INT i;
2767 group_info *group_info
2768 = rtx_group_vec[store_info->group_id];
2769 if (group_info->process_globally)
2770 for (i = store_info->begin; i < store_info->end; i++)
2772 int index = get_bitmap_index (group_info, i);
2773 if (index != 0)
2775 bitmap_set_bit (gen, index);
2776 if (kill)
2777 bitmap_clear_bit (kill, index);
2780 store_info = store_info->next;
2785 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
2786 may be NULL. */
2788 static void
2789 scan_reads (insn_info_t insn_info, bitmap gen, bitmap kill)
2791 read_info_t read_info = insn_info->read_rec;
2792 int i;
2793 group_info *group;
2795 /* If this insn reads the frame, kill all the frame related stores. */
2796 if (insn_info->frame_read)
2798 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2799 if (group->process_globally && group->frame_related)
2801 if (kill)
2802 bitmap_ior_into (kill, group->group_kill);
2803 bitmap_and_compl_into (gen, group->group_kill);
2806 if (insn_info->non_frame_wild_read)
2808 /* Kill all non-frame related stores. Kill all stores of variables that
2809 escape. */
2810 if (kill)
2811 bitmap_ior_into (kill, kill_on_calls);
2812 bitmap_and_compl_into (gen, kill_on_calls);
2813 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2814 if (group->process_globally && !group->frame_related)
2816 if (kill)
2817 bitmap_ior_into (kill, group->group_kill);
2818 bitmap_and_compl_into (gen, group->group_kill);
2821 while (read_info)
2823 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2825 if (group->process_globally)
2827 if (i == read_info->group_id)
2829 if (read_info->begin > read_info->end)
2831 /* Begin > end for block mode reads. */
2832 if (kill)
2833 bitmap_ior_into (kill, group->group_kill);
2834 bitmap_and_compl_into (gen, group->group_kill);
2836 else
2838 /* The groups are the same, just process the
2839 offsets. */
2840 HOST_WIDE_INT j;
2841 for (j = read_info->begin; j < read_info->end; j++)
2843 int index = get_bitmap_index (group, j);
2844 if (index != 0)
2846 if (kill)
2847 bitmap_set_bit (kill, index);
2848 bitmap_clear_bit (gen, index);
2853 else
2855 /* The groups are different, if the alias sets
2856 conflict, clear the entire group. We only need
2857 to apply this test if the read_info is a cselib
2858 read. Anything with a constant base cannot alias
2859 something else with a different constant
2860 base. */
2861 if ((read_info->group_id < 0)
2862 && canon_true_dependence (group->base_mem,
2863 GET_MODE (group->base_mem),
2864 group->canon_base_addr,
2865 read_info->mem, NULL_RTX))
2867 if (kill)
2868 bitmap_ior_into (kill, group->group_kill);
2869 bitmap_and_compl_into (gen, group->group_kill);
2875 read_info = read_info->next;
2880 /* Return the insn in BB_INFO before the first wild read or if there
2881 are no wild reads in the block, return the last insn. */
2883 static insn_info_t
2884 find_insn_before_first_wild_read (bb_info_t bb_info)
2886 insn_info_t insn_info = bb_info->last_insn;
2887 insn_info_t last_wild_read = NULL;
2889 while (insn_info)
2891 if (insn_info->wild_read)
2893 last_wild_read = insn_info->prev_insn;
2894 /* Block starts with wild read. */
2895 if (!last_wild_read)
2896 return NULL;
2899 insn_info = insn_info->prev_insn;
2902 if (last_wild_read)
2903 return last_wild_read;
2904 else
2905 return bb_info->last_insn;
2909 /* Scan the insns in BB_INFO starting at PTR and going to the top of
2910 the block in order to build the gen and kill sets for the block.
2911 We start at ptr which may be the last insn in the block or may be
2912 the first insn with a wild read. In the latter case we are able to
2913 skip the rest of the block because it just does not matter:
2914 anything that happens is hidden by the wild read. */
2916 static void
2917 dse_step3_scan (basic_block bb)
2919 bb_info_t bb_info = bb_table[bb->index];
2920 insn_info_t insn_info;
2922 insn_info = find_insn_before_first_wild_read (bb_info);
2924 /* In the spill case or in the no_spill case if there is no wild
2925 read in the block, we will need a kill set. */
2926 if (insn_info == bb_info->last_insn)
2928 if (bb_info->kill)
2929 bitmap_clear (bb_info->kill);
2930 else
2931 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
2933 else
2934 if (bb_info->kill)
2935 BITMAP_FREE (bb_info->kill);
2937 while (insn_info)
2939 /* There may have been code deleted by the dce pass run before
2940 this phase. */
2941 if (insn_info->insn && INSN_P (insn_info->insn))
2943 scan_stores (insn_info->store_rec, bb_info->gen, bb_info->kill);
2944 scan_reads (insn_info, bb_info->gen, bb_info->kill);
2947 insn_info = insn_info->prev_insn;
2952 /* Set the gen set of the exit block, and also any block with no
2953 successors that does not have a wild read. */
2955 static void
2956 dse_step3_exit_block_scan (bb_info_t bb_info)
2958 /* The gen set is all 0's for the exit block except for the
2959 frame_pointer_group. */
2961 if (stores_off_frame_dead_at_return)
2963 unsigned int i;
2964 group_info *group;
2966 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2968 if (group->process_globally && group->frame_related)
2969 bitmap_ior_into (bb_info->gen, group->group_kill);
2975 /* Find all of the blocks that are not backwards reachable from the
2976 exit block or any block with no successors (BB). These are the
2977 infinite loops or infinite self loops. These blocks will still
2978 have their bits set in UNREACHABLE_BLOCKS. */
2980 static void
2981 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
2983 edge e;
2984 edge_iterator ei;
2986 if (bitmap_bit_p (unreachable_blocks, bb->index))
2988 bitmap_clear_bit (unreachable_blocks, bb->index);
2989 FOR_EACH_EDGE (e, ei, bb->preds)
2991 mark_reachable_blocks (unreachable_blocks, e->src);
2996 /* Build the transfer functions for the function. */
2998 static void
2999 dse_step3 ()
3001 basic_block bb;
3002 sbitmap_iterator sbi;
3003 bitmap all_ones = NULL;
3004 unsigned int i;
3006 auto_sbitmap unreachable_blocks (last_basic_block_for_fn (cfun));
3007 bitmap_ones (unreachable_blocks);
3009 FOR_ALL_BB_FN (bb, cfun)
3011 bb_info_t bb_info = bb_table[bb->index];
3012 if (bb_info->gen)
3013 bitmap_clear (bb_info->gen);
3014 else
3015 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3017 if (bb->index == ENTRY_BLOCK)
3019 else if (bb->index == EXIT_BLOCK)
3020 dse_step3_exit_block_scan (bb_info);
3021 else
3022 dse_step3_scan (bb);
3023 if (EDGE_COUNT (bb->succs) == 0)
3024 mark_reachable_blocks (unreachable_blocks, bb);
3026 /* If this is the second time dataflow is run, delete the old
3027 sets. */
3028 if (bb_info->in)
3029 BITMAP_FREE (bb_info->in);
3030 if (bb_info->out)
3031 BITMAP_FREE (bb_info->out);
3034 /* For any block in an infinite loop, we must initialize the out set
3035 to all ones. This could be expensive, but almost never occurs in
3036 practice. However, it is common in regression tests. */
3037 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3039 if (bitmap_bit_p (all_blocks, i))
3041 bb_info_t bb_info = bb_table[i];
3042 if (!all_ones)
3044 unsigned int j;
3045 group_info *group;
3047 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3048 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3049 bitmap_ior_into (all_ones, group->group_kill);
3051 if (!bb_info->out)
3053 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3054 bitmap_copy (bb_info->out, all_ones);
3059 if (all_ones)
3060 BITMAP_FREE (all_ones);
3065 /*----------------------------------------------------------------------------
3066 Fourth step.
3068 Solve the bitvector equations.
3069 ----------------------------------------------------------------------------*/
3072 /* Confluence function for blocks with no successors. Create an out
3073 set from the gen set of the exit block. This block logically has
3074 the exit block as a successor. */
3078 static void
3079 dse_confluence_0 (basic_block bb)
3081 bb_info_t bb_info = bb_table[bb->index];
3083 if (bb->index == EXIT_BLOCK)
3084 return;
3086 if (!bb_info->out)
3088 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3089 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3093 /* Propagate the information from the in set of the dest of E to the
3094 out set of the src of E. If the various in or out sets are not
3095 there, that means they are all ones. */
3097 static bool
3098 dse_confluence_n (edge e)
3100 bb_info_t src_info = bb_table[e->src->index];
3101 bb_info_t dest_info = bb_table[e->dest->index];
3103 if (dest_info->in)
3105 if (src_info->out)
3106 bitmap_and_into (src_info->out, dest_info->in);
3107 else
3109 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3110 bitmap_copy (src_info->out, dest_info->in);
3113 return true;
3117 /* Propagate the info from the out to the in set of BB_INDEX's basic
3118 block. There are three cases:
3120 1) The block has no kill set. In this case the kill set is all
3121 ones. It does not matter what the out set of the block is, none of
3122 the info can reach the top. The only thing that reaches the top is
3123 the gen set and we just copy the set.
3125 2) There is a kill set but no out set and bb has successors. In
3126 this case we just return. Eventually an out set will be created and
3127 it is better to wait than to create a set of ones.
3129 3) There is both a kill and out set. We apply the obvious transfer
3130 function.
3133 static bool
3134 dse_transfer_function (int bb_index)
3136 bb_info_t bb_info = bb_table[bb_index];
3138 if (bb_info->kill)
3140 if (bb_info->out)
3142 /* Case 3 above. */
3143 if (bb_info->in)
3144 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3145 bb_info->out, bb_info->kill);
3146 else
3148 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3149 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3150 bb_info->out, bb_info->kill);
3151 return true;
3154 else
3155 /* Case 2 above. */
3156 return false;
3158 else
3160 /* Case 1 above. If there is already an in set, nothing
3161 happens. */
3162 if (bb_info->in)
3163 return false;
3164 else
3166 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3167 bitmap_copy (bb_info->in, bb_info->gen);
3168 return true;
3173 /* Solve the dataflow equations. */
3175 static void
3176 dse_step4 (void)
3178 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3179 dse_confluence_n, dse_transfer_function,
3180 all_blocks, df_get_postorder (DF_BACKWARD),
3181 df_get_n_blocks (DF_BACKWARD));
3182 if (dump_file && (dump_flags & TDF_DETAILS))
3184 basic_block bb;
3186 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3187 FOR_ALL_BB_FN (bb, cfun)
3189 bb_info_t bb_info = bb_table[bb->index];
3191 df_print_bb_index (bb, dump_file);
3192 if (bb_info->in)
3193 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3194 else
3195 fprintf (dump_file, " in: *MISSING*\n");
3196 if (bb_info->gen)
3197 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3198 else
3199 fprintf (dump_file, " gen: *MISSING*\n");
3200 if (bb_info->kill)
3201 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3202 else
3203 fprintf (dump_file, " kill: *MISSING*\n");
3204 if (bb_info->out)
3205 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3206 else
3207 fprintf (dump_file, " out: *MISSING*\n\n");
3214 /*----------------------------------------------------------------------------
3215 Fifth step.
3217 Delete the stores that can only be deleted using the global information.
3218 ----------------------------------------------------------------------------*/
3221 static void
3222 dse_step5 (void)
3224 basic_block bb;
3225 FOR_EACH_BB_FN (bb, cfun)
3227 bb_info_t bb_info = bb_table[bb->index];
3228 insn_info_t insn_info = bb_info->last_insn;
3229 bitmap v = bb_info->out;
3231 while (insn_info)
3233 bool deleted = false;
3234 if (dump_file && insn_info->insn)
3236 fprintf (dump_file, "starting to process insn %d\n",
3237 INSN_UID (insn_info->insn));
3238 bitmap_print (dump_file, v, " v: ", "\n");
3241 /* There may have been code deleted by the dce pass run before
3242 this phase. */
3243 if (insn_info->insn
3244 && INSN_P (insn_info->insn)
3245 && (!insn_info->cannot_delete)
3246 && (!bitmap_empty_p (v)))
3248 store_info *store_info = insn_info->store_rec;
3250 /* Try to delete the current insn. */
3251 deleted = true;
3253 /* Skip the clobbers. */
3254 while (!store_info->is_set)
3255 store_info = store_info->next;
3257 HOST_WIDE_INT i;
3258 group_info *group_info = rtx_group_vec[store_info->group_id];
3260 for (i = store_info->begin; i < store_info->end; i++)
3262 int index = get_bitmap_index (group_info, i);
3264 if (dump_file && (dump_flags & TDF_DETAILS))
3265 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3266 if (index == 0 || !bitmap_bit_p (v, index))
3268 if (dump_file && (dump_flags & TDF_DETAILS))
3269 fprintf (dump_file, "failing at i = %d\n", (int)i);
3270 deleted = false;
3271 break;
3274 if (deleted)
3276 if (dbg_cnt (dse)
3277 && check_for_inc_dec_1 (insn_info))
3279 delete_insn (insn_info->insn);
3280 insn_info->insn = NULL;
3281 globally_deleted++;
3285 /* We do want to process the local info if the insn was
3286 deleted. For instance, if the insn did a wild read, we
3287 no longer need to trash the info. */
3288 if (insn_info->insn
3289 && INSN_P (insn_info->insn)
3290 && (!deleted))
3292 scan_stores (insn_info->store_rec, v, NULL);
3293 if (insn_info->wild_read)
3295 if (dump_file && (dump_flags & TDF_DETAILS))
3296 fprintf (dump_file, "wild read\n");
3297 bitmap_clear (v);
3299 else if (insn_info->read_rec
3300 || insn_info->non_frame_wild_read)
3302 if (dump_file && !insn_info->non_frame_wild_read)
3303 fprintf (dump_file, "regular read\n");
3304 else if (dump_file && (dump_flags & TDF_DETAILS))
3305 fprintf (dump_file, "non-frame wild read\n");
3306 scan_reads (insn_info, v, NULL);
3310 insn_info = insn_info->prev_insn;
3317 /*----------------------------------------------------------------------------
3318 Sixth step.
3320 Delete stores made redundant by earlier stores (which store the same
3321 value) that couldn't be eliminated.
3322 ----------------------------------------------------------------------------*/
3324 static void
3325 dse_step6 (void)
3327 basic_block bb;
3329 FOR_ALL_BB_FN (bb, cfun)
3331 bb_info_t bb_info = bb_table[bb->index];
3332 insn_info_t insn_info = bb_info->last_insn;
3334 while (insn_info)
3336 /* There may have been code deleted by the dce pass run before
3337 this phase. */
3338 if (insn_info->insn
3339 && INSN_P (insn_info->insn)
3340 && !insn_info->cannot_delete)
3342 store_info *s_info = insn_info->store_rec;
3344 while (s_info && !s_info->is_set)
3345 s_info = s_info->next;
3346 if (s_info
3347 && s_info->redundant_reason
3348 && s_info->redundant_reason->insn
3349 && INSN_P (s_info->redundant_reason->insn))
3351 rtx_insn *rinsn = s_info->redundant_reason->insn;
3352 if (dump_file && (dump_flags & TDF_DETAILS))
3353 fprintf (dump_file, "Locally deleting insn %d "
3354 "because insn %d stores the "
3355 "same value and couldn't be "
3356 "eliminated\n",
3357 INSN_UID (insn_info->insn),
3358 INSN_UID (rinsn));
3359 delete_dead_store_insn (insn_info);
3362 insn_info = insn_info->prev_insn;
3367 /*----------------------------------------------------------------------------
3368 Seventh step.
3370 Destroy everything left standing.
3371 ----------------------------------------------------------------------------*/
3373 static void
3374 dse_step7 (void)
3376 bitmap_obstack_release (&dse_bitmap_obstack);
3377 obstack_free (&dse_obstack, NULL);
3379 end_alias_analysis ();
3380 free (bb_table);
3381 delete rtx_group_table;
3382 rtx_group_table = NULL;
3383 rtx_group_vec.release ();
3384 BITMAP_FREE (all_blocks);
3385 BITMAP_FREE (scratch);
3387 rtx_store_info_pool.release ();
3388 read_info_type_pool.release ();
3389 insn_info_type_pool.release ();
3390 dse_bb_info_type_pool.release ();
3391 group_info_pool.release ();
3392 deferred_change_pool.release ();
3396 /* -------------------------------------------------------------------------
3398 ------------------------------------------------------------------------- */
3400 /* Callback for running pass_rtl_dse. */
3402 static unsigned int
3403 rest_of_handle_dse (void)
3405 df_set_flags (DF_DEFER_INSN_RESCAN);
3407 /* Need the notes since we must track live hardregs in the forwards
3408 direction. */
3409 df_note_add_problem ();
3410 df_analyze ();
3412 dse_step0 ();
3413 dse_step1 ();
3414 dse_step2_init ();
3415 if (dse_step2 ())
3417 df_set_flags (DF_LR_RUN_DCE);
3418 df_analyze ();
3419 if (dump_file && (dump_flags & TDF_DETAILS))
3420 fprintf (dump_file, "doing global processing\n");
3421 dse_step3 ();
3422 dse_step4 ();
3423 dse_step5 ();
3426 dse_step6 ();
3427 dse_step7 ();
3429 if (dump_file)
3430 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d\n",
3431 locally_deleted, globally_deleted);
3433 /* DSE can eliminate potentially-trapping MEMs.
3434 Remove any EH edges associated with them. */
3435 if ((locally_deleted || globally_deleted)
3436 && cfun->can_throw_non_call_exceptions
3437 && purge_all_dead_edges ())
3438 cleanup_cfg (0);
3440 return 0;
3443 namespace {
3445 const pass_data pass_data_rtl_dse1 =
3447 RTL_PASS, /* type */
3448 "dse1", /* name */
3449 OPTGROUP_NONE, /* optinfo_flags */
3450 TV_DSE1, /* tv_id */
3451 0, /* properties_required */
3452 0, /* properties_provided */
3453 0, /* properties_destroyed */
3454 0, /* todo_flags_start */
3455 TODO_df_finish, /* todo_flags_finish */
3458 class pass_rtl_dse1 : public rtl_opt_pass
3460 public:
3461 pass_rtl_dse1 (gcc::context *ctxt)
3462 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3465 /* opt_pass methods: */
3466 virtual bool gate (function *)
3468 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3471 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3473 }; // class pass_rtl_dse1
3475 } // anon namespace
3477 rtl_opt_pass *
3478 make_pass_rtl_dse1 (gcc::context *ctxt)
3480 return new pass_rtl_dse1 (ctxt);
3483 namespace {
3485 const pass_data pass_data_rtl_dse2 =
3487 RTL_PASS, /* type */
3488 "dse2", /* name */
3489 OPTGROUP_NONE, /* optinfo_flags */
3490 TV_DSE2, /* tv_id */
3491 0, /* properties_required */
3492 0, /* properties_provided */
3493 0, /* properties_destroyed */
3494 0, /* todo_flags_start */
3495 TODO_df_finish, /* todo_flags_finish */
3498 class pass_rtl_dse2 : public rtl_opt_pass
3500 public:
3501 pass_rtl_dse2 (gcc::context *ctxt)
3502 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3505 /* opt_pass methods: */
3506 virtual bool gate (function *)
3508 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3511 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3513 }; // class pass_rtl_dse2
3515 } // anon namespace
3517 rtl_opt_pass *
3518 make_pass_rtl_dse2 (gcc::context *ctxt)
3520 return new pass_rtl_dse2 (ctxt);