c2pdg: skip kill statements
[ppn.git] / adg2dot.cc
blobc06bf992d6a9b541e9b2826553e96ea4b998121f
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 */
5 #include <assert.h>
6 #include "adg_parse.h"
7 #include "adg2dot_options.h"
9 /* Add a connection from the element called "id" to the input port
10 * "port" provided it reads from the variable "name".
12 void add_connections_in(FILE *out, __isl_keep isl_id *id, const char *name,
13 adg_port *port)
15 const char *name2;
17 for (int j = 0; j < port->domain->filters.size(); ++j) {
18 name2 = isl_pw_multi_aff_get_tuple_name(
19 port->domain->filters[j]->access, isl_dim_out);
20 if (name != name2)
21 continue;
22 fprintf(out, "\t%s -> %s;\n",
23 isl_id_get_name(id), isl_id_get_name(port->name));
24 break;
28 /* Add a connection from the element called "id" to the output port
29 * "port" provided it reads from the variable "name".
31 void add_connections_out(FILE *out, __isl_keep isl_id *id, const char *name,
32 adg_port *port)
34 const char *name2;
36 for (int i = 0; i < port->vars.size(); ++i) {
37 name2 = isl_pw_multi_aff_get_tuple_name(
38 port->vars[i]->access, isl_dim_out);
39 if (name != name2)
40 continue;
41 fprintf(out, "\t%s -> %s;\n",
42 isl_id_get_name(id), isl_id_get_name(port->name));
43 break;
47 /* Add a connection from the element called "id" to "gate" provided
48 * it reads from the variable "name".
50 void add_connections(FILE *out, __isl_keep isl_id *id, const char *name,
51 adg_gate *gate)
53 const char *name2;
55 for (int j = 0; j < gate->domain->filters.size(); ++j) {
56 name2 = isl_pw_multi_aff_get_tuple_name(
57 gate->domain->filters[j]->access, isl_dim_out);
58 if (name != name2)
59 continue;
60 fprintf(out, "\t%s -> %s;\n",
61 isl_id_get_name(id), isl_id_get_name(gate->name));
62 break;
65 name2 = isl_pw_multi_aff_get_tuple_name(gate->in->access, isl_dim_out);
66 if (name != name2)
67 return;
68 fprintf(out, "\t%s -> %s;\n",
69 isl_id_get_name(id), isl_id_get_name(gate->name));
72 /* Add connections from the element called "id" to "fn" provided
73 * it reads from the variable "name".
75 void add_connections(FILE *out, __isl_keep isl_id *id, const char *name,
76 __isl_keep isl_id *node_id, adg_function *fn)
78 const char *name2;
80 for (int i = 0; i < fn->in.size(); ++i) {
81 name2 = isl_pw_multi_aff_get_tuple_name(fn->in[i]->var->access,
82 isl_dim_out);
83 if (name != name2)
84 continue;
85 fprintf(out, "\t%s -> %s;\n",
86 isl_id_get_name(id), isl_id_get_name(node_id));
87 return;
89 for (int i = 0; i < fn->domain->filters.size(); ++i) {
90 name2 = isl_pw_multi_aff_get_tuple_name(
91 fn->domain->filters[i]->access, isl_dim_out);
92 if (name != name2)
93 continue;
94 fprintf(out, "\t%s -> %s;\n",
95 isl_id_get_name(id), isl_id_get_name(node_id));
96 return;
100 /* Add connections from the element called "id" to elements of "node" that
101 * refer to the variable "name".
103 void add_connections(FILE *out, __isl_keep isl_id *id, const char *name,
104 adg_node *node)
106 for (int k = 0; k < node->input_ports.size(); ++k)
107 add_connections_in(out, id, name, node->input_ports[k]);
108 for (int j = 0; j < node->gates.size(); ++j)
109 add_connections(out, id, name, node->gates[j]);
110 add_connections(out, id, name, node->name, node->function);
111 for (int k = 0; k < node->output_ports.size(); ++k)
112 add_connections_out(out, id, name, node->output_ports[k]);
115 /* Add connections from "port" to elements of "node" that refer to the
116 * same variable.
118 void add_connections(FILE *out, adg_port *port, adg_node *node)
120 for (int i = 0; i < port->vars.size(); ++i) {
121 const char *name;
122 name = isl_pw_multi_aff_get_tuple_name(
123 port->vars[i]->access, isl_dim_out);
124 add_connections(out, port->name, name, node);
128 /* Add connections from "fn" to elements of "node" that refer to the
129 * same variable.
131 void add_connections(FILE *out, adg_function *fn, adg_node *node)
133 for (int i = 0; i < fn->out.size(); ++i) {
134 const char *name;
135 name = isl_pw_multi_aff_get_tuple_name(
136 fn->out[i]->var->access, isl_dim_out);
137 add_connections(out, node->name, name, node);
139 for (int i = 0; i < fn->ctrl.size(); ++i) {
140 const char *name;
141 name = isl_id_get_name(fn->ctrl[i]->name);
142 add_connections(out, node->name, name, node);
146 void print(FILE *out, adg_node *node)
148 fprintf(out, "subgraph cluster_%s {\n", isl_id_get_name(node->name));
149 if (node->function)
150 fprintf(out, "\t%s [label = \"%s\"];\n",
151 isl_id_get_name(node->name),
152 isl_id_get_name(node->function->name));
153 else
154 fprintf(out, "%s;\n", isl_id_get_name(node->name));
155 for (int j = 0; j < node->input_ports.size(); ++j) {
156 adg_port *port = node->input_ports[j];
157 fprintf(out, "\t%s [label = \"%s\"];\n",
158 isl_id_get_name(port->name),
159 isl_pw_multi_aff_get_tuple_name(
160 port->vars[0]->access, isl_dim_out));
161 add_connections(out, port, node);
163 for (int j = 0; j < node->gates.size(); ++j) {
164 adg_gate *gate = node->gates[j];
165 fprintf(out, "\t%s [label = \"%s\"];\n",
166 isl_id_get_name(gate->name),
167 isl_pw_multi_aff_get_tuple_name(gate->out->access,
168 isl_dim_out));
169 fprintf(out, "\t%s -> %s;\n",
170 isl_id_get_name(gate->name),
171 isl_id_get_name(node->name));
173 for (int j = 0; j < node->output_ports.size(); ++j) {
174 adg_port *port = node->output_ports[j];
175 fprintf(out, "\t%s [label = \"%s\"];\n",
176 isl_id_get_name(port->name),
177 isl_pw_multi_aff_get_tuple_name(
178 port->vars[0]->access, isl_dim_out));
180 add_connections(out, node->function, node);
181 fprintf(out, "}\n");
184 void print(adg *graph, FILE *out)
186 fprintf(out, "digraph dummy {\n");
187 for (int i = 0; i < graph->nodes.size(); ++i)
188 print(out, graph->nodes[i]);
189 for (int i = 0; i < graph->edges.size(); ++i) {
190 adg_edge *edge = graph->edges[i];
191 fprintf(out, "%s -> %s [label = \"%s\"];\n",
192 isl_id_get_name(edge->from_port_name),
193 isl_id_get_name(edge->to_port_name),
194 isl_id_get_name(edge->name));
196 fprintf(out, "}\n");
199 static void set_output_name(isl_ctx *ctx, struct options *options)
201 int len;
202 const char *suffix = ".dot";
204 if (!options->input || !strcmp(options->input, "-"))
205 return;
206 if (options->output)
207 return;
209 len = strlen(options->input);
210 if (len > 5 && !strcmp(options->input + len - 5, ".yaml"))
211 len -= 5;
212 options->output = isl_alloc_array(ctx, char, len + strlen(suffix) + 1);
213 assert(options->output);
214 strncpy(options->output, options->input, len);
215 strcpy(options->output + len, suffix);
218 int main(int argc, char *argv[])
220 isl_ctx *ctx;
221 struct options *options = options_new_with_defaults();
222 adg *graph;
223 FILE *in = stdin, *out = stdout;
225 ctx = isl_ctx_alloc();
226 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
228 set_output_name(ctx, options);
230 if (options->input && strcmp(options->input, "-")) {
231 in = fopen(options->input, "r");
232 assert(in);
234 if (options->output && strcmp(options->output, "-")) {
235 out = fopen(options->output, "w");
236 assert(out);
239 graph = adg_parse(ctx, in);
240 assert(graph);
242 print(graph, out);
244 delete graph;
246 isl_ctx_free(ctx);
248 return 0;