summaryrefslogtreecommitdiff
path: root/gcc/graph.c
diff options
context:
space:
mode:
Diffstat (limited to 'gcc/graph.c')
-rw-r--r--gcc/graph.c545
1 files changed, 191 insertions, 354 deletions
diff --git a/gcc/graph.c b/gcc/graph.c
index 847aac2dc1a..bb1bb7b6d93 100644
--- a/gcc/graph.c
+++ b/gcc/graph.c
@@ -1,7 +1,8 @@
/* Output routines for graphical representation.
- Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2007, 2008, 2010
+ Copyright (C) 1998-2012
Free Software Foundation, Inc.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
+ Rewritten for DOT output by Steven Bosscher, 2012.
This file is part of GCC.
@@ -22,396 +23,244 @@ along with GCC; see the file COPYING3. If not see
#include "config.h"
#include "system.h"
#include "coretypes.h"
-#include "tm.h"
-#include "rtl.h"
-#include "flags.h"
-#include "function.h"
-#include "hard-reg-set.h"
-#include "obstack.h"
+#include "diagnostic-core.h" /* for fatal_error */
+#include "sbitmap.h"
#include "basic-block.h"
-#include "diagnostic-core.h"
+#include "rtl.h"
+#include "tree.h"
#include "graph.h"
-#include "emit-rtl.h"
-
-static const char *const graph_ext[] =
-{
- /* no_graph */ "",
- /* vcg */ ".vcg",
-};
-/* The flag to indicate if output is inside of a building block. */
-static int inbb = 0;
+/* 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
+ ignore that recommendatition... */
+static const char *const graph_ext = ".dot";
-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 *);
-
-/* Output text for new basic block. */
-static void
-start_fct (FILE *fp)
+/* 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)
{
- 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 (FILE *fp, int bb)
-{
-#if 0
- reg_set_iterator rsi;
-#endif
-
- 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);
- inbb = 1; /* Now We are inside of a building block. */
- break;
- case no_graph:
- break;
- }
+ size_t namelen = strlen (base);
+ size_t extlen = strlen (graph_ext) + 1;
+ char *buf = XALLOCAVEC (char, namelen + extlen);
+ FILE *fp;
-#if 0
- /* FIXME Should this be printed? It makes the graph significantly larger. */
+ memcpy (buf, base, namelen);
+ memcpy (buf + namelen, graph_ext, extlen);
- /* Print the live-at-start register list. */
- fputc ('\n', fp);
- 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]);
- }
-#endif
+ fp = fopen (buf, mode);
+ if (fp == NULL)
+ fatal_error ("can%'t open %s: %m", buf);
- switch (graph_dump_format)
- {
- case vcg:
- fputs ("\"\n\n", fp);
- break;
- case no_graph:
- break;
- }
+ return fp;
}
-static void
-node_data (FILE *fp, rtx tmp_rtx)
+/* Print the output from print_insn or print_pattern with GraphViz-special
+ characters escaped as necessary. */
+void
+print_escaped_line (FILE *fp, const char *buf)
{
- if (PREV_INSN (tmp_rtx) == 0)
+ const char *p = buf;
+
+ while (*p)
{
- /* This is the first instruction. Add an edge from the starting
- block. */
- switch (graph_dump_format)
+ switch (*p)
{
- case vcg:
- fprintf (fp, "\
-edge: { sourcename: \"%s.0\" targetname: \"%s.%d\" }\n",
- current_function_name (),
- current_function_name (), XINT (tmp_rtx, 0));
+ case '\n':
+ /* Print newlines as a left-aligned newline. */
+ fputs ("\\l\\\n", fp);
break;
- case no_graph:
+
+ case '{':
+ case '}':
+ case '<':
+ case '>':
+ case '|':
+ case '"':
+ case ' ':
+ /* These characters have to be escaped to work with record-shape nodes. */
+ fputc ('\\', fp);
+ /* fall through */
+ default:
+ fputc (*p, fp);
break;
}
+ p++;
}
+ fputs ("\\l\\\n", fp);
+}
- 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),
- 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) ? "\
-darkgrey\n shape: ellipse" : "white",
- GET_RTX_NAME (GET_CODE (tmp_rtx)), XINT (tmp_rtx, 0));
- break;
- case no_graph:
- break;
- }
+/* Draw a basic block BB belonging to the function with FNDECL_UID
+ as its unique number. */
+static void
+draw_cfg_node (FILE *fp, int fndecl_uid, basic_block bb)
+{
+ rtx insn;
+ bool first = true;
+ const char *shape;
+ const char *fillcolor;
- /* Print the RTL. */
- if (NOTE_P (tmp_rtx))
+ if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
{
- const char *name;
- name = GET_NOTE_INSN_NAME (NOTE_KIND (tmp_rtx));
- fprintf (fp, " %s", name);
+ shape = "Mdiamond";
+ fillcolor = "white";
}
- else if (INSN_P (tmp_rtx))
- print_rtl_single (fp, PATTERN (tmp_rtx));
else
- print_rtl_single (fp, tmp_rtx);
-
- switch (graph_dump_format)
{
- case vcg:
- fputs ("\"\n}\n", fp);
- break;
- case no_graph:
- break;
+ shape = "record";
+ fillcolor =
+ BB_PARTITION (bb) == BB_HOT_PARTITION ? "lightpink"
+ : BB_PARTITION (bb) == BB_COLD_PARTITION ? "lightblue"
+ : "lightgrey";
}
-}
-static void
-draw_edge (FILE *fp, int from, int to, int bb_edge, int color_class)
-{
- const char * color;
- switch (graph_dump_format)
- {
- case vcg:
- color = "";
- if (color_class == 2)
- color = "color: red ";
- else if (bb_edge)
- color = "color: blue ";
- else if (color_class == 3)
- color = "color: green ";
- fprintf (fp,
- "edge: { sourcename: \"%s.%d\" targetname: \"%s.%d\" %s",
- current_function_name (), from,
- current_function_name (), to, color);
- if (color_class)
- fprintf (fp, "class: %d ", color_class);
- fputs ("}\n", fp);
- break;
- case no_graph:
- break;
- }
-}
+ fprintf (fp,
+ "\tfn_%d_basic_block_%d [shape=%s,style=filled,fillcolor=%s,label=\"",
+ fndecl_uid, bb->index, shape, fillcolor);
-static void
-end_bb (FILE *fp)
-{
- switch (graph_dump_format)
+ if (bb->index == ENTRY_BLOCK)
+ fputs ("ENTRY", fp);
+ else if (bb->index == EXIT_BLOCK)
+ fputs ("EXIT", fp);
+ else
{
- case vcg:
- /* 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. */
- }
- break;
- case no_graph:
- break;
+ fputc ('{', fp);
+ /* TODO: inter-bb stuff. */
+ FOR_BB_INSNS (bb, insn)
+ {
+ char buf[2048];
+
+ if (! first)
+ fputc ('|', fp);
+
+ print_insn (buf, insn, 1);
+ print_escaped_line (fp, buf);
+ if (INSN_P (insn) && REG_NOTES (insn))
+ for (rtx note = REG_NOTES (insn); note; note = XEXP (note, 1))
+ {
+ fprintf (fp, " %s: ",
+ GET_REG_NOTE_NAME (REG_NOTE_KIND (note)));
+ print_pattern (buf, XEXP (note, 0), 1);
+ print_escaped_line (fp, buf);
+ }
+
+ first = false;
+ }
+ fputc ('}', fp);
}
+
+ fputs ("\"];\n\n", fp);
}
+/* Draw all successor edges of a basic block BB belonging to the function
+ with FNDECL_UID as its unique number. */
static void
-end_fct (FILE *fp)
+draw_cfg_node_succ_edges (FILE *fp, int fndecl_uid, basic_block bb)
{
- switch (graph_dump_format)
+ edge e;
+ edge_iterator ei;
+ FOR_EACH_EDGE (e, ei, bb->succs)
{
- 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 (const char *base, rtx rtx_first)
-{
- rtx tmp_rtx;
- size_t namelen = strlen (base);
- size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
- char *buf = XALLOCAVEC (char, namelen + extlen);
- FILE *fp;
-
- if (!basic_block_info)
- return;
+ const char *style = "\"solid,bold\"";
+ const char *color = "black";
+ int weight = 10;
- memcpy (buf, base, namelen);
- memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
-
- 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 ();
- int *start = XNEWVEC (int, max_uid);
- int *end = XNEWVEC (int, max_uid);
- enum bb_state *in_bb_p = XNEWVEC (enum bb_state, max_uid);
- basic_block bb;
- int i;
-
- for (i = 0; i < max_uid; ++i)
+ if (e->flags & EDGE_FAKE)
{
- start[i] = end[i] = -1;
- in_bb_p[i] = NOT_IN_BB;
+ style = "dotted";
+ color = "green";
+ weight = 0;
}
-
- FOR_EACH_BB_REVERSE (bb)
+ else if (e->flags & EDGE_DFS_BACK)
{
- rtx x;
- 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))
- {
- in_bb_p[INSN_UID (x)]
- = (in_bb_p[INSN_UID (x)] == NOT_IN_BB)
- ? IN_ONE_BB : IN_MULTIPLE_BB;
- if (x == BB_END (bb))
- break;
- }
+ style = "\"dotted,bold\"";
+ color = "blue";
+ weight = 10;
}
-
- /* 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))
+ else if (e->flags & EDGE_FALLTHRU)
{
- int edge_printed = 0;
- rtx next_insn;
-
- if (start[INSN_UID (tmp_rtx)] < 0 && end[INSN_UID (tmp_rtx)] < 0)
- {
- if (BARRIER_P (tmp_rtx))
- continue;
- if (NOTE_P (tmp_rtx)
- && (1 || in_bb_p[INSN_UID (tmp_rtx)] == NOT_IN_BB))
- continue;
- }
-
- if ((i = start[INSN_UID (tmp_rtx)]) >= 0)
- {
- /* We start a subgraph for each basic block. */
- start_bb (fp, i);
-
- if (i == 0)
- draw_edge (fp, 0, INSN_UID (tmp_rtx), 1, 0);
- }
-
- /* Print the data for this node. */
- node_data (fp, tmp_rtx);
- next_insn = next_nonnote_insn (tmp_rtx);
-
- if ((i = end[INSN_UID (tmp_rtx)]) >= 0)
- {
- edge e;
- edge_iterator ei;
-
- bb = BASIC_BLOCK (i);
-
- /* End of the basic block. */
- end_bb (fp);
-
- /* Now specify the edges to all the successors of this
- basic block. */
- FOR_EACH_EDGE (e, ei, bb->succs)
- {
- if (e->dest != EXIT_BLOCK_PTR)
- {
- rtx block_head = BB_HEAD (e->dest);
-
- draw_edge (fp, INSN_UID (tmp_rtx),
- INSN_UID (block_head),
- next_insn != block_head,
- (e->flags & EDGE_ABNORMAL ? 2 : 0));
-
- if (block_head == next_insn)
- edge_printed = 1;
- }
- else
- {
- draw_edge (fp, INSN_UID (tmp_rtx), 999999,
- next_insn != 0,
- (e->flags & EDGE_ABNORMAL ? 2 : 0));
-
- if (next_insn == 0)
- edge_printed = 1;
- }
- }
- }
-
- if (!edge_printed)
- {
- /* Don't print edges to barriers. */
- if (next_insn == 0
- || !BARRIER_P (next_insn))
- draw_edge (fp, XINT (tmp_rtx, 0),
- next_insn ? INSN_UID (next_insn) : 999999, 0, 0);
- else
- {
- /* We draw the remaining edges in class 3. We have
- to skip over the barrier since these nodes are
- not printed at all. */
- do
- next_insn = NEXT_INSN (next_insn);
- while (next_insn
- && (NOTE_P (next_insn)
- || BARRIER_P (next_insn)));
-
- draw_edge (fp, XINT (tmp_rtx, 0),
- next_insn ? INSN_UID (next_insn) : 999999, 0, 3);
- }
- }
+ color = "blue";
+ weight = 100;
}
- dump_for_graph = 0;
+ if (e->flags & EDGE_ABNORMAL)
+ color = "red";
- end_fct (fp);
-
- /* Clean up. */
- free (start);
- free (end);
- free (in_bb_p);
+ fprintf (fp,
+ "\tfn_%d_basic_block_%d:s -> fn_%d_basic_block_%d:n "
+ "[style=%s,color=%s,weight=%d,constraint=%s];\n",
+ fndecl_uid, e->src->index,
+ fndecl_uid, e->dest->index,
+ style, color, weight,
+ (e->flags & (EDGE_FAKE | EDGE_DFS_BACK)) ? "false" : "true");
}
+}
+
+/* Print a graphical representation of the CFG of function FUN.
+ Currently only supports RTL in cfgrtl or cfglayout mode, GIMPLE is TODO. */
+void
+print_rtl_graph_with_bb (const char *base, tree fndecl)
+{
+ const char *funcname = fndecl_name (fndecl);
+ int fndecl_uid = DECL_UID (fndecl);
+ FILE *fp = open_graph_file (base, "a");
+ int *rpo = XNEWVEC (int, n_basic_blocks);
+ basic_block bb;
+ int i, n;
+
+ fprintf (fp,
+ "subgraph \"%s\" {\n"
+ "\tcolor=\"black\";\n"
+ "\tlabel=\"%s\";\n",
+ funcname, funcname);
+
+ /* First print all basic blocks.
+ Visit the blocks in reverse post order to get a good ranking
+ of the nodes. */
+ n = pre_and_rev_post_order_compute (NULL, rpo, true);
+ for (i = 0; i < n; i++)
+ draw_cfg_node (fp, fndecl_uid, BASIC_BLOCK (rpo[i]));
+
+ /* Draw all edges at the end to get subgraphs right for GraphViz,
+ which requires nodes to be defined before edges to cluster
+ nodes properly.
+
+ Draw retreating edges 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.) */
+ mark_dfs_back_edges ();
+ FOR_ALL_BB (bb)
+ draw_cfg_node_succ_edges (fp, fndecl_uid, bb);
+
+ fputs ("\t}\n", fp);
fclose (fp);
}
+/* Start the dump of a graph. */
+static void
+start_graph_dump (FILE *fp)
+{
+ fputs ("digraph \"\" {\n"
+ "overlap=false;\n",
+ fp);
+}
+
+/* End the dump of a graph. */
+static void
+end_graph_dump (FILE *fp)
+{
+ fputs ("}\n", fp);
+}
/* Similar as clean_dump_file, but this time for graph output files. */
-
void
clean_graph_dump_file (const char *base)
{
- size_t namelen = strlen (base);
- size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
- char *buf = XALLOCAVEC (char, namelen + extlen);
- FILE *fp;
-
- memcpy (buf, base, namelen);
- memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
-
- fp = fopen (buf, "w");
-
- if (fp == NULL)
- fatal_error ("can%'t open %s: %m", buf);
-
- gcc_assert (graph_dump_format == vcg);
- fputs ("graph: {\nport_sharing: no\n", fp);
-
+ FILE *fp = open_graph_file (base, "w");
+ start_graph_dump (fp);
fclose (fp);
}
@@ -420,19 +269,7 @@ clean_graph_dump_file (const char *base)
void
finish_graph_dump_file (const char *base)
{
- size_t namelen = strlen (base);
- size_t extlen = strlen (graph_ext[graph_dump_format]) + 1;
- char *buf = XALLOCAVEC (char, namelen + extlen);
- FILE *fp;
-
- memcpy (buf, base, namelen);
- memcpy (buf + namelen, graph_ext[graph_dump_format], extlen);
-
- fp = fopen (buf, "a");
- if (fp != NULL)
- {
- gcc_assert (graph_dump_format == vcg);
- fputs ("}\n", fp);
- fclose (fp);
- }
+ FILE *fp = open_graph_file (base, "a");
+ end_graph_dump (fp);
+ fclose (fp);
}