graph.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. static const char *const graph_ext = ".gml";
  34. static splay_tree gmlrtree = NULL;
  35. static int gmlrid = 1;
  36. /* Open a file with MODE for dumping our graph to.
  37. Return the file pointer. */
  38. static FILE *
  39. open_graph_file (const char *base, const char *mode)
  40. {
  41. size_t namelen = strlen (base);
  42. size_t extlen = strlen (graph_ext) + 1;
  43. char *buf = XALLOCAVEC (char, namelen + extlen);
  44. FILE *fp;
  45. memcpy (buf, base, namelen);
  46. memcpy (buf + namelen, graph_ext, extlen);
  47. fp = fopen (buf, mode);
  48. if (fp == NULL)
  49. fatal_error (input_location, "cannot open %s: %m", buf);
  50. return fp;
  51. }
  52. /* Disable warnings about quoting issues in the pp_xxx calls below
  53. that (intentionally) don't follow GCC diagnostic conventions. */
  54. #if __GNUC__ >= 10
  55. # pragma GCC diagnostic push
  56. # pragma GCC diagnostic ignored "-Wformat-diag"
  57. #endif
  58. /* Draw a basic block BB belonging to the function with FUNCDEF_NO
  59. as its unique number. (used in -fdump-tree-all-graph option) */
  60. static void
  61. draw_cfg_node (pretty_printer *pp, int funcdef_no, basic_block bb, const char *funcname)
  62. {
  63. const char *shape;
  64. const char *fillcolor;
  65. char nbuf[32];
  66. const char *ss=NULL;
  67. memset(nbuf,0,32);
  68. if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
  69. {
  70. shape = "Mdiamond";
  71. /*
  72. fillcolor = "white";
  73. */
  74. fillcolor = "ffffff";
  75. }
  76. else
  77. {
  78. shape = "record";
  79. /*
  80. fillcolor =
  81. BB_PARTITION (bb) == BB_HOT_PARTITION ? "lightpink"
  82. : BB_PARTITION (bb) == BB_COLD_PARTITION ? "lightblue"
  83. : "lightgrey";
  84. */
  85. fillcolor =
  86. BB_PARTITION (bb) == BB_HOT_PARTITION ? "ffb6c1"
  87. : BB_PARTITION (bb) == BB_COLD_PARTITION ? "add8e6"
  88. : "d3d3d3";
  89. }
  90. /*
  91. pp_printf (pp,
  92. "\tfn_%d_basic_block_%d "
  93. "[shape=%s,style=filled,fillcolor=%s,label=\"",
  94. funcdef_no, bb->index, shape, fillcolor);
  95. */
  96. /* node shape is not used */
  97. if (shape) { }
  98. /* few empty lines at start of function() */
  99. if (bb->index == ENTRY_BLOCK)
  100. pp_printf (pp,"\n\n# function number %d\n",funcdef_no);
  101. snprintf(nbuf,32-1,"%d_%d",funcdef_no, bb->index);
  102. ss = ggc_strdup(nbuf);
  103. splay_tree_insert (gmlrtree, (splay_tree_key)ss, (splay_tree_value)gmlrid);
  104. pp_printf (pp,
  105. "\tnode [ id %d graphics [ fill \"#%s\" ] label \"",gmlrid,fillcolor);
  106. if (bb->index == ENTRY_BLOCK)
  107. {
  108. /* pp_string (pp, "ENTRY"); */
  109. pp_printf (pp,"%s ()\nENTRY",funcname);
  110. }
  111. else if (bb->index == EXIT_BLOCK)
  112. {
  113. /* pp_string (pp, "EXIT"); */
  114. pp_printf (pp,"%s ()\nEXIT",funcname);
  115. }
  116. else
  117. {
  118. /* pp_left_brace (pp); */
  119. /* pp_write_text_to_stream (pp); */
  120. dump_bb_for_graph (pp, bb);
  121. /* pp_right_brace (pp); */
  122. }
  123. /*
  124. pp_string (pp, "\"];\n\n");
  125. */
  126. pp_string (pp, "\" ]\n\n");
  127. pp_flush (pp);
  128. gmlrid++;
  129. }
  130. /* Draw all successor edges of a basic block BB belonging to the function
  131. with FUNCDEF_NO as its unique number. */
  132. static void
  133. draw_cfg_node_succ_edges (pretty_printer *pp, int funcdef_no, basic_block bb)
  134. {
  135. char sbuf[32];
  136. char tbuf[32];
  137. char *ss=NULL;
  138. splay_tree_node spn;
  139. int snum=0;
  140. int tnum=0;
  141. edge e;
  142. edge_iterator ei;
  143. memset (sbuf,0,32);
  144. memset (tbuf,0,32);
  145. FOR_EACH_EDGE (e, ei, bb->succs)
  146. {
  147. const char *style = "\"solid,bold\"";
  148. const char *color = "000000"; /* "black"; */
  149. int weight = 10;
  150. if (e->flags & EDGE_FAKE)
  151. {
  152. style = "dotted";
  153. color = "00ff00"; /* "green"; */
  154. weight = 0;
  155. }
  156. else if (e->flags & EDGE_DFS_BACK)
  157. {
  158. style = "\"dotted,bold\"";
  159. color = "0000ff"; /* "blue"; */
  160. weight = 10;
  161. }
  162. else if (e->flags & EDGE_FALLTHRU)
  163. {
  164. color = "0000ff"; /* "blue"; */
  165. weight = 100;
  166. }
  167. if (e->flags & EDGE_ABNORMAL)
  168. color = "ff0000"; /* "red"; */
  169. if (0) {
  170. pp_printf (pp,
  171. "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
  172. "[style=%s,color=%s,weight=%d,constraint=%s",
  173. funcdef_no, e->src->index,
  174. funcdef_no, e->dest->index,
  175. style, color, weight,
  176. (e->flags & (EDGE_FAKE | EDGE_DFS_BACK)) ? "false" : "true");
  177. if (e->probability.initialized_p ())
  178. pp_printf (pp, ",label=\"[%i%%]\"",
  179. e->probability.to_reg_br_prob_base ()
  180. * 100 / REG_BR_PROB_BASE);
  181. pp_printf (pp, "];\n");
  182. }
  183. snprintf(sbuf,32-1,"%d_%d",funcdef_no, e->src->index);
  184. ss=sbuf;
  185. spn=splay_tree_lookup(gmlrtree, (splay_tree_key)ss);
  186. if(spn==NULL){
  187. /* shouldnothappen */
  188. snum=-1;
  189. }else{
  190. snum=(int)spn->value;
  191. }
  192. snprintf(sbuf,32-1,"%d_%d",funcdef_no, e->dest->index);
  193. ss=sbuf;
  194. spn=splay_tree_lookup(gmlrtree, (splay_tree_key)ss);
  195. if(spn==NULL){
  196. /* shouldnothappen */
  197. tnum=-1;
  198. }else{
  199. tnum=(int)spn->value;
  200. }
  201. pp_printf (pp, "\tedge [ source %d target %d graphics [ fill \"#%s\" ] ",
  202. snum,tnum, color);
  203. /* if not initialized, this prints "200%" */
  204. if (e->probability.initialized_p ())
  205. pp_printf (pp, "label \"[%i%%]\"",
  206. e->probability.to_reg_br_prob_base ()
  207. * 100 / REG_BR_PROB_BASE);
  208. pp_printf (pp, "] \n");
  209. }
  210. pp_flush (pp);
  211. }
  212. /* Draw all the basic blocks in the CFG in case loops are not available.
  213. First compute a topological order of the blocks to get a good ranking of
  214. the nodes. Then, if any nodes are not reachable from ENTRY, add them at
  215. the end. */
  216. static void
  217. draw_cfg_nodes_no_loops (pretty_printer *pp, struct function *fun, const char *funcname)
  218. {
  219. int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun));
  220. int i, n;
  221. auto_sbitmap visited (last_basic_block_for_fn (cfun));
  222. bitmap_clear (visited);
  223. n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, true);
  224. for (i = n_basic_blocks_for_fn (fun) - n;
  225. i < n_basic_blocks_for_fn (fun); i++)
  226. {
  227. basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
  228. draw_cfg_node (pp, fun->funcdef_no, bb, funcname);
  229. bitmap_set_bit (visited, bb->index);
  230. }
  231. free (rpo);
  232. if (n != n_basic_blocks_for_fn (fun))
  233. {
  234. /* Some blocks are unreachable. We still want to dump them. */
  235. basic_block bb;
  236. FOR_ALL_BB_FN (bb, fun)
  237. if (! bitmap_bit_p (visited, bb->index))
  238. draw_cfg_node (pp, fun->funcdef_no, bb, funcname);
  239. }
  240. }
  241. /* Draw all the basic blocks in LOOP. Print the blocks in breath-first
  242. order to get a good ranking of the nodes. This function is recursive:
  243. It first prints inner loops, then the body of LOOP itself. */
  244. static void
  245. draw_cfg_nodes_for_loop (pretty_printer *pp, int funcdef_no,
  246. class loop *loop, const char *funcname)
  247. {
  248. basic_block *body;
  249. unsigned int i;
  250. const char *fillcolors[3] = { "grey88", "grey77", "grey66" };
  251. if (0) {
  252. if (loop->header != NULL
  253. && loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
  254. pp_printf (pp,
  255. "\tsubgraph cluster_%d_%d {\n"
  256. "\tstyle=\"filled\";\n"
  257. "\tcolor=\"darkgreen\";\n"
  258. "\tfillcolor=\"%s\";\n"
  259. "\tlabel=\"loop %d\";\n"
  260. "\tlabeljust=l;\n"
  261. "\tpenwidth=2;\n",
  262. funcdef_no, loop->num,
  263. fillcolors[(loop_depth (loop) - 1) % 3],
  264. loop->num);
  265. }
  266. for (class loop *inner = loop->inner; inner; inner = inner->next)
  267. draw_cfg_nodes_for_loop (pp, funcdef_no, inner, funcname);
  268. if (loop->header == NULL)
  269. return;
  270. if (loop->latch == EXIT_BLOCK_PTR_FOR_FN (cfun))
  271. body = get_loop_body (loop);
  272. else
  273. body = get_loop_body_in_bfs_order (loop);
  274. for (i = 0; i < loop->num_nodes; i++)
  275. {
  276. basic_block bb = body[i];
  277. if (bb->loop_father == loop)
  278. draw_cfg_node (pp, funcdef_no, bb, funcname);
  279. }
  280. free (body);
  281. if (0) {
  282. if (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun))
  283. pp_printf (pp, "\t}\n");
  284. }
  285. }
  286. /* Draw all the basic blocks in the CFG in case the loop tree is available.
  287. All loop bodys are printed in clusters. */
  288. static void
  289. draw_cfg_nodes (pretty_printer *pp, struct function *fun, const char *funcname)
  290. {
  291. if (loops_for_fn (fun))
  292. draw_cfg_nodes_for_loop (pp, fun->funcdef_no, get_loop (fun, 0), funcname);
  293. else
  294. draw_cfg_nodes_no_loops (pp, fun, funcname);
  295. }
  296. /* Draw all edges in the CFG. Retreating edges are drawin as not
  297. constraining, this makes the layout of the graph better. */
  298. static void
  299. draw_cfg_edges (pretty_printer *pp, struct function *fun)
  300. {
  301. basic_block bb;
  302. /* Save EDGE_DFS_BACK flag to dfs_back. */
  303. auto_bitmap dfs_back;
  304. edge e;
  305. edge_iterator ei;
  306. unsigned int idx = 0;
  307. FOR_EACH_BB_FN (bb, cfun)
  308. FOR_EACH_EDGE (e, ei, bb->succs)
  309. {
  310. if (e->flags & EDGE_DFS_BACK)
  311. bitmap_set_bit (dfs_back, idx);
  312. idx++;
  313. }
  314. mark_dfs_back_edges ();
  315. FOR_ALL_BB_FN (bb, cfun)
  316. draw_cfg_node_succ_edges (pp, fun->funcdef_no, bb);
  317. /* Restore EDGE_DFS_BACK flag from dfs_back. */
  318. idx = 0;
  319. FOR_EACH_BB_FN (bb, cfun)
  320. FOR_EACH_EDGE (e, ei, bb->succs)
  321. {
  322. if (bitmap_bit_p (dfs_back, idx))
  323. e->flags |= EDGE_DFS_BACK;
  324. else
  325. e->flags &= ~EDGE_DFS_BACK;
  326. idx++;
  327. }
  328. /* Add an invisible edge from ENTRY to EXIT, to improve the graph layout. */
  329. if (0) {
  330. pp_printf (pp,
  331. "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
  332. "[style=\"invis\",constraint=true];\n",
  333. fun->funcdef_no, ENTRY_BLOCK,
  334. fun->funcdef_no, EXIT_BLOCK);
  335. }
  336. /* adding a white edge line, does not improve layout */
  337. if (0) {
  338. pp_printf (pp, "\tedge [ source %d%d target %d%d graphics [ fill \"#%s\" ] ]\n",
  339. fun->funcdef_no, ENTRY_BLOCK,
  340. fun->funcdef_no, EXIT_BLOCK, "ffffff");
  341. }
  342. pp_flush (pp);
  343. }
  344. /* Print a graphical representation of the CFG of function FUN.
  345. First print all basic blocks. Draw all edges at the end to get
  346. subgraphs right for GraphViz, which requires nodes to be defined
  347. before edges to cluster nodes properly. */
  348. void DEBUG_FUNCTION
  349. print_graph_cfg (FILE *fp, struct function *fun)
  350. {
  351. pretty_printer graph_slim_pp;
  352. graph_slim_pp.buffer->stream = fp;
  353. pretty_printer *const pp = &graph_slim_pp;
  354. const char *funcname = function_name (fun);
  355. if (0) {
  356. pp_printf (pp, "subgraph \"cluster_%s\" {\n"
  357. "\tstyle=\"dashed\";\n"
  358. "\tcolor=\"black\";\n"
  359. "\tlabel=\"%s ()\";\n",
  360. funcname, funcname);
  361. }
  362. draw_cfg_nodes (pp, fun, funcname);
  363. draw_cfg_edges (pp, fun);
  364. /* pp_printf (pp, "}\n"); */
  365. pp_flush (pp);
  366. }
  367. /* Overload with additional flag argument. */
  368. void DEBUG_FUNCTION
  369. print_graph_cfg (FILE *fp, struct function *fun, dump_flags_t flags)
  370. {
  371. dump_flags_t saved_dump_flags = dump_flags;
  372. dump_flags = flags;
  373. print_graph_cfg (fp, fun);
  374. dump_flags = saved_dump_flags;
  375. }
  376. /* Print a graphical representation of the CFG of function FUN.
  377. First print all basic blocks. Draw all edges at the end to get
  378. subgraphs right for GraphViz, which requires nodes to be defined
  379. before edges to cluster nodes properly. */
  380. void
  381. print_graph_cfg (const char *base, struct function *fun)
  382. {
  383. FILE *fp = open_graph_file (base, "a");
  384. print_graph_cfg (fp, fun);
  385. fclose (fp);
  386. }
  387. /* Start the dump of a graph. */
  388. static void
  389. start_graph_dump (FILE *fp, const char *base)
  390. {
  391. pretty_printer graph_slim_pp;
  392. graph_slim_pp.buffer->stream = fp;
  393. pretty_printer *const pp = &graph_slim_pp;
  394. if (base) { }
  395. /* pp_string (pp, "digraph \""); */
  396. pp_string (pp, "# RTL graph generated with GCC 10.1\ngraph\n[\n directed 1\n");
  397. if (0){
  398. pp_write_text_to_stream (pp);
  399. pp_string (pp, base);
  400. pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
  401. pp_string (pp, "\" {\n");
  402. pp_string (pp, "overlap=false;\n");
  403. }
  404. pp_flush (pp);
  405. }
  406. /* End the dump of a graph. */
  407. static void
  408. end_graph_dump (FILE *fp)
  409. {
  410. /* fputs ("}\n", fp); */
  411. fputs ("]\n", fp);
  412. }
  413. static void gml_del_key (splay_tree_key k)
  414. {
  415. if(k) {
  416. /* free ((void *)k); */
  417. }
  418. return;
  419. }
  420. /* Similar as clean_dump_file, but this time for graph output files. */
  421. void
  422. clean_graph_dump_file (const char *base)
  423. {
  424. FILE *fp = open_graph_file (base, "w");
  425. gmlrtree=splay_tree_new(splay_tree_compare_strings,gml_del_key,NULL);
  426. gmlrid = 1;
  427. start_graph_dump (fp, base);
  428. fclose (fp);
  429. }
  430. /* Do final work on the graph output file. */
  431. void
  432. finish_graph_dump_file (const char *base)
  433. {
  434. FILE *fp = open_graph_file (base, "a");
  435. end_graph_dump (fp);
  436. /* compiler error crash at this line */
  437. if(0) (void)splay_tree_delete (gmlrtree);
  438. fclose (fp);
  439. }
  440. #if __GNUC__ >= 10
  441. # pragma GCC diagnostic pop
  442. #endif