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_t.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;
};

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

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

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

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

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

	sched_foreach_reverse(block, irn) {
		int cnt;

		if (is_Phi(irn))
			break;

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

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

101
	ir_nodeset_destroy(&live_nodes);
102
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.
 */
static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop, const arch_register_class_t *cls) {
113
114
	int            i, max;
	unsigned       pressure;
115
116
	be_loop_info_t *entry, key;

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

Michael Beck's avatar
Michael Beck committed
126
127
128
129
130
		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);
131
		}
132
133

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

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

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

Matthias Braun's avatar
Matthias Braun committed
161
162
163
	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));
164

165
166
167
168
169
	/* construct control flow loop tree */
	if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
		construct_cf_backedges(irg);
	}

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

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

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

192
193
194
195
196
	/* construct control flow loop tree */
	if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
		construct_cf_backedges(irg);
	}

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

	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;
224
	entry    = set_find(loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
225
226
227
228

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

	return pressure;
}

/**
Christian Würdig's avatar
Christian Würdig committed
235
 * Frees the loop analysis object.
236
237
238
239
240
 */
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
241
242
243
244
245
246
247

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

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana);