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
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
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
24 #include "coretypes.h"
32 #include "langhooks.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
38 static const char *const graph_ext
[] =
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. */
55 switch (graph_dump_format
)
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));
69 start_bb (FILE *fp
, int bb
)
71 switch (graph_dump_format
)
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),
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
,
91 fprintf (fp
, " %d", i
);
92 if (i
< FIRST_PSEUDO_REGISTER
)
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 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2),
121 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2),
129 switch (graph_dump_format
)
132 fprintf (fp
, "node: {\n title: \"%s.%d\"\n color: %s\n \
134 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2),
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));
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
));
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 class)
175 switch (graph_dump_format
)
180 color
= "color: red ";
182 color
= "color: blue ";
184 color
= "color: green ";
186 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
187 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2),
189 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2),
193 fprintf (fp
, "class: %d ", class);
204 switch (graph_dump_format
)
217 switch (graph_dump_format
)
220 fprintf (fp
, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
221 (*lang_hooks
.decl_printable_name
) (current_function_decl
, 2));
228 /* Like print_rtl, but also print out live information for the start of each
231 print_rtl_graph_with_bb (const char *base
, const char *suffix
, rtx rtx_first
)
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
);
240 if (basic_block_info
== NULL
)
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");
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
= 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
));
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 (GET_CODE (tmp_rtx
) == BARRIER
)
300 if (GET_CODE (tmp_rtx
) == NOTE
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)
322 bb
= BASIC_BLOCK (i
);
324 /* End of the basic block. */
327 /* Now specify the edges to all the successors of this
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
)
345 draw_edge (fp
, INSN_UID (tmp_rtx
), 999999,
347 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
357 /* Don't print edges to barriers. */
359 || GET_CODE (next_insn
) != BARRIER
)
360 draw_edge (fp
, XINT (tmp_rtx
, 0),
361 next_insn
? INSN_UID (next_insn
) : 999999, 0, 0);
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
);
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);
393 /* Similar as clean_dump_file, but this time for graph output files. */
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
);
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");
411 fatal_error ("can't open %s: %m", buf
);
413 switch (graph_dump_format
)
416 fputs ("graph: {\nport_sharing: no\n", fp
);
426 /* Do final work on the graph output file. */
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
);
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");
443 switch (graph_dump_format
)