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, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
37 #include "diagnostic-core.h"
42 static void make_edges (basic_block
, basic_block
, int);
43 static void make_label_edge (sbitmap
, basic_block
, rtx
, int);
44 static void find_bb_boundaries (basic_block
);
45 static void compute_outgoing_frequencies (basic_block
);
47 /* Return true if insn is something that should be contained inside basic
51 inside_basic_block_p (const_rtx insn
)
53 switch (GET_CODE (insn
))
56 /* Avoid creating of basic block for jumptables. */
57 return (NEXT_INSN (insn
) == 0
58 || !JUMP_P (NEXT_INSN (insn
))
59 || (GET_CODE (PATTERN (NEXT_INSN (insn
))) != ADDR_VEC
60 && GET_CODE (PATTERN (NEXT_INSN (insn
))) != ADDR_DIFF_VEC
));
63 return (GET_CODE (PATTERN (insn
)) != ADDR_VEC
64 && GET_CODE (PATTERN (insn
)) != ADDR_DIFF_VEC
);
80 /* Return true if INSN may cause control flow transfer, so it should be last in
84 control_flow_insn_p (const_rtx insn
)
86 switch (GET_CODE (insn
))
94 /* Jump insn always causes control transfer except for tablejumps. */
95 return (GET_CODE (PATTERN (insn
)) != ADDR_VEC
96 && GET_CODE (PATTERN (insn
)) != ADDR_DIFF_VEC
);
99 /* Noreturn and sibling call instructions terminate the basic blocks
100 (but only if they happen unconditionally). */
101 if ((SIBLING_CALL_P (insn
)
102 || find_reg_note (insn
, REG_NORETURN
, 0))
103 && GET_CODE (PATTERN (insn
)) != COND_EXEC
)
106 /* Call insn may return to the nonlocal goto handler. */
107 if (can_nonlocal_goto (insn
))
112 /* Treat trap instructions like noreturn calls (same provision). */
113 if (GET_CODE (PATTERN (insn
)) == TRAP_IF
114 && XEXP (PATTERN (insn
), 0) == const1_rtx
)
116 if (!cfun
->can_throw_non_call_exceptions
)
121 /* It is nonsense to reach barrier when looking for the
122 end of basic block, but before dead code is eliminated
130 return can_throw_internal (insn
);
134 /* Create an edge between two basic blocks. FLAGS are auxiliary information
135 about the edge that is accumulated between calls. */
137 /* Create an edge from a basic block to a label. */
140 make_label_edge (sbitmap edge_cache
, basic_block src
, rtx label
, int flags
)
142 gcc_assert (LABEL_P (label
));
144 /* If the label was never emitted, this insn is junk, but avoid a
145 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
146 as a result of a syntax error and a diagnostic has already been
149 if (INSN_UID (label
) == 0)
152 cached_make_edge (edge_cache
, src
, BLOCK_FOR_INSN (label
), flags
);
155 /* Create the edges generated by INSN in REGION. */
158 rtl_make_eh_edge (sbitmap edge_cache
, basic_block src
, rtx insn
)
160 eh_landing_pad lp
= get_eh_landing_pad_from_rtx (insn
);
164 rtx label
= lp
->landing_pad
;
166 /* During initial rtl generation, use the post_landing_pad. */
169 gcc_assert (lp
->post_landing_pad
);
170 label
= label_rtx (lp
->post_landing_pad
);
173 make_label_edge (edge_cache
, src
, label
,
174 EDGE_ABNORMAL
| EDGE_EH
175 | (CALL_P (insn
) ? EDGE_ABNORMAL_CALL
: 0));
179 /* States of basic block as seen by find_many_sub_basic_blocks. */
181 /* Basic blocks created via split_block belong to this state.
182 make_edges will examine these basic blocks to see if we need to
183 create edges going out of them. */
186 /* Basic blocks that do not need examining belong to this state.
187 These blocks will be left intact. In particular, make_edges will
188 not create edges going out of these basic blocks. */
191 /* Basic blocks that may need splitting (due to a label appearing in
192 the middle, etc) belong to this state. After splitting them,
193 make_edges will create edges going out of them as needed. */
197 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
198 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
200 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
201 #define BLOCK_USED_BY_TABLEJUMP 32
202 #define FULL_STATE(BB) ((size_t) (BB)->aux)
204 /* Identify the edges going out of basic blocks between MIN and MAX,
205 inclusive, that have their states set to BLOCK_NEW or
208 UPDATE_P should be nonzero if we are updating CFG and zero if we
209 are building CFG from scratch. */
212 make_edges (basic_block min
, basic_block max
, int update_p
)
215 sbitmap edge_cache
= NULL
;
217 /* Heavy use of computed goto in machine-generated code can lead to
218 nearly fully-connected CFGs. In that case we spend a significant
219 amount of time searching the edge lists for duplicates. */
220 if (forced_labels
|| cfun
->cfg
->max_jumptable_ents
> 100)
221 edge_cache
= sbitmap_alloc (last_basic_block
);
223 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
224 is always the entry. */
225 if (min
== ENTRY_BLOCK_PTR
->next_bb
)
226 make_edge (ENTRY_BLOCK_PTR
, min
, EDGE_FALLTHRU
);
228 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
235 if (STATE (bb
) == BLOCK_ORIGINAL
)
238 /* If we have an edge cache, cache edges going out of BB. */
241 sbitmap_zero (edge_cache
);
244 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
245 if (e
->dest
!= EXIT_BLOCK_PTR
)
246 SET_BIT (edge_cache
, e
->dest
->index
);
250 if (LABEL_P (BB_HEAD (bb
))
251 && LABEL_ALT_ENTRY_P (BB_HEAD (bb
)))
252 cached_make_edge (NULL
, ENTRY_BLOCK_PTR
, bb
, 0);
254 /* Examine the last instruction of the block, and discover the
255 ways we can leave the block. */
258 code
= GET_CODE (insn
);
261 if (code
== JUMP_INSN
)
265 /* Recognize a non-local goto as a branch outside the
267 if (find_reg_note (insn
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
270 /* Recognize a tablejump and do the right thing. */
271 else if (tablejump_p (insn
, NULL
, &tmp
))
276 if (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
)
277 vec
= XVEC (PATTERN (tmp
), 0);
279 vec
= XVEC (PATTERN (tmp
), 1);
281 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
282 make_label_edge (edge_cache
, bb
,
283 XEXP (RTVEC_ELT (vec
, j
), 0), 0);
285 /* Some targets (eg, ARM) emit a conditional jump that also
286 contains the out-of-range target. Scan for these and
287 add an edge if necessary. */
288 if ((tmp
= single_set (insn
)) != NULL
289 && SET_DEST (tmp
) == pc_rtx
290 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
291 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
292 make_label_edge (edge_cache
, bb
,
293 XEXP (XEXP (SET_SRC (tmp
), 2), 0), 0);
296 /* If this is a computed jump, then mark it as reaching
297 everything on the forced_labels list. */
298 else if (computed_jump_p (insn
))
300 for (x
= forced_labels
; x
; x
= XEXP (x
, 1))
301 make_label_edge (edge_cache
, bb
, XEXP (x
, 0), EDGE_ABNORMAL
);
304 /* Returns create an exit out. */
305 else if (returnjump_p (insn
))
306 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, 0);
308 /* Recognize asm goto and do the right thing. */
309 else if ((tmp
= extract_asm_operands (PATTERN (insn
))) != NULL
)
311 int i
, n
= ASM_OPERANDS_LABEL_LENGTH (tmp
);
312 for (i
= 0; i
< n
; ++i
)
313 make_label_edge (edge_cache
, bb
,
314 XEXP (ASM_OPERANDS_LABEL (tmp
, i
), 0), 0);
317 /* Otherwise, we have a plain conditional or unconditional jump. */
320 gcc_assert (JUMP_LABEL (insn
));
321 make_label_edge (edge_cache
, bb
, JUMP_LABEL (insn
), 0);
325 /* If this is a sibling call insn, then this is in effect a combined call
326 and return, and so we need an edge to the exit block. No need to
327 worry about EH edges, since we wouldn't have created the sibling call
328 in the first place. */
329 if (code
== CALL_INSN
&& SIBLING_CALL_P (insn
))
330 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
,
331 EDGE_SIBCALL
| EDGE_ABNORMAL
);
333 /* If this is a CALL_INSN, then mark it as reaching the active EH
334 handler for this CALL_INSN. If we're handling non-call
335 exceptions then any insn can reach any of the active handlers.
336 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
337 else if (code
== CALL_INSN
|| cfun
->can_throw_non_call_exceptions
)
339 /* Add any appropriate EH edges. */
340 rtl_make_eh_edge (edge_cache
, bb
, insn
);
342 if (code
== CALL_INSN
&& nonlocal_goto_handler_labels
)
344 /* ??? This could be made smarter: in some cases it's possible
345 to tell that certain calls will not do a nonlocal goto.
346 For example, if the nested functions that do the nonlocal
347 gotos do not have their addresses taken, then only calls to
348 those functions or to other nested functions that use them
349 could possibly do nonlocal gotos. */
350 if (can_nonlocal_goto (insn
))
351 for (x
= nonlocal_goto_handler_labels
; x
; x
= XEXP (x
, 1))
352 make_label_edge (edge_cache
, bb
, XEXP (x
, 0),
353 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
357 /* Find out if we can drop through to the next block. */
358 insn
= NEXT_INSN (insn
);
359 e
= find_edge (bb
, EXIT_BLOCK_PTR
);
360 if (e
&& e
->flags
& EDGE_FALLTHRU
)
365 && NOTE_KIND (insn
) != NOTE_INSN_BASIC_BLOCK
)
366 insn
= NEXT_INSN (insn
);
369 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, EDGE_FALLTHRU
);
370 else if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
372 if (insn
== BB_HEAD (bb
->next_bb
))
373 cached_make_edge (edge_cache
, bb
, bb
->next_bb
, EDGE_FALLTHRU
);
378 sbitmap_vector_free (edge_cache
);
382 mark_tablejump_edge (rtx label
)
386 gcc_assert (LABEL_P (label
));
387 /* See comment in make_label_edge. */
388 if (INSN_UID (label
) == 0)
390 bb
= BLOCK_FOR_INSN (label
);
391 SET_STATE (bb
, FULL_STATE (bb
) | BLOCK_USED_BY_TABLEJUMP
);
395 purge_dead_tablejump_edges (basic_block bb
, rtx table
)
397 rtx insn
= BB_END (bb
), tmp
;
403 if (GET_CODE (PATTERN (table
)) == ADDR_VEC
)
404 vec
= XVEC (PATTERN (table
), 0);
406 vec
= XVEC (PATTERN (table
), 1);
408 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
409 mark_tablejump_edge (XEXP (RTVEC_ELT (vec
, j
), 0));
411 /* Some targets (eg, ARM) emit a conditional jump that also
412 contains the out-of-range target. Scan for these and
413 add an edge if necessary. */
414 if ((tmp
= single_set (insn
)) != NULL
415 && SET_DEST (tmp
) == pc_rtx
416 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
417 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
418 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp
), 2), 0));
420 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
422 if (FULL_STATE (e
->dest
) & BLOCK_USED_BY_TABLEJUMP
)
423 SET_STATE (e
->dest
, FULL_STATE (e
->dest
)
424 & ~(size_t) BLOCK_USED_BY_TABLEJUMP
);
425 else if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
)))
434 /* Scan basic block BB for possible BB boundaries inside the block
435 and create new basic blocks in the progress. */
438 find_bb_boundaries (basic_block bb
)
440 basic_block orig_bb
= bb
;
441 rtx insn
= BB_HEAD (bb
);
442 rtx end
= BB_END (bb
), x
;
444 rtx flow_transfer_insn
= NULL_RTX
;
445 edge fallthru
= NULL
;
447 if (insn
== BB_END (bb
))
451 insn
= NEXT_INSN (insn
);
453 /* Scan insn chain and try to find new basic block boundaries. */
456 enum rtx_code code
= GET_CODE (insn
);
458 /* In case we've previously seen an insn that effects a control
459 flow transfer, split the block. */
460 if ((flow_transfer_insn
|| code
== CODE_LABEL
)
461 && inside_basic_block_p (insn
))
463 fallthru
= split_block (bb
, PREV_INSN (insn
));
464 if (flow_transfer_insn
)
466 BB_END (bb
) = flow_transfer_insn
;
468 /* Clean up the bb field for the insns between the blocks. */
469 for (x
= NEXT_INSN (flow_transfer_insn
);
470 x
!= BB_HEAD (fallthru
->dest
);
473 set_block_for_insn (x
, NULL
);
477 remove_edge (fallthru
);
478 flow_transfer_insn
= NULL_RTX
;
479 if (code
== CODE_LABEL
&& LABEL_ALT_ENTRY_P (insn
))
480 make_edge (ENTRY_BLOCK_PTR
, bb
, 0);
482 else if (code
== BARRIER
)
484 /* __builtin_unreachable () may cause a barrier to be emitted in
485 the middle of a BB. We need to split it in the same manner as
486 if the barrier were preceded by a control_flow_insn_p insn. */
487 if (!flow_transfer_insn
)
488 flow_transfer_insn
= prev_nonnote_insn_bb (insn
);
491 if (control_flow_insn_p (insn
))
492 flow_transfer_insn
= insn
;
495 insn
= NEXT_INSN (insn
);
498 /* In case expander replaced normal insn by sequence terminating by
499 return and barrier, or possibly other sequence not behaving like
500 ordinary jump, we need to take care and move basic block boundary. */
501 if (flow_transfer_insn
)
503 BB_END (bb
) = flow_transfer_insn
;
505 /* Clean up the bb field for the insns that do not belong to BB. */
506 x
= flow_transfer_insn
;
511 set_block_for_insn (x
, NULL
);
515 /* We've possibly replaced the conditional jump by conditional jump
516 followed by cleanup at fallthru edge, so the outgoing edges may
518 purge_dead_edges (bb
);
520 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
521 basic block, we might need to kill some edges. */
522 if (bb
!= orig_bb
&& tablejump_p (BB_END (bb
), NULL
, &table
))
523 purge_dead_tablejump_edges (bb
, table
);
526 /* Assume that frequency of basic block B is known. Compute frequencies
527 and probabilities of outgoing edges. */
530 compute_outgoing_frequencies (basic_block b
)
535 if (EDGE_COUNT (b
->succs
) == 2)
537 rtx note
= find_reg_note (BB_END (b
), REG_BR_PROB
, NULL
);
542 probability
= INTVAL (XEXP (note
, 0));
544 e
->probability
= probability
;
545 e
->count
= ((b
->count
* probability
+ REG_BR_PROB_BASE
/ 2)
547 f
= FALLTHRU_EDGE (b
);
548 f
->probability
= REG_BR_PROB_BASE
- probability
;
549 f
->count
= b
->count
- e
->count
;
554 if (single_succ_p (b
))
556 e
= single_succ_edge (b
);
557 e
->probability
= REG_BR_PROB_BASE
;
561 guess_outgoing_edge_probabilities (b
);
563 FOR_EACH_EDGE (e
, ei
, b
->succs
)
564 e
->count
= ((b
->count
* e
->probability
+ REG_BR_PROB_BASE
/ 2)
568 /* Assume that some pass has inserted labels or control flow
569 instructions within a basic block. Split basic blocks as needed
573 find_many_sub_basic_blocks (sbitmap blocks
)
575 basic_block bb
, min
, max
;
579 TEST_BIT (blocks
, bb
->index
) ? BLOCK_TO_SPLIT
: BLOCK_ORIGINAL
);
582 if (STATE (bb
) == BLOCK_TO_SPLIT
)
583 find_bb_boundaries (bb
);
586 if (STATE (bb
) != BLOCK_ORIGINAL
)
590 for (; bb
!= EXIT_BLOCK_PTR
; bb
= bb
->next_bb
)
591 if (STATE (bb
) != BLOCK_ORIGINAL
)
594 /* Now re-scan and wire in all edges. This expect simple (conditional)
595 jumps at the end of each new basic blocks. */
596 make_edges (min
, max
, 1);
598 /* Update branch probabilities. Expect only (un)conditional jumps
599 to be created with only the forward edges. */
600 if (profile_status
!= PROFILE_ABSENT
)
601 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
606 if (STATE (bb
) == BLOCK_ORIGINAL
)
608 if (STATE (bb
) == BLOCK_NEW
)
612 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
614 bb
->count
+= e
->count
;
615 bb
->frequency
+= EDGE_FREQUENCY (e
);
619 compute_outgoing_frequencies (bb
);