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;
156 ir_node* block = get_nodes_block(irn);
158 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
162 for (i = 0; i < arity; ++i) {
163 ir_node* pred = get_irn_n(irn, i);
166 if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
168 } else if (get_nodes_block(pred) != block) {
171 flag_and_cost* pred_fc;
174 cost = normal_tree_cost(pred, inst);
175 if (!arch_irn_is_ignore(pred)) {
176 real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
177 pred_fc = get_irn_fc(real_pred);
178 pred_fc->no_root = 1;
179 #if defined NORMAL_DBG
180 ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
186 costs[i].cost = cost;
189 qsort(costs, arity, sizeof(*costs), cost_cmp);
190 set_irn_link(irn, fc);
195 for (i = 0; i < arity; ++i) {
196 ir_node* op = fc->costs[i].irn;
200 mode = get_irn_mode(op);
203 if (mode != mode_T && arch_irn_is_ignore(op))
205 cost = MAX(fc->costs[i].cost + n_op_res, cost);
209 n_res = count_result(irn);
210 cost = MAX(n_res, cost);
212 #if defined NORMAL_DBG
213 ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
220 static void normal_cost_walker(ir_node* irn, void* env)
222 instance_t *inst = (instance_t*)env;
224 #if defined NORMAL_DBG
225 ir_fprintf(stderr, "cost walking node %+F\n", irn);
227 if (is_Block(irn)) return;
228 if (!must_be_scheduled(irn)) return;
229 normal_tree_cost(irn, inst);
233 static void collect_roots(ir_node* irn, void* env)
239 if (is_Block(irn)) return;
240 if (!must_be_scheduled(irn)) return;
242 is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
244 #if defined NORMAL_DBG
245 ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
249 ir_node* block = get_nodes_block(irn);
250 ir_node** roots = (ir_node**)get_irn_link(block);
252 roots = NEW_ARR_F(ir_node*, 0);
254 ARR_APP1(ir_node*, roots, irn);
255 set_irn_link(block, roots);
260 static ir_node** sched_node(ir_node** sched, ir_node* irn)
262 if (irn_visited_else_mark(irn)) return sched;
263 if (is_End(irn)) return sched;
265 if (!is_Phi(irn) && !be_is_Keep(irn)) {
266 ir_node* block = get_nodes_block(irn);
267 int arity = get_irn_arity(irn);
268 flag_and_cost* fc = get_irn_fc(irn);
269 irn_cost_pair* irns = fc->costs;
272 for (i = 0; i < arity; ++i) {
273 ir_node* pred = irns[i].irn;
274 if (get_nodes_block(pred) != block) continue;
275 if (get_irn_mode(pred) == mode_M) continue;
276 if (is_Proj(pred)) pred = get_Proj_pred(pred);
277 sched = sched_node(sched, pred);
281 ARR_APP1(ir_node*, sched, irn);
286 static int root_cmp(const void* a, const void* b)
288 const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
289 const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
291 if (is_irn_forking(a1->irn)) {
293 } else if (is_irn_forking(b1->irn)) {
296 ret = b1->cost - a1->cost;
298 /* place live-out nodes later */
299 ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
302 #if defined NORMAL_DBG
303 ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
309 static void normal_sched_block(ir_node* block, void* env)
311 ir_node** roots = (ir_node**)get_irn_link(block);
312 ir_heights_t* heights = (ir_heights_t*)env;
314 irn_cost_pair* root_costs;
318 #if defined NORMAL_DBG
319 ir_fprintf(stderr, "sched walking block %+F\n", block);
323 #if defined NORMAL_DBG
324 fprintf(stderr, "has no roots\n");
329 root_count = ARR_LEN(roots);
330 NEW_ARR_A(irn_cost_pair, root_costs, root_count);
331 for (i = 0; i < root_count; ++i) {
332 root_costs[i].irn = roots[i];
333 root_costs[i].cost = get_irn_height(heights, roots[i]);
334 #if defined NORMAL_DBG
335 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
338 qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
339 #if defined NORMAL_DBG
344 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
345 for (i = 0; i < n; ++i) {
346 ir_fprintf(stderr, " %+F\n", root_costs[i].irn);
348 fprintf(stderr, "\n");
352 sched = NEW_ARR_F(ir_node*, 0);
353 for (i = 0; i < root_count; ++i) {
354 ir_node* irn = root_costs[i].irn;
355 assert(must_be_scheduled(irn));
356 sched = sched_node(sched, irn);
358 set_irn_link(block, sched);
361 #if defined NORMAL_DBG
363 int n = ARR_LEN(sched);
366 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
367 for (i = 0; i < n; ++i) {
368 ir_fprintf(stderr, " %+F\n", sched[i]);
370 fprintf(stderr, "\n");
376 static void *normal_init_graph(ir_graph *irg)
378 instance_t *inst = XMALLOC(instance_t);
379 ir_heights_t *heights;
383 obstack_init(&inst->obst);
386 heights = heights_new(irg);
388 ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
389 irg_walk_graph(irg, normal_cost_walker, NULL, inst);
390 irg_walk_graph(irg, collect_roots, NULL, NULL);
391 inc_irg_visited(irg);
392 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
393 irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
394 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
396 heights_free(heights);
401 static void *normal_init_block(void *graph_env, ir_node *block)
403 instance_t* inst = (instance_t*)graph_env;
404 ir_node** sched = (ir_node**)get_irn_link(block);
405 ir_node* first = NULL;
408 /* turn into a list, so we can easily remove nodes.
409 The link field is used anyway. */
410 for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
411 ir_node* irn = sched[i];
413 set_irn_link(irn, first);
417 /* note: we can free sched here, there should be no attempt to schedule
420 set_irn_link(block, sched);
421 inst->curr_list = first;
425 static void normal_finish_graph(void *env)
427 instance_t *inst = (instance_t*)env;
429 /* block uses the link field to store the schedule */
430 ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
431 obstack_free(&inst->obst, NULL);
435 static void sched_normal(ir_graph *irg)
437 static const list_sched_selector_t normal_selector = {
441 NULL, /* node_ready */
442 NULL, /* node_selected */
443 NULL, /* finish_block */
446 be_list_sched_graph(irg, &normal_selector);
449 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
450 void be_init_sched_normal(void)
452 be_register_scheduler("normal", sched_normal);