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

6
This file is part of GCC.
7

8 9
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
10
Software Foundation; either version 3, or (at your option) any later
11
version.
12

13 14 15 16
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.
17

18
You should have received a copy of the GNU General Public License
19 20
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
21

22
#include "config.h"
23
#include "system.h"
24 25
#include "coretypes.h"
#include "tm.h"
26 27 28
#include "rtl.h"
#include "flags.h"
#include "output.h"
29
#include "function.h"
30
#include "hard-reg-set.h"
31
#include "obstack.h"
32
#include "basic-block.h"
33
#include "diagnostic-core.h"
34
#include "graph.h"
35
#include "emit-rtl.h"
36

37
static const char *const graph_ext[] =
38 39 40 41 42
{
  /* no_graph */ "",
  /* vcg */      ".vcg",
};

43 44 45
/* The flag to indicate if output is inside of a building block.  */
static int inbb = 0;

46 47 48 49 50 51
static void start_fct (FILE *);
static void start_bb (FILE *, int);
static void node_data (FILE *, rtx);
static void draw_edge (FILE *, int, int, int, int);
static void end_fct (FILE *);
static void end_bb (FILE *);
52

53 54
/* Output text for new basic block.  */
static void
55
start_fct (FILE *fp)
56 57 58 59 60 61
{
  switch (graph_dump_format)
    {
    case vcg:
      fprintf (fp, "\
graph: { title: \"%s\"\nfolding: 1\nhidden: 2\nnode: { title: \"%s.0\" }\n",
62
	       current_function_name (), current_function_name ());
63 64 65 66 67 68 69
      break;
    case no_graph:
      break;
    }
}

static void
70
start_bb (FILE *fp, int bb)
71
{
72 73 74 75
#if 0
  reg_set_iterator rsi;
#endif

76 77 78 79 80 81
  switch (graph_dump_format)
    {
    case vcg:
      fprintf (fp, "\
graph: {\ntitle: \"%s.BB%d\"\nfolding: 1\ncolor: lightblue\n\
label: \"basic block %d",
82
	       current_function_name (), bb, bb);
83
      inbb = 1; /* Now We are inside of a building block.  */
84 85 86 87 88 89
      break;
    case no_graph:
      break;
    }

#if 0
90
  /* FIXME Should this be printed?  It makes the graph significantly larger.  */
91 92 93

  /* Print the live-at-start register list.  */
  fputc ('\n', fp);
94 95 96 97 98 99
  EXECUTE_IF_SET_IN_REG_SET (basic_block_live_at_start[bb], 0, i, rsi)
    {
      fprintf (fp, " %d", i);
      if (i < FIRST_PSEUDO_REGISTER)
	fprintf (fp, " [%s]", reg_names[i]);
    }
100 101 102 103 104 105 106 107 108 109 110 111
#endif

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

112
static void
113
node_data (FILE *fp, rtx tmp_rtx)
114 115 116 117 118 119 120 121 122 123
{
  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",
124 125
		   current_function_name (),
		   current_function_name (), XINT (tmp_rtx, 0));
126 127 128 129 130 131 132 133 134 135 136
	  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",
137
	       current_function_name (), XINT (tmp_rtx, 0),
138 139 140 141 142
	       NOTE_P (tmp_rtx) ? "lightgrey"
	       : NONJUMP_INSN_P (tmp_rtx) ? "green"
	       : JUMP_P (tmp_rtx) ? "darkgreen"
	       : CALL_P (tmp_rtx) ? "darkgreen"
	       : LABEL_P (tmp_rtx) ?  "\
143 144 145 146 147 148 149 150
darkgrey\n  shape: ellipse" : "white",
	       GET_RTX_NAME (GET_CODE (tmp_rtx)), XINT (tmp_rtx, 0));
      break;
    case no_graph:
      break;
    }

  /* Print the RTL.  */
151
  if (NOTE_P (tmp_rtx))
152
    {
153 154
      const char *name;
      name =  GET_NOTE_INSN_NAME (NOTE_KIND (tmp_rtx));
155
      fprintf (fp, " %s", name);
156
    }
157
  else if (INSN_P (tmp_rtx))
158
    print_rtl_single (fp, PATTERN (tmp_rtx));
159
  else
160
    print_rtl_single (fp, tmp_rtx);
161 162 163 164 165 166 167 168 169 170 171 172

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

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

static void
200
end_bb (FILE *fp)
201 202 203 204
{
  switch (graph_dump_format)
    {
    case vcg:
205 206 207 208 209 210
      /* Check if we are inside of a building block.  */
      if (inbb != 0)
        {
          fputs ("}\n", fp);
          inbb = 0; /* Now we are outside of a building block.  */
        }
211 212 213 214 215 216 217
      break;
    case no_graph:
      break;
    }
}

static void
218
end_fct (FILE *fp)
219 220 221 222 223
{
  switch (graph_dump_format)
    {
    case vcg:
      fprintf (fp, "node: { title: \"%s.999999\" label: \"END\" }\n}\n",
224
	       current_function_name ());
225 226 227 228 229 230 231 232 233
      break;
    case no_graph:
      break;
    }
}

/* Like print_rtl, but also print out live information for the start of each
   basic block.  */
void
234
print_rtl_graph_with_bb (const char *base, rtx rtx_first)
235
{
236
  rtx tmp_rtx;
237 238
  size_t namelen = strlen (base);
  size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
239
  char *buf = XALLOCAVEC (char, namelen + extlen);
240 241
  FILE *fp;

242 243
  if (basic_block_info == NULL)
    return;
244 245

  memcpy (buf, base, namelen);
246
  memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
247 248 249 250 251 252 253 254 255 256 257

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

  if (rtx_first == 0)
    fprintf (fp, "(nil)\n");
  else
    {
      enum bb_state { NOT_IN_BB, IN_ONE_BB, IN_MULTIPLE_BB };
      int max_uid = get_max_uid ();
258 259 260
      int *start = XNEWVEC (int, max_uid);
      int *end = XNEWVEC (int, max_uid);
      enum bb_state *in_bb_p = XNEWVEC (enum bb_state, max_uid);
261
      basic_block bb;
262
      int i;
263

264
      for (i = 0; i < max_uid; ++i)
265
	{
266 267
	  start[i] = end[i] = -1;
	  in_bb_p[i] = NOT_IN_BB;
268 269
	}

270
      FOR_EACH_BB_REVERSE (bb)
271 272
	{
	  rtx x;
273 274 275
	  start[INSN_UID (BB_HEAD (bb))] = bb->index;
	  end[INSN_UID (BB_END (bb))] = bb->index;
	  for (x = BB_HEAD (bb); x != NULL_RTX; x = NEXT_INSN (x))
276 277 278 279
	    {
	      in_bb_p[INSN_UID (x)]
		= (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
		 ? IN_ONE_BB : IN_MULTIPLE_BB;
280
	      if (x == BB_END (bb))
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
		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)
	    {
299
	      if (BARRIER_P (tmp_rtx))
300
		continue;
301
	      if (NOTE_P (tmp_rtx)
302 303 304 305
		  && (1 || in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB))
		continue;
	    }

306
	  if ((i = start[INSN_UID (tmp_rtx)]) >= 0)
307 308
	    {
	      /* We start a subgraph for each basic block.  */
309
	      start_bb (fp, i);
310

311
	      if (i == 0)
312 313 314 315
		draw_edge (fp, 0, INSN_UID (tmp_rtx), 1, 0);
	    }

	  /* Print the data for this node.  */
316
	  node_data (fp, tmp_rtx);
317 318
	  next_insn = next_nonnote_insn (tmp_rtx);

319
	  if ((i = end[INSN_UID (tmp_rtx)]) >= 0)
320
	    {
321
	      edge e;
322
	      edge_iterator ei;
323

324
	      bb = BASIC_BLOCK (i);
325 326

	      /* End of the basic block.  */
327
	      end_bb (fp);
328 329 330

	      /* Now specify the edges to all the successors of this
		 basic block.  */
331
	      FOR_EACH_EDGE (e, ei, bb->succs)
332
		{
333
		  if (e->dest != EXIT_BLOCK_PTR)
334
		    {
335
		      rtx block_head = BB_HEAD (e->dest);
336 337 338

		      draw_edge (fp, INSN_UID (tmp_rtx),
				 INSN_UID (block_head),
339 340
				 next_insn != block_head,
				 (e->flags & EDGE_ABNORMAL ? 2 : 0));
341

342
		      if (block_head == next_insn)
343 344
			edge_printed = 1;
		    }
345
		  else
346 347
		    {
		      draw_edge (fp, INSN_UID (tmp_rtx), 999999,
348 349
				 next_insn != 0,
				 (e->flags & EDGE_ABNORMAL ? 2 : 0));
350 351 352 353 354 355 356 357 358 359 360

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

	  if (!edge_printed)
	    {
	      /* Don't print edges to barriers.  */
	      if (next_insn == 0
361
		  || !BARRIER_P (next_insn))
362 363 364 365
		draw_edge (fp, XINT (tmp_rtx, 0),
			   next_insn ? INSN_UID (next_insn) : 999999, 0, 0);
	      else
		{
366 367
		  /* We draw the remaining edges in class 3.  We have
		     to skip over the barrier since these nodes are
368 369 370 371
		     not printed at all.  */
		  do
		    next_insn = NEXT_INSN (next_insn);
		  while (next_insn
372 373
			 && (NOTE_P (next_insn)
			     || BARRIER_P (next_insn)));
374 375

		  draw_edge (fp, XINT (tmp_rtx, 0),
376
			     next_insn ? INSN_UID (next_insn) : 999999, 0, 3);
377 378 379 380 381 382 383
		}
	    }
	}

      dump_for_graph = 0;

      end_fct (fp);
384 385 386 387 388

      /* Clean up.  */
      free (start);
      free (end);
      free (in_bb_p);
389 390 391 392 393 394 395
    }

  fclose (fp);
}


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

397
void
398
clean_graph_dump_file (const char *base)
399 400 401
{
  size_t namelen = strlen (base);
  size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
402
  char *buf = XALLOCAVEC (char, namelen + extlen);
403 404 405
  FILE *fp;

  memcpy (buf, base, namelen);
406
  memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
407 408 409 410

  fp = fopen (buf, "w");

  if (fp == NULL)
411
    fatal_error ("can%'t open %s: %m", buf);
412

413 414
  gcc_assert (graph_dump_format == vcg);
  fputs ("graph: {\nport_sharing: no\n", fp);
415 416 417 418 419 420 421

  fclose (fp);
}


/* Do final work on the graph output file.  */
void
422
finish_graph_dump_file (const char *base)
423 424 425
{
  size_t namelen = strlen (base);
  size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
426
  char *buf = XALLOCAVEC (char, namelen + extlen);
427 428 429
  FILE *fp;

  memcpy (buf, base, namelen);
430
  memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
431 432 433 434

  fp = fopen (buf, "a");
  if (fp != NULL)
    {
435 436
      gcc_assert (graph_dump_format == vcg);
      fputs ("}\n", fp);
437 438 439
      fclose (fp);
    }
}