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