1 /* Control flow graph building 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 /* find_basic_blocks divides the current function's rtl into basic
23 blocks and constructs the CFG. The blocks are recorded in the
24 basic_block_info array; the CFG exists in the edge structures
25 referenced by the blocks.
27 find_basic_blocks also finds any unreachable loops and deletes them.
29 Available functionality:
32 - Local CFG construction
40 #include "hard-reg-set.h"
41 #include "basic-block.h"
51 static int count_basic_blocks
PARAMS ((rtx
));
52 static void find_basic_blocks_1
PARAMS ((rtx
));
53 static rtx find_label_refs
PARAMS ((rtx
, rtx
));
54 static void make_edges
PARAMS ((rtx
, int, int, int));
55 static void make_label_edge
PARAMS ((sbitmap
*, basic_block
,
57 static void make_eh_edge
PARAMS ((sbitmap
*, basic_block
, rtx
));
59 /* Count the basic blocks of the function. */
62 count_basic_blocks (f
)
66 register RTX_CODE prev_code
;
67 register int count
= 0;
68 int saw_abnormal_edge
= 0;
70 prev_code
= JUMP_INSN
;
71 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
73 enum rtx_code code
= GET_CODE (insn
);
75 if (code
== CODE_LABEL
76 || (GET_RTX_CLASS (code
) == 'i'
77 && (prev_code
== JUMP_INSN
78 || prev_code
== BARRIER
79 || saw_abnormal_edge
)))
81 saw_abnormal_edge
= 0;
85 /* Record whether this insn created an edge. */
86 if (code
== CALL_INSN
)
90 /* If there is a nonlocal goto label and the specified
91 region number isn't -1, we have an edge. */
92 if (nonlocal_goto_handler_labels
93 && ((note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
)) == 0
94 || INTVAL (XEXP (note
, 0)) >= 0))
95 saw_abnormal_edge
= 1;
97 else if (can_throw_internal (insn
))
98 saw_abnormal_edge
= 1;
100 else if (flag_non_call_exceptions
102 && can_throw_internal (insn
))
103 saw_abnormal_edge
= 1;
109 /* The rest of the compiler works a bit smoother when we don't have to
110 check for the edge case of do-nothing functions with no basic blocks. */
113 emit_insn (gen_rtx_USE (VOIDmode
, const0_rtx
));
120 /* Scan a list of insns for labels referred to other than by jumps.
121 This is used to scan the alternatives of a call placeholder. */
123 find_label_refs (f
, lvl
)
129 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
130 if (INSN_P (insn
) && GET_CODE (insn
) != JUMP_INSN
)
134 /* Make a list of all labels referred to other than by jumps
135 (which just don't have the REG_LABEL notes).
137 Make a special exception for labels followed by an ADDR*VEC,
138 as this would be a part of the tablejump setup code.
140 Make a special exception to registers loaded with label
141 values just before jump insns that use them. */
143 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
144 if (REG_NOTE_KIND (note
) == REG_LABEL
)
146 rtx lab
= XEXP (note
, 0), next
;
148 if ((next
= next_nonnote_insn (lab
)) != NULL
149 && GET_CODE (next
) == JUMP_INSN
150 && (GET_CODE (PATTERN (next
)) == ADDR_VEC
151 || GET_CODE (PATTERN (next
)) == ADDR_DIFF_VEC
))
153 else if (GET_CODE (lab
) == NOTE
)
155 else if (GET_CODE (NEXT_INSN (insn
)) == JUMP_INSN
156 && find_reg_note (NEXT_INSN (insn
), REG_LABEL
, lab
))
159 lvl
= alloc_EXPR_LIST (0, XEXP (note
, 0), lvl
);
166 /* Create an edge between two basic blocks. FLAGS are auxiliary information
167 about the edge that is accumulated between calls. */
169 /* Create an edge from a basic block to a label. */
172 make_label_edge (edge_cache
, src
, label
, flags
)
178 if (GET_CODE (label
) != CODE_LABEL
)
181 /* If the label was never emitted, this insn is junk, but avoid a
182 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
183 as a result of a syntax error and a diagnostic has already been
186 if (INSN_UID (label
) == 0)
189 cached_make_edge (edge_cache
, src
, BLOCK_FOR_INSN (label
), flags
);
192 /* Create the edges generated by INSN in REGION. */
195 make_eh_edge (edge_cache
, src
, insn
)
200 int is_call
= (GET_CODE (insn
) == CALL_INSN
? EDGE_ABNORMAL_CALL
: 0);
203 handlers
= reachable_handlers (insn
);
205 for (i
= handlers
; i
; i
= XEXP (i
, 1))
206 make_label_edge (edge_cache
, src
, XEXP (i
, 0),
207 EDGE_ABNORMAL
| EDGE_EH
| is_call
);
209 free_INSN_LIST_list (&handlers
);
211 /* Identify the edges between basic blocks MIN to MAX.
213 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
214 that are otherwise unreachable may be reachable with a non-local goto.
216 BB_EH_END is an array indexed by basic block number in which we record
217 the list of exception regions active at the end of the basic block. */
220 make_edges (label_value_list
, min
, max
, update_p
)
221 rtx label_value_list
;
222 int min
, max
, update_p
;
225 sbitmap
*edge_cache
= NULL
;
227 /* Assume no computed jump; revise as we create edges. */
228 current_function_has_computed_jump
= 0;
230 /* Heavy use of computed goto in machine-generated code can lead to
231 nearly fully-connected CFGs. In that case we spend a significant
232 amount of time searching the edge lists for duplicates. */
233 if (forced_labels
|| label_value_list
)
235 edge_cache
= sbitmap_vector_alloc (n_basic_blocks
, n_basic_blocks
);
236 sbitmap_vector_zero (edge_cache
, n_basic_blocks
);
239 for (i
= min
; i
<= max
; ++i
)
242 for (e
= BASIC_BLOCK (i
)->succ
; e
; e
= e
->succ_next
)
243 if (e
->dest
!= EXIT_BLOCK_PTR
)
244 SET_BIT (edge_cache
[i
], e
->dest
->index
);
248 /* By nature of the way these get numbered, block 0 is always the entry. */
249 cached_make_edge (edge_cache
, ENTRY_BLOCK_PTR
, BASIC_BLOCK (0), EDGE_FALLTHRU
);
251 for (i
= min
; i
<= max
; ++i
)
253 basic_block bb
= BASIC_BLOCK (i
);
256 int force_fallthru
= 0;
258 if (GET_CODE (bb
->head
) == CODE_LABEL
259 && LABEL_ALTERNATE_NAME (bb
->head
))
260 cached_make_edge (NULL
, ENTRY_BLOCK_PTR
, bb
, 0);
262 /* Examine the last instruction of the block, and discover the
263 ways we can leave the block. */
266 code
= GET_CODE (insn
);
269 if (code
== JUMP_INSN
)
273 /* Recognize exception handling placeholders. */
274 if (GET_CODE (PATTERN (insn
)) == RESX
)
275 make_eh_edge (edge_cache
, bb
, insn
);
277 /* Recognize a non-local goto as a branch outside the
279 else if (find_reg_note (insn
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
282 /* ??? Recognize a tablejump and do the right thing. */
283 else if ((tmp
= JUMP_LABEL (insn
)) != NULL_RTX
284 && (tmp
= NEXT_INSN (tmp
)) != NULL_RTX
285 && GET_CODE (tmp
) == JUMP_INSN
286 && (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
287 || GET_CODE (PATTERN (tmp
)) == ADDR_DIFF_VEC
))
292 if (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
)
293 vec
= XVEC (PATTERN (tmp
), 0);
295 vec
= XVEC (PATTERN (tmp
), 1);
297 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
298 make_label_edge (edge_cache
, bb
,
299 XEXP (RTVEC_ELT (vec
, j
), 0), 0);
301 /* Some targets (eg, ARM) emit a conditional jump that also
302 contains the out-of-range target. Scan for these and
303 add an edge if necessary. */
304 if ((tmp
= single_set (insn
)) != NULL
305 && SET_DEST (tmp
) == pc_rtx
306 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
307 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
308 make_label_edge (edge_cache
, bb
,
309 XEXP (XEXP (SET_SRC (tmp
), 2), 0), 0);
311 #ifdef CASE_DROPS_THROUGH
312 /* Silly VAXen. The ADDR_VEC is going to be in the way of
313 us naturally detecting fallthru into the next block. */
318 /* If this is a computed jump, then mark it as reaching
319 everything on the label_value_list and forced_labels list. */
320 else if (computed_jump_p (insn
))
322 current_function_has_computed_jump
= 1;
324 for (x
= label_value_list
; x
; x
= XEXP (x
, 1))
325 make_label_edge (edge_cache
, bb
, XEXP (x
, 0), EDGE_ABNORMAL
);
327 for (x
= forced_labels
; x
; x
= XEXP (x
, 1))
328 make_label_edge (edge_cache
, bb
, XEXP (x
, 0), EDGE_ABNORMAL
);
331 /* Returns create an exit out. */
332 else if (returnjump_p (insn
))
333 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, 0);
335 /* Otherwise, we have a plain conditional or unconditional jump. */
338 if (! JUMP_LABEL (insn
))
340 make_label_edge (edge_cache
, bb
, JUMP_LABEL (insn
), 0);
344 /* If this is a sibling call insn, then this is in effect a
345 combined call and return, and so we need an edge to the
346 exit block. No need to worry about EH edges, since we
347 wouldn't have created the sibling call in the first place. */
349 if (code
== CALL_INSN
&& SIBLING_CALL_P (insn
))
350 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
,
351 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
353 /* If this is a CALL_INSN, then mark it as reaching the active EH
354 handler for this CALL_INSN. If we're handling non-call
355 exceptions then any insn can reach any of the active handlers.
357 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
359 else if (code
== CALL_INSN
|| flag_non_call_exceptions
)
361 /* Add any appropriate EH edges. */
362 make_eh_edge (edge_cache
, bb
, insn
);
364 if (code
== CALL_INSN
&& nonlocal_goto_handler_labels
)
366 /* ??? This could be made smarter: in some cases it's possible
367 to tell that certain calls will not do a nonlocal goto.
369 For example, if the nested functions that do the nonlocal
370 gotos do not have their addresses taken, then only calls to
371 those functions or to other nested functions that use them
372 could possibly do nonlocal gotos. */
373 /* We do know that a REG_EH_REGION note with a value less
374 than 0 is guaranteed not to perform a non-local goto. */
375 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
376 if (!note
|| INTVAL (XEXP (note
, 0)) >= 0)
377 for (x
= nonlocal_goto_handler_labels
; x
; x
= XEXP (x
, 1))
378 make_label_edge (edge_cache
, bb
, XEXP (x
, 0),
379 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
383 /* Find out if we can drop through to the next block. */
384 insn
= next_nonnote_insn (insn
);
385 if (!insn
|| (i
+ 1 == n_basic_blocks
&& force_fallthru
))
386 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, EDGE_FALLTHRU
);
387 else if (i
+ 1 < n_basic_blocks
)
389 rtx tmp
= BLOCK_HEAD (i
+ 1);
390 if (GET_CODE (tmp
) == NOTE
)
391 tmp
= next_nonnote_insn (tmp
);
392 if (force_fallthru
|| insn
== tmp
)
393 cached_make_edge (edge_cache
, bb
, BASIC_BLOCK (i
+ 1), EDGE_FALLTHRU
);
398 sbitmap_vector_free (edge_cache
);
401 /* Find all basic blocks of the function whose first insn is F.
403 Collect and return a list of labels whose addresses are taken. This
404 will be used in make_edges for use with computed gotos. */
407 find_basic_blocks_1 (f
)
410 register rtx insn
, next
;
412 rtx bb_note
= NULL_RTX
;
418 /* We process the instructions in a slightly different way than we did
419 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
420 closed out the previous block, so that it gets attached at the proper
421 place. Since this form should be equivalent to the previous,
422 count_basic_blocks continues to use the old form as a check. */
424 for (insn
= f
; insn
; insn
= next
)
426 enum rtx_code code
= GET_CODE (insn
);
428 next
= NEXT_INSN (insn
);
434 int kind
= NOTE_LINE_NUMBER (insn
);
436 /* Look for basic block notes with which to keep the
437 basic_block_info pointers stable. Unthread the note now;
438 we'll put it back at the right place in create_basic_block.
439 Or not at all if we've already found a note in this block. */
440 if (kind
== NOTE_INSN_BASIC_BLOCK
)
442 if (bb_note
== NULL_RTX
)
445 next
= flow_delete_insn (insn
);
451 /* A basic block starts at a label. If we've closed one off due
452 to a barrier or some such, no need to do it again. */
453 if (head
!= NULL_RTX
)
455 create_basic_block_structure (i
++, head
, end
, bb_note
);
463 /* A basic block ends at a jump. */
464 if (head
== NULL_RTX
)
468 /* ??? Make a special check for table jumps. The way this
469 happens is truly and amazingly gross. We are about to
470 create a basic block that contains just a code label and
471 an addr*vec jump insn. Worse, an addr_diff_vec creates
472 its own natural loop.
474 Prevent this bit of brain damage, pasting things together
475 correctly in make_edges.
477 The correct solution involves emitting the table directly
478 on the tablejump instruction as a note, or JUMP_LABEL. */
480 if (GET_CODE (PATTERN (insn
)) == ADDR_VEC
481 || GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
)
489 goto new_bb_inclusive
;
492 /* A basic block ends at a barrier. It may be that an unconditional
493 jump already closed the basic block -- no need to do it again. */
494 if (head
== NULL_RTX
)
496 goto new_bb_exclusive
;
500 /* Record whether this call created an edge. */
501 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
502 int region
= (note
? INTVAL (XEXP (note
, 0)) : 0);
504 if (GET_CODE (PATTERN (insn
)) == CALL_PLACEHOLDER
)
506 /* Scan each of the alternatives for label refs. */
507 lvl
= find_label_refs (XEXP (PATTERN (insn
), 0), lvl
);
508 lvl
= find_label_refs (XEXP (PATTERN (insn
), 1), lvl
);
509 lvl
= find_label_refs (XEXP (PATTERN (insn
), 2), lvl
);
510 /* Record its tail recursion label, if any. */
511 if (XEXP (PATTERN (insn
), 3) != NULL_RTX
)
512 trll
= alloc_EXPR_LIST (0, XEXP (PATTERN (insn
), 3), trll
);
515 /* A basic block ends at a call that can either throw or
516 do a non-local goto. */
517 if ((nonlocal_goto_handler_labels
&& region
>= 0)
518 || can_throw_internal (insn
))
521 if (head
== NULL_RTX
)
526 create_basic_block_structure (i
++, head
, end
, bb_note
);
527 head
= end
= NULL_RTX
;
535 /* Non-call exceptions generate new blocks just like calls. */
536 if (flag_non_call_exceptions
&& can_throw_internal (insn
))
537 goto new_bb_inclusive
;
539 if (head
== NULL_RTX
)
548 if (GET_CODE (insn
) == INSN
|| GET_CODE (insn
) == CALL_INSN
)
552 /* Make a list of all labels referred to other than by jumps.
554 Make a special exception for labels followed by an ADDR*VEC,
555 as this would be a part of the tablejump setup code.
557 Make a special exception to registers loaded with label
558 values just before jump insns that use them. */
560 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
561 if (REG_NOTE_KIND (note
) == REG_LABEL
)
563 rtx lab
= XEXP (note
, 0), next
;
565 if ((next
= next_nonnote_insn (lab
)) != NULL
566 && GET_CODE (next
) == JUMP_INSN
567 && (GET_CODE (PATTERN (next
)) == ADDR_VEC
568 || GET_CODE (PATTERN (next
)) == ADDR_DIFF_VEC
))
570 else if (GET_CODE (lab
) == NOTE
)
572 else if (GET_CODE (NEXT_INSN (insn
)) == JUMP_INSN
573 && find_reg_note (NEXT_INSN (insn
), REG_LABEL
, lab
))
576 lvl
= alloc_EXPR_LIST (0, XEXP (note
, 0), lvl
);
581 if (head
!= NULL_RTX
)
582 create_basic_block_structure (i
++, head
, end
, bb_note
);
584 flow_delete_insn (bb_note
);
586 if (i
!= n_basic_blocks
)
589 label_value_list
= lvl
;
590 tail_recursion_label_list
= trll
;
594 /* Find basic blocks of the current function.
595 F is the first insn of the function and NREGS the number of register
599 find_basic_blocks (f
, nregs
, file
)
601 int nregs ATTRIBUTE_UNUSED
;
602 FILE *file ATTRIBUTE_UNUSED
;
605 timevar_push (TV_CFG
);
607 if (basic_block_for_insn
)
608 VARRAY_FREE (basic_block_for_insn
);
609 basic_block_for_insn
= 0;
611 /* Flush out existing data. */
612 if (basic_block_info
!= NULL
)
618 /* Clear bb->aux on all extant basic blocks. We'll use this as a
619 tag for reuse during create_basic_block, just in case some pass
620 copies around basic block notes improperly. */
621 for (i
= 0; i
< n_basic_blocks
; ++i
)
622 BASIC_BLOCK (i
)->aux
= NULL
;
624 VARRAY_FREE (basic_block_info
);
627 n_basic_blocks
= count_basic_blocks (f
);
629 /* Size the basic block table. The actual structures will be allocated
630 by find_basic_blocks_1, since we want to keep the structure pointers
631 stable across calls to find_basic_blocks. */
632 /* ??? This whole issue would be much simpler if we called find_basic_blocks
633 exactly once, and thereafter we don't have a single long chain of
634 instructions at all until close to the end of compilation when we
635 actually lay them out. */
637 VARRAY_BB_INIT (basic_block_info
, n_basic_blocks
, "basic_block_info");
639 find_basic_blocks_1 (f
);
641 /* Record the block to which an insn belongs. */
642 /* ??? This should be done another way, by which (perhaps) a label is
643 tagged directly with the basic block that it starts. It is used for
644 more than that currently, but IMO that is the only valid use. */
646 max_uid
= get_max_uid ();
648 /* Leave space for insns life_analysis makes in some cases for auto-inc.
649 These cases are rare, so we don't need too much space. */
650 max_uid
+= max_uid
/ 10;
653 compute_bb_for_insn (max_uid
);
655 /* Discover the edges of our cfg. */
656 make_edges (label_value_list
, 0, n_basic_blocks
- 1, 0);
658 /* Do very simple cleanup now, for the benefit of code that runs between
659 here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns. */
660 tidy_fallthru_edges ();
662 #ifdef ENABLE_CHECKING
665 timevar_pop (TV_CFG
);
668 /* Assume that someone emitted code with control flow instructions to the
669 basic block. Update the data structure. */
671 find_sub_basic_blocks (bb
)
676 rtx jump_insn
= NULL_RTX
;
678 basic_block first_bb
= bb
;
684 if (GET_CODE (insn
) == CODE_LABEL
)
685 insn
= NEXT_INSN (insn
);
687 /* Scan insn chain and try to find new basic block boundaries. */
690 enum rtx_code code
= GET_CODE (insn
);
697 /* On code label, split current basic block. */
699 falltru
= split_block (bb
, PREV_INSN (insn
));
703 remove_edge (falltru
);
705 if (LABEL_ALTERNATE_NAME (insn
))
706 make_edge (ENTRY_BLOCK_PTR
, bb
, 0);
710 /* In case we've previously split insn on the JUMP_INSN, move the
711 block header to proper place. */
714 falltru
= split_block (bb
, PREV_INSN (insn
));
717 remove_edge (falltru
);
720 /* We need some special care for those expressions. */
721 if (GET_CODE (insn
) == JUMP_INSN
)
723 if (GET_CODE (PATTERN (insn
)) == ADDR_VEC
724 || GET_CODE (PATTERN (insn
)) == ADDR_DIFF_VEC
)
734 insn
= NEXT_INSN (insn
);
737 /* In case expander replaced normal insn by sequence terminating by
738 return and barrier, or possibly other sequence not behaving like
739 ordinary jump, we need to take care and move basic block boundary. */
740 if (jump_insn
&& GET_CODE (bb
->end
) != JUMP_INSN
)
743 /* We've possibly replaced the conditional jump by conditional jump
744 followed by cleanup at fallthru edge, so the outgoing edges may
746 purge_dead_edges (bb
);
748 /* Now re-scan and wire in all edges. This expect simple (conditional)
749 jumps at the end of each new basic blocks. */
750 make_edges (NULL
, first_bb
->index
, bb
->index
, 1);
752 /* Update branch probabilities. Expect only (un)conditional jumps
753 to be created with only the forward edges. */
754 for (i
= first_bb
->index
; i
<= bb
->index
; i
++)
757 basic_block b
= BASIC_BLOCK (i
);
762 for (e
= b
->pred
; e
; e
=e
->pred_next
)
764 b
->count
+= e
->count
;
765 b
->frequency
+= EDGE_FREQUENCY (e
);
768 if (b
->succ
&& b
->succ
->succ_next
&& !b
->succ
->succ_next
->succ_next
)
770 rtx note
= find_reg_note (b
->end
, REG_BR_PROB
, NULL
);
775 probability
= INTVAL (XEXP (find_reg_note (b
->end
,
779 e
->probability
= probability
;
780 e
->count
= ((b
->count
* probability
+ REG_BR_PROB_BASE
/ 2)
782 f
= FALLTHRU_EDGE (b
);
783 f
->probability
= REG_BR_PROB_BASE
- probability
;
784 f
->count
= b
->count
- e
->count
;
786 if (b
->succ
&& !b
->succ
->succ_next
)
789 e
->probability
= REG_BR_PROB_BASE
;