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