added missing header
[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, *unknown;
105         ir_node  *cnst;
106
107         if(bb == start_block || bb == get_irg_end_block(irg))
108                 return;
109
110         unknown = new_r_Unknown(irg, mode_M);
111         cnst    = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
112         offset  = new_r_Add(irg, bb, address, cnst, mode_P);
113         load    = new_r_Load(irg, bb, unknown, offset, mode_Iu);
114         projm   = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
115         proji   = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
116         cnst    = new_r_Const_long(irg, start_block, mode_Iu, 1);
117         add     = new_r_Add(irg, bb, proji, cnst, mode_Iu);
118         store   = new_r_Store(irg, bb, projm, offset, add);
119         projm   = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
120         set_irn_link(bb, projm);
121         set_irn_link(projm, load);
122 }
123
124 /**
125  * SSA Construction for instumenation code memory
126  */
127 static void
128 fix_ssa(ir_node * bb, void * data)
129 {
130         ir_node  *mem;
131         int       arity = get_Block_n_cfgpreds(bb);
132
133         /* start and end block are not instrumented, skip! */
134         if(bb == get_irg_end_block(current_ir_graph) || bb == get_irg_start_block(current_ir_graph))
135                 return;
136
137         /* first block gets NoMem */
138         if(get_Block_cfgpred_block(bb, 0) == get_irg_start_block(current_ir_graph)) {
139                 mem = new_NoMem();
140         } else {
141                 if(arity == 1) {
142                         mem = get_irn_link(get_Block_cfgpred_block(bb, 0));
143                 } else {
144                         int n;
145                         ir_node **ins;
146
147                         NEW_ARR_A(ir_node*, ins, arity);
148                         for(n=arity-1; n>=0; --n) {
149                                 ins[n]=get_irn_link(get_Block_cfgpred_block(bb, n));
150                         }
151                         mem = new_r_Phi(get_irn_irg(bb), bb, arity, ins, mode_M);
152                 }
153         }
154         set_Load_mem(get_irn_link(get_irn_link(bb)), mem);
155 }
156
157
158 /**
159  * Generates a new irg which calls the initializer
160  */
161 static ir_graph *
162 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
163 {
164         ir_node *start_block;
165
166         ir_node   *ins[4];
167         ident     *name = new_id_from_str("__firmprof_initializer");
168         entity    *ent  = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
169         ir_node   *ret, *call, *symconst;
170         symconst_symbol sym;
171
172         ident     *init_name = new_id_from_str("__init_firmprof");
173         ir_type   *init_type = new_type_method(init_name, 4, 0);
174         ir_type   *uint, *uintptr, *string;
175         entity    *init_ent;
176         ir_graph  *irg;
177         ir_node   *bb;
178         ir_type   *empty_frame_type;
179
180         set_entity_ld_ident(ent, name);
181
182         uint    = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
183         uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
184         string  = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
185
186         set_method_param_type(init_type, 0, string);
187         set_method_param_type(init_type, 1, uintptr);
188         set_method_param_type(init_type, 2, uintptr);
189         set_method_param_type(init_type, 3, uint);
190         init_ent = new_entity(get_glob_type(), init_name, init_type);
191         set_entity_ld_ident(init_ent, init_name);
192
193         irg = new_ir_graph(ent, 0);
194         set_current_ir_graph(irg);
195         empty_frame_type = get_irg_frame_type(irg);
196         set_type_size_bytes(empty_frame_type, 0);
197
198         bb = get_cur_block();
199
200         start_block = get_irg_start_block(irg);
201
202         sym.entity_p = init_ent;
203         symconst     = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
204
205         sym.entity_p = ent_filename;
206         ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
207         sym.entity_p = bblock_id;
208         ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
209         sym.entity_p = bblock_counts;
210         ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
211         ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
212
213         call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
214         ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
215         mature_immBlock(bb);
216
217         add_immBlock_pred(get_irg_end_block(irg), ret);
218         mature_immBlock(get_irg_end_block(irg));
219
220         irg_finalize_cons(irg);
221
222         return irg;
223 }
224
225 static void
226 block_id_walker(ir_node * bb, void * data)
227 {
228         block_id_walker_data_t *wd = data;
229
230         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
231         instrument_block(bb, wd->symconst, wd->id);
232         ++wd->id;
233 }
234
235 ir_graph *
236 be_profile_instrument(char * filename)
237 {
238         int            n, i;
239         unsigned int   n_blocks = 0;
240         entity        *bblock_id, *bblock_counts, *ent_filename;
241         ir_type       *array_type, *integer_type, *string_type, *character_type;
242         tarval       **tarval_array, **tarval_string;
243         int            filename_len = strlen(filename)+1;
244         ident         *cur_ident;
245
246         block_id_walker_data_t  wd;
247         symconst_symbol sym;
248
249         integer_type   = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
250         array_type     = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
251         set_array_bounds_int(array_type, 0, 0, n_blocks);
252
253         character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
254         string_type    = new_type_array(new_id_from_str("__filename"), 1, character_type);
255         set_array_bounds_int(string_type, 0, 0, filename_len);
256
257         cur_ident      = new_id_from_str("__FIRMPROF__BLOCK_IDS");
258         bblock_id      = new_entity(get_glob_type(), cur_ident, array_type);
259         set_entity_ld_ident(bblock_id, cur_ident);
260         set_entity_variability(bblock_id, variability_initialized);
261
262         cur_ident      = new_id_from_str("__FIRMPROF__BLOCK_COUNTS");
263         bblock_counts  = new_entity(get_glob_type(), cur_ident, array_type);
264         set_entity_ld_ident(bblock_counts, cur_ident);
265         set_entity_variability(bblock_counts, variability_initialized);
266
267         cur_ident      = new_id_from_str("__FIRMPROF__FILE_NAME");
268         ent_filename   = new_entity(get_glob_type(), cur_ident, string_type);
269         set_entity_ld_ident(ent_filename, cur_ident);
270         set_entity_variability(ent_filename, variability_initialized);
271
272         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
273                 ir_graph *irg = get_irp_irg(n);
274
275                 n_blocks += count_blocks(irg);
276         }
277
278         /* initialize count array */
279         tarval_array = alloca(sizeof(*tarval_array) * n_blocks);
280         for (i = 0; i < n_blocks; ++i) {
281                 tarval_array[i] = get_tarval_null(mode_Iu);
282         }
283         set_array_entity_values(bblock_counts, tarval_array, n_blocks);
284
285         /* initialize function name string constant */
286         tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
287         for (i = 0; i < filename_len; ++i) {
288                 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
289         }
290         set_array_entity_values(ent_filename, tarval_string, filename_len);
291
292
293         /* initialize block id array and instrument blocks */
294         wd.array = tarval_array;
295         wd.id    = 0;
296         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
297                 ir_graph      *irg = get_irp_irg(n);
298                 int            i;
299                 ir_node       *endbb = get_irg_end_block(irg);
300
301                 set_current_ir_graph(irg);
302
303                 /* generate a symbolic constant pointing to the count array */
304                 sym.entity_p = bblock_counts;
305                 wd.symconst  = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
306
307                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
308                 irg_block_walk_graph(irg, fix_ssa, NULL, NULL);
309                 for(i=get_Block_n_cfgpreds(endbb)-1; i>=0; --i) {
310                         ir_node *ret = get_Block_cfgpred(endbb, i);
311                         ir_node *bb  = get_Block_cfgpred_block(endbb, i);
312                         ir_node *sync;
313                         ir_node *ins[2];
314
315                         ins[0] = get_irn_link(bb);
316                         ins[1] = get_Return_mem(ret);
317                         sync   = new_r_Sync(irg, bb, 2, ins);
318
319                         set_Return_mem(ret, sync);
320                 }
321         }
322         set_array_entity_values(bblock_id, tarval_array, n_blocks);
323
324         return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
325 }
326
327 static void
328 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
329 {
330         if(is_Block(irn)) {
331                 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
332         }
333 }
334
335 static void
336 register_vcg_hook(void)
337 {
338         memset(&hook, 0, sizeof(hook));
339         hook.hook._hook_node_info = profile_node_info;
340         register_hook(hook_node_info, &hook);
341 }
342
343 static void
344 unregister_vcg_hook(void)
345 {
346         unregister_hook(hook_node_info, &hook);
347 }
348
349 /**
350  * Reads the corresponding profile info file if it exists and returns a
351  * profile info struct
352  */
353 void
354 be_profile_read(char * filename)
355 {
356         FILE   *f;
357         char    buf[8];
358         size_t  ret;
359
360         f = fopen(filename, "r");
361         if(f == NULL) {
362                 return;
363         }
364         printf("found profile data.\n");
365
366         /* check magic */
367         ret = fread(buf, 8, 1, f);
368         if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
369                 return;
370         }
371
372         if(profile) be_profile_free();
373         profile = new_set(cmp_execcount, 16);
374
375         do {
376                 execcount_t  query;
377                 ret = fread(&query, sizeof(unsigned int), 2, f);
378
379                 if(ret != 2) break;
380
381                 set_insert(profile, &query, sizeof(query), query.block);
382         } while(1);
383
384         fclose(f);
385         register_vcg_hook();
386 }
387
388 /**
389  * Frees the profile info
390  */
391 void
392 be_profile_free(void)
393 {
394         if(profile) {
395                 unregister_vcg_hook();
396                 del_set(profile);
397         }
398 }
399
400 /**
401  * Tells whether profile module has aquired data
402  */
403 int
404 be_profile_has_data(void)
405 {
406         return (profile != NULL);
407 }
408
409 /**
410  * Get block execution count as determined be profiling
411  */
412 unsigned int
413 be_profile_get_block_execcount(const ir_node * block)
414 {
415         execcount_t *ec, query;
416
417         if(!profile)
418                 return 1;
419
420         query.block = get_irn_node_nr(block);
421         ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
422
423         if(ec) {
424                 return ec->count;
425         } else {
426                 return 1;
427         }
428 }