remove #ifdef HAVE_CONFIG_Hs
[libfirm] / ir / stat / const_stat.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   Statistic functions for constant counting.
23  * @author  Michael Beck
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "firmstat_t.h"
29 #include "tv_t.h"
30
31 /**
32  * calculated the dual logarithm of |value|
33  */
34 static unsigned log2abs(long value) {
35         unsigned res = 0;
36
37         if (value < 0)
38                 value = -value;
39
40         if (value > 0xFFFF) {
41                 res += 16;
42                 value >>= 16;
43         }
44         if (value > 0xFF) {
45                 res += 8;
46                 value >>= 8;
47         }
48         if (value > 0xF) {
49                 res += 4;
50                 value >>= 4;
51         }
52         if (value > 3) {
53                 res += 2;
54                 value >>= 2;
55         }
56         if (value > 1) {
57                 res += 1;
58         }
59
60         return res;
61 }
62
63 /**
64  * classify the value of a float tarval
65  */
66 static float_classify_t classify_float_value(tarval *tv) {
67         ir_mode *mode = get_tarval_mode(tv);
68
69         if (tv == get_mode_null(mode))
70                 return STAT_FC_0;
71         else if (tv == get_mode_one(mode))
72                 return STAT_FC_1;
73         else if (tarval_is_finite(tv) && tarval_ieee754_zero_mantissa(tv)) {
74                 int exp = tarval_ieee754_get_exponent(tv);
75
76                 if (! tarval_is_negative(tv)) {
77                         if (exp == 1)
78                                 return STAT_FC_2;
79                         else if (exp == -1)
80                                 return STAT_FC_0_5;
81                 }
82                 return STAT_FC_POWER_OF_TWO;
83         }
84         return STAT_FC_OTHER;
85 }
86
87 /* return a human readable name for an float classification */
88 const char *stat_fc_name(float_classify_t classification) {
89         switch (classification) {
90         case STAT_FC_0:            return "0.0";
91         case STAT_FC_1:            return "1.0";
92         case STAT_FC_2:            return "2.0";
93         case STAT_FC_0_5:          return "0.5";
94         case STAT_FC_POWER_OF_TWO: return "2.0^x";
95         case STAT_FC_OTHER:        return "other";
96         default:                   return "<UNKNOWN>";
97         }
98 }
99
100 /* update info on Consts */
101 void stat_update_const(stat_info_t *status, ir_node *node, graph_entry_t *graph)
102 {
103         ir_mode *mode = get_irn_mode(node);
104         tarval *tv;
105         unsigned bits;
106         (void) graph;
107
108         if (mode_is_int(mode)) {
109                 tv   = get_Const_tarval(node);
110
111                 /* FIXME: */
112                 if (! tarval_is_long(tv))
113                         return;
114
115                 bits = log2abs(get_tarval_long(tv));
116
117                 if (bits > ARR_SIZE(status->const_info.int_bits_count))
118                         bits = ARR_SIZE(status->const_info.int_bits_count);
119
120                 cnt_inc(&status->const_info.int_bits_count[bits]);
121         } else if (mode_is_float(mode)) {
122                 tv = get_Const_tarval(node);
123
124                 cnt_inc(&status->const_info.floats[classify_float_value(tv)]);
125         } else {
126                 /* something different */
127                 cnt_inc(&status->const_info.others);
128         }
129 }
130
131 /* clears the const statistics for a new snapshot */
132 void stat_const_clear(stat_info_t *status) {
133         size_t i;
134
135         for (i = 0; i < ARR_SIZE(status->const_info.int_bits_count); ++i)
136                 cnt_clr(&status->const_info.int_bits_count[i]);
137
138         for (i = 0; i < ARR_SIZE(status->const_info.floats); ++i)
139                 cnt_clr(&status->const_info.floats[i]);
140
141         cnt_clr(&status->const_info.others);
142 }
143
144 /* initialize the Const statistic. */
145 void stat_init_const_cnt(stat_info_t *status) {
146         (void) status;
147         /* currently nothing */
148 }