optimize next_use calculation (quadratic in number of outs not number of nodes in...
[libfirm] / ir / be / bestatevent.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  * @file
22  * @brief       Statistic events.
23  * @author      Sebastian Hack
24  * @date        03.09.2006
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <assert.h>
32 #include <string.h>
33 #include <stdio.h>
34
35 #include "bestatevent.h"
36
37 #define STACK_SIZE 16
38
39 typedef struct {
40         FILE *f;
41         char tag[512];
42 } ev_env_t;
43
44 static ev_env_t envs[STACK_SIZE];
45 static unsigned sp = 0;
46
47 void be_stat_ev_push(const char **tags, int n_tags, FILE *f)
48 {
49         int i;
50         ev_env_t *env;
51
52         assert(sp < STACK_SIZE && "stat event stack full");
53         env = &envs[sp++];
54
55         env->tag[0] = '\0';
56         for(i = 0; i < n_tags; ++i) {
57                 strncat(env->tag, tags[i], sizeof(env->tag));
58                 strncat(env->tag, ";", sizeof(env->tag));
59         }
60         env->tag[sizeof(env->tag) - 1] = '\0';
61         env->f = f;
62 }
63
64 void be_stat_ev_pop(void)
65 {
66         if (sp == 0)
67                 return;
68
69         envs[--sp].f = NULL;
70 }
71
72 void be_stat_ev(const char *ev, int value)
73 {
74         ev_env_t *env;
75
76         if (sp == 0)
77                 return;
78
79         env = &envs[sp - 1];
80         if (env->f == NULL)
81                 return;
82
83         fprintf(env->f, "%s%s;%d\n", env->tag, ev, value);
84 }
85
86 void be_stat_ev_l(const char *ev, long value)
87 {
88         ev_env_t *env;
89
90         if (sp == 0)
91                 return;
92
93         env = &envs[sp - 1];
94         if (env->f == NULL)
95                 return;
96
97         fprintf(env->f, "%s%s;%ld\n", env->tag, ev, value);
98 }
99
100 void be_stat_ev_dbl(const char *ev, double value)
101 {
102         ev_env_t *env;
103
104         if (sp == 0)
105                 return;
106
107         env = &envs[sp - 1];
108         if (env->f == NULL)
109                 return;
110
111         fprintf(env->f, "%s%s;%f\n", env->tag, ev, value);
112 }
113
114 void be_stat_ev_ull(const char *ev, ulong64 value)
115 {
116         ev_env_t *env;
117
118         if (sp == 0)
119                 return;
120
121         env = &envs[sp - 1];
122         if (env->f == NULL)
123                 return;
124
125         fprintf(env->f, "%s%s;%" ULL_FMT "\n", env->tag, ev, value);
126 }
127
128 int be_stat_ev_is_active(void)
129 {
130         return sp > 0 && envs[sp - 1].f != NULL;
131 }