beverify.c 11.1 KB
Newer Older
1
/*
Christian Würdig's avatar
Christian Würdig committed
2
3
4
5
6
 * Author:    Matthias Braun
 * Date:      05.05.2006
 * Copyright: (c) Universitaet Karlsruhe
 * License:   This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 * CVS-Id:    $Id$
7
8
9
10
11
12
13
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "beverify.h"
#include "belive.h"
14
#include "besched_t.h"
15
16
17
18
19
20

#include "irnode.h"
#include "irgraph.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irdump_t.h"
21
22
#include "set.h"
#include "array.h"
23
#include "benode_t.h"
24
25

typedef struct be_verify_register_pressure_env_t_ {
Christian Würdig's avatar
Christian Würdig committed
26
	ir_graph                    *irg;                 /**< the irg to verify */
Sebastian Hack's avatar
Sebastian Hack committed
27
	 be_lv_t                    *lv;                  /**< Liveness information. */
Christian Würdig's avatar
Christian Würdig committed
28
29
30
31
	const arch_env_t            *arch_env;            /**< an architecture environment */
	const arch_register_class_t *cls;                 /**< the register class to check for */
	int                         registers_available;  /**< number of available registers */
	int                         problem_found;        /**< flag indicating if a problem was found */
32
33
} be_verify_register_pressure_env_t;

Christian Würdig's avatar
Christian Würdig committed
34
35
36
37
/**
 * Print all nodes of a pset into a file.
 */
static void print_living_values(FILE *F, pset *live_nodes)
38
39
40
{
	ir_node *node;

Christian Würdig's avatar
Christian Würdig committed
41
	ir_fprintf(F, "\t");
42
	foreach_pset(live_nodes, node) {
Christian Würdig's avatar
Christian Würdig committed
43
		ir_fprintf(F, "%+F ", node);
44
	}
Christian Würdig's avatar
Christian Würdig committed
45
	ir_fprintf(F, "\n");
46
47
}

Christian Würdig's avatar
Christian Würdig committed
48
49
50
/**
 * Check if number of live nodes never exceeds the number of available registers.
 */
51
static void verify_liveness_walker(ir_node *block, void *data)
52
{
Christian Würdig's avatar
Christian Würdig committed
53
54
	be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data;
	pset    *live_nodes = pset_new_ptr_default();
55
	ir_node *irn;
56
	int pressure;
57

Christian Würdig's avatar
Christian Würdig committed
58
	/* collect register pressure info, start with end of a block */
Sebastian Hack's avatar
Sebastian Hack committed
59
	be_liveness_end_of_block(env->lv, env->arch_env, env->cls, block, live_nodes);
Christian Würdig's avatar
Christian Würdig committed
60

61
62
63
64
65
66
67
	pressure = pset_count(live_nodes);
	if(pressure > env->registers_available) {
		ir_fprintf(stderr, "Verify Warning: Register pressure too high at end of block %+F(%s) (%d/%d):\n",
			block, get_irg_dump_name(env->irg), pressure, env->registers_available);
		print_living_values(stderr, live_nodes);
		env->problem_found = 1;
	}
68

69
	sched_foreach_reverse(block, irn) {
Christian Würdig's avatar
Christian Würdig committed
70
		if (is_Phi(irn))
71
72
			break;

73
74
75
76
		be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);

		pressure = pset_count(live_nodes);

77
		if(pressure > env->registers_available) {
78
79
			ir_fprintf(stderr, "Verify Warning: Register pressure too high before node %+F in %+F(%s) (%d/%d):\n",
				irn, block, get_irg_dump_name(env->irg), pressure, env->registers_available);
Christian Würdig's avatar
Christian Würdig committed
80
			print_living_values(stderr, live_nodes);
81
82
83
84
85
86
			env->problem_found = 1;
		}
	}
	del_pset(live_nodes);
}

Christian Würdig's avatar
Christian Würdig committed
87
88
89
/**
 * Start a walk over the irg and check the register pressure.
 */
90
int be_verify_register_pressure(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_graph *irg)
91
92
93
{
	be_verify_register_pressure_env_t env;

Sebastian Hack's avatar
Sebastian Hack committed
94
	env.lv                  = be_liveness(irg);
Christian Würdig's avatar
Christian Würdig committed
95
96
97
	env.irg                 = irg;
	env.arch_env            = arch_env;
	env.cls                 = cls;
98
	env.registers_available = arch_count_non_ignore_regs(arch_env, cls);
Christian Würdig's avatar
Christian Würdig committed
99
	env.problem_found       = 0;
100

Christian Würdig's avatar
Christian Würdig committed
101
	irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
Sebastian Hack's avatar
Sebastian Hack committed
102
	be_liveness_free(env.lv);
Christian Würdig's avatar
Christian Würdig committed
103
104

	return ! env.problem_found;
105
106
107
}

typedef struct be_verify_schedule_env_t_ {
Christian Würdig's avatar
Christian Würdig committed
108
109
	int      problem_found;    /**< flags indicating if there was a problem */
	ir_graph *irg;             /**< the irg to check */
110
111
} be_verify_schedule_env_t;

Christian Würdig's avatar
Christian Würdig committed
112
113
114
/**
 * Simple schedule checker.
 */
115
static void verify_schedule_walker(ir_node *block, void *data)
116
117
{
	be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
118
	ir_node *node;
Christian Würdig's avatar
Christian Würdig committed
119
	int non_phi_found  = 0;
120
	int cfchange_found = 0;
Matthias Braun's avatar
Matthias Braun committed
121
	// TODO ask arch about delay branches
122
	int delay_branches = 0;
123
	pset *uses = pset_new_ptr_default();
124
125

	/*
126
127
128
129
	 * Tests for the following things:
	 *   1. Make sure that all phi nodes are scheduled at the beginning of the block
	 *   2. There is 1 or no control flow changing node scheduled and exactly delay_branches operations after it.
	 *   3. No value is defined after it has been used
130
	 */
131
132
133
134
135
	sched_foreach(block, node) {
		int i, arity;

		// 1. Check for phis
		if (is_Phi(node)) {
Christian Würdig's avatar
Christian Würdig committed
136
137
			if (non_phi_found) {
				ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
138
					node, block, get_irg_dump_name(env->irg));
139
140
				env->problem_found = 1;
			}
Matthias Braun's avatar
Matthias Braun committed
141
		} else {
142
143
			non_phi_found = 1;
		}
144

145
146
		// 2. Check for control flow changing nodes
		if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
Christian Würdig's avatar
Christian Würdig committed
147
148
149
			/* check, that only one CF operation is scheduled */
			if (cfchange_found == 1) {
				ir_fprintf(stderr, "Verify Warning: More than 1 control flow changing node (%+F) scheduled in block %+F (%s)\n",
150
					node, block, get_irg_dump_name(env->irg));
151
152
153
				env->problem_found = 1;
			}
			cfchange_found = 1;
Christian Würdig's avatar
Christian Würdig committed
154
		} else if (cfchange_found) {
155
156
157
158
159
160
161
162
163
164
			// proj and keepany aren't real instructions...
			if(!is_Proj(node) && !be_is_Keep(node)) {
				/* check for delay branches */
				if (delay_branches == 0) {
					ir_fprintf(stderr, "Verify Warning: Node %+F scheduled after control flow changing node (+delay branches) in block %+F (%s)\n",
						node, block, get_irg_dump_name(env->irg));
					env->problem_found = 1;
				} else {
					delay_branches--;
				}
165
166
			}
		}
167
168
169
170
171
172
173

		// 3. Check for uses
		if(pset_find_ptr(uses, node)) {
			ir_fprintf(stderr, "Verify Warning: Value %+F used before it was defined in block %+F (%s)\n",
				node, block, get_irg_dump_name(env->irg));
			env->problem_found = 1;
		}
174
175
176
177
		if(!is_Phi(node)) {
			for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
				pset_insert_ptr(uses, get_irn_n(node, i));
			}
178
		}
179
	}
180
	del_pset(uses);
181

182
	/* check that all delay branches are filled (at least with NOPs) */
Christian Würdig's avatar
Christian Würdig committed
183
184
	if (cfchange_found && delay_branches != 0) {
		ir_fprintf(stderr, "Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
185
			block, get_irg_dump_name(env->irg));
186
187
188
		env->problem_found = 1;
	}
}
189

Christian Würdig's avatar
Christian Würdig committed
190
191
192
/**
 * Start a walk over the irg and check schedule.
 */
193
int be_verify_schedule(ir_graph *irg)
194
195
196
197
{
	be_verify_schedule_env_t env;

	env.problem_found = 0;
Christian Würdig's avatar
Christian Würdig committed
198
	env.irg           = irg;
199
200
201

	irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);

Christian Würdig's avatar
Christian Würdig committed
202
	return ! env.problem_found;
203
}
204

205
206
207
208
209
210
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
273
274
275
276
277
278
279
280
281
282
283
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395



typedef struct _spill_t {
	ir_node *spill;
	entity *ent;
} spill_t;

typedef struct {
	ir_graph *irg;
	be_lv_t *lv;
	set *spills;
	ir_node **reloads;
	int problem_found;
} be_verify_spillslots_env_t;

static int cmp_spill(const void* d1, const void* d2, size_t size) {
	const spill_t* s1 = d1;
	const spill_t* s2 = d2;
	return s1->spill != s2->spill;
}

static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, entity *ent) {
	spill_t spill, *res;
	int hash = HASH_PTR(node);

	spill.spill = node;
	res = set_find(env->spills, &spill, sizeof(spill), hash);

	if(res == NULL) {
		spill.ent = ent;
		res = set_insert(env->spills, &spill, sizeof(spill), hash);
	}

	return res;
}

static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent);

static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
	entity *spillent = be_get_frame_entity(node);
	get_spill(env, node, ent);

	if(spillent != ent) {
		ir_fprintf(stderr, "Verify warning: Spill %+F has different entity than reload %+F in block %+F(%s)\n",
			node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
		env->problem_found = 1;
	}
}

static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
	int i, arity;
	spill_t spill, *res;
	int hash = HASH_PTR(node);
	int out;
	ir_node* memperm;
	entity *spillent;

	assert(is_Proj(node));

	memperm = get_Proj_pred(node);
	out = get_Proj_proj(node);

	spillent = be_get_MemPerm_out_entity(memperm, out);
	if(spillent != ent) {
		ir_fprintf(stderr, "Verify warning: MemPerm %+F has different entity than reload %+F in block %+F(%s)\n",
			node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
		env->problem_found = 1;
	}

	spill.spill = node;
	res = set_find(env->spills, &spill, sizeof(spill), hash);
	if(res != NULL) {
		return;
	}

	spill.ent = spillent;
	res = set_insert(env->spills, &spill, sizeof(spill), hash);

	for(i = 0, arity = get_irn_arity(memperm); i < arity; ++i) {
		ir_node* arg = get_irn_n(memperm, i);
		entity* argent = be_get_MemPerm_in_entity(memperm, i);

		collect(env, arg, memperm, argent);
	}
}

static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity *ent) {
	int i, arity;
	spill_t spill, *res;
	int hash = HASH_PTR(node);

	assert(is_Phi(node));

	spill.spill = node;
	res = set_find(env->spills, &spill, sizeof(spill), hash);
	if(res != NULL) {
		return;
	}

	spill.ent = ent;
	res = set_insert(env->spills, &spill, sizeof(spill), hash);

	// is 1 of the arguments a spill?
	for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
		ir_node* arg = get_irn_n(node, i);
		collect(env, arg, reload, ent);
	}
}

static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, entity* ent) {
	if(be_is_Spill(node)) {
		collect_spill(env, node, reload, ent);
	} else if(is_Proj(node)) {
		collect_memperm(env, node, reload, ent);
	} else if(is_Phi(node) && get_irn_mode(node) == mode_M) {
		collect_memphi(env, node, reload, ent);
	} else {
		ir_fprintf(stderr, "Verify warning: No spill, memperm or memphi attached to node %+F found from node %+F in block %+F(%s)\n",
			node, reload, get_nodes_block(node), get_irg_dump_name(env->irg));
		env->problem_found = 1;
	}
}

/**
 * This walker function searches for reloads and collects all the spills
 * and memphis attached to them.
 */
static void collect_spills_walker(ir_node *node, void *data) {
	be_verify_spillslots_env_t *env = data;

	if(be_is_Reload(node)) {
		ir_node *spill = get_irn_n(node, be_pos_Reload_mem);
		entity* ent = be_get_frame_entity(node);

		collect(env, spill, node, ent);
		ARR_APP1(ir_node*, env->reloads, node);
	}
}

static void check_spillslot_interference(be_verify_spillslots_env_t *env) {
	int spillcount = set_count(env->spills);
	spill_t **spills = alloca(spillcount * sizeof(spills[0]));
	spill_t *spill;
	int i;

	for(spill = set_first(env->spills), i = 0; spill != NULL; spill = set_next(env->spills), ++i) {
		spills[i] = spill;
	}

	for(i = 0; i < spillcount; ++i) {
		spill_t *sp1 = spills[i];
		int i2;

		for(i2 = i+1; i2 < spillcount; ++i2) {
			spill_t *sp2 = spills[i2];

			if(sp1->ent != sp2->ent)
				continue;

			if(values_interfere(env->lv, sp1->spill, sp2->spill)) {
				ir_fprintf(stderr, "Verify warning: Spillslots for %+F in block %+F(%s) and %+F in block %+F(%s) interfere\n",
					sp1->spill, get_nodes_block(sp1->spill), get_irg_dump_name(env->irg),
					sp2->spill, get_nodes_block(sp2->spill), get_irg_dump_name(env->irg));
				env->problem_found = 1;
			}
		}
	}
}

int be_verify_spillslots(ir_graph *irg)
{
	be_verify_spillslots_env_t env;

	env.irg = irg;
	env.spills = new_set(cmp_spill, 10);
	env.reloads = NEW_ARR_F(ir_node*, 0);
	env.problem_found = 0;
	env.lv = be_liveness(irg);

	irg_walk_graph(irg, collect_spills_walker, NULL, &env);

	check_spillslot_interference(&env);

	DEL_ARR_F(env.reloads);
	del_set(env.spills);
	be_liveness_free(env.lv);

	return ! env.problem_found;
}

396
397
398
/* Ideas for further verifiers:
 *   - make sure that each use is dominated by its definition (except phi arguments)
 */