1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990-2015 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"
35 #include "double-int.h"
42 #include "fold-const.h"
43 #include "stringpool.h"
44 #include "stor-layout.h"
49 #include "hard-reg-set.h"
51 #include "statistics.h"
53 #include "fixed-value.h"
54 #include "insn-config.h"
64 #include "dominance.h"
66 #include "basic-block.h"
71 #include "langhooks.h"
72 #include "hash-table.h"
73 #include "tree-iterator.h"
75 #include "pass_manager.h"
76 #include "tree-pass.h"
79 #include "plugin-api.h"
83 #include "diagnostic-core.h"
85 #include "filenames.h"
88 #include "auto-profile.h"
93 struct GTY((chain_next ("%h.next"))) coverage_data
95 struct coverage_data
*next
; /* next function */
96 unsigned ident
; /* function ident */
97 unsigned lineno_checksum
; /* function lineno checksum */
98 unsigned cfg_checksum
; /* function cfg checksum */
99 tree fn_decl
; /* the function decl */
100 tree ctr_vars
[GCOV_COUNTERS
]; /* counter variables. */
103 /* Counts information for a function. */
104 typedef struct counts_entry
111 unsigned lineno_checksum
;
112 unsigned cfg_checksum
;
114 struct gcov_ctr_summary summary
;
116 /* hash_table support. */
117 typedef counts_entry
*value_type
;
118 typedef counts_entry
*compare_type
;
119 static inline hashval_t
hash (const counts_entry
*);
120 static int equal (const counts_entry
*, const counts_entry
*);
121 static void remove (counts_entry
*);
124 static GTY(()) struct coverage_data
*functions_head
= 0;
125 static struct coverage_data
**functions_tail
= &functions_head
;
126 static unsigned no_coverage
= 0;
128 /* Cumulative counter information for whole program. */
129 static unsigned prg_ctr_mask
; /* Mask of counter types generated. */
131 /* Counter information for current function. */
132 static unsigned fn_ctr_mask
; /* Mask of counters used. */
133 static GTY(()) tree fn_v_ctrs
[GCOV_COUNTERS
]; /* counter variables. */
134 static unsigned fn_n_ctrs
[GCOV_COUNTERS
]; /* Counters allocated. */
135 static unsigned fn_b_ctrs
[GCOV_COUNTERS
]; /* Allocation base. */
137 /* Coverage info VAR_DECL and function info type nodes. */
138 static GTY(()) tree gcov_info_var
;
139 static GTY(()) tree gcov_fn_info_type
;
140 static GTY(()) tree gcov_fn_info_ptr_type
;
142 /* Name of the notes (gcno) output file. The "bbg" prefix is for
143 historical reasons, when the notes file contained only the
144 basic block graph notes.
145 If this is NULL we're not writing to the notes file. */
146 static char *bbg_file_name
;
148 /* File stamp for notes file. */
149 static unsigned bbg_file_stamp
;
151 /* Name of the count data (gcda) file. */
152 static char *da_file_name
;
154 /* The names of merge functions for counters. */
155 #define STR(str) #str
156 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
157 static const char *const ctr_merge_functions
[GCOV_COUNTERS
] = {
158 #include "gcov-counter.def"
160 #undef DEF_GCOV_COUNTER
163 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
164 static const char *const ctr_names
[GCOV_COUNTERS
] = {
165 #include "gcov-counter.def"
167 #undef DEF_GCOV_COUNTER
169 /* Forward declarations. */
170 static void read_counts_file (void);
171 static tree
build_var (tree
, tree
, int);
172 static void build_fn_info_type (tree
, unsigned, tree
);
173 static void build_info_type (tree
, tree
);
174 static tree
build_fn_info (const struct coverage_data
*, tree
, tree
);
175 static tree
build_info (tree
, tree
);
176 static bool coverage_obj_init (void);
177 static vec
<constructor_elt
, va_gc
> *coverage_obj_fn
178 (vec
<constructor_elt
, va_gc
> *, tree
, struct coverage_data
const *);
179 static void coverage_obj_finish (vec
<constructor_elt
, va_gc
> *);
181 /* Return the type node for gcov_type. */
186 machine_mode mode
= smallest_mode_for_size (GCOV_TYPE_SIZE
, MODE_INT
);
187 return lang_hooks
.types
.type_for_mode (mode
, false);
190 /* Return the type node for gcov_unsigned_t. */
193 get_gcov_unsigned_t (void)
195 machine_mode mode
= smallest_mode_for_size (32, MODE_INT
);
196 return lang_hooks
.types
.type_for_mode (mode
, true);
200 counts_entry::hash (const counts_entry
*entry
)
202 return entry
->ident
* GCOV_COUNTERS
+ entry
->ctr
;
206 counts_entry::equal (const counts_entry
*entry1
, const counts_entry
*entry2
)
208 return entry1
->ident
== entry2
->ident
&& entry1
->ctr
== entry2
->ctr
;
212 counts_entry::remove (counts_entry
*entry
)
214 free (entry
->counts
);
218 /* Hash table of count data. */
219 static hash_table
<counts_entry
> *counts_hash
;
221 /* Read in the counts file, if available. */
224 read_counts_file (void)
226 gcov_unsigned_t fn_ident
= 0;
227 struct gcov_summary summary
;
228 unsigned new_summary
= 1;
231 unsigned lineno_checksum
= 0;
232 unsigned cfg_checksum
= 0;
234 if (!gcov_open (da_file_name
, 1))
237 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
239 warning (0, "%qs is not a gcov data file", da_file_name
);
243 else if ((tag
= gcov_read_unsigned ()) != GCOV_VERSION
)
247 GCOV_UNSIGNED2STRING (v
, tag
);
248 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
250 warning (0, "%qs is version %q.*s, expected version %q.*s",
251 da_file_name
, 4, v
, 4, e
);
256 /* Read the stamp, used for creating a generation count. */
257 tag
= gcov_read_unsigned ();
258 bbg_file_stamp
= crc32_unsigned (bbg_file_stamp
, tag
);
260 counts_hash
= new hash_table
<counts_entry
> (10);
261 while ((tag
= gcov_read_unsigned ()))
263 gcov_unsigned_t length
;
264 gcov_position_t offset
;
266 length
= gcov_read_unsigned ();
267 offset
= gcov_position ();
268 if (tag
== GCOV_TAG_FUNCTION
)
272 fn_ident
= gcov_read_unsigned ();
273 lineno_checksum
= gcov_read_unsigned ();
274 cfg_checksum
= gcov_read_unsigned ();
277 fn_ident
= lineno_checksum
= cfg_checksum
= 0;
280 else if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
282 struct gcov_summary sum
;
286 memset (&summary
, 0, sizeof (summary
));
288 gcov_read_summary (&sum
);
289 for (ix
= 0; ix
!= GCOV_COUNTERS_SUMMABLE
; ix
++)
291 summary
.ctrs
[ix
].runs
+= sum
.ctrs
[ix
].runs
;
292 summary
.ctrs
[ix
].sum_all
+= sum
.ctrs
[ix
].sum_all
;
293 if (summary
.ctrs
[ix
].run_max
< sum
.ctrs
[ix
].run_max
)
294 summary
.ctrs
[ix
].run_max
= sum
.ctrs
[ix
].run_max
;
295 summary
.ctrs
[ix
].sum_max
+= sum
.ctrs
[ix
].sum_max
;
298 memcpy (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
299 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
300 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
302 gcov_histogram_merge (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
303 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
);
306 else if (GCOV_TAG_IS_COUNTER (tag
) && fn_ident
)
308 counts_entry_t
**slot
, *entry
, elt
;
309 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
312 elt
.ident
= fn_ident
;
313 elt
.ctr
= GCOV_COUNTER_FOR_TAG (tag
);
315 slot
= counts_hash
->find_slot (&elt
, INSERT
);
319 *slot
= entry
= XCNEW (counts_entry_t
);
320 entry
->ident
= fn_ident
;
321 entry
->ctr
= elt
.ctr
;
322 entry
->lineno_checksum
= lineno_checksum
;
323 entry
->cfg_checksum
= cfg_checksum
;
324 if (elt
.ctr
< GCOV_COUNTERS_SUMMABLE
)
325 entry
->summary
= summary
.ctrs
[elt
.ctr
];
326 entry
->summary
.num
= n_counts
;
327 entry
->counts
= XCNEWVEC (gcov_type
, n_counts
);
329 else if (entry
->lineno_checksum
!= lineno_checksum
330 || entry
->cfg_checksum
!= cfg_checksum
)
332 error ("Profile data for function %u is corrupted", fn_ident
);
333 error ("checksum is (%x,%x) instead of (%x,%x)",
334 entry
->lineno_checksum
, entry
->cfg_checksum
,
335 lineno_checksum
, cfg_checksum
);
340 else if (entry
->summary
.num
!= n_counts
)
342 error ("Profile data for function %u is corrupted", fn_ident
);
343 error ("number of counters is %d instead of %d", entry
->summary
.num
, n_counts
);
348 else if (elt
.ctr
>= GCOV_COUNTERS_SUMMABLE
)
350 error ("cannot merge separate %s counters for function %u",
351 ctr_names
[elt
.ctr
], fn_ident
);
356 entry
->summary
.runs
+= summary
.ctrs
[elt
.ctr
].runs
;
357 entry
->summary
.sum_all
+= summary
.ctrs
[elt
.ctr
].sum_all
;
358 if (entry
->summary
.run_max
< summary
.ctrs
[elt
.ctr
].run_max
)
359 entry
->summary
.run_max
= summary
.ctrs
[elt
.ctr
].run_max
;
360 entry
->summary
.sum_max
+= summary
.ctrs
[elt
.ctr
].sum_max
;
362 for (ix
= 0; ix
!= n_counts
; ix
++)
363 entry
->counts
[ix
] += gcov_read_counter ();
366 gcov_sync (offset
, length
);
367 if ((is_error
= gcov_is_error ()))
369 error (is_error
< 0 ? "%qs has overflowed" : "%qs is corrupted",
380 /* Returns the counters for a particular tag. */
383 get_coverage_counts (unsigned counter
, unsigned expected
,
384 unsigned cfg_checksum
, unsigned lineno_checksum
,
385 const struct gcov_ctr_summary
**summary
)
387 counts_entry_t
*entry
, elt
;
389 /* No hash table, no counts. */
392 static int warned
= 0;
394 if (!warned
++ && dump_enabled_p ())
395 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
396 (flag_guess_branch_prob
397 ? "file %s not found, execution counts estimated\n"
398 : "file %s not found, execution counts assumed to "
403 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
404 elt
.ident
= current_function_funcdef_no
+ 1;
407 gcc_assert (coverage_node_map_initialized_p ());
408 elt
.ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
411 entry
= counts_hash
->find (&elt
);
412 if (!entry
|| !entry
->summary
.num
)
413 /* The function was not emitted, or is weak and not chosen in the
414 final executable. Silently fail, because there's nothing we
418 if (entry
->cfg_checksum
!= cfg_checksum
419 || entry
->summary
.num
!= expected
)
421 static int warned
= 0;
422 bool warning_printed
= false;
423 tree id
= DECL_ASSEMBLER_NAME (current_function_decl
);
426 warning_at (input_location
, OPT_Wcoverage_mismatch
,
427 "the control flow of function %qE does not match "
428 "its profile data (counter %qs)", id
, ctr_names
[counter
]);
429 if (warning_printed
&& dump_enabled_p ())
431 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
432 "use -Wno-error=coverage-mismatch to tolerate "
433 "the mismatch but performance may drop if the "
434 "function is hot\n");
439 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
440 "coverage mismatch ignored\n");
441 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
442 flag_guess_branch_prob
443 ? G_("execution counts estimated\n")
444 : G_("execution counts assumed to be zero\n"));
445 if (!flag_guess_branch_prob
)
446 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
447 "this can result in poorly optimized code\n");
453 else if (entry
->lineno_checksum
!= lineno_checksum
)
455 warning (OPT_Wcoverage_mismatch
,
456 "source locations for function %qE have changed,"
457 " the profile data may be out of date",
458 DECL_ASSEMBLER_NAME (current_function_decl
));
462 *summary
= &entry
->summary
;
464 return entry
->counts
;
467 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
468 allocation succeeded. */
471 coverage_counter_alloc (unsigned counter
, unsigned num
)
479 if (!fn_v_ctrs
[counter
])
481 tree array_type
= build_array_type (get_gcov_type (), NULL_TREE
);
484 = build_var (current_function_decl
, array_type
, counter
);
487 fn_b_ctrs
[counter
] = fn_n_ctrs
[counter
];
488 fn_n_ctrs
[counter
] += num
;
490 fn_ctr_mask
|= 1 << counter
;
494 /* Generate a tree to access COUNTER NO. */
497 tree_coverage_counter_ref (unsigned counter
, unsigned no
)
499 tree gcov_type_node
= get_gcov_type ();
501 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
503 no
+= fn_b_ctrs
[counter
];
505 /* "no" here is an array index, scaled to bytes later. */
506 return build4 (ARRAY_REF
, gcov_type_node
, fn_v_ctrs
[counter
],
507 build_int_cst (integer_type_node
, no
), NULL
, NULL
);
510 /* Generate a tree to access the address of COUNTER NO. */
513 tree_coverage_counter_addr (unsigned counter
, unsigned no
)
515 tree gcov_type_node
= get_gcov_type ();
517 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
518 no
+= fn_b_ctrs
[counter
];
520 /* "no" here is an array index, scaled to bytes later. */
521 return build_fold_addr_expr (build4 (ARRAY_REF
, gcov_type_node
,
523 build_int_cst (integer_type_node
, no
),
528 /* Generate a checksum for a string. CHKSUM is the current
532 coverage_checksum_string (unsigned chksum
, const char *string
)
537 /* Look for everything that looks if it were produced by
538 get_file_function_name and zero out the second part
539 that may result from flag_random_seed. This is not critical
540 as the checksums are used only for sanity checking. */
541 for (i
= 0; string
[i
]; i
++)
544 if (!strncmp (string
+ i
, "_GLOBAL__N_", 11))
546 if (!strncmp (string
+ i
, "_GLOBAL__", 9))
549 /* C++ namespaces do have scheme:
550 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
551 since filename might contain extra underscores there seems
552 to be no better chance then walk all possible offsets looking
556 for (i
= i
+ offset
; string
[i
]; i
++)
561 for (y
= 1; y
< 9; y
++)
562 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
563 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
565 if (y
!= 9 || string
[i
+ 9] != '_')
567 for (y
= 10; y
< 18; y
++)
568 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
569 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
574 string
= dup
= xstrdup (string
);
575 for (y
= 10; y
< 18; y
++)
582 chksum
= crc32_string (chksum
, string
);
588 /* Compute checksum for the current function. We generate a CRC32. */
591 coverage_compute_lineno_checksum (void)
593 expanded_location xloc
594 = expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
595 unsigned chksum
= xloc
.line
;
597 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
598 chksum
= coverage_checksum_string
599 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
)));
604 /* Compute profile ID. This is better to be unique in whole program. */
607 coverage_compute_profile_id (struct cgraph_node
*n
)
611 /* Externally visible symbols have unique name. */
612 if (TREE_PUBLIC (n
->decl
) || DECL_EXTERNAL (n
->decl
) || n
->unique_name
)
614 chksum
= coverage_checksum_string
615 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
619 expanded_location xloc
620 = expand_location (DECL_SOURCE_LOCATION (n
->decl
));
621 bool use_name_only
= (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
) == 0);
623 chksum
= (use_name_only
? 0 : xloc
.line
);
624 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
625 chksum
= coverage_checksum_string
626 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
627 if (!use_name_only
&& first_global_object_name
)
628 chksum
= coverage_checksum_string
629 (chksum
, first_global_object_name
);
630 chksum
= coverage_checksum_string
631 (chksum
, aux_base_name
);
634 /* Non-negative integers are hopefully small enough to fit in all targets.
635 Gcov file formats wants non-zero function IDs. */
636 chksum
= chksum
& 0x7fffffff;
637 return chksum
+ (!chksum
);
640 /* Compute cfg checksum for the function FN given as argument.
641 The checksum is calculated carefully so that
642 source code changes that doesn't affect the control flow graph
643 won't change the checksum.
644 This is to make the profile data useable across source code change.
645 The downside of this is that the compiler may use potentially
646 wrong profile data - that the source code change has non-trivial impact
647 on the validity of profile data (e.g. the reversed condition)
648 but the compiler won't detect the change and use the wrong profile data. */
651 coverage_compute_cfg_checksum (struct function
*fn
)
654 unsigned chksum
= n_basic_blocks_for_fn (fn
);
656 FOR_EACH_BB_FN (bb
, fn
)
660 chksum
= crc32_byte (chksum
, bb
->index
);
661 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
663 chksum
= crc32_byte (chksum
, e
->dest
->index
);
670 /* Begin output to the notes file for the current function.
671 Writes the function header. Returns nonzero if data should be output. */
674 coverage_begin_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
676 expanded_location xloc
;
677 unsigned long offset
;
679 /* We don't need to output .gcno file unless we're under -ftest-coverage
680 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
681 if (no_coverage
|| !bbg_file_name
)
684 xloc
= expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
686 /* Announce function */
687 offset
= gcov_write_tag (GCOV_TAG_FUNCTION
);
688 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
689 gcov_write_unsigned (current_function_funcdef_no
+ 1);
692 gcc_assert (coverage_node_map_initialized_p ());
693 gcov_write_unsigned (
694 cgraph_node::get (current_function_decl
)->profile_id
);
697 gcov_write_unsigned (lineno_checksum
);
698 gcov_write_unsigned (cfg_checksum
);
699 gcov_write_string (IDENTIFIER_POINTER
700 (DECL_ASSEMBLER_NAME (current_function_decl
)));
701 gcov_write_string (xloc
.file
);
702 gcov_write_unsigned (xloc
.line
);
703 gcov_write_length (offset
);
705 return !gcov_is_error ();
708 /* Finish coverage data for the current function. Verify no output
709 error has occurred. Save function coverage counts. */
712 coverage_end_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
716 if (bbg_file_name
&& gcov_is_error ())
718 warning (0, "error writing %qs", bbg_file_name
);
719 unlink (bbg_file_name
);
720 bbg_file_name
= NULL
;
725 struct coverage_data
*item
= 0;
727 item
= ggc_alloc
<coverage_data
> ();
729 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
730 item
->ident
= current_function_funcdef_no
+ 1;
733 gcc_assert (coverage_node_map_initialized_p ());
734 item
->ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
737 item
->lineno_checksum
= lineno_checksum
;
738 item
->cfg_checksum
= cfg_checksum
;
740 item
->fn_decl
= current_function_decl
;
742 *functions_tail
= item
;
743 functions_tail
= &item
->next
;
745 for (i
= 0; i
!= GCOV_COUNTERS
; i
++)
747 tree var
= fn_v_ctrs
[i
];
750 item
->ctr_vars
[i
] = var
;
753 tree array_type
= build_index_type (size_int (fn_n_ctrs
[i
] - 1));
754 array_type
= build_array_type (get_gcov_type (), array_type
);
755 TREE_TYPE (var
) = array_type
;
756 DECL_SIZE (var
) = TYPE_SIZE (array_type
);
757 DECL_SIZE_UNIT (var
) = TYPE_SIZE_UNIT (array_type
);
758 varpool_node::finalize_decl (var
);
761 fn_b_ctrs
[i
] = fn_n_ctrs
[i
] = 0;
762 fn_v_ctrs
[i
] = NULL_TREE
;
764 prg_ctr_mask
|= fn_ctr_mask
;
769 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
770 >= 0 it is a counter array, otherwise it is the function structure. */
773 build_var (tree fn_decl
, tree type
, int counter
)
775 tree var
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
, type
);
776 const char *fn_name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl
));
778 size_t fn_name_len
, len
;
780 fn_name
= targetm
.strip_name_encoding (fn_name
);
781 fn_name_len
= strlen (fn_name
);
782 buf
= XALLOCAVEC (char, fn_name_len
+ 8 + sizeof (int) * 3);
785 strcpy (buf
, "__gcov__");
787 sprintf (buf
, "__gcov%u_", counter
);
789 #ifndef NO_DOT_IN_LABEL
791 #elif !defined NO_DOLLAR_IN_LABEL
794 memcpy (buf
+ len
, fn_name
, fn_name_len
+ 1);
795 DECL_NAME (var
) = get_identifier (buf
);
796 TREE_STATIC (var
) = 1;
797 TREE_ADDRESSABLE (var
) = 1;
798 DECL_NONALIASED (var
) = 1;
799 DECL_ALIGN (var
) = TYPE_ALIGN (type
);
804 /* Creates the gcov_fn_info RECORD_TYPE. */
807 build_fn_info_type (tree type
, unsigned counters
, tree gcov_info_type
)
809 tree ctr_info
= lang_hooks
.types
.make_type (RECORD_TYPE
);
813 gcc_assert (counters
);
816 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
817 get_gcov_unsigned_t ());
820 /* ctr_info::values */
821 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
822 build_pointer_type (get_gcov_type ()));
823 DECL_CHAIN (field
) = fields
;
826 finish_builtin_struct (ctr_info
, "__gcov_ctr_info", fields
, NULL_TREE
);
829 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
830 build_pointer_type (build_qualified_type
831 (gcov_info_type
, TYPE_QUAL_CONST
)));
835 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
836 get_gcov_unsigned_t ());
837 DECL_CHAIN (field
) = fields
;
840 /* lineno_checksum */
841 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
842 get_gcov_unsigned_t ());
843 DECL_CHAIN (field
) = fields
;
847 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
848 get_gcov_unsigned_t ());
849 DECL_CHAIN (field
) = fields
;
852 array_type
= build_index_type (size_int (counters
- 1));
853 array_type
= build_array_type (ctr_info
, array_type
);
856 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
, array_type
);
857 DECL_CHAIN (field
) = fields
;
860 finish_builtin_struct (type
, "__gcov_fn_info", fields
, NULL_TREE
);
863 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
864 the coverage data for the function and TYPE is the gcov_fn_info
865 RECORD_TYPE. KEY is the object file key. */
868 build_fn_info (const struct coverage_data
*data
, tree type
, tree key
)
870 tree fields
= TYPE_FIELDS (type
);
873 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
874 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
877 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
878 build1 (ADDR_EXPR
, TREE_TYPE (fields
), key
));
879 fields
= DECL_CHAIN (fields
);
882 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
883 build_int_cstu (get_gcov_unsigned_t (),
885 fields
= DECL_CHAIN (fields
);
887 /* lineno_checksum */
888 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
889 build_int_cstu (get_gcov_unsigned_t (),
890 data
->lineno_checksum
));
891 fields
= DECL_CHAIN (fields
);
894 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
895 build_int_cstu (get_gcov_unsigned_t (),
896 data
->cfg_checksum
));
897 fields
= DECL_CHAIN (fields
);
900 ctr_type
= TREE_TYPE (TREE_TYPE (fields
));
901 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
902 if (prg_ctr_mask
& (1 << ix
))
904 vec
<constructor_elt
, va_gc
> *ctr
= NULL
;
905 tree var
= data
->ctr_vars
[ix
];
910 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var
))))
913 CONSTRUCTOR_APPEND_ELT (ctr
, TYPE_FIELDS (ctr_type
),
914 build_int_cstu (get_gcov_unsigned_t (),
918 CONSTRUCTOR_APPEND_ELT (ctr
, DECL_CHAIN (TYPE_FIELDS (ctr_type
)),
919 build_fold_addr_expr (var
));
921 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, build_constructor (ctr_type
, ctr
));
924 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
925 build_constructor (TREE_TYPE (fields
), v2
));
927 return build_constructor (type
, v1
);
930 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
931 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
934 build_info_type (tree type
, tree fn_info_ptr_type
)
936 tree field
, fields
= NULL_TREE
;
940 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
941 get_gcov_unsigned_t ());
942 DECL_CHAIN (field
) = fields
;
946 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
947 build_pointer_type (build_qualified_type
948 (type
, TYPE_QUAL_CONST
)));
949 DECL_CHAIN (field
) = fields
;
953 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
954 get_gcov_unsigned_t ());
955 DECL_CHAIN (field
) = fields
;
959 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
960 build_pointer_type (build_qualified_type
961 (char_type_node
, TYPE_QUAL_CONST
)));
962 DECL_CHAIN (field
) = fields
;
967 = build_function_type_list (void_type_node
,
968 build_pointer_type (get_gcov_type ()),
969 get_gcov_unsigned_t (), NULL_TREE
);
971 = build_array_type (build_pointer_type (merge_fn_type
),
972 build_index_type (size_int (GCOV_COUNTERS
- 1)));
973 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
975 DECL_CHAIN (field
) = fields
;
979 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
980 get_gcov_unsigned_t ());
981 DECL_CHAIN (field
) = fields
;
984 /* function_info pointer pointer */
985 fn_info_ptr_type
= build_pointer_type
986 (build_qualified_type (fn_info_ptr_type
, TYPE_QUAL_CONST
));
987 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
989 DECL_CHAIN (field
) = fields
;
992 finish_builtin_struct (type
, "__gcov_info", fields
, NULL_TREE
);
995 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
996 gcov_info structure type, FN_ARY is the array of pointers to
997 function info objects. */
1000 build_info (tree info_type
, tree fn_ary
)
1002 tree info_fields
= TYPE_FIELDS (info_type
);
1003 tree merge_fn_type
, n_funcs
;
1005 tree filename_string
;
1006 int da_file_name_len
;
1007 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
1008 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
1011 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1012 build_int_cstu (TREE_TYPE (info_fields
),
1014 info_fields
= DECL_CHAIN (info_fields
);
1017 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, null_pointer_node
);
1018 info_fields
= DECL_CHAIN (info_fields
);
1021 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1022 build_int_cstu (TREE_TYPE (info_fields
),
1024 info_fields
= DECL_CHAIN (info_fields
);
1027 da_file_name_len
= strlen (da_file_name
);
1028 filename_string
= build_string (da_file_name_len
+ 1, da_file_name
);
1029 TREE_TYPE (filename_string
) = build_array_type
1030 (char_type_node
, build_index_type (size_int (da_file_name_len
)));
1031 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1032 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
),
1034 info_fields
= DECL_CHAIN (info_fields
);
1036 /* merge fn array -- NULL slots indicate unmeasured counters */
1037 merge_fn_type
= TREE_TYPE (TREE_TYPE (info_fields
));
1038 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1040 tree ptr
= null_pointer_node
;
1042 if ((1u << ix
) & prg_ctr_mask
)
1044 tree merge_fn
= build_decl (BUILTINS_LOCATION
,
1046 get_identifier (ctr_merge_functions
[ix
]),
1047 TREE_TYPE (merge_fn_type
));
1048 DECL_EXTERNAL (merge_fn
) = 1;
1049 TREE_PUBLIC (merge_fn
) = 1;
1050 DECL_ARTIFICIAL (merge_fn
) = 1;
1051 TREE_NOTHROW (merge_fn
) = 1;
1052 /* Initialize assembler name so we can stream out. */
1053 DECL_ASSEMBLER_NAME (merge_fn
);
1054 ptr
= build1 (ADDR_EXPR
, merge_fn_type
, merge_fn
);
1056 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, ptr
);
1058 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1059 build_constructor (TREE_TYPE (info_fields
), v2
));
1060 info_fields
= DECL_CHAIN (info_fields
);
1063 n_funcs
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary
)));
1064 n_funcs
= fold_build2 (PLUS_EXPR
, TREE_TYPE (info_fields
),
1065 n_funcs
, size_one_node
);
1066 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, n_funcs
);
1067 info_fields
= DECL_CHAIN (info_fields
);
1070 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1071 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
), fn_ary
));
1072 info_fields
= DECL_CHAIN (info_fields
);
1074 gcc_assert (!info_fields
);
1075 return build_constructor (info_type
, v1
);
1078 /* Generate the constructor function to call __gcov_init. */
1081 build_init_ctor (tree gcov_info_type
)
1083 tree ctor
, stmt
, init_fn
;
1085 /* Build a decl for __gcov_init. */
1086 init_fn
= build_pointer_type (gcov_info_type
);
1087 init_fn
= build_function_type_list (void_type_node
, init_fn
, NULL
);
1088 init_fn
= build_decl (BUILTINS_LOCATION
, FUNCTION_DECL
,
1089 get_identifier ("__gcov_init"), init_fn
);
1090 TREE_PUBLIC (init_fn
) = 1;
1091 DECL_EXTERNAL (init_fn
) = 1;
1092 DECL_ASSEMBLER_NAME (init_fn
);
1094 /* Generate a call to __gcov_init(&gcov_info). */
1096 stmt
= build_fold_addr_expr (gcov_info_var
);
1097 stmt
= build_call_expr (init_fn
, 1, stmt
);
1098 append_to_statement_list (stmt
, &ctor
);
1100 /* Generate a constructor to run it. */
1101 cgraph_build_static_cdtor ('I', ctor
, DEFAULT_INIT_PRIORITY
);
1104 /* Create the gcov_info types and object. Generate the constructor
1105 function to call __gcov_init. Does not generate the initializer
1106 for the object. Returns TRUE if coverage data is being emitted. */
1109 coverage_obj_init (void)
1111 tree gcov_info_type
;
1112 unsigned n_counters
= 0;
1114 struct coverage_data
*fn
;
1115 struct coverage_data
**fn_prev
;
1118 no_coverage
= 1; /* Disable any further coverage. */
1123 if (symtab
->dump_file
)
1124 fprintf (symtab
->dump_file
, "Using data file %s\n", da_file_name
);
1126 /* Prune functions. */
1127 for (fn_prev
= &functions_head
; (fn
= *fn_prev
);)
1128 if (DECL_STRUCT_FUNCTION (fn
->fn_decl
))
1129 fn_prev
= &fn
->next
;
1131 /* The function is not being emitted, remove from list. */
1132 *fn_prev
= fn
->next
;
1134 if (functions_head
== NULL
)
1137 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1138 if ((1u << ix
) & prg_ctr_mask
)
1141 /* Build the info and fn_info types. These are mutually recursive. */
1142 gcov_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1143 gcov_fn_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1144 gcov_fn_info_ptr_type
= build_pointer_type
1145 (build_qualified_type (gcov_fn_info_type
, TYPE_QUAL_CONST
));
1146 build_fn_info_type (gcov_fn_info_type
, n_counters
, gcov_info_type
);
1147 build_info_type (gcov_info_type
, gcov_fn_info_ptr_type
);
1149 /* Build the gcov info var, this is referred to in its own
1151 gcov_info_var
= build_decl (BUILTINS_LOCATION
,
1152 VAR_DECL
, NULL_TREE
, gcov_info_type
);
1153 TREE_STATIC (gcov_info_var
) = 1;
1154 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 0);
1155 DECL_NAME (gcov_info_var
) = get_identifier (name_buf
);
1157 build_init_ctor (gcov_info_type
);
1162 /* Generate the coverage function info for FN and DATA. Append a
1163 pointer to that object to CTOR and return the appended CTOR. */
1165 static vec
<constructor_elt
, va_gc
> *
1166 coverage_obj_fn (vec
<constructor_elt
, va_gc
> *ctor
, tree fn
,
1167 struct coverage_data
const *data
)
1169 tree init
= build_fn_info (data
, gcov_fn_info_type
, gcov_info_var
);
1170 tree var
= build_var (fn
, gcov_fn_info_type
, -1);
1172 DECL_INITIAL (var
) = init
;
1173 varpool_node::finalize_decl (var
);
1175 CONSTRUCTOR_APPEND_ELT (ctor
, NULL
,
1176 build1 (ADDR_EXPR
, gcov_fn_info_ptr_type
, var
));
1180 /* Finalize the coverage data. Generates the array of pointers to
1181 function objects from CTOR. Generate the gcov_info initializer. */
1184 coverage_obj_finish (vec
<constructor_elt
, va_gc
> *ctor
)
1186 unsigned n_functions
= vec_safe_length (ctor
);
1187 tree fn_info_ary_type
= build_array_type
1188 (build_qualified_type (gcov_fn_info_ptr_type
, TYPE_QUAL_CONST
),
1189 build_index_type (size_int (n_functions
- 1)));
1190 tree fn_info_ary
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
,
1194 TREE_STATIC (fn_info_ary
) = 1;
1195 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 1);
1196 DECL_NAME (fn_info_ary
) = get_identifier (name_buf
);
1197 DECL_INITIAL (fn_info_ary
) = build_constructor (fn_info_ary_type
, ctor
);
1198 varpool_node::finalize_decl (fn_info_ary
);
1200 DECL_INITIAL (gcov_info_var
)
1201 = build_info (TREE_TYPE (gcov_info_var
), fn_info_ary
);
1202 varpool_node::finalize_decl (gcov_info_var
);
1205 /* Perform file-level initialization. Read in data file, generate name
1209 coverage_init (const char *filename
)
1211 int len
= strlen (filename
);
1214 /* Since coverage_init is invoked very early, before the pass
1215 manager, we need to set up the dumping explicitly. This is
1216 similar to the handling in finish_optimization_passes. */
1217 int profile_pass_num
=
1218 g
->get_passes ()->get_pass_profile ()->static_pass_number
;
1219 g
->get_dumps ()->dump_start (profile_pass_num
, NULL
);
1221 if (!profile_data_prefix
&& !IS_ABSOLUTE_PATH (filename
))
1222 profile_data_prefix
= getpwd ();
1224 if (profile_data_prefix
)
1225 prefix_len
= strlen (profile_data_prefix
);
1227 /* Name of da file. */
1228 da_file_name
= XNEWVEC (char, len
+ strlen (GCOV_DATA_SUFFIX
)
1231 if (profile_data_prefix
)
1233 memcpy (da_file_name
, profile_data_prefix
, prefix_len
);
1234 da_file_name
[prefix_len
++] = '/';
1236 memcpy (da_file_name
+ prefix_len
, filename
, len
);
1237 strcpy (da_file_name
+ prefix_len
+ len
, GCOV_DATA_SUFFIX
);
1239 bbg_file_stamp
= local_tick
;
1241 if (flag_auto_profile
)
1242 read_autofdo_file ();
1243 else if (flag_branch_probabilities
)
1244 read_counts_file ();
1246 /* Name of bbg file. */
1247 if (flag_test_coverage
&& !flag_compare_debug
)
1249 bbg_file_name
= XNEWVEC (char, len
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1250 memcpy (bbg_file_name
, filename
, len
);
1251 strcpy (bbg_file_name
+ len
, GCOV_NOTE_SUFFIX
);
1253 if (!gcov_open (bbg_file_name
, -1))
1255 error ("cannot open %s", bbg_file_name
);
1256 bbg_file_name
= NULL
;
1260 gcov_write_unsigned (GCOV_NOTE_MAGIC
);
1261 gcov_write_unsigned (GCOV_VERSION
);
1262 gcov_write_unsigned (bbg_file_stamp
);
1266 g
->get_dumps ()->dump_finish (profile_pass_num
);
1269 /* Performs file-level cleanup. Close notes file, generate coverage
1270 variables and constructor. */
1273 coverage_finish (void)
1275 if (bbg_file_name
&& gcov_close ())
1276 unlink (bbg_file_name
);
1278 if (!flag_branch_probabilities
&& flag_test_coverage
1279 && (!local_tick
|| local_tick
== (unsigned)-1))
1280 /* Only remove the da file, if we're emitting coverage code and
1281 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1282 unlink (da_file_name
);
1284 if (coverage_obj_init ())
1286 vec
<constructor_elt
, va_gc
> *fn_ctor
= NULL
;
1287 struct coverage_data
*fn
;
1289 for (fn
= functions_head
; fn
; fn
= fn
->next
)
1290 fn_ctor
= coverage_obj_fn (fn_ctor
, fn
->fn_decl
, fn
);
1291 coverage_obj_finish (fn_ctor
);
1294 XDELETEVEC (da_file_name
);
1295 da_file_name
= NULL
;
1298 #include "gt-coverage.h"