beloopana.c 7.2 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
typedef struct be_loop_info_t {
48
49
50
51
52
	ir_loop                     *loop;
	const arch_register_class_t *cls;
	unsigned                    max_pressure;
} be_loop_info_t;

53
struct be_loopana_t {
54
	set      *data;
55
	ir_graph *irg;
56
57
};

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.
69
 * @param irg       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.
 */
74
75
76
static unsigned be_compute_block_pressure(const ir_graph *irg,
                                          ir_node *block,
                                          const arch_register_class_t *cls)
77
{
78
	be_lv_t      *lv = be_get_irg_liveness(irg);
Michael Beck's avatar
Michael Beck committed
79
80
81
	ir_nodeset_t  live_nodes;
	ir_node      *irn;
	int          max_live;
82

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

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

	sched_foreach_reverse(block, irn) {
		int cnt;

		if (is_Phi(irn))
			break;

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

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

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

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

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

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

	/* update info in set */
142
143
144
145
	key.loop            = loop;
	key.cls             = cls;
	key.max_pressure    = 0;
	entry               = set_insert(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
146
147
148
149
150
151
	entry->max_pressure = MAX(entry->max_pressure, pressure);

	return pressure;
}

/**
152
153
 * Compute the register pressure for a class of all loops in a graph
 * @param irg   The graph
154
 * @param cls   The register class to compute the pressure for
Christian Würdig's avatar
Christian Würdig committed
155
 * @return The loop analysis object.
156
 */
157
be_loopana_t *be_new_loop_pressure_cls(ir_graph *irg,
158
159
                                       const arch_register_class_t *cls)
{
160
	be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
161
162

	loop_ana->data = new_set(cmp_loop_info, 16);
163
	loop_ana->irg  = irg;
164

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

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

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

	return loop_ana;
}

/**
180
181
 * Compute the register pressure for all classes of all loops in the irg.
 * @param irg  The graph
Christian Würdig's avatar
Christian Würdig committed
182
 * @return The loop analysis object.
183
 */
184
be_loopana_t *be_new_loop_pressure(ir_graph *irg,
185
186
                                   const arch_register_class_t *cls)
{
187
	be_loopana_t     *loop_ana = XMALLOC(be_loopana_t);
188
	ir_loop          *irg_loop = get_irg_loop(irg);
189
	const arch_env_t *arch_env = be_get_irg_arch_env(irg);
190
	int               i;
191
192

	loop_ana->data = new_set(cmp_loop_info, 16);
193
	loop_ana->irg  = irg;
194

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
	} else {
203
204
		for (i = arch_env->n_register_classes - 1; i >= 0; --i) {
			const arch_register_class_t *cls = &arch_env->register_classes[i];
205
206
207
208
209
			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");
}