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"
48 #include "bechordal_t.h"
51 #include <libcore/lc_opts.h>
52 #include <libcore/lc_opts_enum.h>
53 #endif /* WITH_LIBCORE */
55 #include "beprofile.h"
57 typedef struct _block_id_walker_data_t {
61 } block_id_walker_data_t;
63 typedef struct _execcount_t {
69 cmp_execcount(const void * a, const void * b, size_t size)
71 return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
75 block_counter(ir_node * bb, void * data)
77 unsigned int *count = data;
83 count_blocks(ir_graph * irg)
85 unsigned int count = 0;
87 irg_block_walk_graph(irg, block_counter, NULL, &count);
91 /* keep the execcounts here because they are only read once per compiler run */
92 static set * profile = NULL;
95 * Instrument a block with code needed for profiling
98 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
100 ir_graph *irg = get_irn_irg(bb);
101 ir_node *start_block = get_irg_start_block(irg);
102 ir_node *load, *store, *offset, *add, *projm, *proji;
105 if(bb == start_block || bb == get_irg_end_block(irg))
108 cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
109 offset = new_r_Add(irg, bb, address, cnst, mode_P);
110 load = new_r_Load(irg, bb, new_NoMem(), offset, mode_Iu);
111 projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
112 proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
113 cnst = new_r_Const_long(irg, start_block, mode_Iu, 1);
114 add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
115 store = new_r_Store(irg, bb, projm, offset, add);
116 projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
121 * Generates a new irg which calls the initializer
124 gen_initializer_irg(entity * ent_filename, entity * bblock_id, entity * bblock_counts, int n_blocks)
126 ir_node *start_block;
129 ident *name = new_id_from_str("__firmprof_initializer");
130 entity *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
131 ir_node *ret, *call, *symconst;
134 ident *init_name = new_id_from_str("__init_firmprof");
135 ir_type *init_type = new_type_method(init_name, 4, 0);
136 ir_type *uint, *uintptr, *string;
140 ir_type *empty_frame_type;
142 uint = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
143 uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
144 string = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
146 set_method_param_type(init_type, 0, string);
147 set_method_param_type(init_type, 1, uintptr);
148 set_method_param_type(init_type, 2, uintptr);
149 set_method_param_type(init_type, 3, uint);
150 init_ent = new_entity(get_glob_type(), init_name, init_type);
152 irg = new_ir_graph(ent, 0);
153 set_current_ir_graph(irg);
154 empty_frame_type = get_irg_frame_type(irg);
155 set_type_size_bytes(empty_frame_type, 0);
157 bb = get_cur_block();
159 start_block = get_irg_start_block(irg);
161 sym.entity_p = init_ent;
162 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
164 sym.entity_p = ent_filename;
165 ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
166 sym.entity_p = bblock_id;
167 ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
168 sym.entity_p = bblock_counts;
169 ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
170 ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
172 call = new_r_Call( irg,
173 bb, //ir_node * block,
174 get_irg_initial_mem(irg), //ir_node * store,
175 symconst, //ir_node * callee,
177 ins, //ir_node ** in,
178 init_type //ir_type * tp
181 ret = new_r_Return ( irg,
182 bb, //ir_node * block,
183 new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), //ir_node * store,
190 add_immBlock_pred(get_irg_end_block(irg), ret);
191 mature_immBlock(get_irg_end_block(irg));
193 irg_finalize_cons(irg);
199 block_id_walker(ir_node * bb, void * data)
201 block_id_walker_data_t *wd = data;
203 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
204 instrument_block(bb, wd->symconst, wd->id);
209 be_profile_instrument(void)
211 ir_graph *const_irg = get_const_code_irg();
212 ir_node *const_block = get_irg_start_block(const_irg);
214 unsigned int n_blocks = 0;
215 entity *bblock_id, *bblock_counts, *ent_filename;
216 ir_type *array_type, *integer_type, *string_type, *character_type;
217 tarval **tarval_array, **tarval_string;
218 char *filename = "test.c"; //FIXME
219 int filename_len = strlen(filename)+1;
221 block_id_walker_data_t wd;
224 integer_type = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
225 array_type = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
226 set_array_bounds_int(array_type, 0, 0, n_blocks);
227 character_type = new_type_primitive(new_id_from_str("__char"), mode_Bs);
228 string_type = new_type_array(new_id_from_str("__function_name"), 1, character_type);
229 set_array_bounds_int(string_type, 0, 0, filename_len);
230 bblock_id = new_entity(get_glob_type(), new_id_from_str("__BLOCK_IDS"), array_type);
231 set_entity_variability(bblock_id, variability_initialized);
232 bblock_counts = new_entity(get_glob_type(), new_id_from_str("__BLOCK_COUNTS"), array_type);
233 set_entity_variability(bblock_counts, variability_initialized);
234 ent_filename = new_entity(get_glob_type(), new_id_from_str("__FUNCTION_NAME"), string_type);
235 set_entity_variability(ent_filename, variability_initialized);
237 for (n = get_irp_n_irgs()-1; n>=0; --n) {
238 ir_graph *irg = get_irp_irg(n);
240 n_blocks += count_blocks(irg);
243 /* initialize count array */
244 tarval_array = alloca(sizeof(*tarval_array) * n_blocks);
245 for(i = 0; i < n_blocks; ++i) {
246 tarval_array[i] = get_tarval_null(mode_Iu);
248 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
250 /* initialize function name string constant */
251 tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
252 for(i = 0; i < filename_len; ++i) {
253 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
255 set_array_entity_values(ent_filename, tarval_string, filename_len);
258 /* initialize block id array and instrument blocks */
259 wd.array = tarval_array;
261 for (n = get_irp_n_irgs()-1; n>=0; --n) {
262 ir_graph *irg = get_irp_irg(n);
264 /* generate a symbolic constant pointing to the count array */
265 sym.entity_p = bblock_counts;
266 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
268 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
270 set_array_entity_values(bblock_id, tarval_array, n_blocks);
272 return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
277 * Reads the corresponding profile info file if it exists and returns a
278 * profile info struct
281 be_profile_read(char * filename)
287 f = fopen(filename, "r");
289 perror("opening of profile data failed");
294 ret = fread(buf, 8, 1, f);
295 if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
299 if(profile) be_profile_free();
300 profile = new_set(cmp_execcount, 16);
304 ret = fread(&query, sizeof(unsigned int), 2, f);
308 set_insert(profile, &query, sizeof(query), query.block);
315 * Frees the profile info
318 be_profile_free(void)
325 * Get block execution count as determined be profiling
328 be_profile_get_block_execcount(const ir_node * block)
330 execcount_t *ec, query;
335 query.block = get_irn_node_nr(block);
336 ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));