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