beloopana.c 4.3 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
13
 */
#include "set.h"
#include "irnode.h"
14
#include "util.h"
15
#include "irloop_t.h"
16
#include "debug.h"
17

18
#include "bearch.h"
19
#include "beirg.h"
20
21
22
#include "belive.h"
#include "besched.h"
#include "beloopana.h"
Matthias Braun's avatar
Matthias Braun committed
23
24
#include "bemodule.h"

25
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
26

27
#define HASH_LOOP_INFO(info) (hash_ptr((info)->loop) ^ hash_ptr((info)->cls))
28

29
typedef struct be_loop_info_t {
30
31
	ir_loop                     *loop;
	const arch_register_class_t *cls;
Matthias Braun's avatar
Matthias Braun committed
32
	unsigned                     max_pressure;
33
34
} be_loop_info_t;

35
struct be_loopana_t {
36
	set *data;
37
38
};

39
40
static int cmp_loop_info(const void *a, const void *b, size_t size)
{
Matthias Braun's avatar
Matthias Braun committed
41
	(void)size;
42
43
	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
44
	return i1->loop != i2->loop || i1->cls != i2->cls;
45
46
47
48
49
50
51
52
}

/**
 * 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.
 */
53
static unsigned be_compute_block_pressure(ir_node *const block, arch_register_class_t const *const cls)
54
{
55

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

58
	/* determine largest pressure with this block */
Matthias Braun's avatar
Matthias Braun committed
59
	ir_nodeset_t live_nodes;
60
	ir_nodeset_init(&live_nodes);
61
	be_lv_t *const lv = be_get_irg_liveness(get_irn_irg(block));
62
	be_liveness_end_of_block(lv, cls, block, &live_nodes);
Matthias Braun's avatar
Matthias Braun committed
63
	unsigned max_live = (unsigned)ir_nodeset_size(&live_nodes);
64

65
	sched_foreach_non_phi_reverse(block, irn) {
66
		be_liveness_transfer(cls, irn, &live_nodes);
Matthias Braun's avatar
Matthias Braun committed
67
		unsigned cnt = (unsigned)ir_nodeset_size(&live_nodes);
Michael Beck's avatar
Michael Beck committed
68
		max_live = MAX(cnt, max_live);
69
70
	}

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

74
	ir_nodeset_destroy(&live_nodes);
75
76
77
78
	return max_live;
}

/**
79
 * Compute the highest register pressure in a loop and its sub-loops.
80
81
82
83
84
 * @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.
 */
85
86
static unsigned be_compute_loop_pressure(be_loopana_t *loop_ana, ir_loop *loop,
                                         const arch_register_class_t *cls)
87
{
Michael Beck's avatar
Michael Beck committed
88
	DBG((dbg, LEVEL_1, "\nProcessing Loop %ld\n", loop->loop_nr));
89
90

	/* determine maximal pressure in all loop elements */
Matthias Braun's avatar
Matthias Braun committed
91
92
	unsigned pressure = 0;
	for (size_t i = 0, max = get_loop_n_elements(loop); i < max; ++i) {
93
		unsigned     son_pressure;
94
		loop_element elem = get_loop_element(loop, i);
95

96
		if (*elem.kind == k_ir_node) {
97
			son_pressure = be_compute_block_pressure(elem.node, cls);
98
		} else {
Michael Beck's avatar
Michael Beck committed
99
100
			assert(*elem.kind == k_ir_loop);
			son_pressure = be_compute_loop_pressure(loop_ana, elem.son, cls);
101
		}
102
103

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

	/* update info in set */
Matthias Braun's avatar
Matthias Braun committed
108
	be_loop_info_t key;
109
110
111
	key.loop            = loop;
	key.cls             = cls;
	key.max_pressure    = 0;
Matthias Braun's avatar
Matthias Braun committed
112
113
	be_loop_info_t *entry
		= set_insert(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
114
115
116
117
118
	entry->max_pressure = MAX(entry->max_pressure, pressure);

	return pressure;
}

119
be_loopana_t *be_new_loop_pressure(ir_graph *const irg, arch_register_class_t const *const cls)
120
{
121
	be_loopana_t *loop_ana = XMALLOC(be_loopana_t);
122
123
	loop_ana->data = new_set(cmp_loop_info, 16);

Matthias Braun's avatar
Matthias Braun committed
124
125
	DBG((dbg, LEVEL_1, "Computing loop register pressure for class %s:\n",
	     cls->name));
126

127
	assure_loopinfo(irg);
128

129
	be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
130
131
132
133

	return loop_ana;
}

134
135
unsigned be_get_loop_pressure(be_loopana_t *loop_ana, const arch_register_class_t *cls, ir_loop *loop)
{
Matthias Braun's avatar
Matthias Braun committed
136
	be_loop_info_t key;
137
138
	key.loop = loop;
	key.cls  = cls;
Matthias Braun's avatar
Matthias Braun committed
139
140
	be_loop_info_t *entry
		= set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
141

Matthias Braun's avatar
Matthias Braun committed
142
	return entry->max_pressure;
143
144
}

145
146
void be_free_loop_pressure(be_loopana_t *loop_ana)
{
147
	del_set(loop_ana->data);
148
	free(loop_ana);
149
}
Matthias Braun's avatar
Matthias Braun committed
150

Matthias Braun's avatar
Matthias Braun committed
151
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_loopana)
Matthias Braun's avatar
Matthias Braun committed
152
153
154
155
void be_init_loopana(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.loopana");
}