Initial Version of pbqp coloring algorithm
[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  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <limits.h>
30
31 #include "pmap.h"
32 #include "pset.h"
33
34 #include "irgwalk.h"
35 #include "irprintf.h"
36 #include "iredges_t.h"
37 #include "irtools.h"
38
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         decl_self(base_plotter_t, _self);
57         self->color = color;
58 }
59
60 static const color_t *get_color(const plotter_t *_self) {
61         decl_self(const base_plotter_t, _self);
62         return self->color;
63 }
64
65 static void set_width(plotter_t *_self, int width) {
66         decl_self(base_plotter_t, _self);
67         self->width = width;
68 }
69
70 static int get_width(const plotter_t *_self) {
71         decl_self(const base_plotter_t, _self);
72         return self->width;
73 }
74
75 static void plotter_default_free(plotter_t *self) {
76         (void) self;
77 }
78
79 typedef struct {
80         base_plotter_t inh;
81         const char     *filename;
82         FILE           *f;
83 } ps_plotter_t;
84
85
86 /*
87   ____  ____    ____  _       _   _
88  |  _ \/ ___|  |  _ \| | ___ | |_| |_ ___ _ __
89  | |_) \___ \  | |_) | |/ _ \| __| __/ _ \ '__|
90  |  __/ ___) | |  __/| | (_) | |_| ||  __/ |
91  |_|   |____/  |_|   |_|\___/ \__|\__\___|_|
92
93 */
94
95 static void ps_begin(plotter_t *_self, const rect_t *vis)
96 {
97         FILE *f;
98         decl_self(ps_plotter_t, _self);
99
100         f = self->f = fopen(self->filename, "wt");
101         fprintf(f, "%%!PS-Adobe-2.0\n");
102         fprintf(f, "%%%%BoundingBox: %d %d %d %d\n", vis->x, vis->y, vis->w, vis->h);
103 #if 0
104         fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", 10.0);
105         fprintf(f, "mainfont setfont\n");
106 #endif /* if 0 */
107 }
108
109 static void ps_setcolor(plotter_t *_self, const color_t *color)
110 {
111         decl_self(ps_plotter_t, _self);
112         set_color(_self, color);
113
114         fprintf(self->f, "%.2f %.2f %.2f setrgbcolor\n",
115                 color->r, color->g, color->b);
116 }
117
118 static void ps_line(plotter_t *_self, int x1, int y1, int x2, int y2)
119 {
120         decl_self(ps_plotter_t, _self);
121
122         fprintf(self->f, "%d %d moveto\n", x1, y1);
123         fprintf(self->f, "%d %d lineto\n", x2, y2);
124         fprintf(self->f, "stroke\n");
125 }
126
127 static void ps_box(plotter_t *_self, const rect_t *rect)
128 {
129         decl_self(ps_plotter_t, _self);
130
131         fprintf(self->f, "%d %d %d %d rectstroke\n",
132                 rect->x, rect->y, rect->w, rect->h);
133 }
134
135 void ps_text(plotter_t *_self, int x, int y, const char *str)
136 {
137         decl_self(ps_plotter_t, _self);
138
139         fprintf(self->f, "%d %d moveto\n", x, y);
140         fprintf(self->f, "(%s) show\n", str);
141 }
142
143 static void ps_finish(plotter_t *_self)
144 {
145         decl_self(ps_plotter_t, _self);
146         fclose(self->f);
147 }
148
149 const plotter_if_t ps_plotter_vtab = {
150         ps_begin,
151         ps_setcolor,
152         get_color,
153         set_width,
154         get_width,
155         ps_line,
156         ps_box,
157         ps_text,
158         ps_finish,
159         plotter_default_free
160 };
161
162 plotter_t *new_plotter_ps(const char *filename)
163 {
164         ps_plotter_t *ps_plotter = XMALLOC(ps_plotter_t);
165         plotter_t *p = (plotter_t *) ps_plotter;
166
167         ps_plotter->filename = filename;
168         p->vtab = &ps_plotter_vtab;
169         return p;
170 }
171
172 /*
173    _____ _ _     _____  ____  _       _   _
174   |_   _(_) | __|__  / |  _ \| | ___ | |_| |_ ___ _ __
175     | | | | |/ /  / /  | |_) | |/ _ \| __| __/ _ \ '__|
176     | | | |   <  / /_  |  __/| | (_) | |_| ||  __/ |
177     |_| |_|_|\_\/____| |_|   |_|\___/ \__|\__\___|_|
178
179 */
180
181 /* chriswue: the following seems to be unused and can be deleted? */
182 #if 0
183 typedef struct {
184         base_plotter_t inh;
185         const char *filename;
186         FILE *f;
187 } tikz_plotter_t;
188
189 static void tikz_begin(plotter_t *_self, const rect_t *vis)
190 {
191         FILE *f;
192         decl_self(tikz_plotter_t, _self);
193
194         f = self->f = fopen(self->filename, "wt");
195         fprintf(f, "\\begin{tikzpicture}\n");
196 }
197
198 static void tikz_setcolor(plotter_t *_self, const color_t *color)
199 {
200         set_color(_self, color);
201 }
202
203 static void tikz_line(plotter_t *_self, int x1, int y1, int x2, int y2)
204 {
205         decl_self(tikz_plotter_t, _self);
206         fprintf(self->f, "\t\\draw (%d,%d) -- (%d,%d);\n", x1, y1, x2, y2);
207 }
208
209 static void tikz_box(plotter_t *_self, const rect_t *rect)
210 {
211         decl_self(tikz_plotter_t, _self);
212
213         fprintf(self->f, "\t\\draw (%d,%d) rectangle (%d, %d)\n",
214                 rect->x, rect->y, rect->x + rect->w, rect->y + rect->h);
215 }
216
217 void tikz_text(plotter_t *_self, int x, int y, const char *str)
218 {
219         decl_self(tikz_plotter_t, _self);
220         fprintf(self->f, "\t\\draw (%d,%d) node {%s};\n", x, y, str);
221 }
222
223 static void tikz_finish(plotter_t *_self)
224 {
225         decl_self(tikz_plotter_t, _self);
226         fclose(self->f);
227 }
228 #endif /* if 0 */
229
230
231 extern void plotter_free(plotter_t *self)
232 {
233         self->vtab->free(self);
234         free(self);
235 }
236
237 const draw_chordal_opts_t draw_chordal_def_opts = {
238         10, 10, 30, 8, 10, 10
239 };
240
241 typedef struct _draw_chordal_env_t {
242         const be_chordal_env_t      *chordal_env;
243         const arch_register_class_t *cls;
244         pmap                        *block_dims;
245         plotter_t                   *plotter;
246         const draw_chordal_opts_t   *opts;
247         struct obstack              obst;
248         int                         max_color;
249 } draw_chordal_env_t;
250
251 struct block_dims {
252         unsigned max_step;
253         int      min_step;
254         int      max_color;
255         rect_t   box;
256         rect_t   subtree_box;
257 };
258
259 #define doz(a, b) MAX((a) - (b), 0)
260
261 static void block_dims_walker(ir_node *block, void *data)
262 {
263         draw_chordal_env_t        *env  = data;
264         struct list_head          *head = get_block_border_head(env->chordal_env, block);
265         const draw_chordal_opts_t *opts = env->opts;
266         struct block_dims         *dims = OALLOCZ(&env->obst, struct block_dims);
267         border_t                  *b;
268
269         dims->min_step = INT_MAX;
270
271         list_for_each_entry_reverse(border_t, b, head, list) {
272                 ir_node               *irn = b->irn;
273                 const arch_register_t *reg = arch_get_irn_register(irn);
274                 int                   col  = arch_register_get_index(reg);
275
276                 dims->max_step  = MAX(dims->max_step, b->step);
277                 dims->max_color = MAX(dims->max_color, col);
278                 env->max_color  = MAX(env->max_color, col);
279         }
280
281         dims->min_step = 1;
282
283 #if 1
284         dims->box.w = (dims->max_color + 2) * opts->h_inter_gap;
285         dims->box.h = dims->max_step * opts->v_inter_gap;
286 #else /* ! if 1 */
287         dims->box.w = dims->box.h = 10;
288 #endif /* if 1 */
289
290         pmap_insert(env->block_dims, block, dims);
291 }
292
293 static void layout(const draw_chordal_env_t *env, ir_node *bl, int x)
294 {
295         const draw_chordal_opts_t *opts   = env->opts;
296         struct block_dims         *dims   = pmap_get(env->block_dims, bl);
297         rect_t                    *rect   = &dims->subtree_box;
298         int                       h_space = 0;
299         int                       v_space = 0;
300         ir_node                   *sub;
301
302         memset(rect, 0, sizeof(*rect));
303         rect->x = x;
304
305         dominates_for_each(bl, sub) {
306                 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
307
308                 layout(env, sub, rect->x + rect->w);
309
310                 rect->w += h_space + bl_dim->subtree_box.w;
311                 rect->h  = MAX(rect->h, bl_dim->subtree_box.h);
312
313                 h_space = opts->h_gap;
314                 v_space = opts->v_gap;
315         }
316
317         rect->w = MAX(rect->w, dims->box.w + opts->h_gap);
318
319         dims->box.x = x + doz(rect->w, dims->box.w) / 2;
320         dims->box.y = rect->h + v_space;
321
322         rect->h = dims->box.y + dims->box.h;
323 }
324
325 static void set_y(const draw_chordal_env_t *env, ir_node *bl, int up)
326 {
327         const draw_chordal_opts_t *opts      = env->opts;
328         struct block_dims         *dims      = pmap_get(env->block_dims, bl);
329         int                       max_height = dims->subtree_box.h - dims->box.h - opts->v_gap;
330         ir_node                   *sub;
331
332         dominates_for_each(bl, sub) {
333                 struct block_dims *bl_dim = pmap_get(env->block_dims, sub);
334                 int height_diff = max_height - bl_dim->subtree_box.h;
335
336                 set_y(env, sub, up + height_diff);
337         }
338
339         dims->subtree_box.y += up;
340         dims->box.y         += up;
341 }
342
343 static color_t *reg_to_color(const draw_chordal_env_t *env,
344                                                          ir_node *rel_bl, ir_node *irn, color_t *color)
345 {
346         int             phi_arg = 0;
347         const ir_edge_t *edge;
348         (void) env;
349         (void) rel_bl;
350
351         foreach_out_edge(irn, edge)
352                 phi_arg |= is_Phi(edge->src);
353
354 #if 1
355         color->r = is_Phi(irn) ? 0.5 : 0.0;
356         color->g = phi_arg ? 0.5 : 0.0;
357         color->b = 0.0;
358 #else /* ! if 1 */
359         {
360                 int live_in  = is_live_in(rel_bl, irn);
361                 int live_out = is_live_out(rel_bl, irn);
362
363                 color->r = live_in;
364                 color->g = live_out;
365                 color->b = 0.0;
366         }
367 #endif /* if 1 */
368
369         return color;
370 }
371
372 static void draw_block(ir_node *bl, void *data)
373 {
374         static const color_t      black    = { 0, 0, 0 };
375         const draw_chordal_env_t  *env     = data;
376         const be_lv_t             *lv      = be_get_birg_liveness(env->chordal_env->birg);
377         struct list_head          *head    = get_block_border_head(env->chordal_env, bl);
378         ir_node                   *dom     = get_Block_idom(bl);
379         const draw_chordal_opts_t *opts    = env->opts;
380         struct block_dims         *dims    = pmap_get(env->block_dims, bl);
381         char                      buf[64];
382         border_t                  *b;
383         int                       idx;
384
385         ir_snprintf(buf, sizeof(buf), "%F", bl);
386
387         env->plotter->vtab->set_color(env->plotter, &black);
388         env->plotter->vtab->box(env->plotter, &dims->box);
389
390 #if 1
391         env->plotter->vtab->text(env->plotter, dims->box.x, dims->box.y, buf);
392 #endif
393
394         list_for_each_entry(border_t, b, head, list) {
395                 if (b->is_def) {
396                         const arch_register_t *reg = arch_get_irn_register(b->irn);
397                         int col      = arch_register_get_index(reg);
398                         int live_out = be_is_live_out(lv, bl, b->irn);
399                         int x        = (col + 1) * opts->h_inter_gap;
400                         int ystart   = (b->step) * opts->v_inter_gap;
401                         int ystop    = (b->other_end->step) * opts->v_inter_gap + (live_out ? 0 : opts->v_inter_gap / 2);
402
403                         color_t color;
404                         reg_to_color(env, bl, b->irn, &color);
405
406                         x      += dims->box.x;
407                         ystart += dims->box.y;
408                         ystop  += dims->box.y;
409
410                         env->plotter->vtab->set_color(env->plotter, &color);
411                         env->plotter->vtab->line(env->plotter, x, ystart, x, ystop);
412
413                         env->plotter->vtab->line(env->plotter, x - 2, ystart, x + 2, ystart);
414                         env->plotter->vtab->line(env->plotter, x - 2, ystop, x + 2, ystop);
415                 }
416         }
417
418         if (dom) {
419                 struct block_dims *dom_dims = pmap_get(env->block_dims, dom);
420
421                 be_lv_foreach(lv, bl, be_lv_state_in, idx) {
422                         ir_node *irn = be_lv_get_irn(lv, bl, idx);
423                         if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
424                                 const arch_register_t *reg = arch_get_irn_register(irn);
425                                 int     col = arch_register_get_index(reg);
426                                 int     x   = (col + 1) * opts->h_inter_gap;
427                                 color_t color;
428
429                                 reg_to_color(env, bl, irn, &color);
430
431                                 env->plotter->vtab->set_color(env->plotter, &color);
432                                 env->plotter->vtab->line(env->plotter,
433                                         dims->box.x + x,
434                                         dims->box.y + dims->box.h,
435                                         dom_dims->box.x + x,
436                                         dom_dims->box.y);
437                         }
438                 }
439         }
440 }
441
442 static void draw(draw_chordal_env_t *env, const rect_t *start_box)
443 {
444         plotter_t *p = env->plotter;
445         be_lv_t *lv;
446         rect_t bbox;
447
448         bbox.x = bbox.y = 0;
449         bbox.w = start_box->w + 2 * env->opts->x_margin;
450         bbox.h = start_box->h + 2 * env->opts->y_margin;
451
452         lv = be_assure_liveness(env->chordal_env->birg);
453         be_liveness_assure_sets(lv);
454         be_liveness_assure_chk(lv);
455
456         p->vtab->begin(p, &bbox);
457         irg_block_walk_graph(env->chordal_env->irg, draw_block, NULL, env);
458         p->vtab->finish(p);
459 }
460
461 void draw_interval_tree(const draw_chordal_opts_t *opts, const be_chordal_env_t *chordal_env, plotter_t *plotter) {
462         draw_chordal_env_t env;
463         struct block_dims  *start_dims;
464         ir_node            *start_block = get_irg_start_block(chordal_env->irg);
465
466         env.opts        = opts;
467         env.block_dims  = pmap_create();
468         env.plotter     = plotter;
469         env.cls         = chordal_env->cls;
470         env.max_color   = 0;
471         env.chordal_env = chordal_env;
472         obstack_init(&env.obst);
473
474         irg_block_walk_graph(chordal_env->irg, block_dims_walker, NULL, &env);
475         layout(&env, start_block, opts->x_margin);
476         set_y(&env, start_block, opts->y_margin);
477         start_dims = pmap_get(env.block_dims, start_block);
478         draw(&env, &start_dims->subtree_box);
479
480         pmap_destroy(env.block_dims);
481         obstack_free(&env.obst, NULL);
482 }