OSDN Git Service

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