2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * File name: ir/ir/distrib.c
23 * Purpose: Statistics for Firm. Distribution tables.
24 * Author: Michael Beck
27 * Copyright: (c) 2004 Universität Karlsruhe
36 #include "firmstat_t.h"
39 * calculates a hash value for an address
41 static unsigned addr_hash(const void *object)
43 return HASH_PTR(object);
47 * calculates a hash value for an integer
49 static unsigned int_hash(const void *object)
51 return (unsigned)PTR_TO_INT(object);
55 * compare function for integer distribution tables
57 static int int_cmp_fun(const void *elt, const void *key)
59 const distrib_entry_t *p1 = elt;
60 const distrib_entry_t *p2 = key;
62 return (char *)p1->object - (char *)p2->object;
66 * create a new distribution table
68 distrib_tbl_t *stat_new_distrib_tbl(pset_cmp_fun cmp_func, distrib_hash_fun hash_func)
72 res = xmalloc(sizeof(*res));
74 obstack_init(&res->cnts);
76 /* create the hash-table */
77 res->hash_map = new_pset(cmp_func, 8);
78 res->hash_func = hash_func ? hash_func : addr_hash;
85 * create a new distribution table for an integer distribution
87 distrib_tbl_t *stat_new_int_distrib_tbl(void)
89 distrib_tbl_t *res = stat_new_distrib_tbl(int_cmp_fun, int_hash);
98 * destroy a distribution table
100 void stat_delete_distrib_tbl(distrib_tbl_t *tbl)
103 /* free all entries */
104 obstack_free(&tbl->cnts, NULL);
106 /* delete the hash table */
107 del_pset(tbl->hash_map);
112 * Returns the associates distrib_entry_t for an object
114 static distrib_entry_t *distrib_get_entry(distrib_tbl_t *tbl, const void *object)
117 distrib_entry_t *elem;
121 elem = pset_find(tbl->hash_map, &key, tbl->hash_func(object));
125 elem = obstack_alloc(&tbl->cnts, sizeof(*elem));
130 elem->object = object;
132 return pset_insert(tbl->hash_map, elem, tbl->hash_func(object));
136 * adds a new object count into the distribution table
138 void stat_add_distrib_tbl(distrib_tbl_t *tbl, const void *object, const counter_t *cnt)
140 distrib_entry_t *elem = distrib_get_entry(tbl, object);
142 cnt_add(&elem->cnt, cnt);
146 * adds a new key count into the integer distribution table
148 void stat_add_int_distrib_tbl(distrib_tbl_t *tbl, int key, const counter_t *cnt)
150 stat_add_distrib_tbl(tbl, (const void *)key, cnt);
154 * increases object count by one
156 void stat_inc_distrib_tbl(distrib_tbl_t *tbl, const void *object)
158 distrib_entry_t *elem = distrib_get_entry(tbl, object);
164 * increases key count by one
166 void stat_inc_int_distrib_tbl(distrib_tbl_t *tbl, int key)
168 stat_inc_distrib_tbl(tbl, (const void *)key);
172 * inserts a new object with count 0 into the distribution table
173 * if object is already present, nothing happens
175 void stat_insert_distrib_tbl(distrib_tbl_t *tbl, const void *object)
177 /* executed for side effect */
178 (void)distrib_get_entry(tbl, object);
182 * inserts a new key with count 0 into the integer distribution table
183 * if key is already present, nothing happens
185 void stat_insert_int_distrib_tbl(distrib_tbl_t *tbl, int key)
187 stat_insert_distrib_tbl(tbl, (const void *)key);
191 * returns the sum over all counters in a distribution table
193 int stat_get_count_distrib_tbl(distrib_tbl_t *tbl)
195 distrib_entry_t *entry;
196 counter_t cnt = ZERO_CNT;
198 foreach_pset(tbl->hash_map, entry)
199 cnt_add(&cnt, &entry->cnt);
200 return cnt_to_uint(&cnt);
204 * calculates the mean value of a distribution
206 double stat_calc_mean_distrib_tbl(distrib_tbl_t *tbl)
208 distrib_entry_t *entry;
213 /* integer distribution, need min, max */
216 entry = pset_first(tbl->hash_map);
222 max = (int)entry->object;
223 sum = cnt_to_dbl(&entry->cnt);
226 for (entry = pset_next(tbl->hash_map); entry; entry = pset_next(tbl->hash_map)) {
227 int value = (int)entry->object;
234 sum += cnt_to_dbl(&entry->cnt);
236 count = max - min + 1;
241 foreach_pset(tbl->hash_map, entry) {
242 sum += cnt_to_dbl(&entry->cnt);
247 return count ? sum / (double)count : 0.0;
251 * calculates the average value of a distribution
253 double stat_calc_avg_distrib_tbl(distrib_tbl_t *tbl)
255 distrib_entry_t *entry;
260 if (pset_count(tbl->hash_map) <= 0)
263 foreach_pset(tbl->hash_map, entry) {
264 sum += cnt_to_dbl(&entry->cnt) * (int)entry->object;
265 count += cnt_to_uint(&entry->cnt);
269 foreach_pset(tbl->hash_map, entry) {
270 sum += cnt_to_dbl(&entry->cnt);
275 return count ? sum / (double)count : 0.0;
279 * iterates over all entries in a distribution table
281 void stat_iterate_distrib_tbl(const distrib_tbl_t *tbl, eval_distrib_entry_fun eval, void *env)
283 distrib_entry_t *entry;
285 foreach_pset(tbl->hash_map, entry)