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;
107 if(bb == start_block || bb == get_irg_end_block(irg))
110 cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
111 offset = new_r_Add(irg, bb, address, cnst, mode_P);
112 load = new_r_Load(irg, bb, new_NoMem(), offset, mode_Iu);
113 projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
114 proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
115 cnst = new_r_Const_long(irg, start_block, mode_Iu, 1);
116 add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
117 store = new_r_Store(irg, bb, projm, offset, add);
118 projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
123 * Generates a new irg which calls the initializer
126 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
128 ir_node *start_block;
131 ident *name = new_id_from_str("__firmprof_initializer");
132 entity *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
133 ir_node *ret, *call, *symconst;
136 ident *init_name = new_id_from_str("__init_firmprof");
137 ir_type *init_type = new_type_method(init_name, 4, 0);
138 ir_type *uint, *uintptr, *string;
142 ir_type *empty_frame_type;
144 set_entity_ld_ident(ent, name);
146 uint = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
147 uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
148 string = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
150 set_method_param_type(init_type, 0, string);
151 set_method_param_type(init_type, 1, uintptr);
152 set_method_param_type(init_type, 2, uintptr);
153 set_method_param_type(init_type, 3, uint);
154 init_ent = new_entity(get_glob_type(), init_name, init_type);
155 set_entity_ld_ident(init_ent, init_name);
157 irg = new_ir_graph(ent, 0);
158 set_current_ir_graph(irg);
159 empty_frame_type = get_irg_frame_type(irg);
160 set_type_size_bytes(empty_frame_type, 0);
162 bb = get_cur_block();
164 start_block = get_irg_start_block(irg);
166 sym.entity_p = init_ent;
167 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
169 sym.entity_p = ent_filename;
170 ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
171 sym.entity_p = bblock_id;
172 ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
173 sym.entity_p = bblock_counts;
174 ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
175 ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
177 call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
178 ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
181 add_immBlock_pred(get_irg_end_block(irg), ret);
182 mature_immBlock(get_irg_end_block(irg));
184 irg_finalize_cons(irg);
190 block_id_walker(ir_node * bb, void * data)
192 block_id_walker_data_t *wd = data;
194 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
195 instrument_block(bb, wd->symconst, wd->id);
200 be_profile_instrument(char * filename)
203 unsigned int n_blocks = 0;
204 entity *bblock_id, *bblock_counts, *ent_filename;
205 ir_type *array_type, *integer_type, *string_type, *character_type;
206 tarval **tarval_array, **tarval_string;
207 int filename_len = strlen(filename)+1;
210 block_id_walker_data_t wd;
213 integer_type = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
214 array_type = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
215 set_array_bounds_int(array_type, 0, 0, n_blocks);
217 character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
218 string_type = new_type_array(new_id_from_str("__filename"), 1, character_type);
219 set_array_bounds_int(string_type, 0, 0, filename_len);
221 cur_ident = new_id_from_str("__FIRMPROF__BLOCK_IDS");
222 bblock_id = new_entity(get_glob_type(), cur_ident, array_type);
223 set_entity_ld_ident(bblock_id, cur_ident);
224 set_entity_variability(bblock_id, variability_initialized);
226 cur_ident = new_id_from_str("__FIRMPROF__BLOCK_COUNTS");
227 bblock_counts = new_entity(get_glob_type(), cur_ident, array_type);
228 set_entity_ld_ident(bblock_counts, cur_ident);
229 set_entity_variability(bblock_counts, variability_initialized);
231 cur_ident = new_id_from_str("__FIRMPROF__FILE_NAME");
232 ent_filename = new_entity(get_glob_type(), cur_ident, string_type);
233 set_entity_ld_ident(ent_filename, cur_ident);
234 set_entity_variability(ent_filename, variability_initialized);
236 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
237 ir_graph *irg = get_irp_irg(n);
239 n_blocks += count_blocks(irg);
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);
247 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
249 /* initialize function name string constant */
250 tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
251 for (i = 0; i < filename_len; ++i) {
252 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
254 set_array_entity_values(ent_filename, tarval_string, filename_len);
257 /* initialize block id array and instrument blocks */
258 wd.array = tarval_array;
260 for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
261 ir_graph *irg = get_irp_irg(n);
263 /* generate a symbolic constant pointing to the count array */
264 sym.entity_p = bblock_counts;
265 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
267 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
269 set_array_entity_values(bblock_id, tarval_array, n_blocks);
271 return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
275 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
278 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
283 register_vcg_hook(void)
285 memset(&hook, 0, sizeof(hook));
286 hook.hook._hook_node_info = profile_node_info;
287 register_hook(hook_node_info, &hook);
291 unregister_vcg_hook(void)
293 unregister_hook(hook_node_info, &hook);
297 * Reads the corresponding profile info file if it exists and returns a
298 * profile info struct
301 be_profile_read(char * filename)
307 f = fopen(filename, "r");
311 printf("found profile data.\n");
314 ret = fread(buf, 8, 1, f);
315 if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
319 if(profile) be_profile_free();
320 profile = new_set(cmp_execcount, 16);
324 ret = fread(&query, sizeof(unsigned int), 2, f);
328 set_insert(profile, &query, sizeof(query), query.block);
336 * Frees the profile info
339 be_profile_free(void)
342 unregister_vcg_hook();
348 * Tells whether profile module has aquired data
351 be_profile_has_data(void)
353 return (profile != NULL);
357 * Get block execution count as determined be profiling
360 be_profile_get_block_execcount(const ir_node * block)
362 execcount_t *ec, query;
367 query.block = get_irn_node_nr(block);
368 ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));