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