pa.c (pa_som_asm_init_sections): Fix comment.
[official-gcc.git] / gcc / dse.c
blobfbc6b25ac1ecc1678894c2267980c3cdeaf39266
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 byte associated with the operation. */
247 HOST_WIDE_INT offset;
249 /* The number of bytes covered by the operation. This is always exact
250 and known (rather than -1). */
251 HOST_WIDE_INT width;
253 union
255 /* A bitmask as wide as the number of bytes in the word that
256 contains a 1 if the byte may be needed. The store is unused if
257 all of the bits are 0. This is used if IS_LARGE is false. */
258 unsigned HOST_WIDE_INT small_bitmask;
260 struct
262 /* A bitmap with one bit per byte. Cleared bit means the position
263 is needed. Used if IS_LARGE is false. */
264 bitmap bmap;
266 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
267 equal to WIDTH, the whole store is unused. */
268 int count;
269 } large;
270 } positions_needed;
272 /* The next store info for this insn. */
273 struct store_info *next;
275 /* The right hand side of the store. This is used if there is a
276 subsequent reload of the mems address somewhere later in the
277 basic block. */
278 rtx rhs;
280 /* If rhs is or holds a constant, this contains that constant,
281 otherwise NULL. */
282 rtx const_rhs;
284 /* Set if this store stores the same constant value as REDUNDANT_REASON
285 insn stored. These aren't eliminated early, because doing that
286 might prevent the earlier larger store to be eliminated. */
287 struct insn_info_type *redundant_reason;
290 /* Return a bitmask with the first N low bits set. */
292 static unsigned HOST_WIDE_INT
293 lowpart_bitmask (int n)
295 unsigned HOST_WIDE_INT mask = HOST_WIDE_INT_M1U;
296 return mask >> (HOST_BITS_PER_WIDE_INT - n);
299 static object_allocator<store_info> cse_store_info_pool ("cse_store_info_pool");
301 static object_allocator<store_info> rtx_store_info_pool ("rtx_store_info_pool");
303 /* This structure holds information about a load. These are only
304 built for rtx bases. */
305 struct read_info_type
307 /* The id of the mem group of the base address. */
308 int group_id;
310 /* The offset of the first byte associated with the operation. */
311 HOST_WIDE_INT offset;
313 /* The number of bytes covered by the operation, or -1 if not known. */
314 HOST_WIDE_INT width;
316 /* The mem being read. */
317 rtx mem;
319 /* The next read_info for this insn. */
320 struct read_info_type *next;
322 typedef struct read_info_type *read_info_t;
324 static object_allocator<read_info_type> read_info_type_pool ("read_info_pool");
326 /* One of these records is created for each insn. */
328 struct insn_info_type
330 /* Set true if the insn contains a store but the insn itself cannot
331 be deleted. This is set if the insn is a parallel and there is
332 more than one non dead output or if the insn is in some way
333 volatile. */
334 bool cannot_delete;
336 /* This field is only used by the global algorithm. It is set true
337 if the insn contains any read of mem except for a (1). This is
338 also set if the insn is a call or has a clobber mem. If the insn
339 contains a wild read, the use_rec will be null. */
340 bool wild_read;
342 /* This is true only for CALL instructions which could potentially read
343 any non-frame memory location. This field is used by the global
344 algorithm. */
345 bool non_frame_wild_read;
347 /* This field is only used for the processing of const functions.
348 These functions cannot read memory, but they can read the stack
349 because that is where they may get their parms. We need to be
350 this conservative because, like the store motion pass, we don't
351 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
352 Moreover, we need to distinguish two cases:
353 1. Before reload (register elimination), the stores related to
354 outgoing arguments are stack pointer based and thus deemed
355 of non-constant base in this pass. This requires special
356 handling but also means that the frame pointer based stores
357 need not be killed upon encountering a const function call.
358 2. After reload, the stores related to outgoing arguments can be
359 either stack pointer or hard frame pointer based. This means
360 that we have no other choice than also killing all the frame
361 pointer based stores upon encountering a const function call.
362 This field is set after reload for const function calls and before
363 reload for const tail function calls on targets where arg pointer
364 is the frame pointer. Having this set is less severe than a wild
365 read, it just means that all the frame related stores are killed
366 rather than all the stores. */
367 bool frame_read;
369 /* This field is only used for the processing of const functions.
370 It is set if the insn may contain a stack pointer based store. */
371 bool stack_pointer_based;
373 /* This is true if any of the sets within the store contains a
374 cselib base. Such stores can only be deleted by the local
375 algorithm. */
376 bool contains_cselib_groups;
378 /* The insn. */
379 rtx_insn *insn;
381 /* The list of mem sets or mem clobbers that are contained in this
382 insn. If the insn is deletable, it contains only one mem set.
383 But it could also contain clobbers. Insns that contain more than
384 one mem set are not deletable, but each of those mems are here in
385 order to provide info to delete other insns. */
386 store_info *store_rec;
388 /* The linked list of mem uses in this insn. Only the reads from
389 rtx bases are listed here. The reads to cselib bases are
390 completely processed during the first scan and so are never
391 created. */
392 read_info_t read_rec;
394 /* The live fixed registers. We assume only fixed registers can
395 cause trouble by being clobbered from an expanded pattern;
396 storing only the live fixed registers (rather than all registers)
397 means less memory needs to be allocated / copied for the individual
398 stores. */
399 regset fixed_regs_live;
401 /* The prev insn in the basic block. */
402 struct insn_info_type * prev_insn;
404 /* The linked list of insns that are in consideration for removal in
405 the forwards pass through the basic block. This pointer may be
406 trash as it is not cleared when a wild read occurs. The only
407 time it is guaranteed to be correct is when the traversal starts
408 at active_local_stores. */
409 struct insn_info_type * next_local_store;
411 typedef struct insn_info_type *insn_info_t;
413 static object_allocator<insn_info_type> insn_info_type_pool ("insn_info_pool");
415 /* The linked list of stores that are under consideration in this
416 basic block. */
417 static insn_info_t active_local_stores;
418 static int active_local_stores_len;
420 struct dse_bb_info_type
422 /* Pointer to the insn info for the last insn in the block. These
423 are linked so this is how all of the insns are reached. During
424 scanning this is the current insn being scanned. */
425 insn_info_t last_insn;
427 /* The info for the global dataflow problem. */
430 /* This is set if the transfer function should and in the wild_read
431 bitmap before applying the kill and gen sets. That vector knocks
432 out most of the bits in the bitmap and thus speeds up the
433 operations. */
434 bool apply_wild_read;
436 /* The following 4 bitvectors hold information about which positions
437 of which stores are live or dead. They are indexed by
438 get_bitmap_index. */
440 /* The set of store positions that exist in this block before a wild read. */
441 bitmap gen;
443 /* The set of load positions that exist in this block above the
444 same position of a store. */
445 bitmap kill;
447 /* The set of stores that reach the top of the block without being
448 killed by a read.
450 Do not represent the in if it is all ones. Note that this is
451 what the bitvector should logically be initialized to for a set
452 intersection problem. However, like the kill set, this is too
453 expensive. So initially, the in set will only be created for the
454 exit block and any block that contains a wild read. */
455 bitmap in;
457 /* The set of stores that reach the bottom of the block from it's
458 successors.
460 Do not represent the in if it is all ones. Note that this is
461 what the bitvector should logically be initialized to for a set
462 intersection problem. However, like the kill and in set, this is
463 too expensive. So what is done is that the confluence operator
464 just initializes the vector from one of the out sets of the
465 successors of the block. */
466 bitmap out;
468 /* The following bitvector is indexed by the reg number. It
469 contains the set of regs that are live at the current instruction
470 being processed. While it contains info for all of the
471 registers, only the hard registers are actually examined. It is used
472 to assure that shift and/or add sequences that are inserted do not
473 accidentally clobber live hard regs. */
474 bitmap regs_live;
477 typedef struct dse_bb_info_type *bb_info_t;
479 static object_allocator<dse_bb_info_type> dse_bb_info_type_pool
480 ("bb_info_pool");
482 /* Table to hold all bb_infos. */
483 static bb_info_t *bb_table;
485 /* There is a group_info for each rtx base that is used to reference
486 memory. There are also not many of the rtx bases because they are
487 very limited in scope. */
489 struct group_info
491 /* The actual base of the address. */
492 rtx rtx_base;
494 /* The sequential id of the base. This allows us to have a
495 canonical ordering of these that is not based on addresses. */
496 int id;
498 /* True if there are any positions that are to be processed
499 globally. */
500 bool process_globally;
502 /* True if the base of this group is either the frame_pointer or
503 hard_frame_pointer. */
504 bool frame_related;
506 /* A mem wrapped around the base pointer for the group in order to do
507 read dependency. It must be given BLKmode in order to encompass all
508 the possible offsets from the base. */
509 rtx base_mem;
511 /* Canonized version of base_mem's address. */
512 rtx canon_base_addr;
514 /* These two sets of two bitmaps are used to keep track of how many
515 stores are actually referencing that position from this base. We
516 only do this for rtx bases as this will be used to assign
517 positions in the bitmaps for the global problem. Bit N is set in
518 store1 on the first store for offset N. Bit N is set in store2
519 for the second store to offset N. This is all we need since we
520 only care about offsets that have two or more stores for them.
522 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
523 for 0 and greater offsets.
525 There is one special case here, for stores into the stack frame,
526 we will or store1 into store2 before deciding which stores look
527 at globally. This is because stores to the stack frame that have
528 no other reads before the end of the function can also be
529 deleted. */
530 bitmap store1_n, store1_p, store2_n, store2_p;
532 /* These bitmaps keep track of offsets in this group escape this function.
533 An offset escapes if it corresponds to a named variable whose
534 addressable flag is set. */
535 bitmap escaped_n, escaped_p;
537 /* The positions in this bitmap have the same assignments as the in,
538 out, gen and kill bitmaps. This bitmap is all zeros except for
539 the positions that are occupied by stores for this group. */
540 bitmap group_kill;
542 /* The offset_map is used to map the offsets from this base into
543 positions in the global bitmaps. It is only created after all of
544 the all of stores have been scanned and we know which ones we
545 care about. */
546 int *offset_map_n, *offset_map_p;
547 int offset_map_size_n, offset_map_size_p;
550 static object_allocator<group_info> group_info_pool ("rtx_group_info_pool");
552 /* Index into the rtx_group_vec. */
553 static int rtx_group_next_id;
556 static vec<group_info *> rtx_group_vec;
559 /* This structure holds the set of changes that are being deferred
560 when removing read operation. See replace_read. */
561 struct deferred_change
564 /* The mem that is being replaced. */
565 rtx *loc;
567 /* The reg it is being replaced with. */
568 rtx reg;
570 struct deferred_change *next;
573 static object_allocator<deferred_change> deferred_change_pool
574 ("deferred_change_pool");
576 static deferred_change *deferred_change_list = NULL;
578 /* This is true except if cfun->stdarg -- i.e. we cannot do
579 this for vararg functions because they play games with the frame. */
580 static bool stores_off_frame_dead_at_return;
582 /* Counter for stats. */
583 static int globally_deleted;
584 static int locally_deleted;
586 static bitmap all_blocks;
588 /* Locations that are killed by calls in the global phase. */
589 static bitmap kill_on_calls;
591 /* The number of bits used in the global bitmaps. */
592 static unsigned int current_position;
594 /* Print offset range [OFFSET, OFFSET + WIDTH) to FILE. */
596 static void
597 print_range (FILE *file, poly_int64 offset, poly_int64 width)
599 fprintf (file, "[");
600 print_dec (offset, file, SIGNED);
601 fprintf (file, "..");
602 print_dec (offset + width, file, SIGNED);
603 fprintf (file, ")");
606 /*----------------------------------------------------------------------------
607 Zeroth step.
609 Initialization.
610 ----------------------------------------------------------------------------*/
613 /* Hashtable callbacks for maintaining the "bases" field of
614 store_group_info, given that the addresses are function invariants. */
616 struct invariant_group_base_hasher : nofree_ptr_hash <group_info>
618 static inline hashval_t hash (const group_info *);
619 static inline bool equal (const group_info *, const group_info *);
622 inline bool
623 invariant_group_base_hasher::equal (const group_info *gi1,
624 const group_info *gi2)
626 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
629 inline hashval_t
630 invariant_group_base_hasher::hash (const group_info *gi)
632 int do_not_record;
633 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
636 /* Tables of group_info structures, hashed by base value. */
637 static hash_table<invariant_group_base_hasher> *rtx_group_table;
640 /* Get the GROUP for BASE. Add a new group if it is not there. */
642 static group_info *
643 get_group_info (rtx base)
645 struct group_info tmp_gi;
646 group_info *gi;
647 group_info **slot;
649 gcc_assert (base != NULL_RTX);
651 /* Find the store_base_info structure for BASE, creating a new one
652 if necessary. */
653 tmp_gi.rtx_base = base;
654 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
655 gi = *slot;
657 if (gi == NULL)
659 *slot = gi = group_info_pool.allocate ();
660 gi->rtx_base = base;
661 gi->id = rtx_group_next_id++;
662 gi->base_mem = gen_rtx_MEM (BLKmode, base);
663 gi->canon_base_addr = canon_rtx (base);
664 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
665 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
666 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
667 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
668 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
669 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
670 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
671 gi->process_globally = false;
672 gi->frame_related =
673 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
674 gi->offset_map_size_n = 0;
675 gi->offset_map_size_p = 0;
676 gi->offset_map_n = NULL;
677 gi->offset_map_p = NULL;
678 rtx_group_vec.safe_push (gi);
681 return gi;
685 /* Initialization of data structures. */
687 static void
688 dse_step0 (void)
690 locally_deleted = 0;
691 globally_deleted = 0;
693 bitmap_obstack_initialize (&dse_bitmap_obstack);
694 gcc_obstack_init (&dse_obstack);
696 scratch = BITMAP_ALLOC (&reg_obstack);
697 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
700 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
702 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
703 rtx_group_next_id = 0;
705 stores_off_frame_dead_at_return = !cfun->stdarg;
707 init_alias_analysis ();
712 /*----------------------------------------------------------------------------
713 First step.
715 Scan all of the insns. Any random ordering of the blocks is fine.
716 Each block is scanned in forward order to accommodate cselib which
717 is used to remove stores with non-constant bases.
718 ----------------------------------------------------------------------------*/
720 /* Delete all of the store_info recs from INSN_INFO. */
722 static void
723 free_store_info (insn_info_t insn_info)
725 store_info *cur = insn_info->store_rec;
726 while (cur)
728 store_info *next = cur->next;
729 if (cur->is_large)
730 BITMAP_FREE (cur->positions_needed.large.bmap);
731 if (cur->cse_base)
732 cse_store_info_pool.remove (cur);
733 else
734 rtx_store_info_pool.remove (cur);
735 cur = next;
738 insn_info->cannot_delete = true;
739 insn_info->contains_cselib_groups = false;
740 insn_info->store_rec = NULL;
743 struct note_add_store_info
745 rtx_insn *first, *current;
746 regset fixed_regs_live;
747 bool failure;
750 /* Callback for emit_inc_dec_insn_before via note_stores.
751 Check if a register is clobbered which is live afterwards. */
753 static void
754 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
756 rtx_insn *insn;
757 note_add_store_info *info = (note_add_store_info *) data;
759 if (!REG_P (loc))
760 return;
762 /* If this register is referenced by the current or an earlier insn,
763 that's OK. E.g. this applies to the register that is being incremented
764 with this addition. */
765 for (insn = info->first;
766 insn != NEXT_INSN (info->current);
767 insn = NEXT_INSN (insn))
768 if (reg_referenced_p (loc, PATTERN (insn)))
769 return;
771 /* If we come here, we have a clobber of a register that's only OK
772 if that register is not live. If we don't have liveness information
773 available, fail now. */
774 if (!info->fixed_regs_live)
776 info->failure = true;
777 return;
779 /* Now check if this is a live fixed register. */
780 unsigned int end_regno = END_REGNO (loc);
781 for (unsigned int regno = REGNO (loc); regno < end_regno; ++regno)
782 if (REGNO_REG_SET_P (info->fixed_regs_live, regno))
783 info->failure = true;
786 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
787 SRC + SRCOFF before insn ARG. */
789 static int
790 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
791 rtx op ATTRIBUTE_UNUSED,
792 rtx dest, rtx src, rtx srcoff, void *arg)
794 insn_info_t insn_info = (insn_info_t) arg;
795 rtx_insn *insn = insn_info->insn, *new_insn, *cur;
796 note_add_store_info info;
798 /* We can reuse all operands without copying, because we are about
799 to delete the insn that contained it. */
800 if (srcoff)
802 start_sequence ();
803 emit_insn (gen_add3_insn (dest, src, srcoff));
804 new_insn = get_insns ();
805 end_sequence ();
807 else
808 new_insn = gen_move_insn (dest, src);
809 info.first = new_insn;
810 info.fixed_regs_live = insn_info->fixed_regs_live;
811 info.failure = false;
812 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
814 info.current = cur;
815 note_stores (PATTERN (cur), note_add_store, &info);
818 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
819 return it immediately, communicating the failure to its caller. */
820 if (info.failure)
821 return 1;
823 emit_insn_before (new_insn, insn);
825 return 0;
828 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
829 is there, is split into a separate insn.
830 Return true on success (or if there was nothing to do), false on failure. */
832 static bool
833 check_for_inc_dec_1 (insn_info_t insn_info)
835 rtx_insn *insn = insn_info->insn;
836 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
837 if (note)
838 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
839 insn_info) == 0;
840 return true;
844 /* Entry point for postreload. If you work on reload_cse, or you need this
845 anywhere else, consider if you can provide register liveness information
846 and add a parameter to this function so that it can be passed down in
847 insn_info.fixed_regs_live. */
848 bool
849 check_for_inc_dec (rtx_insn *insn)
851 insn_info_type insn_info;
852 rtx note;
854 insn_info.insn = insn;
855 insn_info.fixed_regs_live = NULL;
856 note = find_reg_note (insn, REG_INC, NULL_RTX);
857 if (note)
858 return for_each_inc_dec (PATTERN (insn), emit_inc_dec_insn_before,
859 &insn_info) == 0;
860 return true;
863 /* Delete the insn and free all of the fields inside INSN_INFO. */
865 static void
866 delete_dead_store_insn (insn_info_t insn_info)
868 read_info_t read_info;
870 if (!dbg_cnt (dse))
871 return;
873 if (!check_for_inc_dec_1 (insn_info))
874 return;
875 if (dump_file && (dump_flags & TDF_DETAILS))
876 fprintf (dump_file, "Locally deleting insn %d\n",
877 INSN_UID (insn_info->insn));
879 free_store_info (insn_info);
880 read_info = insn_info->read_rec;
882 while (read_info)
884 read_info_t next = read_info->next;
885 read_info_type_pool.remove (read_info);
886 read_info = next;
888 insn_info->read_rec = NULL;
890 delete_insn (insn_info->insn);
891 locally_deleted++;
892 insn_info->insn = NULL;
894 insn_info->wild_read = false;
897 /* Return whether DECL, a local variable, can possibly escape the current
898 function scope. */
900 static bool
901 local_variable_can_escape (tree decl)
903 if (TREE_ADDRESSABLE (decl))
904 return true;
906 /* If this is a partitioned variable, we need to consider all the variables
907 in the partition. This is necessary because a store into one of them can
908 be replaced with a store into another and this may not change the outcome
909 of the escape analysis. */
910 if (cfun->gimple_df->decls_to_pointers != NULL)
912 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
913 if (namep)
914 return TREE_ADDRESSABLE (*namep);
917 return false;
920 /* Return whether EXPR can possibly escape the current function scope. */
922 static bool
923 can_escape (tree expr)
925 tree base;
926 if (!expr)
927 return true;
928 base = get_base_address (expr);
929 if (DECL_P (base)
930 && !may_be_aliased (base)
931 && !(VAR_P (base)
932 && !DECL_EXTERNAL (base)
933 && !TREE_STATIC (base)
934 && local_variable_can_escape (base)))
935 return false;
936 return true;
939 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
940 OFFSET and WIDTH. */
942 static void
943 set_usage_bits (group_info *group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
944 tree expr)
946 HOST_WIDE_INT i;
947 bool expr_escapes = can_escape (expr);
948 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
949 for (i=offset; i<offset+width; i++)
951 bitmap store1;
952 bitmap store2;
953 bitmap escaped;
954 int ai;
955 if (i < 0)
957 store1 = group->store1_n;
958 store2 = group->store2_n;
959 escaped = group->escaped_n;
960 ai = -i;
962 else
964 store1 = group->store1_p;
965 store2 = group->store2_p;
966 escaped = group->escaped_p;
967 ai = i;
970 if (!bitmap_set_bit (store1, ai))
971 bitmap_set_bit (store2, ai);
972 else
974 if (i < 0)
976 if (group->offset_map_size_n < ai)
977 group->offset_map_size_n = ai;
979 else
981 if (group->offset_map_size_p < ai)
982 group->offset_map_size_p = ai;
985 if (expr_escapes)
986 bitmap_set_bit (escaped, ai);
990 static void
991 reset_active_stores (void)
993 active_local_stores = NULL;
994 active_local_stores_len = 0;
997 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
999 static void
1000 free_read_records (bb_info_t bb_info)
1002 insn_info_t insn_info = bb_info->last_insn;
1003 read_info_t *ptr = &insn_info->read_rec;
1004 while (*ptr)
1006 read_info_t next = (*ptr)->next;
1007 read_info_type_pool.remove (*ptr);
1008 *ptr = next;
1012 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1014 static void
1015 add_wild_read (bb_info_t bb_info)
1017 insn_info_t insn_info = bb_info->last_insn;
1018 insn_info->wild_read = true;
1019 free_read_records (bb_info);
1020 reset_active_stores ();
1023 /* Set the BB_INFO so that the last insn is marked as a wild read of
1024 non-frame locations. */
1026 static void
1027 add_non_frame_wild_read (bb_info_t bb_info)
1029 insn_info_t insn_info = bb_info->last_insn;
1030 insn_info->non_frame_wild_read = true;
1031 free_read_records (bb_info);
1032 reset_active_stores ();
1035 /* Return true if X is a constant or one of the registers that behave
1036 as a constant over the life of a function. This is equivalent to
1037 !rtx_varies_p for memory addresses. */
1039 static bool
1040 const_or_frame_p (rtx x)
1042 if (CONSTANT_P (x))
1043 return true;
1045 if (GET_CODE (x) == REG)
1047 /* Note that we have to test for the actual rtx used for the frame
1048 and arg pointers and not just the register number in case we have
1049 eliminated the frame and/or arg pointer and are using it
1050 for pseudos. */
1051 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1052 /* The arg pointer varies if it is not a fixed register. */
1053 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1054 || x == pic_offset_table_rtx)
1055 return true;
1056 return false;
1059 return false;
1062 /* Take all reasonable action to put the address of MEM into the form
1063 that we can do analysis on.
1065 The gold standard is to get the address into the form: address +
1066 OFFSET where address is something that rtx_varies_p considers a
1067 constant. When we can get the address in this form, we can do
1068 global analysis on it. Note that for constant bases, address is
1069 not actually returned, only the group_id. The address can be
1070 obtained from that.
1072 If that fails, we try cselib to get a value we can at least use
1073 locally. If that fails we return false.
1075 The GROUP_ID is set to -1 for cselib bases and the index of the
1076 group for non_varying bases.
1078 FOR_READ is true if this is a mem read and false if not. */
1080 static bool
1081 canon_address (rtx mem,
1082 int *group_id,
1083 HOST_WIDE_INT *offset,
1084 cselib_val **base)
1086 machine_mode address_mode = get_address_mode (mem);
1087 rtx mem_address = XEXP (mem, 0);
1088 rtx expanded_address, address;
1089 int expanded;
1091 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1093 if (dump_file && (dump_flags & TDF_DETAILS))
1095 fprintf (dump_file, " mem: ");
1096 print_inline_rtx (dump_file, mem_address, 0);
1097 fprintf (dump_file, "\n");
1100 /* First see if just canon_rtx (mem_address) is const or frame,
1101 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1102 address = NULL_RTX;
1103 for (expanded = 0; expanded < 2; expanded++)
1105 if (expanded)
1107 /* Use cselib to replace all of the reg references with the full
1108 expression. This will take care of the case where we have
1110 r_x = base + offset;
1111 val = *r_x;
1113 by making it into
1115 val = *(base + offset); */
1117 expanded_address = cselib_expand_value_rtx (mem_address,
1118 scratch, 5);
1120 /* If this fails, just go with the address from first
1121 iteration. */
1122 if (!expanded_address)
1123 break;
1125 else
1126 expanded_address = mem_address;
1128 /* Split the address into canonical BASE + OFFSET terms. */
1129 address = canon_rtx (expanded_address);
1131 *offset = 0;
1133 if (dump_file && (dump_flags & TDF_DETAILS))
1135 if (expanded)
1137 fprintf (dump_file, "\n after cselib_expand address: ");
1138 print_inline_rtx (dump_file, expanded_address, 0);
1139 fprintf (dump_file, "\n");
1142 fprintf (dump_file, "\n after canon_rtx address: ");
1143 print_inline_rtx (dump_file, address, 0);
1144 fprintf (dump_file, "\n");
1147 if (GET_CODE (address) == CONST)
1148 address = XEXP (address, 0);
1150 if (GET_CODE (address) == PLUS
1151 && CONST_INT_P (XEXP (address, 1)))
1153 *offset = INTVAL (XEXP (address, 1));
1154 address = XEXP (address, 0);
1157 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1158 && const_or_frame_p (address))
1160 group_info *group = get_group_info (address);
1162 if (dump_file && (dump_flags & TDF_DETAILS))
1163 fprintf (dump_file, " gid=%d offset=%d \n",
1164 group->id, (int)*offset);
1165 *base = NULL;
1166 *group_id = group->id;
1167 return true;
1171 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1172 *group_id = -1;
1174 if (*base == NULL)
1176 if (dump_file && (dump_flags & TDF_DETAILS))
1177 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1178 return false;
1180 if (dump_file && (dump_flags & TDF_DETAILS))
1181 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1182 (*base)->uid, (*base)->hash, (int)*offset);
1183 return true;
1187 /* Clear the rhs field from the active_local_stores array. */
1189 static void
1190 clear_rhs_from_active_local_stores (void)
1192 insn_info_t ptr = active_local_stores;
1194 while (ptr)
1196 store_info *store_info = ptr->store_rec;
1197 /* Skip the clobbers. */
1198 while (!store_info->is_set)
1199 store_info = store_info->next;
1201 store_info->rhs = NULL;
1202 store_info->const_rhs = NULL;
1204 ptr = ptr->next_local_store;
1209 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1211 static inline void
1212 set_position_unneeded (store_info *s_info, int pos)
1214 if (__builtin_expect (s_info->is_large, false))
1216 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1217 s_info->positions_needed.large.count++;
1219 else
1220 s_info->positions_needed.small_bitmask
1221 &= ~(HOST_WIDE_INT_1U << pos);
1224 /* Mark the whole store S_INFO as unneeded. */
1226 static inline void
1227 set_all_positions_unneeded (store_info *s_info)
1229 if (__builtin_expect (s_info->is_large, false))
1231 bitmap_set_range (s_info->positions_needed.large.bmap,
1232 0, s_info->width);
1233 s_info->positions_needed.large.count = s_info->width;
1235 else
1236 s_info->positions_needed.small_bitmask = HOST_WIDE_INT_0U;
1239 /* Return TRUE if any bytes from S_INFO store are needed. */
1241 static inline bool
1242 any_positions_needed_p (store_info *s_info)
1244 if (__builtin_expect (s_info->is_large, false))
1245 return s_info->positions_needed.large.count < s_info->width;
1246 else
1247 return (s_info->positions_needed.small_bitmask != HOST_WIDE_INT_0U);
1250 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1251 store are needed. */
1253 static inline bool
1254 all_positions_needed_p (store_info *s_info, int start, int width)
1256 if (__builtin_expect (s_info->is_large, false))
1258 int end = start + width;
1259 while (start < end)
1260 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1261 return false;
1262 return true;
1264 else
1266 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1267 return (s_info->positions_needed.small_bitmask & mask) == mask;
1272 static rtx get_stored_val (store_info *, machine_mode, HOST_WIDE_INT,
1273 HOST_WIDE_INT, basic_block, bool);
1276 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1277 there is a candidate store, after adding it to the appropriate
1278 local store group if so. */
1280 static int
1281 record_store (rtx body, bb_info_t bb_info)
1283 rtx mem, rhs, const_rhs, mem_addr;
1284 HOST_WIDE_INT offset = 0;
1285 HOST_WIDE_INT width = 0;
1286 insn_info_t insn_info = bb_info->last_insn;
1287 store_info *store_info = NULL;
1288 int group_id;
1289 cselib_val *base = NULL;
1290 insn_info_t ptr, last, redundant_reason;
1291 bool store_is_unused;
1293 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1294 return 0;
1296 mem = SET_DEST (body);
1298 /* If this is not used, then this cannot be used to keep the insn
1299 from being deleted. On the other hand, it does provide something
1300 that can be used to prove that another store is dead. */
1301 store_is_unused
1302 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1304 /* Check whether that value is a suitable memory location. */
1305 if (!MEM_P (mem))
1307 /* If the set or clobber is unused, then it does not effect our
1308 ability to get rid of the entire insn. */
1309 if (!store_is_unused)
1310 insn_info->cannot_delete = true;
1311 return 0;
1314 /* At this point we know mem is a mem. */
1315 if (GET_MODE (mem) == BLKmode)
1317 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1319 if (dump_file && (dump_flags & TDF_DETAILS))
1320 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1321 add_wild_read (bb_info);
1322 insn_info->cannot_delete = true;
1323 return 0;
1325 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1326 as memset (addr, 0, 36); */
1327 else if (!MEM_SIZE_KNOWN_P (mem)
1328 || MEM_SIZE (mem) <= 0
1329 || MEM_SIZE (mem) > MAX_OFFSET
1330 || GET_CODE (body) != SET
1331 || !CONST_INT_P (SET_SRC (body)))
1333 if (!store_is_unused)
1335 /* If the set or clobber is unused, then it does not effect our
1336 ability to get rid of the entire insn. */
1337 insn_info->cannot_delete = true;
1338 clear_rhs_from_active_local_stores ();
1340 return 0;
1344 /* We can still process a volatile mem, we just cannot delete it. */
1345 if (MEM_VOLATILE_P (mem))
1346 insn_info->cannot_delete = true;
1348 if (!canon_address (mem, &group_id, &offset, &base))
1350 clear_rhs_from_active_local_stores ();
1351 return 0;
1354 if (GET_MODE (mem) == BLKmode)
1355 width = MEM_SIZE (mem);
1356 else
1357 width = GET_MODE_SIZE (GET_MODE (mem));
1359 if (offset > HOST_WIDE_INT_MAX - width)
1361 clear_rhs_from_active_local_stores ();
1362 return 0;
1365 if (group_id >= 0)
1367 /* In the restrictive case where the base is a constant or the
1368 frame pointer we can do global analysis. */
1370 group_info *group
1371 = rtx_group_vec[group_id];
1372 tree expr = MEM_EXPR (mem);
1374 store_info = rtx_store_info_pool.allocate ();
1375 set_usage_bits (group, offset, width, expr);
1377 if (dump_file && (dump_flags & TDF_DETAILS))
1379 fprintf (dump_file, " processing const base store gid=%d",
1380 group_id);
1381 print_range (dump_file, offset, width);
1382 fprintf (dump_file, "\n");
1385 else
1387 if (may_be_sp_based_p (XEXP (mem, 0)))
1388 insn_info->stack_pointer_based = true;
1389 insn_info->contains_cselib_groups = true;
1391 store_info = cse_store_info_pool.allocate ();
1392 group_id = -1;
1394 if (dump_file && (dump_flags & TDF_DETAILS))
1396 fprintf (dump_file, " processing cselib store ");
1397 print_range (dump_file, offset, width);
1398 fprintf (dump_file, "\n");
1402 const_rhs = rhs = NULL_RTX;
1403 if (GET_CODE (body) == SET
1404 /* No place to keep the value after ra. */
1405 && !reload_completed
1406 && (REG_P (SET_SRC (body))
1407 || GET_CODE (SET_SRC (body)) == SUBREG
1408 || CONSTANT_P (SET_SRC (body)))
1409 && !MEM_VOLATILE_P (mem)
1410 /* Sometimes the store and reload is used for truncation and
1411 rounding. */
1412 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1414 rhs = SET_SRC (body);
1415 if (CONSTANT_P (rhs))
1416 const_rhs = rhs;
1417 else if (body == PATTERN (insn_info->insn))
1419 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1420 if (tem && CONSTANT_P (XEXP (tem, 0)))
1421 const_rhs = XEXP (tem, 0);
1423 if (const_rhs == NULL_RTX && REG_P (rhs))
1425 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1427 if (tem && CONSTANT_P (tem))
1428 const_rhs = tem;
1432 /* Check to see if this stores causes some other stores to be
1433 dead. */
1434 ptr = active_local_stores;
1435 last = NULL;
1436 redundant_reason = NULL;
1437 mem = canon_rtx (mem);
1439 if (group_id < 0)
1440 mem_addr = base->val_rtx;
1441 else
1443 group_info *group = rtx_group_vec[group_id];
1444 mem_addr = group->canon_base_addr;
1446 if (offset)
1447 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1449 while (ptr)
1451 insn_info_t next = ptr->next_local_store;
1452 struct store_info *s_info = ptr->store_rec;
1453 bool del = true;
1455 /* Skip the clobbers. We delete the active insn if this insn
1456 shadows the set. To have been put on the active list, it
1457 has exactly on set. */
1458 while (!s_info->is_set)
1459 s_info = s_info->next;
1461 if (s_info->group_id == group_id && s_info->cse_base == base)
1463 HOST_WIDE_INT i;
1464 if (dump_file && (dump_flags & TDF_DETAILS))
1466 fprintf (dump_file, " trying store in insn=%d gid=%d",
1467 INSN_UID (ptr->insn), s_info->group_id);
1468 print_range (dump_file, s_info->offset, s_info->width);
1469 fprintf (dump_file, "\n");
1472 /* Even if PTR won't be eliminated as unneeded, if both
1473 PTR and this insn store the same constant value, we might
1474 eliminate this insn instead. */
1475 if (s_info->const_rhs
1476 && const_rhs
1477 && known_subrange_p (offset, width,
1478 s_info->offset, s_info->width)
1479 && all_positions_needed_p (s_info, offset - s_info->offset,
1480 width))
1482 if (GET_MODE (mem) == BLKmode)
1484 if (GET_MODE (s_info->mem) == BLKmode
1485 && s_info->const_rhs == const_rhs)
1486 redundant_reason = ptr;
1488 else if (s_info->const_rhs == const0_rtx
1489 && const_rhs == const0_rtx)
1490 redundant_reason = ptr;
1491 else
1493 rtx val;
1494 start_sequence ();
1495 val = get_stored_val (s_info, GET_MODE (mem), offset, width,
1496 BLOCK_FOR_INSN (insn_info->insn),
1497 true);
1498 if (get_insns () != NULL)
1499 val = NULL_RTX;
1500 end_sequence ();
1501 if (val && rtx_equal_p (val, const_rhs))
1502 redundant_reason = ptr;
1506 if (known_subrange_p (s_info->offset, s_info->width, offset, width))
1507 /* The new store touches every byte that S_INFO does. */
1508 set_all_positions_unneeded (s_info);
1509 else
1511 HOST_WIDE_INT begin_unneeded = offset - s_info->offset;
1512 HOST_WIDE_INT end_unneeded = begin_unneeded + width;
1513 begin_unneeded = MAX (begin_unneeded, 0);
1514 end_unneeded = MIN (end_unneeded, s_info->width);
1515 for (i = begin_unneeded; i < end_unneeded; ++i)
1516 set_position_unneeded (s_info, i);
1519 else if (s_info->rhs)
1520 /* Need to see if it is possible for this store to overwrite
1521 the value of store_info. If it is, set the rhs to NULL to
1522 keep it from being used to remove a load. */
1524 if (canon_output_dependence (s_info->mem, true,
1525 mem, GET_MODE (mem),
1526 mem_addr))
1528 s_info->rhs = NULL;
1529 s_info->const_rhs = NULL;
1533 /* An insn can be deleted if every position of every one of
1534 its s_infos is zero. */
1535 if (any_positions_needed_p (s_info))
1536 del = false;
1538 if (del)
1540 insn_info_t insn_to_delete = ptr;
1542 active_local_stores_len--;
1543 if (last)
1544 last->next_local_store = ptr->next_local_store;
1545 else
1546 active_local_stores = ptr->next_local_store;
1548 if (!insn_to_delete->cannot_delete)
1549 delete_dead_store_insn (insn_to_delete);
1551 else
1552 last = ptr;
1554 ptr = next;
1557 /* Finish filling in the store_info. */
1558 store_info->next = insn_info->store_rec;
1559 insn_info->store_rec = store_info;
1560 store_info->mem = mem;
1561 store_info->mem_addr = mem_addr;
1562 store_info->cse_base = base;
1563 if (width > HOST_BITS_PER_WIDE_INT)
1565 store_info->is_large = true;
1566 store_info->positions_needed.large.count = 0;
1567 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1569 else
1571 store_info->is_large = false;
1572 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1574 store_info->group_id = group_id;
1575 store_info->offset = offset;
1576 store_info->width = width;
1577 store_info->is_set = GET_CODE (body) == SET;
1578 store_info->rhs = rhs;
1579 store_info->const_rhs = const_rhs;
1580 store_info->redundant_reason = redundant_reason;
1582 /* If this is a clobber, we return 0. We will only be able to
1583 delete this insn if there is only one store USED store, but we
1584 can use the clobber to delete other stores earlier. */
1585 return store_info->is_set ? 1 : 0;
1589 static void
1590 dump_insn_info (const char * start, insn_info_t insn_info)
1592 fprintf (dump_file, "%s insn=%d %s\n", start,
1593 INSN_UID (insn_info->insn),
1594 insn_info->store_rec ? "has store" : "naked");
1598 /* If the modes are different and the value's source and target do not
1599 line up, we need to extract the value from lower part of the rhs of
1600 the store, shift it, and then put it into a form that can be shoved
1601 into the read_insn. This function generates a right SHIFT of a
1602 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1603 shift sequence is returned or NULL if we failed to find a
1604 shift. */
1606 static rtx
1607 find_shift_sequence (int access_size,
1608 store_info *store_info,
1609 machine_mode read_mode,
1610 int shift, bool speed, bool require_cst)
1612 machine_mode store_mode = GET_MODE (store_info->mem);
1613 scalar_int_mode new_mode;
1614 rtx read_reg = NULL;
1616 /* Some machines like the x86 have shift insns for each size of
1617 operand. Other machines like the ppc or the ia-64 may only have
1618 shift insns that shift values within 32 or 64 bit registers.
1619 This loop tries to find the smallest shift insn that will right
1620 justify the value we want to read but is available in one insn on
1621 the machine. */
1623 opt_scalar_int_mode new_mode_iter;
1624 FOR_EACH_MODE_FROM (new_mode_iter,
1625 smallest_int_mode_for_size (access_size * BITS_PER_UNIT))
1627 rtx target, new_reg, new_lhs;
1628 rtx_insn *shift_seq, *insn;
1629 int cost;
1631 new_mode = new_mode_iter.require ();
1632 if (GET_MODE_BITSIZE (new_mode) > BITS_PER_WORD)
1633 break;
1635 /* If a constant was stored into memory, try to simplify it here,
1636 otherwise the cost of the shift might preclude this optimization
1637 e.g. at -Os, even when no actual shift will be needed. */
1638 if (store_info->const_rhs)
1640 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1641 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1642 store_mode, byte);
1643 if (ret && CONSTANT_P (ret))
1645 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1646 ret, GEN_INT (shift));
1647 if (ret && CONSTANT_P (ret))
1649 byte = subreg_lowpart_offset (read_mode, new_mode);
1650 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1651 if (ret && CONSTANT_P (ret)
1652 && (set_src_cost (ret, read_mode, speed)
1653 <= COSTS_N_INSNS (1)))
1654 return ret;
1659 if (require_cst)
1660 return NULL_RTX;
1662 /* Try a wider mode if truncating the store mode to NEW_MODE
1663 requires a real instruction. */
1664 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1665 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1666 continue;
1668 /* Also try a wider mode if the necessary punning is either not
1669 desirable or not possible. */
1670 if (!CONSTANT_P (store_info->rhs)
1671 && !targetm.modes_tieable_p (new_mode, store_mode))
1672 continue;
1674 new_reg = gen_reg_rtx (new_mode);
1676 start_sequence ();
1678 /* In theory we could also check for an ashr. Ian Taylor knows
1679 of one dsp where the cost of these two was not the same. But
1680 this really is a rare case anyway. */
1681 target = expand_binop (new_mode, lshr_optab, new_reg,
1682 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1684 shift_seq = get_insns ();
1685 end_sequence ();
1687 if (target != new_reg || shift_seq == NULL)
1688 continue;
1690 cost = 0;
1691 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1692 if (INSN_P (insn))
1693 cost += insn_cost (insn, speed);
1695 /* The computation up to here is essentially independent
1696 of the arguments and could be precomputed. It may
1697 not be worth doing so. We could precompute if
1698 worthwhile or at least cache the results. The result
1699 technically depends on both SHIFT and ACCESS_SIZE,
1700 but in practice the answer will depend only on ACCESS_SIZE. */
1702 if (cost > COSTS_N_INSNS (1))
1703 continue;
1705 new_lhs = extract_low_bits (new_mode, store_mode,
1706 copy_rtx (store_info->rhs));
1707 if (new_lhs == NULL_RTX)
1708 continue;
1710 /* We found an acceptable shift. Generate a move to
1711 take the value from the store and put it into the
1712 shift pseudo, then shift it, then generate another
1713 move to put in into the target of the read. */
1714 emit_move_insn (new_reg, new_lhs);
1715 emit_insn (shift_seq);
1716 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1717 break;
1720 return read_reg;
1724 /* Call back for note_stores to find the hard regs set or clobbered by
1725 insn. Data is a bitmap of the hardregs set so far. */
1727 static void
1728 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1730 bitmap regs_set = (bitmap) data;
1732 if (REG_P (x)
1733 && HARD_REGISTER_P (x))
1734 bitmap_set_range (regs_set, REGNO (x), REG_NREGS (x));
1737 /* Helper function for replace_read and record_store.
1738 Attempt to return a value of mode READ_MODE stored in STORE_INFO,
1739 consisting of READ_WIDTH bytes starting from READ_OFFSET. Return NULL
1740 if not successful. If REQUIRE_CST is true, return always constant. */
1742 static rtx
1743 get_stored_val (store_info *store_info, machine_mode read_mode,
1744 HOST_WIDE_INT read_offset, HOST_WIDE_INT read_width,
1745 basic_block bb, bool require_cst)
1747 machine_mode store_mode = GET_MODE (store_info->mem);
1748 HOST_WIDE_INT gap;
1749 rtx read_reg;
1751 /* To get here the read is within the boundaries of the write so
1752 shift will never be negative. Start out with the shift being in
1753 bytes. */
1754 if (store_mode == BLKmode)
1755 gap = 0;
1756 else if (BYTES_BIG_ENDIAN)
1757 gap = ((store_info->offset + store_info->width)
1758 - (read_offset + read_width));
1759 else
1760 gap = read_offset - store_info->offset;
1762 if (gap != 0)
1764 HOST_WIDE_INT shift = gap * BITS_PER_UNIT;
1765 HOST_WIDE_INT access_size = GET_MODE_SIZE (read_mode) + gap;
1766 read_reg = find_shift_sequence (access_size, store_info, read_mode,
1767 shift, optimize_bb_for_speed_p (bb),
1768 require_cst);
1770 else if (store_mode == BLKmode)
1772 /* The store is a memset (addr, const_val, const_size). */
1773 gcc_assert (CONST_INT_P (store_info->rhs));
1774 scalar_int_mode int_store_mode;
1775 if (!int_mode_for_mode (read_mode).exists (&int_store_mode))
1776 read_reg = NULL_RTX;
1777 else if (store_info->rhs == const0_rtx)
1778 read_reg = extract_low_bits (read_mode, int_store_mode, const0_rtx);
1779 else if (GET_MODE_BITSIZE (int_store_mode) > HOST_BITS_PER_WIDE_INT
1780 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1781 read_reg = NULL_RTX;
1782 else
1784 unsigned HOST_WIDE_INT c
1785 = INTVAL (store_info->rhs)
1786 & ((HOST_WIDE_INT_1 << BITS_PER_UNIT) - 1);
1787 int shift = BITS_PER_UNIT;
1788 while (shift < HOST_BITS_PER_WIDE_INT)
1790 c |= (c << shift);
1791 shift <<= 1;
1793 read_reg = gen_int_mode (c, int_store_mode);
1794 read_reg = extract_low_bits (read_mode, int_store_mode, read_reg);
1797 else if (store_info->const_rhs
1798 && (require_cst
1799 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1800 read_reg = extract_low_bits (read_mode, store_mode,
1801 copy_rtx (store_info->const_rhs));
1802 else
1803 read_reg = extract_low_bits (read_mode, store_mode,
1804 copy_rtx (store_info->rhs));
1805 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1806 read_reg = NULL_RTX;
1807 return read_reg;
1810 /* Take a sequence of:
1811 A <- r1
1813 ... <- A
1815 and change it into
1816 r2 <- r1
1817 A <- r1
1819 ... <- r2
1823 r3 <- extract (r1)
1824 r3 <- r3 >> shift
1825 r2 <- extract (r3)
1826 ... <- r2
1830 r2 <- extract (r1)
1831 ... <- r2
1833 Depending on the alignment and the mode of the store and
1834 subsequent load.
1837 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1838 and READ_INSN are for the read. Return true if the replacement
1839 went ok. */
1841 static bool
1842 replace_read (store_info *store_info, insn_info_t store_insn,
1843 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1844 bitmap regs_live)
1846 machine_mode store_mode = GET_MODE (store_info->mem);
1847 machine_mode read_mode = GET_MODE (read_info->mem);
1848 rtx_insn *insns, *this_insn;
1849 rtx read_reg;
1850 basic_block bb;
1852 if (!dbg_cnt (dse))
1853 return false;
1855 /* Create a sequence of instructions to set up the read register.
1856 This sequence goes immediately before the store and its result
1857 is read by the load.
1859 We need to keep this in perspective. We are replacing a read
1860 with a sequence of insns, but the read will almost certainly be
1861 in cache, so it is not going to be an expensive one. Thus, we
1862 are not willing to do a multi insn shift or worse a subroutine
1863 call to get rid of the read. */
1864 if (dump_file && (dump_flags & TDF_DETAILS))
1865 fprintf (dump_file, "trying to replace %smode load in insn %d"
1866 " from %smode store in insn %d\n",
1867 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1868 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1869 start_sequence ();
1870 bb = BLOCK_FOR_INSN (read_insn->insn);
1871 read_reg = get_stored_val (store_info,
1872 read_mode, read_info->offset, read_info->width,
1873 bb, false);
1874 if (read_reg == NULL_RTX)
1876 end_sequence ();
1877 if (dump_file && (dump_flags & TDF_DETAILS))
1878 fprintf (dump_file, " -- could not extract bits of stored value\n");
1879 return false;
1881 /* Force the value into a new register so that it won't be clobbered
1882 between the store and the load. */
1883 read_reg = copy_to_mode_reg (read_mode, read_reg);
1884 insns = get_insns ();
1885 end_sequence ();
1887 if (insns != NULL_RTX)
1889 /* Now we have to scan the set of new instructions to see if the
1890 sequence contains and sets of hardregs that happened to be
1891 live at this point. For instance, this can happen if one of
1892 the insns sets the CC and the CC happened to be live at that
1893 point. This does occasionally happen, see PR 37922. */
1894 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
1896 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
1897 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
1899 bitmap_and_into (regs_set, regs_live);
1900 if (!bitmap_empty_p (regs_set))
1902 if (dump_file && (dump_flags & TDF_DETAILS))
1904 fprintf (dump_file,
1905 "abandoning replacement because sequence clobbers live hardregs:");
1906 df_print_regset (dump_file, regs_set);
1909 BITMAP_FREE (regs_set);
1910 return false;
1912 BITMAP_FREE (regs_set);
1915 if (validate_change (read_insn->insn, loc, read_reg, 0))
1917 deferred_change *change = deferred_change_pool.allocate ();
1919 /* Insert this right before the store insn where it will be safe
1920 from later insns that might change it before the read. */
1921 emit_insn_before (insns, store_insn->insn);
1923 /* And now for the kludge part: cselib croaks if you just
1924 return at this point. There are two reasons for this:
1926 1) Cselib has an idea of how many pseudos there are and
1927 that does not include the new ones we just added.
1929 2) Cselib does not know about the move insn we added
1930 above the store_info, and there is no way to tell it
1931 about it, because it has "moved on".
1933 Problem (1) is fixable with a certain amount of engineering.
1934 Problem (2) is requires starting the bb from scratch. This
1935 could be expensive.
1937 So we are just going to have to lie. The move/extraction
1938 insns are not really an issue, cselib did not see them. But
1939 the use of the new pseudo read_insn is a real problem because
1940 cselib has not scanned this insn. The way that we solve this
1941 problem is that we are just going to put the mem back for now
1942 and when we are finished with the block, we undo this. We
1943 keep a table of mems to get rid of. At the end of the basic
1944 block we can put them back. */
1946 *loc = read_info->mem;
1947 change->next = deferred_change_list;
1948 deferred_change_list = change;
1949 change->loc = loc;
1950 change->reg = read_reg;
1952 /* Get rid of the read_info, from the point of view of the
1953 rest of dse, play like this read never happened. */
1954 read_insn->read_rec = read_info->next;
1955 read_info_type_pool.remove (read_info);
1956 if (dump_file && (dump_flags & TDF_DETAILS))
1958 fprintf (dump_file, " -- replaced the loaded MEM with ");
1959 print_simple_rtl (dump_file, read_reg);
1960 fprintf (dump_file, "\n");
1962 return true;
1964 else
1966 if (dump_file && (dump_flags & TDF_DETAILS))
1968 fprintf (dump_file, " -- replacing the loaded MEM with ");
1969 print_simple_rtl (dump_file, read_reg);
1970 fprintf (dump_file, " led to an invalid instruction\n");
1972 return false;
1976 /* Check the address of MEM *LOC and kill any appropriate stores that may
1977 be active. */
1979 static void
1980 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
1982 rtx mem = *loc, mem_addr;
1983 insn_info_t insn_info;
1984 HOST_WIDE_INT offset = 0;
1985 HOST_WIDE_INT width = 0;
1986 cselib_val *base = NULL;
1987 int group_id;
1988 read_info_t read_info;
1990 insn_info = bb_info->last_insn;
1992 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
1993 || (MEM_VOLATILE_P (mem)))
1995 if (dump_file && (dump_flags & TDF_DETAILS))
1996 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
1997 add_wild_read (bb_info);
1998 insn_info->cannot_delete = true;
1999 return;
2002 /* If it is reading readonly mem, then there can be no conflict with
2003 another write. */
2004 if (MEM_READONLY_P (mem))
2005 return;
2007 if (!canon_address (mem, &group_id, &offset, &base))
2009 if (dump_file && (dump_flags & TDF_DETAILS))
2010 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2011 add_wild_read (bb_info);
2012 return;
2015 if (GET_MODE (mem) == BLKmode)
2016 width = -1;
2017 else
2018 width = GET_MODE_SIZE (GET_MODE (mem));
2020 if (width == -1
2021 ? offset == HOST_WIDE_INT_MIN
2022 : offset > HOST_WIDE_INT_MAX - width)
2024 if (dump_file && (dump_flags & TDF_DETAILS))
2025 fprintf (dump_file, " adding wild read, due to overflow.\n");
2026 add_wild_read (bb_info);
2027 return;
2030 read_info = read_info_type_pool.allocate ();
2031 read_info->group_id = group_id;
2032 read_info->mem = mem;
2033 read_info->offset = offset;
2034 read_info->width = width;
2035 read_info->next = insn_info->read_rec;
2036 insn_info->read_rec = read_info;
2037 if (group_id < 0)
2038 mem_addr = base->val_rtx;
2039 else
2041 group_info *group = rtx_group_vec[group_id];
2042 mem_addr = group->canon_base_addr;
2044 if (offset)
2045 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2047 if (group_id >= 0)
2049 /* This is the restricted case where the base is a constant or
2050 the frame pointer and offset is a constant. */
2051 insn_info_t i_ptr = active_local_stores;
2052 insn_info_t last = NULL;
2054 if (dump_file && (dump_flags & TDF_DETAILS))
2056 if (width == -1)
2057 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2058 group_id);
2059 else
2061 fprintf (dump_file, " processing const load gid=%d", group_id);
2062 print_range (dump_file, offset, width);
2063 fprintf (dump_file, "\n");
2067 while (i_ptr)
2069 bool remove = false;
2070 store_info *store_info = i_ptr->store_rec;
2072 /* Skip the clobbers. */
2073 while (!store_info->is_set)
2074 store_info = store_info->next;
2076 /* There are three cases here. */
2077 if (store_info->group_id < 0)
2078 /* We have a cselib store followed by a read from a
2079 const base. */
2080 remove
2081 = canon_true_dependence (store_info->mem,
2082 GET_MODE (store_info->mem),
2083 store_info->mem_addr,
2084 mem, mem_addr);
2086 else if (group_id == store_info->group_id)
2088 /* This is a block mode load. We may get lucky and
2089 canon_true_dependence may save the day. */
2090 if (width == -1)
2091 remove
2092 = canon_true_dependence (store_info->mem,
2093 GET_MODE (store_info->mem),
2094 store_info->mem_addr,
2095 mem, mem_addr);
2097 /* If this read is just reading back something that we just
2098 stored, rewrite the read. */
2099 else
2101 if (store_info->rhs
2102 && known_subrange_p (offset, width, store_info->offset,
2103 store_info->width)
2104 && all_positions_needed_p (store_info,
2105 offset - store_info->offset,
2106 width)
2107 && replace_read (store_info, i_ptr, read_info,
2108 insn_info, loc, bb_info->regs_live))
2109 return;
2111 /* The bases are the same, just see if the offsets
2112 could overlap. */
2113 if (ranges_maybe_overlap_p (offset, width,
2114 store_info->offset,
2115 store_info->width))
2116 remove = true;
2120 /* else
2121 The else case that is missing here is that the
2122 bases are constant but different. There is nothing
2123 to do here because there is no overlap. */
2125 if (remove)
2127 if (dump_file && (dump_flags & TDF_DETAILS))
2128 dump_insn_info ("removing from active", i_ptr);
2130 active_local_stores_len--;
2131 if (last)
2132 last->next_local_store = i_ptr->next_local_store;
2133 else
2134 active_local_stores = i_ptr->next_local_store;
2136 else
2137 last = i_ptr;
2138 i_ptr = i_ptr->next_local_store;
2141 else
2143 insn_info_t i_ptr = active_local_stores;
2144 insn_info_t last = NULL;
2145 if (dump_file && (dump_flags & TDF_DETAILS))
2147 fprintf (dump_file, " processing cselib load mem:");
2148 print_inline_rtx (dump_file, mem, 0);
2149 fprintf (dump_file, "\n");
2152 while (i_ptr)
2154 bool remove = false;
2155 store_info *store_info = i_ptr->store_rec;
2157 if (dump_file && (dump_flags & TDF_DETAILS))
2158 fprintf (dump_file, " processing cselib load against insn %d\n",
2159 INSN_UID (i_ptr->insn));
2161 /* Skip the clobbers. */
2162 while (!store_info->is_set)
2163 store_info = store_info->next;
2165 /* If this read is just reading back something that we just
2166 stored, rewrite the read. */
2167 if (store_info->rhs
2168 && store_info->group_id == -1
2169 && store_info->cse_base == base
2170 && known_subrange_p (offset, width, store_info->offset,
2171 store_info->width)
2172 && all_positions_needed_p (store_info,
2173 offset - store_info->offset, width)
2174 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2175 bb_info->regs_live))
2176 return;
2178 remove = canon_true_dependence (store_info->mem,
2179 GET_MODE (store_info->mem),
2180 store_info->mem_addr,
2181 mem, mem_addr);
2183 if (remove)
2185 if (dump_file && (dump_flags & TDF_DETAILS))
2186 dump_insn_info ("removing from active", i_ptr);
2188 active_local_stores_len--;
2189 if (last)
2190 last->next_local_store = i_ptr->next_local_store;
2191 else
2192 active_local_stores = i_ptr->next_local_store;
2194 else
2195 last = i_ptr;
2196 i_ptr = i_ptr->next_local_store;
2201 /* A note_uses callback in which DATA points the INSN_INFO for
2202 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2203 true for any part of *LOC. */
2205 static void
2206 check_mem_read_use (rtx *loc, void *data)
2208 subrtx_ptr_iterator::array_type array;
2209 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2211 rtx *loc = *iter;
2212 if (MEM_P (*loc))
2213 check_mem_read_rtx (loc, (bb_info_t) data);
2218 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2219 So far it only handles arguments passed in registers. */
2221 static bool
2222 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2224 CUMULATIVE_ARGS args_so_far_v;
2225 cumulative_args_t args_so_far;
2226 tree arg;
2227 int idx;
2229 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2230 args_so_far = pack_cumulative_args (&args_so_far_v);
2232 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2233 for (idx = 0;
2234 arg != void_list_node && idx < nargs;
2235 arg = TREE_CHAIN (arg), idx++)
2237 scalar_int_mode mode;
2238 rtx reg, link, tmp;
2240 if (!is_int_mode (TYPE_MODE (TREE_VALUE (arg)), &mode))
2241 return false;
2243 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2244 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode)
2245 return false;
2247 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2248 link;
2249 link = XEXP (link, 1))
2250 if (GET_CODE (XEXP (link, 0)) == USE)
2252 scalar_int_mode arg_mode;
2253 args[idx] = XEXP (XEXP (link, 0), 0);
2254 if (REG_P (args[idx])
2255 && REGNO (args[idx]) == REGNO (reg)
2256 && (GET_MODE (args[idx]) == mode
2257 || (is_int_mode (GET_MODE (args[idx]), &arg_mode)
2258 && (GET_MODE_SIZE (arg_mode) <= UNITS_PER_WORD)
2259 && (GET_MODE_SIZE (arg_mode) > GET_MODE_SIZE (mode)))))
2260 break;
2262 if (!link)
2263 return false;
2265 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2266 if (GET_MODE (args[idx]) != mode)
2268 if (!tmp || !CONST_INT_P (tmp))
2269 return false;
2270 tmp = gen_int_mode (INTVAL (tmp), mode);
2272 if (tmp)
2273 args[idx] = tmp;
2275 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2277 if (arg != void_list_node || idx != nargs)
2278 return false;
2279 return true;
2282 /* Return a bitmap of the fixed registers contained in IN. */
2284 static bitmap
2285 copy_fixed_regs (const_bitmap in)
2287 bitmap ret;
2289 ret = ALLOC_REG_SET (NULL);
2290 bitmap_and (ret, in, fixed_reg_set_regset);
2291 return ret;
2294 /* Apply record_store to all candidate stores in INSN. Mark INSN
2295 if some part of it is not a candidate store and assigns to a
2296 non-register target. */
2298 static void
2299 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2301 rtx body;
2302 insn_info_type *insn_info = insn_info_type_pool.allocate ();
2303 int mems_found = 0;
2304 memset (insn_info, 0, sizeof (struct insn_info_type));
2306 if (dump_file && (dump_flags & TDF_DETAILS))
2307 fprintf (dump_file, "\n**scanning insn=%d\n",
2308 INSN_UID (insn));
2310 insn_info->prev_insn = bb_info->last_insn;
2311 insn_info->insn = insn;
2312 bb_info->last_insn = insn_info;
2314 if (DEBUG_INSN_P (insn))
2316 insn_info->cannot_delete = true;
2317 return;
2320 /* Look at all of the uses in the insn. */
2321 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2323 if (CALL_P (insn))
2325 bool const_call;
2326 rtx call, sym;
2327 tree memset_call = NULL_TREE;
2329 insn_info->cannot_delete = true;
2331 /* Const functions cannot do anything bad i.e. read memory,
2332 however, they can read their parameters which may have
2333 been pushed onto the stack.
2334 memset and bzero don't read memory either. */
2335 const_call = RTL_CONST_CALL_P (insn);
2336 if (!const_call
2337 && (call = get_call_rtx_from (insn))
2338 && (sym = XEXP (XEXP (call, 0), 0))
2339 && GET_CODE (sym) == SYMBOL_REF
2340 && SYMBOL_REF_DECL (sym)
2341 && TREE_CODE (SYMBOL_REF_DECL (sym)) == FUNCTION_DECL
2342 && DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (sym)) == BUILT_IN_NORMAL
2343 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (sym)) == BUILT_IN_MEMSET)
2344 memset_call = SYMBOL_REF_DECL (sym);
2346 if (const_call || memset_call)
2348 insn_info_t i_ptr = active_local_stores;
2349 insn_info_t last = NULL;
2351 if (dump_file && (dump_flags & TDF_DETAILS))
2352 fprintf (dump_file, "%s call %d\n",
2353 const_call ? "const" : "memset", INSN_UID (insn));
2355 /* See the head comment of the frame_read field. */
2356 if (reload_completed
2357 /* Tail calls are storing their arguments using
2358 arg pointer. If it is a frame pointer on the target,
2359 even before reload we need to kill frame pointer based
2360 stores. */
2361 || (SIBLING_CALL_P (insn)
2362 && HARD_FRAME_POINTER_IS_ARG_POINTER))
2363 insn_info->frame_read = true;
2365 /* Loop over the active stores and remove those which are
2366 killed by the const function call. */
2367 while (i_ptr)
2369 bool remove_store = false;
2371 /* The stack pointer based stores are always killed. */
2372 if (i_ptr->stack_pointer_based)
2373 remove_store = true;
2375 /* If the frame is read, the frame related stores are killed. */
2376 else if (insn_info->frame_read)
2378 store_info *store_info = i_ptr->store_rec;
2380 /* Skip the clobbers. */
2381 while (!store_info->is_set)
2382 store_info = store_info->next;
2384 if (store_info->group_id >= 0
2385 && rtx_group_vec[store_info->group_id]->frame_related)
2386 remove_store = true;
2389 if (remove_store)
2391 if (dump_file && (dump_flags & TDF_DETAILS))
2392 dump_insn_info ("removing from active", i_ptr);
2394 active_local_stores_len--;
2395 if (last)
2396 last->next_local_store = i_ptr->next_local_store;
2397 else
2398 active_local_stores = i_ptr->next_local_store;
2400 else
2401 last = i_ptr;
2403 i_ptr = i_ptr->next_local_store;
2406 if (memset_call)
2408 rtx args[3];
2409 if (get_call_args (insn, memset_call, args, 3)
2410 && CONST_INT_P (args[1])
2411 && CONST_INT_P (args[2])
2412 && INTVAL (args[2]) > 0)
2414 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2415 set_mem_size (mem, INTVAL (args[2]));
2416 body = gen_rtx_SET (mem, args[1]);
2417 mems_found += record_store (body, bb_info);
2418 if (dump_file && (dump_flags & TDF_DETAILS))
2419 fprintf (dump_file, "handling memset as BLKmode store\n");
2420 if (mems_found == 1)
2422 if (active_local_stores_len++
2423 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2425 active_local_stores_len = 1;
2426 active_local_stores = NULL;
2428 insn_info->fixed_regs_live
2429 = copy_fixed_regs (bb_info->regs_live);
2430 insn_info->next_local_store = active_local_stores;
2431 active_local_stores = insn_info;
2434 else
2435 clear_rhs_from_active_local_stores ();
2438 else if (SIBLING_CALL_P (insn) && reload_completed)
2439 /* Arguments for a sibling call that are pushed to memory are passed
2440 using the incoming argument pointer of the current function. After
2441 reload that might be (and likely is) frame pointer based. */
2442 add_wild_read (bb_info);
2443 else
2444 /* Every other call, including pure functions, may read any memory
2445 that is not relative to the frame. */
2446 add_non_frame_wild_read (bb_info);
2448 return;
2451 /* Assuming that there are sets in these insns, we cannot delete
2452 them. */
2453 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2454 || volatile_refs_p (PATTERN (insn))
2455 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2456 || (RTX_FRAME_RELATED_P (insn))
2457 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2458 insn_info->cannot_delete = true;
2460 body = PATTERN (insn);
2461 if (GET_CODE (body) == PARALLEL)
2463 int i;
2464 for (i = 0; i < XVECLEN (body, 0); i++)
2465 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2467 else
2468 mems_found += record_store (body, bb_info);
2470 if (dump_file && (dump_flags & TDF_DETAILS))
2471 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2472 mems_found, insn_info->cannot_delete ? "true" : "false");
2474 /* If we found some sets of mems, add it into the active_local_stores so
2475 that it can be locally deleted if found dead or used for
2476 replace_read and redundant constant store elimination. Otherwise mark
2477 it as cannot delete. This simplifies the processing later. */
2478 if (mems_found == 1)
2480 if (active_local_stores_len++
2481 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2483 active_local_stores_len = 1;
2484 active_local_stores = NULL;
2486 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2487 insn_info->next_local_store = active_local_stores;
2488 active_local_stores = insn_info;
2490 else
2491 insn_info->cannot_delete = true;
2495 /* Remove BASE from the set of active_local_stores. This is a
2496 callback from cselib that is used to get rid of the stores in
2497 active_local_stores. */
2499 static void
2500 remove_useless_values (cselib_val *base)
2502 insn_info_t insn_info = active_local_stores;
2503 insn_info_t last = NULL;
2505 while (insn_info)
2507 store_info *store_info = insn_info->store_rec;
2508 bool del = false;
2510 /* If ANY of the store_infos match the cselib group that is
2511 being deleted, then the insn can not be deleted. */
2512 while (store_info)
2514 if ((store_info->group_id == -1)
2515 && (store_info->cse_base == base))
2517 del = true;
2518 break;
2520 store_info = store_info->next;
2523 if (del)
2525 active_local_stores_len--;
2526 if (last)
2527 last->next_local_store = insn_info->next_local_store;
2528 else
2529 active_local_stores = insn_info->next_local_store;
2530 free_store_info (insn_info);
2532 else
2533 last = insn_info;
2535 insn_info = insn_info->next_local_store;
2540 /* Do all of step 1. */
2542 static void
2543 dse_step1 (void)
2545 basic_block bb;
2546 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2548 cselib_init (0);
2549 all_blocks = BITMAP_ALLOC (NULL);
2550 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2551 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2553 FOR_ALL_BB_FN (bb, cfun)
2555 insn_info_t ptr;
2556 bb_info_t bb_info = dse_bb_info_type_pool.allocate ();
2558 memset (bb_info, 0, sizeof (dse_bb_info_type));
2559 bitmap_set_bit (all_blocks, bb->index);
2560 bb_info->regs_live = regs_live;
2562 bitmap_copy (regs_live, DF_LR_IN (bb));
2563 df_simulate_initialize_forwards (bb, regs_live);
2565 bb_table[bb->index] = bb_info;
2566 cselib_discard_hook = remove_useless_values;
2568 if (bb->index >= NUM_FIXED_BLOCKS)
2570 rtx_insn *insn;
2572 active_local_stores = NULL;
2573 active_local_stores_len = 0;
2574 cselib_clear_table ();
2576 /* Scan the insns. */
2577 FOR_BB_INSNS (bb, insn)
2579 if (INSN_P (insn))
2580 scan_insn (bb_info, insn);
2581 cselib_process_insn (insn);
2582 if (INSN_P (insn))
2583 df_simulate_one_insn_forwards (bb, insn, regs_live);
2586 /* This is something of a hack, because the global algorithm
2587 is supposed to take care of the case where stores go dead
2588 at the end of the function. However, the global
2589 algorithm must take a more conservative view of block
2590 mode reads than the local alg does. So to get the case
2591 where you have a store to the frame followed by a non
2592 overlapping block more read, we look at the active local
2593 stores at the end of the function and delete all of the
2594 frame and spill based ones. */
2595 if (stores_off_frame_dead_at_return
2596 && (EDGE_COUNT (bb->succs) == 0
2597 || (single_succ_p (bb)
2598 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2599 && ! crtl->calls_eh_return)))
2601 insn_info_t i_ptr = active_local_stores;
2602 while (i_ptr)
2604 store_info *store_info = i_ptr->store_rec;
2606 /* Skip the clobbers. */
2607 while (!store_info->is_set)
2608 store_info = store_info->next;
2609 if (store_info->group_id >= 0)
2611 group_info *group = rtx_group_vec[store_info->group_id];
2612 if (group->frame_related && !i_ptr->cannot_delete)
2613 delete_dead_store_insn (i_ptr);
2616 i_ptr = i_ptr->next_local_store;
2620 /* Get rid of the loads that were discovered in
2621 replace_read. Cselib is finished with this block. */
2622 while (deferred_change_list)
2624 deferred_change *next = deferred_change_list->next;
2626 /* There is no reason to validate this change. That was
2627 done earlier. */
2628 *deferred_change_list->loc = deferred_change_list->reg;
2629 deferred_change_pool.remove (deferred_change_list);
2630 deferred_change_list = next;
2633 /* Get rid of all of the cselib based store_infos in this
2634 block and mark the containing insns as not being
2635 deletable. */
2636 ptr = bb_info->last_insn;
2637 while (ptr)
2639 if (ptr->contains_cselib_groups)
2641 store_info *s_info = ptr->store_rec;
2642 while (s_info && !s_info->is_set)
2643 s_info = s_info->next;
2644 if (s_info
2645 && s_info->redundant_reason
2646 && s_info->redundant_reason->insn
2647 && !ptr->cannot_delete)
2649 if (dump_file && (dump_flags & TDF_DETAILS))
2650 fprintf (dump_file, "Locally deleting insn %d "
2651 "because insn %d stores the "
2652 "same value and couldn't be "
2653 "eliminated\n",
2654 INSN_UID (ptr->insn),
2655 INSN_UID (s_info->redundant_reason->insn));
2656 delete_dead_store_insn (ptr);
2658 free_store_info (ptr);
2660 else
2662 store_info *s_info;
2664 /* Free at least positions_needed bitmaps. */
2665 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2666 if (s_info->is_large)
2668 BITMAP_FREE (s_info->positions_needed.large.bmap);
2669 s_info->is_large = false;
2672 ptr = ptr->prev_insn;
2675 cse_store_info_pool.release ();
2677 bb_info->regs_live = NULL;
2680 BITMAP_FREE (regs_live);
2681 cselib_finish ();
2682 rtx_group_table->empty ();
2686 /*----------------------------------------------------------------------------
2687 Second step.
2689 Assign each byte position in the stores that we are going to
2690 analyze globally to a position in the bitmaps. Returns true if
2691 there are any bit positions assigned.
2692 ----------------------------------------------------------------------------*/
2694 static void
2695 dse_step2_init (void)
2697 unsigned int i;
2698 group_info *group;
2700 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2702 /* For all non stack related bases, we only consider a store to
2703 be deletable if there are two or more stores for that
2704 position. This is because it takes one store to make the
2705 other store redundant. However, for the stores that are
2706 stack related, we consider them if there is only one store
2707 for the position. We do this because the stack related
2708 stores can be deleted if their is no read between them and
2709 the end of the function.
2711 To make this work in the current framework, we take the stack
2712 related bases add all of the bits from store1 into store2.
2713 This has the effect of making the eligible even if there is
2714 only one store. */
2716 if (stores_off_frame_dead_at_return && group->frame_related)
2718 bitmap_ior_into (group->store2_n, group->store1_n);
2719 bitmap_ior_into (group->store2_p, group->store1_p);
2720 if (dump_file && (dump_flags & TDF_DETAILS))
2721 fprintf (dump_file, "group %d is frame related ", i);
2724 group->offset_map_size_n++;
2725 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2726 group->offset_map_size_n);
2727 group->offset_map_size_p++;
2728 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2729 group->offset_map_size_p);
2730 group->process_globally = false;
2731 if (dump_file && (dump_flags & TDF_DETAILS))
2733 fprintf (dump_file, "group %d(%d+%d): ", i,
2734 (int)bitmap_count_bits (group->store2_n),
2735 (int)bitmap_count_bits (group->store2_p));
2736 bitmap_print (dump_file, group->store2_n, "n ", " ");
2737 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2743 /* Init the offset tables. */
2745 static bool
2746 dse_step2 (void)
2748 unsigned int i;
2749 group_info *group;
2750 /* Position 0 is unused because 0 is used in the maps to mean
2751 unused. */
2752 current_position = 1;
2753 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2755 bitmap_iterator bi;
2756 unsigned int j;
2758 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2759 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2760 bitmap_clear (group->group_kill);
2762 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2764 bitmap_set_bit (group->group_kill, current_position);
2765 if (bitmap_bit_p (group->escaped_n, j))
2766 bitmap_set_bit (kill_on_calls, current_position);
2767 group->offset_map_n[j] = current_position++;
2768 group->process_globally = true;
2770 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2772 bitmap_set_bit (group->group_kill, current_position);
2773 if (bitmap_bit_p (group->escaped_p, j))
2774 bitmap_set_bit (kill_on_calls, current_position);
2775 group->offset_map_p[j] = current_position++;
2776 group->process_globally = true;
2779 return current_position != 1;
2784 /*----------------------------------------------------------------------------
2785 Third step.
2787 Build the bit vectors for the transfer functions.
2788 ----------------------------------------------------------------------------*/
2791 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2792 there, return 0. */
2794 static int
2795 get_bitmap_index (group_info *group_info, HOST_WIDE_INT offset)
2797 if (offset < 0)
2799 HOST_WIDE_INT offset_p = -offset;
2800 if (offset_p >= group_info->offset_map_size_n)
2801 return 0;
2802 return group_info->offset_map_n[offset_p];
2804 else
2806 if (offset >= group_info->offset_map_size_p)
2807 return 0;
2808 return group_info->offset_map_p[offset];
2813 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2814 may be NULL. */
2816 static void
2817 scan_stores (store_info *store_info, bitmap gen, bitmap kill)
2819 while (store_info)
2821 HOST_WIDE_INT i;
2822 group_info *group_info
2823 = rtx_group_vec[store_info->group_id];
2824 if (group_info->process_globally)
2826 HOST_WIDE_INT end = store_info->offset + store_info->width;
2827 for (i = store_info->offset; i < end; i++)
2829 int index = get_bitmap_index (group_info, i);
2830 if (index != 0)
2832 bitmap_set_bit (gen, index);
2833 if (kill)
2834 bitmap_clear_bit (kill, index);
2838 store_info = store_info->next;
2843 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
2844 may be NULL. */
2846 static void
2847 scan_reads (insn_info_t insn_info, bitmap gen, bitmap kill)
2849 read_info_t read_info = insn_info->read_rec;
2850 int i;
2851 group_info *group;
2853 /* If this insn reads the frame, kill all the frame related stores. */
2854 if (insn_info->frame_read)
2856 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2857 if (group->process_globally && group->frame_related)
2859 if (kill)
2860 bitmap_ior_into (kill, group->group_kill);
2861 bitmap_and_compl_into (gen, group->group_kill);
2864 if (insn_info->non_frame_wild_read)
2866 /* Kill all non-frame related stores. Kill all stores of variables that
2867 escape. */
2868 if (kill)
2869 bitmap_ior_into (kill, kill_on_calls);
2870 bitmap_and_compl_into (gen, kill_on_calls);
2871 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2872 if (group->process_globally && !group->frame_related)
2874 if (kill)
2875 bitmap_ior_into (kill, group->group_kill);
2876 bitmap_and_compl_into (gen, group->group_kill);
2879 while (read_info)
2881 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2883 if (group->process_globally)
2885 if (i == read_info->group_id)
2887 if (!known_size_p (read_info->width))
2889 /* Handle block mode reads. */
2890 if (kill)
2891 bitmap_ior_into (kill, group->group_kill);
2892 bitmap_and_compl_into (gen, group->group_kill);
2894 else
2896 /* The groups are the same, just process the
2897 offsets. */
2898 HOST_WIDE_INT j;
2899 HOST_WIDE_INT end = read_info->offset + read_info->width;
2900 for (j = read_info->offset; j < end; j++)
2902 int index = get_bitmap_index (group, j);
2903 if (index != 0)
2905 if (kill)
2906 bitmap_set_bit (kill, index);
2907 bitmap_clear_bit (gen, index);
2912 else
2914 /* The groups are different, if the alias sets
2915 conflict, clear the entire group. We only need
2916 to apply this test if the read_info is a cselib
2917 read. Anything with a constant base cannot alias
2918 something else with a different constant
2919 base. */
2920 if ((read_info->group_id < 0)
2921 && canon_true_dependence (group->base_mem,
2922 GET_MODE (group->base_mem),
2923 group->canon_base_addr,
2924 read_info->mem, NULL_RTX))
2926 if (kill)
2927 bitmap_ior_into (kill, group->group_kill);
2928 bitmap_and_compl_into (gen, group->group_kill);
2934 read_info = read_info->next;
2939 /* Return the insn in BB_INFO before the first wild read or if there
2940 are no wild reads in the block, return the last insn. */
2942 static insn_info_t
2943 find_insn_before_first_wild_read (bb_info_t bb_info)
2945 insn_info_t insn_info = bb_info->last_insn;
2946 insn_info_t last_wild_read = NULL;
2948 while (insn_info)
2950 if (insn_info->wild_read)
2952 last_wild_read = insn_info->prev_insn;
2953 /* Block starts with wild read. */
2954 if (!last_wild_read)
2955 return NULL;
2958 insn_info = insn_info->prev_insn;
2961 if (last_wild_read)
2962 return last_wild_read;
2963 else
2964 return bb_info->last_insn;
2968 /* Scan the insns in BB_INFO starting at PTR and going to the top of
2969 the block in order to build the gen and kill sets for the block.
2970 We start at ptr which may be the last insn in the block or may be
2971 the first insn with a wild read. In the latter case we are able to
2972 skip the rest of the block because it just does not matter:
2973 anything that happens is hidden by the wild read. */
2975 static void
2976 dse_step3_scan (basic_block bb)
2978 bb_info_t bb_info = bb_table[bb->index];
2979 insn_info_t insn_info;
2981 insn_info = find_insn_before_first_wild_read (bb_info);
2983 /* In the spill case or in the no_spill case if there is no wild
2984 read in the block, we will need a kill set. */
2985 if (insn_info == bb_info->last_insn)
2987 if (bb_info->kill)
2988 bitmap_clear (bb_info->kill);
2989 else
2990 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
2992 else
2993 if (bb_info->kill)
2994 BITMAP_FREE (bb_info->kill);
2996 while (insn_info)
2998 /* There may have been code deleted by the dce pass run before
2999 this phase. */
3000 if (insn_info->insn && INSN_P (insn_info->insn))
3002 scan_stores (insn_info->store_rec, bb_info->gen, bb_info->kill);
3003 scan_reads (insn_info, bb_info->gen, bb_info->kill);
3006 insn_info = insn_info->prev_insn;
3011 /* Set the gen set of the exit block, and also any block with no
3012 successors that does not have a wild read. */
3014 static void
3015 dse_step3_exit_block_scan (bb_info_t bb_info)
3017 /* The gen set is all 0's for the exit block except for the
3018 frame_pointer_group. */
3020 if (stores_off_frame_dead_at_return)
3022 unsigned int i;
3023 group_info *group;
3025 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3027 if (group->process_globally && group->frame_related)
3028 bitmap_ior_into (bb_info->gen, group->group_kill);
3034 /* Find all of the blocks that are not backwards reachable from the
3035 exit block or any block with no successors (BB). These are the
3036 infinite loops or infinite self loops. These blocks will still
3037 have their bits set in UNREACHABLE_BLOCKS. */
3039 static void
3040 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3042 edge e;
3043 edge_iterator ei;
3045 if (bitmap_bit_p (unreachable_blocks, bb->index))
3047 bitmap_clear_bit (unreachable_blocks, bb->index);
3048 FOR_EACH_EDGE (e, ei, bb->preds)
3050 mark_reachable_blocks (unreachable_blocks, e->src);
3055 /* Build the transfer functions for the function. */
3057 static void
3058 dse_step3 ()
3060 basic_block bb;
3061 sbitmap_iterator sbi;
3062 bitmap all_ones = NULL;
3063 unsigned int i;
3065 auto_sbitmap unreachable_blocks (last_basic_block_for_fn (cfun));
3066 bitmap_ones (unreachable_blocks);
3068 FOR_ALL_BB_FN (bb, cfun)
3070 bb_info_t bb_info = bb_table[bb->index];
3071 if (bb_info->gen)
3072 bitmap_clear (bb_info->gen);
3073 else
3074 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3076 if (bb->index == ENTRY_BLOCK)
3078 else if (bb->index == EXIT_BLOCK)
3079 dse_step3_exit_block_scan (bb_info);
3080 else
3081 dse_step3_scan (bb);
3082 if (EDGE_COUNT (bb->succs) == 0)
3083 mark_reachable_blocks (unreachable_blocks, bb);
3085 /* If this is the second time dataflow is run, delete the old
3086 sets. */
3087 if (bb_info->in)
3088 BITMAP_FREE (bb_info->in);
3089 if (bb_info->out)
3090 BITMAP_FREE (bb_info->out);
3093 /* For any block in an infinite loop, we must initialize the out set
3094 to all ones. This could be expensive, but almost never occurs in
3095 practice. However, it is common in regression tests. */
3096 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3098 if (bitmap_bit_p (all_blocks, i))
3100 bb_info_t bb_info = bb_table[i];
3101 if (!all_ones)
3103 unsigned int j;
3104 group_info *group;
3106 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3107 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3108 bitmap_ior_into (all_ones, group->group_kill);
3110 if (!bb_info->out)
3112 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3113 bitmap_copy (bb_info->out, all_ones);
3118 if (all_ones)
3119 BITMAP_FREE (all_ones);
3124 /*----------------------------------------------------------------------------
3125 Fourth step.
3127 Solve the bitvector equations.
3128 ----------------------------------------------------------------------------*/
3131 /* Confluence function for blocks with no successors. Create an out
3132 set from the gen set of the exit block. This block logically has
3133 the exit block as a successor. */
3137 static void
3138 dse_confluence_0 (basic_block bb)
3140 bb_info_t bb_info = bb_table[bb->index];
3142 if (bb->index == EXIT_BLOCK)
3143 return;
3145 if (!bb_info->out)
3147 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3148 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3152 /* Propagate the information from the in set of the dest of E to the
3153 out set of the src of E. If the various in or out sets are not
3154 there, that means they are all ones. */
3156 static bool
3157 dse_confluence_n (edge e)
3159 bb_info_t src_info = bb_table[e->src->index];
3160 bb_info_t dest_info = bb_table[e->dest->index];
3162 if (dest_info->in)
3164 if (src_info->out)
3165 bitmap_and_into (src_info->out, dest_info->in);
3166 else
3168 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3169 bitmap_copy (src_info->out, dest_info->in);
3172 return true;
3176 /* Propagate the info from the out to the in set of BB_INDEX's basic
3177 block. There are three cases:
3179 1) The block has no kill set. In this case the kill set is all
3180 ones. It does not matter what the out set of the block is, none of
3181 the info can reach the top. The only thing that reaches the top is
3182 the gen set and we just copy the set.
3184 2) There is a kill set but no out set and bb has successors. In
3185 this case we just return. Eventually an out set will be created and
3186 it is better to wait than to create a set of ones.
3188 3) There is both a kill and out set. We apply the obvious transfer
3189 function.
3192 static bool
3193 dse_transfer_function (int bb_index)
3195 bb_info_t bb_info = bb_table[bb_index];
3197 if (bb_info->kill)
3199 if (bb_info->out)
3201 /* Case 3 above. */
3202 if (bb_info->in)
3203 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3204 bb_info->out, bb_info->kill);
3205 else
3207 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3208 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3209 bb_info->out, bb_info->kill);
3210 return true;
3213 else
3214 /* Case 2 above. */
3215 return false;
3217 else
3219 /* Case 1 above. If there is already an in set, nothing
3220 happens. */
3221 if (bb_info->in)
3222 return false;
3223 else
3225 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3226 bitmap_copy (bb_info->in, bb_info->gen);
3227 return true;
3232 /* Solve the dataflow equations. */
3234 static void
3235 dse_step4 (void)
3237 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3238 dse_confluence_n, dse_transfer_function,
3239 all_blocks, df_get_postorder (DF_BACKWARD),
3240 df_get_n_blocks (DF_BACKWARD));
3241 if (dump_file && (dump_flags & TDF_DETAILS))
3243 basic_block bb;
3245 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3246 FOR_ALL_BB_FN (bb, cfun)
3248 bb_info_t bb_info = bb_table[bb->index];
3250 df_print_bb_index (bb, dump_file);
3251 if (bb_info->in)
3252 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3253 else
3254 fprintf (dump_file, " in: *MISSING*\n");
3255 if (bb_info->gen)
3256 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3257 else
3258 fprintf (dump_file, " gen: *MISSING*\n");
3259 if (bb_info->kill)
3260 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3261 else
3262 fprintf (dump_file, " kill: *MISSING*\n");
3263 if (bb_info->out)
3264 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3265 else
3266 fprintf (dump_file, " out: *MISSING*\n\n");
3273 /*----------------------------------------------------------------------------
3274 Fifth step.
3276 Delete the stores that can only be deleted using the global information.
3277 ----------------------------------------------------------------------------*/
3280 static void
3281 dse_step5 (void)
3283 basic_block bb;
3284 FOR_EACH_BB_FN (bb, cfun)
3286 bb_info_t bb_info = bb_table[bb->index];
3287 insn_info_t insn_info = bb_info->last_insn;
3288 bitmap v = bb_info->out;
3290 while (insn_info)
3292 bool deleted = false;
3293 if (dump_file && insn_info->insn)
3295 fprintf (dump_file, "starting to process insn %d\n",
3296 INSN_UID (insn_info->insn));
3297 bitmap_print (dump_file, v, " v: ", "\n");
3300 /* There may have been code deleted by the dce pass run before
3301 this phase. */
3302 if (insn_info->insn
3303 && INSN_P (insn_info->insn)
3304 && (!insn_info->cannot_delete)
3305 && (!bitmap_empty_p (v)))
3307 store_info *store_info = insn_info->store_rec;
3309 /* Try to delete the current insn. */
3310 deleted = true;
3312 /* Skip the clobbers. */
3313 while (!store_info->is_set)
3314 store_info = store_info->next;
3316 HOST_WIDE_INT i;
3317 group_info *group_info = rtx_group_vec[store_info->group_id];
3319 HOST_WIDE_INT end = store_info->offset + store_info->width;
3320 for (i = store_info->offset; i < end; i++)
3322 int index = get_bitmap_index (group_info, i);
3324 if (dump_file && (dump_flags & TDF_DETAILS))
3325 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3326 if (index == 0 || !bitmap_bit_p (v, index))
3328 if (dump_file && (dump_flags & TDF_DETAILS))
3329 fprintf (dump_file, "failing at i = %d\n", (int)i);
3330 deleted = false;
3331 break;
3334 if (deleted)
3336 if (dbg_cnt (dse)
3337 && check_for_inc_dec_1 (insn_info))
3339 delete_insn (insn_info->insn);
3340 insn_info->insn = NULL;
3341 globally_deleted++;
3345 /* We do want to process the local info if the insn was
3346 deleted. For instance, if the insn did a wild read, we
3347 no longer need to trash the info. */
3348 if (insn_info->insn
3349 && INSN_P (insn_info->insn)
3350 && (!deleted))
3352 scan_stores (insn_info->store_rec, v, NULL);
3353 if (insn_info->wild_read)
3355 if (dump_file && (dump_flags & TDF_DETAILS))
3356 fprintf (dump_file, "wild read\n");
3357 bitmap_clear (v);
3359 else if (insn_info->read_rec
3360 || insn_info->non_frame_wild_read
3361 || insn_info->frame_read)
3363 if (dump_file && (dump_flags & TDF_DETAILS))
3365 if (!insn_info->non_frame_wild_read
3366 && !insn_info->frame_read)
3367 fprintf (dump_file, "regular read\n");
3368 if (insn_info->non_frame_wild_read)
3369 fprintf (dump_file, "non-frame wild read\n");
3370 if (insn_info->frame_read)
3371 fprintf (dump_file, "frame read\n");
3373 scan_reads (insn_info, v, NULL);
3377 insn_info = insn_info->prev_insn;
3384 /*----------------------------------------------------------------------------
3385 Sixth step.
3387 Delete stores made redundant by earlier stores (which store the same
3388 value) that couldn't be eliminated.
3389 ----------------------------------------------------------------------------*/
3391 static void
3392 dse_step6 (void)
3394 basic_block bb;
3396 FOR_ALL_BB_FN (bb, cfun)
3398 bb_info_t bb_info = bb_table[bb->index];
3399 insn_info_t insn_info = bb_info->last_insn;
3401 while (insn_info)
3403 /* There may have been code deleted by the dce pass run before
3404 this phase. */
3405 if (insn_info->insn
3406 && INSN_P (insn_info->insn)
3407 && !insn_info->cannot_delete)
3409 store_info *s_info = insn_info->store_rec;
3411 while (s_info && !s_info->is_set)
3412 s_info = s_info->next;
3413 if (s_info
3414 && s_info->redundant_reason
3415 && s_info->redundant_reason->insn
3416 && INSN_P (s_info->redundant_reason->insn))
3418 rtx_insn *rinsn = s_info->redundant_reason->insn;
3419 if (dump_file && (dump_flags & TDF_DETAILS))
3420 fprintf (dump_file, "Locally deleting insn %d "
3421 "because insn %d stores the "
3422 "same value and couldn't be "
3423 "eliminated\n",
3424 INSN_UID (insn_info->insn),
3425 INSN_UID (rinsn));
3426 delete_dead_store_insn (insn_info);
3429 insn_info = insn_info->prev_insn;
3434 /*----------------------------------------------------------------------------
3435 Seventh step.
3437 Destroy everything left standing.
3438 ----------------------------------------------------------------------------*/
3440 static void
3441 dse_step7 (void)
3443 bitmap_obstack_release (&dse_bitmap_obstack);
3444 obstack_free (&dse_obstack, NULL);
3446 end_alias_analysis ();
3447 free (bb_table);
3448 delete rtx_group_table;
3449 rtx_group_table = NULL;
3450 rtx_group_vec.release ();
3451 BITMAP_FREE (all_blocks);
3452 BITMAP_FREE (scratch);
3454 rtx_store_info_pool.release ();
3455 read_info_type_pool.release ();
3456 insn_info_type_pool.release ();
3457 dse_bb_info_type_pool.release ();
3458 group_info_pool.release ();
3459 deferred_change_pool.release ();
3463 /* -------------------------------------------------------------------------
3465 ------------------------------------------------------------------------- */
3467 /* Callback for running pass_rtl_dse. */
3469 static unsigned int
3470 rest_of_handle_dse (void)
3472 df_set_flags (DF_DEFER_INSN_RESCAN);
3474 /* Need the notes since we must track live hardregs in the forwards
3475 direction. */
3476 df_note_add_problem ();
3477 df_analyze ();
3479 dse_step0 ();
3480 dse_step1 ();
3481 dse_step2_init ();
3482 if (dse_step2 ())
3484 df_set_flags (DF_LR_RUN_DCE);
3485 df_analyze ();
3486 if (dump_file && (dump_flags & TDF_DETAILS))
3487 fprintf (dump_file, "doing global processing\n");
3488 dse_step3 ();
3489 dse_step4 ();
3490 dse_step5 ();
3493 dse_step6 ();
3494 dse_step7 ();
3496 if (dump_file)
3497 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d\n",
3498 locally_deleted, globally_deleted);
3500 /* DSE can eliminate potentially-trapping MEMs.
3501 Remove any EH edges associated with them. */
3502 if ((locally_deleted || globally_deleted)
3503 && cfun->can_throw_non_call_exceptions
3504 && purge_all_dead_edges ())
3505 cleanup_cfg (0);
3507 return 0;
3510 namespace {
3512 const pass_data pass_data_rtl_dse1 =
3514 RTL_PASS, /* type */
3515 "dse1", /* name */
3516 OPTGROUP_NONE, /* optinfo_flags */
3517 TV_DSE1, /* tv_id */
3518 0, /* properties_required */
3519 0, /* properties_provided */
3520 0, /* properties_destroyed */
3521 0, /* todo_flags_start */
3522 TODO_df_finish, /* todo_flags_finish */
3525 class pass_rtl_dse1 : public rtl_opt_pass
3527 public:
3528 pass_rtl_dse1 (gcc::context *ctxt)
3529 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3532 /* opt_pass methods: */
3533 virtual bool gate (function *)
3535 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3538 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3540 }; // class pass_rtl_dse1
3542 } // anon namespace
3544 rtl_opt_pass *
3545 make_pass_rtl_dse1 (gcc::context *ctxt)
3547 return new pass_rtl_dse1 (ctxt);
3550 namespace {
3552 const pass_data pass_data_rtl_dse2 =
3554 RTL_PASS, /* type */
3555 "dse2", /* name */
3556 OPTGROUP_NONE, /* optinfo_flags */
3557 TV_DSE2, /* tv_id */
3558 0, /* properties_required */
3559 0, /* properties_provided */
3560 0, /* properties_destroyed */
3561 0, /* todo_flags_start */
3562 TODO_df_finish, /* todo_flags_finish */
3565 class pass_rtl_dse2 : public rtl_opt_pass
3567 public:
3568 pass_rtl_dse2 (gcc::context *ctxt)
3569 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3572 /* opt_pass methods: */
3573 virtual bool gate (function *)
3575 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3578 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3580 }; // class pass_rtl_dse2
3582 } // anon namespace
3584 rtl_opt_pass *
3585 make_pass_rtl_dse2 (gcc::context *ctxt)
3587 return new pass_rtl_dse2 (ctxt);