beuses.c 9.05 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
28
29

#include "be_t.h"
#include "beutil.h"
#include "belive_t.h"
30
#include "benode.h"
31
#include "besched.h"
Sebastian Hack's avatar
Sebastian Hack committed
32
#include "beirgmod.h"
33
#include "bearch.h"
Christian Würdig's avatar
Christian Würdig committed
34
#include "beuses.h"
Sebastian Hack's avatar
Sebastian Hack committed
35

Matthias Braun's avatar
Matthias Braun committed
36
37
#define UNKNOWN_OUTERMOST_LOOP  ((unsigned)-1)

Matthias Braun's avatar
Matthias Braun committed
38
39
DEBUG_ONLY(static firm_dbg_module_t *dbg;)

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

48
49
50
/**
 * The "uses" environment.
 */
51
struct be_uses_t {
Matthias Braun's avatar
Matthias Braun committed
52
53
54
55
	set           *uses; /**< cache: contains all computed uses so far. */
	ir_graph      *irg;  /**< the graph for this environment. */
	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
56
57
};

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

69
static be_next_use_t get_next_use(be_uses_t *env, ir_node *from,
Matthias Braun's avatar
Matthias Braun committed
70
								  const ir_node *def, bool skip_from_uses);
71

72
73
74
75
76
77
78
79
/**
 * 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
 */
80
static const be_use_t *get_or_set_use_block(be_uses_t *env,
81
82
                                            const ir_node *block,
                                            const ir_node *def)
Sebastian Hack's avatar
Sebastian Hack committed
83
{
84
85
	be_use_t temp;
	temp.block = block;
Matthias Braun's avatar
Matthias Braun committed
86
87
	temp.node  = def;

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

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

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

112
	return result;
Sebastian Hack's avatar
Sebastian Hack committed
113
114
}

115
116
117
118
119
120
121
122
123
124
/**
 * 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
125
static bool be_is_phi_argument(const ir_node *block, const ir_node *def)
126
{
127
	if (get_irn_n_edges_kind(block, EDGE_KIND_BLOCK) < 1)
Matthias Braun's avatar
Matthias Braun committed
128
		return false;
129

130
131
	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);
132
	if (get_Block_n_cfgpreds(succ_block) <= 1) {
133
		/* no Phis in the successor */
Matthias Braun's avatar
Matthias Braun committed
134
		return false;
135
	}
136

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

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

Matthias Braun's avatar
Matthias Braun committed
150
	return false;
151
152
}

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

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

169
170
171
172
173
174
175
176
/**
 * 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
 */
177
static be_next_use_t get_next_use(be_uses_t *env, ir_node *from,
Matthias Braun's avatar
Matthias Braun committed
178
								  const ir_node *def, bool skip_from_uses)
Sebastian Hack's avatar
Sebastian Hack committed
179
{
180
	if (skip_from_uses) {
181
182
183
		from = sched_next(from);
	}

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

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

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

Matthias Braun's avatar
Matthias Braun committed
214
215
	ir_node *node = sched_last(block);
	unsigned step = get_step(node) + 1 + timestep + skip_from_uses;
216

217
	if (be_is_phi_argument(block, def)) {
218
219
220
		// 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)
221
222

		be_next_use_t result;
223
		result.time           = step;
224
		result.outermost_loop = get_loop_depth(get_irn_loop(block));
225
		result.before         = block;
226
		return result;
227
	}
228

229
	be_next_use_t result;
Matthias Braun's avatar
Matthias Braun committed
230
	result.before = NULL;
Matthias Braun's avatar
Matthias Braun committed
231
232
233
234
235
236
	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;
237
238
	foreach_block_succ(block, edge) {
		const ir_node *succ_block = get_edge_src_irn(edge);
Matthias Braun's avatar
Matthias Braun committed
239
240
		DBG((dbg, LEVEL_5, "Checking succ of block %+F: %+F (for use of %+F)\n",
		     block, succ_block, def));
241
		if (!be_is_live_in(env->lv, succ_block, def)) {
242
			//next_use = USES_INFINITY;
Matthias Braun's avatar
Matthias Braun committed
243
			DBG((dbg, LEVEL_5, "   not live in\n"));
244
245
			continue;
		}
246

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

Matthias Braun's avatar
Matthias Braun committed
257
258
		found_use = true;
		unsigned use_dist = use->next_use;
259

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

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

277
	if (loopdepth < outermost_loop)
278
		outermost_loop = loopdepth;
Sebastian Hack's avatar
Sebastian Hack committed
279

280
	result.time           = next_use + step;
281
	result.outermost_loop = outermost_loop;
Sebastian Hack's avatar
Sebastian Hack committed
282

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

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

300
301
302
303
304
305
306
/**
 * 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.
 */
307
static void set_sched_step_walker(ir_node *block, void *data)
308
{
Matthias Braun's avatar
Matthias Braun committed
309
	(void)data;
Matthias Braun's avatar
Matthias Braun committed
310
	unsigned step = 0;
311
312

	sched_foreach(block, node) {
313
		set_step(node, step);
314
		if (is_Phi(node))
315
316
317
318
319
			continue;
		++step;
	}
}

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

324
	assure_edges(irg);
Sebastian Hack's avatar
Sebastian Hack committed
325

326
327
328
	/* precalculate sched steps */
	irg_block_walk_graph(irg, set_sched_step_walker, NULL, NULL);

Matthias Braun's avatar
Matthias Braun committed
329
	be_uses_t *env = XMALLOCZ(be_uses_t);
330
	env->uses = new_set(cmp_use, 512);
Matthias Braun's avatar
Matthias Braun committed
331
332
	env->irg  = irg;
	env->lv   = lv;
Sebastian Hack's avatar
Sebastian Hack committed
333

334
	return env;
Sebastian Hack's avatar
Sebastian Hack committed
335
336
}

337
void be_end_uses(be_uses_t *env)
Sebastian Hack's avatar
Sebastian Hack committed
338
{
339
340
	del_set(env->uses);
	free(env);
Sebastian Hack's avatar
Sebastian Hack committed
341
}