2013-11-21 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / profile.c
blob1d0e78ab4dc779b7c192122ed55837be50c84fc4
1 /* Calculate branch probabilities, and basic block execution counts.
2 Copyright (C) 1990-2013 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Generate basic block profile instrumentation and auxiliary files.
24 Profile generation is optimized, so that not all arcs in the basic
25 block graph need instrumenting. First, the BB graph is closed with
26 one entry (function start), and one exit (function exit). Any
27 ABNORMAL_EDGE cannot be instrumented (because there is no control
28 path to place the code). We close the graph by inserting fake
29 EDGE_FAKE edges to the EXIT_BLOCK, from the sources of abnormal
30 edges that do not go to the exit_block. We ignore such abnormal
31 edges. Naturally these fake edges are never directly traversed,
32 and so *cannot* be directly instrumented. Some other graph
33 massaging is done. To optimize the instrumentation we generate the
34 BB minimal span tree, only edges that are not on the span tree
35 (plus the entry point) need instrumenting. From that information
36 all other edge counts can be deduced. By construction all fake
37 edges must be on the spanning tree. We also attempt to place
38 EDGE_CRITICAL edges on the spanning tree.
40 The auxiliary files generated are <dumpbase>.gcno (at compile time)
41 and <dumpbase>.gcda (at run time). The format is
42 described in full in gcov-io.h. */
44 /* ??? Register allocation should use basic block execution counts to
45 give preference to the most commonly executed blocks. */
47 /* ??? Should calculate branch probabilities before instrumenting code, since
48 then we can use arc counts to help decide which arcs to instrument. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "rtl.h"
55 #include "flags.h"
56 #include "regs.h"
57 #include "expr.h"
58 #include "function.h"
59 #include "basic-block.h"
60 #include "diagnostic-core.h"
61 #include "coverage.h"
62 #include "value-prof.h"
63 #include "tree.h"
64 #include "gimple.h"
65 #include "gimple-iterator.h"
66 #include "tree-cfg.h"
67 #include "cfgloop.h"
68 #include "dumpfile.h"
69 #include "cgraph.h"
71 #include "profile.h"
73 struct bb_info {
74 unsigned int count_valid : 1;
76 /* Number of successor and predecessor edges. */
77 gcov_type succ_count;
78 gcov_type pred_count;
81 #define BB_INFO(b) ((struct bb_info *) (b)->aux)
84 /* Counter summary from the last set of coverage counts read. */
86 const struct gcov_ctr_summary *profile_info;
88 /* Counter working set information computed from the current counter
89 summary. Not initialized unless profile_info summary is non-NULL. */
90 static gcov_working_set_t gcov_working_sets[NUM_GCOV_WORKING_SETS];
92 /* Collect statistics on the performance of this pass for the entire source
93 file. */
95 static int total_num_blocks;
96 static int total_num_edges;
97 static int total_num_edges_ignored;
98 static int total_num_edges_instrumented;
99 static int total_num_blocks_created;
100 static int total_num_passes;
101 static int total_num_times_called;
102 static int total_hist_br_prob[20];
103 static int total_num_branches;
105 /* Forward declarations. */
106 static void find_spanning_tree (struct edge_list *);
108 /* Add edge instrumentation code to the entire insn chain.
110 F is the first insn of the chain.
111 NUM_BLOCKS is the number of basic blocks found in F. */
113 static unsigned
114 instrument_edges (struct edge_list *el)
116 unsigned num_instr_edges = 0;
117 int num_edges = NUM_EDGES (el);
118 basic_block bb;
120 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
122 edge e;
123 edge_iterator ei;
125 FOR_EACH_EDGE (e, ei, bb->succs)
127 struct edge_info *inf = EDGE_INFO (e);
129 if (!inf->ignore && !inf->on_tree)
131 gcc_assert (!(e->flags & EDGE_ABNORMAL));
132 if (dump_file)
133 fprintf (dump_file, "Edge %d to %d instrumented%s\n",
134 e->src->index, e->dest->index,
135 EDGE_CRITICAL_P (e) ? " (and split)" : "");
136 gimple_gen_edge_profiler (num_instr_edges++, e);
141 total_num_blocks_created += num_edges;
142 if (dump_file)
143 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges);
144 return num_instr_edges;
147 /* Add code to measure histograms for values in list VALUES. */
148 static void
149 instrument_values (histogram_values values)
151 unsigned i;
153 /* Emit code to generate the histograms before the insns. */
155 for (i = 0; i < values.length (); i++)
157 histogram_value hist = values[i];
158 unsigned t = COUNTER_FOR_HIST_TYPE (hist->type);
160 if (!coverage_counter_alloc (t, hist->n_counters))
161 continue;
163 switch (hist->type)
165 case HIST_TYPE_INTERVAL:
166 gimple_gen_interval_profiler (hist, t, 0);
167 break;
169 case HIST_TYPE_POW2:
170 gimple_gen_pow2_profiler (hist, t, 0);
171 break;
173 case HIST_TYPE_SINGLE_VALUE:
174 gimple_gen_one_value_profiler (hist, t, 0);
175 break;
177 case HIST_TYPE_CONST_DELTA:
178 gimple_gen_const_delta_profiler (hist, t, 0);
179 break;
181 case HIST_TYPE_INDIR_CALL:
182 gimple_gen_ic_profiler (hist, t, 0);
183 break;
185 case HIST_TYPE_AVERAGE:
186 gimple_gen_average_profiler (hist, t, 0);
187 break;
189 case HIST_TYPE_IOR:
190 gimple_gen_ior_profiler (hist, t, 0);
191 break;
193 case HIST_TYPE_TIME_PROFILE:
195 basic_block bb =
196 split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
197 gimple_stmt_iterator gsi = gsi_start_bb (bb);
199 gimple_gen_time_profiler (t, 0, gsi);
200 break;
203 default:
204 gcc_unreachable ();
210 /* Fill the working set information into the profile_info structure. */
212 void
213 get_working_sets (void)
215 unsigned ws_ix, pctinc, pct;
216 gcov_working_set_t *ws_info;
218 if (!profile_info)
219 return;
221 compute_working_sets (profile_info, gcov_working_sets);
223 if (dump_file)
225 fprintf (dump_file, "Counter working sets:\n");
226 /* Multiply the percentage by 100 to avoid float. */
227 pctinc = 100 * 100 / NUM_GCOV_WORKING_SETS;
228 for (ws_ix = 0, pct = pctinc; ws_ix < NUM_GCOV_WORKING_SETS;
229 ws_ix++, pct += pctinc)
231 if (ws_ix == NUM_GCOV_WORKING_SETS - 1)
232 pct = 9990;
233 ws_info = &gcov_working_sets[ws_ix];
234 /* Print out the percentage using int arithmatic to avoid float. */
235 fprintf (dump_file, "\t\t%u.%02u%%: num counts=%u, min counter="
236 HOST_WIDEST_INT_PRINT_DEC "\n",
237 pct / 100, pct - (pct / 100 * 100),
238 ws_info->num_counters,
239 (HOST_WIDEST_INT)ws_info->min_counter);
244 /* Given a the desired percentage of the full profile (sum_all from the
245 summary), multiplied by 10 to avoid float in PCT_TIMES_10, returns
246 the corresponding working set information. If an exact match for
247 the percentage isn't found, the closest value is used. */
249 gcov_working_set_t *
250 find_working_set (unsigned pct_times_10)
252 unsigned i;
253 if (!profile_info)
254 return NULL;
255 gcc_assert (pct_times_10 <= 1000);
256 if (pct_times_10 >= 999)
257 return &gcov_working_sets[NUM_GCOV_WORKING_SETS - 1];
258 i = pct_times_10 * NUM_GCOV_WORKING_SETS / 1000;
259 if (!i)
260 return &gcov_working_sets[0];
261 return &gcov_working_sets[i - 1];
264 /* Computes hybrid profile for all matching entries in da_file.
266 CFG_CHECKSUM is the precomputed checksum for the CFG. */
268 static gcov_type *
269 get_exec_counts (unsigned cfg_checksum, unsigned lineno_checksum)
271 unsigned num_edges = 0;
272 basic_block bb;
273 gcov_type *counts;
275 /* Count the edges to be (possibly) instrumented. */
276 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
278 edge e;
279 edge_iterator ei;
281 FOR_EACH_EDGE (e, ei, bb->succs)
282 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
283 num_edges++;
286 counts = get_coverage_counts (GCOV_COUNTER_ARCS, num_edges, cfg_checksum,
287 lineno_checksum, &profile_info);
288 if (!counts)
289 return NULL;
291 get_working_sets ();
293 if (dump_file && profile_info)
294 fprintf (dump_file, "Merged %u profiles with maximal count %u.\n",
295 profile_info->runs, (unsigned) profile_info->sum_max);
297 return counts;
301 static bool
302 is_edge_inconsistent (vec<edge, va_gc> *edges)
304 edge e;
305 edge_iterator ei;
306 FOR_EACH_EDGE (e, ei, edges)
308 if (!EDGE_INFO (e)->ignore)
310 if (e->count < 0
311 && (!(e->flags & EDGE_FAKE)
312 || !block_ends_with_call_p (e->src)))
314 if (dump_file)
316 fprintf (dump_file,
317 "Edge %i->%i is inconsistent, count"HOST_WIDEST_INT_PRINT_DEC,
318 e->src->index, e->dest->index, e->count);
319 dump_bb (dump_file, e->src, 0, TDF_DETAILS);
320 dump_bb (dump_file, e->dest, 0, TDF_DETAILS);
322 return true;
326 return false;
329 static void
330 correct_negative_edge_counts (void)
332 basic_block bb;
333 edge e;
334 edge_iterator ei;
336 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
338 FOR_EACH_EDGE (e, ei, bb->succs)
340 if (e->count < 0)
341 e->count = 0;
346 /* Check consistency.
347 Return true if inconsistency is found. */
348 static bool
349 is_inconsistent (void)
351 basic_block bb;
352 bool inconsistent = false;
353 FOR_EACH_BB (bb)
355 inconsistent |= is_edge_inconsistent (bb->preds);
356 if (!dump_file && inconsistent)
357 return true;
358 inconsistent |= is_edge_inconsistent (bb->succs);
359 if (!dump_file && inconsistent)
360 return true;
361 if (bb->count < 0)
363 if (dump_file)
365 fprintf (dump_file, "BB %i count is negative "
366 HOST_WIDEST_INT_PRINT_DEC,
367 bb->index,
368 bb->count);
369 dump_bb (dump_file, bb, 0, TDF_DETAILS);
371 inconsistent = true;
373 if (bb->count != sum_edge_counts (bb->preds))
375 if (dump_file)
377 fprintf (dump_file, "BB %i count does not match sum of incoming edges "
378 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
379 bb->index,
380 bb->count,
381 sum_edge_counts (bb->preds));
382 dump_bb (dump_file, bb, 0, TDF_DETAILS);
384 inconsistent = true;
386 if (bb->count != sum_edge_counts (bb->succs) &&
387 ! (find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)) != NULL
388 && block_ends_with_call_p (bb)))
390 if (dump_file)
392 fprintf (dump_file, "BB %i count does not match sum of outgoing edges "
393 HOST_WIDEST_INT_PRINT_DEC" should be " HOST_WIDEST_INT_PRINT_DEC,
394 bb->index,
395 bb->count,
396 sum_edge_counts (bb->succs));
397 dump_bb (dump_file, bb, 0, TDF_DETAILS);
399 inconsistent = true;
401 if (!dump_file && inconsistent)
402 return true;
405 return inconsistent;
408 /* Set each basic block count to the sum of its outgoing edge counts */
409 static void
410 set_bb_counts (void)
412 basic_block bb;
413 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
415 bb->count = sum_edge_counts (bb->succs);
416 gcc_assert (bb->count >= 0);
420 /* Reads profile data and returns total number of edge counts read */
421 static int
422 read_profile_edge_counts (gcov_type *exec_counts)
424 basic_block bb;
425 int num_edges = 0;
426 int exec_counts_pos = 0;
427 /* For each edge not on the spanning tree, set its execution count from
428 the .da file. */
429 /* The first count in the .da file is the number of times that the function
430 was entered. This is the exec_count for block zero. */
432 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
434 edge e;
435 edge_iterator ei;
437 FOR_EACH_EDGE (e, ei, bb->succs)
438 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree)
440 num_edges++;
441 if (exec_counts)
443 e->count = exec_counts[exec_counts_pos++];
444 if (e->count > profile_info->sum_max)
446 if (flag_profile_correction)
448 static bool informed = 0;
449 if (dump_enabled_p () && !informed)
450 dump_printf_loc (MSG_NOTE, input_location,
451 "corrupted profile info: edge count"
452 " exceeds maximal count\n");
453 informed = 1;
455 else
456 error ("corrupted profile info: edge from %i to %i exceeds maximal count",
457 bb->index, e->dest->index);
460 else
461 e->count = 0;
463 EDGE_INFO (e)->count_valid = 1;
464 BB_INFO (bb)->succ_count--;
465 BB_INFO (e->dest)->pred_count--;
466 if (dump_file)
468 fprintf (dump_file, "\nRead edge from %i to %i, count:",
469 bb->index, e->dest->index);
470 fprintf (dump_file, HOST_WIDEST_INT_PRINT_DEC,
471 (HOST_WIDEST_INT) e->count);
476 return num_edges;
479 #define OVERLAP_BASE 10000
481 /* Compare the static estimated profile to the actual profile, and
482 return the "degree of overlap" measure between them.
484 Degree of overlap is a number between 0 and OVERLAP_BASE. It is
485 the sum of each basic block's minimum relative weights between
486 two profiles. And overlap of OVERLAP_BASE means two profiles are
487 identical. */
489 static int
490 compute_frequency_overlap (void)
492 gcov_type count_total = 0, freq_total = 0;
493 int overlap = 0;
494 basic_block bb;
496 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
498 count_total += bb->count;
499 freq_total += bb->frequency;
502 if (count_total == 0 || freq_total == 0)
503 return 0;
505 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
506 overlap += MIN (bb->count * OVERLAP_BASE / count_total,
507 bb->frequency * OVERLAP_BASE / freq_total);
509 return overlap;
512 /* Compute the branch probabilities for the various branches.
513 Annotate them accordingly.
515 CFG_CHECKSUM is the precomputed checksum for the CFG. */
517 static void
518 compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum)
520 basic_block bb;
521 int i;
522 int num_edges = 0;
523 int changes;
524 int passes;
525 int hist_br_prob[20];
526 int num_branches;
527 gcov_type *exec_counts = get_exec_counts (cfg_checksum, lineno_checksum);
528 int inconsistent = 0;
530 /* Very simple sanity checks so we catch bugs in our profiling code. */
531 if (!profile_info)
532 return;
534 if (profile_info->sum_all < profile_info->sum_max)
536 error ("corrupted profile info: sum_all is smaller than sum_max");
537 exec_counts = NULL;
540 /* Attach extra info block to each bb. */
541 alloc_aux_for_blocks (sizeof (struct bb_info));
542 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
544 edge e;
545 edge_iterator ei;
547 FOR_EACH_EDGE (e, ei, bb->succs)
548 if (!EDGE_INFO (e)->ignore)
549 BB_INFO (bb)->succ_count++;
550 FOR_EACH_EDGE (e, ei, bb->preds)
551 if (!EDGE_INFO (e)->ignore)
552 BB_INFO (bb)->pred_count++;
555 /* Avoid predicting entry on exit nodes. */
556 BB_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->succ_count = 2;
557 BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (cfun))->pred_count = 2;
559 num_edges = read_profile_edge_counts (exec_counts);
561 if (dump_file)
562 fprintf (dump_file, "\n%d edge counts read\n", num_edges);
564 /* For every block in the file,
565 - if every exit/entrance edge has a known count, then set the block count
566 - if the block count is known, and every exit/entrance edge but one has
567 a known execution count, then set the count of the remaining edge
569 As edge counts are set, decrement the succ/pred count, but don't delete
570 the edge, that way we can easily tell when all edges are known, or only
571 one edge is unknown. */
573 /* The order that the basic blocks are iterated through is important.
574 Since the code that finds spanning trees starts with block 0, low numbered
575 edges are put on the spanning tree in preference to high numbered edges.
576 Hence, most instrumented edges are at the end. Graph solving works much
577 faster if we propagate numbers from the end to the start.
579 This takes an average of slightly more than 3 passes. */
581 changes = 1;
582 passes = 0;
583 while (changes)
585 passes++;
586 changes = 0;
587 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, prev_bb)
589 struct bb_info *bi = BB_INFO (bb);
590 if (! bi->count_valid)
592 if (bi->succ_count == 0)
594 edge e;
595 edge_iterator ei;
596 gcov_type total = 0;
598 FOR_EACH_EDGE (e, ei, bb->succs)
599 total += e->count;
600 bb->count = total;
601 bi->count_valid = 1;
602 changes = 1;
604 else if (bi->pred_count == 0)
606 edge e;
607 edge_iterator ei;
608 gcov_type total = 0;
610 FOR_EACH_EDGE (e, ei, bb->preds)
611 total += e->count;
612 bb->count = total;
613 bi->count_valid = 1;
614 changes = 1;
617 if (bi->count_valid)
619 if (bi->succ_count == 1)
621 edge e;
622 edge_iterator ei;
623 gcov_type total = 0;
625 /* One of the counts will be invalid, but it is zero,
626 so adding it in also doesn't hurt. */
627 FOR_EACH_EDGE (e, ei, bb->succs)
628 total += e->count;
630 /* Search for the invalid edge, and set its count. */
631 FOR_EACH_EDGE (e, ei, bb->succs)
632 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore)
633 break;
635 /* Calculate count for remaining edge by conservation. */
636 total = bb->count - total;
638 gcc_assert (e);
639 EDGE_INFO (e)->count_valid = 1;
640 e->count = total;
641 bi->succ_count--;
643 BB_INFO (e->dest)->pred_count--;
644 changes = 1;
646 if (bi->pred_count == 1)
648 edge e;
649 edge_iterator ei;
650 gcov_type total = 0;
652 /* One of the counts will be invalid, but it is zero,
653 so adding it in also doesn't hurt. */
654 FOR_EACH_EDGE (e, ei, bb->preds)
655 total += e->count;
657 /* Search for the invalid edge, and set its count. */
658 FOR_EACH_EDGE (e, ei, bb->preds)
659 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore)
660 break;
662 /* Calculate count for remaining edge by conservation. */
663 total = bb->count - total + e->count;
665 gcc_assert (e);
666 EDGE_INFO (e)->count_valid = 1;
667 e->count = total;
668 bi->pred_count--;
670 BB_INFO (e->src)->succ_count--;
671 changes = 1;
676 if (dump_file)
678 int overlap = compute_frequency_overlap ();
679 gimple_dump_cfg (dump_file, dump_flags);
680 fprintf (dump_file, "Static profile overlap: %d.%d%%\n",
681 overlap / (OVERLAP_BASE / 100),
682 overlap % (OVERLAP_BASE / 100));
685 total_num_passes += passes;
686 if (dump_file)
687 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes);
689 /* If the graph has been correctly solved, every block will have a
690 succ and pred count of zero. */
691 FOR_EACH_BB (bb)
693 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count);
696 /* Check for inconsistent basic block counts */
697 inconsistent = is_inconsistent ();
699 if (inconsistent)
701 if (flag_profile_correction)
703 /* Inconsistency detected. Make it flow-consistent. */
704 static int informed = 0;
705 if (dump_enabled_p () && informed == 0)
707 informed = 1;
708 dump_printf_loc (MSG_NOTE, input_location,
709 "correcting inconsistent profile data\n");
711 correct_negative_edge_counts ();
712 /* Set bb counts to the sum of the outgoing edge counts */
713 set_bb_counts ();
714 if (dump_file)
715 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n");
716 mcf_smooth_cfg ();
718 else
719 error ("corrupted profile info: profile data is not flow-consistent");
722 /* For every edge, calculate its branch probability and add a reg_note
723 to the branch insn to indicate this. */
725 for (i = 0; i < 20; i++)
726 hist_br_prob[i] = 0;
727 num_branches = 0;
729 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
731 edge e;
732 edge_iterator ei;
734 if (bb->count < 0)
736 error ("corrupted profile info: number of iterations for basic block %d thought to be %i",
737 bb->index, (int)bb->count);
738 bb->count = 0;
740 FOR_EACH_EDGE (e, ei, bb->succs)
742 /* Function may return twice in the cased the called function is
743 setjmp or calls fork, but we can't represent this by extra
744 edge from the entry, since extra edge from the exit is
745 already present. We get negative frequency from the entry
746 point. */
747 if ((e->count < 0
748 && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
749 || (e->count > bb->count
750 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)))
752 if (block_ends_with_call_p (bb))
753 e->count = e->count < 0 ? 0 : bb->count;
755 if (e->count < 0 || e->count > bb->count)
757 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i",
758 e->src->index, e->dest->index,
759 (int)e->count);
760 e->count = bb->count / 2;
763 if (bb->count)
765 FOR_EACH_EDGE (e, ei, bb->succs)
766 e->probability = GCOV_COMPUTE_SCALE (e->count, bb->count);
767 if (bb->index >= NUM_FIXED_BLOCKS
768 && block_ends_with_condjump_p (bb)
769 && EDGE_COUNT (bb->succs) >= 2)
771 int prob;
772 edge e;
773 int index;
775 /* Find the branch edge. It is possible that we do have fake
776 edges here. */
777 FOR_EACH_EDGE (e, ei, bb->succs)
778 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU)))
779 break;
781 prob = e->probability;
782 index = prob * 20 / REG_BR_PROB_BASE;
784 if (index == 20)
785 index = 19;
786 hist_br_prob[index]++;
788 num_branches++;
791 /* As a last resort, distribute the probabilities evenly.
792 Use simple heuristics that if there are normal edges,
793 give all abnormals frequency of 0, otherwise distribute the
794 frequency over abnormals (this is the case of noreturn
795 calls). */
796 else if (profile_status == PROFILE_ABSENT)
798 int total = 0;
800 FOR_EACH_EDGE (e, ei, bb->succs)
801 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
802 total ++;
803 if (total)
805 FOR_EACH_EDGE (e, ei, bb->succs)
806 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE)))
807 e->probability = REG_BR_PROB_BASE / total;
808 else
809 e->probability = 0;
811 else
813 total += EDGE_COUNT (bb->succs);
814 FOR_EACH_EDGE (e, ei, bb->succs)
815 e->probability = REG_BR_PROB_BASE / total;
817 if (bb->index >= NUM_FIXED_BLOCKS
818 && block_ends_with_condjump_p (bb)
819 && EDGE_COUNT (bb->succs) >= 2)
820 num_branches++;
823 counts_to_freqs ();
824 profile_status = PROFILE_READ;
825 compute_function_frequency ();
827 if (dump_file)
829 fprintf (dump_file, "%d branches\n", num_branches);
830 if (num_branches)
831 for (i = 0; i < 10; i++)
832 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
833 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches,
834 5 * i, 5 * i + 5);
836 total_num_branches += num_branches;
837 for (i = 0; i < 20; i++)
838 total_hist_br_prob[i] += hist_br_prob[i];
840 fputc ('\n', dump_file);
841 fputc ('\n', dump_file);
844 free_aux_for_blocks ();
847 /* Load value histograms values whose description is stored in VALUES array
848 from .gcda file.
850 CFG_CHECKSUM is the precomputed checksum for the CFG. */
852 static void
853 compute_value_histograms (histogram_values values, unsigned cfg_checksum,
854 unsigned lineno_checksum)
856 unsigned i, j, t, any;
857 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS];
858 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS];
859 gcov_type *act_count[GCOV_N_VALUE_COUNTERS];
860 gcov_type *aact_count;
861 struct cgraph_node *node;
863 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
864 n_histogram_counters[t] = 0;
866 for (i = 0; i < values.length (); i++)
868 histogram_value hist = values[i];
869 n_histogram_counters[(int) hist->type] += hist->n_counters;
872 any = 0;
873 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
875 if (!n_histogram_counters[t])
877 histogram_counts[t] = NULL;
878 continue;
881 histogram_counts[t] =
882 get_coverage_counts (COUNTER_FOR_HIST_TYPE (t),
883 n_histogram_counters[t], cfg_checksum,
884 lineno_checksum, NULL);
885 if (histogram_counts[t])
886 any = 1;
887 act_count[t] = histogram_counts[t];
889 if (!any)
890 return;
892 for (i = 0; i < values.length (); i++)
894 histogram_value hist = values[i];
895 gimple stmt = hist->hvalue.stmt;
897 t = (int) hist->type;
899 aact_count = act_count[t];
901 if (act_count[t])
902 act_count[t] += hist->n_counters;
904 gimple_add_histogram_value (cfun, stmt, hist);
905 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters);
906 for (j = 0; j < hist->n_counters; j++)
907 if (aact_count)
908 hist->hvalue.counters[j] = aact_count[j];
909 else
910 hist->hvalue.counters[j] = 0;
912 /* Time profiler counter is not related to any statement,
913 so that we have to read the counter and set the value to
914 the corresponding call graph node. */
915 if (hist->type == HIST_TYPE_TIME_PROFILE)
917 node = cgraph_get_node (hist->fun->decl);
919 node->tp_first_run = hist->hvalue.counters[0];
921 if (dump_file)
922 fprintf (dump_file, "Read tp_first_run: %d\n", node->tp_first_run);
926 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++)
927 free (histogram_counts[t]);
930 /* When passed NULL as file_name, initialize.
931 When passed something else, output the necessary commands to change
932 line to LINE and offset to FILE_NAME. */
933 static void
934 output_location (char const *file_name, int line,
935 gcov_position_t *offset, basic_block bb)
937 static char const *prev_file_name;
938 static int prev_line;
939 bool name_differs, line_differs;
941 if (!file_name)
943 prev_file_name = NULL;
944 prev_line = -1;
945 return;
948 name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name);
949 line_differs = prev_line != line;
951 if (name_differs || line_differs)
953 if (!*offset)
955 *offset = gcov_write_tag (GCOV_TAG_LINES);
956 gcov_write_unsigned (bb->index);
957 name_differs = line_differs=true;
960 /* If this is a new source file, then output the
961 file's name to the .bb file. */
962 if (name_differs)
964 prev_file_name = file_name;
965 gcov_write_unsigned (0);
966 gcov_write_string (prev_file_name);
968 if (line_differs)
970 gcov_write_unsigned (line);
971 prev_line = line;
976 /* Instrument and/or analyze program behavior based on program the CFG.
978 This function creates a representation of the control flow graph (of
979 the function being compiled) that is suitable for the instrumentation
980 of edges and/or converting measured edge counts to counts on the
981 complete CFG.
983 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in
984 the flow graph that are needed to reconstruct the dynamic behavior of the
985 flow graph. This data is written to the gcno file for gcov.
987 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary
988 information from the gcda file containing edge count information from
989 previous executions of the function being compiled. In this case, the
990 control flow graph is annotated with actual execution counts by
991 compute_branch_probabilities().
993 Main entry point of this file. */
995 void
996 branch_prob (void)
998 basic_block bb;
999 unsigned i;
1000 unsigned num_edges, ignored_edges;
1001 unsigned num_instrumented;
1002 struct edge_list *el;
1003 histogram_values values = histogram_values ();
1004 unsigned cfg_checksum, lineno_checksum;
1006 total_num_times_called++;
1008 flow_call_edges_add (NULL);
1009 add_noreturn_fake_exit_edges ();
1011 /* We can't handle cyclic regions constructed using abnormal edges.
1012 To avoid these we replace every source of abnormal edge by a fake
1013 edge from entry node and every destination by fake edge to exit.
1014 This keeps graph acyclic and our calculation exact for all normal
1015 edges except for exit and entrance ones.
1017 We also add fake exit edges for each call and asm statement in the
1018 basic, since it may not return. */
1020 FOR_EACH_BB (bb)
1022 int need_exit_edge = 0, need_entry_edge = 0;
1023 int have_exit_edge = 0, have_entry_edge = 0;
1024 edge e;
1025 edge_iterator ei;
1027 /* Functions returning multiple times are not handled by extra edges.
1028 Instead we simply allow negative counts on edges from exit to the
1029 block past call and corresponding probabilities. We can't go
1030 with the extra edges because that would result in flowgraph that
1031 needs to have fake edges outside the spanning tree. */
1033 FOR_EACH_EDGE (e, ei, bb->succs)
1035 gimple_stmt_iterator gsi;
1036 gimple last = NULL;
1038 /* It may happen that there are compiler generated statements
1039 without a locus at all. Go through the basic block from the
1040 last to the first statement looking for a locus. */
1041 for (gsi = gsi_last_nondebug_bb (bb);
1042 !gsi_end_p (gsi);
1043 gsi_prev_nondebug (&gsi))
1045 last = gsi_stmt (gsi);
1046 if (gimple_has_location (last))
1047 break;
1050 /* Edge with goto locus might get wrong coverage info unless
1051 it is the only edge out of BB.
1052 Don't do that when the locuses match, so
1053 if (blah) goto something;
1054 is not computed twice. */
1055 if (last
1056 && gimple_has_location (last)
1057 && LOCATION_LOCUS (e->goto_locus) != UNKNOWN_LOCATION
1058 && !single_succ_p (bb)
1059 && (LOCATION_FILE (e->goto_locus)
1060 != LOCATION_FILE (gimple_location (last))
1061 || (LOCATION_LINE (e->goto_locus)
1062 != LOCATION_LINE (gimple_location (last)))))
1064 basic_block new_bb = split_edge (e);
1065 edge ne = single_succ_edge (new_bb);
1066 ne->goto_locus = e->goto_locus;
1068 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1069 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1070 need_exit_edge = 1;
1071 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1072 have_exit_edge = 1;
1074 FOR_EACH_EDGE (e, ei, bb->preds)
1076 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1077 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
1078 need_entry_edge = 1;
1079 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1080 have_entry_edge = 1;
1083 if (need_exit_edge && !have_exit_edge)
1085 if (dump_file)
1086 fprintf (dump_file, "Adding fake exit edge to bb %i\n",
1087 bb->index);
1088 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE);
1090 if (need_entry_edge && !have_entry_edge)
1092 if (dump_file)
1093 fprintf (dump_file, "Adding fake entry edge to bb %i\n",
1094 bb->index);
1095 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FAKE);
1096 /* Avoid bbs that have both fake entry edge and also some
1097 exit edge. One of those edges wouldn't be added to the
1098 spanning tree, but we can't instrument any of them. */
1099 if (have_exit_edge || need_exit_edge)
1101 gimple_stmt_iterator gsi;
1102 gimple first;
1103 tree fndecl;
1105 gsi = gsi_after_labels (bb);
1106 gcc_checking_assert (!gsi_end_p (gsi));
1107 first = gsi_stmt (gsi);
1108 if (is_gimple_debug (first))
1110 gsi_next_nondebug (&gsi);
1111 gcc_checking_assert (!gsi_end_p (gsi));
1112 first = gsi_stmt (gsi);
1114 /* Don't split the bbs containing __builtin_setjmp_receiver
1115 or __builtin_setjmp_dispatcher calls. These are very
1116 special and don't expect anything to be inserted before
1117 them. */
1118 if (is_gimple_call (first)
1119 && (((fndecl = gimple_call_fndecl (first)) != NULL
1120 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
1121 && (DECL_FUNCTION_CODE (fndecl)
1122 == BUILT_IN_SETJMP_RECEIVER
1123 || (DECL_FUNCTION_CODE (fndecl)
1124 == BUILT_IN_SETJMP_DISPATCHER)))
1125 || gimple_call_flags (first) & ECF_RETURNS_TWICE))
1126 continue;
1128 if (dump_file)
1129 fprintf (dump_file, "Splitting bb %i after labels\n",
1130 bb->index);
1131 split_block_after_labels (bb);
1136 el = create_edge_list ();
1137 num_edges = NUM_EDGES (el);
1138 alloc_aux_for_edges (sizeof (struct edge_info));
1140 /* The basic blocks are expected to be numbered sequentially. */
1141 compact_blocks ();
1143 ignored_edges = 0;
1144 for (i = 0 ; i < num_edges ; i++)
1146 edge e = INDEX_EDGE (el, i);
1147 e->count = 0;
1149 /* Mark edges we've replaced by fake edges above as ignored. */
1150 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL))
1151 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1152 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1154 EDGE_INFO (e)->ignore = 1;
1155 ignored_edges++;
1159 /* Create spanning tree from basic block graph, mark each edge that is
1160 on the spanning tree. We insert as many abnormal and critical edges
1161 as possible to minimize number of edge splits necessary. */
1163 find_spanning_tree (el);
1165 /* Fake edges that are not on the tree will not be instrumented, so
1166 mark them ignored. */
1167 for (num_instrumented = i = 0; i < num_edges; i++)
1169 edge e = INDEX_EDGE (el, i);
1170 struct edge_info *inf = EDGE_INFO (e);
1172 if (inf->ignore || inf->on_tree)
1173 /*NOP*/;
1174 else if (e->flags & EDGE_FAKE)
1176 inf->ignore = 1;
1177 ignored_edges++;
1179 else
1180 num_instrumented++;
1183 total_num_blocks += n_basic_blocks_for_fn (cfun);
1184 if (dump_file)
1185 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks_for_fn (cfun));
1187 total_num_edges += num_edges;
1188 if (dump_file)
1189 fprintf (dump_file, "%d edges\n", num_edges);
1191 total_num_edges_ignored += ignored_edges;
1192 if (dump_file)
1193 fprintf (dump_file, "%d ignored edges\n", ignored_edges);
1195 total_num_edges_instrumented += num_instrumented;
1196 if (dump_file)
1197 fprintf (dump_file, "%d instrumentation edges\n", num_instrumented);
1199 /* Compute two different checksums. Note that we want to compute
1200 the checksum in only once place, since it depends on the shape
1201 of the control flow which can change during
1202 various transformations. */
1203 cfg_checksum = coverage_compute_cfg_checksum ();
1204 lineno_checksum = coverage_compute_lineno_checksum ();
1206 /* Write the data from which gcov can reconstruct the basic block
1207 graph and function line numbers (the gcno file). */
1208 if (coverage_begin_function (lineno_checksum, cfg_checksum))
1210 gcov_position_t offset;
1212 /* Basic block flags */
1213 offset = gcov_write_tag (GCOV_TAG_BLOCKS);
1214 for (i = 0; i != (unsigned) (n_basic_blocks_for_fn (cfun)); i++)
1215 gcov_write_unsigned (0);
1216 gcov_write_length (offset);
1218 /* Arcs */
1219 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun),
1220 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1222 edge e;
1223 edge_iterator ei;
1225 offset = gcov_write_tag (GCOV_TAG_ARCS);
1226 gcov_write_unsigned (bb->index);
1228 FOR_EACH_EDGE (e, ei, bb->succs)
1230 struct edge_info *i = EDGE_INFO (e);
1231 if (!i->ignore)
1233 unsigned flag_bits = 0;
1235 if (i->on_tree)
1236 flag_bits |= GCOV_ARC_ON_TREE;
1237 if (e->flags & EDGE_FAKE)
1238 flag_bits |= GCOV_ARC_FAKE;
1239 if (e->flags & EDGE_FALLTHRU)
1240 flag_bits |= GCOV_ARC_FALLTHROUGH;
1241 /* On trees we don't have fallthru flags, but we can
1242 recompute them from CFG shape. */
1243 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)
1244 && e->src->next_bb == e->dest)
1245 flag_bits |= GCOV_ARC_FALLTHROUGH;
1247 gcov_write_unsigned (e->dest->index);
1248 gcov_write_unsigned (flag_bits);
1252 gcov_write_length (offset);
1255 /* Line numbers. */
1256 /* Initialize the output. */
1257 output_location (NULL, 0, NULL, NULL);
1259 FOR_EACH_BB (bb)
1261 gimple_stmt_iterator gsi;
1262 gcov_position_t offset = 0;
1264 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
1266 expanded_location curr_location =
1267 expand_location (DECL_SOURCE_LOCATION (current_function_decl));
1268 output_location (curr_location.file, curr_location.line,
1269 &offset, bb);
1272 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1274 gimple stmt = gsi_stmt (gsi);
1275 if (gimple_has_location (stmt))
1276 output_location (gimple_filename (stmt), gimple_lineno (stmt),
1277 &offset, bb);
1280 /* Notice GOTO expressions eliminated while constructing the CFG. */
1281 if (single_succ_p (bb)
1282 && LOCATION_LOCUS (single_succ_edge (bb)->goto_locus)
1283 != UNKNOWN_LOCATION)
1285 expanded_location curr_location
1286 = expand_location (single_succ_edge (bb)->goto_locus);
1287 output_location (curr_location.file, curr_location.line,
1288 &offset, bb);
1291 if (offset)
1293 /* A file of NULL indicates the end of run. */
1294 gcov_write_unsigned (0);
1295 gcov_write_string (NULL);
1296 gcov_write_length (offset);
1301 if (flag_profile_values)
1302 gimple_find_values_to_profile (&values);
1304 if (flag_branch_probabilities)
1306 compute_branch_probabilities (cfg_checksum, lineno_checksum);
1307 if (flag_profile_values)
1308 compute_value_histograms (values, cfg_checksum, lineno_checksum);
1311 remove_fake_edges ();
1313 /* For each edge not on the spanning tree, add counting code. */
1314 if (profile_arc_flag
1315 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented))
1317 unsigned n_instrumented;
1319 gimple_init_edge_profiler ();
1321 n_instrumented = instrument_edges (el);
1323 gcc_assert (n_instrumented == num_instrumented);
1325 if (flag_profile_values)
1326 instrument_values (values);
1328 /* Commit changes done by instrumentation. */
1329 gsi_commit_edge_inserts ();
1332 free_aux_for_edges ();
1334 values.release ();
1335 free_edge_list (el);
1336 coverage_end_function (lineno_checksum, cfg_checksum);
1339 /* Union find algorithm implementation for the basic blocks using
1340 aux fields. */
1342 static basic_block
1343 find_group (basic_block bb)
1345 basic_block group = bb, bb1;
1347 while ((basic_block) group->aux != group)
1348 group = (basic_block) group->aux;
1350 /* Compress path. */
1351 while ((basic_block) bb->aux != group)
1353 bb1 = (basic_block) bb->aux;
1354 bb->aux = (void *) group;
1355 bb = bb1;
1357 return group;
1360 static void
1361 union_groups (basic_block bb1, basic_block bb2)
1363 basic_block bb1g = find_group (bb1);
1364 basic_block bb2g = find_group (bb2);
1366 /* ??? I don't have a place for the rank field. OK. Lets go w/o it,
1367 this code is unlikely going to be performance problem anyway. */
1368 gcc_assert (bb1g != bb2g);
1370 bb1g->aux = bb2g;
1373 /* This function searches all of the edges in the program flow graph, and puts
1374 as many bad edges as possible onto the spanning tree. Bad edges include
1375 abnormals edges, which can't be instrumented at the moment. Since it is
1376 possible for fake edges to form a cycle, we will have to develop some
1377 better way in the future. Also put critical edges to the tree, since they
1378 are more expensive to instrument. */
1380 static void
1381 find_spanning_tree (struct edge_list *el)
1383 int i;
1384 int num_edges = NUM_EDGES (el);
1385 basic_block bb;
1387 /* We use aux field for standard union-find algorithm. */
1388 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb)
1389 bb->aux = bb;
1391 /* Add fake edge exit to entry we can't instrument. */
1392 union_groups (EXIT_BLOCK_PTR_FOR_FN (cfun), ENTRY_BLOCK_PTR_FOR_FN (cfun));
1394 /* First add all abnormal edges to the tree unless they form a cycle. Also
1395 add all edges to the exit block to avoid inserting profiling code behind
1396 setting return value from function. */
1397 for (i = 0; i < num_edges; i++)
1399 edge e = INDEX_EDGE (el, i);
1400 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE))
1401 || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1402 && !EDGE_INFO (e)->ignore
1403 && (find_group (e->src) != find_group (e->dest)))
1405 if (dump_file)
1406 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n",
1407 e->src->index, e->dest->index);
1408 EDGE_INFO (e)->on_tree = 1;
1409 union_groups (e->src, e->dest);
1413 /* Now insert all critical edges to the tree unless they form a cycle. */
1414 for (i = 0; i < num_edges; i++)
1416 edge e = INDEX_EDGE (el, i);
1417 if (EDGE_CRITICAL_P (e) && !EDGE_INFO (e)->ignore
1418 && find_group (e->src) != find_group (e->dest))
1420 if (dump_file)
1421 fprintf (dump_file, "Critical edge %d to %d put to tree\n",
1422 e->src->index, e->dest->index);
1423 EDGE_INFO (e)->on_tree = 1;
1424 union_groups (e->src, e->dest);
1428 /* And now the rest. */
1429 for (i = 0; i < num_edges; i++)
1431 edge e = INDEX_EDGE (el, i);
1432 if (!EDGE_INFO (e)->ignore
1433 && find_group (e->src) != find_group (e->dest))
1435 if (dump_file)
1436 fprintf (dump_file, "Normal edge %d to %d put to tree\n",
1437 e->src->index, e->dest->index);
1438 EDGE_INFO (e)->on_tree = 1;
1439 union_groups (e->src, e->dest);
1443 clear_aux_for_blocks ();
1446 /* Perform file-level initialization for branch-prob processing. */
1448 void
1449 init_branch_prob (void)
1451 int i;
1453 total_num_blocks = 0;
1454 total_num_edges = 0;
1455 total_num_edges_ignored = 0;
1456 total_num_edges_instrumented = 0;
1457 total_num_blocks_created = 0;
1458 total_num_passes = 0;
1459 total_num_times_called = 0;
1460 total_num_branches = 0;
1461 for (i = 0; i < 20; i++)
1462 total_hist_br_prob[i] = 0;
1465 /* Performs file-level cleanup after branch-prob processing
1466 is completed. */
1468 void
1469 end_branch_prob (void)
1471 if (dump_file)
1473 fprintf (dump_file, "\n");
1474 fprintf (dump_file, "Total number of blocks: %d\n",
1475 total_num_blocks);
1476 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges);
1477 fprintf (dump_file, "Total number of ignored edges: %d\n",
1478 total_num_edges_ignored);
1479 fprintf (dump_file, "Total number of instrumented edges: %d\n",
1480 total_num_edges_instrumented);
1481 fprintf (dump_file, "Total number of blocks created: %d\n",
1482 total_num_blocks_created);
1483 fprintf (dump_file, "Total number of graph solution passes: %d\n",
1484 total_num_passes);
1485 if (total_num_times_called != 0)
1486 fprintf (dump_file, "Average number of graph solution passes: %d\n",
1487 (total_num_passes + (total_num_times_called >> 1))
1488 / total_num_times_called);
1489 fprintf (dump_file, "Total number of branches: %d\n",
1490 total_num_branches);
1491 if (total_num_branches)
1493 int i;
1495 for (i = 0; i < 10; i++)
1496 fprintf (dump_file, "%d%% branches in range %d-%d%%\n",
1497 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100
1498 / total_num_branches, 5*i, 5*i+5);