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