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