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