xref: /dragonfly/contrib/gcc-8.0/gcc/cfganal.c (revision 38fd1498)
1*38fd1498Szrj /* Control flow graph analysis code for GNU compiler.
2*38fd1498Szrj    Copyright (C) 1987-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 /* This file contains various simple utilities to analyze the CFG.  */
21*38fd1498Szrj 
22*38fd1498Szrj #include "config.h"
23*38fd1498Szrj #include "system.h"
24*38fd1498Szrj #include "coretypes.h"
25*38fd1498Szrj #include "backend.h"
26*38fd1498Szrj #include "cfghooks.h"
27*38fd1498Szrj #include "timevar.h"
28*38fd1498Szrj #include "cfganal.h"
29*38fd1498Szrj #include "cfgloop.h"
30*38fd1498Szrj 
31*38fd1498Szrj namespace {
32*38fd1498Szrj /* Store the data structures necessary for depth-first search.  */
33*38fd1498Szrj class depth_first_search
34*38fd1498Szrj   {
35*38fd1498Szrj public:
36*38fd1498Szrj     depth_first_search ();
37*38fd1498Szrj 
38*38fd1498Szrj     basic_block execute (basic_block);
39*38fd1498Szrj     void add_bb (basic_block);
40*38fd1498Szrj 
41*38fd1498Szrj private:
42*38fd1498Szrj   /* stack for backtracking during the algorithm */
43*38fd1498Szrj   auto_vec<basic_block, 20> m_stack;
44*38fd1498Szrj 
45*38fd1498Szrj   /* record of basic blocks already seen by depth-first search */
46*38fd1498Szrj   auto_sbitmap m_visited_blocks;
47*38fd1498Szrj };
48*38fd1498Szrj }
49*38fd1498Szrj 
50*38fd1498Szrj /* Mark the back edges in DFS traversal.
51*38fd1498Szrj    Return nonzero if a loop (natural or otherwise) is present.
52*38fd1498Szrj    Inspired by Depth_First_Search_PP described in:
53*38fd1498Szrj 
54*38fd1498Szrj      Advanced Compiler Design and Implementation
55*38fd1498Szrj      Steven Muchnick
56*38fd1498Szrj      Morgan Kaufmann, 1997
57*38fd1498Szrj 
58*38fd1498Szrj    and heavily borrowed from pre_and_rev_post_order_compute.  */
59*38fd1498Szrj 
60*38fd1498Szrj bool
mark_dfs_back_edges(void)61*38fd1498Szrj mark_dfs_back_edges (void)
62*38fd1498Szrj {
63*38fd1498Szrj   int *pre;
64*38fd1498Szrj   int *post;
65*38fd1498Szrj   int prenum = 1;
66*38fd1498Szrj   int postnum = 1;
67*38fd1498Szrj   bool found = false;
68*38fd1498Szrj 
69*38fd1498Szrj   /* Allocate the preorder and postorder number arrays.  */
70*38fd1498Szrj   pre = XCNEWVEC (int, last_basic_block_for_fn (cfun));
71*38fd1498Szrj   post = XCNEWVEC (int, last_basic_block_for_fn (cfun));
72*38fd1498Szrj 
73*38fd1498Szrj   /* Allocate stack for back-tracking up CFG.  */
74*38fd1498Szrj   auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (cfun) + 1);
75*38fd1498Szrj 
76*38fd1498Szrj   /* Allocate bitmap to track nodes that have been visited.  */
77*38fd1498Szrj   auto_sbitmap visited (last_basic_block_for_fn (cfun));
78*38fd1498Szrj 
79*38fd1498Szrj   /* None of the nodes in the CFG have been visited yet.  */
80*38fd1498Szrj   bitmap_clear (visited);
81*38fd1498Szrj 
82*38fd1498Szrj   /* Push the first edge on to the stack.  */
83*38fd1498Szrj   stack.quick_push (ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs));
84*38fd1498Szrj 
85*38fd1498Szrj   while (!stack.is_empty ())
86*38fd1498Szrj     {
87*38fd1498Szrj       basic_block src;
88*38fd1498Szrj       basic_block dest;
89*38fd1498Szrj 
90*38fd1498Szrj       /* Look at the edge on the top of the stack.  */
91*38fd1498Szrj       edge_iterator ei = stack.last ();
92*38fd1498Szrj       src = ei_edge (ei)->src;
93*38fd1498Szrj       dest = ei_edge (ei)->dest;
94*38fd1498Szrj       ei_edge (ei)->flags &= ~EDGE_DFS_BACK;
95*38fd1498Szrj 
96*38fd1498Szrj       /* Check if the edge destination has been visited yet.  */
97*38fd1498Szrj       if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun) && ! bitmap_bit_p (visited,
98*38fd1498Szrj 								  dest->index))
99*38fd1498Szrj 	{
100*38fd1498Szrj 	  /* Mark that we have visited the destination.  */
101*38fd1498Szrj 	  bitmap_set_bit (visited, dest->index);
102*38fd1498Szrj 
103*38fd1498Szrj 	  pre[dest->index] = prenum++;
104*38fd1498Szrj 	  if (EDGE_COUNT (dest->succs) > 0)
105*38fd1498Szrj 	    {
106*38fd1498Szrj 	      /* Since the DEST node has been visited for the first
107*38fd1498Szrj 		 time, check its successors.  */
108*38fd1498Szrj 	      stack.quick_push (ei_start (dest->succs));
109*38fd1498Szrj 	    }
110*38fd1498Szrj 	  else
111*38fd1498Szrj 	    post[dest->index] = postnum++;
112*38fd1498Szrj 	}
113*38fd1498Szrj       else
114*38fd1498Szrj 	{
115*38fd1498Szrj 	  if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
116*38fd1498Szrj 	      && src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
117*38fd1498Szrj 	      && pre[src->index] >= pre[dest->index]
118*38fd1498Szrj 	      && post[dest->index] == 0)
119*38fd1498Szrj 	    ei_edge (ei)->flags |= EDGE_DFS_BACK, found = true;
120*38fd1498Szrj 
121*38fd1498Szrj 	  if (ei_one_before_end_p (ei)
122*38fd1498Szrj 	      && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
123*38fd1498Szrj 	    post[src->index] = postnum++;
124*38fd1498Szrj 
125*38fd1498Szrj 	  if (!ei_one_before_end_p (ei))
126*38fd1498Szrj 	    ei_next (&stack.last ());
127*38fd1498Szrj 	  else
128*38fd1498Szrj 	    stack.pop ();
129*38fd1498Szrj 	}
130*38fd1498Szrj     }
131*38fd1498Szrj 
132*38fd1498Szrj   free (pre);
133*38fd1498Szrj   free (post);
134*38fd1498Szrj 
135*38fd1498Szrj   return found;
136*38fd1498Szrj }
137*38fd1498Szrj 
138*38fd1498Szrj /* Find unreachable blocks.  An unreachable block will have 0 in
139*38fd1498Szrj    the reachable bit in block->flags.  A nonzero value indicates the
140*38fd1498Szrj    block is reachable.  */
141*38fd1498Szrj 
142*38fd1498Szrj void
find_unreachable_blocks(void)143*38fd1498Szrj find_unreachable_blocks (void)
144*38fd1498Szrj {
145*38fd1498Szrj   edge e;
146*38fd1498Szrj   edge_iterator ei;
147*38fd1498Szrj   basic_block *tos, *worklist, bb;
148*38fd1498Szrj 
149*38fd1498Szrj   tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
150*38fd1498Szrj 
151*38fd1498Szrj   /* Clear all the reachability flags.  */
152*38fd1498Szrj 
153*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
154*38fd1498Szrj     bb->flags &= ~BB_REACHABLE;
155*38fd1498Szrj 
156*38fd1498Szrj   /* Add our starting points to the worklist.  Almost always there will
157*38fd1498Szrj      be only one.  It isn't inconceivable that we might one day directly
158*38fd1498Szrj      support Fortran alternate entry points.  */
159*38fd1498Szrj 
160*38fd1498Szrj   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
161*38fd1498Szrj     {
162*38fd1498Szrj       *tos++ = e->dest;
163*38fd1498Szrj 
164*38fd1498Szrj       /* Mark the block reachable.  */
165*38fd1498Szrj       e->dest->flags |= BB_REACHABLE;
166*38fd1498Szrj     }
167*38fd1498Szrj 
168*38fd1498Szrj   /* Iterate: find everything reachable from what we've already seen.  */
169*38fd1498Szrj 
170*38fd1498Szrj   while (tos != worklist)
171*38fd1498Szrj     {
172*38fd1498Szrj       basic_block b = *--tos;
173*38fd1498Szrj 
174*38fd1498Szrj       FOR_EACH_EDGE (e, ei, b->succs)
175*38fd1498Szrj 	{
176*38fd1498Szrj 	  basic_block dest = e->dest;
177*38fd1498Szrj 
178*38fd1498Szrj 	  if (!(dest->flags & BB_REACHABLE))
179*38fd1498Szrj 	    {
180*38fd1498Szrj 	      *tos++ = dest;
181*38fd1498Szrj 	      dest->flags |= BB_REACHABLE;
182*38fd1498Szrj 	    }
183*38fd1498Szrj 	}
184*38fd1498Szrj     }
185*38fd1498Szrj 
186*38fd1498Szrj   free (worklist);
187*38fd1498Szrj }
188*38fd1498Szrj 
189*38fd1498Szrj /* Verify that there are no unreachable blocks in the current function.  */
190*38fd1498Szrj 
191*38fd1498Szrj void
verify_no_unreachable_blocks(void)192*38fd1498Szrj verify_no_unreachable_blocks (void)
193*38fd1498Szrj {
194*38fd1498Szrj   find_unreachable_blocks ();
195*38fd1498Szrj 
196*38fd1498Szrj   basic_block bb;
197*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
198*38fd1498Szrj     gcc_assert ((bb->flags & BB_REACHABLE) != 0);
199*38fd1498Szrj }
200*38fd1498Szrj 
201*38fd1498Szrj 
202*38fd1498Szrj /* Functions to access an edge list with a vector representation.
203*38fd1498Szrj    Enough data is kept such that given an index number, the
204*38fd1498Szrj    pred and succ that edge represents can be determined, or
205*38fd1498Szrj    given a pred and a succ, its index number can be returned.
206*38fd1498Szrj    This allows algorithms which consume a lot of memory to
207*38fd1498Szrj    represent the normally full matrix of edge (pred,succ) with a
208*38fd1498Szrj    single indexed vector,  edge (EDGE_INDEX (pred, succ)), with no
209*38fd1498Szrj    wasted space in the client code due to sparse flow graphs.  */
210*38fd1498Szrj 
211*38fd1498Szrj /* This functions initializes the edge list. Basically the entire
212*38fd1498Szrj    flowgraph is processed, and all edges are assigned a number,
213*38fd1498Szrj    and the data structure is filled in.  */
214*38fd1498Szrj 
215*38fd1498Szrj struct edge_list *
create_edge_list(void)216*38fd1498Szrj create_edge_list (void)
217*38fd1498Szrj {
218*38fd1498Szrj   struct edge_list *elist;
219*38fd1498Szrj   edge e;
220*38fd1498Szrj   int num_edges;
221*38fd1498Szrj   basic_block bb;
222*38fd1498Szrj   edge_iterator ei;
223*38fd1498Szrj 
224*38fd1498Szrj   /* Determine the number of edges in the flow graph by counting successor
225*38fd1498Szrj      edges on each basic block.  */
226*38fd1498Szrj   num_edges = 0;
227*38fd1498Szrj   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
228*38fd1498Szrj 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
229*38fd1498Szrj     {
230*38fd1498Szrj       num_edges += EDGE_COUNT (bb->succs);
231*38fd1498Szrj     }
232*38fd1498Szrj 
233*38fd1498Szrj   elist = XNEW (struct edge_list);
234*38fd1498Szrj   elist->num_edges = num_edges;
235*38fd1498Szrj   elist->index_to_edge = XNEWVEC (edge, num_edges);
236*38fd1498Szrj 
237*38fd1498Szrj   num_edges = 0;
238*38fd1498Szrj 
239*38fd1498Szrj   /* Follow successors of blocks, and register these edges.  */
240*38fd1498Szrj   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
241*38fd1498Szrj 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
242*38fd1498Szrj     FOR_EACH_EDGE (e, ei, bb->succs)
243*38fd1498Szrj       elist->index_to_edge[num_edges++] = e;
244*38fd1498Szrj 
245*38fd1498Szrj   return elist;
246*38fd1498Szrj }
247*38fd1498Szrj 
248*38fd1498Szrj /* This function free's memory associated with an edge list.  */
249*38fd1498Szrj 
250*38fd1498Szrj void
free_edge_list(struct edge_list * elist)251*38fd1498Szrj free_edge_list (struct edge_list *elist)
252*38fd1498Szrj {
253*38fd1498Szrj   if (elist)
254*38fd1498Szrj     {
255*38fd1498Szrj       free (elist->index_to_edge);
256*38fd1498Szrj       free (elist);
257*38fd1498Szrj     }
258*38fd1498Szrj }
259*38fd1498Szrj 
260*38fd1498Szrj /* This function provides debug output showing an edge list.  */
261*38fd1498Szrj 
262*38fd1498Szrj DEBUG_FUNCTION void
print_edge_list(FILE * f,struct edge_list * elist)263*38fd1498Szrj print_edge_list (FILE *f, struct edge_list *elist)
264*38fd1498Szrj {
265*38fd1498Szrj   int x;
266*38fd1498Szrj 
267*38fd1498Szrj   fprintf (f, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
268*38fd1498Szrj 	   n_basic_blocks_for_fn (cfun), elist->num_edges);
269*38fd1498Szrj 
270*38fd1498Szrj   for (x = 0; x < elist->num_edges; x++)
271*38fd1498Szrj     {
272*38fd1498Szrj       fprintf (f, " %-4d - edge(", x);
273*38fd1498Szrj       if (INDEX_EDGE_PRED_BB (elist, x) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
274*38fd1498Szrj 	fprintf (f, "entry,");
275*38fd1498Szrj       else
276*38fd1498Szrj 	fprintf (f, "%d,", INDEX_EDGE_PRED_BB (elist, x)->index);
277*38fd1498Szrj 
278*38fd1498Szrj       if (INDEX_EDGE_SUCC_BB (elist, x) == EXIT_BLOCK_PTR_FOR_FN (cfun))
279*38fd1498Szrj 	fprintf (f, "exit)\n");
280*38fd1498Szrj       else
281*38fd1498Szrj 	fprintf (f, "%d)\n", INDEX_EDGE_SUCC_BB (elist, x)->index);
282*38fd1498Szrj     }
283*38fd1498Szrj }
284*38fd1498Szrj 
285*38fd1498Szrj /* This function provides an internal consistency check of an edge list,
286*38fd1498Szrj    verifying that all edges are present, and that there are no
287*38fd1498Szrj    extra edges.  */
288*38fd1498Szrj 
289*38fd1498Szrj DEBUG_FUNCTION void
verify_edge_list(FILE * f,struct edge_list * elist)290*38fd1498Szrj verify_edge_list (FILE *f, struct edge_list *elist)
291*38fd1498Szrj {
292*38fd1498Szrj   int pred, succ, index;
293*38fd1498Szrj   edge e;
294*38fd1498Szrj   basic_block bb, p, s;
295*38fd1498Szrj   edge_iterator ei;
296*38fd1498Szrj 
297*38fd1498Szrj   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
298*38fd1498Szrj 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
299*38fd1498Szrj     {
300*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->succs)
301*38fd1498Szrj 	{
302*38fd1498Szrj 	  pred = e->src->index;
303*38fd1498Szrj 	  succ = e->dest->index;
304*38fd1498Szrj 	  index = EDGE_INDEX (elist, e->src, e->dest);
305*38fd1498Szrj 	  if (index == EDGE_INDEX_NO_EDGE)
306*38fd1498Szrj 	    {
307*38fd1498Szrj 	      fprintf (f, "*p* No index for edge from %d to %d\n", pred, succ);
308*38fd1498Szrj 	      continue;
309*38fd1498Szrj 	    }
310*38fd1498Szrj 
311*38fd1498Szrj 	  if (INDEX_EDGE_PRED_BB (elist, index)->index != pred)
312*38fd1498Szrj 	    fprintf (f, "*p* Pred for index %d should be %d not %d\n",
313*38fd1498Szrj 		     index, pred, INDEX_EDGE_PRED_BB (elist, index)->index);
314*38fd1498Szrj 	  if (INDEX_EDGE_SUCC_BB (elist, index)->index != succ)
315*38fd1498Szrj 	    fprintf (f, "*p* Succ for index %d should be %d not %d\n",
316*38fd1498Szrj 		     index, succ, INDEX_EDGE_SUCC_BB (elist, index)->index);
317*38fd1498Szrj 	}
318*38fd1498Szrj     }
319*38fd1498Szrj 
320*38fd1498Szrj   /* We've verified that all the edges are in the list, now lets make sure
321*38fd1498Szrj      there are no spurious edges in the list.  This is an expensive check!  */
322*38fd1498Szrj 
323*38fd1498Szrj   FOR_BB_BETWEEN (p, ENTRY_BLOCK_PTR_FOR_FN (cfun),
324*38fd1498Szrj 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
325*38fd1498Szrj     FOR_BB_BETWEEN (s, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
326*38fd1498Szrj       {
327*38fd1498Szrj 	int found_edge = 0;
328*38fd1498Szrj 
329*38fd1498Szrj 	FOR_EACH_EDGE (e, ei, p->succs)
330*38fd1498Szrj 	  if (e->dest == s)
331*38fd1498Szrj 	    {
332*38fd1498Szrj 	      found_edge = 1;
333*38fd1498Szrj 	      break;
334*38fd1498Szrj 	    }
335*38fd1498Szrj 
336*38fd1498Szrj 	FOR_EACH_EDGE (e, ei, s->preds)
337*38fd1498Szrj 	  if (e->src == p)
338*38fd1498Szrj 	    {
339*38fd1498Szrj 	      found_edge = 1;
340*38fd1498Szrj 	      break;
341*38fd1498Szrj 	    }
342*38fd1498Szrj 
343*38fd1498Szrj 	if (EDGE_INDEX (elist, p, s)
344*38fd1498Szrj 	    == EDGE_INDEX_NO_EDGE && found_edge != 0)
345*38fd1498Szrj 	  fprintf (f, "*** Edge (%d, %d) appears to not have an index\n",
346*38fd1498Szrj 		   p->index, s->index);
347*38fd1498Szrj 	if (EDGE_INDEX (elist, p, s)
348*38fd1498Szrj 	    != EDGE_INDEX_NO_EDGE && found_edge == 0)
349*38fd1498Szrj 	  fprintf (f, "*** Edge (%d, %d) has index %d, but there is no edge\n",
350*38fd1498Szrj 		   p->index, s->index, EDGE_INDEX (elist, p, s));
351*38fd1498Szrj       }
352*38fd1498Szrj }
353*38fd1498Szrj 
354*38fd1498Szrj 
355*38fd1498Szrj /* Functions to compute control dependences.  */
356*38fd1498Szrj 
357*38fd1498Szrj /* Indicate block BB is control dependent on an edge with index EDGE_INDEX.  */
358*38fd1498Szrj void
set_control_dependence_map_bit(basic_block bb,int edge_index)359*38fd1498Szrj control_dependences::set_control_dependence_map_bit (basic_block bb,
360*38fd1498Szrj 						     int edge_index)
361*38fd1498Szrj {
362*38fd1498Szrj   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
363*38fd1498Szrj     return;
364*38fd1498Szrj   gcc_assert (bb != EXIT_BLOCK_PTR_FOR_FN (cfun));
365*38fd1498Szrj   bitmap_set_bit (control_dependence_map[bb->index], edge_index);
366*38fd1498Szrj }
367*38fd1498Szrj 
368*38fd1498Szrj /* Clear all control dependences for block BB.  */
369*38fd1498Szrj void
clear_control_dependence_bitmap(basic_block bb)370*38fd1498Szrj control_dependences::clear_control_dependence_bitmap (basic_block bb)
371*38fd1498Szrj {
372*38fd1498Szrj   bitmap_clear (control_dependence_map[bb->index]);
373*38fd1498Szrj }
374*38fd1498Szrj 
375*38fd1498Szrj /* Find the immediate postdominator PDOM of the specified basic block BLOCK.
376*38fd1498Szrj    This function is necessary because some blocks have negative numbers.  */
377*38fd1498Szrj 
378*38fd1498Szrj static inline basic_block
find_pdom(basic_block block)379*38fd1498Szrj find_pdom (basic_block block)
380*38fd1498Szrj {
381*38fd1498Szrj   gcc_assert (block != ENTRY_BLOCK_PTR_FOR_FN (cfun));
382*38fd1498Szrj 
383*38fd1498Szrj   if (block == EXIT_BLOCK_PTR_FOR_FN (cfun))
384*38fd1498Szrj     return EXIT_BLOCK_PTR_FOR_FN (cfun);
385*38fd1498Szrj   else
386*38fd1498Szrj     {
387*38fd1498Szrj       basic_block bb = get_immediate_dominator (CDI_POST_DOMINATORS, block);
388*38fd1498Szrj       if (! bb)
389*38fd1498Szrj 	return EXIT_BLOCK_PTR_FOR_FN (cfun);
390*38fd1498Szrj       return bb;
391*38fd1498Szrj     }
392*38fd1498Szrj }
393*38fd1498Szrj 
394*38fd1498Szrj /* Determine all blocks' control dependences on the given edge with edge_list
395*38fd1498Szrj    EL index EDGE_INDEX, ala Morgan, Section 3.6.  */
396*38fd1498Szrj 
397*38fd1498Szrj void
find_control_dependence(int edge_index)398*38fd1498Szrj control_dependences::find_control_dependence (int edge_index)
399*38fd1498Szrj {
400*38fd1498Szrj   basic_block current_block;
401*38fd1498Szrj   basic_block ending_block;
402*38fd1498Szrj 
403*38fd1498Szrj   gcc_assert (get_edge_src (edge_index) != EXIT_BLOCK_PTR_FOR_FN (cfun));
404*38fd1498Szrj 
405*38fd1498Szrj   /* For abnormal edges, we don't make current_block control
406*38fd1498Szrj      dependent because instructions that throw are always necessary
407*38fd1498Szrj      anyway.  */
408*38fd1498Szrj   edge e = find_edge (get_edge_src (edge_index), get_edge_dest (edge_index));
409*38fd1498Szrj   if (e->flags & EDGE_ABNORMAL)
410*38fd1498Szrj     return;
411*38fd1498Szrj 
412*38fd1498Szrj   if (get_edge_src (edge_index) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
413*38fd1498Szrj     ending_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
414*38fd1498Szrj   else
415*38fd1498Szrj     ending_block = find_pdom (get_edge_src (edge_index));
416*38fd1498Szrj 
417*38fd1498Szrj   for (current_block = get_edge_dest (edge_index);
418*38fd1498Szrj        current_block != ending_block
419*38fd1498Szrj        && current_block != EXIT_BLOCK_PTR_FOR_FN (cfun);
420*38fd1498Szrj        current_block = find_pdom (current_block))
421*38fd1498Szrj     set_control_dependence_map_bit (current_block, edge_index);
422*38fd1498Szrj }
423*38fd1498Szrj 
424*38fd1498Szrj /* Record all blocks' control dependences on all edges in the edge
425*38fd1498Szrj    list EL, ala Morgan, Section 3.6.  */
426*38fd1498Szrj 
control_dependences()427*38fd1498Szrj control_dependences::control_dependences ()
428*38fd1498Szrj {
429*38fd1498Szrj   timevar_push (TV_CONTROL_DEPENDENCES);
430*38fd1498Szrj 
431*38fd1498Szrj   /* Initialize the edge list.  */
432*38fd1498Szrj   int num_edges = 0;
433*38fd1498Szrj   basic_block bb;
434*38fd1498Szrj   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
435*38fd1498Szrj 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
436*38fd1498Szrj     num_edges += EDGE_COUNT (bb->succs);
437*38fd1498Szrj   m_el.create (num_edges);
438*38fd1498Szrj   edge e;
439*38fd1498Szrj   edge_iterator ei;
440*38fd1498Szrj   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
441*38fd1498Szrj 		  EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
442*38fd1498Szrj     FOR_EACH_EDGE (e, ei, bb->succs)
443*38fd1498Szrj       m_el.quick_push (std::make_pair (e->src->index, e->dest->index));
444*38fd1498Szrj 
445*38fd1498Szrj   control_dependence_map.create (last_basic_block_for_fn (cfun));
446*38fd1498Szrj   for (int i = 0; i < last_basic_block_for_fn (cfun); ++i)
447*38fd1498Szrj     control_dependence_map.quick_push (BITMAP_ALLOC (NULL));
448*38fd1498Szrj   for (int i = 0; i < num_edges; ++i)
449*38fd1498Szrj     find_control_dependence (i);
450*38fd1498Szrj 
451*38fd1498Szrj   timevar_pop (TV_CONTROL_DEPENDENCES);
452*38fd1498Szrj }
453*38fd1498Szrj 
454*38fd1498Szrj /* Free control dependences and the associated edge list.  */
455*38fd1498Szrj 
~control_dependences()456*38fd1498Szrj control_dependences::~control_dependences ()
457*38fd1498Szrj {
458*38fd1498Szrj   for (unsigned i = 0; i < control_dependence_map.length (); ++i)
459*38fd1498Szrj     BITMAP_FREE (control_dependence_map[i]);
460*38fd1498Szrj   control_dependence_map.release ();
461*38fd1498Szrj   m_el.release ();
462*38fd1498Szrj }
463*38fd1498Szrj 
464*38fd1498Szrj /* Returns the bitmap of edges the basic-block I is dependent on.  */
465*38fd1498Szrj 
466*38fd1498Szrj bitmap
get_edges_dependent_on(int i)467*38fd1498Szrj control_dependences::get_edges_dependent_on (int i)
468*38fd1498Szrj {
469*38fd1498Szrj   return control_dependence_map[i];
470*38fd1498Szrj }
471*38fd1498Szrj 
472*38fd1498Szrj /* Returns the edge source with index I from the edge list.  */
473*38fd1498Szrj 
474*38fd1498Szrj basic_block
get_edge_src(int i)475*38fd1498Szrj control_dependences::get_edge_src (int i)
476*38fd1498Szrj {
477*38fd1498Szrj   return BASIC_BLOCK_FOR_FN (cfun, m_el[i].first);
478*38fd1498Szrj }
479*38fd1498Szrj 
480*38fd1498Szrj /* Returns the edge destination with index I from the edge list.  */
481*38fd1498Szrj 
482*38fd1498Szrj basic_block
get_edge_dest(int i)483*38fd1498Szrj control_dependences::get_edge_dest (int i)
484*38fd1498Szrj {
485*38fd1498Szrj   return BASIC_BLOCK_FOR_FN (cfun, m_el[i].second);
486*38fd1498Szrj }
487*38fd1498Szrj 
488*38fd1498Szrj 
489*38fd1498Szrj /* Given PRED and SUCC blocks, return the edge which connects the blocks.
490*38fd1498Szrj    If no such edge exists, return NULL.  */
491*38fd1498Szrj 
492*38fd1498Szrj edge
find_edge(basic_block pred,basic_block succ)493*38fd1498Szrj find_edge (basic_block pred, basic_block succ)
494*38fd1498Szrj {
495*38fd1498Szrj   edge e;
496*38fd1498Szrj   edge_iterator ei;
497*38fd1498Szrj 
498*38fd1498Szrj   if (EDGE_COUNT (pred->succs) <= EDGE_COUNT (succ->preds))
499*38fd1498Szrj     {
500*38fd1498Szrj       FOR_EACH_EDGE (e, ei, pred->succs)
501*38fd1498Szrj 	if (e->dest == succ)
502*38fd1498Szrj 	  return e;
503*38fd1498Szrj     }
504*38fd1498Szrj   else
505*38fd1498Szrj     {
506*38fd1498Szrj       FOR_EACH_EDGE (e, ei, succ->preds)
507*38fd1498Szrj 	if (e->src == pred)
508*38fd1498Szrj 	  return e;
509*38fd1498Szrj     }
510*38fd1498Szrj 
511*38fd1498Szrj   return NULL;
512*38fd1498Szrj }
513*38fd1498Szrj 
514*38fd1498Szrj /* This routine will determine what, if any, edge there is between
515*38fd1498Szrj    a specified predecessor and successor.  */
516*38fd1498Szrj 
517*38fd1498Szrj int
find_edge_index(struct edge_list * edge_list,basic_block pred,basic_block succ)518*38fd1498Szrj find_edge_index (struct edge_list *edge_list, basic_block pred, basic_block succ)
519*38fd1498Szrj {
520*38fd1498Szrj   int x;
521*38fd1498Szrj 
522*38fd1498Szrj   for (x = 0; x < NUM_EDGES (edge_list); x++)
523*38fd1498Szrj     if (INDEX_EDGE_PRED_BB (edge_list, x) == pred
524*38fd1498Szrj 	&& INDEX_EDGE_SUCC_BB (edge_list, x) == succ)
525*38fd1498Szrj       return x;
526*38fd1498Szrj 
527*38fd1498Szrj   return (EDGE_INDEX_NO_EDGE);
528*38fd1498Szrj }
529*38fd1498Szrj 
530*38fd1498Szrj /* This routine will remove any fake predecessor edges for a basic block.
531*38fd1498Szrj    When the edge is removed, it is also removed from whatever successor
532*38fd1498Szrj    list it is in.  */
533*38fd1498Szrj 
534*38fd1498Szrj static void
remove_fake_predecessors(basic_block bb)535*38fd1498Szrj remove_fake_predecessors (basic_block bb)
536*38fd1498Szrj {
537*38fd1498Szrj   edge e;
538*38fd1498Szrj   edge_iterator ei;
539*38fd1498Szrj 
540*38fd1498Szrj   for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
541*38fd1498Szrj     {
542*38fd1498Szrj       if ((e->flags & EDGE_FAKE) == EDGE_FAKE)
543*38fd1498Szrj 	remove_edge (e);
544*38fd1498Szrj       else
545*38fd1498Szrj 	ei_next (&ei);
546*38fd1498Szrj     }
547*38fd1498Szrj }
548*38fd1498Szrj 
549*38fd1498Szrj /* This routine will remove all fake edges from the flow graph.  If
550*38fd1498Szrj    we remove all fake successors, it will automatically remove all
551*38fd1498Szrj    fake predecessors.  */
552*38fd1498Szrj 
553*38fd1498Szrj void
remove_fake_edges(void)554*38fd1498Szrj remove_fake_edges (void)
555*38fd1498Szrj {
556*38fd1498Szrj   basic_block bb;
557*38fd1498Szrj 
558*38fd1498Szrj   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb, NULL, next_bb)
559*38fd1498Szrj     remove_fake_predecessors (bb);
560*38fd1498Szrj }
561*38fd1498Szrj 
562*38fd1498Szrj /* This routine will remove all fake edges to the EXIT_BLOCK.  */
563*38fd1498Szrj 
564*38fd1498Szrj void
remove_fake_exit_edges(void)565*38fd1498Szrj remove_fake_exit_edges (void)
566*38fd1498Szrj {
567*38fd1498Szrj   remove_fake_predecessors (EXIT_BLOCK_PTR_FOR_FN (cfun));
568*38fd1498Szrj }
569*38fd1498Szrj 
570*38fd1498Szrj 
571*38fd1498Szrj /* This function will add a fake edge between any block which has no
572*38fd1498Szrj    successors, and the exit block. Some data flow equations require these
573*38fd1498Szrj    edges to exist.  */
574*38fd1498Szrj 
575*38fd1498Szrj void
add_noreturn_fake_exit_edges(void)576*38fd1498Szrj add_noreturn_fake_exit_edges (void)
577*38fd1498Szrj {
578*38fd1498Szrj   basic_block bb;
579*38fd1498Szrj 
580*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
581*38fd1498Szrj     if (EDGE_COUNT (bb->succs) == 0)
582*38fd1498Szrj       make_single_succ_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
583*38fd1498Szrj }
584*38fd1498Szrj 
585*38fd1498Szrj /* This function adds a fake edge between any infinite loops to the
586*38fd1498Szrj    exit block.  Some optimizations require a path from each node to
587*38fd1498Szrj    the exit node.
588*38fd1498Szrj 
589*38fd1498Szrj    See also Morgan, Figure 3.10, pp. 82-83.
590*38fd1498Szrj 
591*38fd1498Szrj    The current implementation is ugly, not attempting to minimize the
592*38fd1498Szrj    number of inserted fake edges.  To reduce the number of fake edges
593*38fd1498Szrj    to insert, add fake edges from _innermost_ loops containing only
594*38fd1498Szrj    nodes not reachable from the exit block.  */
595*38fd1498Szrj 
596*38fd1498Szrj void
connect_infinite_loops_to_exit(void)597*38fd1498Szrj connect_infinite_loops_to_exit (void)
598*38fd1498Szrj {
599*38fd1498Szrj   /* Perform depth-first search in the reverse graph to find nodes
600*38fd1498Szrj      reachable from the exit block.  */
601*38fd1498Szrj   depth_first_search dfs;
602*38fd1498Szrj   dfs.add_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
603*38fd1498Szrj 
604*38fd1498Szrj   /* Repeatedly add fake edges, updating the unreachable nodes.  */
605*38fd1498Szrj   basic_block unvisited_block = EXIT_BLOCK_PTR_FOR_FN (cfun);
606*38fd1498Szrj   while (1)
607*38fd1498Szrj     {
608*38fd1498Szrj       unvisited_block = dfs.execute (unvisited_block);
609*38fd1498Szrj       if (!unvisited_block)
610*38fd1498Szrj 	break;
611*38fd1498Szrj 
612*38fd1498Szrj       basic_block deadend_block = dfs_find_deadend (unvisited_block);
613*38fd1498Szrj       edge e = make_edge (deadend_block, EXIT_BLOCK_PTR_FOR_FN (cfun),
614*38fd1498Szrj 			  EDGE_FAKE);
615*38fd1498Szrj       e->probability = profile_probability::never ();
616*38fd1498Szrj       dfs.add_bb (deadend_block);
617*38fd1498Szrj     }
618*38fd1498Szrj }
619*38fd1498Szrj 
620*38fd1498Szrj /* Compute reverse top sort order.  This is computing a post order
621*38fd1498Szrj    numbering of the graph.  If INCLUDE_ENTRY_EXIT is true, then
622*38fd1498Szrj    ENTRY_BLOCK and EXIT_BLOCK are included.  If DELETE_UNREACHABLE is
623*38fd1498Szrj    true, unreachable blocks are deleted.  */
624*38fd1498Szrj 
625*38fd1498Szrj int
post_order_compute(int * post_order,bool include_entry_exit,bool delete_unreachable)626*38fd1498Szrj post_order_compute (int *post_order, bool include_entry_exit,
627*38fd1498Szrj 		    bool delete_unreachable)
628*38fd1498Szrj {
629*38fd1498Szrj   int post_order_num = 0;
630*38fd1498Szrj   int count;
631*38fd1498Szrj 
632*38fd1498Szrj   if (include_entry_exit)
633*38fd1498Szrj     post_order[post_order_num++] = EXIT_BLOCK;
634*38fd1498Szrj 
635*38fd1498Szrj   /* Allocate stack for back-tracking up CFG.  */
636*38fd1498Szrj   auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (cfun) + 1);
637*38fd1498Szrj 
638*38fd1498Szrj   /* Allocate bitmap to track nodes that have been visited.  */
639*38fd1498Szrj   auto_sbitmap visited (last_basic_block_for_fn (cfun));
640*38fd1498Szrj 
641*38fd1498Szrj   /* None of the nodes in the CFG have been visited yet.  */
642*38fd1498Szrj   bitmap_clear (visited);
643*38fd1498Szrj 
644*38fd1498Szrj   /* Push the first edge on to the stack.  */
645*38fd1498Szrj   stack.quick_push (ei_start (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs));
646*38fd1498Szrj 
647*38fd1498Szrj   while (!stack.is_empty ())
648*38fd1498Szrj     {
649*38fd1498Szrj       basic_block src;
650*38fd1498Szrj       basic_block dest;
651*38fd1498Szrj 
652*38fd1498Szrj       /* Look at the edge on the top of the stack.  */
653*38fd1498Szrj       edge_iterator ei = stack.last ();
654*38fd1498Szrj       src = ei_edge (ei)->src;
655*38fd1498Szrj       dest = ei_edge (ei)->dest;
656*38fd1498Szrj 
657*38fd1498Szrj       /* Check if the edge destination has been visited yet.  */
658*38fd1498Szrj       if (dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
659*38fd1498Szrj 	  && ! bitmap_bit_p (visited, dest->index))
660*38fd1498Szrj 	{
661*38fd1498Szrj 	  /* Mark that we have visited the destination.  */
662*38fd1498Szrj 	  bitmap_set_bit (visited, dest->index);
663*38fd1498Szrj 
664*38fd1498Szrj 	  if (EDGE_COUNT (dest->succs) > 0)
665*38fd1498Szrj 	    /* Since the DEST node has been visited for the first
666*38fd1498Szrj 	       time, check its successors.  */
667*38fd1498Szrj 	    stack.quick_push (ei_start (dest->succs));
668*38fd1498Szrj 	  else
669*38fd1498Szrj 	    post_order[post_order_num++] = dest->index;
670*38fd1498Szrj 	}
671*38fd1498Szrj       else
672*38fd1498Szrj 	{
673*38fd1498Szrj 	  if (ei_one_before_end_p (ei)
674*38fd1498Szrj 	      && src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
675*38fd1498Szrj 	    post_order[post_order_num++] = src->index;
676*38fd1498Szrj 
677*38fd1498Szrj 	  if (!ei_one_before_end_p (ei))
678*38fd1498Szrj 	    ei_next (&stack.last ());
679*38fd1498Szrj 	  else
680*38fd1498Szrj 	    stack.pop ();
681*38fd1498Szrj 	}
682*38fd1498Szrj     }
683*38fd1498Szrj 
684*38fd1498Szrj   if (include_entry_exit)
685*38fd1498Szrj     {
686*38fd1498Szrj       post_order[post_order_num++] = ENTRY_BLOCK;
687*38fd1498Szrj       count = post_order_num;
688*38fd1498Szrj     }
689*38fd1498Szrj   else
690*38fd1498Szrj     count = post_order_num + 2;
691*38fd1498Szrj 
692*38fd1498Szrj   /* Delete the unreachable blocks if some were found and we are
693*38fd1498Szrj      supposed to do it.  */
694*38fd1498Szrj   if (delete_unreachable && (count != n_basic_blocks_for_fn (cfun)))
695*38fd1498Szrj     {
696*38fd1498Szrj       basic_block b;
697*38fd1498Szrj       basic_block next_bb;
698*38fd1498Szrj       for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
699*38fd1498Szrj 	   != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
700*38fd1498Szrj 	{
701*38fd1498Szrj 	  next_bb = b->next_bb;
702*38fd1498Szrj 
703*38fd1498Szrj 	  if (!(bitmap_bit_p (visited, b->index)))
704*38fd1498Szrj 	    delete_basic_block (b);
705*38fd1498Szrj 	}
706*38fd1498Szrj 
707*38fd1498Szrj       tidy_fallthru_edges ();
708*38fd1498Szrj     }
709*38fd1498Szrj 
710*38fd1498Szrj   return post_order_num;
711*38fd1498Szrj }
712*38fd1498Szrj 
713*38fd1498Szrj 
714*38fd1498Szrj /* Helper routine for inverted_post_order_compute
715*38fd1498Szrj    flow_dfs_compute_reverse_execute, and the reverse-CFG
716*38fd1498Szrj    deapth first search in dominance.c.
717*38fd1498Szrj    BB has to belong to a region of CFG
718*38fd1498Szrj    unreachable by inverted traversal from the exit.
719*38fd1498Szrj    i.e. there's no control flow path from ENTRY to EXIT
720*38fd1498Szrj    that contains this BB.
721*38fd1498Szrj    This can happen in two cases - if there's an infinite loop
722*38fd1498Szrj    or if there's a block that has no successor
723*38fd1498Szrj    (call to a function with no return).
724*38fd1498Szrj    Some RTL passes deal with this condition by
725*38fd1498Szrj    calling connect_infinite_loops_to_exit () and/or
726*38fd1498Szrj    add_noreturn_fake_exit_edges ().
727*38fd1498Szrj    However, those methods involve modifying the CFG itself
728*38fd1498Szrj    which may not be desirable.
729*38fd1498Szrj    Hence, we deal with the infinite loop/no return cases
730*38fd1498Szrj    by identifying a unique basic block that can reach all blocks
731*38fd1498Szrj    in such a region by inverted traversal.
732*38fd1498Szrj    This function returns a basic block that guarantees
733*38fd1498Szrj    that all blocks in the region are reachable
734*38fd1498Szrj    by starting an inverted traversal from the returned block.  */
735*38fd1498Szrj 
736*38fd1498Szrj basic_block
dfs_find_deadend(basic_block bb)737*38fd1498Szrj dfs_find_deadend (basic_block bb)
738*38fd1498Szrj {
739*38fd1498Szrj   auto_bitmap visited;
740*38fd1498Szrj   basic_block next = bb;
741*38fd1498Szrj 
742*38fd1498Szrj   for (;;)
743*38fd1498Szrj     {
744*38fd1498Szrj       if (EDGE_COUNT (next->succs) == 0)
745*38fd1498Szrj 	return next;
746*38fd1498Szrj 
747*38fd1498Szrj       if (! bitmap_set_bit (visited, next->index))
748*38fd1498Szrj 	return bb;
749*38fd1498Szrj 
750*38fd1498Szrj       bb = next;
751*38fd1498Szrj       /* If we are in an analyzed cycle make sure to try exiting it.
752*38fd1498Szrj          Note this is a heuristic only and expected to work when loop
753*38fd1498Szrj 	 fixup is needed as well.  */
754*38fd1498Szrj       if (! bb->loop_father
755*38fd1498Szrj 	  || ! loop_outer (bb->loop_father))
756*38fd1498Szrj 	next = EDGE_SUCC (bb, 0)->dest;
757*38fd1498Szrj       else
758*38fd1498Szrj 	{
759*38fd1498Szrj 	  edge_iterator ei;
760*38fd1498Szrj 	  edge e;
761*38fd1498Szrj 	  FOR_EACH_EDGE (e, ei, bb->succs)
762*38fd1498Szrj 	    if (loop_exit_edge_p (bb->loop_father, e))
763*38fd1498Szrj 	      break;
764*38fd1498Szrj 	  next = e ? e->dest : EDGE_SUCC (bb, 0)->dest;
765*38fd1498Szrj 	}
766*38fd1498Szrj     }
767*38fd1498Szrj 
768*38fd1498Szrj   gcc_unreachable ();
769*38fd1498Szrj }
770*38fd1498Szrj 
771*38fd1498Szrj 
772*38fd1498Szrj /* Compute the reverse top sort order of the inverted CFG
773*38fd1498Szrj    i.e. starting from the exit block and following the edges backward
774*38fd1498Szrj    (from successors to predecessors).
775*38fd1498Szrj    This ordering can be used for forward dataflow problems among others.
776*38fd1498Szrj 
777*38fd1498Szrj    Optionally if START_POINTS is specified, start from exit block and all
778*38fd1498Szrj    basic blocks in START_POINTS.  This is used by CD-DCE.
779*38fd1498Szrj 
780*38fd1498Szrj    This function assumes that all blocks in the CFG are reachable
781*38fd1498Szrj    from the ENTRY (but not necessarily from EXIT).
782*38fd1498Szrj 
783*38fd1498Szrj    If there's an infinite loop,
784*38fd1498Szrj    a simple inverted traversal starting from the blocks
785*38fd1498Szrj    with no successors can't visit all blocks.
786*38fd1498Szrj    To solve this problem, we first do inverted traversal
787*38fd1498Szrj    starting from the blocks with no successor.
788*38fd1498Szrj    And if there's any block left that's not visited by the regular
789*38fd1498Szrj    inverted traversal from EXIT,
790*38fd1498Szrj    those blocks are in such problematic region.
791*38fd1498Szrj    Among those, we find one block that has
792*38fd1498Szrj    any visited predecessor (which is an entry into such a region),
793*38fd1498Szrj    and start looking for a "dead end" from that block
794*38fd1498Szrj    and do another inverted traversal from that block.  */
795*38fd1498Szrj 
796*38fd1498Szrj void
inverted_post_order_compute(vec<int> * post_order,sbitmap * start_points)797*38fd1498Szrj inverted_post_order_compute (vec<int> *post_order,
798*38fd1498Szrj 			     sbitmap *start_points)
799*38fd1498Szrj {
800*38fd1498Szrj   basic_block bb;
801*38fd1498Szrj   post_order->reserve_exact (n_basic_blocks_for_fn (cfun));
802*38fd1498Szrj 
803*38fd1498Szrj   if (flag_checking)
804*38fd1498Szrj     verify_no_unreachable_blocks ();
805*38fd1498Szrj 
806*38fd1498Szrj   /* Allocate stack for back-tracking up CFG.  */
807*38fd1498Szrj   auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (cfun) + 1);
808*38fd1498Szrj 
809*38fd1498Szrj   /* Allocate bitmap to track nodes that have been visited.  */
810*38fd1498Szrj   auto_sbitmap visited (last_basic_block_for_fn (cfun));
811*38fd1498Szrj 
812*38fd1498Szrj   /* None of the nodes in the CFG have been visited yet.  */
813*38fd1498Szrj   bitmap_clear (visited);
814*38fd1498Szrj 
815*38fd1498Szrj   if (start_points)
816*38fd1498Szrj     {
817*38fd1498Szrj       FOR_ALL_BB_FN (bb, cfun)
818*38fd1498Szrj         if (bitmap_bit_p (*start_points, bb->index)
819*38fd1498Szrj 	    && EDGE_COUNT (bb->preds) > 0)
820*38fd1498Szrj 	  {
821*38fd1498Szrj 	    stack.quick_push (ei_start (bb->preds));
822*38fd1498Szrj             bitmap_set_bit (visited, bb->index);
823*38fd1498Szrj 	  }
824*38fd1498Szrj       if (EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
825*38fd1498Szrj 	{
826*38fd1498Szrj 	  stack.quick_push (ei_start (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds));
827*38fd1498Szrj           bitmap_set_bit (visited, EXIT_BLOCK_PTR_FOR_FN (cfun)->index);
828*38fd1498Szrj 	}
829*38fd1498Szrj     }
830*38fd1498Szrj   else
831*38fd1498Szrj   /* Put all blocks that have no successor into the initial work list.  */
832*38fd1498Szrj   FOR_ALL_BB_FN (bb, cfun)
833*38fd1498Szrj     if (EDGE_COUNT (bb->succs) == 0)
834*38fd1498Szrj       {
835*38fd1498Szrj         /* Push the initial edge on to the stack.  */
836*38fd1498Szrj         if (EDGE_COUNT (bb->preds) > 0)
837*38fd1498Szrj           {
838*38fd1498Szrj 	    stack.quick_push (ei_start (bb->preds));
839*38fd1498Szrj             bitmap_set_bit (visited, bb->index);
840*38fd1498Szrj           }
841*38fd1498Szrj       }
842*38fd1498Szrj 
843*38fd1498Szrj   do
844*38fd1498Szrj     {
845*38fd1498Szrj       bool has_unvisited_bb = false;
846*38fd1498Szrj 
847*38fd1498Szrj       /* The inverted traversal loop. */
848*38fd1498Szrj       while (!stack.is_empty ())
849*38fd1498Szrj         {
850*38fd1498Szrj           edge_iterator ei;
851*38fd1498Szrj           basic_block pred;
852*38fd1498Szrj 
853*38fd1498Szrj           /* Look at the edge on the top of the stack.  */
854*38fd1498Szrj 	  ei = stack.last ();
855*38fd1498Szrj           bb = ei_edge (ei)->dest;
856*38fd1498Szrj           pred = ei_edge (ei)->src;
857*38fd1498Szrj 
858*38fd1498Szrj           /* Check if the predecessor has been visited yet.  */
859*38fd1498Szrj           if (! bitmap_bit_p (visited, pred->index))
860*38fd1498Szrj             {
861*38fd1498Szrj               /* Mark that we have visited the destination.  */
862*38fd1498Szrj               bitmap_set_bit (visited, pred->index);
863*38fd1498Szrj 
864*38fd1498Szrj               if (EDGE_COUNT (pred->preds) > 0)
865*38fd1498Szrj                 /* Since the predecessor node has been visited for the first
866*38fd1498Szrj                    time, check its predecessors.  */
867*38fd1498Szrj 		stack.quick_push (ei_start (pred->preds));
868*38fd1498Szrj               else
869*38fd1498Szrj 		post_order->quick_push (pred->index);
870*38fd1498Szrj             }
871*38fd1498Szrj           else
872*38fd1498Szrj             {
873*38fd1498Szrj 	      if (bb != EXIT_BLOCK_PTR_FOR_FN (cfun)
874*38fd1498Szrj 		  && ei_one_before_end_p (ei))
875*38fd1498Szrj 		post_order->quick_push (bb->index);
876*38fd1498Szrj 
877*38fd1498Szrj               if (!ei_one_before_end_p (ei))
878*38fd1498Szrj 		ei_next (&stack.last ());
879*38fd1498Szrj               else
880*38fd1498Szrj 		stack.pop ();
881*38fd1498Szrj             }
882*38fd1498Szrj         }
883*38fd1498Szrj 
884*38fd1498Szrj       /* Detect any infinite loop and activate the kludge.
885*38fd1498Szrj          Note that this doesn't check EXIT_BLOCK itself
886*38fd1498Szrj 	 since EXIT_BLOCK is always added after the outer do-while loop.  */
887*38fd1498Szrj       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
888*38fd1498Szrj 		      EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
889*38fd1498Szrj         if (!bitmap_bit_p (visited, bb->index))
890*38fd1498Szrj           {
891*38fd1498Szrj             has_unvisited_bb = true;
892*38fd1498Szrj 
893*38fd1498Szrj             if (EDGE_COUNT (bb->preds) > 0)
894*38fd1498Szrj               {
895*38fd1498Szrj                 edge_iterator ei;
896*38fd1498Szrj                 edge e;
897*38fd1498Szrj                 basic_block visited_pred = NULL;
898*38fd1498Szrj 
899*38fd1498Szrj                 /* Find an already visited predecessor.  */
900*38fd1498Szrj                 FOR_EACH_EDGE (e, ei, bb->preds)
901*38fd1498Szrj                   {
902*38fd1498Szrj                     if (bitmap_bit_p (visited, e->src->index))
903*38fd1498Szrj                       visited_pred = e->src;
904*38fd1498Szrj                   }
905*38fd1498Szrj 
906*38fd1498Szrj                 if (visited_pred)
907*38fd1498Szrj                   {
908*38fd1498Szrj                     basic_block be = dfs_find_deadend (bb);
909*38fd1498Szrj                     gcc_assert (be != NULL);
910*38fd1498Szrj                     bitmap_set_bit (visited, be->index);
911*38fd1498Szrj 		    stack.quick_push (ei_start (be->preds));
912*38fd1498Szrj                     break;
913*38fd1498Szrj                   }
914*38fd1498Szrj               }
915*38fd1498Szrj           }
916*38fd1498Szrj 
917*38fd1498Szrj       if (has_unvisited_bb && stack.is_empty ())
918*38fd1498Szrj         {
919*38fd1498Szrj 	  /* No blocks are reachable from EXIT at all.
920*38fd1498Szrj              Find a dead-end from the ENTRY, and restart the iteration. */
921*38fd1498Szrj 	  basic_block be = dfs_find_deadend (ENTRY_BLOCK_PTR_FOR_FN (cfun));
922*38fd1498Szrj           gcc_assert (be != NULL);
923*38fd1498Szrj           bitmap_set_bit (visited, be->index);
924*38fd1498Szrj 	  stack.quick_push (ei_start (be->preds));
925*38fd1498Szrj         }
926*38fd1498Szrj 
927*38fd1498Szrj       /* The only case the below while fires is
928*38fd1498Szrj          when there's an infinite loop.  */
929*38fd1498Szrj     }
930*38fd1498Szrj   while (!stack.is_empty ());
931*38fd1498Szrj 
932*38fd1498Szrj   /* EXIT_BLOCK is always included.  */
933*38fd1498Szrj   post_order->quick_push (EXIT_BLOCK);
934*38fd1498Szrj }
935*38fd1498Szrj 
936*38fd1498Szrj /* Compute the depth first search order of FN and store in the array
937*38fd1498Szrj    PRE_ORDER if nonzero.  If REV_POST_ORDER is nonzero, return the
938*38fd1498Szrj    reverse completion number for each node.  Returns the number of nodes
939*38fd1498Szrj    visited.  A depth first search tries to get as far away from the starting
940*38fd1498Szrj    point as quickly as possible.
941*38fd1498Szrj 
942*38fd1498Szrj    In case the function has unreachable blocks the number of nodes
943*38fd1498Szrj    visited does not include them.
944*38fd1498Szrj 
945*38fd1498Szrj    pre_order is a really a preorder numbering of the graph.
946*38fd1498Szrj    rev_post_order is really a reverse postorder numbering of the graph.  */
947*38fd1498Szrj 
948*38fd1498Szrj int
pre_and_rev_post_order_compute_fn(struct function * fn,int * pre_order,int * rev_post_order,bool include_entry_exit)949*38fd1498Szrj pre_and_rev_post_order_compute_fn (struct function *fn,
950*38fd1498Szrj 				   int *pre_order, int *rev_post_order,
951*38fd1498Szrj 				   bool include_entry_exit)
952*38fd1498Szrj {
953*38fd1498Szrj   int pre_order_num = 0;
954*38fd1498Szrj   int rev_post_order_num = n_basic_blocks_for_fn (cfun) - 1;
955*38fd1498Szrj 
956*38fd1498Szrj   /* Allocate stack for back-tracking up CFG.  */
957*38fd1498Szrj   auto_vec<edge_iterator, 20> stack (n_basic_blocks_for_fn (cfun) + 1);
958*38fd1498Szrj 
959*38fd1498Szrj   if (include_entry_exit)
960*38fd1498Szrj     {
961*38fd1498Szrj       if (pre_order)
962*38fd1498Szrj 	pre_order[pre_order_num] = ENTRY_BLOCK;
963*38fd1498Szrj       pre_order_num++;
964*38fd1498Szrj       if (rev_post_order)
965*38fd1498Szrj 	rev_post_order[rev_post_order_num--] = EXIT_BLOCK;
966*38fd1498Szrj     }
967*38fd1498Szrj   else
968*38fd1498Szrj     rev_post_order_num -= NUM_FIXED_BLOCKS;
969*38fd1498Szrj 
970*38fd1498Szrj   /* Allocate bitmap to track nodes that have been visited.  */
971*38fd1498Szrj   auto_sbitmap visited (last_basic_block_for_fn (cfun));
972*38fd1498Szrj 
973*38fd1498Szrj   /* None of the nodes in the CFG have been visited yet.  */
974*38fd1498Szrj   bitmap_clear (visited);
975*38fd1498Szrj 
976*38fd1498Szrj   /* Push the first edge on to the stack.  */
977*38fd1498Szrj   stack.quick_push (ei_start (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs));
978*38fd1498Szrj 
979*38fd1498Szrj   while (!stack.is_empty ())
980*38fd1498Szrj     {
981*38fd1498Szrj       basic_block src;
982*38fd1498Szrj       basic_block dest;
983*38fd1498Szrj 
984*38fd1498Szrj       /* Look at the edge on the top of the stack.  */
985*38fd1498Szrj       edge_iterator ei = stack.last ();
986*38fd1498Szrj       src = ei_edge (ei)->src;
987*38fd1498Szrj       dest = ei_edge (ei)->dest;
988*38fd1498Szrj 
989*38fd1498Szrj       /* Check if the edge destination has been visited yet.  */
990*38fd1498Szrj       if (dest != EXIT_BLOCK_PTR_FOR_FN (fn)
991*38fd1498Szrj 	  && ! bitmap_bit_p (visited, dest->index))
992*38fd1498Szrj 	{
993*38fd1498Szrj 	  /* Mark that we have visited the destination.  */
994*38fd1498Szrj 	  bitmap_set_bit (visited, dest->index);
995*38fd1498Szrj 
996*38fd1498Szrj 	  if (pre_order)
997*38fd1498Szrj 	    pre_order[pre_order_num] = dest->index;
998*38fd1498Szrj 
999*38fd1498Szrj 	  pre_order_num++;
1000*38fd1498Szrj 
1001*38fd1498Szrj 	  if (EDGE_COUNT (dest->succs) > 0)
1002*38fd1498Szrj 	    /* Since the DEST node has been visited for the first
1003*38fd1498Szrj 	       time, check its successors.  */
1004*38fd1498Szrj 	    stack.quick_push (ei_start (dest->succs));
1005*38fd1498Szrj 	  else if (rev_post_order)
1006*38fd1498Szrj 	    /* There are no successors for the DEST node so assign
1007*38fd1498Szrj 	       its reverse completion number.  */
1008*38fd1498Szrj 	    rev_post_order[rev_post_order_num--] = dest->index;
1009*38fd1498Szrj 	}
1010*38fd1498Szrj       else
1011*38fd1498Szrj 	{
1012*38fd1498Szrj 	  if (ei_one_before_end_p (ei)
1013*38fd1498Szrj 	      && src != ENTRY_BLOCK_PTR_FOR_FN (fn)
1014*38fd1498Szrj 	      && rev_post_order)
1015*38fd1498Szrj 	    /* There are no more successors for the SRC node
1016*38fd1498Szrj 	       so assign its reverse completion number.  */
1017*38fd1498Szrj 	    rev_post_order[rev_post_order_num--] = src->index;
1018*38fd1498Szrj 
1019*38fd1498Szrj 	  if (!ei_one_before_end_p (ei))
1020*38fd1498Szrj 	    ei_next (&stack.last ());
1021*38fd1498Szrj 	  else
1022*38fd1498Szrj 	    stack.pop ();
1023*38fd1498Szrj 	}
1024*38fd1498Szrj     }
1025*38fd1498Szrj 
1026*38fd1498Szrj   if (include_entry_exit)
1027*38fd1498Szrj     {
1028*38fd1498Szrj       if (pre_order)
1029*38fd1498Szrj 	pre_order[pre_order_num] = EXIT_BLOCK;
1030*38fd1498Szrj       pre_order_num++;
1031*38fd1498Szrj       if (rev_post_order)
1032*38fd1498Szrj 	rev_post_order[rev_post_order_num--] = ENTRY_BLOCK;
1033*38fd1498Szrj     }
1034*38fd1498Szrj 
1035*38fd1498Szrj   return pre_order_num;
1036*38fd1498Szrj }
1037*38fd1498Szrj 
1038*38fd1498Szrj /* Like pre_and_rev_post_order_compute_fn but operating on the
1039*38fd1498Szrj    current function and asserting that all nodes were visited.  */
1040*38fd1498Szrj 
1041*38fd1498Szrj int
pre_and_rev_post_order_compute(int * pre_order,int * rev_post_order,bool include_entry_exit)1042*38fd1498Szrj pre_and_rev_post_order_compute (int *pre_order, int *rev_post_order,
1043*38fd1498Szrj 				bool include_entry_exit)
1044*38fd1498Szrj {
1045*38fd1498Szrj   int pre_order_num
1046*38fd1498Szrj     = pre_and_rev_post_order_compute_fn (cfun, pre_order, rev_post_order,
1047*38fd1498Szrj 					 include_entry_exit);
1048*38fd1498Szrj   if (include_entry_exit)
1049*38fd1498Szrj     /* The number of nodes visited should be the number of blocks.  */
1050*38fd1498Szrj     gcc_assert (pre_order_num == n_basic_blocks_for_fn (cfun));
1051*38fd1498Szrj   else
1052*38fd1498Szrj     /* The number of nodes visited should be the number of blocks minus
1053*38fd1498Szrj        the entry and exit blocks which are not visited here.  */
1054*38fd1498Szrj     gcc_assert (pre_order_num
1055*38fd1498Szrj 		== (n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS));
1056*38fd1498Szrj 
1057*38fd1498Szrj   return pre_order_num;
1058*38fd1498Szrj }
1059*38fd1498Szrj 
1060*38fd1498Szrj /* Compute the depth first search order on the _reverse_ graph and
1061*38fd1498Szrj    store in the array DFS_ORDER, marking the nodes visited in VISITED.
1062*38fd1498Szrj    Returns the number of nodes visited.
1063*38fd1498Szrj 
1064*38fd1498Szrj    The computation is split into three pieces:
1065*38fd1498Szrj 
1066*38fd1498Szrj    flow_dfs_compute_reverse_init () creates the necessary data
1067*38fd1498Szrj    structures.
1068*38fd1498Szrj 
1069*38fd1498Szrj    flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1070*38fd1498Szrj    structures.  The block will start the search.
1071*38fd1498Szrj 
1072*38fd1498Szrj    flow_dfs_compute_reverse_execute () continues (or starts) the
1073*38fd1498Szrj    search using the block on the top of the stack, stopping when the
1074*38fd1498Szrj    stack is empty.
1075*38fd1498Szrj 
1076*38fd1498Szrj    flow_dfs_compute_reverse_finish () destroys the necessary data
1077*38fd1498Szrj    structures.
1078*38fd1498Szrj 
1079*38fd1498Szrj    Thus, the user will probably call ..._init(), call ..._add_bb() to
1080*38fd1498Szrj    add a beginning basic block to the stack, call ..._execute(),
1081*38fd1498Szrj    possibly add another bb to the stack and again call ..._execute(),
1082*38fd1498Szrj    ..., and finally call _finish().  */
1083*38fd1498Szrj 
1084*38fd1498Szrj /* Initialize the data structures used for depth-first search on the
1085*38fd1498Szrj    reverse graph.  If INITIALIZE_STACK is nonzero, the exit block is
1086*38fd1498Szrj    added to the basic block stack.  DATA is the current depth-first
1087*38fd1498Szrj    search context.  If INITIALIZE_STACK is nonzero, there is an
1088*38fd1498Szrj    element on the stack.  */
1089*38fd1498Szrj 
depth_first_search()1090*38fd1498Szrj depth_first_search::depth_first_search () :
1091*38fd1498Szrj   m_stack (n_basic_blocks_for_fn (cfun)),
1092*38fd1498Szrj   m_visited_blocks (last_basic_block_for_fn (cfun))
1093*38fd1498Szrj {
1094*38fd1498Szrj   bitmap_clear (m_visited_blocks);
1095*38fd1498Szrj }
1096*38fd1498Szrj 
1097*38fd1498Szrj /* Add the specified basic block to the top of the dfs data
1098*38fd1498Szrj    structures.  When the search continues, it will start at the
1099*38fd1498Szrj    block.  */
1100*38fd1498Szrj 
1101*38fd1498Szrj void
add_bb(basic_block bb)1102*38fd1498Szrj depth_first_search::add_bb (basic_block bb)
1103*38fd1498Szrj {
1104*38fd1498Szrj   m_stack.quick_push (bb);
1105*38fd1498Szrj   bitmap_set_bit (m_visited_blocks, bb->index);
1106*38fd1498Szrj }
1107*38fd1498Szrj 
1108*38fd1498Szrj /* Continue the depth-first search through the reverse graph starting with the
1109*38fd1498Szrj    block at the stack's top and ending when the stack is empty.  Visited nodes
1110*38fd1498Szrj    are marked.  Returns an unvisited basic block, or NULL if there is none
1111*38fd1498Szrj    available.  */
1112*38fd1498Szrj 
1113*38fd1498Szrj basic_block
execute(basic_block last_unvisited)1114*38fd1498Szrj depth_first_search::execute (basic_block last_unvisited)
1115*38fd1498Szrj {
1116*38fd1498Szrj   basic_block bb;
1117*38fd1498Szrj   edge e;
1118*38fd1498Szrj   edge_iterator ei;
1119*38fd1498Szrj 
1120*38fd1498Szrj   while (!m_stack.is_empty ())
1121*38fd1498Szrj     {
1122*38fd1498Szrj       bb = m_stack.pop ();
1123*38fd1498Szrj 
1124*38fd1498Szrj       /* Perform depth-first search on adjacent vertices.  */
1125*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->preds)
1126*38fd1498Szrj 	if (!bitmap_bit_p (m_visited_blocks, e->src->index))
1127*38fd1498Szrj 	  add_bb (e->src);
1128*38fd1498Szrj     }
1129*38fd1498Szrj 
1130*38fd1498Szrj   /* Determine if there are unvisited basic blocks.  */
1131*38fd1498Szrj   FOR_BB_BETWEEN (bb, last_unvisited, NULL, prev_bb)
1132*38fd1498Szrj     if (!bitmap_bit_p (m_visited_blocks, bb->index))
1133*38fd1498Szrj       return bb;
1134*38fd1498Szrj 
1135*38fd1498Szrj   return NULL;
1136*38fd1498Szrj }
1137*38fd1498Szrj 
1138*38fd1498Szrj /* Performs dfs search from BB over vertices satisfying PREDICATE;
1139*38fd1498Szrj    if REVERSE, go against direction of edges.  Returns number of blocks
1140*38fd1498Szrj    found and their list in RSLT.  RSLT can contain at most RSLT_MAX items.  */
1141*38fd1498Szrj int
dfs_enumerate_from(basic_block bb,int reverse,bool (* predicate)(const_basic_block,const void *),basic_block * rslt,int rslt_max,const void * data)1142*38fd1498Szrj dfs_enumerate_from (basic_block bb, int reverse,
1143*38fd1498Szrj 		    bool (*predicate) (const_basic_block, const void *),
1144*38fd1498Szrj 		    basic_block *rslt, int rslt_max, const void *data)
1145*38fd1498Szrj {
1146*38fd1498Szrj   basic_block *st, lbb;
1147*38fd1498Szrj   int sp = 0, tv = 0;
1148*38fd1498Szrj   unsigned size;
1149*38fd1498Szrj 
1150*38fd1498Szrj   /* A bitmap to keep track of visited blocks.  Allocating it each time
1151*38fd1498Szrj      this function is called is not possible, since dfs_enumerate_from
1152*38fd1498Szrj      is often used on small (almost) disjoint parts of cfg (bodies of
1153*38fd1498Szrj      loops), and allocating a large sbitmap would lead to quadratic
1154*38fd1498Szrj      behavior.  */
1155*38fd1498Szrj   static sbitmap visited;
1156*38fd1498Szrj   static unsigned v_size;
1157*38fd1498Szrj 
1158*38fd1498Szrj #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1159*38fd1498Szrj #define UNMARK_VISITED(BB) (bitmap_clear_bit (visited, (BB)->index))
1160*38fd1498Szrj #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1161*38fd1498Szrj 
1162*38fd1498Szrj   /* Resize the VISITED sbitmap if necessary.  */
1163*38fd1498Szrj   size = last_basic_block_for_fn (cfun);
1164*38fd1498Szrj   if (size < 10)
1165*38fd1498Szrj     size = 10;
1166*38fd1498Szrj 
1167*38fd1498Szrj   if (!visited)
1168*38fd1498Szrj     {
1169*38fd1498Szrj 
1170*38fd1498Szrj       visited = sbitmap_alloc (size);
1171*38fd1498Szrj       bitmap_clear (visited);
1172*38fd1498Szrj       v_size = size;
1173*38fd1498Szrj     }
1174*38fd1498Szrj   else if (v_size < size)
1175*38fd1498Szrj     {
1176*38fd1498Szrj       /* Ensure that we increase the size of the sbitmap exponentially.  */
1177*38fd1498Szrj       if (2 * v_size > size)
1178*38fd1498Szrj 	size = 2 * v_size;
1179*38fd1498Szrj 
1180*38fd1498Szrj       visited = sbitmap_resize (visited, size, 0);
1181*38fd1498Szrj       v_size = size;
1182*38fd1498Szrj     }
1183*38fd1498Szrj 
1184*38fd1498Szrj   st = XNEWVEC (basic_block, rslt_max);
1185*38fd1498Szrj   rslt[tv++] = st[sp++] = bb;
1186*38fd1498Szrj   MARK_VISITED (bb);
1187*38fd1498Szrj   while (sp)
1188*38fd1498Szrj     {
1189*38fd1498Szrj       edge e;
1190*38fd1498Szrj       edge_iterator ei;
1191*38fd1498Szrj       lbb = st[--sp];
1192*38fd1498Szrj       if (reverse)
1193*38fd1498Szrj 	{
1194*38fd1498Szrj 	  FOR_EACH_EDGE (e, ei, lbb->preds)
1195*38fd1498Szrj 	    if (!VISITED_P (e->src) && predicate (e->src, data))
1196*38fd1498Szrj 	      {
1197*38fd1498Szrj 		gcc_assert (tv != rslt_max);
1198*38fd1498Szrj 		rslt[tv++] = st[sp++] = e->src;
1199*38fd1498Szrj 		MARK_VISITED (e->src);
1200*38fd1498Szrj 	      }
1201*38fd1498Szrj 	}
1202*38fd1498Szrj       else
1203*38fd1498Szrj 	{
1204*38fd1498Szrj 	  FOR_EACH_EDGE (e, ei, lbb->succs)
1205*38fd1498Szrj 	    if (!VISITED_P (e->dest) && predicate (e->dest, data))
1206*38fd1498Szrj 	      {
1207*38fd1498Szrj 		gcc_assert (tv != rslt_max);
1208*38fd1498Szrj 		rslt[tv++] = st[sp++] = e->dest;
1209*38fd1498Szrj 		MARK_VISITED (e->dest);
1210*38fd1498Szrj 	      }
1211*38fd1498Szrj 	}
1212*38fd1498Szrj     }
1213*38fd1498Szrj   free (st);
1214*38fd1498Szrj   for (sp = 0; sp < tv; sp++)
1215*38fd1498Szrj     UNMARK_VISITED (rslt[sp]);
1216*38fd1498Szrj   return tv;
1217*38fd1498Szrj #undef MARK_VISITED
1218*38fd1498Szrj #undef UNMARK_VISITED
1219*38fd1498Szrj #undef VISITED_P
1220*38fd1498Szrj }
1221*38fd1498Szrj 
1222*38fd1498Szrj 
1223*38fd1498Szrj /* Compute dominance frontiers, ala Harvey, Ferrante, et al.
1224*38fd1498Szrj 
1225*38fd1498Szrj    This algorithm can be found in Timothy Harvey's PhD thesis, at
1226*38fd1498Szrj    http://www.cs.rice.edu/~harv/dissertation.pdf in the section on iterative
1227*38fd1498Szrj    dominance algorithms.
1228*38fd1498Szrj 
1229*38fd1498Szrj    First, we identify each join point, j (any node with more than one
1230*38fd1498Szrj    incoming edge is a join point).
1231*38fd1498Szrj 
1232*38fd1498Szrj    We then examine each predecessor, p, of j and walk up the dominator tree
1233*38fd1498Szrj    starting at p.
1234*38fd1498Szrj 
1235*38fd1498Szrj    We stop the walk when we reach j's immediate dominator - j is in the
1236*38fd1498Szrj    dominance frontier of each of  the nodes in the walk, except for j's
1237*38fd1498Szrj    immediate dominator. Intuitively, all of the rest of j's dominators are
1238*38fd1498Szrj    shared by j's predecessors as well.
1239*38fd1498Szrj    Since they dominate j, they will not have j in their dominance frontiers.
1240*38fd1498Szrj 
1241*38fd1498Szrj    The number of nodes touched by this algorithm is equal to the size
1242*38fd1498Szrj    of the dominance frontiers, no more, no less.
1243*38fd1498Szrj */
1244*38fd1498Szrj 
1245*38fd1498Szrj 
1246*38fd1498Szrj static void
compute_dominance_frontiers_1(bitmap_head * frontiers)1247*38fd1498Szrj compute_dominance_frontiers_1 (bitmap_head *frontiers)
1248*38fd1498Szrj {
1249*38fd1498Szrj   edge p;
1250*38fd1498Szrj   edge_iterator ei;
1251*38fd1498Szrj   basic_block b;
1252*38fd1498Szrj   FOR_EACH_BB_FN (b, cfun)
1253*38fd1498Szrj     {
1254*38fd1498Szrj       if (EDGE_COUNT (b->preds) >= 2)
1255*38fd1498Szrj 	{
1256*38fd1498Szrj 	  FOR_EACH_EDGE (p, ei, b->preds)
1257*38fd1498Szrj 	    {
1258*38fd1498Szrj 	      basic_block runner = p->src;
1259*38fd1498Szrj 	      basic_block domsb;
1260*38fd1498Szrj 	      if (runner == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1261*38fd1498Szrj 		continue;
1262*38fd1498Szrj 
1263*38fd1498Szrj 	      domsb = get_immediate_dominator (CDI_DOMINATORS, b);
1264*38fd1498Szrj 	      while (runner != domsb)
1265*38fd1498Szrj 		{
1266*38fd1498Szrj 		  if (!bitmap_set_bit (&frontiers[runner->index],
1267*38fd1498Szrj 				       b->index))
1268*38fd1498Szrj 		    break;
1269*38fd1498Szrj 		  runner = get_immediate_dominator (CDI_DOMINATORS,
1270*38fd1498Szrj 						    runner);
1271*38fd1498Szrj 		}
1272*38fd1498Szrj 	    }
1273*38fd1498Szrj 	}
1274*38fd1498Szrj     }
1275*38fd1498Szrj }
1276*38fd1498Szrj 
1277*38fd1498Szrj 
1278*38fd1498Szrj void
compute_dominance_frontiers(bitmap_head * frontiers)1279*38fd1498Szrj compute_dominance_frontiers (bitmap_head *frontiers)
1280*38fd1498Szrj {
1281*38fd1498Szrj   timevar_push (TV_DOM_FRONTIERS);
1282*38fd1498Szrj 
1283*38fd1498Szrj   compute_dominance_frontiers_1 (frontiers);
1284*38fd1498Szrj 
1285*38fd1498Szrj   timevar_pop (TV_DOM_FRONTIERS);
1286*38fd1498Szrj }
1287*38fd1498Szrj 
1288*38fd1498Szrj /* Given a set of blocks with variable definitions (DEF_BLOCKS),
1289*38fd1498Szrj    return a bitmap with all the blocks in the iterated dominance
1290*38fd1498Szrj    frontier of the blocks in DEF_BLOCKS.  DFS contains dominance
1291*38fd1498Szrj    frontier information as returned by compute_dominance_frontiers.
1292*38fd1498Szrj 
1293*38fd1498Szrj    The resulting set of blocks are the potential sites where PHI nodes
1294*38fd1498Szrj    are needed.  The caller is responsible for freeing the memory
1295*38fd1498Szrj    allocated for the return value.  */
1296*38fd1498Szrj 
1297*38fd1498Szrj bitmap
compute_idf(bitmap def_blocks,bitmap_head * dfs)1298*38fd1498Szrj compute_idf (bitmap def_blocks, bitmap_head *dfs)
1299*38fd1498Szrj {
1300*38fd1498Szrj   bitmap_iterator bi;
1301*38fd1498Szrj   unsigned bb_index, i;
1302*38fd1498Szrj   bitmap phi_insertion_points;
1303*38fd1498Szrj 
1304*38fd1498Szrj   /* Each block can appear at most twice on the work-stack.  */
1305*38fd1498Szrj   auto_vec<int> work_stack (2 * n_basic_blocks_for_fn (cfun));
1306*38fd1498Szrj   phi_insertion_points = BITMAP_ALLOC (NULL);
1307*38fd1498Szrj 
1308*38fd1498Szrj   /* Seed the work list with all the blocks in DEF_BLOCKS.  We use
1309*38fd1498Szrj      vec::quick_push here for speed.  This is safe because we know that
1310*38fd1498Szrj      the number of definition blocks is no greater than the number of
1311*38fd1498Szrj      basic blocks, which is the initial capacity of WORK_STACK.  */
1312*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1313*38fd1498Szrj     work_stack.quick_push (bb_index);
1314*38fd1498Szrj 
1315*38fd1498Szrj   /* Pop a block off the worklist, add every block that appears in
1316*38fd1498Szrj      the original block's DF that we have not already processed to
1317*38fd1498Szrj      the worklist.  Iterate until the worklist is empty.   Blocks
1318*38fd1498Szrj      which are added to the worklist are potential sites for
1319*38fd1498Szrj      PHI nodes.  */
1320*38fd1498Szrj   while (work_stack.length () > 0)
1321*38fd1498Szrj     {
1322*38fd1498Szrj       bb_index = work_stack.pop ();
1323*38fd1498Szrj 
1324*38fd1498Szrj       /* Since the registration of NEW -> OLD name mappings is done
1325*38fd1498Szrj 	 separately from the call to update_ssa, when updating the SSA
1326*38fd1498Szrj 	 form, the basic blocks where new and/or old names are defined
1327*38fd1498Szrj 	 may have disappeared by CFG cleanup calls.  In this case,
1328*38fd1498Szrj 	 we may pull a non-existing block from the work stack.  */
1329*38fd1498Szrj       gcc_checking_assert (bb_index
1330*38fd1498Szrj 			   < (unsigned) last_basic_block_for_fn (cfun));
1331*38fd1498Szrj 
1332*38fd1498Szrj       EXECUTE_IF_AND_COMPL_IN_BITMAP (&dfs[bb_index], phi_insertion_points,
1333*38fd1498Szrj 	                              0, i, bi)
1334*38fd1498Szrj 	{
1335*38fd1498Szrj 	  work_stack.quick_push (i);
1336*38fd1498Szrj 	  bitmap_set_bit (phi_insertion_points, i);
1337*38fd1498Szrj 	}
1338*38fd1498Szrj     }
1339*38fd1498Szrj 
1340*38fd1498Szrj   return phi_insertion_points;
1341*38fd1498Szrj }
1342*38fd1498Szrj 
1343*38fd1498Szrj /* Intersection and union of preds/succs for sbitmap based data flow
1344*38fd1498Szrj    solvers.  All four functions defined below take the same arguments:
1345*38fd1498Szrj    B is the basic block to perform the operation for.  DST is the
1346*38fd1498Szrj    target sbitmap, i.e. the result.  SRC is an sbitmap vector of size
1347*38fd1498Szrj    last_basic_block so that it can be indexed with basic block indices.
1348*38fd1498Szrj    DST may be (but does not have to be) SRC[B->index].  */
1349*38fd1498Szrj 
1350*38fd1498Szrj /* Set the bitmap DST to the intersection of SRC of successors of
1351*38fd1498Szrj    basic block B.  */
1352*38fd1498Szrj 
1353*38fd1498Szrj void
bitmap_intersection_of_succs(sbitmap dst,sbitmap * src,basic_block b)1354*38fd1498Szrj bitmap_intersection_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1355*38fd1498Szrj {
1356*38fd1498Szrj   unsigned int set_size = dst->size;
1357*38fd1498Szrj   edge e;
1358*38fd1498Szrj   unsigned ix;
1359*38fd1498Szrj 
1360*38fd1498Szrj   for (e = NULL, ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1361*38fd1498Szrj     {
1362*38fd1498Szrj       e = EDGE_SUCC (b, ix);
1363*38fd1498Szrj       if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1364*38fd1498Szrj 	continue;
1365*38fd1498Szrj 
1366*38fd1498Szrj       bitmap_copy (dst, src[e->dest->index]);
1367*38fd1498Szrj       break;
1368*38fd1498Szrj     }
1369*38fd1498Szrj 
1370*38fd1498Szrj   if (e == 0)
1371*38fd1498Szrj     bitmap_ones (dst);
1372*38fd1498Szrj   else
1373*38fd1498Szrj     for (++ix; ix < EDGE_COUNT (b->succs); ix++)
1374*38fd1498Szrj       {
1375*38fd1498Szrj 	unsigned int i;
1376*38fd1498Szrj 	SBITMAP_ELT_TYPE *p, *r;
1377*38fd1498Szrj 
1378*38fd1498Szrj 	e = EDGE_SUCC (b, ix);
1379*38fd1498Szrj 	if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1380*38fd1498Szrj 	  continue;
1381*38fd1498Szrj 
1382*38fd1498Szrj 	p = src[e->dest->index]->elms;
1383*38fd1498Szrj 	r = dst->elms;
1384*38fd1498Szrj 	for (i = 0; i < set_size; i++)
1385*38fd1498Szrj 	  *r++ &= *p++;
1386*38fd1498Szrj       }
1387*38fd1498Szrj }
1388*38fd1498Szrj 
1389*38fd1498Szrj /* Set the bitmap DST to the intersection of SRC of predecessors of
1390*38fd1498Szrj    basic block B.  */
1391*38fd1498Szrj 
1392*38fd1498Szrj void
bitmap_intersection_of_preds(sbitmap dst,sbitmap * src,basic_block b)1393*38fd1498Szrj bitmap_intersection_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1394*38fd1498Szrj {
1395*38fd1498Szrj   unsigned int set_size = dst->size;
1396*38fd1498Szrj   edge e;
1397*38fd1498Szrj   unsigned ix;
1398*38fd1498Szrj 
1399*38fd1498Szrj   for (e = NULL, ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1400*38fd1498Szrj     {
1401*38fd1498Szrj       e = EDGE_PRED (b, ix);
1402*38fd1498Szrj       if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1403*38fd1498Szrj 	continue;
1404*38fd1498Szrj 
1405*38fd1498Szrj       bitmap_copy (dst, src[e->src->index]);
1406*38fd1498Szrj       break;
1407*38fd1498Szrj     }
1408*38fd1498Szrj 
1409*38fd1498Szrj   if (e == 0)
1410*38fd1498Szrj     bitmap_ones (dst);
1411*38fd1498Szrj   else
1412*38fd1498Szrj     for (++ix; ix < EDGE_COUNT (b->preds); ix++)
1413*38fd1498Szrj       {
1414*38fd1498Szrj 	unsigned int i;
1415*38fd1498Szrj 	SBITMAP_ELT_TYPE *p, *r;
1416*38fd1498Szrj 
1417*38fd1498Szrj 	e = EDGE_PRED (b, ix);
1418*38fd1498Szrj 	if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1419*38fd1498Szrj 	  continue;
1420*38fd1498Szrj 
1421*38fd1498Szrj 	p = src[e->src->index]->elms;
1422*38fd1498Szrj 	r = dst->elms;
1423*38fd1498Szrj 	for (i = 0; i < set_size; i++)
1424*38fd1498Szrj 	  *r++ &= *p++;
1425*38fd1498Szrj       }
1426*38fd1498Szrj }
1427*38fd1498Szrj 
1428*38fd1498Szrj /* Set the bitmap DST to the union of SRC of successors of
1429*38fd1498Szrj    basic block B.  */
1430*38fd1498Szrj 
1431*38fd1498Szrj void
bitmap_union_of_succs(sbitmap dst,sbitmap * src,basic_block b)1432*38fd1498Szrj bitmap_union_of_succs (sbitmap dst, sbitmap *src, basic_block b)
1433*38fd1498Szrj {
1434*38fd1498Szrj   unsigned int set_size = dst->size;
1435*38fd1498Szrj   edge e;
1436*38fd1498Szrj   unsigned ix;
1437*38fd1498Szrj 
1438*38fd1498Szrj   for (ix = 0; ix < EDGE_COUNT (b->succs); ix++)
1439*38fd1498Szrj     {
1440*38fd1498Szrj       e = EDGE_SUCC (b, ix);
1441*38fd1498Szrj       if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1442*38fd1498Szrj 	continue;
1443*38fd1498Szrj 
1444*38fd1498Szrj       bitmap_copy (dst, src[e->dest->index]);
1445*38fd1498Szrj       break;
1446*38fd1498Szrj     }
1447*38fd1498Szrj 
1448*38fd1498Szrj   if (ix == EDGE_COUNT (b->succs))
1449*38fd1498Szrj     bitmap_clear (dst);
1450*38fd1498Szrj   else
1451*38fd1498Szrj     for (ix++; ix < EDGE_COUNT (b->succs); ix++)
1452*38fd1498Szrj       {
1453*38fd1498Szrj 	unsigned int i;
1454*38fd1498Szrj 	SBITMAP_ELT_TYPE *p, *r;
1455*38fd1498Szrj 
1456*38fd1498Szrj 	e = EDGE_SUCC (b, ix);
1457*38fd1498Szrj 	if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1458*38fd1498Szrj 	  continue;
1459*38fd1498Szrj 
1460*38fd1498Szrj 	p = src[e->dest->index]->elms;
1461*38fd1498Szrj 	r = dst->elms;
1462*38fd1498Szrj 	for (i = 0; i < set_size; i++)
1463*38fd1498Szrj 	  *r++ |= *p++;
1464*38fd1498Szrj       }
1465*38fd1498Szrj }
1466*38fd1498Szrj 
1467*38fd1498Szrj /* Set the bitmap DST to the union of SRC of predecessors of
1468*38fd1498Szrj    basic block B.  */
1469*38fd1498Szrj 
1470*38fd1498Szrj void
bitmap_union_of_preds(sbitmap dst,sbitmap * src,basic_block b)1471*38fd1498Szrj bitmap_union_of_preds (sbitmap dst, sbitmap *src, basic_block b)
1472*38fd1498Szrj {
1473*38fd1498Szrj   unsigned int set_size = dst->size;
1474*38fd1498Szrj   edge e;
1475*38fd1498Szrj   unsigned ix;
1476*38fd1498Szrj 
1477*38fd1498Szrj   for (ix = 0; ix < EDGE_COUNT (b->preds); ix++)
1478*38fd1498Szrj     {
1479*38fd1498Szrj       e = EDGE_PRED (b, ix);
1480*38fd1498Szrj       if (e->src== ENTRY_BLOCK_PTR_FOR_FN (cfun))
1481*38fd1498Szrj 	continue;
1482*38fd1498Szrj 
1483*38fd1498Szrj       bitmap_copy (dst, src[e->src->index]);
1484*38fd1498Szrj       break;
1485*38fd1498Szrj     }
1486*38fd1498Szrj 
1487*38fd1498Szrj   if (ix == EDGE_COUNT (b->preds))
1488*38fd1498Szrj     bitmap_clear (dst);
1489*38fd1498Szrj   else
1490*38fd1498Szrj     for (ix++; ix < EDGE_COUNT (b->preds); ix++)
1491*38fd1498Szrj       {
1492*38fd1498Szrj 	unsigned int i;
1493*38fd1498Szrj 	SBITMAP_ELT_TYPE *p, *r;
1494*38fd1498Szrj 
1495*38fd1498Szrj 	e = EDGE_PRED (b, ix);
1496*38fd1498Szrj 	if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1497*38fd1498Szrj 	  continue;
1498*38fd1498Szrj 
1499*38fd1498Szrj 	p = src[e->src->index]->elms;
1500*38fd1498Szrj 	r = dst->elms;
1501*38fd1498Szrj 	for (i = 0; i < set_size; i++)
1502*38fd1498Szrj 	  *r++ |= *p++;
1503*38fd1498Szrj       }
1504*38fd1498Szrj }
1505*38fd1498Szrj 
1506*38fd1498Szrj /* Returns the list of basic blocks in the function in an order that guarantees
1507*38fd1498Szrj    that if a block X has just a single predecessor Y, then Y is after X in the
1508*38fd1498Szrj    ordering.  */
1509*38fd1498Szrj 
1510*38fd1498Szrj basic_block *
single_pred_before_succ_order(void)1511*38fd1498Szrj single_pred_before_succ_order (void)
1512*38fd1498Szrj {
1513*38fd1498Szrj   basic_block x, y;
1514*38fd1498Szrj   basic_block *order = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
1515*38fd1498Szrj   unsigned n = n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS;
1516*38fd1498Szrj   unsigned np, i;
1517*38fd1498Szrj   auto_sbitmap visited (last_basic_block_for_fn (cfun));
1518*38fd1498Szrj 
1519*38fd1498Szrj #define MARK_VISITED(BB) (bitmap_set_bit (visited, (BB)->index))
1520*38fd1498Szrj #define VISITED_P(BB) (bitmap_bit_p (visited, (BB)->index))
1521*38fd1498Szrj 
1522*38fd1498Szrj   bitmap_clear (visited);
1523*38fd1498Szrj 
1524*38fd1498Szrj   MARK_VISITED (ENTRY_BLOCK_PTR_FOR_FN (cfun));
1525*38fd1498Szrj   FOR_EACH_BB_FN (x, cfun)
1526*38fd1498Szrj     {
1527*38fd1498Szrj       if (VISITED_P (x))
1528*38fd1498Szrj 	continue;
1529*38fd1498Szrj 
1530*38fd1498Szrj       /* Walk the predecessors of x as long as they have precisely one
1531*38fd1498Szrj 	 predecessor and add them to the list, so that they get stored
1532*38fd1498Szrj 	 after x.  */
1533*38fd1498Szrj       for (y = x, np = 1;
1534*38fd1498Szrj 	   single_pred_p (y) && !VISITED_P (single_pred (y));
1535*38fd1498Szrj 	   y = single_pred (y))
1536*38fd1498Szrj 	np++;
1537*38fd1498Szrj       for (y = x, i = n - np;
1538*38fd1498Szrj 	   single_pred_p (y) && !VISITED_P (single_pred (y));
1539*38fd1498Szrj 	   y = single_pred (y), i++)
1540*38fd1498Szrj 	{
1541*38fd1498Szrj 	  order[i] = y;
1542*38fd1498Szrj 	  MARK_VISITED (y);
1543*38fd1498Szrj 	}
1544*38fd1498Szrj       order[i] = y;
1545*38fd1498Szrj       MARK_VISITED (y);
1546*38fd1498Szrj 
1547*38fd1498Szrj       gcc_assert (i == n - 1);
1548*38fd1498Szrj       n -= np;
1549*38fd1498Szrj     }
1550*38fd1498Szrj 
1551*38fd1498Szrj   gcc_assert (n == 0);
1552*38fd1498Szrj   return order;
1553*38fd1498Szrj 
1554*38fd1498Szrj #undef MARK_VISITED
1555*38fd1498Szrj #undef VISITED_P
1556*38fd1498Szrj }
1557*38fd1498Szrj 
1558*38fd1498Szrj /* Ignoring loop backedges, if BB has precisely one incoming edge then
1559*38fd1498Szrj    return that edge.  Otherwise return NULL.
1560*38fd1498Szrj 
1561*38fd1498Szrj    When IGNORE_NOT_EXECUTABLE is true, also ignore edges that are not marked
1562*38fd1498Szrj    as executable.  */
1563*38fd1498Szrj 
1564*38fd1498Szrj edge
single_pred_edge_ignoring_loop_edges(basic_block bb,bool ignore_not_executable)1565*38fd1498Szrj single_pred_edge_ignoring_loop_edges (basic_block bb,
1566*38fd1498Szrj 				      bool ignore_not_executable)
1567*38fd1498Szrj {
1568*38fd1498Szrj   edge retval = NULL;
1569*38fd1498Szrj   edge e;
1570*38fd1498Szrj   edge_iterator ei;
1571*38fd1498Szrj 
1572*38fd1498Szrj   FOR_EACH_EDGE (e, ei, bb->preds)
1573*38fd1498Szrj     {
1574*38fd1498Szrj       /* A loop back edge can be identified by the destination of
1575*38fd1498Szrj 	 the edge dominating the source of the edge.  */
1576*38fd1498Szrj       if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
1577*38fd1498Szrj 	continue;
1578*38fd1498Szrj 
1579*38fd1498Szrj       /* We can safely ignore edges that are not executable.  */
1580*38fd1498Szrj       if (ignore_not_executable
1581*38fd1498Szrj 	  && (e->flags & EDGE_EXECUTABLE) == 0)
1582*38fd1498Szrj 	continue;
1583*38fd1498Szrj 
1584*38fd1498Szrj       /* If we have already seen a non-loop edge, then we must have
1585*38fd1498Szrj 	 multiple incoming non-loop edges and thus we return NULL.  */
1586*38fd1498Szrj       if (retval)
1587*38fd1498Szrj 	return NULL;
1588*38fd1498Szrj 
1589*38fd1498Szrj       /* This is the first non-loop incoming edge we have found.  Record
1590*38fd1498Szrj 	 it.  */
1591*38fd1498Szrj       retval = e;
1592*38fd1498Szrj     }
1593*38fd1498Szrj 
1594*38fd1498Szrj   return retval;
1595*38fd1498Szrj }
1596