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