1 /* Output routines for graphical representation.
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
32 static const char *graph_ext[] =
38 /* Output text for new basic block. */
44 switch (graph_dump_format)
48 graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
49 current_function_name, current_function_name);
61 switch (graph_dump_format)
65 graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
66 label: \"basic block %d",
67 current_function_name, bb, bb);
74 /* FIXME Should this be printed? It makes the graph significantly larger. */
76 /* Print the live-at-start register list. */
78 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i,
80 fprintf (fp, " %d", i);
81 if (i < FIRST_PSEUDO_REGISTER)
87 switch (graph_dump_format)
98 node_data (fp, tmp_rtx)
103 if (PREV_INSN (tmp_rtx) == 0)
105 /* This is the first instruction. Add an edge from the starting
107 switch (graph_dump_format)
111 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
112 current_function_name,
113 current_function_name, XINT (tmp_rtx, 0));
120 switch (graph_dump_format)
123 fprintf (fp, "node: {\n title: \"%s.%d\"\n color: %s\n \
125 current_function_name, XINT (tmp_rtx, 0),
126 GET_CODE (tmp_rtx) == NOTE ? "lightgrey"
127 : GET_CODE (tmp_rtx) == INSN ? "green"
128 : GET_CODE (tmp_rtx) == JUMP_INSN ? "darkgreen"
129 : GET_CODE (tmp_rtx) == CALL_INSN ? "darkgreen"
130 : GET_CODE (tmp_rtx) == CODE_LABEL ? "\
131 darkgrey\n shape: ellipse" : "white",
132 GET_RTX_NAME (GET_CODE (tmp_rtx)), XINT (tmp_rtx, 0));
139 if (GET_CODE (tmp_rtx) == NOTE)
141 static const char *note_names[] =
159 "repeated_line_number",
167 XINT (tmp_rtx, 4) < 0 ? note_names[-XINT (tmp_rtx, 4)] : "");
169 else if (GET_RTX_CLASS (GET_CODE (tmp_rtx)) == 'i')
170 print_rtl_single (fp, PATTERN (tmp_rtx));
172 print_rtl_single (fp, tmp_rtx);
174 switch (graph_dump_format)
177 fputs ("\"\n}\n", fp);
185 draw_edge (fp, from, to, bb_edge, class)
193 switch (graph_dump_format)
198 color = "color: red ";
200 color = "color: blue ";
202 color = "color: green ";
204 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
205 current_function_name, from,
206 current_function_name, to, color);
208 fprintf (fp, "class: %d ", class);
219 int bb ATTRIBUTE_UNUSED;
221 switch (graph_dump_format)
235 switch (graph_dump_format)
238 fprintf (fp, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
239 current_function_name);
246 /* Like print_rtl, but also print out live information for the start of each
249 print_rtl_graph_with_bb (base, suffix, rtx_first)
254 register rtx tmp_rtx;
255 size_t namelen = strlen (base);
256 size_t suffixlen = strlen (suffix);
257 size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
258 char *buf = (char *) alloca (namelen + suffixlen + extlen);
261 if (basic_block_info == NULL)
264 memcpy (buf, base, namelen);
265 memcpy (buf + namelen, suffix, suffixlen);
266 memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);
268 fp = fopen (buf, "a");
273 fprintf (fp, "(nil)\n");
277 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
278 int max_uid = get_max_uid ();
279 int *start = (int *) alloca (max_uid * sizeof (int));
280 int *end = (int *) alloca (max_uid * sizeof (int));
281 enum bb_state *in_bb_p = (enum bb_state *)
282 alloca (max_uid * sizeof (enum bb_state));
285 for (i = 0; i < max_uid; ++i)
287 start[i] = end[i] = -1;
288 in_bb_p[i] = NOT_IN_BB;
291 for (i = n_basic_blocks - 1; i >= 0; --i)
294 bb = BASIC_BLOCK (i);
295 start[INSN_UID (bb->head)] = i;
296 end[INSN_UID (bb->end)] = i;
297 for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
299 in_bb_p[INSN_UID (x)]
300 = (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
301 ? IN_ONE_BB : IN_MULTIPLE_BB;
307 /* Tell print-rtl that we want graph output. */
310 /* Start new function. */
313 for (tmp_rtx = NEXT_INSN (rtx_first); NULL != tmp_rtx;
314 tmp_rtx = NEXT_INSN (tmp_rtx))
316 int edge_printed = 0;
319 if (start[INSN_UID (tmp_rtx)] < 0 && end[INSN_UID (tmp_rtx)] < 0)
321 if (GET_CODE (tmp_rtx) == BARRIER)
323 if (GET_CODE (tmp_rtx) == NOTE
324 && (1 || in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB))
328 if ((i = start[INSN_UID (tmp_rtx)]) >= 0)
330 /* We start a subgraph for each basic block. */
334 draw_edge (fp, 0, INSN_UID (tmp_rtx), 1, 0);
337 /* Print the data for this node. */
338 node_data (fp, tmp_rtx);
339 next_insn = next_nonnote_insn (tmp_rtx);
341 if ((i = end[INSN_UID (tmp_rtx)]) >= 0)
345 bb = BASIC_BLOCK (i);
347 /* End of the basic block. */
350 /* Now specify the edges to all the successors of this
352 for (e = bb->succ; e ; e = e->succ_next)
354 if (e->dest != EXIT_BLOCK_PTR)
356 rtx block_head = e->dest->head;
358 draw_edge (fp, INSN_UID (tmp_rtx),
359 INSN_UID (block_head),
360 next_insn != block_head,
361 (e->flags & EDGE_ABNORMAL ? 2 : 0));
363 if (block_head == next_insn)
368 draw_edge (fp, INSN_UID (tmp_rtx), 999999,
370 (e->flags & EDGE_ABNORMAL ? 2 : 0));
380 /* Don't print edges to barriers. */
382 || GET_CODE (next_insn) != BARRIER)
383 draw_edge (fp, XINT (tmp_rtx, 0),
384 next_insn ? INSN_UID (next_insn) : 999999, 0, 0);
387 /* We draw the remaining edges in class 3. We have
388 to skip over the barrier since these nodes are
389 not printed at all. */
391 next_insn = NEXT_INSN (next_insn);
393 && (GET_CODE (next_insn) == NOTE
394 || GET_CODE (next_insn) == BARRIER));
396 draw_edge (fp, XINT (tmp_rtx, 0),
397 next_insn ? INSN_UID (next_insn) : 999999, 0, 3);
411 /* Similar as clean_dump_file, but this time for graph output files. */
413 clean_graph_dump_file (base, suffix)
417 size_t namelen = strlen (base);
418 size_t suffixlen = strlen (suffix);
419 size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
420 char *buf = (char *) alloca (namelen + extlen + suffixlen);
423 memcpy (buf, base, namelen);
424 memcpy (buf + namelen, suffix, suffixlen);
425 memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);
427 fp = fopen (buf, "w");
430 pfatal_with_name (buf);
432 switch (graph_dump_format)
435 fputs ("graph: {\nport_sharing: no\n", fp);
445 /* Do final work on the graph output file. */
447 finish_graph_dump_file (base, suffix)
451 size_t namelen = strlen (base);
452 size_t suffixlen = strlen (suffix);
453 size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
454 char *buf = (char *) alloca (namelen + suffixlen + extlen);
457 memcpy (buf, base, namelen);
458 memcpy (buf + namelen, suffix, suffixlen);
459 memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);
461 fp = fopen (buf, "a");
464 switch (graph_dump_format)