OSDN Git Service

* cse.c (cse_reg_info_free_list, cse_reg_info_used_list,
[pf3gnuchains/gcc-fork.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      - 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   /* Heavy use of computed goto in machine-generated code can lead to
231      nearly fully-connected CFGs.  In that case we spend a significant
232      amount of time searching the edge lists for duplicates.  */
233   if (forced_labels || cfun->max_jumptable_ents > 100)
234     {
235       edge_cache = sbitmap_vector_alloc (last_basic_block, last_basic_block);
236       sbitmap_vector_zero (edge_cache, last_basic_block);
237
238       if (update_p)
239         FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
240           {
241             edge e;
242             edge_iterator ei;
243
244             FOR_EACH_EDGE (e, ei, bb->succs)
245               if (e->dest != EXIT_BLOCK_PTR)
246                 SET_BIT (edge_cache[bb->index], e->dest->index);
247           }
248     }
249
250   /* By nature of the way these get numbered, ENTRY_BLOCK_PTR->next_bb block
251      is always the entry.  */
252   if (min == ENTRY_BLOCK_PTR->next_bb)
253     cached_make_edge (edge_cache, ENTRY_BLOCK_PTR, min,
254                       EDGE_FALLTHRU);
255
256   FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
257     {
258       rtx insn, x;
259       enum rtx_code code;
260       edge e;
261
262       if (LABEL_P (BB_HEAD (bb))
263           && LABEL_ALT_ENTRY_P (BB_HEAD (bb)))
264         cached_make_edge (NULL, ENTRY_BLOCK_PTR, bb, 0);
265
266       /* Examine the last instruction of the block, and discover the
267          ways we can leave the block.  */
268
269       insn = BB_END (bb);
270       code = GET_CODE (insn);
271
272       /* A branch.  */
273       if (code == JUMP_INSN)
274         {
275           rtx tmp;
276
277           /* Recognize exception handling placeholders.  */
278           if (GET_CODE (PATTERN (insn)) == RESX)
279             rtl_make_eh_edge (edge_cache, bb, insn);
280
281           /* Recognize a non-local goto as a branch outside the
282              current function.  */
283           else if (find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
284             ;
285
286           /* Recognize a tablejump and do the right thing.  */
287           else if (tablejump_p (insn, NULL, &tmp))
288             {
289               rtvec vec;
290               int j;
291
292               if (GET_CODE (PATTERN (tmp)) == ADDR_VEC)
293                 vec = XVEC (PATTERN (tmp), 0);
294               else
295                 vec = XVEC (PATTERN (tmp), 1);
296
297               for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
298                 make_label_edge (edge_cache, bb,
299                                  XEXP (RTVEC_ELT (vec, j), 0), 0);
300
301               /* Some targets (eg, ARM) emit a conditional jump that also
302                  contains the out-of-range target.  Scan for these and
303                  add an edge if necessary.  */
304               if ((tmp = single_set (insn)) != NULL
305                   && SET_DEST (tmp) == pc_rtx
306                   && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
307                   && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
308                 make_label_edge (edge_cache, bb,
309                                  XEXP (XEXP (SET_SRC (tmp), 2), 0), 0);
310             }
311
312           /* If this is a computed jump, then mark it as reaching
313              everything on the forced_labels list.  */
314           else if (computed_jump_p (insn))
315             {
316               for (x = forced_labels; x; x = XEXP (x, 1))
317                 make_label_edge (edge_cache, bb, XEXP (x, 0), EDGE_ABNORMAL);
318             }
319
320           /* Returns create an exit out.  */
321           else if (returnjump_p (insn))
322             cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, 0);
323
324           /* Otherwise, we have a plain conditional or unconditional jump.  */
325           else
326             {
327               gcc_assert (JUMP_LABEL (insn));
328               make_label_edge (edge_cache, bb, JUMP_LABEL (insn), 0);
329             }
330         }
331
332       /* If this is a sibling call insn, then this is in effect a combined call
333          and return, and so we need an edge to the exit block.  No need to
334          worry about EH edges, since we wouldn't have created the sibling call
335          in the first place.  */
336       if (code == CALL_INSN && SIBLING_CALL_P (insn))
337         cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR,
338                           EDGE_SIBCALL | EDGE_ABNORMAL);
339
340       /* If this is a CALL_INSN, then mark it as reaching the active EH
341          handler for this CALL_INSN.  If we're handling non-call
342          exceptions then any insn can reach any of the active handlers.
343          Also mark the CALL_INSN as reaching any nonlocal goto handler.  */
344       else if (code == CALL_INSN || flag_non_call_exceptions)
345         {
346           /* Add any appropriate EH edges.  */
347           rtl_make_eh_edge (edge_cache, bb, insn);
348
349           if (code == CALL_INSN && nonlocal_goto_handler_labels)
350             {
351               /* ??? This could be made smarter: in some cases it's possible
352                  to tell that certain calls will not do a nonlocal goto.
353                  For example, if the nested functions that do the nonlocal
354                  gotos do not have their addresses taken, then only calls to
355                  those functions or to other nested functions that use them
356                  could possibly do nonlocal gotos.  */
357
358               /* We do know that a REG_EH_REGION note with a value less
359                  than 0 is guaranteed not to perform a non-local goto.  */
360               rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
361
362               if (!note || INTVAL (XEXP (note, 0)) >=  0)
363                 for (x = nonlocal_goto_handler_labels; x; x = XEXP (x, 1))
364                   make_label_edge (edge_cache, bb, XEXP (x, 0),
365                                    EDGE_ABNORMAL | EDGE_ABNORMAL_CALL);
366             }
367         }
368
369       /* Find out if we can drop through to the next block.  */
370       insn = NEXT_INSN (insn);
371       e = find_edge (bb, EXIT_BLOCK_PTR);
372       if (e && e->flags & EDGE_FALLTHRU)
373         insn = NULL;
374
375       while (insn
376              && NOTE_P (insn)
377              && NOTE_LINE_NUMBER (insn) != NOTE_INSN_BASIC_BLOCK)
378         insn = NEXT_INSN (insn);
379
380       if (!insn)
381         cached_make_edge (edge_cache, bb, EXIT_BLOCK_PTR, EDGE_FALLTHRU);
382       else if (bb->next_bb != EXIT_BLOCK_PTR)
383         {
384           if (insn == BB_HEAD (bb->next_bb))
385             cached_make_edge (edge_cache, bb, bb->next_bb, EDGE_FALLTHRU);
386         }
387     }
388
389   if (edge_cache)
390     sbitmap_vector_free (edge_cache);
391 }
392 \f
393 /* Find all basic blocks of the function whose first insn is F.
394
395    Collect and return a list of labels whose addresses are taken.  This
396    will be used in make_edges for use with computed gotos.  */
397
398 static void
399 find_basic_blocks_1 (rtx f)
400 {
401   rtx insn, next;
402   rtx bb_note = NULL_RTX;
403   rtx head = NULL_RTX;
404   rtx end = NULL_RTX;
405   basic_block prev = ENTRY_BLOCK_PTR;
406
407   /* We process the instructions in a slightly different way than we did
408      previously.  This is so that we see a NOTE_BASIC_BLOCK after we have
409      closed out the previous block, so that it gets attached at the proper
410      place.  Since this form should be equivalent to the previous,
411      count_basic_blocks continues to use the old form as a check.  */
412
413   for (insn = f; insn; insn = next)
414     {
415       enum rtx_code code = GET_CODE (insn);
416
417       next = NEXT_INSN (insn);
418
419       if ((LABEL_P (insn) || BARRIER_P (insn))
420           && head)
421         {
422           prev = create_basic_block_structure (head, end, bb_note, prev);
423           head = end = NULL_RTX;
424           bb_note = NULL_RTX;
425         }
426
427       if (inside_basic_block_p (insn))
428         {
429           if (head == NULL_RTX)
430             head = insn;
431           end = insn;
432         }
433
434       if (head && control_flow_insn_p (insn))
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       switch (code)
442         {
443         case NOTE:
444           {
445             int kind = NOTE_LINE_NUMBER (insn);
446
447             /* Look for basic block notes with which to keep the
448                basic_block_info pointers stable.  Unthread the note now;
449                we'll put it back at the right place in create_basic_block.
450                Or not at all if we've already found a note in this block.  */
451             if (kind == NOTE_INSN_BASIC_BLOCK)
452               {
453                 if (bb_note == NULL_RTX)
454                   bb_note = insn;
455                 else
456                   next = delete_insn (insn);
457               }
458             break;
459           }
460
461         case CODE_LABEL:
462         case JUMP_INSN:
463         case CALL_INSN:
464         case INSN:
465         case BARRIER:
466           break;
467
468         default:
469           gcc_unreachable ();
470         }
471     }
472
473   if (head != NULL_RTX)
474     create_basic_block_structure (head, end, bb_note, prev);
475   else if (bb_note)
476     delete_insn (bb_note);
477
478   gcc_assert (last_basic_block == n_basic_blocks);
479
480   clear_aux_for_blocks ();
481 }
482
483
484 /* Find basic blocks of the current function.
485    F is the first insn of the function and NREGS the number of register
486    numbers in use.  */
487
488 void
489 find_basic_blocks (rtx f, int nregs ATTRIBUTE_UNUSED,
490                    FILE *file ATTRIBUTE_UNUSED)
491 {
492   basic_block bb;
493
494   timevar_push (TV_CFG);
495
496   /* Flush out existing data.  */
497   if (basic_block_info != NULL)
498     {
499       clear_edges ();
500
501       /* Clear bb->aux on all extant basic blocks.  We'll use this as a
502          tag for reuse during create_basic_block, just in case some pass
503          copies around basic block notes improperly.  */
504       FOR_EACH_BB (bb)
505         bb->aux = NULL;
506
507       basic_block_info = NULL;
508     }
509
510   n_basic_blocks = count_basic_blocks (f);
511   last_basic_block = 0;
512   ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
513   EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
514
515   /* Size the basic block table.  The actual structures will be allocated
516      by find_basic_blocks_1, since we want to keep the structure pointers
517      stable across calls to find_basic_blocks.  */
518   /* ??? This whole issue would be much simpler if we called find_basic_blocks
519      exactly once, and thereafter we don't have a single long chain of
520      instructions at all until close to the end of compilation when we
521      actually lay them out.  */
522
523   VARRAY_BB_INIT (basic_block_info, n_basic_blocks, "basic_block_info");
524
525   find_basic_blocks_1 (f);
526
527   profile_status = PROFILE_ABSENT;
528
529   /* Discover the edges of our cfg.  */
530   make_edges (ENTRY_BLOCK_PTR->next_bb, EXIT_BLOCK_PTR->prev_bb, 0);
531
532   /* Do very simple cleanup now, for the benefit of code that runs between
533      here and cleanup_cfg, e.g. thread_prologue_and_epilogue_insns.  */
534   tidy_fallthru_edges ();
535
536 #ifdef ENABLE_CHECKING
537   verify_flow_info ();
538 #endif
539   timevar_pop (TV_CFG);
540 }
541 \f
542 /* State of basic block as seen by find_sub_basic_blocks.  */
543 enum state {BLOCK_NEW = 0, BLOCK_ORIGINAL, BLOCK_TO_SPLIT};
544
545 #define STATE(BB) (enum state) ((size_t) (BB)->aux)
546 #define SET_STATE(BB, STATE) ((BB)->aux = (void *) (size_t) (STATE))
547
548 /* Used internally by purge_dead_tablejump_edges, ORed into state.  */
549 #define BLOCK_USED_BY_TABLEJUMP         32
550 #define FULL_STATE(BB) ((size_t) (BB)->aux)
551
552 static void
553 mark_tablejump_edge (rtx label)
554 {
555   basic_block bb;
556
557   gcc_assert (LABEL_P (label));
558   /* See comment in make_label_edge.  */
559   if (INSN_UID (label) == 0)
560     return;
561   bb = BLOCK_FOR_INSN (label);
562   SET_STATE (bb, FULL_STATE (bb) | BLOCK_USED_BY_TABLEJUMP);
563 }
564
565 static void
566 purge_dead_tablejump_edges (basic_block bb, rtx table)
567 {
568   rtx insn = BB_END (bb), tmp;
569   rtvec vec;
570   int j;
571   edge_iterator ei;
572   edge e;
573
574   if (GET_CODE (PATTERN (table)) == ADDR_VEC)
575     vec = XVEC (PATTERN (table), 0);
576   else
577     vec = XVEC (PATTERN (table), 1);
578
579   for (j = GET_NUM_ELEM (vec) - 1; j >= 0; --j)
580     mark_tablejump_edge (XEXP (RTVEC_ELT (vec, j), 0));
581
582   /* Some targets (eg, ARM) emit a conditional jump that also
583      contains the out-of-range target.  Scan for these and
584      add an edge if necessary.  */
585   if ((tmp = single_set (insn)) != NULL
586        && SET_DEST (tmp) == pc_rtx
587        && GET_CODE (SET_SRC (tmp)) == IF_THEN_ELSE
588        && GET_CODE (XEXP (SET_SRC (tmp), 2)) == LABEL_REF)
589     mark_tablejump_edge (XEXP (XEXP (SET_SRC (tmp), 2), 0));
590
591   for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
592     {
593       if (FULL_STATE (e->dest) & BLOCK_USED_BY_TABLEJUMP)
594         SET_STATE (e->dest, FULL_STATE (e->dest)
595                             & ~(size_t) BLOCK_USED_BY_TABLEJUMP);
596       else if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
597         {
598           remove_edge (e);
599           continue;
600         }
601       ei_next (&ei);
602     }
603 }
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   basic_block orig_bb = bb;
612   rtx insn = BB_HEAD (bb);
613   rtx end = BB_END (bb);
614   rtx table;
615   rtx flow_transfer_insn = NULL_RTX;
616   edge fallthru = NULL;
617
618   if (insn == BB_END (bb))
619     return;
620
621   if (LABEL_P (insn))
622     insn = NEXT_INSN (insn);
623
624   /* Scan insn chain and try to find new basic block boundaries.  */
625   while (1)
626     {
627       enum rtx_code code = GET_CODE (insn);
628
629       /* On code label, split current basic block.  */
630       if (code == CODE_LABEL)
631         {
632           fallthru = split_block (bb, PREV_INSN (insn));
633           if (flow_transfer_insn)
634             BB_END (bb) = flow_transfer_insn;
635
636           bb = fallthru->dest;
637           remove_edge (fallthru);
638           flow_transfer_insn = NULL_RTX;
639           if (LABEL_ALT_ENTRY_P (insn))
640             make_edge (ENTRY_BLOCK_PTR, bb, 0);
641         }
642
643       /* In case we've previously seen an insn that effects a control
644          flow transfer, split the block.  */
645       if (flow_transfer_insn && inside_basic_block_p (insn))
646         {
647           fallthru = split_block (bb, PREV_INSN (insn));
648           BB_END (bb) = flow_transfer_insn;
649           bb = fallthru->dest;
650           remove_edge (fallthru);
651           flow_transfer_insn = NULL_RTX;
652         }
653
654       if (control_flow_insn_p (insn))
655         flow_transfer_insn = insn;
656       if (insn == end)
657         break;
658       insn = NEXT_INSN (insn);
659     }
660
661   /* In case expander replaced normal insn by sequence terminating by
662      return and barrier, or possibly other sequence not behaving like
663      ordinary jump, we need to take care and move basic block boundary.  */
664   if (flow_transfer_insn)
665     BB_END (bb) = flow_transfer_insn;
666
667   /* We've possibly replaced the conditional jump by conditional jump
668      followed by cleanup at fallthru edge, so the outgoing edges may
669      be dead.  */
670   purge_dead_edges (bb);
671
672   /* purge_dead_edges doesn't handle tablejump's, but if we have split the
673      basic block, we might need to kill some edges.  */
674   if (bb != orig_bb && tablejump_p (BB_END (bb), NULL, &table))
675     purge_dead_tablejump_edges (bb, table);
676 }
677
678 /*  Assume that frequency of basic block B is known.  Compute frequencies
679     and probabilities of outgoing edges.  */
680
681 static void
682 compute_outgoing_frequencies (basic_block b)
683 {
684   edge e, f;
685   edge_iterator ei;
686
687   if (EDGE_COUNT (b->succs) == 2)
688     {
689       rtx note = find_reg_note (BB_END (b), REG_BR_PROB, NULL);
690       int probability;
691
692       if (note)
693         {
694           probability = INTVAL (XEXP (note, 0));
695           e = BRANCH_EDGE (b);
696           e->probability = probability;
697           e->count = ((b->count * probability + REG_BR_PROB_BASE / 2)
698                       / REG_BR_PROB_BASE);
699           f = FALLTHRU_EDGE (b);
700           f->probability = REG_BR_PROB_BASE - probability;
701           f->count = b->count - e->count;
702           return;
703         }
704     }
705
706   if (EDGE_COUNT (b->succs) == 1)
707     {
708       e = EDGE_SUCC (b, 0);
709       e->probability = REG_BR_PROB_BASE;
710       e->count = b->count;
711       return;
712     }
713   guess_outgoing_edge_probabilities (b);
714   if (b->count)
715     FOR_EACH_EDGE (e, ei, b->succs)
716       e->count = ((b->count * e->probability + REG_BR_PROB_BASE / 2)
717                   / REG_BR_PROB_BASE);
718 }
719
720 /* Assume that someone emitted code with control flow instructions to the
721    basic block.  Update the data structure.  */
722
723 void
724 find_many_sub_basic_blocks (sbitmap blocks)
725 {
726   basic_block bb, min, max;
727
728   FOR_EACH_BB (bb)
729     SET_STATE (bb,
730                TEST_BIT (blocks, bb->index) ? BLOCK_TO_SPLIT : BLOCK_ORIGINAL);
731
732   FOR_EACH_BB (bb)
733     if (STATE (bb) == BLOCK_TO_SPLIT)
734       find_bb_boundaries (bb);
735
736   FOR_EACH_BB (bb)
737     if (STATE (bb) != BLOCK_ORIGINAL)
738       break;
739
740   min = max = bb;
741   for (; bb != EXIT_BLOCK_PTR; bb = bb->next_bb)
742     if (STATE (bb) != BLOCK_ORIGINAL)
743       max = bb;
744
745   /* Now re-scan and wire in all edges.  This expect simple (conditional)
746      jumps at the end of each new basic blocks.  */
747   make_edges (min, max, 1);
748
749   /* Update branch probabilities.  Expect only (un)conditional jumps
750      to be created with only the forward edges.  */
751   if (profile_status != PROFILE_ABSENT)
752     FOR_BB_BETWEEN (bb, min, max->next_bb, next_bb)
753       {
754         edge e;
755         edge_iterator ei;
756
757         if (STATE (bb) == BLOCK_ORIGINAL)
758           continue;
759         if (STATE (bb) == BLOCK_NEW)
760           {
761             bb->count = 0;
762             bb->frequency = 0;
763             FOR_EACH_EDGE (e, ei, bb->preds)
764               {
765                 bb->count += e->count;
766                 bb->frequency += EDGE_FREQUENCY (e);
767               }
768           }
769
770         compute_outgoing_frequencies (bb);
771       }
772
773   FOR_EACH_BB (bb)
774     SET_STATE (bb, 0);
775 }
776
777 /* Like above but for single basic block only.  */
778
779 void
780 find_sub_basic_blocks (basic_block bb)
781 {
782   basic_block min, max, b;
783   basic_block next = bb->next_bb;
784
785   min = bb;
786   find_bb_boundaries (bb);
787   max = next->prev_bb;
788
789   /* Now re-scan and wire in all edges.  This expect simple (conditional)
790      jumps at the end of each new basic blocks.  */
791   make_edges (min, max, 1);
792
793   /* Update branch probabilities.  Expect only (un)conditional jumps
794      to be created with only the forward edges.  */
795   FOR_BB_BETWEEN (b, min, max->next_bb, next_bb)
796     {
797       edge e;
798       edge_iterator ei;
799
800       if (b != min)
801         {
802           b->count = 0;
803           b->frequency = 0;
804           FOR_EACH_EDGE (e, ei, b->preds)
805             {
806               b->count += e->count;
807               b->frequency += EDGE_FREQUENCY (e);
808             }
809         }
810
811       compute_outgoing_frequencies (b);
812     }
813 }