1 /* Output routines for graphical representation.
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2007, 2008, 2010
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "diagnostic-core.h"
37 static const char *const graph_ext
[] =
43 /* The flag to indicate if output is inside of a building block. */
46 static void start_fct (FILE *);
47 static void start_bb (FILE *, int);
48 static void node_data (FILE *, rtx
);
49 static void draw_edge (FILE *, int, int, int, int);
50 static void end_fct (FILE *);
51 static void end_bb (FILE *);
53 /* Output text for new basic block. */
57 switch (graph_dump_format
)
61 graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
62 current_function_name (), current_function_name ());
70 start_bb (FILE *fp
, int bb
)
76 switch (graph_dump_format
)
80 graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
81 label: \"basic block %d",
82 current_function_name (), bb
, bb
);
83 inbb
= 1; /* Now We are inside of a building block. */
90 /* FIXME Should this be printed? It makes the graph significantly larger. */
92 /* Print the live-at-start register list. */
94 EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start
[bb
], 0, i
, rsi
)
96 fprintf (fp
, " %d", i
);
97 if (i
< FIRST_PSEUDO_REGISTER
)
98 fprintf (fp
, " [%s]", reg_names
[i
]);
102 switch (graph_dump_format
)
105 fputs ("\"\n\n", fp
);
113 node_data (FILE *fp
, rtx tmp_rtx
)
115 if (PREV_INSN (tmp_rtx
) == 0)
117 /* This is the first instruction. Add an edge from the starting
119 switch (graph_dump_format
)
123 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
124 current_function_name (),
125 current_function_name (), XINT (tmp_rtx
, 0));
132 switch (graph_dump_format
)
135 fprintf (fp
, "node: {\n title: \"%s.%d\"\n color: %s\n \
137 current_function_name (), XINT (tmp_rtx
, 0),
138 NOTE_P (tmp_rtx
) ? "lightgrey"
139 : NONJUMP_INSN_P (tmp_rtx
) ? "green"
140 : JUMP_P (tmp_rtx
) ? "darkgreen"
141 : CALL_P (tmp_rtx
) ? "darkgreen"
142 : LABEL_P (tmp_rtx
) ? "\
143 darkgrey\n shape: ellipse" : "white",
144 GET_RTX_NAME (GET_CODE (tmp_rtx
)), XINT (tmp_rtx
, 0));
151 if (NOTE_P (tmp_rtx
))
154 name
= GET_NOTE_INSN_NAME (NOTE_KIND (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 (FILE *fp
, int from
, int to
, int bb_edge
, int color_class
)
176 switch (graph_dump_format
)
180 if (color_class
== 2)
181 color
= "color: red ";
183 color
= "color: blue ";
184 else if (color_class
== 3)
185 color
= "color: green ";
187 "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
188 current_function_name (), from
,
189 current_function_name (), to
, color
);
191 fprintf (fp
, "class: %d ", color_class
);
202 switch (graph_dump_format
)
205 /* Check if we are inside of a building block. */
209 inbb
= 0; /* Now we are outside of a building block. */
220 switch (graph_dump_format
)
223 fprintf (fp
, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
224 current_function_name ());
231 /* Like print_rtl, but also print out live information for the start of each
234 print_rtl_graph_with_bb (const char *base
, rtx rtx_first
)
237 size_t namelen
= strlen (base
);
238 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
239 char *buf
= XALLOCAVEC (char, namelen
+ extlen
);
242 if (basic_block_info
== NULL
)
245 memcpy (buf
, base
, namelen
);
246 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
248 fp
= fopen (buf
, "a");
253 fprintf (fp
, "(nil)\n");
256 enum bb_state
{ NOT_IN_BB
, IN_ONE_BB
, IN_MULTIPLE_BB
};
257 int max_uid
= get_max_uid ();
258 int *start
= XNEWVEC (int, max_uid
);
259 int *end
= XNEWVEC (int, max_uid
);
260 enum bb_state
*in_bb_p
= XNEWVEC (enum bb_state
, max_uid
);
264 for (i
= 0; i
< max_uid
; ++i
)
266 start
[i
] = end
[i
] = -1;
267 in_bb_p
[i
] = NOT_IN_BB
;
270 FOR_EACH_BB_REVERSE (bb
)
273 start
[INSN_UID (BB_HEAD (bb
))] = bb
->index
;
274 end
[INSN_UID (BB_END (bb
))] = bb
->index
;
275 for (x
= BB_HEAD (bb
); x
!= NULL_RTX
; x
= NEXT_INSN (x
))
277 in_bb_p
[INSN_UID (x
)]
278 = (in_bb_p
[INSN_UID (x
)] == NOT_IN_BB
)
279 ? IN_ONE_BB
: IN_MULTIPLE_BB
;
280 if (x
== BB_END (bb
))
285 /* Tell print-rtl that we want graph output. */
288 /* Start new function. */
291 for (tmp_rtx
= NEXT_INSN (rtx_first
); NULL
!= tmp_rtx
;
292 tmp_rtx
= NEXT_INSN (tmp_rtx
))
294 int edge_printed
= 0;
297 if (start
[INSN_UID (tmp_rtx
)] < 0 && end
[INSN_UID (tmp_rtx
)] < 0)
299 if (BARRIER_P (tmp_rtx
))
302 && (1 || in_bb_p
[INSN_UID (tmp_rtx
)] == NOT_IN_BB
))
306 if ((i
= start
[INSN_UID (tmp_rtx
)]) >= 0)
308 /* We start a subgraph for each basic block. */
312 draw_edge (fp
, 0, INSN_UID (tmp_rtx
), 1, 0);
315 /* Print the data for this node. */
316 node_data (fp
, tmp_rtx
);
317 next_insn
= next_nonnote_insn (tmp_rtx
);
319 if ((i
= end
[INSN_UID (tmp_rtx
)]) >= 0)
324 bb
= BASIC_BLOCK (i
);
326 /* End of the basic block. */
329 /* Now specify the edges to all the successors of this
331 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
333 if (e
->dest
!= EXIT_BLOCK_PTR
)
335 rtx block_head
= BB_HEAD (e
->dest
);
337 draw_edge (fp
, INSN_UID (tmp_rtx
),
338 INSN_UID (block_head
),
339 next_insn
!= block_head
,
340 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
342 if (block_head
== next_insn
)
347 draw_edge (fp
, INSN_UID (tmp_rtx
), 999999,
349 (e
->flags
& EDGE_ABNORMAL
? 2 : 0));
359 /* Don't print edges to barriers. */
361 || !BARRIER_P (next_insn
))
362 draw_edge (fp
, XINT (tmp_rtx
, 0),
363 next_insn
? INSN_UID (next_insn
) : 999999, 0, 0);
366 /* We draw the remaining edges in class 3. We have
367 to skip over the barrier since these nodes are
368 not printed at all. */
370 next_insn
= NEXT_INSN (next_insn
);
372 && (NOTE_P (next_insn
)
373 || BARRIER_P (next_insn
)));
375 draw_edge (fp
, XINT (tmp_rtx
, 0),
376 next_insn
? INSN_UID (next_insn
) : 999999, 0, 3);
395 /* Similar as clean_dump_file, but this time for graph output files. */
398 clean_graph_dump_file (const char *base
)
400 size_t namelen
= strlen (base
);
401 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
402 char *buf
= XALLOCAVEC (char, namelen
+ extlen
);
405 memcpy (buf
, base
, namelen
);
406 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
408 fp
= fopen (buf
, "w");
411 fatal_error ("can%'t open %s: %m", buf
);
413 gcc_assert (graph_dump_format
== vcg
);
414 fputs ("graph: {\nport_sharing: no\n", fp
);
420 /* Do final work on the graph output file. */
422 finish_graph_dump_file (const char *base
)
424 size_t namelen
= strlen (base
);
425 size_t extlen
= strlen (graph_ext
[graph_dump_format
]) + 1;
426 char *buf
= XALLOCAVEC (char, namelen
+ extlen
);
429 memcpy (buf
, base
, namelen
);
430 memcpy (buf
+ namelen
, graph_ext
[graph_dump_format
], extlen
);
432 fp
= fopen (buf
, "a");
435 gcc_assert (graph_dump_format
== vcg
);