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