d0ff4fb5f4b8e6801a6130a9bb5ec30f225a00af
[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  *
6  * Code instrumentation and execution count profiling
7  *
8  * Copyright (C) 2006 Universitaet Karlsruhe
9  * Released under the GPL
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <math.h>
16
17 #include "hashptr.h"
18 #include "debug.h"
19 #include "obst.h"
20 #include "set.h"
21 #include "list.h"
22 #include "pmap.h"
23
24 #include "entity.h"
25 #include "irprintf.h"
26 #include "irgwalk.h"
27 #include "irdump_t.h"
28 #include "irnode_t.h"
29 #include "ircons_t.h"
30 #include "irloop_t.h"
31 #include "iredges.h"
32 #include "execfreq.h"
33 #include "irvrfy.h"
34 #include "type.h"
35 #include "entity.h"
36
37 #include "be_t.h"
38 #include "belive_t.h"
39 #include "besched_t.h"
40 #include "beirgmod.h"
41 #include "bearch.h"
42 #include "beabi.h"
43 #include "benode_t.h"
44 #include "beutil.h"
45 #include "ircons.h"
46
47 #include "bechordal_t.h"
48
49 #ifdef WITH_LIBCORE
50 #include <libcore/lc_opts.h>
51 #include <libcore/lc_opts_enum.h>
52 #endif /* WITH_LIBCORE */
53
54 typedef struct _block_id_walker_data {
55         tarval        **array;
56         unsigned int    id;
57         ir_node *symconst;
58 } block_id_walker_data;
59
60 static void
61 block_counter(ir_node * bb, void * data)
62 {
63         unsigned int  *count = data;
64         *count = *count + 1;
65 }
66
67 static unsigned int
68 count_blocks(ir_graph * irg)
69 {
70         unsigned int count = 0;
71
72         irg_block_walk_graph(irg, block_counter, NULL, &count);
73         return count;
74 }
75
76 /**
77  * Instrument a block with code needed for profiling
78  */
79 static void
80 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
81 {
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;
85         ir_node *cnst;
86
87         if(bb == start_block || bb == get_irg_end_block(irg))
88                 return;
89
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);
99         keep_alive(projm);
100 }
101
102 /**
103  * Generates a new irg which calls the initializer
104  */
105 static ir_graph *
106 gen_initializer_irg(entity * bblock_id, entity * bblock_counts, int n_blocks)
107 {
108         ir_node *start_block;
109
110         ir_node   *ins[3];
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;
114         symconst_symbol sym;
115
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;
119
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);
122
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);
127
128         ir_graph *irg = new_ir_graph(ent, 0);
129         set_current_ir_graph(irg);
130
131         ir_node *bb = get_cur_block();
132
133         start_block = get_irg_start_block(irg);
134
135         sym.entity_p = init_ent;
136         symconst = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
137
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);
143
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,
148                         3,                                                      //int   arity,
149                         ins,                                            //ir_node **    in,
150                         init_type                                       //ir_type *     tp
151                         );
152
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,
156                         0,                                                                              //int   arity,
157                         NULL                                                                    //ir_node **    in
158                         );
159
160         mature_immBlock(bb);
161
162         add_immBlock_pred(get_irg_end_block(irg), ret);
163         mature_immBlock(get_irg_end_block(irg));
164
165         irg_finalize_cons(irg);
166
167         return irg;
168 }
169
170 static void
171 block_id_walker(ir_node * bb, void * data)
172 {
173         block_id_walker_data *wd = data;
174
175         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
176         instrument_block(bb, wd->symconst, wd->id);
177         ++wd->id;
178 }
179
180 void
181 be_profile_instrument(void)
182 {
183         ir_graph *const_irg = get_const_code_irg();
184         ir_node *const_block = get_irg_start_block(const_irg);
185         int            n, i;
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;
190
191         block_id_walker_data  wd;
192         symconst_symbol sym;
193
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);
203
204         for (n = get_irp_n_irgs()-1; n>=0; --n) {
205                 ir_graph      *irg = get_irp_irg(n);
206
207                 n_blocks += count_blocks(irg);
208         }
209
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);
214         }
215         set_array_entity_values(bblock_counts, tarval_array, n_blocks);
216
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));
219
220         /* generate a symbolic constant pointing to the count array */
221         sym.entity_p = bblock_count;
222
223         /* initialize block id array and instrument blocks */
224         wd.array = tarval_array;
225         wd.id = 0;
226         for (n = get_irp_n_irgs()-1; n>=0; --n) {
227                 ir_graph      *irg = get_irp_irg(n);
228
229                 wd.symconst = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
230
231                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
232         }
233         set_array_entity_values(bblock_id, tarval_array, n_blocks);
234
235         gen_initializer_irg(bblock_id, bblock_counts, n_blocks);
236 }
237
238
239 void
240 be_profile_read(void)
241 {
242
243 }