Fixed some bugs
[libfirm] / ir / be / becopystat.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                19.04.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <string.h>
12 #include <libcore/lc_timing.h>
13
14 #include "xmalloc.h"
15 #include "irgraph.h"
16 #include "irgwalk.h"
17 #include "irprog.h"
18 #include "iredges_t.h"
19 #include "phiclass_t.h"
20 #include "bechordal_t.h"
21 #include "beutil.h"
22 #include "becopyopt_t.h"
23 #include "becopystat.h"
24
25 #ifdef COPYOPT_STAT
26
27 #define DEBUG_LVL SET_LEVEL_1
28 static firm_dbg_module_t *dbg = NULL;
29
30 #define MAX_ARITY 20
31 #define MAX_CLS_SIZE 20
32 #define MAX_CLS_PHIS 20
33
34 /**
35  * For an explanation of these values see the code of copystat_dump_pretty
36  */
37 enum vals_t {
38         /* FROM HERE: PROBLEM CHARACTERIZATION */
39
40         I_ALL_NODES = 0,
41         I_BLOCKS,
42
43         /* phi nodes */
44         I_PHI_CNT,                      /* number of phi nodes */
45         I_PHI_ARG_CNT,          /* number of arguments of phis */
46         I_PHI_ARG_SELF,         /* number of arguments of phis being the phi itself */
47         I_PHI_ARG_CONST,        /* number of arguments of phis being consts */
48         I_PHI_ARG_PRED,         /* ... being defined in a cf-pred */
49         I_PHI_ARG_GLOB,         /* ... being defined elsewhere */
50         I_PHI_ARITY_S,
51         I_PHI_ARITY_E    = I_PHI_ARITY_S+MAX_ARITY,
52
53         /* copy nodes */
54         I_CPY_CNT,                      /* number of copynodes */
55
56         /* phi classes */
57         I_CLS_CNT,                      /* number of phi classes */
58         I_CLS_IF_FREE,          /* number of pc having no interference */
59         I_CLS_IF_MAX,           /* number of possible interferences in all classes */
60         I_CLS_IF_CNT,           /* number of actual interferences in all classes */
61         I_CLS_SIZE_S,
62         I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
63         I_CLS_PHIS_S,
64         I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
65
66         /* FROM HERE: RESULT VLAUES */
67         /* all of them are external set */
68
69         /* ilp values */
70         I_HEUR_TIME,            /* solving time in milli seconds */
71         I_ILP_TIME,                     /* solving time in milli seconds */
72     I_ILP_VARS,
73     I_ILP_CSTR,
74         I_ILP_ITER,                     /* number of simplex iterations */
75
76         /* copy instructions */
77         I_COPIES_MAX,           /* max possible costs of copies*/
78         I_COPIES_INIT,          /* number of copies in initial allocation */
79         I_COPIES_HEUR,          /* number of copies after heuristic */
80         I_COPIES_5SEC,          /* number of copies after ilp with max n sec */
81         I_COPIES_30SEC,         /* number of copies after ilp with max n sec */
82         I_COPIES_OPT,           /* number of copies after ilp */
83         I_COPIES_IF,            /* number of copies inevitable due to root-arg-interf */
84
85         ASIZE
86 };
87
88 /**
89  * Holds current values. Values are added till next copystat_reset
90  */
91 int curr_vals[ASIZE];
92
93 static pset *all_phi_nodes;
94 static pset *all_phi_classes;
95 static pset *all_copy_nodes;
96 static ir_graph *last_irg;
97
98 void copystat_init(void) {
99         dbg = firm_dbg_register("ir.be.copystat");
100         firm_dbg_set_mask(dbg, DEBUG_LVL);
101
102         all_phi_nodes = pset_new_ptr_default();
103         all_phi_classes = pset_new_ptr_default();
104         all_copy_nodes = pset_new_ptr_default();
105 }
106
107 void copystat_reset(void) {
108         int i;
109         for (i = 0; i < ASIZE; ++i)
110                 curr_vals[i] = 0;
111         del_pset(all_phi_nodes);
112         del_pset(all_phi_classes);
113         del_pset(all_copy_nodes);
114         all_phi_nodes = pset_new_ptr_default();
115         all_phi_classes = pset_new_ptr_default();
116         all_copy_nodes = pset_new_ptr_default();
117 }
118
119 /**
120  * Collect general data
121  */
122 static void irg_stat_walker(ir_node *node, void *env) {
123         arch_env_t *arch_env = env;
124         curr_vals[I_ALL_NODES]++; /* count all nodes */
125
126         if (is_Block(node)) /* count all blocks */
127                 curr_vals[I_BLOCKS]++;
128
129         if (is_Reg_Phi(node)) /* collect phis */
130                 pset_insert_ptr(all_phi_nodes, node);
131
132         if (is_Perm_Proj(arch_env, node))
133                 pset_insert_ptr(all_copy_nodes, node);
134
135         /* TODO: Add 2-Addr-Code nodes */
136 }
137
138 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
139         irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
140         all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
141         last_irg = irg;
142 }
143
144 /**
145  * @return 1 if the block at pos @p pos removed a critical edge
146  *                 0 else
147  */
148 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
149         const ir_edge_t *edge;
150         const ir_node *bl_at_pos, *bl_before;
151         assert(is_Block(bl));
152
153         /* Does bl have several predecessors ?*/
154         if (get_irn_arity(bl) <= 1)
155                 return 0;
156
157         /* Does the pred have exactly one predecessor */
158         bl_at_pos = get_irn_n(bl, pos);
159         if (get_irn_arity(bl_at_pos) != 1)
160                 return 0;
161
162         /* Does the pred of the pred have several sucsecessors */
163         bl_before = get_irn_n(bl_at_pos, 0);
164         edge = get_block_succ_first(bl_before);
165         return get_block_succ_next(bl_before, edge) ? 1 : 0;
166 }
167
168 /**
169  * Collect phi node data
170  */
171 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
172         int arity, i;
173         ir_node *phi_bl;
174         assert(is_Phi(phi));
175
176         /* count all phi phis */
177         curr_vals[I_PHI_CNT]++;
178
179         /* argument count */
180         arity = get_irn_arity(phi);
181         curr_vals[I_PHI_ARG_CNT] += arity;
182         if (arity > MAX_ARITY)
183                 curr_vals[I_PHI_ARITY_E]++;
184         else
185                 curr_vals[I_PHI_ARITY_S + arity]++;
186
187         phi_bl = get_nodes_block(phi);
188         /* type of argument {self, const, pred, glob} */
189         for (i = 0; i < arity; i++) {
190         ir_node *block_of_arg, *block_ith_pred;
191                 ir_node *arg = get_irn_n(phi, i);
192
193                 if (arg == phi) {
194                         curr_vals[I_PHI_ARG_SELF]++;
195                         continue;
196                 }
197
198                 if (iro_Const == get_irn_opcode(arg)) {
199                         curr_vals[I_PHI_ARG_CONST]++;
200                         continue;
201                 }
202
203                 /* get the pred block skipping blocks on critical edges */
204                 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
205                 if (was_edge_critical(phi_bl, i))
206                         block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
207
208                 block_of_arg = get_nodes_block(arg);
209                 if (block_of_arg == block_ith_pred) {
210                         curr_vals[I_PHI_ARG_PRED]++;
211                         continue;
212                 }
213
214                 curr_vals[I_PHI_ARG_GLOB]++;
215         }
216 }
217
218 /**
219  * Collect register-constrained node data
220  */
221 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
222         curr_vals[I_CPY_CNT]++;
223         curr_vals[I_COPIES_MAX]++;
224         if (nodes_interfere(chordal_env, root, get_Copy_src(root))) {
225                 curr_vals[I_COPIES_IF]++;
226                 assert(0 && "A Perm pair (in/out) should never interfere!");
227         }
228 }
229
230 /**
231  * Collect phi class data
232  */
233 static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
234         int i, o, size, if_free, phis;
235         ir_node **members, *p;
236
237         /* phi class count */
238         curr_vals[I_CLS_CNT]++;
239
240         /* phi class size */
241         size = pset_count(pc);
242         if (size > MAX_CLS_SIZE)
243                 curr_vals[I_CLS_SIZE_E]++;
244         else
245                 curr_vals[I_CLS_SIZE_S + size]++;
246
247         /* get an array of all members for double iterating */
248         members = xmalloc(size * sizeof(*members));
249         DBG((dbg, LEVEL_2, "Phi-class:\n"));
250         for (i = 0, p = pset_first(pc); p; p = pset_next(pc)) {
251                 DBG((dbg, LEVEL_2, "  %+F\n", p));
252                 members[i++] = p;
253         }
254         assert(i == size);
255
256         /* determine number of phis on this class */
257         phis = 0;
258         for (i = 0; i < size; ++i)
259                 if (is_Phi(members[i]))
260                         phis++;
261         if (phis > MAX_CLS_PHIS)
262                 curr_vals[I_CLS_PHIS_E]++;
263         else
264                 curr_vals[I_CLS_PHIS_S + phis]++;
265
266         /* determine interference of phi class members */
267         curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
268         if_free = 1;
269         for (i = 0; i < size-1; ++i)
270                 for (o = i+1; o < size; ++o)
271                         if (nodes_interfere(chordal_env, members[i], members[o])) {
272                                 if_free = 0;
273                                 curr_vals[I_CLS_IF_CNT]++;
274                         }
275
276         /* Does this phi class have an inner interference? */
277         curr_vals[I_CLS_IF_FREE] += if_free;
278
279         xfree(members);
280 }
281
282 void copystat_collect_cls(be_chordal_env_t *cenv) {
283         ir_node *n;
284         pset *pc;
285         ir_graph *irg = cenv->irg;
286         arch_env_t *aenv = cenv->birg->main_env->arch_env;
287
288         if (last_irg != irg) {
289                 copystat_reset();
290                 copystat_collect_irg(irg, aenv);
291         }
292
293         for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
294                 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
295                         stat_phi_node(cenv, n);
296
297         for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
298                 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
299                         stat_copy_node(cenv, n);
300
301         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
302                 ir_node *member = pset_first(pc);
303                 pset_break(pc);
304                 if (arch_get_irn_reg_class(aenv, member, -1) == cenv->cls)
305                         stat_phi_class(cenv, pc);
306         }
307 }
308
309 void copystat_add_max_costs(int costs) {
310         curr_vals[I_COPIES_MAX] += costs;
311 }
312 void copystat_add_inevit_costs(int costs) {
313         curr_vals[I_COPIES_IF] += costs;
314 }
315 void copystat_add_init_costs(int costs) {
316         curr_vals[I_COPIES_INIT] += costs;
317 }
318 void copystat_add_heur_costs(int costs) {
319         curr_vals[I_COPIES_HEUR] += costs;
320 }
321 void copystat_add_ilp_5_sec_costs(int costs) {
322         curr_vals[I_COPIES_5SEC] += costs;
323 }
324 void copystat_add_ilp_30_sec_costs(int costs) {
325         curr_vals[I_COPIES_30SEC] += costs;
326 }
327 void copystat_add_opt_costs(int costs) {
328         curr_vals[I_COPIES_OPT] += costs;
329 }
330 void copystat_add_heur_time(int time) {
331         curr_vals[I_HEUR_TIME] += time;
332 }
333 void copystat_add_ilp_time(int time) {
334         curr_vals[I_ILP_TIME] += time;
335 }
336 void copystat_add_ilp_vars(int vars) {
337         curr_vals[I_ILP_VARS] += vars;
338 }
339 void copystat_add_ilp_csts(int csts) {
340         curr_vals[I_ILP_CSTR] += csts;
341 }
342 void copystat_add_ilp_iter(int iters) {
343         curr_vals[I_ILP_ITER] += iters;
344 }
345
346 void copystat_dump(ir_graph *irg) {
347         int i;
348         char buf[1024];
349         FILE *out;
350
351         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
352         out = ffopen(buf, "stat", "wt");
353
354         fprintf(out, "%d\n", ASIZE);
355         for (i = 0; i < ASIZE; i++) {
356 #if 0
357                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
358                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
359                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
360                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
361                 else
362 #endif
363                         fprintf(out, "%i\n", curr_vals[i]);
364         }
365
366     fclose(out);
367 }
368
369 void copystat_dump_pretty(ir_graph *irg) {
370         int i;
371         char buf[1024];
372         FILE *out;
373
374         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
375         out = ffopen(buf, "pstat", "wt");
376
377         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
378         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
379         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
380
381         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
382         fprintf(out, "... argument types\n");
383         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
384         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
385         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
386         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
387         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
388         fprintf(out, "... arities\n");
389         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
390                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
391
392         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
393         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
394         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
395         fprintf(out, "... sizes\n");
396         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
397                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
398         fprintf(out, "... contained phis\n");
399         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
400                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
401
402         fprintf(out, "\nILP stat\n");
403         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
404         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
405
406         fprintf(out, "\nCopy stat\n");
407         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
408         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
409         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
410         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
411         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
412
413         fclose(out);
414 }
415
416 /**
417  * Helpers for saving and restoring colors of nodes.
418  * Used to get dependable and comparable benchmark results.
419  */
420 typedef struct color_saver {
421         arch_env_t *arch_env;
422         be_chordal_env_t *chordal_env;
423         pmap *saved_colors;
424         int flag; /* 0 save, 1 load */
425 } color_save_t;
426
427 static void save_load(ir_node *irn, void *env) {
428         color_save_t *saver = env;
429         if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
430                 if (saver->flag == 0) { /* save */
431                         const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
432                         pmap_insert(saver->saved_colors, irn, (void *) reg);
433                 } else { /*load */
434                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
435                         arch_set_irn_register(saver->arch_env, irn, reg);
436                 }
437         }
438 }
439
440 static void save_colors(color_save_t *color_saver) {
441         color_saver->flag = 0;
442         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
443 }
444
445 static void load_colors(color_save_t *color_saver) {
446         color_saver->flag = 1;
447         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
448 }
449
450 /**
451  * Main compare routine
452  */
453 void co_compare_solvers(be_chordal_env_t *chordal_env) {
454         copy_opt_t *co;
455         lc_timer_t *timer;
456         color_save_t saver;
457         int costs_inevit, costs_init, costs_heur, costs_ilp1, costs_ilp2, lower_bound;
458
459         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
460         DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
461         phi_class_compute(chordal_env->irg);
462
463         /* save colors */
464         saver.arch_env = chordal_env->birg->main_env->arch_env;
465         saver.chordal_env = chordal_env;
466         saver.saved_colors = pmap_create();
467         save_colors(&saver);
468
469         /* initial values */
470         costs_inevit = co_get_inevit_copy_costs(co);
471         lower_bound  = co_get_lower_bound(co);
472         costs_init   = co_get_copy_costs(co);
473
474         DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
475         DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
476         DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
477
478         copystat_add_inevit_costs(costs_inevit);
479         copystat_add_init_costs(costs_init);
480         copystat_add_max_costs(co_get_max_copy_costs(co));
481
482
483 #ifdef DO_HEUR
484         timer = lc_timer_register("heur", NULL);
485         lc_timer_reset_and_start(timer);
486
487         co_solve_heuristic(co);
488
489         lc_timer_stop(timer);
490         costs_heur = co_get_copy_costs(co);
491         DBG((dbg, LEVEL_1, "HEUR costs: %3d\n", costs_heur));
492         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
493         copystat_add_heur_costs(costs_heur);
494         assert(lower_bound <= costs_heur);
495 #endif /* DO_HEUR */
496
497
498 #ifdef DO_ILP1
499         load_colors(&saver);
500
501         co_solve_ilp1(co, 60.0);
502
503         costs_ilp1 = co_get_copy_costs(co);
504         DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_ilp1));
505         copystat_add_opt_costs(costs_ilp1); /*TODO ADAPT */
506         assert(lower_bound <= costs_ilp1);
507 #endif /* DO_ILP1 */
508
509
510 #ifdef DO_ILP2
511         load_colors(&saver);
512
513         co_solve_ilp2(co, 60.0);
514
515         costs_ilp2 = co_get_copy_costs(co);
516         DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_ilp2));
517         copystat_add_opt_costs(costs_ilp2); /*TODO ADAPT */
518         assert(lower_bound <= costs_ilp2);
519 #endif /* DO_ILP2 */
520
521         pmap_destroy(saver.saved_colors);
522         free_copy_opt(co);
523 }
524
525
526 #endif /* COPYOPT_STAT */