beschednormal.c 9.51 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
9
10
11
 */

/**
 * @brief   Use the strong normal form theorem (though it does not hold)
 * @author  Christoph Mallon
 */
#include <stdlib.h>

12
#include "besched.h"
13
#include "belistsched.h"
Matthias Braun's avatar
Matthias Braun committed
14
#include "belive.h"
15
#include "beutil.h"
16
#include "heights.h"
17
#include "irgwalk.h"
18
#include "benode.h"
19
#include "bemodule.h"
20
#include "util.h"
21
#include "array.h"
22

Matthias Braun's avatar
Matthias Braun committed
23
#include "irprintf.h"
24
#include "irtools.h"
25

Matthias Braun's avatar
Matthias Braun committed
26
27
//#define NORMAL_DBG

Michael Beck's avatar
Michael Beck committed
28
29
/** An instance of the normal scheduler. */
typedef struct instance_t {
Matthias Braun's avatar
Matthias Braun committed
30
	ir_graph      *irg;          /**< the IR graph of this instance */
Michael Beck's avatar
Michael Beck committed
31
	struct obstack obst;         /**< obstack for temporary data */
Matthias Braun's avatar
Matthias Braun committed
32
	ir_node       *curr_list;    /**< current block schedule list */
Michael Beck's avatar
Michael Beck committed
33
} instance_t;
34

35
36
static int must_be_scheduled(const ir_node* const irn)
{
37
	return !is_Proj(irn) && !arch_irn_is(irn, not_scheduled);
38
39
40
}


41
static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set)
42
{
Matthias Braun's avatar
Matthias Braun committed
43
44
45
	instance_t *inst = (instance_t*)block_env;
	for (ir_node *irn = inst->curr_list, *last = NULL, *next; irn != NULL;
	     last = irn, irn = next) {
46
		next = (ir_node*)get_irn_link(irn);
47
		if (ir_nodeset_contains(ready_set, irn)) {
Matthias Braun's avatar
Matthias Braun committed
48
#ifdef NORMAL_DBG
49
50
			ir_fprintf(stderr, "scheduling %+F\n", irn);
#endif
51
52
53
54
			if (last == NULL)
				inst->curr_list = next;
			else
				set_irn_link(last, next);
55
56
57
58
			return irn;
		}
	}

59
	return ir_nodeset_first(ready_set);
60
61
62
63
}


typedef struct irn_cost_pair {
Matthias Braun's avatar
Matthias Braun committed
64
65
	ir_node *irn;
	unsigned cost;
66
67
68
69
} irn_cost_pair;

static int cost_cmp(const void* a, const void* b)
{
70
71
	const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
Matthias Braun's avatar
Matthias Braun committed
72
	int ret = (int)b1->cost - (int)a1->cost;
73
74
	if (ret == 0)
		ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
Matthias Braun's avatar
Matthias Braun committed
75
76
77
#ifdef NORMAL_DBG
	ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn,
	           ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
78
79
#endif
	return ret;
80
81
82
83
}


typedef struct flag_and_cost {
Matthias Braun's avatar
Matthias Braun committed
84
	bool          no_root;
85
86
87
	irn_cost_pair costs[];
} flag_and_cost;

Michael Beck's avatar
Michael Beck committed
88
89
90
#define get_irn_fc(irn)     ((flag_and_cost*)get_irn_link(irn))
#define set_irn_fc(irn, fc) set_irn_link(irn, fc)

Matthias Braun's avatar
Matthias Braun committed
91
static unsigned count_result(const ir_node *irn)
92
{
Matthias Braun's avatar
Matthias Braun committed
93
	const ir_mode *mode = get_irn_mode(irn);
94
95
	if (mode == mode_M || mode == mode_X)
		return 0;
96
97
98
	if (mode == mode_T)
		return 1;

99
100
	arch_register_req_t const *const req = arch_get_irn_register_req(irn);
	if (arch_register_req_is(req, ignore))
101
102
103
		return 0;

	return 1;
104
105
}

Christoph Mallon's avatar
Christoph Mallon committed
106
107
108
/* TODO high cost for store trees
 */

Matthias Braun's avatar
Matthias Braun committed
109
static unsigned normal_tree_cost(ir_node *irn, instance_t *inst)
110
{
111
112
	if (be_is_Keep(irn))
		return 0;
Matthias Braun's avatar
Matthias Braun committed
113
	if (is_Proj(irn))
Michael Beck's avatar
Michael Beck committed
114
		return normal_tree_cost(get_Proj_pred(irn), inst);
Michael Beck's avatar
Michael Beck committed
115

Matthias Braun's avatar
Matthias Braun committed
116
117
	int            arity = get_irn_arity(irn);
	flag_and_cost *fc    = get_irn_fc(irn);
118
	if (fc == NULL) {
Matthias Braun's avatar
Matthias Braun committed
119
		ir_node *block = get_nodes_block(irn);
120

121
		fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
Matthias Braun's avatar
Matthias Braun committed
122
123
		fc->no_root = false;
		irn_cost_pair *costs = fc->costs;
124

125
		foreach_irn_in(irn, i, pred) {
Matthias Braun's avatar
Matthias Braun committed
126
			unsigned cost;
127
			if (is_Phi(irn) || get_irn_mode(pred) == mode_M) {
128
129
130
131
				cost = 0;
			} else if (get_nodes_block(pred) != block) {
				cost = 1;
			} else {
Michael Beck's avatar
Michael Beck committed
132
				cost = normal_tree_cost(pred, inst);
133
				if (!arch_irn_is_ignore(pred)) {
Matthias Braun's avatar
Matthias Braun committed
134
135
136
137
138
139
140
					ir_node       *real_pred = is_Proj(pred)
					                         ? get_Proj_pred(pred) : pred;
					flag_and_cost *pred_fc = get_irn_fc(real_pred);
					pred_fc->no_root = true;
#ifdef NORMAL_DBG
					ir_fprintf(stderr, "%+F says that %+F is no root\n", irn,
					           real_pred);
141
#endif
142
				}
143
144
145
146
147
148
			}

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

149
		QSORT(costs, arity, cost_cmp);
150
151
152
		set_irn_link(irn, fc);
	}

Matthias Braun's avatar
Matthias Braun committed
153
154
155
	unsigned cost     = 0;
	unsigned n_op_res = 0;
	ir_node *last     = 0;
156
	for (int i = 0; i < arity; ++i) {
Matthias Braun's avatar
Matthias Braun committed
157
		ir_node *op = fc->costs[i].irn;
158
159
		if (op == last)
			continue;
Matthias Braun's avatar
Matthias Braun committed
160
		ir_mode *mode = get_irn_mode(op);
161
162
		if (mode == mode_M)
			continue;
163
		if (arch_irn_is_ignore(op))
164
			continue;
Christoph Mallon's avatar
Christoph Mallon committed
165
		cost = MAX(fc->costs[i].cost + n_op_res, cost);
166
		last = op;
Christoph Mallon's avatar
Christoph Mallon committed
167
		++n_op_res;
168
	}
Matthias Braun's avatar
Matthias Braun committed
169
	unsigned n_res = count_result(irn);
Christoph Mallon's avatar
Christoph Mallon committed
170
	cost = MAX(n_res, cost);
171

Matthias Braun's avatar
Matthias Braun committed
172
173
#ifdef NORMAL_DBG
	ir_fprintf(stderr, "reguse of %+F is %u\n", irn, cost);
174
175
176
177
178
#endif
	return cost;
}


Matthias Braun's avatar
Matthias Braun committed
179
static void normal_cost_walker(ir_node  *irn, void *env)
180
{
181
	instance_t *inst = (instance_t*)env;
182

Matthias Braun's avatar
Matthias Braun committed
183
#ifdef NORMAL_DBG
184
185
	ir_fprintf(stderr, "cost walking node %+F\n", irn);
#endif
186
187
188
189
190
	if (is_Block(irn)) {
		ir_node **const roots = NEW_ARR_F(ir_node*, 0);
		set_irn_link(irn, roots);
		return;
	}
191
	if (!must_be_scheduled(irn)) return;
Michael Beck's avatar
Michael Beck committed
192
	normal_tree_cost(irn, inst);
193
194
195
}


Matthias Braun's avatar
Matthias Braun committed
196
static void collect_roots(ir_node *irn, void *env)
197
198
{
	(void)env;
Matthias Braun's avatar
Matthias Braun committed
199
200
	if (!must_be_scheduled(irn))
		return;
201

Matthias Braun's avatar
Matthias Braun committed
202
203
	bool is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
#ifdef NORMAL_DBG
204
	ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
205
#endif
206
	if (is_root) {
Matthias Braun's avatar
Matthias Braun committed
207
208
		ir_node  *block = get_nodes_block(irn);
		ir_node **roots = (ir_node**)get_irn_link(block);
209
210
211
212
213
214
		ARR_APP1(ir_node*, roots, irn);
		set_irn_link(block, roots);
	}
}


Matthias Braun's avatar
Matthias Braun committed
215
static ir_node** sched_node(ir_node**sched, ir_node *irn)
216
{
Matthias Braun's avatar
Matthias Braun committed
217
218
	if (irn_visited_else_mark(irn))
		return sched;
219

220
	if (!is_Phi(irn) && !be_is_Keep(irn)) {
Matthias Braun's avatar
Matthias Braun committed
221
222
223
224
225
226
227
228
229
230
231
232
		ir_node       *block = get_nodes_block(irn);
		flag_and_cost *fc    = get_irn_fc(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);
233
234
235
236
237
238
239
240
241
			sched = sched_node(sched, pred);
		}
	}

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


242
243
static int root_cmp(const void* a, const void* b)
{
244
245
	const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
246
	int ret;
247
	if (is_irn_forking(a1->irn) && !is_irn_forking(b1->irn)) {
248
		ret = 1;
249
	} else if (is_irn_forking(b1->irn) && !is_irn_forking(a1->irn)) {
250
251
		ret = -1;
	} else {
Matthias Braun's avatar
Matthias Braun committed
252
		ret = (int)b1->cost - (int)a1->cost;
253
254
255
		if (ret == 0) {
			/* place live-out nodes later */
			ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
256
257
258
259
			if (ret == 0) {
				/* compare node idx */
				ret = get_irn_idx(a1->irn) - get_irn_idx(b1->irn);
			}
260
261
		}
	}
Matthias Braun's avatar
Matthias Braun committed
262
263
264
#ifdef NORMAL_DBG
	ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn,
	           ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
265
266
267
268
269
#endif
	return ret;
}


Matthias Braun's avatar
Matthias Braun committed
270
static void normal_sched_block(ir_node *block, void *env)
271
{
Matthias Braun's avatar
Matthias Braun committed
272
273
	ir_node     **roots   = (ir_node**)get_irn_link(block);
	ir_heights_t *heights = (ir_heights_t*)env;
274

Matthias Braun's avatar
Matthias Braun committed
275
#ifdef NORMAL_DBG
276
277
278
	ir_fprintf(stderr, "sched walking block %+F\n", block);
#endif

279
280
	int const root_count = ARR_LEN(roots);
	if (root_count == 0) {
Matthias Braun's avatar
Matthias Braun committed
281
#ifdef NORMAL_DBG
282
283
284
285
286
		fprintf(stderr, "has no roots\n");
#endif
		return;
	}

287
	irn_cost_pair *root_costs = ALLOCAN(irn_cost_pair, root_count);
Matthias Braun's avatar
Matthias Braun committed
288
	for (int i = 0; i < root_count; ++i) {
289
		root_costs[i].irn  = roots[i];
290
		root_costs[i].cost = get_irn_height(heights, roots[i]);
Matthias Braun's avatar
Matthias Braun committed
291
292
293
#ifdef NORMAL_DBG
		ir_fprintf(stderr, "height of %+F is %u\n", roots[i],
		           root_costs[i].cost);
294
#endif
295
	}
296
	QSORT(root_costs, root_count, root_cmp);
Matthias Braun's avatar
Matthias Braun committed
297
298
299
300
#ifdef NORMAL_DBG
	ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
	for (int i = 0, n = root_count; i < n; ++i) {
		ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
301
	}
Matthias Braun's avatar
Matthias Braun committed
302
	fprintf(stderr, "\n");
303
#endif
304

Matthias Braun's avatar
Matthias Braun committed
305
306
307
	ir_node **sched = NEW_ARR_F(ir_node*, 0);
	for (int i = 0; i < root_count; ++i) {
		ir_node *irn = root_costs[i].irn;
308
		assert(must_be_scheduled(irn));
309
310
		sched = sched_node(sched, irn);
	}
Michael Beck's avatar
Michael Beck committed
311
	set_irn_link(block, sched);
312
	DEL_ARR_F(roots);
313

Matthias Braun's avatar
Matthias Braun committed
314
315
316
317
#ifdef NORMAL_DBG
	ir_fprintf(stderr, "Scheduling of %+F:\n", block);
	for (int i = 0, n = ARR_LEN(sched); i < n; ++i) {
		ir_fprintf(stderr, "  %+F\n", sched[i]);
318
	}
Matthias Braun's avatar
Matthias Braun committed
319
	fprintf(stderr, "\n");
320
321
322
323
#endif
}


324
static void *normal_init_graph(ir_graph *irg)
325
{
326
	irg_walk_graph(irg, firm_clear_link, NULL, NULL);
327

Matthias Braun's avatar
Matthias Braun committed
328
	instance_t *inst = XMALLOC(instance_t);
Michael Beck's avatar
Michael Beck committed
329
	obstack_init(&inst->obst);
Matthias Braun's avatar
Matthias Braun committed
330
	inst->irg = irg;
331

Matthias Braun's avatar
Matthias Braun committed
332
	ir_heights_t *heights = heights_new(irg);
333

Michael Beck's avatar
Michael Beck committed
334
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
335
	irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
336
	irg_walk_graph(irg, collect_roots, NULL, NULL);
337
	inc_irg_visited(irg);
Michael Beck's avatar
Michael Beck committed
338
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
339
	irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
Michael Beck's avatar
Michael Beck committed
340
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
341

342
343
344
345
	heights_free(heights);

	return inst;
}
346

347
348
static void *normal_init_block(void *graph_env, ir_node *block)
{
Matthias Braun's avatar
Matthias Braun committed
349
350
351
	instance_t *inst  = (instance_t*)graph_env;
	ir_node   **sched = (ir_node**)get_irn_link(block);
	ir_node    *first = NULL;
352
353
354

	/* turn into a list, so we can easily remove nodes.
	   The link field is used anyway. */
Matthias Braun's avatar
Matthias Braun committed
355
356
	for (int i = ARR_LEN(sched); i-- > 0; ) {
		ir_node *irn = sched[i];
357
		if (!is_cfop(irn)) {
358
359
360
361
362
363
364
365
366
			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);
	inst->curr_list = first;
Michael Beck's avatar
Michael Beck committed
367
	return inst;
368
369
}

370
static void normal_finish_graph(void *env)
Michael Beck's avatar
Michael Beck committed
371
{
372
	instance_t *inst = (instance_t*)env;
Michael Beck's avatar
Michael Beck committed
373
374
375
376

	/* block uses the link field to store the schedule */
	ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
	obstack_free(&inst->obst, NULL);
377
	free(inst);
Michael Beck's avatar
Michael Beck committed
378
}
379

380
381
382
383
384
385
386
387
388
389
390
391
392
393
static void sched_normal(ir_graph *irg)
{
	static const list_sched_selector_t normal_selector = {
		normal_init_graph,
		normal_init_block,
		normal_select,
		NULL,              /* node_ready */
		NULL,              /* node_selected */
		NULL,              /* finish_block */
		normal_finish_graph
	};
	be_list_sched_graph(irg, &normal_selector);
}

Matthias Braun's avatar
Matthias Braun committed
394
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
395
396
397
398
void be_init_sched_normal(void)
{
	be_register_scheduler("normal", sched_normal);
}