Home | History | Annotate | Line # | Download | only in gcc
      1 /* Control flow graph building code for GNU compiler.
      2    Copyright (C) 1987-2022 Free Software Foundation, Inc.
      3 
      4 This file is part of GCC.
      5 
      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
      9 version.
     10 
     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
     14 for more details.
     15 
     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/>.  */
     19 
     20 
     21 #include "config.h"
     23 #include "system.h"
     24 #include "coretypes.h"
     25 #include "backend.h"
     26 #include "rtl.h"
     27 #include "cfghooks.h"
     28 #include "memmodel.h"
     29 #include "emit-rtl.h"
     30 #include "cfgrtl.h"
     31 #include "cfganal.h"
     32 #include "cfgbuild.h"
     33 #include "except.h"
     34 #include "stmt.h"
     35 
     36 static void make_edges (basic_block, basic_block, int);
     37 static void make_label_edge (sbitmap, basic_block, rtx, int);
     38 static void find_bb_boundaries (basic_block);
     39 static void compute_outgoing_frequencies (basic_block);
     40 
     41 /* Return true if insn is something that should be contained inside basic
     43    block.  */
     44 
     45 bool
     46 inside_basic_block_p (const rtx_insn *insn)
     47 {
     48   switch (GET_CODE (insn))
     49     {
     50     case CODE_LABEL:
     51       /* Avoid creating of basic block for jumptables.  */
     52       return (NEXT_INSN (insn) == 0
     53 	      || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
     54 
     55     case JUMP_INSN:
     56     case CALL_INSN:
     57     case INSN:
     58     case DEBUG_INSN:
     59       return true;
     60 
     61     case JUMP_TABLE_DATA:
     62     case BARRIER:
     63     case NOTE:
     64       return false;
     65 
     66     default:
     67       gcc_unreachable ();
     68     }
     69 }
     70 
     71 /* Return true if INSN may cause control flow transfer, so it should be last in
     72    the basic block.  */
     73 
     74 bool
     75 control_flow_insn_p (const rtx_insn *insn)
     76 {
     77   switch (GET_CODE (insn))
     78     {
     79     case NOTE:
     80     case CODE_LABEL:
     81     case DEBUG_INSN:
     82       return false;
     83 
     84     case JUMP_INSN:
     85       return true;
     86 
     87     case CALL_INSN:
     88       /* Noreturn and sibling call instructions terminate the basic blocks
     89 	 (but only if they happen unconditionally).  */
     90       if ((SIBLING_CALL_P (insn)
     91 	   || find_reg_note (insn, REG_NORETURN, 0))
     92 	  && GET_CODE (PATTERN (insn)) != COND_EXEC)
     93 	return true;
     94 
     95       /* Call insn may return to the nonlocal goto handler.  */
     96       if (can_nonlocal_goto (insn))
     97 	return true;
     98       break;
     99 
    100     case INSN:
    101       /* Treat trap instructions like noreturn calls (same provision).  */
    102       if (GET_CODE (PATTERN (insn)) == TRAP_IF
    103 	  && XEXP (PATTERN (insn), 0) == const1_rtx)
    104 	return true;
    105       if (!cfun->can_throw_non_call_exceptions)
    106 	return false;
    107       break;
    108 
    109     case JUMP_TABLE_DATA:
    110     case BARRIER:
    111       /* It is nonsense to reach this when looking for the
    112 	 end of basic block, but before dead code is eliminated
    113 	 this may happen.  */
    114       return false;
    115 
    116     default:
    117       gcc_unreachable ();
    118     }
    119 
    120   return can_throw_internal (insn);
    121 }
    122 
    123 
    124 /* Create an edge between two basic blocks.  FLAGS are auxiliary information
    126    about the edge that is accumulated between calls.  */
    127 
    128 /* Create an edge from a basic block to a label.  */
    129 
    130 static void
    131 make_label_edge (sbitmap edge_cache, basic_block src, rtx label, int flags)
    132 {
    133   gcc_assert (LABEL_P (label));
    134 
    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
    138      printed.  */
    139 
    140   if (INSN_UID (label) == 0)
    141     return;
    142 
    143   cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
    144 }
    145 
    146 /* Create the edges generated by INSN in REGION.  */
    147 
    148 void
    149 rtl_make_eh_edge (sbitmap edge_cache, basic_block src, rtx insn)
    150 {
    151   eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
    152 
    153   if (lp)
    154     {
    155       rtx_insn *label = lp->landing_pad;
    156 
    157       /* During initial rtl generation, use the post_landing_pad.  */
    158       if (label == NULL)
    159 	{
    160 	  gcc_assert (lp->post_landing_pad);
    161 	  label = label_rtx (lp->post_landing_pad);
    162 	}
    163 
    164       make_label_edge (edge_cache, src, label,
    165 		       EDGE_ABNORMAL | EDGE_EH
    166 		       | (CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0));
    167     }
    168 }
    169 
    170 /* States of basic block as seen by find_many_sub_basic_blocks.  */
    171 enum state {
    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.  */
    175   BLOCK_NEW = 0,
    176 
    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.  */
    180   BLOCK_ORIGINAL,
    181 
    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.  */
    185   BLOCK_TO_SPLIT
    186 };
    187 
    188 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
    189 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
    190 
    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)
    194 
    195 /* Identify the edges going out of basic blocks between MIN and MAX,
    196    inclusive, that have their states set to BLOCK_NEW or
    197    BLOCK_TO_SPLIT.
    198 
    199    UPDATE_P should be nonzero if we are updating CFG and zero if we
    200    are building CFG from scratch.  */
    201 
    202 static void
    203 make_edges (basic_block min, basic_block max, int update_p)
    204 {
    205   basic_block bb;
    206   sbitmap edge_cache = NULL;
    207 
    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 (!vec_safe_is_empty (forced_labels)
    212       || cfun->cfg->max_jumptable_ents > 100)
    213     edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
    214 
    215   /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
    216      is always the entry.  */
    217   if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
    218     make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
    219 
    220   FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
    221     {
    222       rtx_insn *insn;
    223       enum rtx_code code;
    224       edge e;
    225       edge_iterator ei;
    226 
    227       if (STATE (bb) == BLOCK_ORIGINAL)
    228 	continue;
    229 
    230       /* If we have an edge cache, cache edges going out of BB.  */
    231       if (edge_cache)
    232 	{
    233 	  bitmap_clear (edge_cache);
    234 	  if (update_p)
    235 	    {
    236 	      FOR_EACH_EDGE (e, ei, bb->succs)
    237 		if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
    238 		  bitmap_set_bit (edge_cache, e->dest->index);
    239 	    }
    240 	}
    241 
    242       if (LABEL_P (BB_HEAD (bb))
    243 	  && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
    244 	cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
    245 
    246       /* Examine the last instruction of the block, and discover the
    247 	 ways we can leave the block.  */
    248 
    249       insn = BB_END (bb);
    250       code = GET_CODE (insn);
    251 
    252       /* A branch.  */
    253       if (code == JUMP_INSN)
    254 	{
    255 	  rtx tmp;
    256 	  rtx_jump_table_data *table;
    257 
    258 	  /* Recognize a non-local goto as a branch outside the
    259 	     current function.  */
    260 	  if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
    261 	    ;
    262 
    263 	  /* Recognize a tablejump and do the right thing.  */
    264 	  else if (tablejump_p (insn, NULL, &table))
    265 	    {
    266 	      rtvec vec = table->get_labels ();
    267 	      int j;
    268 
    269 	      for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
    270 		make_label_edge (edge_cache, bb,
    271 				 XEXP (RTVEC_ELT (vec, j), 0), 0);
    272 
    273 	      /* Some targets (eg, ARM) emit a conditional jump that also
    274 		 contains the out-of-range target.  Scan for these and
    275 		 add an edge if necessary.  */
    276 	      if ((tmp = single_set (insn)) != NULL
    277 		  && SET_DEST (tmp) == pc_rtx
    278 		  && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
    279 		  && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
    280 		make_label_edge (edge_cache, bb,
    281 				 label_ref_label (XEXP (SET_SRC (tmp), 2)), 0);
    282 	    }
    283 
    284 	  /* If this is a computed jump, then mark it as reaching
    285 	     everything on the forced_labels list.  */
    286 	  else if (computed_jump_p (insn))
    287 	    {
    288 	      rtx_insn *insn;
    289 	      unsigned int i;
    290 	      FOR_EACH_VEC_SAFE_ELT (forced_labels, i, insn)
    291 		make_label_edge (edge_cache, bb, insn, EDGE_ABNORMAL);
    292 	    }
    293 
    294 	  /* Returns create an exit out.  */
    295 	  else if (returnjump_p (insn))
    296 	    cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
    297 
    298 	  /* Recognize asm goto and do the right thing.  */
    299 	  else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
    300 	    {
    301 	      int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
    302 	      for (i = 0; i < n; ++i)
    303 		make_label_edge (edge_cache, bb,
    304 				 XEXP (ASM_OPERANDS_LABEL (tmp, i), 0), 0);
    305 	    }
    306 
    307 	  /* Otherwise, we have a plain conditional or unconditional jump.  */
    308 	  else
    309 	    {
    310 	      gcc_assert (JUMP_LABEL (insn));
    311 	      make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
    312 	    }
    313 	}
    314 
    315       /* If this is a sibling call insn, then this is in effect a combined call
    316 	 and return, and so we need an edge to the exit block.  No need to
    317 	 worry about EH edges, since we wouldn't have created the sibling call
    318 	 in the first place.  */
    319       if (code == CALL_INSN && SIBLING_CALL_P (insn))
    320 	cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
    321 			  EDGE_SIBCALL | EDGE_ABNORMAL);
    322 
    323       /* If this is a CALL_INSN, then mark it as reaching the active EH
    324 	 handler for this CALL_INSN.  If we're handling non-call
    325 	 exceptions then any insn can reach any of the active handlers.
    326 	 Also mark the CALL_INSN as reaching any nonlocal goto handler.  */
    327       else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
    328 	{
    329 	  /* Add any appropriate EH edges.  */
    330 	  rtl_make_eh_edge (edge_cache, bb, insn);
    331 
    332 	  if (code == CALL_INSN)
    333 	    {
    334 	      if (can_nonlocal_goto (insn))
    335 		{
    336 		  /* ??? This could be made smarter: in some cases it's
    337 		     possible to tell that certain calls will not do a
    338 		     nonlocal goto.  For example, if the nested functions
    339 		     that do the nonlocal gotos do not have their addresses
    340 		     taken, then only calls to those functions or to other
    341 		     nested functions that use them could possibly do
    342 		     nonlocal gotos.  */
    343 		  for (rtx_insn_list *x = nonlocal_goto_handler_labels;
    344 		       x;
    345 		       x = x->next ())
    346 		    make_label_edge (edge_cache, bb, x->insn (),
    347 				     EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
    348 		}
    349 
    350 	      if (flag_tm)
    351 		{
    352 		  rtx note;
    353 		  for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
    354 		    if (REG_NOTE_KIND (note) == REG_TM)
    355 		      make_label_edge (edge_cache, bb, XEXP (note, 0),
    356 				       EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
    357 		}
    358 	    }
    359 	}
    360 
    361       /* Find out if we can drop through to the next block.  */
    362       insn = NEXT_INSN (insn);
    363       e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
    364       if (e && e->flags & EDGE_FALLTHRU)
    365 	insn = NULL;
    366 
    367       while (insn
    368 	     && NOTE_P (insn)
    369 	     && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
    370 	insn = NEXT_INSN (insn);
    371 
    372       if (!insn)
    373 	cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
    374 			  EDGE_FALLTHRU);
    375       else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
    376 	{
    377 	  if (insn == BB_HEAD (bb->next_bb))
    378 	    cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
    379 	}
    380     }
    381 
    382   if (edge_cache)
    383     sbitmap_free (edge_cache);
    384 }
    385 
    386 static void
    388 mark_tablejump_edge (rtx label)
    389 {
    390   basic_block bb;
    391 
    392   gcc_assert (LABEL_P (label));
    393   /* See comment in make_label_edge.  */
    394   if (INSN_UID (label) == 0)
    395     return;
    396   bb = BLOCK_FOR_INSN (label);
    397   SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
    398 }
    399 
    400 static void
    401 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
    402 {
    403   rtx_insn *insn = BB_END (bb);
    404   rtx tmp;
    405   rtvec vec;
    406   int j;
    407   edge_iterator ei;
    408   edge e;
    409 
    410   vec = table->get_labels ();
    411 
    412   for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
    413     mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
    414 
    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 (label_ref_label (XEXP (SET_SRC (tmp), 2)));
    423 
    424   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
    425     {
    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)))
    430 	{
    431 	  remove_edge (e);
    432 	  continue;
    433 	}
    434       ei_next (&ei);
    435     }
    436 }
    437 
    438 /* Scan basic block BB for possible BB boundaries inside the block
    439    and create new basic blocks in the progress.  */
    440 
    441 static void
    442 find_bb_boundaries (basic_block bb)
    443 {
    444   basic_block orig_bb = bb;
    445   rtx_insn *insn = BB_HEAD (bb);
    446   rtx_insn *end = BB_END (bb), *x;
    447   rtx_jump_table_data *table;
    448   rtx_insn *flow_transfer_insn = NULL;
    449   rtx_insn *debug_insn = NULL;
    450   edge fallthru = NULL;
    451   bool skip_purge;
    452   bool seen_note_after_debug = false;
    453 
    454   if (insn == end)
    455     return;
    456 
    457   if (DEBUG_INSN_P (insn) || DEBUG_INSN_P (end))
    458     {
    459       /* Check whether, without debug insns, the insn==end test above
    460 	 would have caused us to return immediately, and behave the
    461 	 same way even with debug insns.  If we don't do this, debug
    462 	 insns could cause us to purge dead edges at different times,
    463 	 which could in turn change the cfg and affect codegen
    464 	 decisions in subtle but undesirable ways.  */
    465       while (insn != end && DEBUG_INSN_P (insn))
    466 	insn = NEXT_INSN (insn);
    467       rtx_insn *e = end;
    468       while (insn != e && DEBUG_INSN_P (e))
    469 	e = PREV_INSN (e);
    470       if (insn == e)
    471 	{
    472 	  /* If there are debug insns after a single insn that is a
    473 	     control flow insn in the block, we'd have left right
    474 	     away, but we should clean up the debug insns after the
    475 	     control flow insn, because they can't remain in the same
    476 	     block.  So, do the debug insn cleaning up, but then bail
    477 	     out without purging dead edges as we would if the debug
    478 	     insns hadn't been there.  */
    479 	  if (e != end && !DEBUG_INSN_P (e) && control_flow_insn_p (e))
    480 	    {
    481 	      skip_purge = true;
    482 	      flow_transfer_insn = e;
    483 	      goto clean_up_debug_after_control_flow;
    484 	    }
    485 	  return;
    486 	}
    487     }
    488 
    489   if (LABEL_P (insn))
    490     insn = NEXT_INSN (insn);
    491 
    492   /* Scan insn chain and try to find new basic block boundaries.  */
    493   while (1)
    494     {
    495       enum rtx_code code = GET_CODE (insn);
    496 
    497       if (code == DEBUG_INSN)
    498 	{
    499 	  if (flow_transfer_insn && !debug_insn)
    500 	    {
    501 	      debug_insn = insn;
    502 	      seen_note_after_debug = false;
    503 	    }
    504 	}
    505       /* In case we've previously seen an insn that effects a control
    506 	 flow transfer, split the block.  */
    507       else if ((flow_transfer_insn || code == CODE_LABEL)
    508 	       && inside_basic_block_p (insn))
    509 	{
    510 	  rtx_insn *prev = PREV_INSN (insn);
    511 
    512 	  /* If the first non-debug inside_basic_block_p insn after a control
    513 	     flow transfer is not a label, split the block before the debug
    514 	     insn instead of before the non-debug insn, so that the debug
    515 	     insns are not lost.  */
    516 	  if (debug_insn && code != CODE_LABEL && code != BARRIER)
    517 	    {
    518 	      prev = PREV_INSN (debug_insn);
    519 	      if (seen_note_after_debug)
    520 		{
    521 		  /* Though, if there are NOTEs intermixed with DEBUG_INSNs,
    522 		     move the NOTEs before the DEBUG_INSNs and split after
    523 		     the last NOTE.  */
    524 		  rtx_insn *first = NULL, *last = NULL;
    525 		  for (x = debug_insn; x != insn; x = NEXT_INSN (x))
    526 		    {
    527 		      if (NOTE_P (x))
    528 			{
    529 			  if (first == NULL)
    530 			    first = x;
    531 			  last = x;
    532 			}
    533 		      else
    534 			{
    535 			  gcc_assert (DEBUG_INSN_P (x));
    536 			  if (first)
    537 			    {
    538 			      reorder_insns_nobb (first, last, prev);
    539 			      prev = last;
    540 			      first = last = NULL;
    541 			    }
    542 			}
    543 		    }
    544 		  if (first)
    545 		    {
    546 		      reorder_insns_nobb (first, last, prev);
    547 		      prev = last;
    548 		    }
    549 		}
    550 	    }
    551 	  fallthru = split_block (bb, prev);
    552 	  if (flow_transfer_insn)
    553 	    {
    554 	      BB_END (bb) = flow_transfer_insn;
    555 
    556 	      rtx_insn *next;
    557 	      /* Clean up the bb field for the insns between the blocks.  */
    558 	      for (x = NEXT_INSN (flow_transfer_insn);
    559 		   x != BB_HEAD (fallthru->dest);
    560 		   x = next)
    561 		{
    562 		  next = NEXT_INSN (x);
    563 		  /* Debug insns should not be in between basic blocks,
    564 		     drop them on the floor.  */
    565 		  if (DEBUG_INSN_P (x))
    566 		    delete_insn (x);
    567 		  else if (!BARRIER_P (x))
    568 		    set_block_for_insn (x, NULL);
    569 		}
    570 	    }
    571 
    572 	  bb = fallthru->dest;
    573 	  remove_edge (fallthru);
    574 	  /* BB is unreachable at this point - we need to determine its profile
    575 	     once edges are built.  */
    576 	  bb->count = profile_count::uninitialized ();
    577 	  flow_transfer_insn = NULL;
    578 	  debug_insn = NULL;
    579 	  if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
    580 	    make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
    581 	}
    582       else if (code == BARRIER)
    583 	{
    584 	  /* __builtin_unreachable () may cause a barrier to be emitted in
    585 	     the middle of a BB.  We need to split it in the same manner as
    586 	     if the barrier were preceded by a control_flow_insn_p insn.  */
    587 	  if (!flow_transfer_insn)
    588 	    flow_transfer_insn = prev_nonnote_nondebug_insn_bb (insn);
    589 	  debug_insn = NULL;
    590 	}
    591       else if (debug_insn)
    592 	{
    593 	  if (code == NOTE)
    594 	    seen_note_after_debug = true;
    595 	  else
    596 	    /* Jump tables.  */
    597 	    debug_insn = NULL;
    598 	}
    599 
    600       if (control_flow_insn_p (insn))
    601 	flow_transfer_insn = insn;
    602       if (insn == end)
    603 	break;
    604       insn = NEXT_INSN (insn);
    605     }
    606 
    607   /* In case expander replaced normal insn by sequence terminating by
    608      return and barrier, or possibly other sequence not behaving like
    609      ordinary jump, we need to take care and move basic block boundary.  */
    610   if (flow_transfer_insn && flow_transfer_insn != end)
    611     {
    612       skip_purge = false;
    613 
    614     clean_up_debug_after_control_flow:
    615       BB_END (bb) = flow_transfer_insn;
    616 
    617       /* Clean up the bb field for the insns that do not belong to BB.  */
    618       rtx_insn *next;
    619       for (x = NEXT_INSN (flow_transfer_insn); ; x = next)
    620 	{
    621 	  next = NEXT_INSN (x);
    622 	  /* Debug insns should not be in between basic blocks,
    623 	     drop them on the floor.  */
    624 	  if (DEBUG_INSN_P (x))
    625 	    delete_insn (x);
    626 	  else if (!BARRIER_P (x))
    627 	    set_block_for_insn (x, NULL);
    628 	  if (x == end)
    629 	    break;
    630 	}
    631 
    632       if (skip_purge)
    633 	return;
    634     }
    635 
    636   /* We've possibly replaced the conditional jump by conditional jump
    637      followed by cleanup at fallthru edge, so the outgoing edges may
    638      be dead.  */
    639   purge_dead_edges (bb);
    640 
    641   /* purge_dead_edges doesn't handle tablejump's, but if we have split the
    642      basic block, we might need to kill some edges.  */
    643   if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
    644     purge_dead_tablejump_edges (bb, table);
    645 }
    646 
    647 /*  Assume that frequency of basic block B is known.  Compute frequencies
    648     and probabilities of outgoing edges.  */
    649 
    650 static void
    651 compute_outgoing_frequencies (basic_block b)
    652 {
    653   edge e, f;
    654   edge_iterator ei;
    655 
    656   if (EDGE_COUNT (b->succs) == 2)
    657     {
    658       rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
    659       int probability;
    660 
    661       if (note)
    662 	{
    663 	  probability = XINT (note, 0);
    664 	  e = BRANCH_EDGE (b);
    665 	  e->probability
    666 		 = profile_probability::from_reg_br_prob_note (probability);
    667 	  f = FALLTHRU_EDGE (b);
    668 	  f->probability = e->probability.invert ();
    669 	  return;
    670 	}
    671       else
    672         {
    673           guess_outgoing_edge_probabilities (b);
    674         }
    675     }
    676   else if (single_succ_p (b))
    677     {
    678       e = single_succ_edge (b);
    679       e->probability = profile_probability::always ();
    680       return;
    681     }
    682   else
    683     {
    684       /* We rely on BBs with more than two successors to have sane probabilities
    685          and do not guess them here. For BBs terminated by switch statements
    686          expanded to jump-table jump, we have done the right thing during
    687          expansion. For EH edges, we still guess the probabilities here.  */
    688       bool complex_edge = false;
    689       FOR_EACH_EDGE (e, ei, b->succs)
    690         if (e->flags & EDGE_COMPLEX)
    691           {
    692             complex_edge = true;
    693             break;
    694           }
    695       if (complex_edge)
    696         guess_outgoing_edge_probabilities (b);
    697     }
    698 }
    699 
    700 /* Assume that some pass has inserted labels or control flow
    701    instructions within a basic block.  Split basic blocks as needed
    702    and create edges.  */
    703 
    704 void
    705 find_many_sub_basic_blocks (sbitmap blocks)
    706 {
    707   basic_block bb, min, max;
    708   bool found = false;
    709   auto_vec<unsigned int> n_succs;
    710   n_succs.safe_grow_cleared (last_basic_block_for_fn (cfun), true);
    711 
    712   FOR_EACH_BB_FN (bb, cfun)
    713     SET_STATE (bb,
    714 	       bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
    715 
    716   FOR_EACH_BB_FN (bb, cfun)
    717     if (STATE (bb) == BLOCK_TO_SPLIT)
    718       {
    719 	int n = last_basic_block_for_fn (cfun);
    720 	unsigned int ns = EDGE_COUNT (bb->succs);
    721 
    722         find_bb_boundaries (bb);
    723 	if (n == last_basic_block_for_fn (cfun) && ns == EDGE_COUNT (bb->succs))
    724 	  n_succs[bb->index] = EDGE_COUNT (bb->succs);
    725       }
    726 
    727   FOR_EACH_BB_FN (bb, cfun)
    728     if (STATE (bb) != BLOCK_ORIGINAL)
    729       {
    730 	found = true;
    731         break;
    732       }
    733 
    734   if (!found)
    735     return;
    736 
    737   min = max = bb;
    738   for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
    739     if (STATE (bb) != BLOCK_ORIGINAL)
    740       max = bb;
    741 
    742   /* Now re-scan and wire in all edges.  This expect simple (conditional)
    743      jumps at the end of each new basic blocks.  */
    744   make_edges (min, max, 1);
    745 
    746   /* Update branch probabilities.  Expect only (un)conditional jumps
    747      to be created with only the forward edges.  */
    748   if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
    749     FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
    750       {
    751 	edge e;
    752 	edge_iterator ei;
    753 
    754 	if (STATE (bb) == BLOCK_ORIGINAL)
    755 	  continue;
    756 	if (STATE (bb) == BLOCK_NEW)
    757 	  {
    758 	    bool initialized_src = false, uninitialized_src = false;
    759 	    bb->count = profile_count::zero ();
    760 	    FOR_EACH_EDGE (e, ei, bb->preds)
    761 	      {
    762 		if (e->count ().initialized_p ())
    763 		  {
    764 		    bb->count += e->count ();
    765 		    initialized_src = true;
    766 		  }
    767 		else
    768 		  uninitialized_src = true;
    769 	      }
    770 	    /* When some edges are missing with read profile, this is
    771 	       most likely because RTL expansion introduced loop.
    772 	       When profile is guessed we may have BB that is reachable
    773 	       from unlikely path as well as from normal path.
    774 
    775 	       TODO: We should handle loops created during BB expansion
    776 	       correctly here.  For now we assume all those loop to cycle
    777 	       precisely once.  */
    778 	    if (!initialized_src
    779 		|| (uninitialized_src
    780 		     && profile_status_for_fn (cfun) < PROFILE_GUESSED))
    781 	      bb->count = profile_count::uninitialized ();
    782 	  }
    783  	/* If nothing changed, there is no need to create new BBs.  */
    784 	else if (EDGE_COUNT (bb->succs) == n_succs[bb->index])
    785 	  {
    786 	    /* In rare occassions RTL expansion might have mistakely assigned
    787 	       a probabilities different from what is in CFG.  This happens
    788 	       when we try to split branch to two but optimize out the
    789 	       second branch during the way. See PR81030.  */
    790 	    if (JUMP_P (BB_END (bb)) && any_condjump_p (BB_END (bb))
    791 		&& EDGE_COUNT (bb->succs) >= 2)
    792 	      update_br_prob_note (bb);
    793 	    continue;
    794 	  }
    795 
    796 	compute_outgoing_frequencies (bb);
    797       }
    798 
    799   FOR_EACH_BB_FN (bb, cfun)
    800     SET_STATE (bb, 0);
    801 }
    802