OSDN Git Service

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