re PR tree-optimization/54570 (FAIL: gcc.dg/builtin-object-size-8.c execution test)
[gcc.git] / gcc / graph.c
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.
6
7 This file is part of GCC.
8
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
12 version.
13
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
17 for more details.
18
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/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "diagnostic-core.h" /* for fatal_error */
27 #include "basic-block.h"
28 #include "graph.h"
29 #include "dumpfile.h"
30 #include "pretty-print.h"
31
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";
37
38 /* Open a file with MODE for dumping our graph to.
39 Return the file pointer. */
40 static FILE *
41 open_graph_file (const char *base, const char *mode)
42 {
43 size_t namelen = strlen (base);
44 size_t extlen = strlen (graph_ext) + 1;
45 char *buf = XALLOCAVEC (char, namelen + extlen);
46 FILE *fp;
47
48 memcpy (buf, base, namelen);
49 memcpy (buf + namelen, graph_ext, extlen);
50
51 fp = fopen (buf, mode);
52 if (fp == NULL)
53 fatal_error ("can%'t open %s: %m", buf);
54
55 return fp;
56 }
57
58 /* Return a pretty-print buffer for output to file FP. */
59
60 static pretty_printer *
61 init_graph_slim_pretty_print (FILE *fp)
62 {
63 static bool initialized = false;
64 static pretty_printer graph_slim_pp;
65
66 if (! initialized)
67 {
68 pp_construct (&graph_slim_pp, /*prefix=*/NULL, /*linewidth=*/0);
69 initialized = true;
70 }
71 else
72 gcc_assert (! pp_last_position_in_text (&graph_slim_pp));
73
74 graph_slim_pp.buffer->stream = fp;
75 return &graph_slim_pp;
76 }
77
78 /* Draw a basic block BB belonging to the function with FUNCDEF_NO
79 as its unique number. */
80 static void
81 draw_cfg_node (pretty_printer *pp, int funcdef_no, basic_block bb)
82 {
83 const char *shape;
84 const char *fillcolor;
85
86 if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
87 {
88 shape = "Mdiamond";
89 fillcolor = "white";
90 }
91 else
92 {
93 shape = "record";
94 fillcolor =
95 BB_PARTITION (bb) == BB_HOT_PARTITION ? "lightpink"
96 : BB_PARTITION (bb) == BB_COLD_PARTITION ? "lightblue"
97 : "lightgrey";
98 }
99
100 pp_printf (pp,
101 "\tfn_%d_basic_block_%d "
102 "[shape=%s,style=filled,fillcolor=%s,label=\"",
103 funcdef_no, bb->index, shape, fillcolor);
104
105 if (bb->index == ENTRY_BLOCK)
106 pp_string (pp, "ENTRY");
107 else if (bb->index == EXIT_BLOCK)
108 pp_string (pp, "EXIT");
109 else
110 {
111 pp_character (pp, '{');
112 pp_write_text_to_stream (pp);
113 dump_bb_for_graph (pp, bb);
114 pp_character (pp, '}');
115 }
116
117 pp_string (pp, "\"];\n\n");
118 pp_flush (pp);
119 }
120
121 /* Draw all successor edges of a basic block BB belonging to the function
122 with FUNCDEF_NO as its unique number. */
123 static void
124 draw_cfg_node_succ_edges (pretty_printer *pp, int funcdef_no, basic_block bb)
125 {
126 edge e;
127 edge_iterator ei;
128 FOR_EACH_EDGE (e, ei, bb->succs)
129 {
130 const char *style = "\"solid,bold\"";
131 const char *color = "black";
132 int weight = 10;
133
134 if (e->flags & EDGE_FAKE)
135 {
136 style = "dotted";
137 color = "green";
138 weight = 0;
139 }
140 else if (e->flags & EDGE_DFS_BACK)
141 {
142 style = "\"dotted,bold\"";
143 color = "blue";
144 weight = 10;
145 }
146 else if (e->flags & EDGE_FALLTHRU)
147 {
148 color = "blue";
149 weight = 100;
150 }
151
152 if (e->flags & EDGE_ABNORMAL)
153 color = "red";
154
155 pp_printf (pp,
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");
162 }
163 pp_flush (pp);
164 }
165
166 /* Print a graphical representation of the CFG of function FUN. */
167
168 void
169 print_graph_cfg (const char *base, struct function *fun)
170 {
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);
175 basic_block bb;
176 int i, n;
177 pretty_printer *pp = init_graph_slim_pretty_print (fp);
178
179 pp_printf (pp, "subgraph \"%s\" {\n"
180 "\tcolor=\"black\";\n"
181 "\tlabel=\"%s\";\n",
182 funcname, funcname);
183
184 /* First print all basic blocks.
185 Visit the blocks in reverse post order to get a good ranking
186 of the nodes. */
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]));
190
191 /* Draw all edges at the end to get subgraphs right for GraphViz,
192 which requires nodes to be defined before edges to cluster
193 nodes properly.
194
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 ();
200 FOR_ALL_BB (bb)
201 draw_cfg_node_succ_edges (pp, funcdef_no, bb);
202
203 pp_printf (pp, "\t}\n");
204 pp_flush (pp);
205 fclose (fp);
206 }
207
208 /* Start the dump of a graph. */
209 static void
210 start_graph_dump (FILE *fp)
211 {
212 fputs ("digraph \"\" {\n"
213 "overlap=false;\n",
214 fp);
215 }
216
217 /* End the dump of a graph. */
218 static void
219 end_graph_dump (FILE *fp)
220 {
221 fputs ("}\n", fp);
222 }
223
224 /* Similar as clean_dump_file, but this time for graph output files. */
225 void
226 clean_graph_dump_file (const char *base)
227 {
228 FILE *fp = open_graph_file (base, "w");
229 start_graph_dump (fp);
230 fclose (fp);
231 }
232
233
234 /* Do final work on the graph output file. */
235 void
236 finish_graph_dump_file (const char *base)
237 {
238 FILE *fp = open_graph_file (base, "a");
239 end_graph_dump (fp);
240 fclose (fp);
241 }