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