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