beloopana.c 6.43 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
9
10
 * @file
 * @brief       Compute register pressure in loops.
 * @author      Christian Wuerdig
 * @date        19.02.2007
11
 */
12
#include "config.h"
13
14
15
16

#include "set.h"
#include "pset.h"
#include "irnode.h"
17
#include "util.h"
18
#include "irloop_t.h"
19
#include "error.h"
20
#include "debug.h"
21

22
#include "bearch.h"
23
#include "beirg.h"
24
25
26
#include "belive.h"
#include "besched.h"
#include "beloopana.h"
Matthias Braun's avatar
Matthias Braun committed
27
28
#include "bemodule.h"

29
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
30

31
#define HASH_LOOP_INFO(info) (hash_ptr((info)->loop) ^ hash_ptr((info)->cls))
32

33
typedef struct be_loop_info_t {
34
35
36
37
38
	ir_loop                     *loop;
	const arch_register_class_t *cls;
	unsigned                    max_pressure;
} be_loop_info_t;

39
struct be_loopana_t {
40
	set      *data;
41
	ir_graph *irg;
42
43
};

44
45
static int cmp_loop_info(const void *a, const void *b, size_t size)
{
46
47
	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
48
	(void) size;
49
50
51
52
53
54

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

/**
 * Compute the highest register pressure in a block.
55
 * @param irg       The graph.
56
57
58
59
 * @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.
 */
60
61
62
static unsigned be_compute_block_pressure(const ir_graph *irg,
                                          ir_node *block,
                                          const arch_register_class_t *cls)
63
{
64
65
66
	be_lv_t     *lv = be_get_irg_liveness(irg);
	ir_nodeset_t live_nodes;
	size_t       max_live;
67

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

70
	/* determine largest pressure with this block */
71
	ir_nodeset_init(&live_nodes);
72
	be_liveness_end_of_block(lv, cls, block, &live_nodes);
73
	max_live   = ir_nodeset_size(&live_nodes);
74
75

	sched_foreach_reverse(block, irn) {
76
		size_t cnt;
77
78
79
80

		if (is_Phi(irn))
			break;

81
		be_liveness_transfer(cls, irn, &live_nodes);
Michael Beck's avatar
Michael Beck committed
82
83
		cnt      = ir_nodeset_size(&live_nodes);
		max_live = MAX(cnt, max_live);
84
85
	}

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

88
	ir_nodeset_destroy(&live_nodes);
89
90
91
92
	return max_live;
}

/**
93
 * Compute the highest register pressure in a loop and its sub-loops.
94
95
96
97
98
 * @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.
 */
99
100
static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop,
                                         const arch_register_class_t *cls)
101
{
Michael Beck's avatar
Michael Beck committed
102
	size_t         i, max;
103
	unsigned       pressure;
104
105
	be_loop_info_t *entry, key;

Michael Beck's avatar
Michael Beck committed
106
	DBG((dbg, LEVEL_1, "\nProcessing Loop %ld\n", loop->loop_nr));
107
108
109
110
111
	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) {
112
		unsigned     son_pressure;
113
		loop_element elem = get_loop_element(loop, i);
114

Michael Beck's avatar
Michael Beck committed
115
		if (*elem.kind == k_ir_node)
116
			son_pressure = be_compute_block_pressure(loop_ana->irg, elem.node, cls);
Michael Beck's avatar
Michael Beck committed
117
118
119
		else {
			assert(*elem.kind == k_ir_loop);
			son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
120
		}
121
122

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

	/* update info in set */
127
128
129
	key.loop            = loop;
	key.cls             = cls;
	key.max_pressure    = 0;
130
	entry               = set_insert(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
131
132
133
134
135
136
	entry->max_pressure = MAX(entry->max_pressure, pressure);

	return pressure;
}

/**
137
138
 * Compute the register pressure for a class of all loops in a graph
 * @param irg   The graph
139
 * @param cls   The register class to compute the pressure for
Christian Würdig's avatar
Christian Würdig committed
140
 * @return The loop analysis object.
141
 */
142
be_loopana_t *be_new_loop_pressure_cls(ir_graph *irg,
143
144
                                       const arch_register_class_t *cls)
{
145
	be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
146
147

	loop_ana->data = new_set(cmp_loop_info, 16);
148
	loop_ana->irg  = irg;
149

Matthias Braun's avatar
Matthias Braun committed
150
151
152
	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));
153

154
	assure_loopinfo(irg);
155

156
	be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
157
158
159
160
161

	return loop_ana;
}

/**
162
163
 * 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
164
 * @return The loop analysis object.
165
 */
166
be_loopana_t *be_new_loop_pressure(ir_graph *irg,
167
168
                                   const arch_register_class_t *cls)
{
169
	be_loopana_t     *loop_ana = XMALLOC(be_loopana_t);
170
	ir_loop          *irg_loop = get_irg_loop(irg);
171
	const arch_env_t *arch_env = be_get_irg_arch_env(irg);
172
	int               i;
173
174

	loop_ana->data = new_set(cmp_loop_info, 16);
175
	loop_ana->irg  = irg;
176

177
	assure_loopinfo(irg);
178

179
	if (cls != NULL) {
180
		be_compute_loop_pressure(loop_ana, irg_loop, cls);
181
	} else {
182
183
		for (i = arch_env->n_register_classes - 1; i >= 0; --i) {
			const arch_register_class_t *cls = &arch_env->register_classes[i];
184
185
186
187
188
			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);
		}
189
190
191
192
193
194
195
196
197
	}

	return loop_ana;
}

/**
 * Returns the computed register pressure for the given class and loop.
 * @return The pressure or INT_MAX if not found
 */
198
199
unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop)
{
200
201
202
203
204
205
206
	unsigned pressure = INT_MAX;
	be_loop_info_t *entry, key;

	assert(cls && loop);

	key.loop = loop;
	key.cls  = cls;
207
	entry    = set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
208
209
210
211

	if (entry)
		pressure = entry->max_pressure;
	else
Michael Beck's avatar
Michael Beck committed
212
		panic("Pressure not computed for given class and loop object.");
213
214
215
216
217

	return pressure;
}

/**
Christian Würdig's avatar
Christian Würdig committed
218
 * Frees the loop analysis object.
219
 */
220
221
void be_free_loop_pressure(be_loopana_t *loop_ana)
{
222
223
224
	del_set(loop_ana->data);
	xfree(loop_ana);
}
Matthias Braun's avatar
Matthias Braun committed
225

Matthias Braun's avatar
Matthias Braun committed
226
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
Matthias Braun's avatar
Matthias Braun committed
227
228
229
230
void be_init_loopana(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
}