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