fixed C99 style
[libfirm] / ir / be / beprofile.c
1 /** vim: set sw=4 ts=4:
2  * @file   beprofile.c
3  * @date   2006-04-06
4  * @author Adam M. Szalkowski
5  * @cvs-id $Id$
6  *
7  * Code instrumentation and execution count profiling
8  *
9  * Copyright (C) 2006 Universitaet Karlsruhe
10  * Released under the GPL
11  */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <math.h>
17
18 #include "hashptr.h"
19 #include "debug.h"
20 #include "obst.h"
21 #include "set.h"
22 #include "list.h"
23 #include "pmap.h"
24
25 #include "entity.h"
26 #include "irprintf.h"
27 #include "irgwalk.h"
28 #include "irdump_t.h"
29 #include "irnode_t.h"
30 #include "ircons_t.h"
31 #include "irloop_t.h"
32 #include "iredges.h"
33 #include "execfreq.h"
34 #include "irvrfy.h"
35 #include "type.h"
36 #include "entity.h"
37
38 #include "be_t.h"
39 #include "belive_t.h"
40 #include "besched_t.h"
41 #include "beirgmod.h"
42 #include "bearch.h"
43 #include "beabi.h"
44 #include "benode_t.h"
45 #include "beutil.h"
46 #include "ircons.h"
47
48 #include "bechordal_t.h"
49
50 #ifdef WITH_LIBCORE
51 #include <libcore/lc_opts.h>
52 #include <libcore/lc_opts_enum.h>
53 #endif /* WITH_LIBCORE */
54
55 typedef struct _block_id_walker_data {
56         tarval        **array;
57         unsigned int    id;
58         ir_node *symconst;
59 } block_id_walker_data;
60
61 static void
62 block_counter(ir_node * bb, void * data)
63 {
64         unsigned int  *count = data;
65         *count = *count + 1;
66 }
67
68 static unsigned int
69 count_blocks(ir_graph * irg)
70 {
71         unsigned int count = 0;
72
73         irg_block_walk_graph(irg, block_counter, NULL, &count);
74         return count;
75 }
76
77 /**
78  * Instrument a block with code needed for profiling
79  */
80 static void
81 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
82 {
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;
86         ir_node *cnst;
87
88         if(bb == start_block || bb == get_irg_end_block(irg))
89                 return;
90
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);
100         keep_alive(projm);
101 }
102
103 /**
104  * Generates a new irg which calls the initializer
105  */
106 static ir_graph *
107 gen_initializer_irg(entity * bblock_id, entity * bblock_counts, int n_blocks)
108 {
109         ir_node *start_block;
110
111         ir_node   *ins[3];
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;
115         symconst_symbol sym;
116
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;
120         entity    *init_ent;
121         ir_graph  *irg;
122         ir_node   *bb;
123
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);
126
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);
131
132         irg = new_ir_graph(ent, 0);
133         set_current_ir_graph(irg);
134
135         bb = get_cur_block();
136
137         start_block = get_irg_start_block(irg);
138
139         sym.entity_p = init_ent;
140         symconst     = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
141
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);
147
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,
152                         3,                                                      //int   arity,
153                         ins,                                            //ir_node **    in,
154                         init_type                                       //ir_type *     tp
155                         );
156
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,
160                         0,                                                                              //int   arity,
161                         NULL                                                                    //ir_node **    in
162                         );
163
164         mature_immBlock(bb);
165
166         add_immBlock_pred(get_irg_end_block(irg), ret);
167         mature_immBlock(get_irg_end_block(irg));
168
169         irg_finalize_cons(irg);
170
171         return irg;
172 }
173
174 static void
175 block_id_walker(ir_node * bb, void * data)
176 {
177         block_id_walker_data *wd = data;
178
179         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
180         instrument_block(bb, wd->symconst, wd->id);
181         ++wd->id;
182 }
183
184 ir_graph *
185 be_profile_instrument(void)
186 {
187         ir_graph *const_irg = get_const_code_irg();
188         ir_node *const_block = get_irg_start_block(const_irg);
189         int            n, i;
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;
194
195         block_id_walker_data  wd;
196         symconst_symbol sym;
197
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);
207
208         for (n = get_irp_n_irgs()-1; n>=0; --n) {
209                 ir_graph      *irg = get_irp_irg(n);
210
211                 n_blocks += count_blocks(irg);
212         }
213
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);
218         }
219         set_array_entity_values(bblock_counts, tarval_array, n_blocks);
220
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));
223
224         /* generate a symbolic constant pointing to the count array */
225         sym.entity_p = bblock_count;
226
227         /* initialize block id array and instrument blocks */
228         wd.array = tarval_array;
229         wd.id = 0;
230         for (n = get_irp_n_irgs()-1; n>=0; --n) {
231                 ir_graph      *irg = get_irp_irg(n);
232
233                 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
234
235                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
236         }
237         set_array_entity_values(bblock_id, tarval_array, n_blocks);
238
239         return gen_initializer_irg(bblock_id, bblock_counts, n_blocks);
240 }
241
242
243 void
244 be_profile_read(void)
245 {
246
247 }