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