]>
Commit | Line | Data |
---|---|---|
735a0e33 | 1 | /* Output routines for graphical representation. |
400500c4 | 2 | Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
735a0e33 UD |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. |
4 | ||
1322177d | 5 | This file is part of GCC. |
735a0e33 | 6 | |
1322177d LB |
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. | |
735a0e33 | 11 | |
1322177d LB |
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. | |
735a0e33 | 16 | |
400500c4 | 17 | You should have received a copy of the GNU General Public License |
1322177d LB |
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. */ | |
735a0e33 UD |
21 | |
22 | #include <config.h> | |
23 | #include "system.h" | |
4977bab6 ZW |
24 | #include "coretypes.h" |
25 | #include "tm.h" | |
735a0e33 UD |
26 | |
27 | #include "rtl.h" | |
28 | #include "flags.h" | |
29 | #include "output.h" | |
49ad7cfa | 30 | #include "function.h" |
735a0e33 UD |
31 | #include "hard-reg-set.h" |
32 | #include "basic-block.h" | |
33 | #include "toplev.h" | |
6a2cc2ac | 34 | #include "graph.h" |
735a0e33 | 35 | |
27c38fbe | 36 | static const char *const graph_ext[] = |
735a0e33 UD |
37 | { |
38 | /* no_graph */ "", | |
39 | /* vcg */ ".vcg", | |
40 | }; | |
41 | ||
3fe41456 KG |
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 *)); | |
5f06c983 | 48 | |
735a0e33 UD |
49 | /* Output text for new basic block. */ |
50 | static void | |
51 | start_fct (fp) | |
52 | FILE *fp; | |
53 | { | |
735a0e33 UD |
54 | switch (graph_dump_format) |
55 | { | |
56 | case vcg: | |
57 | fprintf (fp, "\ | |
58 | graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n", | |
59 | current_function_name, current_function_name); | |
60 | break; | |
61 | case no_graph: | |
62 | break; | |
63 | } | |
64 | } | |
65 | ||
66 | static void | |
67 | start_bb (fp, bb) | |
68 | FILE *fp; | |
69 | int bb; | |
70 | { | |
71 | switch (graph_dump_format) | |
72 | { | |
73 | case vcg: | |
74 | fprintf (fp, "\ | |
75 | graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\ | |
76 | label: \"basic block %d", | |
77 | current_function_name, bb, bb); | |
78 | break; | |
79 | case no_graph: | |
80 | break; | |
81 | } | |
82 | ||
83 | #if 0 | |
dc297297 | 84 | /* FIXME Should this be printed? It makes the graph significantly larger. */ |
735a0e33 UD |
85 | |
86 | /* Print the live-at-start register list. */ | |
87 | fputc ('\n', fp); | |
88 | EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i, | |
89 | { | |
90 | fprintf (fp, " %d", i); | |
91 | if (i < FIRST_PSEUDO_REGISTER) | |
92 | fprintf (fp, " [%s]", | |
93 | reg_names[i]); | |
94 | }); | |
95 | #endif | |
96 | ||
97 | switch (graph_dump_format) | |
98 | { | |
99 | case vcg: | |
100 | fputs ("\"\n\n", fp); | |
101 | break; | |
102 | case no_graph: | |
103 | break; | |
104 | } | |
105 | } | |
106 | ||
a30d557c | 107 | static void |
735a0e33 UD |
108 | node_data (fp, tmp_rtx) |
109 | FILE *fp; | |
110 | rtx tmp_rtx; | |
111 | { | |
735a0e33 UD |
112 | if (PREV_INSN (tmp_rtx) == 0) |
113 | { | |
114 | /* This is the first instruction. Add an edge from the starting | |
115 | block. */ | |
116 | switch (graph_dump_format) | |
117 | { | |
118 | case vcg: | |
119 | fprintf (fp, "\ | |
120 | edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n", | |
121 | current_function_name, | |
122 | current_function_name, XINT (tmp_rtx, 0)); | |
123 | break; | |
124 | case no_graph: | |
125 | break; | |
126 | } | |
127 | } | |
128 | ||
129 | switch (graph_dump_format) | |
130 | { | |
131 | case vcg: | |
132 | fprintf (fp, "node: {\n title: \"%s.%d\"\n color: %s\n \ | |
133 | label: \"%s %d\n", | |
134 | current_function_name, XINT (tmp_rtx, 0), | |
135 | GET_CODE (tmp_rtx) == NOTE ? "lightgrey" | |
136 | : GET_CODE (tmp_rtx) == INSN ? "green" | |
137 | : GET_CODE (tmp_rtx) == JUMP_INSN ? "darkgreen" | |
138 | : GET_CODE (tmp_rtx) == CALL_INSN ? "darkgreen" | |
139 | : GET_CODE (tmp_rtx) == CODE_LABEL ? "\ | |
140 | darkgrey\n shape: ellipse" : "white", | |
141 | GET_RTX_NAME (GET_CODE (tmp_rtx)), XINT (tmp_rtx, 0)); | |
142 | break; | |
143 | case no_graph: | |
144 | break; | |
145 | } | |
146 | ||
147 | /* Print the RTL. */ | |
148 | if (GET_CODE (tmp_rtx) == NOTE) | |
149 | { | |
0a2287bf RH |
150 | const char *name = ""; |
151 | if (NOTE_LINE_NUMBER (tmp_rtx) < 0) | |
152 | name = GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (tmp_rtx)); | |
153 | fprintf (fp, " %s", name); | |
735a0e33 | 154 | } |
2c3c49de | 155 | else if (INSN_P (tmp_rtx)) |
a30d557c | 156 | print_rtl_single (fp, PATTERN (tmp_rtx)); |
735a0e33 | 157 | else |
a30d557c | 158 | print_rtl_single (fp, tmp_rtx); |
735a0e33 UD |
159 | |
160 | switch (graph_dump_format) | |
161 | { | |
162 | case vcg: | |
163 | fputs ("\"\n}\n", fp); | |
164 | break; | |
165 | case no_graph: | |
166 | break; | |
167 | } | |
735a0e33 UD |
168 | } |
169 | ||
170 | static void | |
171 | draw_edge (fp, from, to, bb_edge, class) | |
172 | FILE *fp; | |
173 | int from; | |
174 | int to; | |
175 | int bb_edge; | |
176 | int class; | |
177 | { | |
5f06c983 | 178 | const char * color; |
735a0e33 UD |
179 | switch (graph_dump_format) |
180 | { | |
181 | case vcg: | |
e881bb1b RH |
182 | color = ""; |
183 | if (class == 2) | |
184 | color = "color: red "; | |
185 | else if (bb_edge) | |
186 | color = "color: blue "; | |
187 | else if (class == 3) | |
188 | color = "color: green "; | |
735a0e33 UD |
189 | fprintf (fp, |
190 | "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s", | |
191 | current_function_name, from, | |
e881bb1b | 192 | current_function_name, to, color); |
735a0e33 UD |
193 | if (class) |
194 | fprintf (fp, "class: %d ", class); | |
195 | fputs ("}\n", fp); | |
196 | break; | |
197 | case no_graph: | |
198 | break; | |
199 | } | |
200 | } | |
201 | ||
202 | static void | |
5f06c983 | 203 | end_bb (fp) |
735a0e33 | 204 | FILE *fp; |
735a0e33 UD |
205 | { |
206 | switch (graph_dump_format) | |
207 | { | |
208 | case vcg: | |
209 | fputs ("}\n", fp); | |
210 | break; | |
211 | case no_graph: | |
212 | break; | |
213 | } | |
214 | } | |
215 | ||
216 | static void | |
217 | end_fct (fp) | |
218 | FILE *fp; | |
219 | { | |
220 | switch (graph_dump_format) | |
221 | { | |
222 | case vcg: | |
223 | fprintf (fp, "node: { title: \"%s.999999\" label: \"END\" }\n}\n", | |
224 | current_function_name); | |
225 | break; | |
226 | case no_graph: | |
227 | break; | |
228 | } | |
229 | } | |
230 | \f | |
231 | /* Like print_rtl, but also print out live information for the start of each | |
232 | basic block. */ | |
233 | void | |
234 | print_rtl_graph_with_bb (base, suffix, rtx_first) | |
235 | const char *base; | |
236 | const char *suffix; | |
237 | rtx rtx_first; | |
238 | { | |
b3694847 | 239 | rtx tmp_rtx; |
735a0e33 UD |
240 | size_t namelen = strlen (base); |
241 | size_t suffixlen = strlen (suffix); | |
242 | size_t extlen = strlen (graph_ext[graph_dump_format]) + 1; | |
243 | char *buf = (char *) alloca (namelen + suffixlen + extlen); | |
244 | FILE *fp; | |
245 | ||
e881bb1b RH |
246 | if (basic_block_info == NULL) |
247 | return; | |
735a0e33 UD |
248 | |
249 | memcpy (buf, base, namelen); | |
250 | memcpy (buf + namelen, suffix, suffixlen); | |
251 | memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen); | |
252 | ||
253 | fp = fopen (buf, "a"); | |
254 | if (fp == NULL) | |
255 | return; | |
256 | ||
257 | if (rtx_first == 0) | |
258 | fprintf (fp, "(nil)\n"); | |
259 | else | |
260 | { | |
735a0e33 UD |
261 | enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB }; |
262 | int max_uid = get_max_uid (); | |
4da896b2 MM |
263 | int *start = (int *) xmalloc (max_uid * sizeof (int)); |
264 | int *end = (int *) xmalloc (max_uid * sizeof (int)); | |
735a0e33 | 265 | enum bb_state *in_bb_p = (enum bb_state *) |
4da896b2 | 266 | xmalloc (max_uid * sizeof (enum bb_state)); |
e881bb1b | 267 | basic_block bb; |
e0082a72 | 268 | int i; |
735a0e33 | 269 | |
0b17ab2f | 270 | for (i = 0; i < max_uid; ++i) |
735a0e33 | 271 | { |
0b17ab2f RH |
272 | start[i] = end[i] = -1; |
273 | in_bb_p[i] = NOT_IN_BB; | |
735a0e33 UD |
274 | } |
275 | ||
e0082a72 | 276 | FOR_EACH_BB_REVERSE (bb) |
735a0e33 UD |
277 | { |
278 | rtx x; | |
e0082a72 ZD |
279 | start[INSN_UID (bb->head)] = bb->index; |
280 | end[INSN_UID (bb->end)] = bb->index; | |
e881bb1b | 281 | for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x)) |
735a0e33 UD |
282 | { |
283 | in_bb_p[INSN_UID (x)] | |
284 | = (in_bb_p[INSN_UID (x)] == NOT_IN_BB) | |
285 | ? IN_ONE_BB : IN_MULTIPLE_BB; | |
e881bb1b | 286 | if (x == bb->end) |
735a0e33 UD |
287 | break; |
288 | } | |
289 | } | |
290 | ||
735a0e33 UD |
291 | /* Tell print-rtl that we want graph output. */ |
292 | dump_for_graph = 1; | |
293 | ||
294 | /* Start new function. */ | |
295 | start_fct (fp); | |
296 | ||
297 | for (tmp_rtx = NEXT_INSN (rtx_first); NULL != tmp_rtx; | |
298 | tmp_rtx = NEXT_INSN (tmp_rtx)) | |
299 | { | |
735a0e33 UD |
300 | int edge_printed = 0; |
301 | rtx next_insn; | |
302 | ||
303 | if (start[INSN_UID (tmp_rtx)] < 0 && end[INSN_UID (tmp_rtx)] < 0) | |
304 | { | |
305 | if (GET_CODE (tmp_rtx) == BARRIER) | |
306 | continue; | |
307 | if (GET_CODE (tmp_rtx) == NOTE | |
308 | && (1 || in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB)) | |
309 | continue; | |
310 | } | |
311 | ||
0b17ab2f | 312 | if ((i = start[INSN_UID (tmp_rtx)]) >= 0) |
735a0e33 UD |
313 | { |
314 | /* We start a subgraph for each basic block. */ | |
0b17ab2f | 315 | start_bb (fp, i); |
735a0e33 | 316 | |
0b17ab2f | 317 | if (i == 0) |
735a0e33 UD |
318 | draw_edge (fp, 0, INSN_UID (tmp_rtx), 1, 0); |
319 | } | |
320 | ||
321 | /* Print the data for this node. */ | |
a30d557c | 322 | node_data (fp, tmp_rtx); |
735a0e33 UD |
323 | next_insn = next_nonnote_insn (tmp_rtx); |
324 | ||
0b17ab2f | 325 | if ((i = end[INSN_UID (tmp_rtx)]) >= 0) |
735a0e33 | 326 | { |
e881bb1b RH |
327 | edge e; |
328 | ||
0b17ab2f | 329 | bb = BASIC_BLOCK (i); |
735a0e33 UD |
330 | |
331 | /* End of the basic block. */ | |
5f06c983 | 332 | end_bb (fp); |
735a0e33 UD |
333 | |
334 | /* Now specify the edges to all the successors of this | |
335 | basic block. */ | |
e881bb1b | 336 | for (e = bb->succ; e ; e = e->succ_next) |
735a0e33 | 337 | { |
e881bb1b | 338 | if (e->dest != EXIT_BLOCK_PTR) |
735a0e33 | 339 | { |
e881bb1b | 340 | rtx block_head = e->dest->head; |
735a0e33 UD |
341 | |
342 | draw_edge (fp, INSN_UID (tmp_rtx), | |
343 | INSN_UID (block_head), | |
e881bb1b RH |
344 | next_insn != block_head, |
345 | (e->flags & EDGE_ABNORMAL ? 2 : 0)); | |
735a0e33 | 346 | |
e881bb1b | 347 | if (block_head == next_insn) |
735a0e33 UD |
348 | edge_printed = 1; |
349 | } | |
e881bb1b | 350 | else |
735a0e33 UD |
351 | { |
352 | draw_edge (fp, INSN_UID (tmp_rtx), 999999, | |
e881bb1b RH |
353 | next_insn != 0, |
354 | (e->flags & EDGE_ABNORMAL ? 2 : 0)); | |
735a0e33 UD |
355 | |
356 | if (next_insn == 0) | |
357 | edge_printed = 1; | |
358 | } | |
735a0e33 UD |
359 | } |
360 | } | |
361 | ||
362 | if (!edge_printed) | |
363 | { | |
364 | /* Don't print edges to barriers. */ | |
365 | if (next_insn == 0 | |
366 | || GET_CODE (next_insn) != BARRIER) | |
367 | draw_edge (fp, XINT (tmp_rtx, 0), | |
368 | next_insn ? INSN_UID (next_insn) : 999999, 0, 0); | |
369 | else | |
370 | { | |
e881bb1b RH |
371 | /* We draw the remaining edges in class 3. We have |
372 | to skip over the barrier since these nodes are | |
735a0e33 UD |
373 | not printed at all. */ |
374 | do | |
375 | next_insn = NEXT_INSN (next_insn); | |
376 | while (next_insn | |
377 | && (GET_CODE (next_insn) == NOTE | |
378 | || GET_CODE (next_insn) == BARRIER)); | |
379 | ||
380 | draw_edge (fp, XINT (tmp_rtx, 0), | |
e881bb1b | 381 | next_insn ? INSN_UID (next_insn) : 999999, 0, 3); |
735a0e33 UD |
382 | } |
383 | } | |
384 | } | |
385 | ||
386 | dump_for_graph = 0; | |
387 | ||
388 | end_fct (fp); | |
4da896b2 MM |
389 | |
390 | /* Clean up. */ | |
391 | free (start); | |
392 | free (end); | |
393 | free (in_bb_p); | |
735a0e33 UD |
394 | } |
395 | ||
396 | fclose (fp); | |
397 | } | |
398 | ||
399 | ||
400 | /* Similar as clean_dump_file, but this time for graph output files. */ | |
400500c4 | 401 | |
735a0e33 UD |
402 | void |
403 | clean_graph_dump_file (base, suffix) | |
404 | const char *base; | |
405 | const char *suffix; | |
406 | { | |
407 | size_t namelen = strlen (base); | |
408 | size_t suffixlen = strlen (suffix); | |
409 | size_t extlen = strlen (graph_ext[graph_dump_format]) + 1; | |
410 | char *buf = (char *) alloca (namelen + extlen + suffixlen); | |
411 | FILE *fp; | |
412 | ||
413 | memcpy (buf, base, namelen); | |
414 | memcpy (buf + namelen, suffix, suffixlen); | |
415 | memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen); | |
416 | ||
417 | fp = fopen (buf, "w"); | |
418 | ||
419 | if (fp == NULL) | |
fa6ef813 | 420 | fatal_error ("can't open %s: %m", buf); |
735a0e33 UD |
421 | |
422 | switch (graph_dump_format) | |
423 | { | |
424 | case vcg: | |
425 | fputs ("graph: {\nport_sharing: no\n", fp); | |
426 | break; | |
427 | case no_graph: | |
428 | abort (); | |
429 | } | |
430 | ||
431 | fclose (fp); | |
432 | } | |
433 | ||
434 | ||
435 | /* Do final work on the graph output file. */ | |
436 | void | |
437 | finish_graph_dump_file (base, suffix) | |
438 | const char *base; | |
439 | const char *suffix; | |
440 | { | |
441 | size_t namelen = strlen (base); | |
442 | size_t suffixlen = strlen (suffix); | |
443 | size_t extlen = strlen (graph_ext[graph_dump_format]) + 1; | |
444 | char *buf = (char *) alloca (namelen + suffixlen + extlen); | |
445 | FILE *fp; | |
446 | ||
447 | memcpy (buf, base, namelen); | |
448 | memcpy (buf + namelen, suffix, suffixlen); | |
449 | memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen); | |
450 | ||
451 | fp = fopen (buf, "a"); | |
452 | if (fp != NULL) | |
453 | { | |
454 | switch (graph_dump_format) | |
455 | { | |
456 | case vcg: | |
457 | fputs ("}\n", fp); | |
458 | break; | |
459 | case no_graph: | |
460 | abort (); | |
461 | } | |
462 | ||
463 | fclose (fp); | |
464 | } | |
465 | } |