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