beloopana.c 7.02 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
2
 * Copyright (C) 1995-2011 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
 * @file
 * @brief       Compute register pressure in loops.
 * @author      Christian Wuerdig
 * @date        19.02.2007
25
 */
26
#include "config.h"
27
28
29
30

#include "set.h"
#include "pset.h"
#include "irnode.h"
31
#include "util.h"
32
#include "irloop_t.h"
33
#include "error.h"
34
#include "debug.h"
35

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

43
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
44

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

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 = (const be_loop_info_t*)a;
	const be_loop_info_t *i2 = (const be_loop_info_t*)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
79
80
	be_lv_t     *lv = be_get_irg_liveness(irg);
	ir_nodeset_t live_nodes;
	size_t       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

	sched_foreach_reverse(block, irn) {
90
		size_t cnt;
91
92
93
94

		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
	}

100
	DBG((dbg, LEVEL_1, "Finished with Block %+F (%s %zu)\n", block, cls->name, max_live));
101

102
	ir_nodeset_destroy(&live_nodes);
103
104
105
106
	return max_live;
}

/**
107
 * Compute the highest register pressure in a loop and its sub-loops.
108
109
110
111
112
 * @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
{
Michael Beck's avatar
Michael Beck committed
116
	size_t         i, max;
117
	unsigned       pressure;
118
119
	be_loop_info_t *entry, key;

Michael Beck's avatar
Michael Beck committed
120
	DBG((dbg, LEVEL_1, "\nProcessing Loop %ld\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

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

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

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

	return pressure;
}

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

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

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
	assure_loopinfo(irg);
169

170
	be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
171
172
173
174
175

	return loop_ana;
}

/**
176
177
 * 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
178
 * @return The loop analysis object.
179
 */
180
be_loopana_t *be_new_loop_pressure(ir_graph *irg,
181
182
                                   const arch_register_class_t *cls)
{
183
	be_loopana_t     *loop_ana = XMALLOC(be_loopana_t);
184
	ir_loop          *irg_loop = get_irg_loop(irg);
185
	const arch_env_t *arch_env = be_get_irg_arch_env(irg);
186
	int               i;
187
188

	loop_ana->data = new_set(cmp_loop_info, 16);
189
	loop_ana->irg  = irg;
190

191
	assure_loopinfo(irg);
192

193
	if (cls != NULL) {
194
		be_compute_loop_pressure(loop_ana, irg_loop, cls);
195
	} else {
196
197
		for (i = arch_env->n_register_classes - 1; i >= 0; --i) {
			const arch_register_class_t *cls = &arch_env->register_classes[i];
198
199
200
201
202
			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);
		}
203
204
205
206
207
208
209
210
211
	}

	return loop_ana;
}

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

	assert(cls && loop);

	key.loop = loop;
	key.cls  = cls;
221
	entry    = set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
222
223
224
225

	if (entry)
		pressure = entry->max_pressure;
	else
Michael Beck's avatar
Michael Beck committed
226
		panic("Pressure not computed for given class and loop object.");
227
228
229
230
231

	return pressure;
}

/**
Christian Würdig's avatar
Christian Würdig committed
232
 * Frees the loop analysis object.
233
 */
234
235
void be_free_loop_pressure(be_loopana_t *loop_ana)
{
236
237
238
	del_set(loop_ana->data);
	xfree(loop_ana);
}
Matthias Braun's avatar
Matthias Braun committed
239

Matthias Braun's avatar
Matthias Braun committed
240
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
Matthias Braun's avatar
Matthias Braun committed
241
242
243
244
void be_init_loopana(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
}