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 typedef struct _block_id_walker_data {
59 } block_id_walker_data;
62 block_counter(ir_node * bb, void * data)
64 unsigned int *count = data;
69 count_blocks(ir_graph * irg)
71 unsigned int count = 0;
73 irg_block_walk_graph(irg, block_counter, NULL, &count);
78 * Instrument a block with code needed for profiling
81 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
83 ir_graph *irg = get_irn_irg(bb);
84 ir_node *start_block = get_irg_start_block(irg);
85 ir_node *load, *store, *offset, *add, *projm, *proji;
88 if(bb == start_block || bb == get_irg_end_block(irg))
91 cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
92 offset = new_r_Add(irg, bb, address, cnst, mode_P);
93 load = new_r_Load(irg, bb, new_NoMem(), offset, mode_Iu);
94 projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
95 proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
96 cnst = new_r_Const_long(irg, start_block, mode_Iu, 1);
97 add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
98 store = new_r_Store(irg, bb, projm, offset, add);
99 projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
104 * Generates a new irg which calls the initializer
107 gen_initializer_irg(entity * bblock_id, entity * bblock_counts, int n_blocks)
109 ir_node *start_block;
112 ident *name = new_id_from_str("__firmprof_initializer");
113 entity *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
114 ir_node *ret, *call, *symconst;
117 ident *init_name = new_id_from_str("__init_firmprof");
118 ir_type *init_type = new_type_method(init_name, 3, 0);
119 ir_type *uint, *uintptr;
124 uint = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
125 uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
127 set_method_param_type(init_type, 0, uintptr);
128 set_method_param_type(init_type, 1, uintptr);
129 set_method_param_type(init_type, 2, uint);
130 init_ent = new_entity(get_glob_type(), init_name, init_type);
132 irg = new_ir_graph(ent, 0);
133 set_current_ir_graph(irg);
135 bb = get_cur_block();
137 start_block = get_irg_start_block(irg);
139 sym.entity_p = init_ent;
140 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
142 sym.entity_p = bblock_id;
143 ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
144 sym.entity_p = bblock_counts;
145 ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
146 ins[2] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
148 call = new_r_Call( irg,
149 bb, //ir_node * block,
150 get_irg_initial_mem(irg), //ir_node * store,
151 symconst, //ir_node * callee,
153 ins, //ir_node ** in,
154 init_type //ir_type * tp
157 ret = new_r_Return ( irg,
158 bb, //ir_node * block,
159 new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), //ir_node * store,
166 add_immBlock_pred(get_irg_end_block(irg), ret);
167 mature_immBlock(get_irg_end_block(irg));
169 irg_finalize_cons(irg);
175 block_id_walker(ir_node * bb, void * data)
177 block_id_walker_data *wd = data;
179 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
180 instrument_block(bb, wd->symconst, wd->id);
185 be_profile_instrument(void)
187 ir_graph *const_irg = get_const_code_irg();
188 ir_node *const_block = get_irg_start_block(const_irg);
190 unsigned int n_blocks = 0;
191 entity *bblock_id, *bblock_counts, *bblock_count;
192 ir_type *array_type, *integer_type;
193 tarval **tarval_array;
195 block_id_walker_data wd;
198 integer_type = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
199 array_type = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
200 set_array_bounds_int(array_type, 0, 0, n_blocks);
201 bblock_id = new_entity(get_glob_type(), new_id_from_str("__BLOCK_IDS"), array_type);
202 set_entity_variability(bblock_id, variability_initialized);
203 bblock_counts = new_entity(get_glob_type(), new_id_from_str("__BLOCK_COUNTS"), array_type);
204 set_entity_variability(bblock_counts, variability_initialized);
205 bblock_count = new_entity(get_glob_type(), new_id_from_str("__N_BLOCKS"), integer_type);
206 set_entity_variability(bblock_count, variability_initialized);
208 for (n = get_irp_n_irgs()-1; n>=0; --n) {
209 ir_graph *irg = get_irp_irg(n);
211 n_blocks += count_blocks(irg);
214 /* initialize count array */
215 tarval_array = alloca(sizeof(tarval_array[0]) * n_blocks);
216 for(i = 0; i < n_blocks; ++i) {
217 tarval_array[i] = get_tarval_null(mode_Iu);
219 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
221 /* initialize the block count entity */
222 set_atomic_ent_value(bblock_count, new_r_Const_long(const_irg, const_block, mode_Iu, n_blocks));
224 /* generate a symbolic constant pointing to the count array */
225 sym.entity_p = bblock_count;
227 /* initialize block id array and instrument blocks */
228 wd.array = tarval_array;
230 for (n = get_irp_n_irgs()-1; n>=0; --n) {
231 ir_graph *irg = get_irp_irg(n);
233 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
235 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
237 set_array_entity_values(bblock_id, tarval_array, n_blocks);
239 return gen_initializer_irg(bblock_id, bblock_counts, n_blocks);
244 be_profile_read(void)