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