only display something if we found profile data
[libfirm] / ir / be / beprofile.c
1 /** vim: set sw=4 ts=4:
2  * @file   beprofile.c
3  * @date   2006-04-06
4  * @author Adam M. Szalkowski
5  * @cvs-id $Id$
6  *
7  * Code instrumentation and execution count profiling
8  *
9  * Copyright (C) 2006 Universitaet Karlsruhe
10  * Released under the GPL
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <math.h>
17
18 #include "hashptr.h"
19 #include "debug.h"
20 #include "obst.h"
21 #include "set.h"
22 #include "list.h"
23 #include "pmap.h"
24
25 #include "entity.h"
26 #include "irprintf.h"
27 #include "irgwalk.h"
28 #include "irdump_t.h"
29 #include "irnode_t.h"
30 #include "ircons_t.h"
31 #include "irloop_t.h"
32 #include "iredges.h"
33 #include "execfreq.h"
34 #include "irvrfy.h"
35 #include "type.h"
36 #include "entity.h"
37
38 #include "be_t.h"
39 #include "belive_t.h"
40 #include "besched_t.h"
41 #include "beirgmod.h"
42 #include "bearch.h"
43 #include "beabi.h"
44 #include "benode_t.h"
45 #include "beutil.h"
46 #include "ircons.h"
47 #include "irhooks.h"
48
49 #include "bechordal_t.h"
50
51 #ifdef WITH_LIBCORE
52 #include <libcore/lc_opts.h>
53 #include <libcore/lc_opts_enum.h>
54 #endif /* WITH_LIBCORE */
55
56 #include "beprofile.h"
57
58 typedef struct _block_id_walker_data_t {
59         tarval        **array;
60         unsigned int    id;
61         ir_node *symconst;
62 } block_id_walker_data_t;
63
64 typedef struct _execcount_t {
65         unsigned int block;
66         unsigned int count;
67 } execcount_t;
68
69 static int
70 cmp_execcount(const void * a, const void * b, size_t size)
71 {
72         return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
73 }
74
75 static void
76 block_counter(ir_node * bb, void * data)
77 {
78         unsigned int  *count = data;
79         *count = *count + 1;
80
81 }
82
83 static unsigned int
84 count_blocks(ir_graph * irg)
85 {
86         unsigned int count = 0;
87
88         irg_block_walk_graph(irg, block_counter, NULL, &count);
89         return count;
90 }
91
92 /* keep the execcounts here because they are only read once per compiler run */
93 static set * profile = NULL;
94 static hook_entry_t hook;
95
96 /**
97  * Instrument a block with code needed for profiling
98  */
99 static void
100 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
101 {
102         ir_graph *irg = get_irn_irg(bb);
103         ir_node *start_block = get_irg_start_block(irg);
104         ir_node  *load, *store, *offset, *add, *projm, *proji;
105         ir_node *cnst;
106
107         if(bb == start_block || bb == get_irg_end_block(irg))
108                 return;
109
110         cnst   = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
111         offset = new_r_Add(irg, bb, address, cnst, mode_P);
112         load   = new_r_Load(irg, bb, new_NoMem(), offset, mode_Iu);
113         projm  = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
114         proji  = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
115         cnst   = new_r_Const_long(irg, start_block, mode_Iu, 1);
116         add    = new_r_Add(irg, bb, proji, cnst, mode_Iu);
117         store  = new_r_Store(irg, bb, projm, offset, add);
118         projm  = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
119         keep_alive(projm);
120 }
121
122 /**
123  * Generates a new irg which calls the initializer
124  */
125 static ir_graph *
126 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
127 {
128         ir_node *start_block;
129
130         ir_node   *ins[4];
131         ident     *name = new_id_from_str("__firmprof_initializer");
132         entity    *ent  = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
133         ir_node   *ret, *call, *symconst;
134         symconst_symbol sym;
135
136         ident     *init_name = new_id_from_str("__init_firmprof");
137         ir_type   *init_type = new_type_method(init_name, 4, 0);
138         ir_type   *uint, *uintptr, *string;
139         entity    *init_ent;
140         ir_graph  *irg;
141         ir_node   *bb;
142         ir_type   *empty_frame_type;
143
144         set_entity_ld_ident(ent, name);
145
146         uint    = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
147         uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
148         string  = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
149
150         set_method_param_type(init_type, 0, string);
151         set_method_param_type(init_type, 1, uintptr);
152         set_method_param_type(init_type, 2, uintptr);
153         set_method_param_type(init_type, 3, uint);
154         init_ent = new_entity(get_glob_type(), init_name, init_type);
155         set_entity_ld_ident(init_ent, init_name);
156
157         irg = new_ir_graph(ent, 0);
158         set_current_ir_graph(irg);
159         empty_frame_type = get_irg_frame_type(irg);
160         set_type_size_bytes(empty_frame_type, 0);
161
162         bb = get_cur_block();
163
164         start_block = get_irg_start_block(irg);
165
166         sym.entity_p = init_ent;
167         symconst     = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
168
169         sym.entity_p = ent_filename;
170         ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
171         sym.entity_p = bblock_id;
172         ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
173         sym.entity_p = bblock_counts;
174         ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
175         ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
176
177         call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
178         ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
179         mature_immBlock(bb);
180
181         add_immBlock_pred(get_irg_end_block(irg), ret);
182         mature_immBlock(get_irg_end_block(irg));
183
184         irg_finalize_cons(irg);
185
186         return irg;
187 }
188
189 static void
190 block_id_walker(ir_node * bb, void * data)
191 {
192         block_id_walker_data_t *wd = data;
193
194         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
195         instrument_block(bb, wd->symconst, wd->id);
196         ++wd->id;
197 }
198
199 ir_graph *
200 be_profile_instrument(char * filename)
201 {
202         int            n, i;
203         unsigned int   n_blocks = 0;
204         entity        *bblock_id, *bblock_counts, *ent_filename;
205         ir_type       *array_type, *integer_type, *string_type, *character_type;
206         tarval       **tarval_array, **tarval_string;
207         int            filename_len = strlen(filename)+1;
208         ident         *cur_ident;
209
210         block_id_walker_data_t  wd;
211         symconst_symbol sym;
212
213         integer_type   = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
214         array_type     = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
215         set_array_bounds_int(array_type, 0, 0, n_blocks);
216
217         character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
218         string_type    = new_type_array(new_id_from_str("__filename"), 1, character_type);
219         set_array_bounds_int(string_type, 0, 0, filename_len);
220
221         cur_ident      = new_id_from_str("__FIRMPROF__BLOCK_IDS");
222         bblock_id      = new_entity(get_glob_type(), cur_ident, array_type);
223         set_entity_ld_ident(bblock_id, cur_ident);
224         set_entity_variability(bblock_id, variability_initialized);
225
226         cur_ident      = new_id_from_str("__FIRMPROF__BLOCK_COUNTS");
227         bblock_counts  = new_entity(get_glob_type(), cur_ident, array_type);
228         set_entity_ld_ident(bblock_counts, cur_ident);
229         set_entity_variability(bblock_counts, variability_initialized);
230
231         cur_ident      = new_id_from_str("__FIRMPROF__FILE_NAME");
232         ent_filename   = new_entity(get_glob_type(), cur_ident, string_type);
233         set_entity_ld_ident(ent_filename, cur_ident);
234         set_entity_variability(ent_filename, variability_initialized);
235
236         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
237                 ir_graph *irg = get_irp_irg(n);
238
239                 n_blocks += count_blocks(irg);
240         }
241
242         /* initialize count array */
243         tarval_array = alloca(sizeof(*tarval_array) * n_blocks);
244         for (i = 0; i < n_blocks; ++i) {
245                 tarval_array[i] = get_tarval_null(mode_Iu);
246         }
247         set_array_entity_values(bblock_counts, tarval_array, n_blocks);
248
249         /* initialize function name string constant */
250         tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
251         for (i = 0; i < filename_len; ++i) {
252                 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
253         }
254         set_array_entity_values(ent_filename, tarval_string, filename_len);
255
256
257         /* initialize block id array and instrument blocks */
258         wd.array = tarval_array;
259         wd.id    = 0;
260         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
261                 ir_graph *irg = get_irp_irg(n);
262
263                 /* generate a symbolic constant pointing to the count array */
264                 sym.entity_p = bblock_counts;
265                 wd.symconst  = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
266
267                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
268         }
269         set_array_entity_values(bblock_id, tarval_array, n_blocks);
270
271         return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
272 }
273
274 static void
275 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
276 {
277         if(is_Block(irn)) {
278                 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
279         }
280 }
281
282 static void
283 register_vcg_hook(void)
284 {
285         memset(&hook, 0, sizeof(hook));
286         hook.hook._hook_node_info = profile_node_info;
287         register_hook(hook_node_info, &hook);
288 }
289
290 static void
291 unregister_vcg_hook(void)
292 {
293         unregister_hook(hook_node_info, &hook);
294 }
295
296 /**
297  * Reads the corresponding profile info file if it exists and returns a
298  * profile info struct
299  */
300 void
301 be_profile_read(char * filename)
302 {
303         FILE   *f;
304         char    buf[8];
305         size_t  ret;
306
307         f = fopen(filename, "r");
308         if(f == NULL) {
309                 return;
310         }
311         printf("found profile data.\n");
312
313         /* check magic */
314         ret = fread(buf, 8, 1, f);
315         if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
316                 return;
317         }
318
319         if(profile) be_profile_free();
320         profile = new_set(cmp_execcount, 16);
321
322         do {
323                 execcount_t  query;
324                 ret = fread(&query, sizeof(unsigned int), 2, f);
325
326                 if(ret != 2) break;
327
328                 set_insert(profile, &query, sizeof(query), query.block);
329         } while(1);
330
331         fclose(f);
332         register_vcg_hook();
333 }
334
335 /**
336  * Frees the profile info
337  */
338 void
339 be_profile_free(void)
340 {
341         if(profile) {
342                 unregister_vcg_hook();
343                 del_set(profile);
344         }
345 }
346
347 /**
348  * Tells whether profile module has aquired data
349  */
350 int
351 be_profile_has_data(void)
352 {
353         return (profile != NULL);
354 }
355
356 /**
357  * Get block execution count as determined be profiling
358  */
359 unsigned int
360 be_profile_get_block_execcount(const ir_node * block)
361 {
362         execcount_t *ec, query;
363
364         if(!profile)
365                 return 1;
366
367         query.block = get_irn_node_nr(block);
368         ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
369
370         if(ec) {
371                 return ec->count;
372         } else {
373                 return 1;
374         }
375 }