Bug fixes
[libfirm] / ir / be / becopystat.c
1 /**
2  * @author Daniel Grund
3  * @date 19.04.2005
4  */
5 #include <stdlib.h>
6 #include "list.h"
7 #include "irprog.h"
8 #include "beutil.h"
9 #include "becopystat.h"
10
11 struct _irg_stat_t {
12         struct list_head chain;
13         ir_graph *irg;
14         const char *irg_name;
15         int interferers, lb, max;
16         int copies[3];
17         //TODO double ilp_time;
18 };
19
20 typedef struct _all_stat_t {
21         const char *unit_name;
22         struct list_head irgs;
23 } all_stat_t;
24
25 static all_stat_t *all_stats = NULL;
26
27 #define irg_list_entry(lh) list_entry(lh, irg_stat_t, chain)
28
29 irg_stat_t *new_irg_stat(copy_opt_t *co) {
30         irg_stat_t *is, *curr;
31
32         if (!all_stats) {
33                 all_stats = calloc(1, sizeof(*all_stats));
34                 all_stats->unit_name = get_irp_prog_name();
35                 INIT_LIST_HEAD(&all_stats->irgs);
36                 is = calloc(1, sizeof(*is));
37                 is->irg_name = "CUMULATIVE";
38                 list_add_tail(&is->chain, &all_stats->irgs);
39         }
40
41         /* look if we had this irg already */
42         list_for_each_entry(irg_stat_t, curr, &all_stats->irgs, chain)
43                 if (curr->irg == co->irg)
44                         return curr;
45
46         /* else create a new entry */
47         is = calloc(1, sizeof(*is));
48         is->irg = co->irg;
49         is->irg_name = get_entity_name(get_irg_entity(co->irg));
50         list_add_tail(&is->chain, &all_stats->irgs);
51         return is;
52 }
53
54 void irg_stat_count(irg_stat_t *is, copy_opt_t *co, int phase) {
55         unit_t *curr;
56         int max = 0, copies = 0;
57
58         list_for_each_entry(unit_t, curr, &co->units, units) {
59                 int i;
60                 max += curr->interf + curr->node_count - 1;
61                 const ir_node *root = curr->nodes[0];
62                 int root_color = get_irn_color(root);
63                 copies += curr->interf;
64                 for (i=1; i<curr->node_count; ++i) {
65                         const ir_node *arg = curr->nodes[i];
66                         if (root_color != get_irn_color(arg))
67                                 copies++;
68                 }
69         }
70
71         is->copies[phase] += copies;
72         if (phase == 0)
73                 is->max += max;
74         if (phase == 1) {
75                 is->interferers += co_get_interferer_count(co);
76                 is->lb += co_get_lower_bound(co);
77         }
78 }
79
80 void irg_stat_print(irg_stat_t *is) {
81         printf("Irg %s: %3d %3d %3d %3d %3d %3d", is->irg_name, is->interferers, is->lb, is->copies[0], is->copies[1], is->copies[2], is->max);
82 }
83
84 void all_stat_dump(void) {
85         FILE *out;
86         irg_stat_t *cuml, *curr;
87         /* Compute cumulative values */
88         cuml = irg_list_entry(all_stats->irgs.next);
89         cuml->interferers = 0;
90         cuml->lb = 0;
91         cuml->max = 0;
92         cuml->copies[0] = 0;
93         cuml->copies[1] = 0;
94         cuml->copies[2] = 0;
95         list_for_each_entry(irg_stat_t, curr, &all_stats->irgs, chain) {
96                 int i = 0;
97                 cuml->interferers += curr->interferers;
98                 cuml->lb += curr->lb;
99                 cuml->max += curr->max;
100                 for (i=0; i<3; ++i)
101                         cuml->copies[i] += curr->copies[i];
102         }
103
104         /* dump to file */
105         out = ffopen(all_stats->unit_name, "stats", "wt");
106         list_for_each_entry(irg_stat_t, curr, &all_stats->irgs, chain)
107                 fprintf(out, "%15s  %3d %3d %3d %3d %3d %3d", curr->irg_name, curr->interferers, curr->lb, curr->copies[0], curr->copies[1], curr->copies[2], curr->max);
108         fclose(out);
109 }