Fixed size_t issues.
[libfirm] / ir / ir / irprofile.c
1 /*
2  * Copyright (C) 1995-2011 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         ir_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 = (const execcount_t*)a;
80         const execcount_t *eb = (const execcount_t*)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 *count = (unsigned*)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 instrument_block(ir_node *bb, ir_node *address, unsigned int id)
114 {
115         ir_graph *irg = get_irn_irg(bb);
116         ir_node  *load, *store, *offset, *add, *projm, *proji, *unknown;
117         ir_node  *cnst;
118
119         /**
120          * We can't instrument the end block as there are no real instructions there
121          */
122         if (bb == get_irg_end_block(irg))
123                 return;
124
125         unknown = new_r_Unknown(irg, mode_M);
126         cnst    = new_r_Const_long(irg, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
127         offset  = new_r_Add(bb, address, cnst, get_modeP_data());
128         load    = new_r_Load(bb, unknown, offset, mode_Iu, cons_none);
129         projm   = new_r_Proj(load, mode_M, pn_Load_M);
130         proji   = new_r_Proj(load, mode_Iu, pn_Load_res);
131         cnst    = new_r_Const_long(irg, mode_Iu, 1);
132         add     = new_r_Add(bb, proji, cnst, mode_Iu);
133         store   = new_r_Store(bb, projm, offset, add, cons_none);
134         projm   = new_r_Proj(store, mode_M, pn_Store_M);
135         set_irn_link(bb, projm);
136         set_irn_link(projm, load);
137 }
138
139 typedef struct fix_env {
140         ir_node *end_block;
141 } fix_env;
142
143 /**
144  * SSA Construction for instrumentation code memory
145  */
146 static void fix_ssa(ir_node *bb, void *data)
147 {
148         fix_env *env = (fix_env*)data;
149         ir_node *mem;
150         int     arity = get_Block_n_cfgpreds(bb);
151
152         /* end block are not instrumented, skip! */
153         if (bb == env->end_block)
154                 return;
155
156         if (bb == get_irg_start_block(get_irn_irg(bb))) {
157                 mem = get_irg_initial_mem(get_irn_irg(bb));
158         } else if (arity == 1) {
159                 mem = (ir_node*)get_irn_link(get_Block_cfgpred_block(bb, 0));
160         } else {
161                 int n;
162                 ir_node **ins;
163
164                 NEW_ARR_A(ir_node*, ins, arity);
165                 for (n = arity - 1; n >= 0; --n) {
166                         ins[n] = (ir_node*)get_irn_link(get_Block_cfgpred_block(bb, n));
167                 }
168                 mem = new_r_Phi(bb, arity, ins, mode_M);
169         }
170         set_Load_mem((ir_node*)get_irn_link((ir_node*)get_irn_link(bb)), mem);
171 }
172
173 static void add_constructor(ir_entity *method)
174 {
175     ir_type   *method_type  = get_entity_type(method);
176     ir_type   *ptr_type     = new_type_pointer(method_type);
177
178     ir_type   *constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
179     ident     *ide          = id_unique("constructor_ptr.%u");
180     ir_entity *ptr          = new_entity(constructors, ide, ptr_type);
181         ir_graph  *irg          = get_const_code_irg();
182     ir_node   *val          = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
183                                                            method);
184
185     set_entity_compiler_generated(ptr, 1);
186     set_entity_linkage(ptr, IR_LINKAGE_CONSTANT);
187     set_atomic_ent_value(ptr, val);
188 }
189
190 /**
191  * Generates a new irg which calls the initializer
192  *
193  * Pseudocode:
194  *    void __firmprof_initializer(void) { __init_firmprof(ent_filename, bblock_id, bblock_counts, n_blocks); }
195  */
196 static ir_graph *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(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                         size_t      i, len = strlen(fname) + 1;
275                         ir_tarval **tarval_string;
276
277                         snprintf(buf, sizeof(buf), "firm_name_arr.%u", 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.%u", 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(ir_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_linkage(ent, IR_LINKAGE_CONSTANT);
294                         set_array_entity_values(ent, tarval_string, len);
295                 } else {
296                         ent = (ir_entity*)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 block_id_walker(ir_node *bb, void *data)
311 {
312         block_id_walker_data_t *wd = (block_id_walker_data_t*)data;
313
314         wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
315         instrument_block(bb, wd->symconst, wd->id);
316
317         if (wd->flags & profile_with_locations) {
318                 dbg_info *dbg = get_irn_dbg_info(bb);
319                 create_location_data(dbg, wd);
320         }
321         ++wd->id;
322 }
323
324 #define IDENT(x)    new_id_from_chars(x, sizeof(x) - 1)
325
326 ir_graph *ir_profile_instrument(const char *filename, unsigned flags)
327 {
328         int n, i;
329         int n_blocks = 0;
330         ir_entity  *bblock_id;
331         ir_entity  *bblock_counts;
332         ir_entity  *ent_filename;
333         ir_entity  *ent_locations = NULL;
334         ir_entity  *loc_lineno = NULL;
335         ir_entity  *loc_name = NULL;
336         ir_entity  *ent;
337         ir_type    *array_type;
338         ir_type    *uint_type;
339         ir_type    *string_type;
340         ir_type    *character_type;
341         ir_type    *loc_type = NULL;
342         ir_type    *charptr_type;
343         ir_type    *gtp;
344         ir_tarval **tarval_array;
345         ir_tarval **tarval_string;
346         ir_tarval *tv;
347         int filename_len = strlen(filename)+1;
348         ident *cur_ident;
349         unsigned align_l, align_n, size;
350         block_id_walker_data_t  wd;
351         symconst_symbol sym;
352
353         /* count the number of block first */
354         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
355                 ir_graph *irg = get_irp_irg(n);
356
357                 n_blocks += count_blocks(irg);
358         }
359
360         /* create all the necessary types and entities. Note that the
361            types must have a fixed layout, because we already running in the
362            backend */
363         uint_type      = new_type_primitive(mode_Iu);
364         set_type_alignment_bytes(uint_type, get_type_size_bytes(uint_type));
365
366         array_type     = new_type_array(1, uint_type);
367         set_array_bounds_int(array_type, 0, 0, n_blocks);
368         set_type_size_bytes(array_type, n_blocks * get_mode_size_bytes(mode_Iu));
369         set_type_alignment_bytes(array_type, get_mode_size_bytes(mode_Iu));
370         set_type_state(array_type, layout_fixed);
371
372         character_type = new_type_primitive(mode_Bs);
373         string_type    = new_type_array(1, character_type);
374         set_array_bounds_int(string_type, 0, 0, filename_len);
375         set_type_size_bytes(string_type, filename_len);
376         set_type_alignment_bytes(string_type, 1);
377         set_type_state(string_type, layout_fixed);
378
379         gtp            = get_glob_type();
380
381         cur_ident      = IDENT("__FIRMPROF__BLOCK_IDS");
382         bblock_id      = new_entity(gtp, cur_ident, array_type);
383         set_entity_ld_ident(bblock_id, cur_ident);
384
385         cur_ident      = IDENT("__FIRMPROF__BLOCK_COUNTS");
386         bblock_counts  = new_entity(gtp, cur_ident, array_type);
387         set_entity_ld_ident(bblock_counts, cur_ident);
388
389         cur_ident      = IDENT("__FIRMPROF__FILE_NAME");
390         ent_filename   = new_entity(gtp, cur_ident, string_type);
391         set_entity_ld_ident(ent_filename, cur_ident);
392
393         if (flags & profile_with_locations) {
394                 loc_type       = new_type_struct(IDENT("__location"));
395                 loc_lineno     = new_entity(loc_type, IDENT("lineno"), uint_type);
396                 align_l        = get_type_alignment_bytes(uint_type);
397                 size           = get_type_size_bytes(uint_type);
398                 set_entity_offset(loc_lineno, 0);
399
400                 charptr_type   = new_type_pointer(character_type);
401                 align_n        = get_type_size_bytes(charptr_type);
402                 set_type_alignment_bytes(charptr_type, align_n);
403                 loc_name       = new_entity(loc_type, IDENT("name"), charptr_type);
404                 size           = (size + align_n - 1) & ~(align_n - 1);
405                 set_entity_offset(loc_name, size);
406                 size          += align_n;
407
408                 if (align_n > align_l)
409                         align_l = align_n;
410                 size = (size + align_l - 1) & ~(align_l - 1);
411                 set_type_size_bytes(loc_type, size);
412                 set_type_state(loc_type, layout_fixed);
413
414                 loc_type = new_type_array(1, loc_type);
415                 set_array_bounds_int(string_type, 0, 0, n_blocks);
416
417                 cur_ident     = IDENT("__FIRMPROF__LOCATIONS");
418                 ent_locations = new_entity(gtp, cur_ident, loc_type);
419                 set_entity_ld_ident(ent_locations, cur_ident);
420         }
421
422         /* initialize count array */
423         NEW_ARR_A(ir_tarval *, tarval_array, n_blocks);
424         tv = get_mode_null(mode_Iu);
425         for (i = 0; i < n_blocks; ++i) {
426                 tarval_array[i] = tv;
427         }
428         set_array_entity_values(bblock_counts, tarval_array, n_blocks);
429
430         /* initialize function name string constant */
431         tarval_string = ALLOCAN(ir_tarval*, filename_len);
432         for (i = 0; i < filename_len; ++i) {
433                 tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
434         }
435         set_entity_linkage(ent_filename, IR_LINKAGE_CONSTANT);
436         set_array_entity_values(ent_filename, tarval_string, filename_len);
437
438         /* initialize block id array and instrument blocks */
439         wd.array     = tarval_array;
440         wd.id        = 0;
441         wd.tp_char   = character_type;
442         wd.flags     = flags;
443         if (flags & profile_with_locations) {
444                 wd.fname_map = pmap_create();
445                 NEW_ARR_A(loc_entry, wd.locs, n_blocks);
446         }
447
448         for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
449                 ir_graph *irg   = get_irp_irg(i);
450                 ir_node  *endbb = get_irg_end_block(irg);
451                 int       i;
452                 fix_env   env;
453
454                 /* generate a symbolic constant pointing to the count array */
455                 sym.entity_p = bblock_counts;
456                 wd.symconst  = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
457
458                 irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
459                 env.end_block = get_irg_end_block(irg);
460                 irg_block_walk_graph(irg, fix_ssa, NULL, &env);
461                 for (i = get_Block_n_cfgpreds(endbb) - 1; i >= 0; --i) {
462                         ir_node *node = skip_Proj(get_Block_cfgpred(endbb, i));
463                         ir_node *bb   = get_Block_cfgpred_block(endbb, i);
464                         ir_node *sync;
465                         ir_node *ins[2];
466
467                         switch (get_irn_opcode(node)) {
468                         case iro_Return:
469                                 ins[0] = (ir_node*)get_irn_link(bb);
470                                 ins[1] = get_Return_mem(node);
471                                 sync   = new_r_Sync(bb, 2, ins);
472                                 set_Return_mem(node, sync);
473                                 break;
474                         case iro_Raise:
475                                 ins[0] = (ir_node*)get_irn_link(bb);
476                                 ins[1] = get_Raise_mem(node);
477                                 sync   = new_r_Sync(bb, 2, ins);
478                                 set_Raise_mem(node, sync);
479                                 break;
480                         default:
481                                 /* a fragile's op exception. There should be another path to End,
482                                    so ignore it */
483                                 assert(is_fragile_op(node) && "unexpected End control flow predecessor");
484                         }
485                 }
486         }
487         set_array_entity_values(bblock_id, tarval_array, n_blocks);
488
489         if (flags & profile_with_locations) {
490                 ir_graph *irg = get_const_code_irg();
491                 /* build the initializer for the locations */
492                 ent = get_array_element_entity(loc_type);
493                 set_entity_linkage(ent_locations, IR_LINKAGE_CONSTANT);
494                 for (i = 0; i < n_blocks; ++i) {
495                         compound_graph_path *path;
496                         ir_tarval *tv;
497                         ir_node *n;
498
499                         /* lineno */
500                         path = new_compound_graph_path(loc_type, 2);
501                         set_compound_graph_path_array_index(path, 0, i);
502                         set_compound_graph_path_node(path, 0, ent);
503                         set_compound_graph_path_node(path, 1, loc_lineno);
504                         tv = new_tarval_from_long(wd.locs[i].lineno, mode_Iu);
505                         add_compound_ent_value_w_path(ent_locations, new_r_Const(irg, tv), path);
506
507                         /* name */
508                         path = new_compound_graph_path(loc_type, 2);
509                         set_compound_graph_path_array_index(path, 0, i);
510                         set_compound_graph_path_node(path, 0, ent);
511                         set_compound_graph_path_node(path, 1, loc_name);
512                         if (wd.locs[i].fname) {
513                                 sym.entity_p = wd.locs[i].fname;
514                                 n = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
515                         } else {
516                                 n = new_r_Const(irg, get_mode_null(mode_P_data));
517                         }
518                         add_compound_ent_value_w_path(ent_locations, n, path);
519                 }
520                 pmap_destroy(wd.fname_map);
521         }
522         return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
523 }
524
525 static void profile_node_info(void *ctx, FILE *f, const ir_node *irn)
526 {
527         (void) ctx;
528         if (is_Block(irn)) {
529                 fprintf(f, "profiled execution count: %u\n", ir_profile_get_block_execcount(irn));
530         }
531 }
532
533 static void register_vcg_hook(void)
534 {
535         memset(&hook, 0, sizeof(hook));
536         hook.hook._hook_node_info = profile_node_info;
537         register_hook(hook_node_info, &hook);
538 }
539
540 static void unregister_vcg_hook(void)
541 {
542         unregister_hook(hook_node_info, &hook);
543 }
544
545 /**
546  * Reads the corresponding profile info file if it exists and returns a
547  * profile info struct
548  */
549 void ir_profile_read(const char *filename)
550 {
551         FILE   *f;
552         char    buf[8];
553         size_t  ret;
554
555         f = fopen(filename, "r");
556         if (f == NULL) {
557                 return;
558         }
559         printf("found profile data '%s'.\n", filename);
560
561         /* check magic */
562         ret = fread(buf, 8, 1, f);
563         if (ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
564                 return;
565         }
566
567         if (profile) ir_profile_free();
568         profile = new_set(cmp_execcount, 16);
569
570         for (;;) {
571                 execcount_t  query;
572                 ret = fread(&query, sizeof(unsigned int), 2, f);
573
574                 if (ret != 2) break;
575
576                 set_insert(profile, &query, sizeof(query), query.block);
577         }
578
579         fclose(f);
580         register_vcg_hook();
581 }
582
583 /**
584  * Frees the profile info
585  */
586 void ir_profile_free(void)
587 {
588         if (profile) {
589                 unregister_vcg_hook();
590                 del_set(profile);
591         }
592 }
593
594 /**
595  * Tells whether profile module has acquired data
596  */
597 int ir_profile_has_data(void)
598 {
599         return (profile != NULL);
600 }
601
602 /**
603  * Get block execution count as determined be profiling
604  */
605 unsigned int
606 ir_profile_get_block_execcount(const ir_node *block)
607 {
608         execcount_t *ec, query;
609
610         if (!profile)
611                 return 1;
612
613         query.block = get_irn_node_nr(block);
614         ec = (execcount_t*)set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
615
616         if (ec != NULL) {
617                 return ec->count;
618         } else {
619                 ir_fprintf(stderr, "Warning: Profile contains no data for %+F\n",
620                            block);
621                 return 1;
622         }
623 }
624
625 typedef struct intialize_execfreq_env_t {
626         ir_graph *irg;
627         ir_exec_freq *execfreqs;
628         double freq_factor;
629 } initialize_execfreq_env_t;
630
631 // minimal execution frequency (an execfreq of 0 confuses algos)
632 static const double MIN_EXECFREQ = 0.00001;
633
634 static void initialize_execfreq(ir_node *block, void *data)
635 {
636         initialize_execfreq_env_t *env = (initialize_execfreq_env_t*)data;
637         double freq;
638
639         if (block == get_irg_start_block(env->irg)
640            || block == get_irg_end_block(env->irg)) {
641                 freq = 1.0;
642         } else {
643                 freq = ir_profile_get_block_execcount(block);
644                 freq *= env->freq_factor;
645                 if (freq < MIN_EXECFREQ)
646                         freq = MIN_EXECFREQ;
647         }
648
649         set_execfreq(env->execfreqs, block, freq);
650 }
651
652 ir_exec_freq *ir_create_execfreqs_from_profile(ir_graph *irg)
653 {
654         ir_node *start_block;
655         initialize_execfreq_env_t env;
656         unsigned count;
657
658         env.irg = irg;
659         env.execfreqs = create_execfreq(irg);
660         start_block = get_irg_start_block(irg);
661
662         count = ir_profile_get_block_execcount(start_block);
663         if (count == 0) {
664                 // the function was never executed, so fallback to estimated freqs
665                 free_execfreq(env.execfreqs);
666
667                 return compute_execfreq(irg, 10);
668         }
669
670         env.freq_factor = 1.0 / count;
671         irg_block_walk_graph(irg, initialize_execfreq, NULL, &env);
672
673         return env.execfreqs;
674 }