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