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