Daily bump.
[official-gcc.git] / gcc / cfg.c
blobfb13b7de06a19bee08477cc18027769b55cf1cae
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This file contains low level functions to manipulate the CFG and
21 analyze it. All other modules should not transform the data structure
22 directly and use abstraction instead. The file is supposed to be
23 ordered bottom-up and should not contain any code dependent on a
24 particular intermediate language (RTL or trees).
26 Available functionality:
27 - Initialization/deallocation
28 init_flow, clear_edges
29 - Low level basic block manipulation
30 alloc_block, expunge_block
31 - Edge manipulation
32 make_edge, make_single_succ_edge, cached_make_edge, remove_edge
33 - Low level edge redirection (without updating instruction chain)
34 redirect_edge_succ, redirect_edge_succ_nodup, redirect_edge_pred
35 - Dumping and debugging
36 dump_flow_info, debug_flow_info, dump_edge_info
37 - Allocation of AUX fields for basic blocks
38 alloc_aux_for_blocks, free_aux_for_blocks, alloc_aux_for_block
39 - clear_bb_flags
40 - Consistency checking
41 verify_flow_info
42 - Dumping and debugging
43 print_rtl_with_bb, dump_bb, debug_bb, debug_bb_n
45 TODO: Document these "Available functionality" functions in the files
46 that implement them.
49 #include "config.h"
50 #include "system.h"
51 #include "coretypes.h"
52 #include "obstack.h"
53 #include "alloc-pool.h"
54 #include "alias.h"
55 #include "symtab.h"
56 #include "options.h"
57 #include "tree.h"
58 #include "predict.h"
59 #include "tm.h"
60 #include "hard-reg-set.h"
61 #include "function.h"
62 #include "dominance.h"
63 #include "cfg.h"
64 #include "cfganal.h"
65 #include "basic-block.h"
66 #include "df.h"
67 #include "cfgloop.h" /* FIXME: For struct loop. */
68 #include "dumpfile.h"
71 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
73 /* Called once at initialization time. */
75 void
76 init_flow (struct function *the_fun)
78 if (!the_fun->cfg)
79 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> ();
80 n_edges_for_fn (the_fun) = 0;
81 ENTRY_BLOCK_PTR_FOR_FN (the_fun)
82 = ggc_cleared_alloc<basic_block_def> ();
83 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
84 EXIT_BLOCK_PTR_FOR_FN (the_fun)
85 = ggc_cleared_alloc<basic_block_def> ();
86 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
87 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
88 = EXIT_BLOCK_PTR_FOR_FN (the_fun);
89 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
90 = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
93 /* Helper function for remove_edge and clear_edges. Frees edge structure
94 without actually removing it from the pred/succ arrays. */
96 static void
97 free_edge (edge e)
99 n_edges_for_fn (cfun)--;
100 ggc_free (e);
103 /* Free the memory associated with the edge structures. */
105 void
106 clear_edges (void)
108 basic_block bb;
109 edge e;
110 edge_iterator ei;
112 FOR_EACH_BB_FN (bb, cfun)
114 FOR_EACH_EDGE (e, ei, bb->succs)
115 free_edge (e);
116 vec_safe_truncate (bb->succs, 0);
117 vec_safe_truncate (bb->preds, 0);
120 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
121 free_edge (e);
122 vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, 0);
123 vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs, 0);
125 gcc_assert (!n_edges_for_fn (cfun));
128 /* Allocate memory for basic_block. */
130 basic_block
131 alloc_block (void)
133 basic_block bb;
134 bb = ggc_cleared_alloc<basic_block_def> ();
135 return bb;
138 /* Link block B to chain after AFTER. */
139 void
140 link_block (basic_block b, basic_block after)
142 b->next_bb = after->next_bb;
143 b->prev_bb = after;
144 after->next_bb = b;
145 b->next_bb->prev_bb = b;
148 /* Unlink block B from chain. */
149 void
150 unlink_block (basic_block b)
152 b->next_bb->prev_bb = b->prev_bb;
153 b->prev_bb->next_bb = b->next_bb;
154 b->prev_bb = NULL;
155 b->next_bb = NULL;
158 /* Sequentially order blocks and compact the arrays. */
159 void
160 compact_blocks (void)
162 int i;
164 SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun));
165 SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun));
167 if (df)
168 df_compact_blocks ();
169 else
171 basic_block bb;
173 i = NUM_FIXED_BLOCKS;
174 FOR_EACH_BB_FN (bb, cfun)
176 SET_BASIC_BLOCK_FOR_FN (cfun, i, bb);
177 bb->index = i;
178 i++;
180 gcc_assert (i == n_basic_blocks_for_fn (cfun));
182 for (; i < last_basic_block_for_fn (cfun); i++)
183 SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL);
185 last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun);
188 /* Remove block B from the basic block array. */
190 void
191 expunge_block (basic_block b)
193 unlink_block (b);
194 SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL);
195 n_basic_blocks_for_fn (cfun)--;
196 /* We should be able to ggc_free here, but we are not.
197 The dead SSA_NAMES are left pointing to dead statements that are pointing
198 to dead basic blocks making garbage collector to die.
199 We should be able to release all dead SSA_NAMES and at the same time we should
200 clear out BB pointer of dead statements consistently. */
203 /* Connect E to E->src. */
205 static inline void
206 connect_src (edge e)
208 vec_safe_push (e->src->succs, e);
209 df_mark_solutions_dirty ();
212 /* Connect E to E->dest. */
214 static inline void
215 connect_dest (edge e)
217 basic_block dest = e->dest;
218 vec_safe_push (dest->preds, e);
219 e->dest_idx = EDGE_COUNT (dest->preds) - 1;
220 df_mark_solutions_dirty ();
223 /* Disconnect edge E from E->src. */
225 static inline void
226 disconnect_src (edge e)
228 basic_block src = e->src;
229 edge_iterator ei;
230 edge tmp;
232 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
234 if (tmp == e)
236 src->succs->unordered_remove (ei.index);
237 df_mark_solutions_dirty ();
238 return;
240 else
241 ei_next (&ei);
244 gcc_unreachable ();
247 /* Disconnect edge E from E->dest. */
249 static inline void
250 disconnect_dest (edge e)
252 basic_block dest = e->dest;
253 unsigned int dest_idx = e->dest_idx;
255 dest->preds->unordered_remove (dest_idx);
257 /* If we removed an edge in the middle of the edge vector, we need
258 to update dest_idx of the edge that moved into the "hole". */
259 if (dest_idx < EDGE_COUNT (dest->preds))
260 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
261 df_mark_solutions_dirty ();
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. */
268 edge
269 unchecked_make_edge (basic_block src, basic_block dst, int flags)
271 edge e;
272 e = ggc_cleared_alloc<edge_def> ();
273 n_edges_for_fn (cfun)++;
275 e->src = src;
276 e->dest = dst;
277 e->flags = flags;
279 connect_src (e);
280 connect_dest (e);
282 execute_on_growing_pred (e);
283 return e;
286 /* Create an edge connecting SRC and DST with FLAGS optionally using
287 edge cache CACHE. Return the new edge, NULL if already exist. */
289 edge
290 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
292 if (edge_cache == NULL
293 || src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
294 || dst == EXIT_BLOCK_PTR_FOR_FN (cfun))
295 return make_edge (src, dst, flags);
297 /* Does the requested edge already exist? */
298 if (! bitmap_bit_p (edge_cache, dst->index))
300 /* The edge does not exist. Create one and update the
301 cache. */
302 bitmap_set_bit (edge_cache, dst->index);
303 return unchecked_make_edge (src, dst, flags);
306 /* At this point, we know that the requested edge exists. Adjust
307 flags if necessary. */
308 if (flags)
310 edge e = find_edge (src, dst);
311 e->flags |= flags;
314 return NULL;
317 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
318 created edge or NULL if already exist. */
320 edge
321 make_edge (basic_block src, basic_block dest, int flags)
323 edge e = find_edge (src, dest);
325 /* Make sure we don't add duplicate edges. */
326 if (e)
328 e->flags |= flags;
329 return NULL;
332 return unchecked_make_edge (src, dest, flags);
335 /* Create an edge connecting SRC to DEST and set probability by knowing
336 that it is the single edge leaving SRC. */
338 edge
339 make_single_succ_edge (basic_block src, basic_block dest, int flags)
341 edge e = make_edge (src, dest, flags);
343 e->probability = REG_BR_PROB_BASE;
344 e->count = src->count;
345 return e;
348 /* This function will remove an edge from the flow graph. */
350 void
351 remove_edge_raw (edge e)
353 remove_predictions_associated_with_edge (e);
354 execute_on_shrinking_pred (e);
356 disconnect_src (e);
357 disconnect_dest (e);
359 free_edge (e);
362 /* Redirect an edge's successor from one block to another. */
364 void
365 redirect_edge_succ (edge e, basic_block new_succ)
367 execute_on_shrinking_pred (e);
369 disconnect_dest (e);
371 e->dest = new_succ;
373 /* Reconnect the edge to the new successor block. */
374 connect_dest (e);
376 execute_on_growing_pred (e);
379 /* Redirect an edge's predecessor from one block to another. */
381 void
382 redirect_edge_pred (edge e, basic_block new_pred)
384 disconnect_src (e);
386 e->src = new_pred;
388 /* Reconnect the edge to the new predecessor block. */
389 connect_src (e);
392 /* Clear all basic block flags that do not have to be preserved. */
393 void
394 clear_bb_flags (void)
396 basic_block bb;
398 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
399 bb->flags &= BB_FLAGS_TO_PRESERVE;
402 /* Check the consistency of profile information. We can't do that
403 in verify_flow_info, as the counts may get invalid for incompletely
404 solved graphs, later eliminating of conditionals or roundoff errors.
405 It is still practical to have them reported for debugging of simple
406 testcases. */
407 static void
408 check_bb_profile (basic_block bb, FILE * file, int indent, int flags)
410 edge e;
411 int sum = 0;
412 gcov_type lsum;
413 edge_iterator ei;
414 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
415 char *s_indent = (char *) alloca ((size_t) indent + 1);
416 memset ((void *) s_indent, ' ', (size_t) indent);
417 s_indent[indent] = '\0';
419 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
420 return;
422 if (bb != EXIT_BLOCK_PTR_FOR_FN (fun))
424 FOR_EACH_EDGE (e, ei, bb->succs)
425 sum += e->probability;
426 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
427 fprintf (file, "%s%sInvalid sum of outgoing probabilities %.1f%%\n",
428 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
429 sum * 100.0 / REG_BR_PROB_BASE);
430 lsum = 0;
431 FOR_EACH_EDGE (e, ei, bb->succs)
432 lsum += e->count;
433 if (EDGE_COUNT (bb->succs)
434 && (lsum - bb->count > 100 || lsum - bb->count < -100))
435 fprintf (file, "%s%sInvalid sum of outgoing counts %i, should be %i\n",
436 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
437 (int) lsum, (int) bb->count);
439 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
441 sum = 0;
442 FOR_EACH_EDGE (e, ei, bb->preds)
443 sum += EDGE_FREQUENCY (e);
444 if (abs (sum - bb->frequency) > 100)
445 fprintf (file,
446 "%s%sInvalid sum of incoming frequencies %i, should be %i\n",
447 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
448 sum, bb->frequency);
449 lsum = 0;
450 FOR_EACH_EDGE (e, ei, bb->preds)
451 lsum += e->count;
452 if (lsum - bb->count > 100 || lsum - bb->count < -100)
453 fprintf (file, "%s%sInvalid sum of incoming counts %i, should be %i\n",
454 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
455 (int) lsum, (int) bb->count);
457 if (BB_PARTITION (bb) == BB_COLD_PARTITION)
459 /* Warn about inconsistencies in the partitioning that are
460 currently caused by profile insanities created via optimization. */
461 if (!probably_never_executed_bb_p (fun, bb))
462 fprintf (file, "%s%sBlock in cold partition with hot count\n",
463 (flags & TDF_COMMENT) ? ";; " : "", s_indent);
464 FOR_EACH_EDGE (e, ei, bb->preds)
466 if (!probably_never_executed_edge_p (fun, e))
467 fprintf (file,
468 "%s%sBlock in cold partition with incoming hot edge\n",
469 (flags & TDF_COMMENT) ? ";; " : "", s_indent);
474 void
475 dump_edge_info (FILE *file, edge e, int flags, int do_succ)
477 basic_block side = (do_succ ? e->dest : e->src);
478 bool do_details = false;
480 if ((flags & TDF_DETAILS) != 0
481 && (flags & TDF_SLIM) == 0)
482 do_details = true;
484 if (side->index == ENTRY_BLOCK)
485 fputs (" ENTRY", file);
486 else if (side->index == EXIT_BLOCK)
487 fputs (" EXIT", file);
488 else
489 fprintf (file, " %d", side->index);
491 if (e->probability && do_details)
492 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
494 if (e->count && do_details)
496 fputs (" count:", file);
497 fprintf (file, "%" PRId64, e->count);
500 if (e->flags && do_details)
502 static const char * const bitnames[] =
504 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
505 #include "cfg-flags.def"
506 NULL
507 #undef DEF_EDGE_FLAG
509 bool comma = false;
510 int i, flags = e->flags;
512 gcc_assert (e->flags <= EDGE_ALL_FLAGS);
513 fputs (" (", file);
514 for (i = 0; flags; i++)
515 if (flags & (1 << i))
517 flags &= ~(1 << i);
519 if (comma)
520 fputc (',', file);
521 fputs (bitnames[i], file);
522 comma = true;
525 fputc (')', file);
529 DEBUG_FUNCTION void
530 debug (edge_def &ref)
532 /* FIXME (crowl): Is this desireable? */
533 dump_edge_info (stderr, &ref, 0, false);
534 dump_edge_info (stderr, &ref, 0, true);
537 DEBUG_FUNCTION void
538 debug (edge_def *ptr)
540 if (ptr)
541 debug (*ptr);
542 else
543 fprintf (stderr, "<nil>\n");
546 /* Simple routines to easily allocate AUX fields of basic blocks. */
548 static struct obstack block_aux_obstack;
549 static void *first_block_aux_obj = 0;
550 static struct obstack edge_aux_obstack;
551 static void *first_edge_aux_obj = 0;
553 /* Allocate a memory block of SIZE as BB->aux. The obstack must
554 be first initialized by alloc_aux_for_blocks. */
556 static void
557 alloc_aux_for_block (basic_block bb, int size)
559 /* Verify that aux field is clear. */
560 gcc_assert (!bb->aux && first_block_aux_obj);
561 bb->aux = obstack_alloc (&block_aux_obstack, size);
562 memset (bb->aux, 0, size);
565 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
566 alloc_aux_for_block for each basic block. */
568 void
569 alloc_aux_for_blocks (int size)
571 static int initialized;
573 if (!initialized)
575 gcc_obstack_init (&block_aux_obstack);
576 initialized = 1;
578 else
579 /* Check whether AUX data are still allocated. */
580 gcc_assert (!first_block_aux_obj);
582 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
583 if (size)
585 basic_block bb;
587 FOR_ALL_BB_FN (bb, cfun)
588 alloc_aux_for_block (bb, size);
592 /* Clear AUX pointers of all blocks. */
594 void
595 clear_aux_for_blocks (void)
597 basic_block bb;
599 FOR_ALL_BB_FN (bb, cfun)
600 bb->aux = NULL;
603 /* Free data allocated in block_aux_obstack and clear AUX pointers
604 of all blocks. */
606 void
607 free_aux_for_blocks (void)
609 gcc_assert (first_block_aux_obj);
610 obstack_free (&block_aux_obstack, first_block_aux_obj);
611 first_block_aux_obj = NULL;
613 clear_aux_for_blocks ();
616 /* Allocate a memory edge of SIZE as E->aux. The obstack must
617 be first initialized by alloc_aux_for_edges. */
619 void
620 alloc_aux_for_edge (edge e, int size)
622 /* Verify that aux field is clear. */
623 gcc_assert (!e->aux && first_edge_aux_obj);
624 e->aux = obstack_alloc (&edge_aux_obstack, size);
625 memset (e->aux, 0, size);
628 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
629 alloc_aux_for_edge for each basic edge. */
631 void
632 alloc_aux_for_edges (int size)
634 static int initialized;
636 if (!initialized)
638 gcc_obstack_init (&edge_aux_obstack);
639 initialized = 1;
641 else
642 /* Check whether AUX data are still allocated. */
643 gcc_assert (!first_edge_aux_obj);
645 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
646 if (size)
648 basic_block bb;
650 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
651 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
653 edge e;
654 edge_iterator ei;
656 FOR_EACH_EDGE (e, ei, bb->succs)
657 alloc_aux_for_edge (e, size);
662 /* Clear AUX pointers of all edges. */
664 void
665 clear_aux_for_edges (void)
667 basic_block bb;
668 edge e;
670 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
671 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
673 edge_iterator ei;
674 FOR_EACH_EDGE (e, ei, bb->succs)
675 e->aux = NULL;
679 /* Free data allocated in edge_aux_obstack and clear AUX pointers
680 of all edges. */
682 void
683 free_aux_for_edges (void)
685 gcc_assert (first_edge_aux_obj);
686 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
687 first_edge_aux_obj = NULL;
689 clear_aux_for_edges ();
692 DEBUG_FUNCTION void
693 debug_bb (basic_block bb)
695 dump_bb (stderr, bb, 0, dump_flags);
698 DEBUG_FUNCTION basic_block
699 debug_bb_n (int n)
701 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n);
702 debug_bb (bb);
703 return bb;
706 /* Dumps cfg related information about basic block BB to OUTF.
707 If HEADER is true, dump things that appear before the instructions
708 contained in BB. If FOOTER is true, dump things that appear after.
709 Flags are the TDF_* masks as documented in dumpfile.h.
710 NB: With TDF_DETAILS, it is assumed that cfun is available, so
711 that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */
713 void
714 dump_bb_info (FILE *outf, basic_block bb, int indent, int flags,
715 bool do_header, bool do_footer)
717 edge_iterator ei;
718 edge e;
719 static const char * const bb_bitnames[] =
721 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
722 #include "cfg-flags.def"
723 NULL
724 #undef DEF_BASIC_BLOCK_FLAG
726 const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
727 bool first;
728 char *s_indent = (char *) alloca ((size_t) indent + 1);
729 memset ((void *) s_indent, ' ', (size_t) indent);
730 s_indent[indent] = '\0';
732 gcc_assert (bb->flags <= BB_ALL_FLAGS);
734 if (do_header)
736 unsigned i;
738 if (flags & TDF_COMMENT)
739 fputs (";; ", outf);
740 fprintf (outf, "%sbasic block %d, loop depth %d",
741 s_indent, bb->index, bb_loop_depth (bb));
742 if (flags & TDF_DETAILS)
744 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
745 fprintf (outf, ", count " "%" PRId64,
746 (int64_t) bb->count);
747 fprintf (outf, ", freq %i", bb->frequency);
748 if (maybe_hot_bb_p (fun, bb))
749 fputs (", maybe hot", outf);
750 if (probably_never_executed_bb_p (fun, bb))
751 fputs (", probably never executed", outf);
753 fputc ('\n', outf);
755 if (flags & TDF_DETAILS)
757 check_bb_profile (bb, outf, indent, flags);
758 if (flags & TDF_COMMENT)
759 fputs (";; ", outf);
760 fprintf (outf, "%s prev block ", s_indent);
761 if (bb->prev_bb)
762 fprintf (outf, "%d", bb->prev_bb->index);
763 else
764 fprintf (outf, "(nil)");
765 fprintf (outf, ", next block ");
766 if (bb->next_bb)
767 fprintf (outf, "%d", bb->next_bb->index);
768 else
769 fprintf (outf, "(nil)");
771 fputs (", flags:", outf);
772 first = true;
773 for (i = 0; i < n_bitnames; i++)
774 if (bb->flags & (1 << i))
776 if (first)
777 fputs (" (", outf);
778 else
779 fputs (", ", outf);
780 first = false;
781 fputs (bb_bitnames[i], outf);
783 if (!first)
784 fputc (')', outf);
785 fputc ('\n', outf);
788 if (flags & TDF_COMMENT)
789 fputs (";; ", outf);
790 fprintf (outf, "%s pred: ", s_indent);
791 first = true;
792 FOR_EACH_EDGE (e, ei, bb->preds)
794 if (! first)
796 if (flags & TDF_COMMENT)
797 fputs (";; ", outf);
798 fprintf (outf, "%s ", s_indent);
800 first = false;
801 dump_edge_info (outf, e, flags, 0);
802 fputc ('\n', outf);
804 if (first)
805 fputc ('\n', outf);
808 if (do_footer)
810 if (flags & TDF_COMMENT)
811 fputs (";; ", outf);
812 fprintf (outf, "%s succ: ", s_indent);
813 first = true;
814 FOR_EACH_EDGE (e, ei, bb->succs)
816 if (! first)
818 if (flags & TDF_COMMENT)
819 fputs (";; ", outf);
820 fprintf (outf, "%s ", s_indent);
822 first = false;
823 dump_edge_info (outf, e, flags, 1);
824 fputc ('\n', outf);
826 if (first)
827 fputc ('\n', outf);
831 /* Dumps a brief description of cfg to FILE. */
833 void
834 brief_dump_cfg (FILE *file, int flags)
836 basic_block bb;
838 FOR_EACH_BB_FN (bb, cfun)
840 dump_bb_info (file, bb, 0,
841 flags & (TDF_COMMENT | TDF_DETAILS),
842 true, true);
846 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
847 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
848 redirected to destination of TAKEN_EDGE.
850 This function may leave the profile inconsistent in the case TAKEN_EDGE
851 frequency or count is believed to be lower than FREQUENCY or COUNT
852 respectively. */
853 void
854 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
855 gcov_type count, edge taken_edge)
857 edge c;
858 int prob;
859 edge_iterator ei;
861 bb->count -= count;
862 if (bb->count < 0)
864 if (dump_file)
865 fprintf (dump_file, "bb %i count became negative after threading",
866 bb->index);
867 bb->count = 0;
870 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
871 Watch for overflows. */
872 if (bb->frequency)
873 prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency);
874 else
875 prob = 0;
876 if (prob > taken_edge->probability)
878 if (dump_file)
879 fprintf (dump_file, "Jump threading proved probability of edge "
880 "%i->%i too small (it is %i, should be %i).\n",
881 taken_edge->src->index, taken_edge->dest->index,
882 taken_edge->probability, prob);
883 prob = taken_edge->probability;
886 /* Now rescale the probabilities. */
887 taken_edge->probability -= prob;
888 prob = REG_BR_PROB_BASE - prob;
889 bb->frequency -= edge_frequency;
890 if (bb->frequency < 0)
891 bb->frequency = 0;
892 if (prob <= 0)
894 if (dump_file)
895 fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
896 "frequency of block should end up being 0, it is %i\n",
897 bb->index, bb->frequency);
898 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
899 ei = ei_start (bb->succs);
900 ei_next (&ei);
901 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
902 c->probability = 0;
904 else if (prob != REG_BR_PROB_BASE)
906 int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
908 FOR_EACH_EDGE (c, ei, bb->succs)
910 /* Protect from overflow due to additional scaling. */
911 if (c->probability > prob)
912 c->probability = REG_BR_PROB_BASE;
913 else
915 c->probability = RDIV (c->probability * scale, 65536);
916 if (c->probability > REG_BR_PROB_BASE)
917 c->probability = REG_BR_PROB_BASE;
922 gcc_assert (bb == taken_edge->src);
923 taken_edge->count -= count;
924 if (taken_edge->count < 0)
926 if (dump_file)
927 fprintf (dump_file, "edge %i->%i count became negative after threading",
928 taken_edge->src->index, taken_edge->dest->index);
929 taken_edge->count = 0;
933 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
934 by NUM/DEN, in int arithmetic. May lose some accuracy. */
935 void
936 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
938 int i;
939 edge e;
940 if (num < 0)
941 num = 0;
943 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
944 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
945 and still safely fit in int during calculations. */
946 if (den > 1000)
948 if (num > 1000000)
949 return;
951 num = RDIV (1000 * num, den);
952 den = 1000;
954 if (num > 100 * den)
955 return;
957 for (i = 0; i < nbbs; i++)
959 edge_iterator ei;
960 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
961 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
962 if (bbs[i]->frequency > BB_FREQ_MAX)
963 bbs[i]->frequency = BB_FREQ_MAX;
964 bbs[i]->count = RDIV (bbs[i]->count * num, den);
965 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
966 e->count = RDIV (e->count * num, den);
970 /* numbers smaller than this value are safe to multiply without getting
971 64bit overflow. */
972 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (int64_t) * 4 - 1))
974 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
975 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
976 function but considerably slower. */
977 void
978 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
979 gcov_type den)
981 int i;
982 edge e;
983 gcov_type fraction = RDIV (num * 65536, den);
985 gcc_assert (fraction >= 0);
987 if (num < MAX_SAFE_MULTIPLIER)
988 for (i = 0; i < nbbs; i++)
990 edge_iterator ei;
991 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
992 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
993 bbs[i]->count = RDIV (bbs[i]->count * num, den);
994 else
995 bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
996 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
997 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
998 e->count = RDIV (e->count * num, den);
999 else
1000 e->count = RDIV (e->count * fraction, 65536);
1002 else
1003 for (i = 0; i < nbbs; i++)
1005 edge_iterator ei;
1006 if (sizeof (gcov_type) > sizeof (int))
1007 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1008 else
1009 bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
1010 bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
1011 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1012 e->count = RDIV (e->count * fraction, 65536);
1016 /* Helper types for hash tables. */
1018 struct htab_bb_copy_original_entry
1020 /* Block we are attaching info to. */
1021 int index1;
1022 /* Index of original or copy (depending on the hashtable) */
1023 int index2;
1026 struct bb_copy_hasher : nofree_ptr_hash <htab_bb_copy_original_entry>
1028 static inline hashval_t hash (const htab_bb_copy_original_entry *);
1029 static inline bool equal (const htab_bb_copy_original_entry *existing,
1030 const htab_bb_copy_original_entry * candidate);
1033 inline hashval_t
1034 bb_copy_hasher::hash (const htab_bb_copy_original_entry *data)
1036 return data->index1;
1039 inline bool
1040 bb_copy_hasher::equal (const htab_bb_copy_original_entry *data,
1041 const htab_bb_copy_original_entry *data2)
1043 return data->index1 == data2->index1;
1046 /* Data structures used to maintain mapping between basic blocks and
1047 copies. */
1048 static hash_table<bb_copy_hasher> *bb_original;
1049 static hash_table<bb_copy_hasher> *bb_copy;
1051 /* And between loops and copies. */
1052 static hash_table<bb_copy_hasher> *loop_copy;
1053 static pool_allocator<htab_bb_copy_original_entry> *original_copy_bb_pool;
1055 /* Initialize the data structures to maintain mapping between blocks
1056 and its copies. */
1057 void
1058 initialize_original_copy_tables (void)
1061 original_copy_bb_pool = new pool_allocator<htab_bb_copy_original_entry>
1062 ("original_copy", 10);
1063 bb_original = new hash_table<bb_copy_hasher> (10);
1064 bb_copy = new hash_table<bb_copy_hasher> (10);
1065 loop_copy = new hash_table<bb_copy_hasher> (10);
1068 /* Free the data structures to maintain mapping between blocks and
1069 its copies. */
1070 void
1071 free_original_copy_tables (void)
1073 gcc_assert (original_copy_bb_pool);
1074 delete bb_copy;
1075 bb_copy = NULL;
1076 delete bb_original;
1077 bb_copy = NULL;
1078 delete loop_copy;
1079 loop_copy = NULL;
1080 delete original_copy_bb_pool;
1081 original_copy_bb_pool = NULL;
1084 /* Removes the value associated with OBJ from table TAB. */
1086 static void
1087 copy_original_table_clear (hash_table<bb_copy_hasher> *tab, unsigned obj)
1089 htab_bb_copy_original_entry **slot;
1090 struct htab_bb_copy_original_entry key, *elt;
1092 if (!original_copy_bb_pool)
1093 return;
1095 key.index1 = obj;
1096 slot = tab->find_slot (&key, NO_INSERT);
1097 if (!slot)
1098 return;
1100 elt = *slot;
1101 tab->clear_slot (slot);
1102 original_copy_bb_pool->remove (elt);
1105 /* Sets the value associated with OBJ in table TAB to VAL.
1106 Do nothing when data structures are not initialized. */
1108 static void
1109 copy_original_table_set (hash_table<bb_copy_hasher> *tab,
1110 unsigned obj, unsigned val)
1112 struct htab_bb_copy_original_entry **slot;
1113 struct htab_bb_copy_original_entry key;
1115 if (!original_copy_bb_pool)
1116 return;
1118 key.index1 = obj;
1119 slot = tab->find_slot (&key, INSERT);
1120 if (!*slot)
1122 *slot = original_copy_bb_pool->allocate ();
1123 (*slot)->index1 = obj;
1125 (*slot)->index2 = val;
1128 /* Set original for basic block. Do nothing when data structures are not
1129 initialized so passes not needing this don't need to care. */
1130 void
1131 set_bb_original (basic_block bb, basic_block original)
1133 copy_original_table_set (bb_original, bb->index, original->index);
1136 /* Get the original basic block. */
1137 basic_block
1138 get_bb_original (basic_block bb)
1140 struct htab_bb_copy_original_entry *entry;
1141 struct htab_bb_copy_original_entry key;
1143 gcc_assert (original_copy_bb_pool);
1145 key.index1 = bb->index;
1146 entry = bb_original->find (&key);
1147 if (entry)
1148 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1149 else
1150 return NULL;
1153 /* Set copy for basic block. Do nothing when data structures are not
1154 initialized so passes not needing this don't need to care. */
1155 void
1156 set_bb_copy (basic_block bb, basic_block copy)
1158 copy_original_table_set (bb_copy, bb->index, copy->index);
1161 /* Get the copy of basic block. */
1162 basic_block
1163 get_bb_copy (basic_block bb)
1165 struct htab_bb_copy_original_entry *entry;
1166 struct htab_bb_copy_original_entry key;
1168 gcc_assert (original_copy_bb_pool);
1170 key.index1 = bb->index;
1171 entry = bb_copy->find (&key);
1172 if (entry)
1173 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1174 else
1175 return NULL;
1178 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1179 initialized so passes not needing this don't need to care. */
1181 void
1182 set_loop_copy (struct loop *loop, struct loop *copy)
1184 if (!copy)
1185 copy_original_table_clear (loop_copy, loop->num);
1186 else
1187 copy_original_table_set (loop_copy, loop->num, copy->num);
1190 /* Get the copy of LOOP. */
1192 struct loop *
1193 get_loop_copy (struct loop *loop)
1195 struct htab_bb_copy_original_entry *entry;
1196 struct htab_bb_copy_original_entry key;
1198 gcc_assert (original_copy_bb_pool);
1200 key.index1 = loop->num;
1201 entry = loop_copy->find (&key);
1202 if (entry)
1203 return get_loop (cfun, entry->index2);
1204 else
1205 return NULL;