2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * @brief Use the strong normal form theorem (though it does not hold)
22 * @author Christoph Mallon
30 #include "belistsched.h"
40 // XXX there is no one time init for schedulers
44 /** An instance of the normal scheduler. */
45 typedef struct instance_t {
46 ir_graph* irg; /**< the IR graph of this instance */
47 struct obstack obst; /**< obstack for temporary data */
48 ir_node* curr_list; /**< current block schedule list */
51 static int must_be_scheduled(const ir_node* const irn)
53 return !is_Proj(irn) && !is_Sync(irn);
57 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set)
59 instance_t* inst = (instance_t*)block_env;
63 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 #if defined NORMAL_DBG
69 ir_fprintf(stderr, "scheduling %+F\n", irn);
72 inst->curr_list = next;
74 set_irn_link(last, next);
79 ir_nodeset_iterator_init(&iter, ready_set);
80 irn = ir_nodeset_iterator_next(&iter);
85 typedef struct irn_cost_pair {
90 static int cost_cmp(const void* a, const void* b)
92 const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
93 const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
94 int ret = b1->cost - a1->cost;
96 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);
104 typedef struct flag_and_cost {
106 irn_cost_pair costs[];
109 #define get_irn_fc(irn) ((flag_and_cost*)get_irn_link(irn))
110 #define set_irn_fc(irn, fc) set_irn_link(irn, fc)
113 static int count_result(const ir_node* irn)
115 const ir_mode* mode = get_irn_mode(irn);
117 if (mode == mode_M || mode == mode_X)
123 if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
130 /* TODO high cost for store trees
133 static int normal_tree_cost(ir_node* irn, instance_t *inst)
147 return normal_tree_cost(get_Proj_pred(irn), inst);
150 arity = get_irn_arity(irn);
151 fc = get_irn_fc(irn);
154 irn_cost_pair* costs;
155 ir_node* block = get_nodes_block(irn);
157 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
161 for (i = 0; i < arity; ++i) {
162 ir_node* pred = get_irn_n(irn, i);
164 if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
166 } else if (get_nodes_block(pred) != block) {
169 flag_and_cost* pred_fc;
172 cost = normal_tree_cost(pred, inst);
173 if (!arch_irn_is_ignore(pred)) {
174 real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
175 pred_fc = get_irn_fc(real_pred);
176 pred_fc->no_root = 1;
177 #if defined NORMAL_DBG
178 ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
184 costs[i].cost = cost;
187 qsort(costs, arity, sizeof(*costs), cost_cmp);
188 set_irn_link(irn, fc);
193 for (i = 0; i < arity; ++i) {
194 ir_node* op = fc->costs[i].irn;
198 mode = get_irn_mode(op);
201 if (mode != mode_T && arch_irn_is_ignore(op))
203 cost = MAX(fc->costs[i].cost + n_op_res, cost);
207 n_res = count_result(irn);
208 cost = MAX(n_res, cost);
210 #if defined NORMAL_DBG
211 ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
218 static void normal_cost_walker(ir_node* irn, void* env)
220 instance_t *inst = (instance_t*)env;
222 #if defined NORMAL_DBG
223 ir_fprintf(stderr, "cost walking node %+F\n", irn);
225 if (is_Block(irn)) return;
226 if (!must_be_scheduled(irn)) return;
227 normal_tree_cost(irn, inst);
231 static void collect_roots(ir_node* irn, void* env)
237 if (is_Block(irn)) return;
238 if (!must_be_scheduled(irn)) return;
240 is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
242 #if defined NORMAL_DBG
243 ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
247 ir_node* block = get_nodes_block(irn);
248 ir_node** roots = (ir_node**)get_irn_link(block);
250 roots = NEW_ARR_F(ir_node*, 0);
252 ARR_APP1(ir_node*, roots, irn);
253 set_irn_link(block, roots);
258 static ir_node** sched_node(ir_node** sched, ir_node* irn)
260 if (irn_visited_else_mark(irn)) return sched;
261 if (is_End(irn)) return sched;
263 if (!is_Phi(irn) && !be_is_Keep(irn)) {
264 ir_node* block = get_nodes_block(irn);
265 int arity = get_irn_arity(irn);
266 flag_and_cost* fc = get_irn_fc(irn);
267 irn_cost_pair* irns = fc->costs;
270 for (i = 0; i < arity; ++i) {
271 ir_node* pred = irns[i].irn;
272 if (get_nodes_block(pred) != block) continue;
273 if (get_irn_mode(pred) == mode_M) continue;
274 if (is_Proj(pred)) pred = get_Proj_pred(pred);
275 sched = sched_node(sched, pred);
279 ARR_APP1(ir_node*, sched, irn);
284 static int root_cmp(const void* a, const void* b)
286 const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
287 const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
289 if (is_irn_forking(a1->irn)) {
291 } else if (is_irn_forking(b1->irn)) {
294 ret = b1->cost - a1->cost;
296 /* place live-out nodes later */
297 ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
300 #if defined NORMAL_DBG
301 ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
307 static void normal_sched_block(ir_node* block, void* env)
309 ir_node** roots = (ir_node**)get_irn_link(block);
310 ir_heights_t* heights = (ir_heights_t*)env;
312 irn_cost_pair* root_costs;
316 #if defined NORMAL_DBG
317 ir_fprintf(stderr, "sched walking block %+F\n", block);
321 #if defined NORMAL_DBG
322 fprintf(stderr, "has no roots\n");
327 root_count = ARR_LEN(roots);
328 NEW_ARR_A(irn_cost_pair, root_costs, root_count);
329 for (i = 0; i < root_count; ++i) {
330 root_costs[i].irn = roots[i];
331 root_costs[i].cost = get_irn_height(heights, roots[i]);
332 #if defined NORMAL_DBG
333 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
336 qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
337 #if defined NORMAL_DBG
342 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
343 for (i = 0; i < n; ++i) {
344 ir_fprintf(stderr, " %+F\n", root_costs[i].irn);
346 fprintf(stderr, "\n");
350 sched = NEW_ARR_F(ir_node*, 0);
351 for (i = 0; i < root_count; ++i) {
352 ir_node* irn = root_costs[i].irn;
353 assert(must_be_scheduled(irn));
354 sched = sched_node(sched, irn);
356 set_irn_link(block, sched);
359 #if defined NORMAL_DBG
361 int n = ARR_LEN(sched);
364 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
365 for (i = 0; i < n; ++i) {
366 ir_fprintf(stderr, " %+F\n", sched[i]);
368 fprintf(stderr, "\n");
374 static void *normal_init_graph(ir_graph *irg)
376 instance_t *inst = XMALLOC(instance_t);
377 ir_heights_t *heights;
381 obstack_init(&inst->obst);
384 heights = heights_new(irg);
386 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
387 irg_walk_graph(irg, normal_cost_walker, NULL, inst);
388 irg_walk_graph(irg, collect_roots, NULL, NULL);
389 inc_irg_visited(irg);
390 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
391 irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
392 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
394 heights_free(heights);
399 static void *normal_init_block(void *graph_env, ir_node *block)
401 instance_t* inst = (instance_t*)graph_env;
402 ir_node** sched = (ir_node**)get_irn_link(block);
403 ir_node* first = NULL;
406 /* turn into a list, so we can easily remove nodes.
407 The link field is used anyway. */
408 for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
409 ir_node* irn = sched[i];
411 set_irn_link(irn, first);
415 /* note: we can free sched here, there should be no attempt to schedule
418 set_irn_link(block, sched);
419 inst->curr_list = first;
423 static void normal_finish_graph(void *env)
425 instance_t *inst = (instance_t*)env;
427 /* block uses the link field to store the schedule */
428 ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
429 obstack_free(&inst->obst, NULL);
433 static void sched_normal(ir_graph *irg)
435 static const list_sched_selector_t normal_selector = {
439 NULL, /* node_ready */
440 NULL, /* node_selected */
441 NULL, /* finish_block */
444 be_list_sched_graph(irg, &normal_selector);
447 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
448 void be_init_sched_normal(void)
450 be_register_scheduler("normal", sched_normal);