start register allocator again, fix typo
[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 #include "beirg_t.h"
25 #include "bemodule.h"
26
27 #define DEBUG_LVL SET_LEVEL_1
28 DEBUG_ONLY(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 be_init_copystat(void) {
99         FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
100
101         all_phi_nodes   = pset_new_ptr_default();
102         all_phi_classes = pset_new_ptr_default();
103         all_copy_nodes  = pset_new_ptr_default();
104         memset(curr_vals, 0, sizeof(curr_vals));
105 }
106 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat);
107
108 void be_quit_copystat(void) {
109         del_pset(all_phi_nodes);
110         del_pset(all_phi_classes);
111         del_pset(all_copy_nodes);
112 }
113 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat);
114
115 void copystat_reset(void) {
116         be_quit_copystat();
117         be_init_copystat();
118 }
119
120 /**
121  * Collect general data
122  */
123 static void irg_stat_walker(ir_node *node, void *env) {
124         arch_env_t *arch_env = env;
125         curr_vals[I_ALL_NODES]++; /* count all nodes */
126
127         if (is_Block(node)) /* count all blocks */
128                 curr_vals[I_BLOCKS]++;
129
130         if (is_Reg_Phi(node)) /* collect phis */
131                 pset_insert_ptr(all_phi_nodes, node);
132
133         if (is_Perm_Proj(arch_env, node))
134                 pset_insert_ptr(all_copy_nodes, node);
135
136         /* TODO: Add 2-Addr-Code nodes */
137 }
138
139 static void copystat_collect_irg(ir_graph *irg, arch_env_t *arch_env) {
140         irg_walk_graph(irg, irg_stat_walker, NULL, arch_env);
141         all_phi_classes = phi_class_compute_by_phis(all_phi_nodes);
142         last_irg = irg;
143 }
144
145 /**
146  * @return 1 if the block at pos @p pos removed a critical edge
147  *                 0 else
148  */
149 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
150         const ir_edge_t *edge;
151         const ir_node *bl_at_pos, *bl_before;
152         assert(is_Block(bl));
153
154         /* Does bl have several predecessors ?*/
155         if (get_irn_arity(bl) <= 1)
156                 return 0;
157
158         /* Does the pred have exactly one predecessor */
159         bl_at_pos = get_irn_n(bl, pos);
160         if (get_irn_arity(bl_at_pos) != 1)
161                 return 0;
162
163         /* Does the pred of the pred have several successors */
164         bl_before = get_irn_n(bl_at_pos, 0);
165         edge = get_block_succ_first(bl_before);
166         return get_block_succ_next(bl_before, edge) ? 1 : 0;
167 }
168
169 /**
170  * Collect phi node data
171  */
172 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi) {
173         int arity, i;
174         ir_node *phi_bl;
175         assert(is_Phi(phi));
176
177         /* count all phi phis */
178         curr_vals[I_PHI_CNT]++;
179
180         /* argument count */
181         arity = get_irn_arity(phi);
182         curr_vals[I_PHI_ARG_CNT] += arity;
183         if (arity > MAX_ARITY)
184                 curr_vals[I_PHI_ARITY_E]++;
185         else
186                 curr_vals[I_PHI_ARITY_S + arity]++;
187
188         phi_bl = get_nodes_block(phi);
189         /* type of argument {self, const, pred, glob} */
190         for (i = 0; i < arity; i++) {
191         ir_node *block_of_arg, *block_ith_pred;
192                 ir_node *arg = get_irn_n(phi, i);
193
194                 if (arg == phi) {
195                         curr_vals[I_PHI_ARG_SELF]++;
196                         continue;
197                 }
198
199                 if (iro_Const == get_irn_opcode(arg)) {
200                         curr_vals[I_PHI_ARG_CONST]++;
201                         continue;
202                 }
203
204                 /* get the pred block skipping blocks on critical edges */
205                 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
206                 if (was_edge_critical(phi_bl, i))
207                         block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
208
209                 block_of_arg = get_nodes_block(arg);
210                 if (block_of_arg == block_ith_pred) {
211                         curr_vals[I_PHI_ARG_PRED]++;
212                         continue;
213                 }
214
215                 curr_vals[I_PHI_ARG_GLOB]++;
216         }
217 }
218
219 /**
220  * Collect register-constrained node data
221  */
222 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
223         curr_vals[I_CPY_CNT]++;
224         curr_vals[I_COPIES_MAX]++;
225         if (nodes_interfere(chordal_env, root, get_Perm_src(root))) {
226                 curr_vals[I_COPIES_IF]++;
227                 assert(0 && "A Perm pair (in/out) should never interfere!");
228         }
229 }
230
231 /**
232  * Collect phi class data
233  */
234 static void stat_phi_class(be_chordal_env_t *chordal_env, pset *pc) {
235         int i, o, size, if_free, phis;
236         ir_node **members, *p;
237
238         /* phi class count */
239         curr_vals[I_CLS_CNT]++;
240
241         /* phi class size */
242         size = pset_count(pc);
243         if (size > MAX_CLS_SIZE)
244                 curr_vals[I_CLS_SIZE_E]++;
245         else
246                 curr_vals[I_CLS_SIZE_S + size]++;
247
248         /* get an array of all members for double iterating */
249         members = xmalloc(size * sizeof(*members));
250         DBG((dbg, LEVEL_2, "Phi-class:\n"));
251         for (i = 0, p = pset_first(pc); p; p = pset_next(pc)) {
252                 DBG((dbg, LEVEL_2, "  %+F\n", p));
253                 members[i++] = p;
254         }
255         assert(i == size);
256
257         /* determine number of phis on this class */
258         phis = 0;
259         for (i = 0; i < size; ++i)
260                 if (is_Phi(members[i]))
261                         phis++;
262         if (phis > MAX_CLS_PHIS)
263                 curr_vals[I_CLS_PHIS_E]++;
264         else
265                 curr_vals[I_CLS_PHIS_S + phis]++;
266
267         /* determine interference of phi class members */
268         curr_vals[I_CLS_IF_MAX] += size*(size-1)/2;
269         if_free = 1;
270         for (i = 0; i < size-1; ++i)
271                 for (o = i+1; o < size; ++o)
272                         if (nodes_interfere(chordal_env, members[i], members[o])) {
273                                 if_free = 0;
274                                 curr_vals[I_CLS_IF_CNT]++;
275                         }
276
277         /* Does this phi class have an inner interference? */
278         curr_vals[I_CLS_IF_FREE] += if_free;
279
280         xfree(members);
281 }
282
283 void copystat_collect_cls(be_chordal_env_t *cenv) {
284         ir_node *n;
285         pset *pc;
286         ir_graph *irg = cenv->irg;
287         arch_env_t *aenv = cenv->birg->main_env->arch_env;
288
289         copystat_reset();
290         copystat_collect_irg(irg, aenv);
291
292         for (n = pset_first(all_phi_nodes); n; n = pset_next(all_phi_nodes))
293                 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
294                         stat_phi_node(cenv, n);
295
296         for (n = pset_first(all_copy_nodes); n; n = pset_next(all_copy_nodes))
297                 if (arch_get_irn_reg_class(aenv, n, -1) == cenv->cls)
298                         stat_copy_node(cenv, n);
299
300         for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
301                 ir_node *member = pset_first(pc);
302                 pset_break(pc);
303                 if (arch_get_irn_reg_class(aenv, member, -1) == cenv->cls)
304                         stat_phi_class(cenv, pc);
305         }
306 }
307
308 void copystat_add_max_costs(int costs) {
309         curr_vals[I_COPIES_MAX] += costs;
310 }
311 void copystat_add_inevit_costs(int costs) {
312         curr_vals[I_COPIES_IF] += costs;
313 }
314 void copystat_add_init_costs(int costs) {
315         curr_vals[I_COPIES_INIT] += costs;
316 }
317 void copystat_add_heur_costs(int costs) {
318         curr_vals[I_COPIES_HEUR] += costs;
319 }
320 void copystat_add_ilp_5_sec_costs(int costs) {
321         curr_vals[I_COPIES_5SEC] += costs;
322 }
323 void copystat_add_ilp_30_sec_costs(int costs) {
324         curr_vals[I_COPIES_30SEC] += costs;
325 }
326 void copystat_add_opt_costs(int costs) {
327         curr_vals[I_COPIES_OPT] += costs;
328 }
329 void copystat_add_heur_time(int time) {
330         curr_vals[I_HEUR_TIME] += time;
331 }
332
333 #ifdef WITH_ILP
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 #endif /* WITH_ILP */
349
350 void copystat_dump(ir_graph *irg) {
351         int i;
352         char buf[1024];
353         FILE *out;
354
355         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
356         buf[sizeof(buf) - 1] = '\0';
357         out = ffopen(buf, "stat", "wt");
358
359         fprintf(out, "%d\n", ASIZE);
360         for (i = 0; i < ASIZE; i++) {
361 #if 0
362                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
363                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
364                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
365                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
366                 else
367 #endif
368                         fprintf(out, "%i\n", curr_vals[i]);
369         }
370
371         fclose(out);
372 }
373
374 void copystat_dump_pretty(ir_graph *irg) {
375         int i;
376         char buf[1024];
377         FILE *out;
378
379         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
380         buf[sizeof(buf) - 1] = '\0';
381         out = ffopen(buf, "pstat", "wt");
382
383         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
384         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
385         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
386
387         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
388         fprintf(out, "... argument types\n");
389         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
390         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
391         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
392         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
393         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
394         fprintf(out, "... arities\n");
395         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
396                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
397
398         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
399         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
400         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
401         fprintf(out, "... sizes\n");
402         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
403                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
404         fprintf(out, "... contained phis\n");
405         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
406                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
407
408         fprintf(out, "\nILP stat\n");
409         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
410         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
411
412         fprintf(out, "\nCopy stat\n");
413         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
414         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
415         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
416         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
417         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
418
419         fclose(out);
420 }
421
422 /**
423  * Helpers for saving and restoring colors of nodes.
424  * Used to get dependable and comparable benchmark results.
425  */
426 typedef struct color_saver {
427         arch_env_t *arch_env;
428         be_chordal_env_t *chordal_env;
429         pmap *saved_colors;
430         int flag; /* 0 save, 1 load */
431 } color_save_t;
432
433 static void save_load(ir_node *irn, void *env) {
434         color_save_t *saver = env;
435         if (saver->chordal_env->cls == arch_get_irn_reg_class(saver->arch_env, irn, -1)) {
436                 if (saver->flag == 0) { /* save */
437                         const arch_register_t *reg = arch_get_irn_register(saver->arch_env, irn);
438                         pmap_insert(saver->saved_colors, irn, (void *) reg);
439                 } else { /*load */
440                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
441                         arch_set_irn_register(saver->arch_env, irn, reg);
442                 }
443         }
444 }
445
446 static void save_colors(color_save_t *color_saver) {
447         color_saver->flag = 0;
448         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
449 }
450
451 #ifdef WITH_ILP
452 static void load_colors(color_save_t *color_saver) {
453         color_saver->flag = 1;
454         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
455 }
456 #endif
457
458 /**
459  * Main compare routine
460  */
461 void co_compare_solvers(be_chordal_env_t *chordal_env) {
462         copy_opt_t *co;
463         lc_timer_t *timer;
464         color_save_t saver;
465         int costs_inevit, costs_init, costs_solved, lower_bound;
466
467         phi_class_compute(chordal_env->irg);
468         copystat_collect_cls(chordal_env);
469
470         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
471         co_build_ou_structure(co);
472         co_build_graph_structure(co);
473         DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
474
475         /* save colors */
476         saver.arch_env     = chordal_env->birg->main_env->arch_env;
477         saver.chordal_env  = chordal_env;
478         saver.saved_colors = pmap_create();
479         save_colors(&saver);
480         be_ra_chordal_check(co->cenv);
481
482         /* initial values */
483         costs_inevit = co_get_inevit_copy_costs(co);
484         lower_bound  = co_get_lower_bound(co);
485         costs_init   = co_get_copy_costs(co);
486
487         DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
488         DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
489         DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
490
491         copystat_add_inevit_costs(costs_inevit);
492         copystat_add_init_costs(costs_init);
493         copystat_add_max_costs(co_get_max_copy_costs(co));
494
495         /* heuristic 1 (Daniel Grund) */
496         timer = lc_timer_register("heur1", NULL);
497         lc_timer_reset_and_start(timer);
498
499         co_solve_heuristic(co);
500
501         lc_timer_stop(timer);
502
503         be_ra_chordal_check(co->cenv);
504         costs_solved = co_get_copy_costs(co);
505         DBG((dbg, LEVEL_1, "HEUR1 costs: %3d\n", costs_solved));
506         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
507         copystat_add_heur_costs(costs_solved);
508         assert(lower_bound <= costs_solved);
509
510         /* heuristic 2 (Sebastian Hack) */
511         timer = lc_timer_register("heur2", NULL);
512         lc_timer_reset_and_start(timer);
513
514         co_solve_heuristic_new(co);
515
516         lc_timer_stop(timer);
517
518         be_ra_chordal_check(co->cenv);
519         costs_solved = co_get_copy_costs(co);
520         DBG((dbg, LEVEL_1, "HEUR2 costs: %3d\n", costs_solved));
521         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
522         copystat_add_heur_costs(costs_solved);
523         assert(lower_bound <= costs_solved);
524
525         /* Park & Moon register coalescing (Kimon Hoffmann) */
526         timer = lc_timer_register("park", NULL);
527         lc_timer_reset_and_start(timer);
528
529         co_solve_park_moon(co);
530
531         lc_timer_stop(timer);
532
533         be_ra_chordal_check(co->cenv);
534         costs_solved = co_get_copy_costs(co);
535         DBG((dbg, LEVEL_1, "Park/Moon costs: %3d\n", costs_solved));
536         copystat_add_heur_time(lc_timer_elapsed_msec(timer));
537         copystat_add_heur_costs(costs_solved);
538         assert(lower_bound <= costs_solved);
539
540
541 #ifdef WITH_ILP
542
543         /* ILP 1 is not yet implemented, so it makes no sense to compare */
544 #if 0
545         load_colors(&saver);
546
547         co_solve_ilp1(co, 60.0);
548
549         costs_solved = co_get_copy_costs(co);
550         DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_solved));
551         copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
552         assert(lower_bound <= costs_solved);
553 #endif /* 0 */
554
555         /* ILP 2 */
556         load_colors(&saver);
557
558         co_solve_ilp2(co);
559
560         be_ra_chordal_check(co->cenv);
561         costs_solved = co_get_copy_costs(co);
562         DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_solved));
563         copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
564         assert(lower_bound <= costs_solved);
565
566 #endif /* WITH_ILP */
567
568         /* free memory for statistic structures */
569         pmap_destroy(saver.saved_colors);
570         co_free_graph_structure(co);
571         co_free_ou_structure(co);
572         free_copy_opt(co);
573 }