beloopana.c 6.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * Author:      Christian Wuerdig
 * Date:        2007/02/19
 * Copyright:   (c) Universitaet Karlsruhe
 * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 * CVS-Id:      $Id$
 *
 * Compute register pressure in loops
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "set.h"
#include "pset.h"
#include "irnode.h"
#include "irtools.h"
#include "irloop_t.h"

#include "bearch.h"
#include "belive.h"
#include "besched.h"
#include "beloopana.h"

25
26
#define HASH_LOOP_INFO(info) (HASH_PTR((info)->loop) ^ HASH_PTR((info)->cls))

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
typedef struct _be_loop_info_t {
	ir_loop                     *loop;
	const arch_register_class_t *cls;
	unsigned                    max_pressure;
} be_loop_info_t;

struct _be_loopana_t {
	set      *data;
	be_irg_t *birg;
	DEBUG_ONLY(firm_dbg_module_t *dbg);
};

static int cmp_loop_info(const void *a, const void *b, size_t sz) {
	const be_loop_info_t *i1 = a;
	const be_loop_info_t *i2 = b;

	return ! (i1->loop == i2->loop && i1->cls == i2->cls);
}

/**
 * Compute the highest register pressure in a block.
 * @param loop_ana  The loop ana object.
 * @param block     The block to compute pressure for.
 * @param cls       The register class to compute pressure for.
 * @return The highest register pressure in the given block.
 */
static unsigned be_compute_block_pressure(be_loopana_t *loop_ana, ir_node *block, const arch_register_class_t *cls) {
	const arch_env_t *aenv       = loop_ana->birg->main_env->arch_env;
	pset             *live_nodes = pset_new_ptr_default();
	ir_node          *irn;
	int              max_live;

59
60
	DBG((loop_ana->dbg, LEVEL_1, "Processing Block %+F\n", block));

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	/* determine largest pressure with this block */
	live_nodes = be_liveness_end_of_block(loop_ana->birg->lv, aenv, cls, block, live_nodes);
	max_live   = pset_count(live_nodes);

	sched_foreach_reverse(block, irn) {
		int cnt;

		if (is_Phi(irn))
			break;

		live_nodes = be_liveness_transfer(aenv, cls, irn, live_nodes);
		cnt        = pset_count(live_nodes);
		max_live   = MAX(cnt, max_live);
	}

76
77
	DBG((loop_ana->dbg, LEVEL_1, "Finished with Block %+F (%s %u)\n", block, cls->name, max_live));

78
79
80
81
82
83
84
85
86
87
88
89
	del_pset(live_nodes);
	return max_live;
}

/**
 * Compute the highest register pressure in a loop and it's sub-loops.
 * @param loop_ana  The loop ana object.
 * @param loop      The loop to compute pressure for.
 * @param cls       The register class to compute pressure for.
 * @return The highest register pressure in the given loop.
 */
static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls) {
90
91
	int            i, max;
	unsigned       pressure;
92
93
	be_loop_info_t *entry, key;

94
	DBG((loop_ana->dbg, LEVEL_1, "\nProcessing Loop %d\n", loop->loop_nr));
95
96
97
98
99
	assert(get_loop_n_elements(loop) > 0);
	pressure = 0;

	/* determine maximal pressure in all loop elements */
	for (i = 0, max = get_loop_n_elements(loop); i < max; ++i) {
100
		unsigned     son_pressure;
101
		loop_element elem = get_loop_element(loop, i);
102

103
104
		switch (*elem.kind) {
			case k_ir_node:
105
				son_pressure = be_compute_block_pressure(loop_ana, elem.node, cls);
106
				break;
107
108
			case k_ir_loop:
				son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
109
110
111
112
113
				break;
			default:
				assert(0);
				break;
		}
114
115

		pressure = MAX(pressure, son_pressure);
116
117
118
119
   	}
	DBG((loop_ana->dbg, LEVEL_1, "Done with loop %d, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));

	/* update info in set */
120
121
122
123
	key.loop            = loop;
	key.cls             = cls;
	key.max_pressure    = 0;
	entry               = set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
124
125
126
127
128
129
130
131
132
	entry->max_pressure = MAX(entry->max_pressure, pressure);

	return pressure;
}

/**
 * Compute the register pressure for a class of all loops in the birg.
 * @param birg  The backend irg object
 * @param cls   The register class to compute the pressure for
Christian Würdig's avatar
Christian Würdig committed
133
 * @return The loop analysis object.
134
135
136
137
138
139
140
141
 */
be_loopana_t *be_new_loop_pressure_cls(be_irg_t *birg, const arch_register_class_t *cls) {
	be_loopana_t *loop_ana = xmalloc(sizeof(loop_ana));

	loop_ana->data = new_set(cmp_loop_info, 16);
	loop_ana->birg = birg;
	FIRM_DBG_REGISTER(loop_ana->dbg, "firm.be.loopana");

142
143
144
145
	DBG((loop_ana->dbg, LEVEL_1, "\n=====================================================\n", cls->name));
	DBG((loop_ana->dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
	DBG((loop_ana->dbg, LEVEL_1, "=====================================================\n", cls->name));

146
147
148
149
150
151
152
153
	be_compute_loop_pressure(loop_ana, get_irg_loop(birg->irg), cls);

	return loop_ana;
}

/**
 * Compute the register pressure for all classes of all loops in the birg.
 * @param birg  The backend irg object
Christian Würdig's avatar
Christian Würdig committed
154
 * @return The loop analysis object.
155
156
157
158
159
160
161
162
163
164
165
166
 */
be_loopana_t *be_new_loop_pressure(be_irg_t *birg) {
	be_loopana_t *loop_ana = xmalloc(sizeof(loop_ana));
	ir_loop      *irg_loop = get_irg_loop(birg->irg);
	int          i;

	loop_ana->data = new_set(cmp_loop_info, 16);
	loop_ana->birg = birg;
	FIRM_DBG_REGISTER(loop_ana->dbg, "firm.be.loopana");

	for (i = arch_isa_get_n_reg_class(birg->main_env->arch_env->isa) - 1; i >= 0; --i) {
		const arch_register_class_t *cls = arch_isa_get_reg_class(birg->main_env->arch_env->isa, i);
167
168
169
		DBG((loop_ana->dbg, LEVEL_1, "\n=====================================================\n", cls->name));
		DBG((loop_ana->dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
		DBG((loop_ana->dbg, LEVEL_1, "=====================================================\n", cls->name));
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
		be_compute_loop_pressure(loop_ana, irg_loop, cls);
	}

	return loop_ana;
}

/**
 * Returns the computed register pressure for the given class and loop.
 * @return The pressure or INT_MAX if not found
 */
unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop) {
	unsigned pressure = INT_MAX;
	be_loop_info_t *entry, key;

	assert(cls && loop);

	key.loop = loop;
	key.cls  = cls;
188
	entry    = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
189
190
191
192
193
194
195
196
197
198

	if (entry)
		pressure = entry->max_pressure;
	else
		assert(0 && "Pressure not computed for given class and loop object.");

	return pressure;
}

/**
Christian Würdig's avatar
Christian Würdig committed
199
 * Frees the loop analysis object.
200
201
202
203
204
 */
void be_free_loop_pressure(be_loopana_t *loop_ana) {
	del_set(loop_ana->data);
	xfree(loop_ana);
}