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