OSDN Git Service

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