execfreq.c 8.95 KB
Newer Older
1
/*
2
 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
Matthias Braun's avatar
Matthias Braun committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * 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.
 */

/**
 * @file
 * @brief       Compute an estimate of basic block executions.
 * @author      Adam M. Szalkowski
 * @date        28.05.2006
 * @version     $Id$
26
27
28
29
30
31
32
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>
33
#include <limits.h>
Sebastian Hack's avatar
Sebastian Hack committed
34
#include <math.h>
35

36
#include "gaussseidel.h"
37
38
39
40

#include "firm_common_t.h"
#include "set.h"
#include "hashptr.h"
41
#include "debug.h"
42
43
44
#include "statev.h"
#include "dfs_t.h"
#include "absgraph.h"
45
46
47
48
49
50

#include "irprog_t.h"
#include "irgraph_t.h"
#include "irnode_t.h"
#include "irloop.h"
#include "irgwalk.h"
Matthias Braun's avatar
Matthias Braun committed
51
#include "iredges.h"
52
#include "irprintf.h"
Michael Beck's avatar
Michael Beck committed
53
#include "irtools.h"
Sebastian Hack's avatar
Sebastian Hack committed
54
#include "irhooks.h"
55
56
57

#include "execfreq.h"

58
59
60
61
62
63
64
65
66
67
68
/* enable to also solve the equations with Gauss-Jordan */
#undef COMPARE_AGAINST_GAUSSJORDAN

#ifdef COMPARE_AGAINST_GAUSSJORDAN
#include "gaussjordan.h"
#endif


#define EPSILON		     1e-5
#define UNDEF(x)         (fabs(x) < EPSILON)
#define SEIDEL_TOLERANCE 1e-7
69

70
71
#define MAX_INT_FREQ 1000000

72
73
#define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))

Sebastian Hack's avatar
Sebastian Hack committed
74
75
typedef struct _freq_t {
	const ir_node    *irn;
76
	int               idx;
Sebastian Hack's avatar
Sebastian Hack committed
77
78
79
	double            freq;
} freq_t;

80
struct ir_exec_freq {
81
	set *set;
Sebastian Hack's avatar
Sebastian Hack committed
82
	hook_entry_t hook;
83
	double max;
84
	double min_non_zero;
85
	double m, b;
Sebastian Hack's avatar
Sebastian Hack committed
86
87
88
	unsigned infeasible : 1;
};

89
90
91
static int
cmp_freq(const void *a, const void *b, size_t size)
{
92
93
	const freq_t *p = a;
	const freq_t *q = b;
Matthias Braun's avatar
Matthias Braun committed
94
	(void) size;
95

96
	return !(p->irn == q->irn);
97
98
99
100
101
}

static freq_t *
set_find_freq(set * set, const ir_node * irn)
{
102
	freq_t     query;
103

104
105
	query.irn = irn;
	return set_find(set, &query, sizeof(query), HASH_PTR(irn));
106
107
108
109
110
}

static freq_t *
set_insert_freq(set * set, const ir_node * irn)
{
111
	freq_t query;
112

113
114
115
	query.irn = irn;
	query.freq = 0.0;
	return set_insert(set, &query, sizeof(query), HASH_PTR(irn));
116
117
118
}

double
119
get_block_execfreq(const ir_exec_freq *ef, const ir_node * irn)
120
{
Sebastian Hack's avatar
Sebastian Hack committed
121
122
123
124
125
126
	if(!ef->infeasible) {
		set *freqs = ef->set;
		freq_t *freq;
		assert(is_Block(irn));
		freq = set_find_freq(freqs, irn);
		assert(freq);
127
128

		assert(freq->freq >= 0);
Sebastian Hack's avatar
Sebastian Hack committed
129
130
131
132
		return freq->freq;
	}

	return 1.0;
133
134
}

135
unsigned long
136
get_block_execfreq_ulong(const ir_exec_freq *ef, const ir_node *bb)
137
{
138
	double f       = get_block_execfreq(ef, bb);
139
140
	int res        = (int) (f > ef->min_non_zero ? ef->m * f + ef->b : 1.0);
	return res;
141
142
}

143
static double *
144
solve_lgs(gs_matrix_t *mat, double *x, int size)
145
{
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	double init = 1.0 / size;
	double dev;
	int i, iter = 0;

	/* better convergence. */
	for (i = 0; i < size; ++i)
		x[i] = init;

	stat_ev_dbl("execfreq_matrix_size", size);
	stat_ev_tim_push();
	do {
		++iter;
		dev = gs_matrix_gauss_seidel(mat, x, size);
	} while(fabs(dev) > SEIDEL_TOLERANCE);
	stat_ev_tim_pop("execfreq_seidel_time");
	stat_ev_dbl("execfreq_seidel_iter", iter);

#ifdef COMPARE_AGAINST_GAUSSJORDAN
	{
		double *nw = xmalloc(size * size * sizeof(*nw));
		double *nx = xmalloc(size * sizeof(*nx));

		memset(nx, 0, size * sizeof(*nx));
		gs_matrix_export(mat, nw, size);

		stat_ev_tim_push();
		firm_gaussjordansolve(nw, nx, size);
		stat_ev_tim_pop("execfreq_jordan_time");

		xfree(nw);
		xfree(nx);
	}
#endif

	return x;
181
182
183
}

static double
184
get_cf_probability(ir_node *bb, int pos, double loop_weight)
185
{
Matthias Braun's avatar
Matthias Braun committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
	double  sum = 0.0;
	double  cur = 0.0;
	const ir_node *pred = get_Block_cfgpred_block(bb, pos);
	const ir_loop *pred_loop = get_irn_loop(pred);
	int pred_depth = get_loop_depth(pred_loop);
	const ir_edge_t *edge;

	cur = get_loop_depth(get_irn_loop(bb)) < get_loop_depth(get_irn_loop(pred)) ? 1.0 : loop_weight;

	foreach_block_succ(pred, edge) {
		const ir_node *block = get_edge_src_irn(edge);
		const ir_loop *loop = get_irn_loop(block);
		int depth = get_loop_depth(loop);
		sum += depth < pred_depth ? 1.0 : loop_weight;
	}
201

Matthias Braun's avatar
Matthias Braun committed
202
	return cur/sum;
203
204
}

Sebastian Hack's avatar
Sebastian Hack committed
205
206
207
static void exec_freq_node_info(void *ctx, FILE *f, const ir_node *irn)
{
	if(is_Block(irn)) {
208
		ir_exec_freq *ef = ctx;
209
		fprintf(f, "execution frequency: %g/%lu\n", get_block_execfreq(ef, irn), get_block_execfreq_ulong(ef, irn));
Sebastian Hack's avatar
Sebastian Hack committed
210
211
212
	}
}

213
ir_exec_freq *create_execfreq(ir_graph *irg)
214
{
215
	ir_exec_freq *execfreq = xmalloc(sizeof(execfreq[0]));
216
217
218
219
220
221
222
	memset(execfreq, 0, sizeof(execfreq[0]));
	execfreq->set = new_set(cmp_freq, 32);

	memset(&execfreq->hook, 0, sizeof(execfreq->hook));
	execfreq->hook.context = execfreq;
	execfreq->hook.hook._hook_node_info = exec_freq_node_info;
	register_hook(hook_node_info, &execfreq->hook);
Matthias Braun's avatar
Matthias Braun committed
223
	(void) irg;
224
225
226
227

	return execfreq;
}

228
void set_execfreq(ir_exec_freq *execfreq, const ir_node *block, double freq)
229
230
231
232
233
{
	freq_t *f = set_insert_freq(execfreq->set, block);
	f->freq = freq;
}

234
ir_exec_freq *
235
compute_execfreq(ir_graph * irg, double loop_weight)
236
{
237
238
239
240
241
	gs_matrix_t  *mat;
	int           size;
	int           idx;
	freq_t       *freq, *s, *e;
	ir_exec_freq *ef;
Sebastian Hack's avatar
Sebastian Hack committed
242
	set          *freqs;
243
	dfs_t        *dfs;
244
	double       *x;
245
246
247
248
249
250
251
252
253
	double        norm;

	/*
	 * compute a DFS.
	 * using a toposort on the CFG (without back edges) will propagate
	 * the values better for the gauss/seidel iteration.
	 * => they can "flow" from start to end.
	 */
	dfs = dfs_new(&absgraph_irg_cfg_succ, irg);
Sebastian Hack's avatar
Sebastian Hack committed
254
255
	ef = xmalloc(sizeof(ef[0]));
	memset(ef, 0, sizeof(ef[0]));
256
	ef->min_non_zero = HUGE_VAL; /* initialize with a reasonable large number. */
257
	freqs = ef->set = new_set(cmp_freq, 32);
258

259
	construct_cf_backedges(irg);
260
261
	/* TODO: edges are corrupt for EDGE_KIND_BLOCK after the local optimize
		 graph phase merges blocks in the x86 backend */
Matthias Braun's avatar
Matthias Braun committed
262
263
264
	edges_deactivate(irg);
	edges_activate(irg);
	/* edges_assure(irg); */
265

266
267
268
	size = dfs_get_n_nodes(dfs);
	mat  = gs_new_matrix(size, size);
	x    = xmalloc(size*sizeof(*x));
269

270
271
272
273
	for (idx = dfs_get_n_nodes(dfs) - 1; idx >= 0; --idx) {
		ir_node *bb = (ir_node *) dfs_get_post_num_node(dfs, size - idx - 1);
		freq_t *freq;
		int i;
274

275
276
		freq = set_insert_freq(freqs, bb);
		freq->idx = idx;
277

278
		gs_matrix_set(mat, idx, idx, -1.0);
279
		for(i = get_Block_n_cfgpreds(bb) - 1; i >= 0; --i) {
280
281
			ir_node *pred = get_Block_cfgpred_block(bb, i);
			int pred_idx  = size - dfs_get_post_num(dfs, pred) - 1;
282

283
			gs_matrix_set(mat, idx, pred_idx, get_cf_probability(bb, i, loop_weight));
284
285
		}
	}
286

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
	/*
	 * Add a loop from end to start.
	 * The problem is then an eigenvalue problem:
	 * Solve A*x = 1*x => (A-I)x = 0
	 */
	s = set_find_freq(freqs, get_irg_start_block(irg));
	e = set_find_freq(freqs, get_irg_end_block(irg));
	gs_matrix_set(mat, s->idx, e->idx, 1.0);

	/* solve the system and delete the matrix */
	solve_lgs(mat, x, size);
	gs_delete_matrix(mat);

	/*
	 * compute the normalization factor.
	 * 1.0 / exec freq of start block.
	 */
	assert(x[s->idx] > 0.0);
	norm = 1.0 / x[s->idx];

	ef->max = 0.0;
	set_foreach(freqs, freq) {
		int idx = freq->idx;
310

311
312
313
		/* freq->freq = UNDEF(x[idx]) ? EPSILON : x[idx]; */
		/* TODO: Do we need the check for zero? */
		freq->freq = x[idx] * norm;
314

315
316
		/* get the maximum exec freq */
		ef->max = MAX(ef->max, freq->freq);
317

318
319
320
321
		/* Get the minimum non-zero execution frequency. */
		if(freq->freq > 0.0)
			ef->min_non_zero = MIN(ef->min_non_zero, freq->freq);
	}
322

323
324
325
	/* compute m and b of the transformation used to convert the doubles into scaled ints */
	{
		double smallest_diff = 1.0;
326

327
328
329
330
		double l2 = ef->min_non_zero;
		double h2 = ef->max;
		double l1 = 1.0;
		double h1 = MAX_INT_FREQ;
331

332
333
		double *fs = malloc(set_count(freqs) * sizeof(fs[0]));
		int i, j, n = 0;
334

335
336
		set_foreach(freqs, freq)
			fs[n++] = freq->freq;
337

338
339
340
341
342
343
344
		/*
		 * find the smallest difference of the execution frequencies
		 * we try to ressolve it with 1 integer.
		 */
		for(i = 0; i < n; ++i) {
			if(fs[i] <= 0.0)
				continue;
345

346
347
			for(j = i + 1; j < n; ++j) {
				double diff = fabs(fs[i] - fs[j]);
348

349
350
351
352
				if(!UNDEF(diff))
					smallest_diff = MIN(diff, smallest_diff);
			}
		}
353

354
355
		/* according to that the slope of the translation function is 1.0 / smallest diff */
		ef->m = 1.0 / smallest_diff;
356

357
358
		/* the abscissa is then given by */
		ef->b = l1 - ef->m * l2;
Michael Beck's avatar
Michael Beck committed
359

360
361
362
363
364
365
366
		/*
		 * if the slope is so high that the largest integer would be larger than MAX_INT_FREQ
		 * set the largest int freq to that upper limit and recompute the translation function
		 */
		if(ef->m * h2 + ef->b > MAX_INT_FREQ) {
			ef->m = (h1 - l1) / (h2 - l2);
			ef->b = l1 - ef->m * l2;
Michael Beck's avatar
Michael Beck committed
367
		}
368

369
		free(fs);
Michael Beck's avatar
Michael Beck committed
370
	}
Sebastian Hack's avatar
Sebastian Hack committed
371

372
373
374
375
376
377
	memset(&ef->hook, 0, sizeof(ef->hook));
	ef->hook.context = ef;
	ef->hook.hook._hook_node_info = exec_freq_node_info;
	register_hook(hook_node_info, &ef->hook);

	xfree(x);
378

379
	return ef;
380
381
382
}

void
383
free_execfreq(ir_exec_freq *ef)
384
{
Sebastian Hack's avatar
Sebastian Hack committed
385
386
387
	del_set(ef->set);
	unregister_hook(hook_node_info, &ef->hook);
	free(ef);
388
}