comparison gcc/cfgbuild.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents f6334be47118
children 84e7813d76e9
comparison
equal deleted inserted replaced
68:561a7518be6b 111:04ced10e8804
1 /* Control flow graph building code for GNU compiler. 1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2 Copyright (C) 1987-2017 Free Software Foundation, Inc.
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 3
6 This file is part of GCC. 4 This file is part of GCC.
7 5
8 GCC is free software; you can redistribute it and/or modify it under 6 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 7 the terms of the GNU General Public License as published by the Free
21 19
22 20
23 #include "config.h" 21 #include "config.h"
24 #include "system.h" 22 #include "system.h"
25 #include "coretypes.h" 23 #include "coretypes.h"
26 #include "tm.h" 24 #include "backend.h"
27 #include "tree.h"
28 #include "rtl.h" 25 #include "rtl.h"
29 #include "hard-reg-set.h" 26 #include "cfghooks.h"
30 #include "basic-block.h" 27 #include "memmodel.h"
31 #include "regs.h" 28 #include "emit-rtl.h"
32 #include "flags.h" 29 #include "cfgrtl.h"
33 #include "output.h" 30 #include "cfganal.h"
34 #include "function.h" 31 #include "cfgbuild.h"
35 #include "except.h" 32 #include "except.h"
36 #include "expr.h" 33 #include "stmt.h"
37 #include "diagnostic-core.h"
38 #include "timevar.h"
39 #include "sbitmap.h"
40 34
41 static void make_edges (basic_block, basic_block, int); 35 static void make_edges (basic_block, basic_block, int);
42 static void make_label_edge (sbitmap, basic_block, rtx, int); 36 static void make_label_edge (sbitmap, basic_block, rtx, int);
43 static void find_bb_boundaries (basic_block); 37 static void find_bb_boundaries (basic_block);
44 static void compute_outgoing_frequencies (basic_block); 38 static void compute_outgoing_frequencies (basic_block);
45 39
46 /* Return true if insn is something that should be contained inside basic 40 /* Return true if insn is something that should be contained inside basic
47 block. */ 41 block. */
48 42
49 bool 43 bool
50 inside_basic_block_p (const_rtx insn) 44 inside_basic_block_p (const rtx_insn *insn)
51 { 45 {
52 switch (GET_CODE (insn)) 46 switch (GET_CODE (insn))
53 { 47 {
54 case CODE_LABEL: 48 case CODE_LABEL:
55 /* Avoid creating of basic block for jumptables. */ 49 /* Avoid creating of basic block for jumptables. */
56 return (NEXT_INSN (insn) == 0 50 return (NEXT_INSN (insn) == 0
57 || !JUMP_P (NEXT_INSN (insn)) 51 || ! JUMP_TABLE_DATA_P (NEXT_INSN (insn)));
58 || (GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_VEC
59 && GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_DIFF_VEC));
60 52
61 case JUMP_INSN: 53 case JUMP_INSN:
62 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
63 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
64
65 case CALL_INSN: 54 case CALL_INSN:
66 case INSN: 55 case INSN:
67 case DEBUG_INSN: 56 case DEBUG_INSN:
68 return true; 57 return true;
69 58
59 case JUMP_TABLE_DATA:
70 case BARRIER: 60 case BARRIER:
71 case NOTE: 61 case NOTE:
72 return false; 62 return false;
73 63
74 default: 64 default:
78 68
79 /* Return true if INSN may cause control flow transfer, so it should be last in 69 /* Return true if INSN may cause control flow transfer, so it should be last in
80 the basic block. */ 70 the basic block. */
81 71
82 bool 72 bool
83 control_flow_insn_p (const_rtx insn) 73 control_flow_insn_p (const rtx_insn *insn)
84 { 74 {
85 switch (GET_CODE (insn)) 75 switch (GET_CODE (insn))
86 { 76 {
87 case NOTE: 77 case NOTE:
88 case CODE_LABEL: 78 case CODE_LABEL:
89 case DEBUG_INSN: 79 case DEBUG_INSN:
90 return false; 80 return false;
91 81
92 case JUMP_INSN: 82 case JUMP_INSN:
93 /* Jump insn always causes control transfer except for tablejumps. */ 83 return true;
94 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
95 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
96 84
97 case CALL_INSN: 85 case CALL_INSN:
98 /* Noreturn and sibling call instructions terminate the basic blocks 86 /* Noreturn and sibling call instructions terminate the basic blocks
99 (but only if they happen unconditionally). */ 87 (but only if they happen unconditionally). */
100 if ((SIBLING_CALL_P (insn) 88 if ((SIBLING_CALL_P (insn)
114 return true; 102 return true;
115 if (!cfun->can_throw_non_call_exceptions) 103 if (!cfun->can_throw_non_call_exceptions)
116 return false; 104 return false;
117 break; 105 break;
118 106
107 case JUMP_TABLE_DATA:
119 case BARRIER: 108 case BARRIER:
120 /* It is nonsense to reach barrier when looking for the 109 /* It is nonsense to reach this when looking for the
121 end of basic block, but before dead code is eliminated 110 end of basic block, but before dead code is eliminated
122 this may happen. */ 111 this may happen. */
123 return false; 112 return false;
124 113
125 default: 114 default:
158 { 147 {
159 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn); 148 eh_landing_pad lp = get_eh_landing_pad_from_rtx (insn);
160 149
161 if (lp) 150 if (lp)
162 { 151 {
163 rtx label = lp->landing_pad; 152 rtx_insn *label = lp->landing_pad;
164 153
165 /* During initial rtl generation, use the post_landing_pad. */ 154 /* During initial rtl generation, use the post_landing_pad. */
166 if (label == NULL) 155 if (label == NULL)
167 { 156 {
168 gcc_assert (lp->post_landing_pad); 157 gcc_assert (lp->post_landing_pad);
214 sbitmap edge_cache = NULL; 203 sbitmap edge_cache = NULL;
215 204
216 /* Heavy use of computed goto in machine-generated code can lead to 205 /* Heavy use of computed goto in machine-generated code can lead to
217 nearly fully-connected CFGs. In that case we spend a significant 206 nearly fully-connected CFGs. In that case we spend a significant
218 amount of time searching the edge lists for duplicates. */ 207 amount of time searching the edge lists for duplicates. */
219 if (forced_labels || cfun->cfg->max_jumptable_ents > 100) 208 if (!vec_safe_is_empty (forced_labels)
220 edge_cache = sbitmap_alloc (last_basic_block); 209 || cfun->cfg->max_jumptable_ents > 100)
210 edge_cache = sbitmap_alloc (last_basic_block_for_fn (cfun));
221 211
222 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block 212 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
223 is always the entry. */ 213 is always the entry. */
224 if (min == ENTRY_BLOCK_PTR->next_bb) 214 if (min == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
225 make_edge (ENTRY_BLOCK_PTR, min, EDGE_FALLTHRU); 215 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), min, EDGE_FALLTHRU);
226 216
227 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb) 217 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
228 { 218 {
229 rtx insn, x; 219 rtx_insn *insn;
230 enum rtx_code code; 220 enum rtx_code code;
231 edge e; 221 edge e;
232 edge_iterator ei; 222 edge_iterator ei;
233 223
234 if (STATE (bb) == BLOCK_ORIGINAL) 224 if (STATE (bb) == BLOCK_ORIGINAL)
235 continue; 225 continue;
236 226
237 /* If we have an edge cache, cache edges going out of BB. */ 227 /* If we have an edge cache, cache edges going out of BB. */
238 if (edge_cache) 228 if (edge_cache)
239 { 229 {
240 sbitmap_zero (edge_cache); 230 bitmap_clear (edge_cache);
241 if (update_p) 231 if (update_p)
242 { 232 {
243 FOR_EACH_EDGE (e, ei, bb->succs) 233 FOR_EACH_EDGE (e, ei, bb->succs)
244 if (e->dest != EXIT_BLOCK_PTR) 234 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
245 SET_BIT (edge_cache, e->dest->index); 235 bitmap_set_bit (edge_cache, e->dest->index);
246 } 236 }
247 } 237 }
248 238
249 if (LABEL_P (BB_HEAD (bb)) 239 if (LABEL_P (BB_HEAD (bb))
250 && LABEL_ALT_ENTRY_P (BB_HEAD (bb))) 240 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
251 cached_make_edge (NULL, ENTRY_BLOCK_PTR, bb, 0); 241 cached_make_edge (NULL, ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
252 242
253 /* Examine the last instruction of the block, and discover the 243 /* Examine the last instruction of the block, and discover the
254 ways we can leave the block. */ 244 ways we can leave the block. */
255 245
256 insn = BB_END (bb); 246 insn = BB_END (bb);
258 248
259 /* A branch. */ 249 /* A branch. */
260 if (code == JUMP_INSN) 250 if (code == JUMP_INSN)
261 { 251 {
262 rtx tmp; 252 rtx tmp;
253 rtx_jump_table_data *table;
263 254
264 /* Recognize a non-local goto as a branch outside the 255 /* Recognize a non-local goto as a branch outside the
265 current function. */ 256 current function. */
266 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX)) 257 if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
267 ; 258 ;
268 259
269 /* Recognize a tablejump and do the right thing. */ 260 /* Recognize a tablejump and do the right thing. */
270 else if (tablejump_p (insn, NULL, &tmp)) 261 else if (tablejump_p (insn, NULL, &table))
271 { 262 {
272 rtvec vec; 263 rtvec vec = table->get_labels ();
273 int j; 264 int j;
274
275 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
276 vec = XVEC (PATTERN (tmp), 0);
277 else
278 vec = XVEC (PATTERN (tmp), 1);
279 265
280 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j) 266 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
281 make_label_edge (edge_cache, bb, 267 make_label_edge (edge_cache, bb,
282 XEXP (RTVEC_ELT (vec, j), 0), 0); 268 XEXP (RTVEC_ELT (vec, j), 0), 0);
283 269
287 if ((tmp = single_set (insn)) != NULL 273 if ((tmp = single_set (insn)) != NULL
288 && SET_DEST (tmp) == pc_rtx 274 && SET_DEST (tmp) == pc_rtx
289 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE 275 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
290 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF) 276 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
291 make_label_edge (edge_cache, bb, 277 make_label_edge (edge_cache, bb,
292 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0); 278 label_ref_label (XEXP (SET_SRC (tmp), 2)), 0);
293 } 279 }
294 280
295 /* If this is a computed jump, then mark it as reaching 281 /* If this is a computed jump, then mark it as reaching
296 everything on the forced_labels list. */ 282 everything on the forced_labels list. */
297 else if (computed_jump_p (insn)) 283 else if (computed_jump_p (insn))
298 { 284 {
299 for (x = forced_labels; x; x = XEXP (x, 1)) 285 rtx_insn *insn;
300 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL); 286 unsigned int i;
287 FOR_EACH_VEC_SAFE_ELT (forced_labels, i, insn)
288 make_label_edge (edge_cache, bb, insn, EDGE_ABNORMAL);
301 } 289 }
302 290
303 /* Returns create an exit out. */ 291 /* Returns create an exit out. */
304 else if (returnjump_p (insn)) 292 else if (returnjump_p (insn))
305 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0); 293 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun), 0);
306 294
307 /* Recognize asm goto and do the right thing. */ 295 /* Recognize asm goto and do the right thing. */
308 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL) 296 else if ((tmp = extract_asm_operands (PATTERN (insn))) != NULL)
309 { 297 {
310 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp); 298 int i, n = ASM_OPERANDS_LABEL_LENGTH (tmp);
324 /* If this is a sibling call insn, then this is in effect a combined call 312 /* If this is a sibling call insn, then this is in effect a combined call
325 and return, and so we need an edge to the exit block. No need to 313 and return, and so we need an edge to the exit block. No need to
326 worry about EH edges, since we wouldn't have created the sibling call 314 worry about EH edges, since we wouldn't have created the sibling call
327 in the first place. */ 315 in the first place. */
328 if (code == CALL_INSN && SIBLING_CALL_P (insn)) 316 if (code == CALL_INSN && SIBLING_CALL_P (insn))
329 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 317 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
330 EDGE_SIBCALL | EDGE_ABNORMAL); 318 EDGE_SIBCALL | EDGE_ABNORMAL);
331 319
332 /* If this is a CALL_INSN, then mark it as reaching the active EH 320 /* If this is a CALL_INSN, then mark it as reaching the active EH
333 handler for this CALL_INSN. If we're handling non-call 321 handler for this CALL_INSN. If we're handling non-call
334 exceptions then any insn can reach any of the active handlers. 322 exceptions then any insn can reach any of the active handlers.
336 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions) 324 else if (code == CALL_INSN || cfun->can_throw_non_call_exceptions)
337 { 325 {
338 /* Add any appropriate EH edges. */ 326 /* Add any appropriate EH edges. */
339 rtl_make_eh_edge (edge_cache, bb, insn); 327 rtl_make_eh_edge (edge_cache, bb, insn);
340 328
341 if (code == CALL_INSN && nonlocal_goto_handler_labels) 329 if (code == CALL_INSN)
342 { 330 {
343 /* ??? This could be made smarter: in some cases it's possible
344 to tell that certain calls will not do a nonlocal goto.
345 For example, if the nested functions that do the nonlocal
346 gotos do not have their addresses taken, then only calls to
347 those functions or to other nested functions that use them
348 could possibly do nonlocal gotos. */
349 if (can_nonlocal_goto (insn)) 331 if (can_nonlocal_goto (insn))
350 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1)) 332 {
351 make_label_edge (edge_cache, bb, XEXP (x, 0), 333 /* ??? This could be made smarter: in some cases it's
352 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL); 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
339 nonlocal gotos. */
340 for (rtx_insn_list *x = nonlocal_goto_handler_labels;
341 x;
342 x = x->next ())
343 make_label_edge (edge_cache, bb, x->insn (),
344 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
345 }
346
347 if (flag_tm)
348 {
349 rtx note;
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);
354 }
353 } 355 }
354 } 356 }
355 357
356 /* Find out if we can drop through to the next block. */ 358 /* Find out if we can drop through to the next block. */
357 insn = NEXT_INSN (insn); 359 insn = NEXT_INSN (insn);
358 e = find_edge (bb, EXIT_BLOCK_PTR); 360 e = find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun));
359 if (e && e->flags & EDGE_FALLTHRU) 361 if (e && e->flags & EDGE_FALLTHRU)
360 insn = NULL; 362 insn = NULL;
361 363
362 while (insn 364 while (insn
363 && NOTE_P (insn) 365 && NOTE_P (insn)
364 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK) 366 && NOTE_KIND (insn) != NOTE_INSN_BASIC_BLOCK)
365 insn = NEXT_INSN (insn); 367 insn = NEXT_INSN (insn);
366 368
367 if (!insn) 369 if (!insn)
368 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU); 370 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR_FOR_FN (cfun),
369 else if (bb->next_bb != EXIT_BLOCK_PTR) 371 EDGE_FALLTHRU);
372 else if (bb->next_bb != EXIT_BLOCK_PTR_FOR_FN (cfun))
370 { 373 {
371 if (insn == BB_HEAD (bb->next_bb)) 374 if (insn == BB_HEAD (bb->next_bb))
372 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU); 375 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
373 } 376 }
374 } 377 }
375 378
376 if (edge_cache) 379 if (edge_cache)
377 sbitmap_vector_free (edge_cache); 380 sbitmap_free (edge_cache);
378 } 381 }
379 382
380 static void 383 static void
381 mark_tablejump_edge (rtx label) 384 mark_tablejump_edge (rtx label)
382 { 385 {
389 bb = BLOCK_FOR_INSN (label); 392 bb = BLOCK_FOR_INSN (label);
390 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP); 393 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
391 } 394 }
392 395
393 static void 396 static void
394 purge_dead_tablejump_edges (basic_block bb, rtx table) 397 purge_dead_tablejump_edges (basic_block bb, rtx_jump_table_data *table)
395 { 398 {
396 rtx insn = BB_END (bb), tmp; 399 rtx_insn *insn = BB_END (bb);
400 rtx tmp;
397 rtvec vec; 401 rtvec vec;
398 int j; 402 int j;
399 edge_iterator ei; 403 edge_iterator ei;
400 edge e; 404 edge e;
401 405
402 if (GET_CODE (PATTERN (table)) == ADDR_VEC) 406 vec = table->get_labels ();
403 vec = XVEC (PATTERN (table), 0);
404 else
405 vec = XVEC (PATTERN (table), 1);
406 407
407 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j) 408 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
408 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0)); 409 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
409 410
410 /* Some targets (eg, ARM) emit a conditional jump that also 411 /* Some targets (eg, ARM) emit a conditional jump that also
412 add an edge if necessary. */ 413 add an edge if necessary. */
413 if ((tmp = single_set (insn)) != NULL 414 if ((tmp = single_set (insn)) != NULL
414 && SET_DEST (tmp) == pc_rtx 415 && SET_DEST (tmp) == pc_rtx
415 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE 416 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
416 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF) 417 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
417 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0)); 418 mark_tablejump_edge (label_ref_label (XEXP (SET_SRC (tmp), 2)));
418 419
419 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ) 420 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
420 { 421 {
421 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP) 422 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
422 SET_STATE (e->dest, FULL_STATE (e->dest) 423 SET_STATE (e->dest, FULL_STATE (e->dest)
435 436
436 static void 437 static void
437 find_bb_boundaries (basic_block bb) 438 find_bb_boundaries (basic_block bb)
438 { 439 {
439 basic_block orig_bb = bb; 440 basic_block orig_bb = bb;
440 rtx insn = BB_HEAD (bb); 441 rtx_insn *insn = BB_HEAD (bb);
441 rtx end = BB_END (bb), x; 442 rtx_insn *end = BB_END (bb), *x;
442 rtx table; 443 rtx_jump_table_data *table;
443 rtx flow_transfer_insn = NULL_RTX; 444 rtx_insn *flow_transfer_insn = NULL;
445 rtx_insn *debug_insn = NULL;
444 edge fallthru = NULL; 446 edge fallthru = NULL;
445 447
446 if (insn == BB_END (bb)) 448 if (insn == end)
447 return; 449 return;
448 450
449 if (LABEL_P (insn)) 451 if (LABEL_P (insn))
450 insn = NEXT_INSN (insn); 452 insn = NEXT_INSN (insn);
451 453
452 /* Scan insn chain and try to find new basic block boundaries. */ 454 /* Scan insn chain and try to find new basic block boundaries. */
453 while (1) 455 while (1)
454 { 456 {
455 enum rtx_code code = GET_CODE (insn); 457 enum rtx_code code = GET_CODE (insn);
456 458
459 if (code == DEBUG_INSN)
460 {
461 if (flow_transfer_insn && !debug_insn)
462 debug_insn = insn;
463 }
457 /* In case we've previously seen an insn that effects a control 464 /* In case we've previously seen an insn that effects a control
458 flow transfer, split the block. */ 465 flow transfer, split the block. */
459 if ((flow_transfer_insn || code == CODE_LABEL) 466 else if ((flow_transfer_insn || code == CODE_LABEL)
460 && inside_basic_block_p (insn)) 467 && inside_basic_block_p (insn))
461 { 468 {
462 fallthru = split_block (bb, PREV_INSN (insn)); 469 rtx_insn *prev = PREV_INSN (insn);
470
471 /* If the first non-debug inside_basic_block_p insn after a control
472 flow transfer is not a label, split the block before the debug
473 insn instead of before the non-debug insn, so that the debug
474 insns are not lost. */
475 if (debug_insn && code != CODE_LABEL && code != BARRIER)
476 prev = PREV_INSN (debug_insn);
477 fallthru = split_block (bb, prev);
463 if (flow_transfer_insn) 478 if (flow_transfer_insn)
464 { 479 {
465 BB_END (bb) = flow_transfer_insn; 480 BB_END (bb) = flow_transfer_insn;
466 481
482 rtx_insn *next;
467 /* Clean up the bb field for the insns between the blocks. */ 483 /* Clean up the bb field for the insns between the blocks. */
468 for (x = NEXT_INSN (flow_transfer_insn); 484 for (x = NEXT_INSN (flow_transfer_insn);
469 x != BB_HEAD (fallthru->dest); 485 x != BB_HEAD (fallthru->dest);
470 x = NEXT_INSN (x)) 486 x = next)
471 if (!BARRIER_P (x)) 487 {
472 set_block_for_insn (x, NULL); 488 next = NEXT_INSN (x);
489 /* Debug insns should not be in between basic blocks,
490 drop them on the floor. */
491 if (DEBUG_INSN_P (x))
492 delete_insn (x);
493 else if (!BARRIER_P (x))
494 set_block_for_insn (x, NULL);
495 }
473 } 496 }
474 497
475 bb = fallthru->dest; 498 bb = fallthru->dest;
476 remove_edge (fallthru); 499 remove_edge (fallthru);
477 flow_transfer_insn = NULL_RTX; 500 /* BB is unreachable at this point - we need to determine its profile
501 once edges are built. */
502 bb->frequency = 0;
503 bb->count = profile_count::uninitialized ();
504 flow_transfer_insn = NULL;
505 debug_insn = NULL;
478 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn)) 506 if (code == CODE_LABEL && LABEL_ALT_ENTRY_P (insn))
479 make_edge (ENTRY_BLOCK_PTR, bb, 0); 507 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, 0);
480 } 508 }
481 else if (code == BARRIER) 509 else if (code == BARRIER)
482 { 510 {
483 /* __builtin_unreachable () may cause a barrier to be emitted in 511 /* __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 512 the middle of a BB. We need to split it in the same manner as
495 } 523 }
496 524
497 /* In case expander replaced normal insn by sequence terminating by 525 /* In case expander replaced normal insn by sequence terminating by
498 return and barrier, or possibly other sequence not behaving like 526 return and barrier, or possibly other sequence not behaving like
499 ordinary jump, we need to take care and move basic block boundary. */ 527 ordinary jump, we need to take care and move basic block boundary. */
500 if (flow_transfer_insn) 528 if (flow_transfer_insn && flow_transfer_insn != end)
501 { 529 {
502 BB_END (bb) = flow_transfer_insn; 530 BB_END (bb) = flow_transfer_insn;
503 531
504 /* Clean up the bb field for the insns that do not belong to BB. */ 532 /* Clean up the bb field for the insns that do not belong to BB. */
505 x = flow_transfer_insn; 533 rtx_insn *next;
506 while (x != end) 534 for (x = NEXT_INSN (flow_transfer_insn); ; x = next)
507 { 535 {
508 x = NEXT_INSN (x); 536 next = NEXT_INSN (x);
509 if (!BARRIER_P (x)) 537 /* Debug insns should not be in between basic blocks,
538 drop them on the floor. */
539 if (DEBUG_INSN_P (x))
540 delete_insn (x);
541 else if (!BARRIER_P (x))
510 set_block_for_insn (x, NULL); 542 set_block_for_insn (x, NULL);
543 if (x == end)
544 break;
511 } 545 }
512 } 546 }
513 547
514 /* We've possibly replaced the conditional jump by conditional jump 548 /* We've possibly replaced the conditional jump by conditional jump
515 followed by cleanup at fallthru edge, so the outgoing edges may 549 followed by cleanup at fallthru edge, so the outgoing edges may
536 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL); 570 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
537 int probability; 571 int probability;
538 572
539 if (note) 573 if (note)
540 { 574 {
541 probability = INTVAL (XEXP (note, 0)); 575 probability = XINT (note, 0);
542 e = BRANCH_EDGE (b); 576 e = BRANCH_EDGE (b);
543 e->probability = probability; 577 e->probability
544 e->count = ((b->count * probability + REG_BR_PROB_BASE / 2) 578 = profile_probability::from_reg_br_prob_note (probability);
545 / REG_BR_PROB_BASE);
546 f = FALLTHRU_EDGE (b); 579 f = FALLTHRU_EDGE (b);
547 f->probability = REG_BR_PROB_BASE - probability; 580 f->probability = e->probability.invert ();
548 f->count = b->count - e->count;
549 return; 581 return;
550 } 582 }
551 } 583 else
552 584 {
553 if (single_succ_p (b)) 585 guess_outgoing_edge_probabilities (b);
586 }
587 }
588 else if (single_succ_p (b))
554 { 589 {
555 e = single_succ_edge (b); 590 e = single_succ_edge (b);
556 e->probability = REG_BR_PROB_BASE; 591 e->probability = profile_probability::always ();
557 e->count = b->count;
558 return; 592 return;
559 } 593 }
560 guess_outgoing_edge_probabilities (b); 594 else
561 if (b->count) 595 {
562 FOR_EACH_EDGE (e, ei, b->succs) 596 /* We rely on BBs with more than two successors to have sane probabilities
563 e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2) 597 and do not guess them here. For BBs terminated by switch statements
564 / REG_BR_PROB_BASE); 598 expanded to jump-table jump, we have done the right thing during
599 expansion. For EH edges, we still guess the probabilities here. */
600 bool complex_edge = false;
601 FOR_EACH_EDGE (e, ei, b->succs)
602 if (e->flags & EDGE_COMPLEX)
603 {
604 complex_edge = true;
605 break;
606 }
607 if (complex_edge)
608 guess_outgoing_edge_probabilities (b);
609 }
565 } 610 }
566 611
567 /* Assume that some pass has inserted labels or control flow 612 /* Assume that some pass has inserted labels or control flow
568 instructions within a basic block. Split basic blocks as needed 613 instructions within a basic block. Split basic blocks as needed
569 and create edges. */ 614 and create edges. */
570 615
571 void 616 void
572 find_many_sub_basic_blocks (sbitmap blocks) 617 find_many_sub_basic_blocks (sbitmap blocks)
573 { 618 {
574 basic_block bb, min, max; 619 basic_block bb, min, max;
575 620 bool found = false;
576 FOR_EACH_BB (bb) 621 auto_vec<unsigned int> n_succs;
622 n_succs.safe_grow_cleared (last_basic_block_for_fn (cfun));
623
624 FOR_EACH_BB_FN (bb, cfun)
577 SET_STATE (bb, 625 SET_STATE (bb,
578 TEST_BIT (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL); 626 bitmap_bit_p (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
579 627
580 FOR_EACH_BB (bb) 628 FOR_EACH_BB_FN (bb, cfun)
581 if (STATE (bb) == BLOCK_TO_SPLIT) 629 if (STATE (bb) == BLOCK_TO_SPLIT)
582 find_bb_boundaries (bb); 630 {
583 631 int n = last_basic_block_for_fn (cfun);
584 FOR_EACH_BB (bb) 632 unsigned int ns = EDGE_COUNT (bb->succs);
633
634 find_bb_boundaries (bb);
635 if (n == last_basic_block_for_fn (cfun) && ns == EDGE_COUNT (bb->succs))
636 n_succs[bb->index] = EDGE_COUNT (bb->succs);
637 }
638
639 FOR_EACH_BB_FN (bb, cfun)
585 if (STATE (bb) != BLOCK_ORIGINAL) 640 if (STATE (bb) != BLOCK_ORIGINAL)
586 break; 641 {
642 found = true;
643 break;
644 }
645
646 if (!found)
647 return;
587 648
588 min = max = bb; 649 min = max = bb;
589 for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb) 650 for (; bb != EXIT_BLOCK_PTR_FOR_FN (cfun); bb = bb->next_bb)
590 if (STATE (bb) != BLOCK_ORIGINAL) 651 if (STATE (bb) != BLOCK_ORIGINAL)
591 max = bb; 652 max = bb;
592 653
593 /* Now re-scan and wire in all edges. This expect simple (conditional) 654 /* Now re-scan and wire in all edges. This expect simple (conditional)
594 jumps at the end of each new basic blocks. */ 655 jumps at the end of each new basic blocks. */
595 make_edges (min, max, 1); 656 make_edges (min, max, 1);
596 657
597 /* Update branch probabilities. Expect only (un)conditional jumps 658 /* Update branch probabilities. Expect only (un)conditional jumps
598 to be created with only the forward edges. */ 659 to be created with only the forward edges. */
599 if (profile_status != PROFILE_ABSENT) 660 if (profile_status_for_fn (cfun) != PROFILE_ABSENT)
600 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb) 661 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
601 { 662 {
602 edge e; 663 edge e;
603 edge_iterator ei; 664 edge_iterator ei;
604 665
605 if (STATE (bb) == BLOCK_ORIGINAL) 666 if (STATE (bb) == BLOCK_ORIGINAL)
606 continue; 667 continue;
607 if (STATE (bb) == BLOCK_NEW) 668 if (STATE (bb) == BLOCK_NEW)
608 { 669 {
609 bb->count = 0; 670 bool initialized_src = false, uninitialized_src = false;
671 bb->count = profile_count::zero ();
610 bb->frequency = 0; 672 bb->frequency = 0;
611 FOR_EACH_EDGE (e, ei, bb->preds) 673 FOR_EACH_EDGE (e, ei, bb->preds)
612 { 674 {
613 bb->count += e->count; 675 if (e->count ().initialized_p ())
614 bb->frequency += EDGE_FREQUENCY (e); 676 {
677 bb->count += e->count ();
678 initialized_src = true;
679 }
680 else
681 uninitialized_src = true;
682 if (e->probability.initialized_p ())
683 bb->frequency += EDGE_FREQUENCY (e);
615 } 684 }
685 /* When some edges are missing with read profile, this is
686 most likely because RTL expansion introduced loop.
687 When profile is guessed we may have BB that is reachable
688 from unlikely path as well as from normal path.
689
690 TODO: We should handle loops created during BB expansion
691 correctly here. For now we assume all those loop to cycle
692 precisely once. */
693 if (!initialized_src
694 || (uninitialized_src
695 && profile_status_for_fn (cfun) != PROFILE_READ))
696 bb->count = profile_count::uninitialized ();
697 }
698 /* If nothing changed, there is no need to create new BBs. */
699 else if (EDGE_COUNT (bb->succs) == n_succs[bb->index])
700 {
701 /* In rare occassions RTL expansion might have mistakely assigned
702 a probabilities different from what is in CFG. This happens
703 when we try to split branch to two but optimize out the
704 second branch during the way. See PR81030. */
705 if (JUMP_P (BB_END (bb)) && any_condjump_p (BB_END (bb))
706 && EDGE_COUNT (bb->succs) >= 2)
707 update_br_prob_note (bb);
708 continue;
616 } 709 }
617 710
618 compute_outgoing_frequencies (bb); 711 compute_outgoing_frequencies (bb);
619 } 712 }
620 713
621 FOR_EACH_BB (bb) 714 FOR_EACH_BB_FN (bb, cfun)
622 SET_STATE (bb, 0); 715 SET_STATE (bb, 0);
623 } 716 }