xref: /dflybsd-src/contrib/gcc-8.0/gcc/lcm.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Generic partial redundancy elimination with lazy code motion support.
2*38fd1498Szrj    Copyright (C) 1998-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj /* These routines are meant to be used by various optimization
21*38fd1498Szrj    passes which can be modeled as lazy code motion problems.
22*38fd1498Szrj    Including, but not limited to:
23*38fd1498Szrj 
24*38fd1498Szrj 	* Traditional partial redundancy elimination.
25*38fd1498Szrj 
26*38fd1498Szrj 	* Placement of caller/caller register save/restores.
27*38fd1498Szrj 
28*38fd1498Szrj 	* Load/store motion.
29*38fd1498Szrj 
30*38fd1498Szrj 	* Copy motion.
31*38fd1498Szrj 
32*38fd1498Szrj 	* Conversion of flat register files to a stacked register
33*38fd1498Szrj 	model.
34*38fd1498Szrj 
35*38fd1498Szrj 	* Dead load/store elimination.
36*38fd1498Szrj 
37*38fd1498Szrj   These routines accept as input:
38*38fd1498Szrj 
39*38fd1498Szrj 	* Basic block information (number of blocks, lists of
40*38fd1498Szrj 	predecessors and successors).  Note the granularity
41*38fd1498Szrj 	does not need to be basic block, they could be statements
42*38fd1498Szrj 	or functions.
43*38fd1498Szrj 
44*38fd1498Szrj 	* Bitmaps of local properties (computed, transparent and
45*38fd1498Szrj 	anticipatable expressions).
46*38fd1498Szrj 
47*38fd1498Szrj   The output of these routines is bitmap of redundant computations
48*38fd1498Szrj   and a bitmap of optimal placement points.  */
49*38fd1498Szrj 
50*38fd1498Szrj 
51*38fd1498Szrj #include "config.h"
52*38fd1498Szrj #include "system.h"
53*38fd1498Szrj #include "coretypes.h"
54*38fd1498Szrj #include "backend.h"
55*38fd1498Szrj #include "cfganal.h"
56*38fd1498Szrj #include "lcm.h"
57*38fd1498Szrj 
58*38fd1498Szrj /* Edge based LCM routines.  */
59*38fd1498Szrj static void compute_antinout_edge (sbitmap *, sbitmap *, sbitmap *, sbitmap *);
60*38fd1498Szrj static void compute_earliest (struct edge_list *, int, sbitmap *, sbitmap *,
61*38fd1498Szrj 			      sbitmap *, sbitmap *, sbitmap *);
62*38fd1498Szrj static void compute_laterin (struct edge_list *, sbitmap *, sbitmap *,
63*38fd1498Szrj 			     sbitmap *, sbitmap *);
64*38fd1498Szrj static void compute_insert_delete (struct edge_list *edge_list, sbitmap *,
65*38fd1498Szrj 				   sbitmap *, sbitmap *, sbitmap *, sbitmap *);
66*38fd1498Szrj 
67*38fd1498Szrj /* Edge based LCM routines on a reverse flowgraph.  */
68*38fd1498Szrj static void compute_farthest (struct edge_list *, int, sbitmap *, sbitmap *,
69*38fd1498Szrj 			      sbitmap*, sbitmap *, sbitmap *);
70*38fd1498Szrj static void compute_nearerout (struct edge_list *, sbitmap *, sbitmap *,
71*38fd1498Szrj 			       sbitmap *, sbitmap *);
72*38fd1498Szrj static void compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *,
73*38fd1498Szrj 				       sbitmap *, sbitmap *, sbitmap *,
74*38fd1498Szrj 				       sbitmap *);
75*38fd1498Szrj 
76*38fd1498Szrj /* Edge based lcm routines.  */
77*38fd1498Szrj 
78*38fd1498Szrj /* Compute expression anticipatability at entrance and exit of each block.
79*38fd1498Szrj    This is done based on the flow graph, and not on the pred-succ lists.
80*38fd1498Szrj    Other than that, its pretty much identical to compute_antinout.  */
81*38fd1498Szrj 
82*38fd1498Szrj static void
compute_antinout_edge(sbitmap * antloc,sbitmap * transp,sbitmap * antin,sbitmap * antout)83*38fd1498Szrj compute_antinout_edge (sbitmap *antloc, sbitmap *transp, sbitmap *antin,
84*38fd1498Szrj 		       sbitmap *antout)
85*38fd1498Szrj {
86*38fd1498Szrj   basic_block bb;
87*38fd1498Szrj   edge e;
88*38fd1498Szrj   basic_block *worklist, *qin, *qout, *qend;
89*38fd1498Szrj   unsigned int qlen;
90*38fd1498Szrj   edge_iterator ei;
91*38fd1498Szrj 
92*38fd1498Szrj   /* Allocate a worklist array/queue.  Entries are only added to the
93*38fd1498Szrj      list if they were not already on the list.  So the size is
94*38fd1498Szrj      bounded by the number of basic blocks.  */
95*38fd1498Szrj   qin = qout = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
96*38fd1498Szrj 
97*38fd1498Szrj   /* We want a maximal solution, so make an optimistic initialization of
98*38fd1498Szrj      ANTIN.  */
99*38fd1498Szrj   bitmap_vector_ones (antin, last_basic_block_for_fn (cfun));
100*38fd1498Szrj 
101*38fd1498Szrj   /* Put every block on the worklist; this is necessary because of the
102*38fd1498Szrj      optimistic initialization of ANTIN above.  */
103*38fd1498Szrj   int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
104*38fd1498Szrj   int postorder_num = post_order_compute (postorder, false, false);
105*38fd1498Szrj   for (int i = 0; i < postorder_num; ++i)
106*38fd1498Szrj     {
107*38fd1498Szrj       bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
108*38fd1498Szrj       *qin++ = bb;
109*38fd1498Szrj       bb->aux = bb;
110*38fd1498Szrj     }
111*38fd1498Szrj   free (postorder);
112*38fd1498Szrj 
113*38fd1498Szrj   qin = worklist;
114*38fd1498Szrj   qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
115*38fd1498Szrj   qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
116*38fd1498Szrj 
117*38fd1498Szrj   /* Mark blocks which are predecessors of the exit block so that we
118*38fd1498Szrj      can easily identify them below.  */
119*38fd1498Szrj   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
120*38fd1498Szrj     e->src->aux = EXIT_BLOCK_PTR_FOR_FN (cfun);
121*38fd1498Szrj 
122*38fd1498Szrj   /* Iterate until the worklist is empty.  */
123*38fd1498Szrj   while (qlen)
124*38fd1498Szrj     {
125*38fd1498Szrj       /* Take the first entry off the worklist.  */
126*38fd1498Szrj       bb = *qout++;
127*38fd1498Szrj       qlen--;
128*38fd1498Szrj 
129*38fd1498Szrj       if (qout >= qend)
130*38fd1498Szrj 	qout = worklist;
131*38fd1498Szrj 
132*38fd1498Szrj       if (bb->aux == EXIT_BLOCK_PTR_FOR_FN (cfun))
133*38fd1498Szrj 	/* Do not clear the aux field for blocks which are predecessors of
134*38fd1498Szrj 	   the EXIT block.  That way we never add then to the worklist
135*38fd1498Szrj 	   again.  */
136*38fd1498Szrj 	bitmap_clear (antout[bb->index]);
137*38fd1498Szrj       else
138*38fd1498Szrj 	{
139*38fd1498Szrj 	  /* Clear the aux field of this block so that it can be added to
140*38fd1498Szrj 	     the worklist again if necessary.  */
141*38fd1498Szrj 	  bb->aux = NULL;
142*38fd1498Szrj 	  bitmap_intersection_of_succs (antout[bb->index], antin, bb);
143*38fd1498Szrj 	}
144*38fd1498Szrj 
145*38fd1498Szrj       if (bitmap_or_and (antin[bb->index], antloc[bb->index],
146*38fd1498Szrj 				   transp[bb->index], antout[bb->index]))
147*38fd1498Szrj 	/* If the in state of this block changed, then we need
148*38fd1498Szrj 	   to add the predecessors of this block to the worklist
149*38fd1498Szrj 	   if they are not already on the worklist.  */
150*38fd1498Szrj 	FOR_EACH_EDGE (e, ei, bb->preds)
151*38fd1498Szrj 	  if (!e->src->aux && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
152*38fd1498Szrj 	    {
153*38fd1498Szrj 	      *qin++ = e->src;
154*38fd1498Szrj 	      e->src->aux = e;
155*38fd1498Szrj 	      qlen++;
156*38fd1498Szrj 	      if (qin >= qend)
157*38fd1498Szrj 		qin = worklist;
158*38fd1498Szrj 	    }
159*38fd1498Szrj     }
160*38fd1498Szrj 
161*38fd1498Szrj   clear_aux_for_edges ();
162*38fd1498Szrj   clear_aux_for_blocks ();
163*38fd1498Szrj   free (worklist);
164*38fd1498Szrj }
165*38fd1498Szrj 
166*38fd1498Szrj /* Compute the earliest vector for edge based lcm.  */
167*38fd1498Szrj 
168*38fd1498Szrj static void
compute_earliest(struct edge_list * edge_list,int n_exprs,sbitmap * antin,sbitmap * antout,sbitmap * avout,sbitmap * kill,sbitmap * earliest)169*38fd1498Szrj compute_earliest (struct edge_list *edge_list, int n_exprs, sbitmap *antin,
170*38fd1498Szrj 		  sbitmap *antout, sbitmap *avout, sbitmap *kill,
171*38fd1498Szrj 		  sbitmap *earliest)
172*38fd1498Szrj {
173*38fd1498Szrj   int x, num_edges;
174*38fd1498Szrj   basic_block pred, succ;
175*38fd1498Szrj 
176*38fd1498Szrj   num_edges = NUM_EDGES (edge_list);
177*38fd1498Szrj 
178*38fd1498Szrj   auto_sbitmap difference (n_exprs), temp_bitmap (n_exprs);
179*38fd1498Szrj   for (x = 0; x < num_edges; x++)
180*38fd1498Szrj     {
181*38fd1498Szrj       pred = INDEX_EDGE_PRED_BB (edge_list, x);
182*38fd1498Szrj       succ = INDEX_EDGE_SUCC_BB (edge_list, x);
183*38fd1498Szrj       if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
184*38fd1498Szrj 	bitmap_copy (earliest[x], antin[succ->index]);
185*38fd1498Szrj       else
186*38fd1498Szrj 	{
187*38fd1498Szrj 	  if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
188*38fd1498Szrj 	    bitmap_clear (earliest[x]);
189*38fd1498Szrj 	  else
190*38fd1498Szrj 	    {
191*38fd1498Szrj 	      bitmap_and_compl (difference, antin[succ->index],
192*38fd1498Szrj 				  avout[pred->index]);
193*38fd1498Szrj 	      bitmap_not (temp_bitmap, antout[pred->index]);
194*38fd1498Szrj 	      bitmap_and_or (earliest[x], difference,
195*38fd1498Szrj 				    kill[pred->index], temp_bitmap);
196*38fd1498Szrj 	    }
197*38fd1498Szrj 	}
198*38fd1498Szrj     }
199*38fd1498Szrj }
200*38fd1498Szrj 
201*38fd1498Szrj /* later(p,s) is dependent on the calculation of laterin(p).
202*38fd1498Szrj    laterin(p) is dependent on the calculation of later(p2,p).
203*38fd1498Szrj 
204*38fd1498Szrj      laterin(ENTRY) is defined as all 0's
205*38fd1498Szrj      later(ENTRY, succs(ENTRY)) are defined using laterin(ENTRY)
206*38fd1498Szrj      laterin(succs(ENTRY)) is defined by later(ENTRY, succs(ENTRY)).
207*38fd1498Szrj 
208*38fd1498Szrj    If we progress in this manner, starting with all basic blocks
209*38fd1498Szrj    in the work list, anytime we change later(bb), we need to add
210*38fd1498Szrj    succs(bb) to the worklist if they are not already on the worklist.
211*38fd1498Szrj 
212*38fd1498Szrj    Boundary conditions:
213*38fd1498Szrj 
214*38fd1498Szrj      We prime the worklist all the normal basic blocks.   The ENTRY block can
215*38fd1498Szrj      never be added to the worklist since it is never the successor of any
216*38fd1498Szrj      block.  We explicitly prevent the EXIT block from being added to the
217*38fd1498Szrj      worklist.
218*38fd1498Szrj 
219*38fd1498Szrj      We optimistically initialize LATER.  That is the only time this routine
220*38fd1498Szrj      will compute LATER for an edge out of the entry block since the entry
221*38fd1498Szrj      block is never on the worklist.  Thus, LATERIN is neither used nor
222*38fd1498Szrj      computed for the ENTRY block.
223*38fd1498Szrj 
224*38fd1498Szrj      Since the EXIT block is never added to the worklist, we will neither
225*38fd1498Szrj      use nor compute LATERIN for the exit block.  Edges which reach the
226*38fd1498Szrj      EXIT block are handled in the normal fashion inside the loop.  However,
227*38fd1498Szrj      the insertion/deletion computation needs LATERIN(EXIT), so we have
228*38fd1498Szrj      to compute it.  */
229*38fd1498Szrj 
230*38fd1498Szrj static void
compute_laterin(struct edge_list * edge_list,sbitmap * earliest,sbitmap * antloc,sbitmap * later,sbitmap * laterin)231*38fd1498Szrj compute_laterin (struct edge_list *edge_list, sbitmap *earliest,
232*38fd1498Szrj 		 sbitmap *antloc, sbitmap *later, sbitmap *laterin)
233*38fd1498Szrj {
234*38fd1498Szrj   int num_edges, i;
235*38fd1498Szrj   edge e;
236*38fd1498Szrj   basic_block *worklist, *qin, *qout, *qend, bb;
237*38fd1498Szrj   unsigned int qlen;
238*38fd1498Szrj   edge_iterator ei;
239*38fd1498Szrj 
240*38fd1498Szrj   num_edges = NUM_EDGES (edge_list);
241*38fd1498Szrj 
242*38fd1498Szrj   /* Allocate a worklist array/queue.  Entries are only added to the
243*38fd1498Szrj      list if they were not already on the list.  So the size is
244*38fd1498Szrj      bounded by the number of basic blocks.  */
245*38fd1498Szrj   qin = qout = worklist
246*38fd1498Szrj     = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
247*38fd1498Szrj 
248*38fd1498Szrj   /* Initialize a mapping from each edge to its index.  */
249*38fd1498Szrj   for (i = 0; i < num_edges; i++)
250*38fd1498Szrj     INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
251*38fd1498Szrj 
252*38fd1498Szrj   /* We want a maximal solution, so initially consider LATER true for
253*38fd1498Szrj      all edges.  This allows propagation through a loop since the incoming
254*38fd1498Szrj      loop edge will have LATER set, so if all the other incoming edges
255*38fd1498Szrj      to the loop are set, then LATERIN will be set for the head of the
256*38fd1498Szrj      loop.
257*38fd1498Szrj 
258*38fd1498Szrj      If the optimistic setting of LATER on that edge was incorrect (for
259*38fd1498Szrj      example the expression is ANTLOC in a block within the loop) then
260*38fd1498Szrj      this algorithm will detect it when we process the block at the head
261*38fd1498Szrj      of the optimistic edge.  That will requeue the affected blocks.  */
262*38fd1498Szrj   bitmap_vector_ones (later, num_edges);
263*38fd1498Szrj 
264*38fd1498Szrj   /* Note that even though we want an optimistic setting of LATER, we
265*38fd1498Szrj      do not want to be overly optimistic.  Consider an outgoing edge from
266*38fd1498Szrj      the entry block.  That edge should always have a LATER value the
267*38fd1498Szrj      same as EARLIEST for that edge.  */
268*38fd1498Szrj   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
269*38fd1498Szrj     bitmap_copy (later[(size_t) e->aux], earliest[(size_t) e->aux]);
270*38fd1498Szrj 
271*38fd1498Szrj   /* Add all the blocks to the worklist.  This prevents an early exit from
272*38fd1498Szrj      the loop given our optimistic initialization of LATER above.  */
273*38fd1498Szrj   auto_vec<int, 20> postorder;
274*38fd1498Szrj   inverted_post_order_compute (&postorder);
275*38fd1498Szrj   for (unsigned int i = 0; i < postorder.length (); ++i)
276*38fd1498Szrj     {
277*38fd1498Szrj       bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
278*38fd1498Szrj       if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
279*38fd1498Szrj 	  || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
280*38fd1498Szrj 	continue;
281*38fd1498Szrj       *qin++ = bb;
282*38fd1498Szrj       bb->aux = bb;
283*38fd1498Szrj     }
284*38fd1498Szrj 
285*38fd1498Szrj   /* Note that we do not use the last allocated element for our queue,
286*38fd1498Szrj      as EXIT_BLOCK is never inserted into it. */
287*38fd1498Szrj   qin = worklist;
288*38fd1498Szrj   qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
289*38fd1498Szrj   qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
290*38fd1498Szrj 
291*38fd1498Szrj   /* Iterate until the worklist is empty.  */
292*38fd1498Szrj   while (qlen)
293*38fd1498Szrj     {
294*38fd1498Szrj       /* Take the first entry off the worklist.  */
295*38fd1498Szrj       bb = *qout++;
296*38fd1498Szrj       bb->aux = NULL;
297*38fd1498Szrj       qlen--;
298*38fd1498Szrj       if (qout >= qend)
299*38fd1498Szrj 	qout = worklist;
300*38fd1498Szrj 
301*38fd1498Szrj       /* Compute the intersection of LATERIN for each incoming edge to B.  */
302*38fd1498Szrj       bitmap_ones (laterin[bb->index]);
303*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->preds)
304*38fd1498Szrj 	bitmap_and (laterin[bb->index], laterin[bb->index],
305*38fd1498Szrj 		    later[(size_t)e->aux]);
306*38fd1498Szrj 
307*38fd1498Szrj       /* Calculate LATER for all outgoing edges.  */
308*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->succs)
309*38fd1498Szrj 	if (bitmap_ior_and_compl (later[(size_t) e->aux],
310*38fd1498Szrj 				  earliest[(size_t) e->aux],
311*38fd1498Szrj 				  laterin[bb->index],
312*38fd1498Szrj 				  antloc[bb->index])
313*38fd1498Szrj 	    /* If LATER for an outgoing edge was changed, then we need
314*38fd1498Szrj 	       to add the target of the outgoing edge to the worklist.  */
315*38fd1498Szrj 	    && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && e->dest->aux == 0)
316*38fd1498Szrj 	  {
317*38fd1498Szrj 	    *qin++ = e->dest;
318*38fd1498Szrj 	    e->dest->aux = e;
319*38fd1498Szrj 	    qlen++;
320*38fd1498Szrj 	    if (qin >= qend)
321*38fd1498Szrj 	      qin = worklist;
322*38fd1498Szrj 	  }
323*38fd1498Szrj     }
324*38fd1498Szrj 
325*38fd1498Szrj   /* Computation of insertion and deletion points requires computing LATERIN
326*38fd1498Szrj      for the EXIT block.  We allocated an extra entry in the LATERIN array
327*38fd1498Szrj      for just this purpose.  */
328*38fd1498Szrj   bitmap_ones (laterin[last_basic_block_for_fn (cfun)]);
329*38fd1498Szrj   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
330*38fd1498Szrj     bitmap_and (laterin[last_basic_block_for_fn (cfun)],
331*38fd1498Szrj 		laterin[last_basic_block_for_fn (cfun)],
332*38fd1498Szrj 		later[(size_t) e->aux]);
333*38fd1498Szrj 
334*38fd1498Szrj   clear_aux_for_edges ();
335*38fd1498Szrj   free (worklist);
336*38fd1498Szrj }
337*38fd1498Szrj 
338*38fd1498Szrj /* Compute the insertion and deletion points for edge based LCM.  */
339*38fd1498Szrj 
340*38fd1498Szrj static void
compute_insert_delete(struct edge_list * edge_list,sbitmap * antloc,sbitmap * later,sbitmap * laterin,sbitmap * insert,sbitmap * del)341*38fd1498Szrj compute_insert_delete (struct edge_list *edge_list, sbitmap *antloc,
342*38fd1498Szrj 		       sbitmap *later, sbitmap *laterin, sbitmap *insert,
343*38fd1498Szrj 		       sbitmap *del)
344*38fd1498Szrj {
345*38fd1498Szrj   int x;
346*38fd1498Szrj   basic_block bb;
347*38fd1498Szrj 
348*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
349*38fd1498Szrj     bitmap_and_compl (del[bb->index], antloc[bb->index],
350*38fd1498Szrj 			laterin[bb->index]);
351*38fd1498Szrj 
352*38fd1498Szrj   for (x = 0; x < NUM_EDGES (edge_list); x++)
353*38fd1498Szrj     {
354*38fd1498Szrj       basic_block b = INDEX_EDGE_SUCC_BB (edge_list, x);
355*38fd1498Szrj 
356*38fd1498Szrj       if (b == EXIT_BLOCK_PTR_FOR_FN (cfun))
357*38fd1498Szrj 	bitmap_and_compl (insert[x], later[x],
358*38fd1498Szrj 			  laterin[last_basic_block_for_fn (cfun)]);
359*38fd1498Szrj       else
360*38fd1498Szrj 	bitmap_and_compl (insert[x], later[x], laterin[b->index]);
361*38fd1498Szrj     }
362*38fd1498Szrj }
363*38fd1498Szrj 
364*38fd1498Szrj /* Given local properties TRANSP, ANTLOC, AVLOC, KILL return the insert and
365*38fd1498Szrj    delete vectors for edge based LCM  and return the AVIN, AVOUT bitmap.
366*38fd1498Szrj    map the insert vector to what edge an expression should be inserted on.  */
367*38fd1498Szrj 
368*38fd1498Szrj struct edge_list *
pre_edge_lcm_avs(int n_exprs,sbitmap * transp,sbitmap * avloc,sbitmap * antloc,sbitmap * kill,sbitmap * avin,sbitmap * avout,sbitmap ** insert,sbitmap ** del)369*38fd1498Szrj pre_edge_lcm_avs (int n_exprs, sbitmap *transp,
370*38fd1498Szrj 	      sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
371*38fd1498Szrj 	      sbitmap *avin, sbitmap *avout,
372*38fd1498Szrj 	      sbitmap **insert, sbitmap **del)
373*38fd1498Szrj {
374*38fd1498Szrj   sbitmap *antin, *antout, *earliest;
375*38fd1498Szrj   sbitmap *later, *laterin;
376*38fd1498Szrj   struct edge_list *edge_list;
377*38fd1498Szrj   int num_edges;
378*38fd1498Szrj 
379*38fd1498Szrj   edge_list = create_edge_list ();
380*38fd1498Szrj   num_edges = NUM_EDGES (edge_list);
381*38fd1498Szrj 
382*38fd1498Szrj #ifdef LCM_DEBUG_INFO
383*38fd1498Szrj   if (dump_file)
384*38fd1498Szrj     {
385*38fd1498Szrj       fprintf (dump_file, "Edge List:\n");
386*38fd1498Szrj       verify_edge_list (dump_file, edge_list);
387*38fd1498Szrj       print_edge_list (dump_file, edge_list);
388*38fd1498Szrj       dump_bitmap_vector (dump_file, "transp", "", transp,
389*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
390*38fd1498Szrj       dump_bitmap_vector (dump_file, "antloc", "", antloc,
391*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
392*38fd1498Szrj       dump_bitmap_vector (dump_file, "avloc", "", avloc,
393*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
394*38fd1498Szrj       dump_bitmap_vector (dump_file, "kill", "", kill,
395*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
396*38fd1498Szrj     }
397*38fd1498Szrj #endif
398*38fd1498Szrj 
399*38fd1498Szrj   /* Compute global availability.  */
400*38fd1498Szrj   compute_available (avloc, kill, avout, avin);
401*38fd1498Szrj 
402*38fd1498Szrj   /* Compute global anticipatability.  */
403*38fd1498Szrj   antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
404*38fd1498Szrj   antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
405*38fd1498Szrj   compute_antinout_edge (antloc, transp, antin, antout);
406*38fd1498Szrj 
407*38fd1498Szrj #ifdef LCM_DEBUG_INFO
408*38fd1498Szrj   if (dump_file)
409*38fd1498Szrj     {
410*38fd1498Szrj       dump_bitmap_vector (dump_file, "antin", "", antin,
411*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
412*38fd1498Szrj       dump_bitmap_vector (dump_file, "antout", "", antout,
413*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
414*38fd1498Szrj     }
415*38fd1498Szrj #endif
416*38fd1498Szrj 
417*38fd1498Szrj   /* Compute earliestness.  */
418*38fd1498Szrj   earliest = sbitmap_vector_alloc (num_edges, n_exprs);
419*38fd1498Szrj   compute_earliest (edge_list, n_exprs, antin, antout, avout, kill, earliest);
420*38fd1498Szrj 
421*38fd1498Szrj #ifdef LCM_DEBUG_INFO
422*38fd1498Szrj   if (dump_file)
423*38fd1498Szrj     dump_bitmap_vector (dump_file, "earliest", "", earliest, num_edges);
424*38fd1498Szrj #endif
425*38fd1498Szrj 
426*38fd1498Szrj   sbitmap_vector_free (antout);
427*38fd1498Szrj   sbitmap_vector_free (antin);
428*38fd1498Szrj 
429*38fd1498Szrj   later = sbitmap_vector_alloc (num_edges, n_exprs);
430*38fd1498Szrj 
431*38fd1498Szrj   /* Allocate an extra element for the exit block in the laterin vector.  */
432*38fd1498Szrj   laterin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
433*38fd1498Szrj 				  n_exprs);
434*38fd1498Szrj   compute_laterin (edge_list, earliest, antloc, later, laterin);
435*38fd1498Szrj 
436*38fd1498Szrj #ifdef LCM_DEBUG_INFO
437*38fd1498Szrj   if (dump_file)
438*38fd1498Szrj     {
439*38fd1498Szrj       dump_bitmap_vector (dump_file, "laterin", "", laterin,
440*38fd1498Szrj 			  last_basic_block_for_fn (cfun) + 1);
441*38fd1498Szrj       dump_bitmap_vector (dump_file, "later", "", later, num_edges);
442*38fd1498Szrj     }
443*38fd1498Szrj #endif
444*38fd1498Szrj 
445*38fd1498Szrj   sbitmap_vector_free (earliest);
446*38fd1498Szrj 
447*38fd1498Szrj   *insert = sbitmap_vector_alloc (num_edges, n_exprs);
448*38fd1498Szrj   *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
449*38fd1498Szrj   bitmap_vector_clear (*insert, num_edges);
450*38fd1498Szrj   bitmap_vector_clear (*del, last_basic_block_for_fn (cfun));
451*38fd1498Szrj   compute_insert_delete (edge_list, antloc, later, laterin, *insert, *del);
452*38fd1498Szrj 
453*38fd1498Szrj   sbitmap_vector_free (laterin);
454*38fd1498Szrj   sbitmap_vector_free (later);
455*38fd1498Szrj 
456*38fd1498Szrj #ifdef LCM_DEBUG_INFO
457*38fd1498Szrj   if (dump_file)
458*38fd1498Szrj     {
459*38fd1498Szrj       dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
460*38fd1498Szrj       dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
461*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
462*38fd1498Szrj     }
463*38fd1498Szrj #endif
464*38fd1498Szrj 
465*38fd1498Szrj   return edge_list;
466*38fd1498Szrj }
467*38fd1498Szrj 
468*38fd1498Szrj /* Wrapper to allocate avin/avout and call pre_edge_lcm_avs.  */
469*38fd1498Szrj 
470*38fd1498Szrj struct edge_list *
pre_edge_lcm(int n_exprs,sbitmap * transp,sbitmap * avloc,sbitmap * antloc,sbitmap * kill,sbitmap ** insert,sbitmap ** del)471*38fd1498Szrj pre_edge_lcm (int n_exprs, sbitmap *transp,
472*38fd1498Szrj 	      sbitmap *avloc, sbitmap *antloc, sbitmap *kill,
473*38fd1498Szrj 	      sbitmap **insert, sbitmap **del)
474*38fd1498Szrj {
475*38fd1498Szrj   struct edge_list *edge_list;
476*38fd1498Szrj   sbitmap *avin, *avout;
477*38fd1498Szrj 
478*38fd1498Szrj   avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
479*38fd1498Szrj   avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
480*38fd1498Szrj 
481*38fd1498Szrj   edge_list = pre_edge_lcm_avs (n_exprs, transp, avloc, antloc, kill,
482*38fd1498Szrj 				 avin, avout, insert, del);
483*38fd1498Szrj 
484*38fd1498Szrj   sbitmap_vector_free (avout);
485*38fd1498Szrj   sbitmap_vector_free (avin);
486*38fd1498Szrj 
487*38fd1498Szrj   return edge_list;
488*38fd1498Szrj }
489*38fd1498Szrj 
490*38fd1498Szrj /* Compute the AVIN and AVOUT vectors from the AVLOC and KILL vectors.
491*38fd1498Szrj    Return the number of passes we performed to iterate to a solution.  */
492*38fd1498Szrj 
493*38fd1498Szrj void
compute_available(sbitmap * avloc,sbitmap * kill,sbitmap * avout,sbitmap * avin)494*38fd1498Szrj compute_available (sbitmap *avloc, sbitmap *kill, sbitmap *avout,
495*38fd1498Szrj 		   sbitmap *avin)
496*38fd1498Szrj {
497*38fd1498Szrj   edge e;
498*38fd1498Szrj   basic_block *worklist, *qin, *qout, *qend, bb;
499*38fd1498Szrj   unsigned int qlen;
500*38fd1498Szrj   edge_iterator ei;
501*38fd1498Szrj 
502*38fd1498Szrj   /* Allocate a worklist array/queue.  Entries are only added to the
503*38fd1498Szrj      list if they were not already on the list.  So the size is
504*38fd1498Szrj      bounded by the number of basic blocks.  */
505*38fd1498Szrj   qin = qout = worklist =
506*38fd1498Szrj     XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
507*38fd1498Szrj 
508*38fd1498Szrj   /* We want a maximal solution.  */
509*38fd1498Szrj   bitmap_vector_ones (avout, last_basic_block_for_fn (cfun));
510*38fd1498Szrj 
511*38fd1498Szrj   /* Put every block on the worklist; this is necessary because of the
512*38fd1498Szrj      optimistic initialization of AVOUT above.  Use inverted postorder
513*38fd1498Szrj      to make the dataflow problem require less iterations.  */
514*38fd1498Szrj   auto_vec<int, 20> postorder;
515*38fd1498Szrj   inverted_post_order_compute (&postorder);
516*38fd1498Szrj   for (unsigned int i = 0; i < postorder.length (); ++i)
517*38fd1498Szrj     {
518*38fd1498Szrj       bb = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
519*38fd1498Szrj       if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
520*38fd1498Szrj 	  || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
521*38fd1498Szrj 	continue;
522*38fd1498Szrj       *qin++ = bb;
523*38fd1498Szrj       bb->aux = bb;
524*38fd1498Szrj     }
525*38fd1498Szrj 
526*38fd1498Szrj   qin = worklist;
527*38fd1498Szrj   qend = &worklist[n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS];
528*38fd1498Szrj   qlen = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
529*38fd1498Szrj 
530*38fd1498Szrj   /* Mark blocks which are successors of the entry block so that we
531*38fd1498Szrj      can easily identify them below.  */
532*38fd1498Szrj   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
533*38fd1498Szrj     e->dest->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun);
534*38fd1498Szrj 
535*38fd1498Szrj   /* Iterate until the worklist is empty.  */
536*38fd1498Szrj   while (qlen)
537*38fd1498Szrj     {
538*38fd1498Szrj       /* Take the first entry off the worklist.  */
539*38fd1498Szrj       bb = *qout++;
540*38fd1498Szrj       qlen--;
541*38fd1498Szrj 
542*38fd1498Szrj       if (qout >= qend)
543*38fd1498Szrj 	qout = worklist;
544*38fd1498Szrj 
545*38fd1498Szrj       /* If one of the predecessor blocks is the ENTRY block, then the
546*38fd1498Szrj 	 intersection of avouts is the null set.  We can identify such blocks
547*38fd1498Szrj 	 by the special value in the AUX field in the block structure.  */
548*38fd1498Szrj       if (bb->aux == ENTRY_BLOCK_PTR_FOR_FN (cfun))
549*38fd1498Szrj 	/* Do not clear the aux field for blocks which are successors of the
550*38fd1498Szrj 	   ENTRY block.  That way we never add then to the worklist again.  */
551*38fd1498Szrj 	bitmap_clear (avin[bb->index]);
552*38fd1498Szrj       else
553*38fd1498Szrj 	{
554*38fd1498Szrj 	  /* Clear the aux field of this block so that it can be added to
555*38fd1498Szrj 	     the worklist again if necessary.  */
556*38fd1498Szrj 	  bb->aux = NULL;
557*38fd1498Szrj 	  bitmap_intersection_of_preds (avin[bb->index], avout, bb);
558*38fd1498Szrj 	}
559*38fd1498Szrj 
560*38fd1498Szrj       if (bitmap_ior_and_compl (avout[bb->index], avloc[bb->index],
561*38fd1498Szrj 				    avin[bb->index], kill[bb->index]))
562*38fd1498Szrj 	/* If the out state of this block changed, then we need
563*38fd1498Szrj 	   to add the successors of this block to the worklist
564*38fd1498Szrj 	   if they are not already on the worklist.  */
565*38fd1498Szrj 	FOR_EACH_EDGE (e, ei, bb->succs)
566*38fd1498Szrj 	  if (!e->dest->aux && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
567*38fd1498Szrj 	    {
568*38fd1498Szrj 	      *qin++ = e->dest;
569*38fd1498Szrj 	      e->dest->aux = e;
570*38fd1498Szrj 	      qlen++;
571*38fd1498Szrj 
572*38fd1498Szrj 	      if (qin >= qend)
573*38fd1498Szrj 		qin = worklist;
574*38fd1498Szrj 	    }
575*38fd1498Szrj     }
576*38fd1498Szrj 
577*38fd1498Szrj   clear_aux_for_edges ();
578*38fd1498Szrj   clear_aux_for_blocks ();
579*38fd1498Szrj   free (worklist);
580*38fd1498Szrj }
581*38fd1498Szrj 
582*38fd1498Szrj /* Compute the farthest vector for edge based lcm.  */
583*38fd1498Szrj 
584*38fd1498Szrj static void
compute_farthest(struct edge_list * edge_list,int n_exprs,sbitmap * st_avout,sbitmap * st_avin,sbitmap * st_antin,sbitmap * kill,sbitmap * farthest)585*38fd1498Szrj compute_farthest (struct edge_list *edge_list, int n_exprs,
586*38fd1498Szrj 		  sbitmap *st_avout, sbitmap *st_avin, sbitmap *st_antin,
587*38fd1498Szrj 		  sbitmap *kill, sbitmap *farthest)
588*38fd1498Szrj {
589*38fd1498Szrj   int x, num_edges;
590*38fd1498Szrj   basic_block pred, succ;
591*38fd1498Szrj 
592*38fd1498Szrj   num_edges = NUM_EDGES (edge_list);
593*38fd1498Szrj 
594*38fd1498Szrj   auto_sbitmap difference (n_exprs), temp_bitmap (n_exprs);
595*38fd1498Szrj   for (x = 0; x < num_edges; x++)
596*38fd1498Szrj     {
597*38fd1498Szrj       pred = INDEX_EDGE_PRED_BB (edge_list, x);
598*38fd1498Szrj       succ = INDEX_EDGE_SUCC_BB (edge_list, x);
599*38fd1498Szrj       if (succ == EXIT_BLOCK_PTR_FOR_FN (cfun))
600*38fd1498Szrj 	bitmap_copy (farthest[x], st_avout[pred->index]);
601*38fd1498Szrj       else
602*38fd1498Szrj 	{
603*38fd1498Szrj 	  if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
604*38fd1498Szrj 	    bitmap_clear (farthest[x]);
605*38fd1498Szrj 	  else
606*38fd1498Szrj 	    {
607*38fd1498Szrj 	      bitmap_and_compl (difference, st_avout[pred->index],
608*38fd1498Szrj 				  st_antin[succ->index]);
609*38fd1498Szrj 	      bitmap_not (temp_bitmap, st_avin[succ->index]);
610*38fd1498Szrj 	      bitmap_and_or (farthest[x], difference,
611*38fd1498Szrj 				    kill[succ->index], temp_bitmap);
612*38fd1498Szrj 	    }
613*38fd1498Szrj 	}
614*38fd1498Szrj     }
615*38fd1498Szrj }
616*38fd1498Szrj 
617*38fd1498Szrj /* Compute nearer and nearerout vectors for edge based lcm.
618*38fd1498Szrj 
619*38fd1498Szrj    This is the mirror of compute_laterin, additional comments on the
620*38fd1498Szrj    implementation can be found before compute_laterin.  */
621*38fd1498Szrj 
622*38fd1498Szrj static void
compute_nearerout(struct edge_list * edge_list,sbitmap * farthest,sbitmap * st_avloc,sbitmap * nearer,sbitmap * nearerout)623*38fd1498Szrj compute_nearerout (struct edge_list *edge_list, sbitmap *farthest,
624*38fd1498Szrj 		   sbitmap *st_avloc, sbitmap *nearer, sbitmap *nearerout)
625*38fd1498Szrj {
626*38fd1498Szrj   int num_edges, i;
627*38fd1498Szrj   edge e;
628*38fd1498Szrj   basic_block *worklist, *tos, bb;
629*38fd1498Szrj   edge_iterator ei;
630*38fd1498Szrj 
631*38fd1498Szrj   num_edges = NUM_EDGES (edge_list);
632*38fd1498Szrj 
633*38fd1498Szrj   /* Allocate a worklist array/queue.  Entries are only added to the
634*38fd1498Szrj      list if they were not already on the list.  So the size is
635*38fd1498Szrj      bounded by the number of basic blocks.  */
636*38fd1498Szrj   tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
637*38fd1498Szrj 
638*38fd1498Szrj   /* Initialize NEARER for each edge and build a mapping from an edge to
639*38fd1498Szrj      its index.  */
640*38fd1498Szrj   for (i = 0; i < num_edges; i++)
641*38fd1498Szrj     INDEX_EDGE (edge_list, i)->aux = (void *) (size_t) i;
642*38fd1498Szrj 
643*38fd1498Szrj   /* We want a maximal solution.  */
644*38fd1498Szrj   bitmap_vector_ones (nearer, num_edges);
645*38fd1498Szrj 
646*38fd1498Szrj   /* Note that even though we want an optimistic setting of NEARER, we
647*38fd1498Szrj      do not want to be overly optimistic.  Consider an incoming edge to
648*38fd1498Szrj      the exit block.  That edge should always have a NEARER value the
649*38fd1498Szrj      same as FARTHEST for that edge.  */
650*38fd1498Szrj   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
651*38fd1498Szrj     bitmap_copy (nearer[(size_t)e->aux], farthest[(size_t)e->aux]);
652*38fd1498Szrj 
653*38fd1498Szrj   /* Add all the blocks to the worklist.  This prevents an early exit
654*38fd1498Szrj      from the loop given our optimistic initialization of NEARER.  */
655*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
656*38fd1498Szrj     {
657*38fd1498Szrj       *tos++ = bb;
658*38fd1498Szrj       bb->aux = bb;
659*38fd1498Szrj     }
660*38fd1498Szrj 
661*38fd1498Szrj   /* Iterate until the worklist is empty.  */
662*38fd1498Szrj   while (tos != worklist)
663*38fd1498Szrj     {
664*38fd1498Szrj       /* Take the first entry off the worklist.  */
665*38fd1498Szrj       bb = *--tos;
666*38fd1498Szrj       bb->aux = NULL;
667*38fd1498Szrj 
668*38fd1498Szrj       /* Compute the intersection of NEARER for each outgoing edge from B.  */
669*38fd1498Szrj       bitmap_ones (nearerout[bb->index]);
670*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->succs)
671*38fd1498Szrj 	bitmap_and (nearerout[bb->index], nearerout[bb->index],
672*38fd1498Szrj 			 nearer[(size_t) e->aux]);
673*38fd1498Szrj 
674*38fd1498Szrj       /* Calculate NEARER for all incoming edges.  */
675*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->preds)
676*38fd1498Szrj 	if (bitmap_ior_and_compl (nearer[(size_t) e->aux],
677*38fd1498Szrj 				      farthest[(size_t) e->aux],
678*38fd1498Szrj 				      nearerout[e->dest->index],
679*38fd1498Szrj 				      st_avloc[e->dest->index])
680*38fd1498Szrj 	    /* If NEARER for an incoming edge was changed, then we need
681*38fd1498Szrj 	       to add the source of the incoming edge to the worklist.  */
682*38fd1498Szrj 	    && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) && e->src->aux == 0)
683*38fd1498Szrj 	  {
684*38fd1498Szrj 	    *tos++ = e->src;
685*38fd1498Szrj 	    e->src->aux = e;
686*38fd1498Szrj 	  }
687*38fd1498Szrj     }
688*38fd1498Szrj 
689*38fd1498Szrj   /* Computation of insertion and deletion points requires computing NEAREROUT
690*38fd1498Szrj      for the ENTRY block.  We allocated an extra entry in the NEAREROUT array
691*38fd1498Szrj      for just this purpose.  */
692*38fd1498Szrj   bitmap_ones (nearerout[last_basic_block_for_fn (cfun)]);
693*38fd1498Szrj   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
694*38fd1498Szrj     bitmap_and (nearerout[last_basic_block_for_fn (cfun)],
695*38fd1498Szrj 		     nearerout[last_basic_block_for_fn (cfun)],
696*38fd1498Szrj 		     nearer[(size_t) e->aux]);
697*38fd1498Szrj 
698*38fd1498Szrj   clear_aux_for_edges ();
699*38fd1498Szrj   free (tos);
700*38fd1498Szrj }
701*38fd1498Szrj 
702*38fd1498Szrj /* Compute the insertion and deletion points for edge based LCM.  */
703*38fd1498Szrj 
704*38fd1498Szrj static void
compute_rev_insert_delete(struct edge_list * edge_list,sbitmap * st_avloc,sbitmap * nearer,sbitmap * nearerout,sbitmap * insert,sbitmap * del)705*38fd1498Szrj compute_rev_insert_delete (struct edge_list *edge_list, sbitmap *st_avloc,
706*38fd1498Szrj 			   sbitmap *nearer, sbitmap *nearerout,
707*38fd1498Szrj 			   sbitmap *insert, sbitmap *del)
708*38fd1498Szrj {
709*38fd1498Szrj   int x;
710*38fd1498Szrj   basic_block bb;
711*38fd1498Szrj 
712*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
713*38fd1498Szrj     bitmap_and_compl (del[bb->index], st_avloc[bb->index],
714*38fd1498Szrj 			nearerout[bb->index]);
715*38fd1498Szrj 
716*38fd1498Szrj   for (x = 0; x < NUM_EDGES (edge_list); x++)
717*38fd1498Szrj     {
718*38fd1498Szrj       basic_block b = INDEX_EDGE_PRED_BB (edge_list, x);
719*38fd1498Szrj       if (b == ENTRY_BLOCK_PTR_FOR_FN (cfun))
720*38fd1498Szrj 	bitmap_and_compl (insert[x], nearer[x],
721*38fd1498Szrj 			  nearerout[last_basic_block_for_fn (cfun)]);
722*38fd1498Szrj       else
723*38fd1498Szrj 	bitmap_and_compl (insert[x], nearer[x], nearerout[b->index]);
724*38fd1498Szrj     }
725*38fd1498Szrj }
726*38fd1498Szrj 
727*38fd1498Szrj /* Given local properties TRANSP, ST_AVLOC, ST_ANTLOC, KILL return the
728*38fd1498Szrj    insert and delete vectors for edge based reverse LCM.  Returns an
729*38fd1498Szrj    edgelist which is used to map the insert vector to what edge
730*38fd1498Szrj    an expression should be inserted on.  */
731*38fd1498Szrj 
732*38fd1498Szrj struct edge_list *
pre_edge_rev_lcm(int n_exprs,sbitmap * transp,sbitmap * st_avloc,sbitmap * st_antloc,sbitmap * kill,sbitmap ** insert,sbitmap ** del)733*38fd1498Szrj pre_edge_rev_lcm (int n_exprs, sbitmap *transp,
734*38fd1498Szrj 		  sbitmap *st_avloc, sbitmap *st_antloc, sbitmap *kill,
735*38fd1498Szrj 		  sbitmap **insert, sbitmap **del)
736*38fd1498Szrj {
737*38fd1498Szrj   sbitmap *st_antin, *st_antout;
738*38fd1498Szrj   sbitmap *st_avout, *st_avin, *farthest;
739*38fd1498Szrj   sbitmap *nearer, *nearerout;
740*38fd1498Szrj   struct edge_list *edge_list;
741*38fd1498Szrj   int num_edges;
742*38fd1498Szrj 
743*38fd1498Szrj   edge_list = create_edge_list ();
744*38fd1498Szrj   num_edges = NUM_EDGES (edge_list);
745*38fd1498Szrj 
746*38fd1498Szrj   st_antin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
747*38fd1498Szrj   st_antout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
748*38fd1498Szrj   bitmap_vector_clear (st_antin, last_basic_block_for_fn (cfun));
749*38fd1498Szrj   bitmap_vector_clear (st_antout, last_basic_block_for_fn (cfun));
750*38fd1498Szrj   compute_antinout_edge (st_antloc, transp, st_antin, st_antout);
751*38fd1498Szrj 
752*38fd1498Szrj   /* Compute global anticipatability.  */
753*38fd1498Szrj   st_avout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
754*38fd1498Szrj   st_avin = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
755*38fd1498Szrj   compute_available (st_avloc, kill, st_avout, st_avin);
756*38fd1498Szrj 
757*38fd1498Szrj #ifdef LCM_DEBUG_INFO
758*38fd1498Szrj   if (dump_file)
759*38fd1498Szrj     {
760*38fd1498Szrj       fprintf (dump_file, "Edge List:\n");
761*38fd1498Szrj       verify_edge_list (dump_file, edge_list);
762*38fd1498Szrj       print_edge_list (dump_file, edge_list);
763*38fd1498Szrj       dump_bitmap_vector (dump_file, "transp", "", transp,
764*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
765*38fd1498Szrj       dump_bitmap_vector (dump_file, "st_avloc", "", st_avloc,
766*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
767*38fd1498Szrj       dump_bitmap_vector (dump_file, "st_antloc", "", st_antloc,
768*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
769*38fd1498Szrj       dump_bitmap_vector (dump_file, "st_antin", "", st_antin,
770*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
771*38fd1498Szrj       dump_bitmap_vector (dump_file, "st_antout", "", st_antout,
772*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
773*38fd1498Szrj       dump_bitmap_vector (dump_file, "st_kill", "", kill,
774*38fd1498Szrj 			  last_basic_block_for_fn (cfun));
775*38fd1498Szrj     }
776*38fd1498Szrj #endif
777*38fd1498Szrj 
778*38fd1498Szrj #ifdef LCM_DEBUG_INFO
779*38fd1498Szrj   if (dump_file)
780*38fd1498Szrj     {
781*38fd1498Szrj       dump_bitmap_vector (dump_file, "st_avout", "", st_avout, last_basic_block_for_fn (cfun));
782*38fd1498Szrj       dump_bitmap_vector (dump_file, "st_avin", "", st_avin, last_basic_block_for_fn (cfun));
783*38fd1498Szrj     }
784*38fd1498Szrj #endif
785*38fd1498Szrj 
786*38fd1498Szrj   /* Compute farthestness.  */
787*38fd1498Szrj   farthest = sbitmap_vector_alloc (num_edges, n_exprs);
788*38fd1498Szrj   compute_farthest (edge_list, n_exprs, st_avout, st_avin, st_antin,
789*38fd1498Szrj 		    kill, farthest);
790*38fd1498Szrj 
791*38fd1498Szrj #ifdef LCM_DEBUG_INFO
792*38fd1498Szrj   if (dump_file)
793*38fd1498Szrj     dump_bitmap_vector (dump_file, "farthest", "", farthest, num_edges);
794*38fd1498Szrj #endif
795*38fd1498Szrj 
796*38fd1498Szrj   sbitmap_vector_free (st_antin);
797*38fd1498Szrj   sbitmap_vector_free (st_antout);
798*38fd1498Szrj 
799*38fd1498Szrj   sbitmap_vector_free (st_avin);
800*38fd1498Szrj   sbitmap_vector_free (st_avout);
801*38fd1498Szrj 
802*38fd1498Szrj   nearer = sbitmap_vector_alloc (num_edges, n_exprs);
803*38fd1498Szrj 
804*38fd1498Szrj   /* Allocate an extra element for the entry block.  */
805*38fd1498Szrj   nearerout = sbitmap_vector_alloc (last_basic_block_for_fn (cfun) + 1,
806*38fd1498Szrj 				    n_exprs);
807*38fd1498Szrj   compute_nearerout (edge_list, farthest, st_avloc, nearer, nearerout);
808*38fd1498Szrj 
809*38fd1498Szrj #ifdef LCM_DEBUG_INFO
810*38fd1498Szrj   if (dump_file)
811*38fd1498Szrj     {
812*38fd1498Szrj       dump_bitmap_vector (dump_file, "nearerout", "", nearerout,
813*38fd1498Szrj 			   last_basic_block_for_fn (cfun) + 1);
814*38fd1498Szrj       dump_bitmap_vector (dump_file, "nearer", "", nearer, num_edges);
815*38fd1498Szrj     }
816*38fd1498Szrj #endif
817*38fd1498Szrj 
818*38fd1498Szrj   sbitmap_vector_free (farthest);
819*38fd1498Szrj 
820*38fd1498Szrj   *insert = sbitmap_vector_alloc (num_edges, n_exprs);
821*38fd1498Szrj   *del = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), n_exprs);
822*38fd1498Szrj   compute_rev_insert_delete (edge_list, st_avloc, nearer, nearerout,
823*38fd1498Szrj 			     *insert, *del);
824*38fd1498Szrj 
825*38fd1498Szrj   sbitmap_vector_free (nearerout);
826*38fd1498Szrj   sbitmap_vector_free (nearer);
827*38fd1498Szrj 
828*38fd1498Szrj #ifdef LCM_DEBUG_INFO
829*38fd1498Szrj   if (dump_file)
830*38fd1498Szrj     {
831*38fd1498Szrj       dump_bitmap_vector (dump_file, "pre_insert_map", "", *insert, num_edges);
832*38fd1498Szrj       dump_bitmap_vector (dump_file, "pre_delete_map", "", *del,
833*38fd1498Szrj 			   last_basic_block_for_fn (cfun));
834*38fd1498Szrj     }
835*38fd1498Szrj #endif
836*38fd1498Szrj   return edge_list;
837*38fd1498Szrj }
838*38fd1498Szrj 
839