2003-12-30 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / gcc / graph.c
blob483cc7e35a1d4b20fcc48170a0760171f07eca76
1 /* Output routines for graphical representation.
2 Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include <config.h>
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "function.h"
32 #include "langhooks.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "toplev.h"
36 #include "graph.h"
38 static const char *const graph_ext[] =
40 /* no_graph */ "",
41 /* vcg */ ".vcg",
44 static void start_fct (FILE *);
45 static void start_bb (FILE *, int);
46 static void node_data (FILE *, rtx);
47 static void draw_edge (FILE *, int, int, int, int);
48 static void end_fct (FILE *);
49 static void end_bb (FILE *);
51 /* Output text for new basic block. */
52 static void
53 start_fct (FILE *fp)
55 switch (graph_dump_format)
57 case vcg:
58 fprintf (fp, "\
59 graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
60 (*lang_hooks.decl_printable_name) (current_function_decl, 2),
61 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
62 break;
63 case no_graph:
64 break;
68 static void
69 start_bb (FILE *fp, int bb)
71 switch (graph_dump_format)
73 case vcg:
74 fprintf (fp, "\
75 graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
76 label: \"basic block %d",
77 (*lang_hooks.decl_printable_name) (current_function_decl, 2),
78 bb, bb);
79 break;
80 case no_graph:
81 break;
84 #if 0
85 /* FIXME Should this be printed? It makes the graph significantly larger. */
87 /* Print the live-at-start register list. */
88 fputc ('\n', fp);
89 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i,
91 fprintf (fp, " %d", i);
92 if (i < FIRST_PSEUDO_REGISTER)
93 fprintf (fp, " [%s]",
94 reg_names[i]);
95 });
96 #endif
98 switch (graph_dump_format)
100 case vcg:
101 fputs ("\"\n\n", fp);
102 break;
103 case no_graph:
104 break;
108 static void
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
114 block. */
115 switch (graph_dump_format)
117 case vcg:
118 fprintf (fp, "\
119 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
120 (*lang_hooks.decl_printable_name) (current_function_decl, 2),
121 (*lang_hooks.decl_printable_name) (current_function_decl, 2),
122 XINT (tmp_rtx, 0));
123 break;
124 case no_graph:
125 break;
129 switch (graph_dump_format)
131 case vcg:
132 fprintf (fp, "node: {\n title: \"%s.%d\"\n color: %s\n \
133 label: \"%s %d\n",
134 (*lang_hooks.decl_printable_name) (current_function_decl, 2),
135 XINT (tmp_rtx, 0),
136 GET_CODE (tmp_rtx) == NOTE ? "lightgrey"
137 : GET_CODE (tmp_rtx) == INSN ? "green"
138 : GET_CODE (tmp_rtx) == JUMP_INSN ? "darkgreen"
139 : GET_CODE (tmp_rtx) == CALL_INSN ? "darkgreen"
140 : GET_CODE (tmp_rtx) == CODE_LABEL ? "\
141 darkgrey\n shape: ellipse" : "white",
142 GET_RTX_NAME (GET_CODE (tmp_rtx)), XINT (tmp_rtx, 0));
143 break;
144 case no_graph:
145 break;
148 /* Print the RTL. */
149 if (GET_CODE (tmp_rtx) == NOTE)
151 const char *name = "";
152 if (NOTE_LINE_NUMBER (tmp_rtx) < 0)
153 name = GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (tmp_rtx));
154 fprintf (fp, " %s", name);
156 else if (INSN_P (tmp_rtx))
157 print_rtl_single (fp, PATTERN (tmp_rtx));
158 else
159 print_rtl_single (fp, tmp_rtx);
161 switch (graph_dump_format)
163 case vcg:
164 fputs ("\"\n}\n", fp);
165 break;
166 case no_graph:
167 break;
171 static void
172 draw_edge (FILE *fp, int from, int to, int bb_edge, int class)
174 const char * color;
175 switch (graph_dump_format)
177 case vcg:
178 color = "";
179 if (class == 2)
180 color = "color: red ";
181 else if (bb_edge)
182 color = "color: blue ";
183 else if (class == 3)
184 color = "color: green ";
185 fprintf (fp,
186 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
187 (*lang_hooks.decl_printable_name) (current_function_decl, 2),
188 from,
189 (*lang_hooks.decl_printable_name) (current_function_decl, 2),
191 color);
192 if (class)
193 fprintf (fp, "class: %d ", class);
194 fputs ("}\n", fp);
195 break;
196 case no_graph:
197 break;
201 static void
202 end_bb (FILE *fp)
204 switch (graph_dump_format)
206 case vcg:
207 fputs ("}\n", fp);
208 break;
209 case no_graph:
210 break;
214 static void
215 end_fct (FILE *fp)
217 switch (graph_dump_format)
219 case vcg:
220 fprintf (fp, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
221 (*lang_hooks.decl_printable_name) (current_function_decl, 2));
222 break;
223 case no_graph:
224 break;
228 /* Like print_rtl, but also print out live information for the start of each
229 basic block. */
230 void
231 print_rtl_graph_with_bb (const char *base, const char *suffix, rtx rtx_first)
233 rtx tmp_rtx;
234 size_t namelen = strlen (base);
235 size_t suffixlen = strlen (suffix);
236 size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
237 char *buf = alloca (namelen + suffixlen + extlen);
238 FILE *fp;
240 if (basic_block_info == NULL)
241 return;
243 memcpy (buf, base, namelen);
244 memcpy (buf + namelen, suffix, suffixlen);
245 memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);
247 fp = fopen (buf, "a");
248 if (fp == NULL)
249 return;
251 if (rtx_first == 0)
252 fprintf (fp, "(nil)\n");
253 else
255 enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
256 int max_uid = get_max_uid ();
257 int *start = xmalloc (max_uid * sizeof (int));
258 int *end = xmalloc (max_uid * sizeof (int));
259 enum bb_state *in_bb_p = xmalloc (max_uid * sizeof (enum bb_state));
260 basic_block bb;
261 int i;
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)
271 rtx x;
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))
280 break;
284 /* Tell print-rtl that we want graph output. */
285 dump_for_graph = 1;
287 /* Start new function. */
288 start_fct (fp);
290 for (tmp_rtx = NEXT_INSN (rtx_first); NULL != tmp_rtx;
291 tmp_rtx = NEXT_INSN (tmp_rtx))
293 int edge_printed = 0;
294 rtx next_insn;
296 if (start[INSN_UID (tmp_rtx)] < 0 && end[INSN_UID (tmp_rtx)] < 0)
298 if (GET_CODE (tmp_rtx) == BARRIER)
299 continue;
300 if (GET_CODE (tmp_rtx) == NOTE
301 && (1 || in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB))
302 continue;
305 if ((i = start[INSN_UID (tmp_rtx)]) >= 0)
307 /* We start a subgraph for each basic block. */
308 start_bb (fp, i);
310 if (i == 0)
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)
320 edge e;
322 bb = BASIC_BLOCK (i);
324 /* End of the basic block. */
325 end_bb (fp);
327 /* Now specify the edges to all the successors of this
328 basic block. */
329 for (e = bb->succ; e ; e = e->succ_next)
331 if (e->dest != EXIT_BLOCK_PTR)
333 rtx block_head = BB_HEAD (e->dest);
335 draw_edge (fp, INSN_UID (tmp_rtx),
336 INSN_UID (block_head),
337 next_insn != block_head,
338 (e->flags & EDGE_ABNORMAL ? 2 : 0));
340 if (block_head == next_insn)
341 edge_printed = 1;
343 else
345 draw_edge (fp, INSN_UID (tmp_rtx), 999999,
346 next_insn != 0,
347 (e->flags & EDGE_ABNORMAL ? 2 : 0));
349 if (next_insn == 0)
350 edge_printed = 1;
355 if (!edge_printed)
357 /* Don't print edges to barriers. */
358 if (next_insn == 0
359 || GET_CODE (next_insn) != BARRIER)
360 draw_edge (fp, XINT (tmp_rtx, 0),
361 next_insn ? INSN_UID (next_insn) : 999999, 0, 0);
362 else
364 /* We draw the remaining edges in class 3. We have
365 to skip over the barrier since these nodes are
366 not printed at all. */
368 next_insn = NEXT_INSN (next_insn);
369 while (next_insn
370 && (GET_CODE (next_insn) == NOTE
371 || GET_CODE (next_insn) == BARRIER));
373 draw_edge (fp, XINT (tmp_rtx, 0),
374 next_insn ? INSN_UID (next_insn) : 999999, 0, 3);
379 dump_for_graph = 0;
381 end_fct (fp);
383 /* Clean up. */
384 free (start);
385 free (end);
386 free (in_bb_p);
389 fclose (fp);
393 /* Similar as clean_dump_file, but this time for graph output files. */
395 void
396 clean_graph_dump_file (const char *base, const char *suffix)
398 size_t namelen = strlen (base);
399 size_t suffixlen = strlen (suffix);
400 size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
401 char *buf = alloca (namelen + extlen + suffixlen);
402 FILE *fp;
404 memcpy (buf, base, namelen);
405 memcpy (buf + namelen, suffix, suffixlen);
406 memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);
408 fp = fopen (buf, "w");
410 if (fp == NULL)
411 fatal_error ("can't open %s: %m", buf);
413 switch (graph_dump_format)
415 case vcg:
416 fputs ("graph: {\nport_sharing: no\n", fp);
417 break;
418 case no_graph:
419 abort ();
422 fclose (fp);
426 /* Do final work on the graph output file. */
427 void
428 finish_graph_dump_file (const char *base, const char *suffix)
430 size_t namelen = strlen (base);
431 size_t suffixlen = strlen (suffix);
432 size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
433 char *buf = alloca (namelen + suffixlen + extlen);
434 FILE *fp;
436 memcpy (buf, base, namelen);
437 memcpy (buf + namelen, suffix, suffixlen);
438 memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);
440 fp = fopen (buf, "a");
441 if (fp != NULL)
443 switch (graph_dump_format)
445 case vcg:
446 fputs ("}\n", fp);
447 break;
448 case no_graph:
449 abort ();
452 fclose (fp);