beverify.c 4.79 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
14
15
16
17
18
19
20
21
22
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "beverify.h"
#include "belive.h"
#include "besched.h"

#include "irnode.h"
#include "irgraph.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irdump_t.h"

typedef struct be_verify_register_pressure_env_t_ {
Christian Würdig's avatar
Christian Würdig committed
23
24
25
26
27
	ir_graph                    *irg;                 /**< the irg to verify */
	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 */
28
29
} be_verify_register_pressure_env_t;

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

Christian Würdig's avatar
Christian Würdig committed
37
	ir_fprintf(F, "\t");
38
	foreach_pset(live_nodes, node) {
Christian Würdig's avatar
Christian Würdig committed
39
		ir_fprintf(F, "%+F ", node);
40
	}
Christian Würdig's avatar
Christian Würdig committed
41
	ir_fprintf(F, "\n");
42
43
}

Christian Würdig's avatar
Christian Würdig committed
44
45
46
/**
 * Check if number of live nodes never exceeds the number of available registers.
 */
47
48
static void verify_liveness_walker(ir_node *bl, void *data)
{
Christian Würdig's avatar
Christian Würdig committed
49
50
	be_verify_register_pressure_env_t *env = (be_verify_register_pressure_env_t *)data;
	pset    *live_nodes = pset_new_ptr_default();
51
52
	ir_node *irn;

Christian Würdig's avatar
Christian Würdig committed
53
	/* collect register pressure info, start with end of a block */
54
	be_liveness_end_of_block(env->arch_env, env->cls, bl, live_nodes);
Christian Würdig's avatar
Christian Würdig committed
55

56
	sched_foreach_reverse(bl, irn) {
Christian Würdig's avatar
Christian Würdig committed
57
		int pressure = pset_count(live_nodes);
58

Christian Würdig's avatar
Christian Würdig committed
59
		if (is_Phi(irn))
60
61
62
			break;

		if(pressure > env->registers_available) {
Christian Würdig's avatar
Christian Würdig committed
63
64
65
			ir_fprintf(stderr, "Verify Warning: Register pressure too high at end of block %+F(%s) (%d/%d):\n",
				bl, get_irg_dump_name(env->irg), pressure, env->registers_available);
			print_living_values(stderr, live_nodes);
66
67
			env->problem_found = 1;
		}
Christian Würdig's avatar
Christian Würdig committed
68
69

		be_liveness_transfer(env->arch_env, env->cls, irn, live_nodes);
70
71
72
73
	}
	del_pset(live_nodes);
}

Christian Würdig's avatar
Christian Würdig committed
74
75
76
/**
 * Start a walk over the irg and check the register pressure.
 */
77
int be_verify_register_pressure(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_graph *irg)
78
79
80
81
82
{
	be_verify_register_pressure_env_t env;

	be_liveness(irg);

Christian Würdig's avatar
Christian Würdig committed
83
84
85
	env.irg                 = irg;
	env.arch_env            = arch_env;
	env.cls                 = cls;
86
	env.registers_available = arch_count_non_ignore_regs(arch_env, cls);
Christian Würdig's avatar
Christian Würdig committed
87
	env.problem_found       = 0;
88

Christian Würdig's avatar
Christian Würdig committed
89
90
91
	irg_block_walk_graph(irg, verify_liveness_walker, NULL, &env);

	return ! env.problem_found;
92
93
94
}

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

Christian Würdig's avatar
Christian Würdig committed
99
100
101
/**
 * Simple schedule checker.
 */
102
103
104
105
static void verify_schedule_walker(ir_node *bl, void *data)
{
	be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data;
	ir_node *irn;
Christian Würdig's avatar
Christian Würdig committed
106
	int non_phi_found  = 0;
107
108
109
	int cfchange_found = 0;
	// TODO ask ABI about delay branches
	int delay_branches = 0;
110
111

	/*
112
113
	 * Make sure that all phi nodes are scheduled at the beginning of the block, and that there
	 * is 1 or no control flow changing node scheduled and exactly delay_branches operations after it.
114
115
	 */
	sched_foreach(bl, irn) {
Christian Würdig's avatar
Christian Würdig committed
116
117
118
		if (is_Phi(irn)) {
			if (non_phi_found) {
				ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n",
119
120
121
122
123
124
					irn, bl, get_irg_dump_name(env->irg));
				env->problem_found = 1;
			}
			continue;
		}
		non_phi_found = 1;
125

Christian Würdig's avatar
Christian Würdig committed
126
127
128
129
		if (is_cfop(irn) && get_irn_opcode(irn) != iro_Start) {
			/* 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",
130
131
132
133
					irn, bl, get_irg_dump_name(env->irg));
				env->problem_found = 1;
			}
			cfchange_found = 1;
Christian Würdig's avatar
Christian Würdig committed
134
135
136
137
		} else if (cfchange_found) {
			/* 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",
138
139
					irn, bl, get_irg_dump_name(env->irg));
				env->problem_found = 1;
140
141
			} else {
				delay_branches--;
142
143
144
145
			}
		}
	}

Christian Würdig's avatar
Christian Würdig committed
146
147
148
	/* check that all delay branches are used (at least with NOPs) */
	if (cfchange_found && delay_branches != 0) {
		ir_fprintf(stderr, "Not all delay slots filled after jump (%d/%d) in block %+F (%s)\n",
149
150
151
152
			bl, get_irg_dump_name(env->irg));
		env->problem_found = 1;
	}
}
153

Christian Würdig's avatar
Christian Würdig committed
154
155
156
/**
 * Start a walk over the irg and check schedule.
 */
157
int be_verify_schedule(ir_graph *irg)
158
159
160
161
{
	be_verify_schedule_env_t env;

	env.problem_found = 0;
Christian Würdig's avatar
Christian Würdig committed
162
	env.irg           = irg;
163
164
165

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

Christian Würdig's avatar
Christian Würdig committed
166
	return ! env.problem_found;
167
}