beloopana.c 7.23 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
#include "config.h"
28
29
30
31
32
33

#include "set.h"
#include "pset.h"
#include "irnode.h"
#include "irtools.h"
#include "irloop_t.h"
34
#include "error.h"
35
#include "debug.h"
36

37
#include "bearch.h"
38
39
40
#include "belive.h"
#include "besched.h"
#include "beloopana.h"
Matthias Braun's avatar
Matthias Braun committed
41
42
43
#include "bemodule.h"

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL);
44

45
46
#define HASH_LOOP_INFO(info) (HASH_PTR((info)->loop) ^ HASH_PTR((info)->cls))

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

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

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

/**
 * Compute the highest register pressure in a block.
Michael Beck's avatar
Michael Beck committed
69
 * @param birg      The graph.
70
71
72
73
 * @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.
 */
Michael Beck's avatar
Michael Beck committed
74
static unsigned be_compute_block_pressure(const be_irg_t *birg,
75
76
		ir_node *block, const arch_register_class_t *cls)
{
Michael Beck's avatar
Michael Beck committed
77
78
79
80
	be_lv_t      *lv = be_get_birg_liveness(birg);
	ir_nodeset_t  live_nodes;
	ir_node      *irn;
	int          max_live;
81

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

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

	sched_foreach_reverse(block, irn) {
		int cnt;

		if (is_Phi(irn))
			break;

95
		be_liveness_transfer(cls, irn, &live_nodes);
Michael Beck's avatar
Michael Beck committed
96
97
		cnt      = ir_nodeset_size(&live_nodes);
		max_live = MAX(cnt, max_live);
98
99
	}

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

102
	ir_nodeset_destroy(&live_nodes);
103
104
105
106
107
108
109
110
111
112
	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.
 */
113
114
static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls)
{
115
116
	int            i, max;
	unsigned       pressure;
117
118
	be_loop_info_t *entry, key;

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

Michael Beck's avatar
Michael Beck committed
128
129
130
131
132
		if (*elem.kind == k_ir_node)
			son_pressure = be_compute_block_pressure(loop_ana->birg, elem.node, cls);
		else {
			assert(*elem.kind == k_ir_loop);
			son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
133
		}
134
135

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

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

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

Matthias Braun's avatar
Matthias Braun committed
164
165
166
	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));
167

168
169
170
171
172
	/* construct control flow loop tree */
	if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
		construct_cf_backedges(irg);
	}

173
	be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
174
175
176
177
178
179
180

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

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

195
196
197
198
199
	/* construct control flow loop tree */
	if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
		construct_cf_backedges(irg);
	}

200
	if (cls != NULL) {
201
		be_compute_loop_pressure(loop_ana, irg_loop, cls);
202
203
204
205
206
207
208
209
	} 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);
		}
210
211
212
213
214
215
216
217
218
	}

	return loop_ana;
}

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

	assert(cls && loop);

	key.loop = loop;
	key.cls  = cls;
228
	entry    = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
229
230
231
232

	if (entry)
		pressure = entry->max_pressure;
	else
Michael Beck's avatar
Michael Beck committed
233
		panic("Pressure not computed for given class and loop object.");
234
235
236
237
238

	return pressure;
}

/**
Christian Würdig's avatar
Christian Würdig committed
239
 * Frees the loop analysis object.
240
 */
241
242
void be_free_loop_pressure(be_loopana_t *loop_ana)
{
243
244
245
	del_set(loop_ana->data);
	xfree(loop_ana);
}
Matthias Braun's avatar
Matthias Braun committed
246

247
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);
Matthias Braun's avatar
Matthias Braun committed
248
249
250
251
void be_init_loopana(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
}