1 /* Output routines for graphical representation.
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
37 static const char *const graph_ext
[] =
43 static void start_fct (FILE *);
44 static void start_bb (FILE *, int);
45 static void node_data (FILE *, rtx
);
46 static void draw_edge (FILE *, int, int, int, int);
47 static void end_fct (FILE *);
48 static void end_bb (FILE *);
50 /* Output text for new basic block. */
54 switch (graph_dump_format
)
58 graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
59 current_function_name (), current_function_name ());
67 start_bb (FILE *fp
, int bb
)
73 switch (graph_dump_format
)
77 graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
78 label: \"basic block %d",
79 current_function_name (), bb
, bb
);
86 /* FIXME Should this be printed? It makes the graph significantly larger. */
88 /* Print the live-at-start register list. */
90 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start
[bb
], 0, i
, rsi
)
92 fprintf (fp
, " %d", i
);
93 if (i
< FIRST_PSEUDO_REGISTER
)
94 fprintf (fp
, " [%s]", reg_names
[i
]);
98 switch (graph_dump_format
)
101 fputs ("\"\n\n", fp
);
109 node_data (FILE *fp
, rtx tmp_rtx
)
111 if (PREV_INSN (tmp_rtx
) == 0)
113 /* This is the first instruction. Add an edge from the starting
115 switch (graph_dump_format
)
119 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
120 current_function_name (),
121 current_function_name (), XINT (tmp_rtx
, 0));
128 switch (graph_dump_format
)
131 fprintf (fp
, "node: {\n title: \"%s.%d\"\n color: %s\n \
133 current_function_name (), XINT (tmp_rtx
, 0),
134 NOTE_P (tmp_rtx
) ? "lightgrey"
135 : NONJUMP_INSN_P (tmp_rtx
) ? "green"
136 : JUMP_P (tmp_rtx
) ? "darkgreen"
137 : CALL_P (tmp_rtx
) ? "darkgreen"
138 : LABEL_P (tmp_rtx
) ? "\
139 darkgrey\n shape: ellipse" : "white",
140 GET_RTX_NAME (GET_CODE (tmp_rtx
)), XINT (tmp_rtx
, 0));
147 if (NOTE_P (tmp_rtx
))
149 const char *name
= "";
150 if (NOTE_LINE_NUMBER (tmp_rtx
) < 0)
151 name
= GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (tmp_rtx
));
152 fprintf (fp
, " %s", name
);
154 else if (INSN_P (tmp_rtx
))
155 print_rtl_single (fp
, PATTERN (tmp_rtx
));
157 print_rtl_single (fp
, tmp_rtx
);
159 switch (graph_dump_format
)
162 fputs ("\"\n}\n", fp
);
170 draw_edge (FILE *fp
, int from
, int to
, int bb_edge
, int class)
173 switch (graph_dump_format
)
178 color
= "color: red ";
180 color
= "color: blue ";
182 color
= "color: green ";
184 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
185 current_function_name (), from
,
186 current_function_name (), to
, color
);
188 fprintf (fp
, "class: %d ", class);
199 switch (graph_dump_format
)
212 switch (graph_dump_format
)
215 fprintf (fp
, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
216 current_function_name ());
223 /* Like print_rtl, but also print out live information for the start of each
226 print_rtl_graph_with_bb (const char *base
, rtx rtx_first
)
229 size_t namelen
= strlen (base
);
230 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
231 char *buf
= alloca (namelen
+ extlen
);
234 if (basic_block_info
== NULL
)
237 memcpy (buf
, base
, namelen
);
238 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
240 fp
= fopen (buf
, "a");
245 fprintf (fp
, "(nil)\n");
248 enum bb_state
{ NOT_IN_BB
, IN_ONE_BB
, IN_MULTIPLE_BB
};
249 int max_uid
= get_max_uid ();
250 int *start
= xmalloc (max_uid
* sizeof (int));
251 int *end
= xmalloc (max_uid
* sizeof (int));
252 enum bb_state
*in_bb_p
= xmalloc (max_uid
* sizeof (enum bb_state
));
256 for (i
= 0; i
< max_uid
; ++i
)
258 start
[i
] = end
[i
] = -1;
259 in_bb_p
[i
] = NOT_IN_BB
;
262 FOR_EACH_BB_REVERSE (bb
)
265 start
[INSN_UID (BB_HEAD (bb
))] = bb
->index
;
266 end
[INSN_UID (BB_END (bb
))] = bb
->index
;
267 for (x
= BB_HEAD (bb
); x
!= NULL_RTX
; x
= NEXT_INSN (x
))
269 in_bb_p
[INSN_UID (x
)]
270 = (in_bb_p
[INSN_UID (x
)] == NOT_IN_BB
)
271 ? IN_ONE_BB
: IN_MULTIPLE_BB
;
272 if (x
== BB_END (bb
))
277 /* Tell print-rtl that we want graph output. */
280 /* Start new function. */
283 for (tmp_rtx
= NEXT_INSN (rtx_first
); NULL
!= tmp_rtx
;
284 tmp_rtx
= NEXT_INSN (tmp_rtx
))
286 int edge_printed
= 0;
289 if (start
[INSN_UID (tmp_rtx
)] < 0 && end
[INSN_UID (tmp_rtx
)] < 0)
291 if (BARRIER_P (tmp_rtx
))
294 && (1 || in_bb_p
[INSN_UID (tmp_rtx
)] == NOT_IN_BB
))
298 if ((i
= start
[INSN_UID (tmp_rtx
)]) >= 0)
300 /* We start a subgraph for each basic block. */
304 draw_edge (fp
, 0, INSN_UID (tmp_rtx
), 1, 0);
307 /* Print the data for this node. */
308 node_data (fp
, tmp_rtx
);
309 next_insn
= next_nonnote_insn (tmp_rtx
);
311 if ((i
= end
[INSN_UID (tmp_rtx
)]) >= 0)
316 bb
= BASIC_BLOCK (i
);
318 /* End of the basic block. */
321 /* Now specify the edges to all the successors of this
323 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
325 if (e
->dest
!= EXIT_BLOCK_PTR
)
327 rtx block_head
= BB_HEAD (e
->dest
);
329 draw_edge (fp
, INSN_UID (tmp_rtx
),
330 INSN_UID (block_head
),
331 next_insn
!= block_head
,
332 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
334 if (block_head
== next_insn
)
339 draw_edge (fp
, INSN_UID (tmp_rtx
), 999999,
341 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
351 /* Don't print edges to barriers. */
353 || !BARRIER_P (next_insn
))
354 draw_edge (fp
, XINT (tmp_rtx
, 0),
355 next_insn
? INSN_UID (next_insn
) : 999999, 0, 0);
358 /* We draw the remaining edges in class 3. We have
359 to skip over the barrier since these nodes are
360 not printed at all. */
362 next_insn
= NEXT_INSN (next_insn
);
364 && (NOTE_P (next_insn
)
365 || BARRIER_P (next_insn
)));
367 draw_edge (fp
, XINT (tmp_rtx
, 0),
368 next_insn
? INSN_UID (next_insn
) : 999999, 0, 3);
387 /* Similar as clean_dump_file, but this time for graph output files. */
390 clean_graph_dump_file (const char *base
)
392 size_t namelen
= strlen (base
);
393 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
394 char *buf
= alloca (namelen
+ extlen
);
397 memcpy (buf
, base
, namelen
);
398 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
400 fp
= fopen (buf
, "w");
403 fatal_error ("can't open %s: %m", buf
);
405 gcc_assert (graph_dump_format
== vcg
);
406 fputs ("graph: {\nport_sharing: no\n", fp
);
412 /* Do final work on the graph output file. */
414 finish_graph_dump_file (const char *base
)
416 size_t namelen
= strlen (base
);
417 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
418 char *buf
= alloca (namelen
+ extlen
);
421 memcpy (buf
, base
, namelen
);
422 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
424 fp
= fopen (buf
, "a");
427 gcc_assert (graph_dump_format
== vcg
);