* friend.c (make_friend_class): Handle template template parameters.
[official-gcc.git] / gcc / coverage.c
blobb4d22dfd9c69922ed7069deb7eae9c77b35460c9
1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
3 2000, 2001, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
5 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
6 based on some ideas from Dain Samples of UC Berkeley.
7 Further mangling by Bob Manson, Cygnus Support.
8 Further mangled by Nathan Sidwell, CodeSourcery
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 3, or (at your option) any later
15 version.
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 for more details.
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
27 #define GCOV_LINKAGE
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "regs.h"
38 #include "expr.h"
39 #include "function.h"
40 #include "basic-block.h"
41 #include "toplev.h"
42 #include "tm_p.h"
43 #include "ggc.h"
44 #include "coverage.h"
45 #include "langhooks.h"
46 #include "hash-table.h"
47 #include "tree-iterator.h"
48 #include "cgraph.h"
49 #include "dumpfile.h"
50 #include "diagnostic-core.h"
51 #include "intl.h"
52 #include "filenames.h"
53 #include "target.h"
55 #include "gcov-io.h"
56 #include "gcov-io.c"
58 struct GTY((chain_next ("%h.next"))) coverage_data
60 struct coverage_data *next; /* next function */
61 unsigned ident; /* function ident */
62 unsigned lineno_checksum; /* function lineno checksum */
63 unsigned cfg_checksum; /* function cfg checksum */
64 tree fn_decl; /* the function decl */
65 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
68 /* Counts information for a function. */
69 typedef struct counts_entry
71 /* We hash by */
72 unsigned ident;
73 unsigned ctr;
75 /* Store */
76 unsigned lineno_checksum;
77 unsigned cfg_checksum;
78 gcov_type *counts;
79 struct gcov_ctr_summary summary;
81 /* hash_table support. */
82 typedef counts_entry T;
83 static inline hashval_t hash (const counts_entry *);
84 static int equal (const counts_entry *, const counts_entry *);
85 static void remove (counts_entry *);
86 } counts_entry_t;
88 static GTY(()) struct coverage_data *functions_head = 0;
89 static struct coverage_data **functions_tail = &functions_head;
90 static unsigned no_coverage = 0;
92 /* Cumulative counter information for whole program. */
93 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
95 /* Counter information for current function. */
96 static unsigned fn_ctr_mask; /* Mask of counters used. */
97 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
98 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
99 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
101 /* Coverage info VAR_DECL and function info type nodes. */
102 static GTY(()) tree gcov_info_var;
103 static GTY(()) tree gcov_fn_info_type;
104 static GTY(()) tree gcov_fn_info_ptr_type;
106 /* Name of the notes (gcno) output file. The "bbg" prefix is for
107 historical reasons, when the notes file contained only the
108 basic block graph notes.
109 If this is NULL we're not writing to the notes file. */
110 static char *bbg_file_name;
112 /* File stamp for notes file. */
113 static unsigned bbg_file_stamp;
115 /* Name of the count data (gcda) file. */
116 static char *da_file_name;
118 /* The names of merge functions for counters. */
119 static const char *const ctr_merge_functions[GCOV_COUNTERS] = GCOV_MERGE_FUNCTIONS;
120 static const char *const ctr_names[GCOV_COUNTERS] = GCOV_COUNTER_NAMES;
122 /* Forward declarations. */
123 static void read_counts_file (void);
124 static tree build_var (tree, tree, int);
125 static void build_fn_info_type (tree, unsigned, tree);
126 static void build_info_type (tree, tree);
127 static tree build_fn_info (const struct coverage_data *, tree, tree);
128 static tree build_info (tree, tree);
129 static bool coverage_obj_init (void);
130 static VEC(constructor_elt,gc) *coverage_obj_fn
131 (VEC(constructor_elt,gc) *, tree, struct coverage_data const *);
132 static void coverage_obj_finish (VEC(constructor_elt,gc) *);
134 /* Return the type node for gcov_type. */
136 tree
137 get_gcov_type (void)
139 enum machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
140 return lang_hooks.types.type_for_mode (mode, false);
143 /* Return the type node for gcov_unsigned_t. */
145 static tree
146 get_gcov_unsigned_t (void)
148 enum machine_mode mode = smallest_mode_for_size (32, MODE_INT);
149 return lang_hooks.types.type_for_mode (mode, true);
152 inline hashval_t
153 counts_entry::hash (const counts_entry_t *entry)
155 return entry->ident * GCOV_COUNTERS + entry->ctr;
158 inline int
159 counts_entry::equal (const counts_entry_t *entry1,
160 const counts_entry_t *entry2)
162 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
165 inline void
166 counts_entry::remove (counts_entry_t *entry)
168 free (entry->counts);
169 free (entry);
172 /* Hash table of count data. */
173 static hash_table <counts_entry> counts_hash;
175 /* Read in the counts file, if available. */
177 static void
178 read_counts_file (void)
180 gcov_unsigned_t fn_ident = 0;
181 struct gcov_summary summary;
182 unsigned new_summary = 1;
183 gcov_unsigned_t tag;
184 int is_error = 0;
185 unsigned lineno_checksum = 0;
186 unsigned cfg_checksum = 0;
188 if (!gcov_open (da_file_name, 1))
189 return;
191 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
193 warning (0, "%qs is not a gcov data file", da_file_name);
194 gcov_close ();
195 return;
197 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
199 char v[4], e[4];
201 GCOV_UNSIGNED2STRING (v, tag);
202 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
204 warning (0, "%qs is version %q.*s, expected version %q.*s",
205 da_file_name, 4, v, 4, e);
206 gcov_close ();
207 return;
210 /* Read the stamp, used for creating a generation count. */
211 tag = gcov_read_unsigned ();
212 bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
214 counts_hash.create (10);
215 while ((tag = gcov_read_unsigned ()))
217 gcov_unsigned_t length;
218 gcov_position_t offset;
220 length = gcov_read_unsigned ();
221 offset = gcov_position ();
222 if (tag == GCOV_TAG_FUNCTION)
224 if (length)
226 fn_ident = gcov_read_unsigned ();
227 lineno_checksum = gcov_read_unsigned ();
228 cfg_checksum = gcov_read_unsigned ();
230 else
231 fn_ident = lineno_checksum = cfg_checksum = 0;
232 new_summary = 1;
234 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
236 struct gcov_summary sum;
237 unsigned ix;
239 if (new_summary)
240 memset (&summary, 0, sizeof (summary));
242 gcov_read_summary (&sum);
243 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
245 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
246 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
247 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
248 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
249 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
251 new_summary = 0;
253 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
255 counts_entry_t **slot, *entry, elt;
256 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
257 unsigned ix;
259 elt.ident = fn_ident;
260 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
262 slot = counts_hash.find_slot (&elt, INSERT);
263 entry = *slot;
264 if (!entry)
266 *slot = entry = XCNEW (counts_entry_t);
267 entry->ident = fn_ident;
268 entry->ctr = elt.ctr;
269 entry->lineno_checksum = lineno_checksum;
270 entry->cfg_checksum = cfg_checksum;
271 entry->summary = summary.ctrs[elt.ctr];
272 entry->summary.num = n_counts;
273 entry->counts = XCNEWVEC (gcov_type, n_counts);
275 else if (entry->lineno_checksum != lineno_checksum
276 || entry->cfg_checksum != cfg_checksum)
278 error ("Profile data for function %u is corrupted", fn_ident);
279 error ("checksum is (%x,%x) instead of (%x,%x)",
280 entry->lineno_checksum, entry->cfg_checksum,
281 lineno_checksum, cfg_checksum);
282 counts_hash.dispose ();
283 break;
285 else if (entry->summary.num != n_counts)
287 error ("Profile data for function %u is corrupted", fn_ident);
288 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
289 counts_hash.dispose ();
290 break;
292 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
294 error ("cannot merge separate %s counters for function %u",
295 ctr_names[elt.ctr], fn_ident);
296 goto skip_merge;
298 else
300 entry->summary.runs += summary.ctrs[elt.ctr].runs;
301 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
302 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
303 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
304 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
306 for (ix = 0; ix != n_counts; ix++)
307 entry->counts[ix] += gcov_read_counter ();
308 skip_merge:;
310 gcov_sync (offset, length);
311 if ((is_error = gcov_is_error ()))
313 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
314 da_file_name);
315 counts_hash.dispose ();
316 break;
320 gcov_close ();
323 /* Returns the counters for a particular tag. */
325 gcov_type *
326 get_coverage_counts (unsigned counter, unsigned expected,
327 unsigned cfg_checksum, unsigned lineno_checksum,
328 const struct gcov_ctr_summary **summary)
330 counts_entry_t *entry, elt;
332 /* No hash table, no counts. */
333 if (!counts_hash.is_created ())
335 static int warned = 0;
337 if (!warned++)
338 inform (input_location, (flag_guess_branch_prob
339 ? "file %s not found, execution counts estimated"
340 : "file %s not found, execution counts assumed to be zero"),
341 da_file_name);
342 return NULL;
345 elt.ident = current_function_funcdef_no + 1;
346 elt.ctr = counter;
347 entry = counts_hash.find (&elt);
348 if (!entry || !entry->summary.num)
349 /* The function was not emitted, or is weak and not chosen in the
350 final executable. Silently fail, because there's nothing we
351 can do about it. */
352 return NULL;
354 if (entry->cfg_checksum != cfg_checksum
355 || entry->summary.num != expected)
357 static int warned = 0;
358 bool warning_printed = false;
359 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
361 warning_printed =
362 warning_at (input_location, OPT_Wcoverage_mismatch,
363 "the control flow of function %qE does not match "
364 "its profile data (counter %qs)", id, ctr_names[counter]);
365 if (warning_printed)
367 inform (input_location, "use -Wno-error=coverage-mismatch to tolerate "
368 "the mismatch but performance may drop if the function is hot");
370 if (!seen_error ()
371 && !warned++)
373 inform (input_location, "coverage mismatch ignored");
374 inform (input_location, flag_guess_branch_prob
375 ? G_("execution counts estimated")
376 : G_("execution counts assumed to be zero"));
377 if (!flag_guess_branch_prob)
378 inform (input_location,
379 "this can result in poorly optimized code");
383 return NULL;
385 else if (entry->lineno_checksum != lineno_checksum)
387 warning (0, "source locations for function %qE have changed,"
388 " the profile data may be out of date",
389 DECL_ASSEMBLER_NAME (current_function_decl));
392 if (summary)
393 *summary = &entry->summary;
395 return entry->counts;
398 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
399 allocation succeeded. */
402 coverage_counter_alloc (unsigned counter, unsigned num)
404 if (no_coverage)
405 return 0;
407 if (!num)
408 return 1;
410 if (!fn_v_ctrs[counter])
412 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
414 fn_v_ctrs[counter]
415 = build_var (current_function_decl, array_type, counter);
418 fn_b_ctrs[counter] = fn_n_ctrs[counter];
419 fn_n_ctrs[counter] += num;
421 fn_ctr_mask |= 1 << counter;
422 return 1;
425 /* Generate a tree to access COUNTER NO. */
427 tree
428 tree_coverage_counter_ref (unsigned counter, unsigned no)
430 tree gcov_type_node = get_gcov_type ();
432 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
434 no += fn_b_ctrs[counter];
436 /* "no" here is an array index, scaled to bytes later. */
437 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
438 build_int_cst (integer_type_node, no), NULL, NULL);
441 /* Generate a tree to access the address of COUNTER NO. */
443 tree
444 tree_coverage_counter_addr (unsigned counter, unsigned no)
446 tree gcov_type_node = get_gcov_type ();
448 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
449 no += fn_b_ctrs[counter];
451 /* "no" here is an array index, scaled to bytes later. */
452 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
453 fn_v_ctrs[counter],
454 build_int_cst (integer_type_node, no),
455 NULL, NULL));
459 /* Generate a checksum for a string. CHKSUM is the current
460 checksum. */
462 static unsigned
463 coverage_checksum_string (unsigned chksum, const char *string)
465 int i;
466 char *dup = NULL;
468 /* Look for everything that looks if it were produced by
469 get_file_function_name and zero out the second part
470 that may result from flag_random_seed. This is not critical
471 as the checksums are used only for sanity checking. */
472 for (i = 0; string[i]; i++)
474 int offset = 0;
475 if (!strncmp (string + i, "_GLOBAL__N_", 11))
476 offset = 11;
477 if (!strncmp (string + i, "_GLOBAL__", 9))
478 offset = 9;
480 /* C++ namespaces do have scheme:
481 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
482 since filename might contain extra underscores there seems
483 to be no better chance then walk all possible offsets looking
484 for magicnumber. */
485 if (offset)
487 for (i = i + offset; string[i]; i++)
488 if (string[i]=='_')
490 int y;
492 for (y = 1; y < 9; y++)
493 if (!(string[i + y] >= '0' && string[i + y] <= '9')
494 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
495 break;
496 if (y != 9 || string[i + 9] != '_')
497 continue;
498 for (y = 10; y < 18; y++)
499 if (!(string[i + y] >= '0' && string[i + y] <= '9')
500 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
501 break;
502 if (y != 18)
503 continue;
504 if (!dup)
505 string = dup = xstrdup (string);
506 for (y = 10; y < 18; y++)
507 dup[i + y] = '0';
509 break;
513 chksum = crc32_string (chksum, string);
514 free (dup);
516 return chksum;
519 /* Compute checksum for the current function. We generate a CRC32. */
521 unsigned
522 coverage_compute_lineno_checksum (void)
524 expanded_location xloc
525 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
526 unsigned chksum = xloc.line;
528 chksum = coverage_checksum_string (chksum, xloc.file);
529 chksum = coverage_checksum_string
530 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
532 return chksum;
535 /* Compute cfg checksum for the current function.
536 The checksum is calculated carefully so that
537 source code changes that doesn't affect the control flow graph
538 won't change the checksum.
539 This is to make the profile data useable across source code change.
540 The downside of this is that the compiler may use potentially
541 wrong profile data - that the source code change has non-trivial impact
542 on the validity of profile data (e.g. the reversed condition)
543 but the compiler won't detect the change and use the wrong profile data. */
545 unsigned
546 coverage_compute_cfg_checksum (void)
548 basic_block bb;
549 unsigned chksum = n_basic_blocks;
551 FOR_EACH_BB (bb)
553 edge e;
554 edge_iterator ei;
555 chksum = crc32_byte (chksum, bb->index);
556 FOR_EACH_EDGE (e, ei, bb->succs)
558 chksum = crc32_byte (chksum, e->dest->index);
562 return chksum;
565 /* Begin output to the notes file for the current function.
566 Writes the function header. Returns nonzero if data should be output. */
569 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
571 expanded_location xloc;
572 unsigned long offset;
574 /* We don't need to output .gcno file unless we're under -ftest-coverage
575 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
576 if (no_coverage || !bbg_file_name)
577 return 0;
579 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
581 /* Announce function */
582 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
583 gcov_write_unsigned (current_function_funcdef_no + 1);
584 gcov_write_unsigned (lineno_checksum);
585 gcov_write_unsigned (cfg_checksum);
586 gcov_write_string (IDENTIFIER_POINTER
587 (DECL_ASSEMBLER_NAME (current_function_decl)));
588 gcov_write_string (xloc.file);
589 gcov_write_unsigned (xloc.line);
590 gcov_write_length (offset);
592 return !gcov_is_error ();
595 /* Finish coverage data for the current function. Verify no output
596 error has occurred. Save function coverage counts. */
598 void
599 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
601 unsigned i;
603 if (bbg_file_name && gcov_is_error ())
605 warning (0, "error writing %qs", bbg_file_name);
606 unlink (bbg_file_name);
607 bbg_file_name = NULL;
610 if (fn_ctr_mask)
612 struct coverage_data *item = 0;
614 /* If the function is extern (i.e. extern inline), then we won't
615 be outputting it, so don't chain it onto the function
616 list. */
617 if (!DECL_EXTERNAL (current_function_decl))
619 item = ggc_alloc_coverage_data ();
621 item->ident = current_function_funcdef_no + 1;
622 item->lineno_checksum = lineno_checksum;
623 item->cfg_checksum = cfg_checksum;
625 item->fn_decl = current_function_decl;
626 item->next = 0;
627 *functions_tail = item;
628 functions_tail = &item->next;
631 for (i = 0; i != GCOV_COUNTERS; i++)
633 tree var = fn_v_ctrs[i];
635 if (item)
636 item->ctr_vars[i] = var;
637 if (var)
639 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
640 array_type = build_array_type (get_gcov_type (), array_type);
641 TREE_TYPE (var) = array_type;
642 DECL_SIZE (var) = TYPE_SIZE (array_type);
643 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
644 varpool_finalize_decl (var);
647 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
648 fn_v_ctrs[i] = NULL_TREE;
650 prg_ctr_mask |= fn_ctr_mask;
651 fn_ctr_mask = 0;
655 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
656 >= 0 it is a counter array, otherwise it is the function structure. */
658 static tree
659 build_var (tree fn_decl, tree type, int counter)
661 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
662 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
663 char *buf;
664 size_t fn_name_len, len;
666 fn_name = targetm.strip_name_encoding (fn_name);
667 fn_name_len = strlen (fn_name);
668 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
670 if (counter < 0)
671 strcpy (buf, "__gcov__");
672 else
673 sprintf (buf, "__gcov%u_", counter);
674 len = strlen (buf);
675 #ifndef NO_DOT_IN_LABEL
676 buf[len - 1] = '.';
677 #elif !defined NO_DOLLAR_IN_LABEL
678 buf[len - 1] = '$';
679 #endif
680 memcpy (buf + len, fn_name, fn_name_len + 1);
681 DECL_NAME (var) = get_identifier (buf);
682 TREE_STATIC (var) = 1;
683 TREE_ADDRESSABLE (var) = 1;
684 DECL_ALIGN (var) = TYPE_ALIGN (type);
686 return var;
689 /* Creates the gcov_fn_info RECORD_TYPE. */
691 static void
692 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
694 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
695 tree field, fields;
696 tree array_type;
698 gcc_assert (counters);
700 /* ctr_info::num */
701 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
702 get_gcov_unsigned_t ());
703 fields = field;
705 /* ctr_info::values */
706 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
707 build_pointer_type (get_gcov_type ()));
708 DECL_CHAIN (field) = fields;
709 fields = field;
711 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
713 /* key */
714 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
715 build_pointer_type (build_qualified_type
716 (gcov_info_type, TYPE_QUAL_CONST)));
717 fields = field;
719 /* ident */
720 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
721 get_gcov_unsigned_t ());
722 DECL_CHAIN (field) = fields;
723 fields = field;
725 /* lineno_checksum */
726 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
727 get_gcov_unsigned_t ());
728 DECL_CHAIN (field) = fields;
729 fields = field;
731 /* cfg checksum */
732 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
733 get_gcov_unsigned_t ());
734 DECL_CHAIN (field) = fields;
735 fields = field;
737 array_type = build_index_type (size_int (counters - 1));
738 array_type = build_array_type (ctr_info, array_type);
740 /* counters */
741 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
742 DECL_CHAIN (field) = fields;
743 fields = field;
745 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
748 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
749 the coverage data for the function and TYPE is the gcov_fn_info
750 RECORD_TYPE. KEY is the object file key. */
752 static tree
753 build_fn_info (const struct coverage_data *data, tree type, tree key)
755 tree fields = TYPE_FIELDS (type);
756 tree ctr_type;
757 unsigned ix;
758 VEC(constructor_elt,gc) *v1 = NULL;
759 VEC(constructor_elt,gc) *v2 = NULL;
761 /* key */
762 CONSTRUCTOR_APPEND_ELT (v1, fields,
763 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
764 fields = DECL_CHAIN (fields);
766 /* ident */
767 CONSTRUCTOR_APPEND_ELT (v1, fields,
768 build_int_cstu (get_gcov_unsigned_t (),
769 data->ident));
770 fields = DECL_CHAIN (fields);
772 /* lineno_checksum */
773 CONSTRUCTOR_APPEND_ELT (v1, fields,
774 build_int_cstu (get_gcov_unsigned_t (),
775 data->lineno_checksum));
776 fields = DECL_CHAIN (fields);
778 /* cfg_checksum */
779 CONSTRUCTOR_APPEND_ELT (v1, fields,
780 build_int_cstu (get_gcov_unsigned_t (),
781 data->cfg_checksum));
782 fields = DECL_CHAIN (fields);
784 /* counters */
785 ctr_type = TREE_TYPE (TREE_TYPE (fields));
786 for (ix = 0; ix != GCOV_COUNTERS; ix++)
787 if (prg_ctr_mask & (1 << ix))
789 VEC(constructor_elt,gc) *ctr = NULL;
790 tree var = data->ctr_vars[ix];
791 unsigned count = 0;
793 if (var)
794 count
795 = tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))), 0)
796 + 1;
798 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
799 build_int_cstu (get_gcov_unsigned_t (),
800 count));
802 if (var)
803 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
804 build_fold_addr_expr (var));
806 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
809 CONSTRUCTOR_APPEND_ELT (v1, fields,
810 build_constructor (TREE_TYPE (fields), v2));
812 return build_constructor (type, v1);
815 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
816 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
818 static void
819 build_info_type (tree type, tree fn_info_ptr_type)
821 tree field, fields = NULL_TREE;
822 tree merge_fn_type;
824 /* Version ident */
825 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
826 get_gcov_unsigned_t ());
827 DECL_CHAIN (field) = fields;
828 fields = field;
830 /* next pointer */
831 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
832 build_pointer_type (build_qualified_type
833 (type, TYPE_QUAL_CONST)));
834 DECL_CHAIN (field) = fields;
835 fields = field;
837 /* stamp */
838 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
839 get_gcov_unsigned_t ());
840 DECL_CHAIN (field) = fields;
841 fields = field;
843 /* Filename */
844 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
845 build_pointer_type (build_qualified_type
846 (char_type_node, TYPE_QUAL_CONST)));
847 DECL_CHAIN (field) = fields;
848 fields = field;
850 /* merge fn array */
851 merge_fn_type
852 = build_function_type_list (void_type_node,
853 build_pointer_type (get_gcov_type ()),
854 get_gcov_unsigned_t (), NULL_TREE);
855 merge_fn_type
856 = build_array_type (build_pointer_type (merge_fn_type),
857 build_index_type (size_int (GCOV_COUNTERS - 1)));
858 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
859 merge_fn_type);
860 DECL_CHAIN (field) = fields;
861 fields = field;
863 /* n_functions */
864 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
865 get_gcov_unsigned_t ());
866 DECL_CHAIN (field) = fields;
867 fields = field;
869 /* function_info pointer pointer */
870 fn_info_ptr_type = build_pointer_type
871 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
872 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
873 fn_info_ptr_type);
874 DECL_CHAIN (field) = fields;
875 fields = field;
877 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
880 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
881 gcov_info structure type, FN_ARY is the array of pointers to
882 function info objects. */
884 static tree
885 build_info (tree info_type, tree fn_ary)
887 tree info_fields = TYPE_FIELDS (info_type);
888 tree merge_fn_type, n_funcs;
889 unsigned ix;
890 tree filename_string;
891 int da_file_name_len;
892 VEC(constructor_elt,gc) *v1 = NULL;
893 VEC(constructor_elt,gc) *v2 = NULL;
895 /* Version ident */
896 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
897 build_int_cstu (TREE_TYPE (info_fields),
898 GCOV_VERSION));
899 info_fields = DECL_CHAIN (info_fields);
901 /* next -- NULL */
902 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
903 info_fields = DECL_CHAIN (info_fields);
905 /* stamp */
906 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
907 build_int_cstu (TREE_TYPE (info_fields),
908 bbg_file_stamp));
909 info_fields = DECL_CHAIN (info_fields);
911 /* Filename */
912 da_file_name_len = strlen (da_file_name);
913 filename_string = build_string (da_file_name_len + 1, da_file_name);
914 TREE_TYPE (filename_string) = build_array_type
915 (char_type_node, build_index_type (size_int (da_file_name_len)));
916 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
917 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
918 filename_string));
919 info_fields = DECL_CHAIN (info_fields);
921 /* merge fn array -- NULL slots indicate unmeasured counters */
922 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
923 for (ix = 0; ix != GCOV_COUNTERS; ix++)
925 tree ptr = null_pointer_node;
927 if ((1u << ix) & prg_ctr_mask)
929 tree merge_fn = build_decl (BUILTINS_LOCATION,
930 FUNCTION_DECL,
931 get_identifier (ctr_merge_functions[ix]),
932 TREE_TYPE (merge_fn_type));
933 DECL_EXTERNAL (merge_fn) = 1;
934 TREE_PUBLIC (merge_fn) = 1;
935 DECL_ARTIFICIAL (merge_fn) = 1;
936 TREE_NOTHROW (merge_fn) = 1;
937 /* Initialize assembler name so we can stream out. */
938 DECL_ASSEMBLER_NAME (merge_fn);
939 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
941 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
943 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
944 build_constructor (TREE_TYPE (info_fields), v2));
945 info_fields = DECL_CHAIN (info_fields);
947 /* n_functions */
948 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
949 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
950 n_funcs, size_one_node);
951 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
952 info_fields = DECL_CHAIN (info_fields);
954 /* functions */
955 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
956 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
957 info_fields = DECL_CHAIN (info_fields);
959 gcc_assert (!info_fields);
960 return build_constructor (info_type, v1);
963 /* Create the gcov_info types and object. Generate the constructor
964 function to call __gcov_init. Does not generate the initializer
965 for the object. Returns TRUE if coverage data is being emitted. */
967 static bool
968 coverage_obj_init (void)
970 tree gcov_info_type, ctor, stmt, init_fn;
971 unsigned n_counters = 0;
972 unsigned ix;
973 struct coverage_data *fn;
974 struct coverage_data **fn_prev;
975 char name_buf[32];
977 no_coverage = 1; /* Disable any further coverage. */
979 if (!prg_ctr_mask)
980 return false;
982 if (cgraph_dump_file)
983 fprintf (cgraph_dump_file, "Using data file %s\n", da_file_name);
985 /* Prune functions. */
986 for (fn_prev = &functions_head; (fn = *fn_prev);)
987 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
988 fn_prev = &fn->next;
989 else
990 /* The function is not being emitted, remove from list. */
991 *fn_prev = fn->next;
993 for (ix = 0; ix != GCOV_COUNTERS; ix++)
994 if ((1u << ix) & prg_ctr_mask)
995 n_counters++;
997 /* Build the info and fn_info types. These are mutually recursive. */
998 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
999 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1000 gcov_fn_info_ptr_type = build_pointer_type
1001 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1002 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1003 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1005 /* Build the gcov info var, this is referred to in its own
1006 initializer. */
1007 gcov_info_var = build_decl (BUILTINS_LOCATION,
1008 VAR_DECL, NULL_TREE, gcov_info_type);
1009 TREE_STATIC (gcov_info_var) = 1;
1010 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1011 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1013 /* Build a decl for __gcov_init. */
1014 init_fn = build_pointer_type (gcov_info_type);
1015 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
1016 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
1017 get_identifier ("__gcov_init"), init_fn);
1018 TREE_PUBLIC (init_fn) = 1;
1019 DECL_EXTERNAL (init_fn) = 1;
1020 DECL_ASSEMBLER_NAME (init_fn);
1022 /* Generate a call to __gcov_init(&gcov_info). */
1023 ctor = NULL;
1024 stmt = build_fold_addr_expr (gcov_info_var);
1025 stmt = build_call_expr (init_fn, 1, stmt);
1026 append_to_statement_list (stmt, &ctor);
1028 /* Generate a constructor to run it. */
1029 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
1031 return true;
1034 /* Generate the coverage function info for FN and DATA. Append a
1035 pointer to that object to CTOR and return the appended CTOR. */
1037 static VEC(constructor_elt,gc) *
1038 coverage_obj_fn (VEC(constructor_elt,gc) *ctor, tree fn,
1039 struct coverage_data const *data)
1041 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1042 tree var = build_var (fn, gcov_fn_info_type, -1);
1044 DECL_INITIAL (var) = init;
1045 varpool_finalize_decl (var);
1047 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1048 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1049 return ctor;
1052 /* Finalize the coverage data. Generates the array of pointers to
1053 function objects from CTOR. Generate the gcov_info initializer. */
1055 static void
1056 coverage_obj_finish (VEC(constructor_elt,gc) *ctor)
1058 unsigned n_functions = VEC_length(constructor_elt, ctor);
1059 tree fn_info_ary_type = build_array_type
1060 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1061 build_index_type (size_int (n_functions - 1)));
1062 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1063 fn_info_ary_type);
1064 char name_buf[32];
1066 TREE_STATIC (fn_info_ary) = 1;
1067 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1068 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1069 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1070 varpool_finalize_decl (fn_info_ary);
1072 DECL_INITIAL (gcov_info_var)
1073 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1074 varpool_finalize_decl (gcov_info_var);
1077 /* Perform file-level initialization. Read in data file, generate name
1078 of notes file. */
1080 void
1081 coverage_init (const char *filename)
1083 int len = strlen (filename);
1084 int prefix_len = 0;
1086 if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
1087 profile_data_prefix = getpwd ();
1089 if (profile_data_prefix)
1090 prefix_len = strlen (profile_data_prefix);
1092 /* Name of da file. */
1093 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1094 + prefix_len + 2);
1096 if (profile_data_prefix)
1098 memcpy (da_file_name, profile_data_prefix, prefix_len);
1099 da_file_name[prefix_len++] = '/';
1101 memcpy (da_file_name + prefix_len, filename, len);
1102 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1104 bbg_file_stamp = local_tick;
1106 if (flag_branch_probabilities)
1107 read_counts_file ();
1109 /* Name of bbg file. */
1110 if (flag_test_coverage && !flag_compare_debug)
1112 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1113 memcpy (bbg_file_name, filename, len);
1114 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
1116 if (!gcov_open (bbg_file_name, -1))
1118 error ("cannot open %s", bbg_file_name);
1119 bbg_file_name = NULL;
1121 else
1123 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1124 gcov_write_unsigned (GCOV_VERSION);
1125 gcov_write_unsigned (bbg_file_stamp);
1130 /* Performs file-level cleanup. Close notes file, generate coverage
1131 variables and constructor. */
1133 void
1134 coverage_finish (void)
1136 if (bbg_file_name && gcov_close ())
1137 unlink (bbg_file_name);
1139 if (!flag_branch_probabilities && flag_test_coverage
1140 && (!local_tick || local_tick == (unsigned)-1))
1141 /* Only remove the da file, if we're emitting coverage code and
1142 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1143 unlink (da_file_name);
1145 if (coverage_obj_init ())
1147 VEC(constructor_elt,gc) *fn_ctor = NULL;
1148 struct coverage_data *fn;
1150 for (fn = functions_head; fn; fn = fn->next)
1151 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1152 coverage_obj_finish (fn_ctor);
1156 #include "gt-coverage.h"