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