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"
50 #include "bechordal_t.h"
53 #include <libcore/lc_opts.h>
54 #include <libcore/lc_opts_enum.h>
55 #endif /* WITH_LIBCORE */
57 #include "beprofile.h"
59 typedef struct _block_id_walker_data_t {
63 } block_id_walker_data_t;
65 typedef struct _execcount_t {
71 cmp_execcount(const void * a, const void * b, size_t size)
73 return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
77 block_counter(ir_node * bb, void * data)
79 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;
108 * We can't instrument the start and end block as there are no real
109 * instructions in these blocks
111 if(bb == start_block || bb == get_irg_end_block(irg))
114 unknown = new_r_Unknown(irg, mode_M);
115 cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
116 offset = new_r_Add(irg, bb, address, cnst, mode_P);
117 load = new_r_Load(irg, bb, unknown, offset, mode_Iu);
118 projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
119 proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
120 cnst = new_r_Const_long(irg, start_block, mode_Iu, 1);
121 add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
122 store = new_r_Store(irg, bb, projm, offset, add);
123 projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
124 set_irn_link(bb, projm);
125 set_irn_link(projm, load);
128 typedef struct fix_env {
129 ir_node *start_block;
134 * SSA Construction for instrumentation code memory
137 fix_ssa(ir_node * bb, void * data)
141 int arity = get_Block_n_cfgpreds(bb);
143 /* start and end block are not instrumented, skip! */
144 if (bb == env->start_block || bb == env->end_block)
148 mem = get_irn_link(get_Block_cfgpred_block(bb, 0));
152 ir_graph *irg = current_ir_graph;
154 NEW_ARR_A(ir_node*, ins, arity);
155 for (n = arity - 1; n >= 0; --n) {
156 ins[n] = get_irn_link(get_Block_cfgpred_block(bb, n));
158 mem = new_r_Phi(irg, bb, arity, ins, mode_M);
160 set_Load_mem(get_irn_link(get_irn_link(bb)), mem);
165 * Generates a new irg which calls the initializer
168 * void __firmprof_initializer(void) { __init_firmprof(ent_filename, bblock_id, bblock_counts, n_blocks); }
171 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
173 ir_node *start_block;
176 ident *name = new_id_from_str("__firmprof_initializer");
177 entity *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
178 ir_node *ret, *call, *symconst;
181 ident *init_name = new_id_from_str("__init_firmprof");
182 ir_type *init_type = new_type_method(init_name, 4, 0);
183 ir_type *uint, *uintptr, *string;
187 ir_type *empty_frame_type;
189 set_entity_ld_ident(ent, name);
191 uint = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
192 uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
193 string = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
195 set_method_param_type(init_type, 0, string);
196 set_method_param_type(init_type, 1, uintptr);
197 set_method_param_type(init_type, 2, uintptr);
198 set_method_param_type(init_type, 3, uint);
199 init_ent = new_entity(get_glob_type(), init_name, init_type);
200 set_entity_ld_ident(init_ent, init_name);
202 irg = new_ir_graph(ent, 0);
203 empty_frame_type = get_irg_frame_type(irg);
204 set_type_size_bytes(empty_frame_type, 0);
206 bb = get_cur_block();
208 start_block = get_irg_start_block(irg);
210 sym.entity_p = init_ent;
211 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
213 sym.entity_p = ent_filename;
214 ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
215 sym.entity_p = bblock_id;
216 ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
217 sym.entity_p = bblock_counts;
218 ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
219 ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
221 call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
222 ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
225 add_immBlock_pred(get_irg_end_block(irg), ret);
226 mature_immBlock(get_irg_end_block(irg));
228 irg_finalize_cons(irg);
234 block_id_walker(ir_node * bb, void * data)
236 block_id_walker_data_t *wd = data;
238 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
239 instrument_block(bb, wd->symconst, wd->id);
244 be_profile_instrument(const char *filename)
247 unsigned int n_blocks = 0;
248 entity *bblock_id, *bblock_counts, *ent_filename;
249 ir_type *array_type, *integer_type, *string_type, *character_type;
250 tarval **tarval_array, **tarval_string, *tv;
251 int filename_len = strlen(filename)+1;
254 block_id_walker_data_t wd;
257 integer_type = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
258 array_type = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
259 set_array_bounds_int(array_type, 0, 0, n_blocks);
261 character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
262 string_type = new_type_array(new_id_from_str("__filename"), 1, character_type);
263 set_array_bounds_int(string_type, 0, 0, filename_len);
265 cur_ident = new_id_from_str("__FIRMPROF__BLOCK_IDS");
266 bblock_id = new_entity(get_glob_type(), cur_ident, array_type);
267 set_entity_ld_ident(bblock_id, cur_ident);
268 set_entity_variability(bblock_id, variability_initialized);
270 cur_ident = new_id_from_str("__FIRMPROF__BLOCK_COUNTS");
271 bblock_counts = new_entity(get_glob_type(), cur_ident, array_type);
272 set_entity_ld_ident(bblock_counts, cur_ident);
273 set_entity_variability(bblock_counts, variability_initialized);
275 cur_ident = new_id_from_str("__FIRMPROF__FILE_NAME");
276 ent_filename = new_entity(get_glob_type(), cur_ident, string_type);
277 set_entity_ld_ident(ent_filename, cur_ident);
279 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
280 ir_graph *irg = get_irp_irg(n);
282 n_blocks += count_blocks(irg);
285 /* initialize count array */
286 tarval_array = alloca(sizeof(*tarval_array) * n_blocks);
287 tv = get_tarval_null(mode_Iu);
288 for (i = 0; i < n_blocks; ++i) {
289 tarval_array[i] = tv;
291 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
293 /* initialize function name string constant */
294 tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
295 for (i = 0; i < filename_len; ++i) {
296 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
298 set_entity_variability(ent_filename, variability_constant);
299 set_array_entity_values(ent_filename, tarval_string, filename_len);
301 /* initialize block id array and instrument blocks */
302 wd.array = tarval_array;
304 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
305 ir_graph *irg = get_irp_irg(n);
307 ir_node *endbb = get_irg_end_block(irg);
310 set_current_ir_graph(irg);
312 /* generate a symbolic constant pointing to the count array */
313 sym.entity_p = bblock_counts;
314 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
316 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
317 env.start_block = get_irg_start_block(irg);
318 env.end_block = get_irg_end_block(irg);
319 set_irn_link(env.start_block, get_irg_no_mem(irg));
320 irg_block_walk_graph(irg, fix_ssa, NULL, &env);
321 for (i = get_Block_n_cfgpreds(endbb) - 1; i >= 0; --i) {
322 ir_node *node = skip_Proj(get_Block_cfgpred(endbb, i));
323 ir_node *bb = get_Block_cfgpred_block(endbb, i);
327 switch (get_irn_opcode(node)) {
329 ins[0] = get_irn_link(bb);
330 ins[1] = get_Return_mem(node);
331 sync = new_r_Sync(irg, bb, 2, ins);
332 set_Return_mem(node, sync);
335 ins[0] = get_irn_link(bb);
336 ins[1] = get_Raise_mem(node);
337 sync = new_r_Sync(irg, bb, 2, ins);
338 set_Raise_mem(node, sync);
341 /* a fragile's op exception. There should be another path to End,
343 assert(is_fragile_op(node) && "unexpected End control flow predecessor");
347 set_array_entity_values(bblock_id, tarval_array, n_blocks);
349 return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
353 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
356 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
361 register_vcg_hook(void)
363 memset(&hook, 0, sizeof(hook));
364 hook.hook._hook_node_info = profile_node_info;
365 register_hook(hook_node_info, &hook);
369 unregister_vcg_hook(void)
371 unregister_hook(hook_node_info, &hook);
375 * Reads the corresponding profile info file if it exists and returns a
376 * profile info struct
379 be_profile_read(const char *filename)
385 f = fopen(filename, "r");
389 printf("found profile data '%s'.\n", filename);
392 ret = fread(buf, 8, 1, f);
393 if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
397 if(profile) be_profile_free();
398 profile = new_set(cmp_execcount, 16);
402 ret = fread(&query, sizeof(unsigned int), 2, f);
406 set_insert(profile, &query, sizeof(query), query.block);
414 * Frees the profile info
417 be_profile_free(void)
420 unregister_vcg_hook();
426 * Tells whether profile module has acquired data
429 be_profile_has_data(void)
431 return (profile != NULL);
435 * Get block execution count as determined be profiling
438 be_profile_get_block_execcount(const ir_node *block)
440 execcount_t *ec, query;
445 query.block = get_irn_node_nr(block);
446 ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
451 ir_fprintf(stderr, "Warning: Profile contains no data for %+F\n",
457 typedef struct _intialize_execfreq_env_t {
459 exec_freq_t *execfreqs;
461 } initialize_execfreq_env_t;
463 static void initialize_execfreq(ir_node *block, void *data) {
464 initialize_execfreq_env_t *env = data;
467 if(block == get_irg_start_block(env->irg)
468 || block == get_irg_end_block(env->irg)) {
471 freq = be_profile_get_block_execcount(block);
472 freq *= env->freq_factor;
475 set_execfreq(env->execfreqs, block, freq);
478 exec_freq_t *be_create_execfreqs_from_profile(ir_graph *irg)
480 ir_node *block2 = NULL;
481 ir_node *start_block;
482 const ir_edge_t *edge;
483 initialize_execfreq_env_t env;
486 env.execfreqs = create_execfreq(irg);
488 // find the successor to the start block
489 start_block = get_irg_start_block(irg);
490 foreach_block_succ(start_block, edge) {
491 ir_node *succ = get_edge_src_irn(edge);
492 if(succ != start_block) {
497 assert(block2 != NULL);
499 count = be_profile_get_block_execcount(block2);
501 // the function was never executed, so fallback to estimated freqs
502 free_execfreq(env.execfreqs);
504 return compute_execfreq(irg, 10);
507 env.freq_factor = 1 / count;
508 irg_block_walk_graph(irg, initialize_execfreq, NULL, &env);
510 return env.execfreqs;