forgot to close file
[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
48 #include "bechordal_t.h"
49
50 #ifdef WITH_LIBCORE
51 #include <libcore/lc_opts.h>
52 #include <libcore/lc_opts_enum.h>
53 #endif /* WITH_LIBCORE */
54
55 #include "beprofile.h"
56
57 typedef struct _block_id_walker_data_t {
58         tarval        **array;
59         unsigned int    id;
60         ir_node *symconst;
61 } block_id_walker_data_t;
62
63 typedef struct _execcount_t {
64         unsigned int block;
65         unsigned int count;
66 } execcount_t;
67
68 static int
69 cmp_execcount(const void * a, const void * b, size_t size)
70 {
71         return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
72 }
73
74 static void
75 block_counter(ir_node * bb, void * data)
76 {
77         unsigned int  *count = data;
78         *count = *count + 1;
79
80 }
81
82 static unsigned int
83 count_blocks(ir_graph * irg)
84 {
85         unsigned int count = 0;
86
87         irg_block_walk_graph(irg, block_counter, NULL, &count);
88         return count;
89 }
90
91 /* keep the execcounts here because they are only read once per compiler run */
92 static set * profile = NULL;
93
94 /**
95  * Instrument a block with code needed for profiling
96  */
97 static void
98 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
99 {
100         ir_graph *irg = get_irn_irg(bb);
101         ir_node *start_block = get_irg_start_block(irg);
102         ir_node  *load, *store, *offset, *add, *projm, *proji;
103         ir_node *cnst;
104
105         if(bb == start_block || bb == get_irg_end_block(irg))
106                 return;
107
108         cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
109         offset = new_r_Add(irg, bb, address, cnst, mode_P);
110         load = new_r_Load(irg, bb, new_NoMem(), offset, mode_Iu);
111         projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
112         proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
113         cnst =  new_r_Const_long(irg, start_block, mode_Iu, 1);
114         add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
115         store = new_r_Store(irg, bb, projm, offset, add);
116         projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
117         keep_alive(projm);
118 }
119
120 /**
121  * Generates a new irg which calls the initializer
122  */
123 static ir_graph *
124 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
125 {
126         ir_node *start_block;
127
128         ir_node   *ins[4];
129         ident     *name = new_id_from_str("__firmprof_initializer");
130         entity    *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
131         ir_node   *ret, *call, *symconst;
132         symconst_symbol sym;
133
134         ident     *init_name = new_id_from_str("__init_firmprof");
135         ir_type   *init_type = new_type_method(init_name, 4, 0);
136         ir_type   *uint, *uintptr, *string;
137         entity    *init_ent;
138         ir_graph  *irg;
139         ir_node   *bb;
140
141         uint    = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
142         uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
143         string = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
144
145         set_method_param_type(init_type, 0, string);
146         set_method_param_type(init_type, 1, uintptr);
147         set_method_param_type(init_type, 2, uintptr);
148         set_method_param_type(init_type, 3, uint);
149         init_ent = new_entity(get_glob_type(), init_name, init_type);
150
151         irg = new_ir_graph(ent, 0);
152         set_current_ir_graph(irg);
153
154         bb = get_cur_block();
155
156         start_block = get_irg_start_block(irg);
157
158         sym.entity_p = init_ent;
159         symconst     = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
160
161         sym.entity_p = ent_filename;
162         ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
163         sym.entity_p = bblock_id;
164         ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
165         sym.entity_p = bblock_counts;
166         ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
167         ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
168
169         call = new_r_Call( irg,
170                         bb,                                                     //ir_node *     block,
171                         get_irg_initial_mem(irg),       //ir_node *     store,
172                         symconst,                                       //ir_node *     callee,
173                         3,                                                      //int   arity,
174                         ins,                                            //ir_node **    in,
175                         init_type                                       //ir_type *     tp
176                         );
177
178         ret = new_r_Return ( irg,
179                         bb,                                                                             //ir_node *     block,
180                         new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular),   //ir_node *     store,
181                         0,                                                                              //int   arity,
182                         NULL                                                                    //ir_node **    in
183                         );
184
185         mature_immBlock(bb);
186
187         add_immBlock_pred(get_irg_end_block(irg), ret);
188         mature_immBlock(get_irg_end_block(irg));
189
190         irg_finalize_cons(irg);
191
192         return irg;
193 }
194
195 static void
196 block_id_walker(ir_node * bb, void * data)
197 {
198         block_id_walker_data_t *wd = data;
199
200         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
201         instrument_block(bb, wd->symconst, wd->id);
202         ++wd->id;
203 }
204
205 ir_graph *
206 be_profile_instrument(void)
207 {
208         ir_graph      *const_irg = get_const_code_irg();
209         ir_node       *const_block = get_irg_start_block(const_irg);
210         int            n, i;
211         unsigned int   n_blocks = 0;
212         entity        *bblock_id, *bblock_counts, *bblock_count, *ent_filename;
213         ir_type       *array_type, *integer_type, *string_type, *character_type;
214         tarval       **tarval_array, **tarval_string;
215         char          *filename = "test.c"; //FIXME
216         int            filename_len = strlen(filename)+1;
217
218         block_id_walker_data_t  wd;
219         symconst_symbol sym;
220
221         integer_type = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
222         array_type = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
223         set_array_bounds_int(array_type, 0, 0, n_blocks);
224         character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
225         string_type = new_type_array(new_id_from_str("__function_name"), 1, character_type);
226         set_array_bounds_int(string_type, 0, 0, filename_len);
227         bblock_id = new_entity(get_glob_type(), new_id_from_str("__BLOCK_IDS"), array_type);
228         set_entity_variability(bblock_id, variability_initialized);
229         bblock_counts = new_entity(get_glob_type(), new_id_from_str("__BLOCK_COUNTS"), array_type);
230         set_entity_variability(bblock_counts, variability_initialized);
231         bblock_count = new_entity(get_glob_type(), new_id_from_str("__N_BLOCKS"), integer_type);
232         set_entity_variability(bblock_count, variability_initialized);
233         ent_filename = new_entity(get_glob_type(), new_id_from_str("__FUNCTION_NAME"), string_type);
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 the block count entity */
250         set_atomic_ent_value(bblock_count, new_r_Const_long(const_irg, const_block, mode_Iu, n_blocks));
251
252         /* initialize function name string constant */
253         tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
254         for(i = 0; i < filename_len; ++i) {
255                 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
256         }
257         set_array_entity_values(ent_filename, tarval_string, filename_len);
258
259
260         /* initialize block id array and instrument blocks */
261         wd.array = tarval_array;
262         wd.id = 0;
263         for (n = get_irp_n_irgs()-1; n>=0; --n) {
264                 ir_graph      *irg = get_irp_irg(n);
265
266                 /* generate a symbolic constant pointing to the count array */
267                 sym.entity_p = bblock_count;
268                 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
269
270                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
271         }
272         set_array_entity_values(bblock_id, tarval_array, n_blocks);
273
274         return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
275 }
276
277
278 /**
279  * Reads the corresponding profile info file if it exists and returns a
280  * profile info struct
281  */
282 void
283 be_profile_read(char * filename)
284 {
285         FILE   *f;
286         char    buf[8];
287         size_t  ret;
288
289         f = fopen(filename, "r");
290         if(f == NULL) {
291                 perror("opening of profile data failed");
292                 return;
293         }
294
295         /* check magic */
296         ret = fread(buf, 8, 1, f);
297         if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
298                 return;
299         }
300
301         if(profile) be_profile_free();
302         profile = new_set(cmp_execcount, 16);
303
304         do {
305                 execcount_t  query;
306                 ret = fread(&query, sizeof(unsigned int), 2, f);
307
308                 if(ret != 2) break;
309
310                 set_insert(profile, &query, sizeof(query), query.block);
311         } while(1);
312
313         fclose(f);
314 }
315
316 /**
317  * Frees the profile info
318  */
319 void
320 be_profile_free(void)
321 {
322         if(profile)
323                 del_set(profile);
324 }
325
326 /**
327  * Get block execution count as determined be profiling
328  */
329 unsigned int
330 be_profile_get_block_execcount(const ir_node * block)
331 {
332         execcount_t *ec, query;
333
334         if(!profile)
335                 return 1;
336
337         query.block = get_irn_node_nr(block);
338         ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
339
340         if(ec) {
341                 return ec->count;
342         } else {
343                 return 1;
344         }
345 }