beschednormal.c 10.4 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 "array_t.h"
38
39

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

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

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


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

	(void)live_set;

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

81
82
	ir_nodeset_iterator_init(&iter, ready_set);
	irn = ir_nodeset_iterator_next(&iter);
83
84
85
86
87
88
89
90
91
92
93
	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)
{
94
95
	const irn_cost_pair* const a1 = a;
	const irn_cost_pair* const b1 = b;
96
	int ret = b1->cost - a1->cost;
97
98
	if (ret == 0)
		ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
99
#if defined NORMAL_DBG
100
	ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
101
102
#endif
	return ret;
103
104
105
106
107
108
109
110
}


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

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

114
115
116
117

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

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

122
123
124
	if (mode == mode_T)
		return 1;

125
126
127
128
	if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
		return 0;

	return 1;
129
130
131
}


Christoph Mallon's avatar
Christoph Mallon committed
132
133
134
/* TODO high cost for store trees
 */

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

145
146
147
	if (be_is_Keep(irn))
		return 0;

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

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

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

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

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

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

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

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

#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)
{
Michael Beck's avatar
Michael Beck committed
225
	instance_t *inst = env;
226
227
228
229
230

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


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

	(void)env;

	if (is_Block(irn)) return;
243
	if (!must_be_scheduled(irn)) return;
244

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

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

251
	if (is_root) {
252
253
254
255
256
257
258
259
260
261
262
263
264
		ir_node* block = get_nodes_block(irn);
		ir_node** roots = get_irn_link(block);
		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)
{
265
266
	if (irn_visited_else_mark(irn)) return sched;
	if (is_End(irn))                return sched;
267

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

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

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


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


312
313
static void normal_sched_block(ir_node* block, void* env)
{
Michael Beck's avatar
Michael Beck committed
314
	ir_node**      roots = get_irn_link(block);
315
	ir_heights_t*  heights = env;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
	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];
336
337
338
339
		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
340
	}
341
	qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
342
343
344
345
346
347
348
349
350
351
352
353
#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
354
355
356
357

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

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


static void *normal_init_graph(const list_sched_selector_t *vtab,
380
                               ir_graph *irg)
381
{
382
383
	instance_t   *inst = XMALLOC(instance_t);
	ir_heights_t *heights;
384
385
386
387
388

	(void)vtab;

	be_clear_links(irg);

Michael Beck's avatar
Michael Beck committed
389
390
	obstack_init(&inst->obst);
	inst->irg         = irg;
391
392

	heights = heights_new(irg);
393

Michael Beck's avatar
Michael Beck committed
394
	ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
Michael Beck's avatar
Michael Beck committed
395
	irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
396
	irg_walk_graph(irg, collect_roots, NULL, NULL);
397
	inc_irg_visited(irg);
Michael Beck's avatar
Michael Beck committed
398
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
399
	irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
Michael Beck's avatar
Michael Beck committed
400
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
401

402
403
404
405
	heights_free(heights);

	return inst;
}
406

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

431
static void normal_finish_graph(void *env)
Michael Beck's avatar
Michael Beck committed
432
433
434
435
436
437
438
439
{
	instance_t *inst = env;

	/* 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);
}
440

441
const list_sched_selector_t normal_selector = {
442
	normal_init_graph,
443
	normal_init_block,
444
445
446
447
448
449
450
	normal_select,
	NULL,              /* to_appear_in_schedule */
	NULL,              /* node_ready */
	NULL,              /* node_selected */
	NULL,              /* exectime */
	NULL,              /* latency */
	NULL,              /* finish_block */
Michael Beck's avatar
Michael Beck committed
451
	normal_finish_graph
452
};