1 /** vim: set sw=4 ts=4:
4 * @author Adam M. Szalkowski
6 * Code instrumentation and execution count profiling
8 * Copyright (C) 2006 Universitaet Karlsruhe
9 * Released under the GPL
39 #include "besched_t.h"
47 #include "bechordal_t.h"
50 #include <libcore/lc_opts.h>
51 #include <libcore/lc_opts_enum.h>
52 #endif /* WITH_LIBCORE */
54 typedef struct _block_id_walker_data {
58 } block_id_walker_data;
61 block_counter(ir_node * bb, void * data)
63 unsigned int *count = data;
68 count_blocks(ir_graph * irg)
70 unsigned int count = 0;
72 irg_block_walk_graph(irg, block_counter, NULL, &count);
77 * Instrument a block with code needed for profiling
80 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
82 ir_graph *irg = get_irn_irg(bb);
83 ir_node *start_block = get_irg_start_block(irg);
84 ir_node *load, *store, *offset, *add, *projm, *proji;
87 if(bb == start_block || bb == get_irg_end_block(irg))
90 cnst = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
91 offset = new_r_Add(irg, bb, address, cnst, mode_P);
92 load = new_r_Load(irg, bb, new_NoMem(), offset, mode_Iu);
93 projm = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
94 proji = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
95 cnst = new_r_Const_long(irg, start_block, mode_Iu, 1);
96 add = new_r_Add(irg, bb, proji, cnst, mode_Iu);
97 store = new_r_Store(irg, bb, projm, offset, add);
98 projm = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
103 * Generates a new irg which calls the initializer
106 gen_initializer_irg(entity * bblock_id, entity * bblock_counts, int n_blocks)
108 ir_node *start_block;
111 ident *name = new_id_from_str("__firmprof_initializer");
112 entity *ent = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
113 ir_node *ret, *call, *symconst;
116 ident *init_name = new_id_from_str("__init_firmprof");
117 ir_type *init_type = new_type_method(init_name, 3, 0);
118 ir_type *uint, *uintptr;
120 uint = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
121 uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
123 set_method_param_type(init_type, 0, uintptr);
124 set_method_param_type(init_type, 1, uintptr);
125 set_method_param_type(init_type, 2, uint);
126 entity *init_ent = new_entity(get_glob_type(), init_name, init_type);
128 ir_graph *irg = new_ir_graph(ent, 0);
129 set_current_ir_graph(irg);
131 ir_node *bb = get_cur_block();
133 start_block = get_irg_start_block(irg);
135 sym.entity_p = init_ent;
136 symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
138 sym.entity_p = bblock_id;
139 ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
140 sym.entity_p = bblock_counts;
141 ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
142 ins[2] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
144 call = new_r_Call( irg,
145 bb, //ir_node * block,
146 get_irg_initial_mem(irg), //ir_node * store,
147 symconst, //ir_node * callee,
149 ins, //ir_node ** in,
150 init_type //ir_type * tp
153 ret = new_r_Return ( irg,
154 bb, //ir_node * block,
155 new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), //ir_node * store,
162 add_immBlock_pred(get_irg_end_block(irg), ret);
163 mature_immBlock(get_irg_end_block(irg));
165 irg_finalize_cons(irg);
171 block_id_walker(ir_node * bb, void * data)
173 block_id_walker_data *wd = data;
175 wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
176 instrument_block(bb, wd->symconst, wd->id);
181 be_profile_instrument(void)
183 ir_graph *const_irg = get_const_code_irg();
184 ir_node *const_block = get_irg_start_block(const_irg);
186 unsigned int n_blocks = 0;
187 entity *bblock_id, *bblock_counts, *bblock_count;
188 ir_type *array_type, *integer_type;
189 tarval **tarval_array;
191 block_id_walker_data wd;
194 integer_type = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
195 array_type = new_type_array(new_id_from_str("__block_info_array"), 1, integer_type);
196 set_array_bounds_int(array_type, 0, 0, n_blocks);
197 bblock_id = new_entity(get_glob_type(), new_id_from_str("__BLOCK_IDS"), array_type);
198 set_entity_variability(bblock_id, variability_initialized);
199 bblock_counts = new_entity(get_glob_type(), new_id_from_str("__BLOCK_COUNTS"), array_type);
200 set_entity_variability(bblock_counts, variability_initialized);
201 bblock_count = new_entity(get_glob_type(), new_id_from_str("__N_BLOCKS"), integer_type);
202 set_entity_variability(bblock_count, variability_initialized);
204 for (n = get_irp_n_irgs()-1; n>=0; --n) {
205 ir_graph *irg = get_irp_irg(n);
207 n_blocks += count_blocks(irg);
210 /* initialize count array */
211 tarval_array = alloca(sizeof(tarval_array[0]) * n_blocks);
212 for(i = 0; i < n_blocks; ++i) {
213 tarval_array[i] = get_tarval_null(mode_Iu);
215 set_array_entity_values(bblock_counts, tarval_array, n_blocks);
217 /* initialize the block count entity */
218 set_atomic_ent_value(bblock_count, new_r_Const_long(const_irg, const_block, mode_Iu, n_blocks));
220 /* generate a symbolic constant pointing to the count array */
221 sym.entity_p = bblock_count;
223 /* initialize block id array and instrument blocks */
224 wd.array = tarval_array;
226 for (n = get_irp_n_irgs()-1; n>=0; --n) {
227 ir_graph *irg = get_irp_irg(n);
229 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
231 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
233 set_array_entity_values(bblock_id, tarval_array, n_blocks);
235 gen_initializer_irg(bblock_id, bblock_counts, n_blocks);
240 be_profile_read(void)