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
46 static int must_be_scheduled(const ir_node* const irn)
48 return !is_Proj(irn) && !is_Sync(irn);
52 static const arch_env_t *cur_arch_env;
55 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
56 ir_nodeset_t *live_set)
58 ir_nodeset_iterator_t iter;
67 ir_nodeset_iterator_init(&iter, ready_set);
68 irn = ir_nodeset_iterator_next(&iter);
69 block = get_nodes_block(irn);
70 sched = get_irn_link(block);
71 sched_count = ARR_LEN(sched);
72 for (; sched_count-- != 0; ++sched) {
73 ir_node* irn = *sched;
74 if (ir_nodeset_contains(ready_set, irn) &&
75 !arch_irn_class_is(cur_arch_env, irn, branch)) {
76 #if defined NORMAL_DBG
77 ir_fprintf(stderr, "scheduling %+F\n", irn);
87 typedef struct irn_cost_pair {
93 static int cost_cmp(const void* a, const void* b)
95 const irn_cost_pair* const a1 = a;
96 const irn_cost_pair* const b1 = b;
97 int ret = b1->cost - a1->cost;
98 #if defined NORMAL_DBG
99 ir_fprintf(stderr, "%+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
105 typedef struct flag_and_cost {
107 irn_cost_pair costs[];
111 static int count_result(const ir_node* irn)
113 const ir_mode* mode = get_irn_mode(irn);
117 !arch_irn_is(cur_arch_env, irn, ignore);
121 /* TODO high cost for store trees
125 static int normal_tree_cost(ir_node* irn)
127 flag_and_cost* fc = get_irn_link(irn);
128 ir_node* block = get_nodes_block(irn);
129 int arity = get_irn_arity(irn);
139 return normal_tree_cost(get_Proj_pred(irn));
143 irn_cost_pair* costs;
146 fc = malloc(sizeof(*fc) + sizeof(*fc->costs) * arity);
150 for (i = 0; i < arity; ++i) {
151 ir_node* pred = get_irn_n(irn, i);
154 if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
156 } else if (get_nodes_block(pred) != block) {
159 flag_and_cost* pred_fc;
162 cost = normal_tree_cost(pred);
163 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
164 if (!arch_irn_is(cur_arch_env, pred, ignore)) {
165 real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
166 pred_fc = get_irn_link(real_pred);
167 pred_fc->no_root = 1;
168 #if defined NORMAL_DBG
169 ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
175 costs[i].cost = cost;
178 qsort(costs, arity, sizeof(*costs), cost_cmp);
179 set_irn_link(irn, fc);
183 for (i = 0; i < arity; ++i) {
184 if (get_irn_mode(fc->costs[i].irn) == mode_M) continue;
185 if (arch_irn_is(cur_arch_env, fc->costs[i].irn, ignore)) continue;
186 cost = MAX(fc->costs[i].cost + n_op_res, cost);
189 n_res = count_result(irn);
190 cost = MAX(n_res, cost);
192 #if defined NORMAL_DBG
193 ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
200 static void normal_cost_walker(ir_node* irn, void* env)
204 #if defined NORMAL_DBG
205 ir_fprintf(stderr, "cost walking node %+F\n", irn);
207 if (is_Block(irn)) return;
208 if (!must_be_scheduled(irn)) return;
209 normal_tree_cost(irn);
213 static void collect_roots(ir_node* irn, void* env)
219 if (is_Block(irn)) return;
220 if (!must_be_scheduled(irn)) return;
222 is_root = be_is_Keep(irn) || !((flag_and_cost*)get_irn_link(irn))->no_root;
224 #if defined NORMAL_DBG
225 ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
229 ir_node* block = get_nodes_block(irn);
230 ir_node** roots = get_irn_link(block);
232 roots = NEW_ARR_F(ir_node*, 0);
234 ARR_APP1(ir_node*, roots, irn);
235 set_irn_link(block, roots);
240 static ir_node** sched_node(ir_node** sched, ir_node* irn)
242 ir_node* block = get_nodes_block(irn);
243 flag_and_cost* fc = get_irn_link(irn);
244 irn_cost_pair* irns = fc->costs;
245 int arity = get_irn_arity(irn);
248 if (irn_visited(irn)) return sched;
249 if (is_End(irn)) return sched;
251 if (!is_Phi(irn) && !be_is_Keep(irn)) {
252 for (i = 0; i < arity; ++i) {
253 ir_node* pred = irns[i].irn;
254 if (get_nodes_block(pred) != block) continue;
255 if (get_irn_mode(pred) == mode_M) continue;
256 if (is_Proj(pred)) pred = get_Proj_pred(pred);
257 sched = sched_node(sched, pred);
261 mark_irn_visited(irn);
262 ARR_APP1(ir_node*, sched, irn);
267 static int root_cmp(const void* a, const void* b)
269 const irn_cost_pair* const a1 = a;
270 const irn_cost_pair* const b1 = b;
272 if (is_irn_forking(a1->irn)) {
274 } else if (is_irn_forking(b1->irn)) {
277 ret = b1->cost - a1->cost;
279 /* place live-out nodes later */
280 ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
283 #if defined NORMAL_DBG
284 ir_fprintf(stderr, "%+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
290 static void normal_sched_block(ir_node* block, void* env)
292 heights_t* heights = env;
293 ir_node** roots = get_irn_link(block);
295 irn_cost_pair* root_costs;
299 #if defined NORMAL_DBG
300 ir_fprintf(stderr, "sched walking block %+F\n", block);
304 #if defined NORMAL_DBG
305 fprintf(stderr, "has no roots\n");
310 root_count = ARR_LEN(roots);
311 NEW_ARR_A(irn_cost_pair, root_costs, root_count);
312 for (i = 0; i < root_count; ++i) {
313 root_costs[i].irn = roots[i];
314 root_costs[i].cost = get_irn_height(heights, roots[i]);
315 #if defined NORMAL_DBG
316 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
319 qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
320 #if defined NORMAL_DBG
325 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
326 for (i = 0; i < n; ++i) {
327 ir_fprintf(stderr, " %+F\n", root_costs[i].irn);
329 fprintf(stderr, "\n");
333 sched = NEW_ARR_F(ir_node*, 0);
334 for (i = 0; i < root_count; ++i) {
335 ir_node* irn = root_costs[i].irn;
336 assert(must_be_scheduled(irn));
337 sched = sched_node(sched, irn);
339 set_irn_link(block, sched);
342 #if defined NORMAL_DBG
344 int n = ARR_LEN(sched);
347 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
348 for (i = 0; i < n; ++i) {
349 ir_fprintf(stderr, " %+F\n", sched[i]);
351 fprintf(stderr, "\n");
357 static void *normal_init_graph(const list_sched_selector_t *vtab,
358 const be_irg_t *birg)
360 ir_graph *irg = be_get_birg_irg(birg);
365 cur_arch_env = be_get_birg_arch_env(birg);
369 heights = heights_new(irg);
371 irg_walk_graph(irg, normal_cost_walker, NULL, NULL);
372 irg_walk_graph(irg, collect_roots, NULL, NULL);
373 inc_irg_visited(irg);
374 irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
376 heights_free(heights);
382 static void *normal_init_block(void *graph_env, ir_node *block)
391 const list_sched_selector_t normal_selector = {
395 NULL, /* to_appear_in_schedule */
396 NULL, /* node_ready */
397 NULL, /* node_selected */
400 NULL, /* finish_block */
401 NULL /* finish_graph */