- reserve the visited resource
[libfirm] / ir / be / beschednormal.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @brief   Use the strong normal form theorem (though it does not hold)
22  * @author  Christoph Mallon
23  * @version $Id$
24  */
25 #include "config.h"
26
27 #include <stdlib.h>
28
29 #include "besched_t.h"
30 #include "belistsched.h"
31 #include "belive_t.h"
32 #include "beutil.h"
33 #include "height.h"
34 #include "irtools.h"
35 #include "irgwalk.h"
36 #include "benode_t.h"
37 #include "array_t.h"
38
39 // XXX there is no one time init for schedulers
40 //#define NORMAL_DBG
41 #include "irprintf.h"
42
43
44 static int must_be_scheduled(const ir_node* const irn)
45 {
46         return !is_Proj(irn) && !is_Sync(irn);
47 }
48
49
50 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
51                               ir_nodeset_t *live_set)
52 {
53         ir_nodeset_iterator_t iter;
54         ir_node*  block;
55         ir_node*  irn;
56         ir_node** sched;
57         int sched_count;
58
59         (void)block_env;
60         (void)live_set;
61
62         ir_nodeset_iterator_init(&iter, ready_set);
63         irn = ir_nodeset_iterator_next(&iter);
64         block = get_nodes_block(irn);
65         sched = get_irn_link(block);
66         sched_count = ARR_LEN(sched);
67         for (; sched_count-- != 0; ++sched) {
68                 ir_node* irn = *sched;
69                 if (ir_nodeset_contains(ready_set, irn) &&
70                                 !arch_irn_class_is(irn, branch)) {
71 #if defined NORMAL_DBG
72                         ir_fprintf(stderr, "scheduling %+F\n", irn);
73 #endif
74                         return irn;
75                 }
76         }
77
78         return irn;
79 }
80
81
82 typedef struct irn_cost_pair {
83         ir_node* irn;
84         int      cost;
85 } irn_cost_pair;
86
87
88 static int cost_cmp(const void* a, const void* b)
89 {
90         const irn_cost_pair* const a1 = a;
91         const irn_cost_pair* const b1 = b;
92         int ret = b1->cost - a1->cost;
93         if (ret == 0)
94                 ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
95 #if defined NORMAL_DBG
96         ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
97 #endif
98         return ret;
99 }
100
101
102 typedef struct flag_and_cost {
103         int no_root;
104         irn_cost_pair costs[];
105 } flag_and_cost;
106
107
108 static int count_result(const ir_node* irn)
109 {
110         const ir_mode* mode = get_irn_mode(irn);
111
112         if (mode == mode_M || mode == mode_X)
113                 return 0;
114
115         if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
116                 return 0;
117
118         return 1;
119 }
120
121
122 /* TODO high cost for store trees
123  */
124
125 static int normal_tree_cost(ir_node* irn)
126 {
127         flag_and_cost* fc    = get_irn_link(irn);
128         ir_node*       block = get_nodes_block(irn);
129         int            arity = get_irn_arity(irn);
130         ir_node*       last;
131         int            n_res;
132         int            cost;
133         int            n_op_res = 0;
134         int            i;
135
136         if (be_is_Keep(irn))
137                 return 0;
138
139         if (is_Proj(irn)) {
140                 return normal_tree_cost(get_Proj_pred(irn));
141         }
142
143         if (fc == NULL) {
144                 irn_cost_pair* costs;
145                 int            i;
146
147                 fc = xmalloc(sizeof(*fc) + sizeof(*fc->costs) * arity);
148                 fc->no_root = 0;
149                 costs = fc->costs;
150
151                 for (i = 0; i < arity; ++i) {
152                         ir_node* pred = get_irn_n(irn, i);
153                         int cost;
154
155                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
156                                 cost = 0;
157                         } else if (get_nodes_block(pred) != block) {
158                                 cost = 1;
159                         } else {
160                                 flag_and_cost* pred_fc;
161                                 ir_node*       real_pred;
162
163                                 cost = normal_tree_cost(pred);
164                                 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
165                                 if (!arch_irn_is_ignore(pred)) {
166                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
167                                         pred_fc = get_irn_link(real_pred);
168                                         pred_fc->no_root = 1;
169 #if defined NORMAL_DBG
170                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
171 #endif
172                                 }
173                         }
174
175                         costs[i].irn  = pred;
176                         costs[i].cost = cost;
177                 }
178
179                 qsort(costs, arity, sizeof(*costs), cost_cmp);
180                 set_irn_link(irn, fc);
181         }
182
183         cost = 0;
184         last = 0;
185         for (i = 0; i < arity; ++i) {
186                 ir_node* op = fc->costs[i].irn;
187                 if (op == last)                 continue;
188                 if (get_irn_mode(op) == mode_M) continue;
189                 if (arch_irn_is_ignore(op))     continue;
190                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
191                 last = op;
192                 ++n_op_res;
193         }
194         n_res = count_result(irn);
195         cost = MAX(n_res, cost);
196
197 #if defined NORMAL_DBG
198         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
199 #endif
200
201         return cost;
202 }
203
204
205 static void normal_cost_walker(ir_node* irn, void* env)
206 {
207         (void)env;
208
209 #if defined NORMAL_DBG
210         ir_fprintf(stderr, "cost walking node %+F\n", irn);
211 #endif
212         if (is_Block(irn)) return;
213         if (!must_be_scheduled(irn)) return;
214         normal_tree_cost(irn);
215 }
216
217
218 static void collect_roots(ir_node* irn, void* env)
219 {
220         int is_root;
221
222         (void)env;
223
224         if (is_Block(irn)) return;
225         if (!must_be_scheduled(irn)) return;
226
227         is_root = be_is_Keep(irn) || !((flag_and_cost*)get_irn_link(irn))->no_root;
228
229 #if defined NORMAL_DBG
230         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
231 #endif
232
233         if (is_root) {
234                 ir_node* block = get_nodes_block(irn);
235                 ir_node** roots = get_irn_link(block);
236                 if (roots == NULL) {
237                         roots = NEW_ARR_F(ir_node*, 0);
238                 }
239                 ARR_APP1(ir_node*, roots, irn);
240                 set_irn_link(block, roots);
241         }
242 }
243
244
245 static ir_node** sched_node(ir_node** sched, ir_node* irn)
246 {
247         ir_node*       block = get_nodes_block(irn);
248         flag_and_cost* fc    = get_irn_link(irn);
249         irn_cost_pair* irns  = fc->costs;
250         int            arity = get_irn_arity(irn);
251         int            i;
252
253         if (irn_visited_else_mark(irn)) return sched;
254         if (is_End(irn))                return sched;
255
256         if (!is_Phi(irn) && !be_is_Keep(irn)) {
257                 for (i = 0; i < arity; ++i) {
258                         ir_node* pred = irns[i].irn;
259                         if (get_nodes_block(pred) != block) continue;
260                         if (get_irn_mode(pred) == mode_M) continue;
261                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
262                         sched = sched_node(sched, pred);
263                 }
264         }
265
266         ARR_APP1(ir_node*, sched, irn);
267         return sched;
268 }
269
270
271 static int root_cmp(const void* a, const void* b)
272 {
273         const irn_cost_pair* const a1 = a;
274         const irn_cost_pair* const b1 = b;
275         int ret;
276         if (is_irn_forking(a1->irn)) {
277                 ret = 1;
278         } else if (is_irn_forking(b1->irn)) {
279                 ret = -1;
280         } else {
281                 ret = b1->cost - a1->cost;
282                 if (ret == 0) {
283                         /* place live-out nodes later */
284                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
285                 }
286         }
287 #if defined NORMAL_DBG
288         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
289 #endif
290         return ret;
291 }
292
293
294 static void normal_sched_block(ir_node* block, void* env)
295 {
296         heights_t*     heights = env;
297         ir_node**      roots   = get_irn_link(block);
298         int            root_count;
299         irn_cost_pair* root_costs;
300         int i;
301         ir_node**      sched;
302
303 #if defined NORMAL_DBG
304         ir_fprintf(stderr, "sched walking block %+F\n", block);
305 #endif
306
307         if (roots == NULL) {
308 #if defined NORMAL_DBG
309                 fprintf(stderr, "has no roots\n");
310 #endif
311                 return;
312         }
313
314         root_count = ARR_LEN(roots);
315         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
316         for (i = 0; i < root_count; ++i) {
317                 root_costs[i].irn  = roots[i];
318                 root_costs[i].cost = get_irn_height(heights, roots[i]);
319 #if defined NORMAL_DBG
320                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
321 #endif
322         }
323         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
324 #if defined NORMAL_DBG
325         {
326                 int n = root_count;
327                 int i;
328
329                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
330                 for (i = 0; i < n; ++i) {
331                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
332                 }
333                 fprintf(stderr, "\n");
334         }
335 #endif
336
337         sched = NEW_ARR_F(ir_node*, 0);
338         for (i = 0; i < root_count; ++i) {
339                 ir_node* irn = root_costs[i].irn;
340                 assert(must_be_scheduled(irn));
341                 sched = sched_node(sched, irn);
342         }
343         set_irn_link(block, sched);
344         DEL_ARR_F(roots);
345
346 #if defined NORMAL_DBG
347         {
348                 int n = ARR_LEN(sched);
349                 int i;
350
351                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
352                 for (i = 0; i < n; ++i) {
353                         ir_fprintf(stderr, "  %+F\n", sched[i]);
354                 }
355                 fprintf(stderr, "\n");
356         }
357 #endif
358 }
359
360
361 static void *normal_init_graph(const list_sched_selector_t *vtab,
362                                const be_irg_t *birg)
363 {
364         ir_graph  *irg = be_get_birg_irg(birg);
365         heights_t *heights;
366
367         (void)vtab;
368
369         be_clear_links(irg);
370
371         heights = heights_new(irg);
372
373         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
374         irg_walk_graph(irg, normal_cost_walker,  NULL, NULL);
375         irg_walk_graph(irg, collect_roots, NULL, NULL);
376         inc_irg_visited(irg);
377         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
378         irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
379         ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
380
381         heights_free(heights);
382
383         return NULL;
384 }
385
386
387 static void *normal_init_block(void *graph_env, ir_node *block)
388 {
389         (void)graph_env;
390         (void)block;
391
392         return NULL;
393 }
394
395
396 const list_sched_selector_t normal_selector = {
397         normal_init_graph,
398         normal_init_block,
399         normal_select,
400         NULL,              /* to_appear_in_schedule */
401         NULL,              /* node_ready */
402         NULL,              /* node_selected */
403         NULL,              /* exectime */
404         NULL,              /* latency */
405         NULL,              /* finish_block */
406         NULL               /* finish_graph */
407 };