beloopana.c 4.32 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
66
67
68

	sched_foreach_reverse(block, irn) {
		if (is_Phi(irn))
			break;

69
		be_liveness_transfer(cls, irn, &live_nodes);
Matthias Braun's avatar
Matthias Braun committed
70
		unsigned cnt = (unsigned)ir_nodeset_size(&live_nodes);
Michael Beck's avatar
Michael Beck committed
71
		max_live = MAX(cnt, max_live);
72
73
	}

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

77
	ir_nodeset_destroy(&live_nodes);
78
79
80
81
	return max_live;
}

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

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

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

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

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

	return pressure;
}

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

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

130
	assure_loopinfo(irg);
131

132
	be_compute_loop_pressure(loop_ana, get_irg_loop(irg), cls);
133
134
135
136

	return loop_ana;
}

137
138
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
139
	be_loop_info_t key;
140
141
	key.loop = loop;
	key.cls  = cls;
Matthias Braun's avatar
Matthias Braun committed
142
143
	be_loop_info_t *entry
		= set_find(be_loop_info_t, loop_ana->data, &key, sizeof(key), HASH_LOOP_INFO(&key));
144

Matthias Braun's avatar
Matthias Braun committed
145
	return entry->max_pressure;
146
147
}

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

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