1 /* Control flow graph analysis code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* This file contains various simple utilities to analyze the CFG. */
26 #include "hard-reg-set.h"
27 #include "basic-block.h"
28 #include "insn-config.h"
34 /* Store the data structures necessary for depth-first search. */
35 struct depth_first_search_dsS
{
36 /* stack for backtracking during the algorithm */
39 /* number of edges in the stack. That is, positions 0, ..., sp-1
43 /* record of basic blocks already seen by depth-first search */
44 sbitmap visited_blocks
;
46 typedef struct depth_first_search_dsS
*depth_first_search_ds
;
48 static void flow_dfs_compute_reverse_init
49 PARAMS ((depth_first_search_ds
));
50 static void flow_dfs_compute_reverse_add_bb
51 PARAMS ((depth_first_search_ds
, basic_block
));
52 static basic_block flow_dfs_compute_reverse_execute
53 PARAMS ((depth_first_search_ds
));
54 static void flow_dfs_compute_reverse_finish
55 PARAMS ((depth_first_search_ds
));
56 static void remove_fake_successors
PARAMS ((basic_block
));
57 static bool need_fake_edge_p
PARAMS ((rtx
));
58 static bool keep_with_call_p
PARAMS ((rtx
));
60 /* Return true if the block has no effect and only forwards control flow to
61 its single destination. */
64 forwarder_block_p (bb
)
69 if (bb
== EXIT_BLOCK_PTR
|| bb
== ENTRY_BLOCK_PTR
70 || !bb
->succ
|| bb
->succ
->succ_next
)
73 for (insn
= bb
->head
; insn
!= bb
->end
; insn
= NEXT_INSN (insn
))
74 if (INSN_P (insn
) && active_insn_p (insn
))
77 return (!INSN_P (insn
)
78 || (GET_CODE (insn
) == JUMP_INSN
&& simplejump_p (insn
))
79 || !active_insn_p (insn
));
82 /* Return nonzero if we can reach target from src by falling through. */
85 can_fallthru (src
, target
)
86 basic_block src
, target
;
89 rtx insn2
= target
->head
;
91 if (src
->index
+ 1 == target
->index
&& !active_insn_p (insn2
))
92 insn2
= next_active_insn (insn2
);
94 /* ??? Later we may add code to move jump tables offline. */
95 return next_active_insn (insn
) == insn2
;
98 /* Mark the back edges in DFS traversal.
99 Return non-zero if a loop (natural or otherwise) is present.
100 Inspired by Depth_First_Search_PP described in:
102 Advanced Compiler Design and Implementation
104 Morgan Kaufmann, 1997
106 and heavily borrowed from flow_depth_first_order_compute. */
109 mark_dfs_back_edges ()
120 /* Allocate the preorder and postorder number arrays. */
121 pre
= (int *) xcalloc (n_basic_blocks
, sizeof (int));
122 post
= (int *) xcalloc (n_basic_blocks
, sizeof (int));
124 /* Allocate stack for back-tracking up CFG. */
125 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
128 /* Allocate bitmap to track nodes that have been visited. */
129 visited
= sbitmap_alloc (n_basic_blocks
);
131 /* None of the nodes in the CFG have been visited yet. */
132 sbitmap_zero (visited
);
134 /* Push the first edge on to the stack. */
135 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
143 /* Look at the edge on the top of the stack. */
147 e
->flags
&= ~EDGE_DFS_BACK
;
149 /* Check if the edge destination has been visited yet. */
150 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
152 /* Mark that we have visited the destination. */
153 SET_BIT (visited
, dest
->index
);
155 pre
[dest
->index
] = prenum
++;
158 /* Since the DEST node has been visited for the first
159 time, check its successors. */
160 stack
[sp
++] = dest
->succ
;
163 post
[dest
->index
] = postnum
++;
167 if (dest
!= EXIT_BLOCK_PTR
&& src
!= ENTRY_BLOCK_PTR
168 && pre
[src
->index
] >= pre
[dest
->index
]
169 && post
[dest
->index
] == 0)
170 e
->flags
|= EDGE_DFS_BACK
, found
= true;
172 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
173 post
[src
->index
] = postnum
++;
176 stack
[sp
- 1] = e
->succ_next
;
185 sbitmap_free (visited
);
190 /* Return true if we need to add fake edge to exit.
191 Helper function for the flow_call_edges_add. */
194 need_fake_edge_p (insn
)
200 if ((GET_CODE (insn
) == CALL_INSN
201 && !SIBLING_CALL_P (insn
)
202 && !find_reg_note (insn
, REG_NORETURN
, NULL
)
203 && !find_reg_note (insn
, REG_ALWAYS_RETURN
, NULL
)
204 && !CONST_OR_PURE_CALL_P (insn
)))
207 return ((GET_CODE (PATTERN (insn
)) == ASM_OPERANDS
208 && MEM_VOLATILE_P (PATTERN (insn
)))
209 || (GET_CODE (PATTERN (insn
)) == PARALLEL
210 && asm_noperands (insn
) != -1
211 && MEM_VOLATILE_P (XVECEXP (PATTERN (insn
), 0, 0)))
212 || GET_CODE (PATTERN (insn
)) == ASM_INPUT
);
215 /* Return true if INSN should be kept in the same block as a preceding call.
216 This is done for a single-set whose destination is a fixed register or
217 whose source is the function return value. This is a helper function for
218 flow_call_edges_add. */
221 keep_with_call_p (insn
)
226 if (INSN_P (insn
) && (set
= single_set (insn
)) != NULL
)
228 if (GET_CODE (SET_DEST (set
)) == REG
229 && fixed_regs
[REGNO (SET_DEST (set
))]
230 && general_operand (SET_SRC (set
), VOIDmode
))
232 if (GET_CODE (SET_SRC (set
)) == REG
233 && FUNCTION_VALUE_REGNO_P (REGNO (SET_SRC (set
)))
234 && GET_CODE (SET_DEST (set
)) == REG
235 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
241 /* Add fake edges to the function exit for any non constant and non noreturn
242 calls, volatile inline assembly in the bitmap of blocks specified by
243 BLOCKS or to the whole CFG if BLOCKS is zero. Return the number of blocks
246 The goal is to expose cases in which entering a basic block does not imply
247 that all subsequent instructions must be executed. */
250 flow_call_edges_add (blocks
)
254 int blocks_split
= 0;
257 bool check_last_block
= false;
259 /* Map bb indices into basic block pointers since split_block
260 will renumber the basic blocks. */
262 bbs
= xmalloc (n_basic_blocks
* sizeof (*bbs
));
266 for (i
= 0; i
< n_basic_blocks
; i
++)
267 bbs
[bb_num
++] = BASIC_BLOCK (i
);
269 check_last_block
= true;
272 EXECUTE_IF_SET_IN_SBITMAP (blocks
, 0, i
,
274 bbs
[bb_num
++] = BASIC_BLOCK (i
);
275 if (i
== n_basic_blocks
- 1)
276 check_last_block
= true;
279 /* In the last basic block, before epilogue generation, there will be
280 a fallthru edge to EXIT. Special care is required if the last insn
281 of the last basic block is a call because make_edge folds duplicate
282 edges, which would result in the fallthru edge also being marked
283 fake, which would result in the fallthru edge being removed by
284 remove_fake_edges, which would result in an invalid CFG.
286 Moreover, we can't elide the outgoing fake edge, since the block
287 profiler needs to take this into account in order to solve the minimal
288 spanning tree in the case that the call doesn't return.
290 Handle this by adding a dummy instruction in a new last basic block. */
291 if (check_last_block
)
293 basic_block bb
= BASIC_BLOCK (n_basic_blocks
- 1);
296 /* Back up past insns that must be kept in the same block as a call. */
297 while (insn
!= bb
->head
298 && keep_with_call_p (insn
))
299 insn
= PREV_INSN (insn
);
301 if (need_fake_edge_p (insn
))
305 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
306 if (e
->dest
== EXIT_BLOCK_PTR
)
309 insert_insn_on_edge (gen_rtx_USE (VOIDmode
, const0_rtx
), e
);
310 commit_edge_insertions ();
314 /* Now add fake edges to the function exit for any non constant
315 calls since there is no way that we can determine if they will
318 for (i
= 0; i
< bb_num
; i
++)
320 basic_block bb
= bbs
[i
];
324 for (insn
= bb
->end
; ; insn
= prev_insn
)
326 prev_insn
= PREV_INSN (insn
);
327 if (need_fake_edge_p (insn
))
330 rtx split_at_insn
= insn
;
332 /* Don't split the block between a call and an insn that should
333 remain in the same block as the call. */
334 if (GET_CODE (insn
) == CALL_INSN
)
335 while (split_at_insn
!= bb
->end
336 && keep_with_call_p (NEXT_INSN (split_at_insn
)))
337 split_at_insn
= NEXT_INSN (split_at_insn
);
339 /* The handling above of the final block before the epilogue
340 should be enough to verify that there is no edge to the exit
341 block in CFG already. Calling make_edge in such case would
342 cause us to mark that edge as fake and remove it later. */
344 #ifdef ENABLE_CHECKING
345 if (split_at_insn
== bb
->end
)
346 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
347 if (e
->dest
== EXIT_BLOCK_PTR
)
351 /* Note that the following may create a new basic block
352 and renumber the existing basic blocks. */
353 e
= split_block (bb
, split_at_insn
);
357 make_edge (bb
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
360 if (insn
== bb
->head
)
372 /* Find unreachable blocks. An unreachable block will have 0 in
373 the reachable bit in block->flags. A non-zero value indicates the
374 block is reachable. */
377 find_unreachable_blocks ()
381 basic_block
*tos
, *worklist
;
384 tos
= worklist
= (basic_block
*) xmalloc (sizeof (basic_block
) * n
);
386 /* Clear all the reachability flags. */
388 for (i
= 0; i
< n
; ++i
)
389 BASIC_BLOCK (i
)->flags
&= ~BB_REACHABLE
;
391 /* Add our starting points to the worklist. Almost always there will
392 be only one. It isn't inconceivable that we might one day directly
393 support Fortran alternate entry points. */
395 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
399 /* Mark the block reachable. */
400 e
->dest
->flags
|= BB_REACHABLE
;
403 /* Iterate: find everything reachable from what we've already seen. */
405 while (tos
!= worklist
)
407 basic_block b
= *--tos
;
409 for (e
= b
->succ
; e
; e
= e
->succ_next
)
410 if (!(e
->dest
->flags
& BB_REACHABLE
))
413 e
->dest
->flags
|= BB_REACHABLE
;
420 /* Functions to access an edge list with a vector representation.
421 Enough data is kept such that given an index number, the
422 pred and succ that edge represents can be determined, or
423 given a pred and a succ, its index number can be returned.
424 This allows algorithms which consume a lot of memory to
425 represent the normally full matrix of edge (pred,succ) with a
426 single indexed vector, edge (EDGE_INDEX (pred, succ)), with no
427 wasted space in the client code due to sparse flow graphs. */
429 /* This functions initializes the edge list. Basically the entire
430 flowgraph is processed, and all edges are assigned a number,
431 and the data structure is filled in. */
436 struct edge_list
*elist
;
442 block_count
= n_basic_blocks
+ 2; /* Include the entry and exit blocks. */
446 /* Determine the number of edges in the flow graph by counting successor
447 edges on each basic block. */
448 for (x
= 0; x
< n_basic_blocks
; x
++)
450 basic_block bb
= BASIC_BLOCK (x
);
452 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
456 /* Don't forget successors of the entry block. */
457 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
460 elist
= (struct edge_list
*) xmalloc (sizeof (struct edge_list
));
461 elist
->num_blocks
= block_count
;
462 elist
->num_edges
= num_edges
;
463 elist
->index_to_edge
= (edge
*) xmalloc (sizeof (edge
) * num_edges
);
467 /* Follow successors of the entry block, and register these edges. */
468 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
469 elist
->index_to_edge
[num_edges
++] = e
;
471 for (x
= 0; x
< n_basic_blocks
; x
++)
473 basic_block bb
= BASIC_BLOCK (x
);
475 /* Follow all successors of blocks, and register these edges. */
476 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
477 elist
->index_to_edge
[num_edges
++] = e
;
483 /* This function free's memory associated with an edge list. */
486 free_edge_list (elist
)
487 struct edge_list
*elist
;
491 free (elist
->index_to_edge
);
496 /* This function provides debug output showing an edge list. */
499 print_edge_list (f
, elist
)
501 struct edge_list
*elist
;
505 fprintf (f
, "Compressed edge list, %d BBs + entry & exit, and %d edges\n",
506 elist
->num_blocks
- 2, elist
->num_edges
);
508 for (x
= 0; x
< elist
->num_edges
; x
++)
510 fprintf (f
, " %-4d - edge(", x
);
511 if (INDEX_EDGE_PRED_BB (elist
, x
) == ENTRY_BLOCK_PTR
)
512 fprintf (f
, "entry,");
514 fprintf (f
, "%d,", INDEX_EDGE_PRED_BB (elist
, x
)->index
);
516 if (INDEX_EDGE_SUCC_BB (elist
, x
) == EXIT_BLOCK_PTR
)
517 fprintf (f
, "exit)\n");
519 fprintf (f
, "%d)\n", INDEX_EDGE_SUCC_BB (elist
, x
)->index
);
523 /* This function provides an internal consistency check of an edge list,
524 verifying that all edges are present, and that there are no
528 verify_edge_list (f
, elist
)
530 struct edge_list
*elist
;
532 int x
, pred
, succ
, index
;
535 for (x
= 0; x
< n_basic_blocks
; x
++)
537 basic_block bb
= BASIC_BLOCK (x
);
539 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
541 pred
= e
->src
->index
;
542 succ
= e
->dest
->index
;
543 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
544 if (index
== EDGE_INDEX_NO_EDGE
)
546 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
550 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
551 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
552 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
553 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
554 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
555 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
559 for (e
= ENTRY_BLOCK_PTR
->succ
; e
; e
= e
->succ_next
)
561 pred
= e
->src
->index
;
562 succ
= e
->dest
->index
;
563 index
= EDGE_INDEX (elist
, e
->src
, e
->dest
);
564 if (index
== EDGE_INDEX_NO_EDGE
)
566 fprintf (f
, "*p* No index for edge from %d to %d\n", pred
, succ
);
570 if (INDEX_EDGE_PRED_BB (elist
, index
)->index
!= pred
)
571 fprintf (f
, "*p* Pred for index %d should be %d not %d\n",
572 index
, pred
, INDEX_EDGE_PRED_BB (elist
, index
)->index
);
573 if (INDEX_EDGE_SUCC_BB (elist
, index
)->index
!= succ
)
574 fprintf (f
, "*p* Succ for index %d should be %d not %d\n",
575 index
, succ
, INDEX_EDGE_SUCC_BB (elist
, index
)->index
);
578 /* We've verified that all the edges are in the list, no lets make sure
579 there are no spurious edges in the list. */
581 for (pred
= 0; pred
< n_basic_blocks
; pred
++)
582 for (succ
= 0; succ
< n_basic_blocks
; succ
++)
584 basic_block p
= BASIC_BLOCK (pred
);
585 basic_block s
= BASIC_BLOCK (succ
);
588 for (e
= p
->succ
; e
; e
= e
->succ_next
)
595 for (e
= s
->pred
; e
; e
= e
->pred_next
)
602 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), BASIC_BLOCK (succ
))
603 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
604 fprintf (f
, "*** Edge (%d, %d) appears to not have an index\n",
606 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), BASIC_BLOCK (succ
))
607 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
608 fprintf (f
, "*** Edge (%d, %d) has index %d, but there is no edge\n",
609 pred
, succ
, EDGE_INDEX (elist
, BASIC_BLOCK (pred
),
610 BASIC_BLOCK (succ
)));
613 for (succ
= 0; succ
< n_basic_blocks
; succ
++)
615 basic_block p
= ENTRY_BLOCK_PTR
;
616 basic_block s
= BASIC_BLOCK (succ
);
619 for (e
= p
->succ
; e
; e
= e
->succ_next
)
626 for (e
= s
->pred
; e
; e
= e
->pred_next
)
633 if (EDGE_INDEX (elist
, ENTRY_BLOCK_PTR
, BASIC_BLOCK (succ
))
634 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
635 fprintf (f
, "*** Edge (entry, %d) appears to not have an index\n",
637 if (EDGE_INDEX (elist
, ENTRY_BLOCK_PTR
, BASIC_BLOCK (succ
))
638 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
639 fprintf (f
, "*** Edge (entry, %d) has index %d, but no edge exists\n",
640 succ
, EDGE_INDEX (elist
, ENTRY_BLOCK_PTR
,
641 BASIC_BLOCK (succ
)));
644 for (pred
= 0; pred
< n_basic_blocks
; pred
++)
646 basic_block p
= BASIC_BLOCK (pred
);
647 basic_block s
= EXIT_BLOCK_PTR
;
650 for (e
= p
->succ
; e
; e
= e
->succ_next
)
657 for (e
= s
->pred
; e
; e
= e
->pred_next
)
664 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), EXIT_BLOCK_PTR
)
665 == EDGE_INDEX_NO_EDGE
&& found_edge
!= 0)
666 fprintf (f
, "*** Edge (%d, exit) appears to not have an index\n",
668 if (EDGE_INDEX (elist
, BASIC_BLOCK (pred
), EXIT_BLOCK_PTR
)
669 != EDGE_INDEX_NO_EDGE
&& found_edge
== 0)
670 fprintf (f
, "*** Edge (%d, exit) has index %d, but no edge exists\n",
671 pred
, EDGE_INDEX (elist
, BASIC_BLOCK (pred
),
676 /* This routine will determine what, if any, edge there is between
677 a specified predecessor and successor. */
680 find_edge_index (edge_list
, pred
, succ
)
681 struct edge_list
*edge_list
;
682 basic_block pred
, succ
;
686 for (x
= 0; x
< NUM_EDGES (edge_list
); x
++)
687 if (INDEX_EDGE_PRED_BB (edge_list
, x
) == pred
688 && INDEX_EDGE_SUCC_BB (edge_list
, x
) == succ
)
691 return (EDGE_INDEX_NO_EDGE
);
694 /* Dump the list of basic blocks in the bitmap NODES. */
697 flow_nodes_print (str
, nodes
, file
)
707 fprintf (file
, "%s { ", str
);
708 EXECUTE_IF_SET_IN_SBITMAP (nodes
, 0, node
, {fprintf (file
, "%d ", node
);});
712 /* Dump the list of edges in the array EDGE_LIST. */
715 flow_edge_list_print (str
, edge_list
, num_edges
, file
)
717 const edge
*edge_list
;
726 fprintf (file
, "%s { ", str
);
727 for (i
= 0; i
< num_edges
; i
++)
728 fprintf (file
, "%d->%d ", edge_list
[i
]->src
->index
,
729 edge_list
[i
]->dest
->index
);
735 /* This routine will remove any fake successor edges for a basic block.
736 When the edge is removed, it is also removed from whatever predecessor
740 remove_fake_successors (bb
)
745 for (e
= bb
->succ
; e
;)
750 if ((tmp
->flags
& EDGE_FAKE
) == EDGE_FAKE
)
755 /* This routine will remove all fake edges from the flow graph. If
756 we remove all fake successors, it will automatically remove all
757 fake predecessors. */
764 for (x
= 0; x
< n_basic_blocks
; x
++)
765 remove_fake_successors (BASIC_BLOCK (x
));
767 /* We've handled all successors except the entry block's. */
768 remove_fake_successors (ENTRY_BLOCK_PTR
);
771 /* This function will add a fake edge between any block which has no
772 successors, and the exit block. Some data flow equations require these
776 add_noreturn_fake_exit_edges ()
780 for (x
= 0; x
< n_basic_blocks
; x
++)
781 if (BASIC_BLOCK (x
)->succ
== NULL
)
782 make_single_succ_edge (BASIC_BLOCK (x
), EXIT_BLOCK_PTR
, EDGE_FAKE
);
785 /* This function adds a fake edge between any infinite loops to the
786 exit block. Some optimizations require a path from each node to
789 See also Morgan, Figure 3.10, pp. 82-83.
791 The current implementation is ugly, not attempting to minimize the
792 number of inserted fake edges. To reduce the number of fake edges
793 to insert, add fake edges from _innermost_ loops containing only
794 nodes not reachable from the exit block. */
797 connect_infinite_loops_to_exit ()
799 basic_block unvisited_block
;
800 struct depth_first_search_dsS dfs_ds
;
802 /* Perform depth-first search in the reverse graph to find nodes
803 reachable from the exit block. */
804 flow_dfs_compute_reverse_init (&dfs_ds
);
805 flow_dfs_compute_reverse_add_bb (&dfs_ds
, EXIT_BLOCK_PTR
);
807 /* Repeatedly add fake edges, updating the unreachable nodes. */
810 unvisited_block
= flow_dfs_compute_reverse_execute (&dfs_ds
);
811 if (!unvisited_block
)
814 make_edge (unvisited_block
, EXIT_BLOCK_PTR
, EDGE_FAKE
);
815 flow_dfs_compute_reverse_add_bb (&dfs_ds
, unvisited_block
);
818 flow_dfs_compute_reverse_finish (&dfs_ds
);
822 /* Compute reverse top sort order */
825 flow_reverse_top_sort_order_compute (rts_order
)
833 /* Allocate stack for back-tracking up CFG. */
834 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
837 /* Allocate bitmap to track nodes that have been visited. */
838 visited
= sbitmap_alloc (n_basic_blocks
);
840 /* None of the nodes in the CFG have been visited yet. */
841 sbitmap_zero (visited
);
843 /* Push the first edge on to the stack. */
844 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
852 /* Look at the edge on the top of the stack. */
857 /* Check if the edge destination has been visited yet. */
858 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
860 /* Mark that we have visited the destination. */
861 SET_BIT (visited
, dest
->index
);
864 /* Since the DEST node has been visited for the first
865 time, check its successors. */
866 stack
[sp
++] = dest
->succ
;
868 rts_order
[postnum
++] = dest
->index
;
872 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
)
873 rts_order
[postnum
++] = src
->index
;
876 stack
[sp
- 1] = e
->succ_next
;
883 sbitmap_free (visited
);
886 /* Compute the depth first search order and store in the array
887 DFS_ORDER if non-zero, marking the nodes visited in VISITED. If
888 RC_ORDER is non-zero, return the reverse completion number for each
889 node. Returns the number of nodes visited. A depth first search
890 tries to get as far away from the starting point as quickly as
894 flow_depth_first_order_compute (dfs_order
, rc_order
)
901 int rcnum
= n_basic_blocks
- 1;
904 /* Allocate stack for back-tracking up CFG. */
905 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
908 /* Allocate bitmap to track nodes that have been visited. */
909 visited
= sbitmap_alloc (n_basic_blocks
);
911 /* None of the nodes in the CFG have been visited yet. */
912 sbitmap_zero (visited
);
914 /* Push the first edge on to the stack. */
915 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
923 /* Look at the edge on the top of the stack. */
928 /* Check if the edge destination has been visited yet. */
929 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
931 /* Mark that we have visited the destination. */
932 SET_BIT (visited
, dest
->index
);
935 dfs_order
[dfsnum
] = dest
->index
;
940 /* Since the DEST node has been visited for the first
941 time, check its successors. */
942 stack
[sp
++] = dest
->succ
;
944 /* There are no successors for the DEST node so assign
945 its reverse completion number. */
946 rc_order
[rcnum
--] = dest
->index
;
950 if (! e
->succ_next
&& src
!= ENTRY_BLOCK_PTR
952 /* There are no more successors for the SRC node
953 so assign its reverse completion number. */
954 rc_order
[rcnum
--] = src
->index
;
957 stack
[sp
- 1] = e
->succ_next
;
964 sbitmap_free (visited
);
966 /* The number of nodes visited should not be greater than
968 if (dfsnum
> n_basic_blocks
)
971 /* There are some nodes left in the CFG that are unreachable. */
972 if (dfsnum
< n_basic_blocks
)
981 struct dfst_node
**node
;
982 struct dfst_node
*up
;
985 /* Compute a preorder transversal ordering such that a sub-tree which
986 is the source of a cross edge appears before the sub-tree which is
987 the destination of the cross edge. This allows for easy detection
988 of all the entry blocks for a loop.
990 The ordering is compute by:
992 1) Generating a depth first spanning tree.
994 2) Walking the resulting tree from right to left. */
997 flow_preorder_transversal_compute (pot_order
)
1006 struct dfst_node
*node
;
1007 struct dfst_node
*dfst
;
1009 /* Allocate stack for back-tracking up CFG. */
1010 stack
= (edge
*) xmalloc ((n_basic_blocks
+ 1) * sizeof (edge
));
1013 /* Allocate the tree. */
1014 dfst
= (struct dfst_node
*) xcalloc (n_basic_blocks
,
1015 sizeof (struct dfst_node
));
1017 for (i
= 0; i
< n_basic_blocks
; i
++)
1020 for (e
= BASIC_BLOCK (i
)->succ
; e
; e
= e
->succ_next
)
1025 ? (struct dfst_node
**) xcalloc (max_successors
,
1026 sizeof (struct dfst_node
*))
1030 /* Allocate bitmap to track nodes that have been visited. */
1031 visited
= sbitmap_alloc (n_basic_blocks
);
1033 /* None of the nodes in the CFG have been visited yet. */
1034 sbitmap_zero (visited
);
1036 /* Push the first edge on to the stack. */
1037 stack
[sp
++] = ENTRY_BLOCK_PTR
->succ
;
1044 /* Look at the edge on the top of the stack. */
1049 /* Check if the edge destination has been visited yet. */
1050 if (dest
!= EXIT_BLOCK_PTR
&& ! TEST_BIT (visited
, dest
->index
))
1052 /* Mark that we have visited the destination. */
1053 SET_BIT (visited
, dest
->index
);
1055 /* Add the destination to the preorder tree. */
1056 if (src
!= ENTRY_BLOCK_PTR
)
1058 dfst
[src
->index
].node
[dfst
[src
->index
].nnodes
++]
1059 = &dfst
[dest
->index
];
1060 dfst
[dest
->index
].up
= &dfst
[src
->index
];
1064 /* Since the DEST node has been visited for the first
1065 time, check its successors. */
1066 stack
[sp
++] = dest
->succ
;
1069 else if (e
->succ_next
)
1070 stack
[sp
- 1] = e
->succ_next
;
1076 sbitmap_free (visited
);
1078 /* Record the preorder transversal order by
1079 walking the tree from right to left. */
1089 node
= node
->node
[--node
->nnodes
];
1090 pot_order
[i
++] = node
- dfst
;
1096 /* Free the tree. */
1098 for (i
= 0; i
< n_basic_blocks
; i
++)
1100 free (dfst
[i
].node
);
1105 /* Compute the depth first search order on the _reverse_ graph and
1106 store in the array DFS_ORDER, marking the nodes visited in VISITED.
1107 Returns the number of nodes visited.
1109 The computation is split into three pieces:
1111 flow_dfs_compute_reverse_init () creates the necessary data
1114 flow_dfs_compute_reverse_add_bb () adds a basic block to the data
1115 structures. The block will start the search.
1117 flow_dfs_compute_reverse_execute () continues (or starts) the
1118 search using the block on the top of the stack, stopping when the
1121 flow_dfs_compute_reverse_finish () destroys the necessary data
1124 Thus, the user will probably call ..._init(), call ..._add_bb() to
1125 add a beginning basic block to the stack, call ..._execute(),
1126 possibly add another bb to the stack and again call ..._execute(),
1127 ..., and finally call _finish(). */
1129 /* Initialize the data structures used for depth-first search on the
1130 reverse graph. If INITIALIZE_STACK is nonzero, the exit block is
1131 added to the basic block stack. DATA is the current depth-first
1132 search context. If INITIALIZE_STACK is non-zero, there is an
1133 element on the stack. */
1136 flow_dfs_compute_reverse_init (data
)
1137 depth_first_search_ds data
;
1139 /* Allocate stack for back-tracking up CFG. */
1140 data
->stack
= (basic_block
*) xmalloc ((n_basic_blocks
- (INVALID_BLOCK
+ 1))
1141 * sizeof (basic_block
));
1144 /* Allocate bitmap to track nodes that have been visited. */
1145 data
->visited_blocks
= sbitmap_alloc (n_basic_blocks
- (INVALID_BLOCK
+ 1));
1147 /* None of the nodes in the CFG have been visited yet. */
1148 sbitmap_zero (data
->visited_blocks
);
1153 /* Add the specified basic block to the top of the dfs data
1154 structures. When the search continues, it will start at the
1158 flow_dfs_compute_reverse_add_bb (data
, bb
)
1159 depth_first_search_ds data
;
1162 data
->stack
[data
->sp
++] = bb
;
1163 SET_BIT (data
->visited_blocks
, bb
->index
- (INVALID_BLOCK
+ 1));
1166 /* Continue the depth-first search through the reverse graph starting with the
1167 block at the stack's top and ending when the stack is empty. Visited nodes
1168 are marked. Returns an unvisited basic block, or NULL if there is none
1172 flow_dfs_compute_reverse_execute (data
)
1173 depth_first_search_ds data
;
1179 while (data
->sp
> 0)
1181 bb
= data
->stack
[--data
->sp
];
1183 /* Perform depth-first search on adjacent vertices. */
1184 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1185 if (!TEST_BIT (data
->visited_blocks
,
1186 e
->src
->index
- (INVALID_BLOCK
+ 1)))
1187 flow_dfs_compute_reverse_add_bb (data
, e
->src
);
1190 /* Determine if there are unvisited basic blocks. */
1191 for (i
= n_basic_blocks
- (INVALID_BLOCK
+ 1); --i
>= 0; )
1192 if (!TEST_BIT (data
->visited_blocks
, i
))
1193 return BASIC_BLOCK (i
+ (INVALID_BLOCK
+ 1));
1198 /* Destroy the data structures needed for depth-first search on the
1202 flow_dfs_compute_reverse_finish (data
)
1203 depth_first_search_ds data
;
1206 sbitmap_free (data
->visited_blocks
);