introduce bedump
[libfirm] / ir / be / bedump.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Code for dumping backend datastructures (i.e. interference graphs)
23  * @author      Matthias Braun
24  */
25 #include "config.h"
26
27 #include "bedump.h"
28
29 #include "irdump_t.h"
30 #include "beifg.h"
31 #include "becopyopt_t.h"
32
33 static void dump_ifg_nodes(FILE *F, const be_ifg_t *ifg)
34 {
35         nodes_iter_t ifg_iter;
36         ir_node     *node;
37         be_ifg_foreach_node(ifg, &ifg_iter, node) {
38                 dump_node(F, node);
39         }
40 }
41
42 static void dump_ifg_edges(FILE *F, const be_ifg_t *ifg)
43 {
44         nodes_iter_t ifg_iter;
45         ir_node     *node;
46
47         be_ifg_foreach_node(ifg, &ifg_iter, node) {
48                 neighbours_iter_t neigh_iter;
49                 ir_node          *neighbour;
50
51                 be_ifg_foreach_neighbour(ifg, &neigh_iter, node, neighbour) {
52                         /* interference is bidirectional, but it's enough to dump 1
53                          * direction */
54                         if (get_irn_node_nr(node) >= get_irn_node_nr(neighbour))
55                                 continue;
56
57                         fprintf(F, "edge: {sourcename: \"");
58                         PRINT_NODEID(node);
59                         fprintf(F, "\" targetname: \"");
60                         PRINT_NODEID(neighbour);
61                         fprintf(F, "\" arrowstyle:none class:1}\n");
62                 }
63         }
64 }
65
66 void be_dump_ifg(FILE *F, ir_graph *irg, const be_ifg_t *ifg)
67 {
68         ir_fprintf(F,
69                 "graph: { title: \"interference graph of %+F\"\n"
70                 "layoutalgorithm: mindepth //$ \"circular\"\n"
71                 "classname 1: \"interference\"\n"
72                 , irg);
73         dump_vcg_infonames(F);
74         dump_vcg_header_colors(F);
75
76         dump_ifg_nodes(F, ifg);
77         dump_ifg_edges(F, ifg);
78
79         fprintf(F, "}\n");
80 }
81
82 static void dump_affinity_edges(FILE *F, const copy_opt_t *co,
83                                 bool dump_costs, bool dump_colors)
84 {
85         affinity_node_t *a;
86         co_gs_foreach_aff_node(co, a) {
87                 neighb_t *n;
88
89                 co_gs_foreach_neighb(a, n) {
90                         /* edges are bidirection, dumping one direction is enough */
91                         if (get_irn_node_nr(a->irn) >= get_irn_node_nr(n->irn))
92                                 continue;
93
94                         fprintf(F, "edge: {sourcename: \"");
95                         PRINT_NODEID(a->irn);
96                         fprintf(F, "\" targetname: \"");
97                         PRINT_NODEID(n->irn);
98                         fprintf(F, "\" arrowstyle:none");
99
100                         if (dump_costs)
101                                 fprintf(F, " label:\"%d\"", n->costs);
102                         if (dump_colors) {
103                                 const arch_register_t *ar = arch_get_irn_register(a->irn);
104                                 const arch_register_t *nr = arch_get_irn_register(n->irn);
105                                 const char *color = nr == ar ? "blue" : "red";
106                                 fprintf(F, " color:%s", color);
107                         }
108                         fprintf(F, " linestyle:dashed class:2");
109                         fprintf(F, "}\n");
110                 }
111         }
112 }
113
114 void be_dump_ifg_co(FILE *F, const copy_opt_t *co, bool dump_costs,
115                     bool dump_colors)
116 {
117         ir_graph *irg = co->irg;
118         be_ifg_t *ifg = co->cenv->ifg;
119
120         ir_fprintf(F,
121                 "graph: { title: \"interference graph of %+F\"\n"
122                 "layoutalgorithm: mindepth //$ \"circular\"\n"
123                 "classname 1: \"interference\"\n"
124                 "classname 2: \"affinity\"\n"
125                 , irg);
126         dump_vcg_infonames(F);
127         dump_vcg_header_colors(F);
128
129         dump_ifg_nodes(F, ifg);
130         dump_ifg_edges(F, ifg);
131         dump_affinity_edges(F, co, dump_costs, dump_colors);
132
133         fprintf(F, "}\n");
134 }