1 /* Output routines for graphical representation.
2 Copyright (C) 1998, 1999, 2000, 2001 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"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
36 static const char *const graph_ext
[] =
42 static void start_fct
PARAMS ((FILE *));
43 static void start_bb
PARAMS ((FILE *, int));
44 static void node_data
PARAMS ((FILE *, rtx
));
45 static void draw_edge
PARAMS ((FILE *, int, int, int, int));
46 static void end_fct
PARAMS ((FILE *));
47 static void end_bb
PARAMS ((FILE *));
49 /* 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 current_function_name
, current_function_name
);
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
,
91 fprintf (fp
, " %d", i
);
92 if (i
< FIRST_PSEUDO_REGISTER
)
98 switch (graph_dump_format
)
101 fputs ("\"\n\n", fp
);
109 node_data (fp
, tmp_rtx
)
114 if (PREV_INSN (tmp_rtx
) == 0)
116 /* This is the first instruction. Add an edge from the starting
118 switch (graph_dump_format
)
122 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
123 current_function_name
,
124 current_function_name
, XINT (tmp_rtx
, 0));
131 switch (graph_dump_format
)
134 fprintf (fp
, "node: {\n title: \"%s.%d\"\n color: %s\n \
136 current_function_name
, XINT (tmp_rtx
, 0),
137 GET_CODE (tmp_rtx
) == NOTE
? "lightgrey"
138 : GET_CODE (tmp_rtx
) == INSN
? "green"
139 : GET_CODE (tmp_rtx
) == JUMP_INSN
? "darkgreen"
140 : GET_CODE (tmp_rtx
) == CALL_INSN
? "darkgreen"
141 : GET_CODE (tmp_rtx
) == CODE_LABEL
? "\
142 darkgrey\n shape: ellipse" : "white",
143 GET_RTX_NAME (GET_CODE (tmp_rtx
)), XINT (tmp_rtx
, 0));
150 if (GET_CODE (tmp_rtx
) == NOTE
)
152 const char *name
= "";
153 if (NOTE_LINE_NUMBER (tmp_rtx
) < 0)
154 name
= GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (tmp_rtx
));
155 fprintf (fp
, " %s", name
);
157 else if (INSN_P (tmp_rtx
))
158 print_rtl_single (fp
, PATTERN (tmp_rtx
));
160 print_rtl_single (fp
, tmp_rtx
);
162 switch (graph_dump_format
)
165 fputs ("\"\n}\n", fp
);
173 draw_edge (fp
, from
, to
, bb_edge
, class)
181 switch (graph_dump_format
)
186 color
= "color: red ";
188 color
= "color: blue ";
190 color
= "color: green ";
192 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
193 current_function_name
, from
,
194 current_function_name
, to
, color
);
196 fprintf (fp
, "class: %d ", class);
208 switch (graph_dump_format
)
222 switch (graph_dump_format
)
225 fprintf (fp
, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
226 current_function_name
);
233 /* Like print_rtl, but also print out live information for the start of each
236 print_rtl_graph_with_bb (base
, suffix
, rtx_first
)
242 size_t namelen
= strlen (base
);
243 size_t suffixlen
= strlen (suffix
);
244 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
245 char *buf
= (char *) alloca (namelen
+ suffixlen
+ extlen
);
248 if (basic_block_info
== NULL
)
251 memcpy (buf
, base
, namelen
);
252 memcpy (buf
+ namelen
, suffix
, suffixlen
);
253 memcpy (buf
+ namelen
+ suffixlen
, graph_ext
[graph_dump_format
], extlen
);
255 fp
= fopen (buf
, "a");
260 fprintf (fp
, "(nil)\n");
263 enum bb_state
{ NOT_IN_BB
, IN_ONE_BB
, IN_MULTIPLE_BB
};
264 int max_uid
= get_max_uid ();
265 int *start
= (int *) xmalloc (max_uid
* sizeof (int));
266 int *end
= (int *) xmalloc (max_uid
* sizeof (int));
267 enum bb_state
*in_bb_p
= (enum bb_state
*)
268 xmalloc (max_uid
* sizeof (enum bb_state
));
272 for (i
= 0; i
< max_uid
; ++i
)
274 start
[i
] = end
[i
] = -1;
275 in_bb_p
[i
] = NOT_IN_BB
;
278 FOR_EACH_BB_REVERSE (bb
)
281 start
[INSN_UID (bb
->head
)] = bb
->index
;
282 end
[INSN_UID (bb
->end
)] = bb
->index
;
283 for (x
= bb
->head
; x
!= NULL_RTX
; x
= NEXT_INSN (x
))
285 in_bb_p
[INSN_UID (x
)]
286 = (in_bb_p
[INSN_UID (x
)] == NOT_IN_BB
)
287 ? IN_ONE_BB
: IN_MULTIPLE_BB
;
293 /* Tell print-rtl that we want graph output. */
296 /* Start new function. */
299 for (tmp_rtx
= NEXT_INSN (rtx_first
); NULL
!= tmp_rtx
;
300 tmp_rtx
= NEXT_INSN (tmp_rtx
))
302 int edge_printed
= 0;
305 if (start
[INSN_UID (tmp_rtx
)] < 0 && end
[INSN_UID (tmp_rtx
)] < 0)
307 if (GET_CODE (tmp_rtx
) == BARRIER
)
309 if (GET_CODE (tmp_rtx
) == NOTE
310 && (1 || in_bb_p
[INSN_UID (tmp_rtx
)] == NOT_IN_BB
))
314 if ((i
= start
[INSN_UID (tmp_rtx
)]) >= 0)
316 /* We start a subgraph for each basic block. */
320 draw_edge (fp
, 0, INSN_UID (tmp_rtx
), 1, 0);
323 /* Print the data for this node. */
324 node_data (fp
, tmp_rtx
);
325 next_insn
= next_nonnote_insn (tmp_rtx
);
327 if ((i
= end
[INSN_UID (tmp_rtx
)]) >= 0)
331 bb
= BASIC_BLOCK (i
);
333 /* End of the basic block. */
336 /* Now specify the edges to all the successors of this
338 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
340 if (e
->dest
!= EXIT_BLOCK_PTR
)
342 rtx block_head
= e
->dest
->head
;
344 draw_edge (fp
, INSN_UID (tmp_rtx
),
345 INSN_UID (block_head
),
346 next_insn
!= block_head
,
347 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
349 if (block_head
== next_insn
)
354 draw_edge (fp
, INSN_UID (tmp_rtx
), 999999,
356 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
366 /* Don't print edges to barriers. */
368 || GET_CODE (next_insn
) != BARRIER
)
369 draw_edge (fp
, XINT (tmp_rtx
, 0),
370 next_insn
? INSN_UID (next_insn
) : 999999, 0, 0);
373 /* We draw the remaining edges in class 3. We have
374 to skip over the barrier since these nodes are
375 not printed at all. */
377 next_insn
= NEXT_INSN (next_insn
);
379 && (GET_CODE (next_insn
) == NOTE
380 || GET_CODE (next_insn
) == BARRIER
));
382 draw_edge (fp
, XINT (tmp_rtx
, 0),
383 next_insn
? INSN_UID (next_insn
) : 999999, 0, 3);
402 /* Similar as clean_dump_file, but this time for graph output files. */
405 clean_graph_dump_file (base
, suffix
)
409 size_t namelen
= strlen (base
);
410 size_t suffixlen
= strlen (suffix
);
411 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
412 char *buf
= (char *) alloca (namelen
+ extlen
+ suffixlen
);
415 memcpy (buf
, base
, namelen
);
416 memcpy (buf
+ namelen
, suffix
, suffixlen
);
417 memcpy (buf
+ namelen
+ suffixlen
, graph_ext
[graph_dump_format
], extlen
);
419 fp
= fopen (buf
, "w");
422 fatal_error ("can't open %s: %m", buf
);
424 switch (graph_dump_format
)
427 fputs ("graph: {\nport_sharing: no\n", fp
);
437 /* Do final work on the graph output file. */
439 finish_graph_dump_file (base
, suffix
)
443 size_t namelen
= strlen (base
);
444 size_t suffixlen
= strlen (suffix
);
445 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
446 char *buf
= (char *) alloca (namelen
+ suffixlen
+ extlen
);
449 memcpy (buf
, base
, namelen
);
450 memcpy (buf
+ namelen
, suffix
, suffixlen
);
451 memcpy (buf
+ namelen
+ suffixlen
, graph_ext
[graph_dump_format
], extlen
);
453 fp
= fopen (buf
, "a");
456 switch (graph_dump_format
)