1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987-2016 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"
34 static void make_edges (basic_block
, basic_block
, int);
35 static void make_label_edge (sbitmap
, basic_block
, rtx
, int);
36 static void find_bb_boundaries (basic_block
);
37 static void compute_outgoing_frequencies (basic_block
);
39 /* Return true if insn is something that should be contained inside basic
43 inside_basic_block_p (const rtx_insn
*insn
)
45 switch (GET_CODE (insn
))
48 /* Avoid creating of basic block for jumptables. */
49 return (NEXT_INSN (insn
) == 0
50 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn
)));
68 /* Return true if INSN may cause control flow transfer, so it should be last in
72 control_flow_insn_p (const rtx_insn
*insn
)
74 switch (GET_CODE (insn
))
85 /* Noreturn and sibling call instructions terminate the basic blocks
86 (but only if they happen unconditionally). */
87 if ((SIBLING_CALL_P (insn
)
88 || find_reg_note (insn
, REG_NORETURN
, 0))
89 && GET_CODE (PATTERN (insn
)) != COND_EXEC
)
92 /* Call insn may return to the nonlocal goto handler. */
93 if (can_nonlocal_goto (insn
))
98 /* Treat trap instructions like noreturn calls (same provision). */
99 if (GET_CODE (PATTERN (insn
)) == TRAP_IF
100 && XEXP (PATTERN (insn
), 0) == const1_rtx
)
102 if (!cfun
->can_throw_non_call_exceptions
)
106 case JUMP_TABLE_DATA
:
108 /* It is nonsense to reach this when looking for the
109 end of basic block, but before dead code is eliminated
117 return can_throw_internal (insn
);
121 /* Create an edge between two basic blocks. FLAGS are auxiliary information
122 about the edge that is accumulated between calls. */
124 /* Create an edge from a basic block to a label. */
127 make_label_edge (sbitmap edge_cache
, basic_block src
, rtx label
, int flags
)
129 gcc_assert (LABEL_P (label
));
131 /* If the label was never emitted, this insn is junk, but avoid a
132 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
133 as a result of a syntax error and a diagnostic has already been
136 if (INSN_UID (label
) == 0)
139 cached_make_edge (edge_cache
, src
, BLOCK_FOR_INSN (label
), flags
);
142 /* Create the edges generated by INSN in REGION. */
145 rtl_make_eh_edge (sbitmap edge_cache
, basic_block src
, rtx insn
)
147 eh_landing_pad lp
= get_eh_landing_pad_from_rtx (insn
);
151 rtx_insn
*label
= lp
->landing_pad
;
153 /* During initial rtl generation, use the post_landing_pad. */
156 gcc_assert (lp
->post_landing_pad
);
157 label
= label_rtx (lp
->post_landing_pad
);
160 make_label_edge (edge_cache
, src
, label
,
161 EDGE_ABNORMAL
| EDGE_EH
162 | (CALL_P (insn
) ? EDGE_ABNORMAL_CALL
: 0));
166 /* States of basic block as seen by find_many_sub_basic_blocks. */
168 /* Basic blocks created via split_block belong to this state.
169 make_edges will examine these basic blocks to see if we need to
170 create edges going out of them. */
173 /* Basic blocks that do not need examining belong to this state.
174 These blocks will be left intact. In particular, make_edges will
175 not create edges going out of these basic blocks. */
178 /* Basic blocks that may need splitting (due to a label appearing in
179 the middle, etc) belong to this state. After splitting them,
180 make_edges will create edges going out of them as needed. */
184 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
185 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
187 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
188 #define BLOCK_USED_BY_TABLEJUMP 32
189 #define FULL_STATE(BB) ((size_t) (BB)->aux)
191 /* Identify the edges going out of basic blocks between MIN and MAX,
192 inclusive, that have their states set to BLOCK_NEW or
195 UPDATE_P should be nonzero if we are updating CFG and zero if we
196 are building CFG from scratch. */
199 make_edges (basic_block min
, basic_block max
, int update_p
)
202 sbitmap edge_cache
= NULL
;
204 /* Heavy use of computed goto in machine-generated code can lead to
205 nearly fully-connected CFGs. In that case we spend a significant
206 amount of time searching the edge lists for duplicates. */
207 if (!vec_safe_is_empty (forced_labels
)
208 || cfun
->cfg
->max_jumptable_ents
> 100)
209 edge_cache
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
211 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
212 is always the entry. */
213 if (min
== ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
)
214 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), min
, EDGE_FALLTHRU
);
216 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
223 if (STATE (bb
) == BLOCK_ORIGINAL
)
226 /* If we have an edge cache, cache edges going out of BB. */
229 bitmap_clear (edge_cache
);
232 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
233 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
234 bitmap_set_bit (edge_cache
, e
->dest
->index
);
238 if (LABEL_P (BB_HEAD (bb
))
239 && LABEL_ALT_ENTRY_P (BB_HEAD (bb
)))
240 cached_make_edge (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, 0);
242 /* Examine the last instruction of the block, and discover the
243 ways we can leave the block. */
246 code
= GET_CODE (insn
);
249 if (code
== JUMP_INSN
)
252 rtx_jump_table_data
*table
;
254 /* Recognize a non-local goto as a branch outside the
256 if (find_reg_note (insn
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
259 /* Recognize a tablejump and do the right thing. */
260 else if (tablejump_p (insn
, NULL
, &table
))
262 rtvec vec
= table
->get_labels ();
265 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
266 make_label_edge (edge_cache
, bb
,
267 XEXP (RTVEC_ELT (vec
, j
), 0), 0);
269 /* Some targets (eg, ARM) emit a conditional jump that also
270 contains the out-of-range target. Scan for these and
271 add an edge if necessary. */
272 if ((tmp
= single_set (insn
)) != NULL
273 && SET_DEST (tmp
) == pc_rtx
274 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
275 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
276 make_label_edge (edge_cache
, bb
,
277 LABEL_REF_LABEL (XEXP (SET_SRC (tmp
), 2)), 0);
280 /* If this is a computed jump, then mark it as reaching
281 everything on the forced_labels list. */
282 else if (computed_jump_p (insn
))
286 FOR_EACH_VEC_SAFE_ELT (forced_labels
, i
, insn
)
287 make_label_edge (edge_cache
, bb
, insn
, EDGE_ABNORMAL
);
290 /* Returns create an exit out. */
291 else if (returnjump_p (insn
))
292 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
294 /* Recognize asm goto and do the right thing. */
295 else if ((tmp
= extract_asm_operands (PATTERN (insn
))) != NULL
)
297 int i
, n
= ASM_OPERANDS_LABEL_LENGTH (tmp
);
298 for (i
= 0; i
< n
; ++i
)
299 make_label_edge (edge_cache
, bb
,
300 XEXP (ASM_OPERANDS_LABEL (tmp
, i
), 0), 0);
303 /* Otherwise, we have a plain conditional or unconditional jump. */
306 gcc_assert (JUMP_LABEL (insn
));
307 make_label_edge (edge_cache
, bb
, JUMP_LABEL (insn
), 0);
311 /* If this is a sibling call insn, then this is in effect a combined call
312 and return, and so we need an edge to the exit block. No need to
313 worry about EH edges, since we wouldn't have created the sibling call
314 in the first place. */
315 if (code
== CALL_INSN
&& SIBLING_CALL_P (insn
))
316 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
317 EDGE_SIBCALL
| EDGE_ABNORMAL
);
319 /* If this is a CALL_INSN, then mark it as reaching the active EH
320 handler for this CALL_INSN. If we're handling non-call
321 exceptions then any insn can reach any of the active handlers.
322 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
323 else if (code
== CALL_INSN
|| cfun
->can_throw_non_call_exceptions
)
325 /* Add any appropriate EH edges. */
326 rtl_make_eh_edge (edge_cache
, bb
, insn
);
328 if (code
== CALL_INSN
)
330 if (can_nonlocal_goto (insn
))
332 /* ??? This could be made smarter: in some cases it's
333 possible to tell that certain calls will not do a
334 nonlocal goto. For example, if the nested functions
335 that do the nonlocal gotos do not have their addresses
336 taken, then only calls to those functions or to other
337 nested functions that use them could possibly do
339 for (rtx_insn_list
*x
= nonlocal_goto_handler_labels
;
342 make_label_edge (edge_cache
, bb
, x
->insn (),
343 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
349 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
350 if (REG_NOTE_KIND (note
) == REG_TM
)
351 make_label_edge (edge_cache
, bb
, XEXP (note
, 0),
352 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_FOR_FN (cfun
));
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_FOR_FN (cfun
),
371 else if (bb
->next_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
373 if (insn
== BB_HEAD (bb
->next_bb
))
374 cached_make_edge (edge_cache
, bb
, bb
->next_bb
, EDGE_FALLTHRU
);
379 sbitmap_free (edge_cache
);
383 mark_tablejump_edge (rtx label
)
387 gcc_assert (LABEL_P (label
));
388 /* See comment in make_label_edge. */
389 if (INSN_UID (label
) == 0)
391 bb
= BLOCK_FOR_INSN (label
);
392 SET_STATE (bb
, FULL_STATE (bb
) | BLOCK_USED_BY_TABLEJUMP
);
396 purge_dead_tablejump_edges (basic_block bb
, rtx_jump_table_data
*table
)
398 rtx_insn
*insn
= BB_END (bb
);
405 vec
= table
->get_labels ();
407 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
408 mark_tablejump_edge (XEXP (RTVEC_ELT (vec
, j
), 0));
410 /* Some targets (eg, ARM) emit a conditional jump that also
411 contains the out-of-range target. Scan for these and
412 add an edge if necessary. */
413 if ((tmp
= single_set (insn
)) != NULL
414 && SET_DEST (tmp
) == pc_rtx
415 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
416 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
417 mark_tablejump_edge (LABEL_REF_LABEL (XEXP (SET_SRC (tmp
), 2)));
419 for (ei
= ei_start (bb
->succs
); (e
= ei_safe_edge (ei
)); )
421 if (FULL_STATE (e
->dest
) & BLOCK_USED_BY_TABLEJUMP
)
422 SET_STATE (e
->dest
, FULL_STATE (e
->dest
)
423 & ~(size_t) BLOCK_USED_BY_TABLEJUMP
);
424 else if (!(e
->flags
& (EDGE_ABNORMAL
| EDGE_EH
)))
433 /* Scan basic block BB for possible BB boundaries inside the block
434 and create new basic blocks in the progress. */
437 find_bb_boundaries (basic_block bb
)
439 basic_block orig_bb
= bb
;
440 rtx_insn
*insn
= BB_HEAD (bb
);
441 rtx_insn
*end
= BB_END (bb
), *x
;
442 rtx_jump_table_data
*table
;
443 rtx_insn
*flow_transfer_insn
= NULL
;
444 edge fallthru
= NULL
;
446 if (insn
== BB_END (bb
))
450 insn
= NEXT_INSN (insn
);
452 /* Scan insn chain and try to find new basic block boundaries. */
455 enum rtx_code code
= GET_CODE (insn
);
457 /* In case we've previously seen an insn that effects a control
458 flow transfer, split the block. */
459 if ((flow_transfer_insn
|| code
== CODE_LABEL
)
460 && inside_basic_block_p (insn
))
462 fallthru
= split_block (bb
, PREV_INSN (insn
));
463 if (flow_transfer_insn
)
465 BB_END (bb
) = flow_transfer_insn
;
467 /* Clean up the bb field for the insns between the blocks. */
468 for (x
= NEXT_INSN (flow_transfer_insn
);
469 x
!= BB_HEAD (fallthru
->dest
);
472 set_block_for_insn (x
, NULL
);
476 remove_edge (fallthru
);
477 flow_transfer_insn
= NULL
;
478 if (code
== CODE_LABEL
&& LABEL_ALT_ENTRY_P (insn
))
479 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, 0);
481 else if (code
== BARRIER
)
483 /* __builtin_unreachable () may cause a barrier to be emitted in
484 the middle of a BB. We need to split it in the same manner as
485 if the barrier were preceded by a control_flow_insn_p insn. */
486 if (!flow_transfer_insn
)
487 flow_transfer_insn
= prev_nonnote_insn_bb (insn
);
490 if (control_flow_insn_p (insn
))
491 flow_transfer_insn
= insn
;
494 insn
= NEXT_INSN (insn
);
497 /* In case expander replaced normal insn by sequence terminating by
498 return and barrier, or possibly other sequence not behaving like
499 ordinary jump, we need to take care and move basic block boundary. */
500 if (flow_transfer_insn
)
502 BB_END (bb
) = flow_transfer_insn
;
504 /* Clean up the bb field for the insns that do not belong to BB. */
505 x
= flow_transfer_insn
;
510 set_block_for_insn (x
, NULL
);
514 /* We've possibly replaced the conditional jump by conditional jump
515 followed by cleanup at fallthru edge, so the outgoing edges may
517 purge_dead_edges (bb
);
519 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
520 basic block, we might need to kill some edges. */
521 if (bb
!= orig_bb
&& tablejump_p (BB_END (bb
), NULL
, &table
))
522 purge_dead_tablejump_edges (bb
, table
);
525 /* Assume that frequency of basic block B is known. Compute frequencies
526 and probabilities of outgoing edges. */
529 compute_outgoing_frequencies (basic_block b
)
534 if (EDGE_COUNT (b
->succs
) == 2)
536 rtx note
= find_reg_note (BB_END (b
), REG_BR_PROB
, NULL
);
541 probability
= XINT (note
, 0);
543 e
->probability
= probability
;
544 e
->count
= apply_probability (b
->count
, probability
);
545 f
= FALLTHRU_EDGE (b
);
546 f
->probability
= REG_BR_PROB_BASE
- probability
;
547 f
->count
= b
->count
- e
->count
;
552 guess_outgoing_edge_probabilities (b
);
555 else if (single_succ_p (b
))
557 e
= single_succ_edge (b
);
558 e
->probability
= REG_BR_PROB_BASE
;
564 /* We rely on BBs with more than two successors to have sane probabilities
565 and do not guess them here. For BBs terminated by switch statements
566 expanded to jump-table jump, we have done the right thing during
567 expansion. For EH edges, we still guess the probabilities here. */
568 bool complex_edge
= false;
569 FOR_EACH_EDGE (e
, ei
, b
->succs
)
570 if (e
->flags
& EDGE_COMPLEX
)
576 guess_outgoing_edge_probabilities (b
);
580 FOR_EACH_EDGE (e
, ei
, b
->succs
)
581 e
->count
= apply_probability (b
->count
, e
->probability
);
584 /* Assume that some pass has inserted labels or control flow
585 instructions within a basic block. Split basic blocks as needed
589 find_many_sub_basic_blocks (sbitmap blocks
)
591 basic_block bb
, min
, max
;
593 FOR_EACH_BB_FN (bb
, cfun
)
595 bitmap_bit_p (blocks
, bb
->index
) ? BLOCK_TO_SPLIT
: BLOCK_ORIGINAL
);
597 FOR_EACH_BB_FN (bb
, cfun
)
598 if (STATE (bb
) == BLOCK_TO_SPLIT
)
599 find_bb_boundaries (bb
);
601 FOR_EACH_BB_FN (bb
, cfun
)
602 if (STATE (bb
) != BLOCK_ORIGINAL
)
606 for (; bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
); bb
= bb
->next_bb
)
607 if (STATE (bb
) != BLOCK_ORIGINAL
)
610 /* Now re-scan and wire in all edges. This expect simple (conditional)
611 jumps at the end of each new basic blocks. */
612 make_edges (min
, max
, 1);
614 /* Update branch probabilities. Expect only (un)conditional jumps
615 to be created with only the forward edges. */
616 if (profile_status_for_fn (cfun
) != PROFILE_ABSENT
)
617 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
622 if (STATE (bb
) == BLOCK_ORIGINAL
)
624 if (STATE (bb
) == BLOCK_NEW
)
628 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
630 bb
->count
+= e
->count
;
631 bb
->frequency
+= EDGE_FREQUENCY (e
);
635 compute_outgoing_frequencies (bb
);
638 FOR_EACH_BB_FN (bb
, cfun
)