graph.c 9.02 KB
Newer Older
1
/* Output routines for graphical representation.
2
   Copyright (C) 1998-2013 Free Software Foundation, Inc.
3
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4
   Rewritten for DOT output by Steven Bosscher, 2012.
5

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
#include "coretypes.h"
25
#include "diagnostic-core.h" /* for fatal_error */
26
#include "sbitmap.h"
27
#include "basic-block.h"
28
#include "cfgloop.h"
29
#include "graph.h"
30
#include "dumpfile.h"
31
#include "pretty-print.h"
32

33 34 35
/* DOT files with the .dot extension are recognized as document templates
   by a well-known piece of word processing software out of Redmond, WA.
   Therefore some recommend using the .gv extension instead.  Obstinately
36
   ignore that recommendation...  */
37
static const char *const graph_ext = ".dot";
38

39 40 41 42
/* Open a file with MODE for dumping our graph to.
   Return the file pointer.  */
static FILE *
open_graph_file (const char *base, const char *mode)
43
{
44 45 46 47
  size_t namelen = strlen (base);
  size_t extlen = strlen (graph_ext) + 1;
  char *buf = XALLOCAVEC (char, namelen + extlen);
  FILE *fp;
48

49 50
  memcpy (buf, base, namelen);
  memcpy (buf + namelen, graph_ext, extlen);
51

52 53 54
  fp = fopen (buf, mode);
  if (fp == NULL)
    fatal_error ("can%'t open %s: %m", buf);
55

56
  return fp;
57 58
}

59
/* Draw a basic block BB belonging to the function with FUNCDEF_NO
60 61
   as its unique number.  */
static void
62
draw_cfg_node (pretty_printer *pp, int funcdef_no, basic_block bb)
63 64 65
{
  const char *shape;
  const char *fillcolor;
66

67
  if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
68
    {
69 70
      shape = "Mdiamond";
      fillcolor = "white";
71 72 73
    }
  else
    {
74 75 76 77 78
      shape = "record";
      fillcolor =
	BB_PARTITION (bb) == BB_HOT_PARTITION ? "lightpink"
	: BB_PARTITION (bb) == BB_COLD_PARTITION ? "lightblue"
	: "lightgrey";
79 80
    }

81 82 83
  pp_printf (pp,
	     "\tfn_%d_basic_block_%d "
	     "[shape=%s,style=filled,fillcolor=%s,label=\"",
84
	     funcdef_no, bb->index, shape, fillcolor);
85

86
  if (bb->index == ENTRY_BLOCK)
87
    pp_string (pp, "ENTRY");
88
  else if (bb->index == EXIT_BLOCK)
89
    pp_string (pp, "EXIT");
90
  else
91
    {
92
      pp_left_brace (pp);
93
      pp_write_text_to_stream (pp);
94
      dump_bb_for_graph (pp, bb);
95
      pp_right_brace (pp);
96
    }
97

98 99
  pp_string (pp, "\"];\n\n");
  pp_flush (pp);
100 101
}

102
/* Draw all successor edges of a basic block BB belonging to the function
103
   with FUNCDEF_NO as its unique number.  */
104
static void
105
draw_cfg_node_succ_edges (pretty_printer *pp, int funcdef_no, basic_block bb)
106
{
107 108 109
  edge e;
  edge_iterator ei;
  FOR_EACH_EDGE (e, ei, bb->succs)
110
    {
111 112 113
      const char *style = "\"solid,bold\"";
      const char *color = "black";
      int weight = 10;
114

115
      if (e->flags & EDGE_FAKE)
116
	{
117 118 119
	  style = "dotted";
	  color = "green";
	  weight = 0;
120
	}
121
      else if (e->flags & EDGE_DFS_BACK)
122
	{
123 124 125
	  style = "\"dotted,bold\"";
	  color = "blue";
	  weight = 10;
126
	}
127
      else if (e->flags & EDGE_FALLTHRU)
128
	{
129 130
	  color = "blue";
	  weight = 100;
131 132
	}

133 134
      if (e->flags & EDGE_ABNORMAL)
	color = "red";
135

136 137
      pp_printf (pp,
		 "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
138
		 "[style=%s,color=%s,weight=%d,constraint=%s, label=\"[%i%%]\"];\n",
139 140
		 funcdef_no, e->src->index,
		 funcdef_no, e->dest->index,
141
		 style, color, weight,
142 143
		 (e->flags & (EDGE_FAKE | EDGE_DFS_BACK)) ? "false" : "true",
		 e->probability * 100 / REG_BR_PROB_BASE);
144
    }
145
  pp_flush (pp);
146 147
}

148 149 150 151
/* Draw all the basic blocks in the CFG in case loops are not available.
   First compute a topological order of the blocks to get a good ranking of
   the nodes.  Then, if any nodes are not reachable from ENTRY, add them at
   the end.  */
152

153 154
static void
draw_cfg_nodes_no_loops (pretty_printer *pp, struct function *fun)
155
{
156
  int *rpo = XNEWVEC (int, n_basic_blocks_for_function (fun));
157
  int i, n;
158
  sbitmap visited;
159

160 161
  visited = sbitmap_alloc (last_basic_block);
  bitmap_clear (visited);
162

163
  /* FIXME: pre_and_rev_post_order_compute only works if fun == cfun.  */
164 165
  n = pre_and_rev_post_order_compute (NULL, rpo, true);
  for (i = 0; i < n; i++)
166 167 168 169 170 171
    {
      basic_block bb = BASIC_BLOCK (rpo[i]);
      draw_cfg_node (pp, fun->funcdef_no, bb);
      bitmap_set_bit (visited, bb->index);
    }
  free (rpo);
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  if (n != n_basic_blocks_for_function (fun))
    {
      /* Some blocks are unreachable.  We still want to dump them.  */
      basic_block bb;
      FOR_ALL_BB_FN (bb, fun)
	if (! bitmap_bit_p (visited, bb->index))
	  draw_cfg_node (pp, fun->funcdef_no, bb);
    }

  sbitmap_free (visited);
}

/* Draw all the basic blocks in LOOP.  Print the blocks in breath-first
   order to get a good ranking of the nodes.  This function is recursive:
   It first prints inner loops, then the body of LOOP itself.  */
188

189 190 191 192 193 194 195 196
static void
draw_cfg_nodes_for_loop (pretty_printer *pp, int funcdef_no,
			 struct loop *loop)
{
  basic_block *body;
  unsigned int i;
  const char *fillcolors[3] = { "grey88", "grey77", "grey66" };

197 198
  if (loop->header != NULL
      && loop->latch != EXIT_BLOCK_PTR)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    pp_printf (pp,
	       "\tsubgraph cluster_%d_%d {\n"
	       "\tstyle=\"filled\";\n"
	       "\tcolor=\"darkgreen\";\n"
	       "\tfillcolor=\"%s\";\n"
	       "\tlabel=\"loop %d\";\n"
	       "\tlabeljust=l;\n"
	       "\tpenwidth=2;\n",
	       funcdef_no, loop->num,
	       fillcolors[(loop_depth (loop) - 1) % 3],
	       loop->num);

  for (struct loop *inner = loop->inner; inner; inner = inner->next)
    draw_cfg_nodes_for_loop (pp, funcdef_no, inner);

214 215 216
  if (loop->header == NULL)
    return;

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
  if (loop->latch == EXIT_BLOCK_PTR)
    body = get_loop_body (loop);
  else
    body = get_loop_body_in_bfs_order (loop);

  for (i = 0; i < loop->num_nodes; i++)
    {
      basic_block bb = body[i];
      if (bb->loop_father == loop)
	draw_cfg_node (pp, funcdef_no, bb);
    }

  free (body);

  if (loop->latch != EXIT_BLOCK_PTR)
    pp_printf (pp, "\t}\n");
}

/* Draw all the basic blocks in the CFG in case the loop tree is available.
   All loop bodys are printed in clusters.  */

static void
draw_cfg_nodes (pretty_printer *pp, struct function *fun)
{
241 242
  if (loops_for_fn (fun))
    draw_cfg_nodes_for_loop (pp, fun->funcdef_no, get_loop (fun, 0));
243 244 245 246 247 248 249 250 251 252 253 254 255 256
  else
    draw_cfg_nodes_no_loops (pp, fun);
}

/* Draw all edges in the CFG.  Retreating edges are drawin as not
   constraining, this makes the layout of the graph better.
   (??? Calling mark_dfs_back may change the compiler's behavior when
   dumping, but computing back edges here for ourselves is also not
   desirable.)  */

static void
draw_cfg_edges (pretty_printer *pp, struct function *fun)
{
  basic_block bb;
257 258
  mark_dfs_back_edges ();
  FOR_ALL_BB (bb)
259 260 261 262 263 264 265 266 267 268
    draw_cfg_node_succ_edges (pp, fun->funcdef_no, bb);

  /* Add an invisible edge from ENTRY to EXIT, to improve the graph layout.  */
  pp_printf (pp,
	     "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
	     "[style=\"invis\",constraint=true];\n",
	     fun->funcdef_no, ENTRY_BLOCK,
	     fun->funcdef_no, EXIT_BLOCK);
  pp_flush (pp);
}
269

270 271 272 273 274 275 276 277 278 279
/* Print a graphical representation of the CFG of function FUN.
   First print all basic blocks.  Draw all edges at the end to get
   subgraphs right for GraphViz, which requires nodes to be defined
   before edges to cluster nodes properly.  */

void
print_graph_cfg (const char *base, struct function *fun)
{
  const char *funcname = function_name (fun);
  FILE *fp = open_graph_file (base, "a");
280 281 282
  pretty_printer graph_slim_pp;
  graph_slim_pp.buffer->stream = fp;
  pretty_printer *const pp = &graph_slim_pp;
283 284 285 286 287 288 289
  pp_printf (pp, "subgraph \"%s\" {\n"
	         "\tcolor=\"black\";\n"
		 "\tlabel=\"%s\";\n",
		 funcname, funcname);
  draw_cfg_nodes (pp, fun);
  draw_cfg_edges (pp, fun);
  pp_printf (pp, "}\n");
290
  pp_flush (pp);
291 292 293
  fclose (fp);
}

294 295
/* Start the dump of a graph.  */
static void
296
start_graph_dump (FILE *fp, const char *base)
297
{
298 299 300
  pretty_printer graph_slim_pp;
  graph_slim_pp.buffer->stream = fp;
  pretty_printer *const pp = &graph_slim_pp;
301 302 303 304 305 306 307
  pp_string (pp, "digraph \"");
  pp_write_text_to_stream (pp);
  pp_string (pp, base);
  pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/false);
  pp_string (pp, "\" {\n");
  pp_string (pp, "overlap=false;\n");
  pp_flush (pp);
308 309 310 311 312 313 314 315
}

/* End the dump of a graph.  */
static void
end_graph_dump (FILE *fp)
{
  fputs ("}\n", fp);
}
316 317 318

/* Similar as clean_dump_file, but this time for graph output files.  */
void
319
clean_graph_dump_file (const char *base)
320
{
321
  FILE *fp = open_graph_file (base, "w");
322
  start_graph_dump (fp, base);
323 324 325 326 327 328
  fclose (fp);
}


/* Do final work on the graph output file.  */
void
329
finish_graph_dump_file (const char *base)
330
{
331 332 333
  FILE *fp = open_graph_file (base, "a");
  end_graph_dump (fp);
  fclose (fp);
334
}