1 /* Output routines for graphical representation.
2 Copyright (C) 1998-2012
3 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 Rewritten for DOT output by Steven Bosscher, 2012.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
26 #include "diagnostic-core.h" /* for fatal_error */
27 #include "basic-block.h"
30 #include "pretty-print.h"
32 /* DOT files with the .dot extension are recognized as document templates
33 by a well-known piece of word processing software out of Redmond, WA.
34 Therefore some recommend using the .gv extension instead. Obstinately
35 ignore that recommendatition... */
36 static const char *const graph_ext
= ".dot";
38 /* Open a file with MODE for dumping our graph to.
39 Return the file pointer. */
41 open_graph_file (const char *base
, const char *mode
)
43 size_t namelen
= strlen (base
);
44 size_t extlen
= strlen (graph_ext
) + 1;
45 char *buf
= XALLOCAVEC (char, namelen
+ extlen
);
48 memcpy (buf
, base
, namelen
);
49 memcpy (buf
+ namelen
, graph_ext
, extlen
);
51 fp
= fopen (buf
, mode
);
53 fatal_error ("can%'t open %s: %m", buf
);
58 /* Return a pretty-print buffer for output to file FP. */
60 static pretty_printer
*
61 init_graph_slim_pretty_print (FILE *fp
)
63 static bool initialized
= false;
64 static pretty_printer graph_slim_pp
;
68 pp_construct (&graph_slim_pp
, /*prefix=*/NULL
, /*linewidth=*/0);
72 gcc_assert (! pp_last_position_in_text (&graph_slim_pp
));
74 graph_slim_pp
.buffer
->stream
= fp
;
75 return &graph_slim_pp
;
78 /* Draw a basic block BB belonging to the function with FUNCDEF_NO
79 as its unique number. */
81 draw_cfg_node (pretty_printer
*pp
, int funcdef_no
, basic_block bb
)
84 const char *fillcolor
;
86 if (bb
->index
== ENTRY_BLOCK
|| bb
->index
== EXIT_BLOCK
)
95 BB_PARTITION (bb
) == BB_HOT_PARTITION
? "lightpink"
96 : BB_PARTITION (bb
) == BB_COLD_PARTITION
? "lightblue"
101 "\tfn_%d_basic_block_%d "
102 "[shape=%s,style=filled,fillcolor=%s,label=\"",
103 funcdef_no
, bb
->index
, shape
, fillcolor
);
105 if (bb
->index
== ENTRY_BLOCK
)
106 pp_string (pp
, "ENTRY");
107 else if (bb
->index
== EXIT_BLOCK
)
108 pp_string (pp
, "EXIT");
111 pp_character (pp
, '{');
112 pp_write_text_to_stream (pp
);
113 dump_bb_for_graph (pp
, bb
);
114 pp_character (pp
, '}');
117 pp_string (pp
, "\"];\n\n");
121 /* Draw all successor edges of a basic block BB belonging to the function
122 with FUNCDEF_NO as its unique number. */
124 draw_cfg_node_succ_edges (pretty_printer
*pp
, int funcdef_no
, basic_block bb
)
128 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
130 const char *style
= "\"solid,bold\"";
131 const char *color
= "black";
134 if (e
->flags
& EDGE_FAKE
)
140 else if (e
->flags
& EDGE_DFS_BACK
)
142 style
= "\"dotted,bold\"";
146 else if (e
->flags
& EDGE_FALLTHRU
)
152 if (e
->flags
& EDGE_ABNORMAL
)
156 "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
157 "[style=%s,color=%s,weight=%d,constraint=%s];\n",
158 funcdef_no
, e
->src
->index
,
159 funcdef_no
, e
->dest
->index
,
160 style
, color
, weight
,
161 (e
->flags
& (EDGE_FAKE
| EDGE_DFS_BACK
)) ? "false" : "true");
166 /* Print a graphical representation of the CFG of function FUN. */
169 print_graph_cfg (const char *base
, struct function
*fun
)
171 const char *funcname
= function_name (fun
);
172 int funcdef_no
= fun
->funcdef_no
;
173 FILE *fp
= open_graph_file (base
, "a");
174 int *rpo
= XNEWVEC (int, n_basic_blocks
);
177 pretty_printer
*pp
= init_graph_slim_pretty_print (fp
);
179 pp_printf (pp
, "subgraph \"%s\" {\n"
180 "\tcolor=\"black\";\n"
184 /* First print all basic blocks.
185 Visit the blocks in reverse post order to get a good ranking
187 n
= pre_and_rev_post_order_compute (NULL
, rpo
, true);
188 for (i
= 0; i
< n
; i
++)
189 draw_cfg_node (pp
, funcdef_no
, BASIC_BLOCK (rpo
[i
]));
191 /* Draw all edges at the end to get subgraphs right for GraphViz,
192 which requires nodes to be defined before edges to cluster
195 Draw retreating edges as not constraining, this makes the layout
196 of the graph better. (??? Calling mark_dfs_back may change the
197 compiler's behavior when dumping, but computing back edges here
198 for ourselves is also not desirable.) */
199 mark_dfs_back_edges ();
201 draw_cfg_node_succ_edges (pp
, funcdef_no
, bb
);
203 pp_printf (pp
, "\t}\n");
208 /* Start the dump of a graph. */
210 start_graph_dump (FILE *fp
)
212 fputs ("digraph \"\" {\n"
217 /* End the dump of a graph. */
219 end_graph_dump (FILE *fp
)
224 /* Similar as clean_dump_file, but this time for graph output files. */
226 clean_graph_dump_file (const char *base
)
228 FILE *fp
= open_graph_file (base
, "w");
229 start_graph_dump (fp
);
234 /* Do final work on the graph output file. */
236 finish_graph_dump_file (const char *base
)
238 FILE *fp
= open_graph_file (base
, "a");