beinsn: Do not store, whether an insn has constraints.
[libfirm] / ir / stat / statev.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 events.
23  * @author      Sebastian Hack
24  * @date        17.06.2007
25  */
26 #include "config.h"
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <regex.h>
34
35 #include "util.h"
36 #include "stat_timing.h"
37 #include "irprintf.h"
38 #include "statev_t.h"
39
40 #include "config.h"
41
42 #define MAX_TIMER 256
43
44 int (stat_ev_enabled) = 0;
45
46 static FILE          *stat_ev_file     = NULL;
47 static int            stat_ev_timer_sp = 0;
48 static timing_ticks_t stat_ev_timer_elapsed[MAX_TIMER];
49 static timing_ticks_t stat_ev_timer_start[MAX_TIMER];
50
51 static regex_t  regex;
52 static regex_t *filter = NULL;
53 static inline int key_matches(const char *key)
54 {
55         if (!filter)
56                 return 1;
57
58         return regexec(filter, key, 0, NULL, 0) == 0;
59 }
60
61 static void stat_ev_vprintf(char ev, const char *key, const char *fmt, va_list ap)
62 {
63         if (!key_matches(key))
64                 return;
65
66         fprintf(stat_ev_file, "%c;%s", ev, key);
67         if (fmt != NULL) {
68                 char buf[256];
69
70                 ir_vsnprintf(buf, sizeof(buf), fmt, ap);
71                 fprintf(stat_ev_file, ";%s", buf);
72         }
73         fprintf(stat_ev_file, "\n");
74 }
75
76 static void stat_ev_printf(char ev, const char *key, const char *fmt, ...)
77 {
78         va_list ap;
79         va_start(ap, fmt);
80         stat_ev_vprintf(ev, key, fmt, ap);
81         va_end(ap);
82 }
83
84 void stat_ev_tim_push(void)
85 {
86         timing_ticks_t temp;
87         int sp = stat_ev_timer_sp++;
88         timing_ticks(temp);
89         if (sp == 0) {
90                 timing_enter_max_prio();
91         } else {
92                 timing_ticks_sub(temp, stat_ev_timer_start[sp - 1]);
93                 timing_ticks_add(stat_ev_timer_elapsed[sp - 1], temp);
94         }
95         timing_ticks_init(stat_ev_timer_elapsed[sp]);
96         timing_ticks(stat_ev_timer_start[sp]);
97 }
98
99 void stat_ev_tim_pop(const char *name)
100 {
101         int sp;
102         timing_ticks_t temp;
103         timing_ticks(temp);
104         sp = --stat_ev_timer_sp;
105         timing_ticks_sub(temp, stat_ev_timer_start[sp]);
106         timing_ticks_add(stat_ev_timer_elapsed[sp], temp);
107         if (name != NULL && stat_ev_enabled)
108                 stat_ev_printf('E', name, "%g", timing_ticks_dbl(stat_ev_timer_elapsed[sp]));
109         if (sp == 0) {
110                 timing_leave_max_prio();
111         } else {
112                 timing_ticks(stat_ev_timer_start[sp - 1]);
113         }
114 }
115
116 void do_stat_ev_ctx_push_vfmt(const char *key, const char *fmt, va_list ap)
117 {
118         stat_ev_tim_push();
119         stat_ev_vprintf('P', key, fmt, ap);
120         stat_ev_tim_pop(NULL);
121 }
122
123 void (stat_ev_ctx_push_fmt)(const char *key, const char *fmt, ...)
124 {
125         if (!stat_ev_enabled)
126                 return;
127
128         va_list ap;
129         va_start(ap, fmt);
130         do_stat_ev_ctx_push_vfmt(key, fmt, ap);
131         va_end(ap);
132 }
133
134 void (stat_ev_ctx_push_str)(const char *key, const char *str)
135 {
136         stat_ev_ctx_push_str_(key, str);
137 }
138
139 void do_stat_ev_ctx_pop(const char *key)
140 {
141         stat_ev_tim_push();
142         stat_ev_printf('O', key, NULL);
143         stat_ev_tim_pop(NULL);
144 }
145
146 void (stat_ev_ctx_pop)(const char *key)
147 {
148         stat_ev_ctx_pop_(key);
149 }
150
151 void do_stat_ev_dbl(const char *name, double value)
152 {
153         stat_ev_tim_push();
154         stat_ev_printf('E', name, "%g", value);
155         stat_ev_tim_pop(NULL);
156 }
157
158 void (stat_ev_dbl)(const char *name, double value)
159 {
160         stat_ev_dbl_(name, value);
161 }
162
163 void do_stat_ev_int(const char *name, int value)
164 {
165         stat_ev_tim_push();
166         stat_ev_printf('E', name, "%d", value);
167         stat_ev_tim_pop(NULL);
168 }
169
170 void (stat_ev_int)(const char *name, int value)
171 {
172         stat_ev_int_(name, value);
173 }
174
175 void do_stat_ev_ull(const char *name, unsigned long long value)
176 {
177         stat_ev_tim_push();
178         stat_ev_printf('E', name, "%llu", value);
179         stat_ev_tim_pop(NULL);
180 }
181
182 void (stat_ev_ull)(const char *name, unsigned long long value)
183 {
184         stat_ev_ull_(name, value);
185 }
186
187 void do_stat_ev(const char *name)
188 {
189         stat_ev_tim_push();
190         stat_ev_printf('E', name, "0.0");
191         stat_ev_tim_pop(NULL);
192 }
193
194 void (stat_ev)(const char *name)
195 {
196         stat_ev_(name);
197 }
198
199 void stat_ev_begin(const char *prefix, const char *filt)
200 {
201         char buf[512];
202
203         snprintf(buf, sizeof(buf), "%s.ev", prefix);
204         stat_ev_file = fopen(buf, "wt");
205
206         if (filt && filt[0] != '\0') {
207                 filter = NULL;
208                 if (regcomp(&regex, filt, REG_EXTENDED) == 0)
209                         filter = &regex;
210         }
211
212         stat_ev_enabled = stat_ev_file != NULL;
213 }
214
215 void stat_ev_end(void)
216 {
217         if (stat_ev_file) {
218                 fclose(stat_ev_file);
219         }
220         if (filter != NULL)
221                 regfree(filter);
222 }