coverage.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  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
  7. This file is part of GCC.
  8. GCC is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 3, or (at your option) any later
  11. version.
  12. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with GCC; see the file COPYING3. If not see
  18. <http://www.gnu.org/licenses/>. */
  19. #define GCOV_LINKAGE
  20. #include "config.h"
  21. #include "system.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "rtl.h"
  25. #include "hash-set.h"
  26. #include "machmode.h"
  27. #include "vec.h"
  28. #include "double-int.h"
  29. #include "input.h"
  30. #include "alias.h"
  31. #include "symtab.h"
  32. #include "wide-int.h"
  33. #include "inchash.h"
  34. #include "tree.h"
  35. #include "fold-const.h"
  36. #include "stringpool.h"
  37. #include "stor-layout.h"
  38. #include "flags.h"
  39. #include "output.h"
  40. #include "regs.h"
  41. #include "hashtab.h"
  42. #include "hard-reg-set.h"
  43. #include "function.h"
  44. #include "statistics.h"
  45. #include "real.h"
  46. #include "fixed-value.h"
  47. #include "insn-config.h"
  48. #include "expmed.h"
  49. #include "dojump.h"
  50. #include "explow.h"
  51. #include "calls.h"
  52. #include "emit-rtl.h"
  53. #include "varasm.h"
  54. #include "stmt.h"
  55. #include "expr.h"
  56. #include "predict.h"
  57. #include "dominance.h"
  58. #include "cfg.h"
  59. #include "basic-block.h"
  60. #include "toplev.h"
  61. #include "tm_p.h"
  62. #include "ggc.h"
  63. #include "coverage.h"
  64. #include "langhooks.h"
  65. #include "hash-table.h"
  66. #include "tree-iterator.h"
  67. #include "context.h"
  68. #include "pass_manager.h"
  69. #include "tree-pass.h"
  70. #include "hash-map.h"
  71. #include "is-a.h"
  72. #include "plugin-api.h"
  73. #include "ipa-ref.h"
  74. #include "cgraph.h"
  75. #include "dumpfile.h"
  76. #include "diagnostic-core.h"
  77. #include "intl.h"
  78. #include "filenames.h"
  79. #include "target.h"
  80. #include "params.h"
  81. #include "auto-profile.h"
  82. #include "gcov-io.h"
  83. #include "gcov-io.c"
  84. struct GTY((chain_next ("%h.next"))) coverage_data
  85. {
  86. struct coverage_data *next; /* next function */
  87. unsigned ident; /* function ident */
  88. unsigned lineno_checksum; /* function lineno checksum */
  89. unsigned cfg_checksum; /* function cfg checksum */
  90. tree fn_decl; /* the function decl */
  91. tree ctr_vars[GCOV_COUNTERS]; /* counter variables. */
  92. };
  93. /* Counts information for a function. */
  94. typedef struct counts_entry
  95. {
  96. /* We hash by */
  97. unsigned ident;
  98. unsigned ctr;
  99. /* Store */
  100. unsigned lineno_checksum;
  101. unsigned cfg_checksum;
  102. gcov_type *counts;
  103. struct gcov_ctr_summary summary;
  104. /* hash_table support. */
  105. typedef counts_entry value_type;
  106. typedef counts_entry compare_type;
  107. static inline hashval_t hash (const value_type *);
  108. static int equal (const value_type *, const compare_type *);
  109. static void remove (value_type *);
  110. } counts_entry_t;
  111. static GTY(()) struct coverage_data *functions_head = 0;
  112. static struct coverage_data **functions_tail = &functions_head;
  113. static unsigned no_coverage = 0;
  114. /* Cumulative counter information for whole program. */
  115. static unsigned prg_ctr_mask; /* Mask of counter types generated. */
  116. /* Counter information for current function. */
  117. static unsigned fn_ctr_mask; /* Mask of counters used. */
  118. static GTY(()) tree fn_v_ctrs[GCOV_COUNTERS]; /* counter variables. */
  119. static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
  120. static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
  121. /* Coverage info VAR_DECL and function info type nodes. */
  122. static GTY(()) tree gcov_info_var;
  123. static GTY(()) tree gcov_fn_info_type;
  124. static GTY(()) tree gcov_fn_info_ptr_type;
  125. /* Name of the notes (gcno) output file. The "bbg" prefix is for
  126. historical reasons, when the notes file contained only the
  127. basic block graph notes.
  128. If this is NULL we're not writing to the notes file. */
  129. static char *bbg_file_name;
  130. /* File stamp for notes file. */
  131. static unsigned bbg_file_stamp;
  132. /* Name of the count data (gcda) file. */
  133. static char *da_file_name;
  134. /* The names of merge functions for counters. */
  135. #define STR(str) #str
  136. #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) STR(__gcov_merge ## FN_TYPE),
  137. static const char *const ctr_merge_functions[GCOV_COUNTERS] = {
  138. #include "gcov-counter.def"
  139. };
  140. #undef DEF_GCOV_COUNTER
  141. #undef STR
  142. #define DEF_GCOV_COUNTER(COUNTER, NAME, FN_TYPE) NAME,
  143. static const char *const ctr_names[GCOV_COUNTERS] = {
  144. #include "gcov-counter.def"
  145. };
  146. #undef DEF_GCOV_COUNTER
  147. /* Forward declarations. */
  148. static void read_counts_file (void);
  149. static tree build_var (tree, tree, int);
  150. static void build_fn_info_type (tree, unsigned, tree);
  151. static void build_info_type (tree, tree);
  152. static tree build_fn_info (const struct coverage_data *, tree, tree);
  153. static tree build_info (tree, tree);
  154. static bool coverage_obj_init (void);
  155. static vec<constructor_elt, va_gc> *coverage_obj_fn
  156. (vec<constructor_elt, va_gc> *, tree, struct coverage_data const *);
  157. static void coverage_obj_finish (vec<constructor_elt, va_gc> *);
  158. /* Return the type node for gcov_type. */
  159. tree
  160. get_gcov_type (void)
  161. {
  162. machine_mode mode = smallest_mode_for_size (GCOV_TYPE_SIZE, MODE_INT);
  163. return lang_hooks.types.type_for_mode (mode, false);
  164. }
  165. /* Return the type node for gcov_unsigned_t. */
  166. static tree
  167. get_gcov_unsigned_t (void)
  168. {
  169. machine_mode mode = smallest_mode_for_size (32, MODE_INT);
  170. return lang_hooks.types.type_for_mode (mode, true);
  171. }
  172. inline hashval_t
  173. counts_entry::hash (const value_type *entry)
  174. {
  175. return entry->ident * GCOV_COUNTERS + entry->ctr;
  176. }
  177. inline int
  178. counts_entry::equal (const value_type *entry1,
  179. const compare_type *entry2)
  180. {
  181. return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
  182. }
  183. inline void
  184. counts_entry::remove (value_type *entry)
  185. {
  186. free (entry->counts);
  187. free (entry);
  188. }
  189. /* Hash table of count data. */
  190. static hash_table<counts_entry> *counts_hash;
  191. /* Read in the counts file, if available. */
  192. static void
  193. read_counts_file (void)
  194. {
  195. gcov_unsigned_t fn_ident = 0;
  196. struct gcov_summary summary;
  197. unsigned new_summary = 1;
  198. gcov_unsigned_t tag;
  199. int is_error = 0;
  200. unsigned lineno_checksum = 0;
  201. unsigned cfg_checksum = 0;
  202. if (!gcov_open (da_file_name, 1))
  203. return;
  204. if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
  205. {
  206. warning (0, "%qs is not a gcov data file", da_file_name);
  207. gcov_close ();
  208. return;
  209. }
  210. else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
  211. {
  212. char v[4], e[4];
  213. GCOV_UNSIGNED2STRING (v, tag);
  214. GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
  215. warning (0, "%qs is version %q.*s, expected version %q.*s",
  216. da_file_name, 4, v, 4, e);
  217. gcov_close ();
  218. return;
  219. }
  220. /* Read the stamp, used for creating a generation count. */
  221. tag = gcov_read_unsigned ();
  222. bbg_file_stamp = crc32_unsigned (bbg_file_stamp, tag);
  223. counts_hash = new hash_table<counts_entry> (10);
  224. while ((tag = gcov_read_unsigned ()))
  225. {
  226. gcov_unsigned_t length;
  227. gcov_position_t offset;
  228. length = gcov_read_unsigned ();
  229. offset = gcov_position ();
  230. if (tag == GCOV_TAG_FUNCTION)
  231. {
  232. if (length)
  233. {
  234. fn_ident = gcov_read_unsigned ();
  235. lineno_checksum = gcov_read_unsigned ();
  236. cfg_checksum = gcov_read_unsigned ();
  237. }
  238. else
  239. fn_ident = lineno_checksum = cfg_checksum = 0;
  240. new_summary = 1;
  241. }
  242. else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
  243. {
  244. struct gcov_summary sum;
  245. unsigned ix;
  246. if (new_summary)
  247. memset (&summary, 0, sizeof (summary));
  248. gcov_read_summary (&sum);
  249. for (ix = 0; ix != GCOV_COUNTERS_SUMMABLE; ix++)
  250. {
  251. summary.ctrs[ix].runs += sum.ctrs[ix].runs;
  252. summary.ctrs[ix].sum_all += sum.ctrs[ix].sum_all;
  253. if (summary.ctrs[ix].run_max < sum.ctrs[ix].run_max)
  254. summary.ctrs[ix].run_max = sum.ctrs[ix].run_max;
  255. summary.ctrs[ix].sum_max += sum.ctrs[ix].sum_max;
  256. }
  257. if (new_summary)
  258. memcpy (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
  259. sum.ctrs[GCOV_COUNTER_ARCS].histogram,
  260. sizeof (gcov_bucket_type) * GCOV_HISTOGRAM_SIZE);
  261. else
  262. gcov_histogram_merge (summary.ctrs[GCOV_COUNTER_ARCS].histogram,
  263. sum.ctrs[GCOV_COUNTER_ARCS].histogram);
  264. new_summary = 0;
  265. }
  266. else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
  267. {
  268. counts_entry_t **slot, *entry, elt;
  269. unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
  270. unsigned ix;
  271. elt.ident = fn_ident;
  272. elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
  273. slot = counts_hash->find_slot (&elt, INSERT);
  274. entry = *slot;
  275. if (!entry)
  276. {
  277. *slot = entry = XCNEW (counts_entry_t);
  278. entry->ident = fn_ident;
  279. entry->ctr = elt.ctr;
  280. entry->lineno_checksum = lineno_checksum;
  281. entry->cfg_checksum = cfg_checksum;
  282. if (elt.ctr < GCOV_COUNTERS_SUMMABLE)
  283. entry->summary = summary.ctrs[elt.ctr];
  284. entry->summary.num = n_counts;
  285. entry->counts = XCNEWVEC (gcov_type, n_counts);
  286. }
  287. else if (entry->lineno_checksum != lineno_checksum
  288. || entry->cfg_checksum != cfg_checksum)
  289. {
  290. error ("Profile data for function %u is corrupted", fn_ident);
  291. error ("checksum is (%x,%x) instead of (%x,%x)",
  292. entry->lineno_checksum, entry->cfg_checksum,
  293. lineno_checksum, cfg_checksum);
  294. delete counts_hash;
  295. counts_hash = NULL;
  296. break;
  297. }
  298. else if (entry->summary.num != n_counts)
  299. {
  300. error ("Profile data for function %u is corrupted", fn_ident);
  301. error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
  302. delete counts_hash;
  303. counts_hash = NULL;
  304. break;
  305. }
  306. else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
  307. {
  308. error ("cannot merge separate %s counters for function %u",
  309. ctr_names[elt.ctr], fn_ident);
  310. goto skip_merge;
  311. }
  312. else
  313. {
  314. entry->summary.runs += summary.ctrs[elt.ctr].runs;
  315. entry->summary.sum_all += summary.ctrs[elt.ctr].sum_all;
  316. if (entry->summary.run_max < summary.ctrs[elt.ctr].run_max)
  317. entry->summary.run_max = summary.ctrs[elt.ctr].run_max;
  318. entry->summary.sum_max += summary.ctrs[elt.ctr].sum_max;
  319. }
  320. for (ix = 0; ix != n_counts; ix++)
  321. entry->counts[ix] += gcov_read_counter ();
  322. skip_merge:;
  323. }
  324. gcov_sync (offset, length);
  325. if ((is_error = gcov_is_error ()))
  326. {
  327. error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
  328. da_file_name);
  329. delete counts_hash;
  330. counts_hash = NULL;
  331. break;
  332. }
  333. }
  334. gcov_close ();
  335. }
  336. /* Returns the counters for a particular tag. */
  337. gcov_type *
  338. get_coverage_counts (unsigned counter, unsigned expected,
  339. unsigned cfg_checksum, unsigned lineno_checksum,
  340. const struct gcov_ctr_summary **summary)
  341. {
  342. counts_entry_t *entry, elt;
  343. /* No hash table, no counts. */
  344. if (!counts_hash)
  345. {
  346. static int warned = 0;
  347. if (!warned++ && dump_enabled_p ())
  348. dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
  349. (flag_guess_branch_prob
  350. ? "file %s not found, execution counts estimated\n"
  351. : "file %s not found, execution counts assumed to "
  352. "be zero\n"),
  353. da_file_name);
  354. return NULL;
  355. }
  356. if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
  357. elt.ident = current_function_funcdef_no + 1;
  358. else
  359. {
  360. gcc_assert (coverage_node_map_initialized_p ());
  361. elt.ident = cgraph_node::get (cfun->decl)->profile_id;
  362. }
  363. elt.ctr = counter;
  364. entry = counts_hash->find (&elt);
  365. if (!entry || !entry->summary.num)
  366. /* The function was not emitted, or is weak and not chosen in the
  367. final executable. Silently fail, because there's nothing we
  368. can do about it. */
  369. return NULL;
  370. if (entry->cfg_checksum != cfg_checksum
  371. || entry->summary.num != expected)
  372. {
  373. static int warned = 0;
  374. bool warning_printed = false;
  375. tree id = DECL_ASSEMBLER_NAME (current_function_decl);
  376. warning_printed =
  377. warning_at (input_location, OPT_Wcoverage_mismatch,
  378. "the control flow of function %qE does not match "
  379. "its profile data (counter %qs)", id, ctr_names[counter]);
  380. if (warning_printed && dump_enabled_p ())
  381. {
  382. dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
  383. "use -Wno-error=coverage-mismatch to tolerate "
  384. "the mismatch but performance may drop if the "
  385. "function is hot\n");
  386. if (!seen_error ()
  387. && !warned++)
  388. {
  389. dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, input_location,
  390. "coverage mismatch ignored\n");
  391. dump_printf (MSG_OPTIMIZED_LOCATIONS,
  392. flag_guess_branch_prob
  393. ? G_("execution counts estimated\n")
  394. : G_("execution counts assumed to be zero\n"));
  395. if (!flag_guess_branch_prob)
  396. dump_printf (MSG_OPTIMIZED_LOCATIONS,
  397. "this can result in poorly optimized code\n");
  398. }
  399. }
  400. return NULL;
  401. }
  402. else if (entry->lineno_checksum != lineno_checksum)
  403. {
  404. warning (OPT_Wcoverage_mismatch,
  405. "source locations for function %qE have changed,"
  406. " the profile data may be out of date",
  407. DECL_ASSEMBLER_NAME (current_function_decl));
  408. }
  409. if (summary)
  410. *summary = &entry->summary;
  411. return entry->counts;
  412. }
  413. /* Allocate NUM counters of type COUNTER. Returns nonzero if the
  414. allocation succeeded. */
  415. int
  416. coverage_counter_alloc (unsigned counter, unsigned num)
  417. {
  418. if (no_coverage)
  419. return 0;
  420. if (!num)
  421. return 1;
  422. if (!fn_v_ctrs[counter])
  423. {
  424. tree array_type = build_array_type (get_gcov_type (), NULL_TREE);
  425. fn_v_ctrs[counter]
  426. = build_var (current_function_decl, array_type, counter);
  427. }
  428. fn_b_ctrs[counter] = fn_n_ctrs[counter];
  429. fn_n_ctrs[counter] += num;
  430. fn_ctr_mask |= 1 << counter;
  431. return 1;
  432. }
  433. /* Generate a tree to access COUNTER NO. */
  434. tree
  435. tree_coverage_counter_ref (unsigned counter, unsigned no)
  436. {
  437. tree gcov_type_node = get_gcov_type ();
  438. gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
  439. no += fn_b_ctrs[counter];
  440. /* "no" here is an array index, scaled to bytes later. */
  441. return build4 (ARRAY_REF, gcov_type_node, fn_v_ctrs[counter],
  442. build_int_cst (integer_type_node, no), NULL, NULL);
  443. }
  444. /* Generate a tree to access the address of COUNTER NO. */
  445. tree
  446. tree_coverage_counter_addr (unsigned counter, unsigned no)
  447. {
  448. tree gcov_type_node = get_gcov_type ();
  449. gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
  450. no += fn_b_ctrs[counter];
  451. /* "no" here is an array index, scaled to bytes later. */
  452. return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
  453. fn_v_ctrs[counter],
  454. build_int_cst (integer_type_node, no),
  455. NULL, NULL));
  456. }
  457. /* Generate a checksum for a string. CHKSUM is the current
  458. checksum. */
  459. static unsigned
  460. coverage_checksum_string (unsigned chksum, const char *string)
  461. {
  462. int i;
  463. char *dup = NULL;
  464. /* Look for everything that looks if it were produced by
  465. get_file_function_name and zero out the second part
  466. that may result from flag_random_seed. This is not critical
  467. as the checksums are used only for sanity checking. */
  468. for (i = 0; string[i]; i++)
  469. {
  470. int offset = 0;
  471. if (!strncmp (string + i, "_GLOBAL__N_", 11))
  472. offset = 11;
  473. if (!strncmp (string + i, "_GLOBAL__", 9))
  474. offset = 9;
  475. /* C++ namespaces do have scheme:
  476. _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
  477. since filename might contain extra underscores there seems
  478. to be no better chance then walk all possible offsets looking
  479. for magicnumber. */
  480. if (offset)
  481. {
  482. for (i = i + offset; string[i]; i++)
  483. if (string[i]=='_')
  484. {
  485. int y;
  486. for (y = 1; y < 9; y++)
  487. if (!(string[i + y] >= '0' && string[i + y] <= '9')
  488. && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
  489. break;
  490. if (y != 9 || string[i + 9] != '_')
  491. continue;
  492. for (y = 10; y < 18; y++)
  493. if (!(string[i + y] >= '0' && string[i + y] <= '9')
  494. && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
  495. break;
  496. if (y != 18)
  497. continue;
  498. if (!dup)
  499. string = dup = xstrdup (string);
  500. for (y = 10; y < 18; y++)
  501. dup[i + y] = '0';
  502. }
  503. break;
  504. }
  505. }
  506. chksum = crc32_string (chksum, string);
  507. free (dup);
  508. return chksum;
  509. }
  510. /* Compute checksum for the current function. We generate a CRC32. */
  511. unsigned
  512. coverage_compute_lineno_checksum (void)
  513. {
  514. expanded_location xloc
  515. = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
  516. unsigned chksum = xloc.line;
  517. chksum = coverage_checksum_string (chksum, xloc.file);
  518. chksum = coverage_checksum_string
  519. (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
  520. return chksum;
  521. }
  522. /* Compute profile ID. This is better to be unique in whole program. */
  523. unsigned
  524. coverage_compute_profile_id (struct cgraph_node *n)
  525. {
  526. unsigned chksum;
  527. /* Externally visible symbols have unique name. */
  528. if (TREE_PUBLIC (n->decl) || DECL_EXTERNAL (n->decl) || n->unique_name)
  529. {
  530. chksum = coverage_checksum_string
  531. (0, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
  532. }
  533. else
  534. {
  535. expanded_location xloc
  536. = expand_location (DECL_SOURCE_LOCATION (n->decl));
  537. bool use_name_only = (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID) == 0);
  538. chksum = (use_name_only ? 0 : xloc.line);
  539. chksum = coverage_checksum_string (chksum, xloc.file);
  540. chksum = coverage_checksum_string
  541. (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n->decl)));
  542. if (!use_name_only && first_global_object_name)
  543. chksum = coverage_checksum_string
  544. (chksum, first_global_object_name);
  545. chksum = coverage_checksum_string
  546. (chksum, aux_base_name);
  547. }
  548. /* Non-negative integers are hopefully small enough to fit in all targets.
  549. Gcov file formats wants non-zero function IDs. */
  550. chksum = chksum & 0x7fffffff;
  551. return chksum + (!chksum);
  552. }
  553. /* Compute cfg checksum for the function FN given as argument.
  554. The checksum is calculated carefully so that
  555. source code changes that doesn't affect the control flow graph
  556. won't change the checksum.
  557. This is to make the profile data useable across source code change.
  558. The downside of this is that the compiler may use potentially
  559. wrong profile data - that the source code change has non-trivial impact
  560. on the validity of profile data (e.g. the reversed condition)
  561. but the compiler won't detect the change and use the wrong profile data. */
  562. unsigned
  563. coverage_compute_cfg_checksum (struct function *fn)
  564. {
  565. basic_block bb;
  566. unsigned chksum = n_basic_blocks_for_fn (fn);
  567. FOR_EACH_BB_FN (bb, fn)
  568. {
  569. edge e;
  570. edge_iterator ei;
  571. chksum = crc32_byte (chksum, bb->index);
  572. FOR_EACH_EDGE (e, ei, bb->succs)
  573. {
  574. chksum = crc32_byte (chksum, e->dest->index);
  575. }
  576. }
  577. return chksum;
  578. }
  579. /* Begin output to the notes file for the current function.
  580. Writes the function header. Returns nonzero if data should be output. */
  581. int
  582. coverage_begin_function (unsigned lineno_checksum, unsigned cfg_checksum)
  583. {
  584. expanded_location xloc;
  585. unsigned long offset;
  586. /* We don't need to output .gcno file unless we're under -ftest-coverage
  587. (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
  588. if (no_coverage || !bbg_file_name)
  589. return 0;
  590. xloc = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
  591. /* Announce function */
  592. offset = gcov_write_tag (GCOV_TAG_FUNCTION);
  593. if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
  594. gcov_write_unsigned (current_function_funcdef_no + 1);
  595. else
  596. {
  597. gcc_assert (coverage_node_map_initialized_p ());
  598. gcov_write_unsigned (
  599. cgraph_node::get (current_function_decl)->profile_id);
  600. }
  601. gcov_write_unsigned (lineno_checksum);
  602. gcov_write_unsigned (cfg_checksum);
  603. gcov_write_string (IDENTIFIER_POINTER
  604. (DECL_ASSEMBLER_NAME (current_function_decl)));
  605. gcov_write_string (xloc.file);
  606. gcov_write_unsigned (xloc.line);
  607. gcov_write_length (offset);
  608. return !gcov_is_error ();
  609. }
  610. /* Finish coverage data for the current function. Verify no output
  611. error has occurred. Save function coverage counts. */
  612. void
  613. coverage_end_function (unsigned lineno_checksum, unsigned cfg_checksum)
  614. {
  615. unsigned i;
  616. if (bbg_file_name && gcov_is_error ())
  617. {
  618. warning (0, "error writing %qs", bbg_file_name);
  619. unlink (bbg_file_name);
  620. bbg_file_name = NULL;
  621. }
  622. if (fn_ctr_mask)
  623. {
  624. struct coverage_data *item = 0;
  625. item = ggc_alloc<coverage_data> ();
  626. if (PARAM_VALUE (PARAM_PROFILE_FUNC_INTERNAL_ID))
  627. item->ident = current_function_funcdef_no + 1;
  628. else
  629. {
  630. gcc_assert (coverage_node_map_initialized_p ());
  631. item->ident = cgraph_node::get (cfun->decl)->profile_id;
  632. }
  633. item->lineno_checksum = lineno_checksum;
  634. item->cfg_checksum = cfg_checksum;
  635. item->fn_decl = current_function_decl;
  636. item->next = 0;
  637. *functions_tail = item;
  638. functions_tail = &item->next;
  639. for (i = 0; i != GCOV_COUNTERS; i++)
  640. {
  641. tree var = fn_v_ctrs[i];
  642. if (item)
  643. item->ctr_vars[i] = var;
  644. if (var)
  645. {
  646. tree array_type = build_index_type (size_int (fn_n_ctrs[i] - 1));
  647. array_type = build_array_type (get_gcov_type (), array_type);
  648. TREE_TYPE (var) = array_type;
  649. DECL_SIZE (var) = TYPE_SIZE (array_type);
  650. DECL_SIZE_UNIT (var) = TYPE_SIZE_UNIT (array_type);
  651. varpool_node::finalize_decl (var);
  652. }
  653. fn_b_ctrs[i] = fn_n_ctrs[i] = 0;
  654. fn_v_ctrs[i] = NULL_TREE;
  655. }
  656. prg_ctr_mask |= fn_ctr_mask;
  657. fn_ctr_mask = 0;
  658. }
  659. }
  660. /* Build a coverage variable of TYPE for function FN_DECL. If COUNTER
  661. >= 0 it is a counter array, otherwise it is the function structure. */
  662. static tree
  663. build_var (tree fn_decl, tree type, int counter)
  664. {
  665. tree var = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE, type);
  666. const char *fn_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fn_decl));
  667. char *buf;
  668. size_t fn_name_len, len;
  669. fn_name = targetm.strip_name_encoding (fn_name);
  670. fn_name_len = strlen (fn_name);
  671. buf = XALLOCAVEC (char, fn_name_len + 8 + sizeof (int) * 3);
  672. if (counter < 0)
  673. strcpy (buf, "__gcov__");
  674. else
  675. sprintf (buf, "__gcov%u_", counter);
  676. len = strlen (buf);
  677. #ifndef NO_DOT_IN_LABEL
  678. buf[len - 1] = '.';
  679. #elif !defined NO_DOLLAR_IN_LABEL
  680. buf[len - 1] = '$';
  681. #endif
  682. memcpy (buf + len, fn_name, fn_name_len + 1);
  683. DECL_NAME (var) = get_identifier (buf);
  684. TREE_STATIC (var) = 1;
  685. TREE_ADDRESSABLE (var) = 1;
  686. DECL_NONALIASED (var) = 1;
  687. DECL_ALIGN (var) = TYPE_ALIGN (type);
  688. return var;
  689. }
  690. /* Creates the gcov_fn_info RECORD_TYPE. */
  691. static void
  692. build_fn_info_type (tree type, unsigned counters, tree gcov_info_type)
  693. {
  694. tree ctr_info = lang_hooks.types.make_type (RECORD_TYPE);
  695. tree field, fields;
  696. tree array_type;
  697. gcc_assert (counters);
  698. /* ctr_info::num */
  699. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  700. get_gcov_unsigned_t ());
  701. fields = field;
  702. /* ctr_info::values */
  703. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  704. build_pointer_type (get_gcov_type ()));
  705. DECL_CHAIN (field) = fields;
  706. fields = field;
  707. finish_builtin_struct (ctr_info, "__gcov_ctr_info", fields, NULL_TREE);
  708. /* key */
  709. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  710. build_pointer_type (build_qualified_type
  711. (gcov_info_type, TYPE_QUAL_CONST)));
  712. fields = field;
  713. /* ident */
  714. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  715. get_gcov_unsigned_t ());
  716. DECL_CHAIN (field) = fields;
  717. fields = field;
  718. /* lineno_checksum */
  719. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  720. get_gcov_unsigned_t ());
  721. DECL_CHAIN (field) = fields;
  722. fields = field;
  723. /* cfg checksum */
  724. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  725. get_gcov_unsigned_t ());
  726. DECL_CHAIN (field) = fields;
  727. fields = field;
  728. array_type = build_index_type (size_int (counters - 1));
  729. array_type = build_array_type (ctr_info, array_type);
  730. /* counters */
  731. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, array_type);
  732. DECL_CHAIN (field) = fields;
  733. fields = field;
  734. finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
  735. }
  736. /* Returns a CONSTRUCTOR for a gcov_fn_info. DATA is
  737. the coverage data for the function and TYPE is the gcov_fn_info
  738. RECORD_TYPE. KEY is the object file key. */
  739. static tree
  740. build_fn_info (const struct coverage_data *data, tree type, tree key)
  741. {
  742. tree fields = TYPE_FIELDS (type);
  743. tree ctr_type;
  744. unsigned ix;
  745. vec<constructor_elt, va_gc> *v1 = NULL;
  746. vec<constructor_elt, va_gc> *v2 = NULL;
  747. /* key */
  748. CONSTRUCTOR_APPEND_ELT (v1, fields,
  749. build1 (ADDR_EXPR, TREE_TYPE (fields), key));
  750. fields = DECL_CHAIN (fields);
  751. /* ident */
  752. CONSTRUCTOR_APPEND_ELT (v1, fields,
  753. build_int_cstu (get_gcov_unsigned_t (),
  754. data->ident));
  755. fields = DECL_CHAIN (fields);
  756. /* lineno_checksum */
  757. CONSTRUCTOR_APPEND_ELT (v1, fields,
  758. build_int_cstu (get_gcov_unsigned_t (),
  759. data->lineno_checksum));
  760. fields = DECL_CHAIN (fields);
  761. /* cfg_checksum */
  762. CONSTRUCTOR_APPEND_ELT (v1, fields,
  763. build_int_cstu (get_gcov_unsigned_t (),
  764. data->cfg_checksum));
  765. fields = DECL_CHAIN (fields);
  766. /* counters */
  767. ctr_type = TREE_TYPE (TREE_TYPE (fields));
  768. for (ix = 0; ix != GCOV_COUNTERS; ix++)
  769. if (prg_ctr_mask & (1 << ix))
  770. {
  771. vec<constructor_elt, va_gc> *ctr = NULL;
  772. tree var = data->ctr_vars[ix];
  773. unsigned count = 0;
  774. if (var)
  775. count
  776. = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (var))))
  777. + 1;
  778. CONSTRUCTOR_APPEND_ELT (ctr, TYPE_FIELDS (ctr_type),
  779. build_int_cstu (get_gcov_unsigned_t (),
  780. count));
  781. if (var)
  782. CONSTRUCTOR_APPEND_ELT (ctr, DECL_CHAIN (TYPE_FIELDS (ctr_type)),
  783. build_fold_addr_expr (var));
  784. CONSTRUCTOR_APPEND_ELT (v2, NULL, build_constructor (ctr_type, ctr));
  785. }
  786. CONSTRUCTOR_APPEND_ELT (v1, fields,
  787. build_constructor (TREE_TYPE (fields), v2));
  788. return build_constructor (type, v1);
  789. }
  790. /* Create gcov_info struct. TYPE is the incomplete RECORD_TYPE to be
  791. completed, and FN_INFO_PTR_TYPE is a pointer to the function info type. */
  792. static void
  793. build_info_type (tree type, tree fn_info_ptr_type)
  794. {
  795. tree field, fields = NULL_TREE;
  796. tree merge_fn_type;
  797. /* Version ident */
  798. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  799. get_gcov_unsigned_t ());
  800. DECL_CHAIN (field) = fields;
  801. fields = field;
  802. /* next pointer */
  803. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  804. build_pointer_type (build_qualified_type
  805. (type, TYPE_QUAL_CONST)));
  806. DECL_CHAIN (field) = fields;
  807. fields = field;
  808. /* stamp */
  809. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  810. get_gcov_unsigned_t ());
  811. DECL_CHAIN (field) = fields;
  812. fields = field;
  813. /* Filename */
  814. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  815. build_pointer_type (build_qualified_type
  816. (char_type_node, TYPE_QUAL_CONST)));
  817. DECL_CHAIN (field) = fields;
  818. fields = field;
  819. /* merge fn array */
  820. merge_fn_type
  821. = build_function_type_list (void_type_node,
  822. build_pointer_type (get_gcov_type ()),
  823. get_gcov_unsigned_t (), NULL_TREE);
  824. merge_fn_type
  825. = build_array_type (build_pointer_type (merge_fn_type),
  826. build_index_type (size_int (GCOV_COUNTERS - 1)));
  827. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  828. merge_fn_type);
  829. DECL_CHAIN (field) = fields;
  830. fields = field;
  831. /* n_functions */
  832. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  833. get_gcov_unsigned_t ());
  834. DECL_CHAIN (field) = fields;
  835. fields = field;
  836. /* function_info pointer pointer */
  837. fn_info_ptr_type = build_pointer_type
  838. (build_qualified_type (fn_info_ptr_type, TYPE_QUAL_CONST));
  839. field = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE,
  840. fn_info_ptr_type);
  841. DECL_CHAIN (field) = fields;
  842. fields = field;
  843. finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
  844. }
  845. /* Returns a CONSTRUCTOR for the gcov_info object. INFO_TYPE is the
  846. gcov_info structure type, FN_ARY is the array of pointers to
  847. function info objects. */
  848. static tree
  849. build_info (tree info_type, tree fn_ary)
  850. {
  851. tree info_fields = TYPE_FIELDS (info_type);
  852. tree merge_fn_type, n_funcs;
  853. unsigned ix;
  854. tree filename_string;
  855. int da_file_name_len;
  856. vec<constructor_elt, va_gc> *v1 = NULL;
  857. vec<constructor_elt, va_gc> *v2 = NULL;
  858. /* Version ident */
  859. CONSTRUCTOR_APPEND_ELT (v1, info_fields,
  860. build_int_cstu (TREE_TYPE (info_fields),
  861. GCOV_VERSION));
  862. info_fields = DECL_CHAIN (info_fields);
  863. /* next -- NULL */
  864. CONSTRUCTOR_APPEND_ELT (v1, info_fields, null_pointer_node);
  865. info_fields = DECL_CHAIN (info_fields);
  866. /* stamp */
  867. CONSTRUCTOR_APPEND_ELT (v1, info_fields,
  868. build_int_cstu (TREE_TYPE (info_fields),
  869. bbg_file_stamp));
  870. info_fields = DECL_CHAIN (info_fields);
  871. /* Filename */
  872. da_file_name_len = strlen (da_file_name);
  873. filename_string = build_string (da_file_name_len + 1, da_file_name);
  874. TREE_TYPE (filename_string) = build_array_type
  875. (char_type_node, build_index_type (size_int (da_file_name_len)));
  876. CONSTRUCTOR_APPEND_ELT (v1, info_fields,
  877. build1 (ADDR_EXPR, TREE_TYPE (info_fields),
  878. filename_string));
  879. info_fields = DECL_CHAIN (info_fields);
  880. /* merge fn array -- NULL slots indicate unmeasured counters */
  881. merge_fn_type = TREE_TYPE (TREE_TYPE (info_fields));
  882. for (ix = 0; ix != GCOV_COUNTERS; ix++)
  883. {
  884. tree ptr = null_pointer_node;
  885. if ((1u << ix) & prg_ctr_mask)
  886. {
  887. tree merge_fn = build_decl (BUILTINS_LOCATION,
  888. FUNCTION_DECL,
  889. get_identifier (ctr_merge_functions[ix]),
  890. TREE_TYPE (merge_fn_type));
  891. DECL_EXTERNAL (merge_fn) = 1;
  892. TREE_PUBLIC (merge_fn) = 1;
  893. DECL_ARTIFICIAL (merge_fn) = 1;
  894. TREE_NOTHROW (merge_fn) = 1;
  895. /* Initialize assembler name so we can stream out. */
  896. DECL_ASSEMBLER_NAME (merge_fn);
  897. ptr = build1 (ADDR_EXPR, merge_fn_type, merge_fn);
  898. }
  899. CONSTRUCTOR_APPEND_ELT (v2, NULL, ptr);
  900. }
  901. CONSTRUCTOR_APPEND_ELT (v1, info_fields,
  902. build_constructor (TREE_TYPE (info_fields), v2));
  903. info_fields = DECL_CHAIN (info_fields);
  904. /* n_functions */
  905. n_funcs = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (fn_ary)));
  906. n_funcs = fold_build2 (PLUS_EXPR, TREE_TYPE (info_fields),
  907. n_funcs, size_one_node);
  908. CONSTRUCTOR_APPEND_ELT (v1, info_fields, n_funcs);
  909. info_fields = DECL_CHAIN (info_fields);
  910. /* functions */
  911. CONSTRUCTOR_APPEND_ELT (v1, info_fields,
  912. build1 (ADDR_EXPR, TREE_TYPE (info_fields), fn_ary));
  913. info_fields = DECL_CHAIN (info_fields);
  914. gcc_assert (!info_fields);
  915. return build_constructor (info_type, v1);
  916. }
  917. /* Generate the constructor function to call __gcov_init. */
  918. static void
  919. build_init_ctor (tree gcov_info_type)
  920. {
  921. tree ctor, stmt, init_fn;
  922. /* Build a decl for __gcov_init. */
  923. init_fn = build_pointer_type (gcov_info_type);
  924. init_fn = build_function_type_list (void_type_node, init_fn, NULL);
  925. init_fn = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
  926. get_identifier ("__gcov_init"), init_fn);
  927. TREE_PUBLIC (init_fn) = 1;
  928. DECL_EXTERNAL (init_fn) = 1;
  929. DECL_ASSEMBLER_NAME (init_fn);
  930. /* Generate a call to __gcov_init(&gcov_info). */
  931. ctor = NULL;
  932. stmt = build_fold_addr_expr (gcov_info_var);
  933. stmt = build_call_expr (init_fn, 1, stmt);
  934. append_to_statement_list (stmt, &ctor);
  935. /* Generate a constructor to run it. */
  936. cgraph_build_static_cdtor ('I', ctor, DEFAULT_INIT_PRIORITY);
  937. }
  938. /* Create the gcov_info types and object. Generate the constructor
  939. function to call __gcov_init. Does not generate the initializer
  940. for the object. Returns TRUE if coverage data is being emitted. */
  941. static bool
  942. coverage_obj_init (void)
  943. {
  944. tree gcov_info_type;
  945. unsigned n_counters = 0;
  946. unsigned ix;
  947. struct coverage_data *fn;
  948. struct coverage_data **fn_prev;
  949. char name_buf[32];
  950. no_coverage = 1; /* Disable any further coverage. */
  951. if (!prg_ctr_mask)
  952. return false;
  953. if (symtab->dump_file)
  954. fprintf (symtab->dump_file, "Using data file %s\n", da_file_name);
  955. /* Prune functions. */
  956. for (fn_prev = &functions_head; (fn = *fn_prev);)
  957. if (DECL_STRUCT_FUNCTION (fn->fn_decl))
  958. fn_prev = &fn->next;
  959. else
  960. /* The function is not being emitted, remove from list. */
  961. *fn_prev = fn->next;
  962. if (functions_head == NULL)
  963. return false;
  964. for (ix = 0; ix != GCOV_COUNTERS; ix++)
  965. if ((1u << ix) & prg_ctr_mask)
  966. n_counters++;
  967. /* Build the info and fn_info types. These are mutually recursive. */
  968. gcov_info_type = lang_hooks.types.make_type (RECORD_TYPE);
  969. gcov_fn_info_type = lang_hooks.types.make_type (RECORD_TYPE);
  970. gcov_fn_info_ptr_type = build_pointer_type
  971. (build_qualified_type (gcov_fn_info_type, TYPE_QUAL_CONST));
  972. build_fn_info_type (gcov_fn_info_type, n_counters, gcov_info_type);
  973. build_info_type (gcov_info_type, gcov_fn_info_ptr_type);
  974. /* Build the gcov info var, this is referred to in its own
  975. initializer. */
  976. gcov_info_var = build_decl (BUILTINS_LOCATION,
  977. VAR_DECL, NULL_TREE, gcov_info_type);
  978. TREE_STATIC (gcov_info_var) = 1;
  979. ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
  980. DECL_NAME (gcov_info_var) = get_identifier (name_buf);
  981. build_init_ctor (gcov_info_type);
  982. return true;
  983. }
  984. /* Generate the coverage function info for FN and DATA. Append a
  985. pointer to that object to CTOR and return the appended CTOR. */
  986. static vec<constructor_elt, va_gc> *
  987. coverage_obj_fn (vec<constructor_elt, va_gc> *ctor, tree fn,
  988. struct coverage_data const *data)
  989. {
  990. tree init = build_fn_info (data, gcov_fn_info_type, gcov_info_var);
  991. tree var = build_var (fn, gcov_fn_info_type, -1);
  992. DECL_INITIAL (var) = init;
  993. varpool_node::finalize_decl (var);
  994. CONSTRUCTOR_APPEND_ELT (ctor, NULL,
  995. build1 (ADDR_EXPR, gcov_fn_info_ptr_type, var));
  996. return ctor;
  997. }
  998. /* Finalize the coverage data. Generates the array of pointers to
  999. function objects from CTOR. Generate the gcov_info initializer. */
  1000. static void
  1001. coverage_obj_finish (vec<constructor_elt, va_gc> *ctor)
  1002. {
  1003. unsigned n_functions = vec_safe_length (ctor);
  1004. tree fn_info_ary_type = build_array_type
  1005. (build_qualified_type (gcov_fn_info_ptr_type, TYPE_QUAL_CONST),
  1006. build_index_type (size_int (n_functions - 1)));
  1007. tree fn_info_ary = build_decl (BUILTINS_LOCATION, VAR_DECL, NULL_TREE,
  1008. fn_info_ary_type);
  1009. char name_buf[32];
  1010. TREE_STATIC (fn_info_ary) = 1;
  1011. ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 1);
  1012. DECL_NAME (fn_info_ary) = get_identifier (name_buf);
  1013. DECL_INITIAL (fn_info_ary) = build_constructor (fn_info_ary_type, ctor);
  1014. varpool_node::finalize_decl (fn_info_ary);
  1015. DECL_INITIAL (gcov_info_var)
  1016. = build_info (TREE_TYPE (gcov_info_var), fn_info_ary);
  1017. varpool_node::finalize_decl (gcov_info_var);
  1018. }
  1019. /* Perform file-level initialization. Read in data file, generate name
  1020. of notes file. */
  1021. void
  1022. coverage_init (const char *filename)
  1023. {
  1024. int len = strlen (filename);
  1025. int prefix_len = 0;
  1026. /* Since coverage_init is invoked very early, before the pass
  1027. manager, we need to set up the dumping explicitly. This is
  1028. similar to the handling in finish_optimization_passes. */
  1029. int profile_pass_num =
  1030. g->get_passes ()->get_pass_profile ()->static_pass_number;
  1031. g->get_dumps ()->dump_start (profile_pass_num, NULL);
  1032. if (!profile_data_prefix && !IS_ABSOLUTE_PATH (filename))
  1033. profile_data_prefix = getpwd ();
  1034. if (profile_data_prefix)
  1035. prefix_len = strlen (profile_data_prefix);
  1036. /* Name of da file. */
  1037. da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
  1038. + prefix_len + 2);
  1039. if (profile_data_prefix)
  1040. {
  1041. memcpy (da_file_name, profile_data_prefix, prefix_len);
  1042. da_file_name[prefix_len++] = '/';
  1043. }
  1044. memcpy (da_file_name + prefix_len, filename, len);
  1045. strcpy (da_file_name + prefix_len + len, GCOV_DATA_SUFFIX);
  1046. bbg_file_stamp = local_tick;
  1047. if (flag_auto_profile)
  1048. read_autofdo_file ();
  1049. else if (flag_branch_probabilities)
  1050. read_counts_file ();
  1051. /* Name of bbg file. */
  1052. if (flag_test_coverage && !flag_compare_debug)
  1053. {
  1054. bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
  1055. memcpy (bbg_file_name, filename, len);
  1056. strcpy (bbg_file_name + len, GCOV_NOTE_SUFFIX);
  1057. if (!gcov_open (bbg_file_name, -1))
  1058. {
  1059. error ("cannot open %s", bbg_file_name);
  1060. bbg_file_name = NULL;
  1061. }
  1062. else
  1063. {
  1064. gcov_write_unsigned (GCOV_NOTE_MAGIC);
  1065. gcov_write_unsigned (GCOV_VERSION);
  1066. gcov_write_unsigned (bbg_file_stamp);
  1067. }
  1068. }
  1069. g->get_dumps ()->dump_finish (profile_pass_num);
  1070. }
  1071. /* Performs file-level cleanup. Close notes file, generate coverage
  1072. variables and constructor. */
  1073. void
  1074. coverage_finish (void)
  1075. {
  1076. if (bbg_file_name && gcov_close ())
  1077. unlink (bbg_file_name);
  1078. if (!flag_branch_probabilities && flag_test_coverage
  1079. && (!local_tick || local_tick == (unsigned)-1))
  1080. /* Only remove the da file, if we're emitting coverage code and
  1081. cannot uniquely stamp it. If we can stamp it, libgcov will DTRT. */
  1082. unlink (da_file_name);
  1083. if (coverage_obj_init ())
  1084. {
  1085. vec<constructor_elt, va_gc> *fn_ctor = NULL;
  1086. struct coverage_data *fn;
  1087. for (fn = functions_head; fn; fn = fn->next)
  1088. fn_ctor = coverage_obj_fn (fn_ctor, fn->fn_decl, fn);
  1089. coverage_obj_finish (fn_ctor);
  1090. }
  1091. XDELETEVEC (da_file_name);
  1092. da_file_name = NULL;
  1093. }
  1094. #include "gt-coverage.h"