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