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
29 #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)
58 instance_t* inst = (instance_t*)block_env;
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 #if defined NORMAL_DBG
67 ir_fprintf(stderr, "scheduling %+F\n", irn);
70 inst->curr_list = next;
72 set_irn_link(last, next);
77 return ir_nodeset_first(ready_set);
81 typedef struct irn_cost_pair {
86 static int cost_cmp(const void* a, const void* b)
88 const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
89 const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
90 int ret = b1->cost - a1->cost;
92 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);
100 typedef struct flag_and_cost {
102 irn_cost_pair costs[];
105 #define get_irn_fc(irn) ((flag_and_cost*)get_irn_link(irn))
106 #define set_irn_fc(irn, fc) set_irn_link(irn, fc)
109 static int count_result(const ir_node* irn)
111 const ir_mode* mode = get_irn_mode(irn);
113 if (mode == mode_M || mode == mode_X)
119 arch_register_req_t const *const req = arch_get_irn_register_req(irn);
120 if (arch_register_req_is(req, ignore))
127 /* TODO high cost for store trees
130 static int normal_tree_cost(ir_node* irn, instance_t *inst)
144 return normal_tree_cost(get_Proj_pred(irn), inst);
147 arity = get_irn_arity(irn);
148 fc = get_irn_fc(irn);
151 irn_cost_pair* costs;
152 ir_node* block = get_nodes_block(irn);
154 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
158 for (i = 0; i < arity; ++i) {
159 ir_node* pred = get_irn_n(irn, i);
161 if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
163 } else if (get_nodes_block(pred) != block) {
166 flag_and_cost* pred_fc;
169 cost = normal_tree_cost(pred, inst);
170 if (!arch_irn_is_ignore(pred)) {
171 real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
172 pred_fc = get_irn_fc(real_pred);
173 pred_fc->no_root = 1;
174 #if defined NORMAL_DBG
175 ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
181 costs[i].cost = cost;
184 qsort(costs, arity, sizeof(*costs), cost_cmp);
185 set_irn_link(irn, fc);
190 for (i = 0; i < arity; ++i) {
191 ir_node* op = fc->costs[i].irn;
195 mode = get_irn_mode(op);
198 if (mode != mode_T && arch_irn_is_ignore(op))
200 cost = MAX(fc->costs[i].cost + n_op_res, cost);
204 n_res = count_result(irn);
205 cost = MAX(n_res, cost);
207 #if defined NORMAL_DBG
208 ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
215 static void normal_cost_walker(ir_node* irn, void* env)
217 instance_t *inst = (instance_t*)env;
219 #if defined NORMAL_DBG
220 ir_fprintf(stderr, "cost walking node %+F\n", irn);
222 if (is_Block(irn)) return;
223 if (!must_be_scheduled(irn)) return;
224 normal_tree_cost(irn, inst);
228 static void collect_roots(ir_node* irn, void* env)
234 if (is_Block(irn)) return;
235 if (!must_be_scheduled(irn)) return;
237 is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
239 #if defined NORMAL_DBG
240 ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
244 ir_node* block = get_nodes_block(irn);
245 ir_node** roots = (ir_node**)get_irn_link(block);
247 roots = NEW_ARR_F(ir_node*, 0);
249 ARR_APP1(ir_node*, roots, irn);
250 set_irn_link(block, roots);
255 static ir_node** sched_node(ir_node** sched, ir_node* irn)
257 if (irn_visited_else_mark(irn)) return sched;
258 if (is_End(irn)) return sched;
260 if (!is_Phi(irn) && !be_is_Keep(irn)) {
261 ir_node* block = get_nodes_block(irn);
262 int arity = get_irn_arity(irn);
263 flag_and_cost* fc = get_irn_fc(irn);
264 irn_cost_pair* irns = fc->costs;
267 for (i = 0; i < arity; ++i) {
268 ir_node* pred = irns[i].irn;
269 if (get_nodes_block(pred) != block) continue;
270 if (get_irn_mode(pred) == mode_M) continue;
271 if (is_Proj(pred)) pred = get_Proj_pred(pred);
272 sched = sched_node(sched, pred);
276 ARR_APP1(ir_node*, sched, irn);
281 static int root_cmp(const void* a, const void* b)
283 const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
284 const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
286 if (is_irn_forking(a1->irn) && !is_irn_forking(b1->irn)) {
288 } else if (is_irn_forking(b1->irn) && !is_irn_forking(a1->irn)) {
291 ret = b1->cost - a1->cost;
293 /* place live-out nodes later */
294 ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
296 /* compare node idx */
297 ret = get_irn_idx(a1->irn) - get_irn_idx(b1->irn);
301 #if defined NORMAL_DBG
302 ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
308 static void normal_sched_block(ir_node* block, void* env)
310 ir_node** roots = (ir_node**)get_irn_link(block);
311 ir_heights_t* heights = (ir_heights_t*)env;
313 irn_cost_pair* root_costs;
317 #if defined NORMAL_DBG
318 ir_fprintf(stderr, "sched walking block %+F\n", block);
322 #if defined NORMAL_DBG
323 fprintf(stderr, "has no roots\n");
328 root_count = ARR_LEN(roots);
329 NEW_ARR_A(irn_cost_pair, root_costs, root_count);
330 for (i = 0; i < root_count; ++i) {
331 root_costs[i].irn = roots[i];
332 root_costs[i].cost = get_irn_height(heights, roots[i]);
333 #if defined NORMAL_DBG
334 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
337 qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
338 #if defined NORMAL_DBG
343 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
344 for (i = 0; i < n; ++i) {
345 ir_fprintf(stderr, " %+F\n", root_costs[i].irn);
347 fprintf(stderr, "\n");
351 sched = NEW_ARR_F(ir_node*, 0);
352 for (i = 0; i < root_count; ++i) {
353 ir_node* irn = root_costs[i].irn;
354 assert(must_be_scheduled(irn));
355 sched = sched_node(sched, irn);
357 set_irn_link(block, sched);
360 #if defined NORMAL_DBG
362 int n = ARR_LEN(sched);
365 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
366 for (i = 0; i < n; ++i) {
367 ir_fprintf(stderr, " %+F\n", sched[i]);
369 fprintf(stderr, "\n");
375 static void *normal_init_graph(ir_graph *irg)
377 instance_t *inst = XMALLOC(instance_t);
378 ir_heights_t *heights;
382 obstack_init(&inst->obst);
385 heights = heights_new(irg);
387 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
388 irg_walk_graph(irg, normal_cost_walker, NULL, inst);
389 irg_walk_graph(irg, collect_roots, NULL, NULL);
390 inc_irg_visited(irg);
391 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
392 irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
393 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
395 heights_free(heights);
400 static void *normal_init_block(void *graph_env, ir_node *block)
402 instance_t* inst = (instance_t*)graph_env;
403 ir_node** sched = (ir_node**)get_irn_link(block);
404 ir_node* first = NULL;
407 /* turn into a list, so we can easily remove nodes.
408 The link field is used anyway. */
409 for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
410 ir_node* irn = sched[i];
412 set_irn_link(irn, first);
416 /* note: we can free sched here, there should be no attempt to schedule
419 set_irn_link(block, sched);
420 inst->curr_list = first;
424 static void normal_finish_graph(void *env)
426 instance_t *inst = (instance_t*)env;
428 /* block uses the link field to store the schedule */
429 ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
430 obstack_free(&inst->obst, NULL);
434 static void sched_normal(ir_graph *irg)
436 static const list_sched_selector_t normal_selector = {
440 NULL, /* node_ready */
441 NULL, /* node_selected */
442 NULL, /* finish_block */
445 be_list_sched_graph(irg, &normal_selector);
448 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
449 void be_init_sched_normal(void)
451 be_register_scheduler("normal", sched_normal);