OSDN Git Service

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