* name-lookup.h (lookup_field_1): Delete.
[official-gcc.git] / gcc / dse.c
blobcff3ac47356fb3feec39985a7c45f71398e3bb8c
1 /* RTL dead store elimination.
2 Copyright (C) 2005-2017 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 "memmodel.h"
36 #include "tm_p.h"
37 #include "gimple-ssa.h"
38 #include "expmed.h"
39 #include "optabs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "alias.h"
43 #include "stor-layout.h"
44 #include "cfgrtl.h"
45 #include "cselib.h"
46 #include "tree-pass.h"
47 #include "explow.h"
48 #include "expr.h"
49 #include "dbgcnt.h"
50 #include "params.h"
51 #include "rtl-iter.h"
52 #include "cfgcleanup.h"
54 /* This file contains three techniques for performing Dead Store
55 Elimination (dse).
57 * The first technique performs dse locally on any base address. It
58 is based on the cselib which is a local value numbering technique.
59 This technique is local to a basic block but deals with a fairly
60 general addresses.
62 * The second technique performs dse globally but is restricted to
63 base addresses that are either constant or are relative to the
64 frame_pointer.
66 * The third technique, (which is only done after register allocation)
67 processes the spill slots. This differs from the second
68 technique because it takes advantage of the fact that spilling is
69 completely free from the effects of aliasing.
71 Logically, dse is a backwards dataflow problem. A store can be
72 deleted if it if cannot be reached in the backward direction by any
73 use of the value being stored. However, the local technique uses a
74 forwards scan of the basic block because cselib requires that the
75 block be processed in that order.
77 The pass is logically broken into 7 steps:
79 0) Initialization.
81 1) The local algorithm, as well as scanning the insns for the two
82 global algorithms.
84 2) Analysis to see if the global algs are necessary. In the case
85 of stores base on a constant address, there must be at least two
86 stores to that address, to make it possible to delete some of the
87 stores. In the case of stores off of the frame or spill related
88 stores, only one store to an address is necessary because those
89 stores die at the end of the function.
91 3) Set up the global dataflow equations based on processing the
92 info parsed in the first step.
94 4) Solve the dataflow equations.
96 5) Delete the insns that the global analysis has indicated are
97 unnecessary.
99 6) Delete insns that store the same value as preceding store
100 where the earlier store couldn't be eliminated.
102 7) Cleanup.
104 This step uses cselib and canon_rtx to build the largest expression
105 possible for each address. This pass is a forwards pass through
106 each basic block. From the point of view of the global technique,
107 the first pass could examine a block in either direction. The
108 forwards ordering is to accommodate cselib.
110 We make a simplifying assumption: addresses fall into four broad
111 categories:
113 1) base has rtx_varies_p == false, offset is constant.
114 2) base has rtx_varies_p == false, offset variable.
115 3) base has rtx_varies_p == true, offset constant.
116 4) base has rtx_varies_p == true, offset variable.
118 The local passes are able to process all 4 kinds of addresses. The
119 global pass only handles 1).
121 The global problem is formulated as follows:
123 A store, S1, to address A, where A is not relative to the stack
124 frame, can be eliminated if all paths from S1 to the end of the
125 function contain another store to A before a read to A.
127 If the address A is relative to the stack frame, a store S2 to A
128 can be eliminated if there are no paths from S2 that reach the
129 end of the function that read A before another store to A. In
130 this case S2 can be deleted if there are paths from S2 to the
131 end of the function that have no reads or writes to A. This
132 second case allows stores to the stack frame to be deleted that
133 would otherwise die when the function returns. This cannot be
134 done if stores_off_frame_dead_at_return is not true. See the doc
135 for that variable for when this variable is false.
137 The global problem is formulated as a backwards set union
138 dataflow problem where the stores are the gens and reads are the
139 kills. Set union problems are rare and require some special
140 handling given our representation of bitmaps. A straightforward
141 implementation requires a lot of bitmaps filled with 1s.
142 These are expensive and cumbersome in our bitmap formulation so
143 care has been taken to avoid large vectors filled with 1s. See
144 the comments in bb_info and in the dataflow confluence functions
145 for details.
147 There are two places for further enhancements to this algorithm:
149 1) The original dse which was embedded in a pass called flow also
150 did local address forwarding. For example in
152 A <- r100
153 ... <- A
155 flow would replace the right hand side of the second insn with a
156 reference to r100. Most of the information is available to add this
157 to this pass. It has not done it because it is a lot of work in
158 the case that either r100 is assigned to between the first and
159 second insn and/or the second insn is a load of part of the value
160 stored by the first insn.
162 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
163 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
164 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
165 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
167 2) The cleaning up of spill code is quite profitable. It currently
168 depends on reading tea leaves and chicken entrails left by reload.
169 This pass depends on reload creating a singleton alias set for each
170 spill slot and telling the next dse pass which of these alias sets
171 are the singletons. Rather than analyze the addresses of the
172 spills, dse's spill processing just does analysis of the loads and
173 stores that use those alias sets. There are three cases where this
174 falls short:
176 a) Reload sometimes creates the slot for one mode of access, and
177 then inserts loads and/or stores for a smaller mode. In this
178 case, the current code just punts on the slot. The proper thing
179 to do is to back out and use one bit vector position for each
180 byte of the entity associated with the slot. This depends on
181 KNOWING that reload always generates the accesses for each of the
182 bytes in some canonical (read that easy to understand several
183 passes after reload happens) way.
185 b) Reload sometimes decides that spill slot it allocated was not
186 large enough for the mode and goes back and allocates more slots
187 with the same mode and alias set. The backout in this case is a
188 little more graceful than (a). In this case the slot is unmarked
189 as being a spill slot and if final address comes out to be based
190 off the frame pointer, the global algorithm handles this slot.
192 c) For any pass that may prespill, there is currently no
193 mechanism to tell the dse pass that the slot being used has the
194 special properties that reload uses. It may be that all that is
195 required is to have those passes make the same calls that reload
196 does, assuming that the alias sets can be manipulated in the same
197 way. */
199 /* There are limits to the size of constant offsets we model for the
200 global problem. There are certainly test cases, that exceed this
201 limit, however, it is unlikely that there are important programs
202 that really have constant offsets this size. */
203 #define MAX_OFFSET (64 * 1024)
205 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
206 on the default obstack because these bitmaps can grow quite large
207 (~2GB for the small (!) test case of PR54146) and we'll hold on to
208 all that memory until the end of the compiler run.
209 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
210 releasing the whole obstack. */
211 static bitmap_obstack dse_bitmap_obstack;
213 /* Obstack for other data. As for above: Kinda nice to be able to
214 throw it all away at the end in one big sweep. */
215 static struct obstack dse_obstack;
217 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
218 static bitmap scratch = NULL;
220 struct insn_info_type;
222 /* This structure holds information about a candidate store. */
223 struct store_info
226 /* False means this is a clobber. */
227 bool is_set;
229 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
230 bool is_large;
232 /* The id of the mem group of the base address. If rtx_varies_p is
233 true, this is -1. Otherwise, it is the index into the group
234 table. */
235 int group_id;
237 /* This is the cselib value. */
238 cselib_val *cse_base;
240 /* This canonized mem. */
241 rtx mem;
243 /* Canonized MEM address for use by canon_true_dependence. */
244 rtx mem_addr;
246 /* The offset of the first and byte before the last byte associated
247 with the operation. */
248 HOST_WIDE_INT begin, end;
250 union
252 /* A bitmask as wide as the number of bytes in the word that
253 contains a 1 if the byte may be needed. The store is unused if
254 all of the bits are 0. This is used if IS_LARGE is false. */
255 unsigned HOST_WIDE_INT small_bitmask;
257 struct
259 /* A bitmap with one bit per byte. Cleared bit means the position
260 is needed. Used if IS_LARGE is false. */
261 bitmap bmap;
263 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
264 equal to END - BEGIN, the whole store is unused. */
265 int count;
266 } large;
267 } positions_needed;
269 /* The next store info for this insn. */
270 struct store_info *next;
272 /* The right hand side of the store. This is used if there is a
273 subsequent reload of the mems address somewhere later in the
274 basic block. */
275 rtx rhs;
277 /* If rhs is or holds a constant, this contains that constant,
278 otherwise NULL. */
279 rtx const_rhs;
281 /* Set if this store stores the same constant value as REDUNDANT_REASON
282 insn stored. These aren't eliminated early, because doing that
283 might prevent the earlier larger store to be eliminated. */
284 struct insn_info_type *redundant_reason;
287 /* Return a bitmask with the first N low bits set. */
289 static unsigned HOST_WIDE_INT
290 lowpart_bitmask (int n)
292 unsigned HOST_WIDE_INT mask = HOST_WIDE_INT_M1U;
293 return mask >> (HOST_BITS_PER_WIDE_INT - n);
296 static object_allocator<store_info> cse_store_info_pool ("cse_store_info_pool");
298 static object_allocator<store_info> rtx_store_info_pool ("rtx_store_info_pool");
300 /* This structure holds information about a load. These are only
301 built for rtx bases. */
302 struct read_info_type
304 /* The id of the mem group of the base address. */
305 int group_id;
307 /* The offset of the first and byte after the last byte associated
308 with the operation. If begin == end == 0, the read did not have
309 a constant offset. */
310 int begin, end;
312 /* The mem being read. */
313 rtx mem;
315 /* The next read_info for this insn. */
316 struct read_info_type *next;
318 typedef struct read_info_type *read_info_t;
320 static object_allocator<read_info_type> read_info_type_pool ("read_info_pool");
322 /* One of these records is created for each insn. */
324 struct insn_info_type
326 /* Set true if the insn contains a store but the insn itself cannot
327 be deleted. This is set if the insn is a parallel and there is
328 more than one non dead output or if the insn is in some way
329 volatile. */
330 bool cannot_delete;
332 /* This field is only used by the global algorithm. It is set true
333 if the insn contains any read of mem except for a (1). This is
334 also set if the insn is a call or has a clobber mem. If the insn
335 contains a wild read, the use_rec will be null. */
336 bool wild_read;
338 /* This is true only for CALL instructions which could potentially read
339 any non-frame memory location. This field is used by the global
340 algorithm. */
341 bool non_frame_wild_read;
343 /* This field is only used for the processing of const functions.
344 These functions cannot read memory, but they can read the stack
345 because that is where they may get their parms. We need to be
346 this conservative because, like the store motion pass, we don't
347 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
348 Moreover, we need to distinguish two cases:
349 1. Before reload (register elimination), the stores related to
350 outgoing arguments are stack pointer based and thus deemed
351 of non-constant base in this pass. This requires special
352 handling but also means that the frame pointer based stores
353 need not be killed upon encountering a const function call.
354 2. After reload, the stores related to outgoing arguments can be
355 either stack pointer or hard frame pointer based. This means
356 that we have no other choice than also killing all the frame
357 pointer based stores upon encountering a const function call.
358 This field is set after reload for const function calls and before
359 reload for const tail function calls on targets where arg pointer
360 is the frame pointer. Having this set is less severe than a wild
361 read, it just means that all the frame related stores are killed
362 rather than all the stores. */
363 bool frame_read;
365 /* This field is only used for the processing of const functions.
366 It is set if the insn may contain a stack pointer based store. */
367 bool stack_pointer_based;
369 /* This is true if any of the sets within the store contains a
370 cselib base. Such stores can only be deleted by the local
371 algorithm. */
372 bool contains_cselib_groups;
374 /* The insn. */
375 rtx_insn *insn;
377 /* The list of mem sets or mem clobbers that are contained in this
378 insn. If the insn is deletable, it contains only one mem set.
379 But it could also contain clobbers. Insns that contain more than
380 one mem set are not deletable, but each of those mems are here in
381 order to provide info to delete other insns. */
382 store_info *store_rec;
384 /* The linked list of mem uses in this insn. Only the reads from
385 rtx bases are listed here. The reads to cselib bases are
386 completely processed during the first scan and so are never
387 created. */
388 read_info_t read_rec;
390 /* The live fixed registers. We assume only fixed registers can
391 cause trouble by being clobbered from an expanded pattern;
392 storing only the live fixed registers (rather than all registers)
393 means less memory needs to be allocated / copied for the individual
394 stores. */
395 regset fixed_regs_live;
397 /* The prev insn in the basic block. */
398 struct insn_info_type * prev_insn;
400 /* The linked list of insns that are in consideration for removal in
401 the forwards pass through the basic block. This pointer may be
402 trash as it is not cleared when a wild read occurs. The only
403 time it is guaranteed to be correct is when the traversal starts
404 at active_local_stores. */
405 struct insn_info_type * next_local_store;
407 typedef struct insn_info_type *insn_info_t;
409 static object_allocator<insn_info_type> insn_info_type_pool ("insn_info_pool");
411 /* The linked list of stores that are under consideration in this
412 basic block. */
413 static insn_info_t active_local_stores;
414 static int active_local_stores_len;
416 struct dse_bb_info_type
418 /* Pointer to the insn info for the last insn in the block. These
419 are linked so this is how all of the insns are reached. During
420 scanning this is the current insn being scanned. */
421 insn_info_t last_insn;
423 /* The info for the global dataflow problem. */
426 /* This is set if the transfer function should and in the wild_read
427 bitmap before applying the kill and gen sets. That vector knocks
428 out most of the bits in the bitmap and thus speeds up the
429 operations. */
430 bool apply_wild_read;
432 /* The following 4 bitvectors hold information about which positions
433 of which stores are live or dead. They are indexed by
434 get_bitmap_index. */
436 /* The set of store positions that exist in this block before a wild read. */
437 bitmap gen;
439 /* The set of load positions that exist in this block above the
440 same position of a store. */
441 bitmap kill;
443 /* The set of stores that reach the top of the block without being
444 killed by a read.
446 Do not represent the in if it is all ones. Note that this is
447 what the bitvector should logically be initialized to for a set
448 intersection problem. However, like the kill set, this is too
449 expensive. So initially, the in set will only be created for the
450 exit block and any block that contains a wild read. */
451 bitmap in;
453 /* The set of stores that reach the bottom of the block from it's
454 successors.
456 Do not represent the in if it is all ones. Note that this is
457 what the bitvector should logically be initialized to for a set
458 intersection problem. However, like the kill and in set, this is
459 too expensive. So what is done is that the confluence operator
460 just initializes the vector from one of the out sets of the
461 successors of the block. */
462 bitmap out;
464 /* The following bitvector is indexed by the reg number. It
465 contains the set of regs that are live at the current instruction
466 being processed. While it contains info for all of the
467 registers, only the hard registers are actually examined. It is used
468 to assure that shift and/or add sequences that are inserted do not
469 accidentally clobber live hard regs. */
470 bitmap regs_live;
473 typedef struct dse_bb_info_type *bb_info_t;
475 static object_allocator<dse_bb_info_type> dse_bb_info_type_pool
476 ("bb_info_pool");
478 /* Table to hold all bb_infos. */
479 static bb_info_t *bb_table;
481 /* There is a group_info for each rtx base that is used to reference
482 memory. There are also not many of the rtx bases because they are
483 very limited in scope. */
485 struct group_info
487 /* The actual base of the address. */
488 rtx rtx_base;
490 /* The sequential id of the base. This allows us to have a
491 canonical ordering of these that is not based on addresses. */
492 int id;
494 /* True if there are any positions that are to be processed
495 globally. */
496 bool process_globally;
498 /* True if the base of this group is either the frame_pointer or
499 hard_frame_pointer. */
500 bool frame_related;
502 /* A mem wrapped around the base pointer for the group in order to do
503 read dependency. It must be given BLKmode in order to encompass all
504 the possible offsets from the base. */
505 rtx base_mem;
507 /* Canonized version of base_mem's address. */
508 rtx canon_base_addr;
510 /* These two sets of two bitmaps are used to keep track of how many
511 stores are actually referencing that position from this base. We
512 only do this for rtx bases as this will be used to assign
513 positions in the bitmaps for the global problem. Bit N is set in
514 store1 on the first store for offset N. Bit N is set in store2
515 for the second store to offset N. This is all we need since we
516 only care about offsets that have two or more stores for them.
518 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
519 for 0 and greater offsets.
521 There is one special case here, for stores into the stack frame,
522 we will or store1 into store2 before deciding which stores look
523 at globally. This is because stores to the stack frame that have
524 no other reads before the end of the function can also be
525 deleted. */
526 bitmap store1_n, store1_p, store2_n, store2_p;
528 /* These bitmaps keep track of offsets in this group escape this function.
529 An offset escapes if it corresponds to a named variable whose
530 addressable flag is set. */
531 bitmap escaped_n, escaped_p;
533 /* The positions in this bitmap have the same assignments as the in,
534 out, gen and kill bitmaps. This bitmap is all zeros except for
535 the positions that are occupied by stores for this group. */
536 bitmap group_kill;
538 /* The offset_map is used to map the offsets from this base into
539 positions in the global bitmaps. It is only created after all of
540 the all of stores have been scanned and we know which ones we
541 care about. */
542 int *offset_map_n, *offset_map_p;
543 int offset_map_size_n, offset_map_size_p;
546 static object_allocator<group_info> group_info_pool ("rtx_group_info_pool");
548 /* Index into the rtx_group_vec. */
549 static int rtx_group_next_id;
552 static vec<group_info *> rtx_group_vec;
555 /* This structure holds the set of changes that are being deferred
556 when removing read operation. See replace_read. */
557 struct deferred_change
560 /* The mem that is being replaced. */
561 rtx *loc;
563 /* The reg it is being replaced with. */
564 rtx reg;
566 struct deferred_change *next;
569 static object_allocator<deferred_change> deferred_change_pool
570 ("deferred_change_pool");
572 static deferred_change *deferred_change_list = NULL;
574 /* This is true except if cfun->stdarg -- i.e. we cannot do
575 this for vararg functions because they play games with the frame. */
576 static bool stores_off_frame_dead_at_return;
578 /* Counter for stats. */
579 static int globally_deleted;
580 static int locally_deleted;
582 static bitmap all_blocks;
584 /* Locations that are killed by calls in the global phase. */
585 static bitmap kill_on_calls;
587 /* The number of bits used in the global bitmaps. */
588 static unsigned int current_position;
590 /*----------------------------------------------------------------------------
591 Zeroth step.
593 Initialization.
594 ----------------------------------------------------------------------------*/
597 /* Hashtable callbacks for maintaining the "bases" field of
598 store_group_info, given that the addresses are function invariants. */
600 struct invariant_group_base_hasher : nofree_ptr_hash <group_info>
602 static inline hashval_t hash (const group_info *);
603 static inline bool equal (const group_info *, const group_info *);
606 inline bool
607 invariant_group_base_hasher::equal (const group_info *gi1,
608 const group_info *gi2)
610 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
613 inline hashval_t
614 invariant_group_base_hasher::hash (const group_info *gi)
616 int do_not_record;
617 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
620 /* Tables of group_info structures, hashed by base value. */
621 static hash_table<invariant_group_base_hasher> *rtx_group_table;
624 /* Get the GROUP for BASE. Add a new group if it is not there. */
626 static group_info *
627 get_group_info (rtx base)
629 struct group_info tmp_gi;
630 group_info *gi;
631 group_info **slot;
633 gcc_assert (base != NULL_RTX);
635 /* Find the store_base_info structure for BASE, creating a new one
636 if necessary. */
637 tmp_gi.rtx_base = base;
638 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
639 gi = *slot;
641 if (gi == NULL)
643 *slot = gi = group_info_pool.allocate ();
644 gi->rtx_base = base;
645 gi->id = rtx_group_next_id++;
646 gi->base_mem = gen_rtx_MEM (BLKmode, base);
647 gi->canon_base_addr = canon_rtx (base);
648 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
649 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
650 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
651 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
652 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
653 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
654 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
655 gi->process_globally = false;
656 gi->frame_related =
657 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
658 gi->offset_map_size_n = 0;
659 gi->offset_map_size_p = 0;
660 gi->offset_map_n = NULL;
661 gi->offset_map_p = NULL;
662 rtx_group_vec.safe_push (gi);
665 return gi;
669 /* Initialization of data structures. */
671 static void
672 dse_step0 (void)
674 locally_deleted = 0;
675 globally_deleted = 0;
677 bitmap_obstack_initialize (&dse_bitmap_obstack);
678 gcc_obstack_init (&dse_obstack);
680 scratch = BITMAP_ALLOC (&reg_obstack);
681 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
684 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
686 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
687 rtx_group_next_id = 0;
689 stores_off_frame_dead_at_return = !cfun->stdarg;
691 init_alias_analysis ();
696 /*----------------------------------------------------------------------------
697 First step.
699 Scan all of the insns. Any random ordering of the blocks is fine.
700 Each block is scanned in forward order to accommodate cselib which
701 is used to remove stores with non-constant bases.
702 ----------------------------------------------------------------------------*/
704 /* Delete all of the store_info recs from INSN_INFO. */
706 static void
707 free_store_info (insn_info_t insn_info)
709 store_info *cur = insn_info->store_rec;
710 while (cur)
712 store_info *next = cur->next;
713 if (cur->is_large)
714 BITMAP_FREE (cur->positions_needed.large.bmap);
715 if (cur->cse_base)
716 cse_store_info_pool.remove (cur);
717 else
718 rtx_store_info_pool.remove (cur);
719 cur = next;
722 insn_info->cannot_delete = true;
723 insn_info->contains_cselib_groups = false;
724 insn_info->store_rec = NULL;
727 struct note_add_store_info
729 rtx_insn *first, *current;
730 regset fixed_regs_live;
731 bool failure;
734 /* Callback for emit_inc_dec_insn_before via note_stores.
735 Check if a register is clobbered which is live afterwards. */
737 static void
738 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
740 rtx_insn *insn;
741 note_add_store_info *info = (note_add_store_info *) data;
743 if (!REG_P (loc))
744 return;
746 /* If this register is referenced by the current or an earlier insn,
747 that's OK. E.g. this applies to the register that is being incremented
748 with this addition. */
749 for (insn = info->first;
750 insn != NEXT_INSN (info->current);
751 insn = NEXT_INSN (insn))
752 if (reg_referenced_p (loc, PATTERN (insn)))
753 return;
755 /* If we come here, we have a clobber of a register that's only OK
756 if that register is not live. If we don't have liveness information
757 available, fail now. */
758 if (!info->fixed_regs_live)
760 info->failure = true;
761 return;
763 /* Now check if this is a live fixed register. */
764 unsigned int end_regno = END_REGNO (loc);
765 for (unsigned int regno = REGNO (loc); regno < end_regno; ++regno)
766 if (REGNO_REG_SET_P (info->fixed_regs_live, regno))
767 info->failure = true;
770 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
771 SRC + SRCOFF before insn ARG. */
773 static int
774 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
775 rtx op ATTRIBUTE_UNUSED,
776 rtx dest, rtx src, rtx srcoff, void *arg)
778 insn_info_t insn_info = (insn_info_t) arg;
779 rtx_insn *insn = insn_info->insn, *new_insn, *cur;
780 note_add_store_info info;
782 /* We can reuse all operands without copying, because we are about
783 to delete the insn that contained it. */
784 if (srcoff)
786 start_sequence ();
787 emit_insn (gen_add3_insn (dest, src, srcoff));
788 new_insn = get_insns ();
789 end_sequence ();
791 else
792 new_insn = gen_move_insn (dest, src);
793 info.first = new_insn;
794 info.fixed_regs_live = insn_info->fixed_regs_live;
795 info.failure = false;
796 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
798 info.current = cur;
799 note_stores (PATTERN (cur), note_add_store, &info);
802 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
803 return it immediately, communicating the failure to its caller. */
804 if (info.failure)
805 return 1;
807 emit_insn_before (new_insn, insn);
809 return 0;
812 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
813 is there, is split into a separate insn.
814 Return true on success (or if there was nothing to do), false on failure. */
816 static bool
817 check_for_inc_dec_1 (insn_info_t insn_info)
819 rtx_insn *insn = insn_info->insn;
820 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
821 if (note)
822 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
823 insn_info) == 0;
824 return true;
828 /* Entry point for postreload. If you work on reload_cse, or you need this
829 anywhere else, consider if you can provide register liveness information
830 and add a parameter to this function so that it can be passed down in
831 insn_info.fixed_regs_live. */
832 bool
833 check_for_inc_dec (rtx_insn *insn)
835 insn_info_type insn_info;
836 rtx note;
838 insn_info.insn = insn;
839 insn_info.fixed_regs_live = NULL;
840 note = find_reg_note (insn, REG_INC, NULL_RTX);
841 if (note)
842 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
843 &insn_info) == 0;
844 return true;
847 /* Delete the insn and free all of the fields inside INSN_INFO. */
849 static void
850 delete_dead_store_insn (insn_info_t insn_info)
852 read_info_t read_info;
854 if (!dbg_cnt (dse))
855 return;
857 if (!check_for_inc_dec_1 (insn_info))
858 return;
859 if (dump_file && (dump_flags & TDF_DETAILS))
860 fprintf (dump_file, "Locally deleting insn %d\n",
861 INSN_UID (insn_info->insn));
863 free_store_info (insn_info);
864 read_info = insn_info->read_rec;
866 while (read_info)
868 read_info_t next = read_info->next;
869 read_info_type_pool.remove (read_info);
870 read_info = next;
872 insn_info->read_rec = NULL;
874 delete_insn (insn_info->insn);
875 locally_deleted++;
876 insn_info->insn = NULL;
878 insn_info->wild_read = false;
881 /* Return whether DECL, a local variable, can possibly escape the current
882 function scope. */
884 static bool
885 local_variable_can_escape (tree decl)
887 if (TREE_ADDRESSABLE (decl))
888 return true;
890 /* If this is a partitioned variable, we need to consider all the variables
891 in the partition. This is necessary because a store into one of them can
892 be replaced with a store into another and this may not change the outcome
893 of the escape analysis. */
894 if (cfun->gimple_df->decls_to_pointers != NULL)
896 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
897 if (namep)
898 return TREE_ADDRESSABLE (*namep);
901 return false;
904 /* Return whether EXPR can possibly escape the current function scope. */
906 static bool
907 can_escape (tree expr)
909 tree base;
910 if (!expr)
911 return true;
912 base = get_base_address (expr);
913 if (DECL_P (base)
914 && !may_be_aliased (base)
915 && !(VAR_P (base)
916 && !DECL_EXTERNAL (base)
917 && !TREE_STATIC (base)
918 && local_variable_can_escape (base)))
919 return false;
920 return true;
923 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
924 OFFSET and WIDTH. */
926 static void
927 set_usage_bits (group_info *group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
928 tree expr)
930 HOST_WIDE_INT i;
931 bool expr_escapes = can_escape (expr);
932 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
933 for (i=offset; i<offset+width; i++)
935 bitmap store1;
936 bitmap store2;
937 bitmap escaped;
938 int ai;
939 if (i < 0)
941 store1 = group->store1_n;
942 store2 = group->store2_n;
943 escaped = group->escaped_n;
944 ai = -i;
946 else
948 store1 = group->store1_p;
949 store2 = group->store2_p;
950 escaped = group->escaped_p;
951 ai = i;
954 if (!bitmap_set_bit (store1, ai))
955 bitmap_set_bit (store2, ai);
956 else
958 if (i < 0)
960 if (group->offset_map_size_n < ai)
961 group->offset_map_size_n = ai;
963 else
965 if (group->offset_map_size_p < ai)
966 group->offset_map_size_p = ai;
969 if (expr_escapes)
970 bitmap_set_bit (escaped, ai);
974 static void
975 reset_active_stores (void)
977 active_local_stores = NULL;
978 active_local_stores_len = 0;
981 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
983 static void
984 free_read_records (bb_info_t bb_info)
986 insn_info_t insn_info = bb_info->last_insn;
987 read_info_t *ptr = &insn_info->read_rec;
988 while (*ptr)
990 read_info_t next = (*ptr)->next;
991 read_info_type_pool.remove (*ptr);
992 *ptr = next;
996 /* Set the BB_INFO so that the last insn is marked as a wild read. */
998 static void
999 add_wild_read (bb_info_t bb_info)
1001 insn_info_t insn_info = bb_info->last_insn;
1002 insn_info->wild_read = true;
1003 free_read_records (bb_info);
1004 reset_active_stores ();
1007 /* Set the BB_INFO so that the last insn is marked as a wild read of
1008 non-frame locations. */
1010 static void
1011 add_non_frame_wild_read (bb_info_t bb_info)
1013 insn_info_t insn_info = bb_info->last_insn;
1014 insn_info->non_frame_wild_read = true;
1015 free_read_records (bb_info);
1016 reset_active_stores ();
1019 /* Return true if X is a constant or one of the registers that behave
1020 as a constant over the life of a function. This is equivalent to
1021 !rtx_varies_p for memory addresses. */
1023 static bool
1024 const_or_frame_p (rtx x)
1026 if (CONSTANT_P (x))
1027 return true;
1029 if (GET_CODE (x) == REG)
1031 /* Note that we have to test for the actual rtx used for the frame
1032 and arg pointers and not just the register number in case we have
1033 eliminated the frame and/or arg pointer and are using it
1034 for pseudos. */
1035 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1036 /* The arg pointer varies if it is not a fixed register. */
1037 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1038 || x == pic_offset_table_rtx)
1039 return true;
1040 return false;
1043 return false;
1046 /* Take all reasonable action to put the address of MEM into the form
1047 that we can do analysis on.
1049 The gold standard is to get the address into the form: address +
1050 OFFSET where address is something that rtx_varies_p considers a
1051 constant. When we can get the address in this form, we can do
1052 global analysis on it. Note that for constant bases, address is
1053 not actually returned, only the group_id. The address can be
1054 obtained from that.
1056 If that fails, we try cselib to get a value we can at least use
1057 locally. If that fails we return false.
1059 The GROUP_ID is set to -1 for cselib bases and the index of the
1060 group for non_varying bases.
1062 FOR_READ is true if this is a mem read and false if not. */
1064 static bool
1065 canon_address (rtx mem,
1066 int *group_id,
1067 HOST_WIDE_INT *offset,
1068 cselib_val **base)
1070 machine_mode address_mode = get_address_mode (mem);
1071 rtx mem_address = XEXP (mem, 0);
1072 rtx expanded_address, address;
1073 int expanded;
1075 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1077 if (dump_file && (dump_flags & TDF_DETAILS))
1079 fprintf (dump_file, " mem: ");
1080 print_inline_rtx (dump_file, mem_address, 0);
1081 fprintf (dump_file, "\n");
1084 /* First see if just canon_rtx (mem_address) is const or frame,
1085 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1086 address = NULL_RTX;
1087 for (expanded = 0; expanded < 2; expanded++)
1089 if (expanded)
1091 /* Use cselib to replace all of the reg references with the full
1092 expression. This will take care of the case where we have
1094 r_x = base + offset;
1095 val = *r_x;
1097 by making it into
1099 val = *(base + offset); */
1101 expanded_address = cselib_expand_value_rtx (mem_address,
1102 scratch, 5);
1104 /* If this fails, just go with the address from first
1105 iteration. */
1106 if (!expanded_address)
1107 break;
1109 else
1110 expanded_address = mem_address;
1112 /* Split the address into canonical BASE + OFFSET terms. */
1113 address = canon_rtx (expanded_address);
1115 *offset = 0;
1117 if (dump_file && (dump_flags & TDF_DETAILS))
1119 if (expanded)
1121 fprintf (dump_file, "\n after cselib_expand address: ");
1122 print_inline_rtx (dump_file, expanded_address, 0);
1123 fprintf (dump_file, "\n");
1126 fprintf (dump_file, "\n after canon_rtx address: ");
1127 print_inline_rtx (dump_file, address, 0);
1128 fprintf (dump_file, "\n");
1131 if (GET_CODE (address) == CONST)
1132 address = XEXP (address, 0);
1134 if (GET_CODE (address) == PLUS
1135 && CONST_INT_P (XEXP (address, 1)))
1137 *offset = INTVAL (XEXP (address, 1));
1138 address = XEXP (address, 0);
1141 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1142 && const_or_frame_p (address))
1144 group_info *group = get_group_info (address);
1146 if (dump_file && (dump_flags & TDF_DETAILS))
1147 fprintf (dump_file, " gid=%d offset=%d \n",
1148 group->id, (int)*offset);
1149 *base = NULL;
1150 *group_id = group->id;
1151 return true;
1155 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1156 *group_id = -1;
1158 if (*base == NULL)
1160 if (dump_file && (dump_flags & TDF_DETAILS))
1161 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1162 return false;
1164 if (dump_file && (dump_flags & TDF_DETAILS))
1165 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1166 (*base)->uid, (*base)->hash, (int)*offset);
1167 return true;
1171 /* Clear the rhs field from the active_local_stores array. */
1173 static void
1174 clear_rhs_from_active_local_stores (void)
1176 insn_info_t ptr = active_local_stores;
1178 while (ptr)
1180 store_info *store_info = ptr->store_rec;
1181 /* Skip the clobbers. */
1182 while (!store_info->is_set)
1183 store_info = store_info->next;
1185 store_info->rhs = NULL;
1186 store_info->const_rhs = NULL;
1188 ptr = ptr->next_local_store;
1193 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1195 static inline void
1196 set_position_unneeded (store_info *s_info, int pos)
1198 if (__builtin_expect (s_info->is_large, false))
1200 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1201 s_info->positions_needed.large.count++;
1203 else
1204 s_info->positions_needed.small_bitmask
1205 &= ~(HOST_WIDE_INT_1U << pos);
1208 /* Mark the whole store S_INFO as unneeded. */
1210 static inline void
1211 set_all_positions_unneeded (store_info *s_info)
1213 if (__builtin_expect (s_info->is_large, false))
1215 int pos, end = s_info->end - s_info->begin;
1216 for (pos = 0; pos < end; pos++)
1217 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1218 s_info->positions_needed.large.count = end;
1220 else
1221 s_info->positions_needed.small_bitmask = HOST_WIDE_INT_0U;
1224 /* Return TRUE if any bytes from S_INFO store are needed. */
1226 static inline bool
1227 any_positions_needed_p (store_info *s_info)
1229 if (__builtin_expect (s_info->is_large, false))
1230 return (s_info->positions_needed.large.count
1231 < s_info->end - s_info->begin);
1232 else
1233 return (s_info->positions_needed.small_bitmask != HOST_WIDE_INT_0U);
1236 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1237 store are needed. */
1239 static inline bool
1240 all_positions_needed_p (store_info *s_info, int start, int width)
1242 if (__builtin_expect (s_info->is_large, false))
1244 int end = start + width;
1245 while (start < end)
1246 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1247 return false;
1248 return true;
1250 else
1252 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1253 return (s_info->positions_needed.small_bitmask & mask) == mask;
1258 static rtx get_stored_val (store_info *, machine_mode, HOST_WIDE_INT,
1259 HOST_WIDE_INT, basic_block, bool);
1262 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1263 there is a candidate store, after adding it to the appropriate
1264 local store group if so. */
1266 static int
1267 record_store (rtx body, bb_info_t bb_info)
1269 rtx mem, rhs, const_rhs, mem_addr;
1270 HOST_WIDE_INT offset = 0;
1271 HOST_WIDE_INT width = 0;
1272 insn_info_t insn_info = bb_info->last_insn;
1273 store_info *store_info = NULL;
1274 int group_id;
1275 cselib_val *base = NULL;
1276 insn_info_t ptr, last, redundant_reason;
1277 bool store_is_unused;
1279 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1280 return 0;
1282 mem = SET_DEST (body);
1284 /* If this is not used, then this cannot be used to keep the insn
1285 from being deleted. On the other hand, it does provide something
1286 that can be used to prove that another store is dead. */
1287 store_is_unused
1288 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1290 /* Check whether that value is a suitable memory location. */
1291 if (!MEM_P (mem))
1293 /* If the set or clobber is unused, then it does not effect our
1294 ability to get rid of the entire insn. */
1295 if (!store_is_unused)
1296 insn_info->cannot_delete = true;
1297 return 0;
1300 /* At this point we know mem is a mem. */
1301 if (GET_MODE (mem) == BLKmode)
1303 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1305 if (dump_file && (dump_flags & TDF_DETAILS))
1306 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1307 add_wild_read (bb_info);
1308 insn_info->cannot_delete = true;
1309 return 0;
1311 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1312 as memset (addr, 0, 36); */
1313 else if (!MEM_SIZE_KNOWN_P (mem)
1314 || MEM_SIZE (mem) <= 0
1315 || MEM_SIZE (mem) > MAX_OFFSET
1316 || GET_CODE (body) != SET
1317 || !CONST_INT_P (SET_SRC (body)))
1319 if (!store_is_unused)
1321 /* If the set or clobber is unused, then it does not effect our
1322 ability to get rid of the entire insn. */
1323 insn_info->cannot_delete = true;
1324 clear_rhs_from_active_local_stores ();
1326 return 0;
1330 /* We can still process a volatile mem, we just cannot delete it. */
1331 if (MEM_VOLATILE_P (mem))
1332 insn_info->cannot_delete = true;
1334 if (!canon_address (mem, &group_id, &offset, &base))
1336 clear_rhs_from_active_local_stores ();
1337 return 0;
1340 if (GET_MODE (mem) == BLKmode)
1341 width = MEM_SIZE (mem);
1342 else
1343 width = GET_MODE_SIZE (GET_MODE (mem));
1345 if (group_id >= 0)
1347 /* In the restrictive case where the base is a constant or the
1348 frame pointer we can do global analysis. */
1350 group_info *group
1351 = rtx_group_vec[group_id];
1352 tree expr = MEM_EXPR (mem);
1354 store_info = rtx_store_info_pool.allocate ();
1355 set_usage_bits (group, offset, width, expr);
1357 if (dump_file && (dump_flags & TDF_DETAILS))
1358 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1359 group_id, (int)offset, (int)(offset+width));
1361 else
1363 if (may_be_sp_based_p (XEXP (mem, 0)))
1364 insn_info->stack_pointer_based = true;
1365 insn_info->contains_cselib_groups = true;
1367 store_info = cse_store_info_pool.allocate ();
1368 group_id = -1;
1370 if (dump_file && (dump_flags & TDF_DETAILS))
1371 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1372 (int)offset, (int)(offset+width));
1375 const_rhs = rhs = NULL_RTX;
1376 if (GET_CODE (body) == SET
1377 /* No place to keep the value after ra. */
1378 && !reload_completed
1379 && (REG_P (SET_SRC (body))
1380 || GET_CODE (SET_SRC (body)) == SUBREG
1381 || CONSTANT_P (SET_SRC (body)))
1382 && !MEM_VOLATILE_P (mem)
1383 /* Sometimes the store and reload is used for truncation and
1384 rounding. */
1385 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1387 rhs = SET_SRC (body);
1388 if (CONSTANT_P (rhs))
1389 const_rhs = rhs;
1390 else if (body == PATTERN (insn_info->insn))
1392 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1393 if (tem && CONSTANT_P (XEXP (tem, 0)))
1394 const_rhs = XEXP (tem, 0);
1396 if (const_rhs == NULL_RTX && REG_P (rhs))
1398 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1400 if (tem && CONSTANT_P (tem))
1401 const_rhs = tem;
1405 /* Check to see if this stores causes some other stores to be
1406 dead. */
1407 ptr = active_local_stores;
1408 last = NULL;
1409 redundant_reason = NULL;
1410 mem = canon_rtx (mem);
1412 if (group_id < 0)
1413 mem_addr = base->val_rtx;
1414 else
1416 group_info *group = rtx_group_vec[group_id];
1417 mem_addr = group->canon_base_addr;
1419 if (offset)
1420 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1422 while (ptr)
1424 insn_info_t next = ptr->next_local_store;
1425 struct store_info *s_info = ptr->store_rec;
1426 bool del = true;
1428 /* Skip the clobbers. We delete the active insn if this insn
1429 shadows the set. To have been put on the active list, it
1430 has exactly on set. */
1431 while (!s_info->is_set)
1432 s_info = s_info->next;
1434 if (s_info->group_id == group_id && s_info->cse_base == base)
1436 HOST_WIDE_INT i;
1437 if (dump_file && (dump_flags & TDF_DETAILS))
1438 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1439 INSN_UID (ptr->insn), s_info->group_id,
1440 (int)s_info->begin, (int)s_info->end);
1442 /* Even if PTR won't be eliminated as unneeded, if both
1443 PTR and this insn store the same constant value, we might
1444 eliminate this insn instead. */
1445 if (s_info->const_rhs
1446 && const_rhs
1447 && offset >= s_info->begin
1448 && offset + width <= s_info->end
1449 && all_positions_needed_p (s_info, offset - s_info->begin,
1450 width))
1452 if (GET_MODE (mem) == BLKmode)
1454 if (GET_MODE (s_info->mem) == BLKmode
1455 && s_info->const_rhs == const_rhs)
1456 redundant_reason = ptr;
1458 else if (s_info->const_rhs == const0_rtx
1459 && const_rhs == const0_rtx)
1460 redundant_reason = ptr;
1461 else
1463 rtx val;
1464 start_sequence ();
1465 val = get_stored_val (s_info, GET_MODE (mem),
1466 offset, offset + width,
1467 BLOCK_FOR_INSN (insn_info->insn),
1468 true);
1469 if (get_insns () != NULL)
1470 val = NULL_RTX;
1471 end_sequence ();
1472 if (val && rtx_equal_p (val, const_rhs))
1473 redundant_reason = ptr;
1477 for (i = MAX (offset, s_info->begin);
1478 i < offset + width && i < s_info->end;
1479 i++)
1480 set_position_unneeded (s_info, i - s_info->begin);
1482 else if (s_info->rhs)
1483 /* Need to see if it is possible for this store to overwrite
1484 the value of store_info. If it is, set the rhs to NULL to
1485 keep it from being used to remove a load. */
1487 if (canon_output_dependence (s_info->mem, true,
1488 mem, GET_MODE (mem),
1489 mem_addr))
1491 s_info->rhs = NULL;
1492 s_info->const_rhs = NULL;
1496 /* An insn can be deleted if every position of every one of
1497 its s_infos is zero. */
1498 if (any_positions_needed_p (s_info))
1499 del = false;
1501 if (del)
1503 insn_info_t insn_to_delete = ptr;
1505 active_local_stores_len--;
1506 if (last)
1507 last->next_local_store = ptr->next_local_store;
1508 else
1509 active_local_stores = ptr->next_local_store;
1511 if (!insn_to_delete->cannot_delete)
1512 delete_dead_store_insn (insn_to_delete);
1514 else
1515 last = ptr;
1517 ptr = next;
1520 /* Finish filling in the store_info. */
1521 store_info->next = insn_info->store_rec;
1522 insn_info->store_rec = store_info;
1523 store_info->mem = mem;
1524 store_info->mem_addr = mem_addr;
1525 store_info->cse_base = base;
1526 if (width > HOST_BITS_PER_WIDE_INT)
1528 store_info->is_large = true;
1529 store_info->positions_needed.large.count = 0;
1530 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1532 else
1534 store_info->is_large = false;
1535 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1537 store_info->group_id = group_id;
1538 store_info->begin = offset;
1539 store_info->end = offset + width;
1540 store_info->is_set = GET_CODE (body) == SET;
1541 store_info->rhs = rhs;
1542 store_info->const_rhs = const_rhs;
1543 store_info->redundant_reason = redundant_reason;
1545 /* If this is a clobber, we return 0. We will only be able to
1546 delete this insn if there is only one store USED store, but we
1547 can use the clobber to delete other stores earlier. */
1548 return store_info->is_set ? 1 : 0;
1552 static void
1553 dump_insn_info (const char * start, insn_info_t insn_info)
1555 fprintf (dump_file, "%s insn=%d %s\n", start,
1556 INSN_UID (insn_info->insn),
1557 insn_info->store_rec ? "has store" : "naked");
1561 /* If the modes are different and the value's source and target do not
1562 line up, we need to extract the value from lower part of the rhs of
1563 the store, shift it, and then put it into a form that can be shoved
1564 into the read_insn. This function generates a right SHIFT of a
1565 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1566 shift sequence is returned or NULL if we failed to find a
1567 shift. */
1569 static rtx
1570 find_shift_sequence (int access_size,
1571 store_info *store_info,
1572 machine_mode read_mode,
1573 int shift, bool speed, bool require_cst)
1575 machine_mode store_mode = GET_MODE (store_info->mem);
1576 scalar_int_mode new_mode;
1577 rtx read_reg = NULL;
1579 /* Some machines like the x86 have shift insns for each size of
1580 operand. Other machines like the ppc or the ia-64 may only have
1581 shift insns that shift values within 32 or 64 bit registers.
1582 This loop tries to find the smallest shift insn that will right
1583 justify the value we want to read but is available in one insn on
1584 the machine. */
1586 opt_scalar_int_mode new_mode_iter;
1587 FOR_EACH_MODE_FROM (new_mode_iter,
1588 smallest_int_mode_for_size (access_size * BITS_PER_UNIT))
1590 rtx target, new_reg, new_lhs;
1591 rtx_insn *shift_seq, *insn;
1592 int cost;
1594 new_mode = new_mode_iter.require ();
1595 if (GET_MODE_BITSIZE (new_mode) > BITS_PER_WORD)
1596 break;
1598 /* If a constant was stored into memory, try to simplify it here,
1599 otherwise the cost of the shift might preclude this optimization
1600 e.g. at -Os, even when no actual shift will be needed. */
1601 if (store_info->const_rhs)
1603 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1604 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1605 store_mode, byte);
1606 if (ret && CONSTANT_P (ret))
1608 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1609 ret, GEN_INT (shift));
1610 if (ret && CONSTANT_P (ret))
1612 byte = subreg_lowpart_offset (read_mode, new_mode);
1613 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1614 if (ret && CONSTANT_P (ret)
1615 && (set_src_cost (ret, read_mode, speed)
1616 <= COSTS_N_INSNS (1)))
1617 return ret;
1622 if (require_cst)
1623 return NULL_RTX;
1625 /* Try a wider mode if truncating the store mode to NEW_MODE
1626 requires a real instruction. */
1627 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1628 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1629 continue;
1631 /* Also try a wider mode if the necessary punning is either not
1632 desirable or not possible. */
1633 if (!CONSTANT_P (store_info->rhs)
1634 && !targetm.modes_tieable_p (new_mode, store_mode))
1635 continue;
1637 new_reg = gen_reg_rtx (new_mode);
1639 start_sequence ();
1641 /* In theory we could also check for an ashr. Ian Taylor knows
1642 of one dsp where the cost of these two was not the same. But
1643 this really is a rare case anyway. */
1644 target = expand_binop (new_mode, lshr_optab, new_reg,
1645 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1647 shift_seq = get_insns ();
1648 end_sequence ();
1650 if (target != new_reg || shift_seq == NULL)
1651 continue;
1653 cost = 0;
1654 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1655 if (INSN_P (insn))
1656 cost += insn_rtx_cost (PATTERN (insn), speed);
1658 /* The computation up to here is essentially independent
1659 of the arguments and could be precomputed. It may
1660 not be worth doing so. We could precompute if
1661 worthwhile or at least cache the results. The result
1662 technically depends on both SHIFT and ACCESS_SIZE,
1663 but in practice the answer will depend only on ACCESS_SIZE. */
1665 if (cost > COSTS_N_INSNS (1))
1666 continue;
1668 new_lhs = extract_low_bits (new_mode, store_mode,
1669 copy_rtx (store_info->rhs));
1670 if (new_lhs == NULL_RTX)
1671 continue;
1673 /* We found an acceptable shift. Generate a move to
1674 take the value from the store and put it into the
1675 shift pseudo, then shift it, then generate another
1676 move to put in into the target of the read. */
1677 emit_move_insn (new_reg, new_lhs);
1678 emit_insn (shift_seq);
1679 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1680 break;
1683 return read_reg;
1687 /* Call back for note_stores to find the hard regs set or clobbered by
1688 insn. Data is a bitmap of the hardregs set so far. */
1690 static void
1691 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1693 bitmap regs_set = (bitmap) data;
1695 if (REG_P (x)
1696 && HARD_REGISTER_P (x))
1697 bitmap_set_range (regs_set, REGNO (x), REG_NREGS (x));
1700 /* Helper function for replace_read and record_store.
1701 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1702 to one before READ_END bytes read in READ_MODE. Return NULL
1703 if not successful. If REQUIRE_CST is true, return always constant. */
1705 static rtx
1706 get_stored_val (store_info *store_info, machine_mode read_mode,
1707 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1708 basic_block bb, bool require_cst)
1710 machine_mode store_mode = GET_MODE (store_info->mem);
1711 int shift;
1712 int access_size; /* In bytes. */
1713 rtx read_reg;
1715 /* To get here the read is within the boundaries of the write so
1716 shift will never be negative. Start out with the shift being in
1717 bytes. */
1718 if (store_mode == BLKmode)
1719 shift = 0;
1720 else if (BYTES_BIG_ENDIAN)
1721 shift = store_info->end - read_end;
1722 else
1723 shift = read_begin - store_info->begin;
1725 access_size = shift + GET_MODE_SIZE (read_mode);
1727 /* From now on it is bits. */
1728 shift *= BITS_PER_UNIT;
1730 if (shift)
1731 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1732 optimize_bb_for_speed_p (bb),
1733 require_cst);
1734 else if (store_mode == BLKmode)
1736 /* The store is a memset (addr, const_val, const_size). */
1737 gcc_assert (CONST_INT_P (store_info->rhs));
1738 scalar_int_mode int_store_mode;
1739 if (!int_mode_for_mode (read_mode).exists (&int_store_mode))
1740 read_reg = NULL_RTX;
1741 else if (store_info->rhs == const0_rtx)
1742 read_reg = extract_low_bits (read_mode, int_store_mode, const0_rtx);
1743 else if (GET_MODE_BITSIZE (int_store_mode) > HOST_BITS_PER_WIDE_INT
1744 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1745 read_reg = NULL_RTX;
1746 else
1748 unsigned HOST_WIDE_INT c
1749 = INTVAL (store_info->rhs)
1750 & ((HOST_WIDE_INT_1 << BITS_PER_UNIT) - 1);
1751 int shift = BITS_PER_UNIT;
1752 while (shift < HOST_BITS_PER_WIDE_INT)
1754 c |= (c << shift);
1755 shift <<= 1;
1757 read_reg = gen_int_mode (c, int_store_mode);
1758 read_reg = extract_low_bits (read_mode, int_store_mode, read_reg);
1761 else if (store_info->const_rhs
1762 && (require_cst
1763 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1764 read_reg = extract_low_bits (read_mode, store_mode,
1765 copy_rtx (store_info->const_rhs));
1766 else
1767 read_reg = extract_low_bits (read_mode, store_mode,
1768 copy_rtx (store_info->rhs));
1769 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1770 read_reg = NULL_RTX;
1771 return read_reg;
1774 /* Take a sequence of:
1775 A <- r1
1777 ... <- A
1779 and change it into
1780 r2 <- r1
1781 A <- r1
1783 ... <- r2
1787 r3 <- extract (r1)
1788 r3 <- r3 >> shift
1789 r2 <- extract (r3)
1790 ... <- r2
1794 r2 <- extract (r1)
1795 ... <- r2
1797 Depending on the alignment and the mode of the store and
1798 subsequent load.
1801 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1802 and READ_INSN are for the read. Return true if the replacement
1803 went ok. */
1805 static bool
1806 replace_read (store_info *store_info, insn_info_t store_insn,
1807 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1808 bitmap regs_live)
1810 machine_mode store_mode = GET_MODE (store_info->mem);
1811 machine_mode read_mode = GET_MODE (read_info->mem);
1812 rtx_insn *insns, *this_insn;
1813 rtx read_reg;
1814 basic_block bb;
1816 if (!dbg_cnt (dse))
1817 return false;
1819 /* Create a sequence of instructions to set up the read register.
1820 This sequence goes immediately before the store and its result
1821 is read by the load.
1823 We need to keep this in perspective. We are replacing a read
1824 with a sequence of insns, but the read will almost certainly be
1825 in cache, so it is not going to be an expensive one. Thus, we
1826 are not willing to do a multi insn shift or worse a subroutine
1827 call to get rid of the read. */
1828 if (dump_file && (dump_flags & TDF_DETAILS))
1829 fprintf (dump_file, "trying to replace %smode load in insn %d"
1830 " from %smode store in insn %d\n",
1831 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1832 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1833 start_sequence ();
1834 bb = BLOCK_FOR_INSN (read_insn->insn);
1835 read_reg = get_stored_val (store_info,
1836 read_mode, read_info->begin, read_info->end,
1837 bb, false);
1838 if (read_reg == NULL_RTX)
1840 end_sequence ();
1841 if (dump_file && (dump_flags & TDF_DETAILS))
1842 fprintf (dump_file, " -- could not extract bits of stored value\n");
1843 return false;
1845 /* Force the value into a new register so that it won't be clobbered
1846 between the store and the load. */
1847 read_reg = copy_to_mode_reg (read_mode, read_reg);
1848 insns = get_insns ();
1849 end_sequence ();
1851 if (insns != NULL_RTX)
1853 /* Now we have to scan the set of new instructions to see if the
1854 sequence contains and sets of hardregs that happened to be
1855 live at this point. For instance, this can happen if one of
1856 the insns sets the CC and the CC happened to be live at that
1857 point. This does occasionally happen, see PR 37922. */
1858 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
1860 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
1861 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
1863 bitmap_and_into (regs_set, regs_live);
1864 if (!bitmap_empty_p (regs_set))
1866 if (dump_file && (dump_flags & TDF_DETAILS))
1868 fprintf (dump_file,
1869 "abandoning replacement because sequence clobbers live hardregs:");
1870 df_print_regset (dump_file, regs_set);
1873 BITMAP_FREE (regs_set);
1874 return false;
1876 BITMAP_FREE (regs_set);
1879 if (validate_change (read_insn->insn, loc, read_reg, 0))
1881 deferred_change *change = deferred_change_pool.allocate ();
1883 /* Insert this right before the store insn where it will be safe
1884 from later insns that might change it before the read. */
1885 emit_insn_before (insns, store_insn->insn);
1887 /* And now for the kludge part: cselib croaks if you just
1888 return at this point. There are two reasons for this:
1890 1) Cselib has an idea of how many pseudos there are and
1891 that does not include the new ones we just added.
1893 2) Cselib does not know about the move insn we added
1894 above the store_info, and there is no way to tell it
1895 about it, because it has "moved on".
1897 Problem (1) is fixable with a certain amount of engineering.
1898 Problem (2) is requires starting the bb from scratch. This
1899 could be expensive.
1901 So we are just going to have to lie. The move/extraction
1902 insns are not really an issue, cselib did not see them. But
1903 the use of the new pseudo read_insn is a real problem because
1904 cselib has not scanned this insn. The way that we solve this
1905 problem is that we are just going to put the mem back for now
1906 and when we are finished with the block, we undo this. We
1907 keep a table of mems to get rid of. At the end of the basic
1908 block we can put them back. */
1910 *loc = read_info->mem;
1911 change->next = deferred_change_list;
1912 deferred_change_list = change;
1913 change->loc = loc;
1914 change->reg = read_reg;
1916 /* Get rid of the read_info, from the point of view of the
1917 rest of dse, play like this read never happened. */
1918 read_insn->read_rec = read_info->next;
1919 read_info_type_pool.remove (read_info);
1920 if (dump_file && (dump_flags & TDF_DETAILS))
1922 fprintf (dump_file, " -- replaced the loaded MEM with ");
1923 print_simple_rtl (dump_file, read_reg);
1924 fprintf (dump_file, "\n");
1926 return true;
1928 else
1930 if (dump_file && (dump_flags & TDF_DETAILS))
1932 fprintf (dump_file, " -- replacing the loaded MEM with ");
1933 print_simple_rtl (dump_file, read_reg);
1934 fprintf (dump_file, " led to an invalid instruction\n");
1936 return false;
1940 /* Check the address of MEM *LOC and kill any appropriate stores that may
1941 be active. */
1943 static void
1944 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
1946 rtx mem = *loc, mem_addr;
1947 insn_info_t insn_info;
1948 HOST_WIDE_INT offset = 0;
1949 HOST_WIDE_INT width = 0;
1950 cselib_val *base = NULL;
1951 int group_id;
1952 read_info_t read_info;
1954 insn_info = bb_info->last_insn;
1956 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
1957 || (MEM_VOLATILE_P (mem)))
1959 if (dump_file && (dump_flags & TDF_DETAILS))
1960 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
1961 add_wild_read (bb_info);
1962 insn_info->cannot_delete = true;
1963 return;
1966 /* If it is reading readonly mem, then there can be no conflict with
1967 another write. */
1968 if (MEM_READONLY_P (mem))
1969 return;
1971 if (!canon_address (mem, &group_id, &offset, &base))
1973 if (dump_file && (dump_flags & TDF_DETAILS))
1974 fprintf (dump_file, " adding wild read, canon_address failure.\n");
1975 add_wild_read (bb_info);
1976 return;
1979 if (GET_MODE (mem) == BLKmode)
1980 width = -1;
1981 else
1982 width = GET_MODE_SIZE (GET_MODE (mem));
1984 read_info = read_info_type_pool.allocate ();
1985 read_info->group_id = group_id;
1986 read_info->mem = mem;
1987 read_info->begin = offset;
1988 read_info->end = offset + width;
1989 read_info->next = insn_info->read_rec;
1990 insn_info->read_rec = read_info;
1991 if (group_id < 0)
1992 mem_addr = base->val_rtx;
1993 else
1995 group_info *group = rtx_group_vec[group_id];
1996 mem_addr = group->canon_base_addr;
1998 if (offset)
1999 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2001 if (group_id >= 0)
2003 /* This is the restricted case where the base is a constant or
2004 the frame pointer and offset is a constant. */
2005 insn_info_t i_ptr = active_local_stores;
2006 insn_info_t last = NULL;
2008 if (dump_file && (dump_flags & TDF_DETAILS))
2010 if (width == -1)
2011 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2012 group_id);
2013 else
2014 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2015 group_id, (int)offset, (int)(offset+width));
2018 while (i_ptr)
2020 bool remove = false;
2021 store_info *store_info = i_ptr->store_rec;
2023 /* Skip the clobbers. */
2024 while (!store_info->is_set)
2025 store_info = store_info->next;
2027 /* There are three cases here. */
2028 if (store_info->group_id < 0)
2029 /* We have a cselib store followed by a read from a
2030 const base. */
2031 remove
2032 = canon_true_dependence (store_info->mem,
2033 GET_MODE (store_info->mem),
2034 store_info->mem_addr,
2035 mem, mem_addr);
2037 else if (group_id == store_info->group_id)
2039 /* This is a block mode load. We may get lucky and
2040 canon_true_dependence may save the day. */
2041 if (width == -1)
2042 remove
2043 = canon_true_dependence (store_info->mem,
2044 GET_MODE (store_info->mem),
2045 store_info->mem_addr,
2046 mem, mem_addr);
2048 /* If this read is just reading back something that we just
2049 stored, rewrite the read. */
2050 else
2052 if (store_info->rhs
2053 && offset >= store_info->begin
2054 && offset + width <= store_info->end
2055 && all_positions_needed_p (store_info,
2056 offset - store_info->begin,
2057 width)
2058 && replace_read (store_info, i_ptr, read_info,
2059 insn_info, loc, bb_info->regs_live))
2060 return;
2062 /* The bases are the same, just see if the offsets
2063 overlap. */
2064 if ((offset < store_info->end)
2065 && (offset + width > store_info->begin))
2066 remove = true;
2070 /* else
2071 The else case that is missing here is that the
2072 bases are constant but different. There is nothing
2073 to do here because there is no overlap. */
2075 if (remove)
2077 if (dump_file && (dump_flags & TDF_DETAILS))
2078 dump_insn_info ("removing from active", i_ptr);
2080 active_local_stores_len--;
2081 if (last)
2082 last->next_local_store = i_ptr->next_local_store;
2083 else
2084 active_local_stores = i_ptr->next_local_store;
2086 else
2087 last = i_ptr;
2088 i_ptr = i_ptr->next_local_store;
2091 else
2093 insn_info_t i_ptr = active_local_stores;
2094 insn_info_t last = NULL;
2095 if (dump_file && (dump_flags & TDF_DETAILS))
2097 fprintf (dump_file, " processing cselib load mem:");
2098 print_inline_rtx (dump_file, mem, 0);
2099 fprintf (dump_file, "\n");
2102 while (i_ptr)
2104 bool remove = false;
2105 store_info *store_info = i_ptr->store_rec;
2107 if (dump_file && (dump_flags & TDF_DETAILS))
2108 fprintf (dump_file, " processing cselib load against insn %d\n",
2109 INSN_UID (i_ptr->insn));
2111 /* Skip the clobbers. */
2112 while (!store_info->is_set)
2113 store_info = store_info->next;
2115 /* If this read is just reading back something that we just
2116 stored, rewrite the read. */
2117 if (store_info->rhs
2118 && store_info->group_id == -1
2119 && store_info->cse_base == base
2120 && width != -1
2121 && offset >= store_info->begin
2122 && offset + width <= store_info->end
2123 && all_positions_needed_p (store_info,
2124 offset - store_info->begin, width)
2125 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2126 bb_info->regs_live))
2127 return;
2129 remove = canon_true_dependence (store_info->mem,
2130 GET_MODE (store_info->mem),
2131 store_info->mem_addr,
2132 mem, mem_addr);
2134 if (remove)
2136 if (dump_file && (dump_flags & TDF_DETAILS))
2137 dump_insn_info ("removing from active", i_ptr);
2139 active_local_stores_len--;
2140 if (last)
2141 last->next_local_store = i_ptr->next_local_store;
2142 else
2143 active_local_stores = i_ptr->next_local_store;
2145 else
2146 last = i_ptr;
2147 i_ptr = i_ptr->next_local_store;
2152 /* A note_uses callback in which DATA points the INSN_INFO for
2153 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2154 true for any part of *LOC. */
2156 static void
2157 check_mem_read_use (rtx *loc, void *data)
2159 subrtx_ptr_iterator::array_type array;
2160 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2162 rtx *loc = *iter;
2163 if (MEM_P (*loc))
2164 check_mem_read_rtx (loc, (bb_info_t) data);
2169 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2170 So far it only handles arguments passed in registers. */
2172 static bool
2173 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2175 CUMULATIVE_ARGS args_so_far_v;
2176 cumulative_args_t args_so_far;
2177 tree arg;
2178 int idx;
2180 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2181 args_so_far = pack_cumulative_args (&args_so_far_v);
2183 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2184 for (idx = 0;
2185 arg != void_list_node && idx < nargs;
2186 arg = TREE_CHAIN (arg), idx++)
2188 scalar_int_mode mode;
2189 rtx reg, link, tmp;
2191 if (!is_int_mode (TYPE_MODE (TREE_VALUE (arg)), &mode))
2192 return false;
2194 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2195 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode)
2196 return false;
2198 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2199 link;
2200 link = XEXP (link, 1))
2201 if (GET_CODE (XEXP (link, 0)) == USE)
2203 scalar_int_mode arg_mode;
2204 args[idx] = XEXP (XEXP (link, 0), 0);
2205 if (REG_P (args[idx])
2206 && REGNO (args[idx]) == REGNO (reg)
2207 && (GET_MODE (args[idx]) == mode
2208 || (is_int_mode (GET_MODE (args[idx]), &arg_mode)
2209 && (GET_MODE_SIZE (arg_mode) <= UNITS_PER_WORD)
2210 && (GET_MODE_SIZE (arg_mode) > GET_MODE_SIZE (mode)))))
2211 break;
2213 if (!link)
2214 return false;
2216 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2217 if (GET_MODE (args[idx]) != mode)
2219 if (!tmp || !CONST_INT_P (tmp))
2220 return false;
2221 tmp = gen_int_mode (INTVAL (tmp), mode);
2223 if (tmp)
2224 args[idx] = tmp;
2226 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2228 if (arg != void_list_node || idx != nargs)
2229 return false;
2230 return true;
2233 /* Return a bitmap of the fixed registers contained in IN. */
2235 static bitmap
2236 copy_fixed_regs (const_bitmap in)
2238 bitmap ret;
2240 ret = ALLOC_REG_SET (NULL);
2241 bitmap_and (ret, in, fixed_reg_set_regset);
2242 return ret;
2245 /* Apply record_store to all candidate stores in INSN. Mark INSN
2246 if some part of it is not a candidate store and assigns to a
2247 non-register target. */
2249 static void
2250 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2252 rtx body;
2253 insn_info_type *insn_info = insn_info_type_pool.allocate ();
2254 int mems_found = 0;
2255 memset (insn_info, 0, sizeof (struct insn_info_type));
2257 if (dump_file && (dump_flags & TDF_DETAILS))
2258 fprintf (dump_file, "\n**scanning insn=%d\n",
2259 INSN_UID (insn));
2261 insn_info->prev_insn = bb_info->last_insn;
2262 insn_info->insn = insn;
2263 bb_info->last_insn = insn_info;
2265 if (DEBUG_INSN_P (insn))
2267 insn_info->cannot_delete = true;
2268 return;
2271 /* Look at all of the uses in the insn. */
2272 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2274 if (CALL_P (insn))
2276 bool const_call;
2277 rtx call, sym;
2278 tree memset_call = NULL_TREE;
2280 insn_info->cannot_delete = true;
2282 /* Const functions cannot do anything bad i.e. read memory,
2283 however, they can read their parameters which may have
2284 been pushed onto the stack.
2285 memset and bzero don't read memory either. */
2286 const_call = RTL_CONST_CALL_P (insn);
2287 if (!const_call
2288 && (call = get_call_rtx_from (insn))
2289 && (sym = XEXP (XEXP (call, 0), 0))
2290 && GET_CODE (sym) == SYMBOL_REF
2291 && SYMBOL_REF_DECL (sym)
2292 && TREE_CODE (SYMBOL_REF_DECL (sym)) == FUNCTION_DECL
2293 && DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (sym)) == BUILT_IN_NORMAL
2294 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (sym)) == BUILT_IN_MEMSET)
2295 memset_call = SYMBOL_REF_DECL (sym);
2297 if (const_call || memset_call)
2299 insn_info_t i_ptr = active_local_stores;
2300 insn_info_t last = NULL;
2302 if (dump_file && (dump_flags & TDF_DETAILS))
2303 fprintf (dump_file, "%s call %d\n",
2304 const_call ? "const" : "memset", INSN_UID (insn));
2306 /* See the head comment of the frame_read field. */
2307 if (reload_completed
2308 /* Tail calls are storing their arguments using
2309 arg pointer. If it is a frame pointer on the target,
2310 even before reload we need to kill frame pointer based
2311 stores. */
2312 || (SIBLING_CALL_P (insn)
2313 && HARD_FRAME_POINTER_IS_ARG_POINTER))
2314 insn_info->frame_read = true;
2316 /* Loop over the active stores and remove those which are
2317 killed by the const function call. */
2318 while (i_ptr)
2320 bool remove_store = false;
2322 /* The stack pointer based stores are always killed. */
2323 if (i_ptr->stack_pointer_based)
2324 remove_store = true;
2326 /* If the frame is read, the frame related stores are killed. */
2327 else if (insn_info->frame_read)
2329 store_info *store_info = i_ptr->store_rec;
2331 /* Skip the clobbers. */
2332 while (!store_info->is_set)
2333 store_info = store_info->next;
2335 if (store_info->group_id >= 0
2336 && rtx_group_vec[store_info->group_id]->frame_related)
2337 remove_store = true;
2340 if (remove_store)
2342 if (dump_file && (dump_flags & TDF_DETAILS))
2343 dump_insn_info ("removing from active", i_ptr);
2345 active_local_stores_len--;
2346 if (last)
2347 last->next_local_store = i_ptr->next_local_store;
2348 else
2349 active_local_stores = i_ptr->next_local_store;
2351 else
2352 last = i_ptr;
2354 i_ptr = i_ptr->next_local_store;
2357 if (memset_call)
2359 rtx args[3];
2360 if (get_call_args (insn, memset_call, args, 3)
2361 && CONST_INT_P (args[1])
2362 && CONST_INT_P (args[2])
2363 && INTVAL (args[2]) > 0)
2365 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2366 set_mem_size (mem, INTVAL (args[2]));
2367 body = gen_rtx_SET (mem, args[1]);
2368 mems_found += record_store (body, bb_info);
2369 if (dump_file && (dump_flags & TDF_DETAILS))
2370 fprintf (dump_file, "handling memset as BLKmode store\n");
2371 if (mems_found == 1)
2373 if (active_local_stores_len++
2374 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2376 active_local_stores_len = 1;
2377 active_local_stores = NULL;
2379 insn_info->fixed_regs_live
2380 = copy_fixed_regs (bb_info->regs_live);
2381 insn_info->next_local_store = active_local_stores;
2382 active_local_stores = insn_info;
2385 else
2386 clear_rhs_from_active_local_stores ();
2389 else if (SIBLING_CALL_P (insn) && reload_completed)
2390 /* Arguments for a sibling call that are pushed to memory are passed
2391 using the incoming argument pointer of the current function. After
2392 reload that might be (and likely is) frame pointer based. */
2393 add_wild_read (bb_info);
2394 else
2395 /* Every other call, including pure functions, may read any memory
2396 that is not relative to the frame. */
2397 add_non_frame_wild_read (bb_info);
2399 return;
2402 /* Assuming that there are sets in these insns, we cannot delete
2403 them. */
2404 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2405 || volatile_refs_p (PATTERN (insn))
2406 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2407 || (RTX_FRAME_RELATED_P (insn))
2408 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2409 insn_info->cannot_delete = true;
2411 body = PATTERN (insn);
2412 if (GET_CODE (body) == PARALLEL)
2414 int i;
2415 for (i = 0; i < XVECLEN (body, 0); i++)
2416 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2418 else
2419 mems_found += record_store (body, bb_info);
2421 if (dump_file && (dump_flags & TDF_DETAILS))
2422 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2423 mems_found, insn_info->cannot_delete ? "true" : "false");
2425 /* If we found some sets of mems, add it into the active_local_stores so
2426 that it can be locally deleted if found dead or used for
2427 replace_read and redundant constant store elimination. Otherwise mark
2428 it as cannot delete. This simplifies the processing later. */
2429 if (mems_found == 1)
2431 if (active_local_stores_len++
2432 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2434 active_local_stores_len = 1;
2435 active_local_stores = NULL;
2437 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2438 insn_info->next_local_store = active_local_stores;
2439 active_local_stores = insn_info;
2441 else
2442 insn_info->cannot_delete = true;
2446 /* Remove BASE from the set of active_local_stores. This is a
2447 callback from cselib that is used to get rid of the stores in
2448 active_local_stores. */
2450 static void
2451 remove_useless_values (cselib_val *base)
2453 insn_info_t insn_info = active_local_stores;
2454 insn_info_t last = NULL;
2456 while (insn_info)
2458 store_info *store_info = insn_info->store_rec;
2459 bool del = false;
2461 /* If ANY of the store_infos match the cselib group that is
2462 being deleted, then the insn can not be deleted. */
2463 while (store_info)
2465 if ((store_info->group_id == -1)
2466 && (store_info->cse_base == base))
2468 del = true;
2469 break;
2471 store_info = store_info->next;
2474 if (del)
2476 active_local_stores_len--;
2477 if (last)
2478 last->next_local_store = insn_info->next_local_store;
2479 else
2480 active_local_stores = insn_info->next_local_store;
2481 free_store_info (insn_info);
2483 else
2484 last = insn_info;
2486 insn_info = insn_info->next_local_store;
2491 /* Do all of step 1. */
2493 static void
2494 dse_step1 (void)
2496 basic_block bb;
2497 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2499 cselib_init (0);
2500 all_blocks = BITMAP_ALLOC (NULL);
2501 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2502 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2504 FOR_ALL_BB_FN (bb, cfun)
2506 insn_info_t ptr;
2507 bb_info_t bb_info = dse_bb_info_type_pool.allocate ();
2509 memset (bb_info, 0, sizeof (dse_bb_info_type));
2510 bitmap_set_bit (all_blocks, bb->index);
2511 bb_info->regs_live = regs_live;
2513 bitmap_copy (regs_live, DF_LR_IN (bb));
2514 df_simulate_initialize_forwards (bb, regs_live);
2516 bb_table[bb->index] = bb_info;
2517 cselib_discard_hook = remove_useless_values;
2519 if (bb->index >= NUM_FIXED_BLOCKS)
2521 rtx_insn *insn;
2523 active_local_stores = NULL;
2524 active_local_stores_len = 0;
2525 cselib_clear_table ();
2527 /* Scan the insns. */
2528 FOR_BB_INSNS (bb, insn)
2530 if (INSN_P (insn))
2531 scan_insn (bb_info, insn);
2532 cselib_process_insn (insn);
2533 if (INSN_P (insn))
2534 df_simulate_one_insn_forwards (bb, insn, regs_live);
2537 /* This is something of a hack, because the global algorithm
2538 is supposed to take care of the case where stores go dead
2539 at the end of the function. However, the global
2540 algorithm must take a more conservative view of block
2541 mode reads than the local alg does. So to get the case
2542 where you have a store to the frame followed by a non
2543 overlapping block more read, we look at the active local
2544 stores at the end of the function and delete all of the
2545 frame and spill based ones. */
2546 if (stores_off_frame_dead_at_return
2547 && (EDGE_COUNT (bb->succs) == 0
2548 || (single_succ_p (bb)
2549 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2550 && ! crtl->calls_eh_return)))
2552 insn_info_t i_ptr = active_local_stores;
2553 while (i_ptr)
2555 store_info *store_info = i_ptr->store_rec;
2557 /* Skip the clobbers. */
2558 while (!store_info->is_set)
2559 store_info = store_info->next;
2560 if (store_info->group_id >= 0)
2562 group_info *group = rtx_group_vec[store_info->group_id];
2563 if (group->frame_related && !i_ptr->cannot_delete)
2564 delete_dead_store_insn (i_ptr);
2567 i_ptr = i_ptr->next_local_store;
2571 /* Get rid of the loads that were discovered in
2572 replace_read. Cselib is finished with this block. */
2573 while (deferred_change_list)
2575 deferred_change *next = deferred_change_list->next;
2577 /* There is no reason to validate this change. That was
2578 done earlier. */
2579 *deferred_change_list->loc = deferred_change_list->reg;
2580 deferred_change_pool.remove (deferred_change_list);
2581 deferred_change_list = next;
2584 /* Get rid of all of the cselib based store_infos in this
2585 block and mark the containing insns as not being
2586 deletable. */
2587 ptr = bb_info->last_insn;
2588 while (ptr)
2590 if (ptr->contains_cselib_groups)
2592 store_info *s_info = ptr->store_rec;
2593 while (s_info && !s_info->is_set)
2594 s_info = s_info->next;
2595 if (s_info
2596 && s_info->redundant_reason
2597 && s_info->redundant_reason->insn
2598 && !ptr->cannot_delete)
2600 if (dump_file && (dump_flags & TDF_DETAILS))
2601 fprintf (dump_file, "Locally deleting insn %d "
2602 "because insn %d stores the "
2603 "same value and couldn't be "
2604 "eliminated\n",
2605 INSN_UID (ptr->insn),
2606 INSN_UID (s_info->redundant_reason->insn));
2607 delete_dead_store_insn (ptr);
2609 free_store_info (ptr);
2611 else
2613 store_info *s_info;
2615 /* Free at least positions_needed bitmaps. */
2616 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2617 if (s_info->is_large)
2619 BITMAP_FREE (s_info->positions_needed.large.bmap);
2620 s_info->is_large = false;
2623 ptr = ptr->prev_insn;
2626 cse_store_info_pool.release ();
2628 bb_info->regs_live = NULL;
2631 BITMAP_FREE (regs_live);
2632 cselib_finish ();
2633 rtx_group_table->empty ();
2637 /*----------------------------------------------------------------------------
2638 Second step.
2640 Assign each byte position in the stores that we are going to
2641 analyze globally to a position in the bitmaps. Returns true if
2642 there are any bit positions assigned.
2643 ----------------------------------------------------------------------------*/
2645 static void
2646 dse_step2_init (void)
2648 unsigned int i;
2649 group_info *group;
2651 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2653 /* For all non stack related bases, we only consider a store to
2654 be deletable if there are two or more stores for that
2655 position. This is because it takes one store to make the
2656 other store redundant. However, for the stores that are
2657 stack related, we consider them if there is only one store
2658 for the position. We do this because the stack related
2659 stores can be deleted if their is no read between them and
2660 the end of the function.
2662 To make this work in the current framework, we take the stack
2663 related bases add all of the bits from store1 into store2.
2664 This has the effect of making the eligible even if there is
2665 only one store. */
2667 if (stores_off_frame_dead_at_return && group->frame_related)
2669 bitmap_ior_into (group->store2_n, group->store1_n);
2670 bitmap_ior_into (group->store2_p, group->store1_p);
2671 if (dump_file && (dump_flags & TDF_DETAILS))
2672 fprintf (dump_file, "group %d is frame related ", i);
2675 group->offset_map_size_n++;
2676 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2677 group->offset_map_size_n);
2678 group->offset_map_size_p++;
2679 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2680 group->offset_map_size_p);
2681 group->process_globally = false;
2682 if (dump_file && (dump_flags & TDF_DETAILS))
2684 fprintf (dump_file, "group %d(%d+%d): ", i,
2685 (int)bitmap_count_bits (group->store2_n),
2686 (int)bitmap_count_bits (group->store2_p));
2687 bitmap_print (dump_file, group->store2_n, "n ", " ");
2688 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2694 /* Init the offset tables. */
2696 static bool
2697 dse_step2 (void)
2699 unsigned int i;
2700 group_info *group;
2701 /* Position 0 is unused because 0 is used in the maps to mean
2702 unused. */
2703 current_position = 1;
2704 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2706 bitmap_iterator bi;
2707 unsigned int j;
2709 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2710 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2711 bitmap_clear (group->group_kill);
2713 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2715 bitmap_set_bit (group->group_kill, current_position);
2716 if (bitmap_bit_p (group->escaped_n, j))
2717 bitmap_set_bit (kill_on_calls, current_position);
2718 group->offset_map_n[j] = current_position++;
2719 group->process_globally = true;
2721 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2723 bitmap_set_bit (group->group_kill, current_position);
2724 if (bitmap_bit_p (group->escaped_p, j))
2725 bitmap_set_bit (kill_on_calls, current_position);
2726 group->offset_map_p[j] = current_position++;
2727 group->process_globally = true;
2730 return current_position != 1;
2735 /*----------------------------------------------------------------------------
2736 Third step.
2738 Build the bit vectors for the transfer functions.
2739 ----------------------------------------------------------------------------*/
2742 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2743 there, return 0. */
2745 static int
2746 get_bitmap_index (group_info *group_info, HOST_WIDE_INT offset)
2748 if (offset < 0)
2750 HOST_WIDE_INT offset_p = -offset;
2751 if (offset_p >= group_info->offset_map_size_n)
2752 return 0;
2753 return group_info->offset_map_n[offset_p];
2755 else
2757 if (offset >= group_info->offset_map_size_p)
2758 return 0;
2759 return group_info->offset_map_p[offset];
2764 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2765 may be NULL. */
2767 static void
2768 scan_stores (store_info *store_info, bitmap gen, bitmap kill)
2770 while (store_info)
2772 HOST_WIDE_INT i;
2773 group_info *group_info
2774 = rtx_group_vec[store_info->group_id];
2775 if (group_info->process_globally)
2776 for (i = store_info->begin; i < store_info->end; i++)
2778 int index = get_bitmap_index (group_info, i);
2779 if (index != 0)
2781 bitmap_set_bit (gen, index);
2782 if (kill)
2783 bitmap_clear_bit (kill, index);
2786 store_info = store_info->next;
2791 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
2792 may be NULL. */
2794 static void
2795 scan_reads (insn_info_t insn_info, bitmap gen, bitmap kill)
2797 read_info_t read_info = insn_info->read_rec;
2798 int i;
2799 group_info *group;
2801 /* If this insn reads the frame, kill all the frame related stores. */
2802 if (insn_info->frame_read)
2804 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2805 if (group->process_globally && group->frame_related)
2807 if (kill)
2808 bitmap_ior_into (kill, group->group_kill);
2809 bitmap_and_compl_into (gen, group->group_kill);
2812 if (insn_info->non_frame_wild_read)
2814 /* Kill all non-frame related stores. Kill all stores of variables that
2815 escape. */
2816 if (kill)
2817 bitmap_ior_into (kill, kill_on_calls);
2818 bitmap_and_compl_into (gen, kill_on_calls);
2819 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2820 if (group->process_globally && !group->frame_related)
2822 if (kill)
2823 bitmap_ior_into (kill, group->group_kill);
2824 bitmap_and_compl_into (gen, group->group_kill);
2827 while (read_info)
2829 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2831 if (group->process_globally)
2833 if (i == read_info->group_id)
2835 if (read_info->begin > read_info->end)
2837 /* Begin > end for block mode reads. */
2838 if (kill)
2839 bitmap_ior_into (kill, group->group_kill);
2840 bitmap_and_compl_into (gen, group->group_kill);
2842 else
2844 /* The groups are the same, just process the
2845 offsets. */
2846 HOST_WIDE_INT j;
2847 for (j = read_info->begin; j < read_info->end; j++)
2849 int index = get_bitmap_index (group, j);
2850 if (index != 0)
2852 if (kill)
2853 bitmap_set_bit (kill, index);
2854 bitmap_clear_bit (gen, index);
2859 else
2861 /* The groups are different, if the alias sets
2862 conflict, clear the entire group. We only need
2863 to apply this test if the read_info is a cselib
2864 read. Anything with a constant base cannot alias
2865 something else with a different constant
2866 base. */
2867 if ((read_info->group_id < 0)
2868 && canon_true_dependence (group->base_mem,
2869 GET_MODE (group->base_mem),
2870 group->canon_base_addr,
2871 read_info->mem, NULL_RTX))
2873 if (kill)
2874 bitmap_ior_into (kill, group->group_kill);
2875 bitmap_and_compl_into (gen, group->group_kill);
2881 read_info = read_info->next;
2886 /* Return the insn in BB_INFO before the first wild read or if there
2887 are no wild reads in the block, return the last insn. */
2889 static insn_info_t
2890 find_insn_before_first_wild_read (bb_info_t bb_info)
2892 insn_info_t insn_info = bb_info->last_insn;
2893 insn_info_t last_wild_read = NULL;
2895 while (insn_info)
2897 if (insn_info->wild_read)
2899 last_wild_read = insn_info->prev_insn;
2900 /* Block starts with wild read. */
2901 if (!last_wild_read)
2902 return NULL;
2905 insn_info = insn_info->prev_insn;
2908 if (last_wild_read)
2909 return last_wild_read;
2910 else
2911 return bb_info->last_insn;
2915 /* Scan the insns in BB_INFO starting at PTR and going to the top of
2916 the block in order to build the gen and kill sets for the block.
2917 We start at ptr which may be the last insn in the block or may be
2918 the first insn with a wild read. In the latter case we are able to
2919 skip the rest of the block because it just does not matter:
2920 anything that happens is hidden by the wild read. */
2922 static void
2923 dse_step3_scan (basic_block bb)
2925 bb_info_t bb_info = bb_table[bb->index];
2926 insn_info_t insn_info;
2928 insn_info = find_insn_before_first_wild_read (bb_info);
2930 /* In the spill case or in the no_spill case if there is no wild
2931 read in the block, we will need a kill set. */
2932 if (insn_info == bb_info->last_insn)
2934 if (bb_info->kill)
2935 bitmap_clear (bb_info->kill);
2936 else
2937 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
2939 else
2940 if (bb_info->kill)
2941 BITMAP_FREE (bb_info->kill);
2943 while (insn_info)
2945 /* There may have been code deleted by the dce pass run before
2946 this phase. */
2947 if (insn_info->insn && INSN_P (insn_info->insn))
2949 scan_stores (insn_info->store_rec, bb_info->gen, bb_info->kill);
2950 scan_reads (insn_info, bb_info->gen, bb_info->kill);
2953 insn_info = insn_info->prev_insn;
2958 /* Set the gen set of the exit block, and also any block with no
2959 successors that does not have a wild read. */
2961 static void
2962 dse_step3_exit_block_scan (bb_info_t bb_info)
2964 /* The gen set is all 0's for the exit block except for the
2965 frame_pointer_group. */
2967 if (stores_off_frame_dead_at_return)
2969 unsigned int i;
2970 group_info *group;
2972 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2974 if (group->process_globally && group->frame_related)
2975 bitmap_ior_into (bb_info->gen, group->group_kill);
2981 /* Find all of the blocks that are not backwards reachable from the
2982 exit block or any block with no successors (BB). These are the
2983 infinite loops or infinite self loops. These blocks will still
2984 have their bits set in UNREACHABLE_BLOCKS. */
2986 static void
2987 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
2989 edge e;
2990 edge_iterator ei;
2992 if (bitmap_bit_p (unreachable_blocks, bb->index))
2994 bitmap_clear_bit (unreachable_blocks, bb->index);
2995 FOR_EACH_EDGE (e, ei, bb->preds)
2997 mark_reachable_blocks (unreachable_blocks, e->src);
3002 /* Build the transfer functions for the function. */
3004 static void
3005 dse_step3 ()
3007 basic_block bb;
3008 sbitmap_iterator sbi;
3009 bitmap all_ones = NULL;
3010 unsigned int i;
3012 auto_sbitmap unreachable_blocks (last_basic_block_for_fn (cfun));
3013 bitmap_ones (unreachable_blocks);
3015 FOR_ALL_BB_FN (bb, cfun)
3017 bb_info_t bb_info = bb_table[bb->index];
3018 if (bb_info->gen)
3019 bitmap_clear (bb_info->gen);
3020 else
3021 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3023 if (bb->index == ENTRY_BLOCK)
3025 else if (bb->index == EXIT_BLOCK)
3026 dse_step3_exit_block_scan (bb_info);
3027 else
3028 dse_step3_scan (bb);
3029 if (EDGE_COUNT (bb->succs) == 0)
3030 mark_reachable_blocks (unreachable_blocks, bb);
3032 /* If this is the second time dataflow is run, delete the old
3033 sets. */
3034 if (bb_info->in)
3035 BITMAP_FREE (bb_info->in);
3036 if (bb_info->out)
3037 BITMAP_FREE (bb_info->out);
3040 /* For any block in an infinite loop, we must initialize the out set
3041 to all ones. This could be expensive, but almost never occurs in
3042 practice. However, it is common in regression tests. */
3043 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3045 if (bitmap_bit_p (all_blocks, i))
3047 bb_info_t bb_info = bb_table[i];
3048 if (!all_ones)
3050 unsigned int j;
3051 group_info *group;
3053 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3054 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3055 bitmap_ior_into (all_ones, group->group_kill);
3057 if (!bb_info->out)
3059 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3060 bitmap_copy (bb_info->out, all_ones);
3065 if (all_ones)
3066 BITMAP_FREE (all_ones);
3071 /*----------------------------------------------------------------------------
3072 Fourth step.
3074 Solve the bitvector equations.
3075 ----------------------------------------------------------------------------*/
3078 /* Confluence function for blocks with no successors. Create an out
3079 set from the gen set of the exit block. This block logically has
3080 the exit block as a successor. */
3084 static void
3085 dse_confluence_0 (basic_block bb)
3087 bb_info_t bb_info = bb_table[bb->index];
3089 if (bb->index == EXIT_BLOCK)
3090 return;
3092 if (!bb_info->out)
3094 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3095 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3099 /* Propagate the information from the in set of the dest of E to the
3100 out set of the src of E. If the various in or out sets are not
3101 there, that means they are all ones. */
3103 static bool
3104 dse_confluence_n (edge e)
3106 bb_info_t src_info = bb_table[e->src->index];
3107 bb_info_t dest_info = bb_table[e->dest->index];
3109 if (dest_info->in)
3111 if (src_info->out)
3112 bitmap_and_into (src_info->out, dest_info->in);
3113 else
3115 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3116 bitmap_copy (src_info->out, dest_info->in);
3119 return true;
3123 /* Propagate the info from the out to the in set of BB_INDEX's basic
3124 block. There are three cases:
3126 1) The block has no kill set. In this case the kill set is all
3127 ones. It does not matter what the out set of the block is, none of
3128 the info can reach the top. The only thing that reaches the top is
3129 the gen set and we just copy the set.
3131 2) There is a kill set but no out set and bb has successors. In
3132 this case we just return. Eventually an out set will be created and
3133 it is better to wait than to create a set of ones.
3135 3) There is both a kill and out set. We apply the obvious transfer
3136 function.
3139 static bool
3140 dse_transfer_function (int bb_index)
3142 bb_info_t bb_info = bb_table[bb_index];
3144 if (bb_info->kill)
3146 if (bb_info->out)
3148 /* Case 3 above. */
3149 if (bb_info->in)
3150 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3151 bb_info->out, bb_info->kill);
3152 else
3154 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3155 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3156 bb_info->out, bb_info->kill);
3157 return true;
3160 else
3161 /* Case 2 above. */
3162 return false;
3164 else
3166 /* Case 1 above. If there is already an in set, nothing
3167 happens. */
3168 if (bb_info->in)
3169 return false;
3170 else
3172 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3173 bitmap_copy (bb_info->in, bb_info->gen);
3174 return true;
3179 /* Solve the dataflow equations. */
3181 static void
3182 dse_step4 (void)
3184 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3185 dse_confluence_n, dse_transfer_function,
3186 all_blocks, df_get_postorder (DF_BACKWARD),
3187 df_get_n_blocks (DF_BACKWARD));
3188 if (dump_file && (dump_flags & TDF_DETAILS))
3190 basic_block bb;
3192 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3193 FOR_ALL_BB_FN (bb, cfun)
3195 bb_info_t bb_info = bb_table[bb->index];
3197 df_print_bb_index (bb, dump_file);
3198 if (bb_info->in)
3199 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3200 else
3201 fprintf (dump_file, " in: *MISSING*\n");
3202 if (bb_info->gen)
3203 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3204 else
3205 fprintf (dump_file, " gen: *MISSING*\n");
3206 if (bb_info->kill)
3207 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3208 else
3209 fprintf (dump_file, " kill: *MISSING*\n");
3210 if (bb_info->out)
3211 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3212 else
3213 fprintf (dump_file, " out: *MISSING*\n\n");
3220 /*----------------------------------------------------------------------------
3221 Fifth step.
3223 Delete the stores that can only be deleted using the global information.
3224 ----------------------------------------------------------------------------*/
3227 static void
3228 dse_step5 (void)
3230 basic_block bb;
3231 FOR_EACH_BB_FN (bb, cfun)
3233 bb_info_t bb_info = bb_table[bb->index];
3234 insn_info_t insn_info = bb_info->last_insn;
3235 bitmap v = bb_info->out;
3237 while (insn_info)
3239 bool deleted = false;
3240 if (dump_file && insn_info->insn)
3242 fprintf (dump_file, "starting to process insn %d\n",
3243 INSN_UID (insn_info->insn));
3244 bitmap_print (dump_file, v, " v: ", "\n");
3247 /* There may have been code deleted by the dce pass run before
3248 this phase. */
3249 if (insn_info->insn
3250 && INSN_P (insn_info->insn)
3251 && (!insn_info->cannot_delete)
3252 && (!bitmap_empty_p (v)))
3254 store_info *store_info = insn_info->store_rec;
3256 /* Try to delete the current insn. */
3257 deleted = true;
3259 /* Skip the clobbers. */
3260 while (!store_info->is_set)
3261 store_info = store_info->next;
3263 HOST_WIDE_INT i;
3264 group_info *group_info = rtx_group_vec[store_info->group_id];
3266 for (i = store_info->begin; i < store_info->end; i++)
3268 int index = get_bitmap_index (group_info, i);
3270 if (dump_file && (dump_flags & TDF_DETAILS))
3271 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3272 if (index == 0 || !bitmap_bit_p (v, index))
3274 if (dump_file && (dump_flags & TDF_DETAILS))
3275 fprintf (dump_file, "failing at i = %d\n", (int)i);
3276 deleted = false;
3277 break;
3280 if (deleted)
3282 if (dbg_cnt (dse)
3283 && check_for_inc_dec_1 (insn_info))
3285 delete_insn (insn_info->insn);
3286 insn_info->insn = NULL;
3287 globally_deleted++;
3291 /* We do want to process the local info if the insn was
3292 deleted. For instance, if the insn did a wild read, we
3293 no longer need to trash the info. */
3294 if (insn_info->insn
3295 && INSN_P (insn_info->insn)
3296 && (!deleted))
3298 scan_stores (insn_info->store_rec, v, NULL);
3299 if (insn_info->wild_read)
3301 if (dump_file && (dump_flags & TDF_DETAILS))
3302 fprintf (dump_file, "wild read\n");
3303 bitmap_clear (v);
3305 else if (insn_info->read_rec
3306 || insn_info->non_frame_wild_read
3307 || insn_info->frame_read)
3309 if (dump_file && (dump_flags & TDF_DETAILS))
3311 if (!insn_info->non_frame_wild_read
3312 && !insn_info->frame_read)
3313 fprintf (dump_file, "regular read\n");
3314 if (insn_info->non_frame_wild_read)
3315 fprintf (dump_file, "non-frame wild read\n");
3316 if (insn_info->frame_read)
3317 fprintf (dump_file, "frame read\n");
3319 scan_reads (insn_info, v, NULL);
3323 insn_info = insn_info->prev_insn;
3330 /*----------------------------------------------------------------------------
3331 Sixth step.
3333 Delete stores made redundant by earlier stores (which store the same
3334 value) that couldn't be eliminated.
3335 ----------------------------------------------------------------------------*/
3337 static void
3338 dse_step6 (void)
3340 basic_block bb;
3342 FOR_ALL_BB_FN (bb, cfun)
3344 bb_info_t bb_info = bb_table[bb->index];
3345 insn_info_t insn_info = bb_info->last_insn;
3347 while (insn_info)
3349 /* There may have been code deleted by the dce pass run before
3350 this phase. */
3351 if (insn_info->insn
3352 && INSN_P (insn_info->insn)
3353 && !insn_info->cannot_delete)
3355 store_info *s_info = insn_info->store_rec;
3357 while (s_info && !s_info->is_set)
3358 s_info = s_info->next;
3359 if (s_info
3360 && s_info->redundant_reason
3361 && s_info->redundant_reason->insn
3362 && INSN_P (s_info->redundant_reason->insn))
3364 rtx_insn *rinsn = s_info->redundant_reason->insn;
3365 if (dump_file && (dump_flags & TDF_DETAILS))
3366 fprintf (dump_file, "Locally deleting insn %d "
3367 "because insn %d stores the "
3368 "same value and couldn't be "
3369 "eliminated\n",
3370 INSN_UID (insn_info->insn),
3371 INSN_UID (rinsn));
3372 delete_dead_store_insn (insn_info);
3375 insn_info = insn_info->prev_insn;
3380 /*----------------------------------------------------------------------------
3381 Seventh step.
3383 Destroy everything left standing.
3384 ----------------------------------------------------------------------------*/
3386 static void
3387 dse_step7 (void)
3389 bitmap_obstack_release (&dse_bitmap_obstack);
3390 obstack_free (&dse_obstack, NULL);
3392 end_alias_analysis ();
3393 free (bb_table);
3394 delete rtx_group_table;
3395 rtx_group_table = NULL;
3396 rtx_group_vec.release ();
3397 BITMAP_FREE (all_blocks);
3398 BITMAP_FREE (scratch);
3400 rtx_store_info_pool.release ();
3401 read_info_type_pool.release ();
3402 insn_info_type_pool.release ();
3403 dse_bb_info_type_pool.release ();
3404 group_info_pool.release ();
3405 deferred_change_pool.release ();
3409 /* -------------------------------------------------------------------------
3411 ------------------------------------------------------------------------- */
3413 /* Callback for running pass_rtl_dse. */
3415 static unsigned int
3416 rest_of_handle_dse (void)
3418 df_set_flags (DF_DEFER_INSN_RESCAN);
3420 /* Need the notes since we must track live hardregs in the forwards
3421 direction. */
3422 df_note_add_problem ();
3423 df_analyze ();
3425 dse_step0 ();
3426 dse_step1 ();
3427 dse_step2_init ();
3428 if (dse_step2 ())
3430 df_set_flags (DF_LR_RUN_DCE);
3431 df_analyze ();
3432 if (dump_file && (dump_flags & TDF_DETAILS))
3433 fprintf (dump_file, "doing global processing\n");
3434 dse_step3 ();
3435 dse_step4 ();
3436 dse_step5 ();
3439 dse_step6 ();
3440 dse_step7 ();
3442 if (dump_file)
3443 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d\n",
3444 locally_deleted, globally_deleted);
3446 /* DSE can eliminate potentially-trapping MEMs.
3447 Remove any EH edges associated with them. */
3448 if ((locally_deleted || globally_deleted)
3449 && cfun->can_throw_non_call_exceptions
3450 && purge_all_dead_edges ())
3451 cleanup_cfg (0);
3453 return 0;
3456 namespace {
3458 const pass_data pass_data_rtl_dse1 =
3460 RTL_PASS, /* type */
3461 "dse1", /* name */
3462 OPTGROUP_NONE, /* optinfo_flags */
3463 TV_DSE1, /* tv_id */
3464 0, /* properties_required */
3465 0, /* properties_provided */
3466 0, /* properties_destroyed */
3467 0, /* todo_flags_start */
3468 TODO_df_finish, /* todo_flags_finish */
3471 class pass_rtl_dse1 : public rtl_opt_pass
3473 public:
3474 pass_rtl_dse1 (gcc::context *ctxt)
3475 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3478 /* opt_pass methods: */
3479 virtual bool gate (function *)
3481 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3484 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3486 }; // class pass_rtl_dse1
3488 } // anon namespace
3490 rtl_opt_pass *
3491 make_pass_rtl_dse1 (gcc::context *ctxt)
3493 return new pass_rtl_dse1 (ctxt);
3496 namespace {
3498 const pass_data pass_data_rtl_dse2 =
3500 RTL_PASS, /* type */
3501 "dse2", /* name */
3502 OPTGROUP_NONE, /* optinfo_flags */
3503 TV_DSE2, /* tv_id */
3504 0, /* properties_required */
3505 0, /* properties_provided */
3506 0, /* properties_destroyed */
3507 0, /* todo_flags_start */
3508 TODO_df_finish, /* todo_flags_finish */
3511 class pass_rtl_dse2 : public rtl_opt_pass
3513 public:
3514 pass_rtl_dse2 (gcc::context *ctxt)
3515 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3518 /* opt_pass methods: */
3519 virtual bool gate (function *)
3521 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3524 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3526 }; // class pass_rtl_dse2
3528 } // anon namespace
3530 rtl_opt_pass *
3531 make_pass_rtl_dse2 (gcc::context *ctxt)
3533 return new pass_rtl_dse2 (ctxt);