]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cfgbuild.c
a21c726e3c7dd2059540ec76cb511591e4b2acae
[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 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 (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 || !JUMP_P (NEXT_INSN (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 gcc_unreachable ();
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 gcc_unreachable ();
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 ((LABEL_P (insn) || BARRIER_P (insn))
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 gcc_assert (LABEL_P (label));
187
188 /* If the label was never emitted, this insn is junk, but avoid a
189 crash trying to refer to BLOCK_FOR_INSN (label). This can happen
190 as a result of a syntax error and a diagnostic has already been
191 printed. */
192
193 if (INSN_UID (label) == 0)
194 return;
195
196 cached_make_edge (edge_cache, src, BLOCK_FOR_INSN (label), flags);
197 }
198
199 /* Create the edges generated by INSN in REGION. */
200
201 void
202 rtl_make_eh_edge (sbitmap *edge_cache, basic_block src, rtx insn)
203 {
204 int is_call = CALL_P (insn) ? EDGE_ABNORMAL_CALL : 0;
205 rtx handlers, i;
206
207 handlers = reachable_handlers (insn);
208
209 for (i = handlers; i; i = XEXP (i, 1))
210 make_label_edge (edge_cache, src, XEXP (i, 0),
211 EDGE_ABNORMAL | EDGE_EH | is_call);
212
213 free_INSN_LIST_list (&handlers);
214 }
215
216 /* Identify the edges between basic blocks MIN to MAX.
217
218 NONLOCAL_LABEL_LIST is a list of non-local labels in the function. Blocks
219 that are otherwise unreachable may be reachable with a non-local goto.
220
221 BB_EH_END is an array indexed by basic block number in which we record
222 the list of exception regions active at the end of the basic block. */
223
224 static void
225 make_edges (basic_block min, basic_block max, int update_p)
226 {
227 basic_block bb;
228 sbitmap *edge_cache = NULL;
229
230 /* Assume no computed jump; revise as we create edges. */
231 current_function_has_computed_jump = 0;
232
233 /* If we are partitioning hot and cold basic blocks into separate
234 sections, we cannot assume there is no computed jump (partitioning
235 sometimes requires the use of indirect jumps; see comments about
236 partitioning at the top of bb-reorder.c:partition_hot_cold_basic_blocks
237 for complete details). */
238
239 if (flag_reorder_blocks_and_partition)
240 current_function_has_computed_jump = 1;
241
242 /* Heavy use of computed goto in machine-generated code can lead to
243 nearly fully-connected CFGs. In that case we spend a significant
244 amount of time searching the edge lists for duplicates. */
245 if (forced_labels || cfun->max_jumptable_ents > 100)
246 {
247 edge_cache = sbitmap_vector_alloc (last_basic_block, last_basic_block);
248 sbitmap_vector_zero (edge_cache, last_basic_block);
249
250 if (update_p)
251 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
252 {
253 edge e;
254 edge_iterator ei;
255
256 FOR_EACH_EDGE (e, ei, bb->succs)
257 if (e->dest != EXIT_BLOCK_PTR)
258 SET_BIT (edge_cache[bb->index], e->dest->index);
259 }
260 }
261
262 /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
263 is always the entry. */
264 if (min == ENTRY_BLOCK_PTR->next_bb)
265 cached_make_edge (edge_cache, ENTRY_BLOCK_PTR, min,
266 EDGE_FALLTHRU);
267
268 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
269 {
270 rtx insn, x;
271 enum rtx_code code;
272 edge e;
273
274 if (LABEL_P (BB_HEAD (bb))
275 && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
276 cached_make_edge (NULL, ENTRY_BLOCK_PTR, bb, 0);
277
278 /* Examine the last instruction of the block, and discover the
279 ways we can leave the block. */
280
281 insn = BB_END (bb);
282 code = GET_CODE (insn);
283
284 /* A branch. */
285 if (code == JUMP_INSN)
286 {
287 rtx tmp;
288
289 /* Recognize exception handling placeholders. */
290 if (GET_CODE (PATTERN (insn)) == RESX)
291 rtl_make_eh_edge (edge_cache, bb, insn);
292
293 /* Recognize a non-local goto as a branch outside the
294 current function. */
295 else if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
296 ;
297
298 /* Recognize a tablejump and do the right thing. */
299 else if (tablejump_p (insn, NULL, &tmp))
300 {
301 rtvec vec;
302 int j;
303
304 if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
305 vec = XVEC (PATTERN (tmp), 0);
306 else
307 vec = XVEC (PATTERN (tmp), 1);
308
309 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
310 make_label_edge (edge_cache, bb,
311 XEXP (RTVEC_ELT (vec, j), 0), 0);
312
313 /* Some targets (eg, ARM) emit a conditional jump that also
314 contains the out-of-range target. Scan for these and
315 add an edge if necessary. */
316 if ((tmp = single_set (insn)) != NULL
317 && SET_DEST (tmp) == pc_rtx
318 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
319 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
320 make_label_edge (edge_cache, bb,
321 XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
322 }
323
324 /* If this is a computed jump, then mark it as reaching
325 everything on the forced_labels list. */
326 else if (computed_jump_p (insn))
327 {
328 current_function_has_computed_jump = 1;
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 and NREGS the number of register
500 numbers in use. */
501
502 void
503 find_basic_blocks (rtx f, int nregs ATTRIBUTE_UNUSED,
504 FILE *file ATTRIBUTE_UNUSED)
505 {
506 basic_block bb;
507
508 timevar_push (TV_CFG);
509
510 /* Flush out existing data. */
511 if (basic_block_info != NULL)
512 {
513 clear_edges ();
514
515 /* Clear bb->aux on all extant basic blocks. We'll use this as a
516 tag for reuse during create_basic_block, just in case some pass
517 copies around basic block notes improperly. */
518 FOR_EACH_BB (bb)
519 bb->aux = NULL;
520
521 basic_block_info = NULL;
522 }
523
524 n_basic_blocks = count_basic_blocks (f);
525 last_basic_block = 0;
526 ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
527 EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
528
529 /* Size the basic block table. The actual structures will be allocated
530 by find_basic_blocks_1, since we want to keep the structure pointers
531 stable across calls to find_basic_blocks. */
532 /* ??? This whole issue would be much simpler if we called find_basic_blocks
533 exactly once, and thereafter we don't have a single long chain of
534 instructions at all until close to the end of compilation when we
535 actually lay them out. */
536
537 VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
538
539 find_basic_blocks_1 (f);
540
541 profile_status = PROFILE_ABSENT;
542
543 /* Discover the edges of our cfg. */
544 make_edges (ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, 0);
545
546 /* Do very simple cleanup now, for the benefit of code that runs between
547 here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns. */
548 tidy_fallthru_edges ();
549
550 #ifdef ENABLE_CHECKING
551 verify_flow_info ();
552 #endif
553 timevar_pop (TV_CFG);
554 }
555 \f
556 /* State of basic block as seen by find_sub_basic_blocks. */
557 enum state {BLOCK_NEW = 0, BLOCK_ORIGINAL, BLOCK_TO_SPLIT};
558
559 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
560 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
561
562 /* Used internally by purge_dead_tablejump_edges, ORed into state. */
563 #define BLOCK_USED_BY_TABLEJUMP 32
564 #define FULL_STATE(BB) ((size_t) (BB)->aux)
565
566 static void
567 mark_tablejump_edge (rtx label)
568 {
569 basic_block bb;
570
571 gcc_assert (LABEL_P (label));
572 /* See comment in make_label_edge. */
573 if (INSN_UID (label) == 0)
574 return;
575 bb = BLOCK_FOR_INSN (label);
576 SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
577 }
578
579 static void
580 purge_dead_tablejump_edges (basic_block bb, rtx table)
581 {
582 rtx insn = BB_END (bb), tmp;
583 rtvec vec;
584 int j;
585 edge_iterator ei;
586 edge e;
587
588 if (GET_CODE (PATTERN (table)) == ADDR_VEC)
589 vec = XVEC (PATTERN (table), 0);
590 else
591 vec = XVEC (PATTERN (table), 1);
592
593 for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
594 mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
595
596 /* Some targets (eg, ARM) emit a conditional jump that also
597 contains the out-of-range target. Scan for these and
598 add an edge if necessary. */
599 if ((tmp = single_set (insn)) != NULL
600 && SET_DEST (tmp) == pc_rtx
601 && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
602 && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
603 mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
604
605 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
606 {
607 if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
608 SET_STATE (e->dest, FULL_STATE (e->dest)
609 & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
610 else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
611 {
612 remove_edge (e);
613 continue;
614 }
615 ei_next (&ei);
616 }
617 }
618
619 /* Scan basic block BB for possible BB boundaries inside the block
620 and create new basic blocks in the progress. */
621
622 static void
623 find_bb_boundaries (basic_block bb)
624 {
625 basic_block orig_bb = bb;
626 rtx insn = BB_HEAD (bb);
627 rtx end = BB_END (bb);
628 rtx table;
629 rtx flow_transfer_insn = NULL_RTX;
630 edge fallthru = NULL;
631
632 if (insn == BB_END (bb))
633 return;
634
635 if (LABEL_P (insn))
636 insn = NEXT_INSN (insn);
637
638 /* Scan insn chain and try to find new basic block boundaries. */
639 while (1)
640 {
641 enum rtx_code code = GET_CODE (insn);
642
643 /* On code label, split current basic block. */
644 if (code == CODE_LABEL)
645 {
646 fallthru = split_block (bb, PREV_INSN (insn));
647 if (flow_transfer_insn)
648 BB_END (bb) = flow_transfer_insn;
649
650 bb = fallthru->dest;
651 remove_edge (fallthru);
652 flow_transfer_insn = NULL_RTX;
653 if (LABEL_ALT_ENTRY_P (insn))
654 make_edge (ENTRY_BLOCK_PTR, bb, 0);
655 }
656
657 /* In case we've previously seen an insn that effects a control
658 flow transfer, split the block. */
659 if (flow_transfer_insn && inside_basic_block_p (insn))
660 {
661 fallthru = split_block (bb, PREV_INSN (insn));
662 BB_END (bb) = flow_transfer_insn;
663 bb = fallthru->dest;
664 remove_edge (fallthru);
665 flow_transfer_insn = NULL_RTX;
666 }
667
668 if (control_flow_insn_p (insn))
669 flow_transfer_insn = insn;
670 if (insn == end)
671 break;
672 insn = NEXT_INSN (insn);
673 }
674
675 /* In case expander replaced normal insn by sequence terminating by
676 return and barrier, or possibly other sequence not behaving like
677 ordinary jump, we need to take care and move basic block boundary. */
678 if (flow_transfer_insn)
679 BB_END (bb) = flow_transfer_insn;
680
681 /* We've possibly replaced the conditional jump by conditional jump
682 followed by cleanup at fallthru edge, so the outgoing edges may
683 be dead. */
684 purge_dead_edges (bb);
685
686 /* purge_dead_edges doesn't handle tablejump's, but if we have split the
687 basic block, we might need to kill some edges. */
688 if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
689 purge_dead_tablejump_edges (bb, table);
690 }
691
692 /* Assume that frequency of basic block B is known. Compute frequencies
693 and probabilities of outgoing edges. */
694
695 static void
696 compute_outgoing_frequencies (basic_block b)
697 {
698 edge e, f;
699 edge_iterator ei;
700
701 if (EDGE_COUNT (b->succs) == 2)
702 {
703 rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
704 int probability;
705
706 if (note)
707 {
708 probability = INTVAL (XEXP (note, 0));
709 e = BRANCH_EDGE (b);
710 e->probability = probability;
711 e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
712 / REG_BR_PROB_BASE);
713 f = FALLTHRU_EDGE (b);
714 f->probability = REG_BR_PROB_BASE - probability;
715 f->count = b->count - e->count;
716 return;
717 }
718 }
719
720 if (EDGE_COUNT (b->succs) == 1)
721 {
722 e = EDGE_SUCC (b, 0);
723 e->probability = REG_BR_PROB_BASE;
724 e->count = b->count;
725 return;
726 }
727 guess_outgoing_edge_probabilities (b);
728 if (b->count)
729 FOR_EACH_EDGE (e, ei, b->succs)
730 e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2)
731 / REG_BR_PROB_BASE);
732 }
733
734 /* Assume that someone emitted code with control flow instructions to the
735 basic block. Update the data structure. */
736
737 void
738 find_many_sub_basic_blocks (sbitmap blocks)
739 {
740 basic_block bb, min, max;
741
742 FOR_EACH_BB (bb)
743 SET_STATE (bb,
744 TEST_BIT (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
745
746 FOR_EACH_BB (bb)
747 if (STATE (bb) == BLOCK_TO_SPLIT)
748 find_bb_boundaries (bb);
749
750 FOR_EACH_BB (bb)
751 if (STATE (bb) != BLOCK_ORIGINAL)
752 break;
753
754 min = max = bb;
755 for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
756 if (STATE (bb) != BLOCK_ORIGINAL)
757 max = bb;
758
759 /* Now re-scan and wire in all edges. This expect simple (conditional)
760 jumps at the end of each new basic blocks. */
761 make_edges (min, max, 1);
762
763 /* Update branch probabilities. Expect only (un)conditional jumps
764 to be created with only the forward edges. */
765 if (profile_status != PROFILE_ABSENT)
766 FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
767 {
768 edge e;
769 edge_iterator ei;
770
771 if (STATE (bb) == BLOCK_ORIGINAL)
772 continue;
773 if (STATE (bb) == BLOCK_NEW)
774 {
775 bb->count = 0;
776 bb->frequency = 0;
777 FOR_EACH_EDGE (e, ei, bb->preds)
778 {
779 bb->count += e->count;
780 bb->frequency += EDGE_FREQUENCY (e);
781 }
782 }
783
784 compute_outgoing_frequencies (bb);
785 }
786
787 FOR_EACH_BB (bb)
788 SET_STATE (bb, 0);
789 }
790
791 /* Like above but for single basic block only. */
792
793 void
794 find_sub_basic_blocks (basic_block bb)
795 {
796 basic_block min, max, b;
797 basic_block next = bb->next_bb;
798
799 min = bb;
800 find_bb_boundaries (bb);
801 max = next->prev_bb;
802
803 /* Now re-scan and wire in all edges. This expect simple (conditional)
804 jumps at the end of each new basic blocks. */
805 make_edges (min, max, 1);
806
807 /* Update branch probabilities. Expect only (un)conditional jumps
808 to be created with only the forward edges. */
809 FOR_BB_BETWEEN (b, min, max->next_bb, next_bb)
810 {
811 edge e;
812 edge_iterator ei;
813
814 if (b != min)
815 {
816 b->count = 0;
817 b->frequency = 0;
818 FOR_EACH_EDGE (e, ei, b->preds)
819 {
820 b->count += e->count;
821 b->frequency += EDGE_FREQUENCY (e);
822 }
823 }
824
825 compute_outgoing_frequencies (b);
826 }
827 }