Add a gen_int_shift_amount helper function
[official-gcc.git] / gcc / dse.c
blobc14eace483e34f70ca5b793c66b2dde2b4ed491f
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 rtx shift_rtx = gen_int_shift_amount (new_mode, shift);
1646 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1647 ret, shift_rtx);
1648 if (ret && CONSTANT_P (ret))
1650 byte = subreg_lowpart_offset (read_mode, new_mode);
1651 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1652 if (ret && CONSTANT_P (ret)
1653 && (set_src_cost (ret, read_mode, speed)
1654 <= COSTS_N_INSNS (1)))
1655 return ret;
1660 if (require_cst)
1661 return NULL_RTX;
1663 /* Try a wider mode if truncating the store mode to NEW_MODE
1664 requires a real instruction. */
1665 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1666 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1667 continue;
1669 /* Also try a wider mode if the necessary punning is either not
1670 desirable or not possible. */
1671 if (!CONSTANT_P (store_info->rhs)
1672 && !targetm.modes_tieable_p (new_mode, store_mode))
1673 continue;
1675 new_reg = gen_reg_rtx (new_mode);
1677 start_sequence ();
1679 /* In theory we could also check for an ashr. Ian Taylor knows
1680 of one dsp where the cost of these two was not the same. But
1681 this really is a rare case anyway. */
1682 target = expand_binop (new_mode, lshr_optab, new_reg,
1683 gen_int_shift_amount (new_mode, shift),
1684 new_reg, 1, OPTAB_DIRECT);
1686 shift_seq = get_insns ();
1687 end_sequence ();
1689 if (target != new_reg || shift_seq == NULL)
1690 continue;
1692 cost = 0;
1693 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1694 if (INSN_P (insn))
1695 cost += insn_cost (insn, speed);
1697 /* The computation up to here is essentially independent
1698 of the arguments and could be precomputed. It may
1699 not be worth doing so. We could precompute if
1700 worthwhile or at least cache the results. The result
1701 technically depends on both SHIFT and ACCESS_SIZE,
1702 but in practice the answer will depend only on ACCESS_SIZE. */
1704 if (cost > COSTS_N_INSNS (1))
1705 continue;
1707 new_lhs = extract_low_bits (new_mode, store_mode,
1708 copy_rtx (store_info->rhs));
1709 if (new_lhs == NULL_RTX)
1710 continue;
1712 /* We found an acceptable shift. Generate a move to
1713 take the value from the store and put it into the
1714 shift pseudo, then shift it, then generate another
1715 move to put in into the target of the read. */
1716 emit_move_insn (new_reg, new_lhs);
1717 emit_insn (shift_seq);
1718 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1719 break;
1722 return read_reg;
1726 /* Call back for note_stores to find the hard regs set or clobbered by
1727 insn. Data is a bitmap of the hardregs set so far. */
1729 static void
1730 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1732 bitmap regs_set = (bitmap) data;
1734 if (REG_P (x)
1735 && HARD_REGISTER_P (x))
1736 bitmap_set_range (regs_set, REGNO (x), REG_NREGS (x));
1739 /* Helper function for replace_read and record_store.
1740 Attempt to return a value of mode READ_MODE stored in STORE_INFO,
1741 consisting of READ_WIDTH bytes starting from READ_OFFSET. Return NULL
1742 if not successful. If REQUIRE_CST is true, return always constant. */
1744 static rtx
1745 get_stored_val (store_info *store_info, machine_mode read_mode,
1746 HOST_WIDE_INT read_offset, HOST_WIDE_INT read_width,
1747 basic_block bb, bool require_cst)
1749 machine_mode store_mode = GET_MODE (store_info->mem);
1750 HOST_WIDE_INT gap;
1751 rtx read_reg;
1753 /* To get here the read is within the boundaries of the write so
1754 shift will never be negative. Start out with the shift being in
1755 bytes. */
1756 if (store_mode == BLKmode)
1757 gap = 0;
1758 else if (BYTES_BIG_ENDIAN)
1759 gap = ((store_info->offset + store_info->width)
1760 - (read_offset + read_width));
1761 else
1762 gap = read_offset - store_info->offset;
1764 if (gap != 0)
1766 HOST_WIDE_INT shift = gap * BITS_PER_UNIT;
1767 HOST_WIDE_INT access_size = GET_MODE_SIZE (read_mode) + gap;
1768 read_reg = find_shift_sequence (access_size, store_info, read_mode,
1769 shift, optimize_bb_for_speed_p (bb),
1770 require_cst);
1772 else if (store_mode == BLKmode)
1774 /* The store is a memset (addr, const_val, const_size). */
1775 gcc_assert (CONST_INT_P (store_info->rhs));
1776 scalar_int_mode int_store_mode;
1777 if (!int_mode_for_mode (read_mode).exists (&int_store_mode))
1778 read_reg = NULL_RTX;
1779 else if (store_info->rhs == const0_rtx)
1780 read_reg = extract_low_bits (read_mode, int_store_mode, const0_rtx);
1781 else if (GET_MODE_BITSIZE (int_store_mode) > HOST_BITS_PER_WIDE_INT
1782 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1783 read_reg = NULL_RTX;
1784 else
1786 unsigned HOST_WIDE_INT c
1787 = INTVAL (store_info->rhs)
1788 & ((HOST_WIDE_INT_1 << BITS_PER_UNIT) - 1);
1789 int shift = BITS_PER_UNIT;
1790 while (shift < HOST_BITS_PER_WIDE_INT)
1792 c |= (c << shift);
1793 shift <<= 1;
1795 read_reg = gen_int_mode (c, int_store_mode);
1796 read_reg = extract_low_bits (read_mode, int_store_mode, read_reg);
1799 else if (store_info->const_rhs
1800 && (require_cst
1801 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1802 read_reg = extract_low_bits (read_mode, store_mode,
1803 copy_rtx (store_info->const_rhs));
1804 else
1805 read_reg = extract_low_bits (read_mode, store_mode,
1806 copy_rtx (store_info->rhs));
1807 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1808 read_reg = NULL_RTX;
1809 return read_reg;
1812 /* Take a sequence of:
1813 A <- r1
1815 ... <- A
1817 and change it into
1818 r2 <- r1
1819 A <- r1
1821 ... <- r2
1825 r3 <- extract (r1)
1826 r3 <- r3 >> shift
1827 r2 <- extract (r3)
1828 ... <- r2
1832 r2 <- extract (r1)
1833 ... <- r2
1835 Depending on the alignment and the mode of the store and
1836 subsequent load.
1839 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1840 and READ_INSN are for the read. Return true if the replacement
1841 went ok. */
1843 static bool
1844 replace_read (store_info *store_info, insn_info_t store_insn,
1845 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1846 bitmap regs_live)
1848 machine_mode store_mode = GET_MODE (store_info->mem);
1849 machine_mode read_mode = GET_MODE (read_info->mem);
1850 rtx_insn *insns, *this_insn;
1851 rtx read_reg;
1852 basic_block bb;
1854 if (!dbg_cnt (dse))
1855 return false;
1857 /* Create a sequence of instructions to set up the read register.
1858 This sequence goes immediately before the store and its result
1859 is read by the load.
1861 We need to keep this in perspective. We are replacing a read
1862 with a sequence of insns, but the read will almost certainly be
1863 in cache, so it is not going to be an expensive one. Thus, we
1864 are not willing to do a multi insn shift or worse a subroutine
1865 call to get rid of the read. */
1866 if (dump_file && (dump_flags & TDF_DETAILS))
1867 fprintf (dump_file, "trying to replace %smode load in insn %d"
1868 " from %smode store in insn %d\n",
1869 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1870 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1871 start_sequence ();
1872 bb = BLOCK_FOR_INSN (read_insn->insn);
1873 read_reg = get_stored_val (store_info,
1874 read_mode, read_info->offset, read_info->width,
1875 bb, false);
1876 if (read_reg == NULL_RTX)
1878 end_sequence ();
1879 if (dump_file && (dump_flags & TDF_DETAILS))
1880 fprintf (dump_file, " -- could not extract bits of stored value\n");
1881 return false;
1883 /* Force the value into a new register so that it won't be clobbered
1884 between the store and the load. */
1885 read_reg = copy_to_mode_reg (read_mode, read_reg);
1886 insns = get_insns ();
1887 end_sequence ();
1889 if (insns != NULL_RTX)
1891 /* Now we have to scan the set of new instructions to see if the
1892 sequence contains and sets of hardregs that happened to be
1893 live at this point. For instance, this can happen if one of
1894 the insns sets the CC and the CC happened to be live at that
1895 point. This does occasionally happen, see PR 37922. */
1896 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
1898 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
1899 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
1901 bitmap_and_into (regs_set, regs_live);
1902 if (!bitmap_empty_p (regs_set))
1904 if (dump_file && (dump_flags & TDF_DETAILS))
1906 fprintf (dump_file,
1907 "abandoning replacement because sequence clobbers live hardregs:");
1908 df_print_regset (dump_file, regs_set);
1911 BITMAP_FREE (regs_set);
1912 return false;
1914 BITMAP_FREE (regs_set);
1917 if (validate_change (read_insn->insn, loc, read_reg, 0))
1919 deferred_change *change = deferred_change_pool.allocate ();
1921 /* Insert this right before the store insn where it will be safe
1922 from later insns that might change it before the read. */
1923 emit_insn_before (insns, store_insn->insn);
1925 /* And now for the kludge part: cselib croaks if you just
1926 return at this point. There are two reasons for this:
1928 1) Cselib has an idea of how many pseudos there are and
1929 that does not include the new ones we just added.
1931 2) Cselib does not know about the move insn we added
1932 above the store_info, and there is no way to tell it
1933 about it, because it has "moved on".
1935 Problem (1) is fixable with a certain amount of engineering.
1936 Problem (2) is requires starting the bb from scratch. This
1937 could be expensive.
1939 So we are just going to have to lie. The move/extraction
1940 insns are not really an issue, cselib did not see them. But
1941 the use of the new pseudo read_insn is a real problem because
1942 cselib has not scanned this insn. The way that we solve this
1943 problem is that we are just going to put the mem back for now
1944 and when we are finished with the block, we undo this. We
1945 keep a table of mems to get rid of. At the end of the basic
1946 block we can put them back. */
1948 *loc = read_info->mem;
1949 change->next = deferred_change_list;
1950 deferred_change_list = change;
1951 change->loc = loc;
1952 change->reg = read_reg;
1954 /* Get rid of the read_info, from the point of view of the
1955 rest of dse, play like this read never happened. */
1956 read_insn->read_rec = read_info->next;
1957 read_info_type_pool.remove (read_info);
1958 if (dump_file && (dump_flags & TDF_DETAILS))
1960 fprintf (dump_file, " -- replaced the loaded MEM with ");
1961 print_simple_rtl (dump_file, read_reg);
1962 fprintf (dump_file, "\n");
1964 return true;
1966 else
1968 if (dump_file && (dump_flags & TDF_DETAILS))
1970 fprintf (dump_file, " -- replacing the loaded MEM with ");
1971 print_simple_rtl (dump_file, read_reg);
1972 fprintf (dump_file, " led to an invalid instruction\n");
1974 return false;
1978 /* Check the address of MEM *LOC and kill any appropriate stores that may
1979 be active. */
1981 static void
1982 check_mem_read_rtx (rtx *loc, bb_info_t bb_info)
1984 rtx mem = *loc, mem_addr;
1985 insn_info_t insn_info;
1986 HOST_WIDE_INT offset = 0;
1987 HOST_WIDE_INT width = 0;
1988 cselib_val *base = NULL;
1989 int group_id;
1990 read_info_t read_info;
1992 insn_info = bb_info->last_insn;
1994 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
1995 || (MEM_VOLATILE_P (mem)))
1997 if (dump_file && (dump_flags & TDF_DETAILS))
1998 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
1999 add_wild_read (bb_info);
2000 insn_info->cannot_delete = true;
2001 return;
2004 /* If it is reading readonly mem, then there can be no conflict with
2005 another write. */
2006 if (MEM_READONLY_P (mem))
2007 return;
2009 if (!canon_address (mem, &group_id, &offset, &base))
2011 if (dump_file && (dump_flags & TDF_DETAILS))
2012 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2013 add_wild_read (bb_info);
2014 return;
2017 if (GET_MODE (mem) == BLKmode)
2018 width = -1;
2019 else
2020 width = GET_MODE_SIZE (GET_MODE (mem));
2022 if (width == -1
2023 ? offset == HOST_WIDE_INT_MIN
2024 : offset > HOST_WIDE_INT_MAX - width)
2026 if (dump_file && (dump_flags & TDF_DETAILS))
2027 fprintf (dump_file, " adding wild read, due to overflow.\n");
2028 add_wild_read (bb_info);
2029 return;
2032 read_info = read_info_type_pool.allocate ();
2033 read_info->group_id = group_id;
2034 read_info->mem = mem;
2035 read_info->offset = offset;
2036 read_info->width = width;
2037 read_info->next = insn_info->read_rec;
2038 insn_info->read_rec = read_info;
2039 if (group_id < 0)
2040 mem_addr = base->val_rtx;
2041 else
2043 group_info *group = rtx_group_vec[group_id];
2044 mem_addr = group->canon_base_addr;
2046 if (offset)
2047 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2049 if (group_id >= 0)
2051 /* This is the restricted case where the base is a constant or
2052 the frame pointer and offset is a constant. */
2053 insn_info_t i_ptr = active_local_stores;
2054 insn_info_t last = NULL;
2056 if (dump_file && (dump_flags & TDF_DETAILS))
2058 if (width == -1)
2059 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2060 group_id);
2061 else
2063 fprintf (dump_file, " processing const load gid=%d", group_id);
2064 print_range (dump_file, offset, width);
2065 fprintf (dump_file, "\n");
2069 while (i_ptr)
2071 bool remove = false;
2072 store_info *store_info = i_ptr->store_rec;
2074 /* Skip the clobbers. */
2075 while (!store_info->is_set)
2076 store_info = store_info->next;
2078 /* There are three cases here. */
2079 if (store_info->group_id < 0)
2080 /* We have a cselib store followed by a read from a
2081 const base. */
2082 remove
2083 = canon_true_dependence (store_info->mem,
2084 GET_MODE (store_info->mem),
2085 store_info->mem_addr,
2086 mem, mem_addr);
2088 else if (group_id == store_info->group_id)
2090 /* This is a block mode load. We may get lucky and
2091 canon_true_dependence may save the day. */
2092 if (width == -1)
2093 remove
2094 = canon_true_dependence (store_info->mem,
2095 GET_MODE (store_info->mem),
2096 store_info->mem_addr,
2097 mem, mem_addr);
2099 /* If this read is just reading back something that we just
2100 stored, rewrite the read. */
2101 else
2103 if (store_info->rhs
2104 && known_subrange_p (offset, width, store_info->offset,
2105 store_info->width)
2106 && all_positions_needed_p (store_info,
2107 offset - store_info->offset,
2108 width)
2109 && replace_read (store_info, i_ptr, read_info,
2110 insn_info, loc, bb_info->regs_live))
2111 return;
2113 /* The bases are the same, just see if the offsets
2114 could overlap. */
2115 if (ranges_maybe_overlap_p (offset, width,
2116 store_info->offset,
2117 store_info->width))
2118 remove = true;
2122 /* else
2123 The else case that is missing here is that the
2124 bases are constant but different. There is nothing
2125 to do here because there is no overlap. */
2127 if (remove)
2129 if (dump_file && (dump_flags & TDF_DETAILS))
2130 dump_insn_info ("removing from active", i_ptr);
2132 active_local_stores_len--;
2133 if (last)
2134 last->next_local_store = i_ptr->next_local_store;
2135 else
2136 active_local_stores = i_ptr->next_local_store;
2138 else
2139 last = i_ptr;
2140 i_ptr = i_ptr->next_local_store;
2143 else
2145 insn_info_t i_ptr = active_local_stores;
2146 insn_info_t last = NULL;
2147 if (dump_file && (dump_flags & TDF_DETAILS))
2149 fprintf (dump_file, " processing cselib load mem:");
2150 print_inline_rtx (dump_file, mem, 0);
2151 fprintf (dump_file, "\n");
2154 while (i_ptr)
2156 bool remove = false;
2157 store_info *store_info = i_ptr->store_rec;
2159 if (dump_file && (dump_flags & TDF_DETAILS))
2160 fprintf (dump_file, " processing cselib load against insn %d\n",
2161 INSN_UID (i_ptr->insn));
2163 /* Skip the clobbers. */
2164 while (!store_info->is_set)
2165 store_info = store_info->next;
2167 /* If this read is just reading back something that we just
2168 stored, rewrite the read. */
2169 if (store_info->rhs
2170 && store_info->group_id == -1
2171 && store_info->cse_base == base
2172 && known_subrange_p (offset, width, store_info->offset,
2173 store_info->width)
2174 && all_positions_needed_p (store_info,
2175 offset - store_info->offset, width)
2176 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2177 bb_info->regs_live))
2178 return;
2180 remove = canon_true_dependence (store_info->mem,
2181 GET_MODE (store_info->mem),
2182 store_info->mem_addr,
2183 mem, mem_addr);
2185 if (remove)
2187 if (dump_file && (dump_flags & TDF_DETAILS))
2188 dump_insn_info ("removing from active", i_ptr);
2190 active_local_stores_len--;
2191 if (last)
2192 last->next_local_store = i_ptr->next_local_store;
2193 else
2194 active_local_stores = i_ptr->next_local_store;
2196 else
2197 last = i_ptr;
2198 i_ptr = i_ptr->next_local_store;
2203 /* A note_uses callback in which DATA points the INSN_INFO for
2204 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2205 true for any part of *LOC. */
2207 static void
2208 check_mem_read_use (rtx *loc, void *data)
2210 subrtx_ptr_iterator::array_type array;
2211 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
2213 rtx *loc = *iter;
2214 if (MEM_P (*loc))
2215 check_mem_read_rtx (loc, (bb_info_t) data);
2220 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2221 So far it only handles arguments passed in registers. */
2223 static bool
2224 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2226 CUMULATIVE_ARGS args_so_far_v;
2227 cumulative_args_t args_so_far;
2228 tree arg;
2229 int idx;
2231 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2232 args_so_far = pack_cumulative_args (&args_so_far_v);
2234 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2235 for (idx = 0;
2236 arg != void_list_node && idx < nargs;
2237 arg = TREE_CHAIN (arg), idx++)
2239 scalar_int_mode mode;
2240 rtx reg, link, tmp;
2242 if (!is_int_mode (TYPE_MODE (TREE_VALUE (arg)), &mode))
2243 return false;
2245 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2246 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode)
2247 return false;
2249 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2250 link;
2251 link = XEXP (link, 1))
2252 if (GET_CODE (XEXP (link, 0)) == USE)
2254 scalar_int_mode arg_mode;
2255 args[idx] = XEXP (XEXP (link, 0), 0);
2256 if (REG_P (args[idx])
2257 && REGNO (args[idx]) == REGNO (reg)
2258 && (GET_MODE (args[idx]) == mode
2259 || (is_int_mode (GET_MODE (args[idx]), &arg_mode)
2260 && (GET_MODE_SIZE (arg_mode) <= UNITS_PER_WORD)
2261 && (GET_MODE_SIZE (arg_mode) > GET_MODE_SIZE (mode)))))
2262 break;
2264 if (!link)
2265 return false;
2267 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2268 if (GET_MODE (args[idx]) != mode)
2270 if (!tmp || !CONST_INT_P (tmp))
2271 return false;
2272 tmp = gen_int_mode (INTVAL (tmp), mode);
2274 if (tmp)
2275 args[idx] = tmp;
2277 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2279 if (arg != void_list_node || idx != nargs)
2280 return false;
2281 return true;
2284 /* Return a bitmap of the fixed registers contained in IN. */
2286 static bitmap
2287 copy_fixed_regs (const_bitmap in)
2289 bitmap ret;
2291 ret = ALLOC_REG_SET (NULL);
2292 bitmap_and (ret, in, fixed_reg_set_regset);
2293 return ret;
2296 /* Apply record_store to all candidate stores in INSN. Mark INSN
2297 if some part of it is not a candidate store and assigns to a
2298 non-register target. */
2300 static void
2301 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2303 rtx body;
2304 insn_info_type *insn_info = insn_info_type_pool.allocate ();
2305 int mems_found = 0;
2306 memset (insn_info, 0, sizeof (struct insn_info_type));
2308 if (dump_file && (dump_flags & TDF_DETAILS))
2309 fprintf (dump_file, "\n**scanning insn=%d\n",
2310 INSN_UID (insn));
2312 insn_info->prev_insn = bb_info->last_insn;
2313 insn_info->insn = insn;
2314 bb_info->last_insn = insn_info;
2316 if (DEBUG_INSN_P (insn))
2318 insn_info->cannot_delete = true;
2319 return;
2322 /* Look at all of the uses in the insn. */
2323 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2325 if (CALL_P (insn))
2327 bool const_call;
2328 rtx call, sym;
2329 tree memset_call = NULL_TREE;
2331 insn_info->cannot_delete = true;
2333 /* Const functions cannot do anything bad i.e. read memory,
2334 however, they can read their parameters which may have
2335 been pushed onto the stack.
2336 memset and bzero don't read memory either. */
2337 const_call = RTL_CONST_CALL_P (insn);
2338 if (!const_call
2339 && (call = get_call_rtx_from (insn))
2340 && (sym = XEXP (XEXP (call, 0), 0))
2341 && GET_CODE (sym) == SYMBOL_REF
2342 && SYMBOL_REF_DECL (sym)
2343 && TREE_CODE (SYMBOL_REF_DECL (sym)) == FUNCTION_DECL
2344 && DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (sym)) == BUILT_IN_NORMAL
2345 && DECL_FUNCTION_CODE (SYMBOL_REF_DECL (sym)) == BUILT_IN_MEMSET)
2346 memset_call = SYMBOL_REF_DECL (sym);
2348 if (const_call || memset_call)
2350 insn_info_t i_ptr = active_local_stores;
2351 insn_info_t last = NULL;
2353 if (dump_file && (dump_flags & TDF_DETAILS))
2354 fprintf (dump_file, "%s call %d\n",
2355 const_call ? "const" : "memset", INSN_UID (insn));
2357 /* See the head comment of the frame_read field. */
2358 if (reload_completed
2359 /* Tail calls are storing their arguments using
2360 arg pointer. If it is a frame pointer on the target,
2361 even before reload we need to kill frame pointer based
2362 stores. */
2363 || (SIBLING_CALL_P (insn)
2364 && HARD_FRAME_POINTER_IS_ARG_POINTER))
2365 insn_info->frame_read = true;
2367 /* Loop over the active stores and remove those which are
2368 killed by the const function call. */
2369 while (i_ptr)
2371 bool remove_store = false;
2373 /* The stack pointer based stores are always killed. */
2374 if (i_ptr->stack_pointer_based)
2375 remove_store = true;
2377 /* If the frame is read, the frame related stores are killed. */
2378 else if (insn_info->frame_read)
2380 store_info *store_info = i_ptr->store_rec;
2382 /* Skip the clobbers. */
2383 while (!store_info->is_set)
2384 store_info = store_info->next;
2386 if (store_info->group_id >= 0
2387 && rtx_group_vec[store_info->group_id]->frame_related)
2388 remove_store = true;
2391 if (remove_store)
2393 if (dump_file && (dump_flags & TDF_DETAILS))
2394 dump_insn_info ("removing from active", i_ptr);
2396 active_local_stores_len--;
2397 if (last)
2398 last->next_local_store = i_ptr->next_local_store;
2399 else
2400 active_local_stores = i_ptr->next_local_store;
2402 else
2403 last = i_ptr;
2405 i_ptr = i_ptr->next_local_store;
2408 if (memset_call)
2410 rtx args[3];
2411 if (get_call_args (insn, memset_call, args, 3)
2412 && CONST_INT_P (args[1])
2413 && CONST_INT_P (args[2])
2414 && INTVAL (args[2]) > 0)
2416 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2417 set_mem_size (mem, INTVAL (args[2]));
2418 body = gen_rtx_SET (mem, args[1]);
2419 mems_found += record_store (body, bb_info);
2420 if (dump_file && (dump_flags & TDF_DETAILS))
2421 fprintf (dump_file, "handling memset as BLKmode store\n");
2422 if (mems_found == 1)
2424 if (active_local_stores_len++
2425 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2427 active_local_stores_len = 1;
2428 active_local_stores = NULL;
2430 insn_info->fixed_regs_live
2431 = copy_fixed_regs (bb_info->regs_live);
2432 insn_info->next_local_store = active_local_stores;
2433 active_local_stores = insn_info;
2436 else
2437 clear_rhs_from_active_local_stores ();
2440 else if (SIBLING_CALL_P (insn) && reload_completed)
2441 /* Arguments for a sibling call that are pushed to memory are passed
2442 using the incoming argument pointer of the current function. After
2443 reload that might be (and likely is) frame pointer based. */
2444 add_wild_read (bb_info);
2445 else
2446 /* Every other call, including pure functions, may read any memory
2447 that is not relative to the frame. */
2448 add_non_frame_wild_read (bb_info);
2450 return;
2453 /* Assuming that there are sets in these insns, we cannot delete
2454 them. */
2455 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2456 || volatile_refs_p (PATTERN (insn))
2457 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2458 || (RTX_FRAME_RELATED_P (insn))
2459 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2460 insn_info->cannot_delete = true;
2462 body = PATTERN (insn);
2463 if (GET_CODE (body) == PARALLEL)
2465 int i;
2466 for (i = 0; i < XVECLEN (body, 0); i++)
2467 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2469 else
2470 mems_found += record_store (body, bb_info);
2472 if (dump_file && (dump_flags & TDF_DETAILS))
2473 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2474 mems_found, insn_info->cannot_delete ? "true" : "false");
2476 /* If we found some sets of mems, add it into the active_local_stores so
2477 that it can be locally deleted if found dead or used for
2478 replace_read and redundant constant store elimination. Otherwise mark
2479 it as cannot delete. This simplifies the processing later. */
2480 if (mems_found == 1)
2482 if (active_local_stores_len++
2483 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2485 active_local_stores_len = 1;
2486 active_local_stores = NULL;
2488 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2489 insn_info->next_local_store = active_local_stores;
2490 active_local_stores = insn_info;
2492 else
2493 insn_info->cannot_delete = true;
2497 /* Remove BASE from the set of active_local_stores. This is a
2498 callback from cselib that is used to get rid of the stores in
2499 active_local_stores. */
2501 static void
2502 remove_useless_values (cselib_val *base)
2504 insn_info_t insn_info = active_local_stores;
2505 insn_info_t last = NULL;
2507 while (insn_info)
2509 store_info *store_info = insn_info->store_rec;
2510 bool del = false;
2512 /* If ANY of the store_infos match the cselib group that is
2513 being deleted, then the insn can not be deleted. */
2514 while (store_info)
2516 if ((store_info->group_id == -1)
2517 && (store_info->cse_base == base))
2519 del = true;
2520 break;
2522 store_info = store_info->next;
2525 if (del)
2527 active_local_stores_len--;
2528 if (last)
2529 last->next_local_store = insn_info->next_local_store;
2530 else
2531 active_local_stores = insn_info->next_local_store;
2532 free_store_info (insn_info);
2534 else
2535 last = insn_info;
2537 insn_info = insn_info->next_local_store;
2542 /* Do all of step 1. */
2544 static void
2545 dse_step1 (void)
2547 basic_block bb;
2548 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2550 cselib_init (0);
2551 all_blocks = BITMAP_ALLOC (NULL);
2552 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2553 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2555 FOR_ALL_BB_FN (bb, cfun)
2557 insn_info_t ptr;
2558 bb_info_t bb_info = dse_bb_info_type_pool.allocate ();
2560 memset (bb_info, 0, sizeof (dse_bb_info_type));
2561 bitmap_set_bit (all_blocks, bb->index);
2562 bb_info->regs_live = regs_live;
2564 bitmap_copy (regs_live, DF_LR_IN (bb));
2565 df_simulate_initialize_forwards (bb, regs_live);
2567 bb_table[bb->index] = bb_info;
2568 cselib_discard_hook = remove_useless_values;
2570 if (bb->index >= NUM_FIXED_BLOCKS)
2572 rtx_insn *insn;
2574 active_local_stores = NULL;
2575 active_local_stores_len = 0;
2576 cselib_clear_table ();
2578 /* Scan the insns. */
2579 FOR_BB_INSNS (bb, insn)
2581 if (INSN_P (insn))
2582 scan_insn (bb_info, insn);
2583 cselib_process_insn (insn);
2584 if (INSN_P (insn))
2585 df_simulate_one_insn_forwards (bb, insn, regs_live);
2588 /* This is something of a hack, because the global algorithm
2589 is supposed to take care of the case where stores go dead
2590 at the end of the function. However, the global
2591 algorithm must take a more conservative view of block
2592 mode reads than the local alg does. So to get the case
2593 where you have a store to the frame followed by a non
2594 overlapping block more read, we look at the active local
2595 stores at the end of the function and delete all of the
2596 frame and spill based ones. */
2597 if (stores_off_frame_dead_at_return
2598 && (EDGE_COUNT (bb->succs) == 0
2599 || (single_succ_p (bb)
2600 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2601 && ! crtl->calls_eh_return)))
2603 insn_info_t i_ptr = active_local_stores;
2604 while (i_ptr)
2606 store_info *store_info = i_ptr->store_rec;
2608 /* Skip the clobbers. */
2609 while (!store_info->is_set)
2610 store_info = store_info->next;
2611 if (store_info->group_id >= 0)
2613 group_info *group = rtx_group_vec[store_info->group_id];
2614 if (group->frame_related && !i_ptr->cannot_delete)
2615 delete_dead_store_insn (i_ptr);
2618 i_ptr = i_ptr->next_local_store;
2622 /* Get rid of the loads that were discovered in
2623 replace_read. Cselib is finished with this block. */
2624 while (deferred_change_list)
2626 deferred_change *next = deferred_change_list->next;
2628 /* There is no reason to validate this change. That was
2629 done earlier. */
2630 *deferred_change_list->loc = deferred_change_list->reg;
2631 deferred_change_pool.remove (deferred_change_list);
2632 deferred_change_list = next;
2635 /* Get rid of all of the cselib based store_infos in this
2636 block and mark the containing insns as not being
2637 deletable. */
2638 ptr = bb_info->last_insn;
2639 while (ptr)
2641 if (ptr->contains_cselib_groups)
2643 store_info *s_info = ptr->store_rec;
2644 while (s_info && !s_info->is_set)
2645 s_info = s_info->next;
2646 if (s_info
2647 && s_info->redundant_reason
2648 && s_info->redundant_reason->insn
2649 && !ptr->cannot_delete)
2651 if (dump_file && (dump_flags & TDF_DETAILS))
2652 fprintf (dump_file, "Locally deleting insn %d "
2653 "because insn %d stores the "
2654 "same value and couldn't be "
2655 "eliminated\n",
2656 INSN_UID (ptr->insn),
2657 INSN_UID (s_info->redundant_reason->insn));
2658 delete_dead_store_insn (ptr);
2660 free_store_info (ptr);
2662 else
2664 store_info *s_info;
2666 /* Free at least positions_needed bitmaps. */
2667 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2668 if (s_info->is_large)
2670 BITMAP_FREE (s_info->positions_needed.large.bmap);
2671 s_info->is_large = false;
2674 ptr = ptr->prev_insn;
2677 cse_store_info_pool.release ();
2679 bb_info->regs_live = NULL;
2682 BITMAP_FREE (regs_live);
2683 cselib_finish ();
2684 rtx_group_table->empty ();
2688 /*----------------------------------------------------------------------------
2689 Second step.
2691 Assign each byte position in the stores that we are going to
2692 analyze globally to a position in the bitmaps. Returns true if
2693 there are any bit positions assigned.
2694 ----------------------------------------------------------------------------*/
2696 static void
2697 dse_step2_init (void)
2699 unsigned int i;
2700 group_info *group;
2702 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2704 /* For all non stack related bases, we only consider a store to
2705 be deletable if there are two or more stores for that
2706 position. This is because it takes one store to make the
2707 other store redundant. However, for the stores that are
2708 stack related, we consider them if there is only one store
2709 for the position. We do this because the stack related
2710 stores can be deleted if their is no read between them and
2711 the end of the function.
2713 To make this work in the current framework, we take the stack
2714 related bases add all of the bits from store1 into store2.
2715 This has the effect of making the eligible even if there is
2716 only one store. */
2718 if (stores_off_frame_dead_at_return && group->frame_related)
2720 bitmap_ior_into (group->store2_n, group->store1_n);
2721 bitmap_ior_into (group->store2_p, group->store1_p);
2722 if (dump_file && (dump_flags & TDF_DETAILS))
2723 fprintf (dump_file, "group %d is frame related ", i);
2726 group->offset_map_size_n++;
2727 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2728 group->offset_map_size_n);
2729 group->offset_map_size_p++;
2730 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2731 group->offset_map_size_p);
2732 group->process_globally = false;
2733 if (dump_file && (dump_flags & TDF_DETAILS))
2735 fprintf (dump_file, "group %d(%d+%d): ", i,
2736 (int)bitmap_count_bits (group->store2_n),
2737 (int)bitmap_count_bits (group->store2_p));
2738 bitmap_print (dump_file, group->store2_n, "n ", " ");
2739 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2745 /* Init the offset tables. */
2747 static bool
2748 dse_step2 (void)
2750 unsigned int i;
2751 group_info *group;
2752 /* Position 0 is unused because 0 is used in the maps to mean
2753 unused. */
2754 current_position = 1;
2755 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2757 bitmap_iterator bi;
2758 unsigned int j;
2760 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2761 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2762 bitmap_clear (group->group_kill);
2764 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2766 bitmap_set_bit (group->group_kill, current_position);
2767 if (bitmap_bit_p (group->escaped_n, j))
2768 bitmap_set_bit (kill_on_calls, current_position);
2769 group->offset_map_n[j] = current_position++;
2770 group->process_globally = true;
2772 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2774 bitmap_set_bit (group->group_kill, current_position);
2775 if (bitmap_bit_p (group->escaped_p, j))
2776 bitmap_set_bit (kill_on_calls, current_position);
2777 group->offset_map_p[j] = current_position++;
2778 group->process_globally = true;
2781 return current_position != 1;
2786 /*----------------------------------------------------------------------------
2787 Third step.
2789 Build the bit vectors for the transfer functions.
2790 ----------------------------------------------------------------------------*/
2793 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2794 there, return 0. */
2796 static int
2797 get_bitmap_index (group_info *group_info, HOST_WIDE_INT offset)
2799 if (offset < 0)
2801 HOST_WIDE_INT offset_p = -offset;
2802 if (offset_p >= group_info->offset_map_size_n)
2803 return 0;
2804 return group_info->offset_map_n[offset_p];
2806 else
2808 if (offset >= group_info->offset_map_size_p)
2809 return 0;
2810 return group_info->offset_map_p[offset];
2815 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2816 may be NULL. */
2818 static void
2819 scan_stores (store_info *store_info, bitmap gen, bitmap kill)
2821 while (store_info)
2823 HOST_WIDE_INT i;
2824 group_info *group_info
2825 = rtx_group_vec[store_info->group_id];
2826 if (group_info->process_globally)
2828 HOST_WIDE_INT end = store_info->offset + store_info->width;
2829 for (i = store_info->offset; i < end; i++)
2831 int index = get_bitmap_index (group_info, i);
2832 if (index != 0)
2834 bitmap_set_bit (gen, index);
2835 if (kill)
2836 bitmap_clear_bit (kill, index);
2840 store_info = store_info->next;
2845 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
2846 may be NULL. */
2848 static void
2849 scan_reads (insn_info_t insn_info, bitmap gen, bitmap kill)
2851 read_info_t read_info = insn_info->read_rec;
2852 int i;
2853 group_info *group;
2855 /* If this insn reads the frame, kill all the frame related stores. */
2856 if (insn_info->frame_read)
2858 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2859 if (group->process_globally && group->frame_related)
2861 if (kill)
2862 bitmap_ior_into (kill, group->group_kill);
2863 bitmap_and_compl_into (gen, group->group_kill);
2866 if (insn_info->non_frame_wild_read)
2868 /* Kill all non-frame related stores. Kill all stores of variables that
2869 escape. */
2870 if (kill)
2871 bitmap_ior_into (kill, kill_on_calls);
2872 bitmap_and_compl_into (gen, kill_on_calls);
2873 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2874 if (group->process_globally && !group->frame_related)
2876 if (kill)
2877 bitmap_ior_into (kill, group->group_kill);
2878 bitmap_and_compl_into (gen, group->group_kill);
2881 while (read_info)
2883 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2885 if (group->process_globally)
2887 if (i == read_info->group_id)
2889 if (!known_size_p (read_info->width))
2891 /* Handle block mode reads. */
2892 if (kill)
2893 bitmap_ior_into (kill, group->group_kill);
2894 bitmap_and_compl_into (gen, group->group_kill);
2896 else
2898 /* The groups are the same, just process the
2899 offsets. */
2900 HOST_WIDE_INT j;
2901 HOST_WIDE_INT end = read_info->offset + read_info->width;
2902 for (j = read_info->offset; j < end; j++)
2904 int index = get_bitmap_index (group, j);
2905 if (index != 0)
2907 if (kill)
2908 bitmap_set_bit (kill, index);
2909 bitmap_clear_bit (gen, index);
2914 else
2916 /* The groups are different, if the alias sets
2917 conflict, clear the entire group. We only need
2918 to apply this test if the read_info is a cselib
2919 read. Anything with a constant base cannot alias
2920 something else with a different constant
2921 base. */
2922 if ((read_info->group_id < 0)
2923 && canon_true_dependence (group->base_mem,
2924 GET_MODE (group->base_mem),
2925 group->canon_base_addr,
2926 read_info->mem, NULL_RTX))
2928 if (kill)
2929 bitmap_ior_into (kill, group->group_kill);
2930 bitmap_and_compl_into (gen, group->group_kill);
2936 read_info = read_info->next;
2941 /* Return the insn in BB_INFO before the first wild read or if there
2942 are no wild reads in the block, return the last insn. */
2944 static insn_info_t
2945 find_insn_before_first_wild_read (bb_info_t bb_info)
2947 insn_info_t insn_info = bb_info->last_insn;
2948 insn_info_t last_wild_read = NULL;
2950 while (insn_info)
2952 if (insn_info->wild_read)
2954 last_wild_read = insn_info->prev_insn;
2955 /* Block starts with wild read. */
2956 if (!last_wild_read)
2957 return NULL;
2960 insn_info = insn_info->prev_insn;
2963 if (last_wild_read)
2964 return last_wild_read;
2965 else
2966 return bb_info->last_insn;
2970 /* Scan the insns in BB_INFO starting at PTR and going to the top of
2971 the block in order to build the gen and kill sets for the block.
2972 We start at ptr which may be the last insn in the block or may be
2973 the first insn with a wild read. In the latter case we are able to
2974 skip the rest of the block because it just does not matter:
2975 anything that happens is hidden by the wild read. */
2977 static void
2978 dse_step3_scan (basic_block bb)
2980 bb_info_t bb_info = bb_table[bb->index];
2981 insn_info_t insn_info;
2983 insn_info = find_insn_before_first_wild_read (bb_info);
2985 /* In the spill case or in the no_spill case if there is no wild
2986 read in the block, we will need a kill set. */
2987 if (insn_info == bb_info->last_insn)
2989 if (bb_info->kill)
2990 bitmap_clear (bb_info->kill);
2991 else
2992 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
2994 else
2995 if (bb_info->kill)
2996 BITMAP_FREE (bb_info->kill);
2998 while (insn_info)
3000 /* There may have been code deleted by the dce pass run before
3001 this phase. */
3002 if (insn_info->insn && INSN_P (insn_info->insn))
3004 scan_stores (insn_info->store_rec, bb_info->gen, bb_info->kill);
3005 scan_reads (insn_info, bb_info->gen, bb_info->kill);
3008 insn_info = insn_info->prev_insn;
3013 /* Set the gen set of the exit block, and also any block with no
3014 successors that does not have a wild read. */
3016 static void
3017 dse_step3_exit_block_scan (bb_info_t bb_info)
3019 /* The gen set is all 0's for the exit block except for the
3020 frame_pointer_group. */
3022 if (stores_off_frame_dead_at_return)
3024 unsigned int i;
3025 group_info *group;
3027 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3029 if (group->process_globally && group->frame_related)
3030 bitmap_ior_into (bb_info->gen, group->group_kill);
3036 /* Find all of the blocks that are not backwards reachable from the
3037 exit block or any block with no successors (BB). These are the
3038 infinite loops or infinite self loops. These blocks will still
3039 have their bits set in UNREACHABLE_BLOCKS. */
3041 static void
3042 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3044 edge e;
3045 edge_iterator ei;
3047 if (bitmap_bit_p (unreachable_blocks, bb->index))
3049 bitmap_clear_bit (unreachable_blocks, bb->index);
3050 FOR_EACH_EDGE (e, ei, bb->preds)
3052 mark_reachable_blocks (unreachable_blocks, e->src);
3057 /* Build the transfer functions for the function. */
3059 static void
3060 dse_step3 ()
3062 basic_block bb;
3063 sbitmap_iterator sbi;
3064 bitmap all_ones = NULL;
3065 unsigned int i;
3067 auto_sbitmap unreachable_blocks (last_basic_block_for_fn (cfun));
3068 bitmap_ones (unreachable_blocks);
3070 FOR_ALL_BB_FN (bb, cfun)
3072 bb_info_t bb_info = bb_table[bb->index];
3073 if (bb_info->gen)
3074 bitmap_clear (bb_info->gen);
3075 else
3076 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3078 if (bb->index == ENTRY_BLOCK)
3080 else if (bb->index == EXIT_BLOCK)
3081 dse_step3_exit_block_scan (bb_info);
3082 else
3083 dse_step3_scan (bb);
3084 if (EDGE_COUNT (bb->succs) == 0)
3085 mark_reachable_blocks (unreachable_blocks, bb);
3087 /* If this is the second time dataflow is run, delete the old
3088 sets. */
3089 if (bb_info->in)
3090 BITMAP_FREE (bb_info->in);
3091 if (bb_info->out)
3092 BITMAP_FREE (bb_info->out);
3095 /* For any block in an infinite loop, we must initialize the out set
3096 to all ones. This could be expensive, but almost never occurs in
3097 practice. However, it is common in regression tests. */
3098 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3100 if (bitmap_bit_p (all_blocks, i))
3102 bb_info_t bb_info = bb_table[i];
3103 if (!all_ones)
3105 unsigned int j;
3106 group_info *group;
3108 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3109 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3110 bitmap_ior_into (all_ones, group->group_kill);
3112 if (!bb_info->out)
3114 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3115 bitmap_copy (bb_info->out, all_ones);
3120 if (all_ones)
3121 BITMAP_FREE (all_ones);
3126 /*----------------------------------------------------------------------------
3127 Fourth step.
3129 Solve the bitvector equations.
3130 ----------------------------------------------------------------------------*/
3133 /* Confluence function for blocks with no successors. Create an out
3134 set from the gen set of the exit block. This block logically has
3135 the exit block as a successor. */
3139 static void
3140 dse_confluence_0 (basic_block bb)
3142 bb_info_t bb_info = bb_table[bb->index];
3144 if (bb->index == EXIT_BLOCK)
3145 return;
3147 if (!bb_info->out)
3149 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3150 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3154 /* Propagate the information from the in set of the dest of E to the
3155 out set of the src of E. If the various in or out sets are not
3156 there, that means they are all ones. */
3158 static bool
3159 dse_confluence_n (edge e)
3161 bb_info_t src_info = bb_table[e->src->index];
3162 bb_info_t dest_info = bb_table[e->dest->index];
3164 if (dest_info->in)
3166 if (src_info->out)
3167 bitmap_and_into (src_info->out, dest_info->in);
3168 else
3170 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3171 bitmap_copy (src_info->out, dest_info->in);
3174 return true;
3178 /* Propagate the info from the out to the in set of BB_INDEX's basic
3179 block. There are three cases:
3181 1) The block has no kill set. In this case the kill set is all
3182 ones. It does not matter what the out set of the block is, none of
3183 the info can reach the top. The only thing that reaches the top is
3184 the gen set and we just copy the set.
3186 2) There is a kill set but no out set and bb has successors. In
3187 this case we just return. Eventually an out set will be created and
3188 it is better to wait than to create a set of ones.
3190 3) There is both a kill and out set. We apply the obvious transfer
3191 function.
3194 static bool
3195 dse_transfer_function (int bb_index)
3197 bb_info_t bb_info = bb_table[bb_index];
3199 if (bb_info->kill)
3201 if (bb_info->out)
3203 /* Case 3 above. */
3204 if (bb_info->in)
3205 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3206 bb_info->out, bb_info->kill);
3207 else
3209 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3210 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3211 bb_info->out, bb_info->kill);
3212 return true;
3215 else
3216 /* Case 2 above. */
3217 return false;
3219 else
3221 /* Case 1 above. If there is already an in set, nothing
3222 happens. */
3223 if (bb_info->in)
3224 return false;
3225 else
3227 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3228 bitmap_copy (bb_info->in, bb_info->gen);
3229 return true;
3234 /* Solve the dataflow equations. */
3236 static void
3237 dse_step4 (void)
3239 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3240 dse_confluence_n, dse_transfer_function,
3241 all_blocks, df_get_postorder (DF_BACKWARD),
3242 df_get_n_blocks (DF_BACKWARD));
3243 if (dump_file && (dump_flags & TDF_DETAILS))
3245 basic_block bb;
3247 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3248 FOR_ALL_BB_FN (bb, cfun)
3250 bb_info_t bb_info = bb_table[bb->index];
3252 df_print_bb_index (bb, dump_file);
3253 if (bb_info->in)
3254 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3255 else
3256 fprintf (dump_file, " in: *MISSING*\n");
3257 if (bb_info->gen)
3258 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3259 else
3260 fprintf (dump_file, " gen: *MISSING*\n");
3261 if (bb_info->kill)
3262 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3263 else
3264 fprintf (dump_file, " kill: *MISSING*\n");
3265 if (bb_info->out)
3266 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3267 else
3268 fprintf (dump_file, " out: *MISSING*\n\n");
3275 /*----------------------------------------------------------------------------
3276 Fifth step.
3278 Delete the stores that can only be deleted using the global information.
3279 ----------------------------------------------------------------------------*/
3282 static void
3283 dse_step5 (void)
3285 basic_block bb;
3286 FOR_EACH_BB_FN (bb, cfun)
3288 bb_info_t bb_info = bb_table[bb->index];
3289 insn_info_t insn_info = bb_info->last_insn;
3290 bitmap v = bb_info->out;
3292 while (insn_info)
3294 bool deleted = false;
3295 if (dump_file && insn_info->insn)
3297 fprintf (dump_file, "starting to process insn %d\n",
3298 INSN_UID (insn_info->insn));
3299 bitmap_print (dump_file, v, " v: ", "\n");
3302 /* There may have been code deleted by the dce pass run before
3303 this phase. */
3304 if (insn_info->insn
3305 && INSN_P (insn_info->insn)
3306 && (!insn_info->cannot_delete)
3307 && (!bitmap_empty_p (v)))
3309 store_info *store_info = insn_info->store_rec;
3311 /* Try to delete the current insn. */
3312 deleted = true;
3314 /* Skip the clobbers. */
3315 while (!store_info->is_set)
3316 store_info = store_info->next;
3318 HOST_WIDE_INT i;
3319 group_info *group_info = rtx_group_vec[store_info->group_id];
3321 HOST_WIDE_INT end = store_info->offset + store_info->width;
3322 for (i = store_info->offset; i < end; i++)
3324 int index = get_bitmap_index (group_info, i);
3326 if (dump_file && (dump_flags & TDF_DETAILS))
3327 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3328 if (index == 0 || !bitmap_bit_p (v, index))
3330 if (dump_file && (dump_flags & TDF_DETAILS))
3331 fprintf (dump_file, "failing at i = %d\n", (int)i);
3332 deleted = false;
3333 break;
3336 if (deleted)
3338 if (dbg_cnt (dse)
3339 && check_for_inc_dec_1 (insn_info))
3341 delete_insn (insn_info->insn);
3342 insn_info->insn = NULL;
3343 globally_deleted++;
3347 /* We do want to process the local info if the insn was
3348 deleted. For instance, if the insn did a wild read, we
3349 no longer need to trash the info. */
3350 if (insn_info->insn
3351 && INSN_P (insn_info->insn)
3352 && (!deleted))
3354 scan_stores (insn_info->store_rec, v, NULL);
3355 if (insn_info->wild_read)
3357 if (dump_file && (dump_flags & TDF_DETAILS))
3358 fprintf (dump_file, "wild read\n");
3359 bitmap_clear (v);
3361 else if (insn_info->read_rec
3362 || insn_info->non_frame_wild_read
3363 || insn_info->frame_read)
3365 if (dump_file && (dump_flags & TDF_DETAILS))
3367 if (!insn_info->non_frame_wild_read
3368 && !insn_info->frame_read)
3369 fprintf (dump_file, "regular read\n");
3370 if (insn_info->non_frame_wild_read)
3371 fprintf (dump_file, "non-frame wild read\n");
3372 if (insn_info->frame_read)
3373 fprintf (dump_file, "frame read\n");
3375 scan_reads (insn_info, v, NULL);
3379 insn_info = insn_info->prev_insn;
3386 /*----------------------------------------------------------------------------
3387 Sixth step.
3389 Delete stores made redundant by earlier stores (which store the same
3390 value) that couldn't be eliminated.
3391 ----------------------------------------------------------------------------*/
3393 static void
3394 dse_step6 (void)
3396 basic_block bb;
3398 FOR_ALL_BB_FN (bb, cfun)
3400 bb_info_t bb_info = bb_table[bb->index];
3401 insn_info_t insn_info = bb_info->last_insn;
3403 while (insn_info)
3405 /* There may have been code deleted by the dce pass run before
3406 this phase. */
3407 if (insn_info->insn
3408 && INSN_P (insn_info->insn)
3409 && !insn_info->cannot_delete)
3411 store_info *s_info = insn_info->store_rec;
3413 while (s_info && !s_info->is_set)
3414 s_info = s_info->next;
3415 if (s_info
3416 && s_info->redundant_reason
3417 && s_info->redundant_reason->insn
3418 && INSN_P (s_info->redundant_reason->insn))
3420 rtx_insn *rinsn = s_info->redundant_reason->insn;
3421 if (dump_file && (dump_flags & TDF_DETAILS))
3422 fprintf (dump_file, "Locally deleting insn %d "
3423 "because insn %d stores the "
3424 "same value and couldn't be "
3425 "eliminated\n",
3426 INSN_UID (insn_info->insn),
3427 INSN_UID (rinsn));
3428 delete_dead_store_insn (insn_info);
3431 insn_info = insn_info->prev_insn;
3436 /*----------------------------------------------------------------------------
3437 Seventh step.
3439 Destroy everything left standing.
3440 ----------------------------------------------------------------------------*/
3442 static void
3443 dse_step7 (void)
3445 bitmap_obstack_release (&dse_bitmap_obstack);
3446 obstack_free (&dse_obstack, NULL);
3448 end_alias_analysis ();
3449 free (bb_table);
3450 delete rtx_group_table;
3451 rtx_group_table = NULL;
3452 rtx_group_vec.release ();
3453 BITMAP_FREE (all_blocks);
3454 BITMAP_FREE (scratch);
3456 rtx_store_info_pool.release ();
3457 read_info_type_pool.release ();
3458 insn_info_type_pool.release ();
3459 dse_bb_info_type_pool.release ();
3460 group_info_pool.release ();
3461 deferred_change_pool.release ();
3465 /* -------------------------------------------------------------------------
3467 ------------------------------------------------------------------------- */
3469 /* Callback for running pass_rtl_dse. */
3471 static unsigned int
3472 rest_of_handle_dse (void)
3474 df_set_flags (DF_DEFER_INSN_RESCAN);
3476 /* Need the notes since we must track live hardregs in the forwards
3477 direction. */
3478 df_note_add_problem ();
3479 df_analyze ();
3481 dse_step0 ();
3482 dse_step1 ();
3483 dse_step2_init ();
3484 if (dse_step2 ())
3486 df_set_flags (DF_LR_RUN_DCE);
3487 df_analyze ();
3488 if (dump_file && (dump_flags & TDF_DETAILS))
3489 fprintf (dump_file, "doing global processing\n");
3490 dse_step3 ();
3491 dse_step4 ();
3492 dse_step5 ();
3495 dse_step6 ();
3496 dse_step7 ();
3498 if (dump_file)
3499 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d\n",
3500 locally_deleted, globally_deleted);
3502 /* DSE can eliminate potentially-trapping MEMs.
3503 Remove any EH edges associated with them. */
3504 if ((locally_deleted || globally_deleted)
3505 && cfun->can_throw_non_call_exceptions
3506 && purge_all_dead_edges ())
3507 cleanup_cfg (0);
3509 return 0;
3512 namespace {
3514 const pass_data pass_data_rtl_dse1 =
3516 RTL_PASS, /* type */
3517 "dse1", /* name */
3518 OPTGROUP_NONE, /* optinfo_flags */
3519 TV_DSE1, /* tv_id */
3520 0, /* properties_required */
3521 0, /* properties_provided */
3522 0, /* properties_destroyed */
3523 0, /* todo_flags_start */
3524 TODO_df_finish, /* todo_flags_finish */
3527 class pass_rtl_dse1 : public rtl_opt_pass
3529 public:
3530 pass_rtl_dse1 (gcc::context *ctxt)
3531 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3534 /* opt_pass methods: */
3535 virtual bool gate (function *)
3537 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3540 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3542 }; // class pass_rtl_dse1
3544 } // anon namespace
3546 rtl_opt_pass *
3547 make_pass_rtl_dse1 (gcc::context *ctxt)
3549 return new pass_rtl_dse1 (ctxt);
3552 namespace {
3554 const pass_data pass_data_rtl_dse2 =
3556 RTL_PASS, /* type */
3557 "dse2", /* name */
3558 OPTGROUP_NONE, /* optinfo_flags */
3559 TV_DSE2, /* tv_id */
3560 0, /* properties_required */
3561 0, /* properties_provided */
3562 0, /* properties_destroyed */
3563 0, /* todo_flags_start */
3564 TODO_df_finish, /* todo_flags_finish */
3567 class pass_rtl_dse2 : public rtl_opt_pass
3569 public:
3570 pass_rtl_dse2 (gcc::context *ctxt)
3571 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3574 /* opt_pass methods: */
3575 virtual bool gate (function *)
3577 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3580 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3582 }; // class pass_rtl_dse2
3584 } // anon namespace
3586 rtl_opt_pass *
3587 make_pass_rtl_dse2 (gcc::context *ctxt)
3589 return new pass_rtl_dse2 (ctxt);