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
23
24
25
26
27
 *
 * 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
 */
#include "config.h"

#include <stdlib.h>

28
#include "besched.h"
29
30
31
#include "belistsched.h"
#include "belive_t.h"
#include "beutil.h"
32
#include "heights.h"
33
#include "irgwalk.h"
34
#include "benode.h"
35
#include "bemodule.h"
36
#include "util.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
static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set)
57
{
58
	instance_t* inst = (instance_t*)block_env;
59
60
61
	ir_node*    irn;
	ir_node*    next;
	ir_node*    last = NULL;
62

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

77
	return ir_nodeset_first(ready_set);
78
79
80
81
82
83
84
85
86
87
}


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

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


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

Michael Beck's avatar
Michael Beck committed
105
106
107
#define get_irn_fc(irn)     ((flag_and_cost*)get_irn_link(irn))
#define set_irn_fc(irn, fc) set_irn_link(irn, fc)

108
109
110
111

static int count_result(const ir_node* irn)
{
	const ir_mode* mode = get_irn_mode(irn);
112
113
114
115

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

116
117
118
	if (mode == mode_T)
		return 1;

119
	if (arch_get_irn_register_req(irn)->type & arch_register_req_type_ignore)
120
121
122
		return 0;

	return 1;
123
124
125
}


Christoph Mallon's avatar
Christoph Mallon committed
126
127
128
/* TODO high cost for store trees
 */

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

139
140
141
	if (be_is_Keep(irn))
		return 0;

142
	if (is_Proj(irn)) {
Michael Beck's avatar
Michael Beck committed
143
		return normal_tree_cost(get_Proj_pred(irn), inst);
144
145
	}

Michael Beck's avatar
Michael Beck committed
146
147
148
	arity = get_irn_arity(irn);
	fc    = get_irn_fc(irn);

149
150
	if (fc == NULL) {
		irn_cost_pair* costs;
Michael Beck's avatar
Michael Beck committed
151
		ir_node*       block = get_nodes_block(irn);
152

153
		fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
154
155
156
157
158
159
		fc->no_root = 0;
		costs = fc->costs;

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

160
			if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
161
162
163
164
				cost = 0;
			} else if (get_nodes_block(pred) != block) {
				cost = 1;
			} else {
Michael Beck's avatar
Michael Beck committed
165
				flag_and_cost* pred_fc;
166
				ir_node*       real_pred;
Michael Beck's avatar
Michael Beck committed
167

Michael Beck's avatar
Michael Beck committed
168
				cost = normal_tree_cost(pred, inst);
169
				if (!arch_irn_is_ignore(pred)) {
170
					real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
Michael Beck's avatar
Michael Beck committed
171
					pred_fc = get_irn_fc(real_pred);
172
					pred_fc->no_root = 1;
173
#if defined NORMAL_DBG
174
					ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
175
#endif
176
				}
177
178
179
180
181
182
183
184
185
186
			}

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

#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)
{
218
	instance_t *inst = (instance_t*)env;
219
220
221
222
223

#if defined NORMAL_DBG
	ir_fprintf(stderr, "cost walking node %+F\n", irn);
#endif
	if (is_Block(irn)) return;
224
	if (!must_be_scheduled(irn)) return;
Michael Beck's avatar
Michael Beck committed
225
	normal_tree_cost(irn, inst);
226
227
228
229
230
}


static void collect_roots(ir_node* irn, void* env)
{
231
	int is_root;
232
233
234
235

	(void)env;

	if (is_Block(irn)) return;
236
	if (!must_be_scheduled(irn)) return;
237

Michael Beck's avatar
Michael Beck committed
238
	is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
239
240

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

244
	if (is_root) {
245
		ir_node* block = get_nodes_block(irn);
246
		ir_node** roots = (ir_node**)get_irn_link(block);
247
248
249
250
251
252
253
254
255
256
257
		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)
{
258
259
	if (irn_visited_else_mark(irn)) return sched;
	if (is_End(irn))                return sched;
260

261
	if (!is_Phi(irn) && !be_is_Keep(irn)) {
Michael Beck's avatar
Michael Beck committed
262
263
264
265
266
267
		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;

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

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


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


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

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

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


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

	be_clear_links(irg);

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

	heights = heights_new(irg);
387

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

396
397
398
399
	heights_free(heights);

	return inst;
}
400

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

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

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

435
436
437
438
439
440
441
442
443
444
445
446
447
448
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
449
BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
450
451
452
453
void be_init_sched_normal(void)
{
	be_register_scheduler("normal", sched_normal);
}