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