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