beuses.c 8.93 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
 */

Sebastian Hack's avatar
Sebastian Hack committed
6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
9
10
 * @file
 * @brief       Methods to compute when a value will be used again.
 * @author      Sebastian Hack, Matthias Braun
 * @date        27.06.2005
Sebastian Hack's avatar
Sebastian Hack committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 */
#include <limits.h>
#include <stdlib.h>

#include "obst.h"
#include "pmap.h"
#include "debug.h"

#include "irgwalk.h"
#include "irnode_t.h"
#include "ircons_t.h"
#include "irgraph_t.h"
#include "iredges_t.h"
#include "irdom_t.h"
25
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
26
27

#include "be_t.h"
Matthias Braun's avatar
Matthias Braun committed
28
#include "belive.h"
29
#include "benode.h"
30
31
#include "besched.h"
#include "bearch.h"
Christian Würdig's avatar
Christian Würdig committed
32
#include "beuses.h"
Sebastian Hack's avatar
Sebastian Hack committed
33

Matthias Braun's avatar
Matthias Braun committed
34
35
#define UNKNOWN_OUTERMOST_LOOP  ((unsigned)-1)

Matthias Braun's avatar
Matthias Braun committed
36
37
DEBUG_ONLY(static firm_dbg_module_t *dbg;)

38
typedef struct be_use_t {
39
40
	const ir_node *block;
	const ir_node *node;
Matthias Braun's avatar
Matthias Braun committed
41
42
43
	unsigned       outermost_loop;
	unsigned       next_use;
	ir_visited_t   visited;
Sebastian Hack's avatar
Sebastian Hack committed
44
45
} be_use_t;

46
47
48
/**
 * The "uses" environment.
 */
49
struct be_uses_t {
Matthias Braun's avatar
Matthias Braun committed
50
51
52
	set           *uses; /**< cache: contains all computed uses so far. */
	const be_lv_t *lv;   /**< the liveness for the graph. */
	ir_visited_t   visited_counter; /**< current search counter. */
Sebastian Hack's avatar
Sebastian Hack committed
53
54
};

55
56
57
/**
 * Set-compare two uses.
 */
Sebastian Hack's avatar
Sebastian Hack committed
58
59
static int cmp_use(const void *a, const void *b, size_t n)
{
Matthias Braun's avatar
Matthias Braun committed
60
	(void)n;
61
62
	const be_use_t *p = (const be_use_t*)a;
	const be_use_t *q = (const be_use_t*)b;
Matthias Braun's avatar
Matthias Braun committed
63
	return p->block != q->block || p->node != q->node;
Sebastian Hack's avatar
Sebastian Hack committed
64
65
}

66
static be_next_use_t get_next_use(be_uses_t *env, ir_node *from,
Matthias Braun's avatar
Matthias Braun committed
67
								  const ir_node *def, bool skip_from_uses);
68

69
70
71
72
73
74
75
76
/**
 * Return the use for the given definition in the given block if exists,
 * else create it.
 *
 * @param env    the uses environment
 * @param block  the block we search the use in
 * @param def    the definition of the value we are searching
 */
77
static const be_use_t *get_or_set_use_block(be_uses_t *env,
78
79
                                            const ir_node *block,
                                            const ir_node *def)
Sebastian Hack's avatar
Sebastian Hack committed
80
{
81
82
	be_use_t temp;
	temp.block = block;
Matthias Braun's avatar
Matthias Braun committed
83
84
	temp.node  = def;

Matthias Braun's avatar
Matthias Braun committed
85
	unsigned  hash   = hash_combine(hash_irn(block), hash_irn(def));
Matthias Braun's avatar
Matthias Braun committed
86
	be_use_t *result = set_find(be_use_t, env->uses, &temp, sizeof(temp), hash);
Sebastian Hack's avatar
Sebastian Hack committed
87

88
	if (result == NULL) {
89
90
		// insert templ first as we might end in a loop in the get_next_use
		// call otherwise
Matthias Braun's avatar
Matthias Braun committed
91
		temp.next_use       = USES_INFINITY;
Matthias Braun's avatar
Matthias Braun committed
92
		temp.outermost_loop = UNKNOWN_OUTERMOST_LOOP;
Matthias Braun's avatar
Matthias Braun committed
93
		temp.visited        = 0;
94
		result = set_insert(be_use_t, env->uses, &temp, sizeof(temp), hash);
95
96
	}

Matthias Braun's avatar
Matthias Braun committed
97
	if (result->outermost_loop == UNKNOWN_OUTERMOST_LOOP
Matthias Braun's avatar
Matthias Braun committed
98
	 && result->visited < env->visited_counter) {
99
		result->visited = env->visited_counter;
Matthias Braun's avatar
Matthias Braun committed
100
		be_next_use_t next_use = get_next_use(env, sched_first(block), def, 0);
Matthias Braun's avatar
Matthias Braun committed
101
		if (next_use.outermost_loop != UNKNOWN_OUTERMOST_LOOP) {
Matthias Braun's avatar
Matthias Braun committed
102
			result->next_use       = next_use.time;
103
			result->outermost_loop = next_use.outermost_loop;
Matthias Braun's avatar
Matthias Braun committed
104
			DBG((dbg, LEVEL_5, "Setting nextuse of %+F in block %+F to %u (outermostloop %d)\n",
105
				def, block, result->next_use, result->outermost_loop));
106
		}
107
	}
108

109
	return result;
Sebastian Hack's avatar
Sebastian Hack committed
110
111
}

112
113
114
115
116
117
118
119
120
121
/**
 * Check if a value of the given definition is used in the given block
 * as a Phi argument.
 *
 * @param block  the block to check
 * @param def    the definition of the value
 *
 * @return non-zero if the value is used in the given block as a Phi argument
 * in one of its successor blocks.
 */
Matthias Braun's avatar
Matthias Braun committed
122
static bool be_is_phi_argument(const ir_node *block, const ir_node *def)
123
{
124
	if (get_irn_n_edges_kind(block, EDGE_KIND_BLOCK) < 1)
Matthias Braun's avatar
Matthias Braun committed
125
		return false;
126

127
128
	const ir_edge_t *edge = get_irn_out_edge_first_kind(block, EDGE_KIND_BLOCK);
	ir_node *const succ_block = get_edge_src_irn(edge);
129
	if (get_Block_n_cfgpreds(succ_block) <= 1) {
130
		/* no Phis in the successor */
Matthias Braun's avatar
Matthias Braun committed
131
		return false;
132
	}
133

134
135
	/* iterate over the Phi nodes in the successor and check if def is
	 * one of its arguments */
136
	const int i = get_edge_src_pos(edge);
137
	sched_foreach(succ_block, node) {
Matthias Braun's avatar
Matthias Braun committed
138
139
		/* we can stop the search on the first non-phi node */
		if (!is_Phi(node))
140
141
			break;

Matthias Braun's avatar
Matthias Braun committed
142
		const ir_node *arg = get_irn_n(node, i);
143
		if (arg == def)
Matthias Braun's avatar
Matthias Braun committed
144
			return true;
145
146
	}

Matthias Braun's avatar
Matthias Braun committed
147
	return false;
148
149
}

150
/**
Matthias Braun's avatar
Matthias Braun committed
151
 * Retrieve the scheduled index (the "step") of this node in its block.
152
 */
153
static inline unsigned get_step(const ir_node *node)
154
{
155
156
157
158
	return (unsigned)PTR_TO_INT(get_irn_link(node));
}

/**
Matthias Braun's avatar
Matthias Braun committed
159
 * Set the scheduled index (the "step") of this node in its block.
160
161
162
163
 */
static inline void set_step(ir_node *node, unsigned step)
{
	set_irn_link(node, INT_TO_PTR(step));
164
165
}

166
167
168
169
170
171
172
173
/**
 * Find the next use of a value defined by def, starting at node from.
 *
 * @param env             the uses environment
 * @param from            the node at which we should start the search
 * @param def             the definition of the value
 * @param skip_from_uses  if non-zero, ignore from uses
 */
174
static be_next_use_t get_next_use(be_uses_t *env, ir_node *from,
Matthias Braun's avatar
Matthias Braun committed
175
								  const ir_node *def, bool skip_from_uses)
Sebastian Hack's avatar
Sebastian Hack committed
176
{
177
	if (skip_from_uses) {
178
179
180
		from = sched_next(from);
	}

Matthias Braun's avatar
Matthias Braun committed
181
182
183
184
	ir_node *next_use_node = NULL;
	unsigned next_use_step = INT_MAX;
	unsigned timestep      = get_step(from);
	ir_node *block         = get_nodes_block(from);
185
	foreach_out_edge(def, edge) {
Matthias Braun's avatar
Matthias Braun committed
186
		ir_node *node = get_edge_src_irn(edge);
187
		if (is_Anchor(node))
188
			continue;
189
		if (get_nodes_block(node) != block)
190
			continue;
191
		if (is_Phi(node))
192
193
			continue;

Matthias Braun's avatar
Matthias Braun committed
194
		unsigned node_step = get_step(node);
195
		if (node_step < timestep)
196
			continue;
197
		if (node_step < next_use_step) {
198
			next_use_node = node;
199
200
201
202
			next_use_step = node_step;
		}
	}

203
	if (next_use_node != NULL) {
204
205
206
		be_next_use_t result;
		result.time           = next_use_step - timestep + skip_from_uses;
		result.outermost_loop = get_loop_depth(get_irn_loop(block));
207
		result.before         = next_use_node;
208
209
210
		return result;
	}

Matthias Braun's avatar
Matthias Braun committed
211
212
	ir_node *node = sched_last(block);
	unsigned step = get_step(node) + 1 + timestep + skip_from_uses;
213

214
	if (be_is_phi_argument(block, def)) {
215
216
217
		// TODO we really should continue searching the uses of the phi,
		// as a phi isn't a real use that implies a reload (because we could
		// easily spill the whole phi)
218
219

		be_next_use_t result;
220
		result.time           = step;
221
		result.outermost_loop = get_loop_depth(get_irn_loop(block));
222
		result.before         = block;
223
		return result;
224
	}
225

226
	be_next_use_t result;
Matthias Braun's avatar
Matthias Braun committed
227
	result.before = NULL;
Matthias Braun's avatar
Matthias Braun committed
228
229
230
231
232
233
	ir_loop  *loop           = get_irn_loop(block);
	unsigned  loopdepth      = get_loop_depth(loop);
	bool      found_visited  = false;
	bool      found_use      = false;
	unsigned  outermost_loop = loopdepth;
	unsigned  next_use       = USES_INFINITY;
234
235
	foreach_block_succ(block, edge) {
		const ir_node *succ_block = get_edge_src_irn(edge);
Matthias Braun's avatar
Matthias Braun committed
236
237
		DBG((dbg, LEVEL_5, "Checking succ of block %+F: %+F (for use of %+F)\n",
		     block, succ_block, def));
238
		if (!be_is_live_in(env->lv, succ_block, def)) {
239
			//next_use = USES_INFINITY;
Matthias Braun's avatar
Matthias Braun committed
240
			DBG((dbg, LEVEL_5, "   not live in\n"));
241
242
			continue;
		}
243

Matthias Braun's avatar
Matthias Braun committed
244
		const be_use_t *use = get_or_set_use_block(env, succ_block, def);
Matthias Braun's avatar
Matthias Braun committed
245
246
		DBG((dbg, LEVEL_5, "Found %u (loopdepth %u) (we're in block %+F)\n",
		     use->next_use, use->outermost_loop, block));
247
		if (USES_IS_INFINITE(use->next_use)) {
Matthias Braun's avatar
Matthias Braun committed
248
			if (use->outermost_loop == UNKNOWN_OUTERMOST_LOOP) {
Matthias Braun's avatar
Matthias Braun committed
249
				found_visited = true;
250
			}
251
252
			continue;
		}
253

Matthias Braun's avatar
Matthias Braun committed
254
255
		found_use = true;
		unsigned use_dist = use->next_use;
256

Matthias Braun's avatar
Matthias Braun committed
257
		ir_loop *succ_loop = get_irn_loop(succ_block);
258
		if (get_loop_depth(succ_loop) < loopdepth) {
259
			unsigned factor = (loopdepth - get_loop_depth(succ_loop)) * 5000;
Matthias Braun's avatar
Matthias Braun committed
260
			DBG((dbg, LEVEL_5,
Matthias Braun's avatar
Matthias Braun committed
261
262
			     "Increase usestep because of loop out edge %d -> %d (%u)\n",
			     factor));
263
264
			// TODO we should use the number of nodes in the loop or so...
			use_dist += factor;
Sebastian Hack's avatar
Sebastian Hack committed
265
		}
266

267
		if (use_dist < next_use) {
268
			next_use       = use_dist;
269
			outermost_loop = use->outermost_loop;
270
			result.before  = use->node;
271
		}
Daniel Grund's avatar
Daniel Grund committed
272
	}
Sebastian Hack's avatar
Sebastian Hack committed
273

274
	if (loopdepth < outermost_loop)
275
		outermost_loop = loopdepth;
Sebastian Hack's avatar
Sebastian Hack committed
276

277
	result.time           = next_use + step;
278
	result.outermost_loop = outermost_loop;
Sebastian Hack's avatar
Sebastian Hack committed
279

280
	if (!found_use && found_visited) {
281
282
		// the current result is correct for the current search, but isn't
		// generally correct, so mark it
Matthias Braun's avatar
Matthias Braun committed
283
		result.outermost_loop = UNKNOWN_OUTERMOST_LOOP;
284
	}
Matthias Braun's avatar
Matthias Braun committed
285
286
	DBG((dbg, LEVEL_5, "Result: %d (outerloop: %u)\n", result.time,
	     result.outermost_loop));
287
	return result;
Sebastian Hack's avatar
Sebastian Hack committed
288
289
}

290
be_next_use_t be_get_next_use(be_uses_t *env, ir_node *from,
Matthias Braun's avatar
Matthias Braun committed
291
                              const ir_node *def, bool skip_from_uses)
292
{
293
294
	++env->visited_counter;
	return get_next_use(env, from, def, skip_from_uses);
295
296
}

297
298
299
300
301
302
303
/**
 * Pre-block walker, set the step number for every scheduled node
 * in increasing order.
 *
 * After this, two scheduled nodes can be easily compared for the
 * "scheduled earlier in block" property.
 */
304
static void set_sched_step_walker(ir_node *block, void *data)
305
{
Matthias Braun's avatar
Matthias Braun committed
306
	(void)data;
Matthias Braun's avatar
Matthias Braun committed
307
	unsigned step = 0;
308
309

	sched_foreach(block, node) {
310
		set_step(node, step);
311
		if (is_Phi(node))
312
313
314
315
316
			continue;
		++step;
	}
}

317
be_uses_t *be_begin_uses(ir_graph *irg, const be_lv_t *lv)
Sebastian Hack's avatar
Sebastian Hack committed
318
{
Matthias Braun's avatar
Matthias Braun committed
319
320
	FIRM_DBG_REGISTER(dbg, "firm.be.uses");

321
	assure_edges(irg);
Sebastian Hack's avatar
Sebastian Hack committed
322

323
324
325
	/* precalculate sched steps */
	irg_block_walk_graph(irg, set_sched_step_walker, NULL, NULL);

Matthias Braun's avatar
Matthias Braun committed
326
	be_uses_t *env = XMALLOCZ(be_uses_t);
327
	env->uses = new_set(cmp_use, 512);
Matthias Braun's avatar
Matthias Braun committed
328
	env->lv   = lv;
Sebastian Hack's avatar
Sebastian Hack committed
329

330
	return env;
Sebastian Hack's avatar
Sebastian Hack committed
331
332
}

333
void be_end_uses(be_uses_t *env)
Sebastian Hack's avatar
Sebastian Hack committed
334
{
335
336
	del_set(env->uses);
	free(env);
Sebastian Hack's avatar
Sebastian Hack committed
337
}