made cnt_to_int unsigned
[libfirm] / ir / stat / const_stat.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/firmstat.c
4  * Purpose:     Statistics for Firm.
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 #include "firmstat_t.h"
12 #include "tv_t.h"
13
14 static stat_info_t *status;
15
16 /**
17  * calculated the dual logarithmus of |value|
18  */
19 static unsigned log2abs(long value) {
20   unsigned res = 0;
21
22   if (value < 0)
23     value = -value;
24
25   if (value > 0xFFFF) {
26     res += 16;
27     value >>= 16;
28   }
29   if (value > 0xFF) {
30     res += 8;
31     value >>= 8;
32   }
33   if (value > 0xF) {
34     res += 4;
35     value >>= 4;
36   }
37   if (value > 3) {
38     res += 2;
39     value >>= 2;
40   }
41   if (value > 1) {
42     res += 1;
43   }
44
45   return res;
46 }
47
48 /**
49  * classify the value of a float tarval
50  */
51 static float_classify_t classify_float_value(tarval *tv)
52 {
53   ir_mode *mode = get_tarval_mode(tv);
54
55   if (tv == get_mode_null(mode))
56     return STAT_FC_1;
57   else if (tv == get_mode_one(mode))
58     return STAT_FC_1;
59
60   return STAT_FC_OTHER;
61 }
62
63 /* return a human readable name for an float classification */
64 const char *stat_fc_name(float_classify_t classification)
65 {
66   switch (classification) {
67   case STAT_FC_0:     return "0.0";
68   case STAT_FC_1:     return "1.0";
69   case STAT_FC_2:     return "2.0";
70   case STAT_FC_0_5:   return "0.5";
71   case STAT_FC_EXACT: return "exact";
72   case STAT_FC_OTHER: return "other";
73   default:            return "<UNKNOWN>";
74   }
75 }
76
77 /**
78  * update info on Consts
79  *
80  * @param status statistic status
81  * @param node   The Const node
82  * @param graph  The graph entry containing the call
83  */
84 void stat_update_const(stat_info_t *status, ir_node *node, graph_entry_t *graph)
85 {
86   ir_mode *mode = get_irn_mode(node);
87   tarval *tv;
88   unsigned bits;
89
90   if (mode_is_int(mode)) {
91     tv   = get_Const_tarval(node);
92
93     /* FIXME: */
94     if (! tarval_is_long(tv))
95       return;
96
97     bits = log2abs(get_tarval_long(tv));
98
99     if (bits > ARR_SIZE(status->const_info.int_bits_count))
100       bits = ARR_SIZE(status->const_info.int_bits_count);
101
102     cnt_inc(&status->const_info.int_bits_count[bits]);
103   }
104   else if (mode_is_float(mode)) {
105     tv = get_Const_tarval(node);
106
107     cnt_inc(&status->const_info.floats[classify_float_value(tv)]);
108   }
109   else {
110     /* something different */
111     cnt_inc(&status->const_info.others);
112   }
113 }
114
115 /* clears the const statistics for a new snapshot */
116 void stat_const_clear(stat_info_t *status)
117 {
118   int i;
119
120   for (i = 0; i < ARR_SIZE(status->const_info.int_bits_count); ++i)
121     cnt_clr(&status->const_info.int_bits_count[i]);
122
123   for (i = 0; i < ARR_SIZE(status->const_info.floats); ++i)
124     cnt_clr(&status->const_info.floats[i]);
125
126   cnt_clr(&status->const_info.others);
127 }
128
129 /* initialize the Const statistic. */
130 void stat_init_const_cnt(stat_info_t *status)
131 {
132 }