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