irprofile.c 18.8 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Christian Würdig's avatar
Christian Würdig committed
20
21
22
23
24
25
/**
 * @file
 * @brief       Code instrumentation and execution count profiling.
 * @author      Adam M. Szalkowski
 * @date        06.04.2006
 * @version     $Id$
26
27
28
29
30
31
32
33
34
35
36
 */
#include "config.h"

#include <math.h>

#include "hashptr.h"
#include "debug.h"
#include "obst.h"
#include "set.h"
#include "list.h"
#include "pmap.h"
37
#include "array_t.h"
38
39
40
41
42
43
44

#include "irprintf.h"
#include "irgwalk.h"
#include "irdump_t.h"
#include "irnode_t.h"
#include "ircons_t.h"
#include "execfreq.h"
45
#include "typerep.h"
46

Michael Beck's avatar
Michael Beck committed
47
#include "dbginfo.h"
48
#include "irhooks.h"
49
#include "iredges.h"
50

Michael Beck's avatar
Michael Beck committed
51
#include "irprofile.h"
52

53
54
/** An entry in the id-to-location map */
typedef struct loc_entry {
55
	ir_entity    *fname;   /**< the entity holding the file name */
56
57
58
	unsigned int lineno;   /**< line number */
} loc_entry;

59
typedef struct block_id_walker_data_t {
Matthias Braun's avatar
Matthias Braun committed
60
	ir_tarval      **array;    /**< the entity the holds the block counts */
61
62
63
64
65
66
	unsigned int   id;         /**< current block id number */
	ir_node        *symconst;  /**< the SymConst representing array */
	pmap           *fname_map; /**< set containing all found filenames */
	loc_entry      *locs;      /**< locations */
	ir_type        *tp_char;   /**< the character type */
	unsigned       flags;      /**< profile flags */
67
68
} block_id_walker_data_t;

69
typedef struct execcount_t {
Michael Beck's avatar
Michael Beck committed
70
	unsigned long block;
71
72
73
	unsigned int count;
} execcount_t;

Michael Beck's avatar
Michael Beck committed
74
75
76
/**
 * Compare two execcount_t entries.
 */
77
78
static int cmp_execcount(const void *a, const void *b, size_t size)
{
79
80
	const execcount_t *ea = (const execcount_t*)a;
	const execcount_t *eb = (const execcount_t*)b;
81
	(void) size;
Michael Beck's avatar
Michael Beck committed
82
	return ea->block != eb->block;
83
}
84

Michael Beck's avatar
Michael Beck committed
85
86
87
/**
 * Block walker, count number of blocks.
 */
88
89
static void block_counter(ir_node * bb, void * data)
{
90
	unsigned *count = (unsigned*)data;
91
	(void) bb;
92
93
94
	*count = *count + 1;
}

Michael Beck's avatar
Michael Beck committed
95
96
97
/**
 * Return the number of blocks the given graph.
 */
98
99
static unsigned int count_blocks(ir_graph *irg)
{
100
101
102
103
104
105
	unsigned int count = 0;

	irg_block_walk_graph(irg, block_counter, NULL, &count);
	return count;
}

106
107
/* keep the execcounts here because they are only read once per compiler run */
static set * profile = NULL;
108
static hook_entry_t hook;
109

110
111
112
/**
 * Instrument a block with code needed for profiling
 */
113
static void instrument_block(ir_node *bb, ir_node *address, unsigned int id)
114
{
Matthias Braun's avatar
fixes    
Matthias Braun committed
115
	ir_graph *irg = get_irn_irg(bb);
116
117
	ir_node  *load, *store, *offset, *add, *projm, *proji, *unknown;
	ir_node  *cnst;
Matthias Braun's avatar
fixes    
Matthias Braun committed
118

119
	/**
120
	 * We can't instrument the end block as there are no real instructions there
121
	 */
122
	if (bb == get_irg_end_block(irg))
Matthias Braun's avatar
fixes    
Matthias Braun committed
123
124
		return;

125
	unknown = new_r_Unknown(irg, mode_M);
126
	cnst    = new_r_Const_long(irg, mode_Iu, get_mode_size_bytes(mode_Iu) * id);
127
	offset  = new_r_Add(bb, address, cnst, get_modeP_data());
128
	load    = new_r_Load(bb, unknown, offset, mode_Iu, cons_none);
129
130
	projm   = new_r_Proj(load, mode_M, pn_Load_M);
	proji   = new_r_Proj(load, mode_Iu, pn_Load_res);
131
	cnst    = new_r_Const_long(irg, mode_Iu, 1);
132
	add     = new_r_Add(bb, proji, cnst, mode_Iu);
133
	store   = new_r_Store(bb, projm, offset, add, cons_none);
134
	projm   = new_r_Proj(store, mode_M, pn_Store_M);
135
136
	set_irn_link(bb, projm);
	set_irn_link(projm, load);
137
138
}

Michael Beck's avatar
BugFix:    
Michael Beck committed
139
140
141
142
typedef struct fix_env {
	ir_node *end_block;
} fix_env;

143
/**
Michael Beck's avatar
BugFix:    
Michael Beck committed
144
 * SSA Construction for instrumentation code memory
145
 */
146
static void fix_ssa(ir_node *bb, void *data)
147
{
148
	fix_env *env = (fix_env*)data;
Michael Beck's avatar
BugFix:    
Michael Beck committed
149
150
	ir_node *mem;
	int     arity = get_Block_n_cfgpreds(bb);
151

152
	/* end block are not instrumented, skip! */
153
	if (bb == env->end_block)
154
155
		return;

156
	if (bb == get_irg_start_block(get_irn_irg(bb))) {
157
		mem = get_irg_initial_mem(get_irn_irg(bb));
158
	} else if (arity == 1) {
159
		mem = (ir_node*)get_irn_link(get_Block_cfgpred_block(bb, 0));
160
	} else {
Michael Beck's avatar
BugFix:    
Michael Beck committed
161
162
163
164
165
		int n;
		ir_node **ins;

		NEW_ARR_A(ir_node*, ins, arity);
		for (n = arity - 1; n >= 0; --n) {
166
			ins[n] = (ir_node*)get_irn_link(get_Block_cfgpred_block(bb, n));
167
		}
168
		mem = new_r_Phi(bb, arity, ins, mode_M);
169
	}
170
	set_Load_mem((ir_node*)get_irn_link((ir_node*)get_irn_link(bb)), mem);
171
172
}

173
174
175
static void add_constructor(ir_entity *method)
{
    ir_type   *method_type  = get_entity_type(method);
176
    ir_type   *ptr_type     = new_type_pointer(method_type);
177

178
    ir_type   *constructors = get_segment_type(IR_SEGMENT_CONSTRUCTORS);
179
180
181
182
    ident     *ide          = id_unique("constructor_ptr.%u");
    ir_entity *ptr          = new_entity(constructors, ide, ptr_type);
	ir_graph  *irg          = get_const_code_irg();
    ir_node   *val          = new_rd_SymConst_addr_ent(NULL, irg, mode_P_code,
183
	                                                   method);
184
185

    set_entity_compiler_generated(ptr, 1);
Matthias Braun's avatar
Matthias Braun committed
186
    set_entity_linkage(ptr, IR_LINKAGE_CONSTANT);
187
188
    set_atomic_ent_value(ptr, val);
}
189

190
191
/**
 * Generates a new irg which calls the initializer
Michael Beck's avatar
BugFix:    
Michael Beck committed
192
193
 *
 * Pseudocode:
194
 *    void __firmprof_initializer(void) { __init_firmprof(ent_filename, bblock_id, bblock_counts, n_blocks); }
195
 */
196
static ir_graph *gen_initializer_irg(ir_entity *ent_filename, ir_entity *bblock_id, ir_entity *bblock_counts, int n_blocks)
197
{
198
	ir_node   *ins[4];
199
	ident     *name = new_id_from_str("__firmprof_initializer");
200
	ir_entity *ent  = new_entity(get_glob_type(), name, new_type_method(0, 0));
Adam Szalkowski's avatar
Adam Szalkowski committed
201
202
	ir_node   *ret, *call, *symconst;
	symconst_symbol sym;
203
204

	ident     *init_name = new_id_from_str("__init_firmprof");
205
	ir_type   *init_type = new_type_method(4, 0);
206
	ir_type   *uint, *uintptr, *string;
207
	ir_entity *init_ent;
Christian Würdig's avatar
Christian Würdig committed
208
209
	ir_graph  *irg;
	ir_node   *bb;
Christian Würdig's avatar
Christian Würdig committed
210
	ir_type   *empty_frame_type;
Adam Szalkowski's avatar
Adam Szalkowski committed
211

Christian Würdig's avatar
Christian Würdig committed
212
213
	set_entity_ld_ident(ent, name);

214
215
216
	uint    = new_type_primitive(mode_Iu);
	uintptr = new_type_pointer(uint);
	string  = new_type_pointer(new_type_primitive(mode_Bs));
Adam Szalkowski's avatar
Adam Szalkowski committed
217

218
	set_method_param_type(init_type, 0, string);
Adam Szalkowski's avatar
Adam Szalkowski committed
219
	set_method_param_type(init_type, 1, uintptr);
220
221
	set_method_param_type(init_type, 2, uintptr);
	set_method_param_type(init_type, 3, uint);
Christian Würdig's avatar
Christian Würdig committed
222
	init_ent = new_entity(get_glob_type(), init_name, init_type);
Christian Würdig's avatar
Christian Würdig committed
223
	set_entity_ld_ident(init_ent, init_name);
Adam Szalkowski's avatar
Adam Szalkowski committed
224

Christian Würdig's avatar
Christian Würdig committed
225
	irg = new_ir_graph(ent, 0);
Christian Würdig's avatar
Christian Würdig committed
226
227
	empty_frame_type = get_irg_frame_type(irg);
	set_type_size_bytes(empty_frame_type, 0);
228
	set_type_state(empty_frame_type, layout_fixed);
229

Christian Würdig's avatar
Christian Würdig committed
230
	bb = get_cur_block();
Adam Szalkowski's avatar
Adam Szalkowski committed
231
232

	sym.entity_p = init_ent;
233
	symconst     = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
Adam Szalkowski's avatar
Adam Szalkowski committed
234

235
	sym.entity_p = ent_filename;
236
	ins[0] = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
237
	sym.entity_p = bblock_id;
238
	ins[1] = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
239
	sym.entity_p = bblock_counts;
240
	ins[2] = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
241
	ins[3] = new_r_Const_long(irg, mode_Iu, n_blocks);
242

243
	call = new_r_Call(bb, get_irg_initial_mem(irg), symconst, 4, ins, init_type);
244
	ret = new_r_Return(bb, new_r_Proj(call, mode_M, pn_Call_M), 0, NULL);
245
246
247
248
249
	mature_immBlock(bb);

	add_immBlock_pred(get_irg_end_block(irg), ret);
	mature_immBlock(get_irg_end_block(irg));

Matthias Braun's avatar
fixes    
Matthias Braun committed
250
251
	irg_finalize_cons(irg);

252
253
	add_constructor(ent);

254
255
256
	return irg;
}

257
258
259
260
261
262
/**
 * Create the location data for the given debug info.
 */
static void create_location_data(dbg_info *dbg, block_id_walker_data_t *wd)
{
	unsigned lineno;
Michael Beck's avatar
Michael Beck committed
263
	const char *fname = ir_retrieve_dbg_info(dbg, &lineno);
264
265
266

	if (fname) {
		pmap_entry *entry = pmap_find(wd->fname_map, (void *)fname);
267
		ir_entity  *ent;
268
269
270

		if (! entry) {
			static unsigned nr = 0;
Matthias Braun's avatar
Matthias Braun committed
271
272
273
274
275
			ident       *id;
			char        buf[128];
			ir_type     *arr;
			int         i, len = strlen(fname) + 1;
			ir_tarval **tarval_string;
276

277
			snprintf(buf, sizeof(buf), "firm_name_arr.%u", nr);
278
			arr = new_type_array(1, wd->tp_char);
279
280
			set_array_bounds_int(arr, 0, 0, len);

281
			snprintf(buf, sizeof(buf), "__firm_name.%u", nr++);
282
283
284
285
286
287
288
			id = new_id_from_str(buf);
			ent = new_entity(get_glob_type(), id, arr);
			set_entity_ld_ident(ent, id);

			pmap_insert(wd->fname_map, (void *)fname, ent);

			/* initialize file name string constant */
Matthias Braun's avatar
Matthias Braun committed
289
			tarval_string = ALLOCAN(ir_tarval*, len);
290
291
292
			for (i = 0; i < len; ++i) {
				tarval_string[i] = new_tarval_from_long(fname[i], mode_Bs);
			}
Matthias Braun's avatar
Matthias Braun committed
293
			set_entity_linkage(ent, IR_LINKAGE_CONSTANT);
294
295
			set_array_entity_values(ent, tarval_string, len);
		} else {
296
			ent = (ir_entity*)entry->value;
297
298
299
300
301
302
303
304
305
306
307
308
309
		}
		wd->locs[wd->id].fname  = ent;
		wd->locs[wd->id].lineno = lineno;
	} else {
		wd->locs[wd->id].fname  = NULL;
		wd->locs[wd->id].lineno = 0;
	}
}

/**
 * Walker: assigns an ID to every block.
 * Builds the string table
 */
310
static void block_id_walker(ir_node *bb, void *data)
311
{
312
	block_id_walker_data_t *wd = (block_id_walker_data_t*)data;
313

314
	wd->array[wd->id] = new_tarval_from_long(get_irn_node_nr(bb), mode_Iu);
315
	instrument_block(bb, wd->symconst, wd->id);
316
317
318
319
320

	if (wd->flags & profile_with_locations) {
		dbg_info *dbg = get_irn_dbg_info(bb);
		create_location_data(dbg, wd);
	}
321
322
323
	++wd->id;
}

324
#define IDENT(x)    new_id_from_chars(x, sizeof(x) - 1)
325

326
ir_graph *ir_profile_instrument(const char *filename, unsigned flags)
327
{
Matthias Braun's avatar
Matthias Braun committed
328
	int n, i;
329
	int n_blocks = 0;
Matthias Braun's avatar
Matthias Braun committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
	ir_entity  *bblock_id;
	ir_entity  *bblock_counts;
	ir_entity  *ent_filename;
	ir_entity  *ent_locations = NULL;
	ir_entity  *loc_lineno = NULL;
	ir_entity  *loc_name = NULL;
	ir_entity  *ent;
	ir_type    *array_type;
	ir_type    *uint_type;
	ir_type    *string_type;
	ir_type    *character_type;
	ir_type    *loc_type = NULL;
	ir_type    *charptr_type;
	ir_type    *gtp;
	ir_tarval **tarval_array;
	ir_tarval **tarval_string;
	ir_tarval *tv;
Matthias Braun's avatar
Matthias Braun committed
347
348
	int filename_len = strlen(filename)+1;
	ident *cur_ident;
349
	unsigned align_l, align_n, size;
350
	block_id_walker_data_t  wd;
Matthias Braun's avatar
fixes    
Matthias Braun committed
351
	symconst_symbol sym;
352

353
354
355
356
357
358
359
360
361
362
	/* count the number of block first */
	for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
		ir_graph *irg = get_irp_irg(n);

		n_blocks += count_blocks(irg);
	}

	/* create all the necessary types and entities. Note that the
	   types must have a fixed layout, because we already running in the
	   backend */
363
	uint_type      = new_type_primitive(mode_Iu);
364
	set_type_alignment_bytes(uint_type, get_type_size_bytes(uint_type));
365

366
	array_type     = new_type_array(1, uint_type);
367
	set_array_bounds_int(array_type, 0, 0, n_blocks);
368
369
370
	set_type_size_bytes(array_type, n_blocks * get_mode_size_bytes(mode_Iu));
	set_type_alignment_bytes(array_type, get_mode_size_bytes(mode_Iu));
	set_type_state(array_type, layout_fixed);
Christian Würdig's avatar
Christian Würdig committed
371

372
373
	character_type = new_type_primitive(mode_Bs);
	string_type    = new_type_array(1, character_type);
374
	set_array_bounds_int(string_type, 0, 0, filename_len);
375
376
377
	set_type_size_bytes(string_type, filename_len);
	set_type_alignment_bytes(string_type, 1);
	set_type_state(string_type, layout_fixed);
Christian Würdig's avatar
Christian Würdig committed
378

379
380
381
382
	gtp            = get_glob_type();

	cur_ident      = IDENT("__FIRMPROF__BLOCK_IDS");
	bblock_id      = new_entity(gtp, cur_ident, array_type);
Christian Würdig's avatar
Christian Würdig committed
383
384
	set_entity_ld_ident(bblock_id, cur_ident);

385
386
	cur_ident      = IDENT("__FIRMPROF__BLOCK_COUNTS");
	bblock_counts  = new_entity(gtp, cur_ident, array_type);
Christian Würdig's avatar
Christian Würdig committed
387
388
	set_entity_ld_ident(bblock_counts, cur_ident);

389
390
	cur_ident      = IDENT("__FIRMPROF__FILE_NAME");
	ent_filename   = new_entity(gtp, cur_ident, string_type);
Christian Würdig's avatar
Christian Würdig committed
391
	set_entity_ld_ident(ent_filename, cur_ident);
392

393
394
395
396
397
	if (flags & profile_with_locations) {
		loc_type       = new_type_struct(IDENT("__location"));
		loc_lineno     = new_entity(loc_type, IDENT("lineno"), uint_type);
		align_l        = get_type_alignment_bytes(uint_type);
		size           = get_type_size_bytes(uint_type);
398
		set_entity_offset(loc_lineno, 0);
399

400
		charptr_type   = new_type_pointer(character_type);
401
402
403
		align_n        = get_type_size_bytes(charptr_type);
		set_type_alignment_bytes(charptr_type, align_n);
		loc_name       = new_entity(loc_type, IDENT("name"), charptr_type);
404
		size           = (size + align_n - 1) & ~(align_n - 1);
405
		set_entity_offset(loc_name, size);
406
407
408
409
		size          += align_n;

		if (align_n > align_l)
			align_l = align_n;
410
		size = (size + align_l - 1) & ~(align_l - 1);
411
412
413
		set_type_size_bytes(loc_type, size);
		set_type_state(loc_type, layout_fixed);

414
		loc_type = new_type_array(1, loc_type);
415
416
		set_array_bounds_int(string_type, 0, 0, n_blocks);

417
418
		cur_ident     = IDENT("__FIRMPROF__LOCATIONS");
		ent_locations = new_entity(gtp, cur_ident, loc_type);
419
		set_entity_ld_ident(ent_locations, cur_ident);
420
421
422
	}

	/* initialize count array */
Matthias Braun's avatar
Matthias Braun committed
423
	NEW_ARR_A(ir_tarval *, tarval_array, n_blocks);
424
	tv = get_mode_null(mode_Iu);
Christian Würdig's avatar
Christian Würdig committed
425
	for (i = 0; i < n_blocks; ++i) {
Michael Beck's avatar
BugFix:    
Michael Beck committed
426
		tarval_array[i] = tv;
427
	}
Adam Szalkowski's avatar
Adam Szalkowski committed
428
	set_array_entity_values(bblock_counts, tarval_array, n_blocks);
429

430
	/* initialize function name string constant */
Matthias Braun's avatar
Matthias Braun committed
431
	tarval_string = ALLOCAN(ir_tarval*, filename_len);
Christian Würdig's avatar
Christian Würdig committed
432
	for (i = 0; i < filename_len; ++i) {
433
434
		tarval_string[i] = new_tarval_from_long(filename[i], mode_Bs);
	}
Matthias Braun's avatar
Matthias Braun committed
435
	set_entity_linkage(ent_filename, IR_LINKAGE_CONSTANT);
436
437
	set_array_entity_values(ent_filename, tarval_string, filename_len);

438
	/* initialize block id array and instrument blocks */
439
440
441
442
443
444
445
446
447
	wd.array     = tarval_array;
	wd.id        = 0;
	wd.tp_char   = character_type;
	wd.flags     = flags;
	if (flags & profile_with_locations) {
		wd.fname_map = pmap_create();
		NEW_ARR_A(loc_entry, wd.locs, n_blocks);
	}

Christian Würdig's avatar
Christian Würdig committed
448
	for (n = get_irp_n_irgs() - 1; n >= 0; --n) {
449
450
451
452
		ir_graph *irg   = get_irp_irg(i);
		ir_node  *endbb = get_irg_end_block(irg);
		int       i;
		fix_env   env;
453

454
		/* generate a symbolic constant pointing to the count array */
Christian Würdig's avatar
Christian Würdig committed
455
		sym.entity_p = bblock_counts;
456
		wd.symconst  = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
Matthias Braun's avatar
fixes    
Matthias Braun committed
457

458
		irg_block_walk_graph(irg, block_id_walker, NULL, &wd);
459
		env.end_block = get_irg_end_block(irg);
Michael Beck's avatar
BugFix:    
Michael Beck committed
460
461
462
463
		irg_block_walk_graph(irg, fix_ssa, NULL, &env);
		for (i = get_Block_n_cfgpreds(endbb) - 1; i >= 0; --i) {
			ir_node *node = skip_Proj(get_Block_cfgpred(endbb, i));
			ir_node *bb   = get_Block_cfgpred_block(endbb, i);
464
465
466
			ir_node *sync;
			ir_node *ins[2];

Michael Beck's avatar
BugFix:    
Michael Beck committed
467
468
			switch (get_irn_opcode(node)) {
			case iro_Return:
469
				ins[0] = (ir_node*)get_irn_link(bb);
Michael Beck's avatar
BugFix:    
Michael Beck committed
470
				ins[1] = get_Return_mem(node);
471
				sync   = new_r_Sync(bb, 2, ins);
Michael Beck's avatar
BugFix:    
Michael Beck committed
472
473
474
				set_Return_mem(node, sync);
				break;
			case iro_Raise:
475
				ins[0] = (ir_node*)get_irn_link(bb);
Michael Beck's avatar
BugFix:    
Michael Beck committed
476
				ins[1] = get_Raise_mem(node);
477
				sync   = new_r_Sync(bb, 2, ins);
Michael Beck's avatar
BugFix:    
Michael Beck committed
478
479
480
481
482
483
484
				set_Raise_mem(node, sync);
				break;
			default:
				/* a fragile's op exception. There should be another path to End,
				   so ignore it */
				assert(is_fragile_op(node) && "unexpected End control flow predecessor");
			}
485
		}
486
	}
Adam Szalkowski's avatar
Adam Szalkowski committed
487
	set_array_entity_values(bblock_id, tarval_array, n_blocks);
488

489
	if (flags & profile_with_locations) {
490
		ir_graph *irg = get_const_code_irg();
491
492
		/* build the initializer for the locations */
		ent = get_array_element_entity(loc_type);
Matthias Braun's avatar
Matthias Braun committed
493
		set_entity_linkage(ent_locations, IR_LINKAGE_CONSTANT);
494
495
		for (i = 0; i < n_blocks; ++i) {
			compound_graph_path *path;
Matthias Braun's avatar
Matthias Braun committed
496
			ir_tarval *tv;
497
498
499
500
501
502
503
504
			ir_node *n;

			/* lineno */
			path = new_compound_graph_path(loc_type, 2);
			set_compound_graph_path_array_index(path, 0, i);
			set_compound_graph_path_node(path, 0, ent);
			set_compound_graph_path_node(path, 1, loc_lineno);
			tv = new_tarval_from_long(wd.locs[i].lineno, mode_Iu);
505
			add_compound_ent_value_w_path(ent_locations, new_r_Const(irg, tv), path);
506
507
508
509
510
511
512
513

			/* name */
			path = new_compound_graph_path(loc_type, 2);
			set_compound_graph_path_array_index(path, 0, i);
			set_compound_graph_path_node(path, 0, ent);
			set_compound_graph_path_node(path, 1, loc_name);
			if (wd.locs[i].fname) {
				sym.entity_p = wd.locs[i].fname;
514
				n = new_r_SymConst(irg, mode_P_data, sym, symconst_addr_ent);
515
			} else {
516
				n = new_r_Const(irg, get_mode_null(mode_P_data));
517
518
519
520
521
			}
			add_compound_ent_value_w_path(ent_locations, n, path);
		}
		pmap_destroy(wd.fname_map);
	}
522
	return gen_initializer_irg(ent_filename, bblock_id, bblock_counts, n_blocks);
523
524
}

525
static void profile_node_info(void *ctx, FILE *f, const ir_node *irn)
526
{
527
	(void) ctx;
528
	if (is_Block(irn)) {
Michael Beck's avatar
Michael Beck committed
529
		fprintf(f, "profiled execution count: %u\n", ir_profile_get_block_execcount(irn));
530
531
532
	}
}

533
static void register_vcg_hook(void)
534
535
536
537
538
539
{
	memset(&hook, 0, sizeof(hook));
	hook.hook._hook_node_info = profile_node_info;
	register_hook(hook_node_info, &hook);
}

540
static void unregister_vcg_hook(void)
541
542
543
{
	unregister_hook(hook_node_info, &hook);
}
544

545
546
547
548
/**
 * Reads the corresponding profile info file if it exists and returns a
 * profile info struct
 */
549
void ir_profile_read(const char *filename)
550
{
551
552
553
554
555
	FILE   *f;
	char    buf[8];
	size_t  ret;

	f = fopen(filename, "r");
556
	if (f == NULL) {
Adam Szalkowski's avatar
oops    
Adam Szalkowski committed
557
558
		return;
	}
559
	printf("found profile data '%s'.\n", filename);
560
561
562

	/* check magic */
	ret = fread(buf, 8, 1, f);
563
	if (ret == 0 || strncmp(buf, "firmprof", 8) != 0) {
564
565
		return;
	}
566

567
	if (profile) ir_profile_free();
568
569
	profile = new_set(cmp_execcount, 16);

570
	for (;;) {
571
572
573
		execcount_t  query;
		ret = fread(&query, sizeof(unsigned int), 2, f);

574
		if (ret != 2) break;
575
576

		set_insert(profile, &query, sizeof(query), query.block);
577
	}
Adam Szalkowski's avatar
Adam Szalkowski committed
578
579

	fclose(f);
580
	register_vcg_hook();
581
582
583
584
585
}

/**
 * Frees the profile info
 */
586
void ir_profile_free(void)
587
{
588
	if (profile) {
589
		unregister_vcg_hook();
590
		del_set(profile);
591
	}
592
593
}

Adam Szalkowski's avatar
Adam Szalkowski committed
594
/**
Michael Beck's avatar
BugFix:    
Michael Beck committed
595
 * Tells whether profile module has acquired data
Adam Szalkowski's avatar
Adam Szalkowski committed
596
 */
597
int ir_profile_has_data(void)
Adam Szalkowski's avatar
Adam Szalkowski committed
598
599
600
601
{
	return (profile != NULL);
}

602
603
604
605
/**
 * Get block execution count as determined be profiling
 */
unsigned int
Michael Beck's avatar
Michael Beck committed
606
ir_profile_get_block_execcount(const ir_node *block)
607
608
609
{
	execcount_t *ec, query;

610
	if (!profile)
611
612
613
		return 1;

	query.block = get_irn_node_nr(block);
614
	ec = (execcount_t*)set_find(profile, &query, sizeof(query), get_irn_node_nr(block));
615

616
	if (ec != NULL) {
617
618
		return ec->count;
	} else {
619
620
		ir_fprintf(stderr, "Warning: Profile contains no data for %+F\n",
		           block);
621
622
		return 1;
	}
623
}
624

625
typedef struct intialize_execfreq_env_t {
626
	ir_graph *irg;
627
	ir_exec_freq *execfreqs;
628
629
630
	double freq_factor;
} initialize_execfreq_env_t;

631
632
633
// minimal execution frequency (an execfreq of 0 confuses algos)
static const double MIN_EXECFREQ = 0.00001;

634
635
static void initialize_execfreq(ir_node *block, void *data)
{
636
	initialize_execfreq_env_t *env = (initialize_execfreq_env_t*)data;
637
638
	double freq;

639
	if (block == get_irg_start_block(env->irg)
640
641
642
	   || block == get_irg_end_block(env->irg)) {
		freq = 1.0;
	} else {
Michael Beck's avatar
Michael Beck committed
643
		freq = ir_profile_get_block_execcount(block);
644
		freq *= env->freq_factor;
645
		if (freq < MIN_EXECFREQ)
646
			freq = MIN_EXECFREQ;
647
648
649
650
651
	}

	set_execfreq(env->execfreqs, block, freq);
}

Michael Beck's avatar
Michael Beck committed
652
ir_exec_freq *ir_create_execfreqs_from_profile(ir_graph *irg)
653
654
655
656
657
{
	ir_node *start_block;
	initialize_execfreq_env_t env;
	unsigned count;

658
	env.irg = irg;
659
660
661
	env.execfreqs = create_execfreq(irg);
	start_block = get_irg_start_block(irg);

Michael Beck's avatar
Michael Beck committed
662
	count = ir_profile_get_block_execcount(start_block);
663
	if (count == 0) {
664
665
666
667
668
669
		// the function was never executed, so fallback to estimated freqs
		free_execfreq(env.execfreqs);

		return compute_execfreq(irg, 10);
	}

670
	env.freq_factor = 1.0 / count;
671
672
673
674
	irg_block_walk_graph(irg, initialize_execfreq, NULL, &env);

	return env.execfreqs;
}