beuses: Remove stale start loop test.
[libfirm] / ir / be / bechordal_draw.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       Paint chordal graphs.
23  * @author      Sebastian Hack
24  * @date        12.05.2005
25  */
26 #include "config.h"
27
28 #include <limits.h>
29
30 #include "pmap.h"
31 #include "pset.h"
32
33 #include "irgwalk.h"
34 #include "irprintf.h"
35 #include "iredges_t.h"
36 #include "util.h"
37
38 #include "bearch.h"
39 #include "belive_t.h"
40 #include "bechordal_t.h"
41 #include "besched.h"
42 #include "bechordal_draw.h"
43 #include "beirg.h"
44
45 typedef struct {
46         be_chordal_env_t *env;
47         plotter_t inh;
48         const color_t *color;
49         int width;
50 } base_plotter_t;
51
52 #define decl_self(type, from) \
53   type *self = (type *) from
54
55 static void set_color(plotter_t *_self, const color_t *color)
56 {
57         decl_self(base_plotter_t, _self);
58         self->color = color;
59 }
60
61 static const color_t *get_color(const plotter_t *_self)
62 {
63         decl_self(const base_plotter_t, _self);
64         return self->color;
65 }
66
67 static void set_width(plotter_t *_self, int width)
68 {
69         decl_self(base_plotter_t, _self);
70         self->width = width;
71 }
72
73 static int get_width(const plotter_t *_self)
74 {
75         decl_self(const base_plotter_t, _self);
76         return self->width;
77 }
78
79 static void plotter_default_free(plotter_t *self)
80 {
81         (void) self;
82 }
83
84 typedef struct {
85         base_plotter_t inh;
86         const char     *filename;
87         FILE           *f;
88 } ps_plotter_t;
89
90
91 /*
92   ____  ____    ____  _       _   _
93  |  _ \/ ___|  |  _ \| | ___ | |_| |_ ___ _ __
94  | |_) \___ \  | |_) | |/ _ \| __| __/ _ \ '__|
95  |  __/ ___) | |  __/| | (_) | |_| ||  __/ |
96  |_|   |____/  |_|   |_|\___/ \__|\__\___|_|
97
98 */
99
100 static void ps_begin(plotter_t *_self, const rect_t *vis)
101 {
102         FILE *f;
103         decl_self(ps_plotter_t, _self);
104
105         f = self->f = fopen(self->filename, "wt");
106         fprintf(f, "%%!PS-Adobe-2.0\n");
107         fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
108 }
109
110 static void ps_setcolor(plotter_t *_self, const color_t *color)
111 {
112         decl_self(ps_plotter_t, _self);
113         set_color(_self, color);
114
115         fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
116                 color->r, color->g, color->b);
117 }
118
119 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
120 {
121         decl_self(ps_plotter_t, _self);
122
123         fprintf(self->f, "%d %d moveto\n", x1, y1);
124         fprintf(self->f, "%d %d lineto\n", x2, y2);
125         fprintf(self->f, "stroke\n");
126 }
127
128 static void ps_box(plotter_t *_self, const rect_t *rect)
129 {
130         decl_self(ps_plotter_t, _self);
131
132         fprintf(self->f, "%d %d %d %d rectstroke\n",
133                 rect->x, rect->y, rect->w, rect->h);
134 }
135
136 static void ps_text(plotter_t *_self, int x, int y, const char *str)
137 {
138         decl_self(ps_plotter_t, _self);
139
140         fprintf(self->f, "%d %d moveto\n", x, y);
141         fprintf(self->f, "(%s) show\n", str);
142 }
143
144 static void ps_finish(plotter_t *_self)
145 {
146         decl_self(ps_plotter_t, _self);
147         fclose(self->f);
148 }
149
150 static const plotter_if_t ps_plotter_vtab = {
151         ps_begin,
152         ps_setcolor,
153         get_color,
154         set_width,
155         get_width,
156         ps_line,
157         ps_box,
158         ps_text,
159         ps_finish,
160         plotter_default_free
161 };
162
163 plotter_t *new_plotter_ps(const char *filename)
164 {
165         ps_plotter_t *ps_plotter = XMALLOC(ps_plotter_t);
166         plotter_t *p = (plotter_t *) ps_plotter;
167
168         ps_plotter->filename = filename;
169         p->vtab = &ps_plotter_vtab;
170         return p;
171 }
172
173 extern void plotter_free(plotter_t *self)
174 {
175         self->vtab->free(self);
176         free(self);
177 }
178
179 const draw_chordal_opts_t draw_chordal_def_opts = {
180         10, 10, 30, 8, 10, 10
181 };
182
183 typedef struct draw_chordal_env_t {
184         const be_chordal_env_t      *chordal_env;
185         const arch_register_class_t *cls;
186         pmap                        *block_dims;
187         plotter_t                   *plotter;
188         const draw_chordal_opts_t   *opts;
189         struct obstack              obst;
190         int                         max_color;
191 } draw_chordal_env_t;
192
193 struct block_dims {
194         unsigned max_step;
195         int      min_step;
196         int      max_color;
197         rect_t   box;
198         rect_t   subtree_box;
199 };
200
201 #define doz(a, b) MAX((a) - (b), 0)
202
203 static void block_dims_walker(ir_node *block, void *data)
204 {
205         draw_chordal_env_t        *env  = (draw_chordal_env_t*)data;
206         struct list_head          *head = get_block_border_head(env->chordal_env, block);
207         const draw_chordal_opts_t *opts = env->opts;
208         struct block_dims         *dims = OALLOCZ(&env->obst, struct block_dims);
209
210         dims->min_step = INT_MAX;
211
212         foreach_border_head(head, b) {
213                 ir_node               *irn = b->irn;
214                 const arch_register_t *reg = arch_get_irn_register(irn);
215                 int                    col = reg->index;
216
217                 dims->max_step  = MAX(dims->max_step, b->step);
218                 dims->max_color = MAX(dims->max_color, col);
219                 env->max_color  = MAX(env->max_color, col);
220         }
221
222         dims->min_step = 1;
223
224         dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
225         dims->box.h = dims->max_step * opts->v_inter_gap;
226
227         pmap_insert(env->block_dims, block, dims);
228 }
229
230 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
231 {
232         const draw_chordal_opts_t *opts   = env->opts;
233         struct block_dims         *dims   = pmap_get(struct block_dims, env->block_dims, bl);
234         rect_t                    *rect   = &dims->subtree_box;
235         int                       h_space = 0;
236         int                       v_space = 0;
237         ir_node                   *sub;
238
239         memset(rect, 0, sizeof(*rect));
240         rect->x = x;
241
242         dominates_for_each(bl, sub) {
243                 struct block_dims *bl_dim = pmap_get(struct block_dims, env->block_dims, sub);
244
245                 layout(env, sub, rect->x + rect->w);
246
247                 rect->w += h_space + bl_dim->subtree_box.w;
248                 rect->h  = MAX(rect->h, bl_dim->subtree_box.h);
249
250                 h_space = opts->h_gap;
251                 v_space = opts->v_gap;
252         }
253
254         rect->w = MAX(rect->w, dims->box.w + opts->h_gap);
255
256         dims->box.x = x + doz(rect->w, dims->box.w) / 2;
257         dims->box.y = rect->h + v_space;
258
259         rect->h = dims->box.y + dims->box.h;
260 }
261
262 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
263 {
264         const draw_chordal_opts_t *opts      = env->opts;
265         struct block_dims         *dims      = pmap_get(struct block_dims, env->block_dims, bl);
266         int                       max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
267         ir_node                   *sub;
268
269         dominates_for_each(bl, sub) {
270                 struct block_dims *bl_dim = pmap_get(struct block_dims, env->block_dims, sub);
271                 int height_diff = max_height - bl_dim->subtree_box.h;
272
273                 set_y(env, sub, up + height_diff);
274         }
275
276         dims->subtree_box.y += up;
277         dims->box.y         += up;
278 }
279
280 static color_t *reg_to_color(const draw_chordal_env_t *env,
281                                                          ir_node *rel_bl, ir_node *irn, color_t *color)
282 {
283         int phi_arg = 0;
284         (void) env;
285         (void) rel_bl;
286
287         foreach_out_edge(irn, edge)
288                 phi_arg |= is_Phi(edge->src);
289
290         color->r = is_Phi(irn) ? 0.5 : 0.0;
291         color->g = phi_arg ? 0.5 : 0.0;
292         color->b = 0.0;
293         return color;
294 }
295
296 static void draw_block(ir_node *bl, void *data)
297 {
298         static const color_t      black    = { 0, 0, 0 };
299         const draw_chordal_env_t  *env     = (const draw_chordal_env_t*)data;
300         const be_lv_t             *lv      = be_get_irg_liveness(env->chordal_env->irg);
301         struct list_head          *head    = get_block_border_head(env->chordal_env, bl);
302         ir_node                   *dom     = get_Block_idom(bl);
303         const draw_chordal_opts_t *opts    = env->opts;
304         struct block_dims         *dims    = pmap_get(struct block_dims, env->block_dims, bl);
305         char                      buf[64];
306
307         ir_snprintf(buf, sizeof(buf), "%F", bl);
308
309         env->plotter->vtab->set_color(env->plotter, &black);
310         env->plotter->vtab->box(env->plotter, &dims->box);
311
312         env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
313
314         foreach_border_head(head, b) {
315                 if (b->is_def) {
316                         const arch_register_t *reg = arch_get_irn_register(b->irn);
317                         int live_out = be_is_live_out(lv, bl, b->irn);
318                         int x        = (reg->index + 1) * opts->h_inter_gap;
319                         int ystart   = (b->step) * opts->v_inter_gap;
320                         int ystop    = (b->other_end->step) * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
321
322                         color_t color;
323                         reg_to_color(env, bl, b->irn, &color);
324
325                         x      += dims->box.x;
326                         ystart += dims->box.y;
327                         ystop  += dims->box.y;
328
329                         env->plotter->vtab->set_color(env->plotter, &color);
330                         env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
331
332                         env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
333                         env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
334                 }
335         }
336
337         if (dom) {
338                 struct block_dims *dom_dims = pmap_get(struct block_dims, env->block_dims, dom);
339
340                 be_lv_foreach_cls(lv, bl, be_lv_state_in, env->cls, irn) {
341                         const arch_register_t *reg = arch_get_irn_register(irn);
342                         int                    x   = (reg->index + 1) * opts->h_inter_gap;
343                         color_t                color;
344
345                         reg_to_color(env, bl, irn, &color);
346
347                         env->plotter->vtab->set_color(env->plotter, &color);
348                         env->plotter->vtab->line(env->plotter,
349                                 dims->box.x + x,
350                                 dims->box.y + dims->box.h,
351                                 dom_dims->box.x + x,
352                                 dom_dims->box.y);
353                 }
354         }
355 }
356
357 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
358 {
359         ir_graph  *irg = env->chordal_env->irg;
360         plotter_t *p = env->plotter;
361         rect_t bbox;
362
363         bbox.x = bbox.y = 0;
364         bbox.w = start_box->w + 2 * env->opts->x_margin;
365         bbox.h = start_box->h + 2 * env->opts->y_margin;
366
367         be_assure_live_sets(irg);
368         be_assure_live_chk(irg);
369
370         p->vtab->begin(p, &bbox);
371         irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
372         p->vtab->finish(p);
373 }
374
375 void draw_interval_tree(const draw_chordal_opts_t *opts,
376                         const be_chordal_env_t *chordal_env, plotter_t *plotter)
377 {
378         draw_chordal_env_t env;
379         struct block_dims  *start_dims;
380         ir_node            *start_block = get_irg_start_block(chordal_env->irg);
381
382         env.opts        = opts;
383         env.block_dims  = pmap_create();
384         env.plotter     = plotter;
385         env.cls         = chordal_env->cls;
386         env.max_color   = 0;
387         env.chordal_env = chordal_env;
388         obstack_init(&env.obst);
389
390         irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
391         layout(&env, start_block, opts->x_margin);
392         set_y(&env, start_block, opts->y_margin);
393         start_dims = pmap_get(struct block_dims, env.block_dims, start_block);
394         draw(&env, &start_dims->subtree_box);
395
396         pmap_destroy(env.block_dims);
397         obstack_free(&env.obst, NULL);
398 }