statistics.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* Optimization statistics functions.
  2. Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. Contributed by Richard Guenther <rguenther@suse.de>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tree-pass.h"
  20. #include "tree-dump.h"
  21. #include "statistics.h"
  22. #include "hash-table.h"
  23. #include "hashtab.h"
  24. #include "hash-set.h"
  25. #include "vec.h"
  26. #include "machmode.h"
  27. #include "tm.h"
  28. #include "hard-reg-set.h"
  29. #include "input.h"
  30. #include "function.h"
  31. #include "context.h"
  32. #include "pass_manager.h"
  33. static int statistics_dump_nr;
  34. static int statistics_dump_flags;
  35. static FILE *statistics_dump_file;
  36. /* Statistics entry. A integer counter associated to a string ID
  37. and value. */
  38. typedef struct statistics_counter_s {
  39. const char *id;
  40. int val;
  41. bool histogram_p;
  42. unsigned HOST_WIDE_INT count;
  43. unsigned HOST_WIDE_INT prev_dumped_count;
  44. } statistics_counter_t;
  45. /* Hashtable helpers. */
  46. struct stats_counter_hasher
  47. {
  48. typedef statistics_counter_t value_type;
  49. typedef statistics_counter_t compare_type;
  50. static inline hashval_t hash (const value_type *);
  51. static inline bool equal (const value_type *, const compare_type *);
  52. static inline void remove (value_type *);
  53. };
  54. /* Hash a statistic counter by its string ID. */
  55. inline hashval_t
  56. stats_counter_hasher::hash (const value_type *c)
  57. {
  58. return htab_hash_string (c->id) + c->val;
  59. }
  60. /* Compare two statistic counters by their string IDs. */
  61. inline bool
  62. stats_counter_hasher::equal (const value_type *c1, const compare_type *c2)
  63. {
  64. return c1->val == c2->val && strcmp (c1->id, c2->id) == 0;
  65. }
  66. /* Free a statistics entry. */
  67. inline void
  68. stats_counter_hasher::remove (value_type *v)
  69. {
  70. free (CONST_CAST (char *, v->id));
  71. free (v);
  72. }
  73. typedef hash_table<stats_counter_hasher> stats_counter_table_type;
  74. /* Array of statistic hashes, indexed by pass id. */
  75. static stats_counter_table_type **statistics_hashes;
  76. static unsigned nr_statistics_hashes;
  77. /* Return the current hashtable to be used for recording or printing
  78. statistics. */
  79. static stats_counter_table_type *
  80. curr_statistics_hash (void)
  81. {
  82. unsigned idx;
  83. gcc_assert (current_pass->static_pass_number >= 0);
  84. idx = current_pass->static_pass_number;
  85. if (idx < nr_statistics_hashes
  86. && statistics_hashes[idx])
  87. return statistics_hashes[idx];
  88. if (idx >= nr_statistics_hashes)
  89. {
  90. statistics_hashes = XRESIZEVEC (stats_counter_table_type *,
  91. statistics_hashes, idx+1);
  92. memset (statistics_hashes + nr_statistics_hashes, 0,
  93. (idx + 1 - nr_statistics_hashes)
  94. * sizeof (stats_counter_table_type *));
  95. nr_statistics_hashes = idx + 1;
  96. }
  97. statistics_hashes[idx] = new stats_counter_table_type (15);
  98. return statistics_hashes[idx];
  99. }
  100. /* Helper for statistics_fini_pass. Print the counter difference
  101. since the last dump for the pass dump files. */
  102. int
  103. statistics_fini_pass_1 (statistics_counter_t **slot,
  104. void *data ATTRIBUTE_UNUSED)
  105. {
  106. statistics_counter_t *counter = *slot;
  107. unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
  108. if (count == 0)
  109. return 1;
  110. if (counter->histogram_p)
  111. fprintf (dump_file, "%s == %d: " HOST_WIDE_INT_PRINT_DEC "\n",
  112. counter->id, counter->val, count);
  113. else
  114. fprintf (dump_file, "%s: " HOST_WIDE_INT_PRINT_DEC "\n",
  115. counter->id, count);
  116. counter->prev_dumped_count = counter->count;
  117. return 1;
  118. }
  119. /* Helper for statistics_fini_pass. Print the counter difference
  120. since the last dump for the statistics dump. */
  121. int
  122. statistics_fini_pass_2 (statistics_counter_t **slot,
  123. void *data ATTRIBUTE_UNUSED)
  124. {
  125. statistics_counter_t *counter = *slot;
  126. unsigned HOST_WIDE_INT count = counter->count - counter->prev_dumped_count;
  127. if (count == 0)
  128. return 1;
  129. counter->prev_dumped_count = counter->count;
  130. if (counter->histogram_p)
  131. fprintf (statistics_dump_file,
  132. "%d %s \"%s == %d\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
  133. current_pass->static_pass_number,
  134. current_pass->name,
  135. counter->id, counter->val,
  136. current_function_name (),
  137. count);
  138. else
  139. fprintf (statistics_dump_file,
  140. "%d %s \"%s\" \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
  141. current_pass->static_pass_number,
  142. current_pass->name,
  143. counter->id,
  144. current_function_name (),
  145. count);
  146. counter->prev_dumped_count = counter->count;
  147. return 1;
  148. }
  149. /* Helper for statistics_fini_pass, reset the counters. */
  150. int
  151. statistics_fini_pass_3 (statistics_counter_t **slot,
  152. void *data ATTRIBUTE_UNUSED)
  153. {
  154. statistics_counter_t *counter = *slot;
  155. counter->prev_dumped_count = counter->count;
  156. return 1;
  157. }
  158. /* Dump the current statistics incrementally. */
  159. void
  160. statistics_fini_pass (void)
  161. {
  162. if (current_pass->static_pass_number == -1)
  163. return;
  164. if (dump_file
  165. && dump_flags & TDF_STATS)
  166. {
  167. fprintf (dump_file, "\n");
  168. fprintf (dump_file, "Pass statistics:\n");
  169. fprintf (dump_file, "----------------\n");
  170. curr_statistics_hash ()
  171. ->traverse_noresize <void *, statistics_fini_pass_1> (NULL);
  172. fprintf (dump_file, "\n");
  173. }
  174. if (statistics_dump_file
  175. && !(statistics_dump_flags & TDF_STATS
  176. || statistics_dump_flags & TDF_DETAILS))
  177. curr_statistics_hash ()
  178. ->traverse_noresize <void *, statistics_fini_pass_2> (NULL);
  179. curr_statistics_hash ()
  180. ->traverse_noresize <void *, statistics_fini_pass_3> (NULL);
  181. }
  182. /* Helper for printing summary information. */
  183. int
  184. statistics_fini_1 (statistics_counter_t **slot, opt_pass *pass)
  185. {
  186. statistics_counter_t *counter = *slot;
  187. if (counter->count == 0)
  188. return 1;
  189. if (counter->histogram_p)
  190. fprintf (statistics_dump_file,
  191. "%d %s \"%s == %d\" " HOST_WIDE_INT_PRINT_DEC "\n",
  192. pass->static_pass_number,
  193. pass->name,
  194. counter->id, counter->val,
  195. counter->count);
  196. else
  197. fprintf (statistics_dump_file,
  198. "%d %s \"%s\" " HOST_WIDE_INT_PRINT_DEC "\n",
  199. pass->static_pass_number,
  200. pass->name,
  201. counter->id,
  202. counter->count);
  203. return 1;
  204. }
  205. /* Finish the statistics and dump summary information. */
  206. void
  207. statistics_fini (void)
  208. {
  209. gcc::pass_manager *passes = g->get_passes ();
  210. if (!statistics_dump_file)
  211. return;
  212. if (statistics_dump_flags & TDF_STATS)
  213. {
  214. unsigned i;
  215. for (i = 0; i < nr_statistics_hashes; ++i)
  216. if (statistics_hashes[i]
  217. && passes->get_pass_for_id (i) != NULL)
  218. statistics_hashes[i]
  219. ->traverse_noresize <opt_pass *, statistics_fini_1>
  220. (passes->get_pass_for_id (i));
  221. }
  222. dump_end (statistics_dump_nr, statistics_dump_file);
  223. }
  224. /* Register the statistics dump file. */
  225. void
  226. statistics_early_init (void)
  227. {
  228. gcc::dump_manager *dumps = g->get_dumps ();
  229. statistics_dump_nr = dumps->dump_register (".statistics", "statistics",
  230. "statistics", TDF_TREE,
  231. OPTGROUP_NONE,
  232. false);
  233. }
  234. /* Init the statistics. */
  235. void
  236. statistics_init (void)
  237. {
  238. gcc::dump_manager *dumps = g->get_dumps ();
  239. statistics_dump_file = dump_begin (statistics_dump_nr, NULL);
  240. statistics_dump_flags = dumps->get_dump_file_info (statistics_dump_nr)->pflags;
  241. }
  242. /* Lookup or add a statistics counter in the hashtable HASH with ID, VAL
  243. and HISTOGRAM_P. */
  244. static statistics_counter_t *
  245. lookup_or_add_counter (stats_counter_table_type *hash, const char *id, int val,
  246. bool histogram_p)
  247. {
  248. statistics_counter_t **counter;
  249. statistics_counter_t c;
  250. c.id = id;
  251. c.val = val;
  252. counter = hash->find_slot (&c, INSERT);
  253. if (!*counter)
  254. {
  255. *counter = XNEW (struct statistics_counter_s);
  256. (*counter)->id = xstrdup (id);
  257. (*counter)->val = val;
  258. (*counter)->histogram_p = histogram_p;
  259. (*counter)->prev_dumped_count = 0;
  260. (*counter)->count = 0;
  261. }
  262. return *counter;
  263. }
  264. /* Add statistics information about event ID in function FN.
  265. This will increment the counter associated with ID by INCR.
  266. It will also dump the event to the global statistics file if requested. */
  267. void
  268. statistics_counter_event (struct function *fn, const char *id, int incr)
  269. {
  270. statistics_counter_t *counter;
  271. if ((!(dump_flags & TDF_STATS)
  272. && !statistics_dump_file)
  273. || incr == 0)
  274. return;
  275. if (current_pass->static_pass_number != -1)
  276. {
  277. counter = lookup_or_add_counter (curr_statistics_hash (), id, 0, false);
  278. gcc_assert (!counter->histogram_p);
  279. counter->count += incr;
  280. }
  281. if (!statistics_dump_file
  282. || !(statistics_dump_flags & TDF_DETAILS))
  283. return;
  284. fprintf (statistics_dump_file,
  285. "%d %s \"%s\" \"%s\" %d\n",
  286. current_pass->static_pass_number,
  287. current_pass->name,
  288. id,
  289. function_name (fn),
  290. incr);
  291. }
  292. /* Add statistics information about event ID in function FN with the
  293. histogram value VAL.
  294. It will dump the event to the global statistics file if requested. */
  295. void
  296. statistics_histogram_event (struct function *fn, const char *id, int val)
  297. {
  298. statistics_counter_t *counter;
  299. if (!(dump_flags & TDF_STATS)
  300. && !statistics_dump_file)
  301. return;
  302. counter = lookup_or_add_counter (curr_statistics_hash (), id, val, true);
  303. gcc_assert (counter->histogram_p);
  304. counter->count += 1;
  305. if (!statistics_dump_file
  306. || !(statistics_dump_flags & TDF_DETAILS))
  307. return;
  308. fprintf (statistics_dump_file,
  309. "%d %s \"%s == %d\" \"%s\" 1\n",
  310. current_pass->static_pass_number,
  311. current_pass->name,
  312. id, val,
  313. function_name (fn));
  314. }