Added support for DAG's and distribution tables
[libfirm] / ir / stat / distrib.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/distrib.c
4  * Purpose:     Statistics for Firm. Distribution tables.
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11 #ifdef HAVE_CONFIG_H
12 # include "config.h"
13 #endif
14
15 #include "firmstat_t.h"
16
17 /**
18  * calculates a hash value for an address
19  * Addresses are typically aligned at 32bit, so we ignore the lowest bits
20  */
21 static unsigned addr_hash(const void *object)
22 {
23   return (unsigned)object >> 3;
24 }
25
26 /**
27  * calculates a hash value for an integer
28  */
29 static unsigned int_hash(const void *object)
30 {
31   return (unsigned)object;
32 }
33
34 /**
35  * compare function for integer distribution tables
36  */
37 static int int_cmp_fun(const void *elt, const void *key)
38 {
39   int p1 = (int)elt;
40   int p2 = (int)key;
41
42   return p1 - p2;
43 }
44
45 /*
46  * create a new distribution table
47  */
48 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
49 {
50   distrib_tbl_t *res;
51
52   res = malloc(sizeof(*res));
53
54   if (! res)
55     return res;
56
57   obstack_init(&res->cnts);
58
59   /* create the hash-table */
60   res->hash_map  = new_pset(cmp_func, 8);
61   res->hash_func = hash_func ? hash_func : addr_hash;
62   res->int_dist  = 0;
63
64   return res;
65 }
66
67 /*
68  * create a new distribution table for an integer distribution
69  */
70 distrib_tbl_t *stat_new_int_distrib_tbl(void)
71 {
72   distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
73
74   if (res)
75     res->int_dist = 1;
76
77   return res;
78 }
79
80 /*
81  * destroy a distribution table
82  */
83 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
84 {
85   if (tbl) {
86     /* free all entries */
87     obstack_free(&tbl->cnts, NULL);
88
89     /* delete the hash table */
90     del_pset(tbl->hash_map);
91   }
92 }
93
94 /**
95  * Returns the associates distrib_entry_t for an object
96  */
97 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
98 {
99   distrib_entry_t key;
100   distrib_entry_t *elem;
101
102   key.object = object;
103
104   elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
105   if (elem)
106     return elem;
107
108   elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
109
110   /* clear counter */
111   cnt_clr(&elem->cnt);
112
113   elem->object = object;
114
115   return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
116 }
117
118 /*
119  * adds a new object count into the distribution table
120  */
121 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
122 {
123   distrib_entry_t *elem = distrib_get_entry(tbl, object);
124
125   cnt_add(&elem->cnt, cnt);
126 }
127
128 /**
129  * adds a new key count into the integer distribution table
130  */
131 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
132 {
133    stat_add_distrib_tbl(tbl, (const void *)key, cnt);
134 }
135
136 /*
137  * calculates the mean value of a distribution
138  */
139 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
140 {
141   distrib_entry_t *entry;
142   unsigned count;
143   double sum;
144
145   if (tbl->int_dist) {
146     /* integer distribution, need min, max */
147     int min, max;
148
149     entry = pset_first(tbl->hash_map);
150
151     if (! entry)
152       return 0.0;
153
154     min =
155     max = (int)entry->object;
156     sum = cnt_to_dbl(&entry->cnt);
157
158
159     for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
160       int value = (int)entry->object;
161
162       if (value < min)
163         min = value;
164       if (value > max);
165         max = value;
166
167       sum += cnt_to_dbl(&entry->cnt);
168     }
169     count = max - min + 1;
170   }
171   else {
172     sum = 0.0;
173     count = 0;
174     for (entry = pset_first(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
175       sum += cnt_to_dbl(&entry->cnt);
176       ++count;
177     }
178   }
179
180   return count ? sum / (double)count : 0.0;
181 }
182
183 /**
184  * iterates over all entries in a distribution table
185  */
186 void stat_iterate_distrib_tbl(distrib_tbl_t *tbl, eval_distrib_entry_fun eval)
187 {
188   distrib_entry_t *entry;
189
190   for (entry = pset_first(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
191     eval(entry);
192   }
193 }