libgo: add misc/cgo files
[official-gcc.git] / gcc / cfg.c
blob3cbe684b743a53a4d047aef4decb1e88a797dff9
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2017 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 "backend.h"
53 #include "hard-reg-set.h"
54 #include "tree.h"
55 #include "cfghooks.h"
56 #include "df.h"
57 #include "cfganal.h"
58 #include "cfgloop.h" /* FIXME: For struct loop. */
59 #include "dumpfile.h"
63 /* Called once at initialization time. */
65 void
66 init_flow (struct function *the_fun)
68 if (!the_fun->cfg)
69 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> ();
70 n_edges_for_fn (the_fun) = 0;
71 ENTRY_BLOCK_PTR_FOR_FN (the_fun)
72 = alloc_block ();
73 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
74 EXIT_BLOCK_PTR_FOR_FN (the_fun)
75 = alloc_block ();
76 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
77 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
78 = EXIT_BLOCK_PTR_FOR_FN (the_fun);
79 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
80 = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
83 /* Helper function for remove_edge and clear_edges. Frees edge structure
84 without actually removing it from the pred/succ arrays. */
86 static void
87 free_edge (function *fn, edge e)
89 n_edges_for_fn (fn)--;
90 ggc_free (e);
93 /* Free the memory associated with the edge structures. */
95 void
96 clear_edges (struct function *fn)
98 basic_block bb;
99 edge e;
100 edge_iterator ei;
102 FOR_EACH_BB_FN (bb, fn)
104 FOR_EACH_EDGE (e, ei, bb->succs)
105 free_edge (fn, e);
106 vec_safe_truncate (bb->succs, 0);
107 vec_safe_truncate (bb->preds, 0);
110 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fn)->succs)
111 free_edge (fn, e);
112 vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (fn)->preds, 0);
113 vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (fn)->succs, 0);
115 gcc_assert (!n_edges_for_fn (fn));
118 /* Allocate memory for basic_block. */
120 basic_block
121 alloc_block (void)
123 basic_block bb;
124 bb = ggc_cleared_alloc<basic_block_def> ();
125 bb->count = profile_count::uninitialized ();
126 return bb;
129 /* Link block B to chain after AFTER. */
130 void
131 link_block (basic_block b, basic_block after)
133 b->next_bb = after->next_bb;
134 b->prev_bb = after;
135 after->next_bb = b;
136 b->next_bb->prev_bb = b;
139 /* Unlink block B from chain. */
140 void
141 unlink_block (basic_block b)
143 b->next_bb->prev_bb = b->prev_bb;
144 b->prev_bb->next_bb = b->next_bb;
145 b->prev_bb = NULL;
146 b->next_bb = NULL;
149 /* Sequentially order blocks and compact the arrays. */
150 void
151 compact_blocks (void)
153 int i;
155 SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun));
156 SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun));
158 if (df)
159 df_compact_blocks ();
160 else
162 basic_block bb;
164 i = NUM_FIXED_BLOCKS;
165 FOR_EACH_BB_FN (bb, cfun)
167 SET_BASIC_BLOCK_FOR_FN (cfun, i, bb);
168 bb->index = i;
169 i++;
171 gcc_assert (i == n_basic_blocks_for_fn (cfun));
173 for (; i < last_basic_block_for_fn (cfun); i++)
174 SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL);
176 last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun);
179 /* Remove block B from the basic block array. */
181 void
182 expunge_block (basic_block b)
184 unlink_block (b);
185 SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL);
186 n_basic_blocks_for_fn (cfun)--;
187 /* We should be able to ggc_free here, but we are not.
188 The dead SSA_NAMES are left pointing to dead statements that are pointing
189 to dead basic blocks making garbage collector to die.
190 We should be able to release all dead SSA_NAMES and at the same time we should
191 clear out BB pointer of dead statements consistently. */
194 /* Connect E to E->src. */
196 static inline void
197 connect_src (edge e)
199 vec_safe_push (e->src->succs, e);
200 df_mark_solutions_dirty ();
203 /* Connect E to E->dest. */
205 static inline void
206 connect_dest (edge e)
208 basic_block dest = e->dest;
209 vec_safe_push (dest->preds, e);
210 e->dest_idx = EDGE_COUNT (dest->preds) - 1;
211 df_mark_solutions_dirty ();
214 /* Disconnect edge E from E->src. */
216 static inline void
217 disconnect_src (edge e)
219 basic_block src = e->src;
220 edge_iterator ei;
221 edge tmp;
223 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
225 if (tmp == e)
227 src->succs->unordered_remove (ei.index);
228 df_mark_solutions_dirty ();
229 return;
231 else
232 ei_next (&ei);
235 gcc_unreachable ();
238 /* Disconnect edge E from E->dest. */
240 static inline void
241 disconnect_dest (edge e)
243 basic_block dest = e->dest;
244 unsigned int dest_idx = e->dest_idx;
246 dest->preds->unordered_remove (dest_idx);
248 /* If we removed an edge in the middle of the edge vector, we need
249 to update dest_idx of the edge that moved into the "hole". */
250 if (dest_idx < EDGE_COUNT (dest->preds))
251 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
252 df_mark_solutions_dirty ();
255 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
256 created edge. Use this only if you are sure that this edge can't
257 possibly already exist. */
259 edge
260 unchecked_make_edge (basic_block src, basic_block dst, int flags)
262 edge e;
263 e = ggc_cleared_alloc<edge_def> ();
264 n_edges_for_fn (cfun)++;
266 e->count = profile_count::uninitialized ();
267 e->src = src;
268 e->dest = dst;
269 e->flags = flags;
271 connect_src (e);
272 connect_dest (e);
274 execute_on_growing_pred (e);
275 return e;
278 /* Create an edge connecting SRC and DST with FLAGS optionally using
279 edge cache CACHE. Return the new edge, NULL if already exist. */
281 edge
282 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
284 if (edge_cache == NULL
285 || src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
286 || dst == EXIT_BLOCK_PTR_FOR_FN (cfun))
287 return make_edge (src, dst, flags);
289 /* Does the requested edge already exist? */
290 if (! bitmap_bit_p (edge_cache, dst->index))
292 /* The edge does not exist. Create one and update the
293 cache. */
294 bitmap_set_bit (edge_cache, dst->index);
295 return unchecked_make_edge (src, dst, flags);
298 /* At this point, we know that the requested edge exists. Adjust
299 flags if necessary. */
300 if (flags)
302 edge e = find_edge (src, dst);
303 e->flags |= flags;
306 return NULL;
309 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
310 created edge or NULL if already exist. */
312 edge
313 make_edge (basic_block src, basic_block dest, int flags)
315 edge e = find_edge (src, dest);
317 /* Make sure we don't add duplicate edges. */
318 if (e)
320 e->flags |= flags;
321 return NULL;
324 return unchecked_make_edge (src, dest, flags);
327 /* Create an edge connecting SRC to DEST and set probability by knowing
328 that it is the single edge leaving SRC. */
330 edge
331 make_single_succ_edge (basic_block src, basic_block dest, int flags)
333 edge e = make_edge (src, dest, flags);
335 e->probability = REG_BR_PROB_BASE;
336 e->count = src->count;
337 return e;
340 /* This function will remove an edge from the flow graph. */
342 void
343 remove_edge_raw (edge e)
345 remove_predictions_associated_with_edge (e);
346 execute_on_shrinking_pred (e);
348 disconnect_src (e);
349 disconnect_dest (e);
351 free_edge (cfun, e);
354 /* Redirect an edge's successor from one block to another. */
356 void
357 redirect_edge_succ (edge e, basic_block new_succ)
359 execute_on_shrinking_pred (e);
361 disconnect_dest (e);
363 e->dest = new_succ;
365 /* Reconnect the edge to the new successor block. */
366 connect_dest (e);
368 execute_on_growing_pred (e);
371 /* Redirect an edge's predecessor from one block to another. */
373 void
374 redirect_edge_pred (edge e, basic_block new_pred)
376 disconnect_src (e);
378 e->src = new_pred;
380 /* Reconnect the edge to the new predecessor block. */
381 connect_src (e);
384 /* Clear all basic block flags that do not have to be preserved. */
385 void
386 clear_bb_flags (void)
388 basic_block bb;
390 FOR_ALL_BB_FN (bb, cfun)
391 bb->flags &= BB_FLAGS_TO_PRESERVE;
394 /* Check the consistency of profile information. We can't do that
395 in verify_flow_info, as the counts may get invalid for incompletely
396 solved graphs, later eliminating of conditionals or roundoff errors.
397 It is still practical to have them reported for debugging of simple
398 testcases. */
399 static void
400 check_bb_profile (basic_block bb, FILE * file, int indent)
402 edge e;
403 int sum = 0;
404 edge_iterator ei;
405 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
406 char *s_indent = (char *) alloca ((size_t) indent + 1);
407 memset ((void *) s_indent, ' ', (size_t) indent);
408 s_indent[indent] = '\0';
410 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
411 return;
413 if (bb != EXIT_BLOCK_PTR_FOR_FN (fun))
415 bool found = false;
416 FOR_EACH_EDGE (e, ei, bb->succs)
418 if (!(e->flags & EDGE_EH))
419 found = true;
420 sum += e->probability;
422 /* Only report mismatches for non-EH control flow. If there are only EH
423 edges it means that the BB ends by noreturn call. Here the control
424 flow may just terminate. */
425 if (found)
427 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
428 fprintf (file,
429 ";; %sInvalid sum of outgoing probabilities %.1f%%\n",
430 s_indent, sum * 100.0 / REG_BR_PROB_BASE);
431 profile_count lsum = profile_count::zero ();
432 FOR_EACH_EDGE (e, ei, bb->succs)
433 lsum += e->count;
434 if (EDGE_COUNT (bb->succs) && lsum.differs_from_p (bb->count))
436 fprintf (file, ";; %sInvalid sum of outgoing counts ",
437 s_indent);
438 lsum.dump (file);
439 fprintf (file, ", should be ");
440 bb->count.dump (file);
441 fprintf (file, "\n");
445 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
447 sum = 0;
448 FOR_EACH_EDGE (e, ei, bb->preds)
449 sum += EDGE_FREQUENCY (e);
450 if (abs (sum - bb->frequency) > 100)
451 fprintf (file,
452 ";; %sInvalid sum of incoming frequencies %i, should be %i\n",
453 s_indent, sum, bb->frequency);
454 profile_count lsum = profile_count::zero ();
455 FOR_EACH_EDGE (e, ei, bb->preds)
456 lsum += e->count;
457 if (lsum.differs_from_p (bb->count))
459 fprintf (file, ";; %sInvalid sum of incoming counts ",
460 s_indent);
461 lsum.dump (file);
462 fprintf (file, ", should be ");
463 bb->count.dump (file);
464 fprintf (file, "\n");
467 if (BB_PARTITION (bb) == BB_COLD_PARTITION)
469 /* Warn about inconsistencies in the partitioning that are
470 currently caused by profile insanities created via optimization. */
471 if (!probably_never_executed_bb_p (fun, bb))
472 fprintf (file, ";; %sBlock in cold partition with hot count\n",
473 s_indent);
474 FOR_EACH_EDGE (e, ei, bb->preds)
476 if (!probably_never_executed_edge_p (fun, e))
477 fprintf (file,
478 ";; %sBlock in cold partition with incoming hot edge\n",
479 s_indent);
484 void
485 dump_edge_info (FILE *file, edge e, dump_flags_t flags, int do_succ)
487 basic_block side = (do_succ ? e->dest : e->src);
488 bool do_details = false;
490 if ((flags & TDF_DETAILS) != 0
491 && (flags & TDF_SLIM) == 0)
492 do_details = true;
494 if (side->index == ENTRY_BLOCK)
495 fputs (" ENTRY", file);
496 else if (side->index == EXIT_BLOCK)
497 fputs (" EXIT", file);
498 else
499 fprintf (file, " %d", side->index);
501 if (e->probability && do_details)
502 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
504 if (e->count.initialized_p () && do_details)
506 fputs (" count:", file);
507 e->count.dump (file);
510 if (e->flags && do_details)
512 static const char * const bitnames[] =
514 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
515 #include "cfg-flags.def"
516 NULL
517 #undef DEF_EDGE_FLAG
519 bool comma = false;
520 int i, flags = e->flags;
522 gcc_assert (e->flags <= EDGE_ALL_FLAGS);
523 fputs (" (", file);
524 for (i = 0; flags; i++)
525 if (flags & (1 << i))
527 flags &= ~(1 << i);
529 if (comma)
530 fputc (',', file);
531 fputs (bitnames[i], file);
532 comma = true;
535 fputc (')', file);
539 DEBUG_FUNCTION void
540 debug (edge_def &ref)
542 /* FIXME (crowl): Is this desireable? */
543 dump_edge_info (stderr, &ref, 0, false);
544 dump_edge_info (stderr, &ref, 0, true);
547 DEBUG_FUNCTION void
548 debug (edge_def *ptr)
550 if (ptr)
551 debug (*ptr);
552 else
553 fprintf (stderr, "<nil>\n");
556 /* Simple routines to easily allocate AUX fields of basic blocks. */
558 static struct obstack block_aux_obstack;
559 static void *first_block_aux_obj = 0;
560 static struct obstack edge_aux_obstack;
561 static void *first_edge_aux_obj = 0;
563 /* Allocate a memory block of SIZE as BB->aux. The obstack must
564 be first initialized by alloc_aux_for_blocks. */
566 static void
567 alloc_aux_for_block (basic_block bb, int size)
569 /* Verify that aux field is clear. */
570 gcc_assert (!bb->aux && first_block_aux_obj);
571 bb->aux = obstack_alloc (&block_aux_obstack, size);
572 memset (bb->aux, 0, size);
575 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
576 alloc_aux_for_block for each basic block. */
578 void
579 alloc_aux_for_blocks (int size)
581 static int initialized;
583 if (!initialized)
585 gcc_obstack_init (&block_aux_obstack);
586 initialized = 1;
588 else
589 /* Check whether AUX data are still allocated. */
590 gcc_assert (!first_block_aux_obj);
592 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
593 if (size)
595 basic_block bb;
597 FOR_ALL_BB_FN (bb, cfun)
598 alloc_aux_for_block (bb, size);
602 /* Clear AUX pointers of all blocks. */
604 void
605 clear_aux_for_blocks (void)
607 basic_block bb;
609 FOR_ALL_BB_FN (bb, cfun)
610 bb->aux = NULL;
613 /* Free data allocated in block_aux_obstack and clear AUX pointers
614 of all blocks. */
616 void
617 free_aux_for_blocks (void)
619 gcc_assert (first_block_aux_obj);
620 obstack_free (&block_aux_obstack, first_block_aux_obj);
621 first_block_aux_obj = NULL;
623 clear_aux_for_blocks ();
626 /* Allocate a memory edge of SIZE as E->aux. The obstack must
627 be first initialized by alloc_aux_for_edges. */
629 void
630 alloc_aux_for_edge (edge e, int size)
632 /* Verify that aux field is clear. */
633 gcc_assert (!e->aux && first_edge_aux_obj);
634 e->aux = obstack_alloc (&edge_aux_obstack, size);
635 memset (e->aux, 0, size);
638 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
639 alloc_aux_for_edge for each basic edge. */
641 void
642 alloc_aux_for_edges (int size)
644 static int initialized;
646 if (!initialized)
648 gcc_obstack_init (&edge_aux_obstack);
649 initialized = 1;
651 else
652 /* Check whether AUX data are still allocated. */
653 gcc_assert (!first_edge_aux_obj);
655 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
656 if (size)
658 basic_block bb;
660 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
661 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
663 edge e;
664 edge_iterator ei;
666 FOR_EACH_EDGE (e, ei, bb->succs)
667 alloc_aux_for_edge (e, size);
672 /* Clear AUX pointers of all edges. */
674 void
675 clear_aux_for_edges (void)
677 basic_block bb;
678 edge e;
680 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
681 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
683 edge_iterator ei;
684 FOR_EACH_EDGE (e, ei, bb->succs)
685 e->aux = NULL;
689 /* Free data allocated in edge_aux_obstack and clear AUX pointers
690 of all edges. */
692 void
693 free_aux_for_edges (void)
695 gcc_assert (first_edge_aux_obj);
696 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
697 first_edge_aux_obj = NULL;
699 clear_aux_for_edges ();
702 DEBUG_FUNCTION void
703 debug_bb (basic_block bb)
705 dump_bb (stderr, bb, 0, dump_flags);
708 DEBUG_FUNCTION basic_block
709 debug_bb_n (int n)
711 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n);
712 debug_bb (bb);
713 return bb;
716 /* Dumps cfg related information about basic block BB to OUTF.
717 If HEADER is true, dump things that appear before the instructions
718 contained in BB. If FOOTER is true, dump things that appear after.
719 Flags are the TDF_* masks as documented in dumpfile.h.
720 NB: With TDF_DETAILS, it is assumed that cfun is available, so
721 that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */
723 void
724 dump_bb_info (FILE *outf, basic_block bb, int indent, dump_flags_t flags,
725 bool do_header, bool do_footer)
727 edge_iterator ei;
728 edge e;
729 static const char * const bb_bitnames[] =
731 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
732 #include "cfg-flags.def"
733 NULL
734 #undef DEF_BASIC_BLOCK_FLAG
736 const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
737 bool first;
738 char *s_indent = (char *) alloca ((size_t) indent + 1);
739 memset ((void *) s_indent, ' ', (size_t) indent);
740 s_indent[indent] = '\0';
742 gcc_assert (bb->flags <= BB_ALL_FLAGS);
744 if (do_header)
746 unsigned i;
748 fputs (";; ", outf);
749 fprintf (outf, "%sbasic block %d, loop depth %d",
750 s_indent, bb->index, bb_loop_depth (bb));
751 if (flags & TDF_DETAILS)
753 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
754 if (bb->count.initialized_p ())
756 fputs (", count ", outf);
757 bb->count.dump (outf);
759 fprintf (outf, ", freq %i", bb->frequency);
760 if (maybe_hot_bb_p (fun, bb))
761 fputs (", maybe hot", outf);
762 if (probably_never_executed_bb_p (fun, bb))
763 fputs (", probably never executed", outf);
765 fputc ('\n', outf);
767 if (flags & TDF_DETAILS)
769 check_bb_profile (bb, outf, indent);
770 fputs (";; ", outf);
771 fprintf (outf, "%s prev block ", s_indent);
772 if (bb->prev_bb)
773 fprintf (outf, "%d", bb->prev_bb->index);
774 else
775 fprintf (outf, "(nil)");
776 fprintf (outf, ", next block ");
777 if (bb->next_bb)
778 fprintf (outf, "%d", bb->next_bb->index);
779 else
780 fprintf (outf, "(nil)");
782 fputs (", flags:", outf);
783 first = true;
784 for (i = 0; i < n_bitnames; i++)
785 if (bb->flags & (1 << i))
787 if (first)
788 fputs (" (", outf);
789 else
790 fputs (", ", outf);
791 first = false;
792 fputs (bb_bitnames[i], outf);
794 if (!first)
795 fputc (')', outf);
796 fputc ('\n', outf);
799 fputs (";; ", outf);
800 fprintf (outf, "%s pred: ", s_indent);
801 first = true;
802 FOR_EACH_EDGE (e, ei, bb->preds)
804 if (! first)
806 fputs (";; ", outf);
807 fprintf (outf, "%s ", s_indent);
809 first = false;
810 dump_edge_info (outf, e, flags, 0);
811 fputc ('\n', outf);
813 if (first)
814 fputc ('\n', outf);
817 if (do_footer)
819 fputs (";; ", outf);
820 fprintf (outf, "%s succ: ", s_indent);
821 first = true;
822 FOR_EACH_EDGE (e, ei, bb->succs)
824 if (! first)
826 fputs (";; ", outf);
827 fprintf (outf, "%s ", s_indent);
829 first = false;
830 dump_edge_info (outf, e, flags, 1);
831 fputc ('\n', outf);
833 if (first)
834 fputc ('\n', outf);
838 /* Dumps a brief description of cfg to FILE. */
840 void
841 brief_dump_cfg (FILE *file, dump_flags_t flags)
843 basic_block bb;
845 FOR_EACH_BB_FN (bb, cfun)
847 dump_bb_info (file, bb, 0, flags & TDF_DETAILS, true, true);
851 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
852 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
853 redirected to destination of TAKEN_EDGE.
855 This function may leave the profile inconsistent in the case TAKEN_EDGE
856 frequency or count is believed to be lower than FREQUENCY or COUNT
857 respectively. */
858 void
859 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
860 profile_count count, edge taken_edge)
862 edge c;
863 int prob;
864 edge_iterator ei;
866 if (bb->count < count)
868 if (dump_file)
869 fprintf (dump_file, "bb %i count became negative after threading",
870 bb->index);
872 bb->count -= count;
874 bb->frequency -= edge_frequency;
875 if (bb->frequency < 0)
876 bb->frequency = 0;
878 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
879 Watch for overflows. */
880 if (bb->frequency)
881 prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency);
882 else
883 prob = 0;
884 if (prob > taken_edge->probability)
886 if (dump_file)
887 fprintf (dump_file, "Jump threading proved probability of edge "
888 "%i->%i too small (it is %i, should be %i).\n",
889 taken_edge->src->index, taken_edge->dest->index,
890 taken_edge->probability, prob);
891 prob = taken_edge->probability * 6 / 8;
894 /* Now rescale the probabilities. */
895 taken_edge->probability -= prob;
896 prob = REG_BR_PROB_BASE - prob;
897 if (prob <= 0)
899 if (dump_file)
900 fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
901 "frequency of block should end up being 0, it is %i\n",
902 bb->index, bb->frequency);
903 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
904 ei = ei_start (bb->succs);
905 ei_next (&ei);
906 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
907 c->probability = 0;
909 else if (prob != REG_BR_PROB_BASE)
911 int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
913 FOR_EACH_EDGE (c, ei, bb->succs)
915 /* Protect from overflow due to additional scaling. */
916 if (c->probability > prob)
917 c->probability = REG_BR_PROB_BASE;
918 else
920 c->probability = RDIV (c->probability * scale, 65536);
921 if (c->probability > REG_BR_PROB_BASE)
922 c->probability = REG_BR_PROB_BASE;
927 gcc_assert (bb == taken_edge->src);
928 if (taken_edge->count < count)
930 if (dump_file)
931 fprintf (dump_file, "edge %i->%i count became negative after threading",
932 taken_edge->src->index, taken_edge->dest->index);
934 taken_edge->count -= count;
937 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
938 by NUM/DEN, in int arithmetic. May lose some accuracy. */
939 void
940 scale_bbs_frequencies_int (basic_block *bbs, int nbbs, int num, int den)
942 int i;
943 edge e;
944 if (num < 0)
945 num = 0;
947 /* Scale NUM and DEN to avoid overflows. Frequencies are in order of
948 10^4, if we make DEN <= 10^3, we can afford to upscale by 100
949 and still safely fit in int during calculations. */
950 if (den > 1000)
952 if (num > 1000000)
953 return;
955 num = RDIV (1000 * num, den);
956 den = 1000;
958 if (num > 100 * den)
959 return;
961 for (i = 0; i < nbbs; i++)
963 edge_iterator ei;
964 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
965 /* Make sure the frequencies do not grow over BB_FREQ_MAX. */
966 if (bbs[i]->frequency > BB_FREQ_MAX)
967 bbs[i]->frequency = BB_FREQ_MAX;
968 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
969 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
970 e->count = e->count.apply_scale (num, den);
974 /* numbers smaller than this value are safe to multiply without getting
975 64bit overflow. */
976 #define MAX_SAFE_MULTIPLIER (1 << (sizeof (int64_t) * 4 - 1))
978 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
979 by NUM/DEN, in gcov_type arithmetic. More accurate than previous
980 function but considerably slower. */
981 void
982 scale_bbs_frequencies_gcov_type (basic_block *bbs, int nbbs, gcov_type num,
983 gcov_type den)
985 int i;
986 edge e;
987 gcov_type fraction = RDIV (num * 65536, den);
989 gcc_assert (fraction >= 0);
991 if (num < MAX_SAFE_MULTIPLIER)
992 for (i = 0; i < nbbs; i++)
994 edge_iterator ei;
995 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
996 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
997 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
998 else
999 bbs[i]->count = bbs[i]->count.apply_scale (fraction, 65536);
1000 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1001 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
1002 e->count = e->count.apply_scale (num, den);
1003 else
1004 e->count = e->count.apply_scale (fraction, 65536);
1006 else
1007 for (i = 0; i < nbbs; i++)
1009 edge_iterator ei;
1010 if (sizeof (gcov_type) > sizeof (int))
1011 bbs[i]->frequency = RDIV (bbs[i]->frequency * num, den);
1012 else
1013 bbs[i]->frequency = RDIV (bbs[i]->frequency * fraction, 65536);
1014 bbs[i]->count = bbs[i]->count.apply_scale (fraction, 65536);
1015 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1016 e->count = e->count.apply_scale (fraction, 65536);
1020 /* Multiply all frequencies of basic blocks in array BBS of length NBBS
1021 by NUM/DEN, in profile_count arithmetic. More accurate than previous
1022 function but considerably slower. */
1023 void
1024 scale_bbs_frequencies_profile_count (basic_block *bbs, int nbbs,
1025 profile_count num, profile_count den)
1027 int i;
1028 edge e;
1030 for (i = 0; i < nbbs; i++)
1032 edge_iterator ei;
1033 bbs[i]->frequency = RDIV (bbs[i]->frequency * num.to_gcov_type (),
1034 den.to_gcov_type ());
1035 bbs[i]->count = bbs[i]->count.apply_scale (num, den);
1036 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1037 e->count = e->count.apply_scale (num, den);
1041 /* Helper types for hash tables. */
1043 struct htab_bb_copy_original_entry
1045 /* Block we are attaching info to. */
1046 int index1;
1047 /* Index of original or copy (depending on the hashtable) */
1048 int index2;
1051 struct bb_copy_hasher : nofree_ptr_hash <htab_bb_copy_original_entry>
1053 static inline hashval_t hash (const htab_bb_copy_original_entry *);
1054 static inline bool equal (const htab_bb_copy_original_entry *existing,
1055 const htab_bb_copy_original_entry * candidate);
1058 inline hashval_t
1059 bb_copy_hasher::hash (const htab_bb_copy_original_entry *data)
1061 return data->index1;
1064 inline bool
1065 bb_copy_hasher::equal (const htab_bb_copy_original_entry *data,
1066 const htab_bb_copy_original_entry *data2)
1068 return data->index1 == data2->index1;
1071 /* Data structures used to maintain mapping between basic blocks and
1072 copies. */
1073 static hash_table<bb_copy_hasher> *bb_original;
1074 static hash_table<bb_copy_hasher> *bb_copy;
1076 /* And between loops and copies. */
1077 static hash_table<bb_copy_hasher> *loop_copy;
1078 static object_allocator<htab_bb_copy_original_entry> *original_copy_bb_pool;
1080 /* Initialize the data structures to maintain mapping between blocks
1081 and its copies. */
1082 void
1083 initialize_original_copy_tables (void)
1085 original_copy_bb_pool = new object_allocator<htab_bb_copy_original_entry>
1086 ("original_copy");
1087 bb_original = new hash_table<bb_copy_hasher> (10);
1088 bb_copy = new hash_table<bb_copy_hasher> (10);
1089 loop_copy = new hash_table<bb_copy_hasher> (10);
1092 /* Reset the data structures to maintain mapping between blocks and
1093 its copies. */
1095 void
1096 reset_original_copy_tables (void)
1098 gcc_assert (original_copy_bb_pool);
1099 bb_original->empty ();
1100 bb_copy->empty ();
1101 loop_copy->empty ();
1104 /* Free the data structures to maintain mapping between blocks and
1105 its copies. */
1106 void
1107 free_original_copy_tables (void)
1109 gcc_assert (original_copy_bb_pool);
1110 delete bb_copy;
1111 bb_copy = NULL;
1112 delete bb_original;
1113 bb_original = NULL;
1114 delete loop_copy;
1115 loop_copy = NULL;
1116 delete original_copy_bb_pool;
1117 original_copy_bb_pool = NULL;
1120 /* Return true iff we have had a call to initialize_original_copy_tables
1121 without a corresponding call to free_original_copy_tables. */
1123 bool
1124 original_copy_tables_initialized_p (void)
1126 return original_copy_bb_pool != NULL;
1129 /* Removes the value associated with OBJ from table TAB. */
1131 static void
1132 copy_original_table_clear (hash_table<bb_copy_hasher> *tab, unsigned obj)
1134 htab_bb_copy_original_entry **slot;
1135 struct htab_bb_copy_original_entry key, *elt;
1137 if (!original_copy_bb_pool)
1138 return;
1140 key.index1 = obj;
1141 slot = tab->find_slot (&key, NO_INSERT);
1142 if (!slot)
1143 return;
1145 elt = *slot;
1146 tab->clear_slot (slot);
1147 original_copy_bb_pool->remove (elt);
1150 /* Sets the value associated with OBJ in table TAB to VAL.
1151 Do nothing when data structures are not initialized. */
1153 static void
1154 copy_original_table_set (hash_table<bb_copy_hasher> *tab,
1155 unsigned obj, unsigned val)
1157 struct htab_bb_copy_original_entry **slot;
1158 struct htab_bb_copy_original_entry key;
1160 if (!original_copy_bb_pool)
1161 return;
1163 key.index1 = obj;
1164 slot = tab->find_slot (&key, INSERT);
1165 if (!*slot)
1167 *slot = original_copy_bb_pool->allocate ();
1168 (*slot)->index1 = obj;
1170 (*slot)->index2 = val;
1173 /* Set original for basic block. Do nothing when data structures are not
1174 initialized so passes not needing this don't need to care. */
1175 void
1176 set_bb_original (basic_block bb, basic_block original)
1178 copy_original_table_set (bb_original, bb->index, original->index);
1181 /* Get the original basic block. */
1182 basic_block
1183 get_bb_original (basic_block bb)
1185 struct htab_bb_copy_original_entry *entry;
1186 struct htab_bb_copy_original_entry key;
1188 gcc_assert (original_copy_bb_pool);
1190 key.index1 = bb->index;
1191 entry = bb_original->find (&key);
1192 if (entry)
1193 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1194 else
1195 return NULL;
1198 /* Set copy for basic block. Do nothing when data structures are not
1199 initialized so passes not needing this don't need to care. */
1200 void
1201 set_bb_copy (basic_block bb, basic_block copy)
1203 copy_original_table_set (bb_copy, bb->index, copy->index);
1206 /* Get the copy of basic block. */
1207 basic_block
1208 get_bb_copy (basic_block bb)
1210 struct htab_bb_copy_original_entry *entry;
1211 struct htab_bb_copy_original_entry key;
1213 gcc_assert (original_copy_bb_pool);
1215 key.index1 = bb->index;
1216 entry = bb_copy->find (&key);
1217 if (entry)
1218 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1219 else
1220 return NULL;
1223 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1224 initialized so passes not needing this don't need to care. */
1226 void
1227 set_loop_copy (struct loop *loop, struct loop *copy)
1229 if (!copy)
1230 copy_original_table_clear (loop_copy, loop->num);
1231 else
1232 copy_original_table_set (loop_copy, loop->num, copy->num);
1235 /* Get the copy of LOOP. */
1237 struct loop *
1238 get_loop_copy (struct loop *loop)
1240 struct htab_bb_copy_original_entry *entry;
1241 struct htab_bb_copy_original_entry key;
1243 gcc_assert (original_copy_bb_pool);
1245 key.index1 = loop->num;
1246 entry = loop_copy->find (&key);
1247 if (entry)
1248 return get_loop (cfun, entry->index2);
1249 else
1250 return NULL;