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, 2009, 2010
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"
36 #include "diagnostic-core.h"
40 static void make_edges (basic_block
, basic_block
, int);
41 static void make_label_edge (sbitmap
, basic_block
, rtx
, int);
42 static void find_bb_boundaries (basic_block
);
43 static void compute_outgoing_frequencies (basic_block
);
45 /* Return true if insn is something that should be contained inside basic
49 inside_basic_block_p (const_rtx insn
)
51 switch (GET_CODE (insn
))
54 /* Avoid creating of basic block for jumptables. */
55 return (NEXT_INSN (insn
) == 0
56 || !JUMP_P (NEXT_INSN (insn
))
57 || (GET_CODE (PATTERN (NEXT_INSN (insn
))) != ADDR_VEC
58 && GET_CODE (PATTERN (NEXT_INSN (insn
))) != ADDR_DIFF_VEC
));
61 return (GET_CODE (PATTERN (insn
)) != ADDR_VEC
62 && GET_CODE (PATTERN (insn
)) != ADDR_DIFF_VEC
);
78 /* Return true if INSN may cause control flow transfer, so it should be last in
82 control_flow_insn_p (const_rtx insn
)
84 switch (GET_CODE (insn
))
92 /* Jump insn always causes control transfer except for tablejumps. */
93 return (GET_CODE (PATTERN (insn
)) != ADDR_VEC
94 && GET_CODE (PATTERN (insn
)) != ADDR_DIFF_VEC
);
97 /* Noreturn and sibling call instructions terminate the basic blocks
98 (but only if they happen unconditionally). */
99 if ((SIBLING_CALL_P (insn
)
100 || find_reg_note (insn
, REG_NORETURN
, 0))
101 && GET_CODE (PATTERN (insn
)) != COND_EXEC
)
104 /* Call insn may return to the nonlocal goto handler. */
105 if (can_nonlocal_goto (insn
))
110 /* Treat trap instructions like noreturn calls (same provision). */
111 if (GET_CODE (PATTERN (insn
)) == TRAP_IF
112 && XEXP (PATTERN (insn
), 0) == const1_rtx
)
114 if (!cfun
->can_throw_non_call_exceptions
)
119 /* It is nonsense to reach barrier when looking for the
120 end of basic block, but before dead code is eliminated
128 return can_throw_internal (insn
);
132 /* Create an edge between two basic blocks. FLAGS are auxiliary information
133 about the edge that is accumulated between calls. */
135 /* Create an edge from a basic block to a label. */
138 make_label_edge (sbitmap edge_cache
, basic_block src
, rtx label
, int flags
)
140 gcc_assert (LABEL_P (label
));
142 /* If the label was never emitted, this insn is junk, but avoid a
143 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
144 as a result of a syntax error and a diagnostic has already been
147 if (INSN_UID (label
) == 0)
150 cached_make_edge (edge_cache
, src
, BLOCK_FOR_INSN (label
), flags
);
153 /* Create the edges generated by INSN in REGION. */
156 rtl_make_eh_edge (sbitmap edge_cache
, basic_block src
, rtx insn
)
158 eh_landing_pad lp
= get_eh_landing_pad_from_rtx (insn
);
162 rtx label
= lp
->landing_pad
;
164 /* During initial rtl generation, use the post_landing_pad. */
167 gcc_assert (lp
->post_landing_pad
);
168 label
= label_rtx (lp
->post_landing_pad
);
171 make_label_edge (edge_cache
, src
, label
,
172 EDGE_ABNORMAL
| EDGE_EH
173 | (CALL_P (insn
) ? EDGE_ABNORMAL_CALL
: 0));
177 /* States of basic block as seen by find_many_sub_basic_blocks. */
179 /* Basic blocks created via split_block belong to this state.
180 make_edges will examine these basic blocks to see if we need to
181 create edges going out of them. */
184 /* Basic blocks that do not need examining belong to this state.
185 These blocks will be left intact. In particular, make_edges will
186 not create edges going out of these basic blocks. */
189 /* Basic blocks that may need splitting (due to a label appearing in
190 the middle, etc) belong to this state. After splitting them,
191 make_edges will create edges going out of them as needed. */
195 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
196 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
198 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
199 #define BLOCK_USED_BY_TABLEJUMP 32
200 #define FULL_STATE(BB) ((size_t) (BB)->aux)
202 /* Identify the edges going out of basic blocks between MIN and MAX,
203 inclusive, that have their states set to BLOCK_NEW or
206 UPDATE_P should be nonzero if we are updating CFG and zero if we
207 are building CFG from scratch. */
210 make_edges (basic_block min
, basic_block max
, int update_p
)
213 sbitmap edge_cache
= NULL
;
215 /* Heavy use of computed goto in machine-generated code can lead to
216 nearly fully-connected CFGs. In that case we spend a significant
217 amount of time searching the edge lists for duplicates. */
218 if (forced_labels
|| cfun
->cfg
->max_jumptable_ents
> 100)
219 edge_cache
= sbitmap_alloc (last_basic_block
);
221 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
222 is always the entry. */
223 if (min
== ENTRY_BLOCK_PTR
->next_bb
)
224 make_edge (ENTRY_BLOCK_PTR
, min
, EDGE_FALLTHRU
);
226 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
233 if (STATE (bb
) == BLOCK_ORIGINAL
)
236 /* If we have an edge cache, cache edges going out of BB. */
239 sbitmap_zero (edge_cache
);
242 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
243 if (e
->dest
!= EXIT_BLOCK_PTR
)
244 SET_BIT (edge_cache
, e
->dest
->index
);
248 if (LABEL_P (BB_HEAD (bb
))
249 && LABEL_ALT_ENTRY_P (BB_HEAD (bb
)))
250 cached_make_edge (NULL
, ENTRY_BLOCK_PTR
, bb
, 0);
252 /* Examine the last instruction of the block, and discover the
253 ways we can leave the block. */
256 code
= GET_CODE (insn
);
259 if (code
== JUMP_INSN
)
263 /* Recognize a non-local goto as a branch outside the
265 if (find_reg_note (insn
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
268 /* Recognize a tablejump and do the right thing. */
269 else if (tablejump_p (insn
, NULL
, &tmp
))
274 if (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
)
275 vec
= XVEC (PATTERN (tmp
), 0);
277 vec
= XVEC (PATTERN (tmp
), 1);
279 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
280 make_label_edge (edge_cache
, bb
,
281 XEXP (RTVEC_ELT (vec
, j
), 0), 0);
283 /* Some targets (eg, ARM) emit a conditional jump that also
284 contains the out-of-range target. Scan for these and
285 add an edge if necessary. */
286 if ((tmp
= single_set (insn
)) != NULL
287 && SET_DEST (tmp
) == pc_rtx
288 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
289 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
290 make_label_edge (edge_cache
, bb
,
291 XEXP (XEXP (SET_SRC (tmp
), 2), 0), 0);
294 /* If this is a computed jump, then mark it as reaching
295 everything on the forced_labels list. */
296 else if (computed_jump_p (insn
))
298 for (x
= forced_labels
; x
; x
= XEXP (x
, 1))
299 make_label_edge (edge_cache
, bb
, XEXP (x
, 0), EDGE_ABNORMAL
);
302 /* Returns create an exit out. */
303 else if (returnjump_p (insn
))
304 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, 0);
306 /* Recognize asm goto and do the right thing. */
307 else if ((tmp
= extract_asm_operands (PATTERN (insn
))) != NULL
)
309 int i
, n
= ASM_OPERANDS_LABEL_LENGTH (tmp
);
310 for (i
= 0; i
< n
; ++i
)
311 make_label_edge (edge_cache
, bb
,
312 XEXP (ASM_OPERANDS_LABEL (tmp
, i
), 0), 0);
315 /* Otherwise, we have a plain conditional or unconditional jump. */
318 gcc_assert (JUMP_LABEL (insn
));
319 make_label_edge (edge_cache
, bb
, JUMP_LABEL (insn
), 0);
323 /* If this is a sibling call insn, then this is in effect a combined call
324 and return, and so we need an edge to the exit block. No need to
325 worry about EH edges, since we wouldn't have created the sibling call
326 in the first place. */
327 if (code
== CALL_INSN
&& SIBLING_CALL_P (insn
))
328 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
,
329 EDGE_SIBCALL
| EDGE_ABNORMAL
);
331 /* If this is a CALL_INSN, then mark it as reaching the active EH
332 handler for this CALL_INSN. If we're handling non-call
333 exceptions then any insn can reach any of the active handlers.
334 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
335 else if (code
== CALL_INSN
|| cfun
->can_throw_non_call_exceptions
)
337 /* Add any appropriate EH edges. */
338 rtl_make_eh_edge (edge_cache
, bb
, insn
);
340 if (code
== CALL_INSN
)
342 if (can_nonlocal_goto (insn
))
344 /* ??? This could be made smarter: in some cases it's
345 possible to tell that certain calls will not do a
346 nonlocal goto. For example, if the nested functions
347 that do the nonlocal gotos do not have their addresses
348 taken, then only calls to those functions or to other
349 nested functions that use them could possibly do
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
);
359 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
360 if (REG_NOTE_KIND (note
) == REG_TM
)
361 make_label_edge (edge_cache
, bb
, XEXP (note
, 0),
362 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
367 /* Find out if we can drop through to the next block. */
368 insn
= NEXT_INSN (insn
);
369 e
= find_edge (bb
, EXIT_BLOCK_PTR
);
370 if (e
&& e
->flags
& EDGE_FALLTHRU
)
375 && NOTE_KIND (insn
) != NOTE_INSN_BASIC_BLOCK
)
376 insn
= NEXT_INSN (insn
);
379 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR
, EDGE_FALLTHRU
);
380 else if (bb
->next_bb
!= EXIT_BLOCK_PTR
)
382 if (insn
== BB_HEAD (bb
->next_bb
))
383 cached_make_edge (edge_cache
, bb
, bb
->next_bb
, EDGE_FALLTHRU
);
388 sbitmap_vector_free (edge_cache
);
392 mark_tablejump_edge (rtx label
)
396 gcc_assert (LABEL_P (label
));
397 /* See comment in make_label_edge. */
398 if (INSN_UID (label
) == 0)
400 bb
= BLOCK_FOR_INSN (label
);
401 SET_STATE (bb
, FULL_STATE (bb
) | BLOCK_USED_BY_TABLEJUMP
);
405 purge_dead_tablejump_edges (basic_block bb
, rtx table
)
407 rtx insn
= BB_END (bb
), tmp
;
413 if (GET_CODE (PATTERN (table
)) == ADDR_VEC
)
414 vec
= XVEC (PATTERN (table
), 0);
416 vec
= XVEC (PATTERN (table
), 1);
418 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
419 mark_tablejump_edge (XEXP (RTVEC_ELT (vec
, j
), 0));
421 /* Some targets (eg, ARM) emit a conditional jump that also
422 contains the out-of-range target. Scan for these and
423 add an edge if necessary. */
424 if ((tmp
= single_set (insn
)) != NULL
425 && SET_DEST (tmp
) == pc_rtx
426 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
427 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
428 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp
), 2), 0));
430 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
432 if (FULL_STATE (e
->dest
) & BLOCK_USED_BY_TABLEJUMP
)
433 SET_STATE (e
->dest
, FULL_STATE (e
->dest
)
434 & ~(size_t) BLOCK_USED_BY_TABLEJUMP
);
435 else if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
)))
444 /* Scan basic block BB for possible BB boundaries inside the block
445 and create new basic blocks in the progress. */
448 find_bb_boundaries (basic_block bb
)
450 basic_block orig_bb
= bb
;
451 rtx insn
= BB_HEAD (bb
);
452 rtx end
= BB_END (bb
), x
;
454 rtx flow_transfer_insn
= NULL_RTX
;
455 edge fallthru
= NULL
;
457 if (insn
== BB_END (bb
))
461 insn
= NEXT_INSN (insn
);
463 /* Scan insn chain and try to find new basic block boundaries. */
466 enum rtx_code code
= GET_CODE (insn
);
468 /* In case we've previously seen an insn that effects a control
469 flow transfer, split the block. */
470 if ((flow_transfer_insn
|| code
== CODE_LABEL
)
471 && inside_basic_block_p (insn
))
473 fallthru
= split_block (bb
, PREV_INSN (insn
));
474 if (flow_transfer_insn
)
476 BB_END (bb
) = flow_transfer_insn
;
478 /* Clean up the bb field for the insns between the blocks. */
479 for (x
= NEXT_INSN (flow_transfer_insn
);
480 x
!= BB_HEAD (fallthru
->dest
);
483 set_block_for_insn (x
, NULL
);
487 remove_edge (fallthru
);
488 flow_transfer_insn
= NULL_RTX
;
489 if (code
== CODE_LABEL
&& LABEL_ALT_ENTRY_P (insn
))
490 make_edge (ENTRY_BLOCK_PTR
, bb
, 0);
492 else if (code
== BARRIER
)
494 /* __builtin_unreachable () may cause a barrier to be emitted in
495 the middle of a BB. We need to split it in the same manner as
496 if the barrier were preceded by a control_flow_insn_p insn. */
497 if (!flow_transfer_insn
)
498 flow_transfer_insn
= prev_nonnote_insn_bb (insn
);
501 if (control_flow_insn_p (insn
))
502 flow_transfer_insn
= insn
;
505 insn
= NEXT_INSN (insn
);
508 /* In case expander replaced normal insn by sequence terminating by
509 return and barrier, or possibly other sequence not behaving like
510 ordinary jump, we need to take care and move basic block boundary. */
511 if (flow_transfer_insn
)
513 BB_END (bb
) = flow_transfer_insn
;
515 /* Clean up the bb field for the insns that do not belong to BB. */
516 x
= flow_transfer_insn
;
521 set_block_for_insn (x
, NULL
);
525 /* We've possibly replaced the conditional jump by conditional jump
526 followed by cleanup at fallthru edge, so the outgoing edges may
528 purge_dead_edges (bb
);
530 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
531 basic block, we might need to kill some edges. */
532 if (bb
!= orig_bb
&& tablejump_p (BB_END (bb
), NULL
, &table
))
533 purge_dead_tablejump_edges (bb
, table
);
536 /* Assume that frequency of basic block B is known. Compute frequencies
537 and probabilities of outgoing edges. */
540 compute_outgoing_frequencies (basic_block b
)
545 if (EDGE_COUNT (b
->succs
) == 2)
547 rtx note
= find_reg_note (BB_END (b
), REG_BR_PROB
, NULL
);
552 probability
= INTVAL (XEXP (note
, 0));
554 e
->probability
= probability
;
555 e
->count
= ((b
->count
* probability
+ REG_BR_PROB_BASE
/ 2)
557 f
= FALLTHRU_EDGE (b
);
558 f
->probability
= REG_BR_PROB_BASE
- probability
;
559 f
->count
= b
->count
- e
->count
;
564 if (single_succ_p (b
))
566 e
= single_succ_edge (b
);
567 e
->probability
= REG_BR_PROB_BASE
;
571 guess_outgoing_edge_probabilities (b
);
573 FOR_EACH_EDGE (e
, ei
, b
->succs
)
574 e
->count
= ((b
->count
* e
->probability
+ REG_BR_PROB_BASE
/ 2)
578 /* Assume that some pass has inserted labels or control flow
579 instructions within a basic block. Split basic blocks as needed
583 find_many_sub_basic_blocks (sbitmap blocks
)
585 basic_block bb
, min
, max
;
589 TEST_BIT (blocks
, bb
->index
) ? BLOCK_TO_SPLIT
: BLOCK_ORIGINAL
);
592 if (STATE (bb
) == BLOCK_TO_SPLIT
)
593 find_bb_boundaries (bb
);
596 if (STATE (bb
) != BLOCK_ORIGINAL
)
600 for (; bb
!= EXIT_BLOCK_PTR
; bb
= bb
->next_bb
)
601 if (STATE (bb
) != BLOCK_ORIGINAL
)
604 /* Now re-scan and wire in all edges. This expect simple (conditional)
605 jumps at the end of each new basic blocks. */
606 make_edges (min
, max
, 1);
608 /* Update branch probabilities. Expect only (un)conditional jumps
609 to be created with only the forward edges. */
610 if (profile_status
!= PROFILE_ABSENT
)
611 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
616 if (STATE (bb
) == BLOCK_ORIGINAL
)
618 if (STATE (bb
) == BLOCK_NEW
)
622 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
624 bb
->count
+= e
->count
;
625 bb
->frequency
+= EDGE_FREQUENCY (e
);
629 compute_outgoing_frequencies (bb
);