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