beloopana.c 4.34 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"
Matthias Braun's avatar
Matthias Braun committed
16
#include "panic.h"
17
#include "debug.h"
18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return pressure;
}

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

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

131
	assure_loopinfo(irg);
132

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

	return loop_ana;
}

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

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

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

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