graph.c.orig 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Output routines for graphical representation.
  2. Copyright (C) 1998-2020 Free Software Foundation, Inc.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  4. Rewritten for DOT output by Steven Bosscher, 2012.
  5. This file is part of GCC.
  6. GCC is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GCC; see the file COPYING3. If not see
  16. <http://www.gnu.org/licenses/>. */
  17. #include "config.h"
  18. #include "system.h"
  19. #include "coretypes.h"
  20. #include "backend.h"
  21. #include "cfghooks.h"
  22. #include "pretty-print.h"
  23. #include "diagnostic-core.h" /* for fatal_error */
  24. #include "cfganal.h"
  25. #include "cfgloop.h"
  26. #include "graph.h"
  27. #include "dumpfile.h"
  28. /* DOT files with the .dot extension are recognized as document templates
  29. by a well-known piece of word processing software out of Redmond, WA.
  30. Therefore some recommend using the .gv extension instead. Obstinately
  31. ignore that recommendation... */
  32. static const char *const graph_ext = ".dot";
  33. /* Open a file with MODE for dumping our graph to.
  34. Return the file pointer. */
  35. static FILE *
  36. open_graph_file (const char *base, const char *mode)
  37. {
  38. size_t namelen = strlen (base);
  39. size_t extlen = strlen (graph_ext) + 1;
  40. char *buf = XALLOCAVEC (char, namelen + extlen);
  41. FILE *fp;
  42. memcpy (buf, base, namelen);
  43. memcpy (buf + namelen, graph_ext, extlen);
  44. fp = fopen (buf, mode);
  45. if (fp == NULL)
  46. fatal_error (input_location, "cannot open %s: %m", buf);
  47. return fp;
  48. }
  49. /* Disable warnings about quoting issues in the pp_xxx calls below
  50. that (intentionally) don't follow GCC diagnostic conventions. */
  51. #if __GNUC__ >= 10
  52. # pragma GCC diagnostic push
  53. # pragma GCC diagnostic ignored "-Wformat-diag"
  54. #endif
  55. /* Draw a basic block BB belonging to the function with FUNCDEF_NO
  56. as its unique number. */
  57. static void
  58. draw_cfg_node (pretty_printer *pp, int funcdef_no, basic_block bb)
  59. {
  60. const char *shape;
  61. const char *fillcolor;
  62. if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
  63. {
  64. shape = "Mdiamond";
  65. fillcolor = "white";
  66. }
  67. else
  68. {
  69. shape = "record";
  70. fillcolor =
  71. BB_PARTITION (bb) == BB_HOT_PARTITION ? "lightpink"
  72. : BB_PARTITION (bb) == BB_COLD_PARTITION ? "lightblue"
  73. : "lightgrey";
  74. }
  75. pp_printf (pp,
  76. "\tfn_%d_basic_block_%d "
  77. "[shape=%s,style=filled,fillcolor=%s,label=\"",
  78. funcdef_no, bb->index, shape, fillcolor);
  79. if (bb->index == ENTRY_BLOCK)
  80. pp_string (pp, "ENTRY");
  81. else if (bb->index == EXIT_BLOCK)
  82. pp_string (pp, "EXIT");
  83. else
  84. {
  85. pp_left_brace (pp);
  86. pp_write_text_to_stream (pp);
  87. dump_bb_for_graph (pp, bb);
  88. pp_right_brace (pp);
  89. }
  90. pp_string (pp, "\"];\n\n");
  91. pp_flush (pp);
  92. }
  93. /* Draw all successor edges of a basic block BB belonging to the function
  94. with FUNCDEF_NO as its unique number. */
  95. static void
  96. draw_cfg_node_succ_edges (pretty_printer *pp, int funcdef_no, basic_block bb)
  97. {
  98. edge e;
  99. edge_iterator ei;
  100. FOR_EACH_EDGE (e, ei, bb->succs)
  101. {
  102. const char *style = "\"solid,bold\"";
  103. const char *color = "black";
  104. int weight = 10;
  105. if (e->flags & EDGE_FAKE)
  106. {
  107. style = "dotted";
  108. color = "green";
  109. weight = 0;
  110. }
  111. else if (e->flags & EDGE_DFS_BACK)
  112. {
  113. style = "\"dotted,bold\"";
  114. color = "blue";
  115. weight = 10;
  116. }
  117. else if (e->flags & EDGE_FALLTHRU)
  118. {
  119. color = "blue";
  120. weight = 100;
  121. }
  122. if (e->flags & EDGE_ABNORMAL)
  123. color = "red";
  124. pp_printf (pp,
  125. "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
  126. "[style=%s,color=%s,weight=%d,constraint=%s",
  127. funcdef_no, e->src->index,
  128. funcdef_no, e->dest->index,
  129. style, color, weight,
  130. (e->flags & (EDGE_FAKE | EDGE_DFS_BACK)) ? "false" : "true");
  131. if (e->probability.initialized_p ())
  132. pp_printf (pp, ",label=\"[%i%%]\"",
  133. e->probability.to_reg_br_prob_base ()
  134. * 100 / REG_BR_PROB_BASE);
  135. pp_printf (pp, "];\n");
  136. }
  137. pp_flush (pp);
  138. }
  139. /* Draw all the basic blocks in the CFG in case loops are not available.
  140. First compute a topological order of the blocks to get a good ranking of
  141. the nodes. Then, if any nodes are not reachable from ENTRY, add them at
  142. the end. */
  143. static void
  144. draw_cfg_nodes_no_loops (pretty_printer *pp, struct function *fun)
  145. {
  146. int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
  147. int i, n;
  148. auto_sbitmap visited (last_basic_block_for_fn (cfun));
  149. bitmap_clear (visited);
  150. n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, true);
  151. for (i = n_basic_blocks_for_fn (fun) - n;
  152. i < n_basic_blocks_for_fn (fun); i++)
  153. {
  154. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
  155. draw_cfg_node (pp, fun->funcdef_no, bb);
  156. bitmap_set_bit (visited, bb->index);
  157. }
  158. free (rpo);
  159. if (n != n_basic_blocks_for_fn (fun))
  160. {
  161. /* Some blocks are unreachable. We still want to dump them. */
  162. basic_block bb;
  163. FOR_ALL_BB_FN (bb, fun)
  164. if (! bitmap_bit_p (visited, bb->index))
  165. draw_cfg_node (pp, fun->funcdef_no, bb);
  166. }
  167. }
  168. /* Draw all the basic blocks in LOOP. Print the blocks in breath-first
  169. order to get a good ranking of the nodes. This function is recursive:
  170. It first prints inner loops, then the body of LOOP itself. */
  171. static void
  172. draw_cfg_nodes_for_loop (pretty_printer *pp, int funcdef_no,
  173. class loop *loop)
  174. {
  175. basic_block *body;
  176. unsigned int i;
  177. const char *fillcolors[3] = { "grey88", "grey77", "grey66" };
  178. if (loop->header != NULL
  179. && loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
  180. pp_printf (pp,
  181. "\tsubgraph cluster_%d_%d {\n"
  182. "\tstyle=\"filled\";\n"
  183. "\tcolor=\"darkgreen\";\n"
  184. "\tfillcolor=\"%s\";\n"
  185. "\tlabel=\"loop %d\";\n"
  186. "\tlabeljust=l;\n"
  187. "\tpenwidth=2;\n",
  188. funcdef_no, loop->num,
  189. fillcolors[(loop_depth (loop) - 1) % 3],
  190. loop->num);
  191. for (class loop *inner = loop->inner; inner; inner = inner->next)
  192. draw_cfg_nodes_for_loop (pp, funcdef_no, inner);
  193. if (loop->header == NULL)
  194. return;
  195. if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
  196. body = get_loop_body (loop);
  197. else
  198. body = get_loop_body_in_bfs_order (loop);
  199. for (i = 0; i < loop->num_nodes; i++)
  200. {
  201. basic_block bb = body[i];
  202. if (bb->loop_father == loop)
  203. draw_cfg_node (pp, funcdef_no, bb);
  204. }
  205. free (body);
  206. if (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
  207. pp_printf (pp, "\t}\n");
  208. }
  209. /* Draw all the basic blocks in the CFG in case the loop tree is available.
  210. All loop bodys are printed in clusters. */
  211. static void
  212. draw_cfg_nodes (pretty_printer *pp, struct function *fun)
  213. {
  214. if (loops_for_fn (fun))
  215. draw_cfg_nodes_for_loop (pp, fun->funcdef_no, get_loop (fun, 0));
  216. else
  217. draw_cfg_nodes_no_loops (pp, fun);
  218. }
  219. /* Draw all edges in the CFG. Retreating edges are drawin as not
  220. constraining, this makes the layout of the graph better. */
  221. static void
  222. draw_cfg_edges (pretty_printer *pp, struct function *fun)
  223. {
  224. basic_block bb;
  225. /* Save EDGE_DFS_BACK flag to dfs_back. */
  226. auto_bitmap dfs_back;
  227. edge e;
  228. edge_iterator ei;
  229. unsigned int idx = 0;
  230. FOR_EACH_BB_FN (bb, cfun)
  231. FOR_EACH_EDGE (e, ei, bb->succs)
  232. {
  233. if (e->flags & EDGE_DFS_BACK)
  234. bitmap_set_bit (dfs_back, idx);
  235. idx++;
  236. }
  237. mark_dfs_back_edges ();
  238. FOR_ALL_BB_FN (bb, cfun)
  239. draw_cfg_node_succ_edges (pp, fun->funcdef_no, bb);
  240. /* Restore EDGE_DFS_BACK flag from dfs_back. */
  241. idx = 0;
  242. FOR_EACH_BB_FN (bb, cfun)
  243. FOR_EACH_EDGE (e, ei, bb->succs)
  244. {
  245. if (bitmap_bit_p (dfs_back, idx))
  246. e->flags |= EDGE_DFS_BACK;
  247. else
  248. e->flags &= ~EDGE_DFS_BACK;
  249. idx++;
  250. }
  251. /* Add an invisible edge from ENTRY to EXIT, to improve the graph layout. */
  252. pp_printf (pp,
  253. "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
  254. "[style=\"invis\",constraint=true];\n",
  255. fun->funcdef_no, ENTRY_BLOCK,
  256. fun->funcdef_no, EXIT_BLOCK);
  257. pp_flush (pp);
  258. }
  259. /* Print a graphical representation of the CFG of function FUN.
  260. First print all basic blocks. Draw all edges at the end to get
  261. subgraphs right for GraphViz, which requires nodes to be defined
  262. before edges to cluster nodes properly. */
  263. void DEBUG_FUNCTION
  264. print_graph_cfg (FILE *fp, struct function *fun)
  265. {
  266. pretty_printer graph_slim_pp;
  267. graph_slim_pp.buffer->stream = fp;
  268. pretty_printer *const pp = &graph_slim_pp;
  269. const char *funcname = function_name (fun);
  270. pp_printf (pp, "subgraph \"cluster_%s\" {\n"
  271. "\tstyle=\"dashed\";\n"
  272. "\tcolor=\"black\";\n"
  273. "\tlabel=\"%s ()\";\n",
  274. funcname, funcname);
  275. draw_cfg_nodes (pp, fun);
  276. draw_cfg_edges (pp, fun);
  277. pp_printf (pp, "}\n");
  278. pp_flush (pp);
  279. }
  280. /* Overload with additional flag argument. */
  281. void DEBUG_FUNCTION
  282. print_graph_cfg (FILE *fp, struct function *fun, dump_flags_t flags)
  283. {
  284. dump_flags_t saved_dump_flags = dump_flags;
  285. dump_flags = flags;
  286. print_graph_cfg (fp, fun);
  287. dump_flags = saved_dump_flags;
  288. }
  289. /* Print a graphical representation of the CFG of function FUN.
  290. First print all basic blocks. Draw all edges at the end to get
  291. subgraphs right for GraphViz, which requires nodes to be defined
  292. before edges to cluster nodes properly. */
  293. void
  294. print_graph_cfg (const char *base, struct function *fun)
  295. {
  296. FILE *fp = open_graph_file (base, "a");
  297. print_graph_cfg (fp, fun);
  298. fclose (fp);
  299. }
  300. /* Start the dump of a graph. */
  301. static void
  302. start_graph_dump (FILE *fp, const char *base)
  303. {
  304. pretty_printer graph_slim_pp;
  305. graph_slim_pp.buffer->stream = fp;
  306. pretty_printer *const pp = &graph_slim_pp;
  307. pp_string (pp, "digraph \"");
  308. pp_write_text_to_stream (pp);
  309. pp_string (pp, base);
  310. pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
  311. pp_string (pp, "\" {\n");
  312. pp_string (pp, "overlap=false;\n");
  313. pp_flush (pp);
  314. }
  315. /* End the dump of a graph. */
  316. static void
  317. end_graph_dump (FILE *fp)
  318. {
  319. fputs ("}\n", fp);
  320. }
  321. /* Similar as clean_dump_file, but this time for graph output files. */
  322. void
  323. clean_graph_dump_file (const char *base)
  324. {
  325. FILE *fp = open_graph_file (base, "w");
  326. start_graph_dump (fp, base);
  327. fclose (fp);
  328. }
  329. /* Do final work on the graph output file. */
  330. void
  331. finish_graph_dump_file (const char *base)
  332. {
  333. FILE *fp = open_graph_file (base, "a");
  334. end_graph_dump (fp);
  335. fclose (fp);
  336. }
  337. #if __GNUC__ >= 10
  338. # pragma GCC diagnostic pop
  339. #endif