registering hook for exec count info in dumped graphs
[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,
178                         bb,                                                     //ir_node *     block,
179                         get_irg_initial_mem(irg),       //ir_node *     store,
180                         symconst,                                       //ir_node *     callee,
181                         4,                                                      //int   arity,
182                         ins,                                            //ir_node **    in,
183                         init_type                                       //ir_type *     tp
184                         );
185
186         ret = new_r_Return ( irg,
187                         bb,                                                                             //ir_node *     block,
188                         new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular),   //ir_node *     store,
189                         0,                                                                              //int   arity,
190                         NULL                                                                    //ir_node **    in
191                         );
192
193         mature_immBlock(bb);
194
195         add_immBlock_pred(get_irg_end_block(irg), ret);
196         mature_immBlock(get_irg_end_block(irg));
197
198         irg_finalize_cons(irg);
199
200         return irg;
201 }
202
203 static void
204 block_id_walker(ir_node * bb, void * data)
205 {
206         block_id_walker_data_t *wd = data;
207
208         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
209         instrument_block(bb, wd->symconst, wd->id);
210         ++wd->id;
211 }
212
213 ir_graph *
214 be_profile_instrument(void)
215 {
216         int            n, i;
217         unsigned int   n_blocks = 0;
218         entity        *bblock_id, *bblock_counts, *ent_filename;
219         ir_type       *array_type, *integer_type, *string_type, *character_type;
220         tarval       **tarval_array, **tarval_string;
221         char          *filename = "test.c"; //FIXME
222         int            filename_len = strlen(filename)+1;
223         ident         *cur_ident;
224
225         block_id_walker_data_t  wd;
226         symconst_symbol sym;
227
228         integer_type   = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
229         array_type     = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
230         set_array_bounds_int(array_type, 0, 0, n_blocks);
231
232         character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
233         string_type    = new_type_array(new_id_from_str("__function_name"), 1, character_type);
234         set_array_bounds_int(string_type, 0, 0, filename_len);
235
236         cur_ident      = new_id_from_str("__BLOCK_IDS");
237         bblock_id      = new_entity(get_glob_type(), cur_ident, array_type);
238         set_entity_ld_ident(bblock_id, cur_ident);
239         set_entity_variability(bblock_id, variability_initialized);
240
241         cur_ident      = new_id_from_str("__BLOCK_COUNTS");
242         bblock_counts  = new_entity(get_glob_type(), cur_ident, array_type);
243         set_entity_ld_ident(bblock_counts, cur_ident);
244         set_entity_variability(bblock_counts, variability_initialized);
245
246         cur_ident      = new_id_from_str("__FUNCTION_NAME");
247         ent_filename   = new_entity(get_glob_type(), cur_ident, string_type);
248         set_entity_ld_ident(ent_filename, cur_ident);
249         set_entity_variability(ent_filename, variability_initialized);
250
251         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
252                 ir_graph *irg = get_irp_irg(n);
253
254                 n_blocks += count_blocks(irg);
255         }
256
257         /* initialize count array */
258         tarval_array = alloca(sizeof(*tarval_array) * n_blocks);
259         for (i = 0; i < n_blocks; ++i) {
260                 tarval_array[i] = get_tarval_null(mode_Iu);
261         }
262         set_array_entity_values(bblock_counts, tarval_array, n_blocks);
263
264         /* initialize function name string constant */
265         tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
266         for (i = 0; i < filename_len; ++i) {
267                 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
268         }
269         set_array_entity_values(ent_filename, tarval_string, filename_len);
270
271
272         /* initialize block id array and instrument blocks */
273         wd.array = tarval_array;
274         wd.id    = 0;
275         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
276                 ir_graph *irg = get_irp_irg(n);
277
278                 /* generate a symbolic constant pointing to the count array */
279                 sym.entity_p = bblock_counts;
280                 wd.symconst  = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
281
282                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
283         }
284         set_array_entity_values(bblock_id, tarval_array, n_blocks);
285
286         return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
287 }
288
289 static void
290 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
291 {
292         if(is_Block(irn)) {
293                 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
294         }
295 }
296
297 static void
298 register_vcg_hook(void)
299 {
300         memset(&hook, 0, sizeof(hook));
301         hook.hook._hook_node_info = profile_node_info;
302         register_hook(hook_node_info, &hook);
303 }
304
305 static void
306 unregister_vcg_hook(void)
307 {
308         unregister_hook(hook_node_info, &hook);
309 }
310
311 /**
312  * Reads the corresponding profile info file if it exists and returns a
313  * profile info struct
314  */
315 void
316 be_profile_read(char * filename)
317 {
318         FILE   *f;
319         char    buf[8];
320         size_t  ret;
321
322         f = fopen(filename, "r");
323         if(f == NULL) {
324                 perror("opening of profile data failed");
325                 return;
326         }
327
328         /* check magic */
329         ret = fread(buf, 8, 1, f);
330         if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
331                 return;
332         }
333
334         if(profile) be_profile_free();
335         profile = new_set(cmp_execcount, 16);
336
337         do {
338                 execcount_t  query;
339                 ret = fread(&query, sizeof(unsigned int), 2, f);
340
341                 if(ret != 2) break;
342
343                 set_insert(profile, &query, sizeof(query), query.block);
344         } while(1);
345
346         fclose(f);
347         register_vcg_hook();
348 }
349
350 /**
351  * Frees the profile info
352  */
353 void
354 be_profile_free(void)
355 {
356         if(profile) {
357                 unregister_vcg_hook();
358                 del_set(profile);
359         }
360 }
361
362 /**
363  * Tells whether profile module has aquired data
364  */
365 int
366 be_profile_has_data(void)
367 {
368         return (profile != NULL);
369 }
370
371 /**
372  * Get block execution count as determined be profiling
373  */
374 unsigned int
375 be_profile_get_block_execcount(const ir_node * block)
376 {
377         execcount_t *ec, query;
378
379         if(!profile)
380                 return 1;
381
382         query.block = get_irn_node_nr(block);
383         ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
384
385         if(ec) {
386                 return ec->count;
387         } else {
388                 return 1;
389         }
390 }