1 /** vim: set sw=4 ts=4:
4 * @author Adam M. Szalkowski
7 * Code instrumentation and execution count profiling
9 * Copyright (C) 2006 Universitaet Karlsruhe
10 * Released under the GPL
40 #include "besched_t.h"
49 #include "bechordal_t.h"
52 #include <libcore/lc_opts.h>
53 #include <libcore/lc_opts_enum.h>
54 #endif /* WITH_LIBCORE */
56 #include "beprofile.h"
58 typedef struct _block_id_walker_data_t {
62 } block_id_walker_data_t;
64 typedef struct _execcount_t {
70 cmp_execcount(const void * a, const void * b, size_t size)
72 return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
76 block_counter(ir_node * bb, void * data)
78 unsigned int *count = data;
84 count_blocks(ir_graph * irg)
86 unsigned int count = 0;
88 irg_block_walk_graph(irg, block_counter, NULL, &count);
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;
97 * Instrument a block with code needed for profiling
100 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
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;
107 if(bb == start_block || bb == get_irg_end_block(irg))
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);
125 * SSA Construction for instumenation code memory
128 fix_ssa(ir_node * bb, void * data)
131 int arity = get_Block_n_cfgpreds(bb);
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))
137 /* first block gets NoMem */
138 if(get_Block_cfgpred_block(bb, 0) == get_irg_start_block(current_ir_graph)) {
142 mem = get_irn_link(get_Block_cfgpred_block(bb, 0));
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));
151 mem = new_r_Phi(get_irn_irg(bb), bb, arity, ins, mode_M);
154 set_Load_mem(get_irn_link(get_irn_link(bb)), mem);
159 * Generates a new irg which calls the initializer
162 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
164 ir_node *start_block;
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;
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;
178 ir_type *empty_frame_type;
180 set_entity_ld_ident(ent, name);
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);
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);
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);
198 bb = get_cur_block();
200 start_block = get_irg_start_block(irg);
202 sym.entity_p = init_ent;
203 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
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);
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);
217 add_immBlock_pred(get_irg_end_block(irg), ret);
218 mature_immBlock(get_irg_end_block(irg));
220 irg_finalize_cons(irg);
226 block_id_walker(ir_node * bb, void * data)
228 block_id_walker_data_t *wd = data;
230 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
231 instrument_block(bb, wd->symconst, wd->id);
236 be_profile_instrument(char * filename)
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;
246 block_id_walker_data_t wd;
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);
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);
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);
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);
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);
272 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
273 ir_graph *irg = get_irp_irg(n);
275 n_blocks += count_blocks(irg);
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);
283 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
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);
290 set_array_entity_values(ent_filename, tarval_string, filename_len);
293 /* initialize block id array and instrument blocks */
294 wd.array = tarval_array;
296 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
297 ir_graph *irg = get_irp_irg(n);
299 ir_node *endbb = get_irg_end_block(irg);
301 set_current_ir_graph(irg);
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);
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);
315 ins[0] = get_irn_link(bb);
316 ins[1] = get_Return_mem(ret);
317 sync = new_r_Sync(irg, bb, 2, ins);
319 set_Return_mem(ret, sync);
322 set_array_entity_values(bblock_id, tarval_array, n_blocks);
324 return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
328 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
331 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
336 register_vcg_hook(void)
338 memset(&hook, 0, sizeof(hook));
339 hook.hook._hook_node_info = profile_node_info;
340 register_hook(hook_node_info, &hook);
344 unregister_vcg_hook(void)
346 unregister_hook(hook_node_info, &hook);
350 * Reads the corresponding profile info file if it exists and returns a
351 * profile info struct
354 be_profile_read(char * filename)
360 f = fopen(filename, "r");
364 printf("found profile data.\n");
367 ret = fread(buf, 8, 1, f);
368 if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
372 if(profile) be_profile_free();
373 profile = new_set(cmp_execcount, 16);
377 ret = fread(&query, sizeof(unsigned int), 2, f);
381 set_insert(profile, &query, sizeof(query), query.block);
389 * Frees the profile info
392 be_profile_free(void)
395 unregister_vcg_hook();
401 * Tells whether profile module has aquired data
404 be_profile_has_data(void)
406 return (profile != NULL);
410 * Get block execution count as determined be profiling
413 be_profile_get_block_execcount(const ir_node * block)
415 execcount_t *ec, query;
420 query.block = get_irn_node_nr(block);
421 ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));