beverify.c 23.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
 */
#ifdef HAVE_CONFIG_H
9
#include <config.h>
10
11
#endif

12
13
#include <limits.h>

14
15
16
#include "bitset.h"
#include "set.h"
#include "array.h"
17
18
19
20
21
22

#include "irnode.h"
#include "irgraph.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irdump_t.h"
23
#include "iredges.h"
24
25
26
27

#include "beverify.h"
#include "belive.h"
#include "besched_t.h"
28
#include "benode_t.h"
29

30
31
static int my_values_interfere(const ir_node *a, const ir_node *b);

32
typedef struct be_verify_register_pressure_env_t_ {
Christian Würdig's avatar
Christian Würdig committed
33
	ir_graph                    *irg;                 /**< the irg to verify */
Sebastian Hack's avatar
Sebastian Hack committed
34
	 be_lv_t                    *lv;                  /**< Liveness information. */
Christian Würdig's avatar
Christian Würdig committed
35
36
37
38
	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 */
39
40
} be_verify_register_pressure_env_t;

Christian Würdig's avatar
Christian Würdig committed
41
42
43
/**
 * Print all nodes of a pset into a file.
 */
44
static void print_living_values(FILE *F, pset *live_nodes) {
45
46
	ir_node *node;

Christian Würdig's avatar
Christian Würdig committed
47
	ir_fprintf(F, "\t");
48
	foreach_pset(live_nodes, node) {
Christian Würdig's avatar
Christian Würdig committed
49
		ir_fprintf(F, "%+F ", node);
50
	}
Christian Würdig's avatar
Christian Würdig committed
51
	ir_fprintf(F, "\n");
52
53
}

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

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

66
67
68
69
70
71
72
	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;
	}
73

74
	sched_foreach_reverse(block, irn) {
Christian Würdig's avatar
Christian Würdig committed
75
		if (is_Phi(irn))
76
77
			break;

78
79
80
81
		be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);

		pressure = pset_count(live_nodes);

82
		if(pressure > env->registers_available) {
83
84
			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
85
			print_living_values(stderr, live_nodes);
86
87
88
89
90
91
			env->problem_found = 1;
		}
	}
	del_pset(live_nodes);
}

Christian Würdig's avatar
Christian Würdig committed
92
93
94
/**
 * Start a walk over the irg and check the register pressure.
 */
Sebastian Hack's avatar
Sebastian Hack committed
95
int be_verify_register_pressure(const be_irg_t *birg, const arch_register_class_t *cls, ir_graph *irg) {
96
97
	be_verify_register_pressure_env_t env;

Sebastian Hack's avatar
Sebastian Hack committed
98
	env.lv                  = be_liveness(irg);
Christian Würdig's avatar
Christian Würdig committed
99
	env.irg                 = irg;
Sebastian Hack's avatar
Sebastian Hack committed
100
	env.arch_env            = birg->main_env->arch_env;
Christian Würdig's avatar
Christian Würdig committed
101
	env.cls                 = cls;
Sebastian Hack's avatar
Sebastian Hack committed
102
	env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL);
Christian Würdig's avatar
Christian Würdig committed
103
	env.problem_found       = 0;
104

Christian Würdig's avatar
Christian Würdig committed
105
	irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);
Sebastian Hack's avatar
Sebastian Hack committed
106
	be_liveness_free(env.lv);
Christian Würdig's avatar
Christian Würdig committed
107
108

	return ! env.problem_found;
109
110
111
}

typedef struct be_verify_schedule_env_t_ {
Christian Würdig's avatar
Christian Würdig committed
112
	int      problem_found;    /**< flags indicating if there was a problem */
113
	bitset_t *scheduled;       /**< bitset of scheduled nodes */
Christian Würdig's avatar
Christian Würdig committed
114
	ir_graph *irg;             /**< the irg to check */
115
116
} be_verify_schedule_env_t;

Christian Würdig's avatar
Christian Würdig committed
117
118
119
/**
 * Simple schedule checker.
 */
120
static void verify_schedule_walker(ir_node *block, void *data) {
121
	be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
122
	ir_node *node;
Christian Würdig's avatar
Christian Würdig committed
123
	int non_phi_found  = 0;
124
	int cfchange_found = 0;
Matthias Braun's avatar
Matthias Braun committed
125
	// TODO ask arch about delay branches
126
	int delay_branches = 0;
127
	int last_timestep = INT_MIN;
128
129

	/*
130
131
132
133
	 * 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
134
	 */
135
136
	sched_foreach(block, node) {
		int i, arity;
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
		int timestep;

		// this node is scheduled
		if(bitset_is_set(env->scheduled, get_irn_idx(node))) {
			ir_fprintf(stderr, "Verify warning: %+F appears to be schedule twice\n");
			env->problem_found = 1;
		}
		bitset_set(env->scheduled, get_irn_idx(node));

		// Check that scheduled nodes are in the correct block
		if(get_nodes_block(node) != block) {
			ir_fprintf(stderr, "Verify warning: %+F is in block %+F but scheduled in %+F\n", node, get_nodes_block(node), block);
			env->problem_found = 1;
		}

		// Check that timesteps are increasing
		timestep = sched_get_time_step(node);
		if(timestep <= last_timestep) {
			ir_fprintf(stderr, "Verify warning: Schedule timestep did not increase at node %+F\n",
			           node);
			env->problem_found = 1;
		}
		last_timestep = timestep;
160

161
		// Check that phis come before any other node
162
		if (is_Phi(node)) {
Christian Würdig's avatar
Christian Würdig committed
163
164
			if (non_phi_found) {
				ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
165
					node, block, get_irg_dump_name(env->irg));
166
167
				env->problem_found = 1;
			}
Matthias Braun's avatar
Matthias Braun committed
168
		} else {
169
170
			non_phi_found = 1;
		}
171

172
		// Check for control flow changing nodes
173
		if (is_cfop(node) && get_irn_opcode(node) != iro_Start) {
Christian Würdig's avatar
Christian Würdig committed
174
175
176
			/* 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",
177
					node, block, get_irg_dump_name(env->irg));
178
179
180
				env->problem_found = 1;
			}
			cfchange_found = 1;
Christian Würdig's avatar
Christian Würdig committed
181
		} else if (cfchange_found) {
182
183
184
185
186
187
188
189
190
191
			// 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--;
				}
192
193
			}
		}
194

195
		// Check that all uses come before their definitions
196
		if(!is_Phi(node)) {
197
			int nodetime = sched_get_time_step(node);
198
			for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
199
200
201
202
203
204
205
206
207
208
				ir_node *arg = get_irn_n(node, i);
				if(get_nodes_block(arg) != block
				   || !sched_is_scheduled(arg))
					continue;

				if(sched_get_time_step(arg) >= nodetime) {
					ir_fprintf(stderr, "Verify Warning: Value %+F used by %+F before it was defined in block %+F (%s)\n",
					           arg, node, block, get_irg_dump_name(env->irg));
					env->problem_found = 1;
				}
209
			}
210
		}
211

212
		// Check that no dead nodes are scheduled
213
214
215
216
217
		if(get_irn_n_edges(node) == 0) {
			ir_fprintf(stderr, "Verify warning: Node %+F is dead but scheduled in block %+F (%s)\n",
			           node, block, get_irg_dump_name(env->irg));
			env->problem_found = 1;
		}
218
219
	}

220
	/* check that all delay branches are filled (at least with NOPs) */
Christian Würdig's avatar
Christian Würdig committed
221
	if (cfchange_found && delay_branches != 0) {
222
		ir_fprintf(stderr, "Verify warning: Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
223
			block, get_irg_dump_name(env->irg));
224
225
226
		env->problem_found = 1;
	}
}
227

228
static int should_be_scheduled(ir_node *node) {
229
230
231
	if(is_Block(node))
		return -1;

232
	if(get_irn_mode(node) == mode_M) {
233
234
		if(is_Proj(node))
			return -1;
Michael Beck's avatar
Michael Beck committed
235
		if(is_Phi(node) || is_Sync(node) || is_Pin(node))
236
237
238
239
			return 0;
	}
	if(is_Proj(node) && get_irn_mode(node) == mode_X)
		return 0;
240
	if(be_is_Keep(node) && get_irn_opcode(get_nodes_block(node)) == iro_Bad)
241
242
		return 0;

243
244
245
246
	switch(get_irn_opcode(node)) {
	case iro_End:
	case iro_NoMem:
	case iro_Bad:
247
	case iro_Unknown:
248
249
250
251
252
		return 0;
	default:
		break;
	}

253
254
255
256
257
258
	return 1;
}

static void check_schedule(ir_node *node, void *data) {
	be_verify_schedule_env_t *env = data;
	int should_be;
259
	int scheduled;
260
261

	should_be = should_be_scheduled(node);
262
263
	if(should_be == -1)
		return;
264

265
266
267
	scheduled = bitset_is_set(env->scheduled, get_irn_idx(node)) ? 1 : 0;
	should_be = should_be ? 1 : 0;
	if(should_be != scheduled) {
268
		ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should%s be scheduled\n",
269
			node, get_nodes_block(node), get_irg_dump_name(env->irg), should_be ? "" : " not");
270
271
272
273
		env->problem_found = 1;
	}
}

Christian Würdig's avatar
Christian Würdig committed
274
275
276
/**
 * Start a walk over the irg and check schedule.
 */
277
int be_verify_schedule(ir_graph *irg)
278
279
280
281
{
	be_verify_schedule_env_t env;

	env.problem_found = 0;
282
	env.scheduled     = bitset_alloca(get_irg_last_idx(irg));
Christian Würdig's avatar
Christian Würdig committed
283
	env.irg           = irg;
284
285

	irg_block_walk_graph(irg, verify_schedule_walker, NULL, &env);
286
287
	// check if all nodes are scheduled
	irg_walk_graph(irg, check_schedule, NULL, &env);
288

Christian Würdig's avatar
Christian Würdig committed
289
	return ! env.problem_found;
290
}
291

292
293


294
295
296
//---------------------------------------------------------------------------


297
298
299

typedef struct _spill_t {
	ir_node *spill;
300
	ir_entity *ent;
301
302
303
} spill_t;

typedef struct {
304
	const arch_env_t *arch_env;
305
306
307
308
309
310
311
312
313
314
315
316
	ir_graph *irg;
	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;
}

317
318
319
320
321
322
323
static spill_t *find_spill(be_verify_spillslots_env_t *env, ir_node *node) {
	spill_t spill;

	spill.spill = node;
	return set_find(env->spills, &spill, sizeof(spill), HASH_PTR(node));
}

324
static spill_t *get_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) {
325
326
327
328
329
330
331
332
333
334
335
336
337
338
	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;
}

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
static ir_node *get_memory_edge(const ir_node *node) {
	int i, arity;
	ir_node *result = NULL;

	arity = get_irn_arity(node);
	for(i = arity - 1; i >= 0; --i) {
		ir_node *arg = get_irn_n(node, i);
		if(get_irn_mode(arg) == mode_M) {
			assert(result == NULL);
			result = arg;
		}
	}

	return result;
}

355
static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent);
356

357
static void check_entity(be_verify_spillslots_env_t *env, ir_node *node, ir_entity *ent) {
358
359
360
361
362
363
	if(ent == NULL) {
		ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have an entity assigned\n",
		           node, get_nodes_block(node), get_irg_dump_name(env->irg));
	}
}

364
365
static void collect_spill(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) {
	ir_entity *spillent = arch_get_frame_entity(env->arch_env, node);
366
	check_entity(env, node, spillent);
367
368
369
370
371
372
373
374
375
	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;
	}
}

376
static void collect_memperm(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) {
377
378
379
380
381
	int i, arity;
	spill_t spill, *res;
	int hash = HASH_PTR(node);
	int out;
	ir_node* memperm;
382
	ir_entity *spillent;
383
384
385
386
387
388
389

	assert(is_Proj(node));

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

	spillent = be_get_MemPerm_out_entity(memperm, out);
390
	check_entity(env, memperm, spillent);
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
	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);

406
407
	for(i = 0, arity = be_get_MemPerm_entity_arity(memperm); i < arity; ++i) {
		ir_node* arg = get_irn_n(memperm, i + 1);
408
		ir_entity* argent = be_get_MemPerm_in_entity(memperm, i);
409
410
411
412
413

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

414
static void collect_memphi(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity *ent) {
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
	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);
	}
}

437
static void collect(be_verify_spillslots_env_t *env, ir_node *node, ir_node *reload, ir_entity* ent) {
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
	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;
457
	const arch_env_t *arch_env = env->arch_env;
458

459
460
461
462
463
464
	// @@@ ia32_classify returns classification of Proj_pred :-/
	if(is_Proj(node))
		return;

	if(arch_irn_class_is(arch_env, node, reload)) {
		ir_node *spill = get_memory_edge(node);
465
		ir_entity *ent;
Matthias Braun's avatar
Matthias Braun committed
466

467
468
469
470
471
472
		if(spill == NULL) {
			ir_fprintf(stderr, "Verify warning: No spill attached to reload %+F in block %+F(%s)\n",
			           node, get_nodes_block(node), get_irg_dump_name(env->irg));
			env->problem_found = 1;
			return;
		}
Matthias Braun's avatar
Matthias Braun committed
473
		ent = arch_get_frame_entity(env->arch_env, node);
474
		check_entity(env, node, ent);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

		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;

501
			if(my_values_interfere(sp1->spill, sp2->spill)) {
502
503
504
505
				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;
506
				my_values_interfere(sp1->spill, sp2->spill);
507
508
509
510
511
			}
		}
	}
}

512
513
514
515
516
517
static void check_lonely_spills(ir_node *node, void *data) {
	be_verify_spillslots_env_t *env = data;

	if(be_is_Spill(node) || (is_Proj(node) && be_is_MemPerm(get_Proj_pred(node)))) {
		spill_t *spill = find_spill(env, node);
		if(be_is_Spill(node)) {
518
			ir_entity *ent = arch_get_frame_entity(env->arch_env, node);
519
520
521
522
523
524
525
526
527
528
			check_entity(env, node, ent);
		}

		if(spill == NULL) {
			ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) not connected to a reaload\n",
			           node, get_nodes_block(node), get_irg_dump_name(env->irg));
		}
	}
}

529
int be_verify_spillslots(const arch_env_t *arch_env, ir_graph *irg)
530
531
532
{
	be_verify_spillslots_env_t env;

533
	env.arch_env = arch_env;
534
535
536
537
538
539
	env.irg = irg;
	env.spills = new_set(cmp_spill, 10);
	env.reloads = NEW_ARR_F(ir_node*, 0);
	env.problem_found = 0;

	irg_walk_graph(irg, collect_spills_walker, NULL, &env);
540
	irg_walk_graph(irg, check_lonely_spills, NULL, &env);
541
542
543
544
545
546
547
548
549

	check_spillslot_interference(&env);

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

	return ! env.problem_found;
}

550
551
552
553
554
555
556
557
558
559
560


//---------------------------------------------------------------------------



/**
 * Check, if two values interfere.
 * @param a The first value.
 * @param b The second value.
 * @return 1, if a and b interfere, 0 if not.
561
 */
562
static int my_values_interfere(const ir_node *a, const ir_node *b) {
563
564
	const ir_edge_t *edge;
	ir_node *bb;
565
566
	int a2b = value_dominates(a, b);
	int b2a = value_dominates(b, a);
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599

	/* If there is no dominance relation, they do not interfere. */
	if(!a2b && !b2a)
		return 0;

	/*
	 * Adjust a and b so, that a dominates b if
	 * a dominates b or vice versa.
	 */
	if(b2a) {
		const ir_node *t = a;
		a = b;
		b = t;
	}

	bb = get_nodes_block(b);

	/*
	 * Look at all usages of a.
	 * If there's one usage of a in the block of b, then
	 * we check, if this use is dominated by b, if that's true
	 * a and b interfere. Note that b must strictly dominate the user,
	 * since if b is the last user of in the block, b and a do not
	 * interfere.
	 * Uses of a not in b's block can be disobeyed, because the
	 * check for a being live at the end of b's block is already
	 * performed.
	 */
	foreach_out_edge(a, edge) {
		const ir_node *user = get_edge_src_irn(edge);
		if(b == user)
			continue;

600
601
602
		if(get_irn_opcode(user) == iro_End)
			continue;

603
604
605
		// in case of phi arguments we compare with the block the value comes from
		if(is_Phi(user)) {
			ir_node *phiblock = get_nodes_block(user);
606
607
			if(phiblock == bb)
				continue;
608
609
610
			user = get_irn_n(phiblock, get_edge_src_pos(edge));
		}

611
		if(value_dominates(b, user))
612
613
614
615
616
			return 1;
	}

	return 0;
}
617
618
619
620
621
622
623
624
625
626
627
628
629
630



//---------------------------------------------------------------------------



typedef struct _be_verify_register_allocation_env_t {
	const arch_env_t *arch_env;
	ir_graph *irg;
	be_lv_t *lv;
	int problem_found;
} be_verify_register_allocation_env_t;

631
static void check_register_constraints(ir_node *node, be_verify_register_allocation_env_t *env) {
632
	const arch_env_t      *arch_env = env->arch_env;
633
	const arch_register_t *reg;
634
	int                   i, arity;
635

636
637
	/* verify output register */
	if (arch_get_irn_reg_class(arch_env, node, -1) != NULL) {
638
		reg = arch_get_irn_register(arch_env, node);
639
		if (reg == NULL) {
640
641
642
643
			ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
					node, get_nodes_block(node), get_irg_dump_name(env->irg));
			env->problem_found = 1;
		}
644
		else if (! arch_register_type_is(reg, joker) && !arch_reg_is_allocatable(arch_env, node, -1, reg)) {
645
646
647
648
649
650
			ir_fprintf(stderr, "Verify warning: Register %s assigned as output of %+F not allowed (register constraint) in block %+F(%s)\n",
					reg->name, node, get_nodes_block(node), get_irg_dump_name(env->irg));
			env->problem_found = 1;
		}
	}

651
	/* verify input register */
652
	arity = get_irn_arity(node);
653
	for (i = 0; i < arity; ++i) {
654
655
		ir_node *pred = get_irn_n(node, i);

656
657
658
		if (is_Unknown(pred))
			continue;

Christian Würdig's avatar
Christian Würdig committed
659
660
661
662
663
664
665
		if (is_Bad(pred)) {
			ir_fprintf(stderr, "Verify warning: %+F in block %+F(%s) has Bad as input %d\n",
				node, get_nodes_block(node), get_irg_dump_name(env->irg), i);
			env->problem_found = 1;
			continue;
		}

666
		if (arch_get_irn_reg_class(arch_env, node, i) == NULL)
667
668
669
			continue;

		reg = arch_get_irn_register(arch_env, pred);
670
		if (reg == NULL) {
671
672
673
674
675
			ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) should have a register assigned\n",
			           pred, get_nodes_block(pred), get_irg_dump_name(env->irg));
			env->problem_found = 1;
			continue;
		}
676
		else if (! arch_register_type_is(reg, joker) && ! arch_reg_is_allocatable(arch_env, node, i, reg)) {
677
678
679
680
681
682
683
			ir_fprintf(stderr, "Verify warning: Register %s as input %d of %+F not allowed (register constraint) in block %+F(%s)\n",
			           reg->name, i, node, get_nodes_block(node), get_irg_dump_name(env->irg));
			env->problem_found = 1;
		}
	}
}

684
685
static void check_register_allocation(be_verify_register_allocation_env_t *env,
                                      const arch_register_class_t *regclass, pset *nodes) {
686
687
688
689
690
	const arch_env_t      *arch_env  = env->arch_env;
	const arch_register_t *reg       = NULL;
	int                   fail       = 0;
	bitset_t              *registers = bitset_alloca(arch_register_class_n_regs(regclass));
	ir_node               *node;
691
692

	foreach_pset(nodes, node) {
693
		if (arch_get_irn_reg_class(arch_env, node, -1) != regclass)
694
695
696
			continue;

		reg = arch_get_irn_register(arch_env, node);
697
698
699
700
701
702

		/* this problem is already reported in 'check_register_constraints' */
		if (! reg)
			continue;

		if (bitset_is_set(registers, reg->index)) {
703
704
705
			pset_break(nodes);
			fail = 1;
			break;
706
707
708
		}
		bitset_set(registers, reg->index);
	}
709

710
711
712
713
714
715
716
717
718
719
720
	if (fail) {
		ir_fprintf(stderr, "Verify warning: Register %s assigned more than once in block %+F(%s)\n",
			       reg->name, get_nodes_block(node), get_irg_dump_name(env->irg));
		env->problem_found = 1;

		foreach_pset(nodes, node) {
			if (arch_get_irn_register(arch_env, node) == reg) {
				ir_fprintf(stderr, "  at node %+F\n", node);
			}
		}
	}
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
}

static void verify_block_register_allocation(ir_node *block, void *data) {
	be_verify_register_allocation_env_t *env = data;
	const arch_env_t *arch_env = env->arch_env;
	const arch_isa_t *isa = arch_env->isa;
	int i, nregclasses;

	nregclasses = arch_isa_get_n_reg_class(isa);
	for (i = 0; i < nregclasses; ++i) {
		const arch_register_class_t *regclass = arch_isa_get_reg_class(isa, i);
		ir_node *node;
		pset *live_nodes = pset_new_ptr_default();

		be_liveness_end_of_block(env->lv, env->arch_env, regclass, block, live_nodes);
		check_register_allocation(env, regclass, live_nodes);

		sched_foreach_reverse(block, node) {
			if (is_Phi(node))
				break;

			be_liveness_transfer(env->arch_env, regclass, node, live_nodes);
			check_register_allocation(env, regclass, live_nodes);
744
			check_register_constraints(node, env);
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
		}

		del_pset(live_nodes);
	}
}

int be_verify_register_allocation(const arch_env_t *arch_env, ir_graph *irg) {
	be_verify_register_allocation_env_t env;

	env.arch_env = arch_env;
	env.irg = irg;
	env.lv = be_liveness(irg);
	env.problem_found = 0;

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

	be_liveness_free(env.lv);

	return !env.problem_found;
}
765
766
767
768
769
770
771
772
773
774
775
776
777
778



//---------------------------------------------------------------------------



typedef struct _verify_out_dead_nodes_env {
	ir_graph *irg;
	bitset_t *reachable;
	int problem_found;
} verify_out_dead_nodes_env;

static void check_out_edges(ir_node *node, verify_out_dead_nodes_env *env) {
779
	ir_graph *irg = env->irg;
780
781
	const ir_edge_t* edge;

782
783
784
785
	if(irn_visited(node))
		return;
	mark_irn_visited(node);

786
787
788
	foreach_out_edge(node, edge) {
		ir_node* src = get_edge_src_irn(edge);

789
		if(!bitset_is_set(env->reachable, get_irn_idx(src)) && !is_Block(node)) {
Matthias Braun's avatar
Matthias Braun committed
790
791
792
			ir_fprintf(stderr, "Verify warning: Node %+F in block %+F(%s) only reachable through out edges from %+F\n",
			           src, get_nodes_block(src), get_irg_dump_name(irg), node);
			env->problem_found = 1;
793
			continue;
794
795
		}

Matthias Braun's avatar
Matthias Braun committed
796
		check_out_edges(src, env);
797
798
799
800
801
802
803
804
805
806
807
	}
}

static void set_reachable(ir_node *node, void* data)
{
	bitset_t* reachable = data;
	bitset_set(reachable, get_irn_idx(node));
}

int be_verify_out_edges(ir_graph *irg) {
	verify_out_dead_nodes_env env;
808
809
810
811

	env.irg           = irg;
	env.reachable     = bitset_alloca(get_irg_last_idx(irg));
	env.problem_found = edges_verify(irg);
812

813
	irg_walk_in_or_dep_graph(irg, set_reachable, NULL, env.reachable);
Matthias Braun's avatar
Matthias Braun committed
814
	irg_walk_anchors(irg, set_reachable, NULL, env.reachable);
815
	inc_irg_visited(irg);
816
817
	check_out_edges(get_irg_start(irg), &env);

818
	return ! env.problem_found;
819
}