2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Code instrumentation and execution count profiling.
23 * @author Adam M. Szalkowski
53 #include "besched_t.h"
63 #include "bechordal_t.h"
65 #include "beprofile.h"
67 /** An entry in the id-to-location map */
68 typedef struct loc_entry {
69 ir_entity *fname; /**< the entity holding the file name */
70 unsigned int lineno; /**< line number */
73 typedef struct _block_id_walker_data_t {
74 tarval **array; /**< the entity the holds the block counts */
75 unsigned int id; /**< current block id number */
76 ir_node *symconst; /**< the SymConst representing array */
77 pmap *fname_map; /**< set containing all found filenames */
78 loc_entry *locs; /**< locations */
79 ir_type *tp_char; /**< the character type */
80 unsigned flags; /**< profile flags */
81 } block_id_walker_data_t;
83 typedef struct _execcount_t {
89 cmp_execcount(const void * a, const void * b, size_t size)
92 return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
96 block_counter(ir_node * bb, void * data)
98 unsigned int *count = data;
104 count_blocks(ir_graph * irg)
106 unsigned int count = 0;
108 irg_block_walk_graph(irg, block_counter, NULL, &count);
112 /* keep the execcounts here because they are only read once per compiler run */
113 static set * profile = NULL;
114 static hook_entry_t hook;
117 * Instrument a block with code needed for profiling
120 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
122 ir_graph *irg = get_irn_irg(bb);
123 ir_node *start_block = get_irg_start_block(irg);
124 ir_node *load, *store, *offset, *add, *projm, *proji, *unknown;
128 * We can't instrument the start and end block as there are no real
129 * instructions in these blocks
131 if(bb == start_block || bb == get_irg_end_block(irg))
134 unknown = new_r_Unknown(irg, mode_M);
135 cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
136 offset = new_r_Add(irg, bb, address, cnst, mode_P);
137 load = new_r_Load(irg, bb, unknown, offset, mode_Iu);
138 projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
139 proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
140 cnst = new_r_Const_long(irg, start_block, mode_Iu, 1);
141 add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
142 store = new_r_Store(irg, bb, projm, offset, add);
143 projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
144 set_irn_link(bb, projm);
145 set_irn_link(projm, load);
148 typedef struct fix_env {
149 ir_node *start_block;
154 * SSA Construction for instrumentation code memory
157 fix_ssa(ir_node * bb, void * data)
161 int arity = get_Block_n_cfgpreds(bb);
163 /* start and end block are not instrumented, skip! */
164 if (bb == env->start_block || bb == env->end_block)
168 mem = get_irn_link(get_Block_cfgpred_block(bb, 0));
172 ir_graph *irg = current_ir_graph;
174 NEW_ARR_A(ir_node*, ins, arity);
175 for (n = arity - 1; n >= 0; --n) {
176 ins[n] = get_irn_link(get_Block_cfgpred_block(bb, n));
178 mem = new_r_Phi(irg, bb, arity, ins, mode_M);
180 set_Load_mem(get_irn_link(get_irn_link(bb)), mem);
185 * Generates a new irg which calls the initializer
188 * void __firmprof_initializer(void) { __init_firmprof(ent_filename, bblock_id, bblock_counts, n_blocks); }
191 gen_initializer_irg(ir_entity * ent_filename, ir_entity * bblock_id, ir_entity * bblock_counts, int n_blocks)
193 ir_node *start_block;
196 ident *name = new_id_from_str("__firmprof_initializer");
197 ir_entity *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
198 ir_node *ret, *call, *symconst;
201 ident *init_name = new_id_from_str("__init_firmprof");
202 ir_type *init_type = new_type_method(init_name, 4, 0);
203 ir_type *uint, *uintptr, *string;
207 ir_type *empty_frame_type;
209 set_entity_ld_ident(ent, name);
211 uint = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
212 uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
213 string = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
215 set_method_param_type(init_type, 0, string);
216 set_method_param_type(init_type, 1, uintptr);
217 set_method_param_type(init_type, 2, uintptr);
218 set_method_param_type(init_type, 3, uint);
219 init_ent = new_entity(get_glob_type(), init_name, init_type);
220 set_entity_ld_ident(init_ent, init_name);
222 irg = new_ir_graph(ent, 0);
223 empty_frame_type = get_irg_frame_type(irg);
224 set_type_size_bytes(empty_frame_type, 0);
226 bb = get_cur_block();
228 start_block = get_irg_start_block(irg);
230 sym.entity_p = init_ent;
231 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
233 sym.entity_p = ent_filename;
234 ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
235 sym.entity_p = bblock_id;
236 ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
237 sym.entity_p = bblock_counts;
238 ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
239 ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
241 call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
242 ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
245 add_immBlock_pred(get_irg_end_block(irg), ret);
246 mature_immBlock(get_irg_end_block(irg));
248 irg_finalize_cons(irg);
254 * Create the location data for the given debug info.
256 static void create_location_data(dbg_info *dbg, block_id_walker_data_t *wd)
259 const char *fname = be_retrieve_dbg_info(dbg, &lineno);
262 pmap_entry *entry = pmap_find(wd->fname_map, (void *)fname);
266 static unsigned nr = 0;
270 int i, len = strlen(fname) + 1;
271 tarval **tarval_string;
273 snprintf(buf, sizeof(buf), "firm_name_arr.%d", nr);
274 arr = new_type_array(new_id_from_str(buf), 1, wd->tp_char);
275 set_array_bounds_int(arr, 0, 0, len);
277 snprintf(buf, sizeof(buf), "__firm_name.%d", nr++);
278 id = new_id_from_str(buf);
279 ent = new_entity(get_glob_type(), id, arr);
280 set_entity_ld_ident(ent, id);
282 pmap_insert(wd->fname_map, (void *)fname, ent);
284 /* initialize file name string constant */
285 tarval_string = alloca(sizeof(*tarval_string) * (len));
286 for (i = 0; i < len; ++i) {
287 tarval_string[i] = new_tarval_from_long(fname[i], mode_Bs);
289 set_entity_variability(ent, variability_constant);
290 set_array_entity_values(ent, tarval_string, len);
294 wd->locs[wd->id].fname = ent;
295 wd->locs[wd->id].lineno = lineno;
297 wd->locs[wd->id].fname = NULL;
298 wd->locs[wd->id].lineno = 0;
303 * Walker: assigns an ID to every block.
304 * Builds the string table
307 block_id_walker(ir_node * bb, void * data)
309 block_id_walker_data_t *wd = data;
311 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
312 instrument_block(bb, wd->symconst, wd->id);
314 if (wd->flags & profile_with_locations) {
315 dbg_info *dbg = get_irn_dbg_info(bb);
316 create_location_data(dbg, wd);
321 #define IDENT(x) new_id_from_chars(x, sizeof(x) - 1)
324 be_profile_instrument(const char *filename, unsigned flags)
328 ir_entity *bblock_id;
329 ir_entity *bblock_counts;
330 ir_entity *ent_filename;
331 ir_entity *ent_locations = NULL;
332 ir_entity *loc_lineno = NULL;
333 ir_entity *loc_name = NULL;
337 ir_type *string_type;
338 ir_type *character_type;
339 ir_type *loc_type = NULL;
340 ir_type *charptr_type;
342 tarval **tarval_array;
343 tarval **tarval_string;
345 int filename_len = strlen(filename)+1;
347 int align_l, align_n, size;
349 block_id_walker_data_t wd;
352 /* count the number of block first */
353 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
354 ir_graph *irg = get_irp_irg(n);
356 n_blocks += count_blocks(irg);
359 /* create all the necessary types and entities. Note that the
360 types must have a fixed layout, because we already running in the
362 uint_type = new_type_primitive(IDENT("__uint"), mode_Iu);
363 set_type_alignment_bytes(uint_type, get_type_size_bytes(uint_type));
364 array_type = new_type_array(IDENT("__block_info_array"), 1, uint_type);
365 set_array_bounds_int(array_type, 0, 0, n_blocks);
367 character_type = new_type_primitive(IDENT("__char"), mode_Bs);
368 string_type = new_type_array(IDENT("__filename"), 1, character_type);
369 set_array_bounds_int(string_type, 0, 0, filename_len);
371 gtp = get_glob_type();
373 cur_ident = IDENT("__FIRMPROF__BLOCK_IDS");
374 bblock_id = new_entity(gtp, cur_ident, array_type);
375 set_entity_ld_ident(bblock_id, cur_ident);
376 set_entity_variability(bblock_id, variability_initialized);
378 cur_ident = IDENT("__FIRMPROF__BLOCK_COUNTS");
379 bblock_counts = new_entity(gtp, cur_ident, array_type);
380 set_entity_ld_ident(bblock_counts, cur_ident);
381 set_entity_variability(bblock_counts, variability_initialized);
383 cur_ident = IDENT("__FIRMPROF__FILE_NAME");
384 ent_filename = new_entity(gtp, cur_ident, string_type);
385 set_entity_ld_ident(ent_filename, cur_ident);
387 if (flags & profile_with_locations) {
388 loc_type = new_type_struct(IDENT("__location"));
389 loc_lineno = new_entity(loc_type, IDENT("lineno"), uint_type);
390 align_l = get_type_alignment_bytes(uint_type);
391 size = get_type_size_bytes(uint_type);
392 set_entity_offset(loc_lineno, 0);
394 charptr_type = new_type_pointer(IDENT("__charptr"), character_type, mode_P_data);
395 align_n = get_type_size_bytes(charptr_type);
396 set_type_alignment_bytes(charptr_type, align_n);
397 loc_name = new_entity(loc_type, IDENT("name"), charptr_type);
398 size = (size + align_n - 1) & -align_n;
399 set_entity_offset(loc_name, size);
402 if (align_n > align_l)
404 size = (size + align_l - 1) & -align_l;
405 set_type_size_bytes(loc_type, size);
406 set_type_state(loc_type, layout_fixed);
408 loc_type = new_type_array(IDENT("__locarray"), 1, loc_type);
409 set_array_bounds_int(string_type, 0, 0, n_blocks);
411 cur_ident = IDENT("__FIRMPROF__LOCATIONS");
412 ent_locations = new_entity(gtp, cur_ident, loc_type);
413 set_entity_ld_ident(ent_locations, cur_ident);
416 /* initialize count array */
417 NEW_ARR_A(tarval *, tarval_array, n_blocks);
418 tv = get_tarval_null(mode_Iu);
419 for (i = 0; i < n_blocks; ++i) {
420 tarval_array[i] = tv;
422 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
424 /* initialize function name string constant */
425 tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
426 for (i = 0; i < filename_len; ++i) {
427 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
429 set_entity_variability(ent_filename, variability_constant);
430 set_array_entity_values(ent_filename, tarval_string, filename_len);
432 /* initialize block id array and instrument blocks */
433 wd.array = tarval_array;
435 wd.tp_char = character_type;
437 if (flags & profile_with_locations) {
438 wd.fname_map = pmap_create();
439 NEW_ARR_A(loc_entry, wd.locs, n_blocks);
442 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
443 ir_graph *irg = get_irp_irg(n);
445 ir_node *endbb = get_irg_end_block(irg);
448 set_current_ir_graph(irg);
450 /* generate a symbolic constant pointing to the count array */
451 sym.entity_p = bblock_counts;
452 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
454 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
455 env.start_block = get_irg_start_block(irg);
456 env.end_block = get_irg_end_block(irg);
457 set_irn_link(env.start_block, get_irg_no_mem(irg));
458 irg_block_walk_graph(irg, fix_ssa, NULL, &env);
459 for (i = get_Block_n_cfgpreds(endbb) - 1; i >= 0; --i) {
460 ir_node *node = skip_Proj(get_Block_cfgpred(endbb, i));
461 ir_node *bb = get_Block_cfgpred_block(endbb, i);
465 switch (get_irn_opcode(node)) {
467 ins[0] = get_irn_link(bb);
468 ins[1] = get_Return_mem(node);
469 sync = new_r_Sync(irg, bb, 2, ins);
470 set_Return_mem(node, sync);
473 ins[0] = get_irn_link(bb);
474 ins[1] = get_Raise_mem(node);
475 sync = new_r_Sync(irg, bb, 2, ins);
476 set_Raise_mem(node, sync);
479 /* a fragile's op exception. There should be another path to End,
481 assert(is_fragile_op(node) && "unexpected End control flow predecessor");
485 set_array_entity_values(bblock_id, tarval_array, n_blocks);
487 if (flags & profile_with_locations) {
488 /* build the initializer for the locations */
489 rem = current_ir_graph;
490 current_ir_graph = get_const_code_irg();
491 ent = get_array_element_entity(loc_type);
492 set_entity_variability(ent_locations, variability_constant);
493 for (i = 0; i < n_blocks; ++i) {
494 compound_graph_path *path;
499 path = new_compound_graph_path(loc_type, 2);
500 set_compound_graph_path_array_index(path, 0, i);
501 set_compound_graph_path_node(path, 0, ent);
502 set_compound_graph_path_node(path, 1, loc_lineno);
503 tv = new_tarval_from_long(wd.locs[i].lineno, mode_Iu);
504 add_compound_ent_value_w_path(ent_locations, new_Const(mode_Iu, tv), path);
507 path = new_compound_graph_path(loc_type, 2);
508 set_compound_graph_path_array_index(path, 0, i);
509 set_compound_graph_path_node(path, 0, ent);
510 set_compound_graph_path_node(path, 1, loc_name);
511 if (wd.locs[i].fname) {
512 sym.entity_p = wd.locs[i].fname;
513 n = new_SymConst(sym, symconst_addr_ent);
515 n = new_Const(mode_P_data, get_mode_null(mode_P_data));
517 add_compound_ent_value_w_path(ent_locations, n, path);
519 pmap_destroy(wd.fname_map);
521 return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
525 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
529 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
534 register_vcg_hook(void)
536 memset(&hook, 0, sizeof(hook));
537 hook.hook._hook_node_info = profile_node_info;
538 register_hook(hook_node_info, &hook);
542 unregister_vcg_hook(void)
544 unregister_hook(hook_node_info, &hook);
548 * Reads the corresponding profile info file if it exists and returns a
549 * profile info struct
552 be_profile_read(const char *filename)
558 f = fopen(filename, "r");
562 printf("found profile data '%s'.\n", filename);
565 ret = fread(buf, 8, 1, f);
566 if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
570 if(profile) be_profile_free();
571 profile = new_set(cmp_execcount, 16);
575 ret = fread(&query, sizeof(unsigned int), 2, f);
579 set_insert(profile, &query, sizeof(query), query.block);
587 * Frees the profile info
590 be_profile_free(void)
593 unregister_vcg_hook();
599 * Tells whether profile module has acquired data
602 be_profile_has_data(void)
604 return (profile != NULL);
608 * Get block execution count as determined be profiling
611 be_profile_get_block_execcount(const ir_node *block)
613 execcount_t *ec, query;
618 query.block = get_irn_node_nr(block);
619 ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
624 ir_fprintf(stderr, "Warning: Profile contains no data for %+F\n",
630 typedef struct _intialize_execfreq_env_t {
632 ir_exec_freq *execfreqs;
634 } initialize_execfreq_env_t;
636 // minimal execution frequency (an execfreq of 0 confuses algos)
637 static const double MIN_EXECFREQ = 0.00001;
639 static void initialize_execfreq(ir_node *block, void *data) {
640 initialize_execfreq_env_t *env = data;
643 if(block == get_irg_start_block(env->irg)
644 || block == get_irg_end_block(env->irg)) {
647 freq = be_profile_get_block_execcount(block);
648 freq *= env->freq_factor;
649 if(freq < MIN_EXECFREQ)
653 set_execfreq(env->execfreqs, block, freq);
656 ir_exec_freq *be_create_execfreqs_from_profile(ir_graph *irg)
658 ir_node *block2 = NULL;
659 ir_node *start_block;
660 const ir_edge_t *edge;
661 initialize_execfreq_env_t env;
665 env.execfreqs = create_execfreq(irg);
667 // find the successor to the start block
668 start_block = get_irg_start_block(irg);
669 foreach_block_succ(start_block, edge) {
670 ir_node *succ = get_edge_src_irn(edge);
671 if(succ != start_block) {
676 assert(block2 != NULL);
678 count = be_profile_get_block_execcount(block2);
680 // the function was never executed, so fallback to estimated freqs
681 free_execfreq(env.execfreqs);
683 return compute_execfreq(irg, 10);
686 env.freq_factor = 1.0 / count;
687 irg_block_walk_graph(irg, initialize_execfreq, NULL, &env);
689 return env.execfreqs;