xref: /dflybsd-src/contrib/gcc-8.0/gcc/df-problems.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Standard problems for dataflow support routines.
2*38fd1498Szrj    Copyright (C) 1999-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Originally contributed by Michael P. Hayes
4*38fd1498Szrj              (m.hayes@elec.canterbury.ac.nz, mhayes@redhat.com)
5*38fd1498Szrj    Major rewrite contributed by Danny Berlin (dberlin@dberlin.org)
6*38fd1498Szrj              and Kenneth Zadeck (zadeck@naturalbridge.com).
7*38fd1498Szrj 
8*38fd1498Szrj This file is part of GCC.
9*38fd1498Szrj 
10*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
11*38fd1498Szrj the terms of the GNU General Public License as published by the Free
12*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
13*38fd1498Szrj version.
14*38fd1498Szrj 
15*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
17*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18*38fd1498Szrj for more details.
19*38fd1498Szrj 
20*38fd1498Szrj You should have received a copy of the GNU General Public License
21*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
22*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
23*38fd1498Szrj 
24*38fd1498Szrj #include "config.h"
25*38fd1498Szrj #include "system.h"
26*38fd1498Szrj #include "coretypes.h"
27*38fd1498Szrj #include "backend.h"
28*38fd1498Szrj #include "target.h"
29*38fd1498Szrj #include "rtl.h"
30*38fd1498Szrj #include "df.h"
31*38fd1498Szrj #include "memmodel.h"
32*38fd1498Szrj #include "tm_p.h"
33*38fd1498Szrj #include "insn-config.h"
34*38fd1498Szrj #include "cfganal.h"
35*38fd1498Szrj #include "dce.h"
36*38fd1498Szrj #include "valtrack.h"
37*38fd1498Szrj #include "dumpfile.h"
38*38fd1498Szrj #include "rtl-iter.h"
39*38fd1498Szrj 
40*38fd1498Szrj /* Note that turning REG_DEAD_DEBUGGING on will cause
41*38fd1498Szrj    gcc.c-torture/unsorted/dump-noaddr.c to fail because it prints
42*38fd1498Szrj    addresses in the dumps.  */
43*38fd1498Szrj #define REG_DEAD_DEBUGGING 0
44*38fd1498Szrj 
45*38fd1498Szrj #define DF_SPARSE_THRESHOLD 32
46*38fd1498Szrj 
47*38fd1498Szrj static bitmap_head seen_in_block;
48*38fd1498Szrj static bitmap_head seen_in_insn;
49*38fd1498Szrj 
50*38fd1498Szrj /*----------------------------------------------------------------------------
51*38fd1498Szrj    Utility functions.
52*38fd1498Szrj ----------------------------------------------------------------------------*/
53*38fd1498Szrj 
54*38fd1498Szrj /* Generic versions to get the void* version of the block info.  Only
55*38fd1498Szrj    used inside the problem instance vectors.  */
56*38fd1498Szrj 
57*38fd1498Szrj /* Dump a def-use or use-def chain for REF to FILE.  */
58*38fd1498Szrj 
59*38fd1498Szrj void
df_chain_dump(struct df_link * link,FILE * file)60*38fd1498Szrj df_chain_dump (struct df_link *link, FILE *file)
61*38fd1498Szrj {
62*38fd1498Szrj   fprintf (file, "{ ");
63*38fd1498Szrj   for (; link; link = link->next)
64*38fd1498Szrj     {
65*38fd1498Szrj       fprintf (file, "%c%d(bb %d insn %d) ",
66*38fd1498Szrj 	       DF_REF_REG_DEF_P (link->ref)
67*38fd1498Szrj 	       ? 'd'
68*38fd1498Szrj 	       : (DF_REF_FLAGS (link->ref) & DF_REF_IN_NOTE) ? 'e' : 'u',
69*38fd1498Szrj 	       DF_REF_ID (link->ref),
70*38fd1498Szrj 	       DF_REF_BBNO (link->ref),
71*38fd1498Szrj 	       DF_REF_IS_ARTIFICIAL (link->ref)
72*38fd1498Szrj 	       ? -1 : DF_REF_INSN_UID (link->ref));
73*38fd1498Szrj     }
74*38fd1498Szrj   fprintf (file, "}");
75*38fd1498Szrj }
76*38fd1498Szrj 
77*38fd1498Szrj 
78*38fd1498Szrj /* Print some basic block info as part of df_dump.  */
79*38fd1498Szrj 
80*38fd1498Szrj void
df_print_bb_index(basic_block bb,FILE * file)81*38fd1498Szrj df_print_bb_index (basic_block bb, FILE *file)
82*38fd1498Szrj {
83*38fd1498Szrj   edge e;
84*38fd1498Szrj   edge_iterator ei;
85*38fd1498Szrj 
86*38fd1498Szrj   fprintf (file, "\n( ");
87*38fd1498Szrj     FOR_EACH_EDGE (e, ei, bb->preds)
88*38fd1498Szrj     {
89*38fd1498Szrj       basic_block pred = e->src;
90*38fd1498Szrj       fprintf (file, "%d%s ", pred->index, e->flags & EDGE_EH ? "(EH)" : "");
91*38fd1498Szrj     }
92*38fd1498Szrj   fprintf (file, ")->[%d]->( ", bb->index);
93*38fd1498Szrj   FOR_EACH_EDGE (e, ei, bb->succs)
94*38fd1498Szrj     {
95*38fd1498Szrj       basic_block succ = e->dest;
96*38fd1498Szrj       fprintf (file, "%d%s ", succ->index, e->flags & EDGE_EH ? "(EH)" : "");
97*38fd1498Szrj     }
98*38fd1498Szrj   fprintf (file, ")\n");
99*38fd1498Szrj }
100*38fd1498Szrj 
101*38fd1498Szrj 
102*38fd1498Szrj /*----------------------------------------------------------------------------
103*38fd1498Szrj    REACHING DEFINITIONS
104*38fd1498Szrj 
105*38fd1498Szrj    Find the locations in the function where each definition site for a
106*38fd1498Szrj    pseudo reaches.  In and out bitvectors are built for each basic
107*38fd1498Szrj    block.  The id field in the ref is used to index into these sets.
108*38fd1498Szrj    See df.h for details.
109*38fd1498Szrj 
110*38fd1498Szrj    If the DF_RD_PRUNE_DEAD_DEFS changeable flag is set, only DEFs reaching
111*38fd1498Szrj    existing uses are included in the global reaching DEFs set, or in other
112*38fd1498Szrj    words only DEFs that are still live.  This is a kind of pruned version
113*38fd1498Szrj    of the traditional reaching definitions problem that is much less
114*38fd1498Szrj    complex to compute and produces enough information to compute UD-chains.
115*38fd1498Szrj    In this context, live must be interpreted in the DF_LR sense: Uses that
116*38fd1498Szrj    are upward exposed but maybe not initialized on all paths through the
117*38fd1498Szrj    CFG.  For a USE that is not reached by a DEF on all paths, we still want
118*38fd1498Szrj    to make those DEFs that do reach the USE visible, and pruning based on
119*38fd1498Szrj    DF_LIVE would make that impossible.
120*38fd1498Szrj    ----------------------------------------------------------------------------*/
121*38fd1498Szrj 
122*38fd1498Szrj /* This problem plays a large number of games for the sake of
123*38fd1498Szrj    efficiency.
124*38fd1498Szrj 
125*38fd1498Szrj    1) The order of the bits in the bitvectors.  After the scanning
126*38fd1498Szrj    phase, all of the defs are sorted.  All of the defs for the reg 0
127*38fd1498Szrj    are first, followed by all defs for reg 1 and so on.
128*38fd1498Szrj 
129*38fd1498Szrj    2) There are two kill sets, one if the number of defs is less or
130*38fd1498Szrj    equal to DF_SPARSE_THRESHOLD and another if the number of defs is
131*38fd1498Szrj    greater.
132*38fd1498Szrj 
133*38fd1498Szrj    <= : Data is built directly in the kill set.
134*38fd1498Szrj 
135*38fd1498Szrj    > : One level of indirection is used to keep from generating long
136*38fd1498Szrj    strings of 1 bits in the kill sets.  Bitvectors that are indexed
137*38fd1498Szrj    by the regnum are used to represent that there is a killing def
138*38fd1498Szrj    for the register.  The confluence and transfer functions use
139*38fd1498Szrj    these along with the bitmap_clear_range call to remove ranges of
140*38fd1498Szrj    bits without actually generating a knockout vector.
141*38fd1498Szrj 
142*38fd1498Szrj    The kill and sparse_kill and the dense_invalidated_by_call and
143*38fd1498Szrj    sparse_invalidated_by_call both play this game.  */
144*38fd1498Szrj 
145*38fd1498Szrj /* Private data used to compute the solution for this problem.  These
146*38fd1498Szrj    data structures are not accessible outside of this module.  */
147*38fd1498Szrj struct df_rd_problem_data
148*38fd1498Szrj {
149*38fd1498Szrj   /* The set of defs to regs invalidated by call.  */
150*38fd1498Szrj   bitmap_head sparse_invalidated_by_call;
151*38fd1498Szrj   /* The set of defs to regs invalidate by call for rd.  */
152*38fd1498Szrj   bitmap_head dense_invalidated_by_call;
153*38fd1498Szrj   /* An obstack for the bitmaps we need for this problem.  */
154*38fd1498Szrj   bitmap_obstack rd_bitmaps;
155*38fd1498Szrj };
156*38fd1498Szrj 
157*38fd1498Szrj 
158*38fd1498Szrj /* Free basic block info.  */
159*38fd1498Szrj 
160*38fd1498Szrj static void
df_rd_free_bb_info(basic_block bb ATTRIBUTE_UNUSED,void * vbb_info)161*38fd1498Szrj df_rd_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
162*38fd1498Szrj 		    void *vbb_info)
163*38fd1498Szrj {
164*38fd1498Szrj   struct df_rd_bb_info *bb_info = (struct df_rd_bb_info *) vbb_info;
165*38fd1498Szrj   if (bb_info)
166*38fd1498Szrj     {
167*38fd1498Szrj       bitmap_clear (&bb_info->kill);
168*38fd1498Szrj       bitmap_clear (&bb_info->sparse_kill);
169*38fd1498Szrj       bitmap_clear (&bb_info->gen);
170*38fd1498Szrj       bitmap_clear (&bb_info->in);
171*38fd1498Szrj       bitmap_clear (&bb_info->out);
172*38fd1498Szrj     }
173*38fd1498Szrj }
174*38fd1498Szrj 
175*38fd1498Szrj 
176*38fd1498Szrj /* Allocate or reset bitmaps for DF_RD blocks. The solution bits are
177*38fd1498Szrj    not touched unless the block is new.  */
178*38fd1498Szrj 
179*38fd1498Szrj static void
df_rd_alloc(bitmap all_blocks)180*38fd1498Szrj df_rd_alloc (bitmap all_blocks)
181*38fd1498Szrj {
182*38fd1498Szrj   unsigned int bb_index;
183*38fd1498Szrj   bitmap_iterator bi;
184*38fd1498Szrj   struct df_rd_problem_data *problem_data;
185*38fd1498Szrj 
186*38fd1498Szrj   if (df_rd->problem_data)
187*38fd1498Szrj     {
188*38fd1498Szrj       problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
189*38fd1498Szrj       bitmap_clear (&problem_data->sparse_invalidated_by_call);
190*38fd1498Szrj       bitmap_clear (&problem_data->dense_invalidated_by_call);
191*38fd1498Szrj     }
192*38fd1498Szrj   else
193*38fd1498Szrj     {
194*38fd1498Szrj       problem_data = XNEW (struct df_rd_problem_data);
195*38fd1498Szrj       df_rd->problem_data = problem_data;
196*38fd1498Szrj 
197*38fd1498Szrj       bitmap_obstack_initialize (&problem_data->rd_bitmaps);
198*38fd1498Szrj       bitmap_initialize (&problem_data->sparse_invalidated_by_call,
199*38fd1498Szrj 			 &problem_data->rd_bitmaps);
200*38fd1498Szrj       bitmap_initialize (&problem_data->dense_invalidated_by_call,
201*38fd1498Szrj 			 &problem_data->rd_bitmaps);
202*38fd1498Szrj     }
203*38fd1498Szrj 
204*38fd1498Szrj   df_grow_bb_info (df_rd);
205*38fd1498Szrj 
206*38fd1498Szrj   /* Because of the clustering of all use sites for the same pseudo,
207*38fd1498Szrj      we have to process all of the blocks before doing the analysis.  */
208*38fd1498Szrj 
209*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
210*38fd1498Szrj     {
211*38fd1498Szrj       struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
212*38fd1498Szrj 
213*38fd1498Szrj       /* When bitmaps are already initialized, just clear them.  */
214*38fd1498Szrj       if (bb_info->kill.obstack)
215*38fd1498Szrj 	{
216*38fd1498Szrj 	  bitmap_clear (&bb_info->kill);
217*38fd1498Szrj 	  bitmap_clear (&bb_info->sparse_kill);
218*38fd1498Szrj 	  bitmap_clear (&bb_info->gen);
219*38fd1498Szrj 	}
220*38fd1498Szrj       else
221*38fd1498Szrj 	{
222*38fd1498Szrj 	  bitmap_initialize (&bb_info->kill, &problem_data->rd_bitmaps);
223*38fd1498Szrj 	  bitmap_initialize (&bb_info->sparse_kill, &problem_data->rd_bitmaps);
224*38fd1498Szrj 	  bitmap_initialize (&bb_info->gen, &problem_data->rd_bitmaps);
225*38fd1498Szrj 	  bitmap_initialize (&bb_info->in, &problem_data->rd_bitmaps);
226*38fd1498Szrj 	  bitmap_initialize (&bb_info->out, &problem_data->rd_bitmaps);
227*38fd1498Szrj 	}
228*38fd1498Szrj     }
229*38fd1498Szrj   df_rd->optional_p = true;
230*38fd1498Szrj }
231*38fd1498Szrj 
232*38fd1498Szrj 
233*38fd1498Szrj /* Add the effect of the top artificial defs of BB to the reaching definitions
234*38fd1498Szrj    bitmap LOCAL_RD.  */
235*38fd1498Szrj 
236*38fd1498Szrj void
df_rd_simulate_artificial_defs_at_top(basic_block bb,bitmap local_rd)237*38fd1498Szrj df_rd_simulate_artificial_defs_at_top (basic_block bb, bitmap local_rd)
238*38fd1498Szrj {
239*38fd1498Szrj   int bb_index = bb->index;
240*38fd1498Szrj   df_ref def;
241*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
242*38fd1498Szrj     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
243*38fd1498Szrj       {
244*38fd1498Szrj 	unsigned int dregno = DF_REF_REGNO (def);
245*38fd1498Szrj 	if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
246*38fd1498Szrj 	  bitmap_clear_range (local_rd,
247*38fd1498Szrj 			      DF_DEFS_BEGIN (dregno),
248*38fd1498Szrj 			      DF_DEFS_COUNT (dregno));
249*38fd1498Szrj 	bitmap_set_bit (local_rd, DF_REF_ID (def));
250*38fd1498Szrj       }
251*38fd1498Szrj }
252*38fd1498Szrj 
253*38fd1498Szrj /* Add the effect of the defs of INSN to the reaching definitions bitmap
254*38fd1498Szrj    LOCAL_RD.  */
255*38fd1498Szrj 
256*38fd1498Szrj void
df_rd_simulate_one_insn(basic_block bb ATTRIBUTE_UNUSED,rtx_insn * insn,bitmap local_rd)257*38fd1498Szrj df_rd_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
258*38fd1498Szrj 			 bitmap local_rd)
259*38fd1498Szrj {
260*38fd1498Szrj   df_ref def;
261*38fd1498Szrj 
262*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
263*38fd1498Szrj     {
264*38fd1498Szrj       unsigned int dregno = DF_REF_REGNO (def);
265*38fd1498Szrj       if ((!(df->changeable_flags & DF_NO_HARD_REGS))
266*38fd1498Szrj           || (dregno >= FIRST_PSEUDO_REGISTER))
267*38fd1498Szrj         {
268*38fd1498Szrj           if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
269*38fd1498Szrj 	    bitmap_clear_range (local_rd,
270*38fd1498Szrj 				DF_DEFS_BEGIN (dregno),
271*38fd1498Szrj 				DF_DEFS_COUNT (dregno));
272*38fd1498Szrj 	  if (!(DF_REF_FLAGS (def)
273*38fd1498Szrj 		& (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
274*38fd1498Szrj 	    bitmap_set_bit (local_rd, DF_REF_ID (def));
275*38fd1498Szrj 	}
276*38fd1498Szrj     }
277*38fd1498Szrj }
278*38fd1498Szrj 
279*38fd1498Szrj /* Process a list of DEFs for df_rd_bb_local_compute.  This is a bit
280*38fd1498Szrj    more complicated than just simulating, because we must produce the
281*38fd1498Szrj    gen and kill sets and hence deal with the two possible representations
282*38fd1498Szrj    of kill sets.   */
283*38fd1498Szrj 
284*38fd1498Szrj static void
df_rd_bb_local_compute_process_def(struct df_rd_bb_info * bb_info,df_ref def,int top_flag)285*38fd1498Szrj df_rd_bb_local_compute_process_def (struct df_rd_bb_info *bb_info,
286*38fd1498Szrj 				    df_ref def,
287*38fd1498Szrj 				    int top_flag)
288*38fd1498Szrj {
289*38fd1498Szrj   for (; def; def = DF_REF_NEXT_LOC (def))
290*38fd1498Szrj     {
291*38fd1498Szrj       if (top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
292*38fd1498Szrj 	{
293*38fd1498Szrj 	  unsigned int regno = DF_REF_REGNO (def);
294*38fd1498Szrj 	  unsigned int begin = DF_DEFS_BEGIN (regno);
295*38fd1498Szrj 	  unsigned int n_defs = DF_DEFS_COUNT (regno);
296*38fd1498Szrj 
297*38fd1498Szrj 	  if ((!(df->changeable_flags & DF_NO_HARD_REGS))
298*38fd1498Szrj 	      || (regno >= FIRST_PSEUDO_REGISTER))
299*38fd1498Szrj 	    {
300*38fd1498Szrj 	      /* Only the last def(s) for a regno in the block has any
301*38fd1498Szrj 		 effect.  */
302*38fd1498Szrj 	      if (!bitmap_bit_p (&seen_in_block, regno))
303*38fd1498Szrj 		{
304*38fd1498Szrj 		  /* The first def for regno in insn gets to knock out the
305*38fd1498Szrj 		     defs from other instructions.  */
306*38fd1498Szrj 		  if ((!bitmap_bit_p (&seen_in_insn, regno))
307*38fd1498Szrj 		      /* If the def is to only part of the reg, it does
308*38fd1498Szrj 			 not kill the other defs that reach here.  */
309*38fd1498Szrj 		      && (!(DF_REF_FLAGS (def) &
310*38fd1498Szrj 			    (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))))
311*38fd1498Szrj 		    {
312*38fd1498Szrj 		      if (n_defs > DF_SPARSE_THRESHOLD)
313*38fd1498Szrj 			{
314*38fd1498Szrj 			  bitmap_set_bit (&bb_info->sparse_kill, regno);
315*38fd1498Szrj 			  bitmap_clear_range (&bb_info->gen, begin, n_defs);
316*38fd1498Szrj 			}
317*38fd1498Szrj 		      else
318*38fd1498Szrj 			{
319*38fd1498Szrj 			  bitmap_set_range (&bb_info->kill, begin, n_defs);
320*38fd1498Szrj 			  bitmap_clear_range (&bb_info->gen, begin, n_defs);
321*38fd1498Szrj 			}
322*38fd1498Szrj 		    }
323*38fd1498Szrj 
324*38fd1498Szrj 		  bitmap_set_bit (&seen_in_insn, regno);
325*38fd1498Szrj 		  /* All defs for regno in the instruction may be put into
326*38fd1498Szrj 		     the gen set.  */
327*38fd1498Szrj 		  if (!(DF_REF_FLAGS (def)
328*38fd1498Szrj 			& (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
329*38fd1498Szrj 		    bitmap_set_bit (&bb_info->gen, DF_REF_ID (def));
330*38fd1498Szrj 		}
331*38fd1498Szrj 	    }
332*38fd1498Szrj 	}
333*38fd1498Szrj     }
334*38fd1498Szrj }
335*38fd1498Szrj 
336*38fd1498Szrj /* Compute local reaching def info for basic block BB.  */
337*38fd1498Szrj 
338*38fd1498Szrj static void
df_rd_bb_local_compute(unsigned int bb_index)339*38fd1498Szrj df_rd_bb_local_compute (unsigned int bb_index)
340*38fd1498Szrj {
341*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
342*38fd1498Szrj   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
343*38fd1498Szrj   rtx_insn *insn;
344*38fd1498Szrj 
345*38fd1498Szrj   bitmap_clear (&seen_in_block);
346*38fd1498Szrj   bitmap_clear (&seen_in_insn);
347*38fd1498Szrj 
348*38fd1498Szrj   /* Artificials are only hard regs.  */
349*38fd1498Szrj   if (!(df->changeable_flags & DF_NO_HARD_REGS))
350*38fd1498Szrj     df_rd_bb_local_compute_process_def (bb_info,
351*38fd1498Szrj 					df_get_artificial_defs (bb_index),
352*38fd1498Szrj 					0);
353*38fd1498Szrj 
354*38fd1498Szrj   FOR_BB_INSNS_REVERSE (bb, insn)
355*38fd1498Szrj     {
356*38fd1498Szrj       unsigned int uid = INSN_UID (insn);
357*38fd1498Szrj 
358*38fd1498Szrj       if (!INSN_P (insn))
359*38fd1498Szrj 	continue;
360*38fd1498Szrj 
361*38fd1498Szrj       df_rd_bb_local_compute_process_def (bb_info,
362*38fd1498Szrj 					  DF_INSN_UID_DEFS (uid), 0);
363*38fd1498Szrj 
364*38fd1498Szrj       /* This complex dance with the two bitmaps is required because
365*38fd1498Szrj 	 instructions can assign twice to the same pseudo.  This
366*38fd1498Szrj 	 generally happens with calls that will have one def for the
367*38fd1498Szrj 	 result and another def for the clobber.  If only one vector
368*38fd1498Szrj 	 is used and the clobber goes first, the result will be
369*38fd1498Szrj 	 lost.  */
370*38fd1498Szrj       bitmap_ior_into (&seen_in_block, &seen_in_insn);
371*38fd1498Szrj       bitmap_clear (&seen_in_insn);
372*38fd1498Szrj     }
373*38fd1498Szrj 
374*38fd1498Szrj   /* Process the artificial defs at the top of the block last since we
375*38fd1498Szrj      are going backwards through the block and these are logically at
376*38fd1498Szrj      the start.  */
377*38fd1498Szrj   if (!(df->changeable_flags & DF_NO_HARD_REGS))
378*38fd1498Szrj     df_rd_bb_local_compute_process_def (bb_info,
379*38fd1498Szrj 					df_get_artificial_defs (bb_index),
380*38fd1498Szrj 					DF_REF_AT_TOP);
381*38fd1498Szrj }
382*38fd1498Szrj 
383*38fd1498Szrj 
384*38fd1498Szrj /* Compute local reaching def info for each basic block within BLOCKS.  */
385*38fd1498Szrj 
386*38fd1498Szrj static void
df_rd_local_compute(bitmap all_blocks)387*38fd1498Szrj df_rd_local_compute (bitmap all_blocks)
388*38fd1498Szrj {
389*38fd1498Szrj   unsigned int bb_index;
390*38fd1498Szrj   bitmap_iterator bi;
391*38fd1498Szrj   unsigned int regno;
392*38fd1498Szrj   struct df_rd_problem_data *problem_data
393*38fd1498Szrj     = (struct df_rd_problem_data *) df_rd->problem_data;
394*38fd1498Szrj   bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
395*38fd1498Szrj   bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
396*38fd1498Szrj 
397*38fd1498Szrj   bitmap_initialize (&seen_in_block, &df_bitmap_obstack);
398*38fd1498Szrj   bitmap_initialize (&seen_in_insn, &df_bitmap_obstack);
399*38fd1498Szrj 
400*38fd1498Szrj   df_maybe_reorganize_def_refs (DF_REF_ORDER_BY_REG);
401*38fd1498Szrj 
402*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
403*38fd1498Szrj     {
404*38fd1498Szrj       df_rd_bb_local_compute (bb_index);
405*38fd1498Szrj     }
406*38fd1498Szrj 
407*38fd1498Szrj   /* Set up the knockout bit vectors to be applied across EH_EDGES.  */
408*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (regs_invalidated_by_call_regset, 0, regno, bi)
409*38fd1498Szrj     {
410*38fd1498Szrj       if (! HARD_REGISTER_NUM_P (regno)
411*38fd1498Szrj 	  || !(df->changeable_flags & DF_NO_HARD_REGS))
412*38fd1498Szrj 	{
413*38fd1498Szrj 	  if (DF_DEFS_COUNT (regno) > DF_SPARSE_THRESHOLD)
414*38fd1498Szrj 	    bitmap_set_bit (sparse_invalidated, regno);
415*38fd1498Szrj 	  else
416*38fd1498Szrj 	    bitmap_set_range (dense_invalidated,
417*38fd1498Szrj 			      DF_DEFS_BEGIN (regno),
418*38fd1498Szrj 			      DF_DEFS_COUNT (regno));
419*38fd1498Szrj 	}
420*38fd1498Szrj     }
421*38fd1498Szrj 
422*38fd1498Szrj   bitmap_clear (&seen_in_block);
423*38fd1498Szrj   bitmap_clear (&seen_in_insn);
424*38fd1498Szrj }
425*38fd1498Szrj 
426*38fd1498Szrj 
427*38fd1498Szrj /* Initialize the solution bit vectors for problem.  */
428*38fd1498Szrj 
429*38fd1498Szrj static void
df_rd_init_solution(bitmap all_blocks)430*38fd1498Szrj df_rd_init_solution (bitmap all_blocks)
431*38fd1498Szrj {
432*38fd1498Szrj   unsigned int bb_index;
433*38fd1498Szrj   bitmap_iterator bi;
434*38fd1498Szrj 
435*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
436*38fd1498Szrj     {
437*38fd1498Szrj       struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
438*38fd1498Szrj 
439*38fd1498Szrj       bitmap_copy (&bb_info->out, &bb_info->gen);
440*38fd1498Szrj       bitmap_clear (&bb_info->in);
441*38fd1498Szrj     }
442*38fd1498Szrj }
443*38fd1498Szrj 
444*38fd1498Szrj /* In of target gets or of out of source.  */
445*38fd1498Szrj 
446*38fd1498Szrj static bool
df_rd_confluence_n(edge e)447*38fd1498Szrj df_rd_confluence_n (edge e)
448*38fd1498Szrj {
449*38fd1498Szrj   bitmap op1 = &df_rd_get_bb_info (e->dest->index)->in;
450*38fd1498Szrj   bitmap op2 = &df_rd_get_bb_info (e->src->index)->out;
451*38fd1498Szrj   bool changed = false;
452*38fd1498Szrj 
453*38fd1498Szrj   if (e->flags & EDGE_FAKE)
454*38fd1498Szrj     return false;
455*38fd1498Szrj 
456*38fd1498Szrj   if (e->flags & EDGE_EH)
457*38fd1498Szrj     {
458*38fd1498Szrj       struct df_rd_problem_data *problem_data
459*38fd1498Szrj 	= (struct df_rd_problem_data *) df_rd->problem_data;
460*38fd1498Szrj       bitmap sparse_invalidated = &problem_data->sparse_invalidated_by_call;
461*38fd1498Szrj       bitmap dense_invalidated = &problem_data->dense_invalidated_by_call;
462*38fd1498Szrj       bitmap_iterator bi;
463*38fd1498Szrj       unsigned int regno;
464*38fd1498Szrj 
465*38fd1498Szrj       auto_bitmap tmp (&df_bitmap_obstack);
466*38fd1498Szrj       bitmap_and_compl (tmp, op2, dense_invalidated);
467*38fd1498Szrj 
468*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (sparse_invalidated, 0, regno, bi)
469*38fd1498Szrj  	{
470*38fd1498Szrj 	  bitmap_clear_range (tmp,
471*38fd1498Szrj  			      DF_DEFS_BEGIN (regno),
472*38fd1498Szrj  			      DF_DEFS_COUNT (regno));
473*38fd1498Szrj 	}
474*38fd1498Szrj       changed |= bitmap_ior_into (op1, tmp);
475*38fd1498Szrj       return changed;
476*38fd1498Szrj     }
477*38fd1498Szrj   else
478*38fd1498Szrj     return bitmap_ior_into (op1, op2);
479*38fd1498Szrj }
480*38fd1498Szrj 
481*38fd1498Szrj 
482*38fd1498Szrj /* Transfer function.  */
483*38fd1498Szrj 
484*38fd1498Szrj static bool
df_rd_transfer_function(int bb_index)485*38fd1498Szrj df_rd_transfer_function (int bb_index)
486*38fd1498Szrj {
487*38fd1498Szrj   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
488*38fd1498Szrj   unsigned int regno;
489*38fd1498Szrj   bitmap_iterator bi;
490*38fd1498Szrj   bitmap in = &bb_info->in;
491*38fd1498Szrj   bitmap out = &bb_info->out;
492*38fd1498Szrj   bitmap gen = &bb_info->gen;
493*38fd1498Szrj   bitmap kill = &bb_info->kill;
494*38fd1498Szrj   bitmap sparse_kill = &bb_info->sparse_kill;
495*38fd1498Szrj   bool changed = false;
496*38fd1498Szrj 
497*38fd1498Szrj   if (bitmap_empty_p (sparse_kill))
498*38fd1498Szrj     changed = bitmap_ior_and_compl (out, gen, in, kill);
499*38fd1498Szrj   else
500*38fd1498Szrj     {
501*38fd1498Szrj       struct df_rd_problem_data *problem_data;
502*38fd1498Szrj       bitmap_head tmp;
503*38fd1498Szrj 
504*38fd1498Szrj       /* Note that TMP is _not_ a temporary bitmap if we end up replacing
505*38fd1498Szrj 	 OUT with TMP.  Therefore, allocate TMP in the RD bitmaps obstack.  */
506*38fd1498Szrj       problem_data = (struct df_rd_problem_data *) df_rd->problem_data;
507*38fd1498Szrj       bitmap_initialize (&tmp, &problem_data->rd_bitmaps);
508*38fd1498Szrj 
509*38fd1498Szrj       bitmap_and_compl (&tmp, in, kill);
510*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (sparse_kill, 0, regno, bi)
511*38fd1498Szrj 	{
512*38fd1498Szrj 	  bitmap_clear_range (&tmp,
513*38fd1498Szrj 			      DF_DEFS_BEGIN (regno),
514*38fd1498Szrj 			      DF_DEFS_COUNT (regno));
515*38fd1498Szrj 	}
516*38fd1498Szrj       bitmap_ior_into (&tmp, gen);
517*38fd1498Szrj       changed = !bitmap_equal_p (&tmp, out);
518*38fd1498Szrj       if (changed)
519*38fd1498Szrj 	bitmap_move (out, &tmp);
520*38fd1498Szrj       else
521*38fd1498Szrj 	bitmap_clear (&tmp);
522*38fd1498Szrj     }
523*38fd1498Szrj 
524*38fd1498Szrj   if (df->changeable_flags & DF_RD_PRUNE_DEAD_DEFS)
525*38fd1498Szrj     {
526*38fd1498Szrj       /* Create a mask of DEFs for all registers live at the end of this
527*38fd1498Szrj 	 basic block, and mask out DEFs of registers that are not live.
528*38fd1498Szrj 	 Computing the mask looks costly, but the benefit of the pruning
529*38fd1498Szrj 	 outweighs the cost.  */
530*38fd1498Szrj       struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
531*38fd1498Szrj       bitmap regs_live_out = &df_lr_get_bb_info (bb_index)->out;
532*38fd1498Szrj       bitmap live_defs = BITMAP_ALLOC (&df_bitmap_obstack);
533*38fd1498Szrj       unsigned int regno;
534*38fd1498Szrj       bitmap_iterator bi;
535*38fd1498Szrj 
536*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (regs_live_out, 0, regno, bi)
537*38fd1498Szrj 	bitmap_set_range (live_defs,
538*38fd1498Szrj 			  DF_DEFS_BEGIN (regno),
539*38fd1498Szrj 			  DF_DEFS_COUNT (regno));
540*38fd1498Szrj       changed |= bitmap_and_into (&bb_info->out, live_defs);
541*38fd1498Szrj       BITMAP_FREE (live_defs);
542*38fd1498Szrj     }
543*38fd1498Szrj 
544*38fd1498Szrj   return changed;
545*38fd1498Szrj }
546*38fd1498Szrj 
547*38fd1498Szrj /* Free all storage associated with the problem.  */
548*38fd1498Szrj 
549*38fd1498Szrj static void
df_rd_free(void)550*38fd1498Szrj df_rd_free (void)
551*38fd1498Szrj {
552*38fd1498Szrj   struct df_rd_problem_data *problem_data
553*38fd1498Szrj     = (struct df_rd_problem_data *) df_rd->problem_data;
554*38fd1498Szrj 
555*38fd1498Szrj   if (problem_data)
556*38fd1498Szrj     {
557*38fd1498Szrj       bitmap_obstack_release (&problem_data->rd_bitmaps);
558*38fd1498Szrj 
559*38fd1498Szrj       df_rd->block_info_size = 0;
560*38fd1498Szrj       free (df_rd->block_info);
561*38fd1498Szrj       df_rd->block_info = NULL;
562*38fd1498Szrj       free (df_rd->problem_data);
563*38fd1498Szrj     }
564*38fd1498Szrj   free (df_rd);
565*38fd1498Szrj }
566*38fd1498Szrj 
567*38fd1498Szrj 
568*38fd1498Szrj /* Debugging info.  */
569*38fd1498Szrj 
570*38fd1498Szrj static void
df_rd_start_dump(FILE * file)571*38fd1498Szrj df_rd_start_dump (FILE *file)
572*38fd1498Szrj {
573*38fd1498Szrj   struct df_rd_problem_data *problem_data
574*38fd1498Szrj     = (struct df_rd_problem_data *) df_rd->problem_data;
575*38fd1498Szrj   unsigned int m = DF_REG_SIZE (df);
576*38fd1498Szrj   unsigned int regno;
577*38fd1498Szrj 
578*38fd1498Szrj   if (!df_rd->block_info)
579*38fd1498Szrj     return;
580*38fd1498Szrj 
581*38fd1498Szrj   fprintf (file, ";; Reaching defs:\n");
582*38fd1498Szrj 
583*38fd1498Szrj   fprintf (file, ";;  sparse invalidated \t");
584*38fd1498Szrj   dump_bitmap (file, &problem_data->sparse_invalidated_by_call);
585*38fd1498Szrj   fprintf (file, ";;  dense invalidated \t");
586*38fd1498Szrj   dump_bitmap (file, &problem_data->dense_invalidated_by_call);
587*38fd1498Szrj 
588*38fd1498Szrj   fprintf (file, ";;  reg->defs[] map:\t");
589*38fd1498Szrj   for (regno = 0; regno < m; regno++)
590*38fd1498Szrj     if (DF_DEFS_COUNT (regno))
591*38fd1498Szrj       fprintf (file, "%d[%d,%d] ", regno,
592*38fd1498Szrj 	       DF_DEFS_BEGIN (regno),
593*38fd1498Szrj 	       DF_DEFS_BEGIN (regno) + DF_DEFS_COUNT (regno) - 1);
594*38fd1498Szrj   fprintf (file, "\n");
595*38fd1498Szrj }
596*38fd1498Szrj 
597*38fd1498Szrj 
598*38fd1498Szrj static void
df_rd_dump_defs_set(bitmap defs_set,const char * prefix,FILE * file)599*38fd1498Szrj df_rd_dump_defs_set (bitmap defs_set, const char *prefix, FILE *file)
600*38fd1498Szrj {
601*38fd1498Szrj   bitmap_head tmp;
602*38fd1498Szrj   unsigned int regno;
603*38fd1498Szrj   unsigned int m = DF_REG_SIZE (df);
604*38fd1498Szrj   bool first_reg = true;
605*38fd1498Szrj 
606*38fd1498Szrj   fprintf (file, "%s\t(%d) ", prefix, (int) bitmap_count_bits (defs_set));
607*38fd1498Szrj 
608*38fd1498Szrj   bitmap_initialize (&tmp, &df_bitmap_obstack);
609*38fd1498Szrj   for (regno = 0; regno < m; regno++)
610*38fd1498Szrj     {
611*38fd1498Szrj       if (HARD_REGISTER_NUM_P (regno)
612*38fd1498Szrj 	  && (df->changeable_flags & DF_NO_HARD_REGS))
613*38fd1498Szrj 	continue;
614*38fd1498Szrj       bitmap_set_range (&tmp, DF_DEFS_BEGIN (regno), DF_DEFS_COUNT (regno));
615*38fd1498Szrj       bitmap_and_into (&tmp, defs_set);
616*38fd1498Szrj       if (! bitmap_empty_p (&tmp))
617*38fd1498Szrj 	{
618*38fd1498Szrj 	  bitmap_iterator bi;
619*38fd1498Szrj 	  unsigned int ix;
620*38fd1498Szrj 	  bool first_def = true;
621*38fd1498Szrj 
622*38fd1498Szrj 	  if (! first_reg)
623*38fd1498Szrj 	    fprintf (file, ",");
624*38fd1498Szrj 	  first_reg = false;
625*38fd1498Szrj 
626*38fd1498Szrj 	  fprintf (file, "%u[", regno);
627*38fd1498Szrj 	  EXECUTE_IF_SET_IN_BITMAP (&tmp, 0, ix, bi)
628*38fd1498Szrj 	    {
629*38fd1498Szrj 	      fprintf (file, "%s%u", first_def ? "" : ",", ix);
630*38fd1498Szrj 	      first_def = false;
631*38fd1498Szrj 	    }
632*38fd1498Szrj 	  fprintf (file, "]");
633*38fd1498Szrj 	}
634*38fd1498Szrj       bitmap_clear (&tmp);
635*38fd1498Szrj     }
636*38fd1498Szrj 
637*38fd1498Szrj   fprintf (file, "\n");
638*38fd1498Szrj   bitmap_clear (&tmp);
639*38fd1498Szrj }
640*38fd1498Szrj 
641*38fd1498Szrj /* Debugging info at top of bb.  */
642*38fd1498Szrj 
643*38fd1498Szrj static void
df_rd_top_dump(basic_block bb,FILE * file)644*38fd1498Szrj df_rd_top_dump (basic_block bb, FILE *file)
645*38fd1498Szrj {
646*38fd1498Szrj   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
647*38fd1498Szrj   if (!bb_info)
648*38fd1498Szrj     return;
649*38fd1498Szrj 
650*38fd1498Szrj   df_rd_dump_defs_set (&bb_info->in, ";; rd  in  ", file);
651*38fd1498Szrj   df_rd_dump_defs_set (&bb_info->gen, ";; rd  gen ", file);
652*38fd1498Szrj   df_rd_dump_defs_set (&bb_info->kill, ";; rd  kill", file);
653*38fd1498Szrj }
654*38fd1498Szrj 
655*38fd1498Szrj 
656*38fd1498Szrj /* Debugging info at bottom of bb.  */
657*38fd1498Szrj 
658*38fd1498Szrj static void
df_rd_bottom_dump(basic_block bb,FILE * file)659*38fd1498Szrj df_rd_bottom_dump (basic_block bb, FILE *file)
660*38fd1498Szrj {
661*38fd1498Szrj   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb->index);
662*38fd1498Szrj   if (!bb_info)
663*38fd1498Szrj     return;
664*38fd1498Szrj 
665*38fd1498Szrj   df_rd_dump_defs_set (&bb_info->out, ";; rd  out ", file);
666*38fd1498Szrj }
667*38fd1498Szrj 
668*38fd1498Szrj /* All of the information associated with every instance of the problem.  */
669*38fd1498Szrj 
670*38fd1498Szrj static const struct df_problem problem_RD =
671*38fd1498Szrj {
672*38fd1498Szrj   DF_RD,                      /* Problem id.  */
673*38fd1498Szrj   DF_FORWARD,                 /* Direction.  */
674*38fd1498Szrj   df_rd_alloc,                /* Allocate the problem specific data.  */
675*38fd1498Szrj   NULL,                       /* Reset global information.  */
676*38fd1498Szrj   df_rd_free_bb_info,         /* Free basic block info.  */
677*38fd1498Szrj   df_rd_local_compute,        /* Local compute function.  */
678*38fd1498Szrj   df_rd_init_solution,        /* Init the solution specific data.  */
679*38fd1498Szrj   df_worklist_dataflow,       /* Worklist solver.  */
680*38fd1498Szrj   NULL,                       /* Confluence operator 0.  */
681*38fd1498Szrj   df_rd_confluence_n,         /* Confluence operator n.  */
682*38fd1498Szrj   df_rd_transfer_function,    /* Transfer function.  */
683*38fd1498Szrj   NULL,                       /* Finalize function.  */
684*38fd1498Szrj   df_rd_free,                 /* Free all of the problem information.  */
685*38fd1498Szrj   df_rd_free,                 /* Remove this problem from the stack of dataflow problems.  */
686*38fd1498Szrj   df_rd_start_dump,           /* Debugging.  */
687*38fd1498Szrj   df_rd_top_dump,             /* Debugging start block.  */
688*38fd1498Szrj   df_rd_bottom_dump,          /* Debugging end block.  */
689*38fd1498Szrj   NULL,                       /* Debugging start insn.  */
690*38fd1498Szrj   NULL,                       /* Debugging end insn.  */
691*38fd1498Szrj   NULL,                       /* Incremental solution verify start.  */
692*38fd1498Szrj   NULL,                       /* Incremental solution verify end.  */
693*38fd1498Szrj   NULL,                       /* Dependent problem.  */
694*38fd1498Szrj   sizeof (struct df_rd_bb_info),/* Size of entry of block_info array.  */
695*38fd1498Szrj   TV_DF_RD,                   /* Timing variable.  */
696*38fd1498Szrj   true                        /* Reset blocks on dropping out of blocks_to_analyze.  */
697*38fd1498Szrj };
698*38fd1498Szrj 
699*38fd1498Szrj 
700*38fd1498Szrj 
701*38fd1498Szrj /* Create a new RD instance and add it to the existing instance
702*38fd1498Szrj    of DF.  */
703*38fd1498Szrj 
704*38fd1498Szrj void
df_rd_add_problem(void)705*38fd1498Szrj df_rd_add_problem (void)
706*38fd1498Szrj {
707*38fd1498Szrj   df_add_problem (&problem_RD);
708*38fd1498Szrj }
709*38fd1498Szrj 
710*38fd1498Szrj 
711*38fd1498Szrj 
712*38fd1498Szrj /*----------------------------------------------------------------------------
713*38fd1498Szrj    LIVE REGISTERS
714*38fd1498Szrj 
715*38fd1498Szrj    Find the locations in the function where any use of a pseudo can
716*38fd1498Szrj    reach in the backwards direction.  In and out bitvectors are built
717*38fd1498Szrj    for each basic block.  The regno is used to index into these sets.
718*38fd1498Szrj    See df.h for details.
719*38fd1498Szrj    ----------------------------------------------------------------------------*/
720*38fd1498Szrj 
721*38fd1498Szrj /* Private data used to verify the solution for this problem.  */
722*38fd1498Szrj struct df_lr_problem_data
723*38fd1498Szrj {
724*38fd1498Szrj   bitmap_head *in;
725*38fd1498Szrj   bitmap_head *out;
726*38fd1498Szrj   /* An obstack for the bitmaps we need for this problem.  */
727*38fd1498Szrj   bitmap_obstack lr_bitmaps;
728*38fd1498Szrj };
729*38fd1498Szrj 
730*38fd1498Szrj /* Free basic block info.  */
731*38fd1498Szrj 
732*38fd1498Szrj static void
df_lr_free_bb_info(basic_block bb ATTRIBUTE_UNUSED,void * vbb_info)733*38fd1498Szrj df_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
734*38fd1498Szrj 		    void *vbb_info)
735*38fd1498Szrj {
736*38fd1498Szrj   struct df_lr_bb_info *bb_info = (struct df_lr_bb_info *) vbb_info;
737*38fd1498Szrj   if (bb_info)
738*38fd1498Szrj     {
739*38fd1498Szrj       bitmap_clear (&bb_info->use);
740*38fd1498Szrj       bitmap_clear (&bb_info->def);
741*38fd1498Szrj       bitmap_clear (&bb_info->in);
742*38fd1498Szrj       bitmap_clear (&bb_info->out);
743*38fd1498Szrj     }
744*38fd1498Szrj }
745*38fd1498Szrj 
746*38fd1498Szrj 
747*38fd1498Szrj /* Allocate or reset bitmaps for DF_LR blocks. The solution bits are
748*38fd1498Szrj    not touched unless the block is new.  */
749*38fd1498Szrj 
750*38fd1498Szrj static void
df_lr_alloc(bitmap all_blocks ATTRIBUTE_UNUSED)751*38fd1498Szrj df_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
752*38fd1498Szrj {
753*38fd1498Szrj   unsigned int bb_index;
754*38fd1498Szrj   bitmap_iterator bi;
755*38fd1498Szrj   struct df_lr_problem_data *problem_data;
756*38fd1498Szrj 
757*38fd1498Szrj   df_grow_bb_info (df_lr);
758*38fd1498Szrj   if (df_lr->problem_data)
759*38fd1498Szrj     problem_data = (struct df_lr_problem_data *) df_lr->problem_data;
760*38fd1498Szrj   else
761*38fd1498Szrj     {
762*38fd1498Szrj       problem_data = XNEW (struct df_lr_problem_data);
763*38fd1498Szrj       df_lr->problem_data = problem_data;
764*38fd1498Szrj 
765*38fd1498Szrj       problem_data->out = NULL;
766*38fd1498Szrj       problem_data->in = NULL;
767*38fd1498Szrj       bitmap_obstack_initialize (&problem_data->lr_bitmaps);
768*38fd1498Szrj     }
769*38fd1498Szrj 
770*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
771*38fd1498Szrj     {
772*38fd1498Szrj       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
773*38fd1498Szrj 
774*38fd1498Szrj       /* When bitmaps are already initialized, just clear them.  */
775*38fd1498Szrj       if (bb_info->use.obstack)
776*38fd1498Szrj 	{
777*38fd1498Szrj 	  bitmap_clear (&bb_info->def);
778*38fd1498Szrj 	  bitmap_clear (&bb_info->use);
779*38fd1498Szrj 	}
780*38fd1498Szrj       else
781*38fd1498Szrj 	{
782*38fd1498Szrj 	  bitmap_initialize (&bb_info->use, &problem_data->lr_bitmaps);
783*38fd1498Szrj 	  bitmap_initialize (&bb_info->def, &problem_data->lr_bitmaps);
784*38fd1498Szrj 	  bitmap_initialize (&bb_info->in, &problem_data->lr_bitmaps);
785*38fd1498Szrj 	  bitmap_initialize (&bb_info->out, &problem_data->lr_bitmaps);
786*38fd1498Szrj 	}
787*38fd1498Szrj     }
788*38fd1498Szrj 
789*38fd1498Szrj   df_lr->optional_p = false;
790*38fd1498Szrj }
791*38fd1498Szrj 
792*38fd1498Szrj 
793*38fd1498Szrj /* Reset the global solution for recalculation.  */
794*38fd1498Szrj 
795*38fd1498Szrj static void
df_lr_reset(bitmap all_blocks)796*38fd1498Szrj df_lr_reset (bitmap all_blocks)
797*38fd1498Szrj {
798*38fd1498Szrj   unsigned int bb_index;
799*38fd1498Szrj   bitmap_iterator bi;
800*38fd1498Szrj 
801*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
802*38fd1498Szrj     {
803*38fd1498Szrj       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
804*38fd1498Szrj       gcc_assert (bb_info);
805*38fd1498Szrj       bitmap_clear (&bb_info->in);
806*38fd1498Szrj       bitmap_clear (&bb_info->out);
807*38fd1498Szrj     }
808*38fd1498Szrj }
809*38fd1498Szrj 
810*38fd1498Szrj 
811*38fd1498Szrj /* Compute local live register info for basic block BB.  */
812*38fd1498Szrj 
813*38fd1498Szrj static void
df_lr_bb_local_compute(unsigned int bb_index)814*38fd1498Szrj df_lr_bb_local_compute (unsigned int bb_index)
815*38fd1498Szrj {
816*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
817*38fd1498Szrj   struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
818*38fd1498Szrj   rtx_insn *insn;
819*38fd1498Szrj   df_ref def, use;
820*38fd1498Szrj 
821*38fd1498Szrj   /* Process the registers set in an exception handler.  */
822*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
823*38fd1498Szrj     if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
824*38fd1498Szrj       {
825*38fd1498Szrj 	unsigned int dregno = DF_REF_REGNO (def);
826*38fd1498Szrj 	bitmap_set_bit (&bb_info->def, dregno);
827*38fd1498Szrj 	bitmap_clear_bit (&bb_info->use, dregno);
828*38fd1498Szrj       }
829*38fd1498Szrj 
830*38fd1498Szrj   /* Process the hardware registers that are always live.  */
831*38fd1498Szrj   FOR_EACH_ARTIFICIAL_USE (use, bb_index)
832*38fd1498Szrj     /* Add use to set of uses in this BB.  */
833*38fd1498Szrj     if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
834*38fd1498Szrj       bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
835*38fd1498Szrj 
836*38fd1498Szrj   FOR_BB_INSNS_REVERSE (bb, insn)
837*38fd1498Szrj     {
838*38fd1498Szrj       if (!NONDEBUG_INSN_P (insn))
839*38fd1498Szrj 	continue;
840*38fd1498Szrj 
841*38fd1498Szrj       df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
842*38fd1498Szrj       FOR_EACH_INSN_INFO_DEF (def, insn_info)
843*38fd1498Szrj 	/* If the def is to only part of the reg, it does
844*38fd1498Szrj 	   not kill the other defs that reach here.  */
845*38fd1498Szrj 	if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
846*38fd1498Szrj 	  {
847*38fd1498Szrj 	    unsigned int dregno = DF_REF_REGNO (def);
848*38fd1498Szrj 	    bitmap_set_bit (&bb_info->def, dregno);
849*38fd1498Szrj 	    bitmap_clear_bit (&bb_info->use, dregno);
850*38fd1498Szrj 	  }
851*38fd1498Szrj 
852*38fd1498Szrj       FOR_EACH_INSN_INFO_USE (use, insn_info)
853*38fd1498Szrj 	/* Add use to set of uses in this BB.  */
854*38fd1498Szrj 	bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
855*38fd1498Szrj     }
856*38fd1498Szrj 
857*38fd1498Szrj   /* Process the registers set in an exception handler or the hard
858*38fd1498Szrj      frame pointer if this block is the target of a non local
859*38fd1498Szrj      goto.  */
860*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
861*38fd1498Szrj     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
862*38fd1498Szrj       {
863*38fd1498Szrj 	unsigned int dregno = DF_REF_REGNO (def);
864*38fd1498Szrj 	bitmap_set_bit (&bb_info->def, dregno);
865*38fd1498Szrj 	bitmap_clear_bit (&bb_info->use, dregno);
866*38fd1498Szrj       }
867*38fd1498Szrj 
868*38fd1498Szrj #ifdef EH_USES
869*38fd1498Szrj   /* Process the uses that are live into an exception handler.  */
870*38fd1498Szrj   FOR_EACH_ARTIFICIAL_USE (use, bb_index)
871*38fd1498Szrj     /* Add use to set of uses in this BB.  */
872*38fd1498Szrj     if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
873*38fd1498Szrj       bitmap_set_bit (&bb_info->use, DF_REF_REGNO (use));
874*38fd1498Szrj #endif
875*38fd1498Szrj 
876*38fd1498Szrj   /* If the df_live problem is not defined, such as at -O0 and -O1, we
877*38fd1498Szrj      still need to keep the luids up to date.  This is normally done
878*38fd1498Szrj      in the df_live problem since this problem has a forwards
879*38fd1498Szrj      scan.  */
880*38fd1498Szrj   if (!df_live)
881*38fd1498Szrj     df_recompute_luids (bb);
882*38fd1498Szrj }
883*38fd1498Szrj 
884*38fd1498Szrj 
885*38fd1498Szrj /* Compute local live register info for each basic block within BLOCKS.  */
886*38fd1498Szrj 
887*38fd1498Szrj static void
df_lr_local_compute(bitmap all_blocks ATTRIBUTE_UNUSED)888*38fd1498Szrj df_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
889*38fd1498Szrj {
890*38fd1498Szrj   unsigned int bb_index, i;
891*38fd1498Szrj   bitmap_iterator bi;
892*38fd1498Szrj 
893*38fd1498Szrj   bitmap_clear (&df->hardware_regs_used);
894*38fd1498Szrj 
895*38fd1498Szrj   /* The all-important stack pointer must always be live.  */
896*38fd1498Szrj   bitmap_set_bit (&df->hardware_regs_used, STACK_POINTER_REGNUM);
897*38fd1498Szrj 
898*38fd1498Szrj   /* Global regs are always live, too.  */
899*38fd1498Szrj   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
900*38fd1498Szrj     if (global_regs[i])
901*38fd1498Szrj       bitmap_set_bit (&df->hardware_regs_used, i);
902*38fd1498Szrj 
903*38fd1498Szrj   /* Before reload, there are a few registers that must be forced
904*38fd1498Szrj      live everywhere -- which might not already be the case for
905*38fd1498Szrj      blocks within infinite loops.  */
906*38fd1498Szrj   if (!reload_completed)
907*38fd1498Szrj     {
908*38fd1498Szrj       unsigned int pic_offset_table_regnum = PIC_OFFSET_TABLE_REGNUM;
909*38fd1498Szrj       /* Any reference to any pseudo before reload is a potential
910*38fd1498Szrj 	 reference of the frame pointer.  */
911*38fd1498Szrj       bitmap_set_bit (&df->hardware_regs_used, FRAME_POINTER_REGNUM);
912*38fd1498Szrj 
913*38fd1498Szrj       /* Pseudos with argument area equivalences may require
914*38fd1498Szrj 	 reloading via the argument pointer.  */
915*38fd1498Szrj       if (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
916*38fd1498Szrj 	  && fixed_regs[ARG_POINTER_REGNUM])
917*38fd1498Szrj 	bitmap_set_bit (&df->hardware_regs_used, ARG_POINTER_REGNUM);
918*38fd1498Szrj 
919*38fd1498Szrj       /* Any constant, or pseudo with constant equivalences, may
920*38fd1498Szrj 	 require reloading from memory using the pic register.  */
921*38fd1498Szrj       if (pic_offset_table_regnum != INVALID_REGNUM
922*38fd1498Szrj 	  && fixed_regs[pic_offset_table_regnum])
923*38fd1498Szrj 	bitmap_set_bit (&df->hardware_regs_used, pic_offset_table_regnum);
924*38fd1498Szrj     }
925*38fd1498Szrj 
926*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (df_lr->out_of_date_transfer_functions, 0, bb_index, bi)
927*38fd1498Szrj     {
928*38fd1498Szrj       if (bb_index == EXIT_BLOCK)
929*38fd1498Szrj 	{
930*38fd1498Szrj 	  /* The exit block is special for this problem and its bits are
931*38fd1498Szrj 	     computed from thin air.  */
932*38fd1498Szrj 	  struct df_lr_bb_info *bb_info = df_lr_get_bb_info (EXIT_BLOCK);
933*38fd1498Szrj 	  bitmap_copy (&bb_info->use, df->exit_block_uses);
934*38fd1498Szrj 	}
935*38fd1498Szrj       else
936*38fd1498Szrj 	df_lr_bb_local_compute (bb_index);
937*38fd1498Szrj     }
938*38fd1498Szrj 
939*38fd1498Szrj   bitmap_clear (df_lr->out_of_date_transfer_functions);
940*38fd1498Szrj }
941*38fd1498Szrj 
942*38fd1498Szrj 
943*38fd1498Szrj /* Initialize the solution vectors.  */
944*38fd1498Szrj 
945*38fd1498Szrj static void
df_lr_init(bitmap all_blocks)946*38fd1498Szrj df_lr_init (bitmap all_blocks)
947*38fd1498Szrj {
948*38fd1498Szrj   unsigned int bb_index;
949*38fd1498Szrj   bitmap_iterator bi;
950*38fd1498Szrj 
951*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
952*38fd1498Szrj     {
953*38fd1498Szrj       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
954*38fd1498Szrj       bitmap_copy (&bb_info->in, &bb_info->use);
955*38fd1498Szrj       bitmap_clear (&bb_info->out);
956*38fd1498Szrj     }
957*38fd1498Szrj }
958*38fd1498Szrj 
959*38fd1498Szrj 
960*38fd1498Szrj /* Confluence function that processes infinite loops.  This might be a
961*38fd1498Szrj    noreturn function that throws.  And even if it isn't, getting the
962*38fd1498Szrj    unwind info right helps debugging.  */
963*38fd1498Szrj static void
df_lr_confluence_0(basic_block bb)964*38fd1498Szrj df_lr_confluence_0 (basic_block bb)
965*38fd1498Szrj {
966*38fd1498Szrj   bitmap op1 = &df_lr_get_bb_info (bb->index)->out;
967*38fd1498Szrj   if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
968*38fd1498Szrj     bitmap_copy (op1, &df->hardware_regs_used);
969*38fd1498Szrj }
970*38fd1498Szrj 
971*38fd1498Szrj 
972*38fd1498Szrj /* Confluence function that ignores fake edges.  */
973*38fd1498Szrj 
974*38fd1498Szrj static bool
df_lr_confluence_n(edge e)975*38fd1498Szrj df_lr_confluence_n (edge e)
976*38fd1498Szrj {
977*38fd1498Szrj   bitmap op1 = &df_lr_get_bb_info (e->src->index)->out;
978*38fd1498Szrj   bitmap op2 = &df_lr_get_bb_info (e->dest->index)->in;
979*38fd1498Szrj   bool changed = false;
980*38fd1498Szrj 
981*38fd1498Szrj   /* Call-clobbered registers die across exception and call edges.  */
982*38fd1498Szrj   /* ??? Abnormal call edges ignored for the moment, as this gets
983*38fd1498Szrj      confused by sibling call edges, which crashes reg-stack.  */
984*38fd1498Szrj   if (e->flags & EDGE_EH)
985*38fd1498Szrj     changed = bitmap_ior_and_compl_into (op1, op2, regs_invalidated_by_call_regset);
986*38fd1498Szrj   else
987*38fd1498Szrj     changed = bitmap_ior_into (op1, op2);
988*38fd1498Szrj 
989*38fd1498Szrj   changed |= bitmap_ior_into (op1, &df->hardware_regs_used);
990*38fd1498Szrj   return changed;
991*38fd1498Szrj }
992*38fd1498Szrj 
993*38fd1498Szrj 
994*38fd1498Szrj /* Transfer function.  */
995*38fd1498Szrj 
996*38fd1498Szrj static bool
df_lr_transfer_function(int bb_index)997*38fd1498Szrj df_lr_transfer_function (int bb_index)
998*38fd1498Szrj {
999*38fd1498Szrj   struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb_index);
1000*38fd1498Szrj   bitmap in = &bb_info->in;
1001*38fd1498Szrj   bitmap out = &bb_info->out;
1002*38fd1498Szrj   bitmap use = &bb_info->use;
1003*38fd1498Szrj   bitmap def = &bb_info->def;
1004*38fd1498Szrj 
1005*38fd1498Szrj   return bitmap_ior_and_compl (in, use, out, def);
1006*38fd1498Szrj }
1007*38fd1498Szrj 
1008*38fd1498Szrj 
1009*38fd1498Szrj /* Run the fast dce as a side effect of building LR.  */
1010*38fd1498Szrj 
1011*38fd1498Szrj static void
df_lr_finalize(bitmap all_blocks)1012*38fd1498Szrj df_lr_finalize (bitmap all_blocks)
1013*38fd1498Szrj {
1014*38fd1498Szrj   df_lr->solutions_dirty = false;
1015*38fd1498Szrj   if (df->changeable_flags & DF_LR_RUN_DCE)
1016*38fd1498Szrj     {
1017*38fd1498Szrj       run_fast_df_dce ();
1018*38fd1498Szrj 
1019*38fd1498Szrj       /* If dce deletes some instructions, we need to recompute the lr
1020*38fd1498Szrj 	 solution before proceeding further.  The problem is that fast
1021*38fd1498Szrj 	 dce is a pessimestic dataflow algorithm.  In the case where
1022*38fd1498Szrj 	 it deletes a statement S inside of a loop, the uses inside of
1023*38fd1498Szrj 	 S may not be deleted from the dataflow solution because they
1024*38fd1498Szrj 	 were carried around the loop.  While it is conservatively
1025*38fd1498Szrj 	 correct to leave these extra bits, the standards of df
1026*38fd1498Szrj 	 require that we maintain the best possible (least fixed
1027*38fd1498Szrj 	 point) solution.  The only way to do that is to redo the
1028*38fd1498Szrj 	 iteration from the beginning.  See PR35805 for an
1029*38fd1498Szrj 	 example.  */
1030*38fd1498Szrj       if (df_lr->solutions_dirty)
1031*38fd1498Szrj 	{
1032*38fd1498Szrj 	  df_clear_flags (DF_LR_RUN_DCE);
1033*38fd1498Szrj 	  df_lr_alloc (all_blocks);
1034*38fd1498Szrj 	  df_lr_local_compute (all_blocks);
1035*38fd1498Szrj 	  df_worklist_dataflow (df_lr, all_blocks, df->postorder, df->n_blocks);
1036*38fd1498Szrj 	  df_lr_finalize (all_blocks);
1037*38fd1498Szrj 	  df_set_flags (DF_LR_RUN_DCE);
1038*38fd1498Szrj 	}
1039*38fd1498Szrj     }
1040*38fd1498Szrj }
1041*38fd1498Szrj 
1042*38fd1498Szrj 
1043*38fd1498Szrj /* Free all storage associated with the problem.  */
1044*38fd1498Szrj 
1045*38fd1498Szrj static void
df_lr_free(void)1046*38fd1498Szrj df_lr_free (void)
1047*38fd1498Szrj {
1048*38fd1498Szrj   struct df_lr_problem_data *problem_data
1049*38fd1498Szrj     = (struct df_lr_problem_data *) df_lr->problem_data;
1050*38fd1498Szrj   if (df_lr->block_info)
1051*38fd1498Szrj     {
1052*38fd1498Szrj 
1053*38fd1498Szrj       df_lr->block_info_size = 0;
1054*38fd1498Szrj       free (df_lr->block_info);
1055*38fd1498Szrj       df_lr->block_info = NULL;
1056*38fd1498Szrj       bitmap_obstack_release (&problem_data->lr_bitmaps);
1057*38fd1498Szrj       free (df_lr->problem_data);
1058*38fd1498Szrj       df_lr->problem_data = NULL;
1059*38fd1498Szrj     }
1060*38fd1498Szrj 
1061*38fd1498Szrj   BITMAP_FREE (df_lr->out_of_date_transfer_functions);
1062*38fd1498Szrj   free (df_lr);
1063*38fd1498Szrj }
1064*38fd1498Szrj 
1065*38fd1498Szrj 
1066*38fd1498Szrj /* Debugging info at top of bb.  */
1067*38fd1498Szrj 
1068*38fd1498Szrj static void
df_lr_top_dump(basic_block bb,FILE * file)1069*38fd1498Szrj df_lr_top_dump (basic_block bb, FILE *file)
1070*38fd1498Szrj {
1071*38fd1498Szrj   struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1072*38fd1498Szrj   struct df_lr_problem_data *problem_data;
1073*38fd1498Szrj   if (!bb_info)
1074*38fd1498Szrj     return;
1075*38fd1498Szrj 
1076*38fd1498Szrj   fprintf (file, ";; lr  in  \t");
1077*38fd1498Szrj   df_print_regset (file, &bb_info->in);
1078*38fd1498Szrj   if (df_lr->problem_data)
1079*38fd1498Szrj     {
1080*38fd1498Szrj       problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1081*38fd1498Szrj       if (problem_data->in)
1082*38fd1498Szrj 	{
1083*38fd1498Szrj       	  fprintf (file, ";;  old in  \t");
1084*38fd1498Szrj       	  df_print_regset (file, &problem_data->in[bb->index]);
1085*38fd1498Szrj 	}
1086*38fd1498Szrj     }
1087*38fd1498Szrj   fprintf (file, ";; lr  use \t");
1088*38fd1498Szrj   df_print_regset (file, &bb_info->use);
1089*38fd1498Szrj   fprintf (file, ";; lr  def \t");
1090*38fd1498Szrj   df_print_regset (file, &bb_info->def);
1091*38fd1498Szrj }
1092*38fd1498Szrj 
1093*38fd1498Szrj 
1094*38fd1498Szrj /* Debugging info at bottom of bb.  */
1095*38fd1498Szrj 
1096*38fd1498Szrj static void
df_lr_bottom_dump(basic_block bb,FILE * file)1097*38fd1498Szrj df_lr_bottom_dump (basic_block bb, FILE *file)
1098*38fd1498Szrj {
1099*38fd1498Szrj   struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1100*38fd1498Szrj   struct df_lr_problem_data *problem_data;
1101*38fd1498Szrj   if (!bb_info)
1102*38fd1498Szrj     return;
1103*38fd1498Szrj 
1104*38fd1498Szrj   fprintf (file, ";; lr  out \t");
1105*38fd1498Szrj   df_print_regset (file, &bb_info->out);
1106*38fd1498Szrj   if (df_lr->problem_data)
1107*38fd1498Szrj     {
1108*38fd1498Szrj       problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1109*38fd1498Szrj       if (problem_data->out)
1110*38fd1498Szrj 	{
1111*38fd1498Szrj           fprintf (file, ";;  old out  \t");
1112*38fd1498Szrj           df_print_regset (file, &problem_data->out[bb->index]);
1113*38fd1498Szrj 	}
1114*38fd1498Szrj     }
1115*38fd1498Szrj }
1116*38fd1498Szrj 
1117*38fd1498Szrj 
1118*38fd1498Szrj /* Build the datastructure to verify that the solution to the dataflow
1119*38fd1498Szrj    equations is not dirty.  */
1120*38fd1498Szrj 
1121*38fd1498Szrj static void
df_lr_verify_solution_start(void)1122*38fd1498Szrj df_lr_verify_solution_start (void)
1123*38fd1498Szrj {
1124*38fd1498Szrj   basic_block bb;
1125*38fd1498Szrj   struct df_lr_problem_data *problem_data;
1126*38fd1498Szrj   if (df_lr->solutions_dirty)
1127*38fd1498Szrj     return;
1128*38fd1498Szrj 
1129*38fd1498Szrj   /* Set it true so that the solution is recomputed.  */
1130*38fd1498Szrj   df_lr->solutions_dirty = true;
1131*38fd1498Szrj 
1132*38fd1498Szrj   problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1133*38fd1498Szrj   problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1134*38fd1498Szrj   problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1135*38fd1498Szrj 
1136*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1137*38fd1498Szrj     {
1138*38fd1498Szrj       bitmap_initialize (&problem_data->in[bb->index], &problem_data->lr_bitmaps);
1139*38fd1498Szrj       bitmap_initialize (&problem_data->out[bb->index], &problem_data->lr_bitmaps);
1140*38fd1498Szrj       bitmap_copy (&problem_data->in[bb->index], DF_LR_IN (bb));
1141*38fd1498Szrj       bitmap_copy (&problem_data->out[bb->index], DF_LR_OUT (bb));
1142*38fd1498Szrj     }
1143*38fd1498Szrj }
1144*38fd1498Szrj 
1145*38fd1498Szrj 
1146*38fd1498Szrj /* Compare the saved datastructure and the new solution to the dataflow
1147*38fd1498Szrj    equations.  */
1148*38fd1498Szrj 
1149*38fd1498Szrj static void
df_lr_verify_solution_end(void)1150*38fd1498Szrj df_lr_verify_solution_end (void)
1151*38fd1498Szrj {
1152*38fd1498Szrj   struct df_lr_problem_data *problem_data;
1153*38fd1498Szrj   basic_block bb;
1154*38fd1498Szrj 
1155*38fd1498Szrj   problem_data = (struct df_lr_problem_data *)df_lr->problem_data;
1156*38fd1498Szrj 
1157*38fd1498Szrj   if (!problem_data->out)
1158*38fd1498Szrj     return;
1159*38fd1498Szrj 
1160*38fd1498Szrj   if (df_lr->solutions_dirty)
1161*38fd1498Szrj     /* Do not check if the solution is still dirty.  See the comment
1162*38fd1498Szrj        in df_lr_finalize for details.  */
1163*38fd1498Szrj     df_lr->solutions_dirty = false;
1164*38fd1498Szrj   else
1165*38fd1498Szrj     FOR_ALL_BB_FN (bb, cfun)
1166*38fd1498Szrj       {
1167*38fd1498Szrj 	if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LR_IN (bb)))
1168*38fd1498Szrj 	    || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LR_OUT (bb))))
1169*38fd1498Szrj 	  {
1170*38fd1498Szrj 	    /*df_dump (stderr);*/
1171*38fd1498Szrj 	    gcc_unreachable ();
1172*38fd1498Szrj 	  }
1173*38fd1498Szrj       }
1174*38fd1498Szrj 
1175*38fd1498Szrj   /* Cannot delete them immediately because you may want to dump them
1176*38fd1498Szrj      if the comparison fails.  */
1177*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1178*38fd1498Szrj     {
1179*38fd1498Szrj       bitmap_clear (&problem_data->in[bb->index]);
1180*38fd1498Szrj       bitmap_clear (&problem_data->out[bb->index]);
1181*38fd1498Szrj     }
1182*38fd1498Szrj 
1183*38fd1498Szrj   free (problem_data->in);
1184*38fd1498Szrj   free (problem_data->out);
1185*38fd1498Szrj   problem_data->in = NULL;
1186*38fd1498Szrj   problem_data->out = NULL;
1187*38fd1498Szrj }
1188*38fd1498Szrj 
1189*38fd1498Szrj 
1190*38fd1498Szrj /* All of the information associated with every instance of the problem.  */
1191*38fd1498Szrj 
1192*38fd1498Szrj static const struct df_problem problem_LR =
1193*38fd1498Szrj {
1194*38fd1498Szrj   DF_LR,                      /* Problem id.  */
1195*38fd1498Szrj   DF_BACKWARD,                /* Direction.  */
1196*38fd1498Szrj   df_lr_alloc,                /* Allocate the problem specific data.  */
1197*38fd1498Szrj   df_lr_reset,                /* Reset global information.  */
1198*38fd1498Szrj   df_lr_free_bb_info,         /* Free basic block info.  */
1199*38fd1498Szrj   df_lr_local_compute,        /* Local compute function.  */
1200*38fd1498Szrj   df_lr_init,                 /* Init the solution specific data.  */
1201*38fd1498Szrj   df_worklist_dataflow,       /* Worklist solver.  */
1202*38fd1498Szrj   df_lr_confluence_0,         /* Confluence operator 0.  */
1203*38fd1498Szrj   df_lr_confluence_n,         /* Confluence operator n.  */
1204*38fd1498Szrj   df_lr_transfer_function,    /* Transfer function.  */
1205*38fd1498Szrj   df_lr_finalize,             /* Finalize function.  */
1206*38fd1498Szrj   df_lr_free,                 /* Free all of the problem information.  */
1207*38fd1498Szrj   NULL,                       /* Remove this problem from the stack of dataflow problems.  */
1208*38fd1498Szrj   NULL,                       /* Debugging.  */
1209*38fd1498Szrj   df_lr_top_dump,             /* Debugging start block.  */
1210*38fd1498Szrj   df_lr_bottom_dump,          /* Debugging end block.  */
1211*38fd1498Szrj   NULL,                       /* Debugging start insn.  */
1212*38fd1498Szrj   NULL,                       /* Debugging end insn.  */
1213*38fd1498Szrj   df_lr_verify_solution_start,/* Incremental solution verify start.  */
1214*38fd1498Szrj   df_lr_verify_solution_end,  /* Incremental solution verify end.  */
1215*38fd1498Szrj   NULL,                       /* Dependent problem.  */
1216*38fd1498Szrj   sizeof (struct df_lr_bb_info),/* Size of entry of block_info array.  */
1217*38fd1498Szrj   TV_DF_LR,                   /* Timing variable.  */
1218*38fd1498Szrj   false                       /* Reset blocks on dropping out of blocks_to_analyze.  */
1219*38fd1498Szrj };
1220*38fd1498Szrj 
1221*38fd1498Szrj 
1222*38fd1498Szrj /* Create a new DATAFLOW instance and add it to an existing instance
1223*38fd1498Szrj    of DF.  The returned structure is what is used to get at the
1224*38fd1498Szrj    solution.  */
1225*38fd1498Szrj 
1226*38fd1498Szrj void
df_lr_add_problem(void)1227*38fd1498Szrj df_lr_add_problem (void)
1228*38fd1498Szrj {
1229*38fd1498Szrj   df_add_problem (&problem_LR);
1230*38fd1498Szrj   /* These will be initialized when df_scan_blocks processes each
1231*38fd1498Szrj      block.  */
1232*38fd1498Szrj   df_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1233*38fd1498Szrj }
1234*38fd1498Szrj 
1235*38fd1498Szrj 
1236*38fd1498Szrj /* Verify that all of the lr related info is consistent and
1237*38fd1498Szrj    correct.  */
1238*38fd1498Szrj 
1239*38fd1498Szrj void
df_lr_verify_transfer_functions(void)1240*38fd1498Szrj df_lr_verify_transfer_functions (void)
1241*38fd1498Szrj {
1242*38fd1498Szrj   basic_block bb;
1243*38fd1498Szrj   bitmap_head saved_def;
1244*38fd1498Szrj   bitmap_head saved_use;
1245*38fd1498Szrj   bitmap_head all_blocks;
1246*38fd1498Szrj 
1247*38fd1498Szrj   if (!df)
1248*38fd1498Szrj     return;
1249*38fd1498Szrj 
1250*38fd1498Szrj   bitmap_initialize (&saved_def, &bitmap_default_obstack);
1251*38fd1498Szrj   bitmap_initialize (&saved_use, &bitmap_default_obstack);
1252*38fd1498Szrj   bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1253*38fd1498Szrj 
1254*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1255*38fd1498Szrj     {
1256*38fd1498Szrj       struct df_lr_bb_info *bb_info = df_lr_get_bb_info (bb->index);
1257*38fd1498Szrj       bitmap_set_bit (&all_blocks, bb->index);
1258*38fd1498Szrj 
1259*38fd1498Szrj       if (bb_info)
1260*38fd1498Szrj 	{
1261*38fd1498Szrj 	  /* Make a copy of the transfer functions and then compute
1262*38fd1498Szrj 	     new ones to see if the transfer functions have
1263*38fd1498Szrj 	     changed.  */
1264*38fd1498Szrj 	  if (!bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1265*38fd1498Szrj 			     bb->index))
1266*38fd1498Szrj 	    {
1267*38fd1498Szrj 	      bitmap_copy (&saved_def, &bb_info->def);
1268*38fd1498Szrj 	      bitmap_copy (&saved_use, &bb_info->use);
1269*38fd1498Szrj 	      bitmap_clear (&bb_info->def);
1270*38fd1498Szrj 	      bitmap_clear (&bb_info->use);
1271*38fd1498Szrj 
1272*38fd1498Szrj 	      df_lr_bb_local_compute (bb->index);
1273*38fd1498Szrj 	      gcc_assert (bitmap_equal_p (&saved_def, &bb_info->def));
1274*38fd1498Szrj 	      gcc_assert (bitmap_equal_p (&saved_use, &bb_info->use));
1275*38fd1498Szrj 	    }
1276*38fd1498Szrj 	}
1277*38fd1498Szrj       else
1278*38fd1498Szrj 	{
1279*38fd1498Szrj 	  /* If we do not have basic block info, the block must be in
1280*38fd1498Szrj 	     the list of dirty blocks or else some one has added a
1281*38fd1498Szrj 	     block behind our backs. */
1282*38fd1498Szrj 	  gcc_assert (bitmap_bit_p (df_lr->out_of_date_transfer_functions,
1283*38fd1498Szrj 				    bb->index));
1284*38fd1498Szrj 	}
1285*38fd1498Szrj       /* Make sure no one created a block without following
1286*38fd1498Szrj 	 procedures.  */
1287*38fd1498Szrj       gcc_assert (df_scan_get_bb_info (bb->index));
1288*38fd1498Szrj     }
1289*38fd1498Szrj 
1290*38fd1498Szrj   /* Make sure there are no dirty bits in blocks that have been deleted.  */
1291*38fd1498Szrj   gcc_assert (!bitmap_intersect_compl_p (df_lr->out_of_date_transfer_functions,
1292*38fd1498Szrj 					 &all_blocks));
1293*38fd1498Szrj 
1294*38fd1498Szrj   bitmap_clear (&saved_def);
1295*38fd1498Szrj   bitmap_clear (&saved_use);
1296*38fd1498Szrj   bitmap_clear (&all_blocks);
1297*38fd1498Szrj }
1298*38fd1498Szrj 
1299*38fd1498Szrj 
1300*38fd1498Szrj 
1301*38fd1498Szrj /*----------------------------------------------------------------------------
1302*38fd1498Szrj    LIVE AND MAY-INITIALIZED REGISTERS.
1303*38fd1498Szrj 
1304*38fd1498Szrj    This problem first computes the IN and OUT bitvectors for the
1305*38fd1498Szrj    may-initialized registers problems, which is a forward problem.
1306*38fd1498Szrj    It gives the set of registers for which we MAY have an available
1307*38fd1498Szrj    definition, i.e. for which there is an available definition on
1308*38fd1498Szrj    at least one path from the entry block to the entry/exit of a
1309*38fd1498Szrj    basic block.  Sets generate a definition, while clobbers kill
1310*38fd1498Szrj    a definition.
1311*38fd1498Szrj 
1312*38fd1498Szrj    In and out bitvectors are built for each basic block and are indexed by
1313*38fd1498Szrj    regnum (see df.h for details).  In and out bitvectors in struct
1314*38fd1498Szrj    df_live_bb_info actually refers to the may-initialized problem;
1315*38fd1498Szrj 
1316*38fd1498Szrj    Then, the in and out sets for the LIVE problem itself are computed.
1317*38fd1498Szrj    These are the logical AND of the IN and OUT sets from the LR problem
1318*38fd1498Szrj    and the may-initialized problem.
1319*38fd1498Szrj ----------------------------------------------------------------------------*/
1320*38fd1498Szrj 
1321*38fd1498Szrj /* Private data used to verify the solution for this problem.  */
1322*38fd1498Szrj struct df_live_problem_data
1323*38fd1498Szrj {
1324*38fd1498Szrj   bitmap_head *in;
1325*38fd1498Szrj   bitmap_head *out;
1326*38fd1498Szrj   /* An obstack for the bitmaps we need for this problem.  */
1327*38fd1498Szrj   bitmap_obstack live_bitmaps;
1328*38fd1498Szrj };
1329*38fd1498Szrj 
1330*38fd1498Szrj /* Scratch var used by transfer functions.  This is used to implement
1331*38fd1498Szrj    an optimization to reduce the amount of space used to compute the
1332*38fd1498Szrj    combined lr and live analysis.  */
1333*38fd1498Szrj static bitmap_head df_live_scratch;
1334*38fd1498Szrj 
1335*38fd1498Szrj 
1336*38fd1498Szrj /* Free basic block info.  */
1337*38fd1498Szrj 
1338*38fd1498Szrj static void
df_live_free_bb_info(basic_block bb ATTRIBUTE_UNUSED,void * vbb_info)1339*38fd1498Szrj df_live_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1340*38fd1498Szrj 		    void *vbb_info)
1341*38fd1498Szrj {
1342*38fd1498Szrj   struct df_live_bb_info *bb_info = (struct df_live_bb_info *) vbb_info;
1343*38fd1498Szrj   if (bb_info)
1344*38fd1498Szrj     {
1345*38fd1498Szrj       bitmap_clear (&bb_info->gen);
1346*38fd1498Szrj       bitmap_clear (&bb_info->kill);
1347*38fd1498Szrj       bitmap_clear (&bb_info->in);
1348*38fd1498Szrj       bitmap_clear (&bb_info->out);
1349*38fd1498Szrj     }
1350*38fd1498Szrj }
1351*38fd1498Szrj 
1352*38fd1498Szrj 
1353*38fd1498Szrj /* Allocate or reset bitmaps for DF_LIVE blocks. The solution bits are
1354*38fd1498Szrj    not touched unless the block is new.  */
1355*38fd1498Szrj 
1356*38fd1498Szrj static void
df_live_alloc(bitmap all_blocks ATTRIBUTE_UNUSED)1357*38fd1498Szrj df_live_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
1358*38fd1498Szrj {
1359*38fd1498Szrj   unsigned int bb_index;
1360*38fd1498Szrj   bitmap_iterator bi;
1361*38fd1498Szrj   struct df_live_problem_data *problem_data;
1362*38fd1498Szrj 
1363*38fd1498Szrj   if (df_live->problem_data)
1364*38fd1498Szrj     problem_data = (struct df_live_problem_data *) df_live->problem_data;
1365*38fd1498Szrj   else
1366*38fd1498Szrj     {
1367*38fd1498Szrj       problem_data = XNEW (struct df_live_problem_data);
1368*38fd1498Szrj       df_live->problem_data = problem_data;
1369*38fd1498Szrj 
1370*38fd1498Szrj       problem_data->out = NULL;
1371*38fd1498Szrj       problem_data->in = NULL;
1372*38fd1498Szrj       bitmap_obstack_initialize (&problem_data->live_bitmaps);
1373*38fd1498Szrj       bitmap_initialize (&df_live_scratch, &problem_data->live_bitmaps);
1374*38fd1498Szrj     }
1375*38fd1498Szrj 
1376*38fd1498Szrj   df_grow_bb_info (df_live);
1377*38fd1498Szrj 
1378*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions, 0, bb_index, bi)
1379*38fd1498Szrj     {
1380*38fd1498Szrj       struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1381*38fd1498Szrj 
1382*38fd1498Szrj       /* When bitmaps are already initialized, just clear them.  */
1383*38fd1498Szrj       if (bb_info->kill.obstack)
1384*38fd1498Szrj 	{
1385*38fd1498Szrj 	  bitmap_clear (&bb_info->kill);
1386*38fd1498Szrj 	  bitmap_clear (&bb_info->gen);
1387*38fd1498Szrj 	}
1388*38fd1498Szrj       else
1389*38fd1498Szrj 	{
1390*38fd1498Szrj 	  bitmap_initialize (&bb_info->kill, &problem_data->live_bitmaps);
1391*38fd1498Szrj 	  bitmap_initialize (&bb_info->gen, &problem_data->live_bitmaps);
1392*38fd1498Szrj 	  bitmap_initialize (&bb_info->in, &problem_data->live_bitmaps);
1393*38fd1498Szrj 	  bitmap_initialize (&bb_info->out, &problem_data->live_bitmaps);
1394*38fd1498Szrj 	}
1395*38fd1498Szrj     }
1396*38fd1498Szrj   df_live->optional_p = (optimize <= 1);
1397*38fd1498Szrj }
1398*38fd1498Szrj 
1399*38fd1498Szrj 
1400*38fd1498Szrj /* Reset the global solution for recalculation.  */
1401*38fd1498Szrj 
1402*38fd1498Szrj static void
df_live_reset(bitmap all_blocks)1403*38fd1498Szrj df_live_reset (bitmap all_blocks)
1404*38fd1498Szrj {
1405*38fd1498Szrj   unsigned int bb_index;
1406*38fd1498Szrj   bitmap_iterator bi;
1407*38fd1498Szrj 
1408*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1409*38fd1498Szrj     {
1410*38fd1498Szrj       struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1411*38fd1498Szrj       gcc_assert (bb_info);
1412*38fd1498Szrj       bitmap_clear (&bb_info->in);
1413*38fd1498Szrj       bitmap_clear (&bb_info->out);
1414*38fd1498Szrj     }
1415*38fd1498Szrj }
1416*38fd1498Szrj 
1417*38fd1498Szrj 
1418*38fd1498Szrj /* Compute local uninitialized register info for basic block BB.  */
1419*38fd1498Szrj 
1420*38fd1498Szrj static void
df_live_bb_local_compute(unsigned int bb_index)1421*38fd1498Szrj df_live_bb_local_compute (unsigned int bb_index)
1422*38fd1498Szrj {
1423*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1424*38fd1498Szrj   struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1425*38fd1498Szrj   rtx_insn *insn;
1426*38fd1498Szrj   df_ref def;
1427*38fd1498Szrj   int luid = 0;
1428*38fd1498Szrj 
1429*38fd1498Szrj   FOR_BB_INSNS (bb, insn)
1430*38fd1498Szrj     {
1431*38fd1498Szrj       unsigned int uid = INSN_UID (insn);
1432*38fd1498Szrj       struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1433*38fd1498Szrj 
1434*38fd1498Szrj       /* Inserting labels does not always trigger the incremental
1435*38fd1498Szrj 	 rescanning.  */
1436*38fd1498Szrj       if (!insn_info)
1437*38fd1498Szrj 	{
1438*38fd1498Szrj 	  gcc_assert (!INSN_P (insn));
1439*38fd1498Szrj 	  insn_info = df_insn_create_insn_record (insn);
1440*38fd1498Szrj 	}
1441*38fd1498Szrj 
1442*38fd1498Szrj       DF_INSN_INFO_LUID (insn_info) = luid;
1443*38fd1498Szrj       if (!INSN_P (insn))
1444*38fd1498Szrj 	continue;
1445*38fd1498Szrj 
1446*38fd1498Szrj       luid++;
1447*38fd1498Szrj       FOR_EACH_INSN_INFO_DEF (def, insn_info)
1448*38fd1498Szrj 	{
1449*38fd1498Szrj 	  unsigned int regno = DF_REF_REGNO (def);
1450*38fd1498Szrj 
1451*38fd1498Szrj 	  if (DF_REF_FLAGS_IS_SET (def,
1452*38fd1498Szrj 				   DF_REF_PARTIAL | DF_REF_CONDITIONAL))
1453*38fd1498Szrj 	    /* All partial or conditional def
1454*38fd1498Szrj 	       seen are included in the gen set. */
1455*38fd1498Szrj 	    bitmap_set_bit (&bb_info->gen, regno);
1456*38fd1498Szrj 	  else if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
1457*38fd1498Szrj 	    /* Only must clobbers for the entire reg destroy the
1458*38fd1498Szrj 	       value.  */
1459*38fd1498Szrj 	    bitmap_set_bit (&bb_info->kill, regno);
1460*38fd1498Szrj 	  else if (! DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
1461*38fd1498Szrj 	    bitmap_set_bit (&bb_info->gen, regno);
1462*38fd1498Szrj 	}
1463*38fd1498Szrj     }
1464*38fd1498Szrj 
1465*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
1466*38fd1498Szrj     bitmap_set_bit (&bb_info->gen, DF_REF_REGNO (def));
1467*38fd1498Szrj }
1468*38fd1498Szrj 
1469*38fd1498Szrj 
1470*38fd1498Szrj /* Compute local uninitialized register info.  */
1471*38fd1498Szrj 
1472*38fd1498Szrj static void
df_live_local_compute(bitmap all_blocks ATTRIBUTE_UNUSED)1473*38fd1498Szrj df_live_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
1474*38fd1498Szrj {
1475*38fd1498Szrj   unsigned int bb_index;
1476*38fd1498Szrj   bitmap_iterator bi;
1477*38fd1498Szrj 
1478*38fd1498Szrj   df_grow_insn_info ();
1479*38fd1498Szrj 
1480*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (df_live->out_of_date_transfer_functions,
1481*38fd1498Szrj 			    0, bb_index, bi)
1482*38fd1498Szrj     {
1483*38fd1498Szrj       df_live_bb_local_compute (bb_index);
1484*38fd1498Szrj     }
1485*38fd1498Szrj 
1486*38fd1498Szrj   bitmap_clear (df_live->out_of_date_transfer_functions);
1487*38fd1498Szrj }
1488*38fd1498Szrj 
1489*38fd1498Szrj 
1490*38fd1498Szrj /* Initialize the solution vectors.  */
1491*38fd1498Szrj 
1492*38fd1498Szrj static void
df_live_init(bitmap all_blocks)1493*38fd1498Szrj df_live_init (bitmap all_blocks)
1494*38fd1498Szrj {
1495*38fd1498Szrj   unsigned int bb_index;
1496*38fd1498Szrj   bitmap_iterator bi;
1497*38fd1498Szrj 
1498*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1499*38fd1498Szrj     {
1500*38fd1498Szrj       struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1501*38fd1498Szrj       struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1502*38fd1498Szrj 
1503*38fd1498Szrj       /* No register may reach a location where it is not used.  Thus
1504*38fd1498Szrj 	 we trim the rr result to the places where it is used.  */
1505*38fd1498Szrj       bitmap_and (&bb_info->out, &bb_info->gen, &bb_lr_info->out);
1506*38fd1498Szrj       bitmap_clear (&bb_info->in);
1507*38fd1498Szrj     }
1508*38fd1498Szrj }
1509*38fd1498Szrj 
1510*38fd1498Szrj /* Forward confluence function that ignores fake edges.  */
1511*38fd1498Szrj 
1512*38fd1498Szrj static bool
df_live_confluence_n(edge e)1513*38fd1498Szrj df_live_confluence_n (edge e)
1514*38fd1498Szrj {
1515*38fd1498Szrj   bitmap op1 = &df_live_get_bb_info (e->dest->index)->in;
1516*38fd1498Szrj   bitmap op2 = &df_live_get_bb_info (e->src->index)->out;
1517*38fd1498Szrj 
1518*38fd1498Szrj   if (e->flags & EDGE_FAKE)
1519*38fd1498Szrj     return false;
1520*38fd1498Szrj 
1521*38fd1498Szrj   return bitmap_ior_into (op1, op2);
1522*38fd1498Szrj }
1523*38fd1498Szrj 
1524*38fd1498Szrj 
1525*38fd1498Szrj /* Transfer function for the forwards may-initialized problem.  */
1526*38fd1498Szrj 
1527*38fd1498Szrj static bool
df_live_transfer_function(int bb_index)1528*38fd1498Szrj df_live_transfer_function (int bb_index)
1529*38fd1498Szrj {
1530*38fd1498Szrj   struct df_live_bb_info *bb_info = df_live_get_bb_info (bb_index);
1531*38fd1498Szrj   struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1532*38fd1498Szrj   bitmap in = &bb_info->in;
1533*38fd1498Szrj   bitmap out = &bb_info->out;
1534*38fd1498Szrj   bitmap gen = &bb_info->gen;
1535*38fd1498Szrj   bitmap kill = &bb_info->kill;
1536*38fd1498Szrj 
1537*38fd1498Szrj   /* We need to use a scratch set here so that the value returned from this
1538*38fd1498Szrj      function invocation properly reflects whether the sets changed in a
1539*38fd1498Szrj      significant way; i.e. not just because the lr set was anded in.  */
1540*38fd1498Szrj   bitmap_and (&df_live_scratch, gen, &bb_lr_info->out);
1541*38fd1498Szrj   /* No register may reach a location where it is not used.  Thus
1542*38fd1498Szrj      we trim the rr result to the places where it is used.  */
1543*38fd1498Szrj   bitmap_and_into (in, &bb_lr_info->in);
1544*38fd1498Szrj 
1545*38fd1498Szrj   return bitmap_ior_and_compl (out, &df_live_scratch, in, kill);
1546*38fd1498Szrj }
1547*38fd1498Szrj 
1548*38fd1498Szrj 
1549*38fd1498Szrj /* And the LR info with the may-initialized registers to produce the LIVE info.  */
1550*38fd1498Szrj 
1551*38fd1498Szrj static void
df_live_finalize(bitmap all_blocks)1552*38fd1498Szrj df_live_finalize (bitmap all_blocks)
1553*38fd1498Szrj {
1554*38fd1498Szrj 
1555*38fd1498Szrj   if (df_live->solutions_dirty)
1556*38fd1498Szrj     {
1557*38fd1498Szrj       bitmap_iterator bi;
1558*38fd1498Szrj       unsigned int bb_index;
1559*38fd1498Szrj 
1560*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1561*38fd1498Szrj 	{
1562*38fd1498Szrj 	  struct df_lr_bb_info *bb_lr_info = df_lr_get_bb_info (bb_index);
1563*38fd1498Szrj 	  struct df_live_bb_info *bb_live_info = df_live_get_bb_info (bb_index);
1564*38fd1498Szrj 
1565*38fd1498Szrj 	  /* No register may reach a location where it is not used.  Thus
1566*38fd1498Szrj 	     we trim the rr result to the places where it is used.  */
1567*38fd1498Szrj 	  bitmap_and_into (&bb_live_info->in, &bb_lr_info->in);
1568*38fd1498Szrj 	  bitmap_and_into (&bb_live_info->out, &bb_lr_info->out);
1569*38fd1498Szrj 	}
1570*38fd1498Szrj 
1571*38fd1498Szrj       df_live->solutions_dirty = false;
1572*38fd1498Szrj     }
1573*38fd1498Szrj }
1574*38fd1498Szrj 
1575*38fd1498Szrj 
1576*38fd1498Szrj /* Free all storage associated with the problem.  */
1577*38fd1498Szrj 
1578*38fd1498Szrj static void
df_live_free(void)1579*38fd1498Szrj df_live_free (void)
1580*38fd1498Szrj {
1581*38fd1498Szrj   struct df_live_problem_data *problem_data
1582*38fd1498Szrj     = (struct df_live_problem_data *) df_live->problem_data;
1583*38fd1498Szrj   if (df_live->block_info)
1584*38fd1498Szrj     {
1585*38fd1498Szrj       df_live->block_info_size = 0;
1586*38fd1498Szrj       free (df_live->block_info);
1587*38fd1498Szrj       df_live->block_info = NULL;
1588*38fd1498Szrj       bitmap_clear (&df_live_scratch);
1589*38fd1498Szrj       bitmap_obstack_release (&problem_data->live_bitmaps);
1590*38fd1498Szrj       free (problem_data);
1591*38fd1498Szrj       df_live->problem_data = NULL;
1592*38fd1498Szrj     }
1593*38fd1498Szrj   BITMAP_FREE (df_live->out_of_date_transfer_functions);
1594*38fd1498Szrj   free (df_live);
1595*38fd1498Szrj }
1596*38fd1498Szrj 
1597*38fd1498Szrj 
1598*38fd1498Szrj /* Debugging info at top of bb.  */
1599*38fd1498Szrj 
1600*38fd1498Szrj static void
df_live_top_dump(basic_block bb,FILE * file)1601*38fd1498Szrj df_live_top_dump (basic_block bb, FILE *file)
1602*38fd1498Szrj {
1603*38fd1498Szrj   struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1604*38fd1498Szrj   struct df_live_problem_data *problem_data;
1605*38fd1498Szrj 
1606*38fd1498Szrj   if (!bb_info)
1607*38fd1498Szrj     return;
1608*38fd1498Szrj 
1609*38fd1498Szrj   fprintf (file, ";; live  in  \t");
1610*38fd1498Szrj   df_print_regset (file, &bb_info->in);
1611*38fd1498Szrj   if (df_live->problem_data)
1612*38fd1498Szrj     {
1613*38fd1498Szrj       problem_data = (struct df_live_problem_data *)df_live->problem_data;
1614*38fd1498Szrj       if (problem_data->in)
1615*38fd1498Szrj 	{
1616*38fd1498Szrj 	  fprintf (file, ";;  old in  \t");
1617*38fd1498Szrj 	  df_print_regset (file, &problem_data->in[bb->index]);
1618*38fd1498Szrj 	}
1619*38fd1498Szrj     }
1620*38fd1498Szrj   fprintf (file, ";; live  gen \t");
1621*38fd1498Szrj   df_print_regset (file, &bb_info->gen);
1622*38fd1498Szrj   fprintf (file, ";; live  kill\t");
1623*38fd1498Szrj   df_print_regset (file, &bb_info->kill);
1624*38fd1498Szrj }
1625*38fd1498Szrj 
1626*38fd1498Szrj 
1627*38fd1498Szrj /* Debugging info at bottom of bb.  */
1628*38fd1498Szrj 
1629*38fd1498Szrj static void
df_live_bottom_dump(basic_block bb,FILE * file)1630*38fd1498Szrj df_live_bottom_dump (basic_block bb, FILE *file)
1631*38fd1498Szrj {
1632*38fd1498Szrj   struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1633*38fd1498Szrj   struct df_live_problem_data *problem_data;
1634*38fd1498Szrj 
1635*38fd1498Szrj   if (!bb_info)
1636*38fd1498Szrj     return;
1637*38fd1498Szrj 
1638*38fd1498Szrj   fprintf (file, ";; live  out \t");
1639*38fd1498Szrj   df_print_regset (file, &bb_info->out);
1640*38fd1498Szrj   if (df_live->problem_data)
1641*38fd1498Szrj     {
1642*38fd1498Szrj       problem_data = (struct df_live_problem_data *)df_live->problem_data;
1643*38fd1498Szrj       if (problem_data->out)
1644*38fd1498Szrj 	{
1645*38fd1498Szrj 	  fprintf (file, ";;  old out  \t");
1646*38fd1498Szrj 	  df_print_regset (file, &problem_data->out[bb->index]);
1647*38fd1498Szrj 	}
1648*38fd1498Szrj     }
1649*38fd1498Szrj }
1650*38fd1498Szrj 
1651*38fd1498Szrj 
1652*38fd1498Szrj /* Build the datastructure to verify that the solution to the dataflow
1653*38fd1498Szrj    equations is not dirty.  */
1654*38fd1498Szrj 
1655*38fd1498Szrj static void
df_live_verify_solution_start(void)1656*38fd1498Szrj df_live_verify_solution_start (void)
1657*38fd1498Szrj {
1658*38fd1498Szrj   basic_block bb;
1659*38fd1498Szrj   struct df_live_problem_data *problem_data;
1660*38fd1498Szrj   if (df_live->solutions_dirty)
1661*38fd1498Szrj     return;
1662*38fd1498Szrj 
1663*38fd1498Szrj   /* Set it true so that the solution is recomputed.  */
1664*38fd1498Szrj   df_live->solutions_dirty = true;
1665*38fd1498Szrj 
1666*38fd1498Szrj   problem_data = (struct df_live_problem_data *)df_live->problem_data;
1667*38fd1498Szrj   problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1668*38fd1498Szrj   problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
1669*38fd1498Szrj 
1670*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1671*38fd1498Szrj     {
1672*38fd1498Szrj       bitmap_initialize (&problem_data->in[bb->index], &problem_data->live_bitmaps);
1673*38fd1498Szrj       bitmap_initialize (&problem_data->out[bb->index], &problem_data->live_bitmaps);
1674*38fd1498Szrj       bitmap_copy (&problem_data->in[bb->index], DF_LIVE_IN (bb));
1675*38fd1498Szrj       bitmap_copy (&problem_data->out[bb->index], DF_LIVE_OUT (bb));
1676*38fd1498Szrj     }
1677*38fd1498Szrj }
1678*38fd1498Szrj 
1679*38fd1498Szrj 
1680*38fd1498Szrj /* Compare the saved datastructure and the new solution to the dataflow
1681*38fd1498Szrj    equations.  */
1682*38fd1498Szrj 
1683*38fd1498Szrj static void
df_live_verify_solution_end(void)1684*38fd1498Szrj df_live_verify_solution_end (void)
1685*38fd1498Szrj {
1686*38fd1498Szrj   struct df_live_problem_data *problem_data;
1687*38fd1498Szrj   basic_block bb;
1688*38fd1498Szrj 
1689*38fd1498Szrj   problem_data = (struct df_live_problem_data *)df_live->problem_data;
1690*38fd1498Szrj   if (!problem_data->out)
1691*38fd1498Szrj     return;
1692*38fd1498Szrj 
1693*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1694*38fd1498Szrj     {
1695*38fd1498Szrj       if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_LIVE_IN (bb)))
1696*38fd1498Szrj 	  || (!bitmap_equal_p (&problem_data->out[bb->index], DF_LIVE_OUT (bb))))
1697*38fd1498Szrj 	{
1698*38fd1498Szrj 	  /*df_dump (stderr);*/
1699*38fd1498Szrj 	  gcc_unreachable ();
1700*38fd1498Szrj 	}
1701*38fd1498Szrj     }
1702*38fd1498Szrj 
1703*38fd1498Szrj   /* Cannot delete them immediately because you may want to dump them
1704*38fd1498Szrj      if the comparison fails.  */
1705*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1706*38fd1498Szrj     {
1707*38fd1498Szrj       bitmap_clear (&problem_data->in[bb->index]);
1708*38fd1498Szrj       bitmap_clear (&problem_data->out[bb->index]);
1709*38fd1498Szrj     }
1710*38fd1498Szrj 
1711*38fd1498Szrj   free (problem_data->in);
1712*38fd1498Szrj   free (problem_data->out);
1713*38fd1498Szrj   free (problem_data);
1714*38fd1498Szrj   df_live->problem_data = NULL;
1715*38fd1498Szrj }
1716*38fd1498Szrj 
1717*38fd1498Szrj 
1718*38fd1498Szrj /* All of the information associated with every instance of the problem.  */
1719*38fd1498Szrj 
1720*38fd1498Szrj static const struct df_problem problem_LIVE =
1721*38fd1498Szrj {
1722*38fd1498Szrj   DF_LIVE,                      /* Problem id.  */
1723*38fd1498Szrj   DF_FORWARD,                   /* Direction.  */
1724*38fd1498Szrj   df_live_alloc,                /* Allocate the problem specific data.  */
1725*38fd1498Szrj   df_live_reset,                /* Reset global information.  */
1726*38fd1498Szrj   df_live_free_bb_info,         /* Free basic block info.  */
1727*38fd1498Szrj   df_live_local_compute,        /* Local compute function.  */
1728*38fd1498Szrj   df_live_init,                 /* Init the solution specific data.  */
1729*38fd1498Szrj   df_worklist_dataflow,         /* Worklist solver.  */
1730*38fd1498Szrj   NULL,                         /* Confluence operator 0.  */
1731*38fd1498Szrj   df_live_confluence_n,         /* Confluence operator n.  */
1732*38fd1498Szrj   df_live_transfer_function,    /* Transfer function.  */
1733*38fd1498Szrj   df_live_finalize,             /* Finalize function.  */
1734*38fd1498Szrj   df_live_free,                 /* Free all of the problem information.  */
1735*38fd1498Szrj   df_live_free,                 /* Remove this problem from the stack of dataflow problems.  */
1736*38fd1498Szrj   NULL,                         /* Debugging.  */
1737*38fd1498Szrj   df_live_top_dump,             /* Debugging start block.  */
1738*38fd1498Szrj   df_live_bottom_dump,          /* Debugging end block.  */
1739*38fd1498Szrj   NULL,                         /* Debugging start insn.  */
1740*38fd1498Szrj   NULL,                         /* Debugging end insn.  */
1741*38fd1498Szrj   df_live_verify_solution_start,/* Incremental solution verify start.  */
1742*38fd1498Szrj   df_live_verify_solution_end,  /* Incremental solution verify end.  */
1743*38fd1498Szrj   &problem_LR,                  /* Dependent problem.  */
1744*38fd1498Szrj   sizeof (struct df_live_bb_info),/* Size of entry of block_info array.  */
1745*38fd1498Szrj   TV_DF_LIVE,                   /* Timing variable.  */
1746*38fd1498Szrj   false                         /* Reset blocks on dropping out of blocks_to_analyze.  */
1747*38fd1498Szrj };
1748*38fd1498Szrj 
1749*38fd1498Szrj 
1750*38fd1498Szrj /* Create a new DATAFLOW instance and add it to an existing instance
1751*38fd1498Szrj    of DF.  The returned structure is what is used to get at the
1752*38fd1498Szrj    solution.  */
1753*38fd1498Szrj 
1754*38fd1498Szrj void
df_live_add_problem(void)1755*38fd1498Szrj df_live_add_problem (void)
1756*38fd1498Szrj {
1757*38fd1498Szrj   df_add_problem (&problem_LIVE);
1758*38fd1498Szrj   /* These will be initialized when df_scan_blocks processes each
1759*38fd1498Szrj      block.  */
1760*38fd1498Szrj   df_live->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
1761*38fd1498Szrj }
1762*38fd1498Szrj 
1763*38fd1498Szrj 
1764*38fd1498Szrj /* Set all of the blocks as dirty.  This needs to be done if this
1765*38fd1498Szrj    problem is added after all of the insns have been scanned.  */
1766*38fd1498Szrj 
1767*38fd1498Szrj void
df_live_set_all_dirty(void)1768*38fd1498Szrj df_live_set_all_dirty (void)
1769*38fd1498Szrj {
1770*38fd1498Szrj   basic_block bb;
1771*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1772*38fd1498Szrj     bitmap_set_bit (df_live->out_of_date_transfer_functions,
1773*38fd1498Szrj 		    bb->index);
1774*38fd1498Szrj }
1775*38fd1498Szrj 
1776*38fd1498Szrj 
1777*38fd1498Szrj /* Verify that all of the lr related info is consistent and
1778*38fd1498Szrj    correct.  */
1779*38fd1498Szrj 
1780*38fd1498Szrj void
df_live_verify_transfer_functions(void)1781*38fd1498Szrj df_live_verify_transfer_functions (void)
1782*38fd1498Szrj {
1783*38fd1498Szrj   basic_block bb;
1784*38fd1498Szrj   bitmap_head saved_gen;
1785*38fd1498Szrj   bitmap_head saved_kill;
1786*38fd1498Szrj   bitmap_head all_blocks;
1787*38fd1498Szrj 
1788*38fd1498Szrj   if (!df)
1789*38fd1498Szrj     return;
1790*38fd1498Szrj 
1791*38fd1498Szrj   bitmap_initialize (&saved_gen, &bitmap_default_obstack);
1792*38fd1498Szrj   bitmap_initialize (&saved_kill, &bitmap_default_obstack);
1793*38fd1498Szrj   bitmap_initialize (&all_blocks, &bitmap_default_obstack);
1794*38fd1498Szrj 
1795*38fd1498Szrj   df_grow_insn_info ();
1796*38fd1498Szrj 
1797*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
1798*38fd1498Szrj     {
1799*38fd1498Szrj       struct df_live_bb_info *bb_info = df_live_get_bb_info (bb->index);
1800*38fd1498Szrj       bitmap_set_bit (&all_blocks, bb->index);
1801*38fd1498Szrj 
1802*38fd1498Szrj       if (bb_info)
1803*38fd1498Szrj 	{
1804*38fd1498Szrj 	  /* Make a copy of the transfer functions and then compute
1805*38fd1498Szrj 	     new ones to see if the transfer functions have
1806*38fd1498Szrj 	     changed.  */
1807*38fd1498Szrj 	  if (!bitmap_bit_p (df_live->out_of_date_transfer_functions,
1808*38fd1498Szrj 			     bb->index))
1809*38fd1498Szrj 	    {
1810*38fd1498Szrj 	      bitmap_copy (&saved_gen, &bb_info->gen);
1811*38fd1498Szrj 	      bitmap_copy (&saved_kill, &bb_info->kill);
1812*38fd1498Szrj 	      bitmap_clear (&bb_info->gen);
1813*38fd1498Szrj 	      bitmap_clear (&bb_info->kill);
1814*38fd1498Szrj 
1815*38fd1498Szrj 	      df_live_bb_local_compute (bb->index);
1816*38fd1498Szrj 	      gcc_assert (bitmap_equal_p (&saved_gen, &bb_info->gen));
1817*38fd1498Szrj 	      gcc_assert (bitmap_equal_p (&saved_kill, &bb_info->kill));
1818*38fd1498Szrj 	    }
1819*38fd1498Szrj 	}
1820*38fd1498Szrj       else
1821*38fd1498Szrj 	{
1822*38fd1498Szrj 	  /* If we do not have basic block info, the block must be in
1823*38fd1498Szrj 	     the list of dirty blocks or else some one has added a
1824*38fd1498Szrj 	     block behind our backs. */
1825*38fd1498Szrj 	  gcc_assert (bitmap_bit_p (df_live->out_of_date_transfer_functions,
1826*38fd1498Szrj 				    bb->index));
1827*38fd1498Szrj 	}
1828*38fd1498Szrj       /* Make sure no one created a block without following
1829*38fd1498Szrj 	 procedures.  */
1830*38fd1498Szrj       gcc_assert (df_scan_get_bb_info (bb->index));
1831*38fd1498Szrj     }
1832*38fd1498Szrj 
1833*38fd1498Szrj   /* Make sure there are no dirty bits in blocks that have been deleted.  */
1834*38fd1498Szrj   gcc_assert (!bitmap_intersect_compl_p (df_live->out_of_date_transfer_functions,
1835*38fd1498Szrj 					 &all_blocks));
1836*38fd1498Szrj   bitmap_clear (&saved_gen);
1837*38fd1498Szrj   bitmap_clear (&saved_kill);
1838*38fd1498Szrj   bitmap_clear (&all_blocks);
1839*38fd1498Szrj }
1840*38fd1498Szrj 
1841*38fd1498Szrj /*----------------------------------------------------------------------------
1842*38fd1498Szrj    MUST-INITIALIZED REGISTERS.
1843*38fd1498Szrj ----------------------------------------------------------------------------*/
1844*38fd1498Szrj 
1845*38fd1498Szrj /* Private data used to verify the solution for this problem.  */
1846*38fd1498Szrj struct df_mir_problem_data
1847*38fd1498Szrj {
1848*38fd1498Szrj   bitmap_head *in;
1849*38fd1498Szrj   bitmap_head *out;
1850*38fd1498Szrj   /* An obstack for the bitmaps we need for this problem.  */
1851*38fd1498Szrj   bitmap_obstack mir_bitmaps;
1852*38fd1498Szrj };
1853*38fd1498Szrj 
1854*38fd1498Szrj 
1855*38fd1498Szrj /* Free basic block info.  */
1856*38fd1498Szrj 
1857*38fd1498Szrj static void
df_mir_free_bb_info(basic_block bb ATTRIBUTE_UNUSED,void * vbb_info)1858*38fd1498Szrj df_mir_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
1859*38fd1498Szrj 		     void *vbb_info)
1860*38fd1498Szrj {
1861*38fd1498Szrj   struct df_mir_bb_info *bb_info = (struct df_mir_bb_info *) vbb_info;
1862*38fd1498Szrj   if (bb_info)
1863*38fd1498Szrj     {
1864*38fd1498Szrj       bitmap_clear (&bb_info->gen);
1865*38fd1498Szrj       bitmap_clear (&bb_info->kill);
1866*38fd1498Szrj       bitmap_clear (&bb_info->in);
1867*38fd1498Szrj       bitmap_clear (&bb_info->out);
1868*38fd1498Szrj     }
1869*38fd1498Szrj }
1870*38fd1498Szrj 
1871*38fd1498Szrj 
1872*38fd1498Szrj /* Allocate or reset bitmaps for DF_MIR blocks. The solution bits are
1873*38fd1498Szrj    not touched unless the block is new.  */
1874*38fd1498Szrj 
1875*38fd1498Szrj static void
df_mir_alloc(bitmap all_blocks)1876*38fd1498Szrj df_mir_alloc (bitmap all_blocks)
1877*38fd1498Szrj {
1878*38fd1498Szrj   unsigned int bb_index;
1879*38fd1498Szrj   bitmap_iterator bi;
1880*38fd1498Szrj   struct df_mir_problem_data *problem_data;
1881*38fd1498Szrj 
1882*38fd1498Szrj   if (df_mir->problem_data)
1883*38fd1498Szrj     problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
1884*38fd1498Szrj   else
1885*38fd1498Szrj     {
1886*38fd1498Szrj       problem_data = XNEW (struct df_mir_problem_data);
1887*38fd1498Szrj       df_mir->problem_data = problem_data;
1888*38fd1498Szrj 
1889*38fd1498Szrj       problem_data->out = NULL;
1890*38fd1498Szrj       problem_data->in = NULL;
1891*38fd1498Szrj       bitmap_obstack_initialize (&problem_data->mir_bitmaps);
1892*38fd1498Szrj     }
1893*38fd1498Szrj 
1894*38fd1498Szrj   df_grow_bb_info (df_mir);
1895*38fd1498Szrj 
1896*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1897*38fd1498Szrj     {
1898*38fd1498Szrj       struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1899*38fd1498Szrj 
1900*38fd1498Szrj       /* When bitmaps are already initialized, just clear them.  */
1901*38fd1498Szrj       if (bb_info->kill.obstack)
1902*38fd1498Szrj 	{
1903*38fd1498Szrj 	  bitmap_clear (&bb_info->kill);
1904*38fd1498Szrj 	  bitmap_clear (&bb_info->gen);
1905*38fd1498Szrj 	}
1906*38fd1498Szrj       else
1907*38fd1498Szrj 	{
1908*38fd1498Szrj 	  bitmap_initialize (&bb_info->kill, &problem_data->mir_bitmaps);
1909*38fd1498Szrj 	  bitmap_initialize (&bb_info->gen, &problem_data->mir_bitmaps);
1910*38fd1498Szrj 	  bitmap_initialize (&bb_info->in, &problem_data->mir_bitmaps);
1911*38fd1498Szrj 	  bitmap_initialize (&bb_info->out, &problem_data->mir_bitmaps);
1912*38fd1498Szrj 	  bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1913*38fd1498Szrj 	  bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1914*38fd1498Szrj 	}
1915*38fd1498Szrj     }
1916*38fd1498Szrj 
1917*38fd1498Szrj   df_mir->optional_p = 1;
1918*38fd1498Szrj }
1919*38fd1498Szrj 
1920*38fd1498Szrj 
1921*38fd1498Szrj /* Reset the global solution for recalculation.  */
1922*38fd1498Szrj 
1923*38fd1498Szrj static void
df_mir_reset(bitmap all_blocks)1924*38fd1498Szrj df_mir_reset (bitmap all_blocks)
1925*38fd1498Szrj {
1926*38fd1498Szrj   unsigned int bb_index;
1927*38fd1498Szrj   bitmap_iterator bi;
1928*38fd1498Szrj 
1929*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1930*38fd1498Szrj     {
1931*38fd1498Szrj       struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1932*38fd1498Szrj 
1933*38fd1498Szrj       gcc_assert (bb_info);
1934*38fd1498Szrj 
1935*38fd1498Szrj       bitmap_clear (&bb_info->in);
1936*38fd1498Szrj       bitmap_set_range (&bb_info->in, 0, DF_REG_SIZE (df));
1937*38fd1498Szrj       bitmap_clear (&bb_info->out);
1938*38fd1498Szrj       bitmap_set_range (&bb_info->out, 0, DF_REG_SIZE (df));
1939*38fd1498Szrj     }
1940*38fd1498Szrj }
1941*38fd1498Szrj 
1942*38fd1498Szrj 
1943*38fd1498Szrj /* Compute local uninitialized register info for basic block BB.  */
1944*38fd1498Szrj 
1945*38fd1498Szrj static void
df_mir_bb_local_compute(unsigned int bb_index)1946*38fd1498Szrj df_mir_bb_local_compute (unsigned int bb_index)
1947*38fd1498Szrj {
1948*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
1949*38fd1498Szrj   struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
1950*38fd1498Szrj   rtx_insn *insn;
1951*38fd1498Szrj   int luid = 0;
1952*38fd1498Szrj 
1953*38fd1498Szrj   /* Ignoring artificial defs is intentional: these often pretend that some
1954*38fd1498Szrj      registers carry incoming arguments (when they are FUNCTION_ARG_REGNO) even
1955*38fd1498Szrj      though they are not used for that.  As a result, conservatively assume
1956*38fd1498Szrj      they may be uninitialized.  */
1957*38fd1498Szrj 
1958*38fd1498Szrj   FOR_BB_INSNS (bb, insn)
1959*38fd1498Szrj     {
1960*38fd1498Szrj       unsigned int uid = INSN_UID (insn);
1961*38fd1498Szrj       struct df_insn_info *insn_info = DF_INSN_UID_GET (uid);
1962*38fd1498Szrj 
1963*38fd1498Szrj       /* Inserting labels does not always trigger the incremental
1964*38fd1498Szrj 	 rescanning.  */
1965*38fd1498Szrj       if (!insn_info)
1966*38fd1498Szrj 	{
1967*38fd1498Szrj 	  gcc_assert (!INSN_P (insn));
1968*38fd1498Szrj 	  insn_info = df_insn_create_insn_record (insn);
1969*38fd1498Szrj 	}
1970*38fd1498Szrj 
1971*38fd1498Szrj       DF_INSN_INFO_LUID (insn_info) = luid;
1972*38fd1498Szrj       if (!INSN_P (insn))
1973*38fd1498Szrj 	continue;
1974*38fd1498Szrj 
1975*38fd1498Szrj       luid++;
1976*38fd1498Szrj       df_mir_simulate_one_insn (bb, insn, &bb_info->kill, &bb_info->gen);
1977*38fd1498Szrj     }
1978*38fd1498Szrj }
1979*38fd1498Szrj 
1980*38fd1498Szrj 
1981*38fd1498Szrj /* Compute local uninitialized register info.  */
1982*38fd1498Szrj 
1983*38fd1498Szrj static void
df_mir_local_compute(bitmap all_blocks)1984*38fd1498Szrj df_mir_local_compute (bitmap all_blocks)
1985*38fd1498Szrj {
1986*38fd1498Szrj   unsigned int bb_index;
1987*38fd1498Szrj   bitmap_iterator bi;
1988*38fd1498Szrj 
1989*38fd1498Szrj   df_grow_insn_info ();
1990*38fd1498Szrj 
1991*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
1992*38fd1498Szrj     {
1993*38fd1498Szrj       df_mir_bb_local_compute (bb_index);
1994*38fd1498Szrj     }
1995*38fd1498Szrj }
1996*38fd1498Szrj 
1997*38fd1498Szrj 
1998*38fd1498Szrj /* Initialize the solution vectors.  */
1999*38fd1498Szrj 
2000*38fd1498Szrj static void
df_mir_init(bitmap all_blocks)2001*38fd1498Szrj df_mir_init (bitmap all_blocks)
2002*38fd1498Szrj {
2003*38fd1498Szrj   df_mir_reset (all_blocks);
2004*38fd1498Szrj }
2005*38fd1498Szrj 
2006*38fd1498Szrj 
2007*38fd1498Szrj /* Initialize IN sets for blocks with no predecessors: when landing on such
2008*38fd1498Szrj    blocks, assume all registers are uninitialized.  */
2009*38fd1498Szrj 
2010*38fd1498Szrj static void
df_mir_confluence_0(basic_block bb)2011*38fd1498Szrj df_mir_confluence_0 (basic_block bb)
2012*38fd1498Szrj {
2013*38fd1498Szrj   struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2014*38fd1498Szrj 
2015*38fd1498Szrj   bitmap_clear (&bb_info->in);
2016*38fd1498Szrj }
2017*38fd1498Szrj 
2018*38fd1498Szrj 
2019*38fd1498Szrj /* Forward confluence function that ignores fake edges.  */
2020*38fd1498Szrj 
2021*38fd1498Szrj static bool
df_mir_confluence_n(edge e)2022*38fd1498Szrj df_mir_confluence_n (edge e)
2023*38fd1498Szrj {
2024*38fd1498Szrj   bitmap op1 = &df_mir_get_bb_info (e->dest->index)->in;
2025*38fd1498Szrj   bitmap op2 = &df_mir_get_bb_info (e->src->index)->out;
2026*38fd1498Szrj 
2027*38fd1498Szrj   if (e->flags & EDGE_FAKE)
2028*38fd1498Szrj     return false;
2029*38fd1498Szrj 
2030*38fd1498Szrj   /* A register is must-initialized at the entry of a basic block iff it is
2031*38fd1498Szrj      must-initialized at the exit of all the predecessors.  */
2032*38fd1498Szrj   return bitmap_and_into (op1, op2);
2033*38fd1498Szrj }
2034*38fd1498Szrj 
2035*38fd1498Szrj 
2036*38fd1498Szrj /* Transfer function for the forwards must-initialized problem.  */
2037*38fd1498Szrj 
2038*38fd1498Szrj static bool
df_mir_transfer_function(int bb_index)2039*38fd1498Szrj df_mir_transfer_function (int bb_index)
2040*38fd1498Szrj {
2041*38fd1498Szrj   struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb_index);
2042*38fd1498Szrj   bitmap in = &bb_info->in;
2043*38fd1498Szrj   bitmap out = &bb_info->out;
2044*38fd1498Szrj   bitmap gen = &bb_info->gen;
2045*38fd1498Szrj   bitmap kill = &bb_info->kill;
2046*38fd1498Szrj 
2047*38fd1498Szrj   return bitmap_ior_and_compl (out, gen, in, kill);
2048*38fd1498Szrj }
2049*38fd1498Szrj 
2050*38fd1498Szrj 
2051*38fd1498Szrj /* Free all storage associated with the problem.  */
2052*38fd1498Szrj 
2053*38fd1498Szrj static void
df_mir_free(void)2054*38fd1498Szrj df_mir_free (void)
2055*38fd1498Szrj {
2056*38fd1498Szrj   struct df_mir_problem_data *problem_data
2057*38fd1498Szrj     = (struct df_mir_problem_data *) df_mir->problem_data;
2058*38fd1498Szrj   if (df_mir->block_info)
2059*38fd1498Szrj     {
2060*38fd1498Szrj       df_mir->block_info_size = 0;
2061*38fd1498Szrj       free (df_mir->block_info);
2062*38fd1498Szrj       df_mir->block_info = NULL;
2063*38fd1498Szrj       bitmap_obstack_release (&problem_data->mir_bitmaps);
2064*38fd1498Szrj       free (problem_data);
2065*38fd1498Szrj       df_mir->problem_data = NULL;
2066*38fd1498Szrj     }
2067*38fd1498Szrj   free (df_mir);
2068*38fd1498Szrj }
2069*38fd1498Szrj 
2070*38fd1498Szrj 
2071*38fd1498Szrj /* Debugging info at top of bb.  */
2072*38fd1498Szrj 
2073*38fd1498Szrj static void
df_mir_top_dump(basic_block bb,FILE * file)2074*38fd1498Szrj df_mir_top_dump (basic_block bb, FILE *file)
2075*38fd1498Szrj {
2076*38fd1498Szrj   struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2077*38fd1498Szrj 
2078*38fd1498Szrj   if (!bb_info)
2079*38fd1498Szrj     return;
2080*38fd1498Szrj 
2081*38fd1498Szrj   fprintf (file, ";; mir   in  \t");
2082*38fd1498Szrj   df_print_regset (file, &bb_info->in);
2083*38fd1498Szrj   fprintf (file, ";; mir   kill\t");
2084*38fd1498Szrj   df_print_regset (file, &bb_info->kill);
2085*38fd1498Szrj   fprintf (file, ";; mir   gen \t");
2086*38fd1498Szrj   df_print_regset (file, &bb_info->gen);
2087*38fd1498Szrj }
2088*38fd1498Szrj 
2089*38fd1498Szrj /* Debugging info at bottom of bb.  */
2090*38fd1498Szrj 
2091*38fd1498Szrj static void
df_mir_bottom_dump(basic_block bb,FILE * file)2092*38fd1498Szrj df_mir_bottom_dump (basic_block bb, FILE *file)
2093*38fd1498Szrj {
2094*38fd1498Szrj   struct df_mir_bb_info *bb_info = df_mir_get_bb_info (bb->index);
2095*38fd1498Szrj 
2096*38fd1498Szrj   if (!bb_info)
2097*38fd1498Szrj     return;
2098*38fd1498Szrj 
2099*38fd1498Szrj   fprintf (file, ";; mir   out \t");
2100*38fd1498Szrj   df_print_regset (file, &bb_info->out);
2101*38fd1498Szrj }
2102*38fd1498Szrj 
2103*38fd1498Szrj 
2104*38fd1498Szrj /* Build the datastructure to verify that the solution to the dataflow
2105*38fd1498Szrj    equations is not dirty.  */
2106*38fd1498Szrj 
2107*38fd1498Szrj static void
df_mir_verify_solution_start(void)2108*38fd1498Szrj df_mir_verify_solution_start (void)
2109*38fd1498Szrj {
2110*38fd1498Szrj   basic_block bb;
2111*38fd1498Szrj   struct df_mir_problem_data *problem_data;
2112*38fd1498Szrj   if (df_mir->solutions_dirty)
2113*38fd1498Szrj     return;
2114*38fd1498Szrj 
2115*38fd1498Szrj   /* Set it true so that the solution is recomputed.  */
2116*38fd1498Szrj   df_mir->solutions_dirty = true;
2117*38fd1498Szrj 
2118*38fd1498Szrj   problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2119*38fd1498Szrj   problem_data->in = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2120*38fd1498Szrj   problem_data->out = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
2121*38fd1498Szrj   bitmap_obstack_initialize (&problem_data->mir_bitmaps);
2122*38fd1498Szrj 
2123*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
2124*38fd1498Szrj     {
2125*38fd1498Szrj       bitmap_initialize (&problem_data->in[bb->index], &problem_data->mir_bitmaps);
2126*38fd1498Szrj       bitmap_initialize (&problem_data->out[bb->index], &problem_data->mir_bitmaps);
2127*38fd1498Szrj       bitmap_copy (&problem_data->in[bb->index], DF_MIR_IN (bb));
2128*38fd1498Szrj       bitmap_copy (&problem_data->out[bb->index], DF_MIR_OUT (bb));
2129*38fd1498Szrj     }
2130*38fd1498Szrj }
2131*38fd1498Szrj 
2132*38fd1498Szrj 
2133*38fd1498Szrj /* Compare the saved datastructure and the new solution to the dataflow
2134*38fd1498Szrj    equations.  */
2135*38fd1498Szrj 
2136*38fd1498Szrj static void
df_mir_verify_solution_end(void)2137*38fd1498Szrj df_mir_verify_solution_end (void)
2138*38fd1498Szrj {
2139*38fd1498Szrj   struct df_mir_problem_data *problem_data;
2140*38fd1498Szrj   basic_block bb;
2141*38fd1498Szrj 
2142*38fd1498Szrj   problem_data = (struct df_mir_problem_data *) df_mir->problem_data;
2143*38fd1498Szrj   if (!problem_data->out)
2144*38fd1498Szrj     return;
2145*38fd1498Szrj 
2146*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
2147*38fd1498Szrj     {
2148*38fd1498Szrj       if ((!bitmap_equal_p (&problem_data->in[bb->index], DF_MIR_IN (bb)))
2149*38fd1498Szrj 	  || (!bitmap_equal_p (&problem_data->out[bb->index], DF_MIR_OUT (bb))))
2150*38fd1498Szrj 	gcc_unreachable ();
2151*38fd1498Szrj     }
2152*38fd1498Szrj 
2153*38fd1498Szrj   /* Cannot delete them immediately because you may want to dump them
2154*38fd1498Szrj      if the comparison fails.  */
2155*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
2156*38fd1498Szrj     {
2157*38fd1498Szrj       bitmap_clear (&problem_data->in[bb->index]);
2158*38fd1498Szrj       bitmap_clear (&problem_data->out[bb->index]);
2159*38fd1498Szrj     }
2160*38fd1498Szrj 
2161*38fd1498Szrj   free (problem_data->in);
2162*38fd1498Szrj   free (problem_data->out);
2163*38fd1498Szrj   bitmap_obstack_release (&problem_data->mir_bitmaps);
2164*38fd1498Szrj   free (problem_data);
2165*38fd1498Szrj   df_mir->problem_data = NULL;
2166*38fd1498Szrj }
2167*38fd1498Szrj 
2168*38fd1498Szrj 
2169*38fd1498Szrj /* All of the information associated with every instance of the problem.  */
2170*38fd1498Szrj 
2171*38fd1498Szrj static const struct df_problem problem_MIR =
2172*38fd1498Szrj {
2173*38fd1498Szrj   DF_MIR,                       /* Problem id.  */
2174*38fd1498Szrj   DF_FORWARD,                   /* Direction.  */
2175*38fd1498Szrj   df_mir_alloc,                 /* Allocate the problem specific data.  */
2176*38fd1498Szrj   df_mir_reset,                 /* Reset global information.  */
2177*38fd1498Szrj   df_mir_free_bb_info,          /* Free basic block info.  */
2178*38fd1498Szrj   df_mir_local_compute,         /* Local compute function.  */
2179*38fd1498Szrj   df_mir_init,                  /* Init the solution specific data.  */
2180*38fd1498Szrj   df_worklist_dataflow,         /* Worklist solver.  */
2181*38fd1498Szrj   df_mir_confluence_0,          /* Confluence operator 0.  */
2182*38fd1498Szrj   df_mir_confluence_n,          /* Confluence operator n.  */
2183*38fd1498Szrj   df_mir_transfer_function,     /* Transfer function.  */
2184*38fd1498Szrj   NULL,                         /* Finalize function.  */
2185*38fd1498Szrj   df_mir_free,                  /* Free all of the problem information.  */
2186*38fd1498Szrj   df_mir_free,                  /* Remove this problem from the stack of dataflow problems.  */
2187*38fd1498Szrj   NULL,                         /* Debugging.  */
2188*38fd1498Szrj   df_mir_top_dump,              /* Debugging start block.  */
2189*38fd1498Szrj   df_mir_bottom_dump,           /* Debugging end block.  */
2190*38fd1498Szrj   NULL,                         /* Debugging start insn.  */
2191*38fd1498Szrj   NULL,                         /* Debugging end insn.  */
2192*38fd1498Szrj   df_mir_verify_solution_start, /* Incremental solution verify start.  */
2193*38fd1498Szrj   df_mir_verify_solution_end,   /* Incremental solution verify end.  */
2194*38fd1498Szrj   NULL,                         /* Dependent problem.  */
2195*38fd1498Szrj   sizeof (struct df_mir_bb_info),/* Size of entry of block_info array.  */
2196*38fd1498Szrj   TV_DF_MIR,                    /* Timing variable.  */
2197*38fd1498Szrj   false                         /* Reset blocks on dropping out of blocks_to_analyze.  */
2198*38fd1498Szrj };
2199*38fd1498Szrj 
2200*38fd1498Szrj 
2201*38fd1498Szrj /* Create a new DATAFLOW instance and add it to an existing instance
2202*38fd1498Szrj    of DF.  */
2203*38fd1498Szrj 
2204*38fd1498Szrj void
df_mir_add_problem(void)2205*38fd1498Szrj df_mir_add_problem (void)
2206*38fd1498Szrj {
2207*38fd1498Szrj   df_add_problem (&problem_MIR);
2208*38fd1498Szrj   /* These will be initialized when df_scan_blocks processes each
2209*38fd1498Szrj      block.  */
2210*38fd1498Szrj   df_mir->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2211*38fd1498Szrj }
2212*38fd1498Szrj 
2213*38fd1498Szrj 
2214*38fd1498Szrj /* Apply the effects of the gen/kills in INSN to the corresponding bitmaps.  */
2215*38fd1498Szrj 
2216*38fd1498Szrj void
df_mir_simulate_one_insn(basic_block bb ATTRIBUTE_UNUSED,rtx_insn * insn,bitmap kill,bitmap gen)2217*38fd1498Szrj df_mir_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
2218*38fd1498Szrj 			  bitmap kill, bitmap gen)
2219*38fd1498Szrj {
2220*38fd1498Szrj   df_ref def;
2221*38fd1498Szrj 
2222*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
2223*38fd1498Szrj     {
2224*38fd1498Szrj       unsigned int regno = DF_REF_REGNO (def);
2225*38fd1498Szrj 
2226*38fd1498Szrj       /* The order of GENs/KILLs matters, so if this def clobbers a reg, any
2227*38fd1498Szrj 	 previous gen is irrelevant (and reciprocally).  Also, claim that a
2228*38fd1498Szrj 	 register is GEN only if it is in all cases.  */
2229*38fd1498Szrj       if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
2230*38fd1498Szrj 	{
2231*38fd1498Szrj 	  bitmap_set_bit (kill, regno);
2232*38fd1498Szrj 	  bitmap_clear_bit (gen, regno);
2233*38fd1498Szrj 	}
2234*38fd1498Szrj       /* In the worst case, partial and conditional defs can leave bits
2235*38fd1498Szrj 	 uninitialized, so assume they do not change anything.  */
2236*38fd1498Szrj       else if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
2237*38fd1498Szrj 	{
2238*38fd1498Szrj 	  bitmap_set_bit (gen, regno);
2239*38fd1498Szrj 	  bitmap_clear_bit (kill, regno);
2240*38fd1498Szrj 	}
2241*38fd1498Szrj     }
2242*38fd1498Szrj }
2243*38fd1498Szrj 
2244*38fd1498Szrj /*----------------------------------------------------------------------------
2245*38fd1498Szrj    CREATE DEF_USE (DU) and / or USE_DEF (UD) CHAINS
2246*38fd1498Szrj 
2247*38fd1498Szrj    Link either the defs to the uses and / or the uses to the defs.
2248*38fd1498Szrj 
2249*38fd1498Szrj    These problems are set up like the other dataflow problems so that
2250*38fd1498Szrj    they nicely fit into the framework.  They are much simpler and only
2251*38fd1498Szrj    involve a single traversal of instructions and an examination of
2252*38fd1498Szrj    the reaching defs information (the dependent problem).
2253*38fd1498Szrj ----------------------------------------------------------------------------*/
2254*38fd1498Szrj 
2255*38fd1498Szrj #define df_chain_problem_p(FLAG) (((enum df_chain_flags)df_chain->local_flags)&(FLAG))
2256*38fd1498Szrj 
2257*38fd1498Szrj /* Create a du or ud chain from SRC to DST and link it into SRC.   */
2258*38fd1498Szrj 
2259*38fd1498Szrj struct df_link *
df_chain_create(df_ref src,df_ref dst)2260*38fd1498Szrj df_chain_create (df_ref src, df_ref dst)
2261*38fd1498Szrj {
2262*38fd1498Szrj   struct df_link *head = DF_REF_CHAIN (src);
2263*38fd1498Szrj   struct df_link *link = df_chain->block_pool->allocate ();
2264*38fd1498Szrj 
2265*38fd1498Szrj   DF_REF_CHAIN (src) = link;
2266*38fd1498Szrj   link->next = head;
2267*38fd1498Szrj   link->ref = dst;
2268*38fd1498Szrj   return link;
2269*38fd1498Szrj }
2270*38fd1498Szrj 
2271*38fd1498Szrj 
2272*38fd1498Szrj /* Delete any du or ud chains that start at REF and point to
2273*38fd1498Szrj    TARGET.  */
2274*38fd1498Szrj static void
df_chain_unlink_1(df_ref ref,df_ref target)2275*38fd1498Szrj df_chain_unlink_1 (df_ref ref, df_ref target)
2276*38fd1498Szrj {
2277*38fd1498Szrj   struct df_link *chain = DF_REF_CHAIN (ref);
2278*38fd1498Szrj   struct df_link *prev = NULL;
2279*38fd1498Szrj 
2280*38fd1498Szrj   while (chain)
2281*38fd1498Szrj     {
2282*38fd1498Szrj       if (chain->ref == target)
2283*38fd1498Szrj 	{
2284*38fd1498Szrj 	  if (prev)
2285*38fd1498Szrj 	    prev->next = chain->next;
2286*38fd1498Szrj 	  else
2287*38fd1498Szrj 	    DF_REF_CHAIN (ref) = chain->next;
2288*38fd1498Szrj 	  df_chain->block_pool->remove (chain);
2289*38fd1498Szrj 	  return;
2290*38fd1498Szrj 	}
2291*38fd1498Szrj       prev = chain;
2292*38fd1498Szrj       chain = chain->next;
2293*38fd1498Szrj     }
2294*38fd1498Szrj }
2295*38fd1498Szrj 
2296*38fd1498Szrj 
2297*38fd1498Szrj /* Delete a du or ud chain that leave or point to REF.  */
2298*38fd1498Szrj 
2299*38fd1498Szrj void
df_chain_unlink(df_ref ref)2300*38fd1498Szrj df_chain_unlink (df_ref ref)
2301*38fd1498Szrj {
2302*38fd1498Szrj   struct df_link *chain = DF_REF_CHAIN (ref);
2303*38fd1498Szrj   while (chain)
2304*38fd1498Szrj     {
2305*38fd1498Szrj       struct df_link *next = chain->next;
2306*38fd1498Szrj       /* Delete the other side if it exists.  */
2307*38fd1498Szrj       df_chain_unlink_1 (chain->ref, ref);
2308*38fd1498Szrj       df_chain->block_pool->remove (chain);
2309*38fd1498Szrj       chain = next;
2310*38fd1498Szrj     }
2311*38fd1498Szrj   DF_REF_CHAIN (ref) = NULL;
2312*38fd1498Szrj }
2313*38fd1498Szrj 
2314*38fd1498Szrj 
2315*38fd1498Szrj /* Copy the du or ud chain starting at FROM_REF and attach it to
2316*38fd1498Szrj    TO_REF.  */
2317*38fd1498Szrj 
2318*38fd1498Szrj void
df_chain_copy(df_ref to_ref,struct df_link * from_ref)2319*38fd1498Szrj df_chain_copy (df_ref to_ref,
2320*38fd1498Szrj 	       struct df_link *from_ref)
2321*38fd1498Szrj {
2322*38fd1498Szrj   while (from_ref)
2323*38fd1498Szrj     {
2324*38fd1498Szrj       df_chain_create (to_ref, from_ref->ref);
2325*38fd1498Szrj       from_ref = from_ref->next;
2326*38fd1498Szrj     }
2327*38fd1498Szrj }
2328*38fd1498Szrj 
2329*38fd1498Szrj 
2330*38fd1498Szrj /* Remove this problem from the stack of dataflow problems.  */
2331*38fd1498Szrj 
2332*38fd1498Szrj static void
df_chain_remove_problem(void)2333*38fd1498Szrj df_chain_remove_problem (void)
2334*38fd1498Szrj {
2335*38fd1498Szrj   bitmap_iterator bi;
2336*38fd1498Szrj   unsigned int bb_index;
2337*38fd1498Szrj 
2338*38fd1498Szrj   /* Wholesale destruction of the old chains.  */
2339*38fd1498Szrj   if (df_chain->block_pool)
2340*38fd1498Szrj     delete df_chain->block_pool;
2341*38fd1498Szrj 
2342*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (df_chain->out_of_date_transfer_functions, 0, bb_index, bi)
2343*38fd1498Szrj     {
2344*38fd1498Szrj       rtx_insn *insn;
2345*38fd1498Szrj       df_ref def, use;
2346*38fd1498Szrj       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2347*38fd1498Szrj 
2348*38fd1498Szrj       if (df_chain_problem_p (DF_DU_CHAIN))
2349*38fd1498Szrj 	FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2350*38fd1498Szrj 	  DF_REF_CHAIN (def) = NULL;
2351*38fd1498Szrj       if (df_chain_problem_p (DF_UD_CHAIN))
2352*38fd1498Szrj 	FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2353*38fd1498Szrj 	  DF_REF_CHAIN (use) = NULL;
2354*38fd1498Szrj 
2355*38fd1498Szrj       FOR_BB_INSNS (bb, insn)
2356*38fd1498Szrj 	if (INSN_P (insn))
2357*38fd1498Szrj 	  {
2358*38fd1498Szrj 	    df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2359*38fd1498Szrj 	    if (df_chain_problem_p (DF_DU_CHAIN))
2360*38fd1498Szrj 	      FOR_EACH_INSN_INFO_DEF (def, insn_info)
2361*38fd1498Szrj 		DF_REF_CHAIN (def) = NULL;
2362*38fd1498Szrj 	    if (df_chain_problem_p (DF_UD_CHAIN))
2363*38fd1498Szrj 	      {
2364*38fd1498Szrj 		FOR_EACH_INSN_INFO_USE (use, insn_info)
2365*38fd1498Szrj 		  DF_REF_CHAIN (use) = NULL;
2366*38fd1498Szrj 		FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2367*38fd1498Szrj 		  DF_REF_CHAIN (use) = NULL;
2368*38fd1498Szrj 	      }
2369*38fd1498Szrj 	  }
2370*38fd1498Szrj     }
2371*38fd1498Szrj 
2372*38fd1498Szrj   bitmap_clear (df_chain->out_of_date_transfer_functions);
2373*38fd1498Szrj   df_chain->block_pool = NULL;
2374*38fd1498Szrj }
2375*38fd1498Szrj 
2376*38fd1498Szrj 
2377*38fd1498Szrj /* Remove the chain problem completely.  */
2378*38fd1498Szrj 
2379*38fd1498Szrj static void
df_chain_fully_remove_problem(void)2380*38fd1498Szrj df_chain_fully_remove_problem (void)
2381*38fd1498Szrj {
2382*38fd1498Szrj   df_chain_remove_problem ();
2383*38fd1498Szrj   BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2384*38fd1498Szrj   free (df_chain);
2385*38fd1498Szrj }
2386*38fd1498Szrj 
2387*38fd1498Szrj 
2388*38fd1498Szrj /* Create def-use or use-def chains.  */
2389*38fd1498Szrj 
2390*38fd1498Szrj static void
df_chain_alloc(bitmap all_blocks ATTRIBUTE_UNUSED)2391*38fd1498Szrj df_chain_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2392*38fd1498Szrj {
2393*38fd1498Szrj   df_chain_remove_problem ();
2394*38fd1498Szrj   df_chain->block_pool = new object_allocator<df_link> ("df_chain_block pool");
2395*38fd1498Szrj   df_chain->optional_p = true;
2396*38fd1498Szrj }
2397*38fd1498Szrj 
2398*38fd1498Szrj 
2399*38fd1498Szrj /* Reset all of the chains when the set of basic blocks changes.  */
2400*38fd1498Szrj 
2401*38fd1498Szrj static void
df_chain_reset(bitmap blocks_to_clear ATTRIBUTE_UNUSED)2402*38fd1498Szrj df_chain_reset (bitmap blocks_to_clear ATTRIBUTE_UNUSED)
2403*38fd1498Szrj {
2404*38fd1498Szrj   df_chain_remove_problem ();
2405*38fd1498Szrj }
2406*38fd1498Szrj 
2407*38fd1498Szrj 
2408*38fd1498Szrj /* Create the chains for a list of USEs.  */
2409*38fd1498Szrj 
2410*38fd1498Szrj static void
df_chain_create_bb_process_use(bitmap local_rd,df_ref use,int top_flag)2411*38fd1498Szrj df_chain_create_bb_process_use (bitmap local_rd,
2412*38fd1498Szrj 				df_ref use,
2413*38fd1498Szrj 				int top_flag)
2414*38fd1498Szrj {
2415*38fd1498Szrj   bitmap_iterator bi;
2416*38fd1498Szrj   unsigned int def_index;
2417*38fd1498Szrj 
2418*38fd1498Szrj   for (; use; use = DF_REF_NEXT_LOC (use))
2419*38fd1498Szrj     {
2420*38fd1498Szrj       unsigned int uregno = DF_REF_REGNO (use);
2421*38fd1498Szrj       if ((!(df->changeable_flags & DF_NO_HARD_REGS))
2422*38fd1498Szrj 	  || (uregno >= FIRST_PSEUDO_REGISTER))
2423*38fd1498Szrj 	{
2424*38fd1498Szrj 	  /* Do not want to go through this for an uninitialized var.  */
2425*38fd1498Szrj 	  int count = DF_DEFS_COUNT (uregno);
2426*38fd1498Szrj 	  if (count)
2427*38fd1498Szrj 	    {
2428*38fd1498Szrj 	      if (top_flag == (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2429*38fd1498Szrj 		{
2430*38fd1498Szrj 		  unsigned int first_index = DF_DEFS_BEGIN (uregno);
2431*38fd1498Szrj 		  unsigned int last_index = first_index + count - 1;
2432*38fd1498Szrj 
2433*38fd1498Szrj 		  EXECUTE_IF_SET_IN_BITMAP (local_rd, first_index, def_index, bi)
2434*38fd1498Szrj 		    {
2435*38fd1498Szrj 		      df_ref def;
2436*38fd1498Szrj 		      if (def_index > last_index)
2437*38fd1498Szrj 			break;
2438*38fd1498Szrj 
2439*38fd1498Szrj 		      def = DF_DEFS_GET (def_index);
2440*38fd1498Szrj 		      if (df_chain_problem_p (DF_DU_CHAIN))
2441*38fd1498Szrj 			df_chain_create (def, use);
2442*38fd1498Szrj 		      if (df_chain_problem_p (DF_UD_CHAIN))
2443*38fd1498Szrj 			df_chain_create (use, def);
2444*38fd1498Szrj 		    }
2445*38fd1498Szrj 		}
2446*38fd1498Szrj 	    }
2447*38fd1498Szrj 	}
2448*38fd1498Szrj     }
2449*38fd1498Szrj }
2450*38fd1498Szrj 
2451*38fd1498Szrj 
2452*38fd1498Szrj /* Create chains from reaching defs bitmaps for basic block BB.  */
2453*38fd1498Szrj 
2454*38fd1498Szrj static void
df_chain_create_bb(unsigned int bb_index)2455*38fd1498Szrj df_chain_create_bb (unsigned int bb_index)
2456*38fd1498Szrj {
2457*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2458*38fd1498Szrj   struct df_rd_bb_info *bb_info = df_rd_get_bb_info (bb_index);
2459*38fd1498Szrj   rtx_insn *insn;
2460*38fd1498Szrj   bitmap_head cpy;
2461*38fd1498Szrj 
2462*38fd1498Szrj   bitmap_initialize (&cpy, &bitmap_default_obstack);
2463*38fd1498Szrj   bitmap_copy (&cpy, &bb_info->in);
2464*38fd1498Szrj   bitmap_set_bit (df_chain->out_of_date_transfer_functions, bb_index);
2465*38fd1498Szrj 
2466*38fd1498Szrj   /* Since we are going forwards, process the artificial uses first
2467*38fd1498Szrj      then the artificial defs second.  */
2468*38fd1498Szrj 
2469*38fd1498Szrj #ifdef EH_USES
2470*38fd1498Szrj   /* Create the chains for the artificial uses from the EH_USES at the
2471*38fd1498Szrj      beginning of the block.  */
2472*38fd1498Szrj 
2473*38fd1498Szrj   /* Artificials are only hard regs.  */
2474*38fd1498Szrj   if (!(df->changeable_flags & DF_NO_HARD_REGS))
2475*38fd1498Szrj     df_chain_create_bb_process_use (&cpy,
2476*38fd1498Szrj 				    df_get_artificial_uses (bb->index),
2477*38fd1498Szrj 				    DF_REF_AT_TOP);
2478*38fd1498Szrj #endif
2479*38fd1498Szrj 
2480*38fd1498Szrj   df_rd_simulate_artificial_defs_at_top (bb, &cpy);
2481*38fd1498Szrj 
2482*38fd1498Szrj   /* Process the regular instructions next.  */
2483*38fd1498Szrj   FOR_BB_INSNS (bb, insn)
2484*38fd1498Szrj     if (INSN_P (insn))
2485*38fd1498Szrj       {
2486*38fd1498Szrj         unsigned int uid = INSN_UID (insn);
2487*38fd1498Szrj 
2488*38fd1498Szrj         /* First scan the uses and link them up with the defs that remain
2489*38fd1498Szrj 	   in the cpy vector.  */
2490*38fd1498Szrj         df_chain_create_bb_process_use (&cpy, DF_INSN_UID_USES (uid), 0);
2491*38fd1498Szrj         if (df->changeable_flags & DF_EQ_NOTES)
2492*38fd1498Szrj 	  df_chain_create_bb_process_use (&cpy, DF_INSN_UID_EQ_USES (uid), 0);
2493*38fd1498Szrj 
2494*38fd1498Szrj         /* Since we are going forwards, process the defs second.  */
2495*38fd1498Szrj         df_rd_simulate_one_insn (bb, insn, &cpy);
2496*38fd1498Szrj       }
2497*38fd1498Szrj 
2498*38fd1498Szrj   /* Create the chains for the artificial uses of the hard registers
2499*38fd1498Szrj      at the end of the block.  */
2500*38fd1498Szrj   if (!(df->changeable_flags & DF_NO_HARD_REGS))
2501*38fd1498Szrj     df_chain_create_bb_process_use (&cpy,
2502*38fd1498Szrj 				    df_get_artificial_uses (bb->index),
2503*38fd1498Szrj 				    0);
2504*38fd1498Szrj 
2505*38fd1498Szrj   bitmap_clear (&cpy);
2506*38fd1498Szrj }
2507*38fd1498Szrj 
2508*38fd1498Szrj /* Create def-use chains from reaching use bitmaps for basic blocks
2509*38fd1498Szrj    in BLOCKS.  */
2510*38fd1498Szrj 
2511*38fd1498Szrj static void
df_chain_finalize(bitmap all_blocks)2512*38fd1498Szrj df_chain_finalize (bitmap all_blocks)
2513*38fd1498Szrj {
2514*38fd1498Szrj   unsigned int bb_index;
2515*38fd1498Szrj   bitmap_iterator bi;
2516*38fd1498Szrj 
2517*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2518*38fd1498Szrj     {
2519*38fd1498Szrj       df_chain_create_bb (bb_index);
2520*38fd1498Szrj     }
2521*38fd1498Szrj }
2522*38fd1498Szrj 
2523*38fd1498Szrj 
2524*38fd1498Szrj /* Free all storage associated with the problem.  */
2525*38fd1498Szrj 
2526*38fd1498Szrj static void
df_chain_free(void)2527*38fd1498Szrj df_chain_free (void)
2528*38fd1498Szrj {
2529*38fd1498Szrj   delete df_chain->block_pool;
2530*38fd1498Szrj   BITMAP_FREE (df_chain->out_of_date_transfer_functions);
2531*38fd1498Szrj   free (df_chain);
2532*38fd1498Szrj }
2533*38fd1498Szrj 
2534*38fd1498Szrj 
2535*38fd1498Szrj /* Debugging info.  */
2536*38fd1498Szrj 
2537*38fd1498Szrj static void
df_chain_bb_dump(basic_block bb,FILE * file,bool top)2538*38fd1498Szrj df_chain_bb_dump (basic_block bb, FILE *file, bool top)
2539*38fd1498Szrj {
2540*38fd1498Szrj   /* Artificials are only hard regs.  */
2541*38fd1498Szrj   if (df->changeable_flags & DF_NO_HARD_REGS)
2542*38fd1498Szrj     return;
2543*38fd1498Szrj   if (df_chain_problem_p (DF_UD_CHAIN))
2544*38fd1498Szrj     {
2545*38fd1498Szrj       df_ref use;
2546*38fd1498Szrj 
2547*38fd1498Szrj       fprintf (file,
2548*38fd1498Szrj 	       ";;  UD chains for artificial uses at %s\n",
2549*38fd1498Szrj 	       top ? "top" : "bottom");
2550*38fd1498Szrj       FOR_EACH_ARTIFICIAL_USE (use, bb->index)
2551*38fd1498Szrj 	if ((top && (DF_REF_FLAGS (use) & DF_REF_AT_TOP))
2552*38fd1498Szrj 	    || (!top && !(DF_REF_FLAGS (use) & DF_REF_AT_TOP)))
2553*38fd1498Szrj 	  {
2554*38fd1498Szrj 	    fprintf (file, ";;   reg %d ", DF_REF_REGNO (use));
2555*38fd1498Szrj 	    df_chain_dump (DF_REF_CHAIN (use), file);
2556*38fd1498Szrj 	    fprintf (file, "\n");
2557*38fd1498Szrj 	  }
2558*38fd1498Szrj     }
2559*38fd1498Szrj   if (df_chain_problem_p (DF_DU_CHAIN))
2560*38fd1498Szrj     {
2561*38fd1498Szrj       df_ref def;
2562*38fd1498Szrj 
2563*38fd1498Szrj       fprintf (file,
2564*38fd1498Szrj 	       ";;  DU chains for artificial defs at %s\n",
2565*38fd1498Szrj 	       top ? "top" : "bottom");
2566*38fd1498Szrj       FOR_EACH_ARTIFICIAL_DEF (def, bb->index)
2567*38fd1498Szrj 	if ((top && (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
2568*38fd1498Szrj 	    || (!top && !(DF_REF_FLAGS (def) & DF_REF_AT_TOP)))
2569*38fd1498Szrj 	  {
2570*38fd1498Szrj 	    fprintf (file, ";;   reg %d ", DF_REF_REGNO (def));
2571*38fd1498Szrj 	    df_chain_dump (DF_REF_CHAIN (def), file);
2572*38fd1498Szrj 	    fprintf (file, "\n");
2573*38fd1498Szrj 	  }
2574*38fd1498Szrj     }
2575*38fd1498Szrj }
2576*38fd1498Szrj 
2577*38fd1498Szrj static void
df_chain_top_dump(basic_block bb,FILE * file)2578*38fd1498Szrj df_chain_top_dump (basic_block bb, FILE *file)
2579*38fd1498Szrj {
2580*38fd1498Szrj   df_chain_bb_dump (bb, file, /*top=*/true);
2581*38fd1498Szrj }
2582*38fd1498Szrj 
2583*38fd1498Szrj static void
df_chain_bottom_dump(basic_block bb,FILE * file)2584*38fd1498Szrj df_chain_bottom_dump (basic_block bb, FILE *file)
2585*38fd1498Szrj {
2586*38fd1498Szrj   df_chain_bb_dump (bb, file, /*top=*/false);
2587*38fd1498Szrj }
2588*38fd1498Szrj 
2589*38fd1498Szrj static void
df_chain_insn_top_dump(const rtx_insn * insn,FILE * file)2590*38fd1498Szrj df_chain_insn_top_dump (const rtx_insn *insn, FILE *file)
2591*38fd1498Szrj {
2592*38fd1498Szrj   if (df_chain_problem_p (DF_UD_CHAIN) && INSN_P (insn))
2593*38fd1498Szrj     {
2594*38fd1498Szrj       struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2595*38fd1498Szrj       df_ref use;
2596*38fd1498Szrj 
2597*38fd1498Szrj       fprintf (file, ";;   UD chains for insn luid %d uid %d\n",
2598*38fd1498Szrj 	       DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2599*38fd1498Szrj       FOR_EACH_INSN_INFO_USE (use, insn_info)
2600*38fd1498Szrj 	if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2601*38fd1498Szrj 	    || !(df->changeable_flags & DF_NO_HARD_REGS))
2602*38fd1498Szrj 	  {
2603*38fd1498Szrj 	    fprintf (file, ";;      reg %d ", DF_REF_REGNO (use));
2604*38fd1498Szrj 	    if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
2605*38fd1498Szrj 	      fprintf (file, "read/write ");
2606*38fd1498Szrj 	    df_chain_dump (DF_REF_CHAIN (use), file);
2607*38fd1498Szrj 	    fprintf (file, "\n");
2608*38fd1498Szrj 	  }
2609*38fd1498Szrj       FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
2610*38fd1498Szrj 	if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (use))
2611*38fd1498Szrj 	    || !(df->changeable_flags & DF_NO_HARD_REGS))
2612*38fd1498Szrj 	  {
2613*38fd1498Szrj 	    fprintf (file, ";;   eq_note reg %d ", DF_REF_REGNO (use));
2614*38fd1498Szrj 	    df_chain_dump (DF_REF_CHAIN (use), file);
2615*38fd1498Szrj 	    fprintf (file, "\n");
2616*38fd1498Szrj 	  }
2617*38fd1498Szrj     }
2618*38fd1498Szrj }
2619*38fd1498Szrj 
2620*38fd1498Szrj static void
df_chain_insn_bottom_dump(const rtx_insn * insn,FILE * file)2621*38fd1498Szrj df_chain_insn_bottom_dump (const rtx_insn *insn, FILE *file)
2622*38fd1498Szrj {
2623*38fd1498Szrj   if (df_chain_problem_p (DF_DU_CHAIN) && INSN_P (insn))
2624*38fd1498Szrj     {
2625*38fd1498Szrj       struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2626*38fd1498Szrj       df_ref def;
2627*38fd1498Szrj       fprintf (file, ";;   DU chains for insn luid %d uid %d\n",
2628*38fd1498Szrj 	       DF_INSN_INFO_LUID (insn_info), INSN_UID (insn));
2629*38fd1498Szrj       FOR_EACH_INSN_INFO_DEF (def, insn_info)
2630*38fd1498Szrj 	if (!HARD_REGISTER_NUM_P (DF_REF_REGNO (def))
2631*38fd1498Szrj 	    || !(df->changeable_flags & DF_NO_HARD_REGS))
2632*38fd1498Szrj 	  {
2633*38fd1498Szrj 	    fprintf (file, ";;      reg %d ", DF_REF_REGNO (def));
2634*38fd1498Szrj 	    if (DF_REF_FLAGS (def) & DF_REF_READ_WRITE)
2635*38fd1498Szrj 	      fprintf (file, "read/write ");
2636*38fd1498Szrj 	    df_chain_dump (DF_REF_CHAIN (def), file);
2637*38fd1498Szrj 	    fprintf (file, "\n");
2638*38fd1498Szrj 	  }
2639*38fd1498Szrj       fprintf (file, "\n");
2640*38fd1498Szrj     }
2641*38fd1498Szrj }
2642*38fd1498Szrj 
2643*38fd1498Szrj static const struct df_problem problem_CHAIN =
2644*38fd1498Szrj {
2645*38fd1498Szrj   DF_CHAIN,                   /* Problem id.  */
2646*38fd1498Szrj   DF_NONE,                    /* Direction.  */
2647*38fd1498Szrj   df_chain_alloc,             /* Allocate the problem specific data.  */
2648*38fd1498Szrj   df_chain_reset,             /* Reset global information.  */
2649*38fd1498Szrj   NULL,                       /* Free basic block info.  */
2650*38fd1498Szrj   NULL,                       /* Local compute function.  */
2651*38fd1498Szrj   NULL,                       /* Init the solution specific data.  */
2652*38fd1498Szrj   NULL,                       /* Iterative solver.  */
2653*38fd1498Szrj   NULL,                       /* Confluence operator 0.  */
2654*38fd1498Szrj   NULL,                       /* Confluence operator n.  */
2655*38fd1498Szrj   NULL,                       /* Transfer function.  */
2656*38fd1498Szrj   df_chain_finalize,          /* Finalize function.  */
2657*38fd1498Szrj   df_chain_free,              /* Free all of the problem information.  */
2658*38fd1498Szrj   df_chain_fully_remove_problem,/* Remove this problem from the stack of dataflow problems.  */
2659*38fd1498Szrj   NULL,                       /* Debugging.  */
2660*38fd1498Szrj   df_chain_top_dump,          /* Debugging start block.  */
2661*38fd1498Szrj   df_chain_bottom_dump,       /* Debugging end block.  */
2662*38fd1498Szrj   df_chain_insn_top_dump,     /* Debugging start insn.  */
2663*38fd1498Szrj   df_chain_insn_bottom_dump,  /* Debugging end insn.  */
2664*38fd1498Szrj   NULL,                       /* Incremental solution verify start.  */
2665*38fd1498Szrj   NULL,                       /* Incremental solution verify end.  */
2666*38fd1498Szrj   &problem_RD,                /* Dependent problem.  */
2667*38fd1498Szrj   sizeof (struct df_scan_bb_info),/* Size of entry of block_info array.  */
2668*38fd1498Szrj   TV_DF_CHAIN,                /* Timing variable.  */
2669*38fd1498Szrj   false                       /* Reset blocks on dropping out of blocks_to_analyze.  */
2670*38fd1498Szrj };
2671*38fd1498Szrj 
2672*38fd1498Szrj 
2673*38fd1498Szrj /* Create a new DATAFLOW instance and add it to an existing instance
2674*38fd1498Szrj    of DF.  The returned structure is what is used to get at the
2675*38fd1498Szrj    solution.  */
2676*38fd1498Szrj 
2677*38fd1498Szrj void
df_chain_add_problem(unsigned int chain_flags)2678*38fd1498Szrj df_chain_add_problem (unsigned int chain_flags)
2679*38fd1498Szrj {
2680*38fd1498Szrj   df_add_problem (&problem_CHAIN);
2681*38fd1498Szrj   df_chain->local_flags = chain_flags;
2682*38fd1498Szrj   df_chain->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
2683*38fd1498Szrj }
2684*38fd1498Szrj 
2685*38fd1498Szrj #undef df_chain_problem_p
2686*38fd1498Szrj 
2687*38fd1498Szrj 
2688*38fd1498Szrj /*----------------------------------------------------------------------------
2689*38fd1498Szrj    WORD LEVEL LIVE REGISTERS
2690*38fd1498Szrj 
2691*38fd1498Szrj    Find the locations in the function where any use of a pseudo can
2692*38fd1498Szrj    reach in the backwards direction.  In and out bitvectors are built
2693*38fd1498Szrj    for each basic block.  We only track pseudo registers that have a
2694*38fd1498Szrj    size of 2 * UNITS_PER_WORD; bitmaps are indexed by 2 * regno and
2695*38fd1498Szrj    contain two bits corresponding to each of the subwords.
2696*38fd1498Szrj 
2697*38fd1498Szrj    ----------------------------------------------------------------------------*/
2698*38fd1498Szrj 
2699*38fd1498Szrj /* Private data used to verify the solution for this problem.  */
2700*38fd1498Szrj struct df_word_lr_problem_data
2701*38fd1498Szrj {
2702*38fd1498Szrj   /* An obstack for the bitmaps we need for this problem.  */
2703*38fd1498Szrj   bitmap_obstack word_lr_bitmaps;
2704*38fd1498Szrj };
2705*38fd1498Szrj 
2706*38fd1498Szrj 
2707*38fd1498Szrj /* Free basic block info.  */
2708*38fd1498Szrj 
2709*38fd1498Szrj static void
df_word_lr_free_bb_info(basic_block bb ATTRIBUTE_UNUSED,void * vbb_info)2710*38fd1498Szrj df_word_lr_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
2711*38fd1498Szrj 			 void *vbb_info)
2712*38fd1498Szrj {
2713*38fd1498Szrj   struct df_word_lr_bb_info *bb_info = (struct df_word_lr_bb_info *) vbb_info;
2714*38fd1498Szrj   if (bb_info)
2715*38fd1498Szrj     {
2716*38fd1498Szrj       bitmap_clear (&bb_info->use);
2717*38fd1498Szrj       bitmap_clear (&bb_info->def);
2718*38fd1498Szrj       bitmap_clear (&bb_info->in);
2719*38fd1498Szrj       bitmap_clear (&bb_info->out);
2720*38fd1498Szrj     }
2721*38fd1498Szrj }
2722*38fd1498Szrj 
2723*38fd1498Szrj 
2724*38fd1498Szrj /* Allocate or reset bitmaps for DF_WORD_LR blocks. The solution bits are
2725*38fd1498Szrj    not touched unless the block is new.  */
2726*38fd1498Szrj 
2727*38fd1498Szrj static void
df_word_lr_alloc(bitmap all_blocks ATTRIBUTE_UNUSED)2728*38fd1498Szrj df_word_lr_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
2729*38fd1498Szrj {
2730*38fd1498Szrj   unsigned int bb_index;
2731*38fd1498Szrj   bitmap_iterator bi;
2732*38fd1498Szrj   basic_block bb;
2733*38fd1498Szrj   struct df_word_lr_problem_data *problem_data
2734*38fd1498Szrj     = XNEW (struct df_word_lr_problem_data);
2735*38fd1498Szrj 
2736*38fd1498Szrj   df_word_lr->problem_data = problem_data;
2737*38fd1498Szrj 
2738*38fd1498Szrj   df_grow_bb_info (df_word_lr);
2739*38fd1498Szrj 
2740*38fd1498Szrj   /* Create the mapping from regnos to slots. This does not change
2741*38fd1498Szrj      unless the problem is destroyed and recreated.  In particular, if
2742*38fd1498Szrj      we end up deleting the only insn that used a subreg, we do not
2743*38fd1498Szrj      want to redo the mapping because this would invalidate everything
2744*38fd1498Szrj      else.  */
2745*38fd1498Szrj 
2746*38fd1498Szrj   bitmap_obstack_initialize (&problem_data->word_lr_bitmaps);
2747*38fd1498Szrj 
2748*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
2749*38fd1498Szrj     bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, bb->index);
2750*38fd1498Szrj 
2751*38fd1498Szrj   bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, ENTRY_BLOCK);
2752*38fd1498Szrj   bitmap_set_bit (df_word_lr->out_of_date_transfer_functions, EXIT_BLOCK);
2753*38fd1498Szrj 
2754*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2755*38fd1498Szrj     {
2756*38fd1498Szrj       struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2757*38fd1498Szrj 
2758*38fd1498Szrj       /* When bitmaps are already initialized, just clear them.  */
2759*38fd1498Szrj       if (bb_info->use.obstack)
2760*38fd1498Szrj 	{
2761*38fd1498Szrj 	  bitmap_clear (&bb_info->def);
2762*38fd1498Szrj 	  bitmap_clear (&bb_info->use);
2763*38fd1498Szrj 	}
2764*38fd1498Szrj       else
2765*38fd1498Szrj 	{
2766*38fd1498Szrj 	  bitmap_initialize (&bb_info->use, &problem_data->word_lr_bitmaps);
2767*38fd1498Szrj 	  bitmap_initialize (&bb_info->def, &problem_data->word_lr_bitmaps);
2768*38fd1498Szrj 	  bitmap_initialize (&bb_info->in, &problem_data->word_lr_bitmaps);
2769*38fd1498Szrj 	  bitmap_initialize (&bb_info->out, &problem_data->word_lr_bitmaps);
2770*38fd1498Szrj 	}
2771*38fd1498Szrj     }
2772*38fd1498Szrj 
2773*38fd1498Szrj   df_word_lr->optional_p = true;
2774*38fd1498Szrj }
2775*38fd1498Szrj 
2776*38fd1498Szrj 
2777*38fd1498Szrj /* Reset the global solution for recalculation.  */
2778*38fd1498Szrj 
2779*38fd1498Szrj static void
df_word_lr_reset(bitmap all_blocks)2780*38fd1498Szrj df_word_lr_reset (bitmap all_blocks)
2781*38fd1498Szrj {
2782*38fd1498Szrj   unsigned int bb_index;
2783*38fd1498Szrj   bitmap_iterator bi;
2784*38fd1498Szrj 
2785*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2786*38fd1498Szrj     {
2787*38fd1498Szrj       struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2788*38fd1498Szrj       gcc_assert (bb_info);
2789*38fd1498Szrj       bitmap_clear (&bb_info->in);
2790*38fd1498Szrj       bitmap_clear (&bb_info->out);
2791*38fd1498Szrj     }
2792*38fd1498Szrj }
2793*38fd1498Szrj 
2794*38fd1498Szrj /* Examine REF, and if it is for a reg we're interested in, set or
2795*38fd1498Szrj    clear the bits corresponding to its subwords from the bitmap
2796*38fd1498Szrj    according to IS_SET.  LIVE is the bitmap we should update.  We do
2797*38fd1498Szrj    not track hard regs or pseudos of any size other than 2 *
2798*38fd1498Szrj    UNITS_PER_WORD.
2799*38fd1498Szrj    We return true if we changed the bitmap, or if we encountered a register
2800*38fd1498Szrj    we're not tracking.  */
2801*38fd1498Szrj 
2802*38fd1498Szrj bool
df_word_lr_mark_ref(df_ref ref,bool is_set,regset live)2803*38fd1498Szrj df_word_lr_mark_ref (df_ref ref, bool is_set, regset live)
2804*38fd1498Szrj {
2805*38fd1498Szrj   rtx orig_reg = DF_REF_REG (ref);
2806*38fd1498Szrj   rtx reg = orig_reg;
2807*38fd1498Szrj   machine_mode reg_mode;
2808*38fd1498Szrj   unsigned regno;
2809*38fd1498Szrj   /* Left at -1 for whole accesses.  */
2810*38fd1498Szrj   int which_subword = -1;
2811*38fd1498Szrj   bool changed = false;
2812*38fd1498Szrj 
2813*38fd1498Szrj   if (GET_CODE (reg) == SUBREG)
2814*38fd1498Szrj     reg = SUBREG_REG (orig_reg);
2815*38fd1498Szrj   regno = REGNO (reg);
2816*38fd1498Szrj   reg_mode = GET_MODE (reg);
2817*38fd1498Szrj   if (regno < FIRST_PSEUDO_REGISTER
2818*38fd1498Szrj       || maybe_ne (GET_MODE_SIZE (reg_mode), 2 * UNITS_PER_WORD))
2819*38fd1498Szrj     return true;
2820*38fd1498Szrj 
2821*38fd1498Szrj   if (GET_CODE (orig_reg) == SUBREG
2822*38fd1498Szrj       && read_modify_subreg_p (orig_reg))
2823*38fd1498Szrj     {
2824*38fd1498Szrj       gcc_assert (DF_REF_FLAGS_IS_SET (ref, DF_REF_PARTIAL));
2825*38fd1498Szrj       if (subreg_lowpart_p (orig_reg))
2826*38fd1498Szrj 	which_subword = 0;
2827*38fd1498Szrj       else
2828*38fd1498Szrj 	which_subword = 1;
2829*38fd1498Szrj     }
2830*38fd1498Szrj   if (is_set)
2831*38fd1498Szrj     {
2832*38fd1498Szrj       if (which_subword != 1)
2833*38fd1498Szrj 	changed |= bitmap_set_bit (live, regno * 2);
2834*38fd1498Szrj       if (which_subword != 0)
2835*38fd1498Szrj 	changed |= bitmap_set_bit (live, regno * 2 + 1);
2836*38fd1498Szrj     }
2837*38fd1498Szrj   else
2838*38fd1498Szrj     {
2839*38fd1498Szrj       if (which_subword != 1)
2840*38fd1498Szrj 	changed |= bitmap_clear_bit (live, regno * 2);
2841*38fd1498Szrj       if (which_subword != 0)
2842*38fd1498Szrj 	changed |= bitmap_clear_bit (live, regno * 2 + 1);
2843*38fd1498Szrj     }
2844*38fd1498Szrj   return changed;
2845*38fd1498Szrj }
2846*38fd1498Szrj 
2847*38fd1498Szrj /* Compute local live register info for basic block BB.  */
2848*38fd1498Szrj 
2849*38fd1498Szrj static void
df_word_lr_bb_local_compute(unsigned int bb_index)2850*38fd1498Szrj df_word_lr_bb_local_compute (unsigned int bb_index)
2851*38fd1498Szrj {
2852*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
2853*38fd1498Szrj   struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2854*38fd1498Szrj   rtx_insn *insn;
2855*38fd1498Szrj   df_ref def, use;
2856*38fd1498Szrj 
2857*38fd1498Szrj   /* Ensure that artificial refs don't contain references to pseudos.  */
2858*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
2859*38fd1498Szrj     gcc_assert (DF_REF_REGNO (def) < FIRST_PSEUDO_REGISTER);
2860*38fd1498Szrj 
2861*38fd1498Szrj   FOR_EACH_ARTIFICIAL_USE (use, bb_index)
2862*38fd1498Szrj     gcc_assert (DF_REF_REGNO (use) < FIRST_PSEUDO_REGISTER);
2863*38fd1498Szrj 
2864*38fd1498Szrj   FOR_BB_INSNS_REVERSE (bb, insn)
2865*38fd1498Szrj     {
2866*38fd1498Szrj       if (!NONDEBUG_INSN_P (insn))
2867*38fd1498Szrj 	continue;
2868*38fd1498Szrj 
2869*38fd1498Szrj       df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
2870*38fd1498Szrj       FOR_EACH_INSN_INFO_DEF (def, insn_info)
2871*38fd1498Szrj 	/* If the def is to only part of the reg, it does
2872*38fd1498Szrj 	   not kill the other defs that reach here.  */
2873*38fd1498Szrj 	if (!(DF_REF_FLAGS (def) & (DF_REF_CONDITIONAL)))
2874*38fd1498Szrj 	  {
2875*38fd1498Szrj 	    df_word_lr_mark_ref (def, true, &bb_info->def);
2876*38fd1498Szrj 	    df_word_lr_mark_ref (def, false, &bb_info->use);
2877*38fd1498Szrj 	  }
2878*38fd1498Szrj       FOR_EACH_INSN_INFO_USE (use, insn_info)
2879*38fd1498Szrj 	df_word_lr_mark_ref (use, true, &bb_info->use);
2880*38fd1498Szrj     }
2881*38fd1498Szrj }
2882*38fd1498Szrj 
2883*38fd1498Szrj 
2884*38fd1498Szrj /* Compute local live register info for each basic block within BLOCKS.  */
2885*38fd1498Szrj 
2886*38fd1498Szrj static void
df_word_lr_local_compute(bitmap all_blocks ATTRIBUTE_UNUSED)2887*38fd1498Szrj df_word_lr_local_compute (bitmap all_blocks ATTRIBUTE_UNUSED)
2888*38fd1498Szrj {
2889*38fd1498Szrj   unsigned int bb_index;
2890*38fd1498Szrj   bitmap_iterator bi;
2891*38fd1498Szrj 
2892*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (df_word_lr->out_of_date_transfer_functions, 0, bb_index, bi)
2893*38fd1498Szrj     {
2894*38fd1498Szrj       if (bb_index == EXIT_BLOCK)
2895*38fd1498Szrj 	{
2896*38fd1498Szrj 	  unsigned regno;
2897*38fd1498Szrj 	  bitmap_iterator bi;
2898*38fd1498Szrj 	  EXECUTE_IF_SET_IN_BITMAP (df->exit_block_uses, FIRST_PSEUDO_REGISTER,
2899*38fd1498Szrj 				    regno, bi)
2900*38fd1498Szrj 	    gcc_unreachable ();
2901*38fd1498Szrj 	}
2902*38fd1498Szrj       else
2903*38fd1498Szrj 	df_word_lr_bb_local_compute (bb_index);
2904*38fd1498Szrj     }
2905*38fd1498Szrj 
2906*38fd1498Szrj   bitmap_clear (df_word_lr->out_of_date_transfer_functions);
2907*38fd1498Szrj }
2908*38fd1498Szrj 
2909*38fd1498Szrj 
2910*38fd1498Szrj /* Initialize the solution vectors.  */
2911*38fd1498Szrj 
2912*38fd1498Szrj static void
df_word_lr_init(bitmap all_blocks)2913*38fd1498Szrj df_word_lr_init (bitmap all_blocks)
2914*38fd1498Szrj {
2915*38fd1498Szrj   unsigned int bb_index;
2916*38fd1498Szrj   bitmap_iterator bi;
2917*38fd1498Szrj 
2918*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
2919*38fd1498Szrj     {
2920*38fd1498Szrj       struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2921*38fd1498Szrj       bitmap_copy (&bb_info->in, &bb_info->use);
2922*38fd1498Szrj       bitmap_clear (&bb_info->out);
2923*38fd1498Szrj     }
2924*38fd1498Szrj }
2925*38fd1498Szrj 
2926*38fd1498Szrj 
2927*38fd1498Szrj /* Confluence function that ignores fake edges.  */
2928*38fd1498Szrj 
2929*38fd1498Szrj static bool
df_word_lr_confluence_n(edge e)2930*38fd1498Szrj df_word_lr_confluence_n (edge e)
2931*38fd1498Szrj {
2932*38fd1498Szrj   bitmap op1 = &df_word_lr_get_bb_info (e->src->index)->out;
2933*38fd1498Szrj   bitmap op2 = &df_word_lr_get_bb_info (e->dest->index)->in;
2934*38fd1498Szrj 
2935*38fd1498Szrj   return bitmap_ior_into (op1, op2);
2936*38fd1498Szrj }
2937*38fd1498Szrj 
2938*38fd1498Szrj 
2939*38fd1498Szrj /* Transfer function.  */
2940*38fd1498Szrj 
2941*38fd1498Szrj static bool
df_word_lr_transfer_function(int bb_index)2942*38fd1498Szrj df_word_lr_transfer_function (int bb_index)
2943*38fd1498Szrj {
2944*38fd1498Szrj   struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb_index);
2945*38fd1498Szrj   bitmap in = &bb_info->in;
2946*38fd1498Szrj   bitmap out = &bb_info->out;
2947*38fd1498Szrj   bitmap use = &bb_info->use;
2948*38fd1498Szrj   bitmap def = &bb_info->def;
2949*38fd1498Szrj 
2950*38fd1498Szrj   return bitmap_ior_and_compl (in, use, out, def);
2951*38fd1498Szrj }
2952*38fd1498Szrj 
2953*38fd1498Szrj 
2954*38fd1498Szrj /* Free all storage associated with the problem.  */
2955*38fd1498Szrj 
2956*38fd1498Szrj static void
df_word_lr_free(void)2957*38fd1498Szrj df_word_lr_free (void)
2958*38fd1498Szrj {
2959*38fd1498Szrj   struct df_word_lr_problem_data *problem_data
2960*38fd1498Szrj     = (struct df_word_lr_problem_data *)df_word_lr->problem_data;
2961*38fd1498Szrj 
2962*38fd1498Szrj   if (df_word_lr->block_info)
2963*38fd1498Szrj     {
2964*38fd1498Szrj       df_word_lr->block_info_size = 0;
2965*38fd1498Szrj       free (df_word_lr->block_info);
2966*38fd1498Szrj       df_word_lr->block_info = NULL;
2967*38fd1498Szrj     }
2968*38fd1498Szrj 
2969*38fd1498Szrj   BITMAP_FREE (df_word_lr->out_of_date_transfer_functions);
2970*38fd1498Szrj   bitmap_obstack_release (&problem_data->word_lr_bitmaps);
2971*38fd1498Szrj   free (problem_data);
2972*38fd1498Szrj   free (df_word_lr);
2973*38fd1498Szrj }
2974*38fd1498Szrj 
2975*38fd1498Szrj 
2976*38fd1498Szrj /* Debugging info at top of bb.  */
2977*38fd1498Szrj 
2978*38fd1498Szrj static void
df_word_lr_top_dump(basic_block bb,FILE * file)2979*38fd1498Szrj df_word_lr_top_dump (basic_block bb, FILE *file)
2980*38fd1498Szrj {
2981*38fd1498Szrj   struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
2982*38fd1498Szrj   if (!bb_info)
2983*38fd1498Szrj     return;
2984*38fd1498Szrj 
2985*38fd1498Szrj   fprintf (file, ";; blr  in  \t");
2986*38fd1498Szrj   df_print_word_regset (file, &bb_info->in);
2987*38fd1498Szrj   fprintf (file, ";; blr  use \t");
2988*38fd1498Szrj   df_print_word_regset (file, &bb_info->use);
2989*38fd1498Szrj   fprintf (file, ";; blr  def \t");
2990*38fd1498Szrj   df_print_word_regset (file, &bb_info->def);
2991*38fd1498Szrj }
2992*38fd1498Szrj 
2993*38fd1498Szrj 
2994*38fd1498Szrj /* Debugging info at bottom of bb.  */
2995*38fd1498Szrj 
2996*38fd1498Szrj static void
df_word_lr_bottom_dump(basic_block bb,FILE * file)2997*38fd1498Szrj df_word_lr_bottom_dump (basic_block bb, FILE *file)
2998*38fd1498Szrj {
2999*38fd1498Szrj   struct df_word_lr_bb_info *bb_info = df_word_lr_get_bb_info (bb->index);
3000*38fd1498Szrj   if (!bb_info)
3001*38fd1498Szrj     return;
3002*38fd1498Szrj 
3003*38fd1498Szrj   fprintf (file, ";; blr  out \t");
3004*38fd1498Szrj   df_print_word_regset (file, &bb_info->out);
3005*38fd1498Szrj }
3006*38fd1498Szrj 
3007*38fd1498Szrj 
3008*38fd1498Szrj /* All of the information associated with every instance of the problem.  */
3009*38fd1498Szrj 
3010*38fd1498Szrj static const struct df_problem problem_WORD_LR =
3011*38fd1498Szrj {
3012*38fd1498Szrj   DF_WORD_LR,                      /* Problem id.  */
3013*38fd1498Szrj   DF_BACKWARD,                     /* Direction.  */
3014*38fd1498Szrj   df_word_lr_alloc,                /* Allocate the problem specific data.  */
3015*38fd1498Szrj   df_word_lr_reset,                /* Reset global information.  */
3016*38fd1498Szrj   df_word_lr_free_bb_info,         /* Free basic block info.  */
3017*38fd1498Szrj   df_word_lr_local_compute,        /* Local compute function.  */
3018*38fd1498Szrj   df_word_lr_init,                 /* Init the solution specific data.  */
3019*38fd1498Szrj   df_worklist_dataflow,            /* Worklist solver.  */
3020*38fd1498Szrj   NULL,                            /* Confluence operator 0.  */
3021*38fd1498Szrj   df_word_lr_confluence_n,         /* Confluence operator n.  */
3022*38fd1498Szrj   df_word_lr_transfer_function,    /* Transfer function.  */
3023*38fd1498Szrj   NULL,                            /* Finalize function.  */
3024*38fd1498Szrj   df_word_lr_free,                 /* Free all of the problem information.  */
3025*38fd1498Szrj   df_word_lr_free,                 /* Remove this problem from the stack of dataflow problems.  */
3026*38fd1498Szrj   NULL,                            /* Debugging.  */
3027*38fd1498Szrj   df_word_lr_top_dump,             /* Debugging start block.  */
3028*38fd1498Szrj   df_word_lr_bottom_dump,          /* Debugging end block.  */
3029*38fd1498Szrj   NULL,                            /* Debugging start insn.  */
3030*38fd1498Szrj   NULL,                            /* Debugging end insn.  */
3031*38fd1498Szrj   NULL,                            /* Incremental solution verify start.  */
3032*38fd1498Szrj   NULL,                            /* Incremental solution verify end.  */
3033*38fd1498Szrj   NULL,                            /* Dependent problem.  */
3034*38fd1498Szrj   sizeof (struct df_word_lr_bb_info),/* Size of entry of block_info array.  */
3035*38fd1498Szrj   TV_DF_WORD_LR,                   /* Timing variable.  */
3036*38fd1498Szrj   false                            /* Reset blocks on dropping out of blocks_to_analyze.  */
3037*38fd1498Szrj };
3038*38fd1498Szrj 
3039*38fd1498Szrj 
3040*38fd1498Szrj /* Create a new DATAFLOW instance and add it to an existing instance
3041*38fd1498Szrj    of DF.  The returned structure is what is used to get at the
3042*38fd1498Szrj    solution.  */
3043*38fd1498Szrj 
3044*38fd1498Szrj void
df_word_lr_add_problem(void)3045*38fd1498Szrj df_word_lr_add_problem (void)
3046*38fd1498Szrj {
3047*38fd1498Szrj   df_add_problem (&problem_WORD_LR);
3048*38fd1498Szrj   /* These will be initialized when df_scan_blocks processes each
3049*38fd1498Szrj      block.  */
3050*38fd1498Szrj   df_word_lr->out_of_date_transfer_functions = BITMAP_ALLOC (&df_bitmap_obstack);
3051*38fd1498Szrj }
3052*38fd1498Szrj 
3053*38fd1498Szrj 
3054*38fd1498Szrj /* Simulate the effects of the defs of INSN on LIVE.  Return true if we changed
3055*38fd1498Szrj    any bits, which is used by the caller to determine whether a set is
3056*38fd1498Szrj    necessary.  We also return true if there are other reasons not to delete
3057*38fd1498Szrj    an insn.  */
3058*38fd1498Szrj 
3059*38fd1498Szrj bool
df_word_lr_simulate_defs(rtx_insn * insn,bitmap live)3060*38fd1498Szrj df_word_lr_simulate_defs (rtx_insn *insn, bitmap live)
3061*38fd1498Szrj {
3062*38fd1498Szrj   bool changed = false;
3063*38fd1498Szrj   df_ref def;
3064*38fd1498Szrj 
3065*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
3066*38fd1498Szrj     if (DF_REF_FLAGS (def) & DF_REF_CONDITIONAL)
3067*38fd1498Szrj       changed = true;
3068*38fd1498Szrj     else
3069*38fd1498Szrj       changed |= df_word_lr_mark_ref (def, false, live);
3070*38fd1498Szrj   return changed;
3071*38fd1498Szrj }
3072*38fd1498Szrj 
3073*38fd1498Szrj 
3074*38fd1498Szrj /* Simulate the effects of the uses of INSN on LIVE.  */
3075*38fd1498Szrj 
3076*38fd1498Szrj void
df_word_lr_simulate_uses(rtx_insn * insn,bitmap live)3077*38fd1498Szrj df_word_lr_simulate_uses (rtx_insn *insn, bitmap live)
3078*38fd1498Szrj {
3079*38fd1498Szrj   df_ref use;
3080*38fd1498Szrj 
3081*38fd1498Szrj   FOR_EACH_INSN_USE (use, insn)
3082*38fd1498Szrj     df_word_lr_mark_ref (use, true, live);
3083*38fd1498Szrj }
3084*38fd1498Szrj 
3085*38fd1498Szrj /*----------------------------------------------------------------------------
3086*38fd1498Szrj    This problem computes REG_DEAD and REG_UNUSED notes.
3087*38fd1498Szrj    ----------------------------------------------------------------------------*/
3088*38fd1498Szrj 
3089*38fd1498Szrj static void
df_note_alloc(bitmap all_blocks ATTRIBUTE_UNUSED)3090*38fd1498Szrj df_note_alloc (bitmap all_blocks ATTRIBUTE_UNUSED)
3091*38fd1498Szrj {
3092*38fd1498Szrj   df_note->optional_p = true;
3093*38fd1498Szrj }
3094*38fd1498Szrj 
3095*38fd1498Szrj /* This is only used if REG_DEAD_DEBUGGING is in effect.  */
3096*38fd1498Szrj static void
df_print_note(const char * prefix,rtx_insn * insn,rtx note)3097*38fd1498Szrj df_print_note (const char *prefix, rtx_insn *insn, rtx note)
3098*38fd1498Szrj {
3099*38fd1498Szrj   if (dump_file)
3100*38fd1498Szrj     {
3101*38fd1498Szrj       fprintf (dump_file, "%s %d ", prefix, INSN_UID (insn));
3102*38fd1498Szrj       print_rtl (dump_file, note);
3103*38fd1498Szrj       fprintf (dump_file, "\n");
3104*38fd1498Szrj     }
3105*38fd1498Szrj }
3106*38fd1498Szrj 
3107*38fd1498Szrj 
3108*38fd1498Szrj /* After reg-stack, the x86 floating point stack regs are difficult to
3109*38fd1498Szrj    analyze because of all of the pushes, pops and rotations.  Thus, we
3110*38fd1498Szrj    just leave the notes alone. */
3111*38fd1498Szrj 
3112*38fd1498Szrj #ifdef STACK_REGS
3113*38fd1498Szrj static inline bool
df_ignore_stack_reg(int regno)3114*38fd1498Szrj df_ignore_stack_reg (int regno)
3115*38fd1498Szrj {
3116*38fd1498Szrj   return regstack_completed
3117*38fd1498Szrj     && IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG);
3118*38fd1498Szrj }
3119*38fd1498Szrj #else
3120*38fd1498Szrj static inline bool
df_ignore_stack_reg(int regno ATTRIBUTE_UNUSED)3121*38fd1498Szrj df_ignore_stack_reg (int regno ATTRIBUTE_UNUSED)
3122*38fd1498Szrj {
3123*38fd1498Szrj   return false;
3124*38fd1498Szrj }
3125*38fd1498Szrj #endif
3126*38fd1498Szrj 
3127*38fd1498Szrj 
3128*38fd1498Szrj /* Remove all of the REG_DEAD or REG_UNUSED notes from INSN.  */
3129*38fd1498Szrj 
3130*38fd1498Szrj static void
df_remove_dead_and_unused_notes(rtx_insn * insn)3131*38fd1498Szrj df_remove_dead_and_unused_notes (rtx_insn *insn)
3132*38fd1498Szrj {
3133*38fd1498Szrj   rtx *pprev = &REG_NOTES (insn);
3134*38fd1498Szrj   rtx link = *pprev;
3135*38fd1498Szrj 
3136*38fd1498Szrj   while (link)
3137*38fd1498Szrj     {
3138*38fd1498Szrj       switch (REG_NOTE_KIND (link))
3139*38fd1498Szrj 	{
3140*38fd1498Szrj 	case REG_DEAD:
3141*38fd1498Szrj 	  /* After reg-stack, we need to ignore any unused notes
3142*38fd1498Szrj 	     for the stack registers.  */
3143*38fd1498Szrj 	  if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3144*38fd1498Szrj 	    {
3145*38fd1498Szrj 	      pprev = &XEXP (link, 1);
3146*38fd1498Szrj 	      link = *pprev;
3147*38fd1498Szrj 	    }
3148*38fd1498Szrj 	  else
3149*38fd1498Szrj 	    {
3150*38fd1498Szrj 	      rtx next = XEXP (link, 1);
3151*38fd1498Szrj 	      if (REG_DEAD_DEBUGGING)
3152*38fd1498Szrj 		df_print_note ("deleting: ", insn, link);
3153*38fd1498Szrj 	      free_EXPR_LIST_node (link);
3154*38fd1498Szrj 	      *pprev = link = next;
3155*38fd1498Szrj 	    }
3156*38fd1498Szrj 	  break;
3157*38fd1498Szrj 
3158*38fd1498Szrj 	case REG_UNUSED:
3159*38fd1498Szrj 	  /* After reg-stack, we need to ignore any unused notes
3160*38fd1498Szrj 	     for the stack registers.  */
3161*38fd1498Szrj 	  if (df_ignore_stack_reg (REGNO (XEXP (link, 0))))
3162*38fd1498Szrj 	    {
3163*38fd1498Szrj 	      pprev = &XEXP (link, 1);
3164*38fd1498Szrj 	      link = *pprev;
3165*38fd1498Szrj 	    }
3166*38fd1498Szrj 	  else
3167*38fd1498Szrj 	    {
3168*38fd1498Szrj 	      rtx next = XEXP (link, 1);
3169*38fd1498Szrj 	      if (REG_DEAD_DEBUGGING)
3170*38fd1498Szrj 		df_print_note ("deleting: ", insn, link);
3171*38fd1498Szrj 	      free_EXPR_LIST_node (link);
3172*38fd1498Szrj 	      *pprev = link = next;
3173*38fd1498Szrj 	    }
3174*38fd1498Szrj 	  break;
3175*38fd1498Szrj 
3176*38fd1498Szrj 	default:
3177*38fd1498Szrj 	  pprev = &XEXP (link, 1);
3178*38fd1498Szrj 	  link = *pprev;
3179*38fd1498Szrj 	  break;
3180*38fd1498Szrj 	}
3181*38fd1498Szrj     }
3182*38fd1498Szrj }
3183*38fd1498Szrj 
3184*38fd1498Szrj /* Remove REG_EQUAL/REG_EQUIV notes referring to dead pseudos using LIVE
3185*38fd1498Szrj    as the bitmap of currently live registers.  */
3186*38fd1498Szrj 
3187*38fd1498Szrj static void
df_remove_dead_eq_notes(rtx_insn * insn,bitmap live)3188*38fd1498Szrj df_remove_dead_eq_notes (rtx_insn *insn, bitmap live)
3189*38fd1498Szrj {
3190*38fd1498Szrj   rtx *pprev = &REG_NOTES (insn);
3191*38fd1498Szrj   rtx link = *pprev;
3192*38fd1498Szrj 
3193*38fd1498Szrj   while (link)
3194*38fd1498Szrj     {
3195*38fd1498Szrj       switch (REG_NOTE_KIND (link))
3196*38fd1498Szrj 	{
3197*38fd1498Szrj 	case REG_EQUAL:
3198*38fd1498Szrj 	case REG_EQUIV:
3199*38fd1498Szrj 	  {
3200*38fd1498Szrj 	    /* Remove the notes that refer to dead registers.  As we have at most
3201*38fd1498Szrj 	       one REG_EQUAL/EQUIV note, all of EQ_USES will refer to this note
3202*38fd1498Szrj 	       so we need to purge the complete EQ_USES vector when removing
3203*38fd1498Szrj 	       the note using df_notes_rescan.  */
3204*38fd1498Szrj 	    df_ref use;
3205*38fd1498Szrj 	    bool deleted = false;
3206*38fd1498Szrj 
3207*38fd1498Szrj 	    FOR_EACH_INSN_EQ_USE (use, insn)
3208*38fd1498Szrj 	      if (DF_REF_REGNO (use) > FIRST_PSEUDO_REGISTER
3209*38fd1498Szrj 		  && DF_REF_LOC (use)
3210*38fd1498Szrj 		  && (DF_REF_FLAGS (use) & DF_REF_IN_NOTE)
3211*38fd1498Szrj 		  && !bitmap_bit_p (live, DF_REF_REGNO (use))
3212*38fd1498Szrj 		  && loc_mentioned_in_p (DF_REF_LOC (use), XEXP (link, 0)))
3213*38fd1498Szrj 		{
3214*38fd1498Szrj 		  deleted = true;
3215*38fd1498Szrj 		  break;
3216*38fd1498Szrj 		}
3217*38fd1498Szrj 	    if (deleted)
3218*38fd1498Szrj 	      {
3219*38fd1498Szrj 		rtx next;
3220*38fd1498Szrj 		if (REG_DEAD_DEBUGGING)
3221*38fd1498Szrj 		  df_print_note ("deleting: ", insn, link);
3222*38fd1498Szrj 		next = XEXP (link, 1);
3223*38fd1498Szrj 		free_EXPR_LIST_node (link);
3224*38fd1498Szrj 		*pprev = link = next;
3225*38fd1498Szrj 		df_notes_rescan (insn);
3226*38fd1498Szrj 	      }
3227*38fd1498Szrj 	    else
3228*38fd1498Szrj 	      {
3229*38fd1498Szrj 		pprev = &XEXP (link, 1);
3230*38fd1498Szrj 		link = *pprev;
3231*38fd1498Szrj 	      }
3232*38fd1498Szrj 	    break;
3233*38fd1498Szrj 	  }
3234*38fd1498Szrj 
3235*38fd1498Szrj 	default:
3236*38fd1498Szrj 	  pprev = &XEXP (link, 1);
3237*38fd1498Szrj 	  link = *pprev;
3238*38fd1498Szrj 	  break;
3239*38fd1498Szrj 	}
3240*38fd1498Szrj     }
3241*38fd1498Szrj }
3242*38fd1498Szrj 
3243*38fd1498Szrj /* Set a NOTE_TYPE note for REG in INSN.  */
3244*38fd1498Szrj 
3245*38fd1498Szrj static inline void
df_set_note(enum reg_note note_type,rtx_insn * insn,rtx reg)3246*38fd1498Szrj df_set_note (enum reg_note note_type, rtx_insn *insn, rtx reg)
3247*38fd1498Szrj {
3248*38fd1498Szrj   gcc_checking_assert (!DEBUG_INSN_P (insn));
3249*38fd1498Szrj   add_reg_note (insn, note_type, reg);
3250*38fd1498Szrj }
3251*38fd1498Szrj 
3252*38fd1498Szrj /* A subroutine of df_set_unused_notes_for_mw, with a selection of its
3253*38fd1498Szrj    arguments.  Return true if the register value described by MWS's
3254*38fd1498Szrj    mw_reg is known to be completely unused, and if mw_reg can therefore
3255*38fd1498Szrj    be used in a REG_UNUSED note.  */
3256*38fd1498Szrj 
3257*38fd1498Szrj static bool
df_whole_mw_reg_unused_p(struct df_mw_hardreg * mws,bitmap live,bitmap artificial_uses)3258*38fd1498Szrj df_whole_mw_reg_unused_p (struct df_mw_hardreg *mws,
3259*38fd1498Szrj 			  bitmap live, bitmap artificial_uses)
3260*38fd1498Szrj {
3261*38fd1498Szrj   unsigned int r;
3262*38fd1498Szrj 
3263*38fd1498Szrj   /* If MWS describes a partial reference, create REG_UNUSED notes for
3264*38fd1498Szrj      individual hard registers.  */
3265*38fd1498Szrj   if (mws->flags & DF_REF_PARTIAL)
3266*38fd1498Szrj     return false;
3267*38fd1498Szrj 
3268*38fd1498Szrj   /* Likewise if some part of the register is used.  */
3269*38fd1498Szrj   for (r = mws->start_regno; r <= mws->end_regno; r++)
3270*38fd1498Szrj     if (bitmap_bit_p (live, r)
3271*38fd1498Szrj 	|| bitmap_bit_p (artificial_uses, r))
3272*38fd1498Szrj       return false;
3273*38fd1498Szrj 
3274*38fd1498Szrj   gcc_assert (REG_P (mws->mw_reg));
3275*38fd1498Szrj   return true;
3276*38fd1498Szrj }
3277*38fd1498Szrj 
3278*38fd1498Szrj 
3279*38fd1498Szrj /* Set the REG_UNUSED notes for the multiword hardreg defs in INSN
3280*38fd1498Szrj    based on the bits in LIVE.  Do not generate notes for registers in
3281*38fd1498Szrj    artificial uses.  DO_NOT_GEN is updated so that REG_DEAD notes are
3282*38fd1498Szrj    not generated if the reg is both read and written by the
3283*38fd1498Szrj    instruction.
3284*38fd1498Szrj */
3285*38fd1498Szrj 
3286*38fd1498Szrj static void
df_set_unused_notes_for_mw(rtx_insn * insn,struct df_mw_hardreg * mws,bitmap live,bitmap do_not_gen,bitmap artificial_uses,struct dead_debug_local * debug)3287*38fd1498Szrj df_set_unused_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3288*38fd1498Szrj 			    bitmap live, bitmap do_not_gen,
3289*38fd1498Szrj 			    bitmap artificial_uses,
3290*38fd1498Szrj 			    struct dead_debug_local *debug)
3291*38fd1498Szrj {
3292*38fd1498Szrj   unsigned int r;
3293*38fd1498Szrj 
3294*38fd1498Szrj   if (REG_DEAD_DEBUGGING && dump_file)
3295*38fd1498Szrj     fprintf (dump_file, "mw_set_unused looking at mws[%d..%d]\n",
3296*38fd1498Szrj 	     mws->start_regno, mws->end_regno);
3297*38fd1498Szrj 
3298*38fd1498Szrj   if (df_whole_mw_reg_unused_p (mws, live, artificial_uses))
3299*38fd1498Szrj     {
3300*38fd1498Szrj       unsigned int regno = mws->start_regno;
3301*38fd1498Szrj       df_set_note (REG_UNUSED, insn, mws->mw_reg);
3302*38fd1498Szrj       dead_debug_insert_temp (debug, regno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3303*38fd1498Szrj 
3304*38fd1498Szrj       if (REG_DEAD_DEBUGGING)
3305*38fd1498Szrj 	df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3306*38fd1498Szrj 
3307*38fd1498Szrj       bitmap_set_bit (do_not_gen, regno);
3308*38fd1498Szrj       /* Only do this if the value is totally dead.  */
3309*38fd1498Szrj     }
3310*38fd1498Szrj   else
3311*38fd1498Szrj     for (r = mws->start_regno; r <= mws->end_regno; r++)
3312*38fd1498Szrj       {
3313*38fd1498Szrj 	if (!bitmap_bit_p (live, r)
3314*38fd1498Szrj 	    && !bitmap_bit_p (artificial_uses, r))
3315*38fd1498Szrj 	  {
3316*38fd1498Szrj 	    df_set_note (REG_UNUSED, insn, regno_reg_rtx[r]);
3317*38fd1498Szrj 	    dead_debug_insert_temp (debug, r, insn, DEBUG_TEMP_AFTER_WITH_REG);
3318*38fd1498Szrj 	    if (REG_DEAD_DEBUGGING)
3319*38fd1498Szrj 	      df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3320*38fd1498Szrj 	  }
3321*38fd1498Szrj 	bitmap_set_bit (do_not_gen, r);
3322*38fd1498Szrj       }
3323*38fd1498Szrj }
3324*38fd1498Szrj 
3325*38fd1498Szrj 
3326*38fd1498Szrj /* A subroutine of df_set_dead_notes_for_mw, with a selection of its
3327*38fd1498Szrj    arguments.  Return true if the register value described by MWS's
3328*38fd1498Szrj    mw_reg is known to be completely dead, and if mw_reg can therefore
3329*38fd1498Szrj    be used in a REG_DEAD note.  */
3330*38fd1498Szrj 
3331*38fd1498Szrj static bool
df_whole_mw_reg_dead_p(struct df_mw_hardreg * mws,bitmap live,bitmap artificial_uses,bitmap do_not_gen)3332*38fd1498Szrj df_whole_mw_reg_dead_p (struct df_mw_hardreg *mws,
3333*38fd1498Szrj 			bitmap live, bitmap artificial_uses,
3334*38fd1498Szrj 			bitmap do_not_gen)
3335*38fd1498Szrj {
3336*38fd1498Szrj   unsigned int r;
3337*38fd1498Szrj 
3338*38fd1498Szrj   /* If MWS describes a partial reference, create REG_DEAD notes for
3339*38fd1498Szrj      individual hard registers.  */
3340*38fd1498Szrj   if (mws->flags & DF_REF_PARTIAL)
3341*38fd1498Szrj     return false;
3342*38fd1498Szrj 
3343*38fd1498Szrj   /* Likewise if some part of the register is not dead.  */
3344*38fd1498Szrj   for (r = mws->start_regno; r <= mws->end_regno; r++)
3345*38fd1498Szrj     if (bitmap_bit_p (live, r)
3346*38fd1498Szrj 	|| bitmap_bit_p (artificial_uses, r)
3347*38fd1498Szrj 	|| bitmap_bit_p (do_not_gen, r))
3348*38fd1498Szrj       return false;
3349*38fd1498Szrj 
3350*38fd1498Szrj   gcc_assert (REG_P (mws->mw_reg));
3351*38fd1498Szrj   return true;
3352*38fd1498Szrj }
3353*38fd1498Szrj 
3354*38fd1498Szrj /* Set the REG_DEAD notes for the multiword hardreg use in INSN based
3355*38fd1498Szrj    on the bits in LIVE.  DO_NOT_GEN is used to keep REG_DEAD notes
3356*38fd1498Szrj    from being set if the instruction both reads and writes the
3357*38fd1498Szrj    register.  */
3358*38fd1498Szrj 
3359*38fd1498Szrj static void
df_set_dead_notes_for_mw(rtx_insn * insn,struct df_mw_hardreg * mws,bitmap live,bitmap do_not_gen,bitmap artificial_uses,bool * added_notes_p)3360*38fd1498Szrj df_set_dead_notes_for_mw (rtx_insn *insn, struct df_mw_hardreg *mws,
3361*38fd1498Szrj 			  bitmap live, bitmap do_not_gen,
3362*38fd1498Szrj 			  bitmap artificial_uses, bool *added_notes_p)
3363*38fd1498Szrj {
3364*38fd1498Szrj   unsigned int r;
3365*38fd1498Szrj   bool is_debug = *added_notes_p;
3366*38fd1498Szrj 
3367*38fd1498Szrj   *added_notes_p = false;
3368*38fd1498Szrj 
3369*38fd1498Szrj   if (REG_DEAD_DEBUGGING && dump_file)
3370*38fd1498Szrj     {
3371*38fd1498Szrj       fprintf (dump_file, "mw_set_dead looking at mws[%d..%d]\n  do_not_gen =",
3372*38fd1498Szrj 	       mws->start_regno, mws->end_regno);
3373*38fd1498Szrj       df_print_regset (dump_file, do_not_gen);
3374*38fd1498Szrj       fprintf (dump_file, "  live =");
3375*38fd1498Szrj       df_print_regset (dump_file, live);
3376*38fd1498Szrj       fprintf (dump_file, "  artificial uses =");
3377*38fd1498Szrj       df_print_regset (dump_file, artificial_uses);
3378*38fd1498Szrj     }
3379*38fd1498Szrj 
3380*38fd1498Szrj   if (df_whole_mw_reg_dead_p (mws, live, artificial_uses, do_not_gen))
3381*38fd1498Szrj     {
3382*38fd1498Szrj       if (is_debug)
3383*38fd1498Szrj 	{
3384*38fd1498Szrj 	  *added_notes_p = true;
3385*38fd1498Szrj 	  return;
3386*38fd1498Szrj 	}
3387*38fd1498Szrj       /* Add a dead note for the entire multi word register.  */
3388*38fd1498Szrj       df_set_note (REG_DEAD, insn, mws->mw_reg);
3389*38fd1498Szrj       if (REG_DEAD_DEBUGGING)
3390*38fd1498Szrj 	df_print_note ("adding 1: ", insn, REG_NOTES (insn));
3391*38fd1498Szrj     }
3392*38fd1498Szrj   else
3393*38fd1498Szrj     {
3394*38fd1498Szrj       for (r = mws->start_regno; r <= mws->end_regno; r++)
3395*38fd1498Szrj 	if (!bitmap_bit_p (live, r)
3396*38fd1498Szrj 	    && !bitmap_bit_p (artificial_uses, r)
3397*38fd1498Szrj 	    && !bitmap_bit_p (do_not_gen, r))
3398*38fd1498Szrj 	  {
3399*38fd1498Szrj 	    if (is_debug)
3400*38fd1498Szrj 	      {
3401*38fd1498Szrj 		*added_notes_p = true;
3402*38fd1498Szrj 		return;
3403*38fd1498Szrj 	      }
3404*38fd1498Szrj 	    df_set_note (REG_DEAD, insn, regno_reg_rtx[r]);
3405*38fd1498Szrj 	    if (REG_DEAD_DEBUGGING)
3406*38fd1498Szrj 	      df_print_note ("adding 2: ", insn, REG_NOTES (insn));
3407*38fd1498Szrj 	  }
3408*38fd1498Szrj     }
3409*38fd1498Szrj   return;
3410*38fd1498Szrj }
3411*38fd1498Szrj 
3412*38fd1498Szrj 
3413*38fd1498Szrj /* Create a REG_UNUSED note if necessary for DEF in INSN updating
3414*38fd1498Szrj    LIVE.  Do not generate notes for registers in ARTIFICIAL_USES.  */
3415*38fd1498Szrj 
3416*38fd1498Szrj static void
df_create_unused_note(rtx_insn * insn,df_ref def,bitmap live,bitmap artificial_uses,struct dead_debug_local * debug)3417*38fd1498Szrj df_create_unused_note (rtx_insn *insn, df_ref def,
3418*38fd1498Szrj 		       bitmap live, bitmap artificial_uses,
3419*38fd1498Szrj 		       struct dead_debug_local *debug)
3420*38fd1498Szrj {
3421*38fd1498Szrj   unsigned int dregno = DF_REF_REGNO (def);
3422*38fd1498Szrj 
3423*38fd1498Szrj   if (REG_DEAD_DEBUGGING && dump_file)
3424*38fd1498Szrj     {
3425*38fd1498Szrj       fprintf (dump_file, "  regular looking at def ");
3426*38fd1498Szrj       df_ref_debug (def, dump_file);
3427*38fd1498Szrj     }
3428*38fd1498Szrj 
3429*38fd1498Szrj   if (!((DF_REF_FLAGS (def) & DF_REF_MW_HARDREG)
3430*38fd1498Szrj 	|| bitmap_bit_p (live, dregno)
3431*38fd1498Szrj 	|| bitmap_bit_p (artificial_uses, dregno)
3432*38fd1498Szrj 	|| df_ignore_stack_reg (dregno)))
3433*38fd1498Szrj     {
3434*38fd1498Szrj       rtx reg = (DF_REF_LOC (def))
3435*38fd1498Szrj                 ? *DF_REF_REAL_LOC (def): DF_REF_REG (def);
3436*38fd1498Szrj       df_set_note (REG_UNUSED, insn, reg);
3437*38fd1498Szrj       dead_debug_insert_temp (debug, dregno, insn, DEBUG_TEMP_AFTER_WITH_REG);
3438*38fd1498Szrj       if (REG_DEAD_DEBUGGING)
3439*38fd1498Szrj 	df_print_note ("adding 3: ", insn, REG_NOTES (insn));
3440*38fd1498Szrj     }
3441*38fd1498Szrj 
3442*38fd1498Szrj   return;
3443*38fd1498Szrj }
3444*38fd1498Szrj 
3445*38fd1498Szrj 
3446*38fd1498Szrj /* Recompute the REG_DEAD and REG_UNUSED notes and compute register
3447*38fd1498Szrj    info: lifetime, bb, and number of defs and uses for basic block
3448*38fd1498Szrj    BB.  The three bitvectors are scratch regs used here.  */
3449*38fd1498Szrj 
3450*38fd1498Szrj static void
df_note_bb_compute(unsigned int bb_index,bitmap live,bitmap do_not_gen,bitmap artificial_uses)3451*38fd1498Szrj df_note_bb_compute (unsigned int bb_index,
3452*38fd1498Szrj 		    bitmap live, bitmap do_not_gen, bitmap artificial_uses)
3453*38fd1498Szrj {
3454*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
3455*38fd1498Szrj   rtx_insn *insn;
3456*38fd1498Szrj   df_ref def, use;
3457*38fd1498Szrj   struct dead_debug_local debug;
3458*38fd1498Szrj 
3459*38fd1498Szrj   dead_debug_local_init (&debug, NULL, NULL);
3460*38fd1498Szrj 
3461*38fd1498Szrj   bitmap_copy (live, df_get_live_out (bb));
3462*38fd1498Szrj   bitmap_clear (artificial_uses);
3463*38fd1498Szrj 
3464*38fd1498Szrj   if (REG_DEAD_DEBUGGING && dump_file)
3465*38fd1498Szrj     {
3466*38fd1498Szrj       fprintf (dump_file, "live at bottom ");
3467*38fd1498Szrj       df_print_regset (dump_file, live);
3468*38fd1498Szrj     }
3469*38fd1498Szrj 
3470*38fd1498Szrj   /* Process the artificial defs and uses at the bottom of the block
3471*38fd1498Szrj      to begin processing.  */
3472*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3473*38fd1498Szrj     {
3474*38fd1498Szrj       if (REG_DEAD_DEBUGGING && dump_file)
3475*38fd1498Szrj 	fprintf (dump_file, "artificial def %d\n", DF_REF_REGNO (def));
3476*38fd1498Szrj 
3477*38fd1498Szrj       if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3478*38fd1498Szrj 	bitmap_clear_bit (live, DF_REF_REGNO (def));
3479*38fd1498Szrj     }
3480*38fd1498Szrj 
3481*38fd1498Szrj   FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3482*38fd1498Szrj     if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3483*38fd1498Szrj       {
3484*38fd1498Szrj 	unsigned int regno = DF_REF_REGNO (use);
3485*38fd1498Szrj 	bitmap_set_bit (live, regno);
3486*38fd1498Szrj 
3487*38fd1498Szrj 	/* Notes are not generated for any of the artificial registers
3488*38fd1498Szrj 	   at the bottom of the block.  */
3489*38fd1498Szrj 	bitmap_set_bit (artificial_uses, regno);
3490*38fd1498Szrj       }
3491*38fd1498Szrj 
3492*38fd1498Szrj   if (REG_DEAD_DEBUGGING && dump_file)
3493*38fd1498Szrj     {
3494*38fd1498Szrj       fprintf (dump_file, "live before artificials out ");
3495*38fd1498Szrj       df_print_regset (dump_file, live);
3496*38fd1498Szrj     }
3497*38fd1498Szrj 
3498*38fd1498Szrj   FOR_BB_INSNS_REVERSE (bb, insn)
3499*38fd1498Szrj     {
3500*38fd1498Szrj       if (!INSN_P (insn))
3501*38fd1498Szrj 	continue;
3502*38fd1498Szrj 
3503*38fd1498Szrj       df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3504*38fd1498Szrj       df_mw_hardreg *mw;
3505*38fd1498Szrj       int debug_insn;
3506*38fd1498Szrj 
3507*38fd1498Szrj       debug_insn = DEBUG_INSN_P (insn);
3508*38fd1498Szrj 
3509*38fd1498Szrj       bitmap_clear (do_not_gen);
3510*38fd1498Szrj       df_remove_dead_and_unused_notes (insn);
3511*38fd1498Szrj 
3512*38fd1498Szrj       /* Process the defs.  */
3513*38fd1498Szrj       if (CALL_P (insn))
3514*38fd1498Szrj 	{
3515*38fd1498Szrj 	  if (REG_DEAD_DEBUGGING && dump_file)
3516*38fd1498Szrj 	    {
3517*38fd1498Szrj 	      fprintf (dump_file, "processing call %d\n  live =",
3518*38fd1498Szrj 		       INSN_UID (insn));
3519*38fd1498Szrj 	      df_print_regset (dump_file, live);
3520*38fd1498Szrj 	    }
3521*38fd1498Szrj 
3522*38fd1498Szrj 	  /* We only care about real sets for calls.  Clobbers cannot
3523*38fd1498Szrj 	     be depended on to really die.  */
3524*38fd1498Szrj 	  FOR_EACH_INSN_INFO_MW (mw, insn_info)
3525*38fd1498Szrj 	    if ((DF_MWS_REG_DEF_P (mw))
3526*38fd1498Szrj 		&& !df_ignore_stack_reg (mw->start_regno))
3527*38fd1498Szrj 	      df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3528*38fd1498Szrj 					  artificial_uses, &debug);
3529*38fd1498Szrj 
3530*38fd1498Szrj 	  /* All of the defs except the return value are some sort of
3531*38fd1498Szrj 	     clobber.  This code is for the return.  */
3532*38fd1498Szrj 	  FOR_EACH_INSN_INFO_DEF (def, insn_info)
3533*38fd1498Szrj 	    {
3534*38fd1498Szrj 	      unsigned int dregno = DF_REF_REGNO (def);
3535*38fd1498Szrj 	      if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3536*38fd1498Szrj 		{
3537*38fd1498Szrj 		  df_create_unused_note (insn,
3538*38fd1498Szrj 					 def, live, artificial_uses, &debug);
3539*38fd1498Szrj 		  bitmap_set_bit (do_not_gen, dregno);
3540*38fd1498Szrj 		}
3541*38fd1498Szrj 
3542*38fd1498Szrj 	      if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3543*38fd1498Szrj 		bitmap_clear_bit (live, dregno);
3544*38fd1498Szrj 	    }
3545*38fd1498Szrj 	}
3546*38fd1498Szrj       else
3547*38fd1498Szrj 	{
3548*38fd1498Szrj 	  /* Regular insn.  */
3549*38fd1498Szrj 	  FOR_EACH_INSN_INFO_MW (mw, insn_info)
3550*38fd1498Szrj 	    if (DF_MWS_REG_DEF_P (mw))
3551*38fd1498Szrj 	      df_set_unused_notes_for_mw (insn, mw, live, do_not_gen,
3552*38fd1498Szrj 					  artificial_uses, &debug);
3553*38fd1498Szrj 
3554*38fd1498Szrj 	  FOR_EACH_INSN_INFO_DEF (def, insn_info)
3555*38fd1498Szrj 	    {
3556*38fd1498Szrj 	      unsigned int dregno = DF_REF_REGNO (def);
3557*38fd1498Szrj 	      df_create_unused_note (insn,
3558*38fd1498Szrj 				     def, live, artificial_uses, &debug);
3559*38fd1498Szrj 
3560*38fd1498Szrj 	      if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER))
3561*38fd1498Szrj 		bitmap_set_bit (do_not_gen, dregno);
3562*38fd1498Szrj 
3563*38fd1498Szrj 	      if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3564*38fd1498Szrj 		bitmap_clear_bit (live, dregno);
3565*38fd1498Szrj 	    }
3566*38fd1498Szrj 	}
3567*38fd1498Szrj 
3568*38fd1498Szrj       /* Process the uses.  */
3569*38fd1498Szrj       FOR_EACH_INSN_INFO_MW (mw, insn_info)
3570*38fd1498Szrj 	if (DF_MWS_REG_USE_P (mw)
3571*38fd1498Szrj 	    && !df_ignore_stack_reg (mw->start_regno))
3572*38fd1498Szrj 	  {
3573*38fd1498Szrj 	    bool really_add_notes = debug_insn != 0;
3574*38fd1498Szrj 
3575*38fd1498Szrj 	    df_set_dead_notes_for_mw (insn, mw, live, do_not_gen,
3576*38fd1498Szrj 				      artificial_uses,
3577*38fd1498Szrj 				      &really_add_notes);
3578*38fd1498Szrj 
3579*38fd1498Szrj 	    if (really_add_notes)
3580*38fd1498Szrj 	      debug_insn = -1;
3581*38fd1498Szrj 	  }
3582*38fd1498Szrj 
3583*38fd1498Szrj       FOR_EACH_INSN_INFO_USE (use, insn_info)
3584*38fd1498Szrj 	{
3585*38fd1498Szrj 	  unsigned int uregno = DF_REF_REGNO (use);
3586*38fd1498Szrj 
3587*38fd1498Szrj 	  if (REG_DEAD_DEBUGGING && dump_file && !debug_insn)
3588*38fd1498Szrj 	    {
3589*38fd1498Szrj 	      fprintf (dump_file, "  regular looking at use ");
3590*38fd1498Szrj 	      df_ref_debug (use, dump_file);
3591*38fd1498Szrj 	    }
3592*38fd1498Szrj 
3593*38fd1498Szrj 	  if (!bitmap_bit_p (live, uregno))
3594*38fd1498Szrj 	    {
3595*38fd1498Szrj 	      if (debug_insn)
3596*38fd1498Szrj 		{
3597*38fd1498Szrj 		  if (debug_insn > 0)
3598*38fd1498Szrj 		    {
3599*38fd1498Szrj 		      /* We won't add REG_UNUSED or REG_DEAD notes for
3600*38fd1498Szrj 			 these, so we don't have to mess with them in
3601*38fd1498Szrj 			 debug insns either.  */
3602*38fd1498Szrj 		      if (!bitmap_bit_p (artificial_uses, uregno)
3603*38fd1498Szrj 			  && !df_ignore_stack_reg (uregno))
3604*38fd1498Szrj 			dead_debug_add (&debug, use, uregno);
3605*38fd1498Szrj 		      continue;
3606*38fd1498Szrj 		    }
3607*38fd1498Szrj 		  break;
3608*38fd1498Szrj 		}
3609*38fd1498Szrj 	      else
3610*38fd1498Szrj 		dead_debug_insert_temp (&debug, uregno, insn,
3611*38fd1498Szrj 					DEBUG_TEMP_BEFORE_WITH_REG);
3612*38fd1498Szrj 
3613*38fd1498Szrj 	      if ( (!(DF_REF_FLAGS (use)
3614*38fd1498Szrj 		      & (DF_REF_MW_HARDREG | DF_REF_READ_WRITE)))
3615*38fd1498Szrj 		   && (!bitmap_bit_p (do_not_gen, uregno))
3616*38fd1498Szrj 		   && (!bitmap_bit_p (artificial_uses, uregno))
3617*38fd1498Szrj 		   && (!df_ignore_stack_reg (uregno)))
3618*38fd1498Szrj 		{
3619*38fd1498Szrj 		  rtx reg = (DF_REF_LOC (use))
3620*38fd1498Szrj                             ? *DF_REF_REAL_LOC (use) : DF_REF_REG (use);
3621*38fd1498Szrj 		  df_set_note (REG_DEAD, insn, reg);
3622*38fd1498Szrj 
3623*38fd1498Szrj 		  if (REG_DEAD_DEBUGGING)
3624*38fd1498Szrj 		    df_print_note ("adding 4: ", insn, REG_NOTES (insn));
3625*38fd1498Szrj 		}
3626*38fd1498Szrj 	      /* This register is now live.  */
3627*38fd1498Szrj 	      bitmap_set_bit (live, uregno);
3628*38fd1498Szrj 	    }
3629*38fd1498Szrj 	}
3630*38fd1498Szrj 
3631*38fd1498Szrj       df_remove_dead_eq_notes (insn, live);
3632*38fd1498Szrj 
3633*38fd1498Szrj       if (debug_insn == -1)
3634*38fd1498Szrj 	{
3635*38fd1498Szrj 	  /* ??? We could probably do better here, replacing dead
3636*38fd1498Szrj 	     registers with their definitions.  */
3637*38fd1498Szrj 	  INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
3638*38fd1498Szrj 	  df_insn_rescan_debug_internal (insn);
3639*38fd1498Szrj 	}
3640*38fd1498Szrj     }
3641*38fd1498Szrj 
3642*38fd1498Szrj   dead_debug_local_finish (&debug, NULL);
3643*38fd1498Szrj }
3644*38fd1498Szrj 
3645*38fd1498Szrj 
3646*38fd1498Szrj /* Compute register info: lifetime, bb, and number of defs and uses.  */
3647*38fd1498Szrj static void
df_note_compute(bitmap all_blocks)3648*38fd1498Szrj df_note_compute (bitmap all_blocks)
3649*38fd1498Szrj {
3650*38fd1498Szrj   unsigned int bb_index;
3651*38fd1498Szrj   bitmap_iterator bi;
3652*38fd1498Szrj   bitmap_head live, do_not_gen, artificial_uses;
3653*38fd1498Szrj 
3654*38fd1498Szrj   bitmap_initialize (&live, &df_bitmap_obstack);
3655*38fd1498Szrj   bitmap_initialize (&do_not_gen, &df_bitmap_obstack);
3656*38fd1498Szrj   bitmap_initialize (&artificial_uses, &df_bitmap_obstack);
3657*38fd1498Szrj 
3658*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
3659*38fd1498Szrj   {
3660*38fd1498Szrj     /* ??? Unlike fast DCE, we don't use global_debug for uses of dead
3661*38fd1498Szrj        pseudos in debug insns because we don't always (re)visit blocks
3662*38fd1498Szrj        with death points after visiting dead uses.  Even changing this
3663*38fd1498Szrj        loop to postorder would still leave room for visiting a death
3664*38fd1498Szrj        point before visiting a subsequent debug use.  */
3665*38fd1498Szrj     df_note_bb_compute (bb_index, &live, &do_not_gen, &artificial_uses);
3666*38fd1498Szrj   }
3667*38fd1498Szrj 
3668*38fd1498Szrj   bitmap_clear (&live);
3669*38fd1498Szrj   bitmap_clear (&do_not_gen);
3670*38fd1498Szrj   bitmap_clear (&artificial_uses);
3671*38fd1498Szrj }
3672*38fd1498Szrj 
3673*38fd1498Szrj 
3674*38fd1498Szrj /* Free all storage associated with the problem.  */
3675*38fd1498Szrj 
3676*38fd1498Szrj static void
df_note_free(void)3677*38fd1498Szrj df_note_free (void)
3678*38fd1498Szrj {
3679*38fd1498Szrj   free (df_note);
3680*38fd1498Szrj }
3681*38fd1498Szrj 
3682*38fd1498Szrj 
3683*38fd1498Szrj /* All of the information associated every instance of the problem.  */
3684*38fd1498Szrj 
3685*38fd1498Szrj static const struct df_problem problem_NOTE =
3686*38fd1498Szrj {
3687*38fd1498Szrj   DF_NOTE,                    /* Problem id.  */
3688*38fd1498Szrj   DF_NONE,                    /* Direction.  */
3689*38fd1498Szrj   df_note_alloc,              /* Allocate the problem specific data.  */
3690*38fd1498Szrj   NULL,                       /* Reset global information.  */
3691*38fd1498Szrj   NULL,                       /* Free basic block info.  */
3692*38fd1498Szrj   df_note_compute,            /* Local compute function.  */
3693*38fd1498Szrj   NULL,                       /* Init the solution specific data.  */
3694*38fd1498Szrj   NULL,                       /* Iterative solver.  */
3695*38fd1498Szrj   NULL,                       /* Confluence operator 0.  */
3696*38fd1498Szrj   NULL,                       /* Confluence operator n.  */
3697*38fd1498Szrj   NULL,                       /* Transfer function.  */
3698*38fd1498Szrj   NULL,                       /* Finalize function.  */
3699*38fd1498Szrj   df_note_free,               /* Free all of the problem information.  */
3700*38fd1498Szrj   df_note_free,               /* Remove this problem from the stack of dataflow problems.  */
3701*38fd1498Szrj   NULL,                       /* Debugging.  */
3702*38fd1498Szrj   NULL,                       /* Debugging start block.  */
3703*38fd1498Szrj   NULL,                       /* Debugging end block.  */
3704*38fd1498Szrj   NULL,                       /* Debugging start insn.  */
3705*38fd1498Szrj   NULL,                       /* Debugging end insn.  */
3706*38fd1498Szrj   NULL,                       /* Incremental solution verify start.  */
3707*38fd1498Szrj   NULL,                       /* Incremental solution verify end.  */
3708*38fd1498Szrj   &problem_LR,                /* Dependent problem.  */
3709*38fd1498Szrj   sizeof (struct df_scan_bb_info),/* Size of entry of block_info array.  */
3710*38fd1498Szrj   TV_DF_NOTE,                 /* Timing variable.  */
3711*38fd1498Szrj   false                       /* Reset blocks on dropping out of blocks_to_analyze.  */
3712*38fd1498Szrj };
3713*38fd1498Szrj 
3714*38fd1498Szrj 
3715*38fd1498Szrj /* Create a new DATAFLOW instance and add it to an existing instance
3716*38fd1498Szrj    of DF.  The returned structure is what is used to get at the
3717*38fd1498Szrj    solution.  */
3718*38fd1498Szrj 
3719*38fd1498Szrj void
df_note_add_problem(void)3720*38fd1498Szrj df_note_add_problem (void)
3721*38fd1498Szrj {
3722*38fd1498Szrj   df_add_problem (&problem_NOTE);
3723*38fd1498Szrj }
3724*38fd1498Szrj 
3725*38fd1498Szrj 
3726*38fd1498Szrj 
3727*38fd1498Szrj 
3728*38fd1498Szrj /*----------------------------------------------------------------------------
3729*38fd1498Szrj    Functions for simulating the effects of single insns.
3730*38fd1498Szrj 
3731*38fd1498Szrj    You can either simulate in the forwards direction, starting from
3732*38fd1498Szrj    the top of a block or the backwards direction from the end of the
3733*38fd1498Szrj    block.  If you go backwards, defs are examined first to clear bits,
3734*38fd1498Szrj    then uses are examined to set bits.  If you go forwards, defs are
3735*38fd1498Szrj    examined first to set bits, then REG_DEAD and REG_UNUSED notes
3736*38fd1498Szrj    are examined to clear bits.  In either case, the result of examining
3737*38fd1498Szrj    a def can be undone (respectively by a use or a REG_UNUSED note).
3738*38fd1498Szrj 
3739*38fd1498Szrj    If you start at the top of the block, use one of DF_LIVE_IN or
3740*38fd1498Szrj    DF_LR_IN.  If you start at the bottom of the block use one of
3741*38fd1498Szrj    DF_LIVE_OUT or DF_LR_OUT.  BE SURE TO PASS A COPY OF THESE SETS,
3742*38fd1498Szrj    THEY WILL BE DESTROYED.
3743*38fd1498Szrj ----------------------------------------------------------------------------*/
3744*38fd1498Szrj 
3745*38fd1498Szrj 
3746*38fd1498Szrj /* Find the set of DEFs for INSN.  */
3747*38fd1498Szrj 
3748*38fd1498Szrj void
df_simulate_find_defs(rtx_insn * insn,bitmap defs)3749*38fd1498Szrj df_simulate_find_defs (rtx_insn *insn, bitmap defs)
3750*38fd1498Szrj {
3751*38fd1498Szrj   df_ref def;
3752*38fd1498Szrj 
3753*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
3754*38fd1498Szrj     bitmap_set_bit (defs, DF_REF_REGNO (def));
3755*38fd1498Szrj }
3756*38fd1498Szrj 
3757*38fd1498Szrj /* Find the set of uses for INSN.  This includes partial defs.  */
3758*38fd1498Szrj 
3759*38fd1498Szrj static void
df_simulate_find_uses(rtx_insn * insn,bitmap uses)3760*38fd1498Szrj df_simulate_find_uses (rtx_insn *insn, bitmap uses)
3761*38fd1498Szrj {
3762*38fd1498Szrj   df_ref def, use;
3763*38fd1498Szrj   struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
3764*38fd1498Szrj 
3765*38fd1498Szrj   FOR_EACH_INSN_INFO_DEF (def, insn_info)
3766*38fd1498Szrj     if (DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL))
3767*38fd1498Szrj       bitmap_set_bit (uses, DF_REF_REGNO (def));
3768*38fd1498Szrj   FOR_EACH_INSN_INFO_USE (use, insn_info)
3769*38fd1498Szrj     bitmap_set_bit (uses, DF_REF_REGNO (use));
3770*38fd1498Szrj }
3771*38fd1498Szrj 
3772*38fd1498Szrj /* Find the set of real DEFs, which are not clobbers, for INSN.  */
3773*38fd1498Szrj 
3774*38fd1498Szrj void
df_simulate_find_noclobber_defs(rtx_insn * insn,bitmap defs)3775*38fd1498Szrj df_simulate_find_noclobber_defs (rtx_insn *insn, bitmap defs)
3776*38fd1498Szrj {
3777*38fd1498Szrj   df_ref def;
3778*38fd1498Szrj 
3779*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
3780*38fd1498Szrj     if (!(DF_REF_FLAGS (def) & (DF_REF_MUST_CLOBBER | DF_REF_MAY_CLOBBER)))
3781*38fd1498Szrj       bitmap_set_bit (defs, DF_REF_REGNO (def));
3782*38fd1498Szrj }
3783*38fd1498Szrj 
3784*38fd1498Szrj 
3785*38fd1498Szrj /* Simulate the effects of the defs of INSN on LIVE.  */
3786*38fd1498Szrj 
3787*38fd1498Szrj void
df_simulate_defs(rtx_insn * insn,bitmap live)3788*38fd1498Szrj df_simulate_defs (rtx_insn *insn, bitmap live)
3789*38fd1498Szrj {
3790*38fd1498Szrj   df_ref def;
3791*38fd1498Szrj 
3792*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
3793*38fd1498Szrj     {
3794*38fd1498Szrj       unsigned int dregno = DF_REF_REGNO (def);
3795*38fd1498Szrj 
3796*38fd1498Szrj       /* If the def is to only part of the reg, it does
3797*38fd1498Szrj 	 not kill the other defs that reach here.  */
3798*38fd1498Szrj       if (!(DF_REF_FLAGS (def) & (DF_REF_PARTIAL | DF_REF_CONDITIONAL)))
3799*38fd1498Szrj 	bitmap_clear_bit (live, dregno);
3800*38fd1498Szrj     }
3801*38fd1498Szrj }
3802*38fd1498Szrj 
3803*38fd1498Szrj 
3804*38fd1498Szrj /* Simulate the effects of the uses of INSN on LIVE.  */
3805*38fd1498Szrj 
3806*38fd1498Szrj void
df_simulate_uses(rtx_insn * insn,bitmap live)3807*38fd1498Szrj df_simulate_uses (rtx_insn *insn, bitmap live)
3808*38fd1498Szrj {
3809*38fd1498Szrj   df_ref use;
3810*38fd1498Szrj 
3811*38fd1498Szrj   if (DEBUG_INSN_P (insn))
3812*38fd1498Szrj     return;
3813*38fd1498Szrj 
3814*38fd1498Szrj   FOR_EACH_INSN_USE (use, insn)
3815*38fd1498Szrj     /* Add use to set of uses in this BB.  */
3816*38fd1498Szrj     bitmap_set_bit (live, DF_REF_REGNO (use));
3817*38fd1498Szrj }
3818*38fd1498Szrj 
3819*38fd1498Szrj 
3820*38fd1498Szrj /* Add back the always live regs in BB to LIVE.  */
3821*38fd1498Szrj 
3822*38fd1498Szrj static inline void
df_simulate_fixup_sets(basic_block bb,bitmap live)3823*38fd1498Szrj df_simulate_fixup_sets (basic_block bb, bitmap live)
3824*38fd1498Szrj {
3825*38fd1498Szrj   /* These regs are considered always live so if they end up dying
3826*38fd1498Szrj      because of some def, we need to bring the back again.  */
3827*38fd1498Szrj   if (bb_has_eh_pred (bb))
3828*38fd1498Szrj     bitmap_ior_into (live, &df->eh_block_artificial_uses);
3829*38fd1498Szrj   else
3830*38fd1498Szrj     bitmap_ior_into (live, &df->regular_block_artificial_uses);
3831*38fd1498Szrj }
3832*38fd1498Szrj 
3833*38fd1498Szrj 
3834*38fd1498Szrj /*----------------------------------------------------------------------------
3835*38fd1498Szrj    The following three functions are used only for BACKWARDS scanning:
3836*38fd1498Szrj    i.e. they process the defs before the uses.
3837*38fd1498Szrj 
3838*38fd1498Szrj    df_simulate_initialize_backwards should be called first with a
3839*38fd1498Szrj    bitvector copyied from the DF_LIVE_OUT or DF_LR_OUT.  Then
3840*38fd1498Szrj    df_simulate_one_insn_backwards should be called for each insn in
3841*38fd1498Szrj    the block, starting with the last one.  Finally,
3842*38fd1498Szrj    df_simulate_finalize_backwards can be called to get a new value
3843*38fd1498Szrj    of the sets at the top of the block (this is rarely used).
3844*38fd1498Szrj    ----------------------------------------------------------------------------*/
3845*38fd1498Szrj 
3846*38fd1498Szrj /* Apply the artificial uses and defs at the end of BB in a backwards
3847*38fd1498Szrj    direction.  */
3848*38fd1498Szrj 
3849*38fd1498Szrj void
df_simulate_initialize_backwards(basic_block bb,bitmap live)3850*38fd1498Szrj df_simulate_initialize_backwards (basic_block bb, bitmap live)
3851*38fd1498Szrj {
3852*38fd1498Szrj   df_ref def, use;
3853*38fd1498Szrj   int bb_index = bb->index;
3854*38fd1498Szrj 
3855*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3856*38fd1498Szrj     if ((DF_REF_FLAGS (def) & DF_REF_AT_TOP) == 0)
3857*38fd1498Szrj       bitmap_clear_bit (live, DF_REF_REGNO (def));
3858*38fd1498Szrj 
3859*38fd1498Szrj   FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3860*38fd1498Szrj     if ((DF_REF_FLAGS (use) & DF_REF_AT_TOP) == 0)
3861*38fd1498Szrj       bitmap_set_bit (live, DF_REF_REGNO (use));
3862*38fd1498Szrj }
3863*38fd1498Szrj 
3864*38fd1498Szrj 
3865*38fd1498Szrj /* Simulate the backwards effects of INSN on the bitmap LIVE.  */
3866*38fd1498Szrj 
3867*38fd1498Szrj void
df_simulate_one_insn_backwards(basic_block bb,rtx_insn * insn,bitmap live)3868*38fd1498Szrj df_simulate_one_insn_backwards (basic_block bb, rtx_insn *insn, bitmap live)
3869*38fd1498Szrj {
3870*38fd1498Szrj   if (!NONDEBUG_INSN_P (insn))
3871*38fd1498Szrj     return;
3872*38fd1498Szrj 
3873*38fd1498Szrj   df_simulate_defs (insn, live);
3874*38fd1498Szrj   df_simulate_uses (insn, live);
3875*38fd1498Szrj   df_simulate_fixup_sets (bb, live);
3876*38fd1498Szrj }
3877*38fd1498Szrj 
3878*38fd1498Szrj 
3879*38fd1498Szrj /* Apply the artificial uses and defs at the top of BB in a backwards
3880*38fd1498Szrj    direction.  */
3881*38fd1498Szrj 
3882*38fd1498Szrj void
df_simulate_finalize_backwards(basic_block bb,bitmap live)3883*38fd1498Szrj df_simulate_finalize_backwards (basic_block bb, bitmap live)
3884*38fd1498Szrj {
3885*38fd1498Szrj   df_ref def;
3886*38fd1498Szrj #ifdef EH_USES
3887*38fd1498Szrj   df_ref use;
3888*38fd1498Szrj #endif
3889*38fd1498Szrj   int bb_index = bb->index;
3890*38fd1498Szrj 
3891*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3892*38fd1498Szrj     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3893*38fd1498Szrj       bitmap_clear_bit (live, DF_REF_REGNO (def));
3894*38fd1498Szrj 
3895*38fd1498Szrj #ifdef EH_USES
3896*38fd1498Szrj   FOR_EACH_ARTIFICIAL_USE (use, bb_index)
3897*38fd1498Szrj     if (DF_REF_FLAGS (use) & DF_REF_AT_TOP)
3898*38fd1498Szrj       bitmap_set_bit (live, DF_REF_REGNO (use));
3899*38fd1498Szrj #endif
3900*38fd1498Szrj }
3901*38fd1498Szrj /*----------------------------------------------------------------------------
3902*38fd1498Szrj    The following three functions are used only for FORWARDS scanning:
3903*38fd1498Szrj    i.e. they process the defs and the REG_DEAD and REG_UNUSED notes.
3904*38fd1498Szrj    Thus it is important to add the DF_NOTES problem to the stack of
3905*38fd1498Szrj    problems computed before using these functions.
3906*38fd1498Szrj 
3907*38fd1498Szrj    df_simulate_initialize_forwards should be called first with a
3908*38fd1498Szrj    bitvector copyied from the DF_LIVE_IN or DF_LR_IN.  Then
3909*38fd1498Szrj    df_simulate_one_insn_forwards should be called for each insn in
3910*38fd1498Szrj    the block, starting with the first one.
3911*38fd1498Szrj    ----------------------------------------------------------------------------*/
3912*38fd1498Szrj 
3913*38fd1498Szrj /* Initialize the LIVE bitmap, which should be copied from DF_LIVE_IN or
3914*38fd1498Szrj    DF_LR_IN for basic block BB, for forward scanning by marking artificial
3915*38fd1498Szrj    defs live.  */
3916*38fd1498Szrj 
3917*38fd1498Szrj void
df_simulate_initialize_forwards(basic_block bb,bitmap live)3918*38fd1498Szrj df_simulate_initialize_forwards (basic_block bb, bitmap live)
3919*38fd1498Szrj {
3920*38fd1498Szrj   df_ref def;
3921*38fd1498Szrj   int bb_index = bb->index;
3922*38fd1498Szrj 
3923*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
3924*38fd1498Szrj     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
3925*38fd1498Szrj       bitmap_set_bit (live, DF_REF_REGNO (def));
3926*38fd1498Szrj }
3927*38fd1498Szrj 
3928*38fd1498Szrj /* Simulate the forwards effects of INSN on the bitmap LIVE.  */
3929*38fd1498Szrj 
3930*38fd1498Szrj void
df_simulate_one_insn_forwards(basic_block bb,rtx_insn * insn,bitmap live)3931*38fd1498Szrj df_simulate_one_insn_forwards (basic_block bb, rtx_insn *insn, bitmap live)
3932*38fd1498Szrj {
3933*38fd1498Szrj   rtx link;
3934*38fd1498Szrj   if (! INSN_P (insn))
3935*38fd1498Szrj     return;
3936*38fd1498Szrj 
3937*38fd1498Szrj   /* Make sure that DF_NOTE really is an active df problem.  */
3938*38fd1498Szrj   gcc_assert (df_note);
3939*38fd1498Szrj 
3940*38fd1498Szrj   /* Note that this is the opposite as how the problem is defined, because
3941*38fd1498Szrj      in the LR problem defs _kill_ liveness.  However, they do so backwards,
3942*38fd1498Szrj      while here the scan is performed forwards!  So, first assume that the
3943*38fd1498Szrj      def is live, and if this is not true REG_UNUSED notes will rectify the
3944*38fd1498Szrj      situation.  */
3945*38fd1498Szrj   df_simulate_find_noclobber_defs (insn, live);
3946*38fd1498Szrj 
3947*38fd1498Szrj   /* Clear all of the registers that go dead.  */
3948*38fd1498Szrj   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3949*38fd1498Szrj     {
3950*38fd1498Szrj       switch (REG_NOTE_KIND (link))
3951*38fd1498Szrj 	{
3952*38fd1498Szrj 	case REG_DEAD:
3953*38fd1498Szrj 	case REG_UNUSED:
3954*38fd1498Szrj 	  {
3955*38fd1498Szrj 	    rtx reg = XEXP (link, 0);
3956*38fd1498Szrj 	    bitmap_clear_range (live, REGNO (reg), REG_NREGS (reg));
3957*38fd1498Szrj 	  }
3958*38fd1498Szrj 	  break;
3959*38fd1498Szrj 	default:
3960*38fd1498Szrj 	  break;
3961*38fd1498Szrj 	}
3962*38fd1498Szrj     }
3963*38fd1498Szrj   df_simulate_fixup_sets (bb, live);
3964*38fd1498Szrj }
3965*38fd1498Szrj 
3966*38fd1498Szrj /* Used by the next two functions to encode information about the
3967*38fd1498Szrj    memory references we found.  */
3968*38fd1498Szrj #define MEMREF_NORMAL 1
3969*38fd1498Szrj #define MEMREF_VOLATILE 2
3970*38fd1498Szrj 
3971*38fd1498Szrj /* Return an OR of MEMREF_NORMAL or MEMREF_VOLATILE for the MEMs in X.  */
3972*38fd1498Szrj 
3973*38fd1498Szrj static int
find_memory(rtx_insn * insn)3974*38fd1498Szrj find_memory (rtx_insn *insn)
3975*38fd1498Szrj {
3976*38fd1498Szrj   int flags = 0;
3977*38fd1498Szrj   subrtx_iterator::array_type array;
3978*38fd1498Szrj   FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3979*38fd1498Szrj     {
3980*38fd1498Szrj       const_rtx x = *iter;
3981*38fd1498Szrj       if (GET_CODE (x) == ASM_OPERANDS && MEM_VOLATILE_P (x))
3982*38fd1498Szrj 	flags |= MEMREF_VOLATILE;
3983*38fd1498Szrj       else if (MEM_P (x))
3984*38fd1498Szrj 	{
3985*38fd1498Szrj 	  if (MEM_VOLATILE_P (x))
3986*38fd1498Szrj 	    flags |= MEMREF_VOLATILE;
3987*38fd1498Szrj 	  else if (!MEM_READONLY_P (x))
3988*38fd1498Szrj 	    flags |= MEMREF_NORMAL;
3989*38fd1498Szrj 	}
3990*38fd1498Szrj     }
3991*38fd1498Szrj   return flags;
3992*38fd1498Szrj }
3993*38fd1498Szrj 
3994*38fd1498Szrj /* A subroutine of can_move_insns_across_p called through note_stores.
3995*38fd1498Szrj    DATA points to an integer in which we set either the bit for
3996*38fd1498Szrj    MEMREF_NORMAL or the bit for MEMREF_VOLATILE if we find a MEM
3997*38fd1498Szrj    of either kind.  */
3998*38fd1498Szrj 
3999*38fd1498Szrj static void
find_memory_stores(rtx x,const_rtx pat ATTRIBUTE_UNUSED,void * data ATTRIBUTE_UNUSED)4000*38fd1498Szrj find_memory_stores (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
4001*38fd1498Szrj 		    void *data ATTRIBUTE_UNUSED)
4002*38fd1498Szrj {
4003*38fd1498Szrj   int *pflags = (int *)data;
4004*38fd1498Szrj   if (GET_CODE (x) == SUBREG)
4005*38fd1498Szrj     x = XEXP (x, 0);
4006*38fd1498Szrj   /* Treat stores to SP as stores to memory, this will prevent problems
4007*38fd1498Szrj      when there are references to the stack frame.  */
4008*38fd1498Szrj   if (x == stack_pointer_rtx)
4009*38fd1498Szrj     *pflags |= MEMREF_VOLATILE;
4010*38fd1498Szrj   if (!MEM_P (x))
4011*38fd1498Szrj     return;
4012*38fd1498Szrj   *pflags |= MEM_VOLATILE_P (x) ? MEMREF_VOLATILE : MEMREF_NORMAL;
4013*38fd1498Szrj }
4014*38fd1498Szrj 
4015*38fd1498Szrj /* Scan BB backwards, using df_simulate functions to keep track of
4016*38fd1498Szrj    lifetimes, up to insn POINT.  The result is stored in LIVE.  */
4017*38fd1498Szrj 
4018*38fd1498Szrj void
simulate_backwards_to_point(basic_block bb,regset live,rtx point)4019*38fd1498Szrj simulate_backwards_to_point (basic_block bb, regset live, rtx point)
4020*38fd1498Szrj {
4021*38fd1498Szrj   rtx_insn *insn;
4022*38fd1498Szrj   bitmap_copy (live, df_get_live_out (bb));
4023*38fd1498Szrj   df_simulate_initialize_backwards (bb, live);
4024*38fd1498Szrj 
4025*38fd1498Szrj   /* Scan and update life information until we reach the point we're
4026*38fd1498Szrj      interested in.  */
4027*38fd1498Szrj   for (insn = BB_END (bb); insn != point; insn = PREV_INSN (insn))
4028*38fd1498Szrj     df_simulate_one_insn_backwards (bb, insn, live);
4029*38fd1498Szrj }
4030*38fd1498Szrj 
4031*38fd1498Szrj /* Return true if it is safe to move a group of insns, described by
4032*38fd1498Szrj    the range FROM to TO, backwards across another group of insns,
4033*38fd1498Szrj    described by ACROSS_FROM to ACROSS_TO.  It is assumed that there
4034*38fd1498Szrj    are no insns between ACROSS_TO and FROM, but they may be in
4035*38fd1498Szrj    different basic blocks; MERGE_BB is the block from which the
4036*38fd1498Szrj    insns will be moved.  The caller must pass in a regset MERGE_LIVE
4037*38fd1498Szrj    which specifies the registers live after TO.
4038*38fd1498Szrj 
4039*38fd1498Szrj    This function may be called in one of two cases: either we try to
4040*38fd1498Szrj    move identical instructions from all successor blocks into their
4041*38fd1498Szrj    predecessor, or we try to move from only one successor block.  If
4042*38fd1498Szrj    OTHER_BRANCH_LIVE is nonnull, it indicates that we're dealing with
4043*38fd1498Szrj    the second case.  It should contain a set of registers live at the
4044*38fd1498Szrj    end of ACROSS_TO which must not be clobbered by moving the insns.
4045*38fd1498Szrj    In that case, we're also more careful about moving memory references
4046*38fd1498Szrj    and trapping insns.
4047*38fd1498Szrj 
4048*38fd1498Szrj    We return false if it is not safe to move the entire group, but it
4049*38fd1498Szrj    may still be possible to move a subgroup.  PMOVE_UPTO, if nonnull,
4050*38fd1498Szrj    is set to point at the last moveable insn in such a case.  */
4051*38fd1498Szrj 
4052*38fd1498Szrj bool
can_move_insns_across(rtx_insn * from,rtx_insn * to,rtx_insn * across_from,rtx_insn * across_to,basic_block merge_bb,regset merge_live,regset other_branch_live,rtx_insn ** pmove_upto)4053*38fd1498Szrj can_move_insns_across (rtx_insn *from, rtx_insn *to,
4054*38fd1498Szrj 		       rtx_insn *across_from, rtx_insn *across_to,
4055*38fd1498Szrj 		       basic_block merge_bb, regset merge_live,
4056*38fd1498Szrj 		       regset other_branch_live, rtx_insn **pmove_upto)
4057*38fd1498Szrj {
4058*38fd1498Szrj   rtx_insn *insn, *next, *max_to;
4059*38fd1498Szrj   bitmap merge_set, merge_use, local_merge_live;
4060*38fd1498Szrj   bitmap test_set, test_use;
4061*38fd1498Szrj   unsigned i, fail = 0;
4062*38fd1498Szrj   bitmap_iterator bi;
4063*38fd1498Szrj   int memrefs_in_across = 0;
4064*38fd1498Szrj   int mem_sets_in_across = 0;
4065*38fd1498Szrj   bool trapping_insns_in_across = false;
4066*38fd1498Szrj 
4067*38fd1498Szrj   if (pmove_upto != NULL)
4068*38fd1498Szrj     *pmove_upto = NULL;
4069*38fd1498Szrj 
4070*38fd1498Szrj   /* Find real bounds, ignoring debug insns.  */
4071*38fd1498Szrj   while (!NONDEBUG_INSN_P (from) && from != to)
4072*38fd1498Szrj     from = NEXT_INSN (from);
4073*38fd1498Szrj   while (!NONDEBUG_INSN_P (to) && from != to)
4074*38fd1498Szrj     to = PREV_INSN (to);
4075*38fd1498Szrj 
4076*38fd1498Szrj   for (insn = across_to; ; insn = next)
4077*38fd1498Szrj     {
4078*38fd1498Szrj       if (CALL_P (insn))
4079*38fd1498Szrj 	{
4080*38fd1498Szrj 	  if (RTL_CONST_OR_PURE_CALL_P (insn))
4081*38fd1498Szrj 	    /* Pure functions can read from memory.  Const functions can
4082*38fd1498Szrj 	       read from arguments that the ABI has forced onto the stack.
4083*38fd1498Szrj 	       Neither sort of read can be volatile.  */
4084*38fd1498Szrj 	    memrefs_in_across |= MEMREF_NORMAL;
4085*38fd1498Szrj 	  else
4086*38fd1498Szrj 	    {
4087*38fd1498Szrj 	      memrefs_in_across |= MEMREF_VOLATILE;
4088*38fd1498Szrj 	      mem_sets_in_across |= MEMREF_VOLATILE;
4089*38fd1498Szrj 	    }
4090*38fd1498Szrj 	}
4091*38fd1498Szrj       if (NONDEBUG_INSN_P (insn))
4092*38fd1498Szrj 	{
4093*38fd1498Szrj 	  if (volatile_insn_p (PATTERN (insn)))
4094*38fd1498Szrj 	    return false;
4095*38fd1498Szrj 	  memrefs_in_across |= find_memory (insn);
4096*38fd1498Szrj 	  note_stores (PATTERN (insn), find_memory_stores,
4097*38fd1498Szrj 		       &mem_sets_in_across);
4098*38fd1498Szrj 	  /* This is used just to find sets of the stack pointer.  */
4099*38fd1498Szrj 	  memrefs_in_across |= mem_sets_in_across;
4100*38fd1498Szrj 	  trapping_insns_in_across |= may_trap_p (PATTERN (insn));
4101*38fd1498Szrj 	}
4102*38fd1498Szrj       next = PREV_INSN (insn);
4103*38fd1498Szrj       if (insn == across_from)
4104*38fd1498Szrj 	break;
4105*38fd1498Szrj     }
4106*38fd1498Szrj 
4107*38fd1498Szrj   /* Collect:
4108*38fd1498Szrj      MERGE_SET = set of registers set in MERGE_BB
4109*38fd1498Szrj      MERGE_USE = set of registers used in MERGE_BB and live at its top
4110*38fd1498Szrj      MERGE_LIVE = set of registers live at the point inside the MERGE
4111*38fd1498Szrj      range that we've reached during scanning
4112*38fd1498Szrj      TEST_SET = set of registers set between ACROSS_FROM and ACROSS_END.
4113*38fd1498Szrj      TEST_USE = set of registers used between ACROSS_FROM and ACROSS_END,
4114*38fd1498Szrj      and live before ACROSS_FROM.  */
4115*38fd1498Szrj 
4116*38fd1498Szrj   merge_set = BITMAP_ALLOC (&reg_obstack);
4117*38fd1498Szrj   merge_use = BITMAP_ALLOC (&reg_obstack);
4118*38fd1498Szrj   local_merge_live = BITMAP_ALLOC (&reg_obstack);
4119*38fd1498Szrj   test_set = BITMAP_ALLOC (&reg_obstack);
4120*38fd1498Szrj   test_use = BITMAP_ALLOC (&reg_obstack);
4121*38fd1498Szrj 
4122*38fd1498Szrj   /* Compute the set of registers set and used in the ACROSS range.  */
4123*38fd1498Szrj   if (other_branch_live != NULL)
4124*38fd1498Szrj     bitmap_copy (test_use, other_branch_live);
4125*38fd1498Szrj   df_simulate_initialize_backwards (merge_bb, test_use);
4126*38fd1498Szrj   for (insn = across_to; ; insn = next)
4127*38fd1498Szrj     {
4128*38fd1498Szrj       if (NONDEBUG_INSN_P (insn))
4129*38fd1498Szrj 	{
4130*38fd1498Szrj 	  df_simulate_find_defs (insn, test_set);
4131*38fd1498Szrj 	  df_simulate_defs (insn, test_use);
4132*38fd1498Szrj 	  df_simulate_uses (insn, test_use);
4133*38fd1498Szrj 	}
4134*38fd1498Szrj       next = PREV_INSN (insn);
4135*38fd1498Szrj       if (insn == across_from)
4136*38fd1498Szrj 	break;
4137*38fd1498Szrj     }
4138*38fd1498Szrj 
4139*38fd1498Szrj   /* Compute an upper bound for the amount of insns moved, by finding
4140*38fd1498Szrj      the first insn in MERGE that sets a register in TEST_USE, or uses
4141*38fd1498Szrj      a register in TEST_SET.  We also check for calls, trapping operations,
4142*38fd1498Szrj      and memory references.  */
4143*38fd1498Szrj   max_to = NULL;
4144*38fd1498Szrj   for (insn = from; ; insn = next)
4145*38fd1498Szrj     {
4146*38fd1498Szrj       if (CALL_P (insn))
4147*38fd1498Szrj 	break;
4148*38fd1498Szrj       if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_EPILOGUE_BEG)
4149*38fd1498Szrj 	break;
4150*38fd1498Szrj       if (NONDEBUG_INSN_P (insn))
4151*38fd1498Szrj 	{
4152*38fd1498Szrj 	  if (may_trap_or_fault_p (PATTERN (insn))
4153*38fd1498Szrj 	      && (trapping_insns_in_across
4154*38fd1498Szrj 		  || other_branch_live != NULL
4155*38fd1498Szrj 		  || volatile_insn_p (PATTERN (insn))))
4156*38fd1498Szrj 	    break;
4157*38fd1498Szrj 
4158*38fd1498Szrj 	  /* We cannot move memory stores past each other, or move memory
4159*38fd1498Szrj 	     reads past stores, at least not without tracking them and
4160*38fd1498Szrj 	     calling true_dependence on every pair.
4161*38fd1498Szrj 
4162*38fd1498Szrj 	     If there is no other branch and no memory references or
4163*38fd1498Szrj 	     sets in the ACROSS range, we can move memory references
4164*38fd1498Szrj 	     freely, even volatile ones.
4165*38fd1498Szrj 
4166*38fd1498Szrj 	     Otherwise, the rules are as follows: volatile memory
4167*38fd1498Szrj 	     references and stores can't be moved at all, and any type
4168*38fd1498Szrj 	     of memory reference can't be moved if there are volatile
4169*38fd1498Szrj 	     accesses or stores in the ACROSS range.  That leaves
4170*38fd1498Szrj 	     normal reads, which can be moved, as the trapping case is
4171*38fd1498Szrj 	     dealt with elsewhere.  */
4172*38fd1498Szrj 	  if (other_branch_live != NULL || memrefs_in_across != 0)
4173*38fd1498Szrj 	    {
4174*38fd1498Szrj 	      int mem_ref_flags = 0;
4175*38fd1498Szrj 	      int mem_set_flags = 0;
4176*38fd1498Szrj 	      note_stores (PATTERN (insn), find_memory_stores, &mem_set_flags);
4177*38fd1498Szrj 	      mem_ref_flags = find_memory (insn);
4178*38fd1498Szrj 	      /* Catch sets of the stack pointer.  */
4179*38fd1498Szrj 	      mem_ref_flags |= mem_set_flags;
4180*38fd1498Szrj 
4181*38fd1498Szrj 	      if ((mem_ref_flags | mem_set_flags) & MEMREF_VOLATILE)
4182*38fd1498Szrj 		break;
4183*38fd1498Szrj 	      if ((memrefs_in_across & MEMREF_VOLATILE) && mem_ref_flags != 0)
4184*38fd1498Szrj 		break;
4185*38fd1498Szrj 	      if (mem_set_flags != 0
4186*38fd1498Szrj 		  || (mem_sets_in_across != 0 && mem_ref_flags != 0))
4187*38fd1498Szrj 		break;
4188*38fd1498Szrj 	    }
4189*38fd1498Szrj 	  df_simulate_find_uses (insn, merge_use);
4190*38fd1498Szrj 	  /* We're only interested in uses which use a value live at
4191*38fd1498Szrj 	     the top, not one previously set in this block.  */
4192*38fd1498Szrj 	  bitmap_and_compl_into (merge_use, merge_set);
4193*38fd1498Szrj 	  df_simulate_find_defs (insn, merge_set);
4194*38fd1498Szrj 	  if (bitmap_intersect_p (merge_set, test_use)
4195*38fd1498Szrj 	      || bitmap_intersect_p (merge_use, test_set))
4196*38fd1498Szrj 	    break;
4197*38fd1498Szrj 	  if (!HAVE_cc0 || !sets_cc0_p (insn))
4198*38fd1498Szrj 	    max_to = insn;
4199*38fd1498Szrj 	}
4200*38fd1498Szrj       next = NEXT_INSN (insn);
4201*38fd1498Szrj       if (insn == to)
4202*38fd1498Szrj 	break;
4203*38fd1498Szrj     }
4204*38fd1498Szrj   if (max_to != to)
4205*38fd1498Szrj     fail = 1;
4206*38fd1498Szrj 
4207*38fd1498Szrj   if (max_to == NULL_RTX || (fail && pmove_upto == NULL))
4208*38fd1498Szrj     goto out;
4209*38fd1498Szrj 
4210*38fd1498Szrj   /* Now, lower this upper bound by also taking into account that
4211*38fd1498Szrj      a range of insns moved across ACROSS must not leave a register
4212*38fd1498Szrj      live at the end that will be clobbered in ACROSS.  We need to
4213*38fd1498Szrj      find a point where TEST_SET & LIVE == 0.
4214*38fd1498Szrj 
4215*38fd1498Szrj      Insns in the MERGE range that set registers which are also set
4216*38fd1498Szrj      in the ACROSS range may still be moved as long as we also move
4217*38fd1498Szrj      later insns which use the results of the set, and make the
4218*38fd1498Szrj      register dead again.  This is verified by the condition stated
4219*38fd1498Szrj      above.  We only need to test it for registers that are set in
4220*38fd1498Szrj      the moved region.
4221*38fd1498Szrj 
4222*38fd1498Szrj      MERGE_LIVE is provided by the caller and holds live registers after
4223*38fd1498Szrj      TO.  */
4224*38fd1498Szrj   bitmap_copy (local_merge_live, merge_live);
4225*38fd1498Szrj   for (insn = to; insn != max_to; insn = PREV_INSN (insn))
4226*38fd1498Szrj     df_simulate_one_insn_backwards (merge_bb, insn, local_merge_live);
4227*38fd1498Szrj 
4228*38fd1498Szrj   /* We're not interested in registers that aren't set in the moved
4229*38fd1498Szrj      region at all.  */
4230*38fd1498Szrj   bitmap_and_into (local_merge_live, merge_set);
4231*38fd1498Szrj   for (;;)
4232*38fd1498Szrj     {
4233*38fd1498Szrj       if (NONDEBUG_INSN_P (insn))
4234*38fd1498Szrj 	{
4235*38fd1498Szrj 	  if (!bitmap_intersect_p (test_set, local_merge_live)
4236*38fd1498Szrj 	      && (!HAVE_cc0 || !sets_cc0_p (insn)))
4237*38fd1498Szrj 	    {
4238*38fd1498Szrj 	      max_to = insn;
4239*38fd1498Szrj 	      break;
4240*38fd1498Szrj 	    }
4241*38fd1498Szrj 
4242*38fd1498Szrj 	  df_simulate_one_insn_backwards (merge_bb, insn,
4243*38fd1498Szrj 					  local_merge_live);
4244*38fd1498Szrj 	}
4245*38fd1498Szrj       if (insn == from)
4246*38fd1498Szrj 	{
4247*38fd1498Szrj 	  fail = 1;
4248*38fd1498Szrj 	  goto out;
4249*38fd1498Szrj 	}
4250*38fd1498Szrj       insn = PREV_INSN (insn);
4251*38fd1498Szrj     }
4252*38fd1498Szrj 
4253*38fd1498Szrj   if (max_to != to)
4254*38fd1498Szrj     fail = 1;
4255*38fd1498Szrj 
4256*38fd1498Szrj   if (pmove_upto)
4257*38fd1498Szrj     *pmove_upto = max_to;
4258*38fd1498Szrj 
4259*38fd1498Szrj   /* For small register class machines, don't lengthen lifetimes of
4260*38fd1498Szrj      hard registers before reload.  */
4261*38fd1498Szrj   if (! reload_completed
4262*38fd1498Szrj       && targetm.small_register_classes_for_mode_p (VOIDmode))
4263*38fd1498Szrj     {
4264*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (merge_set, 0, i, bi)
4265*38fd1498Szrj 	{
4266*38fd1498Szrj 	  if (i < FIRST_PSEUDO_REGISTER
4267*38fd1498Szrj 	      && ! fixed_regs[i]
4268*38fd1498Szrj 	      && ! global_regs[i])
4269*38fd1498Szrj 	    {
4270*38fd1498Szrj 	      fail = 1;
4271*38fd1498Szrj 	      break;
4272*38fd1498Szrj 	    }
4273*38fd1498Szrj 	}
4274*38fd1498Szrj     }
4275*38fd1498Szrj 
4276*38fd1498Szrj  out:
4277*38fd1498Szrj   BITMAP_FREE (merge_set);
4278*38fd1498Szrj   BITMAP_FREE (merge_use);
4279*38fd1498Szrj   BITMAP_FREE (local_merge_live);
4280*38fd1498Szrj   BITMAP_FREE (test_set);
4281*38fd1498Szrj   BITMAP_FREE (test_use);
4282*38fd1498Szrj 
4283*38fd1498Szrj   return !fail;
4284*38fd1498Szrj }
4285*38fd1498Szrj 
4286*38fd1498Szrj 
4287*38fd1498Szrj /*----------------------------------------------------------------------------
4288*38fd1498Szrj    MULTIPLE DEFINITIONS
4289*38fd1498Szrj 
4290*38fd1498Szrj    Find the locations in the function reached by multiple definition sites
4291*38fd1498Szrj    for a live pseudo.  In and out bitvectors are built for each basic
4292*38fd1498Szrj    block.  They are restricted for efficiency to live registers.
4293*38fd1498Szrj 
4294*38fd1498Szrj    The gen and kill sets for the problem are obvious.  Together they
4295*38fd1498Szrj    include all defined registers in a basic block; the gen set includes
4296*38fd1498Szrj    registers where a partial or conditional or may-clobber definition is
4297*38fd1498Szrj    last in the BB, while the kill set includes registers with a complete
4298*38fd1498Szrj    definition coming last.  However, the computation of the dataflow
4299*38fd1498Szrj    itself is interesting.
4300*38fd1498Szrj 
4301*38fd1498Szrj    The idea behind it comes from SSA form's iterated dominance frontier
4302*38fd1498Szrj    criterion for inserting PHI functions.  Just like in that case, we can use
4303*38fd1498Szrj    the dominance frontier to find places where multiple definitions meet;
4304*38fd1498Szrj    a register X defined in a basic block BB1 has multiple definitions in
4305*38fd1498Szrj    basic blocks in BB1's dominance frontier.
4306*38fd1498Szrj 
4307*38fd1498Szrj    So, the in-set of a basic block BB2 is not just the union of the
4308*38fd1498Szrj    out-sets of BB2's predecessors, but includes some more bits that come
4309*38fd1498Szrj    from the basic blocks of whose dominance frontier BB2 is part (BB1 in
4310*38fd1498Szrj    the previous paragraph).  I called this set the init-set of BB2.
4311*38fd1498Szrj 
4312*38fd1498Szrj       (Note: I actually use the kill-set only to build the init-set.
4313*38fd1498Szrj       gen bits are anyway propagated from BB1 to BB2 by dataflow).
4314*38fd1498Szrj 
4315*38fd1498Szrj     For example, if you have
4316*38fd1498Szrj 
4317*38fd1498Szrj        BB1 : r10 = 0
4318*38fd1498Szrj              r11 = 0
4319*38fd1498Szrj              if <...> goto BB2 else goto BB3;
4320*38fd1498Szrj 
4321*38fd1498Szrj        BB2 : r10 = 1
4322*38fd1498Szrj              r12 = 1
4323*38fd1498Szrj              goto BB3;
4324*38fd1498Szrj 
4325*38fd1498Szrj        BB3 :
4326*38fd1498Szrj 
4327*38fd1498Szrj     you have BB3 in BB2's dominance frontier but not in BB1's, so that the
4328*38fd1498Szrj     init-set of BB3 includes r10 and r12, but not r11.  Note that we do
4329*38fd1498Szrj     not need to iterate the dominance frontier, because we do not insert
4330*38fd1498Szrj     anything like PHI functions there!  Instead, dataflow will take care of
4331*38fd1498Szrj     propagating the information to BB3's successors.
4332*38fd1498Szrj    ---------------------------------------------------------------------------*/
4333*38fd1498Szrj 
4334*38fd1498Szrj /* Private data used to verify the solution for this problem.  */
4335*38fd1498Szrj struct df_md_problem_data
4336*38fd1498Szrj {
4337*38fd1498Szrj   /* An obstack for the bitmaps we need for this problem.  */
4338*38fd1498Szrj   bitmap_obstack md_bitmaps;
4339*38fd1498Szrj };
4340*38fd1498Szrj 
4341*38fd1498Szrj /* Scratch var used by transfer functions.  This is used to do md analysis
4342*38fd1498Szrj    only for live registers.  */
4343*38fd1498Szrj static bitmap_head df_md_scratch;
4344*38fd1498Szrj 
4345*38fd1498Szrj 
4346*38fd1498Szrj static void
df_md_free_bb_info(basic_block bb ATTRIBUTE_UNUSED,void * vbb_info)4347*38fd1498Szrj df_md_free_bb_info (basic_block bb ATTRIBUTE_UNUSED,
4348*38fd1498Szrj                     void *vbb_info)
4349*38fd1498Szrj {
4350*38fd1498Szrj   struct df_md_bb_info *bb_info = (struct df_md_bb_info *) vbb_info;
4351*38fd1498Szrj   if (bb_info)
4352*38fd1498Szrj     {
4353*38fd1498Szrj       bitmap_clear (&bb_info->kill);
4354*38fd1498Szrj       bitmap_clear (&bb_info->gen);
4355*38fd1498Szrj       bitmap_clear (&bb_info->init);
4356*38fd1498Szrj       bitmap_clear (&bb_info->in);
4357*38fd1498Szrj       bitmap_clear (&bb_info->out);
4358*38fd1498Szrj     }
4359*38fd1498Szrj }
4360*38fd1498Szrj 
4361*38fd1498Szrj 
4362*38fd1498Szrj /* Allocate or reset bitmaps for DF_MD. The solution bits are
4363*38fd1498Szrj    not touched unless the block is new.  */
4364*38fd1498Szrj 
4365*38fd1498Szrj static void
df_md_alloc(bitmap all_blocks)4366*38fd1498Szrj df_md_alloc (bitmap all_blocks)
4367*38fd1498Szrj {
4368*38fd1498Szrj   unsigned int bb_index;
4369*38fd1498Szrj   bitmap_iterator bi;
4370*38fd1498Szrj   struct df_md_problem_data *problem_data;
4371*38fd1498Szrj 
4372*38fd1498Szrj   df_grow_bb_info (df_md);
4373*38fd1498Szrj   if (df_md->problem_data)
4374*38fd1498Szrj     problem_data = (struct df_md_problem_data *) df_md->problem_data;
4375*38fd1498Szrj   else
4376*38fd1498Szrj     {
4377*38fd1498Szrj       problem_data = XNEW (struct df_md_problem_data);
4378*38fd1498Szrj       df_md->problem_data = problem_data;
4379*38fd1498Szrj       bitmap_obstack_initialize (&problem_data->md_bitmaps);
4380*38fd1498Szrj     }
4381*38fd1498Szrj   bitmap_initialize (&df_md_scratch, &problem_data->md_bitmaps);
4382*38fd1498Szrj 
4383*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4384*38fd1498Szrj     {
4385*38fd1498Szrj       struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4386*38fd1498Szrj       /* When bitmaps are already initialized, just clear them.  */
4387*38fd1498Szrj       if (bb_info->init.obstack)
4388*38fd1498Szrj         {
4389*38fd1498Szrj           bitmap_clear (&bb_info->init);
4390*38fd1498Szrj           bitmap_clear (&bb_info->gen);
4391*38fd1498Szrj           bitmap_clear (&bb_info->kill);
4392*38fd1498Szrj           bitmap_clear (&bb_info->in);
4393*38fd1498Szrj           bitmap_clear (&bb_info->out);
4394*38fd1498Szrj         }
4395*38fd1498Szrj       else
4396*38fd1498Szrj         {
4397*38fd1498Szrj 	  bitmap_initialize (&bb_info->init, &problem_data->md_bitmaps);
4398*38fd1498Szrj 	  bitmap_initialize (&bb_info->gen, &problem_data->md_bitmaps);
4399*38fd1498Szrj 	  bitmap_initialize (&bb_info->kill, &problem_data->md_bitmaps);
4400*38fd1498Szrj 	  bitmap_initialize (&bb_info->in, &problem_data->md_bitmaps);
4401*38fd1498Szrj 	  bitmap_initialize (&bb_info->out, &problem_data->md_bitmaps);
4402*38fd1498Szrj         }
4403*38fd1498Szrj     }
4404*38fd1498Szrj 
4405*38fd1498Szrj   df_md->optional_p = true;
4406*38fd1498Szrj }
4407*38fd1498Szrj 
4408*38fd1498Szrj /* Add the effect of the top artificial defs of BB to the multiple definitions
4409*38fd1498Szrj    bitmap LOCAL_MD.  */
4410*38fd1498Szrj 
4411*38fd1498Szrj void
df_md_simulate_artificial_defs_at_top(basic_block bb,bitmap local_md)4412*38fd1498Szrj df_md_simulate_artificial_defs_at_top (basic_block bb, bitmap local_md)
4413*38fd1498Szrj {
4414*38fd1498Szrj   int bb_index = bb->index;
4415*38fd1498Szrj   df_ref def;
4416*38fd1498Szrj   FOR_EACH_ARTIFICIAL_DEF (def, bb_index)
4417*38fd1498Szrj     if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
4418*38fd1498Szrj       {
4419*38fd1498Szrj 	unsigned int dregno = DF_REF_REGNO (def);
4420*38fd1498Szrj 	if (DF_REF_FLAGS (def)
4421*38fd1498Szrj 	    & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4422*38fd1498Szrj 	  bitmap_set_bit (local_md, dregno);
4423*38fd1498Szrj 	else
4424*38fd1498Szrj 	  bitmap_clear_bit (local_md, dregno);
4425*38fd1498Szrj       }
4426*38fd1498Szrj }
4427*38fd1498Szrj 
4428*38fd1498Szrj 
4429*38fd1498Szrj /* Add the effect of the defs of INSN to the reaching definitions bitmap
4430*38fd1498Szrj    LOCAL_MD.  */
4431*38fd1498Szrj 
4432*38fd1498Szrj void
df_md_simulate_one_insn(basic_block bb ATTRIBUTE_UNUSED,rtx_insn * insn,bitmap local_md)4433*38fd1498Szrj df_md_simulate_one_insn (basic_block bb ATTRIBUTE_UNUSED, rtx_insn *insn,
4434*38fd1498Szrj 			 bitmap local_md)
4435*38fd1498Szrj {
4436*38fd1498Szrj   df_ref def;
4437*38fd1498Szrj 
4438*38fd1498Szrj   FOR_EACH_INSN_DEF (def, insn)
4439*38fd1498Szrj     {
4440*38fd1498Szrj       unsigned int dregno = DF_REF_REGNO (def);
4441*38fd1498Szrj       if ((!(df->changeable_flags & DF_NO_HARD_REGS))
4442*38fd1498Szrj           || (dregno >= FIRST_PSEUDO_REGISTER))
4443*38fd1498Szrj         {
4444*38fd1498Szrj           if (DF_REF_FLAGS (def)
4445*38fd1498Szrj 	      & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4446*38fd1498Szrj            bitmap_set_bit (local_md, DF_REF_ID (def));
4447*38fd1498Szrj          else
4448*38fd1498Szrj            bitmap_clear_bit (local_md, DF_REF_ID (def));
4449*38fd1498Szrj         }
4450*38fd1498Szrj     }
4451*38fd1498Szrj }
4452*38fd1498Szrj 
4453*38fd1498Szrj static void
df_md_bb_local_compute_process_def(struct df_md_bb_info * bb_info,df_ref def,int top_flag)4454*38fd1498Szrj df_md_bb_local_compute_process_def (struct df_md_bb_info *bb_info,
4455*38fd1498Szrj                                     df_ref def,
4456*38fd1498Szrj                                     int top_flag)
4457*38fd1498Szrj {
4458*38fd1498Szrj   bitmap_clear (&seen_in_insn);
4459*38fd1498Szrj 
4460*38fd1498Szrj   for (; def; def = DF_REF_NEXT_LOC (def))
4461*38fd1498Szrj     {
4462*38fd1498Szrj       unsigned int dregno = DF_REF_REGNO (def);
4463*38fd1498Szrj       if (((!(df->changeable_flags & DF_NO_HARD_REGS))
4464*38fd1498Szrj 	    || (dregno >= FIRST_PSEUDO_REGISTER))
4465*38fd1498Szrj 	  && top_flag == (DF_REF_FLAGS (def) & DF_REF_AT_TOP))
4466*38fd1498Szrj 	{
4467*38fd1498Szrj           if (!bitmap_bit_p (&seen_in_insn, dregno))
4468*38fd1498Szrj 	    {
4469*38fd1498Szrj 	      if (DF_REF_FLAGS (def)
4470*38fd1498Szrj 	          & (DF_REF_PARTIAL | DF_REF_CONDITIONAL | DF_REF_MAY_CLOBBER))
4471*38fd1498Szrj 	        {
4472*38fd1498Szrj 	          bitmap_set_bit (&bb_info->gen, dregno);
4473*38fd1498Szrj 	          bitmap_clear_bit (&bb_info->kill, dregno);
4474*38fd1498Szrj 	        }
4475*38fd1498Szrj 	      else
4476*38fd1498Szrj 	        {
4477*38fd1498Szrj 		  /* When we find a clobber and a regular def,
4478*38fd1498Szrj 		     make sure the regular def wins.  */
4479*38fd1498Szrj 	          bitmap_set_bit (&seen_in_insn, dregno);
4480*38fd1498Szrj 	          bitmap_set_bit (&bb_info->kill, dregno);
4481*38fd1498Szrj 	          bitmap_clear_bit (&bb_info->gen, dregno);
4482*38fd1498Szrj 	        }
4483*38fd1498Szrj 	    }
4484*38fd1498Szrj 	}
4485*38fd1498Szrj     }
4486*38fd1498Szrj }
4487*38fd1498Szrj 
4488*38fd1498Szrj 
4489*38fd1498Szrj /* Compute local multiple def info for basic block BB.  */
4490*38fd1498Szrj 
4491*38fd1498Szrj static void
df_md_bb_local_compute(unsigned int bb_index)4492*38fd1498Szrj df_md_bb_local_compute (unsigned int bb_index)
4493*38fd1498Szrj {
4494*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4495*38fd1498Szrj   struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4496*38fd1498Szrj   rtx_insn *insn;
4497*38fd1498Szrj 
4498*38fd1498Szrj   /* Artificials are only hard regs.  */
4499*38fd1498Szrj   if (!(df->changeable_flags & DF_NO_HARD_REGS))
4500*38fd1498Szrj     df_md_bb_local_compute_process_def (bb_info,
4501*38fd1498Szrj                                         df_get_artificial_defs (bb_index),
4502*38fd1498Szrj                                         DF_REF_AT_TOP);
4503*38fd1498Szrj 
4504*38fd1498Szrj   FOR_BB_INSNS (bb, insn)
4505*38fd1498Szrj     {
4506*38fd1498Szrj       unsigned int uid = INSN_UID (insn);
4507*38fd1498Szrj       if (!INSN_P (insn))
4508*38fd1498Szrj         continue;
4509*38fd1498Szrj 
4510*38fd1498Szrj       df_md_bb_local_compute_process_def (bb_info, DF_INSN_UID_DEFS (uid), 0);
4511*38fd1498Szrj     }
4512*38fd1498Szrj 
4513*38fd1498Szrj   if (!(df->changeable_flags & DF_NO_HARD_REGS))
4514*38fd1498Szrj     df_md_bb_local_compute_process_def (bb_info,
4515*38fd1498Szrj                                         df_get_artificial_defs (bb_index),
4516*38fd1498Szrj                                         0);
4517*38fd1498Szrj }
4518*38fd1498Szrj 
4519*38fd1498Szrj /* Compute local reaching def info for each basic block within BLOCKS.  */
4520*38fd1498Szrj 
4521*38fd1498Szrj static void
df_md_local_compute(bitmap all_blocks)4522*38fd1498Szrj df_md_local_compute (bitmap all_blocks)
4523*38fd1498Szrj {
4524*38fd1498Szrj   unsigned int bb_index, df_bb_index;
4525*38fd1498Szrj   bitmap_iterator bi1, bi2;
4526*38fd1498Szrj   basic_block bb;
4527*38fd1498Szrj   bitmap_head *frontiers;
4528*38fd1498Szrj 
4529*38fd1498Szrj   bitmap_initialize (&seen_in_insn, &bitmap_default_obstack);
4530*38fd1498Szrj 
4531*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4532*38fd1498Szrj     {
4533*38fd1498Szrj       df_md_bb_local_compute (bb_index);
4534*38fd1498Szrj     }
4535*38fd1498Szrj 
4536*38fd1498Szrj   bitmap_clear (&seen_in_insn);
4537*38fd1498Szrj 
4538*38fd1498Szrj   frontiers = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
4539*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
4540*38fd1498Szrj     bitmap_initialize (&frontiers[bb->index], &bitmap_default_obstack);
4541*38fd1498Szrj 
4542*38fd1498Szrj   compute_dominance_frontiers (frontiers);
4543*38fd1498Szrj 
4544*38fd1498Szrj   /* Add each basic block's kills to the nodes in the frontier of the BB.  */
4545*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi1)
4546*38fd1498Szrj     {
4547*38fd1498Szrj       bitmap kill = &df_md_get_bb_info (bb_index)->kill;
4548*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (&frontiers[bb_index], 0, df_bb_index, bi2)
4549*38fd1498Szrj 	{
4550*38fd1498Szrj 	  basic_block bb = BASIC_BLOCK_FOR_FN (cfun, df_bb_index);
4551*38fd1498Szrj 	  if (bitmap_bit_p (all_blocks, df_bb_index))
4552*38fd1498Szrj 	    bitmap_ior_and_into (&df_md_get_bb_info (df_bb_index)->init, kill,
4553*38fd1498Szrj 				 df_get_live_in (bb));
4554*38fd1498Szrj 	}
4555*38fd1498Szrj     }
4556*38fd1498Szrj 
4557*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
4558*38fd1498Szrj     bitmap_clear (&frontiers[bb->index]);
4559*38fd1498Szrj   free (frontiers);
4560*38fd1498Szrj }
4561*38fd1498Szrj 
4562*38fd1498Szrj 
4563*38fd1498Szrj /* Reset the global solution for recalculation.  */
4564*38fd1498Szrj 
4565*38fd1498Szrj static void
df_md_reset(bitmap all_blocks)4566*38fd1498Szrj df_md_reset (bitmap all_blocks)
4567*38fd1498Szrj {
4568*38fd1498Szrj   unsigned int bb_index;
4569*38fd1498Szrj   bitmap_iterator bi;
4570*38fd1498Szrj 
4571*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4572*38fd1498Szrj     {
4573*38fd1498Szrj       struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4574*38fd1498Szrj       gcc_assert (bb_info);
4575*38fd1498Szrj       bitmap_clear (&bb_info->in);
4576*38fd1498Szrj       bitmap_clear (&bb_info->out);
4577*38fd1498Szrj     }
4578*38fd1498Szrj }
4579*38fd1498Szrj 
4580*38fd1498Szrj static bool
df_md_transfer_function(int bb_index)4581*38fd1498Szrj df_md_transfer_function (int bb_index)
4582*38fd1498Szrj {
4583*38fd1498Szrj   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
4584*38fd1498Szrj   struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4585*38fd1498Szrj   bitmap in = &bb_info->in;
4586*38fd1498Szrj   bitmap out = &bb_info->out;
4587*38fd1498Szrj   bitmap gen = &bb_info->gen;
4588*38fd1498Szrj   bitmap kill = &bb_info->kill;
4589*38fd1498Szrj 
4590*38fd1498Szrj   /* We need to use a scratch set here so that the value returned from this
4591*38fd1498Szrj      function invocation properly reflects whether the sets changed in a
4592*38fd1498Szrj      significant way; i.e. not just because the live set was anded in.  */
4593*38fd1498Szrj   bitmap_and (&df_md_scratch, gen, df_get_live_out (bb));
4594*38fd1498Szrj 
4595*38fd1498Szrj   /* Multiple definitions of a register are not relevant if it is not
4596*38fd1498Szrj      live.  Thus we trim the result to the places where it is live.  */
4597*38fd1498Szrj   bitmap_and_into (in, df_get_live_in (bb));
4598*38fd1498Szrj 
4599*38fd1498Szrj   return bitmap_ior_and_compl (out, &df_md_scratch, in, kill);
4600*38fd1498Szrj }
4601*38fd1498Szrj 
4602*38fd1498Szrj /* Initialize the solution bit vectors for problem.  */
4603*38fd1498Szrj 
4604*38fd1498Szrj static void
df_md_init(bitmap all_blocks)4605*38fd1498Szrj df_md_init (bitmap all_blocks)
4606*38fd1498Szrj {
4607*38fd1498Szrj   unsigned int bb_index;
4608*38fd1498Szrj   bitmap_iterator bi;
4609*38fd1498Szrj 
4610*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (all_blocks, 0, bb_index, bi)
4611*38fd1498Szrj     {
4612*38fd1498Szrj       struct df_md_bb_info *bb_info = df_md_get_bb_info (bb_index);
4613*38fd1498Szrj 
4614*38fd1498Szrj       bitmap_copy (&bb_info->in, &bb_info->init);
4615*38fd1498Szrj       df_md_transfer_function (bb_index);
4616*38fd1498Szrj     }
4617*38fd1498Szrj }
4618*38fd1498Szrj 
4619*38fd1498Szrj static void
df_md_confluence_0(basic_block bb)4620*38fd1498Szrj df_md_confluence_0 (basic_block bb)
4621*38fd1498Szrj {
4622*38fd1498Szrj   struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4623*38fd1498Szrj   bitmap_copy (&bb_info->in, &bb_info->init);
4624*38fd1498Szrj }
4625*38fd1498Szrj 
4626*38fd1498Szrj /* In of target gets or of out of source.  */
4627*38fd1498Szrj 
4628*38fd1498Szrj static bool
df_md_confluence_n(edge e)4629*38fd1498Szrj df_md_confluence_n (edge e)
4630*38fd1498Szrj {
4631*38fd1498Szrj   bitmap op1 = &df_md_get_bb_info (e->dest->index)->in;
4632*38fd1498Szrj   bitmap op2 = &df_md_get_bb_info (e->src->index)->out;
4633*38fd1498Szrj 
4634*38fd1498Szrj   if (e->flags & EDGE_FAKE)
4635*38fd1498Szrj     return false;
4636*38fd1498Szrj 
4637*38fd1498Szrj   if (e->flags & EDGE_EH)
4638*38fd1498Szrj     return bitmap_ior_and_compl_into (op1, op2,
4639*38fd1498Szrj 				      regs_invalidated_by_call_regset);
4640*38fd1498Szrj   else
4641*38fd1498Szrj     return bitmap_ior_into (op1, op2);
4642*38fd1498Szrj }
4643*38fd1498Szrj 
4644*38fd1498Szrj /* Free all storage associated with the problem.  */
4645*38fd1498Szrj 
4646*38fd1498Szrj static void
df_md_free(void)4647*38fd1498Szrj df_md_free (void)
4648*38fd1498Szrj {
4649*38fd1498Szrj   struct df_md_problem_data *problem_data
4650*38fd1498Szrj     = (struct df_md_problem_data *) df_md->problem_data;
4651*38fd1498Szrj 
4652*38fd1498Szrj   bitmap_obstack_release (&problem_data->md_bitmaps);
4653*38fd1498Szrj   free (problem_data);
4654*38fd1498Szrj   df_md->problem_data = NULL;
4655*38fd1498Szrj 
4656*38fd1498Szrj   df_md->block_info_size = 0;
4657*38fd1498Szrj   free (df_md->block_info);
4658*38fd1498Szrj   df_md->block_info = NULL;
4659*38fd1498Szrj   free (df_md);
4660*38fd1498Szrj }
4661*38fd1498Szrj 
4662*38fd1498Szrj 
4663*38fd1498Szrj /* Debugging info at top of bb.  */
4664*38fd1498Szrj 
4665*38fd1498Szrj static void
df_md_top_dump(basic_block bb,FILE * file)4666*38fd1498Szrj df_md_top_dump (basic_block bb, FILE *file)
4667*38fd1498Szrj {
4668*38fd1498Szrj   struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4669*38fd1498Szrj   if (!bb_info)
4670*38fd1498Szrj     return;
4671*38fd1498Szrj 
4672*38fd1498Szrj   fprintf (file, ";; md  in  \t");
4673*38fd1498Szrj   df_print_regset (file, &bb_info->in);
4674*38fd1498Szrj   fprintf (file, ";; md  init  \t");
4675*38fd1498Szrj   df_print_regset (file, &bb_info->init);
4676*38fd1498Szrj   fprintf (file, ";; md  gen \t");
4677*38fd1498Szrj   df_print_regset (file, &bb_info->gen);
4678*38fd1498Szrj   fprintf (file, ";; md  kill \t");
4679*38fd1498Szrj   df_print_regset (file, &bb_info->kill);
4680*38fd1498Szrj }
4681*38fd1498Szrj 
4682*38fd1498Szrj /* Debugging info at bottom of bb.  */
4683*38fd1498Szrj 
4684*38fd1498Szrj static void
df_md_bottom_dump(basic_block bb,FILE * file)4685*38fd1498Szrj df_md_bottom_dump (basic_block bb, FILE *file)
4686*38fd1498Szrj {
4687*38fd1498Szrj   struct df_md_bb_info *bb_info = df_md_get_bb_info (bb->index);
4688*38fd1498Szrj   if (!bb_info)
4689*38fd1498Szrj     return;
4690*38fd1498Szrj 
4691*38fd1498Szrj   fprintf (file, ";; md  out \t");
4692*38fd1498Szrj   df_print_regset (file, &bb_info->out);
4693*38fd1498Szrj }
4694*38fd1498Szrj 
4695*38fd1498Szrj static const struct df_problem problem_MD =
4696*38fd1498Szrj {
4697*38fd1498Szrj   DF_MD,                      /* Problem id.  */
4698*38fd1498Szrj   DF_FORWARD,                 /* Direction.  */
4699*38fd1498Szrj   df_md_alloc,                /* Allocate the problem specific data.  */
4700*38fd1498Szrj   df_md_reset,                /* Reset global information.  */
4701*38fd1498Szrj   df_md_free_bb_info,         /* Free basic block info.  */
4702*38fd1498Szrj   df_md_local_compute,        /* Local compute function.  */
4703*38fd1498Szrj   df_md_init,                 /* Init the solution specific data.  */
4704*38fd1498Szrj   df_worklist_dataflow,       /* Worklist solver.  */
4705*38fd1498Szrj   df_md_confluence_0,         /* Confluence operator 0.  */
4706*38fd1498Szrj   df_md_confluence_n,         /* Confluence operator n.  */
4707*38fd1498Szrj   df_md_transfer_function,    /* Transfer function.  */
4708*38fd1498Szrj   NULL,                       /* Finalize function.  */
4709*38fd1498Szrj   df_md_free,                 /* Free all of the problem information.  */
4710*38fd1498Szrj   df_md_free,                 /* Remove this problem from the stack of dataflow problems.  */
4711*38fd1498Szrj   NULL,                       /* Debugging.  */
4712*38fd1498Szrj   df_md_top_dump,             /* Debugging start block.  */
4713*38fd1498Szrj   df_md_bottom_dump,          /* Debugging end block.  */
4714*38fd1498Szrj   NULL,                       /* Debugging start insn.  */
4715*38fd1498Szrj   NULL,                       /* Debugging end insn.  */
4716*38fd1498Szrj   NULL,			      /* Incremental solution verify start.  */
4717*38fd1498Szrj   NULL,			      /* Incremental solution verify end.  */
4718*38fd1498Szrj   NULL,                       /* Dependent problem.  */
4719*38fd1498Szrj   sizeof (struct df_md_bb_info),/* Size of entry of block_info array.  */
4720*38fd1498Szrj   TV_DF_MD,                   /* Timing variable.  */
4721*38fd1498Szrj   false                       /* Reset blocks on dropping out of blocks_to_analyze.  */
4722*38fd1498Szrj };
4723*38fd1498Szrj 
4724*38fd1498Szrj /* Create a new MD instance and add it to the existing instance
4725*38fd1498Szrj    of DF.  */
4726*38fd1498Szrj 
4727*38fd1498Szrj void
df_md_add_problem(void)4728*38fd1498Szrj df_md_add_problem (void)
4729*38fd1498Szrj {
4730*38fd1498Szrj   df_add_problem (&problem_MD);
4731*38fd1498Szrj }
4732*38fd1498Szrj 
4733*38fd1498Szrj 
4734*38fd1498Szrj 
4735