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 /** An entry in the id-to-location map */
60 typedef struct loc_entry {
61 entity *fname; /**< the entity holding the file name */
62 unsigned int lineno; /**< line number */
65 typedef struct _block_id_walker_data_t {
66 tarval **array; /**< the entity the holds the block counts */
67 unsigned int id; /**< current block id number */
68 ir_node *symconst; /**< the SymConst representing array */
69 pmap *fname_map; /**< set containing all found filenames */
70 loc_entry *locs; /**< locations */
71 ir_type *tp_char; /**< the character type */
72 unsigned flags; /**< profile flags */
73 } block_id_walker_data_t;
75 typedef struct _execcount_t {
81 cmp_execcount(const void * a, const void * b, size_t size)
83 return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
87 block_counter(ir_node * bb, void * data)
89 unsigned int *count = data;
94 count_blocks(ir_graph * irg)
96 unsigned int count = 0;
98 irg_block_walk_graph(irg, block_counter, NULL, &count);
102 /* keep the execcounts here because they are only read once per compiler run */
103 static set * profile = NULL;
104 static hook_entry_t hook;
107 * Instrument a block with code needed for profiling
110 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
112 ir_graph *irg = get_irn_irg(bb);
113 ir_node *start_block = get_irg_start_block(irg);
114 ir_node *load, *store, *offset, *add, *projm, *proji, *unknown;
118 * We can't instrument the start and end block as there are no real
119 * instructions in these blocks
121 if(bb == start_block || bb == get_irg_end_block(irg))
124 unknown = new_r_Unknown(irg, mode_M);
125 cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
126 offset = new_r_Add(irg, bb, address, cnst, mode_P);
127 load = new_r_Load(irg, bb, unknown, offset, mode_Iu);
128 projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
129 proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
130 cnst = new_r_Const_long(irg, start_block, mode_Iu, 1);
131 add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
132 store = new_r_Store(irg, bb, projm, offset, add);
133 projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
134 set_irn_link(bb, projm);
135 set_irn_link(projm, load);
138 typedef struct fix_env {
139 ir_node *start_block;
144 * SSA Construction for instrumentation code memory
147 fix_ssa(ir_node * bb, void * data)
151 int arity = get_Block_n_cfgpreds(bb);
153 /* start and end block are not instrumented, skip! */
154 if (bb == env->start_block || bb == env->end_block)
158 mem = get_irn_link(get_Block_cfgpred_block(bb, 0));
162 ir_graph *irg = current_ir_graph;
164 NEW_ARR_A(ir_node*, ins, arity);
165 for (n = arity - 1; n >= 0; --n) {
166 ins[n] = get_irn_link(get_Block_cfgpred_block(bb, n));
168 mem = new_r_Phi(irg, bb, arity, ins, mode_M);
170 set_Load_mem(get_irn_link(get_irn_link(bb)), mem);
175 * Generates a new irg which calls the initializer
178 * void __firmprof_initializer(void) { __init_firmprof(ent_filename, bblock_id, bblock_counts, n_blocks); }
181 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
183 ir_node *start_block;
186 ident *name = new_id_from_str("__firmprof_initializer");
187 entity *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
188 ir_node *ret, *call, *symconst;
191 ident *init_name = new_id_from_str("__init_firmprof");
192 ir_type *init_type = new_type_method(init_name, 4, 0);
193 ir_type *uint, *uintptr, *string;
197 ir_type *empty_frame_type;
199 set_entity_ld_ident(ent, name);
201 uint = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
202 uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
203 string = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
205 set_method_param_type(init_type, 0, string);
206 set_method_param_type(init_type, 1, uintptr);
207 set_method_param_type(init_type, 2, uintptr);
208 set_method_param_type(init_type, 3, uint);
209 init_ent = new_entity(get_glob_type(), init_name, init_type);
210 set_entity_ld_ident(init_ent, init_name);
212 irg = new_ir_graph(ent, 0);
213 empty_frame_type = get_irg_frame_type(irg);
214 set_type_size_bytes(empty_frame_type, 0);
216 bb = get_cur_block();
218 start_block = get_irg_start_block(irg);
220 sym.entity_p = init_ent;
221 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
223 sym.entity_p = ent_filename;
224 ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
225 sym.entity_p = bblock_id;
226 ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
227 sym.entity_p = bblock_counts;
228 ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
229 ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
231 call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
232 ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
235 add_immBlock_pred(get_irg_end_block(irg), ret);
236 mature_immBlock(get_irg_end_block(irg));
238 irg_finalize_cons(irg);
244 * Create the location data for the given debug info.
246 static void create_location_data(dbg_info *dbg, block_id_walker_data_t *wd)
249 const char *fname = be_retrieve_dbg_info(dbg, &lineno);
252 pmap_entry *entry = pmap_find(wd->fname_map, (void *)fname);
256 static unsigned nr = 0;
260 int i, len = strlen(fname) + 1;
261 tarval **tarval_string;
263 snprintf(buf, sizeof(buf), "firm_name_arr.%d", nr);
264 arr = new_type_array(new_id_from_str(buf), 1, wd->tp_char);
265 set_array_bounds_int(arr, 0, 0, len);
267 snprintf(buf, sizeof(buf), "__firm_name.%d", nr++);
268 id = new_id_from_str(buf);
269 ent = new_entity(get_glob_type(), id, arr);
270 set_entity_ld_ident(ent, id);
272 pmap_insert(wd->fname_map, (void *)fname, ent);
274 /* initialize file name string constant */
275 tarval_string = alloca(sizeof(*tarval_string) * (len));
276 for (i = 0; i < len; ++i) {
277 tarval_string[i] = new_tarval_from_long(fname[i], mode_Bs);
279 set_entity_variability(ent, variability_constant);
280 set_array_entity_values(ent, tarval_string, len);
284 wd->locs[wd->id].fname = ent;
285 wd->locs[wd->id].lineno = lineno;
287 wd->locs[wd->id].fname = NULL;
288 wd->locs[wd->id].lineno = 0;
293 * Walker: assigns an ID to every block.
294 * Builds the string table
297 block_id_walker(ir_node * bb, void * data)
299 block_id_walker_data_t *wd = data;
301 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
302 instrument_block(bb, wd->symconst, wd->id);
304 if (wd->flags & profile_with_locations) {
305 dbg_info *dbg = get_irn_dbg_info(bb);
306 create_location_data(dbg, wd);
311 #define IDENT(x) new_id_from_chars(x, sizeof(x) - 1)
314 be_profile_instrument(const char *filename, unsigned flags)
317 unsigned int n_blocks = 0;
318 entity *bblock_id, *bblock_counts, *ent_filename, *ent_locations,
319 *loc_lineno, *loc_name, *ent;
320 ir_type *array_type, *uint_type, *string_type, *character_type,
321 *loc_type, *charptr_type, *gtp;
322 tarval **tarval_array, **tarval_string, *tv;
323 int filename_len = strlen(filename)+1;
325 int align_l, align_n, size;
328 block_id_walker_data_t wd;
331 /* count the number of block first */
332 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
333 ir_graph *irg = get_irp_irg(n);
335 n_blocks += count_blocks(irg);
338 /* create all the necessary types and entities. Note that the
339 types must have a fixed layout, because we already running in the
341 uint_type = new_type_primitive(IDENT("__uint"), mode_Iu);
342 set_type_alignment_bytes(uint_type, get_type_size_bytes(uint_type));
343 array_type = new_type_array(IDENT("__block_info_array"), 1, uint_type);
344 set_array_bounds_int(array_type, 0, 0, n_blocks);
346 character_type = new_type_primitive(IDENT("__char"), mode_Bs);
347 string_type = new_type_array(IDENT("__filename"), 1, character_type);
348 set_array_bounds_int(string_type, 0, 0, filename_len);
350 gtp = get_glob_type();
352 cur_ident = IDENT("__FIRMPROF__BLOCK_IDS");
353 bblock_id = new_entity(gtp, cur_ident, array_type);
354 set_entity_ld_ident(bblock_id, cur_ident);
355 set_entity_variability(bblock_id, variability_initialized);
357 cur_ident = IDENT("__FIRMPROF__BLOCK_COUNTS");
358 bblock_counts = new_entity(gtp, cur_ident, array_type);
359 set_entity_ld_ident(bblock_counts, cur_ident);
360 set_entity_variability(bblock_counts, variability_initialized);
362 cur_ident = IDENT("__FIRMPROF__FILE_NAME");
363 ent_filename = new_entity(gtp, cur_ident, string_type);
364 set_entity_ld_ident(ent_filename, cur_ident);
366 if (flags & profile_with_locations) {
367 loc_type = new_type_struct(IDENT("__location"));
368 loc_lineno = new_entity(loc_type, IDENT("lineno"), uint_type);
369 align_l = get_type_alignment_bytes(uint_type);
370 size = get_type_size_bytes(uint_type);
371 set_entity_offset_bytes(loc_lineno, 0);
373 charptr_type = new_type_pointer(IDENT("__charptr"), character_type, mode_P_data);
374 align_n = get_type_size_bytes(charptr_type);
375 set_type_alignment_bytes(charptr_type, align_n);
376 loc_name = new_entity(loc_type, IDENT("name"), charptr_type);
377 size = (size + align_n - 1) & -align_n;
378 set_entity_offset_bytes(loc_name, size);
381 if (align_n > align_l)
383 size = (size + align_l - 1) & -align_l;
384 set_type_size_bytes(loc_type, size);
385 set_type_state(loc_type, layout_fixed);
387 loc_type = new_type_array(IDENT("__locarray"), 1, loc_type);
388 set_array_bounds_int(string_type, 0, 0, n_blocks);
390 cur_ident = IDENT("__FIRMPROF__LOCATIONS");
391 ent_locations = new_entity(gtp, cur_ident, loc_type);
392 set_entity_ld_ident(ent_locations, cur_ident);
395 /* initialize count array */
396 NEW_ARR_A(tarval *, tarval_array, n_blocks);
397 tv = get_tarval_null(mode_Iu);
398 for (i = 0; i < n_blocks; ++i) {
399 tarval_array[i] = tv;
401 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
403 /* initialize function name string constant */
404 tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
405 for (i = 0; i < filename_len; ++i) {
406 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
408 set_entity_variability(ent_filename, variability_constant);
409 set_array_entity_values(ent_filename, tarval_string, filename_len);
411 /* initialize block id array and instrument blocks */
412 wd.array = tarval_array;
414 wd.tp_char = character_type;
416 if (flags & profile_with_locations) {
417 wd.fname_map = pmap_create();
418 NEW_ARR_A(loc_entry, wd.locs, n_blocks);
421 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
422 ir_graph *irg = get_irp_irg(n);
424 ir_node *endbb = get_irg_end_block(irg);
427 set_current_ir_graph(irg);
429 /* generate a symbolic constant pointing to the count array */
430 sym.entity_p = bblock_counts;
431 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
433 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
434 env.start_block = get_irg_start_block(irg);
435 env.end_block = get_irg_end_block(irg);
436 set_irn_link(env.start_block, get_irg_no_mem(irg));
437 irg_block_walk_graph(irg, fix_ssa, NULL, &env);
438 for (i = get_Block_n_cfgpreds(endbb) - 1; i >= 0; --i) {
439 ir_node *node = skip_Proj(get_Block_cfgpred(endbb, i));
440 ir_node *bb = get_Block_cfgpred_block(endbb, i);
444 switch (get_irn_opcode(node)) {
446 ins[0] = get_irn_link(bb);
447 ins[1] = get_Return_mem(node);
448 sync = new_r_Sync(irg, bb, 2, ins);
449 set_Return_mem(node, sync);
452 ins[0] = get_irn_link(bb);
453 ins[1] = get_Raise_mem(node);
454 sync = new_r_Sync(irg, bb, 2, ins);
455 set_Raise_mem(node, sync);
458 /* a fragile's op exception. There should be another path to End,
460 assert(is_fragile_op(node) && "unexpected End control flow predecessor");
464 set_array_entity_values(bblock_id, tarval_array, n_blocks);
466 if (flags & profile_with_locations) {
467 /* build the initializer for the locations */
468 rem = current_ir_graph;
469 current_ir_graph = get_const_code_irg();
470 ent = get_array_element_entity(loc_type);
471 set_entity_variability(ent_locations, variability_constant);
472 for (i = 0; i < n_blocks; ++i) {
473 compound_graph_path *path;
478 path = new_compound_graph_path(loc_type, 2);
479 set_compound_graph_path_array_index(path, 0, i);
480 set_compound_graph_path_node(path, 0, ent);
481 set_compound_graph_path_node(path, 1, loc_lineno);
482 tv = new_tarval_from_long(wd.locs[i].lineno, mode_Iu);
483 add_compound_ent_value_w_path(ent_locations, new_Const(mode_Iu, tv), path);
486 path = new_compound_graph_path(loc_type, 2);
487 set_compound_graph_path_array_index(path, 0, i);
488 set_compound_graph_path_node(path, 0, ent);
489 set_compound_graph_path_node(path, 1, loc_name);
490 if (wd.locs[i].fname) {
491 sym.entity_p = wd.locs[i].fname;
492 n = new_SymConst(sym, symconst_addr_ent);
494 n = new_Const(mode_P_data, get_mode_null(mode_P_data));
496 add_compound_ent_value_w_path(ent_locations, n, path);
498 pmap_destroy(wd.fname_map);
500 return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
504 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
507 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
512 register_vcg_hook(void)
514 memset(&hook, 0, sizeof(hook));
515 hook.hook._hook_node_info = profile_node_info;
516 register_hook(hook_node_info, &hook);
520 unregister_vcg_hook(void)
522 unregister_hook(hook_node_info, &hook);
526 * Reads the corresponding profile info file if it exists and returns a
527 * profile info struct
530 be_profile_read(const char *filename)
536 f = fopen(filename, "r");
540 printf("found profile data '%s'.\n", filename);
543 ret = fread(buf, 8, 1, f);
544 if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
548 if(profile) be_profile_free();
549 profile = new_set(cmp_execcount, 16);
553 ret = fread(&query, sizeof(unsigned int), 2, f);
557 set_insert(profile, &query, sizeof(query), query.block);
565 * Frees the profile info
568 be_profile_free(void)
571 unregister_vcg_hook();
577 * Tells whether profile module has acquired data
580 be_profile_has_data(void)
582 return (profile != NULL);
586 * Get block execution count as determined be profiling
589 be_profile_get_block_execcount(const ir_node *block)
591 execcount_t *ec, query;
596 query.block = get_irn_node_nr(block);
597 ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
602 ir_fprintf(stderr, "Warning: Profile contains no data for %+F\n",
608 typedef struct _intialize_execfreq_env_t {
610 ir_exec_freq *execfreqs;
612 } initialize_execfreq_env_t;
614 // minimal execution frequency (an execfreq of 0 confuses algos)
615 static const double MIN_EXECFREQ = 0.00001;
617 static void initialize_execfreq(ir_node *block, void *data) {
618 initialize_execfreq_env_t *env = data;
621 if(block == get_irg_start_block(env->irg)
622 || block == get_irg_end_block(env->irg)) {
625 freq = be_profile_get_block_execcount(block);
626 freq *= env->freq_factor;
627 if(freq < MIN_EXECFREQ)
631 set_execfreq(env->execfreqs, block, freq);
634 ir_exec_freq *be_create_execfreqs_from_profile(ir_graph *irg)
636 ir_node *block2 = NULL;
637 ir_node *start_block;
638 const ir_edge_t *edge;
639 initialize_execfreq_env_t env;
643 env.execfreqs = create_execfreq(irg);
645 // find the successor to the start block
646 start_block = get_irg_start_block(irg);
647 foreach_block_succ(start_block, edge) {
648 ir_node *succ = get_edge_src_irn(edge);
649 if(succ != start_block) {
654 assert(block2 != NULL);
656 count = be_profile_get_block_execcount(block2);
658 // the function was never executed, so fallback to estimated freqs
659 free_execfreq(env.execfreqs);
661 return compute_execfreq(irg, 10);
664 env.freq_factor = 1.0 / count;
665 irg_block_walk_graph(irg, initialize_execfreq, NULL, &env);
667 return env.execfreqs;