Mercurial > hg > CbC > CbC_gcc
annotate gcc/coverage.c @ 158:494b0b89df80 default tip
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2020 18:13:55 +0900 |
parents | 1830386684a0 |
children |
rev | line source |
---|---|
0 | 1 /* Read and write coverage files, and associated functionality. |
145 | 2 Copyright (C) 1990-2020 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 Further mangled by Nathan Sidwell, CodeSourcery | |
7 | |
8 This file is part of GCC. | |
9 | |
10 GCC is free software; you can redistribute it and/or modify it under | |
11 the terms of the GNU General Public License as published by the Free | |
12 Software Foundation; either version 3, or (at your option) any later | |
13 version. | |
14 | |
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
21 along with GCC; see the file COPYING3. If not see | |
22 <http://www.gnu.org/licenses/>. */ | |
23 | |
24 | |
25 #define GCOV_LINKAGE | |
26 | |
27 #include "config.h" | |
28 #include "system.h" | |
29 #include "coretypes.h" | |
111 | 30 #include "backend.h" |
31 #include "target.h" | |
0 | 32 #include "rtl.h" |
33 #include "tree.h" | |
111 | 34 #include "tree-pass.h" |
35 #include "memmodel.h" | |
0 | 36 #include "tm_p.h" |
111 | 37 #include "stringpool.h" |
38 #include "cgraph.h" | |
0 | 39 #include "coverage.h" |
111 | 40 #include "diagnostic-core.h" |
41 #include "fold-const.h" | |
42 #include "stor-layout.h" | |
43 #include "output.h" | |
44 #include "toplev.h" | |
0 | 45 #include "langhooks.h" |
46 #include "tree-iterator.h" | |
111 | 47 #include "context.h" |
48 #include "pass_manager.h" | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
49 #include "intl.h" |
111 | 50 #include "auto-profile.h" |
131 | 51 #include "profile.h" |
0 | 52 |
53 #include "gcov-io.c" | |
54 | |
111 | 55 struct GTY((chain_next ("%h.next"))) coverage_data |
0 | 56 { |
111 | 57 struct coverage_data *next; /* next function */ |
0 | 58 unsigned ident; /* function ident */ |
111 | 59 unsigned lineno_checksum; /* function lineno checksum */ |
60 unsigned cfg_checksum; /* function cfg checksum */ | |
61 tree fn_decl; /* the function decl */ | |
62 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */ | |
0 | 63 }; |
64 | |
65 /* Counts information for a function. */ | |
111 | 66 struct counts_entry : pointer_hash <counts_entry> |
0 | 67 { |
68 /* We hash by */ | |
69 unsigned ident; | |
70 unsigned ctr; | |
71 | |
72 /* Store */ | |
111 | 73 unsigned lineno_checksum; |
74 unsigned cfg_checksum; | |
0 | 75 gcov_type *counts; |
145 | 76 unsigned n_counts; |
0 | 77 |
111 | 78 /* hash_table support. */ |
79 static inline hashval_t hash (const counts_entry *); | |
80 static int equal (const counts_entry *, const counts_entry *); | |
81 static void remove (counts_entry *); | |
82 }; | |
0 | 83 |
111 | 84 static GTY(()) struct coverage_data *functions_head = 0; |
85 static struct coverage_data **functions_tail = &functions_head; | |
0 | 86 static unsigned no_coverage = 0; |
87 | |
88 /* Cumulative counter information for whole program. */ | |
89 static unsigned prg_ctr_mask; /* Mask of counter types generated. */ | |
90 | |
91 /* Counter information for current function. */ | |
92 static unsigned fn_ctr_mask; /* Mask of counters used. */ | |
111 | 93 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */ |
0 | 94 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */ |
95 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */ | |
96 | |
111 | 97 /* Coverage info VAR_DECL and function info type nodes. */ |
98 static GTY(()) tree gcov_info_var; | |
99 static GTY(()) tree gcov_fn_info_type; | |
100 static GTY(()) tree gcov_fn_info_ptr_type; | |
101 | |
102 /* Name of the notes (gcno) output file. The "bbg" prefix is for | |
103 historical reasons, when the notes file contained only the | |
104 basic block graph notes. | |
105 If this is NULL we're not writing to the notes file. */ | |
0 | 106 static char *bbg_file_name; |
107 | |
111 | 108 /* File stamp for notes file. */ |
109 static unsigned bbg_file_stamp; | |
110 | |
111 /* Name of the count data (gcda) file. */ | |
0 | 112 static char *da_file_name; |
113 | |
114 /* The names of merge functions for counters. */ | |
111 | 115 #define STR(str) #str |
116 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE), | |
117 static const char *const ctr_merge_functions[GCOV_COUNTERS] = { | |
118 #include "gcov-counter.def" | |
119 }; | |
120 #undef DEF_GCOV_COUNTER | |
121 #undef STR | |
122 | |
123 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME, | |
124 static const char *const ctr_names[GCOV_COUNTERS] = { | |
125 #include "gcov-counter.def" | |
126 }; | |
127 #undef DEF_GCOV_COUNTER | |
0 | 128 |
129 /* Forward declarations. */ | |
130 static void read_counts_file (void); | |
111 | 131 static tree build_var (tree, tree, int); |
132 static void build_fn_info_type (tree, unsigned, tree); | |
133 static void build_info_type (tree, tree); | |
134 static tree build_fn_info (const struct coverage_data *, tree, tree); | |
135 static tree build_info (tree, tree); | |
136 static bool coverage_obj_init (void); | |
137 static vec<constructor_elt, va_gc> *coverage_obj_fn | |
138 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *); | |
139 static void coverage_obj_finish (vec<constructor_elt, va_gc> *); | |
0 | 140 |
141 /* Return the type node for gcov_type. */ | |
142 | |
143 tree | |
144 get_gcov_type (void) | |
145 { | |
111 | 146 scalar_int_mode mode |
147 = smallest_int_mode_for_size (LONG_LONG_TYPE_SIZE > 32 ? 64 : 32); | |
148 return lang_hooks.types.type_for_mode (mode, false); | |
0 | 149 } |
150 | |
151 /* Return the type node for gcov_unsigned_t. */ | |
152 | |
153 static tree | |
154 get_gcov_unsigned_t (void) | |
155 { | |
111 | 156 scalar_int_mode mode = smallest_int_mode_for_size (32); |
157 return lang_hooks.types.type_for_mode (mode, true); | |
0 | 158 } |
159 | |
111 | 160 inline hashval_t |
161 counts_entry::hash (const counts_entry *entry) | |
0 | 162 { |
163 return entry->ident * GCOV_COUNTERS + entry->ctr; | |
164 } | |
165 | |
111 | 166 inline int |
167 counts_entry::equal (const counts_entry *entry1, const counts_entry *entry2) | |
0 | 168 { |
169 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr; | |
170 } | |
171 | |
111 | 172 inline void |
173 counts_entry::remove (counts_entry *entry) | |
0 | 174 { |
175 free (entry->counts); | |
176 free (entry); | |
177 } | |
178 | |
111 | 179 /* Hash table of count data. */ |
180 static hash_table<counts_entry> *counts_hash; | |
181 | |
0 | 182 /* Read in the counts file, if available. */ |
183 | |
184 static void | |
185 read_counts_file (void) | |
186 { | |
187 gcov_unsigned_t fn_ident = 0; | |
188 gcov_unsigned_t tag; | |
189 int is_error = 0; | |
111 | 190 unsigned lineno_checksum = 0; |
191 unsigned cfg_checksum = 0; | |
0 | 192 |
193 if (!gcov_open (da_file_name, 1)) | |
194 return; | |
195 | |
196 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC)) | |
197 { | |
198 warning (0, "%qs is not a gcov data file", da_file_name); | |
199 gcov_close (); | |
200 return; | |
201 } | |
202 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION) | |
203 { | |
204 char v[4], e[4]; | |
205 | |
206 GCOV_UNSIGNED2STRING (v, tag); | |
207 GCOV_UNSIGNED2STRING (e, GCOV_VERSION); | |
208 | |
209 warning (0, "%qs is version %q.*s, expected version %q.*s", | |
210 da_file_name, 4, v, 4, e); | |
211 gcov_close (); | |
212 return; | |
213 } | |
214 | |
111 | 215 /* Read the stamp, used for creating a generation count. */ |
216 tag = gcov_read_unsigned (); | |
217 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag); | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
218 |
111 | 219 counts_hash = new hash_table<counts_entry> (10); |
0 | 220 while ((tag = gcov_read_unsigned ())) |
221 { | |
222 gcov_unsigned_t length; | |
223 gcov_position_t offset; | |
224 | |
225 length = gcov_read_unsigned (); | |
226 offset = gcov_position (); | |
227 if (tag == GCOV_TAG_FUNCTION) | |
228 { | |
111 | 229 if (length) |
0 | 230 { |
111 | 231 fn_ident = gcov_read_unsigned (); |
232 lineno_checksum = gcov_read_unsigned (); | |
233 cfg_checksum = gcov_read_unsigned (); | |
0 | 234 } |
111 | 235 else |
236 fn_ident = lineno_checksum = cfg_checksum = 0; | |
0 | 237 } |
131 | 238 else if (tag == GCOV_TAG_OBJECT_SUMMARY) |
0 | 239 { |
131 | 240 profile_info = XCNEW (gcov_summary); |
241 profile_info->runs = gcov_read_unsigned (); | |
242 profile_info->sum_max = gcov_read_unsigned (); | |
0 | 243 } |
244 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident) | |
245 { | |
111 | 246 counts_entry **slot, *entry, elt; |
0 | 247 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length); |
248 unsigned ix; | |
249 | |
250 elt.ident = fn_ident; | |
251 elt.ctr = GCOV_COUNTER_FOR_TAG (tag); | |
252 | |
111 | 253 slot = counts_hash->find_slot (&elt, INSERT); |
0 | 254 entry = *slot; |
255 if (!entry) | |
256 { | |
111 | 257 *slot = entry = XCNEW (counts_entry); |
258 entry->ident = fn_ident; | |
0 | 259 entry->ctr = elt.ctr; |
111 | 260 entry->lineno_checksum = lineno_checksum; |
261 entry->cfg_checksum = cfg_checksum; | |
0 | 262 entry->counts = XCNEWVEC (gcov_type, n_counts); |
145 | 263 entry->n_counts = n_counts; |
0 | 264 } |
111 | 265 else if (entry->lineno_checksum != lineno_checksum |
266 || entry->cfg_checksum != cfg_checksum) | |
0 | 267 { |
145 | 268 error ("profile data for function %u is corrupted", fn_ident); |
111 | 269 error ("checksum is (%x,%x) instead of (%x,%x)", |
270 entry->lineno_checksum, entry->cfg_checksum, | |
271 lineno_checksum, cfg_checksum); | |
272 delete counts_hash; | |
273 counts_hash = NULL; | |
0 | 274 break; |
275 } | |
276 for (ix = 0; ix != n_counts; ix++) | |
277 entry->counts[ix] += gcov_read_counter (); | |
278 } | |
279 gcov_sync (offset, length); | |
280 if ((is_error = gcov_is_error ())) | |
281 { | |
111 | 282 error (is_error < 0 |
283 ? G_("%qs has overflowed") | |
284 : G_("%qs is corrupted"), | |
0 | 285 da_file_name); |
111 | 286 delete counts_hash; |
287 counts_hash = NULL; | |
0 | 288 break; |
289 } | |
290 } | |
291 | |
292 gcov_close (); | |
293 } | |
294 | |
295 /* Returns the counters for a particular tag. */ | |
296 | |
297 gcov_type * | |
131 | 298 get_coverage_counts (unsigned counter, unsigned cfg_checksum, |
145 | 299 unsigned lineno_checksum, unsigned int n_counts) |
0 | 300 { |
111 | 301 counts_entry *entry, elt; |
0 | 302 |
303 /* No hash table, no counts. */ | |
304 if (!counts_hash) | |
305 { | |
306 static int warned = 0; | |
307 | |
131 | 308 if (!warned++) |
309 { | |
310 warning (OPT_Wmissing_profile, | |
311 "%qs profile count data file not found", | |
312 da_file_name); | |
313 if (dump_enabled_p ()) | |
314 { | |
315 dump_user_location_t loc | |
316 = dump_user_location_t::from_location_t (input_location); | |
317 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, | |
318 "file %s not found, %s\n", da_file_name, | |
319 (flag_guess_branch_prob | |
320 ? "execution counts estimated" | |
321 : "execution counts assumed to be zero")); | |
322 } | |
323 } | |
0 | 324 return NULL; |
325 } | |
145 | 326 if (param_profile_func_internal_id) |
111 | 327 elt.ident = current_function_funcdef_no + 1; |
328 else | |
0 | 329 { |
111 | 330 gcc_assert (coverage_node_map_initialized_p ()); |
145 | 331 elt.ident = cgraph_node::get (current_function_decl)->profile_id; |
0 | 332 } |
111 | 333 elt.ctr = counter; |
334 entry = counts_hash->find (&elt); | |
131 | 335 if (!entry) |
336 { | |
337 if (counter == GCOV_COUNTER_ARCS) | |
338 warning_at (DECL_SOURCE_LOCATION (current_function_decl), | |
339 OPT_Wmissing_profile, | |
340 "profile for function %qD not found in profile data", | |
341 current_function_decl); | |
342 /* The function was not emitted, or is weak and not chosen in the | |
343 final executable. Silently fail, because there's nothing we | |
344 can do about it. */ | |
345 return NULL; | |
346 } | |
111 | 347 |
145 | 348 if (entry->cfg_checksum != cfg_checksum || entry->n_counts != n_counts) |
0 | 349 { |
350 static int warned = 0; | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
351 bool warning_printed = false; |
0 | 352 |
145 | 353 if (entry->n_counts != n_counts) |
354 warning_printed = | |
355 warning_at (DECL_SOURCE_LOCATION (current_function_decl), | |
356 OPT_Wcoverage_mismatch, | |
357 "number of counters in profile data for function %qD " | |
358 "does not match " | |
359 "its profile data (counter %qs, expected %i and have %i)", | |
360 current_function_decl, | |
361 ctr_names[counter], entry->n_counts, n_counts); | |
362 else | |
363 warning_printed = | |
364 warning_at (DECL_SOURCE_LOCATION (current_function_decl), | |
365 OPT_Wcoverage_mismatch, | |
366 "the control flow of function %qD does not match " | |
367 "its profile data (counter %qs)", current_function_decl, | |
368 ctr_names[counter]); | |
111 | 369 if (warning_printed && dump_enabled_p ()) |
0 | 370 { |
131 | 371 dump_user_location_t loc |
145 | 372 = dump_user_location_t::from_function_decl (current_function_decl); |
131 | 373 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, |
111 | 374 "use -Wno-error=coverage-mismatch to tolerate " |
375 "the mismatch but performance may drop if the " | |
376 "function is hot\n"); | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
377 |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
378 if (!seen_error () |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
379 && !warned++) |
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
380 { |
131 | 381 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc, |
111 | 382 "coverage mismatch ignored\n"); |
131 | 383 dump_printf (MSG_MISSED_OPTIMIZATION, |
111 | 384 flag_guess_branch_prob |
385 ? G_("execution counts estimated\n") | |
386 : G_("execution counts assumed to be zero\n")); | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
387 if (!flag_guess_branch_prob) |
131 | 388 dump_printf (MSG_MISSED_OPTIMIZATION, |
111 | 389 "this can result in poorly optimized code\n"); |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
390 } |
0 | 391 } |
392 | |
393 return NULL; | |
394 } | |
111 | 395 else if (entry->lineno_checksum != lineno_checksum) |
396 { | |
131 | 397 warning_at (DECL_SOURCE_LOCATION (current_function_decl), |
398 OPT_Wcoverage_mismatch, | |
399 "source locations for function %qD have changed," | |
400 " the profile data may be out of date", | |
401 current_function_decl); | |
111 | 402 } |
0 | 403 |
404 return entry->counts; | |
405 } | |
406 | |
407 /* Allocate NUM counters of type COUNTER. Returns nonzero if the | |
408 allocation succeeded. */ | |
409 | |
410 int | |
411 coverage_counter_alloc (unsigned counter, unsigned num) | |
412 { | |
413 if (no_coverage) | |
414 return 0; | |
415 | |
416 if (!num) | |
417 return 1; | |
418 | |
111 | 419 if (!fn_v_ctrs[counter]) |
0 | 420 { |
111 | 421 tree array_type = build_array_type (get_gcov_type (), NULL_TREE); |
0 | 422 |
111 | 423 fn_v_ctrs[counter] |
424 = build_var (current_function_decl, array_type, counter); | |
0 | 425 } |
111 | 426 |
0 | 427 fn_b_ctrs[counter] = fn_n_ctrs[counter]; |
428 fn_n_ctrs[counter] += num; | |
111 | 429 |
0 | 430 fn_ctr_mask |= 1 << counter; |
431 return 1; | |
432 } | |
433 | |
434 /* Generate a tree to access COUNTER NO. */ | |
435 | |
436 tree | |
437 tree_coverage_counter_ref (unsigned counter, unsigned no) | |
438 { | |
439 tree gcov_type_node = get_gcov_type (); | |
440 | |
441 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]); | |
442 | |
111 | 443 no += fn_b_ctrs[counter]; |
444 | |
0 | 445 /* "no" here is an array index, scaled to bytes later. */ |
111 | 446 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter], |
447 build_int_cst (integer_type_node, no), NULL, NULL); | |
0 | 448 } |
449 | |
450 /* Generate a tree to access the address of COUNTER NO. */ | |
451 | |
452 tree | |
453 tree_coverage_counter_addr (unsigned counter, unsigned no) | |
454 { | |
455 tree gcov_type_node = get_gcov_type (); | |
456 | |
457 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]); | |
111 | 458 no += fn_b_ctrs[counter]; |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
459 |
0 | 460 /* "no" here is an array index, scaled to bytes later. */ |
461 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node, | |
111 | 462 fn_v_ctrs[counter], |
463 build_int_cst (integer_type_node, no), | |
0 | 464 NULL, NULL)); |
465 } | |
466 | |
111 | 467 |
0 | 468 /* Generate a checksum for a string. CHKSUM is the current |
469 checksum. */ | |
470 | |
471 static unsigned | |
472 coverage_checksum_string (unsigned chksum, const char *string) | |
473 { | |
474 int i; | |
475 char *dup = NULL; | |
476 | |
477 /* Look for everything that looks if it were produced by | |
478 get_file_function_name and zero out the second part | |
479 that may result from flag_random_seed. This is not critical | |
480 as the checksums are used only for sanity checking. */ | |
481 for (i = 0; string[i]; i++) | |
482 { | |
483 int offset = 0; | |
484 if (!strncmp (string + i, "_GLOBAL__N_", 11)) | |
485 offset = 11; | |
486 if (!strncmp (string + i, "_GLOBAL__", 9)) | |
487 offset = 9; | |
488 | |
489 /* C++ namespaces do have scheme: | |
490 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname | |
491 since filename might contain extra underscores there seems | |
492 to be no better chance then walk all possible offsets looking | |
493 for magicnumber. */ | |
494 if (offset) | |
495 { | |
496 for (i = i + offset; string[i]; i++) | |
497 if (string[i]=='_') | |
498 { | |
499 int y; | |
500 | |
501 for (y = 1; y < 9; y++) | |
502 if (!(string[i + y] >= '0' && string[i + y] <= '9') | |
503 && !(string[i + y] >= 'A' && string[i + y] <= 'F')) | |
504 break; | |
505 if (y != 9 || string[i + 9] != '_') | |
506 continue; | |
507 for (y = 10; y < 18; y++) | |
508 if (!(string[i + y] >= '0' && string[i + y] <= '9') | |
509 && !(string[i + y] >= 'A' && string[i + y] <= 'F')) | |
510 break; | |
511 if (y != 18) | |
512 continue; | |
513 if (!dup) | |
514 string = dup = xstrdup (string); | |
515 for (y = 10; y < 18; y++) | |
516 dup[i + y] = '0'; | |
517 } | |
518 break; | |
519 } | |
520 } | |
521 | |
522 chksum = crc32_string (chksum, string); | |
111 | 523 free (dup); |
0 | 524 |
525 return chksum; | |
526 } | |
527 | |
528 /* Compute checksum for the current function. We generate a CRC32. */ | |
529 | |
111 | 530 unsigned |
531 coverage_compute_lineno_checksum (void) | |
0 | 532 { |
533 expanded_location xloc | |
534 = expand_location (DECL_SOURCE_LOCATION (current_function_decl)); | |
535 unsigned chksum = xloc.line; | |
536 | |
111 | 537 if (xloc.file) |
538 chksum = coverage_checksum_string (chksum, xloc.file); | |
0 | 539 chksum = coverage_checksum_string |
540 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl))); | |
541 | |
542 return chksum; | |
543 } | |
111 | 544 |
545 /* Compute profile ID. This is better to be unique in whole program. */ | |
546 | |
547 unsigned | |
548 coverage_compute_profile_id (struct cgraph_node *n) | |
549 { | |
550 unsigned chksum; | |
0 | 551 |
111 | 552 /* Externally visible symbols have unique name. */ |
553 if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name) | |
554 { | |
555 chksum = coverage_checksum_string | |
556 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl))); | |
557 } | |
558 else | |
0 | 559 { |
560 expanded_location xloc | |
111 | 561 = expand_location (DECL_SOURCE_LOCATION (n->decl)); |
145 | 562 bool use_name_only = (param_profile_func_internal_id == 0); |
111 | 563 |
564 chksum = (use_name_only ? 0 : xloc.line); | |
565 if (xloc.file) | |
566 chksum = coverage_checksum_string (chksum, xloc.file); | |
567 chksum = coverage_checksum_string | |
568 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl))); | |
569 if (!use_name_only && first_global_object_name) | |
570 chksum = coverage_checksum_string | |
571 (chksum, first_global_object_name); | |
572 chksum = coverage_checksum_string | |
573 (chksum, aux_base_name); | |
574 } | |
575 | |
576 /* Non-negative integers are hopefully small enough to fit in all targets. | |
577 Gcov file formats wants non-zero function IDs. */ | |
578 chksum = chksum & 0x7fffffff; | |
579 return chksum + (!chksum); | |
580 } | |
581 | |
582 /* Compute cfg checksum for the function FN given as argument. | |
583 The checksum is calculated carefully so that | |
584 source code changes that doesn't affect the control flow graph | |
585 won't change the checksum. | |
586 This is to make the profile data useable across source code change. | |
587 The downside of this is that the compiler may use potentially | |
588 wrong profile data - that the source code change has non-trivial impact | |
589 on the validity of profile data (e.g. the reversed condition) | |
590 but the compiler won't detect the change and use the wrong profile data. */ | |
591 | |
592 unsigned | |
593 coverage_compute_cfg_checksum (struct function *fn) | |
594 { | |
595 basic_block bb; | |
596 unsigned chksum = n_basic_blocks_for_fn (fn); | |
0 | 597 |
111 | 598 FOR_EACH_BB_FN (bb, fn) |
599 { | |
600 edge e; | |
601 edge_iterator ei; | |
602 chksum = crc32_byte (chksum, bb->index); | |
603 FOR_EACH_EDGE (e, ei, bb->succs) | |
604 { | |
605 chksum = crc32_byte (chksum, e->dest->index); | |
606 } | |
607 } | |
608 | |
609 return chksum; | |
610 } | |
611 | |
612 /* Begin output to the notes file for the current function. | |
613 Writes the function header. Returns nonzero if data should be output. */ | |
614 | |
615 int | |
616 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum) | |
617 { | |
618 expanded_location xloc; | |
619 unsigned long offset; | |
0 | 620 |
111 | 621 /* We don't need to output .gcno file unless we're under -ftest-coverage |
622 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */ | |
623 if (no_coverage || !bbg_file_name) | |
624 return 0; | |
625 | |
626 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl)); | |
0 | 627 |
111 | 628 /* Announce function */ |
629 offset = gcov_write_tag (GCOV_TAG_FUNCTION); | |
145 | 630 if (param_profile_func_internal_id) |
111 | 631 gcov_write_unsigned (current_function_funcdef_no + 1); |
632 else | |
633 { | |
634 gcc_assert (coverage_node_map_initialized_p ()); | |
635 gcov_write_unsigned ( | |
636 cgraph_node::get (current_function_decl)->profile_id); | |
0 | 637 } |
111 | 638 |
639 gcov_write_unsigned (lineno_checksum); | |
640 gcov_write_unsigned (cfg_checksum); | |
641 gcov_write_string (IDENTIFIER_POINTER | |
642 (DECL_ASSEMBLER_NAME (current_function_decl))); | |
131 | 643 gcov_write_unsigned (DECL_ARTIFICIAL (current_function_decl) |
644 && !DECL_FUNCTION_VERSIONED (current_function_decl) | |
145 | 645 && !DECL_LAMBDA_FUNCTION_P (current_function_decl)); |
111 | 646 gcov_write_filename (xloc.file); |
647 gcov_write_unsigned (xloc.line); | |
131 | 648 gcov_write_unsigned (xloc.column); |
649 | |
650 expanded_location endloc = expand_location (cfun->function_end_locus); | |
651 | |
652 /* Function can start in a single file and end in another one. */ | |
145 | 653 int end_line = endloc.file == xloc.file ? endloc.line : xloc.line; |
654 int end_column = endloc.file == xloc.file ? endloc.column: xloc.column; | |
655 gcc_assert (xloc.line <= end_line); | |
656 gcov_write_unsigned (end_line); | |
657 gcov_write_unsigned (end_column); | |
111 | 658 gcov_write_length (offset); |
659 | |
0 | 660 return !gcov_is_error (); |
661 } | |
662 | |
663 /* Finish coverage data for the current function. Verify no output | |
664 error has occurred. Save function coverage counts. */ | |
665 | |
666 void | |
111 | 667 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum) |
0 | 668 { |
669 unsigned i; | |
670 | |
111 | 671 if (bbg_file_name && gcov_is_error ()) |
0 | 672 { |
673 warning (0, "error writing %qs", bbg_file_name); | |
111 | 674 unlink (bbg_file_name); |
675 bbg_file_name = NULL; | |
0 | 676 } |
677 | |
678 if (fn_ctr_mask) | |
679 { | |
111 | 680 struct coverage_data *item = 0; |
681 | |
682 item = ggc_alloc<coverage_data> (); | |
0 | 683 |
145 | 684 if (param_profile_func_internal_id) |
111 | 685 item->ident = current_function_funcdef_no + 1; |
686 else | |
687 { | |
688 gcc_assert (coverage_node_map_initialized_p ()); | |
689 item->ident = cgraph_node::get (cfun->decl)->profile_id; | |
690 } | |
0 | 691 |
111 | 692 item->lineno_checksum = lineno_checksum; |
693 item->cfg_checksum = cfg_checksum; | |
694 | |
695 item->fn_decl = current_function_decl; | |
696 item->next = 0; | |
0 | 697 *functions_tail = item; |
698 functions_tail = &item->next; | |
699 | |
700 for (i = 0; i != GCOV_COUNTERS; i++) | |
701 { | |
111 | 702 tree var = fn_v_ctrs[i]; |
703 | |
704 if (item) | |
705 item->ctr_vars[i] = var; | |
706 if (var) | |
707 { | |
708 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1)); | |
709 array_type = build_array_type (get_gcov_type (), array_type); | |
710 TREE_TYPE (var) = array_type; | |
711 DECL_SIZE (var) = TYPE_SIZE (array_type); | |
712 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type); | |
713 varpool_node::finalize_decl (var); | |
714 } | |
715 | |
716 fn_b_ctrs[i] = fn_n_ctrs[i] = 0; | |
717 fn_v_ctrs[i] = NULL_TREE; | |
0 | 718 } |
719 prg_ctr_mask |= fn_ctr_mask; | |
720 fn_ctr_mask = 0; | |
721 } | |
111 | 722 } |
723 | |
724 /* Remove coverage file if opened. */ | |
725 | |
726 void | |
727 coverage_remove_note_file (void) | |
728 { | |
729 if (bbg_file_name) | |
730 { | |
731 gcov_close (); | |
732 unlink (bbg_file_name); | |
733 } | |
734 } | |
735 | |
736 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER | |
737 >= 0 it is a counter array, otherwise it is the function structure. */ | |
738 | |
739 static tree | |
740 build_var (tree fn_decl, tree type, int counter) | |
741 { | |
742 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type); | |
743 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl)); | |
744 char *buf; | |
745 size_t fn_name_len, len; | |
746 | |
747 fn_name = targetm.strip_name_encoding (fn_name); | |
748 fn_name_len = strlen (fn_name); | |
749 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3); | |
750 | |
751 if (counter < 0) | |
752 strcpy (buf, "__gcov__"); | |
753 else | |
754 sprintf (buf, "__gcov%u_", counter); | |
755 len = strlen (buf); | |
756 buf[len - 1] = symbol_table::symbol_suffix_separator (); | |
757 memcpy (buf + len, fn_name, fn_name_len + 1); | |
758 DECL_NAME (var) = get_identifier (buf); | |
759 TREE_STATIC (var) = 1; | |
760 TREE_ADDRESSABLE (var) = 1; | |
761 DECL_NONALIASED (var) = 1; | |
762 SET_DECL_ALIGN (var, TYPE_ALIGN (type)); | |
763 | |
764 return var; | |
0 | 765 } |
766 | |
767 /* Creates the gcov_fn_info RECORD_TYPE. */ | |
768 | |
111 | 769 static void |
770 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type) | |
0 | 771 { |
111 | 772 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE); |
0 | 773 tree field, fields; |
774 tree array_type; | |
775 | |
111 | 776 gcc_assert (counters); |
777 | |
778 /* ctr_info::num */ | |
779 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
780 get_gcov_unsigned_t ()); | |
781 fields = field; | |
782 | |
783 /* ctr_info::values */ | |
784 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
785 build_pointer_type (get_gcov_type ())); | |
786 DECL_CHAIN (field) = fields; | |
787 fields = field; | |
788 | |
789 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE); | |
0 | 790 |
111 | 791 /* key */ |
792 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
793 build_pointer_type (build_qualified_type | |
794 (gcov_info_type, TYPE_QUAL_CONST))); | |
795 fields = field; | |
796 | |
797 /* ident */ | |
798 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
799 get_gcov_unsigned_t ()); | |
800 DECL_CHAIN (field) = fields; | |
801 fields = field; | |
802 | |
803 /* lineno_checksum */ | |
804 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
805 get_gcov_unsigned_t ()); | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
806 DECL_CHAIN (field) = fields; |
0 | 807 fields = field; |
808 | |
111 | 809 /* cfg checksum */ |
810 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
811 get_gcov_unsigned_t ()); | |
812 DECL_CHAIN (field) = fields; | |
813 fields = field; | |
814 | |
815 array_type = build_index_type (size_int (counters - 1)); | |
816 array_type = build_array_type (ctr_info, array_type); | |
0 | 817 |
818 /* counters */ | |
111 | 819 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type); |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
820 DECL_CHAIN (field) = fields; |
0 | 821 fields = field; |
822 | |
823 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE); | |
824 } | |
825 | |
111 | 826 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is |
827 the coverage data for the function and TYPE is the gcov_fn_info | |
828 RECORD_TYPE. KEY is the object file key. */ | |
0 | 829 |
830 static tree | |
111 | 831 build_fn_info (const struct coverage_data *data, tree type, tree key) |
0 | 832 { |
833 tree fields = TYPE_FIELDS (type); | |
111 | 834 tree ctr_type; |
0 | 835 unsigned ix; |
111 | 836 vec<constructor_elt, va_gc> *v1 = NULL; |
837 vec<constructor_elt, va_gc> *v2 = NULL; | |
0 | 838 |
111 | 839 /* key */ |
840 CONSTRUCTOR_APPEND_ELT (v1, fields, | |
841 build1 (ADDR_EXPR, TREE_TYPE (fields), key)); | |
842 fields = DECL_CHAIN (fields); | |
843 | |
0 | 844 /* ident */ |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
845 CONSTRUCTOR_APPEND_ELT (v1, fields, |
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
846 build_int_cstu (get_gcov_unsigned_t (), |
111 | 847 data->ident)); |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
848 fields = DECL_CHAIN (fields); |
0 | 849 |
111 | 850 /* lineno_checksum */ |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
851 CONSTRUCTOR_APPEND_ELT (v1, fields, |
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
852 build_int_cstu (get_gcov_unsigned_t (), |
111 | 853 data->lineno_checksum)); |
854 fields = DECL_CHAIN (fields); | |
855 | |
856 /* cfg_checksum */ | |
857 CONSTRUCTOR_APPEND_ELT (v1, fields, | |
858 build_int_cstu (get_gcov_unsigned_t (), | |
859 data->cfg_checksum)); | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
860 fields = DECL_CHAIN (fields); |
0 | 861 |
862 /* counters */ | |
111 | 863 ctr_type = TREE_TYPE (TREE_TYPE (fields)); |
0 | 864 for (ix = 0; ix != GCOV_COUNTERS; ix++) |
865 if (prg_ctr_mask & (1 << ix)) | |
111 | 866 { |
867 vec<constructor_elt, va_gc> *ctr = NULL; | |
868 tree var = data->ctr_vars[ix]; | |
869 unsigned count = 0; | |
870 | |
871 if (var) | |
872 count | |
873 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var)))) | |
874 + 1; | |
0 | 875 |
111 | 876 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type), |
877 build_int_cstu (get_gcov_unsigned_t (), | |
878 count)); | |
879 | |
880 if (var) | |
881 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)), | |
882 build_fold_addr_expr (var)); | |
883 | |
884 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr)); | |
885 } | |
886 | |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
887 CONSTRUCTOR_APPEND_ELT (v1, fields, |
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
888 build_constructor (TREE_TYPE (fields), v2)); |
0 | 889 |
63
b7f97abdc517
update gcc from gcc-4.5.0 to gcc-4.6
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
55
diff
changeset
|
890 return build_constructor (type, v1); |
0 | 891 } |
892 | |
111 | 893 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be |
894 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */ | |
0 | 895 |
111 | 896 static void |
897 build_info_type (tree type, tree fn_info_ptr_type) | |
0 | 898 { |
899 tree field, fields = NULL_TREE; | |
111 | 900 tree merge_fn_type; |
901 | |
902 /* Version ident */ | |
903 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
904 get_gcov_unsigned_t ()); | |
905 DECL_CHAIN (field) = fields; | |
906 fields = field; | |
0 | 907 |
111 | 908 /* next pointer */ |
909 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
910 build_pointer_type (build_qualified_type | |
911 (type, TYPE_QUAL_CONST))); | |
912 DECL_CHAIN (field) = fields; | |
913 fields = field; | |
914 | |
915 /* stamp */ | |
916 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
917 get_gcov_unsigned_t ()); | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
918 DECL_CHAIN (field) = fields; |
0 | 919 fields = field; |
920 | |
111 | 921 /* Filename */ |
922 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
923 build_pointer_type (build_qualified_type | |
924 (char_type_node, TYPE_QUAL_CONST))); | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
925 DECL_CHAIN (field) = fields; |
0 | 926 fields = field; |
927 | |
111 | 928 /* merge fn array */ |
929 merge_fn_type | |
930 = build_function_type_list (void_type_node, | |
931 build_pointer_type (get_gcov_type ()), | |
932 get_gcov_unsigned_t (), NULL_TREE); | |
933 merge_fn_type | |
934 = build_array_type (build_pointer_type (merge_fn_type), | |
935 build_index_type (size_int (GCOV_COUNTERS - 1))); | |
936 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
937 merge_fn_type); | |
938 DECL_CHAIN (field) = fields; | |
939 fields = field; | |
940 | |
941 /* n_functions */ | |
942 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
943 get_gcov_unsigned_t ()); | |
944 DECL_CHAIN (field) = fields; | |
945 fields = field; | |
946 | |
947 /* function_info pointer pointer */ | |
948 fn_info_ptr_type = build_pointer_type | |
949 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST)); | |
950 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
951 fn_info_ptr_type); | |
67
f6334be47118
update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents:
63
diff
changeset
|
952 DECL_CHAIN (field) = fields; |
0 | 953 fields = field; |
954 | |
111 | 955 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE); |
0 | 956 } |
957 | |
111 | 958 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the |
959 gcov_info structure type, FN_ARY is the array of pointers to | |
960 function info objects. */ | |
0 | 961 |
962 static tree | |
111 | 963 build_info (tree info_type, tree fn_ary) |
0 | 964 { |
111 | 965 tree info_fields = TYPE_FIELDS (info_type); |
966 tree merge_fn_type, n_funcs; | |
967 unsigned ix; | |
0 | 968 tree filename_string; |
969 int da_file_name_len; | |
111 | 970 vec<constructor_elt, va_gc> *v1 = NULL; |
971 vec<constructor_elt, va_gc> *v2 = NULL; | |
0 | 972 |
973 /* Version ident */ | |
111 | 974 CONSTRUCTOR_APPEND_ELT (v1, info_fields, |
975 build_int_cstu (TREE_TYPE (info_fields), | |
976 GCOV_VERSION)); | |
977 info_fields = DECL_CHAIN (info_fields); | |
0 | 978 |
979 /* next -- NULL */ | |
111 | 980 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node); |
981 info_fields = DECL_CHAIN (info_fields); | |
982 | |
0 | 983 /* stamp */ |
111 | 984 CONSTRUCTOR_APPEND_ELT (v1, info_fields, |
985 build_int_cstu (TREE_TYPE (info_fields), | |
986 bbg_file_stamp)); | |
987 info_fields = DECL_CHAIN (info_fields); | |
0 | 988 |
989 /* Filename */ | |
990 da_file_name_len = strlen (da_file_name); | |
991 filename_string = build_string (da_file_name_len + 1, da_file_name); | |
992 TREE_TYPE (filename_string) = build_array_type | |
111 | 993 (char_type_node, build_index_type (size_int (da_file_name_len))); |
994 CONSTRUCTOR_APPEND_ELT (v1, info_fields, | |
995 build1 (ADDR_EXPR, TREE_TYPE (info_fields), | |
996 filename_string)); | |
997 info_fields = DECL_CHAIN (info_fields); | |
0 | 998 |
111 | 999 /* merge fn array -- NULL slots indicate unmeasured counters */ |
1000 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields)); | |
1001 for (ix = 0; ix != GCOV_COUNTERS; ix++) | |
1002 { | |
1003 tree ptr = null_pointer_node; | |
0 | 1004 |
111 | 1005 if ((1u << ix) & prg_ctr_mask) |
1006 { | |
1007 tree merge_fn = build_decl (BUILTINS_LOCATION, | |
1008 FUNCTION_DECL, | |
1009 get_identifier (ctr_merge_functions[ix]), | |
1010 TREE_TYPE (merge_fn_type)); | |
1011 DECL_EXTERNAL (merge_fn) = 1; | |
1012 TREE_PUBLIC (merge_fn) = 1; | |
1013 DECL_ARTIFICIAL (merge_fn) = 1; | |
1014 TREE_NOTHROW (merge_fn) = 1; | |
1015 /* Initialize assembler name so we can stream out. */ | |
1016 DECL_ASSEMBLER_NAME (merge_fn); | |
1017 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn); | |
1018 } | |
1019 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr); | |
1020 } | |
1021 CONSTRUCTOR_APPEND_ELT (v1, info_fields, | |
1022 build_constructor (TREE_TYPE (info_fields), v2)); | |
1023 info_fields = DECL_CHAIN (info_fields); | |
0 | 1024 |
111 | 1025 /* n_functions */ |
1026 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary))); | |
1027 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields), | |
1028 n_funcs, size_one_node); | |
1029 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs); | |
1030 info_fields = DECL_CHAIN (info_fields); | |
0 | 1031 |
111 | 1032 /* functions */ |
1033 CONSTRUCTOR_APPEND_ELT (v1, info_fields, | |
1034 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary)); | |
1035 info_fields = DECL_CHAIN (info_fields); | |
0 | 1036 |
111 | 1037 gcc_assert (!info_fields); |
1038 return build_constructor (info_type, v1); | |
0 | 1039 } |
1040 | |
111 | 1041 /* Generate the constructor function to call __gcov_init. */ |
1042 | |
1043 static void | |
1044 build_init_ctor (tree gcov_info_type) | |
1045 { | |
1046 tree ctor, stmt, init_fn; | |
1047 | |
1048 /* Build a decl for __gcov_init. */ | |
1049 init_fn = build_pointer_type (gcov_info_type); | |
1050 init_fn = build_function_type_list (void_type_node, init_fn, NULL); | |
1051 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, | |
1052 get_identifier ("__gcov_init"), init_fn); | |
1053 TREE_PUBLIC (init_fn) = 1; | |
1054 DECL_EXTERNAL (init_fn) = 1; | |
1055 DECL_ASSEMBLER_NAME (init_fn); | |
1056 | |
1057 /* Generate a call to __gcov_init(&gcov_info). */ | |
1058 ctor = NULL; | |
1059 stmt = build_fold_addr_expr (gcov_info_var); | |
1060 stmt = build_call_expr (init_fn, 1, stmt); | |
1061 append_to_statement_list (stmt, &ctor); | |
1062 | |
1063 /* Generate a constructor to run it. */ | |
1064 int priority = SUPPORTS_INIT_PRIORITY | |
1065 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY; | |
1066 cgraph_build_static_cdtor ('I', ctor, priority); | |
1067 } | |
1068 | |
1069 /* Generate the destructor function to call __gcov_exit. */ | |
0 | 1070 |
1071 static void | |
111 | 1072 build_gcov_exit_decl (void) |
0 | 1073 { |
145 | 1074 tree init_fn = build_function_type_list (void_type_node, NULL); |
111 | 1075 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, |
1076 get_identifier ("__gcov_exit"), init_fn); | |
1077 TREE_PUBLIC (init_fn) = 1; | |
1078 DECL_EXTERNAL (init_fn) = 1; | |
1079 DECL_ASSEMBLER_NAME (init_fn); | |
1080 | |
1081 /* Generate a call to __gcov_exit (). */ | |
1082 tree dtor = NULL; | |
1083 tree stmt = build_call_expr (init_fn, 0); | |
1084 append_to_statement_list (stmt, &dtor); | |
1085 | |
1086 /* Generate a destructor to run it. */ | |
1087 int priority = SUPPORTS_INIT_PRIORITY | |
1088 ? MAX_RESERVED_INIT_PRIORITY: DEFAULT_INIT_PRIORITY; | |
1089 | |
1090 cgraph_build_static_cdtor ('D', dtor, priority); | |
1091 } | |
1092 | |
1093 /* Create the gcov_info types and object. Generate the constructor | |
1094 function to call __gcov_init. Does not generate the initializer | |
1095 for the object. Returns TRUE if coverage data is being emitted. */ | |
1096 | |
1097 static bool | |
1098 coverage_obj_init (void) | |
1099 { | |
1100 tree gcov_info_type; | |
1101 unsigned n_counters = 0; | |
1102 unsigned ix; | |
1103 struct coverage_data *fn; | |
1104 struct coverage_data **fn_prev; | |
0 | 1105 char name_buf[32]; |
1106 | |
1107 no_coverage = 1; /* Disable any further coverage. */ | |
1108 | |
1109 if (!prg_ctr_mask) | |
111 | 1110 return false; |
1111 | |
1112 if (symtab->dump_file) | |
1113 fprintf (symtab->dump_file, "Using data file %s\n", da_file_name); | |
0 | 1114 |
111 | 1115 /* Prune functions. */ |
1116 for (fn_prev = &functions_head; (fn = *fn_prev);) | |
1117 if (DECL_STRUCT_FUNCTION (fn->fn_decl)) | |
1118 fn_prev = &fn->next; | |
1119 else | |
1120 /* The function is not being emitted, remove from list. */ | |
1121 *fn_prev = fn->next; | |
1122 | |
1123 if (functions_head == NULL) | |
1124 return false; | |
0 | 1125 |
111 | 1126 for (ix = 0; ix != GCOV_COUNTERS; ix++) |
1127 if ((1u << ix) & prg_ctr_mask) | |
1128 n_counters++; | |
1129 | |
1130 /* Build the info and fn_info types. These are mutually recursive. */ | |
1131 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE); | |
1132 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE); | |
1133 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type); | |
1134 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE); | |
1135 gcov_fn_info_ptr_type = build_pointer_type | |
1136 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST)); | |
1137 build_info_type (gcov_info_type, gcov_fn_info_ptr_type); | |
1138 | |
1139 /* Build the gcov info var, this is referred to in its own | |
1140 initializer. */ | |
1141 gcov_info_var = build_decl (BUILTINS_LOCATION, | |
1142 VAR_DECL, NULL_TREE, gcov_info_type); | |
1143 TREE_STATIC (gcov_info_var) = 1; | |
0 | 1144 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0); |
111 | 1145 DECL_NAME (gcov_info_var) = get_identifier (name_buf); |
0 | 1146 |
111 | 1147 build_init_ctor (gcov_info_type); |
1148 build_gcov_exit_decl (); | |
1149 | |
1150 return true; | |
1151 } | |
0 | 1152 |
111 | 1153 /* Generate the coverage function info for FN and DATA. Append a |
1154 pointer to that object to CTOR and return the appended CTOR. */ | |
1155 | |
1156 static vec<constructor_elt, va_gc> * | |
1157 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn, | |
1158 struct coverage_data const *data) | |
1159 { | |
1160 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var); | |
1161 tree var = build_var (fn, gcov_fn_info_type, -1); | |
1162 | |
1163 DECL_INITIAL (var) = init; | |
1164 varpool_node::finalize_decl (var); | |
1165 | |
1166 CONSTRUCTOR_APPEND_ELT (ctor, NULL, | |
1167 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var)); | |
1168 return ctor; | |
1169 } | |
1170 | |
1171 /* Finalize the coverage data. Generates the array of pointers to | |
1172 function objects from CTOR. Generate the gcov_info initializer. */ | |
0 | 1173 |
111 | 1174 static void |
1175 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor) | |
1176 { | |
1177 unsigned n_functions = vec_safe_length (ctor); | |
1178 tree fn_info_ary_type = build_array_type | |
1179 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST), | |
1180 build_index_type (size_int (n_functions - 1))); | |
1181 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, | |
1182 fn_info_ary_type); | |
1183 char name_buf[32]; | |
0 | 1184 |
111 | 1185 TREE_STATIC (fn_info_ary) = 1; |
1186 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1); | |
1187 DECL_NAME (fn_info_ary) = get_identifier (name_buf); | |
1188 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor); | |
1189 varpool_node::finalize_decl (fn_info_ary); | |
1190 | |
1191 DECL_INITIAL (gcov_info_var) | |
1192 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary); | |
1193 varpool_node::finalize_decl (gcov_info_var); | |
0 | 1194 } |
111 | 1195 |
0 | 1196 /* Perform file-level initialization. Read in data file, generate name |
111 | 1197 of notes file. */ |
0 | 1198 |
1199 void | |
1200 coverage_init (const char *filename) | |
1201 { | |
1202 int len = strlen (filename); | |
111 | 1203 int prefix_len = 0; |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1204 |
111 | 1205 /* Since coverage_init is invoked very early, before the pass |
1206 manager, we need to set up the dumping explicitly. This is | |
1207 similar to the handling in finish_optimization_passes. */ | |
1208 int profile_pass_num = | |
1209 g->get_passes ()->get_pass_profile ()->static_pass_number; | |
1210 g->get_dumps ()->dump_start (profile_pass_num, NULL); | |
1211 | |
131 | 1212 if (!IS_ABSOLUTE_PATH (filename)) |
1213 { | |
1214 /* When a profile_data_prefix is provided, then mangle full path | |
1215 of filename in order to prevent file path clashing. */ | |
1216 if (profile_data_prefix) | |
1217 { | |
1218 #if HAVE_DOS_BASED_FILE_SYSTEM | |
1219 const char *separator = "\\"; | |
1220 #else | |
1221 const char *separator = "/"; | |
1222 #endif | |
1223 filename = concat (getpwd (), separator, filename, NULL); | |
1224 filename = mangle_path (filename); | |
1225 len = strlen (filename); | |
1226 } | |
1227 else | |
1228 profile_data_prefix = getpwd (); | |
1229 } | |
0 | 1230 |
111 | 1231 if (profile_data_prefix) |
1232 prefix_len = strlen (profile_data_prefix); | |
0 | 1233 |
1234 /* Name of da file. */ | |
55
77e2b8dfacca
update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
1235 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX) |
111 | 1236 + prefix_len + 2); |
0 | 1237 |
1238 if (profile_data_prefix) | |
1239 { | |
111 | 1240 memcpy (da_file_name, profile_data_prefix, prefix_len); |
1241 da_file_name[prefix_len++] = '/'; | |
0 | 1242 } |
111 | 1243 memcpy (da_file_name + prefix_len, filename, len); |
1244 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX); | |
1245 | |
1246 bbg_file_stamp = local_tick; | |
1247 | |
1248 if (flag_auto_profile) | |
1249 read_autofdo_file (); | |
1250 else if (flag_branch_probabilities) | |
1251 read_counts_file (); | |
0 | 1252 |
1253 /* Name of bbg file. */ | |
111 | 1254 if (flag_test_coverage && !flag_compare_debug) |
1255 { | |
145 | 1256 if (profile_note_location) |
1257 bbg_file_name = xstrdup (profile_note_location); | |
1258 else | |
1259 { | |
1260 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1); | |
1261 memcpy (bbg_file_name, filename, len); | |
1262 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX); | |
1263 } | |
0 | 1264 |
111 | 1265 if (!gcov_open (bbg_file_name, -1)) |
1266 { | |
1267 error ("cannot open %s", bbg_file_name); | |
1268 bbg_file_name = NULL; | |
1269 } | |
1270 else | |
1271 { | |
1272 gcov_write_unsigned (GCOV_NOTE_MAGIC); | |
1273 gcov_write_unsigned (GCOV_VERSION); | |
1274 gcov_write_unsigned (bbg_file_stamp); | |
131 | 1275 gcov_write_string (getpwd ()); |
1276 | |
1277 /* Do not support has_unexecuted_blocks for Ada. */ | |
1278 gcov_write_unsigned (strcmp (lang_hooks.name, "GNU Ada") != 0); | |
111 | 1279 } |
1280 } | |
1281 | |
1282 g->get_dumps ()->dump_finish (profile_pass_num); | |
0 | 1283 } |
1284 | |
111 | 1285 /* Performs file-level cleanup. Close notes file, generate coverage |
0 | 1286 variables and constructor. */ |
1287 | |
1288 void | |
1289 coverage_finish (void) | |
1290 { | |
111 | 1291 if (bbg_file_name && gcov_close ()) |
1292 unlink (bbg_file_name); | |
1293 | |
1294 if (!flag_branch_probabilities && flag_test_coverage | |
1295 && (!local_tick || local_tick == (unsigned)-1)) | |
1296 /* Only remove the da file, if we're emitting coverage code and | |
1297 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */ | |
1298 unlink (da_file_name); | |
0 | 1299 |
111 | 1300 if (coverage_obj_init ()) |
1301 { | |
1302 vec<constructor_elt, va_gc> *fn_ctor = NULL; | |
1303 struct coverage_data *fn; | |
1304 | |
1305 for (fn = functions_head; fn; fn = fn->next) | |
1306 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn); | |
1307 coverage_obj_finish (fn_ctor); | |
0 | 1308 } |
111 | 1309 |
1310 XDELETEVEC (da_file_name); | |
1311 da_file_name = NULL; | |
0 | 1312 } |
1313 | |
1314 #include "gt-coverage.h" |