]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/dse.c
cselib and incdec
[thirdparty/gcc.git] / gcc / dse.c
1 /* RTL dead store elimination.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3
4 Contributed by Richard Sandiford <rsandifor@codesourcery.com>
5 and Kenneth Zadeck <zadeck@naturalbridge.com>
6
7 This file is part of GCC.
8
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.
13
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.
18
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/>. */
22
23 #undef BASELINE
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "hash-table.h"
29 #include "tm.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "tm_p.h"
34 #include "regs.h"
35 #include "hard-reg-set.h"
36 #include "regset.h"
37 #include "flags.h"
38 #include "df.h"
39 #include "cselib.h"
40 #include "tree-pass.h"
41 #include "alloc-pool.h"
42 #include "alias.h"
43 #include "insn-config.h"
44 #include "expr.h"
45 #include "recog.h"
46 #include "optabs.h"
47 #include "dbgcnt.h"
48 #include "target.h"
49 #include "params.h"
50 #include "tree-ssa-alias.h"
51 #include "internal-fn.h"
52 #include "gimple-expr.h"
53 #include "is-a.h"
54 #include "gimple.h"
55 #include "gimple-ssa.h"
56
57 /* This file contains three techniques for performing Dead Store
58 Elimination (dse).
59
60 * The first technique performs dse locally on any base address. It
61 is based on the cselib which is a local value numbering technique.
62 This technique is local to a basic block but deals with a fairly
63 general addresses.
64
65 * The second technique performs dse globally but is restricted to
66 base addresses that are either constant or are relative to the
67 frame_pointer.
68
69 * The third technique, (which is only done after register allocation)
70 processes the spill spill slots. This differs from the second
71 technique because it takes advantage of the fact that spilling is
72 completely free from the effects of aliasing.
73
74 Logically, dse is a backwards dataflow problem. A store can be
75 deleted if it if cannot be reached in the backward direction by any
76 use of the value being stored. However, the local technique uses a
77 forwards scan of the basic block because cselib requires that the
78 block be processed in that order.
79
80 The pass is logically broken into 7 steps:
81
82 0) Initialization.
83
84 1) The local algorithm, as well as scanning the insns for the two
85 global algorithms.
86
87 2) Analysis to see if the global algs are necessary. In the case
88 of stores base on a constant address, there must be at least two
89 stores to that address, to make it possible to delete some of the
90 stores. In the case of stores off of the frame or spill related
91 stores, only one store to an address is necessary because those
92 stores die at the end of the function.
93
94 3) Set up the global dataflow equations based on processing the
95 info parsed in the first step.
96
97 4) Solve the dataflow equations.
98
99 5) Delete the insns that the global analysis has indicated are
100 unnecessary.
101
102 6) Delete insns that store the same value as preceding store
103 where the earlier store couldn't be eliminated.
104
105 7) Cleanup.
106
107 This step uses cselib and canon_rtx to build the largest expression
108 possible for each address. This pass is a forwards pass through
109 each basic block. From the point of view of the global technique,
110 the first pass could examine a block in either direction. The
111 forwards ordering is to accommodate cselib.
112
113 We make a simplifying assumption: addresses fall into four broad
114 categories:
115
116 1) base has rtx_varies_p == false, offset is constant.
117 2) base has rtx_varies_p == false, offset variable.
118 3) base has rtx_varies_p == true, offset constant.
119 4) base has rtx_varies_p == true, offset variable.
120
121 The local passes are able to process all 4 kinds of addresses. The
122 global pass only handles 1).
123
124 The global problem is formulated as follows:
125
126 A store, S1, to address A, where A is not relative to the stack
127 frame, can be eliminated if all paths from S1 to the end of the
128 function contain another store to A before a read to A.
129
130 If the address A is relative to the stack frame, a store S2 to A
131 can be eliminated if there are no paths from S2 that reach the
132 end of the function that read A before another store to A. In
133 this case S2 can be deleted if there are paths from S2 to the
134 end of the function that have no reads or writes to A. This
135 second case allows stores to the stack frame to be deleted that
136 would otherwise die when the function returns. This cannot be
137 done if stores_off_frame_dead_at_return is not true. See the doc
138 for that variable for when this variable is false.
139
140 The global problem is formulated as a backwards set union
141 dataflow problem where the stores are the gens and reads are the
142 kills. Set union problems are rare and require some special
143 handling given our representation of bitmaps. A straightforward
144 implementation requires a lot of bitmaps filled with 1s.
145 These are expensive and cumbersome in our bitmap formulation so
146 care has been taken to avoid large vectors filled with 1s. See
147 the comments in bb_info and in the dataflow confluence functions
148 for details.
149
150 There are two places for further enhancements to this algorithm:
151
152 1) The original dse which was embedded in a pass called flow also
153 did local address forwarding. For example in
154
155 A <- r100
156 ... <- A
157
158 flow would replace the right hand side of the second insn with a
159 reference to r100. Most of the information is available to add this
160 to this pass. It has not done it because it is a lot of work in
161 the case that either r100 is assigned to between the first and
162 second insn and/or the second insn is a load of part of the value
163 stored by the first insn.
164
165 insn 5 in gcc.c-torture/compile/990203-1.c simple case.
166 insn 15 in gcc.c-torture/execute/20001017-2.c simple case.
167 insn 25 in gcc.c-torture/execute/20001026-1.c simple case.
168 insn 44 in gcc.c-torture/execute/20010910-1.c simple case.
169
170 2) The cleaning up of spill code is quite profitable. It currently
171 depends on reading tea leaves and chicken entrails left by reload.
172 This pass depends on reload creating a singleton alias set for each
173 spill slot and telling the next dse pass which of these alias sets
174 are the singletons. Rather than analyze the addresses of the
175 spills, dse's spill processing just does analysis of the loads and
176 stores that use those alias sets. There are three cases where this
177 falls short:
178
179 a) Reload sometimes creates the slot for one mode of access, and
180 then inserts loads and/or stores for a smaller mode. In this
181 case, the current code just punts on the slot. The proper thing
182 to do is to back out and use one bit vector position for each
183 byte of the entity associated with the slot. This depends on
184 KNOWING that reload always generates the accesses for each of the
185 bytes in some canonical (read that easy to understand several
186 passes after reload happens) way.
187
188 b) Reload sometimes decides that spill slot it allocated was not
189 large enough for the mode and goes back and allocates more slots
190 with the same mode and alias set. The backout in this case is a
191 little more graceful than (a). In this case the slot is unmarked
192 as being a spill slot and if final address comes out to be based
193 off the frame pointer, the global algorithm handles this slot.
194
195 c) For any pass that may prespill, there is currently no
196 mechanism to tell the dse pass that the slot being used has the
197 special properties that reload uses. It may be that all that is
198 required is to have those passes make the same calls that reload
199 does, assuming that the alias sets can be manipulated in the same
200 way. */
201
202 /* There are limits to the size of constant offsets we model for the
203 global problem. There are certainly test cases, that exceed this
204 limit, however, it is unlikely that there are important programs
205 that really have constant offsets this size. */
206 #define MAX_OFFSET (64 * 1024)
207
208 /* Obstack for the DSE dataflow bitmaps. We don't want to put these
209 on the default obstack because these bitmaps can grow quite large
210 (~2GB for the small (!) test case of PR54146) and we'll hold on to
211 all that memory until the end of the compiler run.
212 As a bonus, delete_tree_live_info can destroy all the bitmaps by just
213 releasing the whole obstack. */
214 static bitmap_obstack dse_bitmap_obstack;
215
216 /* Obstack for other data. As for above: Kinda nice to be able to
217 throw it all away at the end in one big sweep. */
218 static struct obstack dse_obstack;
219
220 /* Scratch bitmap for cselib's cselib_expand_value_rtx. */
221 static bitmap scratch = NULL;
222
223 struct insn_info;
224
225 /* This structure holds information about a candidate store. */
226 struct store_info
227 {
228
229 /* False means this is a clobber. */
230 bool is_set;
231
232 /* False if a single HOST_WIDE_INT bitmap is used for positions_needed. */
233 bool is_large;
234
235 /* The id of the mem group of the base address. If rtx_varies_p is
236 true, this is -1. Otherwise, it is the index into the group
237 table. */
238 int group_id;
239
240 /* This is the cselib value. */
241 cselib_val *cse_base;
242
243 /* This canonized mem. */
244 rtx mem;
245
246 /* Canonized MEM address for use by canon_true_dependence. */
247 rtx mem_addr;
248
249 /* If this is non-zero, it is the alias set of a spill location. */
250 alias_set_type alias_set;
251
252 /* The offset of the first and byte before the last byte associated
253 with the operation. */
254 HOST_WIDE_INT begin, end;
255
256 union
257 {
258 /* A bitmask as wide as the number of bytes in the word that
259 contains a 1 if the byte may be needed. The store is unused if
260 all of the bits are 0. This is used if IS_LARGE is false. */
261 unsigned HOST_WIDE_INT small_bitmask;
262
263 struct
264 {
265 /* A bitmap with one bit per byte. Cleared bit means the position
266 is needed. Used if IS_LARGE is false. */
267 bitmap bmap;
268
269 /* Number of set bits (i.e. unneeded bytes) in BITMAP. If it is
270 equal to END - BEGIN, the whole store is unused. */
271 int count;
272 } large;
273 } positions_needed;
274
275 /* The next store info for this insn. */
276 struct store_info *next;
277
278 /* The right hand side of the store. This is used if there is a
279 subsequent reload of the mems address somewhere later in the
280 basic block. */
281 rtx rhs;
282
283 /* If rhs is or holds a constant, this contains that constant,
284 otherwise NULL. */
285 rtx const_rhs;
286
287 /* Set if this store stores the same constant value as REDUNDANT_REASON
288 insn stored. These aren't eliminated early, because doing that
289 might prevent the earlier larger store to be eliminated. */
290 struct insn_info *redundant_reason;
291 };
292
293 /* Return a bitmask with the first N low bits set. */
294
295 static unsigned HOST_WIDE_INT
296 lowpart_bitmask (int n)
297 {
298 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT) 0;
299 return mask >> (HOST_BITS_PER_WIDE_INT - n);
300 }
301
302 typedef struct store_info *store_info_t;
303 static alloc_pool cse_store_info_pool;
304 static alloc_pool rtx_store_info_pool;
305
306 /* This structure holds information about a load. These are only
307 built for rtx bases. */
308 struct read_info
309 {
310 /* The id of the mem group of the base address. */
311 int group_id;
312
313 /* If this is non-zero, it is the alias set of a spill location. */
314 alias_set_type alias_set;
315
316 /* The offset of the first and byte after the last byte associated
317 with the operation. If begin == end == 0, the read did not have
318 a constant offset. */
319 int begin, end;
320
321 /* The mem being read. */
322 rtx mem;
323
324 /* The next read_info for this insn. */
325 struct read_info *next;
326 };
327 typedef struct read_info *read_info_t;
328 static alloc_pool read_info_pool;
329
330
331 /* One of these records is created for each insn. */
332
333 struct insn_info
334 {
335 /* Set true if the insn contains a store but the insn itself cannot
336 be deleted. This is set if the insn is a parallel and there is
337 more than one non dead output or if the insn is in some way
338 volatile. */
339 bool cannot_delete;
340
341 /* This field is only used by the global algorithm. It is set true
342 if the insn contains any read of mem except for a (1). This is
343 also set if the insn is a call or has a clobber mem. If the insn
344 contains a wild read, the use_rec will be null. */
345 bool wild_read;
346
347 /* This is true only for CALL instructions which could potentially read
348 any non-frame memory location. This field is used by the global
349 algorithm. */
350 bool non_frame_wild_read;
351
352 /* This field is only used for the processing of const functions.
353 These functions cannot read memory, but they can read the stack
354 because that is where they may get their parms. We need to be
355 this conservative because, like the store motion pass, we don't
356 consider CALL_INSN_FUNCTION_USAGE when processing call insns.
357 Moreover, we need to distinguish two cases:
358 1. Before reload (register elimination), the stores related to
359 outgoing arguments are stack pointer based and thus deemed
360 of non-constant base in this pass. This requires special
361 handling but also means that the frame pointer based stores
362 need not be killed upon encountering a const function call.
363 2. After reload, the stores related to outgoing arguments can be
364 either stack pointer or hard frame pointer based. This means
365 that we have no other choice than also killing all the frame
366 pointer based stores upon encountering a const function call.
367 This field is set after reload for const function calls. Having
368 this set is less severe than a wild read, it just means that all
369 the frame related stores are killed rather than all the stores. */
370 bool frame_read;
371
372 /* This field is only used for the processing of const functions.
373 It is set if the insn may contain a stack pointer based store. */
374 bool stack_pointer_based;
375
376 /* This is true if any of the sets within the store contains a
377 cselib base. Such stores can only be deleted by the local
378 algorithm. */
379 bool contains_cselib_groups;
380
381 /* The insn. */
382 rtx_insn *insn;
383
384 /* The list of mem sets or mem clobbers that are contained in this
385 insn. If the insn is deletable, it contains only one mem set.
386 But it could also contain clobbers. Insns that contain more than
387 one mem set are not deletable, but each of those mems are here in
388 order to provide info to delete other insns. */
389 store_info_t store_rec;
390
391 /* The linked list of mem uses in this insn. Only the reads from
392 rtx bases are listed here. The reads to cselib bases are
393 completely processed during the first scan and so are never
394 created. */
395 read_info_t read_rec;
396
397 /* The live fixed registers. We assume only fixed registers can
398 cause trouble by being clobbered from an expanded pattern;
399 storing only the live fixed registers (rather than all registers)
400 means less memory needs to be allocated / copied for the individual
401 stores. */
402 regset fixed_regs_live;
403
404 /* The prev insn in the basic block. */
405 struct insn_info * prev_insn;
406
407 /* The linked list of insns that are in consideration for removal in
408 the forwards pass through the basic block. This pointer may be
409 trash as it is not cleared when a wild read occurs. The only
410 time it is guaranteed to be correct is when the traversal starts
411 at active_local_stores. */
412 struct insn_info * next_local_store;
413 };
414
415 typedef struct insn_info *insn_info_t;
416 static alloc_pool insn_info_pool;
417
418 /* The linked list of stores that are under consideration in this
419 basic block. */
420 static insn_info_t active_local_stores;
421 static int active_local_stores_len;
422
423 struct bb_info
424 {
425
426 /* Pointer to the insn info for the last insn in the block. These
427 are linked so this is how all of the insns are reached. During
428 scanning this is the current insn being scanned. */
429 insn_info_t last_insn;
430
431 /* The info for the global dataflow problem. */
432
433
434 /* This is set if the transfer function should and in the wild_read
435 bitmap before applying the kill and gen sets. That vector knocks
436 out most of the bits in the bitmap and thus speeds up the
437 operations. */
438 bool apply_wild_read;
439
440 /* The following 4 bitvectors hold information about which positions
441 of which stores are live or dead. They are indexed by
442 get_bitmap_index. */
443
444 /* The set of store positions that exist in this block before a wild read. */
445 bitmap gen;
446
447 /* The set of load positions that exist in this block above the
448 same position of a store. */
449 bitmap kill;
450
451 /* The set of stores that reach the top of the block without being
452 killed by a read.
453
454 Do not represent the in if it is all ones. Note that this is
455 what the bitvector should logically be initialized to for a set
456 intersection problem. However, like the kill set, this is too
457 expensive. So initially, the in set will only be created for the
458 exit block and any block that contains a wild read. */
459 bitmap in;
460
461 /* The set of stores that reach the bottom of the block from it's
462 successors.
463
464 Do not represent the in if it is all ones. Note that this is
465 what the bitvector should logically be initialized to for a set
466 intersection problem. However, like the kill and in set, this is
467 too expensive. So what is done is that the confluence operator
468 just initializes the vector from one of the out sets of the
469 successors of the block. */
470 bitmap out;
471
472 /* The following bitvector is indexed by the reg number. It
473 contains the set of regs that are live at the current instruction
474 being processed. While it contains info for all of the
475 registers, only the hard registers are actually examined. It is used
476 to assure that shift and/or add sequences that are inserted do not
477 accidentally clobber live hard regs. */
478 bitmap regs_live;
479 };
480
481 typedef struct bb_info *bb_info_t;
482 static alloc_pool bb_info_pool;
483
484 /* Table to hold all bb_infos. */
485 static bb_info_t *bb_table;
486
487 /* There is a group_info for each rtx base that is used to reference
488 memory. There are also not many of the rtx bases because they are
489 very limited in scope. */
490
491 struct group_info
492 {
493 /* The actual base of the address. */
494 rtx rtx_base;
495
496 /* The sequential id of the base. This allows us to have a
497 canonical ordering of these that is not based on addresses. */
498 int id;
499
500 /* True if there are any positions that are to be processed
501 globally. */
502 bool process_globally;
503
504 /* True if the base of this group is either the frame_pointer or
505 hard_frame_pointer. */
506 bool frame_related;
507
508 /* A mem wrapped around the base pointer for the group in order to do
509 read dependency. It must be given BLKmode in order to encompass all
510 the possible offsets from the base. */
511 rtx base_mem;
512
513 /* Canonized version of base_mem's address. */
514 rtx canon_base_addr;
515
516 /* These two sets of two bitmaps are used to keep track of how many
517 stores are actually referencing that position from this base. We
518 only do this for rtx bases as this will be used to assign
519 positions in the bitmaps for the global problem. Bit N is set in
520 store1 on the first store for offset N. Bit N is set in store2
521 for the second store to offset N. This is all we need since we
522 only care about offsets that have two or more stores for them.
523
524 The "_n" suffix is for offsets less than 0 and the "_p" suffix is
525 for 0 and greater offsets.
526
527 There is one special case here, for stores into the stack frame,
528 we will or store1 into store2 before deciding which stores look
529 at globally. This is because stores to the stack frame that have
530 no other reads before the end of the function can also be
531 deleted. */
532 bitmap store1_n, store1_p, store2_n, store2_p;
533
534 /* These bitmaps keep track of offsets in this group escape this function.
535 An offset escapes if it corresponds to a named variable whose
536 addressable flag is set. */
537 bitmap escaped_n, escaped_p;
538
539 /* The positions in this bitmap have the same assignments as the in,
540 out, gen and kill bitmaps. This bitmap is all zeros except for
541 the positions that are occupied by stores for this group. */
542 bitmap group_kill;
543
544 /* The offset_map is used to map the offsets from this base into
545 positions in the global bitmaps. It is only created after all of
546 the all of stores have been scanned and we know which ones we
547 care about. */
548 int *offset_map_n, *offset_map_p;
549 int offset_map_size_n, offset_map_size_p;
550 };
551 typedef struct group_info *group_info_t;
552 typedef const struct group_info *const_group_info_t;
553 static alloc_pool rtx_group_info_pool;
554
555 /* Index into the rtx_group_vec. */
556 static int rtx_group_next_id;
557
558
559 static vec<group_info_t> rtx_group_vec;
560
561
562 /* This structure holds the set of changes that are being deferred
563 when removing read operation. See replace_read. */
564 struct deferred_change
565 {
566
567 /* The mem that is being replaced. */
568 rtx *loc;
569
570 /* The reg it is being replaced with. */
571 rtx reg;
572
573 struct deferred_change *next;
574 };
575
576 typedef struct deferred_change *deferred_change_t;
577 static alloc_pool deferred_change_pool;
578
579 static deferred_change_t deferred_change_list = NULL;
580
581 /* The group that holds all of the clear_alias_sets. */
582 static group_info_t clear_alias_group;
583
584 /* The modes of the clear_alias_sets. */
585 static htab_t clear_alias_mode_table;
586
587 /* Hash table element to look up the mode for an alias set. */
588 struct clear_alias_mode_holder
589 {
590 alias_set_type alias_set;
591 enum machine_mode mode;
592 };
593
594 /* This is true except if cfun->stdarg -- i.e. we cannot do
595 this for vararg functions because they play games with the frame. */
596 static bool stores_off_frame_dead_at_return;
597
598 /* Counter for stats. */
599 static int globally_deleted;
600 static int locally_deleted;
601 static int spill_deleted;
602
603 static bitmap all_blocks;
604
605 /* Locations that are killed by calls in the global phase. */
606 static bitmap kill_on_calls;
607
608 /* The number of bits used in the global bitmaps. */
609 static unsigned int current_position;
610 \f
611 /*----------------------------------------------------------------------------
612 Zeroth step.
613
614 Initialization.
615 ----------------------------------------------------------------------------*/
616
617
618 /* Find the entry associated with ALIAS_SET. */
619
620 static struct clear_alias_mode_holder *
621 clear_alias_set_lookup (alias_set_type alias_set)
622 {
623 struct clear_alias_mode_holder tmp_holder;
624 void **slot;
625
626 tmp_holder.alias_set = alias_set;
627 slot = htab_find_slot (clear_alias_mode_table, &tmp_holder, NO_INSERT);
628 gcc_assert (*slot);
629
630 return (struct clear_alias_mode_holder *) *slot;
631 }
632
633
634 /* Hashtable callbacks for maintaining the "bases" field of
635 store_group_info, given that the addresses are function invariants. */
636
637 struct invariant_group_base_hasher : typed_noop_remove <group_info>
638 {
639 typedef group_info value_type;
640 typedef group_info compare_type;
641 static inline hashval_t hash (const value_type *);
642 static inline bool equal (const value_type *, const compare_type *);
643 };
644
645 inline bool
646 invariant_group_base_hasher::equal (const value_type *gi1,
647 const compare_type *gi2)
648 {
649 return rtx_equal_p (gi1->rtx_base, gi2->rtx_base);
650 }
651
652 inline hashval_t
653 invariant_group_base_hasher::hash (const value_type *gi)
654 {
655 int do_not_record;
656 return hash_rtx (gi->rtx_base, Pmode, &do_not_record, NULL, false);
657 }
658
659 /* Tables of group_info structures, hashed by base value. */
660 static hash_table<invariant_group_base_hasher> *rtx_group_table;
661
662
663 /* Get the GROUP for BASE. Add a new group if it is not there. */
664
665 static group_info_t
666 get_group_info (rtx base)
667 {
668 struct group_info tmp_gi;
669 group_info_t gi;
670 group_info **slot;
671
672 if (base)
673 {
674 /* Find the store_base_info structure for BASE, creating a new one
675 if necessary. */
676 tmp_gi.rtx_base = base;
677 slot = rtx_group_table->find_slot (&tmp_gi, INSERT);
678 gi = (group_info_t) *slot;
679 }
680 else
681 {
682 if (!clear_alias_group)
683 {
684 clear_alias_group = gi =
685 (group_info_t) pool_alloc (rtx_group_info_pool);
686 memset (gi, 0, sizeof (struct group_info));
687 gi->id = rtx_group_next_id++;
688 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
689 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
690 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
691 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
692 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
693 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
694 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
695 gi->process_globally = false;
696 gi->offset_map_size_n = 0;
697 gi->offset_map_size_p = 0;
698 gi->offset_map_n = NULL;
699 gi->offset_map_p = NULL;
700 rtx_group_vec.safe_push (gi);
701 }
702 return clear_alias_group;
703 }
704
705 if (gi == NULL)
706 {
707 *slot = gi = (group_info_t) pool_alloc (rtx_group_info_pool);
708 gi->rtx_base = base;
709 gi->id = rtx_group_next_id++;
710 gi->base_mem = gen_rtx_MEM (BLKmode, base);
711 gi->canon_base_addr = canon_rtx (base);
712 gi->store1_n = BITMAP_ALLOC (&dse_bitmap_obstack);
713 gi->store1_p = BITMAP_ALLOC (&dse_bitmap_obstack);
714 gi->store2_n = BITMAP_ALLOC (&dse_bitmap_obstack);
715 gi->store2_p = BITMAP_ALLOC (&dse_bitmap_obstack);
716 gi->escaped_p = BITMAP_ALLOC (&dse_bitmap_obstack);
717 gi->escaped_n = BITMAP_ALLOC (&dse_bitmap_obstack);
718 gi->group_kill = BITMAP_ALLOC (&dse_bitmap_obstack);
719 gi->process_globally = false;
720 gi->frame_related =
721 (base == frame_pointer_rtx) || (base == hard_frame_pointer_rtx);
722 gi->offset_map_size_n = 0;
723 gi->offset_map_size_p = 0;
724 gi->offset_map_n = NULL;
725 gi->offset_map_p = NULL;
726 rtx_group_vec.safe_push (gi);
727 }
728
729 return gi;
730 }
731
732
733 /* Initialization of data structures. */
734
735 static void
736 dse_step0 (void)
737 {
738 locally_deleted = 0;
739 globally_deleted = 0;
740 spill_deleted = 0;
741
742 bitmap_obstack_initialize (&dse_bitmap_obstack);
743 gcc_obstack_init (&dse_obstack);
744
745 scratch = BITMAP_ALLOC (&reg_obstack);
746 kill_on_calls = BITMAP_ALLOC (&dse_bitmap_obstack);
747
748 rtx_store_info_pool
749 = create_alloc_pool ("rtx_store_info_pool",
750 sizeof (struct store_info), 100);
751 read_info_pool
752 = create_alloc_pool ("read_info_pool",
753 sizeof (struct read_info), 100);
754 insn_info_pool
755 = create_alloc_pool ("insn_info_pool",
756 sizeof (struct insn_info), 100);
757 bb_info_pool
758 = create_alloc_pool ("bb_info_pool",
759 sizeof (struct bb_info), 100);
760 rtx_group_info_pool
761 = create_alloc_pool ("rtx_group_info_pool",
762 sizeof (struct group_info), 100);
763 deferred_change_pool
764 = create_alloc_pool ("deferred_change_pool",
765 sizeof (struct deferred_change), 10);
766
767 rtx_group_table = new hash_table<invariant_group_base_hasher> (11);
768
769 bb_table = XNEWVEC (bb_info_t, last_basic_block_for_fn (cfun));
770 rtx_group_next_id = 0;
771
772 stores_off_frame_dead_at_return = !cfun->stdarg;
773
774 init_alias_analysis ();
775
776 clear_alias_group = NULL;
777 }
778
779
780 \f
781 /*----------------------------------------------------------------------------
782 First step.
783
784 Scan all of the insns. Any random ordering of the blocks is fine.
785 Each block is scanned in forward order to accommodate cselib which
786 is used to remove stores with non-constant bases.
787 ----------------------------------------------------------------------------*/
788
789 /* Delete all of the store_info recs from INSN_INFO. */
790
791 static void
792 free_store_info (insn_info_t insn_info)
793 {
794 store_info_t store_info = insn_info->store_rec;
795 while (store_info)
796 {
797 store_info_t next = store_info->next;
798 if (store_info->is_large)
799 BITMAP_FREE (store_info->positions_needed.large.bmap);
800 if (store_info->cse_base)
801 pool_free (cse_store_info_pool, store_info);
802 else
803 pool_free (rtx_store_info_pool, store_info);
804 store_info = next;
805 }
806
807 insn_info->cannot_delete = true;
808 insn_info->contains_cselib_groups = false;
809 insn_info->store_rec = NULL;
810 }
811
812 typedef struct
813 {
814 rtx first, current;
815 regset fixed_regs_live;
816 bool failure;
817 } note_add_store_info;
818
819 /* Callback for emit_inc_dec_insn_before via note_stores.
820 Check if a register is clobbered which is live afterwards. */
821
822 static void
823 note_add_store (rtx loc, const_rtx expr ATTRIBUTE_UNUSED, void *data)
824 {
825 rtx insn;
826 note_add_store_info *info = (note_add_store_info *) data;
827 int r, n;
828
829 if (!REG_P (loc))
830 return;
831
832 /* If this register is referenced by the current or an earlier insn,
833 that's OK. E.g. this applies to the register that is being incremented
834 with this addition. */
835 for (insn = info->first;
836 insn != NEXT_INSN (info->current);
837 insn = NEXT_INSN (insn))
838 if (reg_referenced_p (loc, PATTERN (insn)))
839 return;
840
841 /* If we come here, we have a clobber of a register that's only OK
842 if that register is not live. If we don't have liveness information
843 available, fail now. */
844 if (!info->fixed_regs_live)
845 {
846 info->failure = true;
847 return;
848 }
849 /* Now check if this is a live fixed register. */
850 r = REGNO (loc);
851 n = hard_regno_nregs[r][GET_MODE (loc)];
852 while (--n >= 0)
853 if (REGNO_REG_SET_P (info->fixed_regs_live, r+n))
854 info->failure = true;
855 }
856
857 /* Callback for for_each_inc_dec that emits an INSN that sets DEST to
858 SRC + SRCOFF before insn ARG. */
859
860 static int
861 emit_inc_dec_insn_before (rtx mem ATTRIBUTE_UNUSED,
862 rtx op ATTRIBUTE_UNUSED,
863 rtx dest, rtx src, rtx srcoff, void *arg)
864 {
865 insn_info_t insn_info = (insn_info_t) arg;
866 rtx insn = insn_info->insn, new_insn, cur;
867 note_add_store_info info;
868
869 /* We can reuse all operands without copying, because we are about
870 to delete the insn that contained it. */
871 if (srcoff)
872 {
873 start_sequence ();
874 emit_insn (gen_add3_insn (dest, src, srcoff));
875 new_insn = get_insns ();
876 end_sequence ();
877 }
878 else
879 new_insn = gen_move_insn (dest, src);
880 info.first = new_insn;
881 info.fixed_regs_live = insn_info->fixed_regs_live;
882 info.failure = false;
883 for (cur = new_insn; cur; cur = NEXT_INSN (cur))
884 {
885 info.current = cur;
886 note_stores (PATTERN (cur), note_add_store, &info);
887 }
888
889 /* If a failure was flagged above, return 1 so that for_each_inc_dec will
890 return it immediately, communicating the failure to its caller. */
891 if (info.failure)
892 return 1;
893
894 emit_insn_before (new_insn, insn);
895
896 return -1;
897 }
898
899 /* Before we delete INSN_INFO->INSN, make sure that the auto inc/dec, if it
900 is there, is split into a separate insn.
901 Return true on success (or if there was nothing to do), false on failure. */
902
903 static bool
904 check_for_inc_dec_1 (insn_info_t insn_info)
905 {
906 rtx_insn *insn = insn_info->insn;
907 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
908 if (note)
909 return for_each_inc_dec (&insn, emit_inc_dec_insn_before, insn_info) == 0;
910 return true;
911 }
912
913
914 /* Entry point for postreload. If you work on reload_cse, or you need this
915 anywhere else, consider if you can provide register liveness information
916 and add a parameter to this function so that it can be passed down in
917 insn_info.fixed_regs_live. */
918 bool
919 check_for_inc_dec (rtx_insn *insn)
920 {
921 struct insn_info insn_info;
922 rtx note;
923
924 insn_info.insn = insn;
925 insn_info.fixed_regs_live = NULL;
926 note = find_reg_note (insn, REG_INC, NULL_RTX);
927 if (note)
928 return for_each_inc_dec (&insn, emit_inc_dec_insn_before, &insn_info) == 0;
929 return true;
930 }
931
932 /* Delete the insn and free all of the fields inside INSN_INFO. */
933
934 static void
935 delete_dead_store_insn (insn_info_t insn_info)
936 {
937 read_info_t read_info;
938
939 if (!dbg_cnt (dse))
940 return;
941
942 if (!check_for_inc_dec_1 (insn_info))
943 return;
944 if (dump_file && (dump_flags & TDF_DETAILS))
945 {
946 fprintf (dump_file, "Locally deleting insn %d ",
947 INSN_UID (insn_info->insn));
948 if (insn_info->store_rec->alias_set)
949 fprintf (dump_file, "alias set %d\n",
950 (int) insn_info->store_rec->alias_set);
951 else
952 fprintf (dump_file, "\n");
953 }
954
955 free_store_info (insn_info);
956 read_info = insn_info->read_rec;
957
958 while (read_info)
959 {
960 read_info_t next = read_info->next;
961 pool_free (read_info_pool, read_info);
962 read_info = next;
963 }
964 insn_info->read_rec = NULL;
965
966 delete_insn (insn_info->insn);
967 locally_deleted++;
968 insn_info->insn = NULL;
969
970 insn_info->wild_read = false;
971 }
972
973 /* Return whether DECL, a local variable, can possibly escape the current
974 function scope. */
975
976 static bool
977 local_variable_can_escape (tree decl)
978 {
979 if (TREE_ADDRESSABLE (decl))
980 return true;
981
982 /* If this is a partitioned variable, we need to consider all the variables
983 in the partition. This is necessary because a store into one of them can
984 be replaced with a store into another and this may not change the outcome
985 of the escape analysis. */
986 if (cfun->gimple_df->decls_to_pointers != NULL)
987 {
988 tree *namep = cfun->gimple_df->decls_to_pointers->get (decl);
989 if (namep)
990 return TREE_ADDRESSABLE (*namep);
991 }
992
993 return false;
994 }
995
996 /* Return whether EXPR can possibly escape the current function scope. */
997
998 static bool
999 can_escape (tree expr)
1000 {
1001 tree base;
1002 if (!expr)
1003 return true;
1004 base = get_base_address (expr);
1005 if (DECL_P (base)
1006 && !may_be_aliased (base)
1007 && !(TREE_CODE (base) == VAR_DECL
1008 && !DECL_EXTERNAL (base)
1009 && !TREE_STATIC (base)
1010 && local_variable_can_escape (base)))
1011 return false;
1012 return true;
1013 }
1014
1015 /* Set the store* bitmaps offset_map_size* fields in GROUP based on
1016 OFFSET and WIDTH. */
1017
1018 static void
1019 set_usage_bits (group_info_t group, HOST_WIDE_INT offset, HOST_WIDE_INT width,
1020 tree expr)
1021 {
1022 HOST_WIDE_INT i;
1023 bool expr_escapes = can_escape (expr);
1024 if (offset > -MAX_OFFSET && offset + width < MAX_OFFSET)
1025 for (i=offset; i<offset+width; i++)
1026 {
1027 bitmap store1;
1028 bitmap store2;
1029 bitmap escaped;
1030 int ai;
1031 if (i < 0)
1032 {
1033 store1 = group->store1_n;
1034 store2 = group->store2_n;
1035 escaped = group->escaped_n;
1036 ai = -i;
1037 }
1038 else
1039 {
1040 store1 = group->store1_p;
1041 store2 = group->store2_p;
1042 escaped = group->escaped_p;
1043 ai = i;
1044 }
1045
1046 if (!bitmap_set_bit (store1, ai))
1047 bitmap_set_bit (store2, ai);
1048 else
1049 {
1050 if (i < 0)
1051 {
1052 if (group->offset_map_size_n < ai)
1053 group->offset_map_size_n = ai;
1054 }
1055 else
1056 {
1057 if (group->offset_map_size_p < ai)
1058 group->offset_map_size_p = ai;
1059 }
1060 }
1061 if (expr_escapes)
1062 bitmap_set_bit (escaped, ai);
1063 }
1064 }
1065
1066 static void
1067 reset_active_stores (void)
1068 {
1069 active_local_stores = NULL;
1070 active_local_stores_len = 0;
1071 }
1072
1073 /* Free all READ_REC of the LAST_INSN of BB_INFO. */
1074
1075 static void
1076 free_read_records (bb_info_t bb_info)
1077 {
1078 insn_info_t insn_info = bb_info->last_insn;
1079 read_info_t *ptr = &insn_info->read_rec;
1080 while (*ptr)
1081 {
1082 read_info_t next = (*ptr)->next;
1083 if ((*ptr)->alias_set == 0)
1084 {
1085 pool_free (read_info_pool, *ptr);
1086 *ptr = next;
1087 }
1088 else
1089 ptr = &(*ptr)->next;
1090 }
1091 }
1092
1093 /* Set the BB_INFO so that the last insn is marked as a wild read. */
1094
1095 static void
1096 add_wild_read (bb_info_t bb_info)
1097 {
1098 insn_info_t insn_info = bb_info->last_insn;
1099 insn_info->wild_read = true;
1100 free_read_records (bb_info);
1101 reset_active_stores ();
1102 }
1103
1104 /* Set the BB_INFO so that the last insn is marked as a wild read of
1105 non-frame locations. */
1106
1107 static void
1108 add_non_frame_wild_read (bb_info_t bb_info)
1109 {
1110 insn_info_t insn_info = bb_info->last_insn;
1111 insn_info->non_frame_wild_read = true;
1112 free_read_records (bb_info);
1113 reset_active_stores ();
1114 }
1115
1116 /* Return true if X is a constant or one of the registers that behave
1117 as a constant over the life of a function. This is equivalent to
1118 !rtx_varies_p for memory addresses. */
1119
1120 static bool
1121 const_or_frame_p (rtx x)
1122 {
1123 if (CONSTANT_P (x))
1124 return true;
1125
1126 if (GET_CODE (x) == REG)
1127 {
1128 /* Note that we have to test for the actual rtx used for the frame
1129 and arg pointers and not just the register number in case we have
1130 eliminated the frame and/or arg pointer and are using it
1131 for pseudos. */
1132 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
1133 /* The arg pointer varies if it is not a fixed register. */
1134 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
1135 || x == pic_offset_table_rtx)
1136 return true;
1137 return false;
1138 }
1139
1140 return false;
1141 }
1142
1143 /* Take all reasonable action to put the address of MEM into the form
1144 that we can do analysis on.
1145
1146 The gold standard is to get the address into the form: address +
1147 OFFSET where address is something that rtx_varies_p considers a
1148 constant. When we can get the address in this form, we can do
1149 global analysis on it. Note that for constant bases, address is
1150 not actually returned, only the group_id. The address can be
1151 obtained from that.
1152
1153 If that fails, we try cselib to get a value we can at least use
1154 locally. If that fails we return false.
1155
1156 The GROUP_ID is set to -1 for cselib bases and the index of the
1157 group for non_varying bases.
1158
1159 FOR_READ is true if this is a mem read and false if not. */
1160
1161 static bool
1162 canon_address (rtx mem,
1163 alias_set_type *alias_set_out,
1164 int *group_id,
1165 HOST_WIDE_INT *offset,
1166 cselib_val **base)
1167 {
1168 enum machine_mode address_mode = get_address_mode (mem);
1169 rtx mem_address = XEXP (mem, 0);
1170 rtx expanded_address, address;
1171 int expanded;
1172
1173 *alias_set_out = 0;
1174
1175 cselib_lookup (mem_address, address_mode, 1, GET_MODE (mem));
1176
1177 if (dump_file && (dump_flags & TDF_DETAILS))
1178 {
1179 fprintf (dump_file, " mem: ");
1180 print_inline_rtx (dump_file, mem_address, 0);
1181 fprintf (dump_file, "\n");
1182 }
1183
1184 /* First see if just canon_rtx (mem_address) is const or frame,
1185 if not, try cselib_expand_value_rtx and call canon_rtx on that. */
1186 address = NULL_RTX;
1187 for (expanded = 0; expanded < 2; expanded++)
1188 {
1189 if (expanded)
1190 {
1191 /* Use cselib to replace all of the reg references with the full
1192 expression. This will take care of the case where we have
1193
1194 r_x = base + offset;
1195 val = *r_x;
1196
1197 by making it into
1198
1199 val = *(base + offset); */
1200
1201 expanded_address = cselib_expand_value_rtx (mem_address,
1202 scratch, 5);
1203
1204 /* If this fails, just go with the address from first
1205 iteration. */
1206 if (!expanded_address)
1207 break;
1208 }
1209 else
1210 expanded_address = mem_address;
1211
1212 /* Split the address into canonical BASE + OFFSET terms. */
1213 address = canon_rtx (expanded_address);
1214
1215 *offset = 0;
1216
1217 if (dump_file && (dump_flags & TDF_DETAILS))
1218 {
1219 if (expanded)
1220 {
1221 fprintf (dump_file, "\n after cselib_expand address: ");
1222 print_inline_rtx (dump_file, expanded_address, 0);
1223 fprintf (dump_file, "\n");
1224 }
1225
1226 fprintf (dump_file, "\n after canon_rtx address: ");
1227 print_inline_rtx (dump_file, address, 0);
1228 fprintf (dump_file, "\n");
1229 }
1230
1231 if (GET_CODE (address) == CONST)
1232 address = XEXP (address, 0);
1233
1234 if (GET_CODE (address) == PLUS
1235 && CONST_INT_P (XEXP (address, 1)))
1236 {
1237 *offset = INTVAL (XEXP (address, 1));
1238 address = XEXP (address, 0);
1239 }
1240
1241 if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (mem))
1242 && const_or_frame_p (address))
1243 {
1244 group_info_t group = get_group_info (address);
1245
1246 if (dump_file && (dump_flags & TDF_DETAILS))
1247 fprintf (dump_file, " gid=%d offset=%d \n",
1248 group->id, (int)*offset);
1249 *base = NULL;
1250 *group_id = group->id;
1251 return true;
1252 }
1253 }
1254
1255 *base = cselib_lookup (address, address_mode, true, GET_MODE (mem));
1256 *group_id = -1;
1257
1258 if (*base == NULL)
1259 {
1260 if (dump_file && (dump_flags & TDF_DETAILS))
1261 fprintf (dump_file, " no cselib val - should be a wild read.\n");
1262 return false;
1263 }
1264 if (dump_file && (dump_flags & TDF_DETAILS))
1265 fprintf (dump_file, " varying cselib base=%u:%u offset = %d\n",
1266 (*base)->uid, (*base)->hash, (int)*offset);
1267 return true;
1268 }
1269
1270
1271 /* Clear the rhs field from the active_local_stores array. */
1272
1273 static void
1274 clear_rhs_from_active_local_stores (void)
1275 {
1276 insn_info_t ptr = active_local_stores;
1277
1278 while (ptr)
1279 {
1280 store_info_t store_info = ptr->store_rec;
1281 /* Skip the clobbers. */
1282 while (!store_info->is_set)
1283 store_info = store_info->next;
1284
1285 store_info->rhs = NULL;
1286 store_info->const_rhs = NULL;
1287
1288 ptr = ptr->next_local_store;
1289 }
1290 }
1291
1292
1293 /* Mark byte POS bytes from the beginning of store S_INFO as unneeded. */
1294
1295 static inline void
1296 set_position_unneeded (store_info_t s_info, int pos)
1297 {
1298 if (__builtin_expect (s_info->is_large, false))
1299 {
1300 if (bitmap_set_bit (s_info->positions_needed.large.bmap, pos))
1301 s_info->positions_needed.large.count++;
1302 }
1303 else
1304 s_info->positions_needed.small_bitmask
1305 &= ~(((unsigned HOST_WIDE_INT) 1) << pos);
1306 }
1307
1308 /* Mark the whole store S_INFO as unneeded. */
1309
1310 static inline void
1311 set_all_positions_unneeded (store_info_t s_info)
1312 {
1313 if (__builtin_expect (s_info->is_large, false))
1314 {
1315 int pos, end = s_info->end - s_info->begin;
1316 for (pos = 0; pos < end; pos++)
1317 bitmap_set_bit (s_info->positions_needed.large.bmap, pos);
1318 s_info->positions_needed.large.count = end;
1319 }
1320 else
1321 s_info->positions_needed.small_bitmask = (unsigned HOST_WIDE_INT) 0;
1322 }
1323
1324 /* Return TRUE if any bytes from S_INFO store are needed. */
1325
1326 static inline bool
1327 any_positions_needed_p (store_info_t s_info)
1328 {
1329 if (__builtin_expect (s_info->is_large, false))
1330 return (s_info->positions_needed.large.count
1331 < s_info->end - s_info->begin);
1332 else
1333 return (s_info->positions_needed.small_bitmask
1334 != (unsigned HOST_WIDE_INT) 0);
1335 }
1336
1337 /* Return TRUE if all bytes START through START+WIDTH-1 from S_INFO
1338 store are needed. */
1339
1340 static inline bool
1341 all_positions_needed_p (store_info_t s_info, int start, int width)
1342 {
1343 if (__builtin_expect (s_info->is_large, false))
1344 {
1345 int end = start + width;
1346 while (start < end)
1347 if (bitmap_bit_p (s_info->positions_needed.large.bmap, start++))
1348 return false;
1349 return true;
1350 }
1351 else
1352 {
1353 unsigned HOST_WIDE_INT mask = lowpart_bitmask (width) << start;
1354 return (s_info->positions_needed.small_bitmask & mask) == mask;
1355 }
1356 }
1357
1358
1359 static rtx get_stored_val (store_info_t, enum machine_mode, HOST_WIDE_INT,
1360 HOST_WIDE_INT, basic_block, bool);
1361
1362
1363 /* BODY is an instruction pattern that belongs to INSN. Return 1 if
1364 there is a candidate store, after adding it to the appropriate
1365 local store group if so. */
1366
1367 static int
1368 record_store (rtx body, bb_info_t bb_info)
1369 {
1370 rtx mem, rhs, const_rhs, mem_addr;
1371 HOST_WIDE_INT offset = 0;
1372 HOST_WIDE_INT width = 0;
1373 alias_set_type spill_alias_set;
1374 insn_info_t insn_info = bb_info->last_insn;
1375 store_info_t store_info = NULL;
1376 int group_id;
1377 cselib_val *base = NULL;
1378 insn_info_t ptr, last, redundant_reason;
1379 bool store_is_unused;
1380
1381 if (GET_CODE (body) != SET && GET_CODE (body) != CLOBBER)
1382 return 0;
1383
1384 mem = SET_DEST (body);
1385
1386 /* If this is not used, then this cannot be used to keep the insn
1387 from being deleted. On the other hand, it does provide something
1388 that can be used to prove that another store is dead. */
1389 store_is_unused
1390 = (find_reg_note (insn_info->insn, REG_UNUSED, mem) != NULL);
1391
1392 /* Check whether that value is a suitable memory location. */
1393 if (!MEM_P (mem))
1394 {
1395 /* If the set or clobber is unused, then it does not effect our
1396 ability to get rid of the entire insn. */
1397 if (!store_is_unused)
1398 insn_info->cannot_delete = true;
1399 return 0;
1400 }
1401
1402 /* At this point we know mem is a mem. */
1403 if (GET_MODE (mem) == BLKmode)
1404 {
1405 if (GET_CODE (XEXP (mem, 0)) == SCRATCH)
1406 {
1407 if (dump_file && (dump_flags & TDF_DETAILS))
1408 fprintf (dump_file, " adding wild read for (clobber (mem:BLK (scratch))\n");
1409 add_wild_read (bb_info);
1410 insn_info->cannot_delete = true;
1411 return 0;
1412 }
1413 /* Handle (set (mem:BLK (addr) [... S36 ...]) (const_int 0))
1414 as memset (addr, 0, 36); */
1415 else if (!MEM_SIZE_KNOWN_P (mem)
1416 || MEM_SIZE (mem) <= 0
1417 || MEM_SIZE (mem) > MAX_OFFSET
1418 || GET_CODE (body) != SET
1419 || !CONST_INT_P (SET_SRC (body)))
1420 {
1421 if (!store_is_unused)
1422 {
1423 /* If the set or clobber is unused, then it does not effect our
1424 ability to get rid of the entire insn. */
1425 insn_info->cannot_delete = true;
1426 clear_rhs_from_active_local_stores ();
1427 }
1428 return 0;
1429 }
1430 }
1431
1432 /* We can still process a volatile mem, we just cannot delete it. */
1433 if (MEM_VOLATILE_P (mem))
1434 insn_info->cannot_delete = true;
1435
1436 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
1437 {
1438 clear_rhs_from_active_local_stores ();
1439 return 0;
1440 }
1441
1442 if (GET_MODE (mem) == BLKmode)
1443 width = MEM_SIZE (mem);
1444 else
1445 width = GET_MODE_SIZE (GET_MODE (mem));
1446
1447 if (spill_alias_set)
1448 {
1449 bitmap store1 = clear_alias_group->store1_p;
1450 bitmap store2 = clear_alias_group->store2_p;
1451
1452 gcc_assert (GET_MODE (mem) != BLKmode);
1453
1454 if (!bitmap_set_bit (store1, spill_alias_set))
1455 bitmap_set_bit (store2, spill_alias_set);
1456
1457 if (clear_alias_group->offset_map_size_p < spill_alias_set)
1458 clear_alias_group->offset_map_size_p = spill_alias_set;
1459
1460 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1461
1462 if (dump_file && (dump_flags & TDF_DETAILS))
1463 fprintf (dump_file, " processing spill store %d(%s)\n",
1464 (int) spill_alias_set, GET_MODE_NAME (GET_MODE (mem)));
1465 }
1466 else if (group_id >= 0)
1467 {
1468 /* In the restrictive case where the base is a constant or the
1469 frame pointer we can do global analysis. */
1470
1471 group_info_t group
1472 = rtx_group_vec[group_id];
1473 tree expr = MEM_EXPR (mem);
1474
1475 store_info = (store_info_t) pool_alloc (rtx_store_info_pool);
1476 set_usage_bits (group, offset, width, expr);
1477
1478 if (dump_file && (dump_flags & TDF_DETAILS))
1479 fprintf (dump_file, " processing const base store gid=%d[%d..%d)\n",
1480 group_id, (int)offset, (int)(offset+width));
1481 }
1482 else
1483 {
1484 if (may_be_sp_based_p (XEXP (mem, 0)))
1485 insn_info->stack_pointer_based = true;
1486 insn_info->contains_cselib_groups = true;
1487
1488 store_info = (store_info_t) pool_alloc (cse_store_info_pool);
1489 group_id = -1;
1490
1491 if (dump_file && (dump_flags & TDF_DETAILS))
1492 fprintf (dump_file, " processing cselib store [%d..%d)\n",
1493 (int)offset, (int)(offset+width));
1494 }
1495
1496 const_rhs = rhs = NULL_RTX;
1497 if (GET_CODE (body) == SET
1498 /* No place to keep the value after ra. */
1499 && !reload_completed
1500 && (REG_P (SET_SRC (body))
1501 || GET_CODE (SET_SRC (body)) == SUBREG
1502 || CONSTANT_P (SET_SRC (body)))
1503 && !MEM_VOLATILE_P (mem)
1504 /* Sometimes the store and reload is used for truncation and
1505 rounding. */
1506 && !(FLOAT_MODE_P (GET_MODE (mem)) && (flag_float_store)))
1507 {
1508 rhs = SET_SRC (body);
1509 if (CONSTANT_P (rhs))
1510 const_rhs = rhs;
1511 else if (body == PATTERN (insn_info->insn))
1512 {
1513 rtx tem = find_reg_note (insn_info->insn, REG_EQUAL, NULL_RTX);
1514 if (tem && CONSTANT_P (XEXP (tem, 0)))
1515 const_rhs = XEXP (tem, 0);
1516 }
1517 if (const_rhs == NULL_RTX && REG_P (rhs))
1518 {
1519 rtx tem = cselib_expand_value_rtx (rhs, scratch, 5);
1520
1521 if (tem && CONSTANT_P (tem))
1522 const_rhs = tem;
1523 }
1524 }
1525
1526 /* Check to see if this stores causes some other stores to be
1527 dead. */
1528 ptr = active_local_stores;
1529 last = NULL;
1530 redundant_reason = NULL;
1531 mem = canon_rtx (mem);
1532 /* For alias_set != 0 canon_true_dependence should be never called. */
1533 if (spill_alias_set)
1534 mem_addr = NULL_RTX;
1535 else
1536 {
1537 if (group_id < 0)
1538 mem_addr = base->val_rtx;
1539 else
1540 {
1541 group_info_t group
1542 = rtx_group_vec[group_id];
1543 mem_addr = group->canon_base_addr;
1544 }
1545 if (offset)
1546 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
1547 }
1548
1549 while (ptr)
1550 {
1551 insn_info_t next = ptr->next_local_store;
1552 store_info_t s_info = ptr->store_rec;
1553 bool del = true;
1554
1555 /* Skip the clobbers. We delete the active insn if this insn
1556 shadows the set. To have been put on the active list, it
1557 has exactly on set. */
1558 while (!s_info->is_set)
1559 s_info = s_info->next;
1560
1561 if (s_info->alias_set != spill_alias_set)
1562 del = false;
1563 else if (s_info->alias_set)
1564 {
1565 struct clear_alias_mode_holder *entry
1566 = clear_alias_set_lookup (s_info->alias_set);
1567 /* Generally, spills cannot be processed if and of the
1568 references to the slot have a different mode. But if
1569 we are in the same block and mode is exactly the same
1570 between this store and one before in the same block,
1571 we can still delete it. */
1572 if ((GET_MODE (mem) == GET_MODE (s_info->mem))
1573 && (GET_MODE (mem) == entry->mode))
1574 {
1575 del = true;
1576 set_all_positions_unneeded (s_info);
1577 }
1578 if (dump_file && (dump_flags & TDF_DETAILS))
1579 fprintf (dump_file, " trying spill store in insn=%d alias_set=%d\n",
1580 INSN_UID (ptr->insn), (int) s_info->alias_set);
1581 }
1582 else if ((s_info->group_id == group_id)
1583 && (s_info->cse_base == base))
1584 {
1585 HOST_WIDE_INT i;
1586 if (dump_file && (dump_flags & TDF_DETAILS))
1587 fprintf (dump_file, " trying store in insn=%d gid=%d[%d..%d)\n",
1588 INSN_UID (ptr->insn), s_info->group_id,
1589 (int)s_info->begin, (int)s_info->end);
1590
1591 /* Even if PTR won't be eliminated as unneeded, if both
1592 PTR and this insn store the same constant value, we might
1593 eliminate this insn instead. */
1594 if (s_info->const_rhs
1595 && const_rhs
1596 && offset >= s_info->begin
1597 && offset + width <= s_info->end
1598 && all_positions_needed_p (s_info, offset - s_info->begin,
1599 width))
1600 {
1601 if (GET_MODE (mem) == BLKmode)
1602 {
1603 if (GET_MODE (s_info->mem) == BLKmode
1604 && s_info->const_rhs == const_rhs)
1605 redundant_reason = ptr;
1606 }
1607 else if (s_info->const_rhs == const0_rtx
1608 && const_rhs == const0_rtx)
1609 redundant_reason = ptr;
1610 else
1611 {
1612 rtx val;
1613 start_sequence ();
1614 val = get_stored_val (s_info, GET_MODE (mem),
1615 offset, offset + width,
1616 BLOCK_FOR_INSN (insn_info->insn),
1617 true);
1618 if (get_insns () != NULL)
1619 val = NULL_RTX;
1620 end_sequence ();
1621 if (val && rtx_equal_p (val, const_rhs))
1622 redundant_reason = ptr;
1623 }
1624 }
1625
1626 for (i = MAX (offset, s_info->begin);
1627 i < offset + width && i < s_info->end;
1628 i++)
1629 set_position_unneeded (s_info, i - s_info->begin);
1630 }
1631 else if (s_info->rhs)
1632 /* Need to see if it is possible for this store to overwrite
1633 the value of store_info. If it is, set the rhs to NULL to
1634 keep it from being used to remove a load. */
1635 {
1636 if (canon_true_dependence (s_info->mem,
1637 GET_MODE (s_info->mem),
1638 s_info->mem_addr,
1639 mem, mem_addr))
1640 {
1641 s_info->rhs = NULL;
1642 s_info->const_rhs = NULL;
1643 }
1644 }
1645
1646 /* An insn can be deleted if every position of every one of
1647 its s_infos is zero. */
1648 if (any_positions_needed_p (s_info))
1649 del = false;
1650
1651 if (del)
1652 {
1653 insn_info_t insn_to_delete = ptr;
1654
1655 active_local_stores_len--;
1656 if (last)
1657 last->next_local_store = ptr->next_local_store;
1658 else
1659 active_local_stores = ptr->next_local_store;
1660
1661 if (!insn_to_delete->cannot_delete)
1662 delete_dead_store_insn (insn_to_delete);
1663 }
1664 else
1665 last = ptr;
1666
1667 ptr = next;
1668 }
1669
1670 /* Finish filling in the store_info. */
1671 store_info->next = insn_info->store_rec;
1672 insn_info->store_rec = store_info;
1673 store_info->mem = mem;
1674 store_info->alias_set = spill_alias_set;
1675 store_info->mem_addr = mem_addr;
1676 store_info->cse_base = base;
1677 if (width > HOST_BITS_PER_WIDE_INT)
1678 {
1679 store_info->is_large = true;
1680 store_info->positions_needed.large.count = 0;
1681 store_info->positions_needed.large.bmap = BITMAP_ALLOC (&dse_bitmap_obstack);
1682 }
1683 else
1684 {
1685 store_info->is_large = false;
1686 store_info->positions_needed.small_bitmask = lowpart_bitmask (width);
1687 }
1688 store_info->group_id = group_id;
1689 store_info->begin = offset;
1690 store_info->end = offset + width;
1691 store_info->is_set = GET_CODE (body) == SET;
1692 store_info->rhs = rhs;
1693 store_info->const_rhs = const_rhs;
1694 store_info->redundant_reason = redundant_reason;
1695
1696 /* If this is a clobber, we return 0. We will only be able to
1697 delete this insn if there is only one store USED store, but we
1698 can use the clobber to delete other stores earlier. */
1699 return store_info->is_set ? 1 : 0;
1700 }
1701
1702
1703 static void
1704 dump_insn_info (const char * start, insn_info_t insn_info)
1705 {
1706 fprintf (dump_file, "%s insn=%d %s\n", start,
1707 INSN_UID (insn_info->insn),
1708 insn_info->store_rec ? "has store" : "naked");
1709 }
1710
1711
1712 /* If the modes are different and the value's source and target do not
1713 line up, we need to extract the value from lower part of the rhs of
1714 the store, shift it, and then put it into a form that can be shoved
1715 into the read_insn. This function generates a right SHIFT of a
1716 value that is at least ACCESS_SIZE bytes wide of READ_MODE. The
1717 shift sequence is returned or NULL if we failed to find a
1718 shift. */
1719
1720 static rtx
1721 find_shift_sequence (int access_size,
1722 store_info_t store_info,
1723 enum machine_mode read_mode,
1724 int shift, bool speed, bool require_cst)
1725 {
1726 enum machine_mode store_mode = GET_MODE (store_info->mem);
1727 enum machine_mode new_mode;
1728 rtx read_reg = NULL;
1729
1730 /* Some machines like the x86 have shift insns for each size of
1731 operand. Other machines like the ppc or the ia-64 may only have
1732 shift insns that shift values within 32 or 64 bit registers.
1733 This loop tries to find the smallest shift insn that will right
1734 justify the value we want to read but is available in one insn on
1735 the machine. */
1736
1737 for (new_mode = smallest_mode_for_size (access_size * BITS_PER_UNIT,
1738 MODE_INT);
1739 GET_MODE_BITSIZE (new_mode) <= BITS_PER_WORD;
1740 new_mode = GET_MODE_WIDER_MODE (new_mode))
1741 {
1742 rtx target, new_reg, shift_seq, insn, new_lhs;
1743 int cost;
1744
1745 /* If a constant was stored into memory, try to simplify it here,
1746 otherwise the cost of the shift might preclude this optimization
1747 e.g. at -Os, even when no actual shift will be needed. */
1748 if (store_info->const_rhs)
1749 {
1750 unsigned int byte = subreg_lowpart_offset (new_mode, store_mode);
1751 rtx ret = simplify_subreg (new_mode, store_info->const_rhs,
1752 store_mode, byte);
1753 if (ret && CONSTANT_P (ret))
1754 {
1755 ret = simplify_const_binary_operation (LSHIFTRT, new_mode,
1756 ret, GEN_INT (shift));
1757 if (ret && CONSTANT_P (ret))
1758 {
1759 byte = subreg_lowpart_offset (read_mode, new_mode);
1760 ret = simplify_subreg (read_mode, ret, new_mode, byte);
1761 if (ret && CONSTANT_P (ret)
1762 && set_src_cost (ret, speed) <= COSTS_N_INSNS (1))
1763 return ret;
1764 }
1765 }
1766 }
1767
1768 if (require_cst)
1769 return NULL_RTX;
1770
1771 /* Try a wider mode if truncating the store mode to NEW_MODE
1772 requires a real instruction. */
1773 if (GET_MODE_BITSIZE (new_mode) < GET_MODE_BITSIZE (store_mode)
1774 && !TRULY_NOOP_TRUNCATION_MODES_P (new_mode, store_mode))
1775 continue;
1776
1777 /* Also try a wider mode if the necessary punning is either not
1778 desirable or not possible. */
1779 if (!CONSTANT_P (store_info->rhs)
1780 && !MODES_TIEABLE_P (new_mode, store_mode))
1781 continue;
1782
1783 new_reg = gen_reg_rtx (new_mode);
1784
1785 start_sequence ();
1786
1787 /* In theory we could also check for an ashr. Ian Taylor knows
1788 of one dsp where the cost of these two was not the same. But
1789 this really is a rare case anyway. */
1790 target = expand_binop (new_mode, lshr_optab, new_reg,
1791 GEN_INT (shift), new_reg, 1, OPTAB_DIRECT);
1792
1793 shift_seq = get_insns ();
1794 end_sequence ();
1795
1796 if (target != new_reg || shift_seq == NULL)
1797 continue;
1798
1799 cost = 0;
1800 for (insn = shift_seq; insn != NULL_RTX; insn = NEXT_INSN (insn))
1801 if (INSN_P (insn))
1802 cost += insn_rtx_cost (PATTERN (insn), speed);
1803
1804 /* The computation up to here is essentially independent
1805 of the arguments and could be precomputed. It may
1806 not be worth doing so. We could precompute if
1807 worthwhile or at least cache the results. The result
1808 technically depends on both SHIFT and ACCESS_SIZE,
1809 but in practice the answer will depend only on ACCESS_SIZE. */
1810
1811 if (cost > COSTS_N_INSNS (1))
1812 continue;
1813
1814 new_lhs = extract_low_bits (new_mode, store_mode,
1815 copy_rtx (store_info->rhs));
1816 if (new_lhs == NULL_RTX)
1817 continue;
1818
1819 /* We found an acceptable shift. Generate a move to
1820 take the value from the store and put it into the
1821 shift pseudo, then shift it, then generate another
1822 move to put in into the target of the read. */
1823 emit_move_insn (new_reg, new_lhs);
1824 emit_insn (shift_seq);
1825 read_reg = extract_low_bits (read_mode, new_mode, new_reg);
1826 break;
1827 }
1828
1829 return read_reg;
1830 }
1831
1832
1833 /* Call back for note_stores to find the hard regs set or clobbered by
1834 insn. Data is a bitmap of the hardregs set so far. */
1835
1836 static void
1837 look_for_hardregs (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
1838 {
1839 bitmap regs_set = (bitmap) data;
1840
1841 if (REG_P (x)
1842 && HARD_REGISTER_P (x))
1843 {
1844 unsigned int regno = REGNO (x);
1845 bitmap_set_range (regs_set, regno,
1846 hard_regno_nregs[regno][GET_MODE (x)]);
1847 }
1848 }
1849
1850 /* Helper function for replace_read and record_store.
1851 Attempt to return a value stored in STORE_INFO, from READ_BEGIN
1852 to one before READ_END bytes read in READ_MODE. Return NULL
1853 if not successful. If REQUIRE_CST is true, return always constant. */
1854
1855 static rtx
1856 get_stored_val (store_info_t store_info, enum machine_mode read_mode,
1857 HOST_WIDE_INT read_begin, HOST_WIDE_INT read_end,
1858 basic_block bb, bool require_cst)
1859 {
1860 enum machine_mode store_mode = GET_MODE (store_info->mem);
1861 int shift;
1862 int access_size; /* In bytes. */
1863 rtx read_reg;
1864
1865 /* To get here the read is within the boundaries of the write so
1866 shift will never be negative. Start out with the shift being in
1867 bytes. */
1868 if (store_mode == BLKmode)
1869 shift = 0;
1870 else if (BYTES_BIG_ENDIAN)
1871 shift = store_info->end - read_end;
1872 else
1873 shift = read_begin - store_info->begin;
1874
1875 access_size = shift + GET_MODE_SIZE (read_mode);
1876
1877 /* From now on it is bits. */
1878 shift *= BITS_PER_UNIT;
1879
1880 if (shift)
1881 read_reg = find_shift_sequence (access_size, store_info, read_mode, shift,
1882 optimize_bb_for_speed_p (bb),
1883 require_cst);
1884 else if (store_mode == BLKmode)
1885 {
1886 /* The store is a memset (addr, const_val, const_size). */
1887 gcc_assert (CONST_INT_P (store_info->rhs));
1888 store_mode = int_mode_for_mode (read_mode);
1889 if (store_mode == BLKmode)
1890 read_reg = NULL_RTX;
1891 else if (store_info->rhs == const0_rtx)
1892 read_reg = extract_low_bits (read_mode, store_mode, const0_rtx);
1893 else if (GET_MODE_BITSIZE (store_mode) > HOST_BITS_PER_WIDE_INT
1894 || BITS_PER_UNIT >= HOST_BITS_PER_WIDE_INT)
1895 read_reg = NULL_RTX;
1896 else
1897 {
1898 unsigned HOST_WIDE_INT c
1899 = INTVAL (store_info->rhs)
1900 & (((HOST_WIDE_INT) 1 << BITS_PER_UNIT) - 1);
1901 int shift = BITS_PER_UNIT;
1902 while (shift < HOST_BITS_PER_WIDE_INT)
1903 {
1904 c |= (c << shift);
1905 shift <<= 1;
1906 }
1907 read_reg = gen_int_mode (c, store_mode);
1908 read_reg = extract_low_bits (read_mode, store_mode, read_reg);
1909 }
1910 }
1911 else if (store_info->const_rhs
1912 && (require_cst
1913 || GET_MODE_CLASS (read_mode) != GET_MODE_CLASS (store_mode)))
1914 read_reg = extract_low_bits (read_mode, store_mode,
1915 copy_rtx (store_info->const_rhs));
1916 else
1917 read_reg = extract_low_bits (read_mode, store_mode,
1918 copy_rtx (store_info->rhs));
1919 if (require_cst && read_reg && !CONSTANT_P (read_reg))
1920 read_reg = NULL_RTX;
1921 return read_reg;
1922 }
1923
1924 /* Take a sequence of:
1925 A <- r1
1926 ...
1927 ... <- A
1928
1929 and change it into
1930 r2 <- r1
1931 A <- r1
1932 ...
1933 ... <- r2
1934
1935 or
1936
1937 r3 <- extract (r1)
1938 r3 <- r3 >> shift
1939 r2 <- extract (r3)
1940 ... <- r2
1941
1942 or
1943
1944 r2 <- extract (r1)
1945 ... <- r2
1946
1947 Depending on the alignment and the mode of the store and
1948 subsequent load.
1949
1950
1951 The STORE_INFO and STORE_INSN are for the store and READ_INFO
1952 and READ_INSN are for the read. Return true if the replacement
1953 went ok. */
1954
1955 static bool
1956 replace_read (store_info_t store_info, insn_info_t store_insn,
1957 read_info_t read_info, insn_info_t read_insn, rtx *loc,
1958 bitmap regs_live)
1959 {
1960 enum machine_mode store_mode = GET_MODE (store_info->mem);
1961 enum machine_mode read_mode = GET_MODE (read_info->mem);
1962 rtx insns, this_insn, read_reg;
1963 basic_block bb;
1964
1965 if (!dbg_cnt (dse))
1966 return false;
1967
1968 /* Create a sequence of instructions to set up the read register.
1969 This sequence goes immediately before the store and its result
1970 is read by the load.
1971
1972 We need to keep this in perspective. We are replacing a read
1973 with a sequence of insns, but the read will almost certainly be
1974 in cache, so it is not going to be an expensive one. Thus, we
1975 are not willing to do a multi insn shift or worse a subroutine
1976 call to get rid of the read. */
1977 if (dump_file && (dump_flags & TDF_DETAILS))
1978 fprintf (dump_file, "trying to replace %smode load in insn %d"
1979 " from %smode store in insn %d\n",
1980 GET_MODE_NAME (read_mode), INSN_UID (read_insn->insn),
1981 GET_MODE_NAME (store_mode), INSN_UID (store_insn->insn));
1982 start_sequence ();
1983 bb = BLOCK_FOR_INSN (read_insn->insn);
1984 read_reg = get_stored_val (store_info,
1985 read_mode, read_info->begin, read_info->end,
1986 bb, false);
1987 if (read_reg == NULL_RTX)
1988 {
1989 end_sequence ();
1990 if (dump_file && (dump_flags & TDF_DETAILS))
1991 fprintf (dump_file, " -- could not extract bits of stored value\n");
1992 return false;
1993 }
1994 /* Force the value into a new register so that it won't be clobbered
1995 between the store and the load. */
1996 read_reg = copy_to_mode_reg (read_mode, read_reg);
1997 insns = get_insns ();
1998 end_sequence ();
1999
2000 if (insns != NULL_RTX)
2001 {
2002 /* Now we have to scan the set of new instructions to see if the
2003 sequence contains and sets of hardregs that happened to be
2004 live at this point. For instance, this can happen if one of
2005 the insns sets the CC and the CC happened to be live at that
2006 point. This does occasionally happen, see PR 37922. */
2007 bitmap regs_set = BITMAP_ALLOC (&reg_obstack);
2008
2009 for (this_insn = insns; this_insn != NULL_RTX; this_insn = NEXT_INSN (this_insn))
2010 note_stores (PATTERN (this_insn), look_for_hardregs, regs_set);
2011
2012 bitmap_and_into (regs_set, regs_live);
2013 if (!bitmap_empty_p (regs_set))
2014 {
2015 if (dump_file && (dump_flags & TDF_DETAILS))
2016 {
2017 fprintf (dump_file,
2018 "abandoning replacement because sequence clobbers live hardregs:");
2019 df_print_regset (dump_file, regs_set);
2020 }
2021
2022 BITMAP_FREE (regs_set);
2023 return false;
2024 }
2025 BITMAP_FREE (regs_set);
2026 }
2027
2028 if (validate_change (read_insn->insn, loc, read_reg, 0))
2029 {
2030 deferred_change_t deferred_change =
2031 (deferred_change_t) pool_alloc (deferred_change_pool);
2032
2033 /* Insert this right before the store insn where it will be safe
2034 from later insns that might change it before the read. */
2035 emit_insn_before (insns, store_insn->insn);
2036
2037 /* And now for the kludge part: cselib croaks if you just
2038 return at this point. There are two reasons for this:
2039
2040 1) Cselib has an idea of how many pseudos there are and
2041 that does not include the new ones we just added.
2042
2043 2) Cselib does not know about the move insn we added
2044 above the store_info, and there is no way to tell it
2045 about it, because it has "moved on".
2046
2047 Problem (1) is fixable with a certain amount of engineering.
2048 Problem (2) is requires starting the bb from scratch. This
2049 could be expensive.
2050
2051 So we are just going to have to lie. The move/extraction
2052 insns are not really an issue, cselib did not see them. But
2053 the use of the new pseudo read_insn is a real problem because
2054 cselib has not scanned this insn. The way that we solve this
2055 problem is that we are just going to put the mem back for now
2056 and when we are finished with the block, we undo this. We
2057 keep a table of mems to get rid of. At the end of the basic
2058 block we can put them back. */
2059
2060 *loc = read_info->mem;
2061 deferred_change->next = deferred_change_list;
2062 deferred_change_list = deferred_change;
2063 deferred_change->loc = loc;
2064 deferred_change->reg = read_reg;
2065
2066 /* Get rid of the read_info, from the point of view of the
2067 rest of dse, play like this read never happened. */
2068 read_insn->read_rec = read_info->next;
2069 pool_free (read_info_pool, read_info);
2070 if (dump_file && (dump_flags & TDF_DETAILS))
2071 {
2072 fprintf (dump_file, " -- replaced the loaded MEM with ");
2073 print_simple_rtl (dump_file, read_reg);
2074 fprintf (dump_file, "\n");
2075 }
2076 return true;
2077 }
2078 else
2079 {
2080 if (dump_file && (dump_flags & TDF_DETAILS))
2081 {
2082 fprintf (dump_file, " -- replacing the loaded MEM with ");
2083 print_simple_rtl (dump_file, read_reg);
2084 fprintf (dump_file, " led to an invalid instruction\n");
2085 }
2086 return false;
2087 }
2088 }
2089
2090 /* A for_each_rtx callback in which DATA is the bb_info. Check to see
2091 if LOC is a mem and if it is look at the address and kill any
2092 appropriate stores that may be active. */
2093
2094 static int
2095 check_mem_read_rtx (rtx *loc, void *data)
2096 {
2097 rtx mem = *loc, mem_addr;
2098 bb_info_t bb_info;
2099 insn_info_t insn_info;
2100 HOST_WIDE_INT offset = 0;
2101 HOST_WIDE_INT width = 0;
2102 alias_set_type spill_alias_set = 0;
2103 cselib_val *base = NULL;
2104 int group_id;
2105 read_info_t read_info;
2106
2107 if (!mem || !MEM_P (mem))
2108 return 0;
2109
2110 bb_info = (bb_info_t) data;
2111 insn_info = bb_info->last_insn;
2112
2113 if ((MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2114 || (MEM_VOLATILE_P (mem)))
2115 {
2116 if (dump_file && (dump_flags & TDF_DETAILS))
2117 fprintf (dump_file, " adding wild read, volatile or barrier.\n");
2118 add_wild_read (bb_info);
2119 insn_info->cannot_delete = true;
2120 return 0;
2121 }
2122
2123 /* If it is reading readonly mem, then there can be no conflict with
2124 another write. */
2125 if (MEM_READONLY_P (mem))
2126 return 0;
2127
2128 if (!canon_address (mem, &spill_alias_set, &group_id, &offset, &base))
2129 {
2130 if (dump_file && (dump_flags & TDF_DETAILS))
2131 fprintf (dump_file, " adding wild read, canon_address failure.\n");
2132 add_wild_read (bb_info);
2133 return 0;
2134 }
2135
2136 if (GET_MODE (mem) == BLKmode)
2137 width = -1;
2138 else
2139 width = GET_MODE_SIZE (GET_MODE (mem));
2140
2141 read_info = (read_info_t) pool_alloc (read_info_pool);
2142 read_info->group_id = group_id;
2143 read_info->mem = mem;
2144 read_info->alias_set = spill_alias_set;
2145 read_info->begin = offset;
2146 read_info->end = offset + width;
2147 read_info->next = insn_info->read_rec;
2148 insn_info->read_rec = read_info;
2149 /* For alias_set != 0 canon_true_dependence should be never called. */
2150 if (spill_alias_set)
2151 mem_addr = NULL_RTX;
2152 else
2153 {
2154 if (group_id < 0)
2155 mem_addr = base->val_rtx;
2156 else
2157 {
2158 group_info_t group
2159 = rtx_group_vec[group_id];
2160 mem_addr = group->canon_base_addr;
2161 }
2162 if (offset)
2163 mem_addr = plus_constant (get_address_mode (mem), mem_addr, offset);
2164 }
2165
2166 /* We ignore the clobbers in store_info. The is mildly aggressive,
2167 but there really should not be a clobber followed by a read. */
2168
2169 if (spill_alias_set)
2170 {
2171 insn_info_t i_ptr = active_local_stores;
2172 insn_info_t last = NULL;
2173
2174 if (dump_file && (dump_flags & TDF_DETAILS))
2175 fprintf (dump_file, " processing spill load %d\n",
2176 (int) spill_alias_set);
2177
2178 while (i_ptr)
2179 {
2180 store_info_t store_info = i_ptr->store_rec;
2181
2182 /* Skip the clobbers. */
2183 while (!store_info->is_set)
2184 store_info = store_info->next;
2185
2186 if (store_info->alias_set == spill_alias_set)
2187 {
2188 if (dump_file && (dump_flags & TDF_DETAILS))
2189 dump_insn_info ("removing from active", i_ptr);
2190
2191 active_local_stores_len--;
2192 if (last)
2193 last->next_local_store = i_ptr->next_local_store;
2194 else
2195 active_local_stores = i_ptr->next_local_store;
2196 }
2197 else
2198 last = i_ptr;
2199 i_ptr = i_ptr->next_local_store;
2200 }
2201 }
2202 else if (group_id >= 0)
2203 {
2204 /* This is the restricted case where the base is a constant or
2205 the frame pointer and offset is a constant. */
2206 insn_info_t i_ptr = active_local_stores;
2207 insn_info_t last = NULL;
2208
2209 if (dump_file && (dump_flags & TDF_DETAILS))
2210 {
2211 if (width == -1)
2212 fprintf (dump_file, " processing const load gid=%d[BLK]\n",
2213 group_id);
2214 else
2215 fprintf (dump_file, " processing const load gid=%d[%d..%d)\n",
2216 group_id, (int)offset, (int)(offset+width));
2217 }
2218
2219 while (i_ptr)
2220 {
2221 bool remove = false;
2222 store_info_t store_info = i_ptr->store_rec;
2223
2224 /* Skip the clobbers. */
2225 while (!store_info->is_set)
2226 store_info = store_info->next;
2227
2228 /* There are three cases here. */
2229 if (store_info->group_id < 0)
2230 /* We have a cselib store followed by a read from a
2231 const base. */
2232 remove
2233 = canon_true_dependence (store_info->mem,
2234 GET_MODE (store_info->mem),
2235 store_info->mem_addr,
2236 mem, mem_addr);
2237
2238 else if (group_id == store_info->group_id)
2239 {
2240 /* This is a block mode load. We may get lucky and
2241 canon_true_dependence may save the day. */
2242 if (width == -1)
2243 remove
2244 = canon_true_dependence (store_info->mem,
2245 GET_MODE (store_info->mem),
2246 store_info->mem_addr,
2247 mem, mem_addr);
2248
2249 /* If this read is just reading back something that we just
2250 stored, rewrite the read. */
2251 else
2252 {
2253 if (store_info->rhs
2254 && offset >= store_info->begin
2255 && offset + width <= store_info->end
2256 && all_positions_needed_p (store_info,
2257 offset - store_info->begin,
2258 width)
2259 && replace_read (store_info, i_ptr, read_info,
2260 insn_info, loc, bb_info->regs_live))
2261 return 0;
2262
2263 /* The bases are the same, just see if the offsets
2264 overlap. */
2265 if ((offset < store_info->end)
2266 && (offset + width > store_info->begin))
2267 remove = true;
2268 }
2269 }
2270
2271 /* else
2272 The else case that is missing here is that the
2273 bases are constant but different. There is nothing
2274 to do here because there is no overlap. */
2275
2276 if (remove)
2277 {
2278 if (dump_file && (dump_flags & TDF_DETAILS))
2279 dump_insn_info ("removing from active", i_ptr);
2280
2281 active_local_stores_len--;
2282 if (last)
2283 last->next_local_store = i_ptr->next_local_store;
2284 else
2285 active_local_stores = i_ptr->next_local_store;
2286 }
2287 else
2288 last = i_ptr;
2289 i_ptr = i_ptr->next_local_store;
2290 }
2291 }
2292 else
2293 {
2294 insn_info_t i_ptr = active_local_stores;
2295 insn_info_t last = NULL;
2296 if (dump_file && (dump_flags & TDF_DETAILS))
2297 {
2298 fprintf (dump_file, " processing cselib load mem:");
2299 print_inline_rtx (dump_file, mem, 0);
2300 fprintf (dump_file, "\n");
2301 }
2302
2303 while (i_ptr)
2304 {
2305 bool remove = false;
2306 store_info_t store_info = i_ptr->store_rec;
2307
2308 if (dump_file && (dump_flags & TDF_DETAILS))
2309 fprintf (dump_file, " processing cselib load against insn %d\n",
2310 INSN_UID (i_ptr->insn));
2311
2312 /* Skip the clobbers. */
2313 while (!store_info->is_set)
2314 store_info = store_info->next;
2315
2316 /* If this read is just reading back something that we just
2317 stored, rewrite the read. */
2318 if (store_info->rhs
2319 && store_info->group_id == -1
2320 && store_info->cse_base == base
2321 && width != -1
2322 && offset >= store_info->begin
2323 && offset + width <= store_info->end
2324 && all_positions_needed_p (store_info,
2325 offset - store_info->begin, width)
2326 && replace_read (store_info, i_ptr, read_info, insn_info, loc,
2327 bb_info->regs_live))
2328 return 0;
2329
2330 if (!store_info->alias_set)
2331 remove = canon_true_dependence (store_info->mem,
2332 GET_MODE (store_info->mem),
2333 store_info->mem_addr,
2334 mem, mem_addr);
2335
2336 if (remove)
2337 {
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 dump_insn_info ("removing from active", i_ptr);
2340
2341 active_local_stores_len--;
2342 if (last)
2343 last->next_local_store = i_ptr->next_local_store;
2344 else
2345 active_local_stores = i_ptr->next_local_store;
2346 }
2347 else
2348 last = i_ptr;
2349 i_ptr = i_ptr->next_local_store;
2350 }
2351 }
2352 return 0;
2353 }
2354
2355 /* A for_each_rtx callback in which DATA points the INSN_INFO for
2356 as check_mem_read_rtx. Nullify the pointer if i_m_r_m_r returns
2357 true for any part of *LOC. */
2358
2359 static void
2360 check_mem_read_use (rtx *loc, void *data)
2361 {
2362 for_each_rtx (loc, check_mem_read_rtx, data);
2363 }
2364
2365
2366 /* Get arguments passed to CALL_INSN. Return TRUE if successful.
2367 So far it only handles arguments passed in registers. */
2368
2369 static bool
2370 get_call_args (rtx call_insn, tree fn, rtx *args, int nargs)
2371 {
2372 CUMULATIVE_ARGS args_so_far_v;
2373 cumulative_args_t args_so_far;
2374 tree arg;
2375 int idx;
2376
2377 INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
2378 args_so_far = pack_cumulative_args (&args_so_far_v);
2379
2380 arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
2381 for (idx = 0;
2382 arg != void_list_node && idx < nargs;
2383 arg = TREE_CHAIN (arg), idx++)
2384 {
2385 enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
2386 rtx reg, link, tmp;
2387 reg = targetm.calls.function_arg (args_so_far, mode, NULL_TREE, true);
2388 if (!reg || !REG_P (reg) || GET_MODE (reg) != mode
2389 || GET_MODE_CLASS (mode) != MODE_INT)
2390 return false;
2391
2392 for (link = CALL_INSN_FUNCTION_USAGE (call_insn);
2393 link;
2394 link = XEXP (link, 1))
2395 if (GET_CODE (XEXP (link, 0)) == USE)
2396 {
2397 args[idx] = XEXP (XEXP (link, 0), 0);
2398 if (REG_P (args[idx])
2399 && REGNO (args[idx]) == REGNO (reg)
2400 && (GET_MODE (args[idx]) == mode
2401 || (GET_MODE_CLASS (GET_MODE (args[idx])) == MODE_INT
2402 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2403 <= UNITS_PER_WORD)
2404 && (GET_MODE_SIZE (GET_MODE (args[idx]))
2405 > GET_MODE_SIZE (mode)))))
2406 break;
2407 }
2408 if (!link)
2409 return false;
2410
2411 tmp = cselib_expand_value_rtx (args[idx], scratch, 5);
2412 if (GET_MODE (args[idx]) != mode)
2413 {
2414 if (!tmp || !CONST_INT_P (tmp))
2415 return false;
2416 tmp = gen_int_mode (INTVAL (tmp), mode);
2417 }
2418 if (tmp)
2419 args[idx] = tmp;
2420
2421 targetm.calls.function_arg_advance (args_so_far, mode, NULL_TREE, true);
2422 }
2423 if (arg != void_list_node || idx != nargs)
2424 return false;
2425 return true;
2426 }
2427
2428 /* Return a bitmap of the fixed registers contained in IN. */
2429
2430 static bitmap
2431 copy_fixed_regs (const_bitmap in)
2432 {
2433 bitmap ret;
2434
2435 ret = ALLOC_REG_SET (NULL);
2436 bitmap_and (ret, in, fixed_reg_set_regset);
2437 return ret;
2438 }
2439
2440 /* Apply record_store to all candidate stores in INSN. Mark INSN
2441 if some part of it is not a candidate store and assigns to a
2442 non-register target. */
2443
2444 static void
2445 scan_insn (bb_info_t bb_info, rtx_insn *insn)
2446 {
2447 rtx body;
2448 insn_info_t insn_info = (insn_info_t) pool_alloc (insn_info_pool);
2449 int mems_found = 0;
2450 memset (insn_info, 0, sizeof (struct insn_info));
2451
2452 if (dump_file && (dump_flags & TDF_DETAILS))
2453 fprintf (dump_file, "\n**scanning insn=%d\n",
2454 INSN_UID (insn));
2455
2456 insn_info->prev_insn = bb_info->last_insn;
2457 insn_info->insn = insn;
2458 bb_info->last_insn = insn_info;
2459
2460 if (DEBUG_INSN_P (insn))
2461 {
2462 insn_info->cannot_delete = true;
2463 return;
2464 }
2465
2466 /* Look at all of the uses in the insn. */
2467 note_uses (&PATTERN (insn), check_mem_read_use, bb_info);
2468
2469 if (CALL_P (insn))
2470 {
2471 bool const_call;
2472 tree memset_call = NULL_TREE;
2473
2474 insn_info->cannot_delete = true;
2475
2476 /* Const functions cannot do anything bad i.e. read memory,
2477 however, they can read their parameters which may have
2478 been pushed onto the stack.
2479 memset and bzero don't read memory either. */
2480 const_call = RTL_CONST_CALL_P (insn);
2481 if (!const_call)
2482 {
2483 rtx call = get_call_rtx_from (insn);
2484 if (call && GET_CODE (XEXP (XEXP (call, 0), 0)) == SYMBOL_REF)
2485 {
2486 rtx symbol = XEXP (XEXP (call, 0), 0);
2487 if (SYMBOL_REF_DECL (symbol)
2488 && TREE_CODE (SYMBOL_REF_DECL (symbol)) == FUNCTION_DECL)
2489 {
2490 if ((DECL_BUILT_IN_CLASS (SYMBOL_REF_DECL (symbol))
2491 == BUILT_IN_NORMAL
2492 && (DECL_FUNCTION_CODE (SYMBOL_REF_DECL (symbol))
2493 == BUILT_IN_MEMSET))
2494 || SYMBOL_REF_DECL (symbol) == block_clear_fn)
2495 memset_call = SYMBOL_REF_DECL (symbol);
2496 }
2497 }
2498 }
2499 if (const_call || memset_call)
2500 {
2501 insn_info_t i_ptr = active_local_stores;
2502 insn_info_t last = NULL;
2503
2504 if (dump_file && (dump_flags & TDF_DETAILS))
2505 fprintf (dump_file, "%s call %d\n",
2506 const_call ? "const" : "memset", INSN_UID (insn));
2507
2508 /* See the head comment of the frame_read field. */
2509 if (reload_completed)
2510 insn_info->frame_read = true;
2511
2512 /* Loop over the active stores and remove those which are
2513 killed by the const function call. */
2514 while (i_ptr)
2515 {
2516 bool remove_store = false;
2517
2518 /* The stack pointer based stores are always killed. */
2519 if (i_ptr->stack_pointer_based)
2520 remove_store = true;
2521
2522 /* If the frame is read, the frame related stores are killed. */
2523 else if (insn_info->frame_read)
2524 {
2525 store_info_t store_info = i_ptr->store_rec;
2526
2527 /* Skip the clobbers. */
2528 while (!store_info->is_set)
2529 store_info = store_info->next;
2530
2531 if (store_info->group_id >= 0
2532 && rtx_group_vec[store_info->group_id]->frame_related)
2533 remove_store = true;
2534 }
2535
2536 if (remove_store)
2537 {
2538 if (dump_file && (dump_flags & TDF_DETAILS))
2539 dump_insn_info ("removing from active", i_ptr);
2540
2541 active_local_stores_len--;
2542 if (last)
2543 last->next_local_store = i_ptr->next_local_store;
2544 else
2545 active_local_stores = i_ptr->next_local_store;
2546 }
2547 else
2548 last = i_ptr;
2549
2550 i_ptr = i_ptr->next_local_store;
2551 }
2552
2553 if (memset_call)
2554 {
2555 rtx args[3];
2556 if (get_call_args (insn, memset_call, args, 3)
2557 && CONST_INT_P (args[1])
2558 && CONST_INT_P (args[2])
2559 && INTVAL (args[2]) > 0)
2560 {
2561 rtx mem = gen_rtx_MEM (BLKmode, args[0]);
2562 set_mem_size (mem, INTVAL (args[2]));
2563 body = gen_rtx_SET (VOIDmode, mem, args[1]);
2564 mems_found += record_store (body, bb_info);
2565 if (dump_file && (dump_flags & TDF_DETAILS))
2566 fprintf (dump_file, "handling memset as BLKmode store\n");
2567 if (mems_found == 1)
2568 {
2569 if (active_local_stores_len++
2570 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2571 {
2572 active_local_stores_len = 1;
2573 active_local_stores = NULL;
2574 }
2575 insn_info->fixed_regs_live
2576 = copy_fixed_regs (bb_info->regs_live);
2577 insn_info->next_local_store = active_local_stores;
2578 active_local_stores = insn_info;
2579 }
2580 }
2581 }
2582 }
2583
2584 else
2585 /* Every other call, including pure functions, may read any memory
2586 that is not relative to the frame. */
2587 add_non_frame_wild_read (bb_info);
2588
2589 return;
2590 }
2591
2592 /* Assuming that there are sets in these insns, we cannot delete
2593 them. */
2594 if ((GET_CODE (PATTERN (insn)) == CLOBBER)
2595 || volatile_refs_p (PATTERN (insn))
2596 || (!cfun->can_delete_dead_exceptions && !insn_nothrow_p (insn))
2597 || (RTX_FRAME_RELATED_P (insn))
2598 || find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX))
2599 insn_info->cannot_delete = true;
2600
2601 body = PATTERN (insn);
2602 if (GET_CODE (body) == PARALLEL)
2603 {
2604 int i;
2605 for (i = 0; i < XVECLEN (body, 0); i++)
2606 mems_found += record_store (XVECEXP (body, 0, i), bb_info);
2607 }
2608 else
2609 mems_found += record_store (body, bb_info);
2610
2611 if (dump_file && (dump_flags & TDF_DETAILS))
2612 fprintf (dump_file, "mems_found = %d, cannot_delete = %s\n",
2613 mems_found, insn_info->cannot_delete ? "true" : "false");
2614
2615 /* If we found some sets of mems, add it into the active_local_stores so
2616 that it can be locally deleted if found dead or used for
2617 replace_read and redundant constant store elimination. Otherwise mark
2618 it as cannot delete. This simplifies the processing later. */
2619 if (mems_found == 1)
2620 {
2621 if (active_local_stores_len++
2622 >= PARAM_VALUE (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES))
2623 {
2624 active_local_stores_len = 1;
2625 active_local_stores = NULL;
2626 }
2627 insn_info->fixed_regs_live = copy_fixed_regs (bb_info->regs_live);
2628 insn_info->next_local_store = active_local_stores;
2629 active_local_stores = insn_info;
2630 }
2631 else
2632 insn_info->cannot_delete = true;
2633 }
2634
2635
2636 /* Remove BASE from the set of active_local_stores. This is a
2637 callback from cselib that is used to get rid of the stores in
2638 active_local_stores. */
2639
2640 static void
2641 remove_useless_values (cselib_val *base)
2642 {
2643 insn_info_t insn_info = active_local_stores;
2644 insn_info_t last = NULL;
2645
2646 while (insn_info)
2647 {
2648 store_info_t store_info = insn_info->store_rec;
2649 bool del = false;
2650
2651 /* If ANY of the store_infos match the cselib group that is
2652 being deleted, then the insn can not be deleted. */
2653 while (store_info)
2654 {
2655 if ((store_info->group_id == -1)
2656 && (store_info->cse_base == base))
2657 {
2658 del = true;
2659 break;
2660 }
2661 store_info = store_info->next;
2662 }
2663
2664 if (del)
2665 {
2666 active_local_stores_len--;
2667 if (last)
2668 last->next_local_store = insn_info->next_local_store;
2669 else
2670 active_local_stores = insn_info->next_local_store;
2671 free_store_info (insn_info);
2672 }
2673 else
2674 last = insn_info;
2675
2676 insn_info = insn_info->next_local_store;
2677 }
2678 }
2679
2680
2681 /* Do all of step 1. */
2682
2683 static void
2684 dse_step1 (void)
2685 {
2686 basic_block bb;
2687 bitmap regs_live = BITMAP_ALLOC (&reg_obstack);
2688
2689 cselib_init (0);
2690 all_blocks = BITMAP_ALLOC (NULL);
2691 bitmap_set_bit (all_blocks, ENTRY_BLOCK);
2692 bitmap_set_bit (all_blocks, EXIT_BLOCK);
2693
2694 FOR_ALL_BB_FN (bb, cfun)
2695 {
2696 insn_info_t ptr;
2697 bb_info_t bb_info = (bb_info_t) pool_alloc (bb_info_pool);
2698
2699 memset (bb_info, 0, sizeof (struct bb_info));
2700 bitmap_set_bit (all_blocks, bb->index);
2701 bb_info->regs_live = regs_live;
2702
2703 bitmap_copy (regs_live, DF_LR_IN (bb));
2704 df_simulate_initialize_forwards (bb, regs_live);
2705
2706 bb_table[bb->index] = bb_info;
2707 cselib_discard_hook = remove_useless_values;
2708
2709 if (bb->index >= NUM_FIXED_BLOCKS)
2710 {
2711 rtx_insn *insn;
2712
2713 cse_store_info_pool
2714 = create_alloc_pool ("cse_store_info_pool",
2715 sizeof (struct store_info), 100);
2716 active_local_stores = NULL;
2717 active_local_stores_len = 0;
2718 cselib_clear_table ();
2719
2720 /* Scan the insns. */
2721 FOR_BB_INSNS (bb, insn)
2722 {
2723 if (INSN_P (insn))
2724 scan_insn (bb_info, insn);
2725 cselib_process_insn (insn);
2726 if (INSN_P (insn))
2727 df_simulate_one_insn_forwards (bb, insn, regs_live);
2728 }
2729
2730 /* This is something of a hack, because the global algorithm
2731 is supposed to take care of the case where stores go dead
2732 at the end of the function. However, the global
2733 algorithm must take a more conservative view of block
2734 mode reads than the local alg does. So to get the case
2735 where you have a store to the frame followed by a non
2736 overlapping block more read, we look at the active local
2737 stores at the end of the function and delete all of the
2738 frame and spill based ones. */
2739 if (stores_off_frame_dead_at_return
2740 && (EDGE_COUNT (bb->succs) == 0
2741 || (single_succ_p (bb)
2742 && single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun)
2743 && ! crtl->calls_eh_return)))
2744 {
2745 insn_info_t i_ptr = active_local_stores;
2746 while (i_ptr)
2747 {
2748 store_info_t store_info = i_ptr->store_rec;
2749
2750 /* Skip the clobbers. */
2751 while (!store_info->is_set)
2752 store_info = store_info->next;
2753 if (store_info->alias_set && !i_ptr->cannot_delete)
2754 delete_dead_store_insn (i_ptr);
2755 else
2756 if (store_info->group_id >= 0)
2757 {
2758 group_info_t group
2759 = rtx_group_vec[store_info->group_id];
2760 if (group->frame_related && !i_ptr->cannot_delete)
2761 delete_dead_store_insn (i_ptr);
2762 }
2763
2764 i_ptr = i_ptr->next_local_store;
2765 }
2766 }
2767
2768 /* Get rid of the loads that were discovered in
2769 replace_read. Cselib is finished with this block. */
2770 while (deferred_change_list)
2771 {
2772 deferred_change_t next = deferred_change_list->next;
2773
2774 /* There is no reason to validate this change. That was
2775 done earlier. */
2776 *deferred_change_list->loc = deferred_change_list->reg;
2777 pool_free (deferred_change_pool, deferred_change_list);
2778 deferred_change_list = next;
2779 }
2780
2781 /* Get rid of all of the cselib based store_infos in this
2782 block and mark the containing insns as not being
2783 deletable. */
2784 ptr = bb_info->last_insn;
2785 while (ptr)
2786 {
2787 if (ptr->contains_cselib_groups)
2788 {
2789 store_info_t s_info = ptr->store_rec;
2790 while (s_info && !s_info->is_set)
2791 s_info = s_info->next;
2792 if (s_info
2793 && s_info->redundant_reason
2794 && s_info->redundant_reason->insn
2795 && !ptr->cannot_delete)
2796 {
2797 if (dump_file && (dump_flags & TDF_DETAILS))
2798 fprintf (dump_file, "Locally deleting insn %d "
2799 "because insn %d stores the "
2800 "same value and couldn't be "
2801 "eliminated\n",
2802 INSN_UID (ptr->insn),
2803 INSN_UID (s_info->redundant_reason->insn));
2804 delete_dead_store_insn (ptr);
2805 }
2806 free_store_info (ptr);
2807 }
2808 else
2809 {
2810 store_info_t s_info;
2811
2812 /* Free at least positions_needed bitmaps. */
2813 for (s_info = ptr->store_rec; s_info; s_info = s_info->next)
2814 if (s_info->is_large)
2815 {
2816 BITMAP_FREE (s_info->positions_needed.large.bmap);
2817 s_info->is_large = false;
2818 }
2819 }
2820 ptr = ptr->prev_insn;
2821 }
2822
2823 free_alloc_pool (cse_store_info_pool);
2824 }
2825 bb_info->regs_live = NULL;
2826 }
2827
2828 BITMAP_FREE (regs_live);
2829 cselib_finish ();
2830 rtx_group_table->empty ();
2831 }
2832
2833 \f
2834 /*----------------------------------------------------------------------------
2835 Second step.
2836
2837 Assign each byte position in the stores that we are going to
2838 analyze globally to a position in the bitmaps. Returns true if
2839 there are any bit positions assigned.
2840 ----------------------------------------------------------------------------*/
2841
2842 static void
2843 dse_step2_init (void)
2844 {
2845 unsigned int i;
2846 group_info_t group;
2847
2848 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2849 {
2850 /* For all non stack related bases, we only consider a store to
2851 be deletable if there are two or more stores for that
2852 position. This is because it takes one store to make the
2853 other store redundant. However, for the stores that are
2854 stack related, we consider them if there is only one store
2855 for the position. We do this because the stack related
2856 stores can be deleted if their is no read between them and
2857 the end of the function.
2858
2859 To make this work in the current framework, we take the stack
2860 related bases add all of the bits from store1 into store2.
2861 This has the effect of making the eligible even if there is
2862 only one store. */
2863
2864 if (stores_off_frame_dead_at_return && group->frame_related)
2865 {
2866 bitmap_ior_into (group->store2_n, group->store1_n);
2867 bitmap_ior_into (group->store2_p, group->store1_p);
2868 if (dump_file && (dump_flags & TDF_DETAILS))
2869 fprintf (dump_file, "group %d is frame related ", i);
2870 }
2871
2872 group->offset_map_size_n++;
2873 group->offset_map_n = XOBNEWVEC (&dse_obstack, int,
2874 group->offset_map_size_n);
2875 group->offset_map_size_p++;
2876 group->offset_map_p = XOBNEWVEC (&dse_obstack, int,
2877 group->offset_map_size_p);
2878 group->process_globally = false;
2879 if (dump_file && (dump_flags & TDF_DETAILS))
2880 {
2881 fprintf (dump_file, "group %d(%d+%d): ", i,
2882 (int)bitmap_count_bits (group->store2_n),
2883 (int)bitmap_count_bits (group->store2_p));
2884 bitmap_print (dump_file, group->store2_n, "n ", " ");
2885 bitmap_print (dump_file, group->store2_p, "p ", "\n");
2886 }
2887 }
2888 }
2889
2890
2891 /* Init the offset tables for the normal case. */
2892
2893 static bool
2894 dse_step2_nospill (void)
2895 {
2896 unsigned int i;
2897 group_info_t group;
2898 /* Position 0 is unused because 0 is used in the maps to mean
2899 unused. */
2900 current_position = 1;
2901 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
2902 {
2903 bitmap_iterator bi;
2904 unsigned int j;
2905
2906 if (group == clear_alias_group)
2907 continue;
2908
2909 memset (group->offset_map_n, 0, sizeof (int) * group->offset_map_size_n);
2910 memset (group->offset_map_p, 0, sizeof (int) * group->offset_map_size_p);
2911 bitmap_clear (group->group_kill);
2912
2913 EXECUTE_IF_SET_IN_BITMAP (group->store2_n, 0, j, bi)
2914 {
2915 bitmap_set_bit (group->group_kill, current_position);
2916 if (bitmap_bit_p (group->escaped_n, j))
2917 bitmap_set_bit (kill_on_calls, current_position);
2918 group->offset_map_n[j] = current_position++;
2919 group->process_globally = true;
2920 }
2921 EXECUTE_IF_SET_IN_BITMAP (group->store2_p, 0, j, bi)
2922 {
2923 bitmap_set_bit (group->group_kill, current_position);
2924 if (bitmap_bit_p (group->escaped_p, j))
2925 bitmap_set_bit (kill_on_calls, current_position);
2926 group->offset_map_p[j] = current_position++;
2927 group->process_globally = true;
2928 }
2929 }
2930 return current_position != 1;
2931 }
2932
2933
2934 \f
2935 /*----------------------------------------------------------------------------
2936 Third step.
2937
2938 Build the bit vectors for the transfer functions.
2939 ----------------------------------------------------------------------------*/
2940
2941
2942 /* Look up the bitmap index for OFFSET in GROUP_INFO. If it is not
2943 there, return 0. */
2944
2945 static int
2946 get_bitmap_index (group_info_t group_info, HOST_WIDE_INT offset)
2947 {
2948 if (offset < 0)
2949 {
2950 HOST_WIDE_INT offset_p = -offset;
2951 if (offset_p >= group_info->offset_map_size_n)
2952 return 0;
2953 return group_info->offset_map_n[offset_p];
2954 }
2955 else
2956 {
2957 if (offset >= group_info->offset_map_size_p)
2958 return 0;
2959 return group_info->offset_map_p[offset];
2960 }
2961 }
2962
2963
2964 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2965 may be NULL. */
2966
2967 static void
2968 scan_stores_nospill (store_info_t store_info, bitmap gen, bitmap kill)
2969 {
2970 while (store_info)
2971 {
2972 HOST_WIDE_INT i;
2973 group_info_t group_info
2974 = rtx_group_vec[store_info->group_id];
2975 if (group_info->process_globally)
2976 for (i = store_info->begin; i < store_info->end; i++)
2977 {
2978 int index = get_bitmap_index (group_info, i);
2979 if (index != 0)
2980 {
2981 bitmap_set_bit (gen, index);
2982 if (kill)
2983 bitmap_clear_bit (kill, index);
2984 }
2985 }
2986 store_info = store_info->next;
2987 }
2988 }
2989
2990
2991 /* Process the STORE_INFOs into the bitmaps into GEN and KILL. KILL
2992 may be NULL. */
2993
2994 static void
2995 scan_stores_spill (store_info_t store_info, bitmap gen, bitmap kill)
2996 {
2997 while (store_info)
2998 {
2999 if (store_info->alias_set)
3000 {
3001 int index = get_bitmap_index (clear_alias_group,
3002 store_info->alias_set);
3003 if (index != 0)
3004 {
3005 bitmap_set_bit (gen, index);
3006 if (kill)
3007 bitmap_clear_bit (kill, index);
3008 }
3009 }
3010 store_info = store_info->next;
3011 }
3012 }
3013
3014
3015 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3016 may be NULL. */
3017
3018 static void
3019 scan_reads_nospill (insn_info_t insn_info, bitmap gen, bitmap kill)
3020 {
3021 read_info_t read_info = insn_info->read_rec;
3022 int i;
3023 group_info_t group;
3024
3025 /* If this insn reads the frame, kill all the frame related stores. */
3026 if (insn_info->frame_read)
3027 {
3028 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3029 if (group->process_globally && group->frame_related)
3030 {
3031 if (kill)
3032 bitmap_ior_into (kill, group->group_kill);
3033 bitmap_and_compl_into (gen, group->group_kill);
3034 }
3035 }
3036 if (insn_info->non_frame_wild_read)
3037 {
3038 /* Kill all non-frame related stores. Kill all stores of variables that
3039 escape. */
3040 if (kill)
3041 bitmap_ior_into (kill, kill_on_calls);
3042 bitmap_and_compl_into (gen, kill_on_calls);
3043 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3044 if (group->process_globally && !group->frame_related)
3045 {
3046 if (kill)
3047 bitmap_ior_into (kill, group->group_kill);
3048 bitmap_and_compl_into (gen, group->group_kill);
3049 }
3050 }
3051 while (read_info)
3052 {
3053 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3054 {
3055 if (group->process_globally)
3056 {
3057 if (i == read_info->group_id)
3058 {
3059 if (read_info->begin > read_info->end)
3060 {
3061 /* Begin > end for block mode reads. */
3062 if (kill)
3063 bitmap_ior_into (kill, group->group_kill);
3064 bitmap_and_compl_into (gen, group->group_kill);
3065 }
3066 else
3067 {
3068 /* The groups are the same, just process the
3069 offsets. */
3070 HOST_WIDE_INT j;
3071 for (j = read_info->begin; j < read_info->end; j++)
3072 {
3073 int index = get_bitmap_index (group, j);
3074 if (index != 0)
3075 {
3076 if (kill)
3077 bitmap_set_bit (kill, index);
3078 bitmap_clear_bit (gen, index);
3079 }
3080 }
3081 }
3082 }
3083 else
3084 {
3085 /* The groups are different, if the alias sets
3086 conflict, clear the entire group. We only need
3087 to apply this test if the read_info is a cselib
3088 read. Anything with a constant base cannot alias
3089 something else with a different constant
3090 base. */
3091 if ((read_info->group_id < 0)
3092 && canon_true_dependence (group->base_mem,
3093 GET_MODE (group->base_mem),
3094 group->canon_base_addr,
3095 read_info->mem, NULL_RTX))
3096 {
3097 if (kill)
3098 bitmap_ior_into (kill, group->group_kill);
3099 bitmap_and_compl_into (gen, group->group_kill);
3100 }
3101 }
3102 }
3103 }
3104
3105 read_info = read_info->next;
3106 }
3107 }
3108
3109 /* Process the READ_INFOs into the bitmaps into GEN and KILL. KILL
3110 may be NULL. */
3111
3112 static void
3113 scan_reads_spill (read_info_t read_info, bitmap gen, bitmap kill)
3114 {
3115 while (read_info)
3116 {
3117 if (read_info->alias_set)
3118 {
3119 int index = get_bitmap_index (clear_alias_group,
3120 read_info->alias_set);
3121 if (index != 0)
3122 {
3123 if (kill)
3124 bitmap_set_bit (kill, index);
3125 bitmap_clear_bit (gen, index);
3126 }
3127 }
3128
3129 read_info = read_info->next;
3130 }
3131 }
3132
3133
3134 /* Return the insn in BB_INFO before the first wild read or if there
3135 are no wild reads in the block, return the last insn. */
3136
3137 static insn_info_t
3138 find_insn_before_first_wild_read (bb_info_t bb_info)
3139 {
3140 insn_info_t insn_info = bb_info->last_insn;
3141 insn_info_t last_wild_read = NULL;
3142
3143 while (insn_info)
3144 {
3145 if (insn_info->wild_read)
3146 {
3147 last_wild_read = insn_info->prev_insn;
3148 /* Block starts with wild read. */
3149 if (!last_wild_read)
3150 return NULL;
3151 }
3152
3153 insn_info = insn_info->prev_insn;
3154 }
3155
3156 if (last_wild_read)
3157 return last_wild_read;
3158 else
3159 return bb_info->last_insn;
3160 }
3161
3162
3163 /* Scan the insns in BB_INFO starting at PTR and going to the top of
3164 the block in order to build the gen and kill sets for the block.
3165 We start at ptr which may be the last insn in the block or may be
3166 the first insn with a wild read. In the latter case we are able to
3167 skip the rest of the block because it just does not matter:
3168 anything that happens is hidden by the wild read. */
3169
3170 static void
3171 dse_step3_scan (bool for_spills, basic_block bb)
3172 {
3173 bb_info_t bb_info = bb_table[bb->index];
3174 insn_info_t insn_info;
3175
3176 if (for_spills)
3177 /* There are no wild reads in the spill case. */
3178 insn_info = bb_info->last_insn;
3179 else
3180 insn_info = find_insn_before_first_wild_read (bb_info);
3181
3182 /* In the spill case or in the no_spill case if there is no wild
3183 read in the block, we will need a kill set. */
3184 if (insn_info == bb_info->last_insn)
3185 {
3186 if (bb_info->kill)
3187 bitmap_clear (bb_info->kill);
3188 else
3189 bb_info->kill = BITMAP_ALLOC (&dse_bitmap_obstack);
3190 }
3191 else
3192 if (bb_info->kill)
3193 BITMAP_FREE (bb_info->kill);
3194
3195 while (insn_info)
3196 {
3197 /* There may have been code deleted by the dce pass run before
3198 this phase. */
3199 if (insn_info->insn && INSN_P (insn_info->insn))
3200 {
3201 /* Process the read(s) last. */
3202 if (for_spills)
3203 {
3204 scan_stores_spill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3205 scan_reads_spill (insn_info->read_rec, bb_info->gen, bb_info->kill);
3206 }
3207 else
3208 {
3209 scan_stores_nospill (insn_info->store_rec, bb_info->gen, bb_info->kill);
3210 scan_reads_nospill (insn_info, bb_info->gen, bb_info->kill);
3211 }
3212 }
3213
3214 insn_info = insn_info->prev_insn;
3215 }
3216 }
3217
3218
3219 /* Set the gen set of the exit block, and also any block with no
3220 successors that does not have a wild read. */
3221
3222 static void
3223 dse_step3_exit_block_scan (bb_info_t bb_info)
3224 {
3225 /* The gen set is all 0's for the exit block except for the
3226 frame_pointer_group. */
3227
3228 if (stores_off_frame_dead_at_return)
3229 {
3230 unsigned int i;
3231 group_info_t group;
3232
3233 FOR_EACH_VEC_ELT (rtx_group_vec, i, group)
3234 {
3235 if (group->process_globally && group->frame_related)
3236 bitmap_ior_into (bb_info->gen, group->group_kill);
3237 }
3238 }
3239 }
3240
3241
3242 /* Find all of the blocks that are not backwards reachable from the
3243 exit block or any block with no successors (BB). These are the
3244 infinite loops or infinite self loops. These blocks will still
3245 have their bits set in UNREACHABLE_BLOCKS. */
3246
3247 static void
3248 mark_reachable_blocks (sbitmap unreachable_blocks, basic_block bb)
3249 {
3250 edge e;
3251 edge_iterator ei;
3252
3253 if (bitmap_bit_p (unreachable_blocks, bb->index))
3254 {
3255 bitmap_clear_bit (unreachable_blocks, bb->index);
3256 FOR_EACH_EDGE (e, ei, bb->preds)
3257 {
3258 mark_reachable_blocks (unreachable_blocks, e->src);
3259 }
3260 }
3261 }
3262
3263 /* Build the transfer functions for the function. */
3264
3265 static void
3266 dse_step3 (bool for_spills)
3267 {
3268 basic_block bb;
3269 sbitmap unreachable_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
3270 sbitmap_iterator sbi;
3271 bitmap all_ones = NULL;
3272 unsigned int i;
3273
3274 bitmap_ones (unreachable_blocks);
3275
3276 FOR_ALL_BB_FN (bb, cfun)
3277 {
3278 bb_info_t bb_info = bb_table[bb->index];
3279 if (bb_info->gen)
3280 bitmap_clear (bb_info->gen);
3281 else
3282 bb_info->gen = BITMAP_ALLOC (&dse_bitmap_obstack);
3283
3284 if (bb->index == ENTRY_BLOCK)
3285 ;
3286 else if (bb->index == EXIT_BLOCK)
3287 dse_step3_exit_block_scan (bb_info);
3288 else
3289 dse_step3_scan (for_spills, bb);
3290 if (EDGE_COUNT (bb->succs) == 0)
3291 mark_reachable_blocks (unreachable_blocks, bb);
3292
3293 /* If this is the second time dataflow is run, delete the old
3294 sets. */
3295 if (bb_info->in)
3296 BITMAP_FREE (bb_info->in);
3297 if (bb_info->out)
3298 BITMAP_FREE (bb_info->out);
3299 }
3300
3301 /* For any block in an infinite loop, we must initialize the out set
3302 to all ones. This could be expensive, but almost never occurs in
3303 practice. However, it is common in regression tests. */
3304 EXECUTE_IF_SET_IN_BITMAP (unreachable_blocks, 0, i, sbi)
3305 {
3306 if (bitmap_bit_p (all_blocks, i))
3307 {
3308 bb_info_t bb_info = bb_table[i];
3309 if (!all_ones)
3310 {
3311 unsigned int j;
3312 group_info_t group;
3313
3314 all_ones = BITMAP_ALLOC (&dse_bitmap_obstack);
3315 FOR_EACH_VEC_ELT (rtx_group_vec, j, group)
3316 bitmap_ior_into (all_ones, group->group_kill);
3317 }
3318 if (!bb_info->out)
3319 {
3320 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3321 bitmap_copy (bb_info->out, all_ones);
3322 }
3323 }
3324 }
3325
3326 if (all_ones)
3327 BITMAP_FREE (all_ones);
3328 sbitmap_free (unreachable_blocks);
3329 }
3330
3331
3332 \f
3333 /*----------------------------------------------------------------------------
3334 Fourth step.
3335
3336 Solve the bitvector equations.
3337 ----------------------------------------------------------------------------*/
3338
3339
3340 /* Confluence function for blocks with no successors. Create an out
3341 set from the gen set of the exit block. This block logically has
3342 the exit block as a successor. */
3343
3344
3345
3346 static void
3347 dse_confluence_0 (basic_block bb)
3348 {
3349 bb_info_t bb_info = bb_table[bb->index];
3350
3351 if (bb->index == EXIT_BLOCK)
3352 return;
3353
3354 if (!bb_info->out)
3355 {
3356 bb_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3357 bitmap_copy (bb_info->out, bb_table[EXIT_BLOCK]->gen);
3358 }
3359 }
3360
3361 /* Propagate the information from the in set of the dest of E to the
3362 out set of the src of E. If the various in or out sets are not
3363 there, that means they are all ones. */
3364
3365 static bool
3366 dse_confluence_n (edge e)
3367 {
3368 bb_info_t src_info = bb_table[e->src->index];
3369 bb_info_t dest_info = bb_table[e->dest->index];
3370
3371 if (dest_info->in)
3372 {
3373 if (src_info->out)
3374 bitmap_and_into (src_info->out, dest_info->in);
3375 else
3376 {
3377 src_info->out = BITMAP_ALLOC (&dse_bitmap_obstack);
3378 bitmap_copy (src_info->out, dest_info->in);
3379 }
3380 }
3381 return true;
3382 }
3383
3384
3385 /* Propagate the info from the out to the in set of BB_INDEX's basic
3386 block. There are three cases:
3387
3388 1) The block has no kill set. In this case the kill set is all
3389 ones. It does not matter what the out set of the block is, none of
3390 the info can reach the top. The only thing that reaches the top is
3391 the gen set and we just copy the set.
3392
3393 2) There is a kill set but no out set and bb has successors. In
3394 this case we just return. Eventually an out set will be created and
3395 it is better to wait than to create a set of ones.
3396
3397 3) There is both a kill and out set. We apply the obvious transfer
3398 function.
3399 */
3400
3401 static bool
3402 dse_transfer_function (int bb_index)
3403 {
3404 bb_info_t bb_info = bb_table[bb_index];
3405
3406 if (bb_info->kill)
3407 {
3408 if (bb_info->out)
3409 {
3410 /* Case 3 above. */
3411 if (bb_info->in)
3412 return bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3413 bb_info->out, bb_info->kill);
3414 else
3415 {
3416 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3417 bitmap_ior_and_compl (bb_info->in, bb_info->gen,
3418 bb_info->out, bb_info->kill);
3419 return true;
3420 }
3421 }
3422 else
3423 /* Case 2 above. */
3424 return false;
3425 }
3426 else
3427 {
3428 /* Case 1 above. If there is already an in set, nothing
3429 happens. */
3430 if (bb_info->in)
3431 return false;
3432 else
3433 {
3434 bb_info->in = BITMAP_ALLOC (&dse_bitmap_obstack);
3435 bitmap_copy (bb_info->in, bb_info->gen);
3436 return true;
3437 }
3438 }
3439 }
3440
3441 /* Solve the dataflow equations. */
3442
3443 static void
3444 dse_step4 (void)
3445 {
3446 df_simple_dataflow (DF_BACKWARD, NULL, dse_confluence_0,
3447 dse_confluence_n, dse_transfer_function,
3448 all_blocks, df_get_postorder (DF_BACKWARD),
3449 df_get_n_blocks (DF_BACKWARD));
3450 if (dump_file && (dump_flags & TDF_DETAILS))
3451 {
3452 basic_block bb;
3453
3454 fprintf (dump_file, "\n\n*** Global dataflow info after analysis.\n");
3455 FOR_ALL_BB_FN (bb, cfun)
3456 {
3457 bb_info_t bb_info = bb_table[bb->index];
3458
3459 df_print_bb_index (bb, dump_file);
3460 if (bb_info->in)
3461 bitmap_print (dump_file, bb_info->in, " in: ", "\n");
3462 else
3463 fprintf (dump_file, " in: *MISSING*\n");
3464 if (bb_info->gen)
3465 bitmap_print (dump_file, bb_info->gen, " gen: ", "\n");
3466 else
3467 fprintf (dump_file, " gen: *MISSING*\n");
3468 if (bb_info->kill)
3469 bitmap_print (dump_file, bb_info->kill, " kill: ", "\n");
3470 else
3471 fprintf (dump_file, " kill: *MISSING*\n");
3472 if (bb_info->out)
3473 bitmap_print (dump_file, bb_info->out, " out: ", "\n");
3474 else
3475 fprintf (dump_file, " out: *MISSING*\n\n");
3476 }
3477 }
3478 }
3479
3480
3481 \f
3482 /*----------------------------------------------------------------------------
3483 Fifth step.
3484
3485 Delete the stores that can only be deleted using the global information.
3486 ----------------------------------------------------------------------------*/
3487
3488
3489 static void
3490 dse_step5_nospill (void)
3491 {
3492 basic_block bb;
3493 FOR_EACH_BB_FN (bb, cfun)
3494 {
3495 bb_info_t bb_info = bb_table[bb->index];
3496 insn_info_t insn_info = bb_info->last_insn;
3497 bitmap v = bb_info->out;
3498
3499 while (insn_info)
3500 {
3501 bool deleted = false;
3502 if (dump_file && insn_info->insn)
3503 {
3504 fprintf (dump_file, "starting to process insn %d\n",
3505 INSN_UID (insn_info->insn));
3506 bitmap_print (dump_file, v, " v: ", "\n");
3507 }
3508
3509 /* There may have been code deleted by the dce pass run before
3510 this phase. */
3511 if (insn_info->insn
3512 && INSN_P (insn_info->insn)
3513 && (!insn_info->cannot_delete)
3514 && (!bitmap_empty_p (v)))
3515 {
3516 store_info_t store_info = insn_info->store_rec;
3517
3518 /* Try to delete the current insn. */
3519 deleted = true;
3520
3521 /* Skip the clobbers. */
3522 while (!store_info->is_set)
3523 store_info = store_info->next;
3524
3525 if (store_info->alias_set)
3526 deleted = false;
3527 else
3528 {
3529 HOST_WIDE_INT i;
3530 group_info_t group_info
3531 = rtx_group_vec[store_info->group_id];
3532
3533 for (i = store_info->begin; i < store_info->end; i++)
3534 {
3535 int index = get_bitmap_index (group_info, i);
3536
3537 if (dump_file && (dump_flags & TDF_DETAILS))
3538 fprintf (dump_file, "i = %d, index = %d\n", (int)i, index);
3539 if (index == 0 || !bitmap_bit_p (v, index))
3540 {
3541 if (dump_file && (dump_flags & TDF_DETAILS))
3542 fprintf (dump_file, "failing at i = %d\n", (int)i);
3543 deleted = false;
3544 break;
3545 }
3546 }
3547 }
3548 if (deleted)
3549 {
3550 if (dbg_cnt (dse)
3551 && check_for_inc_dec_1 (insn_info))
3552 {
3553 delete_insn (insn_info->insn);
3554 insn_info->insn = NULL;
3555 globally_deleted++;
3556 }
3557 }
3558 }
3559 /* We do want to process the local info if the insn was
3560 deleted. For instance, if the insn did a wild read, we
3561 no longer need to trash the info. */
3562 if (insn_info->insn
3563 && INSN_P (insn_info->insn)
3564 && (!deleted))
3565 {
3566 scan_stores_nospill (insn_info->store_rec, v, NULL);
3567 if (insn_info->wild_read)
3568 {
3569 if (dump_file && (dump_flags & TDF_DETAILS))
3570 fprintf (dump_file, "wild read\n");
3571 bitmap_clear (v);
3572 }
3573 else if (insn_info->read_rec
3574 || insn_info->non_frame_wild_read)
3575 {
3576 if (dump_file && !insn_info->non_frame_wild_read)
3577 fprintf (dump_file, "regular read\n");
3578 else if (dump_file && (dump_flags & TDF_DETAILS))
3579 fprintf (dump_file, "non-frame wild read\n");
3580 scan_reads_nospill (insn_info, v, NULL);
3581 }
3582 }
3583
3584 insn_info = insn_info->prev_insn;
3585 }
3586 }
3587 }
3588
3589
3590 \f
3591 /*----------------------------------------------------------------------------
3592 Sixth step.
3593
3594 Delete stores made redundant by earlier stores (which store the same
3595 value) that couldn't be eliminated.
3596 ----------------------------------------------------------------------------*/
3597
3598 static void
3599 dse_step6 (void)
3600 {
3601 basic_block bb;
3602
3603 FOR_ALL_BB_FN (bb, cfun)
3604 {
3605 bb_info_t bb_info = bb_table[bb->index];
3606 insn_info_t insn_info = bb_info->last_insn;
3607
3608 while (insn_info)
3609 {
3610 /* There may have been code deleted by the dce pass run before
3611 this phase. */
3612 if (insn_info->insn
3613 && INSN_P (insn_info->insn)
3614 && !insn_info->cannot_delete)
3615 {
3616 store_info_t s_info = insn_info->store_rec;
3617
3618 while (s_info && !s_info->is_set)
3619 s_info = s_info->next;
3620 if (s_info
3621 && s_info->redundant_reason
3622 && s_info->redundant_reason->insn
3623 && INSN_P (s_info->redundant_reason->insn))
3624 {
3625 rtx rinsn = s_info->redundant_reason->insn;
3626 if (dump_file && (dump_flags & TDF_DETAILS))
3627 fprintf (dump_file, "Locally deleting insn %d "
3628 "because insn %d stores the "
3629 "same value and couldn't be "
3630 "eliminated\n",
3631 INSN_UID (insn_info->insn),
3632 INSN_UID (rinsn));
3633 delete_dead_store_insn (insn_info);
3634 }
3635 }
3636 insn_info = insn_info->prev_insn;
3637 }
3638 }
3639 }
3640 \f
3641 /*----------------------------------------------------------------------------
3642 Seventh step.
3643
3644 Destroy everything left standing.
3645 ----------------------------------------------------------------------------*/
3646
3647 static void
3648 dse_step7 (void)
3649 {
3650 bitmap_obstack_release (&dse_bitmap_obstack);
3651 obstack_free (&dse_obstack, NULL);
3652
3653 end_alias_analysis ();
3654 free (bb_table);
3655 delete rtx_group_table;
3656 rtx_group_table = NULL;
3657 rtx_group_vec.release ();
3658 BITMAP_FREE (all_blocks);
3659 BITMAP_FREE (scratch);
3660
3661 free_alloc_pool (rtx_store_info_pool);
3662 free_alloc_pool (read_info_pool);
3663 free_alloc_pool (insn_info_pool);
3664 free_alloc_pool (bb_info_pool);
3665 free_alloc_pool (rtx_group_info_pool);
3666 free_alloc_pool (deferred_change_pool);
3667 }
3668
3669
3670 /* -------------------------------------------------------------------------
3671 DSE
3672 ------------------------------------------------------------------------- */
3673
3674 /* Callback for running pass_rtl_dse. */
3675
3676 static unsigned int
3677 rest_of_handle_dse (void)
3678 {
3679 df_set_flags (DF_DEFER_INSN_RESCAN);
3680
3681 /* Need the notes since we must track live hardregs in the forwards
3682 direction. */
3683 df_note_add_problem ();
3684 df_analyze ();
3685
3686 dse_step0 ();
3687 dse_step1 ();
3688 dse_step2_init ();
3689 if (dse_step2_nospill ())
3690 {
3691 df_set_flags (DF_LR_RUN_DCE);
3692 df_analyze ();
3693 if (dump_file && (dump_flags & TDF_DETAILS))
3694 fprintf (dump_file, "doing global processing\n");
3695 dse_step3 (false);
3696 dse_step4 ();
3697 dse_step5_nospill ();
3698 }
3699
3700 dse_step6 ();
3701 dse_step7 ();
3702
3703 if (dump_file)
3704 fprintf (dump_file, "dse: local deletions = %d, global deletions = %d, spill deletions = %d\n",
3705 locally_deleted, globally_deleted, spill_deleted);
3706 return 0;
3707 }
3708
3709 namespace {
3710
3711 const pass_data pass_data_rtl_dse1 =
3712 {
3713 RTL_PASS, /* type */
3714 "dse1", /* name */
3715 OPTGROUP_NONE, /* optinfo_flags */
3716 TV_DSE1, /* tv_id */
3717 0, /* properties_required */
3718 0, /* properties_provided */
3719 0, /* properties_destroyed */
3720 0, /* todo_flags_start */
3721 TODO_df_finish, /* todo_flags_finish */
3722 };
3723
3724 class pass_rtl_dse1 : public rtl_opt_pass
3725 {
3726 public:
3727 pass_rtl_dse1 (gcc::context *ctxt)
3728 : rtl_opt_pass (pass_data_rtl_dse1, ctxt)
3729 {}
3730
3731 /* opt_pass methods: */
3732 virtual bool gate (function *)
3733 {
3734 return optimize > 0 && flag_dse && dbg_cnt (dse1);
3735 }
3736
3737 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3738
3739 }; // class pass_rtl_dse1
3740
3741 } // anon namespace
3742
3743 rtl_opt_pass *
3744 make_pass_rtl_dse1 (gcc::context *ctxt)
3745 {
3746 return new pass_rtl_dse1 (ctxt);
3747 }
3748
3749 namespace {
3750
3751 const pass_data pass_data_rtl_dse2 =
3752 {
3753 RTL_PASS, /* type */
3754 "dse2", /* name */
3755 OPTGROUP_NONE, /* optinfo_flags */
3756 TV_DSE2, /* tv_id */
3757 0, /* properties_required */
3758 0, /* properties_provided */
3759 0, /* properties_destroyed */
3760 0, /* todo_flags_start */
3761 TODO_df_finish, /* todo_flags_finish */
3762 };
3763
3764 class pass_rtl_dse2 : public rtl_opt_pass
3765 {
3766 public:
3767 pass_rtl_dse2 (gcc::context *ctxt)
3768 : rtl_opt_pass (pass_data_rtl_dse2, ctxt)
3769 {}
3770
3771 /* opt_pass methods: */
3772 virtual bool gate (function *)
3773 {
3774 return optimize > 0 && flag_dse && dbg_cnt (dse2);
3775 }
3776
3777 virtual unsigned int execute (function *) { return rest_of_handle_dse (); }
3778
3779 }; // class pass_rtl_dse2
3780
3781 } // anon namespace
3782
3783 rtl_opt_pass *
3784 make_pass_rtl_dse2 (gcc::context *ctxt)
3785 {
3786 return new pass_rtl_dse2 (ctxt);
3787 }