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
31 #include "besched_t.h"
32 #include "belistsched.h"
41 // XXX there is no one time init for schedulers
45 static int must_be_scheduled(const ir_node* const irn)
47 return !is_Proj(irn) && !is_Sync(irn);
51 static const arch_env_t *cur_arch_env;
54 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
55 ir_nodeset_t *live_set)
57 ir_nodeset_iterator_t iter;
66 ir_nodeset_iterator_init(&iter, ready_set);
67 irn = ir_nodeset_iterator_next(&iter);
68 block = get_nodes_block(irn);
69 sched = get_irn_link(block);
70 sched_count = ARR_LEN(sched);
71 for (; sched_count-- != 0; ++sched) {
72 ir_node* irn = *sched;
73 if (ir_nodeset_contains(ready_set, irn) &&
74 !arch_irn_class_is(cur_arch_env, irn, branch)) {
75 #if defined NORMAL_DBG
76 ir_fprintf(stderr, "scheduling %+F\n", irn);
86 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;
97 #if defined NORMAL_DBG
98 ir_fprintf(stderr, "%+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
104 typedef struct flag_and_cost {
106 irn_cost_pair costs[];
110 static int count_result(const ir_node* irn)
112 const ir_mode* mode = get_irn_mode(irn);
116 !arch_irn_is(cur_arch_env, irn, ignore);
120 /* TODO high cost for store trees
124 static int normal_tree_cost(ir_node* irn)
126 flag_and_cost* fc = get_irn_link(irn);
127 ir_node* block = get_nodes_block(irn);
128 int arity = get_irn_arity(irn);
138 return normal_tree_cost(get_Proj_pred(irn));
142 irn_cost_pair* costs;
145 fc = malloc(sizeof(*fc) + sizeof(*fc->costs) * arity);
149 for (i = 0; i < arity; ++i) {
150 ir_node* pred = get_irn_n(irn, i);
153 if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
155 } else if (get_nodes_block(pred) != block) {
158 flag_and_cost* pred_fc;
161 cost = normal_tree_cost(pred);
162 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
163 if (!arch_irn_is(cur_arch_env, pred, ignore)) {
164 real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
165 pred_fc = get_irn_link(real_pred);
166 pred_fc->no_root = 1;
167 #if defined NORMAL_DBG
168 ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
174 costs[i].cost = cost;
177 qsort(costs, arity, sizeof(*costs), cost_cmp);
178 set_irn_link(irn, fc);
182 for (i = 0; i < arity; ++i) {
183 if (get_irn_mode(fc->costs[i].irn) == mode_M) continue;
184 if (arch_irn_is(cur_arch_env, fc->costs[i].irn, ignore)) continue;
185 cost = MAX(fc->costs[i].cost + n_op_res, cost);
188 n_res = count_result(irn);
189 cost = MAX(n_res, cost);
191 #if defined NORMAL_DBG
192 ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
199 static void normal_cost_walker(ir_node* irn, void* env)
203 #if defined NORMAL_DBG
204 ir_fprintf(stderr, "cost walking node %+F\n", irn);
206 if (is_Block(irn)) return;
207 if (!must_be_scheduled(irn)) return;
208 normal_tree_cost(irn);
212 static void collect_roots(ir_node* irn, void* env)
218 if (is_Block(irn)) return;
219 if (!must_be_scheduled(irn)) return;
221 is_root = be_is_Keep(irn) || !((flag_and_cost*)get_irn_link(irn))->no_root;
223 #if defined NORMAL_DBG
224 ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
228 ir_node* block = get_nodes_block(irn);
229 ir_node** roots = get_irn_link(block);
231 roots = NEW_ARR_F(ir_node*, 0);
233 ARR_APP1(ir_node*, roots, irn);
234 set_irn_link(block, roots);
239 static ir_node** sched_node(ir_node** sched, ir_node* irn)
241 ir_node* block = get_nodes_block(irn);
242 flag_and_cost* fc = get_irn_link(irn);
243 irn_cost_pair* irns = fc->costs;
244 int arity = get_irn_arity(irn);
247 if (irn_visited(irn)) return sched;
248 if (is_End(irn)) return sched;
250 if (!is_Phi(irn) && !be_is_Keep(irn)) {
251 for (i = 0; i < arity; ++i) {
252 ir_node* pred = irns[i].irn;
253 if (get_nodes_block(pred) != block) continue;
254 if (get_irn_mode(pred) == mode_M) continue;
255 if (is_Proj(pred)) pred = get_Proj_pred(pred);
256 sched = sched_node(sched, pred);
260 mark_irn_visited(irn);
261 ARR_APP1(ir_node*, sched, irn);
266 static int root_cmp(const void* a, const void* b)
268 const irn_cost_pair* const a1 = a;
269 const irn_cost_pair* const b1 = b;
271 if (is_irn_forking(a1->irn)) {
273 } else if (is_irn_forking(b1->irn)) {
276 ret = b1->cost - a1->cost;
278 /* place live-out nodes later */
279 ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
282 #if defined NORMAL_DBG
283 ir_fprintf(stderr, "%+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
289 static void normal_sched_block(ir_node* block, void* env)
291 heights_t* heights = env;
292 ir_node** roots = get_irn_link(block);
294 irn_cost_pair* root_costs;
298 #if defined NORMAL_DBG
299 ir_fprintf(stderr, "sched walking block %+F\n", block);
303 #if defined NORMAL_DBG
304 fprintf(stderr, "has no roots\n");
309 root_count = ARR_LEN(roots);
310 NEW_ARR_A(irn_cost_pair, root_costs, root_count);
311 for (i = 0; i < root_count; ++i) {
312 root_costs[i].irn = roots[i];
313 root_costs[i].cost = get_irn_height(heights, roots[i]);
314 #if defined NORMAL_DBG
315 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
318 qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
319 #if defined NORMAL_DBG
324 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
325 for (i = 0; i < n; ++i) {
326 ir_fprintf(stderr, " %+F\n", root_costs[i].irn);
328 fprintf(stderr, "\n");
332 sched = NEW_ARR_F(ir_node*, 0);
333 for (i = 0; i < root_count; ++i) {
334 ir_node* irn = root_costs[i].irn;
335 assert(must_be_scheduled(irn));
336 sched = sched_node(sched, irn);
338 set_irn_link(block, sched);
341 #if defined NORMAL_DBG
343 int n = ARR_LEN(sched);
346 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
347 for (i = 0; i < n; ++i) {
348 ir_fprintf(stderr, " %+F\n", sched[i]);
350 fprintf(stderr, "\n");
356 static void *normal_init_graph(const list_sched_selector_t *vtab,
357 const be_irg_t *birg)
359 ir_graph *irg = be_get_birg_irg(birg);
364 cur_arch_env = be_get_birg_arch_env(birg);
368 heights = heights_new(irg);
370 irg_walk_graph(irg, normal_cost_walker, NULL, NULL);
371 irg_walk_graph(irg, collect_roots, NULL, NULL);
372 inc_irg_visited(irg);
373 irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
375 heights_free(heights);
381 static void *normal_init_block(void *graph_env, ir_node *block)
390 const list_sched_selector_t normal_selector = {
394 NULL, /* to_appear_in_schedule */
395 NULL, /* node_ready */
396 NULL, /* node_selected */
399 NULL, /* finish_block */
400 NULL /* finish_graph */