beloopana.c 4.77 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

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

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

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

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

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

38
struct be_loopana_t {
39
	set *data;
40
41
};

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

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

/**
 * Compute the highest register pressure in a block.
 * @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.
 */
57
static unsigned be_compute_block_pressure(ir_node *const block, arch_register_class_t const *const cls)
58
{
59
60
	ir_nodeset_t live_nodes;
	size_t       max_live;
61

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

64
	/* determine largest pressure with this block */
65
	ir_nodeset_init(&live_nodes);
66
	be_lv_t *const lv = be_get_irg_liveness(get_Block_irg(block));
67
	be_liveness_end_of_block(lv, cls, block, &live_nodes);
68
	max_live   = ir_nodeset_size(&live_nodes);
69
70

	sched_foreach_reverse(block, irn) {
71
		size_t cnt;
72
73
74
75

		if (is_Phi(irn))
			break;

76
		be_liveness_transfer(cls, irn, &live_nodes);
Michael Beck's avatar
Michael Beck committed
77
78
		cnt      = ir_nodeset_size(&live_nodes);
		max_live = MAX(cnt, max_live);
79
80
	}

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

83
	ir_nodeset_destroy(&live_nodes);
84
85
86
87
	return max_live;
}

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

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

Michael Beck's avatar
Michael Beck committed
110
		if (*elem.kind == k_ir_node)
111
			son_pressure = be_compute_block_pressure(elem.node, cls);
Michael Beck's avatar
Michael Beck committed
112
113
114
		else {
			assert(*elem.kind == k_ir_loop);
			son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
115
		}
116
117

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

	/* update info in set */
122
123
124
	key.loop            = loop;
	key.cls             = cls;
	key.max_pressure    = 0;
125
	entry               = set_insert(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
126
127
128
129
130
	entry->max_pressure = MAX(entry->max_pressure, pressure);

	return pressure;
}

131
be_loopana_t *be_new_loop_pressure(ir_graph *const irg, arch_register_class_t const *const cls)
132
{
133
	be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
134
135
136

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

Matthias Braun's avatar
Matthias Braun committed
137
138
139
	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));
140

141
	assure_loopinfo(irg);
142

143
	be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
144
145
146
147

	return loop_ana;
}

148
149
unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop)
{
150
151
152
153
154
155
156
	unsigned pressure = INT_MAX;
	be_loop_info_t *entry, key;

	assert(cls && loop);

	key.loop = loop;
	key.cls  = cls;
157
	entry    = set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
158
159
160
161

	if (entry)
		pressure = entry->max_pressure;
	else
Michael Beck's avatar
Michael Beck committed
162
		panic("Pressure not computed for given class and loop object.");
163
164
165
166

	return pressure;
}

167
168
void be_free_loop_pressure(be_loopana_t *loop_ana)
{
169
170
171
	del_set(loop_ana->data);
	xfree(loop_ana);
}
Matthias Braun's avatar
Matthias Braun committed
172

Matthias Braun's avatar
Matthias Braun committed
173
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
Matthias Braun's avatar
Matthias Braun committed
174
175
176
177
void be_init_loopana(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
}