* gnat.dg/nan_max.adb: New test.
[official-gcc.git] / gcc / cfg.c
blob158c3182d6218c2360eb0aefa5f4d6e6690d1e37
1 /* Control flow graph manipulation code for GNU compiler.
2 Copyright (C) 1987-2014 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 "ggc.h"
54 #include "hash-table.h"
55 #include "alloc-pool.h"
56 #include "tree.h"
57 #include "predict.h"
58 #include "vec.h"
59 #include "hashtab.h"
60 #include "hash-set.h"
61 #include "machmode.h"
62 #include "tm.h"
63 #include "hard-reg-set.h"
64 #include "input.h"
65 #include "function.h"
66 #include "dominance.h"
67 #include "cfg.h"
68 #include "cfganal.h"
69 #include "basic-block.h"
70 #include "df.h"
71 #include "cfgloop.h" /* FIXME: For struct loop. */
72 #include "dumpfile.h"
75 #define RDIV(X,Y) (((X) + (Y) / 2) / (Y))
77 /* Called once at initialization time. */
79 void
80 init_flow (struct function *the_fun)
82 if (!the_fun->cfg)
83 the_fun->cfg = ggc_cleared_alloc<control_flow_graph> ();
84 n_edges_for_fn (the_fun) = 0;
85 ENTRY_BLOCK_PTR_FOR_FN (the_fun)
86 = ggc_cleared_alloc<basic_block_def> ();
87 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->index = ENTRY_BLOCK;
88 EXIT_BLOCK_PTR_FOR_FN (the_fun)
89 = ggc_cleared_alloc<basic_block_def> ();
90 EXIT_BLOCK_PTR_FOR_FN (the_fun)->index = EXIT_BLOCK;
91 ENTRY_BLOCK_PTR_FOR_FN (the_fun)->next_bb
92 = EXIT_BLOCK_PTR_FOR_FN (the_fun);
93 EXIT_BLOCK_PTR_FOR_FN (the_fun)->prev_bb
94 = ENTRY_BLOCK_PTR_FOR_FN (the_fun);
97 /* Helper function for remove_edge and clear_edges. Frees edge structure
98 without actually removing it from the pred/succ arrays. */
100 static void
101 free_edge (edge e)
103 n_edges_for_fn (cfun)--;
104 ggc_free (e);
107 /* Free the memory associated with the edge structures. */
109 void
110 clear_edges (void)
112 basic_block bb;
113 edge e;
114 edge_iterator ei;
116 FOR_EACH_BB_FN (bb, cfun)
118 FOR_EACH_EDGE (e, ei, bb->succs)
119 free_edge (e);
120 vec_safe_truncate (bb->succs, 0);
121 vec_safe_truncate (bb->preds, 0);
124 FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
125 free_edge (e);
126 vec_safe_truncate (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds, 0);
127 vec_safe_truncate (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs, 0);
129 gcc_assert (!n_edges_for_fn (cfun));
132 /* Allocate memory for basic_block. */
134 basic_block
135 alloc_block (void)
137 basic_block bb;
138 bb = ggc_cleared_alloc<basic_block_def> ();
139 return bb;
142 /* Link block B to chain after AFTER. */
143 void
144 link_block (basic_block b, basic_block after)
146 b->next_bb = after->next_bb;
147 b->prev_bb = after;
148 after->next_bb = b;
149 b->next_bb->prev_bb = b;
152 /* Unlink block B from chain. */
153 void
154 unlink_block (basic_block b)
156 b->next_bb->prev_bb = b->prev_bb;
157 b->prev_bb->next_bb = b->next_bb;
158 b->prev_bb = NULL;
159 b->next_bb = NULL;
162 /* Sequentially order blocks and compact the arrays. */
163 void
164 compact_blocks (void)
166 int i;
168 SET_BASIC_BLOCK_FOR_FN (cfun, ENTRY_BLOCK, ENTRY_BLOCK_PTR_FOR_FN (cfun));
169 SET_BASIC_BLOCK_FOR_FN (cfun, EXIT_BLOCK, EXIT_BLOCK_PTR_FOR_FN (cfun));
171 if (df)
172 df_compact_blocks ();
173 else
175 basic_block bb;
177 i = NUM_FIXED_BLOCKS;
178 FOR_EACH_BB_FN (bb, cfun)
180 SET_BASIC_BLOCK_FOR_FN (cfun, i, bb);
181 bb->index = i;
182 i++;
184 gcc_assert (i == n_basic_blocks_for_fn (cfun));
186 for (; i < last_basic_block_for_fn (cfun); i++)
187 SET_BASIC_BLOCK_FOR_FN (cfun, i, NULL);
189 last_basic_block_for_fn (cfun) = n_basic_blocks_for_fn (cfun);
192 /* Remove block B from the basic block array. */
194 void
195 expunge_block (basic_block b)
197 unlink_block (b);
198 SET_BASIC_BLOCK_FOR_FN (cfun, b->index, NULL);
199 n_basic_blocks_for_fn (cfun)--;
200 /* We should be able to ggc_free here, but we are not.
201 The dead SSA_NAMES are left pointing to dead statements that are pointing
202 to dead basic blocks making garbage collector to die.
203 We should be able to release all dead SSA_NAMES and at the same time we should
204 clear out BB pointer of dead statements consistently. */
207 /* Connect E to E->src. */
209 static inline void
210 connect_src (edge e)
212 vec_safe_push (e->src->succs, e);
213 df_mark_solutions_dirty ();
216 /* Connect E to E->dest. */
218 static inline void
219 connect_dest (edge e)
221 basic_block dest = e->dest;
222 vec_safe_push (dest->preds, e);
223 e->dest_idx = EDGE_COUNT (dest->preds) - 1;
224 df_mark_solutions_dirty ();
227 /* Disconnect edge E from E->src. */
229 static inline void
230 disconnect_src (edge e)
232 basic_block src = e->src;
233 edge_iterator ei;
234 edge tmp;
236 for (ei = ei_start (src->succs); (tmp = ei_safe_edge (ei)); )
238 if (tmp == e)
240 src->succs->unordered_remove (ei.index);
241 df_mark_solutions_dirty ();
242 return;
244 else
245 ei_next (&ei);
248 gcc_unreachable ();
251 /* Disconnect edge E from E->dest. */
253 static inline void
254 disconnect_dest (edge e)
256 basic_block dest = e->dest;
257 unsigned int dest_idx = e->dest_idx;
259 dest->preds->unordered_remove (dest_idx);
261 /* If we removed an edge in the middle of the edge vector, we need
262 to update dest_idx of the edge that moved into the "hole". */
263 if (dest_idx < EDGE_COUNT (dest->preds))
264 EDGE_PRED (dest, dest_idx)->dest_idx = dest_idx;
265 df_mark_solutions_dirty ();
268 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
269 created edge. Use this only if you are sure that this edge can't
270 possibly already exist. */
272 edge
273 unchecked_make_edge (basic_block src, basic_block dst, int flags)
275 edge e;
276 e = ggc_cleared_alloc<edge_def> ();
277 n_edges_for_fn (cfun)++;
279 e->src = src;
280 e->dest = dst;
281 e->flags = flags;
283 connect_src (e);
284 connect_dest (e);
286 execute_on_growing_pred (e);
287 return e;
290 /* Create an edge connecting SRC and DST with FLAGS optionally using
291 edge cache CACHE. Return the new edge, NULL if already exist. */
293 edge
294 cached_make_edge (sbitmap edge_cache, basic_block src, basic_block dst, int flags)
296 if (edge_cache == NULL
297 || src == ENTRY_BLOCK_PTR_FOR_FN (cfun)
298 || dst == EXIT_BLOCK_PTR_FOR_FN (cfun))
299 return make_edge (src, dst, flags);
301 /* Does the requested edge already exist? */
302 if (! bitmap_bit_p (edge_cache, dst->index))
304 /* The edge does not exist. Create one and update the
305 cache. */
306 bitmap_set_bit (edge_cache, dst->index);
307 return unchecked_make_edge (src, dst, flags);
310 /* At this point, we know that the requested edge exists. Adjust
311 flags if necessary. */
312 if (flags)
314 edge e = find_edge (src, dst);
315 e->flags |= flags;
318 return NULL;
321 /* Create an edge connecting SRC and DEST with flags FLAGS. Return newly
322 created edge or NULL if already exist. */
324 edge
325 make_edge (basic_block src, basic_block dest, int flags)
327 edge e = find_edge (src, dest);
329 /* Make sure we don't add duplicate edges. */
330 if (e)
332 e->flags |= flags;
333 return NULL;
336 return unchecked_make_edge (src, dest, flags);
339 /* Create an edge connecting SRC to DEST and set probability by knowing
340 that it is the single edge leaving SRC. */
342 edge
343 make_single_succ_edge (basic_block src, basic_block dest, int flags)
345 edge e = make_edge (src, dest, flags);
347 e->probability = REG_BR_PROB_BASE;
348 e->count = src->count;
349 return e;
352 /* This function will remove an edge from the flow graph. */
354 void
355 remove_edge_raw (edge e)
357 remove_predictions_associated_with_edge (e);
358 execute_on_shrinking_pred (e);
360 disconnect_src (e);
361 disconnect_dest (e);
363 free_edge (e);
366 /* Redirect an edge's successor from one block to another. */
368 void
369 redirect_edge_succ (edge e, basic_block new_succ)
371 execute_on_shrinking_pred (e);
373 disconnect_dest (e);
375 e->dest = new_succ;
377 /* Reconnect the edge to the new successor block. */
378 connect_dest (e);
380 execute_on_growing_pred (e);
383 /* Redirect an edge's predecessor from one block to another. */
385 void
386 redirect_edge_pred (edge e, basic_block new_pred)
388 disconnect_src (e);
390 e->src = new_pred;
392 /* Reconnect the edge to the new predecessor block. */
393 connect_src (e);
396 /* Clear all basic block flags that do not have to be preserved. */
397 void
398 clear_bb_flags (void)
400 basic_block bb;
402 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
403 bb->flags &= BB_FLAGS_TO_PRESERVE;
406 /* Check the consistency of profile information. We can't do that
407 in verify_flow_info, as the counts may get invalid for incompletely
408 solved graphs, later eliminating of conditionals or roundoff errors.
409 It is still practical to have them reported for debugging of simple
410 testcases. */
411 static void
412 check_bb_profile (basic_block bb, FILE * file, int indent, int flags)
414 edge e;
415 int sum = 0;
416 gcov_type lsum;
417 edge_iterator ei;
418 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
419 char *s_indent = (char *) alloca ((size_t) indent + 1);
420 memset ((void *) s_indent, ' ', (size_t) indent);
421 s_indent[indent] = '\0';
423 if (profile_status_for_fn (fun) == PROFILE_ABSENT)
424 return;
426 if (bb != EXIT_BLOCK_PTR_FOR_FN (fun))
428 FOR_EACH_EDGE (e, ei, bb->succs)
429 sum += e->probability;
430 if (EDGE_COUNT (bb->succs) && abs (sum - REG_BR_PROB_BASE) > 100)
431 fprintf (file, "%s%sInvalid sum of outgoing probabilities %.1f%%\n",
432 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
433 sum * 100.0 / REG_BR_PROB_BASE);
434 lsum = 0;
435 FOR_EACH_EDGE (e, ei, bb->succs)
436 lsum += e->count;
437 if (EDGE_COUNT (bb->succs)
438 && (lsum - bb->count > 100 || lsum - bb->count < -100))
439 fprintf (file, "%s%sInvalid sum of outgoing counts %i, should be %i\n",
440 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
441 (int) lsum, (int) bb->count);
443 if (bb != ENTRY_BLOCK_PTR_FOR_FN (fun))
445 sum = 0;
446 FOR_EACH_EDGE (e, ei, bb->preds)
447 sum += EDGE_FREQUENCY (e);
448 if (abs (sum - bb->frequency) > 100)
449 fprintf (file,
450 "%s%sInvalid sum of incoming frequencies %i, should be %i\n",
451 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
452 sum, bb->frequency);
453 lsum = 0;
454 FOR_EACH_EDGE (e, ei, bb->preds)
455 lsum += e->count;
456 if (lsum - bb->count > 100 || lsum - bb->count < -100)
457 fprintf (file, "%s%sInvalid sum of incoming counts %i, should be %i\n",
458 (flags & TDF_COMMENT) ? ";; " : "", s_indent,
459 (int) lsum, (int) bb->count);
461 if (BB_PARTITION (bb) == BB_COLD_PARTITION)
463 /* Warn about inconsistencies in the partitioning that are
464 currently caused by profile insanities created via optimization. */
465 if (!probably_never_executed_bb_p (fun, bb))
466 fprintf (file, "%s%sBlock in cold partition with hot count\n",
467 (flags & TDF_COMMENT) ? ";; " : "", s_indent);
468 FOR_EACH_EDGE (e, ei, bb->preds)
470 if (!probably_never_executed_edge_p (fun, e))
471 fprintf (file,
472 "%s%sBlock in cold partition with incoming hot edge\n",
473 (flags & TDF_COMMENT) ? ";; " : "", s_indent);
478 void
479 dump_edge_info (FILE *file, edge e, int flags, int do_succ)
481 basic_block side = (do_succ ? e->dest : e->src);
482 bool do_details = false;
484 if ((flags & TDF_DETAILS) != 0
485 && (flags & TDF_SLIM) == 0)
486 do_details = true;
488 if (side->index == ENTRY_BLOCK)
489 fputs (" ENTRY", file);
490 else if (side->index == EXIT_BLOCK)
491 fputs (" EXIT", file);
492 else
493 fprintf (file, " %d", side->index);
495 if (e->probability && do_details)
496 fprintf (file, " [%.1f%%] ", e->probability * 100.0 / REG_BR_PROB_BASE);
498 if (e->count && do_details)
500 fputs (" count:", file);
501 fprintf (file, "%"PRId64, e->count);
504 if (e->flags && do_details)
506 static const char * const bitnames[] =
508 #define DEF_EDGE_FLAG(NAME,IDX) #NAME ,
509 #include "cfg-flags.def"
510 NULL
511 #undef DEF_EDGE_FLAG
513 bool comma = false;
514 int i, flags = e->flags;
516 gcc_assert (e->flags <= EDGE_ALL_FLAGS);
517 fputs (" (", file);
518 for (i = 0; flags; i++)
519 if (flags & (1 << i))
521 flags &= ~(1 << i);
523 if (comma)
524 fputc (',', file);
525 fputs (bitnames[i], file);
526 comma = true;
529 fputc (')', file);
533 DEBUG_FUNCTION void
534 debug (edge_def &ref)
536 /* FIXME (crowl): Is this desireable? */
537 dump_edge_info (stderr, &ref, 0, false);
538 dump_edge_info (stderr, &ref, 0, true);
541 DEBUG_FUNCTION void
542 debug (edge_def *ptr)
544 if (ptr)
545 debug (*ptr);
546 else
547 fprintf (stderr, "<nil>\n");
550 /* Simple routines to easily allocate AUX fields of basic blocks. */
552 static struct obstack block_aux_obstack;
553 static void *first_block_aux_obj = 0;
554 static struct obstack edge_aux_obstack;
555 static void *first_edge_aux_obj = 0;
557 /* Allocate a memory block of SIZE as BB->aux. The obstack must
558 be first initialized by alloc_aux_for_blocks. */
560 static void
561 alloc_aux_for_block (basic_block bb, int size)
563 /* Verify that aux field is clear. */
564 gcc_assert (!bb->aux && first_block_aux_obj);
565 bb->aux = obstack_alloc (&block_aux_obstack, size);
566 memset (bb->aux, 0, size);
569 /* Initialize the block_aux_obstack and if SIZE is nonzero, call
570 alloc_aux_for_block for each basic block. */
572 void
573 alloc_aux_for_blocks (int size)
575 static int initialized;
577 if (!initialized)
579 gcc_obstack_init (&block_aux_obstack);
580 initialized = 1;
582 else
583 /* Check whether AUX data are still allocated. */
584 gcc_assert (!first_block_aux_obj);
586 first_block_aux_obj = obstack_alloc (&block_aux_obstack, 0);
587 if (size)
589 basic_block bb;
591 FOR_ALL_BB_FN (bb, cfun)
592 alloc_aux_for_block (bb, size);
596 /* Clear AUX pointers of all blocks. */
598 void
599 clear_aux_for_blocks (void)
601 basic_block bb;
603 FOR_ALL_BB_FN (bb, cfun)
604 bb->aux = NULL;
607 /* Free data allocated in block_aux_obstack and clear AUX pointers
608 of all blocks. */
610 void
611 free_aux_for_blocks (void)
613 gcc_assert (first_block_aux_obj);
614 obstack_free (&block_aux_obstack, first_block_aux_obj);
615 first_block_aux_obj = NULL;
617 clear_aux_for_blocks ();
620 /* Allocate a memory edge of SIZE as E->aux. The obstack must
621 be first initialized by alloc_aux_for_edges. */
623 void
624 alloc_aux_for_edge (edge e, int size)
626 /* Verify that aux field is clear. */
627 gcc_assert (!e->aux && first_edge_aux_obj);
628 e->aux = obstack_alloc (&edge_aux_obstack, size);
629 memset (e->aux, 0, size);
632 /* Initialize the edge_aux_obstack and if SIZE is nonzero, call
633 alloc_aux_for_edge for each basic edge. */
635 void
636 alloc_aux_for_edges (int size)
638 static int initialized;
640 if (!initialized)
642 gcc_obstack_init (&edge_aux_obstack);
643 initialized = 1;
645 else
646 /* Check whether AUX data are still allocated. */
647 gcc_assert (!first_edge_aux_obj);
649 first_edge_aux_obj = obstack_alloc (&edge_aux_obstack, 0);
650 if (size)
652 basic_block bb;
654 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
655 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
657 edge e;
658 edge_iterator ei;
660 FOR_EACH_EDGE (e, ei, bb->succs)
661 alloc_aux_for_edge (e, size);
666 /* Clear AUX pointers of all edges. */
668 void
669 clear_aux_for_edges (void)
671 basic_block bb;
672 edge e;
674 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
675 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
677 edge_iterator ei;
678 FOR_EACH_EDGE (e, ei, bb->succs)
679 e->aux = NULL;
683 /* Free data allocated in edge_aux_obstack and clear AUX pointers
684 of all edges. */
686 void
687 free_aux_for_edges (void)
689 gcc_assert (first_edge_aux_obj);
690 obstack_free (&edge_aux_obstack, first_edge_aux_obj);
691 first_edge_aux_obj = NULL;
693 clear_aux_for_edges ();
696 DEBUG_FUNCTION void
697 debug_bb (basic_block bb)
699 dump_bb (stderr, bb, 0, dump_flags);
702 DEBUG_FUNCTION basic_block
703 debug_bb_n (int n)
705 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, n);
706 debug_bb (bb);
707 return bb;
710 /* Dumps cfg related information about basic block BB to OUTF.
711 If HEADER is true, dump things that appear before the instructions
712 contained in BB. If FOOTER is true, dump things that appear after.
713 Flags are the TDF_* masks as documented in dumpfile.h.
714 NB: With TDF_DETAILS, it is assumed that cfun is available, so
715 that maybe_hot_bb_p and probably_never_executed_bb_p don't ICE. */
717 void
718 dump_bb_info (FILE *outf, basic_block bb, int indent, int flags,
719 bool do_header, bool do_footer)
721 edge_iterator ei;
722 edge e;
723 static const char * const bb_bitnames[] =
725 #define DEF_BASIC_BLOCK_FLAG(NAME,IDX) #NAME ,
726 #include "cfg-flags.def"
727 NULL
728 #undef DEF_BASIC_BLOCK_FLAG
730 const unsigned n_bitnames = sizeof (bb_bitnames) / sizeof (char *);
731 bool first;
732 char *s_indent = (char *) alloca ((size_t) indent + 1);
733 memset ((void *) s_indent, ' ', (size_t) indent);
734 s_indent[indent] = '\0';
736 gcc_assert (bb->flags <= BB_ALL_FLAGS);
738 if (do_header)
740 unsigned i;
742 if (flags & TDF_COMMENT)
743 fputs (";; ", outf);
744 fprintf (outf, "%sbasic block %d, loop depth %d",
745 s_indent, bb->index, bb_loop_depth (bb));
746 if (flags & TDF_DETAILS)
748 struct function *fun = DECL_STRUCT_FUNCTION (current_function_decl);
749 fprintf (outf, ", count " "%"PRId64,
750 (int64_t) bb->count);
751 fprintf (outf, ", freq %i", bb->frequency);
752 if (maybe_hot_bb_p (fun, bb))
753 fputs (", maybe hot", outf);
754 if (probably_never_executed_bb_p (fun, bb))
755 fputs (", probably never executed", outf);
757 fputc ('\n', outf);
759 if (flags & TDF_DETAILS)
761 check_bb_profile (bb, outf, indent, flags);
762 if (flags & TDF_COMMENT)
763 fputs (";; ", outf);
764 fprintf (outf, "%s prev block ", s_indent);
765 if (bb->prev_bb)
766 fprintf (outf, "%d", bb->prev_bb->index);
767 else
768 fprintf (outf, "(nil)");
769 fprintf (outf, ", next block ");
770 if (bb->next_bb)
771 fprintf (outf, "%d", bb->next_bb->index);
772 else
773 fprintf (outf, "(nil)");
775 fputs (", flags:", outf);
776 first = true;
777 for (i = 0; i < n_bitnames; i++)
778 if (bb->flags & (1 << i))
780 if (first)
781 fputs (" (", outf);
782 else
783 fputs (", ", outf);
784 first = false;
785 fputs (bb_bitnames[i], outf);
787 if (!first)
788 fputc (')', outf);
789 fputc ('\n', outf);
792 if (flags & TDF_COMMENT)
793 fputs (";; ", outf);
794 fprintf (outf, "%s pred: ", s_indent);
795 first = true;
796 FOR_EACH_EDGE (e, ei, bb->preds)
798 if (! first)
800 if (flags & TDF_COMMENT)
801 fputs (";; ", outf);
802 fprintf (outf, "%s ", s_indent);
804 first = false;
805 dump_edge_info (outf, e, flags, 0);
806 fputc ('\n', outf);
808 if (first)
809 fputc ('\n', outf);
812 if (do_footer)
814 if (flags & TDF_COMMENT)
815 fputs (";; ", outf);
816 fprintf (outf, "%s succ: ", s_indent);
817 first = true;
818 FOR_EACH_EDGE (e, ei, bb->succs)
820 if (! first)
822 if (flags & TDF_COMMENT)
823 fputs (";; ", outf);
824 fprintf (outf, "%s ", s_indent);
826 first = false;
827 dump_edge_info (outf, e, flags, 1);
828 fputc ('\n', outf);
830 if (first)
831 fputc ('\n', outf);
835 /* Dumps a brief description of cfg to FILE. */
837 void
838 brief_dump_cfg (FILE *file, int flags)
840 basic_block bb;
842 FOR_EACH_BB_FN (bb, cfun)
844 dump_bb_info (file, bb, 0,
845 flags & (TDF_COMMENT | TDF_DETAILS),
846 true, true);
850 /* An edge originally destinating BB of FREQUENCY and COUNT has been proved to
851 leave the block by TAKEN_EDGE. Update profile of BB such that edge E can be
852 redirected to destination of TAKEN_EDGE.
854 This function may leave the profile inconsistent in the case TAKEN_EDGE
855 frequency or count is believed to be lower than FREQUENCY or COUNT
856 respectively. */
857 void
858 update_bb_profile_for_threading (basic_block bb, int edge_frequency,
859 gcov_type count, edge taken_edge)
861 edge c;
862 int prob;
863 edge_iterator ei;
865 bb->count -= count;
866 if (bb->count < 0)
868 if (dump_file)
869 fprintf (dump_file, "bb %i count became negative after threading",
870 bb->index);
871 bb->count = 0;
874 /* Compute the probability of TAKEN_EDGE being reached via threaded edge.
875 Watch for overflows. */
876 if (bb->frequency)
877 prob = GCOV_COMPUTE_SCALE (edge_frequency, bb->frequency);
878 else
879 prob = 0;
880 if (prob > taken_edge->probability)
882 if (dump_file)
883 fprintf (dump_file, "Jump threading proved probability of edge "
884 "%i->%i too small (it is %i, should be %i).\n",
885 taken_edge->src->index, taken_edge->dest->index,
886 taken_edge->probability, prob);
887 prob = taken_edge->probability;
890 /* Now rescale the probabilities. */
891 taken_edge->probability -= prob;
892 prob = REG_BR_PROB_BASE - prob;
893 bb->frequency -= edge_frequency;
894 if (bb->frequency < 0)
895 bb->frequency = 0;
896 if (prob <= 0)
898 if (dump_file)
899 fprintf (dump_file, "Edge frequencies of bb %i has been reset, "
900 "frequency of block should end up being 0, it is %i\n",
901 bb->index, bb->frequency);
902 EDGE_SUCC (bb, 0)->probability = REG_BR_PROB_BASE;
903 ei = ei_start (bb->succs);
904 ei_next (&ei);
905 for (; (c = ei_safe_edge (ei)); ei_next (&ei))
906 c->probability = 0;
908 else if (prob != REG_BR_PROB_BASE)
910 int scale = RDIV (65536 * REG_BR_PROB_BASE, prob);
912 FOR_EACH_EDGE (c, ei, bb->succs)
914 /* Protect from overflow due to additional scaling. */
915 if (c->probability > prob)
916 c->probability = REG_BR_PROB_BASE;
917 else
919 c->probability = RDIV (c->probability * scale, 65536);
920 if (c->probability > REG_BR_PROB_BASE)
921 c->probability = REG_BR_PROB_BASE;
926 gcc_assert (bb == taken_edge->src);
927 taken_edge->count -= count;
928 if (taken_edge->count < 0)
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);
933 taken_edge->count = 0;
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 = RDIV (bbs[i]->count * num, den);
969 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
970 e->count = RDIV (e->count * 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 = RDIV (bbs[i]->count * num, den);
998 else
999 bbs[i]->count = RDIV (bbs[i]->count * fraction, 65536);
1000 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1001 if (bbs[i]->count <= MAX_SAFE_MULTIPLIER)
1002 e->count = RDIV (e->count * num, den);
1003 else
1004 e->count = RDIV (e->count * 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 = RDIV (bbs[i]->count * fraction, 65536);
1015 FOR_EACH_EDGE (e, ei, bbs[i]->succs)
1016 e->count = RDIV (e->count * fraction, 65536);
1020 /* Helper types for hash tables. */
1022 struct htab_bb_copy_original_entry
1024 /* Block we are attaching info to. */
1025 int index1;
1026 /* Index of original or copy (depending on the hashtable) */
1027 int index2;
1030 struct bb_copy_hasher : typed_noop_remove <htab_bb_copy_original_entry>
1032 typedef htab_bb_copy_original_entry value_type;
1033 typedef htab_bb_copy_original_entry compare_type;
1034 static inline hashval_t hash (const value_type *);
1035 static inline bool equal (const value_type *existing,
1036 const compare_type * candidate);
1039 inline hashval_t
1040 bb_copy_hasher::hash (const value_type *data)
1042 return data->index1;
1045 inline bool
1046 bb_copy_hasher::equal (const value_type *data, const compare_type *data2)
1048 return data->index1 == data2->index1;
1051 /* Data structures used to maintain mapping between basic blocks and
1052 copies. */
1053 static hash_table<bb_copy_hasher> *bb_original;
1054 static hash_table<bb_copy_hasher> *bb_copy;
1056 /* And between loops and copies. */
1057 static hash_table<bb_copy_hasher> *loop_copy;
1058 static alloc_pool original_copy_bb_pool;
1061 /* Initialize the data structures to maintain mapping between blocks
1062 and its copies. */
1063 void
1064 initialize_original_copy_tables (void)
1066 gcc_assert (!original_copy_bb_pool);
1067 original_copy_bb_pool
1068 = create_alloc_pool ("original_copy",
1069 sizeof (struct htab_bb_copy_original_entry), 10);
1070 bb_original = new hash_table<bb_copy_hasher> (10);
1071 bb_copy = new hash_table<bb_copy_hasher> (10);
1072 loop_copy = new hash_table<bb_copy_hasher> (10);
1075 /* Free the data structures to maintain mapping between blocks and
1076 its copies. */
1077 void
1078 free_original_copy_tables (void)
1080 gcc_assert (original_copy_bb_pool);
1081 delete bb_copy;
1082 bb_copy = NULL;
1083 delete bb_original;
1084 bb_copy = NULL;
1085 delete loop_copy;
1086 loop_copy = NULL;
1087 free_alloc_pool (original_copy_bb_pool);
1088 original_copy_bb_pool = NULL;
1091 /* Removes the value associated with OBJ from table TAB. */
1093 static void
1094 copy_original_table_clear (hash_table<bb_copy_hasher> *tab, unsigned obj)
1096 htab_bb_copy_original_entry **slot;
1097 struct htab_bb_copy_original_entry key, *elt;
1099 if (!original_copy_bb_pool)
1100 return;
1102 key.index1 = obj;
1103 slot = tab->find_slot (&key, NO_INSERT);
1104 if (!slot)
1105 return;
1107 elt = *slot;
1108 tab->clear_slot (slot);
1109 pool_free (original_copy_bb_pool, elt);
1112 /* Sets the value associated with OBJ in table TAB to VAL.
1113 Do nothing when data structures are not initialized. */
1115 static void
1116 copy_original_table_set (hash_table<bb_copy_hasher> *tab,
1117 unsigned obj, unsigned val)
1119 struct htab_bb_copy_original_entry **slot;
1120 struct htab_bb_copy_original_entry key;
1122 if (!original_copy_bb_pool)
1123 return;
1125 key.index1 = obj;
1126 slot = tab->find_slot (&key, INSERT);
1127 if (!*slot)
1129 *slot = (struct htab_bb_copy_original_entry *)
1130 pool_alloc (original_copy_bb_pool);
1131 (*slot)->index1 = obj;
1133 (*slot)->index2 = val;
1136 /* Set original for basic block. Do nothing when data structures are not
1137 initialized so passes not needing this don't need to care. */
1138 void
1139 set_bb_original (basic_block bb, basic_block original)
1141 copy_original_table_set (bb_original, bb->index, original->index);
1144 /* Get the original basic block. */
1145 basic_block
1146 get_bb_original (basic_block bb)
1148 struct htab_bb_copy_original_entry *entry;
1149 struct htab_bb_copy_original_entry key;
1151 gcc_assert (original_copy_bb_pool);
1153 key.index1 = bb->index;
1154 entry = bb_original->find (&key);
1155 if (entry)
1156 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1157 else
1158 return NULL;
1161 /* Set copy for basic block. Do nothing when data structures are not
1162 initialized so passes not needing this don't need to care. */
1163 void
1164 set_bb_copy (basic_block bb, basic_block copy)
1166 copy_original_table_set (bb_copy, bb->index, copy->index);
1169 /* Get the copy of basic block. */
1170 basic_block
1171 get_bb_copy (basic_block bb)
1173 struct htab_bb_copy_original_entry *entry;
1174 struct htab_bb_copy_original_entry key;
1176 gcc_assert (original_copy_bb_pool);
1178 key.index1 = bb->index;
1179 entry = bb_copy->find (&key);
1180 if (entry)
1181 return BASIC_BLOCK_FOR_FN (cfun, entry->index2);
1182 else
1183 return NULL;
1186 /* Set copy for LOOP to COPY. Do nothing when data structures are not
1187 initialized so passes not needing this don't need to care. */
1189 void
1190 set_loop_copy (struct loop *loop, struct loop *copy)
1192 if (!copy)
1193 copy_original_table_clear (loop_copy, loop->num);
1194 else
1195 copy_original_table_set (loop_copy, loop->num, copy->num);
1198 /* Get the copy of LOOP. */
1200 struct loop *
1201 get_loop_copy (struct loop *loop)
1203 struct htab_bb_copy_original_entry *entry;
1204 struct htab_bb_copy_original_entry key;
1206 gcc_assert (original_copy_bb_pool);
1208 key.index1 = loop->num;
1209 entry = loop_copy->find (&key);
1210 if (entry)
1211 return get_loop (cfun, entry->index2);
1212 else
1213 return NULL;