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 "diagnostic-core.h"
64 #include "filenames.h"
67 #include "auto-profile.h"
72 struct GTY((chain_next ("%h.next"))) coverage_data
74 struct coverage_data
*next
; /* next function */
75 unsigned ident
; /* function ident */
76 unsigned lineno_checksum
; /* function lineno checksum */
77 unsigned cfg_checksum
; /* function cfg checksum */
78 tree fn_decl
; /* the function decl */
79 tree ctr_vars
[GCOV_COUNTERS
]; /* counter variables. */
82 /* Counts information for a function. */
83 typedef struct counts_entry
90 unsigned lineno_checksum
;
91 unsigned cfg_checksum
;
93 struct gcov_ctr_summary summary
;
95 /* hash_table support. */
96 typedef counts_entry value_type
;
97 typedef counts_entry compare_type
;
98 static inline hashval_t
hash (const value_type
*);
99 static int equal (const value_type
*, const compare_type
*);
100 static void remove (value_type
*);
103 static GTY(()) struct coverage_data
*functions_head
= 0;
104 static struct coverage_data
**functions_tail
= &functions_head
;
105 static unsigned no_coverage
= 0;
107 /* Cumulative counter information for whole program. */
108 static unsigned prg_ctr_mask
; /* Mask of counter types generated. */
110 /* Counter information for current function. */
111 static unsigned fn_ctr_mask
; /* Mask of counters used. */
112 static GTY(()) tree fn_v_ctrs
[GCOV_COUNTERS
]; /* counter variables. */
113 static unsigned fn_n_ctrs
[GCOV_COUNTERS
]; /* Counters allocated. */
114 static unsigned fn_b_ctrs
[GCOV_COUNTERS
]; /* Allocation base. */
116 /* Coverage info VAR_DECL and function info type nodes. */
117 static GTY(()) tree gcov_info_var
;
118 static GTY(()) tree gcov_fn_info_type
;
119 static GTY(()) tree gcov_fn_info_ptr_type
;
121 /* Name of the notes (gcno) output file. The "bbg" prefix is for
122 historical reasons, when the notes file contained only the
123 basic block graph notes.
124 If this is NULL we're not writing to the notes file. */
125 static char *bbg_file_name
;
127 /* File stamp for notes file. */
128 static unsigned bbg_file_stamp
;
130 /* Name of the count data (gcda) file. */
131 static char *da_file_name
;
133 /* The names of merge functions for counters. */
134 #define STR(str) #str
135 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
136 static const char *const ctr_merge_functions
[GCOV_COUNTERS
] = {
137 #include "gcov-counter.def"
139 #undef DEF_GCOV_COUNTER
142 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
143 static const char *const ctr_names
[GCOV_COUNTERS
] = {
144 #include "gcov-counter.def"
146 #undef DEF_GCOV_COUNTER
148 /* Forward declarations. */
149 static void read_counts_file (void);
150 static tree
build_var (tree
, tree
, int);
151 static void build_fn_info_type (tree
, unsigned, tree
);
152 static void build_info_type (tree
, tree
);
153 static tree
build_fn_info (const struct coverage_data
*, tree
, tree
);
154 static tree
build_info (tree
, tree
);
155 static bool coverage_obj_init (void);
156 static vec
<constructor_elt
, va_gc
> *coverage_obj_fn
157 (vec
<constructor_elt
, va_gc
> *, tree
, struct coverage_data
const *);
158 static void coverage_obj_finish (vec
<constructor_elt
, va_gc
> *);
160 /* Return the type node for gcov_type. */
165 enum machine_mode mode
= smallest_mode_for_size (GCOV_TYPE_SIZE
, MODE_INT
);
166 return lang_hooks
.types
.type_for_mode (mode
, false);
169 /* Return the type node for gcov_unsigned_t. */
172 get_gcov_unsigned_t (void)
174 enum machine_mode mode
= smallest_mode_for_size (32, MODE_INT
);
175 return lang_hooks
.types
.type_for_mode (mode
, true);
179 counts_entry::hash (const value_type
*entry
)
181 return entry
->ident
* GCOV_COUNTERS
+ entry
->ctr
;
185 counts_entry::equal (const value_type
*entry1
,
186 const compare_type
*entry2
)
188 return entry1
->ident
== entry2
->ident
&& entry1
->ctr
== entry2
->ctr
;
192 counts_entry::remove (value_type
*entry
)
194 free (entry
->counts
);
198 /* Hash table of count data. */
199 static hash_table
<counts_entry
> *counts_hash
;
201 /* Read in the counts file, if available. */
204 read_counts_file (void)
206 gcov_unsigned_t fn_ident
= 0;
207 struct gcov_summary summary
;
208 unsigned new_summary
= 1;
211 unsigned lineno_checksum
= 0;
212 unsigned cfg_checksum
= 0;
214 if (!gcov_open (da_file_name
, 1))
217 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
219 warning (0, "%qs is not a gcov data file", da_file_name
);
223 else if ((tag
= gcov_read_unsigned ()) != GCOV_VERSION
)
227 GCOV_UNSIGNED2STRING (v
, tag
);
228 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
230 warning (0, "%qs is version %q.*s, expected version %q.*s",
231 da_file_name
, 4, v
, 4, e
);
236 /* Read the stamp, used for creating a generation count. */
237 tag
= gcov_read_unsigned ();
238 bbg_file_stamp
= crc32_unsigned (bbg_file_stamp
, tag
);
240 counts_hash
= new hash_table
<counts_entry
> (10);
241 while ((tag
= gcov_read_unsigned ()))
243 gcov_unsigned_t length
;
244 gcov_position_t offset
;
246 length
= gcov_read_unsigned ();
247 offset
= gcov_position ();
248 if (tag
== GCOV_TAG_FUNCTION
)
252 fn_ident
= gcov_read_unsigned ();
253 lineno_checksum
= gcov_read_unsigned ();
254 cfg_checksum
= gcov_read_unsigned ();
257 fn_ident
= lineno_checksum
= cfg_checksum
= 0;
260 else if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
262 struct gcov_summary sum
;
266 memset (&summary
, 0, sizeof (summary
));
268 gcov_read_summary (&sum
);
269 for (ix
= 0; ix
!= GCOV_COUNTERS_SUMMABLE
; ix
++)
271 summary
.ctrs
[ix
].runs
+= sum
.ctrs
[ix
].runs
;
272 summary
.ctrs
[ix
].sum_all
+= sum
.ctrs
[ix
].sum_all
;
273 if (summary
.ctrs
[ix
].run_max
< sum
.ctrs
[ix
].run_max
)
274 summary
.ctrs
[ix
].run_max
= sum
.ctrs
[ix
].run_max
;
275 summary
.ctrs
[ix
].sum_max
+= sum
.ctrs
[ix
].sum_max
;
278 memcpy (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
279 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
280 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
282 gcov_histogram_merge (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
283 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
);
286 else if (GCOV_TAG_IS_COUNTER (tag
) && fn_ident
)
288 counts_entry_t
**slot
, *entry
, elt
;
289 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
292 elt
.ident
= fn_ident
;
293 elt
.ctr
= GCOV_COUNTER_FOR_TAG (tag
);
295 slot
= counts_hash
->find_slot (&elt
, INSERT
);
299 *slot
= entry
= XCNEW (counts_entry_t
);
300 entry
->ident
= fn_ident
;
301 entry
->ctr
= elt
.ctr
;
302 entry
->lineno_checksum
= lineno_checksum
;
303 entry
->cfg_checksum
= cfg_checksum
;
304 if (elt
.ctr
< GCOV_COUNTERS_SUMMABLE
)
305 entry
->summary
= summary
.ctrs
[elt
.ctr
];
306 entry
->summary
.num
= n_counts
;
307 entry
->counts
= XCNEWVEC (gcov_type
, n_counts
);
309 else if (entry
->lineno_checksum
!= lineno_checksum
310 || entry
->cfg_checksum
!= cfg_checksum
)
312 error ("Profile data for function %u is corrupted", fn_ident
);
313 error ("checksum is (%x,%x) instead of (%x,%x)",
314 entry
->lineno_checksum
, entry
->cfg_checksum
,
315 lineno_checksum
, cfg_checksum
);
320 else if (entry
->summary
.num
!= n_counts
)
322 error ("Profile data for function %u is corrupted", fn_ident
);
323 error ("number of counters is %d instead of %d", entry
->summary
.num
, n_counts
);
328 else if (elt
.ctr
>= GCOV_COUNTERS_SUMMABLE
)
330 error ("cannot merge separate %s counters for function %u",
331 ctr_names
[elt
.ctr
], fn_ident
);
336 entry
->summary
.runs
+= summary
.ctrs
[elt
.ctr
].runs
;
337 entry
->summary
.sum_all
+= summary
.ctrs
[elt
.ctr
].sum_all
;
338 if (entry
->summary
.run_max
< summary
.ctrs
[elt
.ctr
].run_max
)
339 entry
->summary
.run_max
= summary
.ctrs
[elt
.ctr
].run_max
;
340 entry
->summary
.sum_max
+= summary
.ctrs
[elt
.ctr
].sum_max
;
342 for (ix
= 0; ix
!= n_counts
; ix
++)
343 entry
->counts
[ix
] += gcov_read_counter ();
346 gcov_sync (offset
, length
);
347 if ((is_error
= gcov_is_error ()))
349 error (is_error
< 0 ? "%qs has overflowed" : "%qs is corrupted",
360 /* Returns the counters for a particular tag. */
363 get_coverage_counts (unsigned counter
, unsigned expected
,
364 unsigned cfg_checksum
, unsigned lineno_checksum
,
365 const struct gcov_ctr_summary
**summary
)
367 counts_entry_t
*entry
, elt
;
369 /* No hash table, no counts. */
372 static int warned
= 0;
374 if (!warned
++ && dump_enabled_p ())
375 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
376 (flag_guess_branch_prob
377 ? "file %s not found, execution counts estimated\n"
378 : "file %s not found, execution counts assumed to "
383 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
384 elt
.ident
= current_function_funcdef_no
+ 1;
387 gcc_assert (coverage_node_map_initialized_p ());
388 elt
.ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
391 entry
= counts_hash
->find (&elt
);
392 if (!entry
|| !entry
->summary
.num
)
393 /* The function was not emitted, or is weak and not chosen in the
394 final executable. Silently fail, because there's nothing we
398 if (entry
->cfg_checksum
!= cfg_checksum
399 || entry
->summary
.num
!= expected
)
401 static int warned
= 0;
402 bool warning_printed
= false;
403 tree id
= DECL_ASSEMBLER_NAME (current_function_decl
);
406 warning_at (input_location
, OPT_Wcoverage_mismatch
,
407 "the control flow of function %qE does not match "
408 "its profile data (counter %qs)", id
, ctr_names
[counter
]);
409 if (warning_printed
&& dump_enabled_p ())
411 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
412 "use -Wno-error=coverage-mismatch to tolerate "
413 "the mismatch but performance may drop if the "
414 "function is hot\n");
419 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
420 "coverage mismatch ignored\n");
421 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
422 flag_guess_branch_prob
423 ? G_("execution counts estimated\n")
424 : G_("execution counts assumed to be zero\n"));
425 if (!flag_guess_branch_prob
)
426 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
427 "this can result in poorly optimized code\n");
433 else if (entry
->lineno_checksum
!= lineno_checksum
)
435 warning (OPT_Wcoverage_mismatch
,
436 "source locations for function %qE have changed,"
437 " the profile data may be out of date",
438 DECL_ASSEMBLER_NAME (current_function_decl
));
442 *summary
= &entry
->summary
;
444 return entry
->counts
;
447 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
448 allocation succeeded. */
451 coverage_counter_alloc (unsigned counter
, unsigned num
)
459 if (!fn_v_ctrs
[counter
])
461 tree array_type
= build_array_type (get_gcov_type (), NULL_TREE
);
464 = build_var (current_function_decl
, array_type
, counter
);
467 fn_b_ctrs
[counter
] = fn_n_ctrs
[counter
];
468 fn_n_ctrs
[counter
] += num
;
470 fn_ctr_mask
|= 1 << counter
;
474 /* Generate a tree to access COUNTER NO. */
477 tree_coverage_counter_ref (unsigned counter
, unsigned no
)
479 tree gcov_type_node
= get_gcov_type ();
481 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
483 no
+= fn_b_ctrs
[counter
];
485 /* "no" here is an array index, scaled to bytes later. */
486 return build4 (ARRAY_REF
, gcov_type_node
, fn_v_ctrs
[counter
],
487 build_int_cst (integer_type_node
, no
), NULL
, NULL
);
490 /* Generate a tree to access the address of COUNTER NO. */
493 tree_coverage_counter_addr (unsigned counter
, unsigned no
)
495 tree gcov_type_node
= get_gcov_type ();
497 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
498 no
+= fn_b_ctrs
[counter
];
500 /* "no" here is an array index, scaled to bytes later. */
501 return build_fold_addr_expr (build4 (ARRAY_REF
, gcov_type_node
,
503 build_int_cst (integer_type_node
, no
),
508 /* Generate a checksum for a string. CHKSUM is the current
512 coverage_checksum_string (unsigned chksum
, const char *string
)
517 /* Look for everything that looks if it were produced by
518 get_file_function_name and zero out the second part
519 that may result from flag_random_seed. This is not critical
520 as the checksums are used only for sanity checking. */
521 for (i
= 0; string
[i
]; i
++)
524 if (!strncmp (string
+ i
, "_GLOBAL__N_", 11))
526 if (!strncmp (string
+ i
, "_GLOBAL__", 9))
529 /* C++ namespaces do have scheme:
530 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
531 since filename might contain extra underscores there seems
532 to be no better chance then walk all possible offsets looking
536 for (i
= i
+ offset
; string
[i
]; i
++)
541 for (y
= 1; y
< 9; y
++)
542 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
543 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
545 if (y
!= 9 || string
[i
+ 9] != '_')
547 for (y
= 10; y
< 18; y
++)
548 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
549 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
554 string
= dup
= xstrdup (string
);
555 for (y
= 10; y
< 18; y
++)
562 chksum
= crc32_string (chksum
, string
);
568 /* Compute checksum for the current function. We generate a CRC32. */
571 coverage_compute_lineno_checksum (void)
573 expanded_location xloc
574 = expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
575 unsigned chksum
= xloc
.line
;
577 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
578 chksum
= coverage_checksum_string
579 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
)));
584 /* Compute profile ID. This is better to be unique in whole program. */
587 coverage_compute_profile_id (struct cgraph_node
*n
)
591 /* Externally visible symbols have unique name. */
592 if (TREE_PUBLIC (n
->decl
) || DECL_EXTERNAL (n
->decl
) || n
->unique_name
)
594 chksum
= coverage_checksum_string
595 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
599 expanded_location xloc
600 = expand_location (DECL_SOURCE_LOCATION (n
->decl
));
601 bool use_name_only
= (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
) == 0);
603 chksum
= (use_name_only
? 0 : xloc
.line
);
604 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
605 chksum
= coverage_checksum_string
606 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
607 if (!use_name_only
&& first_global_object_name
)
608 chksum
= coverage_checksum_string
609 (chksum
, first_global_object_name
);
610 chksum
= coverage_checksum_string
611 (chksum
, aux_base_name
);
614 /* Non-negative integers are hopefully small enough to fit in all targets.
615 Gcov file formats wants non-zero function IDs. */
616 chksum
= chksum
& 0x7fffffff;
617 return chksum
+ (!chksum
);
620 /* Compute cfg checksum for the function FN given as argument.
621 The checksum is calculated carefully so that
622 source code changes that doesn't affect the control flow graph
623 won't change the checksum.
624 This is to make the profile data useable across source code change.
625 The downside of this is that the compiler may use potentially
626 wrong profile data - that the source code change has non-trivial impact
627 on the validity of profile data (e.g. the reversed condition)
628 but the compiler won't detect the change and use the wrong profile data. */
631 coverage_compute_cfg_checksum (struct function
*fn
)
634 unsigned chksum
= n_basic_blocks_for_fn (fn
);
636 FOR_EACH_BB_FN (bb
, fn
)
640 chksum
= crc32_byte (chksum
, bb
->index
);
641 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
643 chksum
= crc32_byte (chksum
, e
->dest
->index
);
650 /* Begin output to the notes file for the current function.
651 Writes the function header. Returns nonzero if data should be output. */
654 coverage_begin_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
656 expanded_location xloc
;
657 unsigned long offset
;
659 /* We don't need to output .gcno file unless we're under -ftest-coverage
660 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
661 if (no_coverage
|| !bbg_file_name
)
664 xloc
= expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
666 /* Announce function */
667 offset
= gcov_write_tag (GCOV_TAG_FUNCTION
);
668 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
669 gcov_write_unsigned (current_function_funcdef_no
+ 1);
672 gcc_assert (coverage_node_map_initialized_p ());
673 gcov_write_unsigned (
674 cgraph_node::get (current_function_decl
)->profile_id
);
677 gcov_write_unsigned (lineno_checksum
);
678 gcov_write_unsigned (cfg_checksum
);
679 gcov_write_string (IDENTIFIER_POINTER
680 (DECL_ASSEMBLER_NAME (current_function_decl
)));
681 gcov_write_string (xloc
.file
);
682 gcov_write_unsigned (xloc
.line
);
683 gcov_write_length (offset
);
685 return !gcov_is_error ();
688 /* Finish coverage data for the current function. Verify no output
689 error has occurred. Save function coverage counts. */
692 coverage_end_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
696 if (bbg_file_name
&& gcov_is_error ())
698 warning (0, "error writing %qs", bbg_file_name
);
699 unlink (bbg_file_name
);
700 bbg_file_name
= NULL
;
705 struct coverage_data
*item
= 0;
707 item
= ggc_alloc
<coverage_data
> ();
709 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
710 item
->ident
= current_function_funcdef_no
+ 1;
713 gcc_assert (coverage_node_map_initialized_p ());
714 item
->ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
717 item
->lineno_checksum
= lineno_checksum
;
718 item
->cfg_checksum
= cfg_checksum
;
720 item
->fn_decl
= current_function_decl
;
722 *functions_tail
= item
;
723 functions_tail
= &item
->next
;
725 for (i
= 0; i
!= GCOV_COUNTERS
; i
++)
727 tree var
= fn_v_ctrs
[i
];
730 item
->ctr_vars
[i
] = var
;
733 tree array_type
= build_index_type (size_int (fn_n_ctrs
[i
] - 1));
734 array_type
= build_array_type (get_gcov_type (), array_type
);
735 TREE_TYPE (var
) = array_type
;
736 DECL_SIZE (var
) = TYPE_SIZE (array_type
);
737 DECL_SIZE_UNIT (var
) = TYPE_SIZE_UNIT (array_type
);
738 varpool_node::finalize_decl (var
);
741 fn_b_ctrs
[i
] = fn_n_ctrs
[i
] = 0;
742 fn_v_ctrs
[i
] = NULL_TREE
;
744 prg_ctr_mask
|= fn_ctr_mask
;
749 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
750 >= 0 it is a counter array, otherwise it is the function structure. */
753 build_var (tree fn_decl
, tree type
, int counter
)
755 tree var
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
, type
);
756 const char *fn_name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl
));
758 size_t fn_name_len
, len
;
760 fn_name
= targetm
.strip_name_encoding (fn_name
);
761 fn_name_len
= strlen (fn_name
);
762 buf
= XALLOCAVEC (char, fn_name_len
+ 8 + sizeof (int) * 3);
765 strcpy (buf
, "__gcov__");
767 sprintf (buf
, "__gcov%u_", counter
);
769 #ifndef NO_DOT_IN_LABEL
771 #elif !defined NO_DOLLAR_IN_LABEL
774 memcpy (buf
+ len
, fn_name
, fn_name_len
+ 1);
775 DECL_NAME (var
) = get_identifier (buf
);
776 TREE_STATIC (var
) = 1;
777 TREE_ADDRESSABLE (var
) = 1;
778 DECL_NONALIASED (var
) = 1;
779 DECL_ALIGN (var
) = TYPE_ALIGN (type
);
784 /* Creates the gcov_fn_info RECORD_TYPE. */
787 build_fn_info_type (tree type
, unsigned counters
, tree gcov_info_type
)
789 tree ctr_info
= lang_hooks
.types
.make_type (RECORD_TYPE
);
793 gcc_assert (counters
);
796 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
797 get_gcov_unsigned_t ());
800 /* ctr_info::values */
801 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
802 build_pointer_type (get_gcov_type ()));
803 DECL_CHAIN (field
) = fields
;
806 finish_builtin_struct (ctr_info
, "__gcov_ctr_info", fields
, NULL_TREE
);
809 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
810 build_pointer_type (build_qualified_type
811 (gcov_info_type
, TYPE_QUAL_CONST
)));
815 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
816 get_gcov_unsigned_t ());
817 DECL_CHAIN (field
) = fields
;
820 /* lineno_checksum */
821 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
822 get_gcov_unsigned_t ());
823 DECL_CHAIN (field
) = fields
;
827 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
828 get_gcov_unsigned_t ());
829 DECL_CHAIN (field
) = fields
;
832 array_type
= build_index_type (size_int (counters
- 1));
833 array_type
= build_array_type (ctr_info
, array_type
);
836 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
, array_type
);
837 DECL_CHAIN (field
) = fields
;
840 finish_builtin_struct (type
, "__gcov_fn_info", fields
, NULL_TREE
);
843 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
844 the coverage data for the function and TYPE is the gcov_fn_info
845 RECORD_TYPE. KEY is the object file key. */
848 build_fn_info (const struct coverage_data
*data
, tree type
, tree key
)
850 tree fields
= TYPE_FIELDS (type
);
853 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
854 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
857 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
858 build1 (ADDR_EXPR
, TREE_TYPE (fields
), key
));
859 fields
= DECL_CHAIN (fields
);
862 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
863 build_int_cstu (get_gcov_unsigned_t (),
865 fields
= DECL_CHAIN (fields
);
867 /* lineno_checksum */
868 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
869 build_int_cstu (get_gcov_unsigned_t (),
870 data
->lineno_checksum
));
871 fields
= DECL_CHAIN (fields
);
874 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
875 build_int_cstu (get_gcov_unsigned_t (),
876 data
->cfg_checksum
));
877 fields
= DECL_CHAIN (fields
);
880 ctr_type
= TREE_TYPE (TREE_TYPE (fields
));
881 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
882 if (prg_ctr_mask
& (1 << ix
))
884 vec
<constructor_elt
, va_gc
> *ctr
= NULL
;
885 tree var
= data
->ctr_vars
[ix
];
890 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var
))))
893 CONSTRUCTOR_APPEND_ELT (ctr
, TYPE_FIELDS (ctr_type
),
894 build_int_cstu (get_gcov_unsigned_t (),
898 CONSTRUCTOR_APPEND_ELT (ctr
, DECL_CHAIN (TYPE_FIELDS (ctr_type
)),
899 build_fold_addr_expr (var
));
901 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, build_constructor (ctr_type
, ctr
));
904 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
905 build_constructor (TREE_TYPE (fields
), v2
));
907 return build_constructor (type
, v1
);
910 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
911 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
914 build_info_type (tree type
, tree fn_info_ptr_type
)
916 tree field
, fields
= NULL_TREE
;
920 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
921 get_gcov_unsigned_t ());
922 DECL_CHAIN (field
) = fields
;
926 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
927 build_pointer_type (build_qualified_type
928 (type
, TYPE_QUAL_CONST
)));
929 DECL_CHAIN (field
) = fields
;
933 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
934 get_gcov_unsigned_t ());
935 DECL_CHAIN (field
) = fields
;
939 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
940 build_pointer_type (build_qualified_type
941 (char_type_node
, TYPE_QUAL_CONST
)));
942 DECL_CHAIN (field
) = fields
;
947 = build_function_type_list (void_type_node
,
948 build_pointer_type (get_gcov_type ()),
949 get_gcov_unsigned_t (), NULL_TREE
);
951 = build_array_type (build_pointer_type (merge_fn_type
),
952 build_index_type (size_int (GCOV_COUNTERS
- 1)));
953 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
955 DECL_CHAIN (field
) = fields
;
959 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
960 get_gcov_unsigned_t ());
961 DECL_CHAIN (field
) = fields
;
964 /* function_info pointer pointer */
965 fn_info_ptr_type
= build_pointer_type
966 (build_qualified_type (fn_info_ptr_type
, TYPE_QUAL_CONST
));
967 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
969 DECL_CHAIN (field
) = fields
;
972 finish_builtin_struct (type
, "__gcov_info", fields
, NULL_TREE
);
975 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
976 gcov_info structure type, FN_ARY is the array of pointers to
977 function info objects. */
980 build_info (tree info_type
, tree fn_ary
)
982 tree info_fields
= TYPE_FIELDS (info_type
);
983 tree merge_fn_type
, n_funcs
;
985 tree filename_string
;
986 int da_file_name_len
;
987 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
988 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
991 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
992 build_int_cstu (TREE_TYPE (info_fields
),
994 info_fields
= DECL_CHAIN (info_fields
);
997 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, null_pointer_node
);
998 info_fields
= DECL_CHAIN (info_fields
);
1001 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1002 build_int_cstu (TREE_TYPE (info_fields
),
1004 info_fields
= DECL_CHAIN (info_fields
);
1007 da_file_name_len
= strlen (da_file_name
);
1008 filename_string
= build_string (da_file_name_len
+ 1, da_file_name
);
1009 TREE_TYPE (filename_string
) = build_array_type
1010 (char_type_node
, build_index_type (size_int (da_file_name_len
)));
1011 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1012 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
),
1014 info_fields
= DECL_CHAIN (info_fields
);
1016 /* merge fn array -- NULL slots indicate unmeasured counters */
1017 merge_fn_type
= TREE_TYPE (TREE_TYPE (info_fields
));
1018 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1020 tree ptr
= null_pointer_node
;
1022 if ((1u << ix
) & prg_ctr_mask
)
1024 tree merge_fn
= build_decl (BUILTINS_LOCATION
,
1026 get_identifier (ctr_merge_functions
[ix
]),
1027 TREE_TYPE (merge_fn_type
));
1028 DECL_EXTERNAL (merge_fn
) = 1;
1029 TREE_PUBLIC (merge_fn
) = 1;
1030 DECL_ARTIFICIAL (merge_fn
) = 1;
1031 TREE_NOTHROW (merge_fn
) = 1;
1032 /* Initialize assembler name so we can stream out. */
1033 DECL_ASSEMBLER_NAME (merge_fn
);
1034 ptr
= build1 (ADDR_EXPR
, merge_fn_type
, merge_fn
);
1036 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, ptr
);
1038 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1039 build_constructor (TREE_TYPE (info_fields
), v2
));
1040 info_fields
= DECL_CHAIN (info_fields
);
1043 n_funcs
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary
)));
1044 n_funcs
= fold_build2 (PLUS_EXPR
, TREE_TYPE (info_fields
),
1045 n_funcs
, size_one_node
);
1046 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, n_funcs
);
1047 info_fields
= DECL_CHAIN (info_fields
);
1050 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1051 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
), fn_ary
));
1052 info_fields
= DECL_CHAIN (info_fields
);
1054 gcc_assert (!info_fields
);
1055 return build_constructor (info_type
, v1
);
1058 /* Generate the constructor function to call __gcov_init. */
1061 build_init_ctor (tree gcov_info_type
)
1063 tree ctor
, stmt
, init_fn
;
1065 /* Build a decl for __gcov_init. */
1066 init_fn
= build_pointer_type (gcov_info_type
);
1067 init_fn
= build_function_type_list (void_type_node
, init_fn
, NULL
);
1068 init_fn
= build_decl (BUILTINS_LOCATION
, FUNCTION_DECL
,
1069 get_identifier ("__gcov_init"), init_fn
);
1070 TREE_PUBLIC (init_fn
) = 1;
1071 DECL_EXTERNAL (init_fn
) = 1;
1072 DECL_ASSEMBLER_NAME (init_fn
);
1074 /* Generate a call to __gcov_init(&gcov_info). */
1076 stmt
= build_fold_addr_expr (gcov_info_var
);
1077 stmt
= build_call_expr (init_fn
, 1, stmt
);
1078 append_to_statement_list (stmt
, &ctor
);
1080 /* Generate a constructor to run it. */
1081 cgraph_build_static_cdtor ('I', ctor
, DEFAULT_INIT_PRIORITY
);
1084 /* Create the gcov_info types and object. Generate the constructor
1085 function to call __gcov_init. Does not generate the initializer
1086 for the object. Returns TRUE if coverage data is being emitted. */
1089 coverage_obj_init (void)
1091 tree gcov_info_type
;
1092 unsigned n_counters
= 0;
1094 struct coverage_data
*fn
;
1095 struct coverage_data
**fn_prev
;
1098 no_coverage
= 1; /* Disable any further coverage. */
1103 if (symtab
->dump_file
)
1104 fprintf (symtab
->dump_file
, "Using data file %s\n", da_file_name
);
1106 /* Prune functions. */
1107 for (fn_prev
= &functions_head
; (fn
= *fn_prev
);)
1108 if (DECL_STRUCT_FUNCTION (fn
->fn_decl
))
1109 fn_prev
= &fn
->next
;
1111 /* The function is not being emitted, remove from list. */
1112 *fn_prev
= fn
->next
;
1114 if (functions_head
== NULL
)
1117 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1118 if ((1u << ix
) & prg_ctr_mask
)
1121 /* Build the info and fn_info types. These are mutually recursive. */
1122 gcov_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1123 gcov_fn_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1124 gcov_fn_info_ptr_type
= build_pointer_type
1125 (build_qualified_type (gcov_fn_info_type
, TYPE_QUAL_CONST
));
1126 build_fn_info_type (gcov_fn_info_type
, n_counters
, gcov_info_type
);
1127 build_info_type (gcov_info_type
, gcov_fn_info_ptr_type
);
1129 /* Build the gcov info var, this is referred to in its own
1131 gcov_info_var
= build_decl (BUILTINS_LOCATION
,
1132 VAR_DECL
, NULL_TREE
, gcov_info_type
);
1133 TREE_STATIC (gcov_info_var
) = 1;
1134 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 0);
1135 DECL_NAME (gcov_info_var
) = get_identifier (name_buf
);
1137 build_init_ctor (gcov_info_type
);
1142 /* Generate the coverage function info for FN and DATA. Append a
1143 pointer to that object to CTOR and return the appended CTOR. */
1145 static vec
<constructor_elt
, va_gc
> *
1146 coverage_obj_fn (vec
<constructor_elt
, va_gc
> *ctor
, tree fn
,
1147 struct coverage_data
const *data
)
1149 tree init
= build_fn_info (data
, gcov_fn_info_type
, gcov_info_var
);
1150 tree var
= build_var (fn
, gcov_fn_info_type
, -1);
1152 DECL_INITIAL (var
) = init
;
1153 varpool_node::finalize_decl (var
);
1155 CONSTRUCTOR_APPEND_ELT (ctor
, NULL
,
1156 build1 (ADDR_EXPR
, gcov_fn_info_ptr_type
, var
));
1160 /* Finalize the coverage data. Generates the array of pointers to
1161 function objects from CTOR. Generate the gcov_info initializer. */
1164 coverage_obj_finish (vec
<constructor_elt
, va_gc
> *ctor
)
1166 unsigned n_functions
= vec_safe_length (ctor
);
1167 tree fn_info_ary_type
= build_array_type
1168 (build_qualified_type (gcov_fn_info_ptr_type
, TYPE_QUAL_CONST
),
1169 build_index_type (size_int (n_functions
- 1)));
1170 tree fn_info_ary
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
,
1174 TREE_STATIC (fn_info_ary
) = 1;
1175 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 1);
1176 DECL_NAME (fn_info_ary
) = get_identifier (name_buf
);
1177 DECL_INITIAL (fn_info_ary
) = build_constructor (fn_info_ary_type
, ctor
);
1178 varpool_node::finalize_decl (fn_info_ary
);
1180 DECL_INITIAL (gcov_info_var
)
1181 = build_info (TREE_TYPE (gcov_info_var
), fn_info_ary
);
1182 varpool_node::finalize_decl (gcov_info_var
);
1185 /* Perform file-level initialization. Read in data file, generate name
1189 coverage_init (const char *filename
)
1191 int len
= strlen (filename
);
1194 /* Since coverage_init is invoked very early, before the pass
1195 manager, we need to set up the dumping explicitly. This is
1196 similar to the handling in finish_optimization_passes. */
1197 int profile_pass_num
=
1198 g
->get_passes ()->get_pass_profile ()->static_pass_number
;
1199 g
->get_dumps ()->dump_start (profile_pass_num
, NULL
);
1201 if (!profile_data_prefix
&& !IS_ABSOLUTE_PATH (filename
))
1202 profile_data_prefix
= getpwd ();
1204 if (profile_data_prefix
)
1205 prefix_len
= strlen (profile_data_prefix
);
1207 /* Name of da file. */
1208 da_file_name
= XNEWVEC (char, len
+ strlen (GCOV_DATA_SUFFIX
)
1211 if (profile_data_prefix
)
1213 memcpy (da_file_name
, profile_data_prefix
, prefix_len
);
1214 da_file_name
[prefix_len
++] = '/';
1216 memcpy (da_file_name
+ prefix_len
, filename
, len
);
1217 strcpy (da_file_name
+ prefix_len
+ len
, GCOV_DATA_SUFFIX
);
1219 bbg_file_stamp
= local_tick
;
1221 if (flag_auto_profile
)
1222 read_autofdo_file ();
1223 else if (flag_branch_probabilities
)
1224 read_counts_file ();
1226 /* Name of bbg file. */
1227 if (flag_test_coverage
&& !flag_compare_debug
)
1229 bbg_file_name
= XNEWVEC (char, len
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1230 memcpy (bbg_file_name
, filename
, len
);
1231 strcpy (bbg_file_name
+ len
, GCOV_NOTE_SUFFIX
);
1233 if (!gcov_open (bbg_file_name
, -1))
1235 error ("cannot open %s", bbg_file_name
);
1236 bbg_file_name
= NULL
;
1240 gcov_write_unsigned (GCOV_NOTE_MAGIC
);
1241 gcov_write_unsigned (GCOV_VERSION
);
1242 gcov_write_unsigned (bbg_file_stamp
);
1246 g
->get_dumps ()->dump_finish (profile_pass_num
);
1249 /* Performs file-level cleanup. Close notes file, generate coverage
1250 variables and constructor. */
1253 coverage_finish (void)
1255 if (bbg_file_name
&& gcov_close ())
1256 unlink (bbg_file_name
);
1258 if (!flag_branch_probabilities
&& flag_test_coverage
1259 && (!local_tick
|| local_tick
== (unsigned)-1))
1260 /* Only remove the da file, if we're emitting coverage code and
1261 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1262 unlink (da_file_name
);
1264 if (coverage_obj_init ())
1266 vec
<constructor_elt
, va_gc
> *fn_ctor
= NULL
;
1267 struct coverage_data
*fn
;
1269 for (fn
= functions_head
; fn
; fn
= fn
->next
)
1270 fn_ctor
= coverage_obj_fn (fn_ctor
, fn
->fn_decl
, fn
);
1271 coverage_obj_finish (fn_ctor
);
1274 XDELETEVEC (da_file_name
);
1275 da_file_name
= NULL
;
1278 #include "gt-coverage.h"