bestack: Fetch the start block only once.
[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  */
24 #include "config.h"
25
26 #include <stdlib.h>
27
28 #include "besched.h"
29 #include "belistsched.h"
30 #include "belive_t.h"
31 #include "beutil.h"
32 #include "heights.h"
33 #include "irgwalk.h"
34 #include "benode.h"
35 #include "bemodule.h"
36 #include "util.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 /** 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 */
48 } instance_t;
49
50 static int must_be_scheduled(const ir_node* const irn)
51 {
52         return !is_Proj(irn) && !arch_irn_is(irn, not_scheduled);
53 }
54
55
56 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set)
57 {
58         instance_t* inst = (instance_t*)block_env;
59         ir_node*    irn;
60         ir_node*    next;
61         ir_node*    last = NULL;
62
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);
68 #endif
69                         if (last == NULL)
70                                 inst->curr_list = next;
71                         else
72                                 set_irn_link(last, next);
73                         return irn;
74                 }
75         }
76
77         return ir_nodeset_first(ready_set);
78 }
79
80
81 typedef struct irn_cost_pair {
82         ir_node* irn;
83         int      cost;
84 } irn_cost_pair;
85
86 static int cost_cmp(const void* a, const void* b)
87 {
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;
91         if (ret == 0)
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);
95 #endif
96         return ret;
97 }
98
99
100 typedef struct flag_and_cost {
101         int no_root;
102         irn_cost_pair costs[];
103 } flag_and_cost;
104
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)
107
108
109 static int count_result(const ir_node* irn)
110 {
111         const ir_mode* mode = get_irn_mode(irn);
112
113         if (mode == mode_M || mode == mode_X)
114                 return 0;
115
116         if (mode == mode_T)
117                 return 1;
118
119         arch_register_req_t const *const req = arch_get_irn_register_req(irn);
120         if (arch_register_req_is(req, ignore))
121                 return 0;
122
123         return 1;
124 }
125
126
127 /* TODO high cost for store trees
128  */
129
130 static int normal_tree_cost(ir_node* irn, instance_t *inst)
131 {
132         flag_and_cost* fc;
133         int            arity;
134         ir_node*       last;
135         int            n_res;
136         int            cost;
137         int            n_op_res = 0;
138         int            i;
139
140         if (be_is_Keep(irn))
141                 return 0;
142
143         if (is_Proj(irn)) {
144                 return normal_tree_cost(get_Proj_pred(irn), inst);
145         }
146
147         arity = get_irn_arity(irn);
148         fc    = get_irn_fc(irn);
149
150         if (fc == NULL) {
151                 irn_cost_pair* costs;
152                 ir_node*       block = get_nodes_block(irn);
153
154                 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
155                 fc->no_root = 0;
156                 costs = fc->costs;
157
158                 for (i = 0; i < arity; ++i) {
159                         ir_node* pred = get_irn_n(irn, i);
160
161                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M) {
162                                 cost = 0;
163                         } else if (get_nodes_block(pred) != block) {
164                                 cost = 1;
165                         } else {
166                                 flag_and_cost* pred_fc;
167                                 ir_node*       real_pred;
168
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);
176 #endif
177                                 }
178                         }
179
180                         costs[i].irn  = pred;
181                         costs[i].cost = cost;
182                 }
183
184                 qsort(costs, arity, sizeof(*costs), cost_cmp);
185                 set_irn_link(irn, fc);
186         }
187
188         cost = 0;
189         last = 0;
190         for (i = 0; i < arity; ++i) {
191                 ir_node* op = fc->costs[i].irn;
192                 ir_mode* mode;
193                 if (op == last)
194                         continue;
195                 mode = get_irn_mode(op);
196                 if (mode == mode_M)
197                         continue;
198                 if (arch_irn_is_ignore(op))
199                         continue;
200                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
201                 last = op;
202                 ++n_op_res;
203         }
204         n_res = count_result(irn);
205         cost = MAX(n_res, cost);
206
207 #if defined NORMAL_DBG
208         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
209 #endif
210
211         return cost;
212 }
213
214
215 static void normal_cost_walker(ir_node* irn, void* env)
216 {
217         instance_t *inst = (instance_t*)env;
218
219 #if defined NORMAL_DBG
220         ir_fprintf(stderr, "cost walking node %+F\n", irn);
221 #endif
222         if (is_Block(irn)) {
223                 ir_node **const roots = NEW_ARR_F(ir_node*, 0);
224                 set_irn_link(irn, roots);
225                 return;
226         }
227         if (!must_be_scheduled(irn)) return;
228         normal_tree_cost(irn, inst);
229 }
230
231
232 static void collect_roots(ir_node* irn, void* env)
233 {
234         int is_root;
235
236         (void)env;
237
238         if (!must_be_scheduled(irn)) return;
239
240         is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
241
242 #if defined NORMAL_DBG
243         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
244 #endif
245
246         if (is_root) {
247                 ir_node* block = get_nodes_block(irn);
248                 ir_node** roots = (ir_node**)get_irn_link(block);
249                 ARR_APP1(ir_node*, roots, irn);
250                 set_irn_link(block, roots);
251         }
252 }
253
254
255 static ir_node** sched_node(ir_node** sched, ir_node* irn)
256 {
257         if (irn_visited_else_mark(irn)) return sched;
258
259         if (!is_Phi(irn) && !be_is_Keep(irn)) {
260                 ir_node*       block = get_nodes_block(irn);
261                 int            arity = get_irn_arity(irn);
262                 flag_and_cost* fc    = get_irn_fc(irn);
263                 irn_cost_pair* irns  = fc->costs;
264                 int            i;
265
266                 for (i = 0; i < arity; ++i) {
267                         ir_node* pred = irns[i].irn;
268                         if (get_nodes_block(pred) != block) continue;
269                         if (get_irn_mode(pred) == mode_M) continue;
270                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
271                         sched = sched_node(sched, pred);
272                 }
273         }
274
275         ARR_APP1(ir_node*, sched, irn);
276         return sched;
277 }
278
279
280 static int root_cmp(const void* a, const void* b)
281 {
282         const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
283         const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
284         int ret;
285         if (is_irn_forking(a1->irn) && !is_irn_forking(b1->irn)) {
286                 ret = 1;
287         } else if (is_irn_forking(b1->irn) && !is_irn_forking(a1->irn)) {
288                 ret = -1;
289         } else {
290                 ret = b1->cost - a1->cost;
291                 if (ret == 0) {
292                         /* place live-out nodes later */
293                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
294                         if (ret == 0) {
295                                 /* compare node idx */
296                                 ret = get_irn_idx(a1->irn) - get_irn_idx(b1->irn);
297                         }
298                 }
299         }
300 #if defined NORMAL_DBG
301         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
302 #endif
303         return ret;
304 }
305
306
307 static void normal_sched_block(ir_node* block, void* env)
308 {
309         ir_node**      roots = (ir_node**)get_irn_link(block);
310         ir_heights_t*  heights = (ir_heights_t*)env;
311         irn_cost_pair* root_costs;
312         int i;
313         ir_node**      sched;
314
315 #if defined NORMAL_DBG
316         ir_fprintf(stderr, "sched walking block %+F\n", block);
317 #endif
318
319         int const root_count = ARR_LEN(roots);
320         if (root_count == 0) {
321 #if defined NORMAL_DBG
322                 fprintf(stderr, "has no roots\n");
323 #endif
324                 return;
325         }
326
327         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
328         for (i = 0; i < root_count; ++i) {
329                 root_costs[i].irn  = roots[i];
330                 root_costs[i].cost = get_irn_height(heights, roots[i]);
331 #if defined NORMAL_DBG
332                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
333 #endif
334         }
335         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
336 #if defined NORMAL_DBG
337         {
338                 int n = root_count;
339                 int i;
340
341                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
342                 for (i = 0; i < n; ++i) {
343                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
344                 }
345                 fprintf(stderr, "\n");
346         }
347 #endif
348
349         sched = NEW_ARR_F(ir_node*, 0);
350         for (i = 0; i < root_count; ++i) {
351                 ir_node* irn = root_costs[i].irn;
352                 assert(must_be_scheduled(irn));
353                 sched = sched_node(sched, irn);
354         }
355         set_irn_link(block, sched);
356         DEL_ARR_F(roots);
357
358 #if defined NORMAL_DBG
359         {
360                 int n = ARR_LEN(sched);
361                 int i;
362
363                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
364                 for (i = 0; i < n; ++i) {
365                         ir_fprintf(stderr, "  %+F\n", sched[i]);
366                 }
367                 fprintf(stderr, "\n");
368         }
369 #endif
370 }
371
372
373 static void *normal_init_graph(ir_graph *irg)
374 {
375         instance_t   *inst = XMALLOC(instance_t);
376         ir_heights_t *heights;
377
378         be_clear_links(irg);
379
380         obstack_init(&inst->obst);
381         inst->irg         = irg;
382
383         heights = heights_new(irg);
384
385         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
386         irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
387         irg_walk_graph(irg, collect_roots, NULL, NULL);
388         inc_irg_visited(irg);
389         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
390         irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
391         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
392
393         heights_free(heights);
394
395         return inst;
396 }
397
398 static void *normal_init_block(void *graph_env, ir_node *block)
399 {
400         instance_t* inst  = (instance_t*)graph_env;
401         ir_node**   sched = (ir_node**)get_irn_link(block);
402         ir_node*    first = NULL;
403         int         i;
404
405         /* turn into a list, so we can easily remove nodes.
406            The link field is used anyway. */
407         for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
408                 ir_node* irn = sched[i];
409                 if (!is_cfop(irn)) {
410                         set_irn_link(irn, first);
411                         first = irn;
412                 }
413         }
414         /* note: we can free sched here, there should be no attempt to schedule
415            a block twice */
416         DEL_ARR_F(sched);
417         set_irn_link(block, sched);
418         inst->curr_list = first;
419         return inst;
420 }
421
422 static void normal_finish_graph(void *env)
423 {
424         instance_t *inst = (instance_t*)env;
425
426         /* block uses the link field to store the schedule */
427         ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
428         obstack_free(&inst->obst, NULL);
429         xfree(inst);
430 }
431
432 static void sched_normal(ir_graph *irg)
433 {
434         static const list_sched_selector_t normal_selector = {
435                 normal_init_graph,
436                 normal_init_block,
437                 normal_select,
438                 NULL,              /* node_ready */
439                 NULL,              /* node_selected */
440                 NULL,              /* finish_block */
441                 normal_finish_graph
442         };
443         be_list_sched_graph(irg, &normal_selector);
444 }
445
446 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal)
447 void be_init_sched_normal(void)
448 {
449         be_register_scheduler("normal", sched_normal);
450 }