03b1e5e15d6a85f3c087a7c105fa4adf1c9e2bbd
[libfirm] / ir / be / becopystat.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Copy node statistics.
23  * @author      Daniel Grund
24  * @date        19.04.2005
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "timing.h"
32 #include "irgraph.h"
33 #include "irgwalk.h"
34 #include "irprog.h"
35 #include "iredges_t.h"
36 #include "phiclass.h"
37 #include "irnodeset.h"
38
39 #include "bechordal_t.h"
40 #include "beutil.h"
41 #include "becopyopt_t.h"
42 #include "becopystat.h"
43 #include "beirg_t.h"
44 #include "bemodule.h"
45 #include "beintlive_t.h"
46
47 #define DEBUG_LVL SET_LEVEL_1
48 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
49
50 #define MAX_ARITY 20
51 #define MAX_CLS_SIZE 20
52 #define MAX_CLS_PHIS 20
53
54 /**
55  * For an explanation of these values see the code of copystat_dump_pretty
56  */
57 enum vals_t {
58         /* FROM HERE: PROBLEM CHARACTERIZATION */
59
60         I_ALL_NODES = 0,
61         I_BLOCKS,
62
63         /* phi nodes */
64         I_PHI_CNT,                      /* number of phi nodes */
65         I_PHI_ARG_CNT,          /* number of arguments of phis */
66         I_PHI_ARG_SELF,         /* number of arguments of phis being the phi itself */
67         I_PHI_ARG_CONST,        /* number of arguments of phis being consts */
68         I_PHI_ARG_PRED,         /* ... being defined in a cf-pred */
69         I_PHI_ARG_GLOB,         /* ... being defined elsewhere */
70         I_PHI_ARITY_S,
71         I_PHI_ARITY_E    = I_PHI_ARITY_S+MAX_ARITY,
72
73         /* copy nodes */
74         I_CPY_CNT,                      /* number of copynodes */
75
76         /* phi classes */
77         I_CLS_CNT,                      /* number of phi classes */
78         I_CLS_IF_FREE,          /* number of pc having no interference */
79         I_CLS_IF_MAX,           /* number of possible interferences in all classes */
80         I_CLS_IF_CNT,           /* number of actual interferences in all classes */
81         I_CLS_SIZE_S,
82         I_CLS_SIZE_E = I_CLS_SIZE_S+MAX_CLS_SIZE,
83         I_CLS_PHIS_S,
84         I_CLS_PHIS_E = I_CLS_PHIS_S+MAX_CLS_PHIS,
85
86         /* FROM HERE: RESULT VLAUES */
87         /* all of them are external set */
88
89         /* ilp values */
90         I_HEUR_TIME,            /* solving time in milli seconds */
91         I_ILP_TIME,                     /* solving time in milli seconds */
92         I_ILP_VARS,
93         I_ILP_CSTR,
94         I_ILP_ITER,                     /* number of simplex iterations */
95
96         /* copy instructions */
97         I_COPIES_MAX,           /* max possible costs of copies*/
98         I_COPIES_INIT,          /* number of copies in initial allocation */
99         I_COPIES_HEUR,          /* number of copies after heuristic */
100         I_COPIES_5SEC,          /* number of copies after ilp with max n sec */
101         I_COPIES_30SEC,         /* number of copies after ilp with max n sec */
102         I_COPIES_OPT,           /* number of copies after ilp */
103         I_COPIES_IF,            /* number of copies inevitable due to root-arg-interf */
104
105         ASIZE
106 };
107
108 /**
109  * Holds current values. Values are added till next copystat_reset
110  */
111 int curr_vals[ASIZE];
112
113 static ir_nodeset_t *all_phi_nodes;
114 static ir_nodeset_t *all_copy_nodes;
115 static ir_graph *last_irg;
116
117 void be_init_copystat(void) {
118         FIRM_DBG_REGISTER(dbg, "firm.be.copystat");
119
120         all_phi_nodes  = ir_nodeset_new(64);
121         all_copy_nodes = ir_nodeset_new(64);
122         memset(curr_vals, 0, sizeof(curr_vals));
123 }
124 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copystat);
125
126 void be_quit_copystat(void) {
127         ir_nodeset_del(all_phi_nodes);
128         ir_nodeset_del(all_copy_nodes);
129 }
130 BE_REGISTER_MODULE_DESTRUCTOR(be_quit_copystat);
131
132 void copystat_reset(void) {
133         be_quit_copystat();
134         be_init_copystat();
135 }
136
137 /**
138  * Collect general data
139  */
140 static void irg_stat_walker(ir_node *node, void *env)
141 {
142         (void)env;
143
144         curr_vals[I_ALL_NODES]++; /* count all nodes */
145
146         if (is_Block(node)) /* count all blocks */
147                 curr_vals[I_BLOCKS]++;
148
149         if (is_Reg_Phi(node)) /* collect phis */
150                 ir_nodeset_insert(all_phi_nodes, node);
151
152         if (is_Perm_Proj(node))
153                 ir_nodeset_insert(all_copy_nodes, node);
154
155         /* TODO: Add 2-Addr-Code nodes */
156 }
157
158 static void copystat_collect_irg(ir_graph *irg)
159 {
160         irg_walk_graph(irg, irg_stat_walker, NULL, NULL);
161         last_irg = irg;
162 }
163
164 /**
165  * @return 1 if the block at pos @p pos removed a critical edge
166  *                 0 else
167  */
168 static INLINE int was_edge_critical(const ir_node *bl, int pos) {
169         const ir_edge_t *edge;
170         const ir_node *bl_at_pos, *bl_before;
171         assert(is_Block(bl));
172
173         /* Does bl have several predecessors ?*/
174         if (get_irn_arity(bl) <= 1)
175                 return 0;
176
177         /* Does the pred have exactly one predecessor */
178         bl_at_pos = get_irn_n(bl, pos);
179         if (get_irn_arity(bl_at_pos) != 1)
180                 return 0;
181
182         /* Does the pred of the pred have several successors */
183         bl_before = get_irn_n(bl_at_pos, 0);
184         edge = get_block_succ_first(bl_before);
185         return get_block_succ_next(bl_before, edge) ? 1 : 0;
186 }
187
188 /**
189  * Collect phi node data
190  */
191 static void stat_phi_node(be_chordal_env_t *chordal_env, ir_node *phi)
192 {
193         int arity, i;
194         ir_node *phi_bl;
195         assert(is_Phi(phi));
196         (void) chordal_env;
197
198         /* count all phi phis */
199         curr_vals[I_PHI_CNT]++;
200
201         /* argument count */
202         arity = get_irn_arity(phi);
203         curr_vals[I_PHI_ARG_CNT] += arity;
204         if (arity > MAX_ARITY)
205                 curr_vals[I_PHI_ARITY_E]++;
206         else
207                 curr_vals[I_PHI_ARITY_S + arity]++;
208
209         phi_bl = get_nodes_block(phi);
210         /* type of argument {self, const, pred, glob} */
211         for (i = 0; i < arity; i++) {
212         ir_node *block_of_arg, *block_ith_pred;
213                 ir_node *arg = get_irn_n(phi, i);
214
215                 if (arg == phi) {
216                         curr_vals[I_PHI_ARG_SELF]++;
217                         continue;
218                 }
219
220                 if (iro_Const == get_irn_opcode(arg)) {
221                         curr_vals[I_PHI_ARG_CONST]++;
222                         continue;
223                 }
224
225                 /* get the pred block skipping blocks on critical edges */
226                 block_ith_pred = get_Block_cfgpred_block(phi_bl, i);
227                 if (was_edge_critical(phi_bl, i))
228                         block_ith_pred = get_Block_cfgpred_block(block_ith_pred, 0);
229
230                 block_of_arg = get_nodes_block(arg);
231                 if (block_of_arg == block_ith_pred) {
232                         curr_vals[I_PHI_ARG_PRED]++;
233                         continue;
234                 }
235
236                 curr_vals[I_PHI_ARG_GLOB]++;
237         }
238 }
239
240 /**
241  * Collect register-constrained node data
242  */
243 static void stat_copy_node(be_chordal_env_t *chordal_env, ir_node *root) {
244         curr_vals[I_CPY_CNT]++;
245         curr_vals[I_COPIES_MAX]++;
246         if (values_interfere(chordal_env->birg, root, get_Perm_src(root))) {
247                 curr_vals[I_COPIES_IF]++;
248                 assert(0 && "A Perm pair (in/out) should never interfere!");
249         }
250 }
251
252 /**
253  * Collect phi class data
254  */
255 static void stat_phi_class(be_chordal_env_t *chordal_env, ir_node **pc) {
256         int i, o, size, if_free, phis;
257
258         /* phi class count */
259         curr_vals[I_CLS_CNT]++;
260
261         /* phi class size */
262         size = ARR_LEN(pc);
263         if (size > MAX_CLS_SIZE)
264                 curr_vals[I_CLS_SIZE_E]++;
265         else
266                 curr_vals[I_CLS_SIZE_S + size]++;
267
268         /* determine number of phis on this class */
269         for (phis = i = 0; i < size; ++i)
270                 if (is_Phi(pc[i]))
271                         phis++;
272
273         if (phis > MAX_CLS_PHIS)
274                 curr_vals[I_CLS_PHIS_E]++;
275         else
276                 curr_vals[I_CLS_PHIS_S + phis]++;
277
278         /* determine interference of phi class members */
279         curr_vals[I_CLS_IF_MAX] += size * (size - 1) / 2;
280         for (if_free = 1, i = 0; i < size - 1; ++i)
281                 for (o = i + 1; o < size; ++o)
282                         if (values_interfere(chordal_env->birg, pc[i], pc[o])) {
283                                 if_free = 0;
284                                 curr_vals[I_CLS_IF_CNT]++;
285                         }
286
287         /* Does this phi class have an inner interference? */
288         curr_vals[I_CLS_IF_FREE] += if_free;
289 }
290
291 static void copystat_collect_cls(be_chordal_env_t *cenv) {
292         ir_graph              *irg  = cenv->irg;
293         ir_node               *n, **pc;
294         phi_classes_t         *pc_obj;
295         pset                  *all_phi_classes;
296         ir_nodeset_iterator_t iter;
297
298         copystat_reset();
299         copystat_collect_irg(irg);
300
301         /* compute the Phi classes of the collected Phis */
302         pc_obj          = phi_class_new_from_set(cenv->irg, all_phi_nodes, 0);
303         all_phi_classes = get_all_phi_classes(pc_obj);
304
305         foreach_ir_nodeset(all_phi_nodes, n, iter) {
306                 if (arch_get_irn_reg_class(n, -1) == cenv->cls)
307                         stat_phi_node(cenv, n);
308         }
309
310         foreach_ir_nodeset(all_copy_nodes, n, iter) {
311                 if (arch_get_irn_reg_class(n, -1) == cenv->cls)
312                         stat_copy_node(cenv, n);
313         }
314
315         foreach_pset(all_phi_classes, pc) {
316                 ir_node *member = pc[0];
317                 if (arch_get_irn_reg_class(member, -1) == cenv->cls)
318                         stat_phi_class(cenv, pc);
319         }
320
321         /* free the phi class object */
322         phi_class_free(pc_obj);
323 }
324
325 void copystat_add_max_costs(int costs) {
326         curr_vals[I_COPIES_MAX] += costs;
327 }
328 void copystat_add_inevit_costs(int costs) {
329         curr_vals[I_COPIES_IF] += costs;
330 }
331 void copystat_add_init_costs(int costs) {
332         curr_vals[I_COPIES_INIT] += costs;
333 }
334 void copystat_add_heur_costs(int costs) {
335         curr_vals[I_COPIES_HEUR] += costs;
336 }
337 void copystat_add_ilp_5_sec_costs(int costs) {
338         curr_vals[I_COPIES_5SEC] += costs;
339 }
340 void copystat_add_ilp_30_sec_costs(int costs) {
341         curr_vals[I_COPIES_30SEC] += costs;
342 }
343 void copystat_add_opt_costs(int costs) {
344         curr_vals[I_COPIES_OPT] += costs;
345 }
346 void copystat_add_heur_time(int time) {
347         curr_vals[I_HEUR_TIME] += time;
348 }
349
350 #ifdef WITH_ILP
351
352 void copystat_add_ilp_time(int time) {
353         curr_vals[I_ILP_TIME] += time;
354 }
355 void copystat_add_ilp_vars(int vars) {
356         curr_vals[I_ILP_VARS] += vars;
357 }
358 void copystat_add_ilp_csts(int csts) {
359         curr_vals[I_ILP_CSTR] += csts;
360 }
361 void copystat_add_ilp_iter(int iters) {
362         curr_vals[I_ILP_ITER] += iters;
363 }
364
365 #endif /* WITH_ILP */
366
367 void copystat_dump(ir_graph *irg) {
368         int i;
369         char buf[1024];
370         FILE *out;
371
372         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
373         buf[sizeof(buf) - 1] = '\0';
374         out = be_ffopen(buf, "stat", "wt");
375
376         fprintf(out, "%d\n", ASIZE);
377         for (i = 0; i < ASIZE; i++) {
378 #if 0
379                 if (i >= I_PHI_ARITY_S && i <= I_PHI_ARITY_E)
380                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_PHI_CNT]);
381                 else if (i >= I_CLS_SIZE_S && i <= I_CLS_SIZE_E)
382                         fprintf(out, "%i %i\n", curr_vals[i], curr_vals[I_CLS_CNT]);
383                 else
384 #endif
385                         fprintf(out, "%i\n", curr_vals[i]);
386         }
387
388         fclose(out);
389 }
390
391 void copystat_dump_pretty(ir_graph *irg) {
392         int i;
393         char buf[1024];
394         FILE *out;
395
396         snprintf(buf, sizeof(buf), "%s__%s", get_irp_prog_name(), get_entity_name(get_irg_entity(irg)));
397         buf[sizeof(buf) - 1] = '\0';
398         out = be_ffopen(buf, "pstat", "wt");
399
400         fprintf(out, "Nodes     %4d\n", curr_vals[I_ALL_NODES]);
401         fprintf(out, "Blocks    %4d\n", curr_vals[I_BLOCKS]);
402         fprintf(out, "CopyIrn   %4d\n", curr_vals[I_CPY_CNT]);
403
404         fprintf(out, "\nPhis      %4d\n", curr_vals[I_PHI_CNT]);
405         fprintf(out, "... argument types\n");
406         fprintf(out, " Total      %4d\n", curr_vals[I_PHI_ARG_CNT]);
407         fprintf(out, " Self       %4d\n", curr_vals[I_PHI_ARG_SELF]);
408         fprintf(out, " Constants  %4d\n", curr_vals[I_PHI_ARG_CONST]);
409         fprintf(out, " CF-Pred    %4d\n", curr_vals[I_PHI_ARG_PRED]);
410         fprintf(out, " Others     %4d\n", curr_vals[I_PHI_ARG_GLOB]);
411         fprintf(out, "... arities\n");
412         for (i = I_PHI_ARITY_S; i<=I_PHI_ARITY_E; i++)
413                 fprintf(out, " %2i %4d\n", i-I_PHI_ARITY_S, curr_vals[i]);
414
415         fprintf(out, "\nPhi classes   %4d\n", curr_vals[I_CLS_CNT]);
416         fprintf(out, " compl. free  %4d\n", curr_vals[I_CLS_IF_FREE]);
417         fprintf(out, " inner intf.  %4d / %4d\n", curr_vals[I_CLS_IF_CNT], curr_vals[I_CLS_IF_MAX]);
418         fprintf(out, "... sizes\n");
419         for (i = I_CLS_SIZE_S; i<=I_CLS_SIZE_E; i++)
420                 fprintf(out, " %2i %4d\n", i-I_CLS_SIZE_S, curr_vals[i]);
421         fprintf(out, "... contained phis\n");
422         for (i = I_CLS_PHIS_S; i<=I_CLS_PHIS_E; i++)
423                 fprintf(out, " %2i %4d\n", i-I_CLS_PHIS_S, curr_vals[i]);
424
425         fprintf(out, "\nILP stat\n");
426         fprintf(out, " Time %8d\n", curr_vals[I_ILP_TIME]);
427         fprintf(out, " Iter %8d\n", curr_vals[I_ILP_ITER]);
428
429         fprintf(out, "\nCopy stat\n");
430         fprintf(out, " Max  %4d\n", curr_vals[I_COPIES_MAX]);
431         fprintf(out, " Init %4d\n", curr_vals[I_COPIES_INIT]);
432         fprintf(out, " Heur %4d\n", curr_vals[I_COPIES_HEUR]);
433         fprintf(out, " Opt  %4d\n", curr_vals[I_COPIES_OPT]);
434         fprintf(out, " Intf %4d\n", curr_vals[I_COPIES_IF]);
435
436         fclose(out);
437 }
438
439 /**
440  * Helpers for saving and restoring colors of nodes.
441  * Used to get dependable and comparable benchmark results.
442  */
443 typedef struct color_saver {
444         be_chordal_env_t *chordal_env;
445         pmap *saved_colors;
446         int flag; /* 0 save, 1 load */
447 } color_save_t;
448
449 static void save_load(ir_node *irn, void *env) {
450         color_save_t *saver = env;
451         if (saver->chordal_env->cls == arch_get_irn_reg_class(irn, -1)) {
452                 if (saver->flag == 0) { /* save */
453                         const arch_register_t *reg = arch_get_irn_register(irn);
454                         pmap_insert(saver->saved_colors, irn, (void *) reg);
455                 } else { /*load */
456                         arch_register_t *reg = pmap_get(saver->saved_colors, irn);
457                         arch_set_irn_register(irn, reg);
458                 }
459         }
460 }
461
462 static void save_colors(color_save_t *color_saver) {
463         color_saver->flag = 0;
464         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
465 }
466
467 #ifdef WITH_ILP
468 static void load_colors(color_save_t *color_saver) {
469         color_saver->flag = 1;
470         irg_walk_graph(color_saver->chordal_env->irg, save_load, NULL, color_saver);
471 }
472 #endif
473
474 /**
475  * Main compare routine
476  */
477 void co_compare_solvers(be_chordal_env_t *chordal_env) {
478         copy_opt_t    *co;
479         ir_timer_t    *timer;
480         color_save_t  saver;
481         int costs_inevit, costs_init, costs_solved, lower_bound;
482
483         copystat_collect_cls(chordal_env);
484
485         co = new_copy_opt(chordal_env, co_get_costs_loop_depth);
486         co_build_ou_structure(co);
487         co_build_graph_structure(co);
488         DBG((dbg, LEVEL_1, "----> CO: %s\n", co->name));
489
490         /* save colors */
491         saver.chordal_env  = chordal_env;
492         saver.saved_colors = pmap_create();
493         save_colors(&saver);
494
495         /* initial values */
496         costs_inevit = co_get_inevit_copy_costs(co);
497         lower_bound  = co_get_lower_bound(co);
498         costs_init   = co_get_copy_costs(co);
499
500         DBG((dbg, LEVEL_1, "Inevit Costs: %3d\n", costs_inevit));
501         DBG((dbg, LEVEL_1, "Lower Bound: %3d\n", lower_bound));
502         DBG((dbg, LEVEL_1, "Init costs: %3d\n", costs_init));
503
504         copystat_add_inevit_costs(costs_inevit);
505         copystat_add_init_costs(costs_init);
506         copystat_add_max_costs(co_get_max_copy_costs(co));
507
508         /* heuristic 1 (Daniel Grund) */
509         timer = ir_timer_register("heur1", NULL);
510         ir_timer_reset_and_start(timer);
511
512         co_solve_heuristic(co);
513
514         ir_timer_stop(timer);
515
516         costs_solved = co_get_copy_costs(co);
517         DBG((dbg, LEVEL_1, "HEUR1 costs: %3d\n", costs_solved));
518         copystat_add_heur_time(ir_timer_elapsed_msec(timer));
519         copystat_add_heur_costs(costs_solved);
520         assert(lower_bound <= costs_solved);
521
522         /* heuristic 2 (Sebastian Hack) */
523         timer = ir_timer_register("heur2", NULL);
524         ir_timer_reset_and_start(timer);
525
526         co_solve_heuristic_new(co);
527
528         ir_timer_stop(timer);
529
530         costs_solved = co_get_copy_costs(co);
531         DBG((dbg, LEVEL_1, "HEUR2 costs: %3d\n", costs_solved));
532         copystat_add_heur_time(ir_timer_elapsed_msec(timer));
533         copystat_add_heur_costs(costs_solved);
534         assert(lower_bound <= costs_solved);
535
536         /* Park & Moon register coalescing (Kimon Hoffmann) */
537         timer = ir_timer_register("park", NULL);
538         ir_timer_reset_and_start(timer);
539
540         co_solve_park_moon(co);
541
542         ir_timer_stop(timer);
543
544         costs_solved = co_get_copy_costs(co);
545         DBG((dbg, LEVEL_1, "Park/Moon costs: %3d\n", costs_solved));
546         copystat_add_heur_time(ir_timer_elapsed_msec(timer));
547         copystat_add_heur_costs(costs_solved);
548         assert(lower_bound <= costs_solved);
549
550
551 #ifdef WITH_ILP
552
553         /* ILP 1 is not yet implemented, so it makes no sense to compare */
554 #if 0
555         load_colors(&saver);
556
557         co_solve_ilp1(co, 60.0);
558
559         costs_solved = co_get_copy_costs(co);
560         DBG((dbg, LEVEL_1, "ILP1 costs: %3d\n", costs_solved));
561         copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
562         assert(lower_bound <= costs_solved);
563 #endif /* 0 */
564
565         /* ILP 2 */
566         load_colors(&saver);
567
568         co_solve_ilp2(co);
569
570         costs_solved = co_get_copy_costs(co);
571         DBG((dbg, LEVEL_1, "ILP2 costs: %3d\n", costs_solved));
572         copystat_add_opt_costs(costs_solved); /* TODO: ADAPT */
573         assert(lower_bound <= costs_solved);
574
575 #endif /* WITH_ILP */
576
577         /* free memory for statistic structures */
578         pmap_destroy(saver.saved_colors);
579         co_free_graph_structure(co);
580         co_free_ou_structure(co);
581         free_copy_opt(co);
582 }