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 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
33 find_sub_basic_blocks */
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
49 static int count_basic_blocks PARAMS ((rtx));
50 static void find_basic_blocks_1 PARAMS ((rtx));
51 static rtx find_label_refs PARAMS ((rtx, rtx));
52 static void make_edges PARAMS ((rtx, basic_block,
54 static void make_label_edge PARAMS ((sbitmap *, basic_block,
56 static void make_eh_edge PARAMS ((sbitmap *, basic_block, rtx));
57 static void find_bb_boundaries PARAMS ((basic_block));
58 static void compute_outgoing_frequencies PARAMS ((basic_block));
59 static bool inside_basic_block_p PARAMS ((rtx));
61 /* Return true if insn is something that should be contained inside basic
65 inside_basic_block_p (insn)
68 switch (GET_CODE (insn))
71 /* Avoid creating of basic block for jumptables. */
72 return (NEXT_INSN (insn) == 0
73 || GET_CODE (NEXT_INSN (insn)) != JUMP_INSN
74 || (GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_VEC
75 && GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_DIFF_VEC));
78 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
79 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
94 /* Return true if INSN may cause control flow transfer, so it should be last in
98 control_flow_insn_p (insn)
103 switch (GET_CODE (insn))
110 /* Jump insn always causes control transfer except for tablejumps. */
111 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
112 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
115 /* Call insn may return to the nonlocal goto handler. */
116 return ((nonlocal_goto_handler_labels
117 && (0 == (note = find_reg_note (insn, REG_EH_REGION,
119 || INTVAL (XEXP (note, 0)) >= 0))
121 || can_throw_internal (insn));
124 return (flag_non_call_exceptions && can_throw_internal (insn));
127 /* It is nonsence to reach barrier when looking for the
128 end of basic block, but before dead code is eliminated
137 /* Count the basic blocks of the function. */
140 count_basic_blocks (f)
144 bool saw_insn = false;
147 for (insn = f; insn; insn = NEXT_INSN (insn))
149 /* Code labels and barriers causes curent basic block to be
150 terminated at previous real insn. */
151 if ((GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == BARRIER)
153 count++, saw_insn = false;
155 /* Start basic block if needed. */
156 if (!saw_insn && inside_basic_block_p (insn))
159 /* Control flow insn causes current basic block to be terminated. */
160 if (saw_insn && control_flow_insn_p (insn))
161 count++, saw_insn = false;
167 /* The rest of the compiler works a bit smoother when we don't have to
168 check for the edge case of do-nothing functions with no basic blocks. */
171 emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
178 /* Scan a list of insns for labels referred to other than by jumps.
179 This is used to scan the alternatives of a call placeholder. */
182 find_label_refs (f, lvl)
188 for (insn = f; insn; insn = NEXT_INSN (insn))
189 if (INSN_P (insn) && GET_CODE (insn) != JUMP_INSN)
193 /* Make a list of all labels referred to other than by jumps
194 (which just don't have the REG_LABEL notes).
196 Make a special exception for labels followed by an ADDR*VEC,
197 as this would be a part of the tablejump setup code.
199 Make a special exception to registers loaded with label
200 values just before jump insns that use them. */
202 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
203 if (REG_NOTE_KIND (note) == REG_LABEL)
205 rtx lab = XEXP (note, 0), next;
207 if ((next = next_nonnote_insn (lab)) != NULL
208 && GET_CODE (next) == JUMP_INSN
209 && (GET_CODE (PATTERN (next)) == ADDR_VEC
210 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
212 else if (GET_CODE (lab) == NOTE)
214 else if (GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
215 && find_reg_note (NEXT_INSN (insn), REG_LABEL, lab))
218 lvl = alloc_EXPR_LIST (0, XEXP (note, 0), lvl);
225 /* Create an edge between two basic blocks. FLAGS are auxiliary information
226 about the edge that is accumulated between calls. */
228 /* Create an edge from a basic block to a label. */
231 make_label_edge (edge_cache, src, label, flags)
237 if (GET_CODE (label) != CODE_LABEL)
240 /* If the label was never emitted, this insn is junk, but avoid a
241 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
242 as a result of a syntax error and a diagnostic has already been
245 if (INSN_UID (label) == 0)
248 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
251 /* Create the edges generated by INSN in REGION. */
254 make_eh_edge (edge_cache, src, insn)
259 int is_call = GET_CODE (insn) == CALL_INSN ? EDGE_ABNORMAL_CALL : 0;
262 handlers = reachable_handlers (insn);
264 for (i = handlers; i; i = XEXP (i, 1))
265 make_label_edge (edge_cache, src, XEXP (i, 0),
266 EDGE_ABNORMAL | EDGE_EH | is_call);
268 free_INSN_LIST_list (&handlers);
271 /* Identify the edges between basic blocks MIN to MAX.
273 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
274 that are otherwise unreachable may be reachable with a non-local goto.
276 BB_EH_END is an array indexed by basic block number in which we record
277 the list of exception regions active at the end of the basic block. */
280 make_edges (label_value_list, min, max, update_p)
281 rtx label_value_list;
282 basic_block min, max;
286 sbitmap *edge_cache = NULL;
288 /* Assume no computed jump; revise as we create edges. */
289 current_function_has_computed_jump = 0;
291 /* Heavy use of computed goto in machine-generated code can lead to
292 nearly fully-connected CFGs. In that case we spend a significant
293 amount of time searching the edge lists for duplicates. */
294 if (forced_labels || label_value_list || cfun->max_jumptable_ents > 100)
296 edge_cache = sbitmap_vector_alloc (last_basic_block, last_basic_block);
297 sbitmap_vector_zero (edge_cache, last_basic_block);
300 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
304 for (e = bb->succ; e ; e = e->succ_next)
305 if (e->dest != EXIT_BLOCK_PTR)
306 SET_BIT (edge_cache[bb->index], e->dest->index);
310 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
311 is always the entry. */
312 if (min == ENTRY_BLOCK_PTR->next_bb)
313 cached_make_edge (edge_cache, ENTRY_BLOCK_PTR, min,
316 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
320 int force_fallthru = 0;
322 if (GET_CODE (bb->head) == CODE_LABEL && LABEL_ALT_ENTRY_P (bb->head))
323 cached_make_edge (NULL, ENTRY_BLOCK_PTR, bb, 0);
325 /* Examine the last instruction of the block, and discover the
326 ways we can leave the block. */
329 code = GET_CODE (insn);
332 if (code == JUMP_INSN)
336 /* Recognize exception handling placeholders. */
337 if (GET_CODE (PATTERN (insn)) == RESX)
338 make_eh_edge (edge_cache, bb, insn);
340 /* Recognize a non-local goto as a branch outside the
342 else if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
345 /* ??? Recognize a tablejump and do the right thing. */
346 else if ((tmp = JUMP_LABEL (insn)) != NULL_RTX
347 && (tmp = NEXT_INSN (tmp)) != NULL_RTX
348 && GET_CODE (tmp) == JUMP_INSN
349 && (GET_CODE (PATTERN (tmp)) == ADDR_VEC
350 || GET_CODE (PATTERN (tmp)) == ADDR_DIFF_VEC))
355 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
356 vec = XVEC (PATTERN (tmp), 0);
358 vec = XVEC (PATTERN (tmp), 1);
360 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
361 make_label_edge (edge_cache, bb,
362 XEXP (RTVEC_ELT (vec, j), 0), 0);
364 /* Some targets (eg, ARM) emit a conditional jump that also
365 contains the out-of-range target. Scan for these and
366 add an edge if necessary. */
367 if ((tmp = single_set (insn)) != NULL
368 && SET_DEST (tmp) == pc_rtx
369 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
370 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
371 make_label_edge (edge_cache, bb,
372 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
374 #ifdef CASE_DROPS_THROUGH
375 /* Silly VAXen. The ADDR_VEC is going to be in the way of
376 us naturally detecting fallthru into the next block. */
381 /* If this is a computed jump, then mark it as reaching
382 everything on the label_value_list and forced_labels list. */
383 else if (computed_jump_p (insn))
385 current_function_has_computed_jump = 1;
387 for (x = label_value_list; x; x = XEXP (x, 1))
388 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
390 for (x = forced_labels; x; x = XEXP (x, 1))
391 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
394 /* Returns create an exit out. */
395 else if (returnjump_p (insn))
396 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
398 /* Otherwise, we have a plain conditional or unconditional jump. */
401 if (! JUMP_LABEL (insn))
403 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
407 /* If this is a sibling call insn, then this is in effect a combined call
408 and return, and so we need an edge to the exit block. No need to
409 worry about EH edges, since we wouldn't have created the sibling call
410 in the first place. */
411 if (code == CALL_INSN && SIBLING_CALL_P (insn))
412 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
413 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
415 /* If this is a CALL_INSN, then mark it as reaching the active EH
416 handler for this CALL_INSN. If we're handling non-call
417 exceptions then any insn can reach any of the active handlers.
418 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
419 else if (code == CALL_INSN || flag_non_call_exceptions)
421 /* Add any appropriate EH edges. */
422 make_eh_edge (edge_cache, bb, insn);
424 if (code == CALL_INSN && nonlocal_goto_handler_labels)
426 /* ??? This could be made smarter: in some cases it's possible
427 to tell that certain calls will not do a nonlocal goto.
428 For example, if the nested functions that do the nonlocal
429 gotos do not have their addresses taken, then only calls to
430 those functions or to other nested functions that use them
431 could possibly do nonlocal gotos. */
433 /* We do know that a REG_EH_REGION note with a value less
434 than 0 is guaranteed not to perform a non-local goto. */
435 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
437 if (!note || INTVAL (XEXP (note, 0)) >= 0)
438 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
439 make_label_edge (edge_cache, bb, XEXP (x, 0),
440 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
444 /* Find out if we can drop through to the next block. */
445 insn = next_nonnote_insn (insn);
446 if (!insn || (bb->next_bb == EXIT_BLOCK_PTR && force_fallthru))
447 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
448 else if (bb->next_bb != EXIT_BLOCK_PTR)
450 rtx tmp = bb->next_bb->head;
451 if (GET_CODE (tmp) == NOTE)
452 tmp = next_nonnote_insn (tmp);
453 if (force_fallthru || insn == tmp)
454 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
459 sbitmap_vector_free (edge_cache);
462 /* Find all basic blocks of the function whose first insn is F.
464 Collect and return a list of labels whose addresses are taken. This
465 will be used in make_edges for use with computed gotos. */
468 find_basic_blocks_1 (f)
472 rtx bb_note = NULL_RTX;
477 basic_block prev = ENTRY_BLOCK_PTR;
479 /* We process the instructions in a slightly different way than we did
480 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
481 closed out the previous block, so that it gets attached at the proper
482 place. Since this form should be equivalent to the previous,
483 count_basic_blocks continues to use the old form as a check. */
485 for (insn = f; insn; insn = next)
487 enum rtx_code code = GET_CODE (insn);
489 next = NEXT_INSN (insn);
491 if ((GET_CODE (insn) == CODE_LABEL || GET_CODE (insn) == BARRIER)
494 prev = create_basic_block_structure (head, end, bb_note, prev);
495 head = end = NULL_RTX;
499 if (inside_basic_block_p (insn))
501 if (head == NULL_RTX)
506 if (head && control_flow_insn_p (insn))
508 prev = create_basic_block_structure (head, end, bb_note, prev);
509 head = end = NULL_RTX;
517 int kind = NOTE_LINE_NUMBER (insn);
519 /* Look for basic block notes with which to keep the
520 basic_block_info pointers stable. Unthread the note now;
521 we'll put it back at the right place in create_basic_block.
522 Or not at all if we've already found a note in this block. */
523 if (kind == NOTE_INSN_BASIC_BLOCK)
525 if (bb_note == NULL_RTX)
528 next = delete_insn (insn);
540 if (GET_CODE (PATTERN (insn)) == CALL_PLACEHOLDER)
542 /* Scan each of the alternatives for label refs. */
543 lvl = find_label_refs (XEXP (PATTERN (insn), 0), lvl);
544 lvl = find_label_refs (XEXP (PATTERN (insn), 1), lvl);
545 lvl = find_label_refs (XEXP (PATTERN (insn), 2), lvl);
546 /* Record its tail recursion label, if any. */
547 if (XEXP (PATTERN (insn), 3) != NULL_RTX)
548 trll = alloc_EXPR_LIST (0, XEXP (PATTERN (insn), 3), trll);
556 if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN)
560 /* Make a list of all labels referred to other than by jumps.
562 Make a special exception for labels followed by an ADDR*VEC,
563 as this would be a part of the tablejump setup code.
565 Make a special exception to registers loaded with label
566 values just before jump insns that use them. */
568 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
569 if (REG_NOTE_KIND (note) == REG_LABEL)
571 rtx lab = XEXP (note, 0), next;
573 if ((next = next_nonnote_insn (lab)) != NULL
574 && GET_CODE (next) == JUMP_INSN
575 && (GET_CODE (PATTERN (next)) == ADDR_VEC
576 || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC))
578 else if (GET_CODE (lab) == NOTE)
580 else if (GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
581 && find_reg_note (NEXT_INSN (insn), REG_LABEL, lab))
584 lvl = alloc_EXPR_LIST (0, XEXP (note, 0), lvl);
589 if (head != NULL_RTX)
590 create_basic_block_structure (head, end, bb_note, prev);
592 delete_insn (bb_note);
594 if (last_basic_block != n_basic_blocks)
597 label_value_list = lvl;
598 tail_recursion_label_list = trll;
599 clear_aux_for_blocks ();
603 /* Find basic blocks of the current function.
604 F is the first insn of the function and NREGS the number of register
608 find_basic_blocks (f, nregs, file)
610 int nregs ATTRIBUTE_UNUSED;
611 FILE *file ATTRIBUTE_UNUSED;
615 timevar_push (TV_CFG);
617 /* Flush out existing data. */
618 if (basic_block_info != NULL)
622 /* Clear bb->aux on all extant basic blocks. We'll use this as a
623 tag for reuse during create_basic_block, just in case some pass
624 copies around basic block notes improperly. */
628 VARRAY_FREE (basic_block_info);
631 n_basic_blocks = count_basic_blocks (f);
632 last_basic_block = 0;
633 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
634 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
636 /* Size the basic block table. The actual structures will be allocated
637 by find_basic_blocks_1, since we want to keep the structure pointers
638 stable across calls to find_basic_blocks. */
639 /* ??? This whole issue would be much simpler if we called find_basic_blocks
640 exactly once, and thereafter we don't have a single long chain of
641 instructions at all until close to the end of compilation when we
642 actually lay them out. */
644 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
646 find_basic_blocks_1 (f);
648 /* Discover the edges of our cfg. */
649 make_edges (label_value_list, ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, 0);
651 /* Do very simple cleanup now, for the benefit of code that runs between
652 here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns. */
653 tidy_fallthru_edges ();
655 #ifdef ENABLE_CHECKING
658 timevar_pop (TV_CFG);
661 /* State of basic block as seen by find_sub_basic_blocks. */
662 enum state {BLOCK_NEW = 0, BLOCK_ORIGINAL, BLOCK_TO_SPLIT};
664 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
665 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
667 /* Scan basic block BB for possible BB boundaries inside the block
668 and create new basic blocks in the progress. */
671 find_bb_boundaries (bb)
676 rtx flow_transfer_insn = NULL_RTX;
677 edge fallthru = NULL;
682 if (GET_CODE (insn) == CODE_LABEL)
683 insn = NEXT_INSN (insn);
685 /* Scan insn chain and try to find new basic block boundaries. */
688 enum rtx_code code = GET_CODE (insn);
690 /* On code label, split current basic block. */
691 if (code == CODE_LABEL)
693 fallthru = split_block (bb, PREV_INSN (insn));
694 if (flow_transfer_insn)
695 bb->end = flow_transfer_insn;
698 remove_edge (fallthru);
699 flow_transfer_insn = NULL_RTX;
700 if (LABEL_ALT_ENTRY_P (insn))
701 make_edge (ENTRY_BLOCK_PTR, bb, 0);
704 /* In case we've previously seen an insn that effects a control
705 flow transfer, split the block. */
706 if (flow_transfer_insn && inside_basic_block_p (insn))
708 fallthru = split_block (bb, PREV_INSN (insn));
709 bb->end = flow_transfer_insn;
711 remove_edge (fallthru);
712 flow_transfer_insn = NULL_RTX;
715 if (control_flow_insn_p (insn))
716 flow_transfer_insn = insn;
719 insn = NEXT_INSN (insn);
722 /* In case expander replaced normal insn by sequence terminating by
723 return and barrier, or possibly other sequence not behaving like
724 ordinary jump, we need to take care and move basic block boundary. */
725 if (flow_transfer_insn)
726 bb->end = flow_transfer_insn;
728 /* We've possibly replaced the conditional jump by conditional jump
729 followed by cleanup at fallthru edge, so the outgoing edges may
731 purge_dead_edges (bb);
734 /* Assume that frequency of basic block B is known. Compute frequencies
735 and probabilities of outgoing edges. */
738 compute_outgoing_frequencies (b)
743 if (b->succ && b->succ->succ_next && !b->succ->succ_next->succ_next)
745 rtx note = find_reg_note (b->end, REG_BR_PROB, NULL);
751 probability = INTVAL (XEXP (find_reg_note (b->end,
755 e->probability = probability;
756 e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
758 f = FALLTHRU_EDGE (b);
759 f->probability = REG_BR_PROB_BASE - probability;
760 f->count = b->count - e->count;
763 if (b->succ && !b->succ->succ_next)
766 e->probability = REG_BR_PROB_BASE;
771 /* Assume that someone emitted code with control flow instructions to the
772 basic block. Update the data structure. */
775 find_many_sub_basic_blocks (blocks)
778 basic_block bb, min, max;
782 TEST_BIT (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
785 if (STATE (bb) == BLOCK_TO_SPLIT)
786 find_bb_boundaries (bb);
789 if (STATE (bb) != BLOCK_ORIGINAL)
793 for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
794 if (STATE (bb) != BLOCK_ORIGINAL)
797 /* Now re-scan and wire in all edges. This expect simple (conditional)
798 jumps at the end of each new basic blocks. */
799 make_edges (NULL, min, max, 1);
801 /* Update branch probabilities. Expect only (un)conditional jumps
802 to be created with only the forward edges. */
803 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
807 if (STATE (bb) == BLOCK_ORIGINAL)
809 if (STATE (bb) == BLOCK_NEW)
813 for (e = bb->pred; e; e=e->pred_next)
815 bb->count += e->count;
816 bb->frequency += EDGE_FREQUENCY (e);
820 compute_outgoing_frequencies (bb);
827 /* Like above but for single basic block only. */
830 find_sub_basic_blocks (bb)
833 basic_block min, max, b;
834 basic_block next = bb->next_bb;
837 find_bb_boundaries (bb);
840 /* Now re-scan and wire in all edges. This expect simple (conditional)
841 jumps at the end of each new basic blocks. */
842 make_edges (NULL, min, max, 1);
844 /* Update branch probabilities. Expect only (un)conditional jumps
845 to be created with only the forward edges. */
846 FOR_BB_BETWEEN (b, min, max->next_bb, next_bb)
854 for (e = b->pred; e; e=e->pred_next)
856 b->count += e->count;
857 b->frequency += EDGE_FREQUENCY (e);
861 compute_outgoing_frequencies (b);