1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2014 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
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
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/>. */
29 #include "coretypes.h"
33 #include "stringpool.h"
34 #include "stor-layout.h"
43 #include "hard-reg-set.h"
47 #include "dominance.h"
49 #include "basic-block.h"
54 #include "langhooks.h"
55 #include "hash-table.h"
56 #include "tree-iterator.h"
58 #include "pass_manager.h"
59 #include "tree-pass.h"
62 #include "plugin-api.h"
66 #include "diagnostic-core.h"
68 #include "filenames.h"
71 #include "auto-profile.h"
76 struct GTY((chain_next ("%h.next"))) coverage_data
78 struct coverage_data
*next
; /* next function */
79 unsigned ident
; /* function ident */
80 unsigned lineno_checksum
; /* function lineno checksum */
81 unsigned cfg_checksum
; /* function cfg checksum */
82 tree fn_decl
; /* the function decl */
83 tree ctr_vars
[GCOV_COUNTERS
]; /* counter variables. */
86 /* Counts information for a function. */
87 typedef struct counts_entry
94 unsigned lineno_checksum
;
95 unsigned cfg_checksum
;
97 struct gcov_ctr_summary summary
;
99 /* hash_table support. */
100 typedef counts_entry value_type
;
101 typedef counts_entry compare_type
;
102 static inline hashval_t
hash (const value_type
*);
103 static int equal (const value_type
*, const compare_type
*);
104 static void remove (value_type
*);
107 static GTY(()) struct coverage_data
*functions_head
= 0;
108 static struct coverage_data
**functions_tail
= &functions_head
;
109 static unsigned no_coverage
= 0;
111 /* Cumulative counter information for whole program. */
112 static unsigned prg_ctr_mask
; /* Mask of counter types generated. */
114 /* Counter information for current function. */
115 static unsigned fn_ctr_mask
; /* Mask of counters used. */
116 static GTY(()) tree fn_v_ctrs
[GCOV_COUNTERS
]; /* counter variables. */
117 static unsigned fn_n_ctrs
[GCOV_COUNTERS
]; /* Counters allocated. */
118 static unsigned fn_b_ctrs
[GCOV_COUNTERS
]; /* Allocation base. */
120 /* Coverage info VAR_DECL and function info type nodes. */
121 static GTY(()) tree gcov_info_var
;
122 static GTY(()) tree gcov_fn_info_type
;
123 static GTY(()) tree gcov_fn_info_ptr_type
;
125 /* Name of the notes (gcno) output file. The "bbg" prefix is for
126 historical reasons, when the notes file contained only the
127 basic block graph notes.
128 If this is NULL we're not writing to the notes file. */
129 static char *bbg_file_name
;
131 /* File stamp for notes file. */
132 static unsigned bbg_file_stamp
;
134 /* Name of the count data (gcda) file. */
135 static char *da_file_name
;
137 /* The names of merge functions for counters. */
138 #define STR(str) #str
139 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
140 static const char *const ctr_merge_functions
[GCOV_COUNTERS
] = {
141 #include "gcov-counter.def"
143 #undef DEF_GCOV_COUNTER
146 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
147 static const char *const ctr_names
[GCOV_COUNTERS
] = {
148 #include "gcov-counter.def"
150 #undef DEF_GCOV_COUNTER
152 /* Forward declarations. */
153 static void read_counts_file (void);
154 static tree
build_var (tree
, tree
, int);
155 static void build_fn_info_type (tree
, unsigned, tree
);
156 static void build_info_type (tree
, tree
);
157 static tree
build_fn_info (const struct coverage_data
*, tree
, tree
);
158 static tree
build_info (tree
, tree
);
159 static bool coverage_obj_init (void);
160 static vec
<constructor_elt
, va_gc
> *coverage_obj_fn
161 (vec
<constructor_elt
, va_gc
> *, tree
, struct coverage_data
const *);
162 static void coverage_obj_finish (vec
<constructor_elt
, va_gc
> *);
164 /* Return the type node for gcov_type. */
169 machine_mode mode
= smallest_mode_for_size (GCOV_TYPE_SIZE
, MODE_INT
);
170 return lang_hooks
.types
.type_for_mode (mode
, false);
173 /* Return the type node for gcov_unsigned_t. */
176 get_gcov_unsigned_t (void)
178 machine_mode mode
= smallest_mode_for_size (32, MODE_INT
);
179 return lang_hooks
.types
.type_for_mode (mode
, true);
183 counts_entry::hash (const value_type
*entry
)
185 return entry
->ident
* GCOV_COUNTERS
+ entry
->ctr
;
189 counts_entry::equal (const value_type
*entry1
,
190 const compare_type
*entry2
)
192 return entry1
->ident
== entry2
->ident
&& entry1
->ctr
== entry2
->ctr
;
196 counts_entry::remove (value_type
*entry
)
198 free (entry
->counts
);
202 /* Hash table of count data. */
203 static hash_table
<counts_entry
> *counts_hash
;
205 /* Read in the counts file, if available. */
208 read_counts_file (void)
210 gcov_unsigned_t fn_ident
= 0;
211 struct gcov_summary summary
;
212 unsigned new_summary
= 1;
215 unsigned lineno_checksum
= 0;
216 unsigned cfg_checksum
= 0;
218 if (!gcov_open (da_file_name
, 1))
221 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
223 warning (0, "%qs is not a gcov data file", da_file_name
);
227 else if ((tag
= gcov_read_unsigned ()) != GCOV_VERSION
)
231 GCOV_UNSIGNED2STRING (v
, tag
);
232 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
234 warning (0, "%qs is version %q.*s, expected version %q.*s",
235 da_file_name
, 4, v
, 4, e
);
240 /* Read the stamp, used for creating a generation count. */
241 tag
= gcov_read_unsigned ();
242 bbg_file_stamp
= crc32_unsigned (bbg_file_stamp
, tag
);
244 counts_hash
= new hash_table
<counts_entry
> (10);
245 while ((tag
= gcov_read_unsigned ()))
247 gcov_unsigned_t length
;
248 gcov_position_t offset
;
250 length
= gcov_read_unsigned ();
251 offset
= gcov_position ();
252 if (tag
== GCOV_TAG_FUNCTION
)
256 fn_ident
= gcov_read_unsigned ();
257 lineno_checksum
= gcov_read_unsigned ();
258 cfg_checksum
= gcov_read_unsigned ();
261 fn_ident
= lineno_checksum
= cfg_checksum
= 0;
264 else if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
266 struct gcov_summary sum
;
270 memset (&summary
, 0, sizeof (summary
));
272 gcov_read_summary (&sum
);
273 for (ix
= 0; ix
!= GCOV_COUNTERS_SUMMABLE
; ix
++)
275 summary
.ctrs
[ix
].runs
+= sum
.ctrs
[ix
].runs
;
276 summary
.ctrs
[ix
].sum_all
+= sum
.ctrs
[ix
].sum_all
;
277 if (summary
.ctrs
[ix
].run_max
< sum
.ctrs
[ix
].run_max
)
278 summary
.ctrs
[ix
].run_max
= sum
.ctrs
[ix
].run_max
;
279 summary
.ctrs
[ix
].sum_max
+= sum
.ctrs
[ix
].sum_max
;
282 memcpy (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
283 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
284 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
286 gcov_histogram_merge (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
287 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
);
290 else if (GCOV_TAG_IS_COUNTER (tag
) && fn_ident
)
292 counts_entry_t
**slot
, *entry
, elt
;
293 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
296 elt
.ident
= fn_ident
;
297 elt
.ctr
= GCOV_COUNTER_FOR_TAG (tag
);
299 slot
= counts_hash
->find_slot (&elt
, INSERT
);
303 *slot
= entry
= XCNEW (counts_entry_t
);
304 entry
->ident
= fn_ident
;
305 entry
->ctr
= elt
.ctr
;
306 entry
->lineno_checksum
= lineno_checksum
;
307 entry
->cfg_checksum
= cfg_checksum
;
308 if (elt
.ctr
< GCOV_COUNTERS_SUMMABLE
)
309 entry
->summary
= summary
.ctrs
[elt
.ctr
];
310 entry
->summary
.num
= n_counts
;
311 entry
->counts
= XCNEWVEC (gcov_type
, n_counts
);
313 else if (entry
->lineno_checksum
!= lineno_checksum
314 || entry
->cfg_checksum
!= cfg_checksum
)
316 error ("Profile data for function %u is corrupted", fn_ident
);
317 error ("checksum is (%x,%x) instead of (%x,%x)",
318 entry
->lineno_checksum
, entry
->cfg_checksum
,
319 lineno_checksum
, cfg_checksum
);
324 else if (entry
->summary
.num
!= n_counts
)
326 error ("Profile data for function %u is corrupted", fn_ident
);
327 error ("number of counters is %d instead of %d", entry
->summary
.num
, n_counts
);
332 else if (elt
.ctr
>= GCOV_COUNTERS_SUMMABLE
)
334 error ("cannot merge separate %s counters for function %u",
335 ctr_names
[elt
.ctr
], fn_ident
);
340 entry
->summary
.runs
+= summary
.ctrs
[elt
.ctr
].runs
;
341 entry
->summary
.sum_all
+= summary
.ctrs
[elt
.ctr
].sum_all
;
342 if (entry
->summary
.run_max
< summary
.ctrs
[elt
.ctr
].run_max
)
343 entry
->summary
.run_max
= summary
.ctrs
[elt
.ctr
].run_max
;
344 entry
->summary
.sum_max
+= summary
.ctrs
[elt
.ctr
].sum_max
;
346 for (ix
= 0; ix
!= n_counts
; ix
++)
347 entry
->counts
[ix
] += gcov_read_counter ();
350 gcov_sync (offset
, length
);
351 if ((is_error
= gcov_is_error ()))
353 error (is_error
< 0 ? "%qs has overflowed" : "%qs is corrupted",
364 /* Returns the counters for a particular tag. */
367 get_coverage_counts (unsigned counter
, unsigned expected
,
368 unsigned cfg_checksum
, unsigned lineno_checksum
,
369 const struct gcov_ctr_summary
**summary
)
371 counts_entry_t
*entry
, elt
;
373 /* No hash table, no counts. */
376 static int warned
= 0;
378 if (!warned
++ && dump_enabled_p ())
379 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
380 (flag_guess_branch_prob
381 ? "file %s not found, execution counts estimated\n"
382 : "file %s not found, execution counts assumed to "
387 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
388 elt
.ident
= current_function_funcdef_no
+ 1;
391 gcc_assert (coverage_node_map_initialized_p ());
392 elt
.ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
395 entry
= counts_hash
->find (&elt
);
396 if (!entry
|| !entry
->summary
.num
)
397 /* The function was not emitted, or is weak and not chosen in the
398 final executable. Silently fail, because there's nothing we
402 if (entry
->cfg_checksum
!= cfg_checksum
403 || entry
->summary
.num
!= expected
)
405 static int warned
= 0;
406 bool warning_printed
= false;
407 tree id
= DECL_ASSEMBLER_NAME (current_function_decl
);
410 warning_at (input_location
, OPT_Wcoverage_mismatch
,
411 "the control flow of function %qE does not match "
412 "its profile data (counter %qs)", id
, ctr_names
[counter
]);
413 if (warning_printed
&& dump_enabled_p ())
415 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
416 "use -Wno-error=coverage-mismatch to tolerate "
417 "the mismatch but performance may drop if the "
418 "function is hot\n");
423 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
424 "coverage mismatch ignored\n");
425 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
426 flag_guess_branch_prob
427 ? G_("execution counts estimated\n")
428 : G_("execution counts assumed to be zero\n"));
429 if (!flag_guess_branch_prob
)
430 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
431 "this can result in poorly optimized code\n");
437 else if (entry
->lineno_checksum
!= lineno_checksum
)
439 warning (OPT_Wcoverage_mismatch
,
440 "source locations for function %qE have changed,"
441 " the profile data may be out of date",
442 DECL_ASSEMBLER_NAME (current_function_decl
));
446 *summary
= &entry
->summary
;
448 return entry
->counts
;
451 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
452 allocation succeeded. */
455 coverage_counter_alloc (unsigned counter
, unsigned num
)
463 if (!fn_v_ctrs
[counter
])
465 tree array_type
= build_array_type (get_gcov_type (), NULL_TREE
);
468 = build_var (current_function_decl
, array_type
, counter
);
471 fn_b_ctrs
[counter
] = fn_n_ctrs
[counter
];
472 fn_n_ctrs
[counter
] += num
;
474 fn_ctr_mask
|= 1 << counter
;
478 /* Generate a tree to access COUNTER NO. */
481 tree_coverage_counter_ref (unsigned counter
, unsigned no
)
483 tree gcov_type_node
= get_gcov_type ();
485 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
487 no
+= fn_b_ctrs
[counter
];
489 /* "no" here is an array index, scaled to bytes later. */
490 return build4 (ARRAY_REF
, gcov_type_node
, fn_v_ctrs
[counter
],
491 build_int_cst (integer_type_node
, no
), NULL
, NULL
);
494 /* Generate a tree to access the address of COUNTER NO. */
497 tree_coverage_counter_addr (unsigned counter
, unsigned no
)
499 tree gcov_type_node
= get_gcov_type ();
501 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
502 no
+= fn_b_ctrs
[counter
];
504 /* "no" here is an array index, scaled to bytes later. */
505 return build_fold_addr_expr (build4 (ARRAY_REF
, gcov_type_node
,
507 build_int_cst (integer_type_node
, no
),
512 /* Generate a checksum for a string. CHKSUM is the current
516 coverage_checksum_string (unsigned chksum
, const char *string
)
521 /* Look for everything that looks if it were produced by
522 get_file_function_name and zero out the second part
523 that may result from flag_random_seed. This is not critical
524 as the checksums are used only for sanity checking. */
525 for (i
= 0; string
[i
]; i
++)
528 if (!strncmp (string
+ i
, "_GLOBAL__N_", 11))
530 if (!strncmp (string
+ i
, "_GLOBAL__", 9))
533 /* C++ namespaces do have scheme:
534 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
535 since filename might contain extra underscores there seems
536 to be no better chance then walk all possible offsets looking
540 for (i
= i
+ offset
; string
[i
]; i
++)
545 for (y
= 1; y
< 9; y
++)
546 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
547 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
549 if (y
!= 9 || string
[i
+ 9] != '_')
551 for (y
= 10; y
< 18; y
++)
552 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
553 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
558 string
= dup
= xstrdup (string
);
559 for (y
= 10; y
< 18; y
++)
566 chksum
= crc32_string (chksum
, string
);
572 /* Compute checksum for the current function. We generate a CRC32. */
575 coverage_compute_lineno_checksum (void)
577 expanded_location xloc
578 = expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
579 unsigned chksum
= xloc
.line
;
581 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
582 chksum
= coverage_checksum_string
583 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
)));
588 /* Compute profile ID. This is better to be unique in whole program. */
591 coverage_compute_profile_id (struct cgraph_node
*n
)
595 /* Externally visible symbols have unique name. */
596 if (TREE_PUBLIC (n
->decl
) || DECL_EXTERNAL (n
->decl
) || n
->unique_name
)
598 chksum
= coverage_checksum_string
599 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
603 expanded_location xloc
604 = expand_location (DECL_SOURCE_LOCATION (n
->decl
));
605 bool use_name_only
= (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
) == 0);
607 chksum
= (use_name_only
? 0 : xloc
.line
);
608 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
609 chksum
= coverage_checksum_string
610 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
611 if (!use_name_only
&& first_global_object_name
)
612 chksum
= coverage_checksum_string
613 (chksum
, first_global_object_name
);
614 chksum
= coverage_checksum_string
615 (chksum
, aux_base_name
);
618 /* Non-negative integers are hopefully small enough to fit in all targets.
619 Gcov file formats wants non-zero function IDs. */
620 chksum
= chksum
& 0x7fffffff;
621 return chksum
+ (!chksum
);
624 /* Compute cfg checksum for the function FN given as argument.
625 The checksum is calculated carefully so that
626 source code changes that doesn't affect the control flow graph
627 won't change the checksum.
628 This is to make the profile data useable across source code change.
629 The downside of this is that the compiler may use potentially
630 wrong profile data - that the source code change has non-trivial impact
631 on the validity of profile data (e.g. the reversed condition)
632 but the compiler won't detect the change and use the wrong profile data. */
635 coverage_compute_cfg_checksum (struct function
*fn
)
638 unsigned chksum
= n_basic_blocks_for_fn (fn
);
640 FOR_EACH_BB_FN (bb
, fn
)
644 chksum
= crc32_byte (chksum
, bb
->index
);
645 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
647 chksum
= crc32_byte (chksum
, e
->dest
->index
);
654 /* Begin output to the notes file for the current function.
655 Writes the function header. Returns nonzero if data should be output. */
658 coverage_begin_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
660 expanded_location xloc
;
661 unsigned long offset
;
663 /* We don't need to output .gcno file unless we're under -ftest-coverage
664 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
665 if (no_coverage
|| !bbg_file_name
)
668 xloc
= expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
670 /* Announce function */
671 offset
= gcov_write_tag (GCOV_TAG_FUNCTION
);
672 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
673 gcov_write_unsigned (current_function_funcdef_no
+ 1);
676 gcc_assert (coverage_node_map_initialized_p ());
677 gcov_write_unsigned (
678 cgraph_node::get (current_function_decl
)->profile_id
);
681 gcov_write_unsigned (lineno_checksum
);
682 gcov_write_unsigned (cfg_checksum
);
683 gcov_write_string (IDENTIFIER_POINTER
684 (DECL_ASSEMBLER_NAME (current_function_decl
)));
685 gcov_write_string (xloc
.file
);
686 gcov_write_unsigned (xloc
.line
);
687 gcov_write_length (offset
);
689 return !gcov_is_error ();
692 /* Finish coverage data for the current function. Verify no output
693 error has occurred. Save function coverage counts. */
696 coverage_end_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
700 if (bbg_file_name
&& gcov_is_error ())
702 warning (0, "error writing %qs", bbg_file_name
);
703 unlink (bbg_file_name
);
704 bbg_file_name
= NULL
;
709 struct coverage_data
*item
= 0;
711 item
= ggc_alloc
<coverage_data
> ();
713 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
714 item
->ident
= current_function_funcdef_no
+ 1;
717 gcc_assert (coverage_node_map_initialized_p ());
718 item
->ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
721 item
->lineno_checksum
= lineno_checksum
;
722 item
->cfg_checksum
= cfg_checksum
;
724 item
->fn_decl
= current_function_decl
;
726 *functions_tail
= item
;
727 functions_tail
= &item
->next
;
729 for (i
= 0; i
!= GCOV_COUNTERS
; i
++)
731 tree var
= fn_v_ctrs
[i
];
734 item
->ctr_vars
[i
] = var
;
737 tree array_type
= build_index_type (size_int (fn_n_ctrs
[i
] - 1));
738 array_type
= build_array_type (get_gcov_type (), array_type
);
739 TREE_TYPE (var
) = array_type
;
740 DECL_SIZE (var
) = TYPE_SIZE (array_type
);
741 DECL_SIZE_UNIT (var
) = TYPE_SIZE_UNIT (array_type
);
742 varpool_node::finalize_decl (var
);
745 fn_b_ctrs
[i
] = fn_n_ctrs
[i
] = 0;
746 fn_v_ctrs
[i
] = NULL_TREE
;
748 prg_ctr_mask
|= fn_ctr_mask
;
753 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
754 >= 0 it is a counter array, otherwise it is the function structure. */
757 build_var (tree fn_decl
, tree type
, int counter
)
759 tree var
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
, type
);
760 const char *fn_name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl
));
762 size_t fn_name_len
, len
;
764 fn_name
= targetm
.strip_name_encoding (fn_name
);
765 fn_name_len
= strlen (fn_name
);
766 buf
= XALLOCAVEC (char, fn_name_len
+ 8 + sizeof (int) * 3);
769 strcpy (buf
, "__gcov__");
771 sprintf (buf
, "__gcov%u_", counter
);
773 #ifndef NO_DOT_IN_LABEL
775 #elif !defined NO_DOLLAR_IN_LABEL
778 memcpy (buf
+ len
, fn_name
, fn_name_len
+ 1);
779 DECL_NAME (var
) = get_identifier (buf
);
780 TREE_STATIC (var
) = 1;
781 TREE_ADDRESSABLE (var
) = 1;
782 DECL_NONALIASED (var
) = 1;
783 DECL_ALIGN (var
) = TYPE_ALIGN (type
);
788 /* Creates the gcov_fn_info RECORD_TYPE. */
791 build_fn_info_type (tree type
, unsigned counters
, tree gcov_info_type
)
793 tree ctr_info
= lang_hooks
.types
.make_type (RECORD_TYPE
);
797 gcc_assert (counters
);
800 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
801 get_gcov_unsigned_t ());
804 /* ctr_info::values */
805 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
806 build_pointer_type (get_gcov_type ()));
807 DECL_CHAIN (field
) = fields
;
810 finish_builtin_struct (ctr_info
, "__gcov_ctr_info", fields
, NULL_TREE
);
813 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
814 build_pointer_type (build_qualified_type
815 (gcov_info_type
, TYPE_QUAL_CONST
)));
819 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
820 get_gcov_unsigned_t ());
821 DECL_CHAIN (field
) = fields
;
824 /* lineno_checksum */
825 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
826 get_gcov_unsigned_t ());
827 DECL_CHAIN (field
) = fields
;
831 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
832 get_gcov_unsigned_t ());
833 DECL_CHAIN (field
) = fields
;
836 array_type
= build_index_type (size_int (counters
- 1));
837 array_type
= build_array_type (ctr_info
, array_type
);
840 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
, array_type
);
841 DECL_CHAIN (field
) = fields
;
844 finish_builtin_struct (type
, "__gcov_fn_info", fields
, NULL_TREE
);
847 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
848 the coverage data for the function and TYPE is the gcov_fn_info
849 RECORD_TYPE. KEY is the object file key. */
852 build_fn_info (const struct coverage_data
*data
, tree type
, tree key
)
854 tree fields
= TYPE_FIELDS (type
);
857 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
858 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
861 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
862 build1 (ADDR_EXPR
, TREE_TYPE (fields
), key
));
863 fields
= DECL_CHAIN (fields
);
866 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
867 build_int_cstu (get_gcov_unsigned_t (),
869 fields
= DECL_CHAIN (fields
);
871 /* lineno_checksum */
872 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
873 build_int_cstu (get_gcov_unsigned_t (),
874 data
->lineno_checksum
));
875 fields
= DECL_CHAIN (fields
);
878 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
879 build_int_cstu (get_gcov_unsigned_t (),
880 data
->cfg_checksum
));
881 fields
= DECL_CHAIN (fields
);
884 ctr_type
= TREE_TYPE (TREE_TYPE (fields
));
885 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
886 if (prg_ctr_mask
& (1 << ix
))
888 vec
<constructor_elt
, va_gc
> *ctr
= NULL
;
889 tree var
= data
->ctr_vars
[ix
];
894 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var
))))
897 CONSTRUCTOR_APPEND_ELT (ctr
, TYPE_FIELDS (ctr_type
),
898 build_int_cstu (get_gcov_unsigned_t (),
902 CONSTRUCTOR_APPEND_ELT (ctr
, DECL_CHAIN (TYPE_FIELDS (ctr_type
)),
903 build_fold_addr_expr (var
));
905 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, build_constructor (ctr_type
, ctr
));
908 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
909 build_constructor (TREE_TYPE (fields
), v2
));
911 return build_constructor (type
, v1
);
914 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
915 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
918 build_info_type (tree type
, tree fn_info_ptr_type
)
920 tree field
, fields
= NULL_TREE
;
924 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
925 get_gcov_unsigned_t ());
926 DECL_CHAIN (field
) = fields
;
930 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
931 build_pointer_type (build_qualified_type
932 (type
, TYPE_QUAL_CONST
)));
933 DECL_CHAIN (field
) = fields
;
937 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
938 get_gcov_unsigned_t ());
939 DECL_CHAIN (field
) = fields
;
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
;
951 = build_function_type_list (void_type_node
,
952 build_pointer_type (get_gcov_type ()),
953 get_gcov_unsigned_t (), NULL_TREE
);
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
,
959 DECL_CHAIN (field
) = fields
;
963 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
964 get_gcov_unsigned_t ());
965 DECL_CHAIN (field
) = fields
;
968 /* function_info pointer pointer */
969 fn_info_ptr_type
= build_pointer_type
970 (build_qualified_type (fn_info_ptr_type
, TYPE_QUAL_CONST
));
971 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
973 DECL_CHAIN (field
) = fields
;
976 finish_builtin_struct (type
, "__gcov_info", fields
, NULL_TREE
);
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. */
984 build_info (tree info_type
, tree fn_ary
)
986 tree info_fields
= TYPE_FIELDS (info_type
);
987 tree merge_fn_type
, n_funcs
;
989 tree filename_string
;
990 int da_file_name_len
;
991 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
992 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
995 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
996 build_int_cstu (TREE_TYPE (info_fields
),
998 info_fields
= DECL_CHAIN (info_fields
);
1001 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, null_pointer_node
);
1002 info_fields
= DECL_CHAIN (info_fields
);
1005 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1006 build_int_cstu (TREE_TYPE (info_fields
),
1008 info_fields
= DECL_CHAIN (info_fields
);
1011 da_file_name_len
= strlen (da_file_name
);
1012 filename_string
= build_string (da_file_name_len
+ 1, da_file_name
);
1013 TREE_TYPE (filename_string
) = build_array_type
1014 (char_type_node
, build_index_type (size_int (da_file_name_len
)));
1015 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1016 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
),
1018 info_fields
= DECL_CHAIN (info_fields
);
1020 /* merge fn array -- NULL slots indicate unmeasured counters */
1021 merge_fn_type
= TREE_TYPE (TREE_TYPE (info_fields
));
1022 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1024 tree ptr
= null_pointer_node
;
1026 if ((1u << ix
) & prg_ctr_mask
)
1028 tree merge_fn
= build_decl (BUILTINS_LOCATION
,
1030 get_identifier (ctr_merge_functions
[ix
]),
1031 TREE_TYPE (merge_fn_type
));
1032 DECL_EXTERNAL (merge_fn
) = 1;
1033 TREE_PUBLIC (merge_fn
) = 1;
1034 DECL_ARTIFICIAL (merge_fn
) = 1;
1035 TREE_NOTHROW (merge_fn
) = 1;
1036 /* Initialize assembler name so we can stream out. */
1037 DECL_ASSEMBLER_NAME (merge_fn
);
1038 ptr
= build1 (ADDR_EXPR
, merge_fn_type
, merge_fn
);
1040 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, ptr
);
1042 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1043 build_constructor (TREE_TYPE (info_fields
), v2
));
1044 info_fields
= DECL_CHAIN (info_fields
);
1047 n_funcs
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary
)));
1048 n_funcs
= fold_build2 (PLUS_EXPR
, TREE_TYPE (info_fields
),
1049 n_funcs
, size_one_node
);
1050 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, n_funcs
);
1051 info_fields
= DECL_CHAIN (info_fields
);
1054 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1055 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
), fn_ary
));
1056 info_fields
= DECL_CHAIN (info_fields
);
1058 gcc_assert (!info_fields
);
1059 return build_constructor (info_type
, v1
);
1062 /* Generate the constructor function to call __gcov_init. */
1065 build_init_ctor (tree gcov_info_type
)
1067 tree ctor
, stmt
, init_fn
;
1069 /* Build a decl for __gcov_init. */
1070 init_fn
= build_pointer_type (gcov_info_type
);
1071 init_fn
= build_function_type_list (void_type_node
, init_fn
, NULL
);
1072 init_fn
= build_decl (BUILTINS_LOCATION
, FUNCTION_DECL
,
1073 get_identifier ("__gcov_init"), init_fn
);
1074 TREE_PUBLIC (init_fn
) = 1;
1075 DECL_EXTERNAL (init_fn
) = 1;
1076 DECL_ASSEMBLER_NAME (init_fn
);
1078 /* Generate a call to __gcov_init(&gcov_info). */
1080 stmt
= build_fold_addr_expr (gcov_info_var
);
1081 stmt
= build_call_expr (init_fn
, 1, stmt
);
1082 append_to_statement_list (stmt
, &ctor
);
1084 /* Generate a constructor to run it. */
1085 cgraph_build_static_cdtor ('I', ctor
, DEFAULT_INIT_PRIORITY
);
1088 /* Create the gcov_info types and object. Generate the constructor
1089 function to call __gcov_init. Does not generate the initializer
1090 for the object. Returns TRUE if coverage data is being emitted. */
1093 coverage_obj_init (void)
1095 tree gcov_info_type
;
1096 unsigned n_counters
= 0;
1098 struct coverage_data
*fn
;
1099 struct coverage_data
**fn_prev
;
1102 no_coverage
= 1; /* Disable any further coverage. */
1107 if (symtab
->dump_file
)
1108 fprintf (symtab
->dump_file
, "Using data file %s\n", da_file_name
);
1110 /* Prune functions. */
1111 for (fn_prev
= &functions_head
; (fn
= *fn_prev
);)
1112 if (DECL_STRUCT_FUNCTION (fn
->fn_decl
))
1113 fn_prev
= &fn
->next
;
1115 /* The function is not being emitted, remove from list. */
1116 *fn_prev
= fn
->next
;
1118 if (functions_head
== NULL
)
1121 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1122 if ((1u << ix
) & prg_ctr_mask
)
1125 /* Build the info and fn_info types. These are mutually recursive. */
1126 gcov_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1127 gcov_fn_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1128 gcov_fn_info_ptr_type
= build_pointer_type
1129 (build_qualified_type (gcov_fn_info_type
, TYPE_QUAL_CONST
));
1130 build_fn_info_type (gcov_fn_info_type
, n_counters
, gcov_info_type
);
1131 build_info_type (gcov_info_type
, gcov_fn_info_ptr_type
);
1133 /* Build the gcov info var, this is referred to in its own
1135 gcov_info_var
= build_decl (BUILTINS_LOCATION
,
1136 VAR_DECL
, NULL_TREE
, gcov_info_type
);
1137 TREE_STATIC (gcov_info_var
) = 1;
1138 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 0);
1139 DECL_NAME (gcov_info_var
) = get_identifier (name_buf
);
1141 build_init_ctor (gcov_info_type
);
1146 /* Generate the coverage function info for FN and DATA. Append a
1147 pointer to that object to CTOR and return the appended CTOR. */
1149 static vec
<constructor_elt
, va_gc
> *
1150 coverage_obj_fn (vec
<constructor_elt
, va_gc
> *ctor
, tree fn
,
1151 struct coverage_data
const *data
)
1153 tree init
= build_fn_info (data
, gcov_fn_info_type
, gcov_info_var
);
1154 tree var
= build_var (fn
, gcov_fn_info_type
, -1);
1156 DECL_INITIAL (var
) = init
;
1157 varpool_node::finalize_decl (var
);
1159 CONSTRUCTOR_APPEND_ELT (ctor
, NULL
,
1160 build1 (ADDR_EXPR
, gcov_fn_info_ptr_type
, var
));
1164 /* Finalize the coverage data. Generates the array of pointers to
1165 function objects from CTOR. Generate the gcov_info initializer. */
1168 coverage_obj_finish (vec
<constructor_elt
, va_gc
> *ctor
)
1170 unsigned n_functions
= vec_safe_length (ctor
);
1171 tree fn_info_ary_type
= build_array_type
1172 (build_qualified_type (gcov_fn_info_ptr_type
, TYPE_QUAL_CONST
),
1173 build_index_type (size_int (n_functions
- 1)));
1174 tree fn_info_ary
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
,
1178 TREE_STATIC (fn_info_ary
) = 1;
1179 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 1);
1180 DECL_NAME (fn_info_ary
) = get_identifier (name_buf
);
1181 DECL_INITIAL (fn_info_ary
) = build_constructor (fn_info_ary_type
, ctor
);
1182 varpool_node::finalize_decl (fn_info_ary
);
1184 DECL_INITIAL (gcov_info_var
)
1185 = build_info (TREE_TYPE (gcov_info_var
), fn_info_ary
);
1186 varpool_node::finalize_decl (gcov_info_var
);
1189 /* Perform file-level initialization. Read in data file, generate name
1193 coverage_init (const char *filename
)
1195 int len
= strlen (filename
);
1198 /* Since coverage_init is invoked very early, before the pass
1199 manager, we need to set up the dumping explicitly. This is
1200 similar to the handling in finish_optimization_passes. */
1201 int profile_pass_num
=
1202 g
->get_passes ()->get_pass_profile ()->static_pass_number
;
1203 g
->get_dumps ()->dump_start (profile_pass_num
, NULL
);
1205 if (!profile_data_prefix
&& !IS_ABSOLUTE_PATH (filename
))
1206 profile_data_prefix
= getpwd ();
1208 if (profile_data_prefix
)
1209 prefix_len
= strlen (profile_data_prefix
);
1211 /* Name of da file. */
1212 da_file_name
= XNEWVEC (char, len
+ strlen (GCOV_DATA_SUFFIX
)
1215 if (profile_data_prefix
)
1217 memcpy (da_file_name
, profile_data_prefix
, prefix_len
);
1218 da_file_name
[prefix_len
++] = '/';
1220 memcpy (da_file_name
+ prefix_len
, filename
, len
);
1221 strcpy (da_file_name
+ prefix_len
+ len
, GCOV_DATA_SUFFIX
);
1223 bbg_file_stamp
= local_tick
;
1225 if (flag_auto_profile
)
1226 read_autofdo_file ();
1227 else if (flag_branch_probabilities
)
1228 read_counts_file ();
1230 /* Name of bbg file. */
1231 if (flag_test_coverage
&& !flag_compare_debug
)
1233 bbg_file_name
= XNEWVEC (char, len
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1234 memcpy (bbg_file_name
, filename
, len
);
1235 strcpy (bbg_file_name
+ len
, GCOV_NOTE_SUFFIX
);
1237 if (!gcov_open (bbg_file_name
, -1))
1239 error ("cannot open %s", bbg_file_name
);
1240 bbg_file_name
= NULL
;
1244 gcov_write_unsigned (GCOV_NOTE_MAGIC
);
1245 gcov_write_unsigned (GCOV_VERSION
);
1246 gcov_write_unsigned (bbg_file_stamp
);
1250 g
->get_dumps ()->dump_finish (profile_pass_num
);
1253 /* Performs file-level cleanup. Close notes file, generate coverage
1254 variables and constructor. */
1257 coverage_finish (void)
1259 if (bbg_file_name
&& gcov_close ())
1260 unlink (bbg_file_name
);
1262 if (!flag_branch_probabilities
&& flag_test_coverage
1263 && (!local_tick
|| local_tick
== (unsigned)-1))
1264 /* Only remove the da file, if we're emitting coverage code and
1265 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1266 unlink (da_file_name
);
1268 if (coverage_obj_init ())
1270 vec
<constructor_elt
, va_gc
> *fn_ctor
= NULL
;
1271 struct coverage_data
*fn
;
1273 for (fn
= functions_head
; fn
; fn
= fn
->next
)
1274 fn_ctor
= coverage_obj_fn (fn_ctor
, fn
->fn_decl
, fn
);
1275 coverage_obj_finish (fn_ctor
);
1278 XDELETEVEC (da_file_name
);
1279 da_file_name
= NULL
;
1282 #include "gt-coverage.h"