beloopana.c 7.42 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

20
/**
Christian Würdig's avatar
Christian Würdig committed
21
22
23
24
25
 * @file
 * @brief       Compute register pressure in loops.
 * @author      Christian Wuerdig
 * @date        19.02.2007
 * @version     $Id$
26
27
 */
#ifdef HAVE_CONFIG_H
28
#include "config.h"
29
30
31
32
33
34
35
#endif

#include "set.h"
#include "pset.h"
#include "irnode.h"
#include "irtools.h"
#include "irloop_t.h"
36
#include "error.h"
37
#include "debug.h"
38

39
#include "bearch_t.h"
40
41
42
#include "belive.h"
#include "besched.h"
#include "beloopana.h"
Matthias Braun's avatar
Matthias Braun committed
43
44
45
#include "bemodule.h"

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL);
46

47
48
#define HASH_LOOP_INFO(info) (HASH_PTR((info)->loop) ^ HASH_PTR((info)->cls))

49
50
51
52
53
54
55
56
57
58
59
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;
};

Matthias Braun's avatar
Matthias Braun committed
60
static int cmp_loop_info(const void *a, const void *b, size_t size) {
61
62
	const be_loop_info_t *i1 = a;
	const be_loop_info_t *i2 = b;
Matthias Braun's avatar
Matthias Braun committed
63
	(void) size;
64
65
66
67
68
69
70
71
72
73
74

	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.
 */
75
76
77
static unsigned be_compute_block_pressure(be_loopana_t *loop_ana,
		ir_node *block, const arch_register_class_t *cls)
{
78
79
	const be_irg_t   *birg       = loop_ana->birg;
	be_lv_t          *lv         = be_get_birg_liveness(birg);
80
	ir_nodeset_t      live_nodes;
81
82
83
	ir_node          *irn;
	int              max_live;

Matthias Braun's avatar
Matthias Braun committed
84
	DBG((dbg, LEVEL_1, "Processing Block %+F\n", block));
85

86
	/* determine largest pressure with this block */
87
	ir_nodeset_init(&live_nodes);
88
	be_liveness_end_of_block(lv, cls, block, &live_nodes);
89
	max_live   = ir_nodeset_size(&live_nodes);
90
91
92
93
94
95
96

	sched_foreach_reverse(block, irn) {
		int cnt;

		if (is_Phi(irn))
			break;

97
		be_liveness_transfer(cls, irn, &live_nodes);
98
		cnt        = ir_nodeset_size(&live_nodes);
99
100
101
		max_live   = MAX(cnt, max_live);
	}

Matthias Braun's avatar
Matthias Braun committed
102
	DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %u)\n", block, cls->name, max_live));
103

104
	ir_nodeset_destroy(&live_nodes);
105
106
107
108
109
110
111
112
113
114
115
	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) {
116
117
	int            i, max;
	unsigned       pressure;
118
119
	be_loop_info_t *entry, key;

Matthias Braun's avatar
Matthias Braun committed
120
	DBG((dbg, LEVEL_1, "\nProcessing Loop %d\n", loop->loop_nr));
121
122
123
124
125
	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) {
126
		unsigned     son_pressure;
127
		loop_element elem = get_loop_element(loop, i);
128

129
130
		switch (*elem.kind) {
			case k_ir_node:
131
				son_pressure = be_compute_block_pressure(loop_ana, elem.node, cls);
132
				break;
133
134
			case k_ir_loop:
				son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
135
136
				break;
			default:
137
				panic("Unknown element found in loop");
138
139
				break;
		}
140
141

		pressure = MAX(pressure, son_pressure);
Christoph Mallon's avatar
Christoph Mallon committed
142
	}
Matthias Braun's avatar
Matthias Braun committed
143
	DBG((dbg, LEVEL_1, "Done with loop %d, pressure %u for class %s\n", loop->loop_nr, pressure, cls->name));
144
145

	/* update info in set */
146
147
148
149
	key.loop            = loop;
	key.cls             = cls;
	key.max_pressure    = 0;
	entry               = set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
150
151
152
153
154
155
156
157
158
	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
159
 * @return The loop analysis object.
160
 */
161
162
163
be_loopana_t *be_new_loop_pressure_cls(be_irg_t *birg,
                                       const arch_register_class_t *cls) {
	ir_graph     *irg      = be_get_birg_irg(birg);
164
	be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
165
166
167
168

	loop_ana->data = new_set(cmp_loop_info, 16);
	loop_ana->birg = birg;

Matthias Braun's avatar
Matthias Braun committed
169
170
171
	DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
	DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
	DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
172

173
174
175
176
177
	/* construct control flow loop tree */
	if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
		construct_cf_backedges(irg);
	}

178
	be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
179
180
181
182
183
184
185

	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
186
 * @return The loop analysis object.
187
 */
188
189
190
be_loopana_t *be_new_loop_pressure(be_irg_t *birg,
                                   const arch_register_class_t *cls)
{
191
	ir_graph         *irg      = be_get_birg_irg(birg);
192
	be_loopana_t     *loop_ana = XMALLOC(be_loopana_t);
193
194
195
	ir_loop          *irg_loop = get_irg_loop(irg);
	const arch_env_t *arch_env = be_get_birg_arch_env(birg);
	int               i;
196
197
198
199

	loop_ana->data = new_set(cmp_loop_info, 16);
	loop_ana->birg = birg;

200
201
202
203
204
	/* construct control flow loop tree */
	if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
		construct_cf_backedges(irg);
	}

205
	if (cls != NULL) {
206
		be_compute_loop_pressure(loop_ana, irg_loop, cls);
207
208
209
210
211
212
213
214
	} else {
		for (i = arch_env_get_n_reg_class(arch_env) - 1; i >= 0; --i) {
			const arch_register_class_t *cls = arch_env_get_reg_class(arch_env, i);
			DBG((dbg, LEVEL_1, "\n=====================================================\n", cls->name));
			DBG((dbg, LEVEL_1, " Computing register pressure for class %s:\n", cls->name));
			DBG((dbg, LEVEL_1, "=====================================================\n", cls->name));
			be_compute_loop_pressure(loop_ana, irg_loop, cls);
		}
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	}

	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;
232
	entry    = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
233
234
235
236
237
238
239
240
241
242

	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
243
 * Frees the loop analysis object.
244
245
246
247
248
 */
void be_free_loop_pressure(be_loopana_t *loop_ana) {
	del_set(loop_ana->data);
	xfree(loop_ana);
}
Matthias Braun's avatar
Matthias Braun committed
249
250
251
252
253
254
255

void be_init_loopana(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
}

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);