beschednormal.c 10.5 KB
Newer Older
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @brief   Use the strong normal form theorem (though it does not hold)
 * @author  Christoph Mallon
Michael Beck's avatar
Michael Beck committed
23
 * @version $Id$
24
25
26
27
28
 */
#include "config.h"

#include <stdlib.h>

29
#include "besched.h"
30
31
32
#include "belistsched.h"
#include "belive_t.h"
#include "beutil.h"
33
#include "heights.h"
34
35
#include "irtools.h"
#include "irgwalk.h"
36
#include "benode.h"
37
#include "bemodule.h"
38
#include "array_t.h"
39
40

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

Michael Beck's avatar
Michael Beck committed
44
45
/** An instance of the normal scheduler. */
typedef struct instance_t {
Michael Beck's avatar
Michael Beck committed
46
47
	ir_graph*      irg;          /**< the IR graph of this instance */
	struct obstack obst;         /**< obstack for temporary data */
48
	ir_node*       curr_list;    /**< current block schedule list */
Michael Beck's avatar
Michael Beck committed
49
} instance_t;
50

51
52
53
54
55
56
static int must_be_scheduled(const ir_node* const irn)
{
	return !is_Proj(irn) && !is_Sync(irn);
}


57
static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set)
58
{
59
	instance_t* inst = (instance_t*)block_env;
60
61
62
	ir_node*    irn;
	ir_node*    next;
	ir_node*    last = NULL;
63
64
	ir_nodeset_iterator_t iter;

65
	for (irn = inst->curr_list; irn != NULL; last = irn, irn = next) {
66
		next = (ir_node*)get_irn_link(irn);
67
		if (ir_nodeset_contains(ready_set, irn)) {
68
69
70
#if defined NORMAL_DBG
			ir_fprintf(stderr, "scheduling %+F\n", irn);
#endif
71
72
73
74
			if (last == NULL)
				inst->curr_list = next;
			else
				set_irn_link(last, next);
75
76
77
78
			return irn;
		}
	}

79
80
	ir_nodeset_iterator_init(&iter, ready_set);
	irn = ir_nodeset_iterator_next(&iter);
81
82
83
84
85
86
87
88
89
90
91
	return irn;
}


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

static int cost_cmp(const void* a, const void* b)
{
92
93
	const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
94
	int ret = b1->cost - a1->cost;
95
96
	if (ret == 0)
		ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
97
#if defined NORMAL_DBG
98
	ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
99
100
#endif
	return ret;
101
102
103
104
105
106
107
108
}


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

Michael Beck's avatar
Michael Beck committed
109
110
111
#define get_irn_fc(irn)     ((flag_and_cost*)get_irn_link(irn))
#define set_irn_fc(irn, fc) set_irn_link(irn, fc)

112
113
114
115

static int count_result(const ir_node* irn)
{
	const ir_mode* mode = get_irn_mode(irn);
116
117
118
119

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

120
121
122
	if (mode == mode_T)
		return 1;

123
124
125
126
	if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
		return 0;

	return 1;
127
128
129
}


Christoph Mallon's avatar
Christoph Mallon committed
130
131
132
/* TODO high cost for store trees
 */

Michael Beck's avatar
Michael Beck committed
133
static int normal_tree_cost(ir_node* irn, instance_t *inst)
134
{
Michael Beck's avatar
Michael Beck committed
135
136
	flag_and_cost* fc;
	int            arity;
137
	ir_node*       last;
138
139
	int            n_res;
	int            cost;
Christoph Mallon's avatar
Christoph Mallon committed
140
141
	int            n_op_res = 0;
	int            i;
142

143
144
145
	if (be_is_Keep(irn))
		return 0;

146
	if (is_Proj(irn)) {
Michael Beck's avatar
Michael Beck committed
147
		return normal_tree_cost(get_Proj_pred(irn), inst);
148
149
	}

Michael Beck's avatar
Michael Beck committed
150
151
152
	arity = get_irn_arity(irn);
	fc    = get_irn_fc(irn);

153
154
155
	if (fc == NULL) {
		irn_cost_pair* costs;
		int            i;
Michael Beck's avatar
Michael Beck committed
156
		ir_node*       block = get_nodes_block(irn);
157

158
		fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
159
160
161
162
163
164
165
		fc->no_root = 0;
		costs = fc->costs;

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

166
			if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
167
168
169
170
				cost = 0;
			} else if (get_nodes_block(pred) != block) {
				cost = 1;
			} else {
Michael Beck's avatar
Michael Beck committed
171
				flag_and_cost* pred_fc;
172
				ir_node*       real_pred;
Michael Beck's avatar
Michael Beck committed
173

Michael Beck's avatar
Michael Beck committed
174
				cost = normal_tree_cost(pred, inst);
Christoph Mallon's avatar
Christoph Mallon committed
175
				if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
176
				if (!arch_irn_is_ignore(pred)) {
177
					real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
Michael Beck's avatar
Michael Beck committed
178
					pred_fc = get_irn_fc(real_pred);
179
					pred_fc->no_root = 1;
180
#if defined NORMAL_DBG
181
					ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
182
#endif
183
				}
184
185
186
187
188
189
190
191
192
193
			}

			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
194
	cost = 0;
195
	last = 0;
Christoph Mallon's avatar
Christoph Mallon committed
196
	for (i = 0; i < arity; ++i) {
197
		ir_node* op = fc->costs[i].irn;
198
199
200
201
202
203
204
205
		ir_mode* mode;
		if (op == last)
			continue;
		mode = get_irn_mode(op);
		if (mode == mode_M)
			continue;
		if (mode != mode_T && arch_irn_is_ignore(op))
			continue;
Christoph Mallon's avatar
Christoph Mallon committed
206
		cost = MAX(fc->costs[i].cost + n_op_res, cost);
207
		last = op;
Christoph Mallon's avatar
Christoph Mallon committed
208
		++n_op_res;
209
	}
Christoph Mallon's avatar
Christoph Mallon committed
210
211
	n_res = count_result(irn);
	cost = MAX(n_res, cost);
212
213
214
215
216
217
218
219
220
221
222

#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)
{
223
	instance_t *inst = (instance_t*)env;
224
225
226
227
228

#if defined NORMAL_DBG
	ir_fprintf(stderr, "cost walking node %+F\n", irn);
#endif
	if (is_Block(irn)) return;
229
	if (!must_be_scheduled(irn)) return;
Michael Beck's avatar
Michael Beck committed
230
	normal_tree_cost(irn, inst);
231
232
233
234
235
}


static void collect_roots(ir_node* irn, void* env)
{
236
	int is_root;
237
238
239
240

	(void)env;

	if (is_Block(irn)) return;
241
	if (!must_be_scheduled(irn)) return;
242

Michael Beck's avatar
Michael Beck committed
243
	is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
244
245

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

249
	if (is_root) {
250
		ir_node* block = get_nodes_block(irn);
251
		ir_node** roots = (ir_node**)get_irn_link(block);
252
253
254
255
256
257
258
259
260
261
262
		if (roots == NULL) {
			roots = NEW_ARR_F(ir_node*, 0);
		}
		ARR_APP1(ir_node*, roots, irn);
		set_irn_link(block, roots);
	}
}


static ir_node** sched_node(ir_node** sched, ir_node* irn)
{
263
264
	if (irn_visited_else_mark(irn)) return sched;
	if (is_End(irn))                return sched;
265

266
	if (!is_Phi(irn) && !be_is_Keep(irn)) {
Michael Beck's avatar
Michael Beck committed
267
268
269
270
271
272
		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;

273
		for (i = 0; i < arity; ++i) {
Christoph Mallon's avatar
Christoph Mallon committed
274
			ir_node* pred = irns[i].irn;
275
			if (get_nodes_block(pred) != block) continue;
Christoph Mallon's avatar
Christoph Mallon committed
276
			if (get_irn_mode(pred) == mode_M) continue;
277
			if (is_Proj(pred)) pred = get_Proj_pred(pred);
278
279
280
281
282
283
284
285
286
			sched = sched_node(sched, pred);
		}
	}

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


287
288
static int root_cmp(const void* a, const void* b)
{
289
290
	const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
	const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
291
292
293
294
295
296
297
298
299
300
301
302
	int ret;
	if (is_irn_forking(a1->irn)) {
		ret = 1;
	} else if (is_irn_forking(b1->irn)) {
		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);
		}
	}
303
#if defined NORMAL_DBG
304
	ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
305
306
307
308
309
#endif
	return ret;
}


310
311
static void normal_sched_block(ir_node* block, void* env)
{
312
313
	ir_node**      roots = (ir_node**)get_irn_link(block);
	ir_heights_t*  heights = (ir_heights_t*)env;
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
	int            root_count;
	irn_cost_pair* root_costs;
	int i;
	ir_node**      sched;

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

	if (roots == NULL) {
#if defined NORMAL_DBG
		fprintf(stderr, "has no roots\n");
#endif
		return;
	}

	root_count = ARR_LEN(roots);
	NEW_ARR_A(irn_cost_pair, root_costs, root_count);
	for (i = 0; i < root_count; ++i) {
		root_costs[i].irn  = roots[i];
334
335
336
337
		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
338
	}
339
	qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
340
341
342
343
344
345
346
347
348
349
350
351
#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
352
353
354
355

	sched = NEW_ARR_F(ir_node*, 0);
	for (i = 0; i < root_count; ++i) {
		ir_node* irn = root_costs[i].irn;
356
		assert(must_be_scheduled(irn));
357
358
		sched = sched_node(sched, irn);
	}
Michael Beck's avatar
Michael Beck committed
359
	set_irn_link(block, sched);
360
	DEL_ARR_F(roots);
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376

#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
}


377
static void *normal_init_graph(ir_graph *irg)
378
{
379
380
	instance_t   *inst = XMALLOC(instance_t);
	ir_heights_t *heights;
381
382
383

	be_clear_links(irg);

Michael Beck's avatar
Michael Beck committed
384
385
	obstack_init(&inst->obst);
	inst->irg         = irg;
386
387

	heights = heights_new(irg);
388

Michael Beck's avatar
Michael Beck committed
389
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
390
	irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
391
	irg_walk_graph(irg, collect_roots, NULL, NULL);
392
	inc_irg_visited(irg);
Michael Beck's avatar
Michael Beck committed
393
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
394
	irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
Michael Beck's avatar
Michael Beck committed
395
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
396

397
398
399
400
	heights_free(heights);

	return inst;
}
401

402
403
static void *normal_init_block(void *graph_env, ir_node *block)
{
404
405
	instance_t* inst  = (instance_t*)graph_env;
	ir_node**   sched = (ir_node**)get_irn_link(block);
406
407
408
409
410
411
412
	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];
413
		if (!is_cfop(irn)) {
414
415
416
417
418
419
420
421
422
			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
423
	return inst;
424
425
}

426
static void normal_finish_graph(void *env)
Michael Beck's avatar
Michael Beck committed
427
{
428
	instance_t *inst = (instance_t*)env;
Michael Beck's avatar
Michael Beck committed
429
430
431
432
433
434

	/* block uses the link field to store the schedule */
	ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
	obstack_free(&inst->obst, NULL);
	xfree(inst);
}
435

436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
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);
}

BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal);
void be_init_sched_normal(void)
{
	be_register_scheduler("normal", sched_normal);
}