beschednormal.c 9.81 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
12
13
 */

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

#include <stdlib.h>

14
#include "besched.h"
15
16
17
#include "belistsched.h"
#include "belive_t.h"
#include "beutil.h"
18
#include "heights.h"
19
#include "irgwalk.h"
20
#include "benode.h"
21
#include "bemodule.h"
22
#include "util.h"
23
#include "array_t.h"
24
25

// XXX there is no one time init for schedulers
26
//#define NORMAL_DBG
Matthias Braun's avatar
Matthias Braun committed
27
#include "irprintf.h"
28

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

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


42
static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set)
43
{
44
	instance_t* inst = (instance_t*)block_env;
45
46
47
	ir_node*    irn;
	ir_node*    next;
	ir_node*    last = NULL;
48

49
	for (irn = inst->curr_list; irn != NULL; last = irn, irn = next) {
50
		next = (ir_node*)get_irn_link(irn);
51
		if (ir_nodeset_contains(ready_set, irn)) {
52
53
54
#if defined NORMAL_DBG
			ir_fprintf(stderr, "scheduling %+F\n", irn);
#endif
55
56
57
58
			if (last == NULL)
				inst->curr_list = next;
			else
				set_irn_link(last, next);
59
60
61
62
			return irn;
		}
	}

63
	return ir_nodeset_first(ready_set);
64
65
66
67
68
69
70
71
72
73
}


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

static int cost_cmp(const void* a, const void* b)
{
74
75
	const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
76
	int ret = b1->cost - a1->cost;
77
78
	if (ret == 0)
		ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
79
#if defined NORMAL_DBG
80
	ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
81
82
#endif
	return ret;
83
84
85
86
87
88
89
90
}


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

Michael Beck's avatar
Michael Beck committed
91
92
93
#define get_irn_fc(irn)     ((flag_and_cost*)get_irn_link(irn))
#define set_irn_fc(irn, fc) set_irn_link(irn, fc)

94
95
96
97

static int count_result(const ir_node* irn)
{
	const ir_mode* mode = get_irn_mode(irn);
98
99
100
101

	if (mode == mode_M || mode == mode_X)
		return 0;

102
103
104
	if (mode == mode_T)
		return 1;

105
106
	arch_register_req_t const *const req = arch_get_irn_register_req(irn);
	if (arch_register_req_is(req, ignore))
107
108
109
		return 0;

	return 1;
110
111
112
}


Christoph Mallon's avatar
Christoph Mallon committed
113
114
115
/* TODO high cost for store trees
 */

Michael Beck's avatar
Michael Beck committed
116
static int normal_tree_cost(ir_node* irn, instance_t *inst)
117
{
Michael Beck's avatar
Michael Beck committed
118
119
	flag_and_cost* fc;
	int            arity;
120
	ir_node*       last;
121
122
	int            n_res;
	int            cost;
Christoph Mallon's avatar
Christoph Mallon committed
123
124
	int            n_op_res = 0;
	int            i;
125

126
127
128
	if (be_is_Keep(irn))
		return 0;

129
	if (is_Proj(irn)) {
Michael Beck's avatar
Michael Beck committed
130
		return normal_tree_cost(get_Proj_pred(irn), inst);
131
132
	}

Michael Beck's avatar
Michael Beck committed
133
134
135
	arity = get_irn_arity(irn);
	fc    = get_irn_fc(irn);

136
137
	if (fc == NULL) {
		irn_cost_pair* costs;
Michael Beck's avatar
Michael Beck committed
138
		ir_node*       block = get_nodes_block(irn);
139

140
		fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
141
142
143
144
145
146
		fc->no_root = 0;
		costs = fc->costs;

		for (i = 0; i < arity; ++i) {
			ir_node* pred = get_irn_n(irn, i);

147
			if (is_Phi(irn) || get_irn_mode(pred) == mode_M) {
148
149
150
151
				cost = 0;
			} else if (get_nodes_block(pred) != block) {
				cost = 1;
			} else {
Michael Beck's avatar
Michael Beck committed
152
				flag_and_cost* pred_fc;
153
				ir_node*       real_pred;
Michael Beck's avatar
Michael Beck committed
154

Michael Beck's avatar
Michael Beck committed
155
				cost = normal_tree_cost(pred, inst);
156
				if (!arch_irn_is_ignore(pred)) {
157
					real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
Michael Beck's avatar
Michael Beck committed
158
					pred_fc = get_irn_fc(real_pred);
159
					pred_fc->no_root = 1;
160
#if defined NORMAL_DBG
161
					ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
162
#endif
163
				}
164
165
166
167
168
169
170
171
172
173
			}

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

		qsort(costs, arity, sizeof(*costs), cost_cmp);
		set_irn_link(irn, fc);
	}

Christoph Mallon's avatar
Christoph Mallon committed
174
	cost = 0;
175
	last = 0;
Christoph Mallon's avatar
Christoph Mallon committed
176
	for (i = 0; i < arity; ++i) {
177
		ir_node* op = fc->costs[i].irn;
178
179
180
181
182
183
		ir_mode* mode;
		if (op == last)
			continue;
		mode = get_irn_mode(op);
		if (mode == mode_M)
			continue;
184
		if (arch_irn_is_ignore(op))
185
			continue;
Christoph Mallon's avatar
Christoph Mallon committed
186
		cost = MAX(fc->costs[i].cost + n_op_res, cost);
187
		last = op;
Christoph Mallon's avatar
Christoph Mallon committed
188
		++n_op_res;
189
	}
Christoph Mallon's avatar
Christoph Mallon committed
190
191
	n_res = count_result(irn);
	cost = MAX(n_res, cost);
192
193
194
195
196
197
198
199
200
201
202

#if defined NORMAL_DBG
	ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
#endif

	return cost;
}


static void normal_cost_walker(ir_node* irn, void* env)
{
203
	instance_t *inst = (instance_t*)env;
204
205
206
207

#if defined NORMAL_DBG
	ir_fprintf(stderr, "cost walking node %+F\n", irn);
#endif
208
209
210
211
212
	if (is_Block(irn)) {
		ir_node **const roots = NEW_ARR_F(ir_node*, 0);
		set_irn_link(irn, roots);
		return;
	}
213
	if (!must_be_scheduled(irn)) return;
Michael Beck's avatar
Michael Beck committed
214
	normal_tree_cost(irn, inst);
215
216
217
218
219
}


static void collect_roots(ir_node* irn, void* env)
{
220
	int is_root;
221
222
223

	(void)env;

224
	if (!must_be_scheduled(irn)) return;
225

Michael Beck's avatar
Michael Beck committed
226
	is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
227
228

#if defined NORMAL_DBG
229
	ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
230
231
#endif

232
	if (is_root) {
233
		ir_node* block = get_nodes_block(irn);
234
		ir_node** roots = (ir_node**)get_irn_link(block);
235
236
237
238
239
240
241
242
		ARR_APP1(ir_node*, roots, irn);
		set_irn_link(block, roots);
	}
}


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

245
	if (!is_Phi(irn) && !be_is_Keep(irn)) {
Michael Beck's avatar
Michael Beck committed
246
247
248
249
250
251
		ir_node*       block = get_nodes_block(irn);
		int            arity = get_irn_arity(irn);
		flag_and_cost* fc    = get_irn_fc(irn);
		irn_cost_pair* irns  = fc->costs;
		int            i;

252
		for (i = 0; i < arity; ++i) {
Christoph Mallon's avatar
Christoph Mallon committed
253
			ir_node* pred = irns[i].irn;
254
			if (get_nodes_block(pred) != block) continue;
Christoph Mallon's avatar
Christoph Mallon committed
255
			if (get_irn_mode(pred) == mode_M) continue;
256
			if (is_Proj(pred)) pred = get_Proj_pred(pred);
257
258
259
260
261
262
263
264
265
			sched = sched_node(sched, pred);
		}
	}

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


266
267
static int root_cmp(const void* a, const void* b)
{
268
269
	const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
270
	int ret;
271
	if (is_irn_forking(a1->irn) && !is_irn_forking(b1->irn)) {
272
		ret = 1;
273
	} else if (is_irn_forking(b1->irn) && !is_irn_forking(a1->irn)) {
274
275
276
277
278
279
		ret = -1;
	} else {
		ret = b1->cost - a1->cost;
		if (ret == 0) {
			/* place live-out nodes later */
			ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
280
281
282
283
			if (ret == 0) {
				/* compare node idx */
				ret = get_irn_idx(a1->irn) - get_irn_idx(b1->irn);
			}
284
285
		}
	}
286
#if defined NORMAL_DBG
287
	ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
288
289
290
291
292
#endif
	return ret;
}


293
294
static void normal_sched_block(ir_node* block, void* env)
{
295
296
	ir_node**      roots = (ir_node**)get_irn_link(block);
	ir_heights_t*  heights = (ir_heights_t*)env;
297
298
299
300
301
302
303
304
	irn_cost_pair* root_costs;
	int i;
	ir_node**      sched;

#if defined NORMAL_DBG
	ir_fprintf(stderr, "sched walking block %+F\n", block);
#endif

305
306
	int const root_count = ARR_LEN(roots);
	if (root_count == 0) {
307
308
309
310
311
312
313
314
315
#if defined NORMAL_DBG
		fprintf(stderr, "has no roots\n");
#endif
		return;
	}

	NEW_ARR_A(irn_cost_pair, root_costs, root_count);
	for (i = 0; i < root_count; ++i) {
		root_costs[i].irn  = roots[i];
316
317
318
319
		root_costs[i].cost = get_irn_height(heights, roots[i]);
#if defined NORMAL_DBG
		ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
#endif
320
	}
321
	qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
322
323
324
325
326
327
328
329
330
331
332
333
#if defined NORMAL_DBG
	{
		int n = root_count;
		int i;

		ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
		for (i = 0; i < n; ++i) {
			ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
		}
		fprintf(stderr, "\n");
	}
#endif
334
335
336
337

	sched = NEW_ARR_F(ir_node*, 0);
	for (i = 0; i < root_count; ++i) {
		ir_node* irn = root_costs[i].irn;
338
		assert(must_be_scheduled(irn));
339
340
		sched = sched_node(sched, irn);
	}
Michael Beck's avatar
Michael Beck committed
341
	set_irn_link(block, sched);
342
	DEL_ARR_F(roots);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358

#if defined NORMAL_DBG
	{
		int n = ARR_LEN(sched);
		int i;

		ir_fprintf(stderr, "Scheduling of %+F:\n", block);
		for (i = 0; i < n; ++i) {
			ir_fprintf(stderr, "  %+F\n", sched[i]);
		}
		fprintf(stderr, "\n");
	}
#endif
}


359
static void *normal_init_graph(ir_graph *irg)
360
{
361
362
	instance_t   *inst = XMALLOC(instance_t);
	ir_heights_t *heights;
363
364
365

	be_clear_links(irg);

Michael Beck's avatar
Michael Beck committed
366
367
	obstack_init(&inst->obst);
	inst->irg         = irg;
368
369

	heights = heights_new(irg);
370

Michael Beck's avatar
Michael Beck committed
371
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
372
	irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
373
	irg_walk_graph(irg, collect_roots, NULL, NULL);
374
	inc_irg_visited(irg);
Michael Beck's avatar
Michael Beck committed
375
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
376
	irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
Michael Beck's avatar
Michael Beck committed
377
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
378

379
380
381
382
	heights_free(heights);

	return inst;
}
383

384
385
static void *normal_init_block(void *graph_env, ir_node *block)
{
386
387
	instance_t* inst  = (instance_t*)graph_env;
	ir_node**   sched = (ir_node**)get_irn_link(block);
388
389
390
391
392
393
394
	ir_node*    first = NULL;
	int         i;

	/* turn into a list, so we can easily remove nodes.
	   The link field is used anyway. */
	for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
		ir_node* irn = sched[i];
395
		if (!is_cfop(irn)) {
396
397
398
399
400
401
402
403
404
			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
405
	return inst;
406
407
}

408
static void normal_finish_graph(void *env)
Michael Beck's avatar
Michael Beck committed
409
{
410
	instance_t *inst = (instance_t*)env;
Michael Beck's avatar
Michael Beck committed
411
412
413
414

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

418
419
420
421
422
423
424
425
426
427
428
429
430
431
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
432
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
433
434
435
436
void be_init_sched_normal(void)
{
	be_register_scheduler("normal", sched_normal);
}