beuses.c 9.06 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
	ir_node *const succ_block = get_first_block_succ(block);
	if (get_Block_n_cfgpreds(succ_block) <= 1) {
132
		/* no Phis in the successor */
Matthias Braun's avatar
Matthias Braun committed
133
		return false;
134
	}
135

136
	/* find the index of block in its successor */
137
138
	int const i = get_Block_cfgpred_pos(succ_block, block);
	assert(i >= 0);
139

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

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

Matthias Braun's avatar
Matthias Braun committed
152
	return false;
153
154
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

279
	if (loopdepth < outermost_loop)
280
		outermost_loop = loopdepth;
Sebastian Hack's avatar
Sebastian Hack committed
281

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

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

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

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

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

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

326
	assure_edges(irg);
Sebastian Hack's avatar
Sebastian Hack committed
327

328
329
330
	/* precalculate sched steps */
	irg_block_walk_graph(irg, set_sched_step_walker, NULL, NULL);

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

336
	return env;
Sebastian Hack's avatar
Sebastian Hack committed
337
338
}

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