OSDN Git Service

2004-09-16 Frank Ch. Eigler <fche@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / graph.c
1 /* Output routines for graphical representation.
2    Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004
3    Free Software Foundation, Inc.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6 This file is part of GCC.
7
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 2, or (at your option) any later
11 version.
12
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
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 #include <config.h>
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27
28 #include "rtl.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "function.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "toplev.h"
35 #include "graph.h"
36
37 static const char *const graph_ext[] =
38 {
39   /* no_graph */ "",
40   /* vcg */      ".vcg",
41 };
42
43 static void start_fct (FILE *);
44 static void start_bb (FILE *, int);
45 static void node_data (FILE *, rtx);
46 static void draw_edge (FILE *, int, int, int, int);
47 static void end_fct (FILE *);
48 static void end_bb (FILE *);
49
50 /* Output text for new basic block.  */
51 static void
52 start_fct (FILE *fp)
53 {
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 (FILE *fp, 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 (FILE *fp, rtx tmp_rtx)
107 {
108   if (PREV_INSN (tmp_rtx) == 0)
109     {
110       /* This is the first instruction.  Add an edge from the starting
111          block.  */
112       switch (graph_dump_format)
113         {
114         case vcg:
115           fprintf (fp, "\
116 edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
117                    current_function_name (),
118                    current_function_name (), XINT (tmp_rtx, 0));
119           break;
120         case no_graph:
121           break;
122         }
123     }
124
125   switch (graph_dump_format)
126     {
127     case vcg:
128       fprintf (fp, "node: {\n  title: \"%s.%d\"\n  color: %s\n  \
129 label: \"%s %d\n",
130                current_function_name (), XINT (tmp_rtx, 0),
131                NOTE_P (tmp_rtx) ? "lightgrey"
132                : NONJUMP_INSN_P (tmp_rtx) ? "green"
133                : JUMP_P (tmp_rtx) ? "darkgreen"
134                : CALL_P (tmp_rtx) ? "darkgreen"
135                : LABEL_P (tmp_rtx) ?  "\
136 darkgrey\n  shape: ellipse" : "white",
137                GET_RTX_NAME (GET_CODE (tmp_rtx)), XINT (tmp_rtx, 0));
138       break;
139     case no_graph:
140       break;
141     }
142
143   /* Print the RTL.  */
144   if (NOTE_P (tmp_rtx))
145     {
146       const char *name = "";
147       if (NOTE_LINE_NUMBER (tmp_rtx) < 0)
148         name =  GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (tmp_rtx));
149       fprintf (fp, " %s", name);
150     }
151   else if (INSN_P (tmp_rtx))
152     print_rtl_single (fp, PATTERN (tmp_rtx));
153   else
154     print_rtl_single (fp, tmp_rtx);
155
156   switch (graph_dump_format)
157     {
158     case vcg:
159       fputs ("\"\n}\n", fp);
160       break;
161     case no_graph:
162       break;
163     }
164 }
165
166 static void
167 draw_edge (FILE *fp, int from, int to, int bb_edge, int class)
168 {
169   const char * color;
170   switch (graph_dump_format)
171     {
172     case vcg:
173       color = "";
174       if (class == 2)
175         color = "color: red ";
176       else if (bb_edge)
177         color = "color: blue ";
178       else if (class == 3)
179         color = "color: green ";
180       fprintf (fp,
181                "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
182                current_function_name (), from,
183                current_function_name (), to, color);
184       if (class)
185         fprintf (fp, "class: %d ", class);
186       fputs ("}\n", fp);
187       break;
188     case no_graph:
189       break;
190     }
191 }
192
193 static void
194 end_bb (FILE *fp)
195 {
196   switch (graph_dump_format)
197     {
198     case vcg:
199       fputs ("}\n", fp);
200       break;
201     case no_graph:
202       break;
203     }
204 }
205
206 static void
207 end_fct (FILE *fp)
208 {
209   switch (graph_dump_format)
210     {
211     case vcg:
212       fprintf (fp, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
213                current_function_name ());
214       break;
215     case no_graph:
216       break;
217     }
218 }
219 \f
220 /* Like print_rtl, but also print out live information for the start of each
221    basic block.  */
222 void
223 print_rtl_graph_with_bb (const char *base, rtx rtx_first)
224 {
225   rtx tmp_rtx;
226   size_t namelen = strlen (base);
227   size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
228   char *buf = alloca (namelen + extlen);
229   FILE *fp;
230
231   if (basic_block_info == NULL)
232     return;
233
234   memcpy (buf, base, namelen);
235   memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
236
237   fp = fopen (buf, "a");
238   if (fp == NULL)
239     return;
240
241   if (rtx_first == 0)
242     fprintf (fp, "(nil)\n");
243   else
244     {
245       enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
246       int max_uid = get_max_uid ();
247       int *start = xmalloc (max_uid * sizeof (int));
248       int *end = xmalloc (max_uid * sizeof (int));
249       enum bb_state *in_bb_p = xmalloc (max_uid * sizeof (enum bb_state));
250       basic_block bb;
251       int i;
252
253       for (i = 0; i < max_uid; ++i)
254         {
255           start[i] = end[i] = -1;
256           in_bb_p[i] = NOT_IN_BB;
257         }
258
259       FOR_EACH_BB_REVERSE (bb)
260         {
261           rtx x;
262           start[INSN_UID (BB_HEAD (bb))] = bb->index;
263           end[INSN_UID (BB_END (bb))] = bb->index;
264           for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
265             {
266               in_bb_p[INSN_UID (x)]
267                 = (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
268                  ? IN_ONE_BB : IN_MULTIPLE_BB;
269               if (x == BB_END (bb))
270                 break;
271             }
272         }
273
274       /* Tell print-rtl that we want graph output.  */
275       dump_for_graph = 1;
276
277       /* Start new function.  */
278       start_fct (fp);
279
280       for (tmp_rtx = NEXT_INSN (rtx_first); NULL != tmp_rtx;
281            tmp_rtx = NEXT_INSN (tmp_rtx))
282         {
283           int edge_printed = 0;
284           rtx next_insn;
285
286           if (start[INSN_UID (tmp_rtx)] < 0 && end[INSN_UID (tmp_rtx)] < 0)
287             {
288               if (BARRIER_P (tmp_rtx))
289                 continue;
290               if (NOTE_P (tmp_rtx)
291                   && (1 || in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB))
292                 continue;
293             }
294
295           if ((i = start[INSN_UID (tmp_rtx)]) >= 0)
296             {
297               /* We start a subgraph for each basic block.  */
298               start_bb (fp, i);
299
300               if (i == 0)
301                 draw_edge (fp, 0, INSN_UID (tmp_rtx), 1, 0);
302             }
303
304           /* Print the data for this node.  */
305           node_data (fp, tmp_rtx);
306           next_insn = next_nonnote_insn (tmp_rtx);
307
308           if ((i = end[INSN_UID (tmp_rtx)]) >= 0)
309             {
310               edge e;
311
312               bb = BASIC_BLOCK (i);
313
314               /* End of the basic block.  */
315               end_bb (fp);
316
317               /* Now specify the edges to all the successors of this
318                  basic block.  */
319               for (e = bb->succ; e ; e = e->succ_next)
320                 {
321                   if (e->dest != EXIT_BLOCK_PTR)
322                     {
323                       rtx block_head = BB_HEAD (e->dest);
324
325                       draw_edge (fp, INSN_UID (tmp_rtx),
326                                  INSN_UID (block_head),
327                                  next_insn != block_head,
328                                  (e->flags & EDGE_ABNORMAL ? 2 : 0));
329
330                       if (block_head == next_insn)
331                         edge_printed = 1;
332                     }
333                   else
334                     {
335                       draw_edge (fp, INSN_UID (tmp_rtx), 999999,
336                                  next_insn != 0,
337                                  (e->flags & EDGE_ABNORMAL ? 2 : 0));
338
339                       if (next_insn == 0)
340                         edge_printed = 1;
341                     }
342                 }
343             }
344
345           if (!edge_printed)
346             {
347               /* Don't print edges to barriers.  */
348               if (next_insn == 0
349                   || !BARRIER_P (next_insn))
350                 draw_edge (fp, XINT (tmp_rtx, 0),
351                            next_insn ? INSN_UID (next_insn) : 999999, 0, 0);
352               else
353                 {
354                   /* We draw the remaining edges in class 3.  We have
355                      to skip over the barrier since these nodes are
356                      not printed at all.  */
357                   do
358                     next_insn = NEXT_INSN (next_insn);
359                   while (next_insn
360                          && (NOTE_P (next_insn)
361                              || BARRIER_P (next_insn)));
362
363                   draw_edge (fp, XINT (tmp_rtx, 0),
364                              next_insn ? INSN_UID (next_insn) : 999999, 0, 3);
365                 }
366             }
367         }
368
369       dump_for_graph = 0;
370
371       end_fct (fp);
372
373       /* Clean up.  */
374       free (start);
375       free (end);
376       free (in_bb_p);
377     }
378
379   fclose (fp);
380 }
381
382
383 /* Similar as clean_dump_file, but this time for graph output files.  */
384
385 void
386 clean_graph_dump_file (const char *base)
387 {
388   size_t namelen = strlen (base);
389   size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
390   char *buf = alloca (namelen + extlen);
391   FILE *fp;
392
393   memcpy (buf, base, namelen);
394   memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
395
396   fp = fopen (buf, "w");
397
398   if (fp == NULL)
399     fatal_error ("can't open %s: %m", buf);
400
401   gcc_assert (graph_dump_format == vcg);
402   fputs ("graph: {\nport_sharing: no\n", fp);
403
404   fclose (fp);
405 }
406
407
408 /* Do final work on the graph output file.  */
409 void
410 finish_graph_dump_file (const char *base)
411 {
412   size_t namelen = strlen (base);
413   size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
414   char *buf = alloca (namelen + extlen);
415   FILE *fp;
416
417   memcpy (buf, base, namelen);
418   memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
419
420   fp = fopen (buf, "a");
421   if (fp != NULL)
422     {
423       gcc_assert (graph_dump_format == vcg);
424       fputs ("}\n", fp);
425       fclose (fp);
426     }
427 }