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