OSDN Git Service

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