Daily bump.
[official-gcc.git] / gcc / coverage.c
blob7c395f4750b17e81740e80043c391552c0d00e56
1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2013 Free Software Foundation, Inc.
3 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
4 based on some ideas from Dain Samples of UC Berkeley.
5 Further mangling by Bob Manson, Cygnus Support.
6 Further mangled by Nathan Sidwell, CodeSourcery
8 This file is part of GCC.
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.
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.
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/>. */
25 #define GCOV_LINKAGE
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "rtl.h"
32 #include "tree.h"
33 #include "flags.h"
34 #include "output.h"
35 #include "regs.h"
36 #include "expr.h"
37 #include "function.h"
38 #include "basic-block.h"
39 #include "toplev.h"
40 #include "tm_p.h"
41 #include "ggc.h"
42 #include "coverage.h"
43 #include "langhooks.h"
44 #include "hash-table.h"
45 #include "tree-iterator.h"
46 #include "cgraph.h"
47 #include "dumpfile.h"
48 #include "diagnostic-core.h"
49 #include "intl.h"
50 #include "filenames.h"
51 #include "target.h"
53 #include "gcov-io.h"
54 #include "gcov-io.c"
56 struct GTY((chain_next ("%h.next"))) coverage_data
58 struct coverage_data *next; /* next function */
59 unsigned ident; /* function ident */
60 unsigned lineno_checksum; /* function lineno checksum */
61 unsigned cfg_checksum; /* function cfg checksum */
62 tree fn_decl; /* the function decl */
63 tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
66 /* Counts information for a function. */
67 typedef struct counts_entry
69 /* We hash by */
70 unsigned ident;
71 unsigned ctr;
73 /* Store */
74 unsigned lineno_checksum;
75 unsigned cfg_checksum;
76 gcov_type *counts;
77 struct gcov_ctr_summary summary;
79 /* hash_table support. */
80 typedef counts_entry value_type;
81 typedef counts_entry compare_type;
82 static inline hashval_t hash (const value_type *);
83 static int equal (const value_type *, const compare_type *);
84 static void remove (value_type *);
85 } counts_entry_t;
87 static GTY(()) struct coverage_data *functions_head = 0;
88 static struct coverage_data **functions_tail = &functions_head;
89 static unsigned no_coverage = 0;
91 /* Cumulative counter information for whole program. */
92 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
94 /* Counter information for current function. */
95 static unsigned fn_ctr_mask; /* Mask of counters used. */
96 static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
97 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
98 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
100 /* Coverage info VAR_DECL and function info type nodes. */
101 static GTY(()) tree gcov_info_var;
102 static GTY(()) tree gcov_fn_info_type;
103 static GTY(()) tree gcov_fn_info_ptr_type;
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. */
109 static char *bbg_file_name;
111 /* File stamp for notes file. */
112 static unsigned bbg_file_stamp;
114 /* Name of the count data (gcda) file. */
115 static char *da_file_name;
117 /* The names of merge functions for counters. */
118 static const char *const ctr_merge_functions[GCOV_COUNTERS] = GCOV_MERGE_FUNCTIONS;
119 static const char *const ctr_names[GCOV_COUNTERS] = GCOV_COUNTER_NAMES;
121 /* Forward declarations. */
122 static void read_counts_file (void);
123 static tree build_var (tree, tree, int);
124 static void build_fn_info_type (tree, unsigned, tree);
125 static void build_info_type (tree, tree);
126 static tree build_fn_info (const struct coverage_data *, tree, tree);
127 static tree build_info (tree, tree);
128 static bool coverage_obj_init (void);
129 static vec<constructor_elt, va_gc> *coverage_obj_fn
130 (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
131 static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
133 /* Return the type node for gcov_type. */
135 tree
136 get_gcov_type (void)
138 enum machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
139 return lang_hooks.types.type_for_mode (mode, false);
142 /* Return the type node for gcov_unsigned_t. */
144 static tree
145 get_gcov_unsigned_t (void)
147 enum machine_mode mode = smallest_mode_for_size (32, MODE_INT);
148 return lang_hooks.types.type_for_mode (mode, true);
151 inline hashval_t
152 counts_entry::hash (const value_type *entry)
154 return entry->ident * GCOV_COUNTERS + entry->ctr;
157 inline int
158 counts_entry::equal (const value_type *entry1,
159 const compare_type *entry2)
161 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
164 inline void
165 counts_entry::remove (value_type *entry)
167 free (entry->counts);
168 free (entry);
171 /* Hash table of count data. */
172 static hash_table <counts_entry> counts_hash;
174 /* Read in the counts file, if available. */
176 static void
177 read_counts_file (void)
179 gcov_unsigned_t fn_ident = 0;
180 struct gcov_summary summary;
181 unsigned new_summary = 1;
182 gcov_unsigned_t tag;
183 int is_error = 0;
184 unsigned lineno_checksum = 0;
185 unsigned cfg_checksum = 0;
187 if (!gcov_open (da_file_name, 1))
188 return;
190 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
192 warning (0, "%qs is not a gcov data file", da_file_name);
193 gcov_close ();
194 return;
196 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
198 char v[4], e[4];
200 GCOV_UNSIGNED2STRING (v, tag);
201 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
203 warning (0, "%qs is version %q.*s, expected version %q.*s",
204 da_file_name, 4, v, 4, e);
205 gcov_close ();
206 return;
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);
213 counts_hash.create (10);
214 while ((tag = gcov_read_unsigned ()))
216 gcov_unsigned_t length;
217 gcov_position_t offset;
219 length = gcov_read_unsigned ();
220 offset = gcov_position ();
221 if (tag == GCOV_TAG_FUNCTION)
223 if (length)
225 fn_ident = gcov_read_unsigned ();
226 lineno_checksum = gcov_read_unsigned ();
227 cfg_checksum = gcov_read_unsigned ();
229 else
230 fn_ident = lineno_checksum = cfg_checksum = 0;
231 new_summary = 1;
233 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
235 struct gcov_summary sum;
236 unsigned ix;
238 if (new_summary)
239 memset (&summary, 0, sizeof (summary));
241 gcov_read_summary (&sum);
242 for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
244 summary.ctrs[ix].runs += sum.ctrs[ix].runs;
245 summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
246 if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
247 summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
248 summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
250 if (new_summary)
251 memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
252 sum.ctrs[GCOV_COUNTER_ARCS].histogram,
253 sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
254 else
255 gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
256 sum.ctrs[GCOV_COUNTER_ARCS].histogram);
257 new_summary = 0;
259 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
261 counts_entry_t **slot, *entry, elt;
262 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
263 unsigned ix;
265 elt.ident = fn_ident;
266 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
268 slot = counts_hash.find_slot (&elt, INSERT);
269 entry = *slot;
270 if (!entry)
272 *slot = entry = XCNEW (counts_entry_t);
273 entry->ident = fn_ident;
274 entry->ctr = elt.ctr;
275 entry->lineno_checksum = lineno_checksum;
276 entry->cfg_checksum = cfg_checksum;
277 if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
278 entry->summary = summary.ctrs[elt.ctr];
279 entry->summary.num = n_counts;
280 entry->counts = XCNEWVEC (gcov_type, n_counts);
282 else if (entry->lineno_checksum != lineno_checksum
283 || entry->cfg_checksum != cfg_checksum)
285 error ("Profile data for function %u is corrupted", fn_ident);
286 error ("checksum is (%x,%x) instead of (%x,%x)",
287 entry->lineno_checksum, entry->cfg_checksum,
288 lineno_checksum, cfg_checksum);
289 counts_hash.dispose ();
290 break;
292 else if (entry->summary.num != n_counts)
294 error ("Profile data for function %u is corrupted", fn_ident);
295 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
296 counts_hash.dispose ();
297 break;
299 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
301 error ("cannot merge separate %s counters for function %u",
302 ctr_names[elt.ctr], fn_ident);
303 goto skip_merge;
305 else
307 entry->summary.runs += summary.ctrs[elt.ctr].runs;
308 entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
309 if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
310 entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
311 entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
313 for (ix = 0; ix != n_counts; ix++)
314 entry->counts[ix] += gcov_read_counter ();
315 skip_merge:;
317 gcov_sync (offset, length);
318 if ((is_error = gcov_is_error ()))
320 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
321 da_file_name);
322 counts_hash.dispose ();
323 break;
327 gcov_close ();
330 /* Returns the counters for a particular tag. */
332 gcov_type *
333 get_coverage_counts (unsigned counter, unsigned expected,
334 unsigned cfg_checksum, unsigned lineno_checksum,
335 const struct gcov_ctr_summary **summary)
337 counts_entry_t *entry, elt;
339 /* No hash table, no counts. */
340 if (!counts_hash.is_created ())
342 static int warned = 0;
344 if (!warned++)
345 inform (input_location, (flag_guess_branch_prob
346 ? "file %s not found, execution counts estimated"
347 : "file %s not found, execution counts assumed to be zero"),
348 da_file_name);
349 return NULL;
352 elt.ident = current_function_funcdef_no + 1;
353 elt.ctr = counter;
354 entry = counts_hash.find (&elt);
355 if (!entry || !entry->summary.num)
356 /* The function was not emitted, or is weak and not chosen in the
357 final executable. Silently fail, because there's nothing we
358 can do about it. */
359 return NULL;
361 if (entry->cfg_checksum != cfg_checksum
362 || entry->summary.num != expected)
364 static int warned = 0;
365 bool warning_printed = false;
366 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
368 warning_printed =
369 warning_at (input_location, OPT_Wcoverage_mismatch,
370 "the control flow of function %qE does not match "
371 "its profile data (counter %qs)", id, ctr_names[counter]);
372 if (warning_printed)
374 inform (input_location, "use -Wno-error=coverage-mismatch to tolerate "
375 "the mismatch but performance may drop if the function is hot");
377 if (!seen_error ()
378 && !warned++)
380 inform (input_location, "coverage mismatch ignored");
381 inform (input_location, flag_guess_branch_prob
382 ? G_("execution counts estimated")
383 : G_("execution counts assumed to be zero"));
384 if (!flag_guess_branch_prob)
385 inform (input_location,
386 "this can result in poorly optimized code");
390 return NULL;
392 else if (entry->lineno_checksum != lineno_checksum)
394 warning (0, "source locations for function %qE have changed,"
395 " the profile data may be out of date",
396 DECL_ASSEMBLER_NAME (current_function_decl));
399 if (summary)
400 *summary = &entry->summary;
402 return entry->counts;
405 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
406 allocation succeeded. */
409 coverage_counter_alloc (unsigned counter, unsigned num)
411 if (no_coverage)
412 return 0;
414 if (!num)
415 return 1;
417 if (!fn_v_ctrs[counter])
419 tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
421 fn_v_ctrs[counter]
422 = build_var (current_function_decl, array_type, counter);
425 fn_b_ctrs[counter] = fn_n_ctrs[counter];
426 fn_n_ctrs[counter] += num;
428 fn_ctr_mask |= 1 << counter;
429 return 1;
432 /* Generate a tree to access COUNTER NO. */
434 tree
435 tree_coverage_counter_ref (unsigned counter, unsigned no)
437 tree gcov_type_node = get_gcov_type ();
439 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
441 no += fn_b_ctrs[counter];
443 /* "no" here is an array index, scaled to bytes later. */
444 return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
445 build_int_cst (integer_type_node, no), NULL, NULL);
448 /* Generate a tree to access the address of COUNTER NO. */
450 tree
451 tree_coverage_counter_addr (unsigned counter, unsigned no)
453 tree gcov_type_node = get_gcov_type ();
455 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
456 no += fn_b_ctrs[counter];
458 /* "no" here is an array index, scaled to bytes later. */
459 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
460 fn_v_ctrs[counter],
461 build_int_cst (integer_type_node, no),
462 NULL, NULL));
466 /* Generate a checksum for a string. CHKSUM is the current
467 checksum. */
469 static unsigned
470 coverage_checksum_string (unsigned chksum, const char *string)
472 int i;
473 char *dup = NULL;
475 /* Look for everything that looks if it were produced by
476 get_file_function_name and zero out the second part
477 that may result from flag_random_seed. This is not critical
478 as the checksums are used only for sanity checking. */
479 for (i = 0; string[i]; i++)
481 int offset = 0;
482 if (!strncmp (string + i, "_GLOBAL__N_", 11))
483 offset = 11;
484 if (!strncmp (string + i, "_GLOBAL__", 9))
485 offset = 9;
487 /* C++ namespaces do have scheme:
488 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
489 since filename might contain extra underscores there seems
490 to be no better chance then walk all possible offsets looking
491 for magicnumber. */
492 if (offset)
494 for (i = i + offset; string[i]; i++)
495 if (string[i]=='_')
497 int y;
499 for (y = 1; y < 9; y++)
500 if (!(string[i + y] >= '0' && string[i + y] <= '9')
501 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
502 break;
503 if (y != 9 || string[i + 9] != '_')
504 continue;
505 for (y = 10; y < 18; y++)
506 if (!(string[i + y] >= '0' && string[i + y] <= '9')
507 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
508 break;
509 if (y != 18)
510 continue;
511 if (!dup)
512 string = dup = xstrdup (string);
513 for (y = 10; y < 18; y++)
514 dup[i + y] = '0';
516 break;
520 chksum = crc32_string (chksum, string);
521 free (dup);
523 return chksum;
526 /* Compute checksum for the current function. We generate a CRC32. */
528 unsigned
529 coverage_compute_lineno_checksum (void)
531 expanded_location xloc
532 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
533 unsigned chksum = xloc.line;
535 chksum = coverage_checksum_string (chksum, xloc.file);
536 chksum = coverage_checksum_string
537 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
539 return chksum;
542 /* Compute cfg checksum for the current function.
543 The checksum is calculated carefully so that
544 source code changes that doesn't affect the control flow graph
545 won't change the checksum.
546 This is to make the profile data useable across source code change.
547 The downside of this is that the compiler may use potentially
548 wrong profile data - that the source code change has non-trivial impact
549 on the validity of profile data (e.g. the reversed condition)
550 but the compiler won't detect the change and use the wrong profile data. */
552 unsigned
553 coverage_compute_cfg_checksum (void)
555 basic_block bb;
556 unsigned chksum = n_basic_blocks;
558 FOR_EACH_BB (bb)
560 edge e;
561 edge_iterator ei;
562 chksum = crc32_byte (chksum, bb->index);
563 FOR_EACH_EDGE (e, ei, bb->succs)
565 chksum = crc32_byte (chksum, e->dest->index);
569 return chksum;
572 /* Begin output to the notes file for the current function.
573 Writes the function header. Returns nonzero if data should be output. */
576 coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
578 expanded_location xloc;
579 unsigned long offset;
581 /* We don't need to output .gcno file unless we're under -ftest-coverage
582 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
583 if (no_coverage || !bbg_file_name)
584 return 0;
586 xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
588 /* Announce function */
589 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
590 gcov_write_unsigned (current_function_funcdef_no + 1);
591 gcov_write_unsigned (lineno_checksum);
592 gcov_write_unsigned (cfg_checksum);
593 gcov_write_string (IDENTIFIER_POINTER
594 (DECL_ASSEMBLER_NAME (current_function_decl)));
595 gcov_write_string (xloc.file);
596 gcov_write_unsigned (xloc.line);
597 gcov_write_length (offset);
599 return !gcov_is_error ();
602 /* Finish coverage data for the current function. Verify no output
603 error has occurred. Save function coverage counts. */
605 void
606 coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
608 unsigned i;
610 if (bbg_file_name && gcov_is_error ())
612 warning (0, "error writing %qs", bbg_file_name);
613 unlink (bbg_file_name);
614 bbg_file_name = NULL;
617 if (fn_ctr_mask)
619 struct coverage_data *item = 0;
621 /* If the function is extern (i.e. extern inline), then we won't
622 be outputting it, so don't chain it onto the function
623 list. */
624 if (!DECL_EXTERNAL (current_function_decl))
626 item = ggc_alloc_coverage_data ();
628 item->ident = current_function_funcdef_no + 1;
629 item->lineno_checksum = lineno_checksum;
630 item->cfg_checksum = cfg_checksum;
632 item->fn_decl = current_function_decl;
633 item->next = 0;
634 *functions_tail = item;
635 functions_tail = &item->next;
638 for (i = 0; i != GCOV_COUNTERS; i++)
640 tree var = fn_v_ctrs[i];
642 if (item)
643 item->ctr_vars[i] = var;
644 if (var)
646 tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
647 array_type = build_array_type (get_gcov_type (), array_type);
648 TREE_TYPE (var) = array_type;
649 DECL_SIZE (var) = TYPE_SIZE (array_type);
650 DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
651 varpool_finalize_decl (var);
654 fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
655 fn_v_ctrs[i] = NULL_TREE;
657 prg_ctr_mask |= fn_ctr_mask;
658 fn_ctr_mask = 0;
662 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
663 >= 0 it is a counter array, otherwise it is the function structure. */
665 static tree
666 build_var (tree fn_decl, tree type, int counter)
668 tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
669 const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
670 char *buf;
671 size_t fn_name_len, len;
673 fn_name = targetm.strip_name_encoding (fn_name);
674 fn_name_len = strlen (fn_name);
675 buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
677 if (counter < 0)
678 strcpy (buf, "__gcov__");
679 else
680 sprintf (buf, "__gcov%u_", counter);
681 len = strlen (buf);
682 #ifndef NO_DOT_IN_LABEL
683 buf[len - 1] = '.';
684 #elif !defined NO_DOLLAR_IN_LABEL
685 buf[len - 1] = '$';
686 #endif
687 memcpy (buf + len, fn_name, fn_name_len + 1);
688 DECL_NAME (var) = get_identifier (buf);
689 TREE_STATIC (var) = 1;
690 TREE_ADDRESSABLE (var) = 1;
691 DECL_ALIGN (var) = TYPE_ALIGN (type);
693 return var;
696 /* Creates the gcov_fn_info RECORD_TYPE. */
698 static void
699 build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
701 tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
702 tree field, fields;
703 tree array_type;
705 gcc_assert (counters);
707 /* ctr_info::num */
708 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
709 get_gcov_unsigned_t ());
710 fields = field;
712 /* ctr_info::values */
713 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
714 build_pointer_type (get_gcov_type ()));
715 DECL_CHAIN (field) = fields;
716 fields = field;
718 finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
720 /* key */
721 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
722 build_pointer_type (build_qualified_type
723 (gcov_info_type, TYPE_QUAL_CONST)));
724 fields = field;
726 /* ident */
727 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
728 get_gcov_unsigned_t ());
729 DECL_CHAIN (field) = fields;
730 fields = field;
732 /* lineno_checksum */
733 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
734 get_gcov_unsigned_t ());
735 DECL_CHAIN (field) = fields;
736 fields = field;
738 /* cfg checksum */
739 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
740 get_gcov_unsigned_t ());
741 DECL_CHAIN (field) = fields;
742 fields = field;
744 array_type = build_index_type (size_int (counters - 1));
745 array_type = build_array_type (ctr_info, array_type);
747 /* counters */
748 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
749 DECL_CHAIN (field) = fields;
750 fields = field;
752 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
755 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
756 the coverage data for the function and TYPE is the gcov_fn_info
757 RECORD_TYPE. KEY is the object file key. */
759 static tree
760 build_fn_info (const struct coverage_data *data, tree type, tree key)
762 tree fields = TYPE_FIELDS (type);
763 tree ctr_type;
764 unsigned ix;
765 vec<constructor_elt, va_gc> *v1 = NULL;
766 vec<constructor_elt, va_gc> *v2 = NULL;
768 /* key */
769 CONSTRUCTOR_APPEND_ELT (v1, fields,
770 build1 (ADDR_EXPR, TREE_TYPE (fields), key));
771 fields = DECL_CHAIN (fields);
773 /* ident */
774 CONSTRUCTOR_APPEND_ELT (v1, fields,
775 build_int_cstu (get_gcov_unsigned_t (),
776 data->ident));
777 fields = DECL_CHAIN (fields);
779 /* lineno_checksum */
780 CONSTRUCTOR_APPEND_ELT (v1, fields,
781 build_int_cstu (get_gcov_unsigned_t (),
782 data->lineno_checksum));
783 fields = DECL_CHAIN (fields);
785 /* cfg_checksum */
786 CONSTRUCTOR_APPEND_ELT (v1, fields,
787 build_int_cstu (get_gcov_unsigned_t (),
788 data->cfg_checksum));
789 fields = DECL_CHAIN (fields);
791 /* counters */
792 ctr_type = TREE_TYPE (TREE_TYPE (fields));
793 for (ix = 0; ix != GCOV_COUNTERS; ix++)
794 if (prg_ctr_mask & (1 << ix))
796 vec<constructor_elt, va_gc> *ctr = NULL;
797 tree var = data->ctr_vars[ix];
798 unsigned count = 0;
800 if (var)
801 count
802 = tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))), 0)
803 + 1;
805 CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
806 build_int_cstu (get_gcov_unsigned_t (),
807 count));
809 if (var)
810 CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
811 build_fold_addr_expr (var));
813 CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
816 CONSTRUCTOR_APPEND_ELT (v1, fields,
817 build_constructor (TREE_TYPE (fields), v2));
819 return build_constructor (type, v1);
822 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
823 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
825 static void
826 build_info_type (tree type, tree fn_info_ptr_type)
828 tree field, fields = NULL_TREE;
829 tree merge_fn_type;
831 /* Version ident */
832 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
833 get_gcov_unsigned_t ());
834 DECL_CHAIN (field) = fields;
835 fields = field;
837 /* next pointer */
838 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
839 build_pointer_type (build_qualified_type
840 (type, TYPE_QUAL_CONST)));
841 DECL_CHAIN (field) = fields;
842 fields = field;
844 /* stamp */
845 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
846 get_gcov_unsigned_t ());
847 DECL_CHAIN (field) = fields;
848 fields = field;
850 /* Filename */
851 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
852 build_pointer_type (build_qualified_type
853 (char_type_node, TYPE_QUAL_CONST)));
854 DECL_CHAIN (field) = fields;
855 fields = field;
857 /* merge fn array */
858 merge_fn_type
859 = build_function_type_list (void_type_node,
860 build_pointer_type (get_gcov_type ()),
861 get_gcov_unsigned_t (), NULL_TREE);
862 merge_fn_type
863 = build_array_type (build_pointer_type (merge_fn_type),
864 build_index_type (size_int (GCOV_COUNTERS - 1)));
865 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
866 merge_fn_type);
867 DECL_CHAIN (field) = fields;
868 fields = field;
870 /* n_functions */
871 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
872 get_gcov_unsigned_t ());
873 DECL_CHAIN (field) = fields;
874 fields = field;
876 /* function_info pointer pointer */
877 fn_info_ptr_type = build_pointer_type
878 (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
879 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
880 fn_info_ptr_type);
881 DECL_CHAIN (field) = fields;
882 fields = field;
884 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
887 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
888 gcov_info structure type, FN_ARY is the array of pointers to
889 function info objects. */
891 static tree
892 build_info (tree info_type, tree fn_ary)
894 tree info_fields = TYPE_FIELDS (info_type);
895 tree merge_fn_type, n_funcs;
896 unsigned ix;
897 tree filename_string;
898 int da_file_name_len;
899 vec<constructor_elt, va_gc> *v1 = NULL;
900 vec<constructor_elt, va_gc> *v2 = NULL;
902 /* Version ident */
903 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
904 build_int_cstu (TREE_TYPE (info_fields),
905 GCOV_VERSION));
906 info_fields = DECL_CHAIN (info_fields);
908 /* next -- NULL */
909 CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
910 info_fields = DECL_CHAIN (info_fields);
912 /* stamp */
913 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
914 build_int_cstu (TREE_TYPE (info_fields),
915 bbg_file_stamp));
916 info_fields = DECL_CHAIN (info_fields);
918 /* Filename */
919 da_file_name_len = strlen (da_file_name);
920 filename_string = build_string (da_file_name_len + 1, da_file_name);
921 TREE_TYPE (filename_string) = build_array_type
922 (char_type_node, build_index_type (size_int (da_file_name_len)));
923 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
924 build1 (ADDR_EXPR, TREE_TYPE (info_fields),
925 filename_string));
926 info_fields = DECL_CHAIN (info_fields);
928 /* merge fn array -- NULL slots indicate unmeasured counters */
929 merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
930 for (ix = 0; ix != GCOV_COUNTERS; ix++)
932 tree ptr = null_pointer_node;
934 if ((1u << ix) & prg_ctr_mask)
936 tree merge_fn = build_decl (BUILTINS_LOCATION,
937 FUNCTION_DECL,
938 get_identifier (ctr_merge_functions[ix]),
939 TREE_TYPE (merge_fn_type));
940 DECL_EXTERNAL (merge_fn) = 1;
941 TREE_PUBLIC (merge_fn) = 1;
942 DECL_ARTIFICIAL (merge_fn) = 1;
943 TREE_NOTHROW (merge_fn) = 1;
944 /* Initialize assembler name so we can stream out. */
945 DECL_ASSEMBLER_NAME (merge_fn);
946 ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
948 CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
950 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
951 build_constructor (TREE_TYPE (info_fields), v2));
952 info_fields = DECL_CHAIN (info_fields);
954 /* n_functions */
955 n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
956 n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
957 n_funcs, size_one_node);
958 CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
959 info_fields = DECL_CHAIN (info_fields);
961 /* functions */
962 CONSTRUCTOR_APPEND_ELT (v1, info_fields,
963 build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
964 info_fields = DECL_CHAIN (info_fields);
966 gcc_assert (!info_fields);
967 return build_constructor (info_type, v1);
970 /* Generate the constructor function to call __gcov_init. */
972 static void
973 build_init_ctor (tree gcov_info_type)
975 tree ctor, stmt, init_fn;
977 /* Build a decl for __gcov_init. */
978 init_fn = build_pointer_type (gcov_info_type);
979 init_fn = build_function_type_list (void_type_node, init_fn, NULL);
980 init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
981 get_identifier ("__gcov_init"), init_fn);
982 TREE_PUBLIC (init_fn) = 1;
983 DECL_EXTERNAL (init_fn) = 1;
984 DECL_ASSEMBLER_NAME (init_fn);
986 /* Generate a call to __gcov_init(&gcov_info). */
987 ctor = NULL;
988 stmt = build_fold_addr_expr (gcov_info_var);
989 stmt = build_call_expr (init_fn, 1, stmt);
990 append_to_statement_list (stmt, &ctor);
992 /* Generate a constructor to run it. */
993 cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
996 /* Create the gcov_info types and object. Generate the constructor
997 function to call __gcov_init. Does not generate the initializer
998 for the object. Returns TRUE if coverage data is being emitted. */
1000 static bool
1001 coverage_obj_init (void)
1003 tree gcov_info_type;
1004 unsigned n_counters = 0;
1005 unsigned ix;
1006 struct coverage_data *fn;
1007 struct coverage_data **fn_prev;
1008 char name_buf[32];
1010 no_coverage = 1; /* Disable any further coverage. */
1012 if (!prg_ctr_mask)
1013 return false;
1015 if (cgraph_dump_file)
1016 fprintf (cgraph_dump_file, "Using data file %s\n", da_file_name);
1018 /* Prune functions. */
1019 for (fn_prev = &functions_head; (fn = *fn_prev);)
1020 if (DECL_STRUCT_FUNCTION (fn->fn_decl))
1021 fn_prev = &fn->next;
1022 else
1023 /* The function is not being emitted, remove from list. */
1024 *fn_prev = fn->next;
1026 if (functions_head == NULL)
1027 return false;
1029 for (ix = 0; ix != GCOV_COUNTERS; ix++)
1030 if ((1u << ix) & prg_ctr_mask)
1031 n_counters++;
1033 /* Build the info and fn_info types. These are mutually recursive. */
1034 gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1035 gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
1036 gcov_fn_info_ptr_type = build_pointer_type
1037 (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
1038 build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
1039 build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
1041 /* Build the gcov info var, this is referred to in its own
1042 initializer. */
1043 gcov_info_var = build_decl (BUILTINS_LOCATION,
1044 VAR_DECL, NULL_TREE, gcov_info_type);
1045 TREE_STATIC (gcov_info_var) = 1;
1046 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
1047 DECL_NAME (gcov_info_var) = get_identifier (name_buf);
1049 build_init_ctor (gcov_info_type);
1051 return true;
1054 /* Generate the coverage function info for FN and DATA. Append a
1055 pointer to that object to CTOR and return the appended CTOR. */
1057 static vec<constructor_elt, va_gc> *
1058 coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
1059 struct coverage_data const *data)
1061 tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
1062 tree var = build_var (fn, gcov_fn_info_type, -1);
1064 DECL_INITIAL (var) = init;
1065 varpool_finalize_decl (var);
1067 CONSTRUCTOR_APPEND_ELT (ctor, NULL,
1068 build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
1069 return ctor;
1072 /* Finalize the coverage data. Generates the array of pointers to
1073 function objects from CTOR. Generate the gcov_info initializer. */
1075 static void
1076 coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
1078 unsigned n_functions = vec_safe_length (ctor);
1079 tree fn_info_ary_type = build_array_type
1080 (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
1081 build_index_type (size_int (n_functions - 1)));
1082 tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
1083 fn_info_ary_type);
1084 char name_buf[32];
1086 TREE_STATIC (fn_info_ary) = 1;
1087 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
1088 DECL_NAME (fn_info_ary) = get_identifier (name_buf);
1089 DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
1090 varpool_finalize_decl (fn_info_ary);
1092 DECL_INITIAL (gcov_info_var)
1093 = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
1094 varpool_finalize_decl (gcov_info_var);
1097 /* Perform file-level initialization. Read in data file, generate name
1098 of notes file. */
1100 void
1101 coverage_init (const char *filename)
1103 int len = strlen (filename);
1104 int prefix_len = 0;
1106 if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
1107 profile_data_prefix = getpwd ();
1109 if (profile_data_prefix)
1110 prefix_len = strlen (profile_data_prefix);
1112 /* Name of da file. */
1113 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1114 + prefix_len + 2);
1116 if (profile_data_prefix)
1118 memcpy (da_file_name, profile_data_prefix, prefix_len);
1119 da_file_name[prefix_len++] = '/';
1121 memcpy (da_file_name + prefix_len, filename, len);
1122 strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
1124 bbg_file_stamp = local_tick;
1126 if (flag_branch_probabilities)
1127 read_counts_file ();
1129 /* Name of bbg file. */
1130 if (flag_test_coverage && !flag_compare_debug)
1132 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1133 memcpy (bbg_file_name, filename, len);
1134 strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
1136 if (!gcov_open (bbg_file_name, -1))
1138 error ("cannot open %s", bbg_file_name);
1139 bbg_file_name = NULL;
1141 else
1143 gcov_write_unsigned (GCOV_NOTE_MAGIC);
1144 gcov_write_unsigned (GCOV_VERSION);
1145 gcov_write_unsigned (bbg_file_stamp);
1150 /* Performs file-level cleanup. Close notes file, generate coverage
1151 variables and constructor. */
1153 void
1154 coverage_finish (void)
1156 if (bbg_file_name && gcov_close ())
1157 unlink (bbg_file_name);
1159 if (!flag_branch_probabilities && flag_test_coverage
1160 && (!local_tick || local_tick == (unsigned)-1))
1161 /* Only remove the da file, if we're emitting coverage code and
1162 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1163 unlink (da_file_name);
1165 if (coverage_obj_init ())
1167 vec<constructor_elt, va_gc> *fn_ctor = NULL;
1168 struct coverage_data *fn;
1170 for (fn = functions_head; fn; fn = fn->next)
1171 fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
1172 coverage_obj_finish (fn_ctor);
1176 #include "gt-coverage.h"