OSDN Git Service

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