OSDN Git Service

2005-06-14 Doug Rupp <rupp@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / cfg.c
1 /* Control flow graph manipulation code for GNU compiler.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 /* This file contains low level functions to manipulate the CFG and
24    analyze it.  All other modules should not transform the data structure
25    directly and use abstraction instead.  The file is supposed to be
26    ordered bottom-up and should not contain any code dependent on a
27    particular intermediate language (RTL or trees).
28
29    Available functionality:
30      - Initialization/deallocation
31          init_flow, clear_edges
32      - Low level basic block manipulation
33          alloc_block, expunge_block
34      - Edge manipulation
35          make_edge, make_single_succ_edge, cached_make_edge, remove_edge
36          - Low level edge redirection (without updating instruction chain)
37              redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
38      - Dumping and debugging
39          dump_flow_info, debug_flow_info, dump_edge_info
40      - Allocation of AUX fields for basic blocks
41          alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
42      - clear_bb_flags
43      - Consistency checking
44          verify_flow_info
45      - Dumping and debugging
46          print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
47  */
48 \f
49 #include "config.h"
50 #include "system.h"
51 #include "coretypes.h"
52 #include "tm.h"
53 #include "tree.h"
54 #include "rtl.h"
55 #include "hard-reg-set.h"
56 #include "regs.h"
57 #include "flags.h"
58 #include "output.h"
59 #include "function.h"
60 #include "except.h"
61 #include "toplev.h"
62 #include "tm_p.h"
63 #include "obstack.h"
64 #include "timevar.h"
65 #include "ggc.h"
66 #include "hashtab.h"
67 #include "alloc-pool.h"
68
69 /* The obstack on which the flow graph components are allocated.  */
70
71 struct bitmap_obstack reg_obstack;
72
73 void debug_flow_info (void);
74 static void free_edge (edge);
75 \f
76 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
77
78 /* Called once at initialization time.  */
79
80 void
81 init_flow (void)
82 {
83   if (!cfun->cfg)
84     cfun->cfg = ggc_alloc_cleared (sizeof (struct control_flow_graph));
85   n_edges = 0;
86   ENTRY_BLOCK_PTR = ggc_alloc_cleared (sizeof (struct basic_block_def));
87   ENTRY_BLOCK_PTR->index = ENTRY_BLOCK;
88   EXIT_BLOCK_PTR = ggc_alloc_cleared (sizeof (struct basic_block_def));
89   EXIT_BLOCK_PTR->index = EXIT_BLOCK;
90   ENTRY_BLOCK_PTR->next_bb = EXIT_BLOCK_PTR;
91   EXIT_BLOCK_PTR->prev_bb = ENTRY_BLOCK_PTR;
92 }
93 \f
94 /* Helper function for remove_edge and clear_edges.  Frees edge structure
95    without actually unlinking it from the pred/succ lists.  */
96
97 static void
98 free_edge (edge e ATTRIBUTE_UNUSED)
99 {
100   n_edges--;
101   ggc_free (e);
102 }
103
104 /* Free the memory associated with the edge structures.  */
105
106 void
107 clear_edges (void)
108 {
109   basic_block bb;
110   edge e;
111   edge_iterator ei;
112
113   FOR_EACH_BB (bb)
114     {
115       FOR_EACH_EDGE (e, ei, bb->succs)
116         free_edge (e);
117       VEC_truncate (edge, bb->succs, 0);
118       VEC_truncate (edge, bb->preds, 0);
119     }
120
121   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
122     free_edge (e);
123   VEC_truncate (edge, EXIT_BLOCK_PTR->preds, 0);
124   VEC_truncate (edge, ENTRY_BLOCK_PTR->succs, 0);
125
126   gcc_assert (!n_edges);
127 }
128 \f
129 /* Allocate memory for basic_block.  */
130
131 basic_block
132 alloc_block (void)
133 {
134   basic_block bb;
135   bb = ggc_alloc_cleared (sizeof (*bb));
136   return bb;
137 }
138
139 /* Initialize rbi (the structure containing data used by basic block
140    duplication and reordering) for the given basic block.  */
141
142 void
143 initialize_bb_rbi (basic_block bb)
144 {
145   gcc_assert (!bb->rbi);
146   bb->rbi = ggc_alloc_cleared (sizeof (struct reorder_block_def));
147 }
148
149 /* Link block B to chain after AFTER.  */
150 void
151 link_block (basic_block b, basic_block after)
152 {
153   b->next_bb = after->next_bb;
154   b->prev_bb = after;
155   after->next_bb = b;
156   b->next_bb->prev_bb = b;
157 }
158
159 /* Unlink block B from chain.  */
160 void
161 unlink_block (basic_block b)
162 {
163   b->next_bb->prev_bb = b->prev_bb;
164   b->prev_bb->next_bb = b->next_bb;
165   b->prev_bb = NULL;
166   b->next_bb = NULL;
167 }
168
169 /* Sequentially order blocks and compact the arrays.  */
170 void
171 compact_blocks (void)
172 {
173   int i;
174   basic_block bb;
175
176   i = 0;
177   FOR_EACH_BB (bb)
178     {
179       BASIC_BLOCK (i) = bb;
180       bb->index = i;
181       i++;
182     }
183
184   gcc_assert (i == n_basic_blocks);
185
186   for (; i < last_basic_block; i++)
187     BASIC_BLOCK (i) = NULL;
188
189   last_basic_block = n_basic_blocks;
190 }
191
192 /* Remove block B from the basic block array.  */
193
194 void
195 expunge_block (basic_block b)
196 {
197   unlink_block (b);
198   BASIC_BLOCK (b->index) = NULL;
199   n_basic_blocks--;
200   /* We should be able to ggc_free here, but we are not.
201      The dead SSA_NAMES are left pointing to dead statements that are pointing
202      to dead basic blocks making garbage collector to die.
203      We should be able to release all dead SSA_NAMES and at the same time we should
204      clear out BB pointer of dead statements consistently.  */
205 }
206 \f
207 /* Connect E to E->src.  */
208
209 static inline void
210 connect_src (edge e)
211 {
212   VEC_safe_push (edge, gc, e->src->succs, e);
213 }
214
215 /* Connect E to E->dest.  */
216
217 static inline void
218 connect_dest (edge e)
219 {
220   basic_block dest = e->dest;
221   VEC_safe_push (edge, gc, dest->preds, e);
222   e->dest_idx = EDGE_COUNT (dest->preds) - 1;
223 }
224
225 /* Disconnect edge E from E->src.  */
226
227 static inline void
228 disconnect_src (edge e)
229 {
230   basic_block src = e->src;
231   edge_iterator ei;
232   edge tmp;
233
234   for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
235     {
236       if (tmp == e)
237         {
238           VEC_unordered_remove (edge, src->succs, ei.index);
239           return;
240         }
241       else
242         ei_next (&ei);
243     }
244
245   gcc_unreachable ();
246 }
247
248 /* Disconnect edge E from E->dest.  */
249
250 static inline void
251 disconnect_dest (edge e)
252 {
253   basic_block dest = e->dest;
254   unsigned int dest_idx = e->dest_idx;
255
256   VEC_unordered_remove (edge, dest->preds, dest_idx);
257
258   /* If we removed an edge in the middle of the edge vector, we need
259      to update dest_idx of the edge that moved into the "hole".  */
260   if (dest_idx < EDGE_COUNT (dest->preds))
261     EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
262 }
263
264 /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
265    created edge.  Use this only if you are sure that this edge can't
266    possibly already exist.  */
267
268 edge
269 unchecked_make_edge (basic_block src, basic_block dst, int flags)
270 {
271   edge e;
272   e = ggc_alloc_cleared (sizeof (*e));
273   n_edges++;
274
275   e->src = src;
276   e->dest = dst;
277   e->flags = flags;
278
279   connect_src (e);
280   connect_dest (e);
281
282   execute_on_growing_pred (e);
283
284   return e;
285 }
286
287 /* Create an edge connecting SRC and DST with FLAGS optionally using
288    edge cache CACHE.  Return the new edge, NULL if already exist.  */
289
290 edge
291 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
292 {
293   if (edge_cache == NULL
294       || src == ENTRY_BLOCK_PTR
295       || dst == EXIT_BLOCK_PTR)
296     return make_edge (src, dst, flags);
297
298   /* Does the requested edge already exist?  */
299   if (! TEST_BIT (edge_cache, dst->index))
300     {
301       /* The edge does not exist.  Create one and update the
302          cache.  */
303       SET_BIT (edge_cache, dst->index);
304       return unchecked_make_edge (src, dst, flags);
305     }
306
307   /* At this point, we know that the requested edge exists.  Adjust
308      flags if necessary.  */
309   if (flags)
310     {
311       edge e = find_edge (src, dst);
312       e->flags |= flags;
313     }
314
315   return NULL;
316 }
317
318 /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
319    created edge or NULL if already exist.  */
320
321 edge
322 make_edge (basic_block src, basic_block dest, int flags)
323 {
324   edge e = find_edge (src, dest);
325
326   /* Make sure we don't add duplicate edges.  */
327   if (e)
328     {
329       e->flags |= flags;
330       return NULL;
331     }
332
333   return unchecked_make_edge (src, dest, flags);
334 }
335
336 /* Create an edge connecting SRC to DEST and set probability by knowing
337    that it is the single edge leaving SRC.  */
338
339 edge
340 make_single_succ_edge (basic_block src, basic_block dest, int flags)
341 {
342   edge e = make_edge (src, dest, flags);
343
344   e->probability = REG_BR_PROB_BASE;
345   e->count = src->count;
346   return e;
347 }
348
349 /* This function will remove an edge from the flow graph.  */
350
351 void
352 remove_edge (edge e)
353 {
354   remove_predictions_associated_with_edge (e);
355   execute_on_shrinking_pred (e);
356
357   disconnect_src (e);
358   disconnect_dest (e);
359
360   free_edge (e);
361 }
362
363 /* Redirect an edge's successor from one block to another.  */
364
365 void
366 redirect_edge_succ (edge e, basic_block new_succ)
367 {
368   execute_on_shrinking_pred (e);
369
370   disconnect_dest (e);
371
372   e->dest = new_succ;
373
374   /* Reconnect the edge to the new successor block.  */
375   connect_dest (e);
376
377   execute_on_growing_pred (e);
378 }
379
380 /* Like previous but avoid possible duplicate edge.  */
381
382 edge
383 redirect_edge_succ_nodup (edge e, basic_block new_succ)
384 {
385   edge s;
386
387   s = find_edge (e->src, new_succ);
388   if (s && s != e)
389     {
390       s->flags |= e->flags;
391       s->probability += e->probability;
392       if (s->probability > REG_BR_PROB_BASE)
393         s->probability = REG_BR_PROB_BASE;
394       s->count += e->count;
395       remove_edge (e);
396       e = s;
397     }
398   else
399     redirect_edge_succ (e, new_succ);
400
401   return e;
402 }
403
404 /* Redirect an edge's predecessor from one block to another.  */
405
406 void
407 redirect_edge_pred (edge e, basic_block new_pred)
408 {
409   disconnect_src (e);
410
411   e->src = new_pred;
412
413   /* Reconnect the edge to the new predecessor block.  */
414   connect_src (e);
415 }
416
417 /* Clear all basic block flags, with the exception of partitioning.  */
418 void
419 clear_bb_flags (void)
420 {
421   basic_block bb;
422
423   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
424     bb->flags = BB_PARTITION (bb)  | (bb->flags & BB_DISABLE_SCHEDULE);
425 }
426 \f
427 /* Check the consistency of profile information.  We can't do that
428    in verify_flow_info, as the counts may get invalid for incompletely
429    solved graphs, later eliminating of conditionals or roundoff errors.
430    It is still practical to have them reported for debugging of simple
431    testcases.  */
432 void
433 check_bb_profile (basic_block bb, FILE * file)
434 {
435   edge e;
436   int sum = 0;
437   gcov_type lsum;
438   edge_iterator ei;
439
440   if (profile_status == PROFILE_ABSENT)
441     return;
442
443   if (bb != EXIT_BLOCK_PTR)
444     {
445       FOR_EACH_EDGE (e, ei, bb->succs)
446         sum += e->probability;
447       if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
448         fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
449                  sum * 100.0 / REG_BR_PROB_BASE);
450       lsum = 0;
451       FOR_EACH_EDGE (e, ei, bb->succs)
452         lsum += e->count;
453       if (EDGE_COUNT (bb->succs)
454           && (lsum - bb->count > 100 || lsum - bb->count < -100))
455         fprintf (file, "Invalid sum of outgoing counts %i, should be %i\n",
456                  (int) lsum, (int) bb->count);
457     }
458   if (bb != ENTRY_BLOCK_PTR)
459     {
460       sum = 0;
461       FOR_EACH_EDGE (e, ei, bb->preds)
462         sum += EDGE_FREQUENCY (e);
463       if (abs (sum - bb->frequency) > 100)
464         fprintf (file,
465                  "Invalid sum of incoming frequencies %i, should be %i\n",
466                  sum, bb->frequency);
467       lsum = 0;
468       FOR_EACH_EDGE (e, ei, bb->preds)
469         lsum += e->count;
470       if (lsum - bb->count > 100 || lsum - bb->count < -100)
471         fprintf (file, "Invalid sum of incoming counts %i, should be %i\n",
472                  (int) lsum, (int) bb->count);
473     }
474 }
475 \f
476 void
477 dump_flow_info (FILE *file)
478 {
479   basic_block bb;
480
481   /* There are no pseudo registers after reload.  Don't dump them.  */
482   if (reg_n_info && !reload_completed)
483     {
484       unsigned int i, max = max_reg_num ();
485       fprintf (file, "%d registers.\n", max);
486       for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
487         if (REG_N_REFS (i))
488           {
489             enum reg_class class, altclass;
490
491             fprintf (file, "\nRegister %d used %d times across %d insns",
492                      i, REG_N_REFS (i), REG_LIVE_LENGTH (i));
493             if (REG_BASIC_BLOCK (i) >= 0)
494               fprintf (file, " in block %d", REG_BASIC_BLOCK (i));
495             if (REG_N_SETS (i))
496               fprintf (file, "; set %d time%s", REG_N_SETS (i),
497                        (REG_N_SETS (i) == 1) ? "" : "s");
498             if (regno_reg_rtx[i] != NULL && REG_USERVAR_P (regno_reg_rtx[i]))
499               fprintf (file, "; user var");
500             if (REG_N_DEATHS (i) != 1)
501               fprintf (file, "; dies in %d places", REG_N_DEATHS (i));
502             if (REG_N_CALLS_CROSSED (i) == 1)
503               fprintf (file, "; crosses 1 call");
504             else if (REG_N_CALLS_CROSSED (i))
505               fprintf (file, "; crosses %d calls", REG_N_CALLS_CROSSED (i));
506             if (regno_reg_rtx[i] != NULL
507                 && PSEUDO_REGNO_BYTES (i) != UNITS_PER_WORD)
508               fprintf (file, "; %d bytes", PSEUDO_REGNO_BYTES (i));
509
510             class = reg_preferred_class (i);
511             altclass = reg_alternate_class (i);
512             if (class != GENERAL_REGS || altclass != ALL_REGS)
513               {
514                 if (altclass == ALL_REGS || class == ALL_REGS)
515                   fprintf (file, "; pref %s", reg_class_names[(int) class]);
516                 else if (altclass == NO_REGS)
517                   fprintf (file, "; %s or none", reg_class_names[(int) class]);
518                 else
519                   fprintf (file, "; pref %s, else %s",
520                            reg_class_names[(int) class],
521                            reg_class_names[(int) altclass]);
522               }
523
524             if (regno_reg_rtx[i] != NULL && REG_POINTER (regno_reg_rtx[i]))
525               fprintf (file, "; pointer");
526             fprintf (file, ".\n");
527           }
528     }
529
530   fprintf (file, "\n%d basic blocks, %d edges.\n", n_basic_blocks, n_edges);
531   FOR_EACH_BB (bb)
532     {
533       edge e;
534       edge_iterator ei;
535
536       fprintf (file, "\nBasic block %d ", bb->index);
537       fprintf (file, "prev %d, next %d, ",
538                bb->prev_bb->index, bb->next_bb->index);
539       fprintf (file, "loop_depth %d, count ", bb->loop_depth);
540       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, bb->count);
541       fprintf (file, ", freq %i", bb->frequency);
542       if (maybe_hot_bb_p (bb))
543         fprintf (file, ", maybe hot");
544       if (probably_never_executed_bb_p (bb))
545         fprintf (file, ", probably never executed");
546       fprintf (file, ".\n");
547
548       fprintf (file, "Predecessors: ");
549       FOR_EACH_EDGE (e, ei, bb->preds)
550         dump_edge_info (file, e, 0);
551
552       fprintf (file, "\nSuccessors: ");
553       FOR_EACH_EDGE (e, ei, bb->succs)
554         dump_edge_info (file, e, 1);
555
556       if (bb->global_live_at_start)
557         {
558           fprintf (file, "\nRegisters live at start:");
559           dump_regset (bb->global_live_at_start, file);
560         }
561
562       if (bb->global_live_at_end)
563         {
564           fprintf (file, "\nRegisters live at end:");
565           dump_regset (bb->global_live_at_end, file);
566         }
567
568       putc ('\n', file);
569       check_bb_profile (bb, file);
570     }
571
572   putc ('\n', file);
573 }
574
575 void
576 debug_flow_info (void)
577 {
578   dump_flow_info (stderr);
579 }
580
581 void
582 dump_edge_info (FILE *file, edge e, int do_succ)
583 {
584   basic_block side = (do_succ ? e->dest : e->src);
585
586   if (side == ENTRY_BLOCK_PTR)
587     fputs (" ENTRY", file);
588   else if (side == EXIT_BLOCK_PTR)
589     fputs (" EXIT", file);
590   else
591     fprintf (file, " %d", side->index);
592
593   if (e->probability)
594     fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
595
596   if (e->count)
597     {
598       fprintf (file, " count:");
599       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
600     }
601
602   if (e->flags)
603     {
604       static const char * const bitnames[] = {
605         "fallthru", "ab", "abcall", "eh", "fake", "dfs_back",
606         "can_fallthru", "irreducible", "sibcall", "loop_exit",
607         "true", "false", "exec"
608       };
609       int comma = 0;
610       int i, flags = e->flags;
611
612       fputs (" (", file);
613       for (i = 0; flags; i++)
614         if (flags & (1 << i))
615           {
616             flags &= ~(1 << i);
617
618             if (comma)
619               fputc (',', file);
620             if (i < (int) ARRAY_SIZE (bitnames))
621               fputs (bitnames[i], file);
622             else
623               fprintf (file, "%d", i);
624             comma = 1;
625           }
626
627       fputc (')', file);
628     }
629 }
630 \f
631 /* Simple routines to easily allocate AUX fields of basic blocks.  */
632
633 static struct obstack block_aux_obstack;
634 static void *first_block_aux_obj = 0;
635 static struct obstack edge_aux_obstack;
636 static void *first_edge_aux_obj = 0;
637
638 /* Allocate a memory block of SIZE as BB->aux.  The obstack must
639    be first initialized by alloc_aux_for_blocks.  */
640
641 inline void
642 alloc_aux_for_block (basic_block bb, int size)
643 {
644   /* Verify that aux field is clear.  */
645   gcc_assert (!bb->aux && first_block_aux_obj);
646   bb->aux = obstack_alloc (&block_aux_obstack, size);
647   memset (bb->aux, 0, size);
648 }
649
650 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
651    alloc_aux_for_block for each basic block.  */
652
653 void
654 alloc_aux_for_blocks (int size)
655 {
656   static int initialized;
657
658   if (!initialized)
659     {
660       gcc_obstack_init (&block_aux_obstack);
661       initialized = 1;
662     }
663   else
664     /* Check whether AUX data are still allocated.  */
665     gcc_assert (!first_block_aux_obj);
666   
667   first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
668   if (size)
669     {
670       basic_block bb;
671
672       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
673         alloc_aux_for_block (bb, size);
674     }
675 }
676
677 /* Clear AUX pointers of all blocks.  */
678
679 void
680 clear_aux_for_blocks (void)
681 {
682   basic_block bb;
683
684   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
685     bb->aux = NULL;
686 }
687
688 /* Free data allocated in block_aux_obstack and clear AUX pointers
689    of all blocks.  */
690
691 void
692 free_aux_for_blocks (void)
693 {
694   gcc_assert (first_block_aux_obj);
695   obstack_free (&block_aux_obstack, first_block_aux_obj);
696   first_block_aux_obj = NULL;
697
698   clear_aux_for_blocks ();
699 }
700
701 /* Allocate a memory edge of SIZE as BB->aux.  The obstack must
702    be first initialized by alloc_aux_for_edges.  */
703
704 inline void
705 alloc_aux_for_edge (edge e, int size)
706 {
707   /* Verify that aux field is clear.  */
708   gcc_assert (!e->aux && first_edge_aux_obj);
709   e->aux = obstack_alloc (&edge_aux_obstack, size);
710   memset (e->aux, 0, size);
711 }
712
713 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
714    alloc_aux_for_edge for each basic edge.  */
715
716 void
717 alloc_aux_for_edges (int size)
718 {
719   static int initialized;
720
721   if (!initialized)
722     {
723       gcc_obstack_init (&edge_aux_obstack);
724       initialized = 1;
725     }
726   else
727     /* Check whether AUX data are still allocated.  */
728     gcc_assert (!first_edge_aux_obj);
729
730   first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
731   if (size)
732     {
733       basic_block bb;
734
735       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
736         {
737           edge e;
738           edge_iterator ei;
739
740           FOR_EACH_EDGE (e, ei, bb->succs)
741             alloc_aux_for_edge (e, size);
742         }
743     }
744 }
745
746 /* Clear AUX pointers of all edges.  */
747
748 void
749 clear_aux_for_edges (void)
750 {
751   basic_block bb;
752   edge e;
753
754   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
755     {
756       edge_iterator ei;
757       FOR_EACH_EDGE (e, ei, bb->succs)
758         e->aux = NULL;
759     }
760 }
761
762 /* Free data allocated in edge_aux_obstack and clear AUX pointers
763    of all edges.  */
764
765 void
766 free_aux_for_edges (void)
767 {
768   gcc_assert (first_edge_aux_obj);
769   obstack_free (&edge_aux_obstack, first_edge_aux_obj);
770   first_edge_aux_obj = NULL;
771
772   clear_aux_for_edges ();
773 }
774
775 void
776 debug_bb (basic_block bb)
777 {
778   dump_bb (bb, stderr, 0);
779 }
780
781 basic_block
782 debug_bb_n (int n)
783 {
784   basic_block bb = BASIC_BLOCK (n);
785   dump_bb (bb, stderr, 0);
786   return bb;
787 }
788
789 /* Dumps cfg related information about basic block BB to FILE.  */
790
791 static void
792 dump_cfg_bb_info (FILE *file, basic_block bb)
793 {
794   unsigned i;
795   edge_iterator ei;
796   bool first = true;
797   static const char * const bb_bitnames[] =
798     {
799       "dirty", "new", "reachable", "visited", "irreducible_loop", "superblock"
800     };
801   const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
802   edge e;
803
804   fprintf (file, "Basic block %d", bb->index);
805   for (i = 0; i < n_bitnames; i++)
806     if (bb->flags & (1 << i))
807       {
808         if (first)
809           fprintf (file, " (");
810         else
811           fprintf (file, ", ");
812         first = false;
813         fprintf (file, bb_bitnames[i]);
814       }
815   if (!first)
816     fprintf (file, ")");
817   fprintf (file, "\n");
818
819   fprintf (file, "Predecessors: ");
820   FOR_EACH_EDGE (e, ei, bb->preds)
821     dump_edge_info (file, e, 0);
822
823   fprintf (file, "\nSuccessors: ");
824   FOR_EACH_EDGE (e, ei, bb->succs)
825     dump_edge_info (file, e, 1);
826   fprintf (file, "\n\n");
827 }
828
829 /* Dumps a brief description of cfg to FILE.  */
830
831 void
832 brief_dump_cfg (FILE *file)
833 {
834   basic_block bb;
835
836   FOR_EACH_BB (bb)
837     {
838       dump_cfg_bb_info (file, bb);
839     }
840 }
841
842 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
843    leave the block by TAKEN_EDGE.  Update profile of BB such that edge E can be
844    redirected to destination of TAKEN_EDGE. 
845
846    This function may leave the profile inconsistent in the case TAKEN_EDGE
847    frequency or count is believed to be lower than FREQUENCY or COUNT
848    respectively.  */
849 void
850 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
851                                  gcov_type count, edge taken_edge)
852 {
853   edge c;
854   int prob;
855   edge_iterator ei;
856
857   bb->count -= count;
858   if (bb->count < 0)
859     bb->count = 0;
860
861   /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
862      Watch for overflows.  */
863   if (bb->frequency)
864     prob = edge_frequency * REG_BR_PROB_BASE / bb->frequency;
865   else
866     prob = 0;
867   if (prob > taken_edge->probability)
868     {
869       if (dump_file)
870         fprintf (dump_file, "Jump threading proved probability of edge "
871                  "%i->%i too small (it is %i, should be %i).\n",
872                  taken_edge->src->index, taken_edge->dest->index,
873                  taken_edge->probability, prob);
874       prob = taken_edge->probability;
875     }
876
877   /* Now rescale the probabilities.  */
878   taken_edge->probability -= prob;
879   prob = REG_BR_PROB_BASE - prob;
880   bb->frequency -= edge_frequency;
881   if (bb->frequency < 0)
882     bb->frequency = 0;
883   if (prob <= 0)
884     {
885       if (dump_file)
886         fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
887                  "frequency of block should end up being 0, it is %i\n",
888                  bb->index, bb->frequency);
889       EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
890       ei = ei_start (bb->succs);
891       ei_next (&ei);
892       for (; (c = ei_safe_edge (ei)); ei_next (&ei))
893         c->probability = 0;
894     }
895   else if (prob != REG_BR_PROB_BASE)
896     {
897       int scale = 65536 * REG_BR_PROB_BASE / prob;
898
899       FOR_EACH_EDGE (c, ei, bb->succs)
900         c->probability *= scale / 65536;
901     }
902
903   gcc_assert (bb == taken_edge->src);
904   taken_edge->count -= count;
905   if (taken_edge->count < 0)
906     taken_edge->count = 0;
907 }
908
909 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
910    by NUM/DEN, in int arithmetic.  May lose some accuracy.  */
911 void
912 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
913 {
914   int i;
915   edge e;
916   for (i = 0; i < nbbs; i++)
917     {
918       edge_iterator ei;
919       bbs[i]->frequency = (bbs[i]->frequency * num) / den;
920       bbs[i]->count = RDIV (bbs[i]->count * num, den);
921       FOR_EACH_EDGE (e, ei, bbs[i]->succs)
922         e->count = (e->count * num) /den;
923     }
924 }
925
926 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
927    by NUM/DEN, in gcov_type arithmetic.  More accurate than previous
928    function but considerably slower.  */
929 void
930 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num, 
931                                  gcov_type den)
932 {
933   int i;
934   edge e;
935
936   for (i = 0; i < nbbs; i++)
937     {
938       edge_iterator ei;
939       bbs[i]->frequency = (bbs[i]->frequency * num) / den;
940       bbs[i]->count = RDIV (bbs[i]->count * num, den);
941       FOR_EACH_EDGE (e, ei, bbs[i]->succs)
942         e->count = (e->count * num) /den;
943     }
944 }
945
946 /* Datastructures used to maintain mapping between basic blocks and copies.  */
947 static htab_t bb_original;
948 static htab_t bb_copy;
949 static alloc_pool original_copy_bb_pool;
950
951 struct htab_bb_copy_original_entry
952 {
953   /* Block we are attaching info to.  */
954   int index1;
955   /* Index of original or copy (depending on the hashtable) */
956   int index2;
957 };
958
959 static hashval_t
960 bb_copy_original_hash (const void *p)
961 {
962   struct htab_bb_copy_original_entry *data
963     = ((struct htab_bb_copy_original_entry *)p);
964
965   return data->index1;
966 }
967 static int
968 bb_copy_original_eq (const void *p, const void *q)
969 {
970   struct htab_bb_copy_original_entry *data
971     = ((struct htab_bb_copy_original_entry *)p);
972   struct htab_bb_copy_original_entry *data2
973     = ((struct htab_bb_copy_original_entry *)q);
974
975   return data->index1 == data2->index1;
976 }
977
978 /* Initialize the datstructures to maintain mapping between blocks and it's copies.  */
979 void
980 initialize_original_copy_tables (void)
981 {
982   gcc_assert (!original_copy_bb_pool);
983   original_copy_bb_pool
984     = create_alloc_pool ("original_copy",
985                          sizeof (struct htab_bb_copy_original_entry), 10);
986   bb_original = htab_create (10, bb_copy_original_hash,
987                              bb_copy_original_eq, NULL);
988   bb_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
989 }
990
991 /* Free the datstructures to maintain mapping between blocks and it's copies.  */
992 void
993 free_original_copy_tables (void)
994 {
995   gcc_assert (original_copy_bb_pool);
996   htab_delete (bb_copy);
997   htab_delete (bb_original);
998   free_alloc_pool (original_copy_bb_pool);
999   bb_copy = NULL;
1000   bb_original = NULL;
1001   original_copy_bb_pool = NULL;
1002 }
1003
1004 /* Set original for basic block.  Do nothing when datstructures are not
1005    intialized so passes not needing this don't need to care.  */
1006 void
1007 set_bb_original (basic_block bb, basic_block original)
1008 {
1009   if (original_copy_bb_pool)
1010     {
1011       struct htab_bb_copy_original_entry **slot;
1012       struct htab_bb_copy_original_entry key;
1013
1014       key.index1 = bb->index;
1015       slot =
1016         (struct htab_bb_copy_original_entry **) htab_find_slot (bb_original,
1017                                                                &key, INSERT);
1018       if (*slot)
1019         (*slot)->index2 = original->index;
1020       else
1021         {
1022           *slot = pool_alloc (original_copy_bb_pool);
1023           (*slot)->index1 = bb->index;
1024           (*slot)->index2 = original->index;
1025         }
1026     }
1027 }
1028
1029 /* Get the original basic block.  */
1030 basic_block
1031 get_bb_original (basic_block bb)
1032 {
1033   struct htab_bb_copy_original_entry *entry;
1034   struct htab_bb_copy_original_entry key;
1035
1036   gcc_assert (original_copy_bb_pool);
1037
1038   key.index1 = bb->index;
1039   entry = (struct htab_bb_copy_original_entry *) htab_find (bb_original, &key);
1040   if (entry)
1041     return BASIC_BLOCK (entry->index2);
1042   else
1043     return NULL;
1044 }
1045
1046 /* Set copy for basic block.  Do nothing when datstructures are not
1047    intialized so passes not needing this don't need to care.  */
1048 void
1049 set_bb_copy (basic_block bb, basic_block copy)
1050 {
1051   if (original_copy_bb_pool)
1052     {
1053       struct htab_bb_copy_original_entry **slot;
1054       struct htab_bb_copy_original_entry key;
1055
1056       key.index1 = bb->index;
1057       slot =
1058         (struct htab_bb_copy_original_entry **) htab_find_slot (bb_copy,
1059                                                                &key, INSERT);
1060       if (*slot)
1061         (*slot)->index2 = copy->index;
1062       else
1063         {
1064           *slot = pool_alloc (original_copy_bb_pool);
1065           (*slot)->index1 = bb->index;
1066           (*slot)->index2 = copy->index;
1067         }
1068     }
1069 }
1070
1071 /* Get the copy of basic block.  */
1072 basic_block
1073 get_bb_copy (basic_block bb)
1074 {
1075   struct htab_bb_copy_original_entry *entry;
1076   struct htab_bb_copy_original_entry key;
1077
1078   gcc_assert (original_copy_bb_pool);
1079
1080   key.index1 = bb->index;
1081   entry = (struct htab_bb_copy_original_entry *) htab_find (bb_copy, &key);
1082   if (entry)
1083     return BASIC_BLOCK (entry->index2);
1084   else
1085     return NULL;
1086 }