1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
34 #include "diagnostic-core.h"
38 static void make_edges (basic_block
, basic_block
, int);
39 static void make_label_edge (sbitmap
, basic_block
, rtx
, int);
40 static void find_bb_boundaries (basic_block
);
41 static void compute_outgoing_frequencies (basic_block
);
43 /* Return true if insn is something that should be contained inside basic
47 inside_basic_block_p (const_rtx insn
)
49 switch (GET_CODE (insn
))
52 /* Avoid creating of basic block for jumptables. */
53 return (NEXT_INSN (insn
) == 0
54 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn
)));
72 /* Return true if INSN may cause control flow transfer, so it should be last in
76 control_flow_insn_p (const_rtx insn
)
78 switch (GET_CODE (insn
))
89 /* Noreturn and sibling call instructions terminate the basic blocks
90 (but only if they happen unconditionally). */
91 if ((SIBLING_CALL_P (insn
)
92 || find_reg_note (insn
, REG_NORETURN
, 0))
93 && GET_CODE (PATTERN (insn
)) != COND_EXEC
)
96 /* Call insn may return to the nonlocal goto handler. */
97 if (can_nonlocal_goto (insn
))
102 /* Treat trap instructions like noreturn calls (same provision). */
103 if (GET_CODE (PATTERN (insn
)) == TRAP_IF
104 && XEXP (PATTERN (insn
), 0) == const1_rtx
)
106 if (!cfun
->can_throw_non_call_exceptions
)
110 case JUMP_TABLE_DATA
:
112 /* It is nonsense to reach this when looking for the
113 end of basic block, but before dead code is eliminated
121 return can_throw_internal (insn
);
125 /* Create an edge between two basic blocks. FLAGS are auxiliary information
126 about the edge that is accumulated between calls. */
128 /* Create an edge from a basic block to a label. */
131 make_label_edge (sbitmap edge_cache
, basic_block src
, rtx label
, int flags
)
133 gcc_assert (LABEL_P (label
));
135 /* If the label was never emitted, this insn is junk, but avoid a
136 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
137 as a result of a syntax error and a diagnostic has already been
140 if (INSN_UID (label
) == 0)
143 cached_make_edge (edge_cache
, src
, BLOCK_FOR_INSN (label
), flags
);
146 /* Create the edges generated by INSN in REGION. */
149 rtl_make_eh_edge (sbitmap edge_cache
, basic_block src
, rtx insn
)
151 eh_landing_pad lp
= get_eh_landing_pad_from_rtx (insn
);
155 rtx label
= lp
->landing_pad
;
157 /* During initial rtl generation, use the post_landing_pad. */
160 gcc_assert (lp
->post_landing_pad
);
161 label
= label_rtx (lp
->post_landing_pad
);
164 make_label_edge (edge_cache
, src
, label
,
165 EDGE_ABNORMAL
| EDGE_EH
166 | (CALL_P (insn
) ? EDGE_ABNORMAL_CALL
: 0));
170 /* States of basic block as seen by find_many_sub_basic_blocks. */
172 /* Basic blocks created via split_block belong to this state.
173 make_edges will examine these basic blocks to see if we need to
174 create edges going out of them. */
177 /* Basic blocks that do not need examining belong to this state.
178 These blocks will be left intact. In particular, make_edges will
179 not create edges going out of these basic blocks. */
182 /* Basic blocks that may need splitting (due to a label appearing in
183 the middle, etc) belong to this state. After splitting them,
184 make_edges will create edges going out of them as needed. */
188 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
189 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
191 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
192 #define BLOCK_USED_BY_TABLEJUMP 32
193 #define FULL_STATE(BB) ((size_t) (BB)->aux)
195 /* Identify the edges going out of basic blocks between MIN and MAX,
196 inclusive, that have their states set to BLOCK_NEW or
199 UPDATE_P should be nonzero if we are updating CFG and zero if we
200 are building CFG from scratch. */
203 make_edges (basic_block min
, basic_block max
, int update_p
)
206 sbitmap edge_cache
= NULL
;
208 /* Heavy use of computed goto in machine-generated code can lead to
209 nearly fully-connected CFGs. In that case we spend a significant
210 amount of time searching the edge lists for duplicates. */
211 if (forced_labels
|| cfun
->cfg
->max_jumptable_ents
> 100)
212 edge_cache
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
214 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
215 is always the entry. */
216 if (min
== ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
)
217 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), min
, EDGE_FALLTHRU
);
219 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
226 if (STATE (bb
) == BLOCK_ORIGINAL
)
229 /* If we have an edge cache, cache edges going out of BB. */
232 bitmap_clear (edge_cache
);
235 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
236 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
237 bitmap_set_bit (edge_cache
, e
->dest
->index
);
241 if (LABEL_P (BB_HEAD (bb
))
242 && LABEL_ALT_ENTRY_P (BB_HEAD (bb
)))
243 cached_make_edge (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, 0);
245 /* Examine the last instruction of the block, and discover the
246 ways we can leave the block. */
249 code
= GET_CODE (insn
);
252 if (code
== JUMP_INSN
)
256 /* Recognize a non-local goto as a branch outside the
258 if (find_reg_note (insn
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
261 /* Recognize a tablejump and do the right thing. */
262 else if (tablejump_p (insn
, NULL
, &tmp
))
267 if (GET_CODE (PATTERN (tmp
)) == ADDR_VEC
)
268 vec
= XVEC (PATTERN (tmp
), 0);
270 vec
= XVEC (PATTERN (tmp
), 1);
272 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
273 make_label_edge (edge_cache
, bb
,
274 XEXP (RTVEC_ELT (vec
, j
), 0), 0);
276 /* Some targets (eg, ARM) emit a conditional jump that also
277 contains the out-of-range target. Scan for these and
278 add an edge if necessary. */
279 if ((tmp
= single_set (insn
)) != NULL
280 && SET_DEST (tmp
) == pc_rtx
281 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
282 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
283 make_label_edge (edge_cache
, bb
,
284 XEXP (XEXP (SET_SRC (tmp
), 2), 0), 0);
287 /* If this is a computed jump, then mark it as reaching
288 everything on the forced_labels list. */
289 else if (computed_jump_p (insn
))
291 for (x
= forced_labels
; x
; x
= XEXP (x
, 1))
292 make_label_edge (edge_cache
, bb
, XEXP (x
, 0), EDGE_ABNORMAL
);
295 /* Returns create an exit out. */
296 else if (returnjump_p (insn
))
297 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
299 /* Recognize asm goto and do the right thing. */
300 else if ((tmp
= extract_asm_operands (PATTERN (insn
))) != NULL
)
302 int i
, n
= ASM_OPERANDS_LABEL_LENGTH (tmp
);
303 for (i
= 0; i
< n
; ++i
)
304 make_label_edge (edge_cache
, bb
,
305 XEXP (ASM_OPERANDS_LABEL (tmp
, i
), 0), 0);
308 /* Otherwise, we have a plain conditional or unconditional jump. */
311 gcc_assert (JUMP_LABEL (insn
));
312 make_label_edge (edge_cache
, bb
, JUMP_LABEL (insn
), 0);
316 /* If this is a sibling call insn, then this is in effect a combined call
317 and return, and so we need an edge to the exit block. No need to
318 worry about EH edges, since we wouldn't have created the sibling call
319 in the first place. */
320 if (code
== CALL_INSN
&& SIBLING_CALL_P (insn
))
321 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
322 EDGE_SIBCALL
| EDGE_ABNORMAL
);
324 /* If this is a CALL_INSN, then mark it as reaching the active EH
325 handler for this CALL_INSN. If we're handling non-call
326 exceptions then any insn can reach any of the active handlers.
327 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
328 else if (code
== CALL_INSN
|| cfun
->can_throw_non_call_exceptions
)
330 /* Add any appropriate EH edges. */
331 rtl_make_eh_edge (edge_cache
, bb
, insn
);
333 if (code
== CALL_INSN
)
335 if (can_nonlocal_goto (insn
))
337 /* ??? This could be made smarter: in some cases it's
338 possible to tell that certain calls will not do a
339 nonlocal goto. For example, if the nested functions
340 that do the nonlocal gotos do not have their addresses
341 taken, then only calls to those functions or to other
342 nested functions that use them could possibly do
344 for (x
= nonlocal_goto_handler_labels
; x
; x
= XEXP (x
, 1))
345 make_label_edge (edge_cache
, bb
, XEXP (x
, 0),
346 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
352 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
353 if (REG_NOTE_KIND (note
) == REG_TM
)
354 make_label_edge (edge_cache
, bb
, XEXP (note
, 0),
355 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
360 /* Find out if we can drop through to the next block. */
361 insn
= NEXT_INSN (insn
);
362 e
= find_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
));
363 if (e
&& e
->flags
& EDGE_FALLTHRU
)
368 && NOTE_KIND (insn
) != NOTE_INSN_BASIC_BLOCK
)
369 insn
= NEXT_INSN (insn
);
372 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
374 else if (bb
->next_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
376 if (insn
== BB_HEAD (bb
->next_bb
))
377 cached_make_edge (edge_cache
, bb
, bb
->next_bb
, EDGE_FALLTHRU
);
382 sbitmap_free (edge_cache
);
386 mark_tablejump_edge (rtx label
)
390 gcc_assert (LABEL_P (label
));
391 /* See comment in make_label_edge. */
392 if (INSN_UID (label
) == 0)
394 bb
= BLOCK_FOR_INSN (label
);
395 SET_STATE (bb
, FULL_STATE (bb
) | BLOCK_USED_BY_TABLEJUMP
);
399 purge_dead_tablejump_edges (basic_block bb
, rtx table
)
401 rtx insn
= BB_END (bb
), tmp
;
407 if (GET_CODE (PATTERN (table
)) == ADDR_VEC
)
408 vec
= XVEC (PATTERN (table
), 0);
410 vec
= XVEC (PATTERN (table
), 1);
412 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
413 mark_tablejump_edge (XEXP (RTVEC_ELT (vec
, j
), 0));
415 /* Some targets (eg, ARM) emit a conditional jump that also
416 contains the out-of-range target. Scan for these and
417 add an edge if necessary. */
418 if ((tmp
= single_set (insn
)) != NULL
419 && SET_DEST (tmp
) == pc_rtx
420 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
421 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
422 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp
), 2), 0));
424 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
426 if (FULL_STATE (e
->dest
) & BLOCK_USED_BY_TABLEJUMP
)
427 SET_STATE (e
->dest
, FULL_STATE (e
->dest
)
428 & ~(size_t) BLOCK_USED_BY_TABLEJUMP
);
429 else if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
)))
438 /* Scan basic block BB for possible BB boundaries inside the block
439 and create new basic blocks in the progress. */
442 find_bb_boundaries (basic_block bb
)
444 basic_block orig_bb
= bb
;
445 rtx insn
= BB_HEAD (bb
);
446 rtx end
= BB_END (bb
), x
;
448 rtx flow_transfer_insn
= NULL_RTX
;
449 edge fallthru
= NULL
;
451 if (insn
== BB_END (bb
))
455 insn
= NEXT_INSN (insn
);
457 /* Scan insn chain and try to find new basic block boundaries. */
460 enum rtx_code code
= GET_CODE (insn
);
462 /* In case we've previously seen an insn that effects a control
463 flow transfer, split the block. */
464 if ((flow_transfer_insn
|| code
== CODE_LABEL
)
465 && inside_basic_block_p (insn
))
467 fallthru
= split_block (bb
, PREV_INSN (insn
));
468 if (flow_transfer_insn
)
470 BB_END (bb
) = flow_transfer_insn
;
472 /* Clean up the bb field for the insns between the blocks. */
473 for (x
= NEXT_INSN (flow_transfer_insn
);
474 x
!= BB_HEAD (fallthru
->dest
);
477 set_block_for_insn (x
, NULL
);
481 remove_edge (fallthru
);
482 flow_transfer_insn
= NULL_RTX
;
483 if (code
== CODE_LABEL
&& LABEL_ALT_ENTRY_P (insn
))
484 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, 0);
486 else if (code
== BARRIER
)
488 /* __builtin_unreachable () may cause a barrier to be emitted in
489 the middle of a BB. We need to split it in the same manner as
490 if the barrier were preceded by a control_flow_insn_p insn. */
491 if (!flow_transfer_insn
)
492 flow_transfer_insn
= prev_nonnote_insn_bb (insn
);
495 if (control_flow_insn_p (insn
))
496 flow_transfer_insn
= insn
;
499 insn
= NEXT_INSN (insn
);
502 /* In case expander replaced normal insn by sequence terminating by
503 return and barrier, or possibly other sequence not behaving like
504 ordinary jump, we need to take care and move basic block boundary. */
505 if (flow_transfer_insn
)
507 BB_END (bb
) = flow_transfer_insn
;
509 /* Clean up the bb field for the insns that do not belong to BB. */
510 x
= flow_transfer_insn
;
515 set_block_for_insn (x
, NULL
);
519 /* We've possibly replaced the conditional jump by conditional jump
520 followed by cleanup at fallthru edge, so the outgoing edges may
522 purge_dead_edges (bb
);
524 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
525 basic block, we might need to kill some edges. */
526 if (bb
!= orig_bb
&& tablejump_p (BB_END (bb
), NULL
, &table
))
527 purge_dead_tablejump_edges (bb
, table
);
530 /* Assume that frequency of basic block B is known. Compute frequencies
531 and probabilities of outgoing edges. */
534 compute_outgoing_frequencies (basic_block b
)
539 if (EDGE_COUNT (b
->succs
) == 2)
541 rtx note
= find_reg_note (BB_END (b
), REG_BR_PROB
, NULL
);
546 probability
= XINT (note
, 0);
548 e
->probability
= probability
;
549 e
->count
= apply_probability (b
->count
, probability
);
550 f
= FALLTHRU_EDGE (b
);
551 f
->probability
= REG_BR_PROB_BASE
- probability
;
552 f
->count
= b
->count
- e
->count
;
557 guess_outgoing_edge_probabilities (b
);
560 else if (single_succ_p (b
))
562 e
= single_succ_edge (b
);
563 e
->probability
= REG_BR_PROB_BASE
;
569 /* We rely on BBs with more than two successors to have sane probabilities
570 and do not guess them here. For BBs terminated by switch statements
571 expanded to jump-table jump, we have done the right thing during
572 expansion. For EH edges, we still guess the probabilities here. */
573 bool complex_edge
= false;
574 FOR_EACH_EDGE (e
, ei
, b
->succs
)
575 if (e
->flags
& EDGE_COMPLEX
)
581 guess_outgoing_edge_probabilities (b
);
585 FOR_EACH_EDGE (e
, ei
, b
->succs
)
586 e
->count
= apply_probability (b
->count
, e
->probability
);
589 /* Assume that some pass has inserted labels or control flow
590 instructions within a basic block. Split basic blocks as needed
594 find_many_sub_basic_blocks (sbitmap blocks
)
596 basic_block bb
, min
, max
;
598 FOR_EACH_BB_FN (bb
, cfun
)
600 bitmap_bit_p (blocks
, bb
->index
) ? BLOCK_TO_SPLIT
: BLOCK_ORIGINAL
);
602 FOR_EACH_BB_FN (bb
, cfun
)
603 if (STATE (bb
) == BLOCK_TO_SPLIT
)
604 find_bb_boundaries (bb
);
606 FOR_EACH_BB_FN (bb
, cfun
)
607 if (STATE (bb
) != BLOCK_ORIGINAL
)
611 for (; bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
); bb
= bb
->next_bb
)
612 if (STATE (bb
) != BLOCK_ORIGINAL
)
615 /* Now re-scan and wire in all edges. This expect simple (conditional)
616 jumps at the end of each new basic blocks. */
617 make_edges (min
, max
, 1);
619 /* Update branch probabilities. Expect only (un)conditional jumps
620 to be created with only the forward edges. */
621 if (profile_status_for_fn (cfun
) != PROFILE_ABSENT
)
622 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
627 if (STATE (bb
) == BLOCK_ORIGINAL
)
629 if (STATE (bb
) == BLOCK_NEW
)
633 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
635 bb
->count
+= e
->count
;
636 bb
->frequency
+= EDGE_FREQUENCY (e
);
640 compute_outgoing_frequencies (bb
);
643 FOR_EACH_BB_FN (bb
, cfun
)