graph.c 10.4 KB
Newer Older
1
/* Output routines for graphical representation.
2
   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 4
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.

5
This file is part of GCC.
6

7 8 9 10
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.
11

12 13 14 15
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
16

17
You should have received a copy of the GNU General Public License
18 19 20
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.  */
21 22 23 24 25 26 27

#include <config.h>
#include "system.h"

#include "rtl.h"
#include "flags.h"
#include "output.h"
28
#include "function.h"
29 30 31
#include "hard-reg-set.h"
#include "basic-block.h"
#include "toplev.h"
32
#include "graph.h"
33

34
static const char *const graph_ext[] =
35 36 37 38 39
{
  /* no_graph */ "",
  /* vcg */      ".vcg",
};

40 41 42 43 44 45
static void start_fct PARAMS ((FILE *));
static void start_bb PARAMS ((FILE *, int));
static void node_data PARAMS ((FILE *, rtx));
static void draw_edge PARAMS ((FILE *, int, int, int, int));
static void end_fct PARAMS ((FILE *));
static void end_bb PARAMS ((FILE *));
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/* Output text for new basic block.  */
static void
start_fct (fp)
     FILE *fp;
{

  switch (graph_dump_format)
    {
    case vcg:
      fprintf (fp, "\
graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
	       current_function_name, current_function_name);
      break;
    case no_graph:
      break;
    }
}

static void
start_bb (fp, bb)
     FILE *fp;
     int bb;
{
  switch (graph_dump_format)
    {
    case vcg:
      fprintf (fp, "\
graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
label: \"basic block %d",
	       current_function_name, bb, bb);
      break;
    case no_graph:
      break;
    }

#if 0
83
  /* FIXME Should this be printed?  It makes the graph significantly larger.  */
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  /* Print the live-at-start register list.  */
  fputc ('\n', fp);
  EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i,
			     {
			       fprintf (fp, " %d", i);
			       if (i < FIRST_PSEUDO_REGISTER)
				 fprintf (fp, " [%s]",
					  reg_names[i]);
			     });
#endif

  switch (graph_dump_format)
    {
    case vcg:
      fputs ("\"\n\n", fp);
      break;
    case no_graph:
      break;
    }
}

106
static void
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
node_data (fp, tmp_rtx)
     FILE *fp;
     rtx tmp_rtx;
{

  if (PREV_INSN (tmp_rtx) == 0)
    {
      /* This is the first instruction.  Add an edge from the starting
	 block.  */
      switch (graph_dump_format)
	{
	case vcg:
	  fprintf (fp, "\
edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
		   current_function_name,
		   current_function_name, XINT (tmp_rtx, 0));
	  break;
	case no_graph:
	  break;
	}
    }

  switch (graph_dump_format)
    {
    case vcg:
      fprintf (fp, "node: {\n  title: \"%s.%d\"\n  color: %s\n  \
label: \"%s %d\n",
	       current_function_name, XINT (tmp_rtx, 0),
	       GET_CODE (tmp_rtx) == NOTE ? "lightgrey"
	       : GET_CODE (tmp_rtx) == INSN ? "green"
	       : GET_CODE (tmp_rtx) == JUMP_INSN ? "darkgreen"
	       : GET_CODE (tmp_rtx) == CALL_INSN ? "darkgreen"
	       : GET_CODE (tmp_rtx) == CODE_LABEL ?  "\
darkgrey\n  shape: ellipse" : "white",
	       GET_RTX_NAME (GET_CODE (tmp_rtx)), XINT (tmp_rtx, 0));
      break;
    case no_graph:
      break;
    }

  /* Print the RTL.  */
  if (GET_CODE (tmp_rtx) == NOTE)
    {
150 151 152 153
      const char *name = "";
      if (NOTE_LINE_NUMBER (tmp_rtx) < 0)
	name =  GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (tmp_rtx));
      fprintf (fp, " %s", name);
154
    }
155
  else if (INSN_P (tmp_rtx))
156
    print_rtl_single (fp, PATTERN (tmp_rtx));
157
  else
158
    print_rtl_single (fp, tmp_rtx);
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

  switch (graph_dump_format)
    {
    case vcg:
      fputs ("\"\n}\n", fp);
      break;
    case no_graph:
      break;
    }
}

static void
draw_edge (fp, from, to, bb_edge, class)
     FILE *fp;
     int from;
     int to;
     int bb_edge;
     int class;
{
178
  const char * color;
179 180 181
  switch (graph_dump_format)
    {
    case vcg:
182 183 184 185 186 187 188
      color = "";
      if (class == 2)
	color = "color: red ";
      else if (bb_edge)
	color = "color: blue ";
      else if (class == 3)
	color = "color: green ";
189 190 191
      fprintf (fp,
	       "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
	       current_function_name, from,
192
	       current_function_name, to, color);
193 194 195 196 197 198 199 200 201 202
      if (class)
	fprintf (fp, "class: %d ", class);
      fputs ("}\n", fp);
      break;
    case no_graph:
      break;
    }
}

static void
203
end_bb (fp)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
     FILE *fp;
{
  switch (graph_dump_format)
    {
    case vcg:
      fputs ("}\n", fp);
      break;
    case no_graph:
      break;
    }
}

static void
end_fct (fp)
     FILE *fp;
{
  switch (graph_dump_format)
    {
    case vcg:
      fprintf (fp, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
	       current_function_name);
      break;
    case no_graph:
      break;
    }
}

/* Like print_rtl, but also print out live information for the start of each
   basic block.  */
void
print_rtl_graph_with_bb (base, suffix, rtx_first)
     const char *base;
     const char *suffix;
     rtx rtx_first;
{
  register rtx tmp_rtx;
  size_t namelen = strlen (base);
  size_t suffixlen = strlen (suffix);
  size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
  char *buf = (char *) alloca (namelen + suffixlen + extlen);
  FILE *fp;

246 247
  if (basic_block_info == NULL)
    return;
248 249 250 251 252 253 254 255 256 257 258 259 260

  memcpy (buf, base, namelen);
  memcpy (buf + namelen, suffix, suffixlen);
  memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);

  fp = fopen (buf, "a");
  if (fp == NULL)
    return;

  if (rtx_first == 0)
    fprintf (fp, "(nil)\n");
  else
    {
261
      int i;
262 263
      enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
      int max_uid = get_max_uid ();
264 265
      int *start = (int *) xmalloc (max_uid * sizeof (int));
      int *end = (int *) xmalloc (max_uid * sizeof (int));
266
      enum bb_state *in_bb_p = (enum bb_state *)
267
	xmalloc (max_uid * sizeof (enum bb_state));
268
      basic_block bb;
269 270 271 272 273 274 275 276 277 278

      for (i = 0; i < max_uid; ++i)
	{
	  start[i] = end[i] = -1;
	  in_bb_p[i] = NOT_IN_BB;
	}

      for (i = n_basic_blocks - 1; i >= 0; --i)
	{
	  rtx x;
279 280 281 282
	  bb = BASIC_BLOCK (i);
	  start[INSN_UID (bb->head)] = i;
	  end[INSN_UID (bb->end)] = i;
	  for (x = bb->head; x != NULL_RTX; x = NEXT_INSN (x))
283 284 285 286
	    {
	      in_bb_p[INSN_UID (x)]
		= (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
		 ? IN_ONE_BB : IN_MULTIPLE_BB;
287
	      if (x == bb->end)
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		break;
	    }
	}

      /* Tell print-rtl that we want graph output.  */
      dump_for_graph = 1;

      /* Start new function.  */
      start_fct (fp);

      for (tmp_rtx = NEXT_INSN (rtx_first); NULL != tmp_rtx;
	   tmp_rtx = NEXT_INSN (tmp_rtx))
	{
	  int edge_printed = 0;
	  rtx next_insn;

	  if (start[INSN_UID (tmp_rtx)] < 0 && end[INSN_UID (tmp_rtx)] < 0)
	    {
	      if (GET_CODE (tmp_rtx) == BARRIER)
		continue;
	      if (GET_CODE (tmp_rtx) == NOTE
		  && (1 || in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB))
		continue;
	    }

313
	  if ((i = start[INSN_UID (tmp_rtx)]) >= 0)
314 315
	    {
	      /* We start a subgraph for each basic block.  */
316
	      start_bb (fp, i);
317

318
	      if (i == 0)
319 320 321 322
		draw_edge (fp, 0, INSN_UID (tmp_rtx), 1, 0);
	    }

	  /* Print the data for this node.  */
323
	  node_data (fp, tmp_rtx);
324 325
	  next_insn = next_nonnote_insn (tmp_rtx);

326
	  if ((i = end[INSN_UID (tmp_rtx)]) >= 0)
327
	    {
328 329 330
	      edge e;

	      bb = BASIC_BLOCK (i);
331 332

	      /* End of the basic block.  */
333
	      end_bb (fp);
334 335 336

	      /* Now specify the edges to all the successors of this
		 basic block.  */
337
	      for (e = bb->succ; e ; e = e->succ_next)
338
		{
339
		  if (e->dest != EXIT_BLOCK_PTR)
340
		    {
341
		      rtx block_head = e->dest->head;
342 343 344

		      draw_edge (fp, INSN_UID (tmp_rtx),
				 INSN_UID (block_head),
345 346
				 next_insn != block_head,
				 (e->flags & EDGE_ABNORMAL ? 2 : 0));
347

348
		      if (block_head == next_insn)
349 350
			edge_printed = 1;
		    }
351
		  else
352 353
		    {
		      draw_edge (fp, INSN_UID (tmp_rtx), 999999,
354 355
				 next_insn != 0,
				 (e->flags & EDGE_ABNORMAL ? 2 : 0));
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

		      if (next_insn == 0)
			edge_printed = 1;
		    }
		}
	    }

	  if (!edge_printed)
	    {
	      /* Don't print edges to barriers.  */
	      if (next_insn == 0
		  || GET_CODE (next_insn) != BARRIER)
		draw_edge (fp, XINT (tmp_rtx, 0),
			   next_insn ? INSN_UID (next_insn) : 999999, 0, 0);
	      else
		{
372 373
		  /* We draw the remaining edges in class 3.  We have
		     to skip over the barrier since these nodes are
374 375 376 377 378 379 380 381
		     not printed at all.  */
		  do
		    next_insn = NEXT_INSN (next_insn);
		  while (next_insn
			 && (GET_CODE (next_insn) == NOTE
			     || GET_CODE (next_insn) == BARRIER));

		  draw_edge (fp, XINT (tmp_rtx, 0),
382
			     next_insn ? INSN_UID (next_insn) : 999999, 0, 3);
383 384 385 386 387 388 389
		}
	    }
	}

      dump_for_graph = 0;

      end_fct (fp);
390 391 392 393 394

      /* Clean up.  */
      free (start);
      free (end);
      free (in_bb_p);
395 396 397 398 399 400 401
    }

  fclose (fp);
}


/* Similar as clean_dump_file, but this time for graph output files.  */
402

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
void
clean_graph_dump_file (base, suffix)
     const char *base;
     const char *suffix;
{
  size_t namelen = strlen (base);
  size_t suffixlen = strlen (suffix);
  size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
  char *buf = (char *) alloca (namelen + extlen + suffixlen);
  FILE *fp;

  memcpy (buf, base, namelen);
  memcpy (buf + namelen, suffix, suffixlen);
  memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);

  fp = fopen (buf, "w");

  if (fp == NULL)
421
    fatal_io_error ("can't open %s", buf);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

  switch (graph_dump_format)
    {
    case vcg:
      fputs ("graph: {\nport_sharing: no\n", fp);
      break;
    case no_graph:
      abort ();
    }

  fclose (fp);
}


/* Do final work on the graph output file.  */
void
finish_graph_dump_file (base, suffix)
     const char *base;
     const char *suffix;
{
  size_t namelen = strlen (base);
  size_t suffixlen = strlen (suffix);
  size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
  char *buf = (char *) alloca (namelen + suffixlen + extlen);
  FILE *fp;

  memcpy (buf, base, namelen);
  memcpy (buf + namelen, suffix, suffixlen);
  memcpy (buf + namelen + suffixlen, graph_ext[graph_dump_format], extlen);

  fp = fopen (buf, "a");
  if (fp != NULL)
    {
      switch (graph_dump_format)
	{
	case vcg:
	  fputs ("}\n", fp);
	  break;
	case no_graph:
	  abort ();
	}

      fclose (fp);
    }
}