bestat.c 6.89 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Christian Würdig's avatar
Christian Würdig committed
6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
 * @file
 * @brief       Provides several statistic functions for the backend.
Matthias Braun's avatar
Matthias Braun committed
9
 * @author      Christian Wuerdig, Matthias Braun
Christian Würdig's avatar
Christian Würdig committed
10
 */
11
12
#include <time.h>

Matthias Braun's avatar
Matthias Braun committed
13
#include "panic.h"
Michael Beck's avatar
Michael Beck committed
14
#include "execfreq.h"
15
#include "firmstat_t.h"
Matthias Braun's avatar
Matthias Braun committed
16
17
18
#include "irgwalk.h"
#include "irhooks.h"
#include "irnode_t.h"
Matthias Braun's avatar
Matthias Braun committed
19
#include "statev_t.h"
Matthias Braun's avatar
Matthias Braun committed
20
#include "util.h"
21

22
23
#include "bearch.h"
#include "beirg.h"
24
#include "bestat.h"
Matthias Braun's avatar
Matthias Braun committed
25
#include "belive.h"
26
#include "besched.h"
27
#include "benode.h"
Christian Würdig's avatar
Christian Würdig committed
28

29
30
typedef struct pressure_walker_env_t pressure_walker_env_t;
struct pressure_walker_env_t {
Matthias Braun's avatar
Matthias Braun committed
31
32
33
34
35
	ir_graph                    *irg;
	be_lv_t                     *lv;
	double                       insn_count;
	double                       regpressure;
	unsigned                     max_pressure;
36
	const arch_register_class_t *cls;
Sebastian Hack's avatar
Sebastian Hack committed
37
38
};

39
40
41
42
static void check_reg_pressure_class(pressure_walker_env_t *env,
                                     ir_node *block,
                                     const arch_register_class_t *cls)
{
43
	ir_nodeset_t live_nodes;
44
	ir_nodeset_init(&live_nodes);
45
	be_liveness_end_of_block(env->lv, cls, block, &live_nodes);
Matthias Braun's avatar
Matthias Braun committed
46
	unsigned max_live = ir_nodeset_size(&live_nodes);
47
48
49
	env->regpressure += max_live;

	sched_foreach_reverse(block, irn) {
50
		size_t cnt;
51

52
		if (is_Phi(irn))
53
54
			break;

55
		be_liveness_transfer(cls, irn, &live_nodes);
56
		cnt      = ir_nodeset_size(&live_nodes);
Christoph Mallon's avatar
Christoph Mallon committed
57
		max_live = MAX(max_live, cnt);
58
59
60
		env->regpressure += cnt;
		env->insn_count++;
	}
61

62
	if (max_live > env->max_pressure)
63
		env->max_pressure = max_live;
64

Matthias Braun's avatar
Matthias Braun committed
65
	stat_be_block_regpressure(env->irg, block, max_live, cls->name);
66
67
	ir_nodeset_destroy(&live_nodes);
}
68

69
70
static void stat_reg_pressure_block(ir_node *block, void *data)
{
71
	pressure_walker_env_t *env = (pressure_walker_env_t*)data;
72

73
	check_reg_pressure_class(env, block, env->cls);
74
75
}

76
void be_do_stat_reg_pressure(ir_graph *irg, const arch_register_class_t *cls)
77
{
Matthias Braun's avatar
Matthias Braun committed
78
79
	be_assure_live_sets(irg);
	pressure_walker_env_t env;
80
	env.irg          = irg;
81
82
83
	env.insn_count   = 0;
	env.max_pressure = 0;
	env.regpressure  = 0;
84
	env.lv           = be_get_irg_liveness(irg);
85
	env.cls          = cls;
86

87
88
	/* Collect register pressure information for each block */
	irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &env);
Sebastian Hack's avatar
Sebastian Hack committed
89

Matthias Braun's avatar
Matthias Braun committed
90
	double average_pressure = env.regpressure / env.insn_count;
Matthias Braun's avatar
Matthias Braun committed
91
92
	stat_ev_dbl("bechordal_average_register_pressure", average_pressure);
	stat_ev_dbl("bechordal_maximum_register_pressure", env.max_pressure);
93
}
94

95
typedef struct estimate_irg_costs_env_t {
96
	double costs;
97
98
99
100
} estimate_irg_costs_env_t;

static void estimate_block_costs(ir_node *block, void *data)
{
101
	estimate_irg_costs_env_t *env = (estimate_irg_costs_env_t*)data;
102
	double costs = 0.0;
103
104

	sched_foreach(block, node) {
105
		costs += isa_if->get_op_estimated_cost(node);
106
107
	}

108
	env->costs += costs * get_block_execfreq(block);
109
}
110

111
double be_estimate_irg_costs(ir_graph *irg)
112
113
{
	estimate_irg_costs_env_t env;
114
	env.costs = 0.0;
115
116
117
118
119

	irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
	return env.costs;
}

Matthias Braun's avatar
Matthias Braun committed
120
121
static void node_stat_walker(ir_node *irn, void *data)
{
122
	be_node_stats_t *const stats = (be_node_stats_t*)data;
Matthias Braun's avatar
Matthias Braun committed
123
124

	/* if the node is a normal phi */
125
	if (is_Phi(irn)) {
Matthias Braun's avatar
Matthias Braun committed
126
127
128
129
130
		if (get_irn_mode(irn) == mode_M) {
			(*stats)[BE_STAT_MEM_PHIS]++;
		} else {
			(*stats)[BE_STAT_PHIS]++;
		}
131
132
133
134
	} else if (be_is_Perm(irn)) {
		(*stats)[BE_STAT_PERMS]++;
	} else if (be_is_Copy(irn)) {
		(*stats)[BE_STAT_COPIES]++;
Matthias Braun's avatar
Matthias Braun committed
135
136
137
	}
}

138
void be_collect_node_stats(be_node_stats_t *new_stats, ir_graph *irg)
Matthias Braun's avatar
Matthias Braun committed
139
{
140
	memset(new_stats, 0, sizeof(*new_stats));
141
	irg_walk_graph(irg, NULL, node_stat_walker, new_stats);
Matthias Braun's avatar
Matthias Braun committed
142
}
143

Matthias Braun's avatar
Matthias Braun committed
144
145
void be_subtract_node_stats(be_node_stats_t *stats, be_node_stats_t *sub)
{
Matthias Braun's avatar
Matthias Braun committed
146
	for (be_stat_tag_t i = BE_STAT_FIRST; i < BE_STAT_COUNT; ++i) {
Matthias Braun's avatar
Matthias Braun committed
147
148
149
150
151
152
		(*stats)[i] -= (*sub)[i];
	}
}

void be_copy_node_stats(be_node_stats_t *dest, be_node_stats_t *src)
{
Christoph Mallon's avatar
Christoph Mallon committed
153
	MEMCPY(dest, src, 1);
Matthias Braun's avatar
Matthias Braun committed
154
155
156
157
}

static const char *get_stat_name(enum be_stat_tag_t tag)
{
158
	switch (tag) {
Matthias Braun's avatar
Matthias Braun committed
159
160
161
162
163
164
165
166
167
168
	case BE_STAT_PHIS:     return "phis";
	case BE_STAT_MEM_PHIS: return "mem_phis";
	case BE_STAT_COPIES:   return "copies";
	case BE_STAT_PERMS:    return "perms";
	default:               panic("unknown stat tag found");
	}
}

void be_emit_node_stats(be_node_stats_t *stats, const char *prefix)
{
Matthias Braun's avatar
Matthias Braun committed
169
170
	for (be_stat_tag_t i = BE_STAT_FIRST; i < BE_STAT_COUNT; ++i) {
		char buf[128];
Matthias Braun's avatar
Matthias Braun committed
171
172
173
174
175
176
177
		snprintf(buf, sizeof(buf), "%s%s", prefix, get_stat_name(i));
		stat_ev_dbl(buf, (*stats)[i]);
	}
}

static void insn_count_walker(ir_node *irn, void *data)
{
178
	unsigned long *cnt = (unsigned long*)data;
Matthias Braun's avatar
Matthias Braun committed
179

180
	switch (get_irn_opcode(irn)) {
Matthias Braun's avatar
Matthias Braun committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
	case iro_Proj:
	case iro_Phi:
	case iro_End:
		break;
	default:
		(*cnt)++;
	}
}

unsigned long be_count_insns(ir_graph *irg)
{
	unsigned long cnt = 0;
	irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
	return cnt;
}

static void block_count_walker(ir_node *node, void *data)
{
199
	unsigned long *cnt = (unsigned long*)data;
200
	if (node == get_irg_end_block(get_irn_irg(node)))
Matthias Braun's avatar
Matthias Braun committed
201
202
203
204
205
206
207
208
209
210
		return;
	(*cnt)++;
}

unsigned long be_count_blocks(ir_graph *irg)
{
	unsigned long cnt = 0;
	irg_block_walk_graph(irg, block_count_walker, NULL, &cnt);
	return cnt;
}
Matthias Braun's avatar
Matthias Braun committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

typedef struct stat_t {
	unsigned long values;
	unsigned long unused_values;
	unsigned long uses;
	unsigned long should_be_sames;
	unsigned long constrained_values;
	unsigned long constrained_uses;
	unsigned long unused_constrained_values;
} stat_t;

static void block_count_values(ir_node *block, void *data)
{
	stat_t *stats = (stat_t*)data;

	sched_foreach(block, node) {
		be_foreach_value(node, value,
			arch_register_req_t const *const req
				= arch_get_irn_register_req(value);
			if (req->cls == NULL)
				continue;
			++stats->values;
			if (arch_register_req_is(req, should_be_same) || is_Phi(value))
				++stats->should_be_sames;
			if (arch_register_req_is(req, limited))
				++stats->constrained_values;
		);
		for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
			const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
			if (req->cls == NULL)
				continue;
			++stats->uses;
			if (be_is_Keep(node)) {
				ir_node *value = get_irn_n(node, i);
				if (get_irn_n_edges(value) <= 1) {
					++stats->unused_values;
					const arch_register_req_t *const req
						= arch_get_irn_register_req(value);
					if (arch_register_req_is(req, limited))
						++stats->unused_constrained_values;
				}
			}
			if (arch_register_req_is(req, limited))
				++stats->constrained_uses;
		}
	}
}

void be_stat_values(ir_graph *irg)
{
	stat_t stats;
	memset(&stats, 0, sizeof(stats));
	irg_block_walk_graph(irg, block_count_values, NULL, &stats);
	stat_ev_ull("valstat_values", stats.values);
	stat_ev_ull("valstat_unused", stats.unused_values);
	stat_ev_ull("valstat_uses", stats.uses);
	stat_ev_ull("valstat_should_be_sames", stats.should_be_sames);
	stat_ev_ull("valstat_constrained_values", stats.constrained_values);
	stat_ev_ull("valstat_constrained_uses", stats.constrained_uses);
	stat_ev_ull("valstat_unused_constrained_values",
	            stats.unused_constrained_values);
}