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"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
36 static const char *const graph_ext
[] =
42 static void start_fct (FILE *);
43 static void start_bb (FILE *, int);
44 static void node_data (FILE *, rtx
);
45 static void draw_edge (FILE *, int, int, int, int);
46 static void end_fct (FILE *);
47 static void end_bb (FILE *);
49 /* Output text for new basic block. */
53 switch (graph_dump_format
)
57 graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
58 current_function_name
, current_function_name
);
66 start_bb (FILE *fp
, int bb
)
68 switch (graph_dump_format
)
72 graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
73 label: \"basic block %d",
74 current_function_name
, bb
, bb
);
81 /* FIXME Should this be printed? It makes the graph significantly larger. */
83 /* Print the live-at-start register list. */
85 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start
[bb
], 0, i
,
87 fprintf (fp
, " %d", i
);
88 if (i
< FIRST_PSEUDO_REGISTER
)
94 switch (graph_dump_format
)
105 node_data (FILE *fp
, rtx tmp_rtx
)
107 if (PREV_INSN (tmp_rtx
) == 0)
109 /* This is the first instruction. Add an edge from the starting
111 switch (graph_dump_format
)
115 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
116 current_function_name
,
117 current_function_name
, XINT (tmp_rtx
, 0));
124 switch (graph_dump_format
)
127 fprintf (fp
, "node: {\n title: \"%s.%d\"\n color: %s\n \
129 current_function_name
, XINT (tmp_rtx
, 0),
130 GET_CODE (tmp_rtx
) == NOTE
? "lightgrey"
131 : GET_CODE (tmp_rtx
) == INSN
? "green"
132 : GET_CODE (tmp_rtx
) == JUMP_INSN
? "darkgreen"
133 : GET_CODE (tmp_rtx
) == CALL_INSN
? "darkgreen"
134 : GET_CODE (tmp_rtx
) == CODE_LABEL
? "\
135 darkgrey\n shape: ellipse" : "white",
136 GET_RTX_NAME (GET_CODE (tmp_rtx
)), XINT (tmp_rtx
, 0));
143 if (GET_CODE (tmp_rtx
) == NOTE
)
145 const char *name
= "";
146 if (NOTE_LINE_NUMBER (tmp_rtx
) < 0)
147 name
= GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (tmp_rtx
));
148 fprintf (fp
, " %s", name
);
150 else if (INSN_P (tmp_rtx
))
151 print_rtl_single (fp
, PATTERN (tmp_rtx
));
153 print_rtl_single (fp
, tmp_rtx
);
155 switch (graph_dump_format
)
158 fputs ("\"\n}\n", fp
);
166 draw_edge (FILE *fp
, int from
, int to
, int bb_edge
, int class)
169 switch (graph_dump_format
)
174 color
= "color: red ";
176 color
= "color: blue ";
178 color
= "color: green ";
180 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
181 current_function_name
, from
,
182 current_function_name
, to
, color
);
184 fprintf (fp
, "class: %d ", class);
195 switch (graph_dump_format
)
208 switch (graph_dump_format
)
211 fprintf (fp
, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
212 current_function_name
);
219 /* Like print_rtl, but also print out live information for the start of each
222 print_rtl_graph_with_bb (const char *base
, const char *suffix
, rtx rtx_first
)
225 size_t namelen
= strlen (base
);
226 size_t suffixlen
= strlen (suffix
);
227 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
228 char *buf
= alloca (namelen
+ suffixlen
+ extlen
);
231 if (basic_block_info
== NULL
)
234 memcpy (buf
, base
, namelen
);
235 memcpy (buf
+ namelen
, suffix
, suffixlen
);
236 memcpy (buf
+ namelen
+ suffixlen
, graph_ext
[graph_dump_format
], extlen
);
238 fp
= fopen (buf
, "a");
243 fprintf (fp
, "(nil)\n");
246 enum bb_state
{ NOT_IN_BB
, IN_ONE_BB
, IN_MULTIPLE_BB
};
247 int max_uid
= get_max_uid ();
248 int *start
= xmalloc (max_uid
* sizeof (int));
249 int *end
= xmalloc (max_uid
* sizeof (int));
250 enum bb_state
*in_bb_p
= xmalloc (max_uid
* sizeof (enum bb_state
));
254 for (i
= 0; i
< max_uid
; ++i
)
256 start
[i
] = end
[i
] = -1;
257 in_bb_p
[i
] = NOT_IN_BB
;
260 FOR_EACH_BB_REVERSE (bb
)
263 start
[INSN_UID (bb
->head
)] = bb
->index
;
264 end
[INSN_UID (bb
->end
)] = bb
->index
;
265 for (x
= bb
->head
; x
!= NULL_RTX
; x
= NEXT_INSN (x
))
267 in_bb_p
[INSN_UID (x
)]
268 = (in_bb_p
[INSN_UID (x
)] == NOT_IN_BB
)
269 ? IN_ONE_BB
: IN_MULTIPLE_BB
;
275 /* Tell print-rtl that we want graph output. */
278 /* Start new function. */
281 for (tmp_rtx
= NEXT_INSN (rtx_first
); NULL
!= tmp_rtx
;
282 tmp_rtx
= NEXT_INSN (tmp_rtx
))
284 int edge_printed
= 0;
287 if (start
[INSN_UID (tmp_rtx
)] < 0 && end
[INSN_UID (tmp_rtx
)] < 0)
289 if (GET_CODE (tmp_rtx
) == BARRIER
)
291 if (GET_CODE (tmp_rtx
) == NOTE
292 && (1 || in_bb_p
[INSN_UID (tmp_rtx
)] == NOT_IN_BB
))
296 if ((i
= start
[INSN_UID (tmp_rtx
)]) >= 0)
298 /* We start a subgraph for each basic block. */
302 draw_edge (fp
, 0, INSN_UID (tmp_rtx
), 1, 0);
305 /* Print the data for this node. */
306 node_data (fp
, tmp_rtx
);
307 next_insn
= next_nonnote_insn (tmp_rtx
);
309 if ((i
= end
[INSN_UID (tmp_rtx
)]) >= 0)
313 bb
= BASIC_BLOCK (i
);
315 /* End of the basic block. */
318 /* Now specify the edges to all the successors of this
320 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
322 if (e
->dest
!= EXIT_BLOCK_PTR
)
324 rtx block_head
= e
->dest
->head
;
326 draw_edge (fp
, INSN_UID (tmp_rtx
),
327 INSN_UID (block_head
),
328 next_insn
!= block_head
,
329 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
331 if (block_head
== next_insn
)
336 draw_edge (fp
, INSN_UID (tmp_rtx
), 999999,
338 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
348 /* Don't print edges to barriers. */
350 || GET_CODE (next_insn
) != BARRIER
)
351 draw_edge (fp
, XINT (tmp_rtx
, 0),
352 next_insn
? INSN_UID (next_insn
) : 999999, 0, 0);
355 /* We draw the remaining edges in class 3. We have
356 to skip over the barrier since these nodes are
357 not printed at all. */
359 next_insn
= NEXT_INSN (next_insn
);
361 && (GET_CODE (next_insn
) == NOTE
362 || GET_CODE (next_insn
) == BARRIER
));
364 draw_edge (fp
, XINT (tmp_rtx
, 0),
365 next_insn
? INSN_UID (next_insn
) : 999999, 0, 3);
384 /* Similar as clean_dump_file, but this time for graph output files. */
387 clean_graph_dump_file (const char *base
, const char *suffix
)
389 size_t namelen
= strlen (base
);
390 size_t suffixlen
= strlen (suffix
);
391 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
392 char *buf
= alloca (namelen
+ extlen
+ suffixlen
);
395 memcpy (buf
, base
, namelen
);
396 memcpy (buf
+ namelen
, suffix
, suffixlen
);
397 memcpy (buf
+ namelen
+ suffixlen
, graph_ext
[graph_dump_format
], extlen
);
399 fp
= fopen (buf
, "w");
402 fatal_error ("can't open %s: %m", buf
);
404 switch (graph_dump_format
)
407 fputs ("graph: {\nport_sharing: no\n", fp
);
417 /* Do final work on the graph output file. */
419 finish_graph_dump_file (const char *base
, const char *suffix
)
421 size_t namelen
= strlen (base
);
422 size_t suffixlen
= strlen (suffix
);
423 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
424 char *buf
= alloca (namelen
+ suffixlen
+ extlen
);
427 memcpy (buf
, base
, namelen
);
428 memcpy (buf
+ namelen
, suffix
, suffixlen
);
429 memcpy (buf
+ namelen
+ suffixlen
, graph_ext
[graph_dump_format
], extlen
);
431 fp
= fopen (buf
, "a");
434 switch (graph_dump_format
)