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