OSDN Git Service

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