fix botched commit
[platform/upstream/gcc.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, 2006, 2007, 2008, 2009, 2010
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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* This file contains low level functions to manipulate the CFG and
23    analyze it.  All other modules should not transform the data structure
24    directly and use abstraction instead.  The file is supposed to be
25    ordered bottom-up and should not contain any code dependent on a
26    particular intermediate language (RTL or trees).
27
28    Available functionality:
29      - Initialization/deallocation
30          init_flow, clear_edges
31      - Low level basic block manipulation
32          alloc_block, expunge_block
33      - Edge manipulation
34          make_edge, make_single_succ_edge, cached_make_edge, remove_edge
35          - Low level edge redirection (without updating instruction chain)
36              redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
37      - Dumping and debugging
38          dump_flow_info, debug_flow_info, dump_edge_info
39      - Allocation of AUX fields for basic blocks
40          alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
41      - clear_bb_flags
42      - Consistency checking
43          verify_flow_info
44      - Dumping and debugging
45          print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
46
47    TODO: Document these "Available functionality" functions in the files
48    that implement them.
49  */
50 \f
51 #include "config.h"
52 #include "system.h"
53 #include "coretypes.h"
54 #include "obstack.h"
55 #include "ggc.h"
56 #include "hashtab.h"
57 #include "alloc-pool.h"
58 #include "basic-block.h"
59 #include "df.h"
60 #include "cfgloop.h" /* FIXME: For struct loop.  */
61 #include "dumpfile.h"
62
63 \f
64 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
65
66 /* Called once at initialization time.  */
67
68 void
69 init_flow (struct function *the_fun)
70 {
71   if (!the_fun->cfg)
72     the_fun->cfg = ggc_alloc_cleared_control_flow_graph ();
73   n_edges_for_function (the_fun) = 0;
74   ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)
75     = ggc_alloc_cleared_basic_block_def ();
76   ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)->index = ENTRY_BLOCK;
77   EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)
78     = ggc_alloc_cleared_basic_block_def ();
79   EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)->index = EXIT_BLOCK;
80   ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun)->next_bb
81     = EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun);
82   EXIT_BLOCK_PTR_FOR_FUNCTION (the_fun)->prev_bb
83     = ENTRY_BLOCK_PTR_FOR_FUNCTION (the_fun);
84 }
85 \f
86 /* Helper function for remove_edge and clear_edges.  Frees edge structure
87    without actually removing it from the pred/succ arrays.  */
88
89 static void
90 free_edge (edge e)
91 {
92   n_edges--;
93   ggc_free (e);
94 }
95
96 /* Free the memory associated with the edge structures.  */
97
98 void
99 clear_edges (void)
100 {
101   basic_block bb;
102   edge e;
103   edge_iterator ei;
104
105   FOR_EACH_BB (bb)
106     {
107       FOR_EACH_EDGE (e, ei, bb->succs)
108         free_edge (e);
109       VEC_truncate (edge, bb->succs, 0);
110       VEC_truncate (edge, bb->preds, 0);
111     }
112
113   FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR->succs)
114     free_edge (e);
115   VEC_truncate (edge, EXIT_BLOCK_PTR->preds, 0);
116   VEC_truncate (edge, ENTRY_BLOCK_PTR->succs, 0);
117
118   gcc_assert (!n_edges);
119 }
120 \f
121 /* Allocate memory for basic_block.  */
122
123 basic_block
124 alloc_block (void)
125 {
126   basic_block bb;
127   bb = ggc_alloc_cleared_basic_block_def ();
128   return bb;
129 }
130
131 /* Link block B to chain after AFTER.  */
132 void
133 link_block (basic_block b, basic_block after)
134 {
135   b->next_bb = after->next_bb;
136   b->prev_bb = after;
137   after->next_bb = b;
138   b->next_bb->prev_bb = b;
139 }
140
141 /* Unlink block B from chain.  */
142 void
143 unlink_block (basic_block b)
144 {
145   b->next_bb->prev_bb = b->prev_bb;
146   b->prev_bb->next_bb = b->next_bb;
147   b->prev_bb = NULL;
148   b->next_bb = NULL;
149 }
150
151 /* Sequentially order blocks and compact the arrays.  */
152 void
153 compact_blocks (void)
154 {
155   int i;
156
157   SET_BASIC_BLOCK (ENTRY_BLOCK, ENTRY_BLOCK_PTR);
158   SET_BASIC_BLOCK (EXIT_BLOCK, EXIT_BLOCK_PTR);
159
160   if (df)
161     df_compact_blocks ();
162   else
163     {
164       basic_block bb;
165
166       i = NUM_FIXED_BLOCKS;
167       FOR_EACH_BB (bb)
168         {
169           SET_BASIC_BLOCK (i, bb);
170           bb->index = i;
171           i++;
172         }
173       gcc_assert (i == n_basic_blocks);
174
175       for (; i < last_basic_block; i++)
176         SET_BASIC_BLOCK (i, NULL);
177     }
178   last_basic_block = n_basic_blocks;
179 }
180
181 /* Remove block B from the basic block array.  */
182
183 void
184 expunge_block (basic_block b)
185 {
186   unlink_block (b);
187   SET_BASIC_BLOCK (b->index, NULL);
188   n_basic_blocks--;
189   /* We should be able to ggc_free here, but we are not.
190      The dead SSA_NAMES are left pointing to dead statements that are pointing
191      to dead basic blocks making garbage collector to die.
192      We should be able to release all dead SSA_NAMES and at the same time we should
193      clear out BB pointer of dead statements consistently.  */
194 }
195 \f
196 /* Connect E to E->src.  */
197
198 static inline void
199 connect_src (edge e)
200 {
201   VEC_safe_push (edge, gc, e->src->succs, e);
202   df_mark_solutions_dirty ();
203 }
204
205 /* Connect E to E->dest.  */
206
207 static inline void
208 connect_dest (edge e)
209 {
210   basic_block dest = e->dest;
211   VEC_safe_push (edge, gc, dest->preds, e);
212   e->dest_idx = EDGE_COUNT (dest->preds) - 1;
213   df_mark_solutions_dirty ();
214 }
215
216 /* Disconnect edge E from E->src.  */
217
218 static inline void
219 disconnect_src (edge e)
220 {
221   basic_block src = e->src;
222   edge_iterator ei;
223   edge tmp;
224
225   for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
226     {
227       if (tmp == e)
228         {
229           VEC_unordered_remove (edge, src->succs, ei.index);
230           df_mark_solutions_dirty ();
231           return;
232         }
233       else
234         ei_next (&ei);
235     }
236
237   gcc_unreachable ();
238 }
239
240 /* Disconnect edge E from E->dest.  */
241
242 static inline void
243 disconnect_dest (edge e)
244 {
245   basic_block dest = e->dest;
246   unsigned int dest_idx = e->dest_idx;
247
248   VEC_unordered_remove (edge, dest->preds, dest_idx);
249
250   /* If we removed an edge in the middle of the edge vector, we need
251      to update dest_idx of the edge that moved into the "hole".  */
252   if (dest_idx < EDGE_COUNT (dest->preds))
253     EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
254   df_mark_solutions_dirty ();
255 }
256
257 /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
258    created edge.  Use this only if you are sure that this edge can't
259    possibly already exist.  */
260
261 edge
262 unchecked_make_edge (basic_block src, basic_block dst, int flags)
263 {
264   edge e;
265   e = ggc_alloc_cleared_edge_def ();
266   n_edges++;
267
268   e->src = src;
269   e->dest = dst;
270   e->flags = flags;
271
272   connect_src (e);
273   connect_dest (e);
274
275   execute_on_growing_pred (e);
276   return e;
277 }
278
279 /* Create an edge connecting SRC and DST with FLAGS optionally using
280    edge cache CACHE.  Return the new edge, NULL if already exist.  */
281
282 edge
283 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
284 {
285   if (edge_cache == NULL
286       || src == ENTRY_BLOCK_PTR
287       || dst == EXIT_BLOCK_PTR)
288     return make_edge (src, dst, flags);
289
290   /* Does the requested edge already exist?  */
291   if (! TEST_BIT (edge_cache, dst->index))
292     {
293       /* The edge does not exist.  Create one and update the
294          cache.  */
295       SET_BIT (edge_cache, dst->index);
296       return unchecked_make_edge (src, dst, flags);
297     }
298
299   /* At this point, we know that the requested edge exists.  Adjust
300      flags if necessary.  */
301   if (flags)
302     {
303       edge e = find_edge (src, dst);
304       e->flags |= flags;
305     }
306
307   return NULL;
308 }
309
310 /* Create an edge connecting SRC and DEST with flags FLAGS.  Return newly
311    created edge or NULL if already exist.  */
312
313 edge
314 make_edge (basic_block src, basic_block dest, int flags)
315 {
316   edge e = find_edge (src, dest);
317
318   /* Make sure we don't add duplicate edges.  */
319   if (e)
320     {
321       e->flags |= flags;
322       return NULL;
323     }
324
325   return unchecked_make_edge (src, dest, flags);
326 }
327
328 /* Create an edge connecting SRC to DEST and set probability by knowing
329    that it is the single edge leaving SRC.  */
330
331 edge
332 make_single_succ_edge (basic_block src, basic_block dest, int flags)
333 {
334   edge e = make_edge (src, dest, flags);
335
336   e->probability = REG_BR_PROB_BASE;
337   e->count = src->count;
338   return e;
339 }
340
341 /* This function will remove an edge from the flow graph.  */
342
343 void
344 remove_edge_raw (edge e)
345 {
346   remove_predictions_associated_with_edge (e);
347   execute_on_shrinking_pred (e);
348
349   disconnect_src (e);
350   disconnect_dest (e);
351
352   free_edge (e);
353 }
354
355 /* Redirect an edge's successor from one block to another.  */
356
357 void
358 redirect_edge_succ (edge e, basic_block new_succ)
359 {
360   execute_on_shrinking_pred (e);
361
362   disconnect_dest (e);
363
364   e->dest = new_succ;
365
366   /* Reconnect the edge to the new successor block.  */
367   connect_dest (e);
368
369   execute_on_growing_pred (e);
370 }
371
372 /* Redirect an edge's predecessor from one block to another.  */
373
374 void
375 redirect_edge_pred (edge e, basic_block new_pred)
376 {
377   disconnect_src (e);
378
379   e->src = new_pred;
380
381   /* Reconnect the edge to the new predecessor block.  */
382   connect_src (e);
383 }
384
385 /* Clear all basic block flags, with the exception of partitioning and
386    setjmp_target.  */
387 void
388 clear_bb_flags (void)
389 {
390   basic_block bb;
391
392   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, NULL, next_bb)
393     bb->flags = (BB_PARTITION (bb)
394                  | (bb->flags & (BB_DISABLE_SCHEDULE + BB_RTL + BB_NON_LOCAL_GOTO_TARGET)));
395 }
396 \f
397 /* Check the consistency of profile information.  We can't do that
398    in verify_flow_info, as the counts may get invalid for incompletely
399    solved graphs, later eliminating of conditionals or roundoff errors.
400    It is still practical to have them reported for debugging of simple
401    testcases.  */
402 void
403 check_bb_profile (basic_block bb, FILE * file)
404 {
405   edge e;
406   int sum = 0;
407   gcov_type lsum;
408   edge_iterator ei;
409
410   if (profile_status == PROFILE_ABSENT)
411     return;
412
413   if (bb != EXIT_BLOCK_PTR)
414     {
415       FOR_EACH_EDGE (e, ei, bb->succs)
416         sum += e->probability;
417       if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
418         fprintf (file, "Invalid sum of outgoing probabilities %.1f%%\n",
419                  sum * 100.0 / REG_BR_PROB_BASE);
420       lsum = 0;
421       FOR_EACH_EDGE (e, ei, bb->succs)
422         lsum += e->count;
423       if (EDGE_COUNT (bb->succs)
424           && (lsum - bb->count > 100 || lsum - bb->count < -100))
425         fprintf (file, "Invalid sum of outgoing counts %i, should be %i\n",
426                  (int) lsum, (int) bb->count);
427     }
428   if (bb != ENTRY_BLOCK_PTR)
429     {
430       sum = 0;
431       FOR_EACH_EDGE (e, ei, bb->preds)
432         sum += EDGE_FREQUENCY (e);
433       if (abs (sum - bb->frequency) > 100)
434         fprintf (file,
435                  "Invalid sum of incoming frequencies %i, should be %i\n",
436                  sum, bb->frequency);
437       lsum = 0;
438       FOR_EACH_EDGE (e, ei, bb->preds)
439         lsum += e->count;
440       if (lsum - bb->count > 100 || lsum - bb->count < -100)
441         fprintf (file, "Invalid sum of incoming counts %i, should be %i\n",
442                  (int) lsum, (int) bb->count);
443     }
444 }
445 \f
446 void
447 dump_edge_info (FILE *file, edge e, int flags, int do_succ)
448 {
449   basic_block side = (do_succ ? e->dest : e->src);
450   bool do_details = false;
451   
452   if ((flags & TDF_DETAILS) != 0
453       && (flags & TDF_SLIM) == 0)
454     do_details = true;
455
456   /* ENTRY_BLOCK_PTR/EXIT_BLOCK_PTR depend on cfun.
457      Compare against ENTRY_BLOCK/EXIT_BLOCK to avoid that dependency.  */
458   if (side->index == ENTRY_BLOCK)
459     fputs (" ENTRY", file);
460   else if (side->index == EXIT_BLOCK)
461     fputs (" EXIT", file);
462   else
463     fprintf (file, " %d", side->index);
464
465   if (e->probability && do_details)
466     fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
467
468   if (e->count && do_details)
469     {
470       fputs (" count:", file);
471       fprintf (file, HOST_WIDEST_INT_PRINT_DEC, e->count);
472     }
473
474   if (e->flags && do_details)
475     {
476       static const char * const bitnames[] =
477         {
478 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
479 #include "cfg-flags.def"
480           NULL
481 #undef DEF_EDGE_FLAG
482         };
483       bool comma = false;
484       int i, flags = e->flags;
485
486       gcc_assert (e->flags <= EDGE_ALL_FLAGS);
487       fputs (" (", file);
488       for (i = 0; flags; i++)
489         if (flags & (1 << i))
490           {
491             flags &= ~(1 << i);
492
493             if (comma)
494               fputc (',', file);
495             fputs (bitnames[i], file);
496             comma = true;
497           }
498
499       fputc (')', file);
500     }
501 }
502 \f
503 /* Simple routines to easily allocate AUX fields of basic blocks.  */
504
505 static struct obstack block_aux_obstack;
506 static void *first_block_aux_obj = 0;
507 static struct obstack edge_aux_obstack;
508 static void *first_edge_aux_obj = 0;
509
510 /* Allocate a memory block of SIZE as BB->aux.  The obstack must
511    be first initialized by alloc_aux_for_blocks.  */
512
513 static void
514 alloc_aux_for_block (basic_block bb, int size)
515 {
516   /* Verify that aux field is clear.  */
517   gcc_assert (!bb->aux && first_block_aux_obj);
518   bb->aux = obstack_alloc (&block_aux_obstack, size);
519   memset (bb->aux, 0, size);
520 }
521
522 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
523    alloc_aux_for_block for each basic block.  */
524
525 void
526 alloc_aux_for_blocks (int size)
527 {
528   static int initialized;
529
530   if (!initialized)
531     {
532       gcc_obstack_init (&block_aux_obstack);
533       initialized = 1;
534     }
535   else
536     /* Check whether AUX data are still allocated.  */
537     gcc_assert (!first_block_aux_obj);
538
539   first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
540   if (size)
541     {
542       basic_block bb;
543
544       FOR_ALL_BB (bb)
545         alloc_aux_for_block (bb, size);
546     }
547 }
548
549 /* Clear AUX pointers of all blocks.  */
550
551 void
552 clear_aux_for_blocks (void)
553 {
554   basic_block bb;
555
556   FOR_ALL_BB (bb)
557     bb->aux = NULL;
558 }
559
560 /* Free data allocated in block_aux_obstack and clear AUX pointers
561    of all blocks.  */
562
563 void
564 free_aux_for_blocks (void)
565 {
566   gcc_assert (first_block_aux_obj);
567   obstack_free (&block_aux_obstack, first_block_aux_obj);
568   first_block_aux_obj = NULL;
569
570   clear_aux_for_blocks ();
571 }
572
573 /* Allocate a memory edge of SIZE as E->aux.  The obstack must
574    be first initialized by alloc_aux_for_edges.  */
575
576 void
577 alloc_aux_for_edge (edge e, int size)
578 {
579   /* Verify that aux field is clear.  */
580   gcc_assert (!e->aux && first_edge_aux_obj);
581   e->aux = obstack_alloc (&edge_aux_obstack, size);
582   memset (e->aux, 0, size);
583 }
584
585 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
586    alloc_aux_for_edge for each basic edge.  */
587
588 void
589 alloc_aux_for_edges (int size)
590 {
591   static int initialized;
592
593   if (!initialized)
594     {
595       gcc_obstack_init (&edge_aux_obstack);
596       initialized = 1;
597     }
598   else
599     /* Check whether AUX data are still allocated.  */
600     gcc_assert (!first_edge_aux_obj);
601
602   first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
603   if (size)
604     {
605       basic_block bb;
606
607       FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
608         {
609           edge e;
610           edge_iterator ei;
611
612           FOR_EACH_EDGE (e, ei, bb->succs)
613             alloc_aux_for_edge (e, size);
614         }
615     }
616 }
617
618 /* Clear AUX pointers of all edges.  */
619
620 void
621 clear_aux_for_edges (void)
622 {
623   basic_block bb;
624   edge e;
625
626   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, next_bb)
627     {
628       edge_iterator ei;
629       FOR_EACH_EDGE (e, ei, bb->succs)
630         e->aux = NULL;
631     }
632 }
633
634 /* Free data allocated in edge_aux_obstack and clear AUX pointers
635    of all edges.  */
636
637 void
638 free_aux_for_edges (void)
639 {
640   gcc_assert (first_edge_aux_obj);
641   obstack_free (&edge_aux_obstack, first_edge_aux_obj);
642   first_edge_aux_obj = NULL;
643
644   clear_aux_for_edges ();
645 }
646
647 DEBUG_FUNCTION void
648 debug_bb (basic_block bb)
649 {
650   dump_bb (stderr, bb, 0, dump_flags | TDF_BLOCKS);
651 }
652
653 DEBUG_FUNCTION basic_block
654 debug_bb_n (int n)
655 {
656   basic_block bb = BASIC_BLOCK (n);
657   debug_bb (bb);
658   return bb;
659 }
660
661 /* Dumps cfg related information about basic block BB to OUTF.
662    If HEADER is true, dump things that appear before the instructions
663    contained in BB.  If FOOTER is true, dump things that appear after.
664    Flags are the TDF_* masks as documented in dumpfile.h.
665    NB: With TDF_DETAILS, it is assumed that cfun is available, so
666    that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE.  */
667
668 void
669 dump_bb_info (FILE *outf, basic_block bb, int indent, int flags,
670               bool do_header, bool do_footer)
671 {
672   edge_iterator ei;
673   edge e;
674   static const char * const bb_bitnames[] =
675     {
676 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
677 #include "cfg-flags.def"
678       NULL
679 #undef DEF_BASIC_BLOCK_FLAG
680     };
681   const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
682   char *s_indent = (char *) alloca ((size_t) indent + 1);
683   memset ((void *) s_indent, ' ', (size_t) indent);
684   s_indent[indent] = '\0';
685
686   gcc_assert (bb->flags <= BB_ALL_FLAGS);
687
688   if (do_header)
689     {
690       unsigned i;
691
692       if (flags & TDF_COMMENT)
693         fputs (";; ", outf);
694       fprintf (outf, "%sbasic block %d", s_indent, bb->index);
695       if (flags & TDF_DETAILS)
696         {
697           fprintf (outf, ", loop depth %d, count " HOST_WIDEST_INT_PRINT_DEC,
698                    bb->loop_depth, (HOST_WIDEST_INT) bb->count);
699           fprintf (outf, ", freq %i", bb->frequency);
700           if (maybe_hot_bb_p (bb))
701             fputs (", maybe hot", outf);
702           if (probably_never_executed_bb_p (bb))
703             fputs (", probably never executed", outf);
704         }
705       fputc ('\n', outf);
706
707       if (flags & TDF_DETAILS)
708         {
709           bool first = true;
710
711           if (flags & TDF_COMMENT)
712             fputs (";; ", outf);
713           fprintf (outf, "%s prev block ", s_indent);
714           if (bb->prev_bb)
715             fprintf (outf, "%d", bb->prev_bb->index);
716           else
717             fprintf (outf, "(nil)");
718           fprintf (outf, ", next block ");
719           if (bb->next_bb)
720             fprintf (outf, "%d", bb->next_bb->index);
721           else
722             fprintf (outf, "(nil)");
723
724           fputs (", flags:", outf);
725           for (i = 0; i < n_bitnames; i++)
726             if (bb->flags & (1 << i))
727               {
728                 if (first)
729                   fputs (" (", outf);
730                 else
731                   fputs (", ", outf);
732                 first = false;
733                 fputs (bb_bitnames[i], outf);
734               }
735           if (!first)
736             fputc (')', outf);
737         }
738       fputc ('\n', outf);
739
740       if (flags & TDF_COMMENT)
741         fputs (";; ", outf);
742       fprintf (outf, "%s pred:      ", s_indent);
743       FOR_EACH_EDGE (e, ei, bb->preds)
744         dump_edge_info (outf, e, flags, 0);
745       fputc ('\n', outf);
746     }
747
748   if (do_footer)
749     {
750       if (flags & TDF_COMMENT)
751         fputs (";; ", outf);
752       fprintf (outf, "%s succ:      ", s_indent);
753       FOR_EACH_EDGE (e, ei, bb->succs)
754         dump_edge_info (outf, e, flags, 1);
755       fputs ("\n\n", outf);
756     }
757 }
758
759 /* Dumps a brief description of cfg to FILE.  */
760
761 void
762 brief_dump_cfg (FILE *file)
763 {
764   basic_block bb;
765
766   FOR_EACH_BB (bb)
767     {
768       dump_bb_info (file, bb, 0, 0, true, true);
769     }
770 }
771
772 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
773    leave the block by TAKEN_EDGE.  Update profile of BB such that edge E can be
774    redirected to destination of TAKEN_EDGE.
775
776    This function may leave the profile inconsistent in the case TAKEN_EDGE
777    frequency or count is believed to be lower than FREQUENCY or COUNT
778    respectively.  */
779 void
780 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
781                                  gcov_type count, edge taken_edge)
782 {
783   edge c;
784   int prob;
785   edge_iterator ei;
786
787   bb->count -= count;
788   if (bb->count < 0)
789     {
790       if (dump_file)
791         fprintf (dump_file, "bb %i count became negative after threading",
792                  bb->index);
793       bb->count = 0;
794     }
795
796   /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
797      Watch for overflows.  */
798   if (bb->frequency)
799     prob = edge_frequency * REG_BR_PROB_BASE / bb->frequency;
800   else
801     prob = 0;
802   if (prob > taken_edge->probability)
803     {
804       if (dump_file)
805         fprintf (dump_file, "Jump threading proved probability of edge "
806                  "%i->%i too small (it is %i, should be %i).\n",
807                  taken_edge->src->index, taken_edge->dest->index,
808                  taken_edge->probability, prob);
809       prob = taken_edge->probability;
810     }
811
812   /* Now rescale the probabilities.  */
813   taken_edge->probability -= prob;
814   prob = REG_BR_PROB_BASE - prob;
815   bb->frequency -= edge_frequency;
816   if (bb->frequency < 0)
817     bb->frequency = 0;
818   if (prob <= 0)
819     {
820       if (dump_file)
821         fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
822                  "frequency of block should end up being 0, it is %i\n",
823                  bb->index, bb->frequency);
824       EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
825       ei = ei_start (bb->succs);
826       ei_next (&ei);
827       for (; (c = ei_safe_edge (ei)); ei_next (&ei))
828         c->probability = 0;
829     }
830   else if (prob != REG_BR_PROB_BASE)
831     {
832       int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
833
834       FOR_EACH_EDGE (c, ei, bb->succs)
835         {
836           /* Protect from overflow due to additional scaling.  */
837           if (c->probability > prob)
838             c->probability = REG_BR_PROB_BASE;
839           else
840             {
841               c->probability = RDIV (c->probability * scale, 65536);
842               if (c->probability > REG_BR_PROB_BASE)
843                 c->probability = REG_BR_PROB_BASE;
844             }
845         }
846     }
847
848   gcc_assert (bb == taken_edge->src);
849   taken_edge->count -= count;
850   if (taken_edge->count < 0)
851     {
852       if (dump_file)
853         fprintf (dump_file, "edge %i->%i count became negative after threading",
854                  taken_edge->src->index, taken_edge->dest->index);
855       taken_edge->count = 0;
856     }
857 }
858
859 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
860    by NUM/DEN, in int arithmetic.  May lose some accuracy.  */
861 void
862 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
863 {
864   int i;
865   edge e;
866   if (num < 0)
867     num = 0;
868
869   /* Scale NUM and DEN to avoid overflows.  Frequencies are in order of
870      10^4, if we make DEN <= 10^3, we can afford to upscale by 100
871      and still safely fit in int during calculations.  */
872   if (den > 1000)
873     {
874       if (num > 1000000)
875         return;
876
877       num = RDIV (1000 * num, den);
878       den = 1000;
879     }
880   if (num > 100 * den)
881     return;
882
883   for (i = 0; i < nbbs; i++)
884     {
885       edge_iterator ei;
886       bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
887       /* Make sure the frequencies do not grow over BB_FREQ_MAX.  */
888       if (bbs[i]->frequency > BB_FREQ_MAX)
889         bbs[i]->frequency = BB_FREQ_MAX;
890       bbs[i]->count = RDIV (bbs[i]->count * num, den);
891       FOR_EACH_EDGE (e, ei, bbs[i]->succs)
892         e->count = RDIV (e->count * num, den);
893     }
894 }
895
896 /* numbers smaller than this value are safe to multiply without getting
897    64bit overflow.  */
898 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (HOST_WIDEST_INT) * 4 - 1))
899
900 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
901    by NUM/DEN, in gcov_type arithmetic.  More accurate than previous
902    function but considerably slower.  */
903 void
904 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
905                                  gcov_type den)
906 {
907   int i;
908   edge e;
909   gcov_type fraction = RDIV (num * 65536, den);
910
911   gcc_assert (fraction >= 0);
912
913   if (num < MAX_SAFE_MULTIPLIER)
914     for (i = 0; i < nbbs; i++)
915       {
916         edge_iterator ei;
917         bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
918         if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
919           bbs[i]->count = RDIV (bbs[i]->count * num, den);
920         else
921           bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
922         FOR_EACH_EDGE (e, ei, bbs[i]->succs)
923           if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
924             e->count = RDIV (e->count * num, den);
925           else
926             e->count = RDIV (e->count * fraction, 65536);
927       }
928    else
929     for (i = 0; i < nbbs; i++)
930       {
931         edge_iterator ei;
932         if (sizeof (gcov_type) > sizeof (int))
933           bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
934         else
935           bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
936         bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
937         FOR_EACH_EDGE (e, ei, bbs[i]->succs)
938           e->count = RDIV (e->count * fraction, 65536);
939       }
940 }
941
942 /* Data structures used to maintain mapping between basic blocks and
943    copies.  */
944 static htab_t bb_original;
945 static htab_t bb_copy;
946
947 /* And between loops and copies.  */
948 static htab_t loop_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   const struct htab_bb_copy_original_entry *data
963     = ((const 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   const struct htab_bb_copy_original_entry *data
971     = ((const struct htab_bb_copy_original_entry *)p);
972   const struct htab_bb_copy_original_entry *data2
973     = ((const struct htab_bb_copy_original_entry *)q);
974
975   return data->index1 == data2->index1;
976 }
977
978 /* Initialize the data structures to maintain mapping between blocks
979    and its copies.  */
980 void
981 initialize_original_copy_tables (void)
982 {
983   gcc_assert (!original_copy_bb_pool);
984   original_copy_bb_pool
985     = create_alloc_pool ("original_copy",
986                          sizeof (struct htab_bb_copy_original_entry), 10);
987   bb_original = htab_create (10, bb_copy_original_hash,
988                              bb_copy_original_eq, NULL);
989   bb_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
990   loop_copy = htab_create (10, bb_copy_original_hash, bb_copy_original_eq, NULL);
991 }
992
993 /* Free the data structures to maintain mapping between blocks and
994    its copies.  */
995 void
996 free_original_copy_tables (void)
997 {
998   gcc_assert (original_copy_bb_pool);
999   htab_delete (bb_copy);
1000   htab_delete (bb_original);
1001   htab_delete (loop_copy);
1002   free_alloc_pool (original_copy_bb_pool);
1003   bb_copy = NULL;
1004   bb_original = NULL;
1005   loop_copy = NULL;
1006   original_copy_bb_pool = NULL;
1007 }
1008
1009 /* Removes the value associated with OBJ from table TAB.  */
1010
1011 static void
1012 copy_original_table_clear (htab_t tab, unsigned obj)
1013 {
1014   void **slot;
1015   struct htab_bb_copy_original_entry key, *elt;
1016
1017   if (!original_copy_bb_pool)
1018     return;
1019
1020   key.index1 = obj;
1021   slot = htab_find_slot (tab, &key, NO_INSERT);
1022   if (!slot)
1023     return;
1024
1025   elt = (struct htab_bb_copy_original_entry *) *slot;
1026   htab_clear_slot (tab, slot);
1027   pool_free (original_copy_bb_pool, elt);
1028 }
1029
1030 /* Sets the value associated with OBJ in table TAB to VAL.
1031    Do nothing when data structures are not initialized.  */
1032
1033 static void
1034 copy_original_table_set (htab_t tab, unsigned obj, unsigned val)
1035 {
1036   struct htab_bb_copy_original_entry **slot;
1037   struct htab_bb_copy_original_entry key;
1038
1039   if (!original_copy_bb_pool)
1040     return;
1041
1042   key.index1 = obj;
1043   slot = (struct htab_bb_copy_original_entry **)
1044                 htab_find_slot (tab, &key, INSERT);
1045   if (!*slot)
1046     {
1047       *slot = (struct htab_bb_copy_original_entry *)
1048                 pool_alloc (original_copy_bb_pool);
1049       (*slot)->index1 = obj;
1050     }
1051   (*slot)->index2 = val;
1052 }
1053
1054 /* Set original for basic block.  Do nothing when data structures are not
1055    initialized so passes not needing this don't need to care.  */
1056 void
1057 set_bb_original (basic_block bb, basic_block original)
1058 {
1059   copy_original_table_set (bb_original, bb->index, original->index);
1060 }
1061
1062 /* Get the original basic block.  */
1063 basic_block
1064 get_bb_original (basic_block bb)
1065 {
1066   struct htab_bb_copy_original_entry *entry;
1067   struct htab_bb_copy_original_entry key;
1068
1069   gcc_assert (original_copy_bb_pool);
1070
1071   key.index1 = bb->index;
1072   entry = (struct htab_bb_copy_original_entry *) htab_find (bb_original, &key);
1073   if (entry)
1074     return BASIC_BLOCK (entry->index2);
1075   else
1076     return NULL;
1077 }
1078
1079 /* Set copy for basic block.  Do nothing when data structures are not
1080    initialized so passes not needing this don't need to care.  */
1081 void
1082 set_bb_copy (basic_block bb, basic_block copy)
1083 {
1084   copy_original_table_set (bb_copy, bb->index, copy->index);
1085 }
1086
1087 /* Get the copy of basic block.  */
1088 basic_block
1089 get_bb_copy (basic_block bb)
1090 {
1091   struct htab_bb_copy_original_entry *entry;
1092   struct htab_bb_copy_original_entry key;
1093
1094   gcc_assert (original_copy_bb_pool);
1095
1096   key.index1 = bb->index;
1097   entry = (struct htab_bb_copy_original_entry *) htab_find (bb_copy, &key);
1098   if (entry)
1099     return BASIC_BLOCK (entry->index2);
1100   else
1101     return NULL;
1102 }
1103
1104 /* Set copy for LOOP to COPY.  Do nothing when data structures are not
1105    initialized so passes not needing this don't need to care.  */
1106
1107 void
1108 set_loop_copy (struct loop *loop, struct loop *copy)
1109 {
1110   if (!copy)
1111     copy_original_table_clear (loop_copy, loop->num);
1112   else
1113     copy_original_table_set (loop_copy, loop->num, copy->num);
1114 }
1115
1116 /* Get the copy of LOOP.  */
1117
1118 struct loop *
1119 get_loop_copy (struct loop *loop)
1120 {
1121   struct htab_bb_copy_original_entry *entry;
1122   struct htab_bb_copy_original_entry key;
1123
1124   gcc_assert (original_copy_bb_pool);
1125
1126   key.index1 = loop->num;
1127   entry = (struct htab_bb_copy_original_entry *) htab_find (loop_copy, &key);
1128   if (entry)
1129     return get_loop (entry->index2);
1130   else
1131     return NULL;
1132 }