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