1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987-2017 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"
35 static void make_edges (basic_block
, basic_block
, int);
36 static void make_label_edge (sbitmap
, basic_block
, rtx
, int);
37 static void find_bb_boundaries (basic_block
);
38 static void compute_outgoing_frequencies (basic_block
);
40 /* Return true if insn is something that should be contained inside basic
44 inside_basic_block_p (const rtx_insn
*insn
)
46 switch (GET_CODE (insn
))
49 /* Avoid creating of basic block for jumptables. */
50 return (NEXT_INSN (insn
) == 0
51 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn
)));
69 /* Return true if INSN may cause control flow transfer, so it should be last in
73 control_flow_insn_p (const rtx_insn
*insn
)
75 switch (GET_CODE (insn
))
86 /* Noreturn and sibling call instructions terminate the basic blocks
87 (but only if they happen unconditionally). */
88 if ((SIBLING_CALL_P (insn
)
89 || find_reg_note (insn
, REG_NORETURN
, 0))
90 && GET_CODE (PATTERN (insn
)) != COND_EXEC
)
93 /* Call insn may return to the nonlocal goto handler. */
94 if (can_nonlocal_goto (insn
))
99 /* Treat trap instructions like noreturn calls (same provision). */
100 if (GET_CODE (PATTERN (insn
)) == TRAP_IF
101 && XEXP (PATTERN (insn
), 0) == const1_rtx
)
103 if (!cfun
->can_throw_non_call_exceptions
)
107 case JUMP_TABLE_DATA
:
109 /* It is nonsense to reach this when looking for the
110 end of basic block, but before dead code is eliminated
118 return can_throw_internal (insn
);
122 /* Create an edge between two basic blocks. FLAGS are auxiliary information
123 about the edge that is accumulated between calls. */
125 /* Create an edge from a basic block to a label. */
128 make_label_edge (sbitmap edge_cache
, basic_block src
, rtx label
, int flags
)
130 gcc_assert (LABEL_P (label
));
132 /* If the label was never emitted, this insn is junk, but avoid a
133 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
134 as a result of a syntax error and a diagnostic has already been
137 if (INSN_UID (label
) == 0)
140 cached_make_edge (edge_cache
, src
, BLOCK_FOR_INSN (label
), flags
);
143 /* Create the edges generated by INSN in REGION. */
146 rtl_make_eh_edge (sbitmap edge_cache
, basic_block src
, rtx insn
)
148 eh_landing_pad lp
= get_eh_landing_pad_from_rtx (insn
);
152 rtx_insn
*label
= lp
->landing_pad
;
154 /* During initial rtl generation, use the post_landing_pad. */
157 gcc_assert (lp
->post_landing_pad
);
158 label
= label_rtx (lp
->post_landing_pad
);
161 make_label_edge (edge_cache
, src
, label
,
162 EDGE_ABNORMAL
| EDGE_EH
163 | (CALL_P (insn
) ? EDGE_ABNORMAL_CALL
: 0));
167 /* States of basic block as seen by find_many_sub_basic_blocks. */
169 /* Basic blocks created via split_block belong to this state.
170 make_edges will examine these basic blocks to see if we need to
171 create edges going out of them. */
174 /* Basic blocks that do not need examining belong to this state.
175 These blocks will be left intact. In particular, make_edges will
176 not create edges going out of these basic blocks. */
179 /* Basic blocks that may need splitting (due to a label appearing in
180 the middle, etc) belong to this state. After splitting them,
181 make_edges will create edges going out of them as needed. */
185 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
186 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
188 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
189 #define BLOCK_USED_BY_TABLEJUMP 32
190 #define FULL_STATE(BB) ((size_t) (BB)->aux)
192 /* Identify the edges going out of basic blocks between MIN and MAX,
193 inclusive, that have their states set to BLOCK_NEW or
196 UPDATE_P should be nonzero if we are updating CFG and zero if we
197 are building CFG from scratch. */
200 make_edges (basic_block min
, basic_block max
, int update_p
)
203 sbitmap edge_cache
= NULL
;
205 /* Heavy use of computed goto in machine-generated code can lead to
206 nearly fully-connected CFGs. In that case we spend a significant
207 amount of time searching the edge lists for duplicates. */
208 if (!vec_safe_is_empty (forced_labels
)
209 || cfun
->cfg
->max_jumptable_ents
> 100)
210 edge_cache
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
212 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
213 is always the entry. */
214 if (min
== ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
)
215 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun
), min
, EDGE_FALLTHRU
);
217 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
224 if (STATE (bb
) == BLOCK_ORIGINAL
)
227 /* If we have an edge cache, cache edges going out of BB. */
230 bitmap_clear (edge_cache
);
233 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
234 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
235 bitmap_set_bit (edge_cache
, e
->dest
->index
);
239 if (LABEL_P (BB_HEAD (bb
))
240 && LABEL_ALT_ENTRY_P (BB_HEAD (bb
)))
241 cached_make_edge (NULL
, ENTRY_BLOCK_PTR_FOR_FN (cfun
), bb
, 0);
243 /* Examine the last instruction of the block, and discover the
244 ways we can leave the block. */
247 code
= GET_CODE (insn
);
250 if (code
== JUMP_INSN
)
253 rtx_jump_table_data
*table
;
255 /* Recognize a non-local goto as a branch outside the
257 if (find_reg_note (insn
, REG_NON_LOCAL_GOTO
, NULL_RTX
))
260 /* Recognize a tablejump and do the right thing. */
261 else if (tablejump_p (insn
, NULL
, &table
))
263 rtvec vec
= table
->get_labels ();
266 for (j
= GET_NUM_ELEM (vec
) - 1; j
>= 0; --j
)
267 make_label_edge (edge_cache
, bb
,
268 XEXP (RTVEC_ELT (vec
, j
), 0), 0);
270 /* Some targets (eg, ARM) emit a conditional jump that also
271 contains the out-of-range target. Scan for these and
272 add an edge if necessary. */
273 if ((tmp
= single_set (insn
)) != NULL
274 && SET_DEST (tmp
) == pc_rtx
275 && GET_CODE (SET_SRC (tmp
)) == IF_THEN_ELSE
276 && GET_CODE (XEXP (SET_SRC (tmp
), 2)) == LABEL_REF
)
277 make_label_edge (edge_cache
, bb
,
278 label_ref_label (XEXP (SET_SRC (tmp
), 2)), 0);
281 /* If this is a computed jump, then mark it as reaching
282 everything on the forced_labels list. */
283 else if (computed_jump_p (insn
))
287 FOR_EACH_VEC_SAFE_ELT (forced_labels
, i
, insn
)
288 make_label_edge (edge_cache
, bb
, insn
, EDGE_ABNORMAL
);
291 /* Returns create an exit out. */
292 else if (returnjump_p (insn
))
293 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
), 0);
295 /* Recognize asm goto and do the right thing. */
296 else if ((tmp
= extract_asm_operands (PATTERN (insn
))) != NULL
)
298 int i
, n
= ASM_OPERANDS_LABEL_LENGTH (tmp
);
299 for (i
= 0; i
< n
; ++i
)
300 make_label_edge (edge_cache
, bb
,
301 XEXP (ASM_OPERANDS_LABEL (tmp
, i
), 0), 0);
304 /* Otherwise, we have a plain conditional or unconditional jump. */
307 gcc_assert (JUMP_LABEL (insn
));
308 make_label_edge (edge_cache
, bb
, JUMP_LABEL (insn
), 0);
312 /* If this is a sibling call insn, then this is in effect a combined call
313 and return, and so we need an edge to the exit block. No need to
314 worry about EH edges, since we wouldn't have created the sibling call
315 in the first place. */
316 if (code
== CALL_INSN
&& SIBLING_CALL_P (insn
))
317 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
318 EDGE_SIBCALL
| EDGE_ABNORMAL
);
320 /* If this is a CALL_INSN, then mark it as reaching the active EH
321 handler for this CALL_INSN. If we're handling non-call
322 exceptions then any insn can reach any of the active handlers.
323 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
324 else if (code
== CALL_INSN
|| cfun
->can_throw_non_call_exceptions
)
326 /* Add any appropriate EH edges. */
327 rtl_make_eh_edge (edge_cache
, bb
, insn
);
329 if (code
== CALL_INSN
)
331 if (can_nonlocal_goto (insn
))
333 /* ??? This could be made smarter: in some cases it's
334 possible to tell that certain calls will not do a
335 nonlocal goto. For example, if the nested functions
336 that do the nonlocal gotos do not have their addresses
337 taken, then only calls to those functions or to other
338 nested functions that use them could possibly do
340 for (rtx_insn_list
*x
= nonlocal_goto_handler_labels
;
343 make_label_edge (edge_cache
, bb
, x
->insn (),
344 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
350 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
351 if (REG_NOTE_KIND (note
) == REG_TM
)
352 make_label_edge (edge_cache
, bb
, XEXP (note
, 0),
353 EDGE_ABNORMAL
| EDGE_ABNORMAL_CALL
);
358 /* Find out if we can drop through to the next block. */
359 insn
= NEXT_INSN (insn
);
360 e
= find_edge (bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
));
361 if (e
&& e
->flags
& EDGE_FALLTHRU
)
366 && NOTE_KIND (insn
) != NOTE_INSN_BASIC_BLOCK
)
367 insn
= NEXT_INSN (insn
);
370 cached_make_edge (edge_cache
, bb
, EXIT_BLOCK_PTR_FOR_FN (cfun
),
372 else if (bb
->next_bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
374 if (insn
== BB_HEAD (bb
->next_bb
))
375 cached_make_edge (edge_cache
, bb
, bb
->next_bb
, EDGE_FALLTHRU
);
380 sbitmap_free (edge_cache
);
384 mark_tablejump_edge (rtx label
)
388 gcc_assert (LABEL_P (label
));
389 /* See comment in make_label_edge. */
390 if (INSN_UID (label
) == 0)
392 bb
= BLOCK_FOR_INSN (label
);
393 SET_STATE (bb
, FULL_STATE (bb
) | BLOCK_USED_BY_TABLEJUMP
);
397 purge_dead_tablejump_edges (basic_block bb
, rtx_jump_table_data
*table
)
399 rtx_insn
*insn
= BB_END (bb
);
406 vec
= table
->get_labels ();
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 (label_ref_label (XEXP (SET_SRC (tmp
), 2)));
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
*insn
= BB_HEAD (bb
);
442 rtx_insn
*end
= BB_END (bb
), *x
;
443 rtx_jump_table_data
*table
;
444 rtx_insn
*flow_transfer_insn
= NULL
;
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 /* BB is unreachable at this point - we need to determine its profile
479 once edges are built. */
481 bb
->count
= profile_count::uninitialized ();
482 flow_transfer_insn
= NULL
;
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);
549 = profile_probability::from_reg_br_prob_note (probability
);
550 e
->count
= b
->count
.apply_probability (e
->probability
);
551 f
= FALLTHRU_EDGE (b
);
552 f
->probability
= e
->probability
.invert ();
553 f
->count
= b
->count
- e
->count
;
558 guess_outgoing_edge_probabilities (b
);
561 else if (single_succ_p (b
))
563 e
= single_succ_edge (b
);
564 e
->probability
= profile_probability::always ();
570 /* We rely on BBs with more than two successors to have sane probabilities
571 and do not guess them here. For BBs terminated by switch statements
572 expanded to jump-table jump, we have done the right thing during
573 expansion. For EH edges, we still guess the probabilities here. */
574 bool complex_edge
= false;
575 FOR_EACH_EDGE (e
, ei
, b
->succs
)
576 if (e
->flags
& EDGE_COMPLEX
)
582 guess_outgoing_edge_probabilities (b
);
585 if (b
->count
.initialized_p ())
586 FOR_EACH_EDGE (e
, ei
, b
->succs
)
587 e
->count
= b
->count
.apply_probability (e
->probability
);
590 /* Assume that some pass has inserted labels or control flow
591 instructions within a basic block. Split basic blocks as needed
595 find_many_sub_basic_blocks (sbitmap blocks
)
597 basic_block bb
, min
, max
;
599 auto_vec
<unsigned int> n_succs
;
600 n_succs
.safe_grow_cleared (last_basic_block_for_fn (cfun
));
602 FOR_EACH_BB_FN (bb
, cfun
)
604 bitmap_bit_p (blocks
, bb
->index
) ? BLOCK_TO_SPLIT
: BLOCK_ORIGINAL
);
606 FOR_EACH_BB_FN (bb
, cfun
)
607 if (STATE (bb
) == BLOCK_TO_SPLIT
)
609 int n
= last_basic_block_for_fn (cfun
);
610 unsigned int ns
= EDGE_COUNT (bb
->succs
);
612 find_bb_boundaries (bb
);
613 if (n
== last_basic_block_for_fn (cfun
) && ns
== EDGE_COUNT (bb
->succs
))
614 n_succs
[bb
->index
] = EDGE_COUNT (bb
->succs
);
617 FOR_EACH_BB_FN (bb
, cfun
)
618 if (STATE (bb
) != BLOCK_ORIGINAL
)
628 for (; bb
!= EXIT_BLOCK_PTR_FOR_FN (cfun
); bb
= bb
->next_bb
)
629 if (STATE (bb
) != BLOCK_ORIGINAL
)
632 /* Now re-scan and wire in all edges. This expect simple (conditional)
633 jumps at the end of each new basic blocks. */
634 make_edges (min
, max
, 1);
636 /* Update branch probabilities. Expect only (un)conditional jumps
637 to be created with only the forward edges. */
638 if (profile_status_for_fn (cfun
) != PROFILE_ABSENT
)
639 FOR_BB_BETWEEN (bb
, min
, max
->next_bb
, next_bb
)
644 if (STATE (bb
) == BLOCK_ORIGINAL
)
646 if (STATE (bb
) == BLOCK_NEW
)
648 bool initialized_src
= false, uninitialized_src
= false;
649 bb
->count
= profile_count::zero ();
651 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
653 if (e
->count
.initialized_p ())
655 bb
->count
+= e
->count
;
656 initialized_src
= true;
659 uninitialized_src
= true;
660 if (e
->probability
.initialized_p ())
661 bb
->frequency
+= EDGE_FREQUENCY (e
);
663 /* When some edges are missing with read profile, this is
664 most likely because RTL expansion introduced loop.
665 When profile is guessed we may have BB that is reachable
666 from unlikely path as well as from normal path.
668 TODO: We should handle loops created during BB expansion
669 correctly here. For now we assume all those loop to cycle
672 || (uninitialized_src
673 && profile_status_for_fn (cfun
) != PROFILE_READ
))
674 bb
->count
= profile_count::uninitialized ();
676 /* If nothing changed, there is no need to create new BBs. */
677 else if (EDGE_COUNT (bb
->succs
) == n_succs
[bb
->index
])
679 /* In rare occassions RTL expansion might have mistakely assigned
680 a probabilities different from what is in CFG. This happens
681 when we try to split branch to two but optimize out the
682 second branch during the way. See PR81030. */
683 if (JUMP_P (BB_END (bb
)) && any_condjump_p (BB_END (bb
))
684 && EDGE_COUNT (bb
->succs
) >= 2)
685 update_br_prob_note (bb
);
689 compute_outgoing_frequencies (bb
);
692 FOR_EACH_BB_FN (bb
, cfun
)