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