]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cfgbuild.c
a4882e2fca14f66b70911d67d527120c4f457e77
[thirdparty/gcc.git] / gcc / cfgbuild.c
1 /* Control flow graph building code for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* find_basic_blocks divides the current function's rtl into basic
23 blocks and constructs the CFG. The blocks are recorded in the
24 basic_block_info array; the CFG exists in the edge structures
25 referenced by the blocks.
26
27 find_basic_blocks also finds any unreachable loops and deletes them.
28
29 Available functionality:
30 - CFG construction
31 find_basic_blocks */
32 \f
33 #include "config.h"
34 #include "system.h"
35 #include "coretypes.h"
36 #include "tm.h"
37 #include "tree.h"
38 #include "rtl.h"
39 #include "hard-reg-set.h"
40 #include "basic-block.h"
41 #include "regs.h"
42 #include "flags.h"
43 #include "output.h"
44 #include "function.h"
45 #include "except.h"
46 #include "toplev.h"
47 #include "timevar.h"
48
49 static int count_basic_blocks (rtx);
50 static void find_basic_blocks_1 (rtx);
51 static void make_edges (basic_block, basic_block, int);
52 static void make_label_edge (sbitmap *, basic_block, rtx, int);
53 static void find_bb_boundaries (basic_block);
54 static void compute_outgoing_frequencies (basic_block);
55 \f
56 /* Return true if insn is something that should be contained inside basic
57 block. */
58
59 bool
60 inside_basic_block_p (rtx insn)
61 {
62 switch (GET_CODE (insn))
63 {
64 case CODE_LABEL:
65 /* Avoid creating of basic block for jumptables. */
66 return (NEXT_INSN (insn) == 0
67 || !JUMP_P (NEXT_INSN (insn))
68 || (GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_VEC
69 && GET_CODE (PATTERN (NEXT_INSN (insn))) != ADDR_DIFF_VEC));
70
71 case JUMP_INSN:
72 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
73 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
74
75 case CALL_INSN:
76 case INSN:
77 return true;
78
79 case BARRIER:
80 case NOTE:
81 return false;
82
83 default:
84 gcc_unreachable ();
85 }
86 }
87
88 /* Return true if INSN may cause control flow transfer, so it should be last in
89 the basic block. */
90
91 bool
92 control_flow_insn_p (rtx insn)
93 {
94 rtx note;
95
96 switch (GET_CODE (insn))
97 {
98 case NOTE:
99 case CODE_LABEL:
100 return false;
101
102 case JUMP_INSN:
103 /* Jump insn always causes control transfer except for tablejumps. */
104 return (GET_CODE (PATTERN (insn)) != ADDR_VEC
105 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC);
106
107 case CALL_INSN:
108 /* Noreturn and sibling call instructions terminate the basic blocks
109 (but only if they happen unconditionally). */
110 if ((SIBLING_CALL_P (insn)
111 || find_reg_note (insn, REG_NORETURN, 0))
112 && GET_CODE (PATTERN (insn)) != COND_EXEC)
113 return true;
114 /* Call insn may return to the nonlocal goto handler. */
115 return ((nonlocal_goto_handler_labels
116 && (0 == (note = find_reg_note (insn, REG_EH_REGION,
117 NULL_RTX))
118 || INTVAL (XEXP (note, 0)) >= 0))
119 /* Or may trap. */
120 || can_throw_internal (insn));
121
122 case INSN:
123 return (flag_non_call_exceptions && can_throw_internal (insn));
124
125 case BARRIER:
126 /* It is nonsense to reach barrier when looking for the
127 end of basic block, but before dead code is eliminated
128 this may happen. */
129 return false;
130
131 default:
132 gcc_unreachable ();
133 }
134 }
135
136 /* Count the basic blocks of the function. */
137
138 static int
139 count_basic_blocks (rtx f)
140 {
141 int count = 0;
142 bool saw_insn = false;
143 rtx insn;
144
145 for (insn = f; insn; insn = NEXT_INSN (insn))
146 {
147 /* Code labels and barriers causes current basic block to be
148 terminated at previous real insn. */
149 if ((LABEL_P (insn) || BARRIER_P (insn))
150 && saw_insn)
151 count++, saw_insn = false;
152
153 /* Start basic block if needed. */
154 if (!saw_insn && inside_basic_block_p (insn))
155 saw_insn = true;
156
157 /* Control flow insn causes current basic block to be terminated. */
158 if (saw_insn && control_flow_insn_p (insn))
159 count++, saw_insn = false;
160 }
161
162 if (saw_insn)
163 count++;
164
165 /* The rest of the compiler works a bit smoother when we don't have to
166 check for the edge case of do-nothing functions with no basic blocks. */
167 if (count == 0)
168 {
169 emit_insn (gen_rtx_USE (VOIDmode, const0_rtx));
170 count = 1;
171 }
172
173 return count;
174 }
175 \f
176 /* Create an edge between two basic blocks. FLAGS are auxiliary information
177 about the edge that is accumulated between calls. */
178
179 /* Create an edge from a basic block to a label. */
180
181 static void
182 make_label_edge (sbitmap *edge_cache, basic_block src, rtx label, int flags)
183 {
184 gcc_assert (LABEL_P (label));
185
186 /* If the label was never emitted, this insn is junk, but avoid a
187 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
188 as a result of a syntax error and a diagnostic has already been
189 printed. */
190
191 if (INSN_UID (label) == 0)
192 return;
193
194 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
195 }
196
197 /* Create the edges generated by INSN in REGION. */
198
199 void
200 rtl_make_eh_edge (sbitmap *edge_cache, basic_block src, rtx insn)
201 {
202 int is_call = CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0;
203 rtx handlers, i;
204
205 handlers = reachable_handlers (insn);
206
207 for (i = handlers; i; i = XEXP (i, 1))
208 make_label_edge (edge_cache, src, XEXP (i, 0),
209 EDGE_ABNORMAL | EDGE_EH | is_call);
210
211 free_INSN_LIST_list (&handlers);
212 }
213
214 /* State of basic block as seen by find_many_sub_basic_blocks. */
215 enum state {BLOCK_NEW = 0, BLOCK_ORIGINAL, BLOCK_TO_SPLIT};
216
217 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
218 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
219
220 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
221 #define BLOCK_USED_BY_TABLEJUMP 32
222 #define FULL_STATE(BB) ((size_t) (BB)->aux)
223
224 /* Identify the edges between basic blocks MIN to MAX.
225
226 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
227 that are otherwise unreachable may be reachable with a non-local goto.
228
229 BB_EH_END is an array indexed by basic block number in which we record
230 the list of exception regions active at the end of the basic block. */
231
232 static void
233 make_edges (basic_block min, basic_block max, int update_p)
234 {
235 basic_block bb;
236 sbitmap *edge_cache = NULL;
237
238 /* Heavy use of computed goto in machine-generated code can lead to
239 nearly fully-connected CFGs. In that case we spend a significant
240 amount of time searching the edge lists for duplicates. */
241 if (forced_labels || cfun->max_jumptable_ents > 100)
242 {
243 edge_cache = sbitmap_vector_alloc (last_basic_block, last_basic_block);
244 sbitmap_vector_zero (edge_cache, last_basic_block);
245
246 if (update_p)
247 {
248 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
249 if (STATE (bb) != BLOCK_ORIGINAL)
250 {
251 edge e;
252 edge_iterator ei;
253
254 FOR_EACH_EDGE (e, ei, bb->succs)
255 if (e->dest != EXIT_BLOCK_PTR)
256 SET_BIT (edge_cache[bb->index], e->dest->index);
257 }
258 }
259 }
260
261 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
262 is always the entry. */
263 if (min == ENTRY_BLOCK_PTR->next_bb)
264 cached_make_edge (edge_cache, ENTRY_BLOCK_PTR, min,
265 EDGE_FALLTHRU);
266
267 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
268 {
269 rtx insn, x;
270 enum rtx_code code;
271 edge e;
272
273 if (STATE (bb) == BLOCK_ORIGINAL)
274 continue;
275
276 if (LABEL_P (BB_HEAD (bb))
277 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
278 cached_make_edge (NULL, ENTRY_BLOCK_PTR, bb, 0);
279
280 /* Examine the last instruction of the block, and discover the
281 ways we can leave the block. */
282
283 insn = BB_END (bb);
284 code = GET_CODE (insn);
285
286 /* A branch. */
287 if (code == JUMP_INSN)
288 {
289 rtx tmp;
290
291 /* Recognize exception handling placeholders. */
292 if (GET_CODE (PATTERN (insn)) == RESX)
293 rtl_make_eh_edge (edge_cache, bb, insn);
294
295 /* Recognize a non-local goto as a branch outside the
296 current function. */
297 else if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
298 ;
299
300 /* Recognize a tablejump and do the right thing. */
301 else if (tablejump_p (insn, NULL, &tmp))
302 {
303 rtvec vec;
304 int j;
305
306 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
307 vec = XVEC (PATTERN (tmp), 0);
308 else
309 vec = XVEC (PATTERN (tmp), 1);
310
311 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
312 make_label_edge (edge_cache, bb,
313 XEXP (RTVEC_ELT (vec, j), 0), 0);
314
315 /* Some targets (eg, ARM) emit a conditional jump that also
316 contains the out-of-range target. Scan for these and
317 add an edge if necessary. */
318 if ((tmp = single_set (insn)) != NULL
319 && SET_DEST (tmp) == pc_rtx
320 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
321 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
322 make_label_edge (edge_cache, bb,
323 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
324 }
325
326 /* If this is a computed jump, then mark it as reaching
327 everything on the forced_labels list. */
328 else if (computed_jump_p (insn))
329 {
330 for (x = forced_labels; x; x = XEXP (x, 1))
331 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
332 }
333
334 /* Returns create an exit out. */
335 else if (returnjump_p (insn))
336 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
337
338 /* Otherwise, we have a plain conditional or unconditional jump. */
339 else
340 {
341 gcc_assert (JUMP_LABEL (insn));
342 make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
343 }
344 }
345
346 /* If this is a sibling call insn, then this is in effect a combined call
347 and return, and so we need an edge to the exit block. No need to
348 worry about EH edges, since we wouldn't have created the sibling call
349 in the first place. */
350 if (code == CALL_INSN && SIBLING_CALL_P (insn))
351 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
352 EDGE_SIBCALL | EDGE_ABNORMAL);
353
354 /* If this is a CALL_INSN, then mark it as reaching the active EH
355 handler for this CALL_INSN. If we're handling non-call
356 exceptions then any insn can reach any of the active handlers.
357 Also mark the CALL_INSN as reaching any nonlocal goto handler. */
358 else if (code == CALL_INSN || flag_non_call_exceptions)
359 {
360 /* Add any appropriate EH edges. */
361 rtl_make_eh_edge (edge_cache, bb, insn);
362
363 if (code == CALL_INSN && nonlocal_goto_handler_labels)
364 {
365 /* ??? This could be made smarter: in some cases it's possible
366 to tell that certain calls will not do a nonlocal goto.
367 For example, if the nested functions that do the nonlocal
368 gotos do not have their addresses taken, then only calls to
369 those functions or to other nested functions that use them
370 could possibly do nonlocal gotos. */
371
372 /* We do know that a REG_EH_REGION note with a value less
373 than 0 is guaranteed not to perform a non-local goto. */
374 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
375
376 if (!note || INTVAL (XEXP (note, 0)) >= 0)
377 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
378 make_label_edge (edge_cache, bb, XEXP (x, 0),
379 EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
380 }
381 }
382
383 /* Find out if we can drop through to the next block. */
384 insn = NEXT_INSN (insn);
385 e = find_edge (bb, EXIT_BLOCK_PTR);
386 if (e && e->flags & EDGE_FALLTHRU)
387 insn = NULL;
388
389 while (insn
390 && NOTE_P (insn)
391 && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK)
392 insn = NEXT_INSN (insn);
393
394 if (!insn)
395 cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
396 else if (bb->next_bb != EXIT_BLOCK_PTR)
397 {
398 if (insn == BB_HEAD (bb->next_bb))
399 cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
400 }
401 }
402
403 if (edge_cache)
404 sbitmap_vector_free (edge_cache);
405 }
406 \f
407 /* Find all basic blocks of the function whose first insn is F.
408
409 Collect and return a list of labels whose addresses are taken. This
410 will be used in make_edges for use with computed gotos. */
411
412 static void
413 find_basic_blocks_1 (rtx f)
414 {
415 rtx insn, next;
416 rtx bb_note = NULL_RTX;
417 rtx head = NULL_RTX;
418 rtx end = NULL_RTX;
419 basic_block prev = ENTRY_BLOCK_PTR;
420
421 /* We process the instructions in a slightly different way than we did
422 previously. This is so that we see a NOTE_BASIC_BLOCK after we have
423 closed out the previous block, so that it gets attached at the proper
424 place. Since this form should be equivalent to the previous,
425 count_basic_blocks continues to use the old form as a check. */
426
427 for (insn = f; insn; insn = next)
428 {
429 enum rtx_code code = GET_CODE (insn);
430
431 next = NEXT_INSN (insn);
432
433 if ((LABEL_P (insn) || BARRIER_P (insn))
434 && head)
435 {
436 prev = create_basic_block_structure (head, end, bb_note, prev);
437 head = end = NULL_RTX;
438 bb_note = NULL_RTX;
439 }
440
441 if (inside_basic_block_p (insn))
442 {
443 if (head == NULL_RTX)
444 head = insn;
445 end = insn;
446 }
447
448 if (head && control_flow_insn_p (insn))
449 {
450 prev = create_basic_block_structure (head, end, bb_note, prev);
451 head = end = NULL_RTX;
452 bb_note = NULL_RTX;
453 }
454
455 switch (code)
456 {
457 case NOTE:
458 {
459 int kind = NOTE_LINE_NUMBER (insn);
460
461 /* Look for basic block notes with which to keep the
462 basic_block_info pointers stable. Unthread the note now;
463 we'll put it back at the right place in create_basic_block.
464 Or not at all if we've already found a note in this block. */
465 if (kind == NOTE_INSN_BASIC_BLOCK)
466 {
467 if (bb_note == NULL_RTX)
468 bb_note = insn;
469 else
470 next = delete_insn (insn);
471 }
472 break;
473 }
474
475 case CODE_LABEL:
476 case JUMP_INSN:
477 case CALL_INSN:
478 case INSN:
479 case BARRIER:
480 break;
481
482 default:
483 gcc_unreachable ();
484 }
485 }
486
487 if (head != NULL_RTX)
488 create_basic_block_structure (head, end, bb_note, prev);
489 else if (bb_note)
490 delete_insn (bb_note);
491
492 gcc_assert (last_basic_block == n_basic_blocks);
493
494 clear_aux_for_blocks ();
495 }
496
497
498 /* Find basic blocks of the current function.
499 F is the first insn of the function. */
500
501 void
502 find_basic_blocks (rtx f)
503 {
504 basic_block bb;
505
506 timevar_push (TV_CFG);
507
508 /* Flush out existing data. */
509 if (basic_block_info != NULL)
510 {
511 clear_edges ();
512
513 /* Clear bb->aux on all extant basic blocks. We'll use this as a
514 tag for reuse during create_basic_block, just in case some pass
515 copies around basic block notes improperly. */
516 FOR_EACH_BB (bb)
517 bb->aux = NULL;
518
519 basic_block_info = NULL;
520 }
521
522 n_basic_blocks = count_basic_blocks (f);
523 last_basic_block = 0;
524 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
525 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
526
527 /* Size the basic block table. The actual structures will be allocated
528 by find_basic_blocks_1, since we want to keep the structure pointers
529 stable across calls to find_basic_blocks. */
530 /* ??? This whole issue would be much simpler if we called find_basic_blocks
531 exactly once, and thereafter we don't have a single long chain of
532 instructions at all until close to the end of compilation when we
533 actually lay them out. */
534
535 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
536
537 find_basic_blocks_1 (f);
538
539 profile_status = PROFILE_ABSENT;
540
541 FOR_EACH_BB (bb)
542 SET_STATE (bb, BLOCK_NEW);
543
544 /* Discover the edges of our cfg. */
545 make_edges (ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, 0);
546
547 /* Do very simple cleanup now, for the benefit of code that runs between
548 here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns. */
549 tidy_fallthru_edges ();
550
551 #ifdef ENABLE_CHECKING
552 verify_flow_info ();
553 #endif
554 timevar_pop (TV_CFG);
555 }
556 \f
557 static void
558 mark_tablejump_edge (rtx label)
559 {
560 basic_block bb;
561
562 gcc_assert (LABEL_P (label));
563 /* See comment in make_label_edge. */
564 if (INSN_UID (label) == 0)
565 return;
566 bb = BLOCK_FOR_INSN (label);
567 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
568 }
569
570 static void
571 purge_dead_tablejump_edges (basic_block bb, rtx table)
572 {
573 rtx insn = BB_END (bb), tmp;
574 rtvec vec;
575 int j;
576 edge_iterator ei;
577 edge e;
578
579 if (GET_CODE (PATTERN (table)) == ADDR_VEC)
580 vec = XVEC (PATTERN (table), 0);
581 else
582 vec = XVEC (PATTERN (table), 1);
583
584 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
585 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
586
587 /* Some targets (eg, ARM) emit a conditional jump that also
588 contains the out-of-range target. Scan for these and
589 add an edge if necessary. */
590 if ((tmp = single_set (insn)) != NULL
591 && SET_DEST (tmp) == pc_rtx
592 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
593 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
594 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
595
596 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
597 {
598 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
599 SET_STATE (e->dest, FULL_STATE (e->dest)
600 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
601 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
602 {
603 remove_edge (e);
604 continue;
605 }
606 ei_next (&ei);
607 }
608 }
609
610 /* Scan basic block BB for possible BB boundaries inside the block
611 and create new basic blocks in the progress. */
612
613 static void
614 find_bb_boundaries (basic_block bb)
615 {
616 basic_block orig_bb = bb;
617 rtx insn = BB_HEAD (bb);
618 rtx end = BB_END (bb);
619 rtx table;
620 rtx flow_transfer_insn = NULL_RTX;
621 edge fallthru = NULL;
622
623 if (insn == BB_END (bb))
624 return;
625
626 if (LABEL_P (insn))
627 insn = NEXT_INSN (insn);
628
629 /* Scan insn chain and try to find new basic block boundaries. */
630 while (1)
631 {
632 enum rtx_code code = GET_CODE (insn);
633
634 /* On code label, split current basic block. */
635 if (code == CODE_LABEL)
636 {
637 fallthru = split_block (bb, PREV_INSN (insn));
638 if (flow_transfer_insn)
639 BB_END (bb) = flow_transfer_insn;
640
641 bb = fallthru->dest;
642 remove_edge (fallthru);
643 flow_transfer_insn = NULL_RTX;
644 if (LABEL_ALT_ENTRY_P (insn))
645 make_edge (ENTRY_BLOCK_PTR, bb, 0);
646 }
647
648 /* In case we've previously seen an insn that effects a control
649 flow transfer, split the block. */
650 if (flow_transfer_insn && inside_basic_block_p (insn))
651 {
652 fallthru = split_block (bb, PREV_INSN (insn));
653 BB_END (bb) = flow_transfer_insn;
654 bb = fallthru->dest;
655 remove_edge (fallthru);
656 flow_transfer_insn = NULL_RTX;
657 }
658
659 if (control_flow_insn_p (insn))
660 flow_transfer_insn = insn;
661 if (insn == end)
662 break;
663 insn = NEXT_INSN (insn);
664 }
665
666 /* In case expander replaced normal insn by sequence terminating by
667 return and barrier, or possibly other sequence not behaving like
668 ordinary jump, we need to take care and move basic block boundary. */
669 if (flow_transfer_insn)
670 BB_END (bb) = flow_transfer_insn;
671
672 /* We've possibly replaced the conditional jump by conditional jump
673 followed by cleanup at fallthru edge, so the outgoing edges may
674 be dead. */
675 purge_dead_edges (bb);
676
677 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
678 basic block, we might need to kill some edges. */
679 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
680 purge_dead_tablejump_edges (bb, table);
681 }
682
683 /* Assume that frequency of basic block B is known. Compute frequencies
684 and probabilities of outgoing edges. */
685
686 static void
687 compute_outgoing_frequencies (basic_block b)
688 {
689 edge e, f;
690 edge_iterator ei;
691
692 if (EDGE_COUNT (b->succs) == 2)
693 {
694 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
695 int probability;
696
697 if (note)
698 {
699 probability = INTVAL (XEXP (note, 0));
700 e = BRANCH_EDGE (b);
701 e->probability = probability;
702 e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
703 / REG_BR_PROB_BASE);
704 f = FALLTHRU_EDGE (b);
705 f->probability = REG_BR_PROB_BASE - probability;
706 f->count = b->count - e->count;
707 return;
708 }
709 }
710
711 if (single_succ_p (b))
712 {
713 e = single_succ_edge (b);
714 e->probability = REG_BR_PROB_BASE;
715 e->count = b->count;
716 return;
717 }
718 guess_outgoing_edge_probabilities (b);
719 if (b->count)
720 FOR_EACH_EDGE (e, ei, b->succs)
721 e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2)
722 / REG_BR_PROB_BASE);
723 }
724
725 /* Assume that someone emitted code with control flow instructions to the
726 basic block. Update the data structure. */
727
728 void
729 find_many_sub_basic_blocks (sbitmap blocks)
730 {
731 basic_block bb, min, max;
732
733 FOR_EACH_BB (bb)
734 SET_STATE (bb,
735 TEST_BIT (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
736
737 FOR_EACH_BB (bb)
738 if (STATE (bb) == BLOCK_TO_SPLIT)
739 find_bb_boundaries (bb);
740
741 FOR_EACH_BB (bb)
742 if (STATE (bb) != BLOCK_ORIGINAL)
743 break;
744
745 min = max = bb;
746 for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
747 if (STATE (bb) != BLOCK_ORIGINAL)
748 max = bb;
749
750 /* Now re-scan and wire in all edges. This expect simple (conditional)
751 jumps at the end of each new basic blocks. */
752 make_edges (min, max, 1);
753
754 /* Update branch probabilities. Expect only (un)conditional jumps
755 to be created with only the forward edges. */
756 if (profile_status != PROFILE_ABSENT)
757 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
758 {
759 edge e;
760 edge_iterator ei;
761
762 if (STATE (bb) == BLOCK_ORIGINAL)
763 continue;
764 if (STATE (bb) == BLOCK_NEW)
765 {
766 bb->count = 0;
767 bb->frequency = 0;
768 FOR_EACH_EDGE (e, ei, bb->preds)
769 {
770 bb->count += e->count;
771 bb->frequency += EDGE_FREQUENCY (e);
772 }
773 }
774
775 compute_outgoing_frequencies (bb);
776 }
777
778 FOR_EACH_BB (bb)
779 SET_STATE (bb, 0);
780 }