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"
36 #include "fold-const.h"
37 #include "stringpool.h"
38 #include "stor-layout.h"
42 #include "hard-reg-set.h"
44 #include "insn-config.h"
54 #include "dominance.h"
56 #include "basic-block.h"
60 #include "langhooks.h"
61 #include "tree-iterator.h"
63 #include "pass_manager.h"
64 #include "tree-pass.h"
66 #include "plugin-api.h"
70 #include "diagnostic-core.h"
72 #include "filenames.h"
75 #include "auto-profile.h"
80 struct GTY((chain_next ("%h.next"))) coverage_data
82 struct coverage_data
*next
; /* next function */
83 unsigned ident
; /* function ident */
84 unsigned lineno_checksum
; /* function lineno checksum */
85 unsigned cfg_checksum
; /* function cfg checksum */
86 tree fn_decl
; /* the function decl */
87 tree ctr_vars
[GCOV_COUNTERS
]; /* counter variables. */
90 /* Counts information for a function. */
91 typedef struct counts_entry
98 unsigned lineno_checksum
;
99 unsigned cfg_checksum
;
101 struct gcov_ctr_summary summary
;
103 /* hash_table support. */
104 typedef counts_entry
*value_type
;
105 typedef counts_entry
*compare_type
;
106 static inline hashval_t
hash (const counts_entry
*);
107 static int equal (const counts_entry
*, const counts_entry
*);
108 static void remove (counts_entry
*);
111 static GTY(()) struct coverage_data
*functions_head
= 0;
112 static struct coverage_data
**functions_tail
= &functions_head
;
113 static unsigned no_coverage
= 0;
115 /* Cumulative counter information for whole program. */
116 static unsigned prg_ctr_mask
; /* Mask of counter types generated. */
118 /* Counter information for current function. */
119 static unsigned fn_ctr_mask
; /* Mask of counters used. */
120 static GTY(()) tree fn_v_ctrs
[GCOV_COUNTERS
]; /* counter variables. */
121 static unsigned fn_n_ctrs
[GCOV_COUNTERS
]; /* Counters allocated. */
122 static unsigned fn_b_ctrs
[GCOV_COUNTERS
]; /* Allocation base. */
124 /* Coverage info VAR_DECL and function info type nodes. */
125 static GTY(()) tree gcov_info_var
;
126 static GTY(()) tree gcov_fn_info_type
;
127 static GTY(()) tree gcov_fn_info_ptr_type
;
129 /* Name of the notes (gcno) output file. The "bbg" prefix is for
130 historical reasons, when the notes file contained only the
131 basic block graph notes.
132 If this is NULL we're not writing to the notes file. */
133 static char *bbg_file_name
;
135 /* File stamp for notes file. */
136 static unsigned bbg_file_stamp
;
138 /* Name of the count data (gcda) file. */
139 static char *da_file_name
;
141 /* The names of merge functions for counters. */
142 #define STR(str) #str
143 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
144 static const char *const ctr_merge_functions
[GCOV_COUNTERS
] = {
145 #include "gcov-counter.def"
147 #undef DEF_GCOV_COUNTER
150 #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
151 static const char *const ctr_names
[GCOV_COUNTERS
] = {
152 #include "gcov-counter.def"
154 #undef DEF_GCOV_COUNTER
156 /* Forward declarations. */
157 static void read_counts_file (void);
158 static tree
build_var (tree
, tree
, int);
159 static void build_fn_info_type (tree
, unsigned, tree
);
160 static void build_info_type (tree
, tree
);
161 static tree
build_fn_info (const struct coverage_data
*, tree
, tree
);
162 static tree
build_info (tree
, tree
);
163 static bool coverage_obj_init (void);
164 static vec
<constructor_elt
, va_gc
> *coverage_obj_fn
165 (vec
<constructor_elt
, va_gc
> *, tree
, struct coverage_data
const *);
166 static void coverage_obj_finish (vec
<constructor_elt
, va_gc
> *);
168 /* Return the type node for gcov_type. */
173 machine_mode mode
= smallest_mode_for_size (GCOV_TYPE_SIZE
, MODE_INT
);
174 return lang_hooks
.types
.type_for_mode (mode
, false);
177 /* Return the type node for gcov_unsigned_t. */
180 get_gcov_unsigned_t (void)
182 machine_mode mode
= smallest_mode_for_size (32, MODE_INT
);
183 return lang_hooks
.types
.type_for_mode (mode
, true);
187 counts_entry::hash (const counts_entry
*entry
)
189 return entry
->ident
* GCOV_COUNTERS
+ entry
->ctr
;
193 counts_entry::equal (const counts_entry
*entry1
, const counts_entry
*entry2
)
195 return entry1
->ident
== entry2
->ident
&& entry1
->ctr
== entry2
->ctr
;
199 counts_entry::remove (counts_entry
*entry
)
201 free (entry
->counts
);
205 /* Hash table of count data. */
206 static hash_table
<counts_entry
> *counts_hash
;
208 /* Read in the counts file, if available. */
211 read_counts_file (void)
213 gcov_unsigned_t fn_ident
= 0;
214 struct gcov_summary summary
;
215 unsigned new_summary
= 1;
218 unsigned lineno_checksum
= 0;
219 unsigned cfg_checksum
= 0;
221 if (!gcov_open (da_file_name
, 1))
224 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
226 warning (0, "%qs is not a gcov data file", da_file_name
);
230 else if ((tag
= gcov_read_unsigned ()) != GCOV_VERSION
)
234 GCOV_UNSIGNED2STRING (v
, tag
);
235 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
237 warning (0, "%qs is version %q.*s, expected version %q.*s",
238 da_file_name
, 4, v
, 4, e
);
243 /* Read the stamp, used for creating a generation count. */
244 tag
= gcov_read_unsigned ();
245 bbg_file_stamp
= crc32_unsigned (bbg_file_stamp
, tag
);
247 counts_hash
= new hash_table
<counts_entry
> (10);
248 while ((tag
= gcov_read_unsigned ()))
250 gcov_unsigned_t length
;
251 gcov_position_t offset
;
253 length
= gcov_read_unsigned ();
254 offset
= gcov_position ();
255 if (tag
== GCOV_TAG_FUNCTION
)
259 fn_ident
= gcov_read_unsigned ();
260 lineno_checksum
= gcov_read_unsigned ();
261 cfg_checksum
= gcov_read_unsigned ();
264 fn_ident
= lineno_checksum
= cfg_checksum
= 0;
267 else if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
269 struct gcov_summary sum
;
273 memset (&summary
, 0, sizeof (summary
));
275 gcov_read_summary (&sum
);
276 for (ix
= 0; ix
!= GCOV_COUNTERS_SUMMABLE
; ix
++)
278 summary
.ctrs
[ix
].runs
+= sum
.ctrs
[ix
].runs
;
279 summary
.ctrs
[ix
].sum_all
+= sum
.ctrs
[ix
].sum_all
;
280 if (summary
.ctrs
[ix
].run_max
< sum
.ctrs
[ix
].run_max
)
281 summary
.ctrs
[ix
].run_max
= sum
.ctrs
[ix
].run_max
;
282 summary
.ctrs
[ix
].sum_max
+= sum
.ctrs
[ix
].sum_max
;
285 memcpy (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
286 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
287 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
289 gcov_histogram_merge (summary
.ctrs
[GCOV_COUNTER_ARCS
].histogram
,
290 sum
.ctrs
[GCOV_COUNTER_ARCS
].histogram
);
293 else if (GCOV_TAG_IS_COUNTER (tag
) && fn_ident
)
295 counts_entry_t
**slot
, *entry
, elt
;
296 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
299 elt
.ident
= fn_ident
;
300 elt
.ctr
= GCOV_COUNTER_FOR_TAG (tag
);
302 slot
= counts_hash
->find_slot (&elt
, INSERT
);
306 *slot
= entry
= XCNEW (counts_entry_t
);
307 entry
->ident
= fn_ident
;
308 entry
->ctr
= elt
.ctr
;
309 entry
->lineno_checksum
= lineno_checksum
;
310 entry
->cfg_checksum
= cfg_checksum
;
311 if (elt
.ctr
< GCOV_COUNTERS_SUMMABLE
)
312 entry
->summary
= summary
.ctrs
[elt
.ctr
];
313 entry
->summary
.num
= n_counts
;
314 entry
->counts
= XCNEWVEC (gcov_type
, n_counts
);
316 else if (entry
->lineno_checksum
!= lineno_checksum
317 || entry
->cfg_checksum
!= cfg_checksum
)
319 error ("Profile data for function %u is corrupted", fn_ident
);
320 error ("checksum is (%x,%x) instead of (%x,%x)",
321 entry
->lineno_checksum
, entry
->cfg_checksum
,
322 lineno_checksum
, cfg_checksum
);
327 else if (entry
->summary
.num
!= n_counts
)
329 error ("Profile data for function %u is corrupted", fn_ident
);
330 error ("number of counters is %d instead of %d", entry
->summary
.num
, n_counts
);
335 else if (elt
.ctr
>= GCOV_COUNTERS_SUMMABLE
)
337 error ("cannot merge separate %s counters for function %u",
338 ctr_names
[elt
.ctr
], fn_ident
);
343 entry
->summary
.runs
+= summary
.ctrs
[elt
.ctr
].runs
;
344 entry
->summary
.sum_all
+= summary
.ctrs
[elt
.ctr
].sum_all
;
345 if (entry
->summary
.run_max
< summary
.ctrs
[elt
.ctr
].run_max
)
346 entry
->summary
.run_max
= summary
.ctrs
[elt
.ctr
].run_max
;
347 entry
->summary
.sum_max
+= summary
.ctrs
[elt
.ctr
].sum_max
;
349 for (ix
= 0; ix
!= n_counts
; ix
++)
350 entry
->counts
[ix
] += gcov_read_counter ();
353 gcov_sync (offset
, length
);
354 if ((is_error
= gcov_is_error ()))
356 error (is_error
< 0 ? "%qs has overflowed" : "%qs is corrupted",
367 /* Returns the counters for a particular tag. */
370 get_coverage_counts (unsigned counter
, unsigned expected
,
371 unsigned cfg_checksum
, unsigned lineno_checksum
,
372 const struct gcov_ctr_summary
**summary
)
374 counts_entry_t
*entry
, elt
;
376 /* No hash table, no counts. */
379 static int warned
= 0;
381 if (!warned
++ && dump_enabled_p ())
382 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
383 (flag_guess_branch_prob
384 ? "file %s not found, execution counts estimated\n"
385 : "file %s not found, execution counts assumed to "
390 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
391 elt
.ident
= current_function_funcdef_no
+ 1;
394 gcc_assert (coverage_node_map_initialized_p ());
395 elt
.ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
398 entry
= counts_hash
->find (&elt
);
399 if (!entry
|| !entry
->summary
.num
)
400 /* The function was not emitted, or is weak and not chosen in the
401 final executable. Silently fail, because there's nothing we
405 if (entry
->cfg_checksum
!= cfg_checksum
406 || entry
->summary
.num
!= expected
)
408 static int warned
= 0;
409 bool warning_printed
= false;
410 tree id
= DECL_ASSEMBLER_NAME (current_function_decl
);
413 warning_at (input_location
, OPT_Wcoverage_mismatch
,
414 "the control flow of function %qE does not match "
415 "its profile data (counter %qs)", id
, ctr_names
[counter
]);
416 if (warning_printed
&& dump_enabled_p ())
418 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
419 "use -Wno-error=coverage-mismatch to tolerate "
420 "the mismatch but performance may drop if the "
421 "function is hot\n");
426 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, input_location
,
427 "coverage mismatch ignored\n");
428 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
429 flag_guess_branch_prob
430 ? G_("execution counts estimated\n")
431 : G_("execution counts assumed to be zero\n"));
432 if (!flag_guess_branch_prob
)
433 dump_printf (MSG_OPTIMIZED_LOCATIONS
,
434 "this can result in poorly optimized code\n");
440 else if (entry
->lineno_checksum
!= lineno_checksum
)
442 warning (OPT_Wcoverage_mismatch
,
443 "source locations for function %qE have changed,"
444 " the profile data may be out of date",
445 DECL_ASSEMBLER_NAME (current_function_decl
));
449 *summary
= &entry
->summary
;
451 return entry
->counts
;
454 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
455 allocation succeeded. */
458 coverage_counter_alloc (unsigned counter
, unsigned num
)
466 if (!fn_v_ctrs
[counter
])
468 tree array_type
= build_array_type (get_gcov_type (), NULL_TREE
);
471 = build_var (current_function_decl
, array_type
, counter
);
474 fn_b_ctrs
[counter
] = fn_n_ctrs
[counter
];
475 fn_n_ctrs
[counter
] += num
;
477 fn_ctr_mask
|= 1 << counter
;
481 /* Generate a tree to access COUNTER NO. */
484 tree_coverage_counter_ref (unsigned counter
, unsigned no
)
486 tree gcov_type_node
= get_gcov_type ();
488 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
490 no
+= fn_b_ctrs
[counter
];
492 /* "no" here is an array index, scaled to bytes later. */
493 return build4 (ARRAY_REF
, gcov_type_node
, fn_v_ctrs
[counter
],
494 build_int_cst (integer_type_node
, no
), NULL
, NULL
);
497 /* Generate a tree to access the address of COUNTER NO. */
500 tree_coverage_counter_addr (unsigned counter
, unsigned no
)
502 tree gcov_type_node
= get_gcov_type ();
504 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
505 no
+= fn_b_ctrs
[counter
];
507 /* "no" here is an array index, scaled to bytes later. */
508 return build_fold_addr_expr (build4 (ARRAY_REF
, gcov_type_node
,
510 build_int_cst (integer_type_node
, no
),
515 /* Generate a checksum for a string. CHKSUM is the current
519 coverage_checksum_string (unsigned chksum
, const char *string
)
524 /* Look for everything that looks if it were produced by
525 get_file_function_name and zero out the second part
526 that may result from flag_random_seed. This is not critical
527 as the checksums are used only for sanity checking. */
528 for (i
= 0; string
[i
]; i
++)
531 if (!strncmp (string
+ i
, "_GLOBAL__N_", 11))
533 if (!strncmp (string
+ i
, "_GLOBAL__", 9))
536 /* C++ namespaces do have scheme:
537 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
538 since filename might contain extra underscores there seems
539 to be no better chance then walk all possible offsets looking
543 for (i
= i
+ offset
; string
[i
]; i
++)
548 for (y
= 1; y
< 9; y
++)
549 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
550 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
552 if (y
!= 9 || string
[i
+ 9] != '_')
554 for (y
= 10; y
< 18; y
++)
555 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
556 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
561 string
= dup
= xstrdup (string
);
562 for (y
= 10; y
< 18; y
++)
569 chksum
= crc32_string (chksum
, string
);
575 /* Compute checksum for the current function. We generate a CRC32. */
578 coverage_compute_lineno_checksum (void)
580 expanded_location xloc
581 = expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
582 unsigned chksum
= xloc
.line
;
584 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
585 chksum
= coverage_checksum_string
586 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
)));
591 /* Compute profile ID. This is better to be unique in whole program. */
594 coverage_compute_profile_id (struct cgraph_node
*n
)
598 /* Externally visible symbols have unique name. */
599 if (TREE_PUBLIC (n
->decl
) || DECL_EXTERNAL (n
->decl
) || n
->unique_name
)
601 chksum
= coverage_checksum_string
602 (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
606 expanded_location xloc
607 = expand_location (DECL_SOURCE_LOCATION (n
->decl
));
608 bool use_name_only
= (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
) == 0);
610 chksum
= (use_name_only
? 0 : xloc
.line
);
611 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
612 chksum
= coverage_checksum_string
613 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
614 if (!use_name_only
&& first_global_object_name
)
615 chksum
= coverage_checksum_string
616 (chksum
, first_global_object_name
);
617 chksum
= coverage_checksum_string
618 (chksum
, aux_base_name
);
621 /* Non-negative integers are hopefully small enough to fit in all targets.
622 Gcov file formats wants non-zero function IDs. */
623 chksum
= chksum
& 0x7fffffff;
624 return chksum
+ (!chksum
);
627 /* Compute cfg checksum for the function FN given as argument.
628 The checksum is calculated carefully so that
629 source code changes that doesn't affect the control flow graph
630 won't change the checksum.
631 This is to make the profile data useable across source code change.
632 The downside of this is that the compiler may use potentially
633 wrong profile data - that the source code change has non-trivial impact
634 on the validity of profile data (e.g. the reversed condition)
635 but the compiler won't detect the change and use the wrong profile data. */
638 coverage_compute_cfg_checksum (struct function
*fn
)
641 unsigned chksum
= n_basic_blocks_for_fn (fn
);
643 FOR_EACH_BB_FN (bb
, fn
)
647 chksum
= crc32_byte (chksum
, bb
->index
);
648 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
650 chksum
= crc32_byte (chksum
, e
->dest
->index
);
657 /* Begin output to the notes file for the current function.
658 Writes the function header. Returns nonzero if data should be output. */
661 coverage_begin_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
663 expanded_location xloc
;
664 unsigned long offset
;
666 /* We don't need to output .gcno file unless we're under -ftest-coverage
667 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
668 if (no_coverage
|| !bbg_file_name
)
671 xloc
= expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
673 /* Announce function */
674 offset
= gcov_write_tag (GCOV_TAG_FUNCTION
);
675 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
676 gcov_write_unsigned (current_function_funcdef_no
+ 1);
679 gcc_assert (coverage_node_map_initialized_p ());
680 gcov_write_unsigned (
681 cgraph_node::get (current_function_decl
)->profile_id
);
684 gcov_write_unsigned (lineno_checksum
);
685 gcov_write_unsigned (cfg_checksum
);
686 gcov_write_string (IDENTIFIER_POINTER
687 (DECL_ASSEMBLER_NAME (current_function_decl
)));
688 gcov_write_string (xloc
.file
);
689 gcov_write_unsigned (xloc
.line
);
690 gcov_write_length (offset
);
692 return !gcov_is_error ();
695 /* Finish coverage data for the current function. Verify no output
696 error has occurred. Save function coverage counts. */
699 coverage_end_function (unsigned lineno_checksum
, unsigned cfg_checksum
)
703 if (bbg_file_name
&& gcov_is_error ())
705 warning (0, "error writing %qs", bbg_file_name
);
706 unlink (bbg_file_name
);
707 bbg_file_name
= NULL
;
712 struct coverage_data
*item
= 0;
714 item
= ggc_alloc
<coverage_data
> ();
716 if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID
))
717 item
->ident
= current_function_funcdef_no
+ 1;
720 gcc_assert (coverage_node_map_initialized_p ());
721 item
->ident
= cgraph_node::get (cfun
->decl
)->profile_id
;
724 item
->lineno_checksum
= lineno_checksum
;
725 item
->cfg_checksum
= cfg_checksum
;
727 item
->fn_decl
= current_function_decl
;
729 *functions_tail
= item
;
730 functions_tail
= &item
->next
;
732 for (i
= 0; i
!= GCOV_COUNTERS
; i
++)
734 tree var
= fn_v_ctrs
[i
];
737 item
->ctr_vars
[i
] = var
;
740 tree array_type
= build_index_type (size_int (fn_n_ctrs
[i
] - 1));
741 array_type
= build_array_type (get_gcov_type (), array_type
);
742 TREE_TYPE (var
) = array_type
;
743 DECL_SIZE (var
) = TYPE_SIZE (array_type
);
744 DECL_SIZE_UNIT (var
) = TYPE_SIZE_UNIT (array_type
);
745 varpool_node::finalize_decl (var
);
748 fn_b_ctrs
[i
] = fn_n_ctrs
[i
] = 0;
749 fn_v_ctrs
[i
] = NULL_TREE
;
751 prg_ctr_mask
|= fn_ctr_mask
;
756 /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
757 >= 0 it is a counter array, otherwise it is the function structure. */
760 build_var (tree fn_decl
, tree type
, int counter
)
762 tree var
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
, type
);
763 const char *fn_name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl
));
765 size_t fn_name_len
, len
;
767 fn_name
= targetm
.strip_name_encoding (fn_name
);
768 fn_name_len
= strlen (fn_name
);
769 buf
= XALLOCAVEC (char, fn_name_len
+ 8 + sizeof (int) * 3);
772 strcpy (buf
, "__gcov__");
774 sprintf (buf
, "__gcov%u_", counter
);
776 #ifndef NO_DOT_IN_LABEL
778 #elif !defined NO_DOLLAR_IN_LABEL
781 memcpy (buf
+ len
, fn_name
, fn_name_len
+ 1);
782 DECL_NAME (var
) = get_identifier (buf
);
783 TREE_STATIC (var
) = 1;
784 TREE_ADDRESSABLE (var
) = 1;
785 DECL_NONALIASED (var
) = 1;
786 DECL_ALIGN (var
) = TYPE_ALIGN (type
);
791 /* Creates the gcov_fn_info RECORD_TYPE. */
794 build_fn_info_type (tree type
, unsigned counters
, tree gcov_info_type
)
796 tree ctr_info
= lang_hooks
.types
.make_type (RECORD_TYPE
);
800 gcc_assert (counters
);
803 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
804 get_gcov_unsigned_t ());
807 /* ctr_info::values */
808 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
809 build_pointer_type (get_gcov_type ()));
810 DECL_CHAIN (field
) = fields
;
813 finish_builtin_struct (ctr_info
, "__gcov_ctr_info", fields
, NULL_TREE
);
816 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
817 build_pointer_type (build_qualified_type
818 (gcov_info_type
, TYPE_QUAL_CONST
)));
822 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
823 get_gcov_unsigned_t ());
824 DECL_CHAIN (field
) = fields
;
827 /* lineno_checksum */
828 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
829 get_gcov_unsigned_t ());
830 DECL_CHAIN (field
) = fields
;
834 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
835 get_gcov_unsigned_t ());
836 DECL_CHAIN (field
) = fields
;
839 array_type
= build_index_type (size_int (counters
- 1));
840 array_type
= build_array_type (ctr_info
, array_type
);
843 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
, array_type
);
844 DECL_CHAIN (field
) = fields
;
847 finish_builtin_struct (type
, "__gcov_fn_info", fields
, NULL_TREE
);
850 /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
851 the coverage data for the function and TYPE is the gcov_fn_info
852 RECORD_TYPE. KEY is the object file key. */
855 build_fn_info (const struct coverage_data
*data
, tree type
, tree key
)
857 tree fields
= TYPE_FIELDS (type
);
860 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
861 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
864 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
865 build1 (ADDR_EXPR
, TREE_TYPE (fields
), key
));
866 fields
= DECL_CHAIN (fields
);
869 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
870 build_int_cstu (get_gcov_unsigned_t (),
872 fields
= DECL_CHAIN (fields
);
874 /* lineno_checksum */
875 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
876 build_int_cstu (get_gcov_unsigned_t (),
877 data
->lineno_checksum
));
878 fields
= DECL_CHAIN (fields
);
881 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
882 build_int_cstu (get_gcov_unsigned_t (),
883 data
->cfg_checksum
));
884 fields
= DECL_CHAIN (fields
);
887 ctr_type
= TREE_TYPE (TREE_TYPE (fields
));
888 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
889 if (prg_ctr_mask
& (1 << ix
))
891 vec
<constructor_elt
, va_gc
> *ctr
= NULL
;
892 tree var
= data
->ctr_vars
[ix
];
897 = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var
))))
900 CONSTRUCTOR_APPEND_ELT (ctr
, TYPE_FIELDS (ctr_type
),
901 build_int_cstu (get_gcov_unsigned_t (),
905 CONSTRUCTOR_APPEND_ELT (ctr
, DECL_CHAIN (TYPE_FIELDS (ctr_type
)),
906 build_fold_addr_expr (var
));
908 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, build_constructor (ctr_type
, ctr
));
911 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
912 build_constructor (TREE_TYPE (fields
), v2
));
914 return build_constructor (type
, v1
);
917 /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
918 completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
921 build_info_type (tree type
, tree fn_info_ptr_type
)
923 tree field
, fields
= NULL_TREE
;
927 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
928 get_gcov_unsigned_t ());
929 DECL_CHAIN (field
) = fields
;
933 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
934 build_pointer_type (build_qualified_type
935 (type
, TYPE_QUAL_CONST
)));
936 DECL_CHAIN (field
) = fields
;
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 (char_type_node
, TYPE_QUAL_CONST
)));
949 DECL_CHAIN (field
) = fields
;
954 = build_function_type_list (void_type_node
,
955 build_pointer_type (get_gcov_type ()),
956 get_gcov_unsigned_t (), NULL_TREE
);
958 = build_array_type (build_pointer_type (merge_fn_type
),
959 build_index_type (size_int (GCOV_COUNTERS
- 1)));
960 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
962 DECL_CHAIN (field
) = fields
;
966 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
967 get_gcov_unsigned_t ());
968 DECL_CHAIN (field
) = fields
;
971 /* function_info pointer pointer */
972 fn_info_ptr_type
= build_pointer_type
973 (build_qualified_type (fn_info_ptr_type
, TYPE_QUAL_CONST
));
974 field
= build_decl (BUILTINS_LOCATION
, FIELD_DECL
, NULL_TREE
,
976 DECL_CHAIN (field
) = fields
;
979 finish_builtin_struct (type
, "__gcov_info", fields
, NULL_TREE
);
982 /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
983 gcov_info structure type, FN_ARY is the array of pointers to
984 function info objects. */
987 build_info (tree info_type
, tree fn_ary
)
989 tree info_fields
= TYPE_FIELDS (info_type
);
990 tree merge_fn_type
, n_funcs
;
992 tree filename_string
;
993 int da_file_name_len
;
994 vec
<constructor_elt
, va_gc
> *v1
= NULL
;
995 vec
<constructor_elt
, va_gc
> *v2
= NULL
;
998 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
999 build_int_cstu (TREE_TYPE (info_fields
),
1001 info_fields
= DECL_CHAIN (info_fields
);
1004 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, null_pointer_node
);
1005 info_fields
= DECL_CHAIN (info_fields
);
1008 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1009 build_int_cstu (TREE_TYPE (info_fields
),
1011 info_fields
= DECL_CHAIN (info_fields
);
1014 da_file_name_len
= strlen (da_file_name
);
1015 filename_string
= build_string (da_file_name_len
+ 1, da_file_name
);
1016 TREE_TYPE (filename_string
) = build_array_type
1017 (char_type_node
, build_index_type (size_int (da_file_name_len
)));
1018 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1019 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
),
1021 info_fields
= DECL_CHAIN (info_fields
);
1023 /* merge fn array -- NULL slots indicate unmeasured counters */
1024 merge_fn_type
= TREE_TYPE (TREE_TYPE (info_fields
));
1025 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1027 tree ptr
= null_pointer_node
;
1029 if ((1u << ix
) & prg_ctr_mask
)
1031 tree merge_fn
= build_decl (BUILTINS_LOCATION
,
1033 get_identifier (ctr_merge_functions
[ix
]),
1034 TREE_TYPE (merge_fn_type
));
1035 DECL_EXTERNAL (merge_fn
) = 1;
1036 TREE_PUBLIC (merge_fn
) = 1;
1037 DECL_ARTIFICIAL (merge_fn
) = 1;
1038 TREE_NOTHROW (merge_fn
) = 1;
1039 /* Initialize assembler name so we can stream out. */
1040 DECL_ASSEMBLER_NAME (merge_fn
);
1041 ptr
= build1 (ADDR_EXPR
, merge_fn_type
, merge_fn
);
1043 CONSTRUCTOR_APPEND_ELT (v2
, NULL
, ptr
);
1045 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1046 build_constructor (TREE_TYPE (info_fields
), v2
));
1047 info_fields
= DECL_CHAIN (info_fields
);
1050 n_funcs
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary
)));
1051 n_funcs
= fold_build2 (PLUS_EXPR
, TREE_TYPE (info_fields
),
1052 n_funcs
, size_one_node
);
1053 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
, n_funcs
);
1054 info_fields
= DECL_CHAIN (info_fields
);
1057 CONSTRUCTOR_APPEND_ELT (v1
, info_fields
,
1058 build1 (ADDR_EXPR
, TREE_TYPE (info_fields
), fn_ary
));
1059 info_fields
= DECL_CHAIN (info_fields
);
1061 gcc_assert (!info_fields
);
1062 return build_constructor (info_type
, v1
);
1065 /* Generate the constructor function to call __gcov_init. */
1068 build_init_ctor (tree gcov_info_type
)
1070 tree ctor
, stmt
, init_fn
;
1072 /* Build a decl for __gcov_init. */
1073 init_fn
= build_pointer_type (gcov_info_type
);
1074 init_fn
= build_function_type_list (void_type_node
, init_fn
, NULL
);
1075 init_fn
= build_decl (BUILTINS_LOCATION
, FUNCTION_DECL
,
1076 get_identifier ("__gcov_init"), init_fn
);
1077 TREE_PUBLIC (init_fn
) = 1;
1078 DECL_EXTERNAL (init_fn
) = 1;
1079 DECL_ASSEMBLER_NAME (init_fn
);
1081 /* Generate a call to __gcov_init(&gcov_info). */
1083 stmt
= build_fold_addr_expr (gcov_info_var
);
1084 stmt
= build_call_expr (init_fn
, 1, stmt
);
1085 append_to_statement_list (stmt
, &ctor
);
1087 /* Generate a constructor to run it. */
1088 cgraph_build_static_cdtor ('I', ctor
, DEFAULT_INIT_PRIORITY
);
1091 /* Create the gcov_info types and object. Generate the constructor
1092 function to call __gcov_init. Does not generate the initializer
1093 for the object. Returns TRUE if coverage data is being emitted. */
1096 coverage_obj_init (void)
1098 tree gcov_info_type
;
1099 unsigned n_counters
= 0;
1101 struct coverage_data
*fn
;
1102 struct coverage_data
**fn_prev
;
1105 no_coverage
= 1; /* Disable any further coverage. */
1110 if (symtab
->dump_file
)
1111 fprintf (symtab
->dump_file
, "Using data file %s\n", da_file_name
);
1113 /* Prune functions. */
1114 for (fn_prev
= &functions_head
; (fn
= *fn_prev
);)
1115 if (DECL_STRUCT_FUNCTION (fn
->fn_decl
))
1116 fn_prev
= &fn
->next
;
1118 /* The function is not being emitted, remove from list. */
1119 *fn_prev
= fn
->next
;
1121 if (functions_head
== NULL
)
1124 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
1125 if ((1u << ix
) & prg_ctr_mask
)
1128 /* Build the info and fn_info types. These are mutually recursive. */
1129 gcov_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1130 gcov_fn_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1131 build_fn_info_type (gcov_fn_info_type
, n_counters
, gcov_info_type
);
1132 gcov_info_type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1133 gcov_fn_info_ptr_type
= build_pointer_type
1134 (build_qualified_type (gcov_fn_info_type
, TYPE_QUAL_CONST
));
1135 build_info_type (gcov_info_type
, gcov_fn_info_ptr_type
);
1137 /* Build the gcov info var, this is referred to in its own
1139 gcov_info_var
= build_decl (BUILTINS_LOCATION
,
1140 VAR_DECL
, NULL_TREE
, gcov_info_type
);
1141 TREE_STATIC (gcov_info_var
) = 1;
1142 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 0);
1143 DECL_NAME (gcov_info_var
) = get_identifier (name_buf
);
1145 build_init_ctor (gcov_info_type
);
1150 /* Generate the coverage function info for FN and DATA. Append a
1151 pointer to that object to CTOR and return the appended CTOR. */
1153 static vec
<constructor_elt
, va_gc
> *
1154 coverage_obj_fn (vec
<constructor_elt
, va_gc
> *ctor
, tree fn
,
1155 struct coverage_data
const *data
)
1157 tree init
= build_fn_info (data
, gcov_fn_info_type
, gcov_info_var
);
1158 tree var
= build_var (fn
, gcov_fn_info_type
, -1);
1160 DECL_INITIAL (var
) = init
;
1161 varpool_node::finalize_decl (var
);
1163 CONSTRUCTOR_APPEND_ELT (ctor
, NULL
,
1164 build1 (ADDR_EXPR
, gcov_fn_info_ptr_type
, var
));
1168 /* Finalize the coverage data. Generates the array of pointers to
1169 function objects from CTOR. Generate the gcov_info initializer. */
1172 coverage_obj_finish (vec
<constructor_elt
, va_gc
> *ctor
)
1174 unsigned n_functions
= vec_safe_length (ctor
);
1175 tree fn_info_ary_type
= build_array_type
1176 (build_qualified_type (gcov_fn_info_ptr_type
, TYPE_QUAL_CONST
),
1177 build_index_type (size_int (n_functions
- 1)));
1178 tree fn_info_ary
= build_decl (BUILTINS_LOCATION
, VAR_DECL
, NULL_TREE
,
1182 TREE_STATIC (fn_info_ary
) = 1;
1183 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 1);
1184 DECL_NAME (fn_info_ary
) = get_identifier (name_buf
);
1185 DECL_INITIAL (fn_info_ary
) = build_constructor (fn_info_ary_type
, ctor
);
1186 varpool_node::finalize_decl (fn_info_ary
);
1188 DECL_INITIAL (gcov_info_var
)
1189 = build_info (TREE_TYPE (gcov_info_var
), fn_info_ary
);
1190 varpool_node::finalize_decl (gcov_info_var
);
1193 /* Perform file-level initialization. Read in data file, generate name
1197 coverage_init (const char *filename
)
1199 int len
= strlen (filename
);
1202 /* Since coverage_init is invoked very early, before the pass
1203 manager, we need to set up the dumping explicitly. This is
1204 similar to the handling in finish_optimization_passes. */
1205 int profile_pass_num
=
1206 g
->get_passes ()->get_pass_profile ()->static_pass_number
;
1207 g
->get_dumps ()->dump_start (profile_pass_num
, NULL
);
1209 if (!profile_data_prefix
&& !IS_ABSOLUTE_PATH (filename
))
1210 profile_data_prefix
= getpwd ();
1212 if (profile_data_prefix
)
1213 prefix_len
= strlen (profile_data_prefix
);
1215 /* Name of da file. */
1216 da_file_name
= XNEWVEC (char, len
+ strlen (GCOV_DATA_SUFFIX
)
1219 if (profile_data_prefix
)
1221 memcpy (da_file_name
, profile_data_prefix
, prefix_len
);
1222 da_file_name
[prefix_len
++] = '/';
1224 memcpy (da_file_name
+ prefix_len
, filename
, len
);
1225 strcpy (da_file_name
+ prefix_len
+ len
, GCOV_DATA_SUFFIX
);
1227 bbg_file_stamp
= local_tick
;
1229 if (flag_auto_profile
)
1230 read_autofdo_file ();
1231 else if (flag_branch_probabilities
)
1232 read_counts_file ();
1234 /* Name of bbg file. */
1235 if (flag_test_coverage
&& !flag_compare_debug
)
1237 bbg_file_name
= XNEWVEC (char, len
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1238 memcpy (bbg_file_name
, filename
, len
);
1239 strcpy (bbg_file_name
+ len
, GCOV_NOTE_SUFFIX
);
1241 if (!gcov_open (bbg_file_name
, -1))
1243 error ("cannot open %s", bbg_file_name
);
1244 bbg_file_name
= NULL
;
1248 gcov_write_unsigned (GCOV_NOTE_MAGIC
);
1249 gcov_write_unsigned (GCOV_VERSION
);
1250 gcov_write_unsigned (bbg_file_stamp
);
1254 g
->get_dumps ()->dump_finish (profile_pass_num
);
1257 /* Performs file-level cleanup. Close notes file, generate coverage
1258 variables and constructor. */
1261 coverage_finish (void)
1263 if (bbg_file_name
&& gcov_close ())
1264 unlink (bbg_file_name
);
1266 if (!flag_branch_probabilities
&& flag_test_coverage
1267 && (!local_tick
|| local_tick
== (unsigned)-1))
1268 /* Only remove the da file, if we're emitting coverage code and
1269 cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
1270 unlink (da_file_name
);
1272 if (coverage_obj_init ())
1274 vec
<constructor_elt
, va_gc
> *fn_ctor
= NULL
;
1275 struct coverage_data
*fn
;
1277 for (fn
= functions_head
; fn
; fn
= fn
->next
)
1278 fn_ctor
= coverage_obj_fn (fn_ctor
, fn
->fn_decl
, fn
);
1279 coverage_obj_finish (fn_ctor
);
1282 XDELETEVEC (da_file_name
);
1283 da_file_name
= NULL
;
1286 #include "gt-coverage.h"