- fixed all memory leaks
[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 /** An instance of the normal scheduler. */
44 typedef struct instance_t {
45         ir_graph*      irg;          /**< the IR graph of this instance */
46         heights_t*     heights;      /**< height for the graph of this instance */
47         struct obstack obst;         /**< obstack for temporary data */
48         ir_node***     block_lists;  /**< list of all block scheduling 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                               ir_nodeset_t *live_set)
59 {
60         ir_nodeset_iterator_t iter;
61         ir_node*  block;
62         ir_node*  irn;
63         ir_node** sched;
64         int sched_count;
65
66         (void)block_env;
67         (void)live_set;
68
69         ir_nodeset_iterator_init(&iter, ready_set);
70         irn = ir_nodeset_iterator_next(&iter);
71         block = get_nodes_block(irn);
72         sched = get_irn_link(block);
73         sched_count = ARR_LEN(sched);
74         for (; sched_count-- != 0; ++sched) {
75                 ir_node* irn = *sched;
76                 if (ir_nodeset_contains(ready_set, irn) &&
77                                 !arch_irn_class_is(irn, branch)) {
78 #if defined NORMAL_DBG
79                         ir_fprintf(stderr, "scheduling %+F\n", irn);
80 #endif
81                         return irn;
82                 }
83         }
84
85         return irn;
86 }
87
88
89 typedef struct irn_cost_pair {
90         ir_node* irn;
91         int      cost;
92 } irn_cost_pair;
93
94 static int cost_cmp(const void* a, const void* b)
95 {
96         const irn_cost_pair* const a1 = a;
97         const irn_cost_pair* const b1 = b;
98         int ret = b1->cost - a1->cost;
99         if (ret == 0)
100                 ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
101 #if defined NORMAL_DBG
102         ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
103 #endif
104         return ret;
105 }
106
107
108 typedef struct flag_and_cost {
109         int no_root;
110         irn_cost_pair costs[];
111 } flag_and_cost;
112
113 #define get_irn_fc(irn)     ((flag_and_cost*)get_irn_link(irn))
114 #define set_irn_fc(irn, fc) set_irn_link(irn, fc)
115
116
117 static int count_result(const ir_node* irn)
118 {
119         const ir_mode* mode = get_irn_mode(irn);
120
121         if (mode == mode_M || mode == mode_X)
122                 return 0;
123
124         if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
125                 return 0;
126
127         return 1;
128 }
129
130
131 /* TODO high cost for store trees
132  */
133
134 static int normal_tree_cost(ir_node* irn, instance_t *inst)
135 {
136         flag_and_cost* fc;
137         int            arity;
138         ir_node*       last;
139         int            n_res;
140         int            cost;
141         int            n_op_res = 0;
142         int            i;
143
144         if (be_is_Keep(irn))
145                 return 0;
146
147         if (is_Proj(irn)) {
148                 return normal_tree_cost(get_Proj_pred(irn), inst);
149         }
150
151         arity = get_irn_arity(irn);
152         fc    = get_irn_fc(irn);
153
154         if (fc == NULL) {
155                 irn_cost_pair* costs;
156                 int            i;
157                 ir_node*       block = get_nodes_block(irn);
158
159                 fc = obstack_alloc(&inst->obst, sizeof(*fc) + sizeof(*fc->costs) * arity);
160                 fc->no_root = 0;
161                 costs = fc->costs;
162
163                 for (i = 0; i < arity; ++i) {
164                         ir_node* pred = get_irn_n(irn, i);
165                         int cost;
166
167                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
168                                 cost = 0;
169                         } else if (get_nodes_block(pred) != block) {
170                                 cost = 1;
171                         } else {
172                                 flag_and_cost* pred_fc;
173                                 ir_node*       real_pred;
174
175                                 cost = normal_tree_cost(pred, inst);
176                                 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
177                                 if (!arch_irn_is_ignore(pred)) {
178                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
179                                         pred_fc = get_irn_fc(real_pred);
180                                         pred_fc->no_root = 1;
181 #if defined NORMAL_DBG
182                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
183 #endif
184                                 }
185                         }
186
187                         costs[i].irn  = pred;
188                         costs[i].cost = cost;
189                 }
190
191                 qsort(costs, arity, sizeof(*costs), cost_cmp);
192                 set_irn_link(irn, fc);
193         }
194
195         cost = 0;
196         last = 0;
197         for (i = 0; i < arity; ++i) {
198                 ir_node* op = fc->costs[i].irn;
199                 if (op == last)                 continue;
200                 if (get_irn_mode(op) == mode_M) continue;
201                 if (arch_irn_is_ignore(op))     continue;
202                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
203                 last = op;
204                 ++n_op_res;
205         }
206         n_res = count_result(irn);
207         cost = MAX(n_res, cost);
208
209 #if defined NORMAL_DBG
210         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
211 #endif
212
213         return cost;
214 }
215
216
217 static void normal_cost_walker(ir_node* irn, void* env)
218 {
219         instance_t *inst = env;
220
221 #if defined NORMAL_DBG
222         ir_fprintf(stderr, "cost walking node %+F\n", irn);
223 #endif
224         if (is_Block(irn)) return;
225         if (!must_be_scheduled(irn)) return;
226         normal_tree_cost(irn, inst);
227 }
228
229
230 static void collect_roots(ir_node* irn, void* env)
231 {
232         int is_root;
233
234         (void)env;
235
236         if (is_Block(irn)) return;
237         if (!must_be_scheduled(irn)) return;
238
239         is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
240
241 #if defined NORMAL_DBG
242         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
243 #endif
244
245         if (is_root) {
246                 ir_node* block = get_nodes_block(irn);
247                 ir_node** roots = get_irn_link(block);
248                 if (roots == NULL) {
249                         roots = NEW_ARR_F(ir_node*, 0);
250                 }
251                 ARR_APP1(ir_node*, roots, irn);
252                 set_irn_link(block, roots);
253         }
254 }
255
256
257 static ir_node** sched_node(ir_node** sched, ir_node* irn)
258 {
259         if (irn_visited_else_mark(irn)) return sched;
260         if (is_End(irn))                return sched;
261
262         if (!is_Phi(irn) && !be_is_Keep(irn)) {
263                 ir_node*       block = get_nodes_block(irn);
264                 int            arity = get_irn_arity(irn);
265                 flag_and_cost* fc    = get_irn_fc(irn);
266                 irn_cost_pair* irns  = fc->costs;
267                 int            i;
268
269                 for (i = 0; i < arity; ++i) {
270                         ir_node* pred = irns[i].irn;
271                         if (get_nodes_block(pred) != block) continue;
272                         if (get_irn_mode(pred) == mode_M) continue;
273                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
274                         sched = sched_node(sched, pred);
275                 }
276         }
277
278         ARR_APP1(ir_node*, sched, irn);
279         return sched;
280 }
281
282
283 static int root_cmp(const void* a, const void* b)
284 {
285         const irn_cost_pair* const a1 = a;
286         const irn_cost_pair* const b1 = b;
287         int ret;
288         if (is_irn_forking(a1->irn)) {
289                 ret = 1;
290         } else if (is_irn_forking(b1->irn)) {
291                 ret = -1;
292         } else {
293                 ret = b1->cost - a1->cost;
294                 if (ret == 0) {
295                         /* place live-out nodes later */
296                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
297                 }
298         }
299 #if defined NORMAL_DBG
300         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
301 #endif
302         return ret;
303 }
304
305
306 static void normal_sched_block(ir_node* block, void* env)
307 {
308         instance_t*    inst  = env;
309         ir_node**      roots = get_irn_link(block);
310         heights_t*     heights;
311         int            root_count;
312         irn_cost_pair* root_costs;
313         int i;
314         ir_node**      sched;
315
316 #if defined NORMAL_DBG
317         ir_fprintf(stderr, "sched walking block %+F\n", block);
318 #endif
319
320         if (roots == NULL) {
321 #if defined NORMAL_DBG
322                 fprintf(stderr, "has no roots\n");
323 #endif
324                 return;
325         }
326
327         heights    = inst->heights;
328         root_count = ARR_LEN(roots);
329         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
330         for (i = 0; i < root_count; ++i) {
331                 root_costs[i].irn  = roots[i];
332                 root_costs[i].cost = get_irn_height(heights, roots[i]);
333 #if defined NORMAL_DBG
334                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
335 #endif
336         }
337         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
338 #if defined NORMAL_DBG
339         {
340                 int n = root_count;
341                 int i;
342
343                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
344                 for (i = 0; i < n; ++i) {
345                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
346                 }
347                 fprintf(stderr, "\n");
348         }
349 #endif
350
351         sched = NEW_ARR_F(ir_node*, 0);
352         for (i = 0; i < root_count; ++i) {
353                 ir_node* irn = root_costs[i].irn;
354                 assert(must_be_scheduled(irn));
355                 sched = sched_node(sched, irn);
356         }
357         DEL_ARR_F(roots);
358         set_irn_link(block, sched);
359         ARR_APP1(ir_node**, inst->block_lists, sched);
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(const list_sched_selector_t *vtab,
377                                const be_irg_t *birg)
378 {
379         instance_t *inst = XMALLOC(instance_t);
380         ir_graph   *irg = be_get_birg_irg(birg);
381
382         (void)vtab;
383
384         be_clear_links(irg);
385
386         obstack_init(&inst->obst);
387         inst->irg         = irg;
388         inst->heights     = heights_new(irg);
389         inst->block_lists = NEW_ARR_F(ir_node**, 0);
390
391         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
392         irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
393         irg_walk_graph(irg, collect_roots, NULL, NULL);
394         inc_irg_visited(irg);
395         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
396         irg_block_walk_graph(irg, normal_sched_block, NULL, inst);
397         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
398
399         heights_free(inst->heights);
400
401         return inst;
402 }
403
404 void normal_finish_graph(void *env)
405 {
406         instance_t *inst = env;
407         int        i;
408
409         for (i = ARR_LEN(inst->block_lists) - 1; i >= 0; --i) {
410                 DEL_ARR_F(inst->block_lists[i]);
411         }
412         DEL_ARR_F(inst->block_lists);
413
414         /* block uses the link field to store the schedule */
415         ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
416         obstack_free(&inst->obst, NULL);
417         xfree(inst);
418 }
419
420 const list_sched_selector_t normal_selector = {
421         normal_init_graph,
422         NULL,              /* init_block */
423         normal_select,
424         NULL,              /* to_appear_in_schedule */
425         NULL,              /* node_ready */
426         NULL,              /* node_selected */
427         NULL,              /* exectime */
428         NULL,              /* latency */
429         NULL,              /* finish_block */
430         normal_finish_graph
431 };