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, 2002, 2003, 2004, 2005, 2007 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* find_basic_blocks divides the current function's rtl into basic
22 blocks and constructs the CFG. The blocks are recorded in the
23 basic_block_info array; the CFG exists in the edge structures
24 referenced by the blocks.
26 find_basic_blocks also finds any unreachable loops and deletes them.
28 Available functionality:
34 #include "coretypes.h"
38 #include "hard-reg-set.h"
39 #include "basic-block.h"
48 static int count_basic_blocks (rtx
);
49 static void find_basic_blocks_1 (rtx
);
50 static void make_edges (basic_block
, basic_block
, int);
51 static void make_label_edge (sbitmap
, basic_block
, rtx
, int);
52 static void find_bb_boundaries (basic_block
);
53 static void compute_outgoing_frequencies (basic_block
);
55 /* Return true if insn is something that should be contained inside basic
59 inside_basic_block_p (rtx insn
)
61 switch (GET_CODE (insn
))
64 /* Avoid creating of basic block for jumptables. */
65 return (NEXT_INSN (insn
) == 0
66 || !JUMP_P (NEXT_INSN (insn
))
67 || (GET_CODE (PATTERN (NEXT_INSN (insn
))) != ADDR_VEC
68 && GET_CODE (PATTERN (NEXT_INSN (insn
))) != ADDR_DIFF_VEC
));
71 return (GET_CODE (PATTERN (insn
)) != ADDR_VEC
72 && GET_CODE (PATTERN (insn
)) != ADDR_DIFF_VEC
);
87 /* Return true if INSN may cause control flow transfer, so it should be last in
91 control_flow_insn_p (rtx insn
)
95 switch (GET_CODE (insn
))
102 /* Jump insn always causes control transfer except for tablejumps. */
103 return (GET_CODE (PATTERN (insn
)) != ADDR_VEC
104 && GET_CODE (PATTERN (insn
)) != ADDR_DIFF_VEC
);
107 /* Noreturn and sibling call instructions terminate the basic blocks
108 (but only if they happen unconditionally). */
109 if ((SIBLING_CALL_P (insn
)
110 || find_reg_note (insn
, REG_NORETURN
, 0))
111 && GET_CODE (PATTERN (insn
)) != COND_EXEC
)
113 /* Call insn may return to the nonlocal goto handler. */
114 return ((nonlocal_goto_handler_labels
115 && (0 == (note
= find_reg_note (insn
, REG_EH_REGION
,
117 || INTVAL (XEXP (note
, 0)) >= 0))
119 || can_throw_internal (insn
));
122 /* Treat trap instructions like noreturn calls (same provision). */
123 if (GET_CODE (PATTERN (insn
)) == TRAP_IF
124 && XEXP (PATTERN (insn
), 0) == const1_rtx
)
127 return (flag_non_call_exceptions
&& can_throw_internal (insn
));
130 /* It is nonsense to reach barrier when looking for the
131 end of basic block, but before dead code is eliminated
140 /* Count the basic blocks of the function. */
143 count_basic_blocks (rtx f
)
145 int count
= NUM_FIXED_BLOCKS
;
146 bool saw_insn
= false;
149 for (insn
= f
; insn
; insn
= NEXT_INSN (insn
))
151 /* Code labels and barriers causes current basic block to be
152 terminated at previous real insn. */
153 if ((LABEL_P (insn
) || BARRIER_P (insn
))
155 count
++, saw_insn
= false;
157 /* Start basic block if needed. */
158 if (!saw_insn
&& inside_basic_block_p (insn
))
161 /* Control flow insn causes current basic block to be terminated. */
162 if (saw_insn
&& control_flow_insn_p (insn
))
163 count
++, saw_insn
= false;
169 /* The rest of the compiler works a bit smoother when we don't have to
170 check for the edge case of do-nothing functions with no basic blocks. */
171 if (count
== NUM_FIXED_BLOCKS
)
173 emit_insn (gen_rtx_USE (VOIDmode
, const0_rtx
));
174 count
= NUM_FIXED_BLOCKS
+ 1;
180 /* Create an edge between two basic blocks. FLAGS are auxiliary information
181 about the edge that is accumulated between calls. */
183 /* Create an edge from a basic block to a label. */
186 make_label_edge (sbitmap edge_cache
, basic_block src
, rtx label
, int flags
)
188 gcc_assert (LABEL_P (label
));
190 /* If the label was never emitted, this insn is junk, but avoid a
191 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
192 as a result of a syntax error and a diagnostic has already been
195 if (INSN_UID (label
) == 0)
198 cached_make_edge (edge_cache
, src
, BLOCK_FOR_INSN (label
), flags
);
201 /* Create the edges generated by INSN in REGION. */
204 rtl_make_eh_edge (sbitmap edge_cache
, basic_block src
, rtx insn
)
206 int is_call
= CALL_P (insn
) ? EDGE_ABNORMAL_CALL
: 0;
209 handlers
= reachable_handlers (insn
);
211 for (i
= handlers
; i
; i
= XEXP (i
, 1))
212 make_label_edge (edge_cache
, src
, XEXP (i
, 0),
213 EDGE_ABNORMAL
| EDGE_EH
| is_call
);
215 free_INSN_LIST_list (&handlers
);
218 /* States of basic block as seen by find_many_sub_basic_blocks. */
220 /* Basic blocks created via split_block belong to this state.
221 make_edges will examine these basic blocks to see if we need to
222 create edges going out of them. */
225 /* Basic blocks that do not need examining belong to this state.
226 These blocks will be left intact. In particular, make_edges will
227 not create edges going out of these basic blocks. */
230 /* Basic blocks that may need splitting (due to a label appearing in
231 the middle, etc) belong to this state. After splitting them,
232 make_edges will create edges going out of them as needed. */
236 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
237 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
239 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
240 #define BLOCK_USED_BY_TABLEJUMP 32
241 #define FULL_STATE(BB) ((size_t) (BB)->aux)
243 /* Identify the edges going out of basic blocks between MIN and MAX,
244 inclusive, that have their states set to BLOCK_NEW or
247 UPDATE_P should be nonzero if we are updating CFG and zero if we
248 are building CFG from scratch. */
251 make_edges (basic_block min
, basic_block max
, int update_p
)
254 sbitmap edge_cache
= NULL
;
256 /* Heavy use of computed goto in machine-generated code can lead to
257 nearly fully-connected CFGs. In that case we spend a significant
258 amount of time searching the edge lists for duplicates. */
259 if (forced_labels
|| cfun
->max_jumptable_ents
> 100)
260 edge_cache
= sbitmap_alloc (last_basic_block
);
262 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
263 is always the entry. */
264 if (min
== ENTRY_BLOCK_PTR
->next_bb
)
265 make_edge (ENTRY_BLOCK_PTR
, min
, EDGE_FALLTHRU
);
267 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
274 if (STATE (bb
) == BLOCK_ORIGINAL
)
277 /* If we have an edge cache, cache edges going out of BB. */
280 sbitmap_zero (edge_cache
);
283 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
284 if (e
->dest
!= EXIT_BLOCK_PTR
)
285 SET_BIT (edge_cache
, e
->dest
->index
);
289 if (LABEL_P (BB_HEAD (bb
))
290 && LABEL_ALT_ENTRY_P (BB_HEAD (bb
)))
291 cached_make_edge (NULL
, ENTRY_BLOCK_PTR
, bb
, 0);
293 /* Examine the last instruction of the block, and discover the
294 ways we can leave the block. */
297 code
= GET_CODE (insn
);
300 if (code
== JUMP_INSN
)
304 /* Recognize exception handling placeholders. */
305 if (GET_CODE (PATTERN (insn
)) == RESX
)
306 rtl_make_eh_edge (edge_cache
, bb
, insn
);
308 /* Recognize a non-local goto as a branch outside the
310 else if (find_reg_note (insn
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
313 /* Recognize a tablejump and do the right thing. */
314 else if (tablejump_p (insn
, NULL
, &tmp
))
319 if (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
)
320 vec
= XVEC (PATTERN (tmp
), 0);
322 vec
= XVEC (PATTERN (tmp
), 1);
324 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
325 make_label_edge (edge_cache
, bb
,
326 XEXP (RTVEC_ELT (vec
, j
), 0), 0);
328 /* Some targets (eg, ARM) emit a conditional jump that also
329 contains the out-of-range target. Scan for these and
330 add an edge if necessary. */
331 if ((tmp
= single_set (insn
)) != NULL
332 && SET_DEST (tmp
) == pc_rtx
333 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
334 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
335 make_label_edge (edge_cache
, bb
,
336 XEXP (XEXP (SET_SRC (tmp
), 2), 0), 0);
339 /* If this is a computed jump, then mark it as reaching
340 everything on the forced_labels list. */
341 else if (computed_jump_p (insn
))
343 for (x
= forced_labels
; x
; x
= XEXP (x
, 1))
344 make_label_edge (edge_cache
, bb
, XEXP (x
, 0), EDGE_ABNORMAL
);
347 /* Returns create an exit out. */
348 else if (returnjump_p (insn
))
349 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, 0);
351 /* Otherwise, we have a plain conditional or unconditional jump. */
354 gcc_assert (JUMP_LABEL (insn
));
355 make_label_edge (edge_cache
, bb
, JUMP_LABEL (insn
), 0);
359 /* If this is a sibling call insn, then this is in effect a combined call
360 and return, and so we need an edge to the exit block. No need to
361 worry about EH edges, since we wouldn't have created the sibling call
362 in the first place. */
363 if (code
== CALL_INSN
&& SIBLING_CALL_P (insn
))
364 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
,
365 EDGE_SIBCALL
| EDGE_ABNORMAL
);
367 /* If this is a CALL_INSN, then mark it as reaching the active EH
368 handler for this CALL_INSN. If we're handling non-call
369 exceptions then any insn can reach any of the active handlers.
370 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
371 else if (code
== CALL_INSN
|| flag_non_call_exceptions
)
373 /* Add any appropriate EH edges. */
374 rtl_make_eh_edge (edge_cache
, bb
, insn
);
376 if (code
== CALL_INSN
&& nonlocal_goto_handler_labels
)
378 /* ??? This could be made smarter: in some cases it's possible
379 to tell that certain calls will not do a nonlocal goto.
380 For example, if the nested functions that do the nonlocal
381 gotos do not have their addresses taken, then only calls to
382 those functions or to other nested functions that use them
383 could possibly do nonlocal gotos. */
385 /* We do know that a REG_EH_REGION note with a value less
386 than 0 is guaranteed not to perform a non-local goto. */
387 rtx note
= find_reg_note (insn
, REG_EH_REGION
, NULL_RTX
);
389 if (!note
|| INTVAL (XEXP (note
, 0)) >= 0)
390 for (x
= nonlocal_goto_handler_labels
; x
; x
= XEXP (x
, 1))
391 make_label_edge (edge_cache
, bb
, XEXP (x
, 0),
392 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
396 /* Find out if we can drop through to the next block. */
397 insn
= NEXT_INSN (insn
);
398 e
= find_edge (bb
, EXIT_BLOCK_PTR
);
399 if (e
&& e
->flags
& EDGE_FALLTHRU
)
404 && NOTE_LINE_NUMBER (insn
) != NOTE_INSN_BASIC_BLOCK
)
405 insn
= NEXT_INSN (insn
);
408 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, EDGE_FALLTHRU
);
409 else if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
411 if (insn
== BB_HEAD (bb
->next_bb
))
412 cached_make_edge (edge_cache
, bb
, bb
->next_bb
, EDGE_FALLTHRU
);
417 sbitmap_vector_free (edge_cache
);
420 /* Find all basic blocks of the function whose first insn is F.
422 Collect and return a list of labels whose addresses are taken. This
423 will be used in make_edges for use with computed gotos. */
426 find_basic_blocks_1 (rtx f
)
429 rtx bb_note
= NULL_RTX
;
432 basic_block prev
= ENTRY_BLOCK_PTR
;
434 /* We process the instructions in a slightly different way than we did
435 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
436 closed out the previous block, so that it gets attached at the proper
437 place. Since this form should be equivalent to the previous,
438 count_basic_blocks continues to use the old form as a check. */
440 for (insn
= f
; insn
; insn
= next
)
442 enum rtx_code code
= GET_CODE (insn
);
444 next
= NEXT_INSN (insn
);
446 if ((LABEL_P (insn
) || BARRIER_P (insn
))
449 prev
= create_basic_block_structure (head
, end
, bb_note
, prev
);
450 head
= end
= NULL_RTX
;
454 if (inside_basic_block_p (insn
))
456 if (head
== NULL_RTX
)
461 if (head
&& control_flow_insn_p (insn
))
463 prev
= create_basic_block_structure (head
, end
, bb_note
, prev
);
464 head
= end
= NULL_RTX
;
472 int kind
= NOTE_LINE_NUMBER (insn
);
474 /* Look for basic block notes with which to keep the
475 basic_block_info pointers stable. Unthread the note now;
476 we'll put it back at the right place in create_basic_block.
477 Or not at all if we've already found a note in this block. */
478 if (kind
== NOTE_INSN_BASIC_BLOCK
)
480 if (bb_note
== NULL_RTX
)
483 next
= delete_insn (insn
);
500 if (head
!= NULL_RTX
)
501 create_basic_block_structure (head
, end
, bb_note
, prev
);
503 delete_insn (bb_note
);
505 gcc_assert (last_basic_block
== n_basic_blocks
);
507 clear_aux_for_blocks ();
511 /* Find basic blocks of the current function.
512 F is the first insn of the function. */
515 find_basic_blocks (rtx f
)
519 timevar_push (TV_CFG
);
521 /* Flush out existing data. */
522 if (basic_block_info
!= NULL
)
526 /* Clear bb->aux on all extant basic blocks. We'll use this as a
527 tag for reuse during create_basic_block, just in case some pass
528 copies around basic block notes improperly. */
532 basic_block_info
= NULL
;
535 n_basic_blocks
= count_basic_blocks (f
);
536 last_basic_block
= NUM_FIXED_BLOCKS
;
537 ENTRY_BLOCK_PTR
->next_bb
= EXIT_BLOCK_PTR
;
538 EXIT_BLOCK_PTR
->prev_bb
= ENTRY_BLOCK_PTR
;
541 /* Size the basic block table. The actual structures will be allocated
542 by find_basic_blocks_1, since we want to keep the structure pointers
543 stable across calls to find_basic_blocks. */
544 /* ??? This whole issue would be much simpler if we called find_basic_blocks
545 exactly once, and thereafter we don't have a single long chain of
546 instructions at all until close to the end of compilation when we
547 actually lay them out. */
549 basic_block_info
= VEC_alloc (basic_block
, gc
, n_basic_blocks
);
550 VEC_safe_grow (basic_block
, gc
, basic_block_info
, n_basic_blocks
);
551 memset (VEC_address (basic_block
, basic_block_info
), 0,
552 sizeof (basic_block
) * n_basic_blocks
);
553 SET_BASIC_BLOCK (ENTRY_BLOCK
, ENTRY_BLOCK_PTR
);
554 SET_BASIC_BLOCK (EXIT_BLOCK
, EXIT_BLOCK_PTR
);
556 find_basic_blocks_1 (f
);
558 profile_status
= PROFILE_ABSENT
;
560 /* Tell make_edges to examine every block for out-going edges. */
562 SET_STATE (bb
, BLOCK_NEW
);
564 /* Discover the edges of our cfg. */
565 make_edges (ENTRY_BLOCK_PTR
->next_bb
, EXIT_BLOCK_PTR
->prev_bb
, 0);
567 /* Do very simple cleanup now, for the benefit of code that runs between
568 here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns. */
569 tidy_fallthru_edges ();
571 #ifdef ENABLE_CHECKING
574 timevar_pop (TV_CFG
);
578 mark_tablejump_edge (rtx label
)
582 gcc_assert (LABEL_P (label
));
583 /* See comment in make_label_edge. */
584 if (INSN_UID (label
) == 0)
586 bb
= BLOCK_FOR_INSN (label
);
587 SET_STATE (bb
, FULL_STATE (bb
) | BLOCK_USED_BY_TABLEJUMP
);
591 purge_dead_tablejump_edges (basic_block bb
, rtx table
)
593 rtx insn
= BB_END (bb
), tmp
;
599 if (GET_CODE (PATTERN (table
)) == ADDR_VEC
)
600 vec
= XVEC (PATTERN (table
), 0);
602 vec
= XVEC (PATTERN (table
), 1);
604 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
605 mark_tablejump_edge (XEXP (RTVEC_ELT (vec
, j
), 0));
607 /* Some targets (eg, ARM) emit a conditional jump that also
608 contains the out-of-range target. Scan for these and
609 add an edge if necessary. */
610 if ((tmp
= single_set (insn
)) != NULL
611 && SET_DEST (tmp
) == pc_rtx
612 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
613 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
614 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp
), 2), 0));
616 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
618 if (FULL_STATE (e
->dest
) & BLOCK_USED_BY_TABLEJUMP
)
619 SET_STATE (e
->dest
, FULL_STATE (e
->dest
)
620 & ~(size_t) BLOCK_USED_BY_TABLEJUMP
);
621 else if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
)))
630 /* Scan basic block BB for possible BB boundaries inside the block
631 and create new basic blocks in the progress. */
634 find_bb_boundaries (basic_block bb
)
636 basic_block orig_bb
= bb
;
637 rtx insn
= BB_HEAD (bb
);
638 rtx end
= BB_END (bb
);
640 rtx flow_transfer_insn
= NULL_RTX
;
641 edge fallthru
= NULL
;
643 if (insn
== BB_END (bb
))
647 insn
= NEXT_INSN (insn
);
649 /* Scan insn chain and try to find new basic block boundaries. */
652 enum rtx_code code
= GET_CODE (insn
);
654 /* On code label, split current basic block. */
655 if (code
== CODE_LABEL
)
657 fallthru
= split_block (bb
, PREV_INSN (insn
));
658 if (flow_transfer_insn
)
659 BB_END (bb
) = flow_transfer_insn
;
662 remove_edge (fallthru
);
663 flow_transfer_insn
= NULL_RTX
;
664 if (LABEL_ALT_ENTRY_P (insn
))
665 make_edge (ENTRY_BLOCK_PTR
, bb
, 0);
668 /* In case we've previously seen an insn that effects a control
669 flow transfer, split the block. */
670 if (flow_transfer_insn
&& inside_basic_block_p (insn
))
672 fallthru
= split_block (bb
, PREV_INSN (insn
));
673 BB_END (bb
) = flow_transfer_insn
;
675 remove_edge (fallthru
);
676 flow_transfer_insn
= NULL_RTX
;
679 if (control_flow_insn_p (insn
))
680 flow_transfer_insn
= insn
;
683 insn
= NEXT_INSN (insn
);
686 /* In case expander replaced normal insn by sequence terminating by
687 return and barrier, or possibly other sequence not behaving like
688 ordinary jump, we need to take care and move basic block boundary. */
689 if (flow_transfer_insn
)
690 BB_END (bb
) = flow_transfer_insn
;
692 /* We've possibly replaced the conditional jump by conditional jump
693 followed by cleanup at fallthru edge, so the outgoing edges may
695 purge_dead_edges (bb
);
697 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
698 basic block, we might need to kill some edges. */
699 if (bb
!= orig_bb
&& tablejump_p (BB_END (bb
), NULL
, &table
))
700 purge_dead_tablejump_edges (bb
, table
);
703 /* Assume that frequency of basic block B is known. Compute frequencies
704 and probabilities of outgoing edges. */
707 compute_outgoing_frequencies (basic_block b
)
712 if (EDGE_COUNT (b
->succs
) == 2)
714 rtx note
= find_reg_note (BB_END (b
), REG_BR_PROB
, NULL
);
719 probability
= INTVAL (XEXP (note
, 0));
721 e
->probability
= probability
;
722 e
->count
= ((b
->count
* probability
+ REG_BR_PROB_BASE
/ 2)
724 f
= FALLTHRU_EDGE (b
);
725 f
->probability
= REG_BR_PROB_BASE
- probability
;
726 f
->count
= b
->count
- e
->count
;
731 if (single_succ_p (b
))
733 e
= single_succ_edge (b
);
734 e
->probability
= REG_BR_PROB_BASE
;
738 guess_outgoing_edge_probabilities (b
);
740 FOR_EACH_EDGE (e
, ei
, b
->succs
)
741 e
->count
= ((b
->count
* e
->probability
+ REG_BR_PROB_BASE
/ 2)
745 /* Assume that some pass has inserted labels or control flow
746 instructions within a basic block. Split basic blocks as needed
750 find_many_sub_basic_blocks (sbitmap blocks
)
752 basic_block bb
, min
, max
;
756 TEST_BIT (blocks
, bb
->index
) ? BLOCK_TO_SPLIT
: BLOCK_ORIGINAL
);
759 if (STATE (bb
) == BLOCK_TO_SPLIT
)
760 find_bb_boundaries (bb
);
763 if (STATE (bb
) != BLOCK_ORIGINAL
)
767 for (; bb
!= EXIT_BLOCK_PTR
; bb
= bb
->next_bb
)
768 if (STATE (bb
) != BLOCK_ORIGINAL
)
771 /* Now re-scan and wire in all edges. This expect simple (conditional)
772 jumps at the end of each new basic blocks. */
773 make_edges (min
, max
, 1);
775 /* Update branch probabilities. Expect only (un)conditional jumps
776 to be created with only the forward edges. */
777 if (profile_status
!= PROFILE_ABSENT
)
778 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
783 if (STATE (bb
) == BLOCK_ORIGINAL
)
785 if (STATE (bb
) == BLOCK_NEW
)
789 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
791 bb
->count
+= e
->count
;
792 bb
->frequency
+= EDGE_FREQUENCY (e
);
796 compute_outgoing_frequencies (bb
);