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