summaryrefslogtreecommitdiffhomepage
path: root/ir/be/beschednormal.c
blob: 3d12c3d9d661cfaca2492b9865ed5909b1a545e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * This file is part of libFirm.
 * Copyright (C) 2012 University of Karlsruhe.
 */

/**
 * @brief   Schedule using the strong normal form theorem (though it does not
 *          hold)
 * @author  Christoph Mallon
 */
#include "array.h"
#include "belistsched.h"
#include "belive.h"
#include "bemodule.h"
#include "benode.h"
#include "besched.h"
#include "debug.h"
#include "heights.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irtools.h"
#include "util.h"
#include <stdlib.h>

DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

static struct obstack obst;
static ir_node       *curr_list;

typedef struct irn_cost_pair {
	ir_node *irn;
	unsigned cost;
} irn_cost_pair;

typedef struct flag_and_cost {
	bool          no_root;
	irn_cost_pair costs[];
} flag_and_cost;

static flag_and_cost *get_irn_flag_and_cost(const ir_node *node)
{
	return (flag_and_cost*)get_irn_link(node);
}

static void set_irn_flag_and_cost(ir_node *node, flag_and_cost *fc)
{
	set_irn_link(node, fc);
}

static ir_node *normal_select(ir_nodeset_t *ready_set)
{
	for (ir_node *irn = curr_list, *last = NULL, *next; irn != NULL;
	     last = irn, irn = next) {
		next = (ir_node*)get_irn_link(irn);
		if (ir_nodeset_contains(ready_set, irn)) {
			DB((dbg, LEVEL_1, "Scheduling %+F\n", irn));
			if (last == NULL)
				curr_list = next;
			else
				set_irn_link(last, next);
			return irn;
		}
	}

	return ir_nodeset_first(ready_set);
}

static int cost_cmp(const void *a, const void *b)
{
	const irn_cost_pair *const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair *const b1 = (const irn_cost_pair*)b;
	int ret = (int)b1->cost - (int)a1->cost;
	if (ret == 0)
		ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
	return ret;
}

static unsigned count_result(const ir_node *irn)
{
	const ir_mode *mode = get_irn_mode(irn);
	if (mode == mode_M || mode == mode_X)
		return 0;
	if (mode == mode_T)
		return 1;

	arch_register_req_t const *const req = arch_get_irn_register_req(irn);
	return req->ignore ? 0 : 1;
}

static unsigned normal_tree_cost(ir_node *irn)
{
	/* TODO: high cost for store trees */
	if (be_is_Keep(irn))
		return 0;
	if (is_Proj(irn))
		return normal_tree_cost(get_Proj_pred(irn));

	int            arity = get_irn_arity(irn);
	flag_and_cost *fc    = get_irn_flag_and_cost(irn);
	if (fc == NULL) {
		ir_node *block = get_nodes_block(irn);

		fc = OALLOCF(&obst, flag_and_cost, costs, arity);
		fc->no_root = false;
		irn_cost_pair *costs = fc->costs;

		foreach_irn_in(irn, i, pred) {
			unsigned cost;
			if (is_Phi(irn) || get_irn_mode(pred) == mode_M) {
				cost = 0;
			} else if (get_nodes_block(pred) != block) {
				cost = 1;
			} else {
				cost = normal_tree_cost(pred);
				if (!arch_irn_is_ignore(pred)) {
					ir_node       *real_pred = is_Proj(pred)
					                         ? get_Proj_pred(pred) : pred;
					flag_and_cost *pred_fc = get_irn_flag_and_cost(real_pred);
					pred_fc->no_root = true;
					DB((dbg, LEVEL_1, "%+F says that %+F is no root\n", irn,
					    real_pred));
				}
			}

			costs[i].irn  = pred;
			costs[i].cost = cost;
		}

		QSORT(costs, arity, cost_cmp);
		set_irn_flag_and_cost(irn, fc);
	}

	unsigned cost     = 0;
	unsigned n_op_res = 0;
	ir_node *last     = 0;
	for (int i = 0; i < arity; ++i) {
		ir_node *op = fc->costs[i].irn;
		if (op == last)
			continue;
		ir_mode *mode = get_irn_mode(op);
		if (mode == mode_M || arch_irn_is_ignore(op))
			continue;
		cost = MAX(fc->costs[i].cost + n_op_res, cost);
		last = op;
		++n_op_res;
	}
	unsigned n_res = count_result(irn);
	cost = MAX(n_res, cost);

	DB((dbg, LEVEL_1, "reguse of %+F is %u\n", irn, cost));
	return cost;
}

static void normal_cost_walker(ir_node *irn, void *data)
{
	(void)data;
	DB((dbg, LEVEL_1, "cost walking node %+F\n", irn));
	if (is_Block(irn)) {
		ir_node **const roots = NEW_ARR_F(ir_node*, 0);
		set_irn_link(irn, roots);
		return;
	}
	if (arch_is_irn_not_scheduled(irn))
		return;
	normal_tree_cost(irn);
}

static void collect_roots(ir_node *irn, void *env)
{
	(void)env;
	if (arch_is_irn_not_scheduled(irn))
		return;

	bool is_root = be_is_Keep(irn) || !get_irn_flag_and_cost(irn)->no_root;
	DB((dbg, LEVEL_1, "%+F is %sroot\n", irn, is_root ? "" : "no "));
	if (is_root) {
		ir_node  *block = get_nodes_block(irn);
		ir_node **roots = (ir_node**)get_irn_link(block);
		ARR_APP1(ir_node*, roots, irn);
		set_irn_link(block, roots);
	}
}

static ir_node **sched_node(ir_node **sched, ir_node *irn)
{
	if (irn_visited_else_mark(irn))
		return sched;

	if (!is_Phi(irn) && !be_is_Keep(irn)) {
		ir_node       *block = get_nodes_block(irn);
		flag_and_cost *fc    = get_irn_flag_and_cost(irn);
		irn_cost_pair *irns  = fc->costs;

		for (int i = 0, arity = get_irn_arity(irn); i < arity; ++i) {
			ir_node *pred = irns[i].irn;
			if (get_nodes_block(pred) != block)
				continue;
			if (get_irn_mode(pred) == mode_M)
				continue;
			if (is_Proj(pred))
				pred = get_Proj_pred(pred);
			sched = sched_node(sched, pred);
		}
	}

	ARR_APP1(ir_node*, sched, irn);
	return sched;
}

static int root_cmp(const void *a, const void *b)
{
	const irn_cost_pair *const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair *const b1 = (const irn_cost_pair*)b;
	int ret;
	if (is_cfop(a1->irn) && !is_cfop(b1->irn)) {
		ret = 1;
	} else if (is_cfop(b1->irn) && !is_cfop(a1->irn)) {
		ret = -1;
	} else {
		ret = (int)b1->cost - (int)a1->cost;
		if (ret == 0) {
			/* place live-out nodes later */
			ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
			/* compare node idx */
			if (ret == 0)
				ret = get_irn_idx(a1->irn) - get_irn_idx(b1->irn);
		}
	}
	DB((dbg, LEVEL_1, "root %+F %s %+F\n", a1->irn,
	    ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn));
	return ret;
}

static void normal_sched_block(ir_node *block, void *env)
{
	ir_node     **roots   = (ir_node**)get_irn_link(block);
	ir_heights_t *heights = (ir_heights_t*)env;

	DB((dbg, LEVEL_1, "sched walking block %+F\n", block));

	int const root_count = ARR_LEN(roots);
	if (root_count == 0) {
		DB((dbg, LEVEL_1, "has no roots\n"));
		return;
	}

	irn_cost_pair *root_costs = ALLOCAN(irn_cost_pair, root_count);
	for (int i = 0; i < root_count; ++i) {
		root_costs[i].irn  = roots[i];
		root_costs[i].cost = get_irn_height(heights, roots[i]);
		DB((dbg, LEVEL_1, "height of %+F is %u\n", roots[i],
		    root_costs[i].cost));
	}
	QSORT(root_costs, root_count, root_cmp);
#ifdef DEBUG_libfirm
	DB((dbg, LEVEL_1, "Root Scheduling of %+F:\n", block));
	for (int i = 0, n = root_count; i < n; ++i) {
		DB((dbg, LEVEL_1, "  %+F\n", root_costs[i].irn));
	}
	DB((dbg, LEVEL_1, "\n"));
#endif

	ir_node **sched = NEW_ARR_F(ir_node*, 0);
	for (int i = 0; i < root_count; ++i) {
		ir_node *irn = root_costs[i].irn;
		assert(!arch_is_irn_not_scheduled(irn));
		sched = sched_node(sched, irn);
	}
	set_irn_link(block, sched);
	DEL_ARR_F(roots);

#ifdef DEBUG_libfirm
	DB((dbg, LEVEL_1, "Scheduling of %+F:\n", block));
	for (int i = 0, n = ARR_LEN(sched); i < n; ++i) {
		DB((dbg, LEVEL_1, "  %+F\n", sched[i]));
	}
	DB((dbg, LEVEL_1, "\n"));
#endif
}

static void real_sched_block(ir_node *block, void *data)
{
	(void)data;
	ir_node **sched = (ir_node**)get_irn_link(block);
	ir_node  *first = NULL;

	/* turn into a list, so we can easily remove nodes. The link field is used
	 * anyway. */
	for (int i = ARR_LEN(sched); i-- > 0; ) {
		ir_node *irn = sched[i];
		set_irn_link(irn, first);
		first = irn;
	}
	/* note: we can free sched here, there should be no attempt to schedule
	 * a block twice */
	DEL_ARR_F(sched);
	set_irn_link(block, sched);
	curr_list = first;

	ir_nodeset_t *cands = be_list_sched_begin_block(block);
	while (ir_nodeset_size(cands) > 0) {
		ir_node *node = normal_select(cands);
		be_list_sched_schedule(node);
	}
	be_list_sched_end_block();
}

static void sched_normal(ir_graph *irg)
{
	/* block uses the link field to store the schedule */
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);

	obstack_init(&obst);

	irg_walk_graph(irg, firm_clear_link, NULL, NULL);
	irg_walk_graph(irg, normal_cost_walker,  NULL, NULL);
	irg_walk_graph(irg, collect_roots, NULL, NULL);
	ir_heights_t *heights = heights_new(irg);
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
	inc_irg_visited(irg);
	irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
	heights_free(heights);

	be_list_sched_begin(irg);
	irg_block_walk_graph(irg, real_sched_block, NULL, NULL);
	be_list_sched_finish();

	ir_free_resources(irg, IR_RESOURCE_IRN_LINK);
	obstack_free(&obst, NULL);
}

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
void be_init_sched_normal(void)
{
	be_register_scheduler("normal", sched_normal);
	FIRM_DBG_REGISTER(dbg, "firm.be.sched.normal");
}