- Split bearch.h correctly into bearch.h and bearch_t.h
[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
37 #include "be_t.h"
38 #include "belive_t.h"
39 #include "besched_t.h"
40 #include "beirgmod.h"
41 #include "bearch_t.h"
42 #include "beabi.h"
43 #include "benode_t.h"
44 #include "beutil.h"
45 #include "ircons.h"
46 #include "irhooks.h"
47 #include "iredges.h"
48
49 #include "bechordal_t.h"
50
51 #include "beprofile.h"
52
53 /** An entry in the id-to-location map */
54 typedef struct loc_entry {
55         ir_entity    *fname;   /**< the entity holding the file name */
56         unsigned int lineno;   /**< line number */
57 } loc_entry;
58
59 typedef struct _block_id_walker_data_t {
60         tarval         **array;    /**< the entity the holds the block counts */
61         unsigned int   id;         /**< current block id number */
62         ir_node        *symconst;  /**< the SymConst representing array */
63         pmap           *fname_map; /**< set containing all found filenames */
64         loc_entry      *locs;      /**< locations */
65         ir_type        *tp_char;   /**< the character type */
66         unsigned       flags;      /**< profile flags */
67 } block_id_walker_data_t;
68
69 typedef struct _execcount_t {
70         unsigned int block;
71         unsigned int count;
72 } execcount_t;
73
74 static int
75 cmp_execcount(const void * a, const void * b, size_t size)
76 {
77         return ((execcount_t*)a)->block != ((execcount_t*)b)->block;
78 }
79
80 static void
81 block_counter(ir_node * bb, void * data)
82 {
83         unsigned int  *count = data;
84         *count = *count + 1;
85 }
86
87 static unsigned int
88 count_blocks(ir_graph * irg)
89 {
90         unsigned int count = 0;
91
92         irg_block_walk_graph(irg, block_counter, NULL, &count);
93         return count;
94 }
95
96 /* keep the execcounts here because they are only read once per compiler run */
97 static set * profile = NULL;
98 static hook_entry_t hook;
99
100 /**
101  * Instrument a block with code needed for profiling
102  */
103 static void
104 instrument_block(ir_node * bb, ir_node * address, unsigned int id)
105 {
106         ir_graph *irg = get_irn_irg(bb);
107         ir_node  *start_block = get_irg_start_block(irg);
108         ir_node  *load, *store, *offset, *add, *projm, *proji, *unknown;
109         ir_node  *cnst;
110
111         /**
112          * We can't instrument the start and end block as there are no real
113          * instructions in these blocks
114          */
115         if(bb == start_block || bb == get_irg_end_block(irg))
116                 return;
117
118         unknown = new_r_Unknown(irg, mode_M);
119         cnst    = new_r_Const_long(irg, start_block, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
120         offset  = new_r_Add(irg, bb, address, cnst, mode_P);
121         load    = new_r_Load(irg, bb, unknown, offset, mode_Iu);
122         projm   = new_r_Proj(irg, bb, load, mode_M, pn_Load_M);
123         proji   = new_r_Proj(irg, bb, load, mode_Iu, pn_Load_res);
124         cnst    = new_r_Const_long(irg, start_block, mode_Iu, 1);
125         add     = new_r_Add(irg, bb, proji, cnst, mode_Iu);
126         store   = new_r_Store(irg, bb, projm, offset, add);
127         projm   = new_r_Proj(irg, bb, store, mode_M, pn_Store_M);
128         set_irn_link(bb, projm);
129         set_irn_link(projm, load);
130 }
131
132 typedef struct fix_env {
133         ir_node *start_block;
134         ir_node *end_block;
135 } fix_env;
136
137 /**
138  * SSA Construction for instrumentation code memory
139  */
140 static void
141 fix_ssa(ir_node * bb, void * data)
142 {
143         fix_env *env = data;
144         ir_node *mem;
145         int     arity = get_Block_n_cfgpreds(bb);
146
147         /* start and end block are not instrumented, skip! */
148         if (bb == env->start_block || bb == env->end_block)
149                 return;
150
151         if (arity == 1) {
152                 mem = get_irn_link(get_Block_cfgpred_block(bb, 0));
153         } else {
154                 int n;
155                 ir_node **ins;
156                 ir_graph *irg = current_ir_graph;
157
158                 NEW_ARR_A(ir_node*, ins, arity);
159                 for (n = arity - 1; n >= 0; --n) {
160                         ins[n] = get_irn_link(get_Block_cfgpred_block(bb, n));
161                 }
162                 mem = new_r_Phi(irg, bb, arity, ins, mode_M);
163         }
164         set_Load_mem(get_irn_link(get_irn_link(bb)), mem);
165 }
166
167
168 /**
169  * Generates a new irg which calls the initializer
170  *
171  * Pseudocode:
172  *       void __firmprof_initializer(void) { __init_firmprof(ent_filename, bblock_id, bblock_counts, n_blocks); }
173  */
174 static ir_graph *
175 gen_initializer_irg(ir_entity * ent_filename, ir_entity * bblock_id, ir_entity * bblock_counts, int n_blocks)
176 {
177         ir_node *start_block;
178
179         ir_node   *ins[4];
180         ident     *name = new_id_from_str("__firmprof_initializer");
181         ir_entity *ent  = new_entity(get_glob_type(), name, new_type_method(name, 0, 0));
182         ir_node   *ret, *call, *symconst;
183         symconst_symbol sym;
184
185         ident     *init_name = new_id_from_str("__init_firmprof");
186         ir_type   *init_type = new_type_method(init_name, 4, 0);
187         ir_type   *uint, *uintptr, *string;
188         ir_entity *init_ent;
189         ir_graph  *irg;
190         ir_node   *bb;
191         ir_type   *empty_frame_type;
192
193         set_entity_ld_ident(ent, name);
194
195         uint    = new_type_primitive(new_id_from_str("__uint"), mode_Iu);
196         uintptr = new_type_pointer(new_id_from_str("__uintptr"), uint, mode_P);
197         string  = new_type_pointer(new_id_from_str("__charptr"), new_type_primitive(new_id_from_str("__char"), mode_Bs), mode_P);
198
199         set_method_param_type(init_type, 0, string);
200         set_method_param_type(init_type, 1, uintptr);
201         set_method_param_type(init_type, 2, uintptr);
202         set_method_param_type(init_type, 3, uint);
203         init_ent = new_entity(get_glob_type(), init_name, init_type);
204         set_entity_ld_ident(init_ent, init_name);
205
206         irg = new_ir_graph(ent, 0);
207         empty_frame_type = get_irg_frame_type(irg);
208         set_type_size_bytes(empty_frame_type, 0);
209
210         bb = get_cur_block();
211
212         start_block = get_irg_start_block(irg);
213
214         sym.entity_p = init_ent;
215         symconst     = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
216
217         sym.entity_p = ent_filename;
218         ins[0] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
219         sym.entity_p = bblock_id;
220         ins[1] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
221         sym.entity_p = bblock_counts;
222         ins[2] = new_r_SymConst(irg, start_block, sym, symconst_addr_ent);
223         ins[3] = new_r_Const_long(irg, start_block, mode_Iu, n_blocks);
224
225         call = new_r_Call(irg, bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
226         ret = new_r_Return(irg, bb, new_r_Proj(irg, bb, call, mode_M, pn_Call_M_regular), 0, NULL);
227         mature_immBlock(bb);
228
229         add_immBlock_pred(get_irg_end_block(irg), ret);
230         mature_immBlock(get_irg_end_block(irg));
231
232         irg_finalize_cons(irg);
233
234         return irg;
235 }
236
237 /**
238  * Create the location data for the given debug info.
239  */
240 static void create_location_data(dbg_info *dbg, block_id_walker_data_t *wd)
241 {
242         unsigned lineno;
243         const char *fname = be_retrieve_dbg_info(dbg, &lineno);
244
245         if (fname) {
246                 pmap_entry *entry = pmap_find(wd->fname_map, (void *)fname);
247                 ir_entity  *ent;
248
249                 if (! entry) {
250                         static unsigned nr = 0;
251                         ident   *id;
252                         char    buf[128];
253                         ir_type *arr;
254                         int     i, len = strlen(fname) + 1;
255                         tarval  **tarval_string;
256
257                         snprintf(buf, sizeof(buf), "firm_name_arr.%d", nr);
258                         arr = new_type_array(new_id_from_str(buf), 1, wd->tp_char);
259                         set_array_bounds_int(arr, 0, 0, len);
260
261                         snprintf(buf, sizeof(buf), "__firm_name.%d", nr++);
262                         id = new_id_from_str(buf);
263                         ent = new_entity(get_glob_type(), id, arr);
264                         set_entity_ld_ident(ent, id);
265
266                         pmap_insert(wd->fname_map, (void *)fname, ent);
267
268                         /* initialize file name string constant */
269                         tarval_string = alloca(sizeof(*tarval_string) * (len));
270                         for (i = 0; i < len; ++i) {
271                                 tarval_string[i] = new_tarval_from_long(fname[i], mode_Bs);
272                         }
273                         set_entity_variability(ent, variability_constant);
274                         set_array_entity_values(ent, tarval_string, len);
275                 } else {
276                         ent = entry->value;
277                 }
278                 wd->locs[wd->id].fname  = ent;
279                 wd->locs[wd->id].lineno = lineno;
280         } else {
281                 wd->locs[wd->id].fname  = NULL;
282                 wd->locs[wd->id].lineno = 0;
283         }
284 }
285
286 /**
287  * Walker: assigns an ID to every block.
288  * Builds the string table
289  */
290 static void
291 block_id_walker(ir_node * bb, void * data)
292 {
293         block_id_walker_data_t *wd = data;
294
295         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
296         instrument_block(bb, wd->symconst, wd->id);
297
298         if (wd->flags & profile_with_locations) {
299                 dbg_info *dbg = get_irn_dbg_info(bb);
300                 create_location_data(dbg, wd);
301         }
302         ++wd->id;
303 }
304
305 #define IDENT(x)        new_id_from_chars(x, sizeof(x) - 1)
306
307 ir_graph *
308 be_profile_instrument(const char *filename, unsigned flags)
309 {
310         int n, i;
311         unsigned int n_blocks = 0;
312         ir_entity *bblock_id;
313         ir_entity *bblock_counts;
314         ir_entity *ent_filename;
315         ir_entity *ent_locations = NULL;
316         ir_entity *loc_lineno = NULL;
317         ir_entity *loc_name = NULL;
318         ir_entity *ent;
319         ir_type *array_type;
320         ir_type *uint_type;
321         ir_type *string_type;
322         ir_type *character_type;
323         ir_type *loc_type = NULL;
324         ir_type *charptr_type;
325         ir_type *gtp;
326         tarval **tarval_array;
327         tarval **tarval_string;
328         tarval *tv;
329         int filename_len = strlen(filename)+1;
330         ident *cur_ident;
331         int align_l, align_n, size;
332         ir_graph *rem;
333         block_id_walker_data_t  wd;
334         symconst_symbol sym;
335
336         /* count the number of block first */
337         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
338                 ir_graph *irg = get_irp_irg(n);
339
340                 n_blocks += count_blocks(irg);
341         }
342
343         /* create all the necessary types and entities. Note that the
344            types must have a fixed layout, because we already running in the
345            backend */
346         uint_type      = new_type_primitive(IDENT("__uint"), mode_Iu);
347         set_type_alignment_bytes(uint_type, get_type_size_bytes(uint_type));
348         array_type     = new_type_array(IDENT("__block_info_array"), 1, uint_type);
349         set_array_bounds_int(array_type, 0, 0, n_blocks);
350
351         character_type = new_type_primitive(IDENT("__char"), mode_Bs);
352         string_type    = new_type_array(IDENT("__filename"), 1, character_type);
353         set_array_bounds_int(string_type, 0, 0, filename_len);
354
355         gtp            = get_glob_type();
356
357         cur_ident      = IDENT("__FIRMPROF__BLOCK_IDS");
358         bblock_id      = new_entity(gtp, cur_ident, array_type);
359         set_entity_ld_ident(bblock_id, cur_ident);
360         set_entity_variability(bblock_id, variability_initialized);
361
362         cur_ident      = IDENT("__FIRMPROF__BLOCK_COUNTS");
363         bblock_counts  = new_entity(gtp, cur_ident, array_type);
364         set_entity_ld_ident(bblock_counts, cur_ident);
365         set_entity_variability(bblock_counts, variability_initialized);
366
367         cur_ident      = IDENT("__FIRMPROF__FILE_NAME");
368         ent_filename   = new_entity(gtp, cur_ident, string_type);
369         set_entity_ld_ident(ent_filename, cur_ident);
370
371         if (flags & profile_with_locations) {
372                 loc_type       = new_type_struct(IDENT("__location"));
373                 loc_lineno     = new_entity(loc_type, IDENT("lineno"), uint_type);
374                 align_l        = get_type_alignment_bytes(uint_type);
375                 size           = get_type_size_bytes(uint_type);
376                 set_entity_offset(loc_lineno, 0);
377
378                 charptr_type   = new_type_pointer(IDENT("__charptr"), character_type, mode_P_data);
379                 align_n        = get_type_size_bytes(charptr_type);
380                 set_type_alignment_bytes(charptr_type, align_n);
381                 loc_name       = new_entity(loc_type, IDENT("name"), charptr_type);
382                 size           = (size + align_n - 1) & -align_n;
383                 set_entity_offset(loc_name, size);
384                 size          += align_n;
385
386                 if (align_n > align_l)
387                         align_l = align_n;
388                 size = (size + align_l - 1) & -align_l;
389                 set_type_size_bytes(loc_type, size);
390                 set_type_state(loc_type, layout_fixed);
391
392                 loc_type = new_type_array(IDENT("__locarray"), 1, loc_type);
393                 set_array_bounds_int(string_type, 0, 0, n_blocks);
394
395                 cur_ident      = IDENT("__FIRMPROF__LOCATIONS");
396                 ent_locations   = new_entity(gtp, cur_ident, loc_type);
397                 set_entity_ld_ident(ent_locations, cur_ident);
398         }
399
400         /* initialize count array */
401         NEW_ARR_A(tarval *, tarval_array, n_blocks);
402         tv = get_tarval_null(mode_Iu);
403         for (i = 0; i < n_blocks; ++i) {
404                 tarval_array[i] = tv;
405         }
406         set_array_entity_values(bblock_counts, tarval_array, n_blocks);
407
408         /* initialize function name string constant */
409         tarval_string = alloca(sizeof(*tarval_string) * (filename_len));
410         for (i = 0; i < filename_len; ++i) {
411                 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
412         }
413         set_entity_variability(ent_filename, variability_constant);
414         set_array_entity_values(ent_filename, tarval_string, filename_len);
415
416         /* initialize block id array and instrument blocks */
417         wd.array     = tarval_array;
418         wd.id        = 0;
419         wd.tp_char   = character_type;
420         wd.flags     = flags;
421         if (flags & profile_with_locations) {
422                 wd.fname_map = pmap_create();
423                 NEW_ARR_A(loc_entry, wd.locs, n_blocks);
424         }
425
426         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
427                 ir_graph      *irg = get_irp_irg(n);
428                 int            i;
429                 ir_node       *endbb = get_irg_end_block(irg);
430                 fix_env       env;
431
432                 set_current_ir_graph(irg);
433
434                 /* generate a symbolic constant pointing to the count array */
435                 sym.entity_p = bblock_counts;
436                 wd.symconst  = new_r_SymConst(irg, get_irg_start_block(irg), sym, symconst_addr_ent);
437
438                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
439                 env.start_block = get_irg_start_block(irg);
440                 env.end_block   = get_irg_end_block(irg);
441                 set_irn_link(env.start_block, get_irg_no_mem(irg));
442                 irg_block_walk_graph(irg, fix_ssa, NULL, &env);
443                 for (i = get_Block_n_cfgpreds(endbb) - 1; i >= 0; --i) {
444                         ir_node *node = skip_Proj(get_Block_cfgpred(endbb, i));
445                         ir_node *bb   = get_Block_cfgpred_block(endbb, i);
446                         ir_node *sync;
447                         ir_node *ins[2];
448
449                         switch (get_irn_opcode(node)) {
450                         case iro_Return:
451                                 ins[0] = get_irn_link(bb);
452                                 ins[1] = get_Return_mem(node);
453                                 sync   = new_r_Sync(irg, bb, 2, ins);
454                                 set_Return_mem(node, sync);
455                                 break;
456                         case iro_Raise:
457                                 ins[0] = get_irn_link(bb);
458                                 ins[1] = get_Raise_mem(node);
459                                 sync   = new_r_Sync(irg, bb, 2, ins);
460                                 set_Raise_mem(node, sync);
461                                 break;
462                         default:
463                                 /* a fragile's op exception. There should be another path to End,
464                                    so ignore it */
465                                 assert(is_fragile_op(node) && "unexpected End control flow predecessor");
466                         }
467                 }
468         }
469         set_array_entity_values(bblock_id, tarval_array, n_blocks);
470
471         if (flags & profile_with_locations) {
472                 /* build the initializer for the locations */
473                 rem = current_ir_graph;
474                 current_ir_graph = get_const_code_irg();
475                 ent = get_array_element_entity(loc_type);
476                 set_entity_variability(ent_locations, variability_constant);
477                 for (i = 0; i < n_blocks; ++i) {
478                         compound_graph_path *path;
479                         tarval *tv;
480                         ir_node *n;
481
482                         /* lineno */
483                         path = new_compound_graph_path(loc_type, 2);
484                         set_compound_graph_path_array_index(path, 0, i);
485                         set_compound_graph_path_node(path, 0, ent);
486                         set_compound_graph_path_node(path, 1, loc_lineno);
487                         tv = new_tarval_from_long(wd.locs[i].lineno, mode_Iu);
488                         add_compound_ent_value_w_path(ent_locations, new_Const(mode_Iu, tv), path);
489
490                         /* name */
491                         path = new_compound_graph_path(loc_type, 2);
492                         set_compound_graph_path_array_index(path, 0, i);
493                         set_compound_graph_path_node(path, 0, ent);
494                         set_compound_graph_path_node(path, 1, loc_name);
495                         if (wd.locs[i].fname) {
496                                 sym.entity_p = wd.locs[i].fname;
497                                 n = new_SymConst(sym, symconst_addr_ent);
498                         } else {
499                                 n = new_Const(mode_P_data, get_mode_null(mode_P_data));
500                         }
501                         add_compound_ent_value_w_path(ent_locations, n, path);
502                 }
503                 pmap_destroy(wd.fname_map);
504         }
505         return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
506 }
507
508 static void
509 profile_node_info(void *ctx, FILE *f, const ir_node *irn)
510 {
511         if(is_Block(irn)) {
512                 fprintf(f, "profiled execution count: %u\n", be_profile_get_block_execcount(irn));
513         }
514 }
515
516 static void
517 register_vcg_hook(void)
518 {
519         memset(&hook, 0, sizeof(hook));
520         hook.hook._hook_node_info = profile_node_info;
521         register_hook(hook_node_info, &hook);
522 }
523
524 static void
525 unregister_vcg_hook(void)
526 {
527         unregister_hook(hook_node_info, &hook);
528 }
529
530 /**
531  * Reads the corresponding profile info file if it exists and returns a
532  * profile info struct
533  */
534 void
535 be_profile_read(const char *filename)
536 {
537         FILE   *f;
538         char    buf[8];
539         size_t  ret;
540
541         f = fopen(filename, "r");
542         if(f == NULL) {
543                 return;
544         }
545         printf("found profile data '%s'.\n", filename);
546
547         /* check magic */
548         ret = fread(buf, 8, 1, f);
549         if(ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
550                 return;
551         }
552
553         if(profile) be_profile_free();
554         profile = new_set(cmp_execcount, 16);
555
556         do {
557                 execcount_t  query;
558                 ret = fread(&query, sizeof(unsigned int), 2, f);
559
560                 if(ret != 2) break;
561
562                 set_insert(profile, &query, sizeof(query), query.block);
563         } while(1);
564
565         fclose(f);
566         register_vcg_hook();
567 }
568
569 /**
570  * Frees the profile info
571  */
572 void
573 be_profile_free(void)
574 {
575         if(profile) {
576                 unregister_vcg_hook();
577                 del_set(profile);
578         }
579 }
580
581 /**
582  * Tells whether profile module has acquired data
583  */
584 int
585 be_profile_has_data(void)
586 {
587         return (profile != NULL);
588 }
589
590 /**
591  * Get block execution count as determined be profiling
592  */
593 unsigned int
594 be_profile_get_block_execcount(const ir_node *block)
595 {
596         execcount_t *ec, query;
597
598         if(!profile)
599                 return 1;
600
601         query.block = get_irn_node_nr(block);
602         ec = set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
603
604         if(ec != NULL) {
605                 return ec->count;
606         } else {
607                 ir_fprintf(stderr, "Warning: Profile contains no data for %+F\n",
608                            block);
609                 return 1;
610         }
611 }
612
613 typedef struct _intialize_execfreq_env_t {
614         ir_graph *irg;
615         ir_exec_freq *execfreqs;
616         double freq_factor;
617 } initialize_execfreq_env_t;
618
619 // minimal execution frequency (an execfreq of 0 confuses algos)
620 static const double MIN_EXECFREQ = 0.00001;
621
622 static void initialize_execfreq(ir_node *block, void *data) {
623         initialize_execfreq_env_t *env = data;
624         double freq;
625
626         if(block == get_irg_start_block(env->irg)
627            || block == get_irg_end_block(env->irg)) {
628                 freq = 1.0;
629         } else {
630                 freq = be_profile_get_block_execcount(block);
631                 freq *= env->freq_factor;
632                 if(freq < MIN_EXECFREQ)
633                         freq = MIN_EXECFREQ;
634         }
635
636         set_execfreq(env->execfreqs, block, freq);
637 }
638
639 ir_exec_freq *be_create_execfreqs_from_profile(ir_graph *irg)
640 {
641         ir_node *block2 = NULL;
642         ir_node *start_block;
643         const ir_edge_t *edge;
644         initialize_execfreq_env_t env;
645         unsigned count;
646
647         env.irg = irg;
648         env.execfreqs = create_execfreq(irg);
649
650         // find the successor to the start block
651         start_block = get_irg_start_block(irg);
652         foreach_block_succ(start_block, edge) {
653                 ir_node *succ = get_edge_src_irn(edge);
654                 if(succ != start_block) {
655                         block2 = succ;
656                         break;
657                 }
658         }
659         assert(block2 != NULL);
660
661         count = be_profile_get_block_execcount(block2);
662         if(count == 0) {
663                 // the function was never executed, so fallback to estimated freqs
664                 free_execfreq(env.execfreqs);
665
666                 return compute_execfreq(irg, 10);
667         }
668
669         env.freq_factor = 1.0 / count;
670         irg_block_walk_graph(irg, initialize_execfreq, NULL, &env);
671
672         return env.execfreqs;
673 }