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