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"
39 // XXX there is no one time init for schedulers
43 /** An instance of the normal scheduler. */
44 typedef struct instance_t {
45 ir_graph* irg; /**< the IR graph of this instance */
46 struct obstack obst; /**< obstack for temporary data */
47 ir_node* curr_list; /**< current block schedule list */
50 static int must_be_scheduled(const ir_node* const irn)
52 return !is_Proj(irn) && !is_Sync(irn);
56 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
57 ir_nodeset_t *live_set)
59 instance_t* inst = block_env;
63 ir_nodeset_iterator_t iter;
67 for (irn = inst->curr_list; irn != NULL; last = irn, irn = next) {
68 next = get_irn_link(irn);
69 if (ir_nodeset_contains(ready_set, irn)) {
70 #if defined NORMAL_DBG
71 ir_fprintf(stderr, "scheduling %+F\n", irn);
74 inst->curr_list = next;
76 set_irn_link(last, next);
81 ir_nodeset_iterator_init(&iter, ready_set);
82 irn = ir_nodeset_iterator_next(&iter);
87 typedef struct irn_cost_pair {
92 static int cost_cmp(const void* a, const void* b)
94 const irn_cost_pair* const a1 = a;
95 const irn_cost_pair* const b1 = b;
96 int ret = b1->cost - a1->cost;
98 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);
106 typedef struct flag_and_cost {
108 irn_cost_pair costs[];
111 #define get_irn_fc(irn) ((flag_and_cost*)get_irn_link(irn))
112 #define set_irn_fc(irn, fc) set_irn_link(irn, fc)
115 static int count_result(const ir_node* irn)
117 const ir_mode* mode = get_irn_mode(irn);
119 if (mode == mode_M || mode == mode_X)
125 if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
132 /* TODO high cost for store trees
135 static int normal_tree_cost(ir_node* irn, instance_t *inst)
149 return normal_tree_cost(get_Proj_pred(irn), inst);
152 arity = get_irn_arity(irn);
153 fc = get_irn_fc(irn);
156 irn_cost_pair* costs;
158 ir_node* block = get_nodes_block(irn);
160 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
164 for (i = 0; i < arity; ++i) {
165 ir_node* pred = get_irn_n(irn, i);
168 if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
170 } else if (get_nodes_block(pred) != block) {
173 flag_and_cost* pred_fc;
176 cost = normal_tree_cost(pred, inst);
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);
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);
189 costs[i].cost = cost;
192 qsort(costs, arity, sizeof(*costs), cost_cmp);
193 set_irn_link(irn, fc);
198 for (i = 0; i < arity; ++i) {
199 ir_node* op = fc->costs[i].irn;
203 mode = get_irn_mode(op);
206 if (mode != mode_T && arch_irn_is_ignore(op))
208 cost = MAX(fc->costs[i].cost + n_op_res, cost);
212 n_res = count_result(irn);
213 cost = MAX(n_res, cost);
215 #if defined NORMAL_DBG
216 ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
223 static void normal_cost_walker(ir_node* irn, void* env)
225 instance_t *inst = env;
227 #if defined NORMAL_DBG
228 ir_fprintf(stderr, "cost walking node %+F\n", irn);
230 if (is_Block(irn)) return;
231 if (!must_be_scheduled(irn)) return;
232 normal_tree_cost(irn, inst);
236 static void collect_roots(ir_node* irn, void* env)
242 if (is_Block(irn)) return;
243 if (!must_be_scheduled(irn)) return;
245 is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
247 #if defined NORMAL_DBG
248 ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
252 ir_node* block = get_nodes_block(irn);
253 ir_node** roots = get_irn_link(block);
255 roots = NEW_ARR_F(ir_node*, 0);
257 ARR_APP1(ir_node*, roots, irn);
258 set_irn_link(block, roots);
263 static ir_node** sched_node(ir_node** sched, ir_node* irn)
265 if (irn_visited_else_mark(irn)) return sched;
266 if (is_End(irn)) return sched;
268 if (!is_Phi(irn) && !be_is_Keep(irn)) {
269 ir_node* block = get_nodes_block(irn);
270 int arity = get_irn_arity(irn);
271 flag_and_cost* fc = get_irn_fc(irn);
272 irn_cost_pair* irns = fc->costs;
275 for (i = 0; i < arity; ++i) {
276 ir_node* pred = irns[i].irn;
277 if (get_nodes_block(pred) != block) continue;
278 if (get_irn_mode(pred) == mode_M) continue;
279 if (is_Proj(pred)) pred = get_Proj_pred(pred);
280 sched = sched_node(sched, pred);
284 ARR_APP1(ir_node*, sched, irn);
289 static int root_cmp(const void* a, const void* b)
291 const irn_cost_pair* const a1 = a;
292 const irn_cost_pair* const b1 = b;
294 if (is_irn_forking(a1->irn)) {
296 } else if (is_irn_forking(b1->irn)) {
299 ret = b1->cost - a1->cost;
301 /* place live-out nodes later */
302 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);
312 static void normal_sched_block(ir_node* block, void* env)
314 ir_node** roots = get_irn_link(block);
315 ir_heights_t* heights = env;
317 irn_cost_pair* root_costs;
321 #if defined NORMAL_DBG
322 ir_fprintf(stderr, "sched walking block %+F\n", block);
326 #if defined NORMAL_DBG
327 fprintf(stderr, "has no roots\n");
332 root_count = ARR_LEN(roots);
333 NEW_ARR_A(irn_cost_pair, root_costs, root_count);
334 for (i = 0; i < root_count; ++i) {
335 root_costs[i].irn = roots[i];
336 root_costs[i].cost = get_irn_height(heights, roots[i]);
337 #if defined NORMAL_DBG
338 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
341 qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
342 #if defined NORMAL_DBG
347 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
348 for (i = 0; i < n; ++i) {
349 ir_fprintf(stderr, " %+F\n", root_costs[i].irn);
351 fprintf(stderr, "\n");
355 sched = NEW_ARR_F(ir_node*, 0);
356 for (i = 0; i < root_count; ++i) {
357 ir_node* irn = root_costs[i].irn;
358 assert(must_be_scheduled(irn));
359 sched = sched_node(sched, irn);
361 set_irn_link(block, sched);
364 #if defined NORMAL_DBG
366 int n = ARR_LEN(sched);
369 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
370 for (i = 0; i < n; ++i) {
371 ir_fprintf(stderr, " %+F\n", sched[i]);
373 fprintf(stderr, "\n");
379 static void *normal_init_graph(const list_sched_selector_t *vtab,
382 instance_t *inst = XMALLOC(instance_t);
383 ir_heights_t *heights;
389 obstack_init(&inst->obst);
392 heights = heights_new(irg);
394 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
395 irg_walk_graph(irg, normal_cost_walker, NULL, inst);
396 irg_walk_graph(irg, collect_roots, NULL, NULL);
397 inc_irg_visited(irg);
398 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
399 irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
400 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
402 heights_free(heights);
407 static void *normal_init_block(void *graph_env, ir_node *block)
409 instance_t* inst = graph_env;
410 ir_node** sched = get_irn_link(block);
411 ir_node* first = NULL;
414 /* turn into a list, so we can easily remove nodes.
415 The link field is used anyway. */
416 for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
417 ir_node* irn = sched[i];
419 set_irn_link(irn, first);
423 /* note: we can free sched here, there should be no attempt to schedule
426 set_irn_link(block, sched);
427 inst->curr_list = first;
431 static void normal_finish_graph(void *env)
433 instance_t *inst = env;
435 /* block uses the link field to store the schedule */
436 ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
437 obstack_free(&inst->obst, NULL);
441 const list_sched_selector_t normal_selector = {
445 NULL, /* node_ready */
446 NULL, /* node_selected */
449 NULL, /* finish_block */