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