Mercurial > hg > CbC > CbC_gcc
annotate gcc/profile.c @ 143:76e1cf5455ef
add cbc_gc test
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 23 Dec 2018 19:24:05 +0900 |
parents | 84e7813d76e9 |
children | 1830386684a0 |
rev | line source |
---|---|
0 | 1 /* Calculate branch probabilities, and basic block execution counts. |
131 | 2 Copyright (C) 1990-2018 Free Software Foundation, Inc. |
0 | 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. | |
6 | |
7 This file is part of GCC. | |
8 | |
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. | |
13 | |
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. | |
18 | |
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/>. */ | |
22 | |
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. | |
39 | |
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. */ | |
43 | |
44 /* ??? Register allocation should use basic block execution counts to | |
45 give preference to the most commonly executed blocks. */ | |
46 | |
47 /* ??? Should calculate branch probabilities before instrumenting code, since | |
48 then we can use arc counts to help decide which arcs to instrument. */ | |
49 | |
50 #include "config.h" | |
51 #include "system.h" | |
52 #include "coretypes.h" | |
111 | 53 #include "backend.h" |
0 | 54 #include "rtl.h" |
111 | 55 #include "tree.h" |
56 #include "gimple.h" | |
57 #include "cfghooks.h" | |
58 #include "cgraph.h" | |
59 #include "coverage.h" | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
60 #include "diagnostic-core.h" |
111 | 61 #include "cfganal.h" |
0 | 62 #include "value-prof.h" |
111 | 63 #include "gimple-iterator.h" |
64 #include "tree-cfg.h" | |
65 #include "dumpfile.h" | |
0 | 66 #include "cfgloop.h" |
67 | |
68 #include "profile.h" | |
69 | |
111 | 70 /* Map from BBs/edges to gcov counters. */ |
71 vec<gcov_type> bb_gcov_counts; | |
72 hash_map<edge,gcov_type> *edge_gcov_counts; | |
73 | |
74 struct bb_profile_info { | |
0 | 75 unsigned int count_valid : 1; |
76 | |
77 /* Number of successor and predecessor edges. */ | |
78 gcov_type succ_count; | |
79 gcov_type pred_count; | |
80 }; | |
81 | |
111 | 82 #define BB_INFO(b) ((struct bb_profile_info *) (b)->aux) |
0 | 83 |
84 | |
85 /* Counter summary from the last set of coverage counts read. */ | |
86 | |
131 | 87 gcov_summary *profile_info; |
111 | 88 |
0 | 89 /* Collect statistics on the performance of this pass for the entire source |
90 file. */ | |
91 | |
92 static int total_num_blocks; | |
93 static int total_num_edges; | |
94 static int total_num_edges_ignored; | |
95 static int total_num_edges_instrumented; | |
96 static int total_num_blocks_created; | |
97 static int total_num_passes; | |
98 static int total_num_times_called; | |
99 static int total_hist_br_prob[20]; | |
100 static int total_num_branches; | |
101 | |
102 /* Forward declarations. */ | |
103 static void find_spanning_tree (struct edge_list *); | |
104 | |
105 /* Add edge instrumentation code to the entire insn chain. | |
106 | |
107 F is the first insn of the chain. | |
108 NUM_BLOCKS is the number of basic blocks found in F. */ | |
109 | |
110 static unsigned | |
111 instrument_edges (struct edge_list *el) | |
112 { | |
113 unsigned num_instr_edges = 0; | |
114 int num_edges = NUM_EDGES (el); | |
115 basic_block bb; | |
116 | |
111 | 117 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) |
0 | 118 { |
119 edge e; | |
120 edge_iterator ei; | |
121 | |
122 FOR_EACH_EDGE (e, ei, bb->succs) | |
123 { | |
111 | 124 struct edge_profile_info *inf = EDGE_INFO (e); |
0 | 125 |
126 if (!inf->ignore && !inf->on_tree) | |
127 { | |
128 gcc_assert (!(e->flags & EDGE_ABNORMAL)); | |
129 if (dump_file) | |
130 fprintf (dump_file, "Edge %d to %d instrumented%s\n", | |
131 e->src->index, e->dest->index, | |
132 EDGE_CRITICAL_P (e) ? " (and split)" : ""); | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
133 gimple_gen_edge_profiler (num_instr_edges++, e); |
0 | 134 } |
135 } | |
136 } | |
137 | |
138 total_num_blocks_created += num_edges; | |
139 if (dump_file) | |
140 fprintf (dump_file, "%d edges instrumented\n", num_instr_edges); | |
141 return num_instr_edges; | |
142 } | |
143 | |
144 /* Add code to measure histograms for values in list VALUES. */ | |
145 static void | |
146 instrument_values (histogram_values values) | |
147 { | |
111 | 148 unsigned i; |
0 | 149 |
150 /* Emit code to generate the histograms before the insns. */ | |
151 | |
111 | 152 for (i = 0; i < values.length (); i++) |
0 | 153 { |
111 | 154 histogram_value hist = values[i]; |
155 unsigned t = COUNTER_FOR_HIST_TYPE (hist->type); | |
0 | 156 |
157 if (!coverage_counter_alloc (t, hist->n_counters)) | |
158 continue; | |
159 | |
160 switch (hist->type) | |
161 { | |
162 case HIST_TYPE_INTERVAL: | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
163 gimple_gen_interval_profiler (hist, t, 0); |
0 | 164 break; |
165 | |
166 case HIST_TYPE_POW2: | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
167 gimple_gen_pow2_profiler (hist, t, 0); |
0 | 168 break; |
169 | |
170 case HIST_TYPE_SINGLE_VALUE: | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
171 gimple_gen_one_value_profiler (hist, t, 0); |
0 | 172 break; |
173 | |
174 case HIST_TYPE_INDIR_CALL: | |
111 | 175 case HIST_TYPE_INDIR_CALL_TOPN: |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
176 gimple_gen_ic_profiler (hist, t, 0); |
0 | 177 break; |
178 | |
179 case HIST_TYPE_AVERAGE: | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
180 gimple_gen_average_profiler (hist, t, 0); |
0 | 181 break; |
182 | |
183 case HIST_TYPE_IOR: | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
184 gimple_gen_ior_profiler (hist, t, 0); |
0 | 185 break; |
186 | |
111 | 187 case HIST_TYPE_TIME_PROFILE: |
188 gimple_gen_time_profiler (t, 0); | |
189 break; | |
190 | |
0 | 191 default: |
192 gcc_unreachable (); | |
193 } | |
194 } | |
195 } | |
196 | |
197 | |
111 | 198 /* Computes hybrid profile for all matching entries in da_file. |
199 | |
200 CFG_CHECKSUM is the precomputed checksum for the CFG. */ | |
0 | 201 |
202 static gcov_type * | |
111 | 203 get_exec_counts (unsigned cfg_checksum, unsigned lineno_checksum) |
0 | 204 { |
205 unsigned num_edges = 0; | |
206 basic_block bb; | |
207 gcov_type *counts; | |
208 | |
209 /* Count the edges to be (possibly) instrumented. */ | |
111 | 210 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) |
0 | 211 { |
212 edge e; | |
213 edge_iterator ei; | |
214 | |
215 FOR_EACH_EDGE (e, ei, bb->succs) | |
216 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree) | |
217 num_edges++; | |
218 } | |
219 | |
131 | 220 counts = get_coverage_counts (GCOV_COUNTER_ARCS, cfg_checksum, |
221 lineno_checksum); | |
0 | 222 if (!counts) |
223 return NULL; | |
224 | |
225 return counts; | |
226 } | |
227 | |
228 static bool | |
111 | 229 is_edge_inconsistent (vec<edge, va_gc> *edges) |
0 | 230 { |
231 edge e; | |
232 edge_iterator ei; | |
233 FOR_EACH_EDGE (e, ei, edges) | |
234 { | |
235 if (!EDGE_INFO (e)->ignore) | |
236 { | |
111 | 237 if (edge_gcov_count (e) < 0 |
0 | 238 && (!(e->flags & EDGE_FAKE) |
239 || !block_ends_with_call_p (e->src))) | |
240 { | |
241 if (dump_file) | |
242 { | |
243 fprintf (dump_file, | |
111 | 244 "Edge %i->%i is inconsistent, count%" PRId64, |
245 e->src->index, e->dest->index, edge_gcov_count (e)); | |
246 dump_bb (dump_file, e->src, 0, TDF_DETAILS); | |
247 dump_bb (dump_file, e->dest, 0, TDF_DETAILS); | |
0 | 248 } |
249 return true; | |
250 } | |
251 } | |
252 } | |
253 return false; | |
254 } | |
255 | |
256 static void | |
257 correct_negative_edge_counts (void) | |
258 { | |
259 basic_block bb; | |
260 edge e; | |
261 edge_iterator ei; | |
262 | |
111 | 263 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) |
0 | 264 { |
265 FOR_EACH_EDGE (e, ei, bb->succs) | |
266 { | |
111 | 267 if (edge_gcov_count (e) < 0) |
268 edge_gcov_count (e) = 0; | |
0 | 269 } |
270 } | |
271 } | |
272 | |
273 /* Check consistency. | |
274 Return true if inconsistency is found. */ | |
275 static bool | |
276 is_inconsistent (void) | |
277 { | |
278 basic_block bb; | |
279 bool inconsistent = false; | |
111 | 280 FOR_EACH_BB_FN (bb, cfun) |
0 | 281 { |
282 inconsistent |= is_edge_inconsistent (bb->preds); | |
283 if (!dump_file && inconsistent) | |
284 return true; | |
285 inconsistent |= is_edge_inconsistent (bb->succs); | |
286 if (!dump_file && inconsistent) | |
287 return true; | |
111 | 288 if (bb_gcov_count (bb) < 0) |
0 | 289 { |
290 if (dump_file) | |
291 { | |
292 fprintf (dump_file, "BB %i count is negative " | |
111 | 293 "%" PRId64, |
0 | 294 bb->index, |
111 | 295 bb_gcov_count (bb)); |
296 dump_bb (dump_file, bb, 0, TDF_DETAILS); | |
0 | 297 } |
298 inconsistent = true; | |
299 } | |
111 | 300 if (bb_gcov_count (bb) != sum_edge_counts (bb->preds)) |
0 | 301 { |
302 if (dump_file) | |
303 { | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
304 fprintf (dump_file, "BB %i count does not match sum of incoming edges " |
111 | 305 "%" PRId64" should be %" PRId64, |
0 | 306 bb->index, |
111 | 307 bb_gcov_count (bb), |
0 | 308 sum_edge_counts (bb->preds)); |
111 | 309 dump_bb (dump_file, bb, 0, TDF_DETAILS); |
0 | 310 } |
311 inconsistent = true; | |
312 } | |
111 | 313 if (bb_gcov_count (bb) != sum_edge_counts (bb->succs) && |
314 ! (find_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun)) != NULL | |
315 && block_ends_with_call_p (bb))) | |
0 | 316 { |
317 if (dump_file) | |
318 { | |
319 fprintf (dump_file, "BB %i count does not match sum of outgoing edges " | |
111 | 320 "%" PRId64" should be %" PRId64, |
0 | 321 bb->index, |
111 | 322 bb_gcov_count (bb), |
0 | 323 sum_edge_counts (bb->succs)); |
111 | 324 dump_bb (dump_file, bb, 0, TDF_DETAILS); |
0 | 325 } |
326 inconsistent = true; | |
327 } | |
328 if (!dump_file && inconsistent) | |
329 return true; | |
330 } | |
331 | |
332 return inconsistent; | |
333 } | |
334 | |
335 /* Set each basic block count to the sum of its outgoing edge counts */ | |
336 static void | |
337 set_bb_counts (void) | |
338 { | |
339 basic_block bb; | |
111 | 340 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) |
0 | 341 { |
111 | 342 bb_gcov_count (bb) = sum_edge_counts (bb->succs); |
343 gcc_assert (bb_gcov_count (bb) >= 0); | |
0 | 344 } |
345 } | |
346 | |
347 /* Reads profile data and returns total number of edge counts read */ | |
348 static int | |
349 read_profile_edge_counts (gcov_type *exec_counts) | |
350 { | |
351 basic_block bb; | |
352 int num_edges = 0; | |
353 int exec_counts_pos = 0; | |
354 /* For each edge not on the spanning tree, set its execution count from | |
355 the .da file. */ | |
356 /* The first count in the .da file is the number of times that the function | |
357 was entered. This is the exec_count for block zero. */ | |
358 | |
111 | 359 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) |
0 | 360 { |
361 edge e; | |
362 edge_iterator ei; | |
363 | |
364 FOR_EACH_EDGE (e, ei, bb->succs) | |
365 if (!EDGE_INFO (e)->ignore && !EDGE_INFO (e)->on_tree) | |
366 { | |
367 num_edges++; | |
368 if (exec_counts) | |
131 | 369 edge_gcov_count (e) = exec_counts[exec_counts_pos++]; |
0 | 370 else |
111 | 371 edge_gcov_count (e) = 0; |
0 | 372 |
373 EDGE_INFO (e)->count_valid = 1; | |
374 BB_INFO (bb)->succ_count--; | |
375 BB_INFO (e->dest)->pred_count--; | |
376 if (dump_file) | |
377 { | |
378 fprintf (dump_file, "\nRead edge from %i to %i, count:", | |
379 bb->index, e->dest->index); | |
111 | 380 fprintf (dump_file, "%" PRId64, |
381 (int64_t) edge_gcov_count (e)); | |
0 | 382 } |
383 } | |
384 } | |
385 | |
386 return num_edges; | |
387 } | |
388 | |
111 | 389 |
0 | 390 /* Compute the branch probabilities for the various branches. |
111 | 391 Annotate them accordingly. |
392 | |
393 CFG_CHECKSUM is the precomputed checksum for the CFG. */ | |
0 | 394 |
395 static void | |
111 | 396 compute_branch_probabilities (unsigned cfg_checksum, unsigned lineno_checksum) |
0 | 397 { |
398 basic_block bb; | |
399 int i; | |
400 int num_edges = 0; | |
401 int changes; | |
402 int passes; | |
403 int hist_br_prob[20]; | |
404 int num_branches; | |
111 | 405 gcov_type *exec_counts = get_exec_counts (cfg_checksum, lineno_checksum); |
0 | 406 int inconsistent = 0; |
407 | |
408 /* Very simple sanity checks so we catch bugs in our profiling code. */ | |
409 if (!profile_info) | |
131 | 410 { |
411 if (dump_file) | |
412 fprintf (dump_file, "Profile info is missing; giving up\n"); | |
413 return; | |
414 } | |
111 | 415 |
416 bb_gcov_counts.safe_grow_cleared (last_basic_block_for_fn (cfun)); | |
417 edge_gcov_counts = new hash_map<edge,gcov_type>; | |
0 | 418 |
419 /* Attach extra info block to each bb. */ | |
111 | 420 alloc_aux_for_blocks (sizeof (struct bb_profile_info)); |
421 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) | |
0 | 422 { |
423 edge e; | |
424 edge_iterator ei; | |
425 | |
426 FOR_EACH_EDGE (e, ei, bb->succs) | |
427 if (!EDGE_INFO (e)->ignore) | |
428 BB_INFO (bb)->succ_count++; | |
429 FOR_EACH_EDGE (e, ei, bb->preds) | |
430 if (!EDGE_INFO (e)->ignore) | |
431 BB_INFO (bb)->pred_count++; | |
432 } | |
433 | |
434 /* Avoid predicting entry on exit nodes. */ | |
111 | 435 BB_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->succ_count = 2; |
436 BB_INFO (ENTRY_BLOCK_PTR_FOR_FN (cfun))->pred_count = 2; | |
0 | 437 |
438 num_edges = read_profile_edge_counts (exec_counts); | |
439 | |
440 if (dump_file) | |
441 fprintf (dump_file, "\n%d edge counts read\n", num_edges); | |
442 | |
443 /* For every block in the file, | |
444 - if every exit/entrance edge has a known count, then set the block count | |
445 - if the block count is known, and every exit/entrance edge but one has | |
446 a known execution count, then set the count of the remaining edge | |
447 | |
448 As edge counts are set, decrement the succ/pred count, but don't delete | |
449 the edge, that way we can easily tell when all edges are known, or only | |
450 one edge is unknown. */ | |
451 | |
452 /* The order that the basic blocks are iterated through is important. | |
453 Since the code that finds spanning trees starts with block 0, low numbered | |
454 edges are put on the spanning tree in preference to high numbered edges. | |
455 Hence, most instrumented edges are at the end. Graph solving works much | |
456 faster if we propagate numbers from the end to the start. | |
457 | |
458 This takes an average of slightly more than 3 passes. */ | |
459 | |
460 changes = 1; | |
461 passes = 0; | |
462 while (changes) | |
463 { | |
464 passes++; | |
465 changes = 0; | |
111 | 466 FOR_BB_BETWEEN (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), NULL, prev_bb) |
0 | 467 { |
111 | 468 struct bb_profile_info *bi = BB_INFO (bb); |
0 | 469 if (! bi->count_valid) |
470 { | |
471 if (bi->succ_count == 0) | |
472 { | |
473 edge e; | |
474 edge_iterator ei; | |
475 gcov_type total = 0; | |
476 | |
477 FOR_EACH_EDGE (e, ei, bb->succs) | |
111 | 478 total += edge_gcov_count (e); |
479 bb_gcov_count (bb) = total; | |
0 | 480 bi->count_valid = 1; |
481 changes = 1; | |
482 } | |
483 else if (bi->pred_count == 0) | |
484 { | |
485 edge e; | |
486 edge_iterator ei; | |
487 gcov_type total = 0; | |
488 | |
489 FOR_EACH_EDGE (e, ei, bb->preds) | |
111 | 490 total += edge_gcov_count (e); |
491 bb_gcov_count (bb) = total; | |
0 | 492 bi->count_valid = 1; |
493 changes = 1; | |
494 } | |
495 } | |
496 if (bi->count_valid) | |
497 { | |
498 if (bi->succ_count == 1) | |
499 { | |
500 edge e; | |
501 edge_iterator ei; | |
502 gcov_type total = 0; | |
503 | |
504 /* One of the counts will be invalid, but it is zero, | |
505 so adding it in also doesn't hurt. */ | |
506 FOR_EACH_EDGE (e, ei, bb->succs) | |
111 | 507 total += edge_gcov_count (e); |
0 | 508 |
509 /* Search for the invalid edge, and set its count. */ | |
510 FOR_EACH_EDGE (e, ei, bb->succs) | |
511 if (! EDGE_INFO (e)->count_valid && ! EDGE_INFO (e)->ignore) | |
512 break; | |
513 | |
514 /* Calculate count for remaining edge by conservation. */ | |
111 | 515 total = bb_gcov_count (bb) - total; |
0 | 516 |
517 gcc_assert (e); | |
518 EDGE_INFO (e)->count_valid = 1; | |
111 | 519 edge_gcov_count (e) = total; |
0 | 520 bi->succ_count--; |
521 | |
522 BB_INFO (e->dest)->pred_count--; | |
523 changes = 1; | |
524 } | |
525 if (bi->pred_count == 1) | |
526 { | |
527 edge e; | |
528 edge_iterator ei; | |
529 gcov_type total = 0; | |
530 | |
531 /* One of the counts will be invalid, but it is zero, | |
532 so adding it in also doesn't hurt. */ | |
533 FOR_EACH_EDGE (e, ei, bb->preds) | |
111 | 534 total += edge_gcov_count (e); |
0 | 535 |
536 /* Search for the invalid edge, and set its count. */ | |
537 FOR_EACH_EDGE (e, ei, bb->preds) | |
538 if (!EDGE_INFO (e)->count_valid && !EDGE_INFO (e)->ignore) | |
539 break; | |
540 | |
541 /* Calculate count for remaining edge by conservation. */ | |
111 | 542 total = bb_gcov_count (bb) - total + edge_gcov_count (e); |
0 | 543 |
544 gcc_assert (e); | |
545 EDGE_INFO (e)->count_valid = 1; | |
111 | 546 edge_gcov_count (e) = total; |
0 | 547 bi->pred_count--; |
548 | |
549 BB_INFO (e->src)->succ_count--; | |
550 changes = 1; | |
551 } | |
552 } | |
553 } | |
554 } | |
555 | |
556 total_num_passes += passes; | |
557 if (dump_file) | |
558 fprintf (dump_file, "Graph solving took %d passes.\n\n", passes); | |
559 | |
560 /* If the graph has been correctly solved, every block will have a | |
561 succ and pred count of zero. */ | |
111 | 562 FOR_EACH_BB_FN (bb, cfun) |
0 | 563 { |
564 gcc_assert (!BB_INFO (bb)->succ_count && !BB_INFO (bb)->pred_count); | |
565 } | |
566 | |
567 /* Check for inconsistent basic block counts */ | |
568 inconsistent = is_inconsistent (); | |
569 | |
570 if (inconsistent) | |
571 { | |
572 if (flag_profile_correction) | |
573 { | |
574 /* Inconsistency detected. Make it flow-consistent. */ | |
575 static int informed = 0; | |
111 | 576 if (dump_enabled_p () && informed == 0) |
0 | 577 { |
578 informed = 1; | |
131 | 579 dump_printf_loc (MSG_NOTE, |
580 dump_location_t::from_location_t (input_location), | |
111 | 581 "correcting inconsistent profile data\n"); |
0 | 582 } |
583 correct_negative_edge_counts (); | |
584 /* Set bb counts to the sum of the outgoing edge counts */ | |
585 set_bb_counts (); | |
586 if (dump_file) | |
587 fprintf (dump_file, "\nCalling mcf_smooth_cfg\n"); | |
588 mcf_smooth_cfg (); | |
589 } | |
590 else | |
591 error ("corrupted profile info: profile data is not flow-consistent"); | |
592 } | |
593 | |
594 /* For every edge, calculate its branch probability and add a reg_note | |
595 to the branch insn to indicate this. */ | |
596 | |
597 for (i = 0; i < 20; i++) | |
598 hist_br_prob[i] = 0; | |
599 num_branches = 0; | |
600 | |
111 | 601 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) |
0 | 602 { |
603 edge e; | |
604 edge_iterator ei; | |
605 | |
111 | 606 if (bb_gcov_count (bb) < 0) |
0 | 607 { |
608 error ("corrupted profile info: number of iterations for basic block %d thought to be %i", | |
111 | 609 bb->index, (int)bb_gcov_count (bb)); |
610 bb_gcov_count (bb) = 0; | |
0 | 611 } |
612 FOR_EACH_EDGE (e, ei, bb->succs) | |
613 { | |
614 /* Function may return twice in the cased the called function is | |
615 setjmp or calls fork, but we can't represent this by extra | |
616 edge from the entry, since extra edge from the exit is | |
617 already present. We get negative frequency from the entry | |
618 point. */ | |
111 | 619 if ((edge_gcov_count (e) < 0 |
620 && e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
621 || (edge_gcov_count (e) > bb_gcov_count (bb) | |
622 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))) | |
0 | 623 { |
624 if (block_ends_with_call_p (bb)) | |
111 | 625 edge_gcov_count (e) = edge_gcov_count (e) < 0 |
626 ? 0 : bb_gcov_count (bb); | |
0 | 627 } |
111 | 628 if (edge_gcov_count (e) < 0 |
629 || edge_gcov_count (e) > bb_gcov_count (bb)) | |
0 | 630 { |
631 error ("corrupted profile info: number of executions for edge %d-%d thought to be %i", | |
632 e->src->index, e->dest->index, | |
111 | 633 (int)edge_gcov_count (e)); |
634 edge_gcov_count (e) = bb_gcov_count (bb) / 2; | |
0 | 635 } |
636 } | |
111 | 637 if (bb_gcov_count (bb)) |
0 | 638 { |
639 FOR_EACH_EDGE (e, ei, bb->succs) | |
111 | 640 e->probability = profile_probability::probability_in_gcov_type |
641 (edge_gcov_count (e), bb_gcov_count (bb)); | |
0 | 642 if (bb->index >= NUM_FIXED_BLOCKS |
643 && block_ends_with_condjump_p (bb) | |
644 && EDGE_COUNT (bb->succs) >= 2) | |
645 { | |
646 int prob; | |
647 edge e; | |
648 int index; | |
649 | |
650 /* Find the branch edge. It is possible that we do have fake | |
651 edges here. */ | |
652 FOR_EACH_EDGE (e, ei, bb->succs) | |
653 if (!(e->flags & (EDGE_FAKE | EDGE_FALLTHRU))) | |
654 break; | |
655 | |
111 | 656 prob = e->probability.to_reg_br_prob_base (); |
0 | 657 index = prob * 20 / REG_BR_PROB_BASE; |
658 | |
659 if (index == 20) | |
660 index = 19; | |
661 hist_br_prob[index]++; | |
662 | |
663 num_branches++; | |
664 } | |
665 } | |
666 /* As a last resort, distribute the probabilities evenly. | |
667 Use simple heuristics that if there are normal edges, | |
668 give all abnormals frequency of 0, otherwise distribute the | |
669 frequency over abnormals (this is the case of noreturn | |
670 calls). */ | |
111 | 671 else if (profile_status_for_fn (cfun) == PROFILE_ABSENT) |
0 | 672 { |
673 int total = 0; | |
674 | |
675 FOR_EACH_EDGE (e, ei, bb->succs) | |
676 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE))) | |
677 total ++; | |
678 if (total) | |
679 { | |
680 FOR_EACH_EDGE (e, ei, bb->succs) | |
681 if (!(e->flags & (EDGE_COMPLEX | EDGE_FAKE))) | |
111 | 682 e->probability |
683 = profile_probability::guessed_always ().apply_scale (1, total); | |
0 | 684 else |
111 | 685 e->probability = profile_probability::never (); |
0 | 686 } |
687 else | |
688 { | |
689 total += EDGE_COUNT (bb->succs); | |
690 FOR_EACH_EDGE (e, ei, bb->succs) | |
111 | 691 e->probability |
692 = profile_probability::guessed_always ().apply_scale (1, total); | |
0 | 693 } |
694 if (bb->index >= NUM_FIXED_BLOCKS | |
695 && block_ends_with_condjump_p (bb) | |
696 && EDGE_COUNT (bb->succs) >= 2) | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
697 num_branches++; |
0 | 698 } |
699 } | |
111 | 700 |
131 | 701 /* If we have real data, use them! */ |
702 if (bb_gcov_count (ENTRY_BLOCK_PTR_FOR_FN (cfun)) | |
703 || !flag_guess_branch_prob) | |
704 FOR_ALL_BB_FN (bb, cfun) | |
111 | 705 bb->count = profile_count::from_gcov_type (bb_gcov_count (bb)); |
131 | 706 /* If function was not trained, preserve local estimates including statically |
707 determined zero counts. */ | |
708 else | |
709 FOR_ALL_BB_FN (bb, cfun) | |
710 if (!(bb->count == profile_count::zero ())) | |
711 bb->count = bb->count.global0 (); | |
712 | |
111 | 713 bb_gcov_counts.release (); |
714 delete edge_gcov_counts; | |
715 edge_gcov_counts = NULL; | |
716 | |
131 | 717 update_max_bb_count (); |
0 | 718 |
719 if (dump_file) | |
720 { | |
721 fprintf (dump_file, "%d branches\n", num_branches); | |
722 if (num_branches) | |
723 for (i = 0; i < 10; i++) | |
724 fprintf (dump_file, "%d%% branches in range %d-%d%%\n", | |
725 (hist_br_prob[i] + hist_br_prob[19-i]) * 100 / num_branches, | |
726 5 * i, 5 * i + 5); | |
727 | |
728 total_num_branches += num_branches; | |
729 for (i = 0; i < 20; i++) | |
730 total_hist_br_prob[i] += hist_br_prob[i]; | |
731 | |
732 fputc ('\n', dump_file); | |
733 fputc ('\n', dump_file); | |
734 } | |
735 | |
736 free_aux_for_blocks (); | |
737 } | |
738 | |
739 /* Load value histograms values whose description is stored in VALUES array | |
111 | 740 from .gcda file. |
741 | |
742 CFG_CHECKSUM is the precomputed checksum for the CFG. */ | |
0 | 743 |
744 static void | |
111 | 745 compute_value_histograms (histogram_values values, unsigned cfg_checksum, |
746 unsigned lineno_checksum) | |
0 | 747 { |
748 unsigned i, j, t, any; | |
749 unsigned n_histogram_counters[GCOV_N_VALUE_COUNTERS]; | |
750 gcov_type *histogram_counts[GCOV_N_VALUE_COUNTERS]; | |
751 gcov_type *act_count[GCOV_N_VALUE_COUNTERS]; | |
752 gcov_type *aact_count; | |
111 | 753 struct cgraph_node *node; |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
754 |
0 | 755 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++) |
756 n_histogram_counters[t] = 0; | |
757 | |
111 | 758 for (i = 0; i < values.length (); i++) |
0 | 759 { |
111 | 760 histogram_value hist = values[i]; |
0 | 761 n_histogram_counters[(int) hist->type] += hist->n_counters; |
762 } | |
763 | |
764 any = 0; | |
765 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++) | |
766 { | |
767 if (!n_histogram_counters[t]) | |
768 { | |
769 histogram_counts[t] = NULL; | |
770 continue; | |
771 } | |
772 | |
131 | 773 histogram_counts[t] = get_coverage_counts (COUNTER_FOR_HIST_TYPE (t), |
774 cfg_checksum, | |
775 lineno_checksum); | |
0 | 776 if (histogram_counts[t]) |
777 any = 1; | |
778 act_count[t] = histogram_counts[t]; | |
779 } | |
780 if (!any) | |
781 return; | |
782 | |
111 | 783 for (i = 0; i < values.length (); i++) |
0 | 784 { |
111 | 785 histogram_value hist = values[i]; |
786 gimple *stmt = hist->hvalue.stmt; | |
0 | 787 |
788 t = (int) hist->type; | |
789 | |
790 aact_count = act_count[t]; | |
111 | 791 |
792 if (act_count[t]) | |
793 act_count[t] += hist->n_counters; | |
0 | 794 |
795 gimple_add_histogram_value (cfun, stmt, hist); | |
796 hist->hvalue.counters = XNEWVEC (gcov_type, hist->n_counters); | |
797 for (j = 0; j < hist->n_counters; j++) | |
111 | 798 if (aact_count) |
799 hist->hvalue.counters[j] = aact_count[j]; | |
800 else | |
801 hist->hvalue.counters[j] = 0; | |
802 | |
803 /* Time profiler counter is not related to any statement, | |
804 so that we have to read the counter and set the value to | |
805 the corresponding call graph node. */ | |
806 if (hist->type == HIST_TYPE_TIME_PROFILE) | |
807 { | |
808 node = cgraph_node::get (hist->fun->decl); | |
809 node->tp_first_run = hist->hvalue.counters[0]; | |
810 | |
811 if (dump_file) | |
812 fprintf (dump_file, "Read tp_first_run: %d\n", node->tp_first_run); | |
813 } | |
0 | 814 } |
815 | |
816 for (t = 0; t < GCOV_N_VALUE_COUNTERS; t++) | |
111 | 817 free (histogram_counts[t]); |
0 | 818 } |
819 | |
131 | 820 /* Location triplet which records a location. */ |
821 struct location_triplet | |
822 { | |
823 const char *filename; | |
824 int lineno; | |
825 int bb_index; | |
826 }; | |
827 | |
828 /* Traits class for streamed_locations hash set below. */ | |
829 | |
830 struct location_triplet_hash : typed_noop_remove <location_triplet> | |
831 { | |
832 typedef location_triplet value_type; | |
833 typedef location_triplet compare_type; | |
834 | |
835 static hashval_t | |
836 hash (const location_triplet &ref) | |
837 { | |
838 inchash::hash hstate (0); | |
839 if (ref.filename) | |
840 hstate.add_int (strlen (ref.filename)); | |
841 hstate.add_int (ref.lineno); | |
842 hstate.add_int (ref.bb_index); | |
843 return hstate.end (); | |
844 } | |
845 | |
846 static bool | |
847 equal (const location_triplet &ref1, const location_triplet &ref2) | |
848 { | |
849 return ref1.lineno == ref2.lineno | |
850 && ref1.bb_index == ref2.bb_index | |
851 && ref1.filename != NULL | |
852 && ref2.filename != NULL | |
853 && strcmp (ref1.filename, ref2.filename) == 0; | |
854 } | |
855 | |
856 static void | |
857 mark_deleted (location_triplet &ref) | |
858 { | |
859 ref.lineno = -1; | |
860 } | |
861 | |
862 static void | |
863 mark_empty (location_triplet &ref) | |
864 { | |
865 ref.lineno = -2; | |
866 } | |
867 | |
868 static bool | |
869 is_deleted (const location_triplet &ref) | |
870 { | |
871 return ref.lineno == -1; | |
872 } | |
873 | |
874 static bool | |
875 is_empty (const location_triplet &ref) | |
876 { | |
877 return ref.lineno == -2; | |
878 } | |
879 }; | |
880 | |
881 | |
882 | |
883 | |
0 | 884 /* When passed NULL as file_name, initialize. |
885 When passed something else, output the necessary commands to change | |
886 line to LINE and offset to FILE_NAME. */ | |
887 static void | |
131 | 888 output_location (hash_set<location_triplet_hash> *streamed_locations, |
889 char const *file_name, int line, | |
0 | 890 gcov_position_t *offset, basic_block bb) |
891 { | |
892 static char const *prev_file_name; | |
893 static int prev_line; | |
894 bool name_differs, line_differs; | |
895 | |
131 | 896 location_triplet triplet; |
897 triplet.filename = file_name; | |
898 triplet.lineno = line; | |
899 triplet.bb_index = bb ? bb->index : 0; | |
900 | |
901 if (streamed_locations->add (triplet)) | |
902 return; | |
903 | |
0 | 904 if (!file_name) |
905 { | |
906 prev_file_name = NULL; | |
907 prev_line = -1; | |
908 return; | |
909 } | |
910 | |
111 | 911 name_differs = !prev_file_name || filename_cmp (file_name, prev_file_name); |
0 | 912 line_differs = prev_line != line; |
913 | |
111 | 914 if (!*offset) |
0 | 915 { |
111 | 916 *offset = gcov_write_tag (GCOV_TAG_LINES); |
917 gcov_write_unsigned (bb->index); | |
918 name_differs = line_differs = true; | |
919 } | |
0 | 920 |
111 | 921 /* If this is a new source file, then output the |
922 file's name to the .bb file. */ | |
923 if (name_differs) | |
924 { | |
925 prev_file_name = file_name; | |
926 gcov_write_unsigned (0); | |
927 gcov_write_filename (prev_file_name); | |
928 } | |
929 if (line_differs) | |
930 { | |
931 gcov_write_unsigned (line); | |
932 prev_line = line; | |
933 } | |
0 | 934 } |
935 | |
111 | 936 /* Helper for qsort so edges get sorted from highest frequency to smallest. |
937 This controls the weight for minimal spanning tree algorithm */ | |
938 static int | |
939 compare_freqs (const void *p1, const void *p2) | |
940 { | |
941 const_edge e1 = *(const const_edge *)p1; | |
942 const_edge e2 = *(const const_edge *)p2; | |
943 | |
944 /* Critical edges needs to be split which introduce extra control flow. | |
945 Make them more heavy. */ | |
946 int m1 = EDGE_CRITICAL_P (e1) ? 2 : 1; | |
947 int m2 = EDGE_CRITICAL_P (e2) ? 2 : 1; | |
948 | |
949 if (EDGE_FREQUENCY (e1) * m1 + m1 != EDGE_FREQUENCY (e2) * m2 + m2) | |
950 return EDGE_FREQUENCY (e2) * m2 + m2 - EDGE_FREQUENCY (e1) * m1 - m1; | |
951 /* Stabilize sort. */ | |
952 if (e1->src->index != e2->src->index) | |
953 return e2->src->index - e1->src->index; | |
954 return e2->dest->index - e1->dest->index; | |
955 } | |
956 | |
957 /* Instrument and/or analyze program behavior based on program the CFG. | |
958 | |
959 This function creates a representation of the control flow graph (of | |
960 the function being compiled) that is suitable for the instrumentation | |
961 of edges and/or converting measured edge counts to counts on the | |
962 complete CFG. | |
0 | 963 |
964 When FLAG_PROFILE_ARCS is nonzero, this function instruments the edges in | |
965 the flow graph that are needed to reconstruct the dynamic behavior of the | |
111 | 966 flow graph. This data is written to the gcno file for gcov. |
0 | 967 |
968 When FLAG_BRANCH_PROBABILITIES is nonzero, this function reads auxiliary | |
111 | 969 information from the gcda file containing edge count information from |
970 previous executions of the function being compiled. In this case, the | |
971 control flow graph is annotated with actual execution counts by | |
972 compute_branch_probabilities(). | |
0 | 973 |
974 Main entry point of this file. */ | |
975 | |
976 void | |
977 branch_prob (void) | |
978 { | |
979 basic_block bb; | |
980 unsigned i; | |
981 unsigned num_edges, ignored_edges; | |
982 unsigned num_instrumented; | |
983 struct edge_list *el; | |
111 | 984 histogram_values values = histogram_values (); |
985 unsigned cfg_checksum, lineno_checksum; | |
0 | 986 |
987 total_num_times_called++; | |
988 | |
989 flow_call_edges_add (NULL); | |
990 add_noreturn_fake_exit_edges (); | |
991 | |
131 | 992 hash_set <location_triplet_hash> streamed_locations; |
993 | |
0 | 994 /* We can't handle cyclic regions constructed using abnormal edges. |
995 To avoid these we replace every source of abnormal edge by a fake | |
996 edge from entry node and every destination by fake edge to exit. | |
997 This keeps graph acyclic and our calculation exact for all normal | |
998 edges except for exit and entrance ones. | |
999 | |
1000 We also add fake exit edges for each call and asm statement in the | |
1001 basic, since it may not return. */ | |
1002 | |
111 | 1003 FOR_EACH_BB_FN (bb, cfun) |
0 | 1004 { |
1005 int need_exit_edge = 0, need_entry_edge = 0; | |
1006 int have_exit_edge = 0, have_entry_edge = 0; | |
1007 edge e; | |
1008 edge_iterator ei; | |
1009 | |
1010 /* Functions returning multiple times are not handled by extra edges. | |
1011 Instead we simply allow negative counts on edges from exit to the | |
1012 block past call and corresponding probabilities. We can't go | |
1013 with the extra edges because that would result in flowgraph that | |
1014 needs to have fake edges outside the spanning tree. */ | |
1015 | |
1016 FOR_EACH_EDGE (e, ei, bb->succs) | |
1017 { | |
1018 gimple_stmt_iterator gsi; | |
111 | 1019 gimple *last = NULL; |
0 | 1020 |
1021 /* It may happen that there are compiler generated statements | |
1022 without a locus at all. Go through the basic block from the | |
1023 last to the first statement looking for a locus. */ | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1024 for (gsi = gsi_last_nondebug_bb (bb); |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1025 !gsi_end_p (gsi); |
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1026 gsi_prev_nondebug (&gsi)) |
0 | 1027 { |
1028 last = gsi_stmt (gsi); | |
111 | 1029 if (!RESERVED_LOCATION_P (gimple_location (last))) |
0 | 1030 break; |
1031 } | |
1032 | |
1033 /* Edge with goto locus might get wrong coverage info unless | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1034 it is the only edge out of BB. |
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1035 Don't do that when the locuses match, so |
0 | 1036 if (blah) goto something; |
1037 is not computed twice. */ | |
1038 if (last | |
1039 && gimple_has_location (last) | |
111 | 1040 && !RESERVED_LOCATION_P (e->goto_locus) |
0 | 1041 && !single_succ_p (bb) |
1042 && (LOCATION_FILE (e->goto_locus) | |
1043 != LOCATION_FILE (gimple_location (last)) | |
1044 || (LOCATION_LINE (e->goto_locus) | |
1045 != LOCATION_LINE (gimple_location (last))))) | |
1046 { | |
1047 basic_block new_bb = split_edge (e); | |
1048 edge ne = single_succ_edge (new_bb); | |
1049 ne->goto_locus = e->goto_locus; | |
1050 } | |
1051 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL)) | |
111 | 1052 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
0 | 1053 need_exit_edge = 1; |
111 | 1054 if (e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
0 | 1055 have_exit_edge = 1; |
1056 } | |
1057 FOR_EACH_EDGE (e, ei, bb->preds) | |
1058 { | |
1059 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL)) | |
111 | 1060 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
0 | 1061 need_entry_edge = 1; |
111 | 1062 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
0 | 1063 have_entry_edge = 1; |
1064 } | |
1065 | |
1066 if (need_exit_edge && !have_exit_edge) | |
1067 { | |
1068 if (dump_file) | |
1069 fprintf (dump_file, "Adding fake exit edge to bb %i\n", | |
1070 bb->index); | |
111 | 1071 make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FAKE); |
0 | 1072 } |
1073 if (need_entry_edge && !have_entry_edge) | |
1074 { | |
1075 if (dump_file) | |
1076 fprintf (dump_file, "Adding fake entry edge to bb %i\n", | |
1077 bb->index); | |
111 | 1078 make_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun), bb, EDGE_FAKE); |
1079 /* Avoid bbs that have both fake entry edge and also some | |
1080 exit edge. One of those edges wouldn't be added to the | |
1081 spanning tree, but we can't instrument any of them. */ | |
1082 if (have_exit_edge || need_exit_edge) | |
1083 { | |
1084 gimple_stmt_iterator gsi; | |
1085 gimple *first; | |
1086 | |
1087 gsi = gsi_start_nondebug_after_labels_bb (bb); | |
1088 gcc_checking_assert (!gsi_end_p (gsi)); | |
1089 first = gsi_stmt (gsi); | |
1090 /* Don't split the bbs containing __builtin_setjmp_receiver | |
1091 or ABNORMAL_DISPATCHER calls. These are very | |
1092 special and don't expect anything to be inserted before | |
1093 them. */ | |
1094 if (is_gimple_call (first) | |
1095 && (gimple_call_builtin_p (first, BUILT_IN_SETJMP_RECEIVER) | |
1096 || (gimple_call_flags (first) & ECF_RETURNS_TWICE) | |
1097 || (gimple_call_internal_p (first) | |
1098 && (gimple_call_internal_fn (first) | |
1099 == IFN_ABNORMAL_DISPATCHER)))) | |
1100 continue; | |
1101 | |
1102 if (dump_file) | |
1103 fprintf (dump_file, "Splitting bb %i after labels\n", | |
1104 bb->index); | |
1105 split_block_after_labels (bb); | |
1106 } | |
0 | 1107 } |
1108 } | |
1109 | |
1110 el = create_edge_list (); | |
1111 num_edges = NUM_EDGES (el); | |
111 | 1112 qsort (el->index_to_edge, num_edges, sizeof (edge), compare_freqs); |
1113 alloc_aux_for_edges (sizeof (struct edge_profile_info)); | |
0 | 1114 |
1115 /* The basic blocks are expected to be numbered sequentially. */ | |
1116 compact_blocks (); | |
1117 | |
1118 ignored_edges = 0; | |
1119 for (i = 0 ; i < num_edges ; i++) | |
1120 { | |
1121 edge e = INDEX_EDGE (el, i); | |
1122 | |
1123 /* Mark edges we've replaced by fake edges above as ignored. */ | |
1124 if ((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL)) | |
111 | 1125 && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) |
1126 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) | |
0 | 1127 { |
1128 EDGE_INFO (e)->ignore = 1; | |
1129 ignored_edges++; | |
1130 } | |
1131 } | |
1132 | |
1133 /* Create spanning tree from basic block graph, mark each edge that is | |
1134 on the spanning tree. We insert as many abnormal and critical edges | |
1135 as possible to minimize number of edge splits necessary. */ | |
1136 | |
1137 find_spanning_tree (el); | |
1138 | |
1139 /* Fake edges that are not on the tree will not be instrumented, so | |
1140 mark them ignored. */ | |
1141 for (num_instrumented = i = 0; i < num_edges; i++) | |
1142 { | |
1143 edge e = INDEX_EDGE (el, i); | |
111 | 1144 struct edge_profile_info *inf = EDGE_INFO (e); |
0 | 1145 |
1146 if (inf->ignore || inf->on_tree) | |
1147 /*NOP*/; | |
1148 else if (e->flags & EDGE_FAKE) | |
1149 { | |
1150 inf->ignore = 1; | |
1151 ignored_edges++; | |
1152 } | |
1153 else | |
1154 num_instrumented++; | |
1155 } | |
1156 | |
111 | 1157 total_num_blocks += n_basic_blocks_for_fn (cfun); |
0 | 1158 if (dump_file) |
111 | 1159 fprintf (dump_file, "%d basic blocks\n", n_basic_blocks_for_fn (cfun)); |
0 | 1160 |
1161 total_num_edges += num_edges; | |
1162 if (dump_file) | |
1163 fprintf (dump_file, "%d edges\n", num_edges); | |
1164 | |
1165 total_num_edges_ignored += ignored_edges; | |
1166 if (dump_file) | |
1167 fprintf (dump_file, "%d ignored edges\n", ignored_edges); | |
1168 | |
111 | 1169 total_num_edges_instrumented += num_instrumented; |
1170 if (dump_file) | |
1171 fprintf (dump_file, "%d instrumentation edges\n", num_instrumented); | |
0 | 1172 |
111 | 1173 /* Compute two different checksums. Note that we want to compute |
1174 the checksum in only once place, since it depends on the shape | |
1175 of the control flow which can change during | |
1176 various transformations. */ | |
1177 cfg_checksum = coverage_compute_cfg_checksum (cfun); | |
1178 lineno_checksum = coverage_compute_lineno_checksum (); | |
1179 | |
1180 /* Write the data from which gcov can reconstruct the basic block | |
1181 graph and function line numbers (the gcno file). */ | |
1182 if (coverage_begin_function (lineno_checksum, cfg_checksum)) | |
0 | 1183 { |
1184 gcov_position_t offset; | |
1185 | |
111 | 1186 /* Basic block flags */ |
0 | 1187 offset = gcov_write_tag (GCOV_TAG_BLOCKS); |
111 | 1188 gcov_write_unsigned (n_basic_blocks_for_fn (cfun)); |
0 | 1189 gcov_write_length (offset); |
1190 | |
111 | 1191 /* Arcs */ |
1192 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), | |
1193 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) | |
0 | 1194 { |
1195 edge e; | |
1196 edge_iterator ei; | |
1197 | |
1198 offset = gcov_write_tag (GCOV_TAG_ARCS); | |
111 | 1199 gcov_write_unsigned (bb->index); |
0 | 1200 |
1201 FOR_EACH_EDGE (e, ei, bb->succs) | |
1202 { | |
111 | 1203 struct edge_profile_info *i = EDGE_INFO (e); |
0 | 1204 if (!i->ignore) |
1205 { | |
1206 unsigned flag_bits = 0; | |
1207 | |
1208 if (i->on_tree) | |
1209 flag_bits |= GCOV_ARC_ON_TREE; | |
1210 if (e->flags & EDGE_FAKE) | |
1211 flag_bits |= GCOV_ARC_FAKE; | |
1212 if (e->flags & EDGE_FALLTHRU) | |
1213 flag_bits |= GCOV_ARC_FALLTHROUGH; | |
1214 /* On trees we don't have fallthru flags, but we can | |
1215 recompute them from CFG shape. */ | |
1216 if (e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE) | |
1217 && e->src->next_bb == e->dest) | |
1218 flag_bits |= GCOV_ARC_FALLTHROUGH; | |
1219 | |
111 | 1220 gcov_write_unsigned (e->dest->index); |
0 | 1221 gcov_write_unsigned (flag_bits); |
1222 } | |
1223 } | |
1224 | |
1225 gcov_write_length (offset); | |
1226 } | |
1227 | |
111 | 1228 /* Line numbers. */ |
0 | 1229 /* Initialize the output. */ |
131 | 1230 output_location (&streamed_locations, NULL, 0, NULL, NULL); |
1231 | |
1232 hash_set<int_hash <location_t, 0, 2> > seen_locations; | |
0 | 1233 |
111 | 1234 FOR_EACH_BB_FN (bb, cfun) |
0 | 1235 { |
1236 gimple_stmt_iterator gsi; | |
111 | 1237 gcov_position_t offset = 0; |
0 | 1238 |
111 | 1239 if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) |
0 | 1240 { |
131 | 1241 location_t loc = DECL_SOURCE_LOCATION (current_function_decl); |
1242 seen_locations.add (loc); | |
1243 expanded_location curr_location = expand_location (loc); | |
1244 output_location (&streamed_locations, curr_location.file, | |
1245 curr_location.line, &offset, bb); | |
0 | 1246 } |
1247 | |
1248 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
1249 { | |
111 | 1250 gimple *stmt = gsi_stmt (gsi); |
131 | 1251 location_t loc = gimple_location (stmt); |
1252 if (!RESERVED_LOCATION_P (loc)) | |
1253 { | |
1254 seen_locations.add (loc); | |
1255 output_location (&streamed_locations, gimple_filename (stmt), | |
1256 gimple_lineno (stmt), &offset, bb); | |
1257 } | |
0 | 1258 } |
1259 | |
131 | 1260 /* Notice GOTO expressions eliminated while constructing the CFG. |
1261 It's hard to distinguish such expression, but goto_locus should | |
1262 not be any of already seen location. */ | |
1263 location_t loc; | |
0 | 1264 if (single_succ_p (bb) |
131 | 1265 && (loc = single_succ_edge (bb)->goto_locus) |
1266 && !RESERVED_LOCATION_P (loc) | |
1267 && !seen_locations.contains (loc)) | |
0 | 1268 { |
131 | 1269 expanded_location curr_location = expand_location (loc); |
1270 output_location (&streamed_locations, curr_location.file, | |
1271 curr_location.line, &offset, bb); | |
0 | 1272 } |
1273 | |
1274 if (offset) | |
1275 { | |
1276 /* A file of NULL indicates the end of run. */ | |
1277 gcov_write_unsigned (0); | |
1278 gcov_write_string (NULL); | |
1279 gcov_write_length (offset); | |
1280 } | |
1281 } | |
1282 } | |
1283 | |
1284 if (flag_profile_values) | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
1285 gimple_find_values_to_profile (&values); |
0 | 1286 |
1287 if (flag_branch_probabilities) | |
1288 { | |
111 | 1289 compute_branch_probabilities (cfg_checksum, lineno_checksum); |
0 | 1290 if (flag_profile_values) |
111 | 1291 compute_value_histograms (values, cfg_checksum, lineno_checksum); |
0 | 1292 } |
1293 | |
1294 remove_fake_edges (); | |
1295 | |
1296 /* For each edge not on the spanning tree, add counting code. */ | |
1297 if (profile_arc_flag | |
1298 && coverage_counter_alloc (GCOV_COUNTER_ARCS, num_instrumented)) | |
1299 { | |
1300 unsigned n_instrumented; | |
1301 | |
111 | 1302 gimple_init_gcov_profiler (); |
0 | 1303 |
1304 n_instrumented = instrument_edges (el); | |
1305 | |
1306 gcc_assert (n_instrumented == num_instrumented); | |
1307 | |
1308 if (flag_profile_values) | |
1309 instrument_values (values); | |
1310 | |
1311 /* Commit changes done by instrumentation. */ | |
1312 gsi_commit_edge_inserts (); | |
1313 } | |
1314 | |
1315 free_aux_for_edges (); | |
1316 | |
111 | 1317 values.release (); |
0 | 1318 free_edge_list (el); |
111 | 1319 coverage_end_function (lineno_checksum, cfg_checksum); |
1320 if (flag_branch_probabilities && profile_info) | |
1321 { | |
1322 struct loop *loop; | |
1323 if (dump_file && (dump_flags & TDF_DETAILS)) | |
1324 report_predictor_hitrates (); | |
1325 profile_status_for_fn (cfun) = PROFILE_READ; | |
1326 | |
1327 /* At this moment we have precise loop iteration count estimates. | |
1328 Record them to loop structure before the profile gets out of date. */ | |
1329 FOR_EACH_LOOP (loop, 0) | |
1330 if (loop->header->count > 0) | |
1331 { | |
1332 gcov_type nit = expected_loop_iterations_unbounded (loop); | |
1333 widest_int bound = gcov_type_to_wide_int (nit); | |
1334 loop->any_estimate = false; | |
1335 record_niter_bound (loop, bound, true, false); | |
1336 } | |
1337 compute_function_frequency (); | |
1338 } | |
0 | 1339 } |
1340 | |
1341 /* Union find algorithm implementation for the basic blocks using | |
1342 aux fields. */ | |
1343 | |
1344 static basic_block | |
1345 find_group (basic_block bb) | |
1346 { | |
1347 basic_block group = bb, bb1; | |
1348 | |
1349 while ((basic_block) group->aux != group) | |
1350 group = (basic_block) group->aux; | |
1351 | |
1352 /* Compress path. */ | |
1353 while ((basic_block) bb->aux != group) | |
1354 { | |
1355 bb1 = (basic_block) bb->aux; | |
1356 bb->aux = (void *) group; | |
1357 bb = bb1; | |
1358 } | |
1359 return group; | |
1360 } | |
1361 | |
1362 static void | |
1363 union_groups (basic_block bb1, basic_block bb2) | |
1364 { | |
1365 basic_block bb1g = find_group (bb1); | |
1366 basic_block bb2g = find_group (bb2); | |
1367 | |
1368 /* ??? I don't have a place for the rank field. OK. Lets go w/o it, | |
1369 this code is unlikely going to be performance problem anyway. */ | |
1370 gcc_assert (bb1g != bb2g); | |
1371 | |
1372 bb1g->aux = bb2g; | |
1373 } | |
1374 | |
1375 /* This function searches all of the edges in the program flow graph, and puts | |
1376 as many bad edges as possible onto the spanning tree. Bad edges include | |
1377 abnormals edges, which can't be instrumented at the moment. Since it is | |
1378 possible for fake edges to form a cycle, we will have to develop some | |
1379 better way in the future. Also put critical edges to the tree, since they | |
1380 are more expensive to instrument. */ | |
1381 | |
1382 static void | |
1383 find_spanning_tree (struct edge_list *el) | |
1384 { | |
1385 int i; | |
1386 int num_edges = NUM_EDGES (el); | |
1387 basic_block bb; | |
1388 | |
1389 /* We use aux field for standard union-find algorithm. */ | |
111 | 1390 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun), NULL, next_bb) |
0 | 1391 bb->aux = bb; |
1392 | |
1393 /* Add fake edge exit to entry we can't instrument. */ | |
111 | 1394 union_groups (EXIT_BLOCK_PTR_FOR_FN (cfun), ENTRY_BLOCK_PTR_FOR_FN (cfun)); |
0 | 1395 |
1396 /* First add all abnormal edges to the tree unless they form a cycle. Also | |
111 | 1397 add all edges to the exit block to avoid inserting profiling code behind |
0 | 1398 setting return value from function. */ |
1399 for (i = 0; i < num_edges; i++) | |
1400 { | |
1401 edge e = INDEX_EDGE (el, i); | |
1402 if (((e->flags & (EDGE_ABNORMAL | EDGE_ABNORMAL_CALL | EDGE_FAKE)) | |
111 | 1403 || e->dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
0 | 1404 && !EDGE_INFO (e)->ignore |
1405 && (find_group (e->src) != find_group (e->dest))) | |
1406 { | |
1407 if (dump_file) | |
1408 fprintf (dump_file, "Abnormal edge %d to %d put to tree\n", | |
1409 e->src->index, e->dest->index); | |
1410 EDGE_INFO (e)->on_tree = 1; | |
1411 union_groups (e->src, e->dest); | |
1412 } | |
1413 } | |
1414 | |
111 | 1415 /* And now the rest. Edge list is sorted according to frequencies and |
1416 thus we will produce minimal spanning tree. */ | |
0 | 1417 for (i = 0; i < num_edges; i++) |
1418 { | |
1419 edge e = INDEX_EDGE (el, i); | |
1420 if (!EDGE_INFO (e)->ignore | |
1421 && find_group (e->src) != find_group (e->dest)) | |
1422 { | |
1423 if (dump_file) | |
1424 fprintf (dump_file, "Normal edge %d to %d put to tree\n", | |
1425 e->src->index, e->dest->index); | |
1426 EDGE_INFO (e)->on_tree = 1; | |
1427 union_groups (e->src, e->dest); | |
1428 } | |
1429 } | |
1430 | |
111 | 1431 clear_aux_for_blocks (); |
0 | 1432 } |
1433 | |
1434 /* Perform file-level initialization for branch-prob processing. */ | |
1435 | |
1436 void | |
1437 init_branch_prob (void) | |
1438 { | |
1439 int i; | |
1440 | |
1441 total_num_blocks = 0; | |
1442 total_num_edges = 0; | |
1443 total_num_edges_ignored = 0; | |
1444 total_num_edges_instrumented = 0; | |
1445 total_num_blocks_created = 0; | |
1446 total_num_passes = 0; | |
1447 total_num_times_called = 0; | |
1448 total_num_branches = 0; | |
1449 for (i = 0; i < 20; i++) | |
1450 total_hist_br_prob[i] = 0; | |
1451 } | |
1452 | |
1453 /* Performs file-level cleanup after branch-prob processing | |
1454 is completed. */ | |
1455 | |
1456 void | |
1457 end_branch_prob (void) | |
1458 { | |
1459 if (dump_file) | |
1460 { | |
1461 fprintf (dump_file, "\n"); | |
1462 fprintf (dump_file, "Total number of blocks: %d\n", | |
1463 total_num_blocks); | |
1464 fprintf (dump_file, "Total number of edges: %d\n", total_num_edges); | |
1465 fprintf (dump_file, "Total number of ignored edges: %d\n", | |
1466 total_num_edges_ignored); | |
1467 fprintf (dump_file, "Total number of instrumented edges: %d\n", | |
1468 total_num_edges_instrumented); | |
1469 fprintf (dump_file, "Total number of blocks created: %d\n", | |
1470 total_num_blocks_created); | |
1471 fprintf (dump_file, "Total number of graph solution passes: %d\n", | |
1472 total_num_passes); | |
1473 if (total_num_times_called != 0) | |
1474 fprintf (dump_file, "Average number of graph solution passes: %d\n", | |
1475 (total_num_passes + (total_num_times_called >> 1)) | |
1476 / total_num_times_called); | |
1477 fprintf (dump_file, "Total number of branches: %d\n", | |
1478 total_num_branches); | |
1479 if (total_num_branches) | |
1480 { | |
1481 int i; | |
1482 | |
1483 for (i = 0; i < 10; i++) | |
1484 fprintf (dump_file, "%d%% branches in range %d-%d%%\n", | |
1485 (total_hist_br_prob[i] + total_hist_br_prob[19-i]) * 100 | |
1486 / total_num_branches, 5*i, 5*i+5); | |
1487 } | |
1488 } | |
1489 } |