1 /* Output routines for graphical representation.
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2007
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"
30 #include "hard-reg-set.h"
32 #include "basic-block.h"
36 static const char *const graph_ext
[] =
42 static void start_fct (FILE *);
43 static void start_bb (FILE *, int);
44 static void node_data (FILE *, rtx
);
45 static void draw_edge (FILE *, int, int, int, int);
46 static void end_fct (FILE *);
47 static void end_bb (FILE *);
49 /* Output text for new basic block. */
53 switch (graph_dump_format
)
57 graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
58 current_function_name (), current_function_name ());
66 start_bb (FILE *fp
, int bb
)
72 switch (graph_dump_format
)
76 graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
77 label: \"basic block %d",
78 current_function_name (), bb
, bb
);
85 /* FIXME Should this be printed? It makes the graph significantly larger. */
87 /* Print the live-at-start register list. */
89 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start
[bb
], 0, i
, rsi
)
91 fprintf (fp
, " %d", i
);
92 if (i
< FIRST_PSEUDO_REGISTER
)
93 fprintf (fp
, " [%s]", reg_names
[i
]);
97 switch (graph_dump_format
)
100 fputs ("\"\n\n", fp
);
108 node_data (FILE *fp
, rtx tmp_rtx
)
110 if (PREV_INSN (tmp_rtx
) == 0)
112 /* This is the first instruction. Add an edge from the starting
114 switch (graph_dump_format
)
118 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
119 current_function_name (),
120 current_function_name (), XINT (tmp_rtx
, 0));
127 switch (graph_dump_format
)
130 fprintf (fp
, "node: {\n title: \"%s.%d\"\n color: %s\n \
132 current_function_name (), XINT (tmp_rtx
, 0),
133 NOTE_P (tmp_rtx
) ? "lightgrey"
134 : NONJUMP_INSN_P (tmp_rtx
) ? "green"
135 : JUMP_P (tmp_rtx
) ? "darkgreen"
136 : CALL_P (tmp_rtx
) ? "darkgreen"
137 : LABEL_P (tmp_rtx
) ? "\
138 darkgrey\n shape: ellipse" : "white",
139 GET_RTX_NAME (GET_CODE (tmp_rtx
)), XINT (tmp_rtx
, 0));
146 if (NOTE_P (tmp_rtx
))
149 name
= GET_NOTE_INSN_NAME (NOTE_KIND (tmp_rtx
));
150 fprintf (fp
, " %s", name
);
152 else if (INSN_P (tmp_rtx
))
153 print_rtl_single (fp
, PATTERN (tmp_rtx
));
155 print_rtl_single (fp
, tmp_rtx
);
157 switch (graph_dump_format
)
160 fputs ("\"\n}\n", fp
);
168 draw_edge (FILE *fp
, int from
, int to
, int bb_edge
, int class)
171 switch (graph_dump_format
)
176 color
= "color: red ";
178 color
= "color: blue ";
180 color
= "color: green ";
182 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
183 current_function_name (), from
,
184 current_function_name (), to
, color
);
186 fprintf (fp
, "class: %d ", class);
197 switch (graph_dump_format
)
210 switch (graph_dump_format
)
213 fprintf (fp
, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
214 current_function_name ());
221 /* Like print_rtl, but also print out live information for the start of each
224 print_rtl_graph_with_bb (const char *base
, rtx rtx_first
)
227 size_t namelen
= strlen (base
);
228 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
229 char *buf
= alloca (namelen
+ extlen
);
232 if (basic_block_info
== NULL
)
235 memcpy (buf
, base
, namelen
);
236 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
238 fp
= fopen (buf
, "a");
243 fprintf (fp
, "(nil)\n");
246 enum bb_state
{ NOT_IN_BB
, IN_ONE_BB
, IN_MULTIPLE_BB
};
247 int max_uid
= get_max_uid ();
248 int *start
= XNEWVEC (int, max_uid
);
249 int *end
= XNEWVEC (int, max_uid
);
250 enum bb_state
*in_bb_p
= XNEWVEC (enum bb_state
, max_uid
);
254 for (i
= 0; i
< max_uid
; ++i
)
256 start
[i
] = end
[i
] = -1;
257 in_bb_p
[i
] = NOT_IN_BB
;
260 FOR_EACH_BB_REVERSE (bb
)
263 start
[INSN_UID (BB_HEAD (bb
))] = bb
->index
;
264 end
[INSN_UID (BB_END (bb
))] = bb
->index
;
265 for (x
= BB_HEAD (bb
); x
!= NULL_RTX
; x
= NEXT_INSN (x
))
267 in_bb_p
[INSN_UID (x
)]
268 = (in_bb_p
[INSN_UID (x
)] == NOT_IN_BB
)
269 ? IN_ONE_BB
: IN_MULTIPLE_BB
;
270 if (x
== BB_END (bb
))
275 /* Tell print-rtl that we want graph output. */
278 /* Start new function. */
281 for (tmp_rtx
= NEXT_INSN (rtx_first
); NULL
!= tmp_rtx
;
282 tmp_rtx
= NEXT_INSN (tmp_rtx
))
284 int edge_printed
= 0;
287 if (start
[INSN_UID (tmp_rtx
)] < 0 && end
[INSN_UID (tmp_rtx
)] < 0)
289 if (BARRIER_P (tmp_rtx
))
292 && (1 || in_bb_p
[INSN_UID (tmp_rtx
)] == NOT_IN_BB
))
296 if ((i
= start
[INSN_UID (tmp_rtx
)]) >= 0)
298 /* We start a subgraph for each basic block. */
302 draw_edge (fp
, 0, INSN_UID (tmp_rtx
), 1, 0);
305 /* Print the data for this node. */
306 node_data (fp
, tmp_rtx
);
307 next_insn
= next_nonnote_insn (tmp_rtx
);
309 if ((i
= end
[INSN_UID (tmp_rtx
)]) >= 0)
314 bb
= BASIC_BLOCK (i
);
316 /* End of the basic block. */
319 /* Now specify the edges to all the successors of this
321 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
323 if (e
->dest
!= EXIT_BLOCK_PTR
)
325 rtx block_head
= BB_HEAD (e
->dest
);
327 draw_edge (fp
, INSN_UID (tmp_rtx
),
328 INSN_UID (block_head
),
329 next_insn
!= block_head
,
330 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
332 if (block_head
== next_insn
)
337 draw_edge (fp
, INSN_UID (tmp_rtx
), 999999,
339 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
349 /* Don't print edges to barriers. */
351 || !BARRIER_P (next_insn
))
352 draw_edge (fp
, XINT (tmp_rtx
, 0),
353 next_insn
? INSN_UID (next_insn
) : 999999, 0, 0);
356 /* We draw the remaining edges in class 3. We have
357 to skip over the barrier since these nodes are
358 not printed at all. */
360 next_insn
= NEXT_INSN (next_insn
);
362 && (NOTE_P (next_insn
)
363 || BARRIER_P (next_insn
)));
365 draw_edge (fp
, XINT (tmp_rtx
, 0),
366 next_insn
? INSN_UID (next_insn
) : 999999, 0, 3);
385 /* Similar as clean_dump_file, but this time for graph output files. */
388 clean_graph_dump_file (const char *base
)
390 size_t namelen
= strlen (base
);
391 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
392 char *buf
= alloca (namelen
+ extlen
);
395 memcpy (buf
, base
, namelen
);
396 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
398 fp
= fopen (buf
, "w");
401 fatal_error ("can't open %s: %m", buf
);
403 gcc_assert (graph_dump_format
== vcg
);
404 fputs ("graph: {\nport_sharing: no\n", fp
);
410 /* Do final work on the graph output file. */
412 finish_graph_dump_file (const char *base
)
414 size_t namelen
= strlen (base
);
415 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
416 char *buf
= alloca (namelen
+ extlen
);
419 memcpy (buf
, base
, namelen
);
420 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
422 fp
= fopen (buf
, "a");
425 gcc_assert (graph_dump_format
== vcg
);