1 /* Output routines for graphical representation.
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2007, 2008, 2010
3 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "diagnostic-core.h"
36 static const char *const graph_ext
[] =
42 /* The flag to indicate if output is inside of a building block. */
45 static void start_fct (FILE *);
46 static void start_bb (FILE *, int);
47 static void node_data (FILE *, rtx
);
48 static void draw_edge (FILE *, int, int, int, int);
49 static void end_fct (FILE *);
50 static void end_bb (FILE *);
52 /* Output text for new basic block. */
56 switch (graph_dump_format
)
60 graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
61 current_function_name (), current_function_name ());
69 start_bb (FILE *fp
, int bb
)
75 switch (graph_dump_format
)
79 graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
80 label: \"basic block %d",
81 current_function_name (), bb
, bb
);
82 inbb
= 1; /* Now We are inside of a building block. */
89 /* FIXME Should this be printed? It makes the graph significantly larger. */
91 /* Print the live-at-start register list. */
93 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start
[bb
], 0, i
, rsi
)
95 fprintf (fp
, " %d", i
);
96 if (i
< FIRST_PSEUDO_REGISTER
)
97 fprintf (fp
, " [%s]", reg_names
[i
]);
101 switch (graph_dump_format
)
104 fputs ("\"\n\n", fp
);
112 node_data (FILE *fp
, rtx tmp_rtx
)
114 if (PREV_INSN (tmp_rtx
) == 0)
116 /* This is the first instruction. Add an edge from the starting
118 switch (graph_dump_format
)
122 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
123 current_function_name (),
124 current_function_name (), XINT (tmp_rtx
, 0));
131 switch (graph_dump_format
)
134 fprintf (fp
, "node: {\n title: \"%s.%d\"\n color: %s\n \
136 current_function_name (), XINT (tmp_rtx
, 0),
137 NOTE_P (tmp_rtx
) ? "lightgrey"
138 : NONJUMP_INSN_P (tmp_rtx
) ? "green"
139 : JUMP_P (tmp_rtx
) ? "darkgreen"
140 : CALL_P (tmp_rtx
) ? "darkgreen"
141 : LABEL_P (tmp_rtx
) ? "\
142 darkgrey\n shape: ellipse" : "white",
143 GET_RTX_NAME (GET_CODE (tmp_rtx
)), XINT (tmp_rtx
, 0));
150 if (NOTE_P (tmp_rtx
))
153 name
= GET_NOTE_INSN_NAME (NOTE_KIND (tmp_rtx
));
154 fprintf (fp
, " %s", name
);
156 else if (INSN_P (tmp_rtx
))
157 print_rtl_single (fp
, PATTERN (tmp_rtx
));
159 print_rtl_single (fp
, tmp_rtx
);
161 switch (graph_dump_format
)
164 fputs ("\"\n}\n", fp
);
172 draw_edge (FILE *fp
, int from
, int to
, int bb_edge
, int color_class
)
175 switch (graph_dump_format
)
179 if (color_class
== 2)
180 color
= "color: red ";
182 color
= "color: blue ";
183 else if (color_class
== 3)
184 color
= "color: green ";
186 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
187 current_function_name (), from
,
188 current_function_name (), to
, color
);
190 fprintf (fp
, "class: %d ", color_class
);
201 switch (graph_dump_format
)
204 /* Check if we are inside of a building block. */
208 inbb
= 0; /* Now we are outside of a building block. */
219 switch (graph_dump_format
)
222 fprintf (fp
, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
223 current_function_name ());
230 /* Like print_rtl, but also print out live information for the start of each
233 print_rtl_graph_with_bb (const char *base
, rtx rtx_first
)
236 size_t namelen
= strlen (base
);
237 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
238 char *buf
= XALLOCAVEC (char, namelen
+ extlen
);
241 if (basic_block_info
== NULL
)
244 memcpy (buf
, base
, namelen
);
245 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
247 fp
= fopen (buf
, "a");
252 fprintf (fp
, "(nil)\n");
255 enum bb_state
{ NOT_IN_BB
, IN_ONE_BB
, IN_MULTIPLE_BB
};
256 int max_uid
= get_max_uid ();
257 int *start
= XNEWVEC (int, max_uid
);
258 int *end
= XNEWVEC (int, max_uid
);
259 enum bb_state
*in_bb_p
= XNEWVEC (enum bb_state
, max_uid
);
263 for (i
= 0; i
< max_uid
; ++i
)
265 start
[i
] = end
[i
] = -1;
266 in_bb_p
[i
] = NOT_IN_BB
;
269 FOR_EACH_BB_REVERSE (bb
)
272 start
[INSN_UID (BB_HEAD (bb
))] = bb
->index
;
273 end
[INSN_UID (BB_END (bb
))] = bb
->index
;
274 for (x
= BB_HEAD (bb
); x
!= NULL_RTX
; x
= NEXT_INSN (x
))
276 in_bb_p
[INSN_UID (x
)]
277 = (in_bb_p
[INSN_UID (x
)] == NOT_IN_BB
)
278 ? IN_ONE_BB
: IN_MULTIPLE_BB
;
279 if (x
== BB_END (bb
))
284 /* Tell print-rtl that we want graph output. */
287 /* Start new function. */
290 for (tmp_rtx
= NEXT_INSN (rtx_first
); NULL
!= tmp_rtx
;
291 tmp_rtx
= NEXT_INSN (tmp_rtx
))
293 int edge_printed
= 0;
296 if (start
[INSN_UID (tmp_rtx
)] < 0 && end
[INSN_UID (tmp_rtx
)] < 0)
298 if (BARRIER_P (tmp_rtx
))
301 && (1 || in_bb_p
[INSN_UID (tmp_rtx
)] == NOT_IN_BB
))
305 if ((i
= start
[INSN_UID (tmp_rtx
)]) >= 0)
307 /* We start a subgraph for each basic block. */
311 draw_edge (fp
, 0, INSN_UID (tmp_rtx
), 1, 0);
314 /* Print the data for this node. */
315 node_data (fp
, tmp_rtx
);
316 next_insn
= next_nonnote_insn (tmp_rtx
);
318 if ((i
= end
[INSN_UID (tmp_rtx
)]) >= 0)
323 bb
= BASIC_BLOCK (i
);
325 /* End of the basic block. */
328 /* Now specify the edges to all the successors of this
330 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
332 if (e
->dest
!= EXIT_BLOCK_PTR
)
334 rtx block_head
= BB_HEAD (e
->dest
);
336 draw_edge (fp
, INSN_UID (tmp_rtx
),
337 INSN_UID (block_head
),
338 next_insn
!= block_head
,
339 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
341 if (block_head
== next_insn
)
346 draw_edge (fp
, INSN_UID (tmp_rtx
), 999999,
348 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
358 /* Don't print edges to barriers. */
360 || !BARRIER_P (next_insn
))
361 draw_edge (fp
, XINT (tmp_rtx
, 0),
362 next_insn
? INSN_UID (next_insn
) : 999999, 0, 0);
365 /* We draw the remaining edges in class 3. We have
366 to skip over the barrier since these nodes are
367 not printed at all. */
369 next_insn
= NEXT_INSN (next_insn
);
371 && (NOTE_P (next_insn
)
372 || BARRIER_P (next_insn
)));
374 draw_edge (fp
, XINT (tmp_rtx
, 0),
375 next_insn
? INSN_UID (next_insn
) : 999999, 0, 3);
394 /* Similar as clean_dump_file, but this time for graph output files. */
397 clean_graph_dump_file (const char *base
)
399 size_t namelen
= strlen (base
);
400 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
401 char *buf
= XALLOCAVEC (char, namelen
+ extlen
);
404 memcpy (buf
, base
, namelen
);
405 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
407 fp
= fopen (buf
, "w");
410 fatal_error ("can%'t open %s: %m", buf
);
412 gcc_assert (graph_dump_format
== vcg
);
413 fputs ("graph: {\nport_sharing: no\n", fp
);
419 /* Do final work on the graph output file. */
421 finish_graph_dump_file (const char *base
)
423 size_t namelen
= strlen (base
);
424 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
425 char *buf
= XALLOCAVEC (char, namelen
+ extlen
);
428 memcpy (buf
, base
, namelen
);
429 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
431 fp
= fopen (buf
, "a");
434 gcc_assert (graph_dump_format
== vcg
);