Another rewrite of prolog/epilog handling: Delay their creation until after register...
[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.h"
30 #include "belistsched.h"
31 #include "belive_t.h"
32 #include "beutil.h"
33 #include "heights.h"
34 #include "irtools.h"
35 #include "irgwalk.h"
36 #include "benode.h"
37 #include "bemodule.h"
38 #include "array_t.h"
39
40 // XXX there is no one time init for schedulers
41 //#define NORMAL_DBG
42 #include "irprintf.h"
43
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 */
49 } instance_t;
50
51 static int must_be_scheduled(const ir_node* const irn)
52 {
53         return !is_Proj(irn) && !is_Sync(irn);
54 }
55
56
57 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set)
58 {
59         instance_t* inst = (instance_t*)block_env;
60         ir_node*    irn;
61         ir_node*    next;
62         ir_node*    last = NULL;
63         ir_nodeset_iterator_t iter;
64
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);
70 #endif
71                         if (last == NULL)
72                                 inst->curr_list = next;
73                         else
74                                 set_irn_link(last, next);
75                         return irn;
76                 }
77         }
78
79         ir_nodeset_iterator_init(&iter, ready_set);
80         irn = ir_nodeset_iterator_next(&iter);
81         return irn;
82 }
83
84
85 typedef struct irn_cost_pair {
86         ir_node* irn;
87         int      cost;
88 } irn_cost_pair;
89
90 static int cost_cmp(const void* a, const void* b)
91 {
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;
95         if (ret == 0)
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);
99 #endif
100         return ret;
101 }
102
103
104 typedef struct flag_and_cost {
105         int no_root;
106         irn_cost_pair costs[];
107 } flag_and_cost;
108
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)
111
112
113 static int count_result(const ir_node* irn)
114 {
115         const ir_mode* mode = get_irn_mode(irn);
116
117         if (mode == mode_M || mode == mode_X)
118                 return 0;
119
120         if (mode == mode_T)
121                 return 1;
122
123         if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
124                 return 0;
125
126         return 1;
127 }
128
129
130 /* TODO high cost for store trees
131  */
132
133 static int normal_tree_cost(ir_node* irn, instance_t *inst)
134 {
135         flag_and_cost* fc;
136         int            arity;
137         ir_node*       last;
138         int            n_res;
139         int            cost;
140         int            n_op_res = 0;
141         int            i;
142
143         if (be_is_Keep(irn))
144                 return 0;
145
146         if (is_Proj(irn)) {
147                 return normal_tree_cost(get_Proj_pred(irn), inst);
148         }
149
150         arity = get_irn_arity(irn);
151         fc    = get_irn_fc(irn);
152
153         if (fc == NULL) {
154                 irn_cost_pair* costs;
155                 int            i;
156                 ir_node*       block = get_nodes_block(irn);
157
158                 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
159                 fc->no_root = 0;
160                 costs = fc->costs;
161
162                 for (i = 0; i < arity; ++i) {
163                         ir_node* pred = get_irn_n(irn, i);
164                         int cost;
165
166                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
167                                 cost = 0;
168                         } else if (get_nodes_block(pred) != block) {
169                                 cost = 1;
170                         } else {
171                                 flag_and_cost* pred_fc;
172                                 ir_node*       real_pred;
173
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);
181 #endif
182                                 }
183                         }
184
185                         costs[i].irn  = pred;
186                         costs[i].cost = cost;
187                 }
188
189                 qsort(costs, arity, sizeof(*costs), cost_cmp);
190                 set_irn_link(irn, fc);
191         }
192
193         cost = 0;
194         last = 0;
195         for (i = 0; i < arity; ++i) {
196                 ir_node* op = fc->costs[i].irn;
197                 ir_mode* mode;
198                 if (op == last)
199                         continue;
200                 mode = get_irn_mode(op);
201                 if (mode == mode_M)
202                         continue;
203                 if (mode != mode_T && arch_irn_is_ignore(op))
204                         continue;
205                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
206                 last = op;
207                 ++n_op_res;
208         }
209         n_res = count_result(irn);
210         cost = MAX(n_res, cost);
211
212 #if defined NORMAL_DBG
213         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
214 #endif
215
216         return cost;
217 }
218
219
220 static void normal_cost_walker(ir_node* irn, void* env)
221 {
222         instance_t *inst = (instance_t*)env;
223
224 #if defined NORMAL_DBG
225         ir_fprintf(stderr, "cost walking node %+F\n", irn);
226 #endif
227         if (is_Block(irn)) return;
228         if (!must_be_scheduled(irn)) return;
229         normal_tree_cost(irn, inst);
230 }
231
232
233 static void collect_roots(ir_node* irn, void* env)
234 {
235         int is_root;
236
237         (void)env;
238
239         if (is_Block(irn)) return;
240         if (!must_be_scheduled(irn)) return;
241
242         is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
243
244 #if defined NORMAL_DBG
245         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
246 #endif
247
248         if (is_root) {
249                 ir_node* block = get_nodes_block(irn);
250                 ir_node** roots = (ir_node**)get_irn_link(block);
251                 if (roots == NULL) {
252                         roots = NEW_ARR_F(ir_node*, 0);
253                 }
254                 ARR_APP1(ir_node*, roots, irn);
255                 set_irn_link(block, roots);
256         }
257 }
258
259
260 static ir_node** sched_node(ir_node** sched, ir_node* irn)
261 {
262         if (irn_visited_else_mark(irn)) return sched;
263         if (is_End(irn))                return sched;
264
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;
270                 int            i;
271
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);
278                 }
279         }
280
281         ARR_APP1(ir_node*, sched, irn);
282         return sched;
283 }
284
285
286 static int root_cmp(const void* a, const void* b)
287 {
288         const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
289         const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
290         int ret;
291         if (is_irn_forking(a1->irn)) {
292                 ret = 1;
293         } else if (is_irn_forking(b1->irn)) {
294                 ret = -1;
295         } else {
296                 ret = b1->cost - a1->cost;
297                 if (ret == 0) {
298                         /* place live-out nodes later */
299                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
300                 }
301         }
302 #if defined NORMAL_DBG
303         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
304 #endif
305         return ret;
306 }
307
308
309 static void normal_sched_block(ir_node* block, void* env)
310 {
311         ir_node**      roots = (ir_node**)get_irn_link(block);
312         ir_heights_t*  heights = (ir_heights_t*)env;
313         int            root_count;
314         irn_cost_pair* root_costs;
315         int i;
316         ir_node**      sched;
317
318 #if defined NORMAL_DBG
319         ir_fprintf(stderr, "sched walking block %+F\n", block);
320 #endif
321
322         if (roots == NULL) {
323 #if defined NORMAL_DBG
324                 fprintf(stderr, "has no roots\n");
325 #endif
326                 return;
327         }
328
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);
336 #endif
337         }
338         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
339 #if defined NORMAL_DBG
340         {
341                 int n = root_count;
342                 int i;
343
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);
347                 }
348                 fprintf(stderr, "\n");
349         }
350 #endif
351
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);
357         }
358         set_irn_link(block, sched);
359         DEL_ARR_F(roots);
360
361 #if defined NORMAL_DBG
362         {
363                 int n = ARR_LEN(sched);
364                 int i;
365
366                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
367                 for (i = 0; i < n; ++i) {
368                         ir_fprintf(stderr, "  %+F\n", sched[i]);
369                 }
370                 fprintf(stderr, "\n");
371         }
372 #endif
373 }
374
375
376 static void *normal_init_graph(ir_graph *irg)
377 {
378         instance_t   *inst = XMALLOC(instance_t);
379         ir_heights_t *heights;
380
381         be_clear_links(irg);
382
383         obstack_init(&inst->obst);
384         inst->irg         = irg;
385
386         heights = heights_new(irg);
387
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);
395
396         heights_free(heights);
397
398         return inst;
399 }
400
401 static void *normal_init_block(void *graph_env, ir_node *block)
402 {
403         instance_t* inst  = (instance_t*)graph_env;
404         ir_node**   sched = (ir_node**)get_irn_link(block);
405         ir_node*    first = NULL;
406         int         i;
407
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];
412                 if (!is_cfop(irn)) {
413                         set_irn_link(irn, first);
414                         first = irn;
415                 }
416         }
417         /* note: we can free sched here, there should be no attempt to schedule
418            a block twice */
419         DEL_ARR_F(sched);
420         set_irn_link(block, sched);
421         inst->curr_list = first;
422         return inst;
423 }
424
425 static void normal_finish_graph(void *env)
426 {
427         instance_t *inst = (instance_t*)env;
428
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);
432         xfree(inst);
433 }
434
435 static void sched_normal(ir_graph *irg)
436 {
437         static const list_sched_selector_t normal_selector = {
438                 normal_init_graph,
439                 normal_init_block,
440                 normal_select,
441                 NULL,              /* node_ready */
442                 NULL,              /* node_selected */
443                 NULL,              /* finish_block */
444                 normal_finish_graph
445         };
446         be_list_sched_graph(irg, &normal_selector);
447 }
448
449 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal);
450 void be_init_sched_normal(void)
451 {
452         be_register_scheduler("normal", sched_normal);
453 }