fix backend nodes not copying flags correctly
[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 (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
176                                 if (!arch_irn_is_ignore(pred)) {
177                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
178                                         pred_fc = get_irn_fc(real_pred);
179                                         pred_fc->no_root = 1;
180 #if defined NORMAL_DBG
181                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
182 #endif
183                                 }
184                         }
185
186                         costs[i].irn  = pred;
187                         costs[i].cost = cost;
188                 }
189
190                 qsort(costs, arity, sizeof(*costs), cost_cmp);
191                 set_irn_link(irn, fc);
192         }
193
194         cost = 0;
195         last = 0;
196         for (i = 0; i < arity; ++i) {
197                 ir_node* op = fc->costs[i].irn;
198                 ir_mode* mode;
199                 if (op == last)
200                         continue;
201                 mode = get_irn_mode(op);
202                 if (mode == mode_M)
203                         continue;
204                 if (mode != mode_T && arch_irn_is_ignore(op))
205                         continue;
206                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
207                 last = op;
208                 ++n_op_res;
209         }
210         n_res = count_result(irn);
211         cost = MAX(n_res, cost);
212
213 #if defined NORMAL_DBG
214         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
215 #endif
216
217         return cost;
218 }
219
220
221 static void normal_cost_walker(ir_node* irn, void* env)
222 {
223         instance_t *inst = (instance_t*)env;
224
225 #if defined NORMAL_DBG
226         ir_fprintf(stderr, "cost walking node %+F\n", irn);
227 #endif
228         if (is_Block(irn)) return;
229         if (!must_be_scheduled(irn)) return;
230         normal_tree_cost(irn, inst);
231 }
232
233
234 static void collect_roots(ir_node* irn, void* env)
235 {
236         int is_root;
237
238         (void)env;
239
240         if (is_Block(irn)) return;
241         if (!must_be_scheduled(irn)) return;
242
243         is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
244
245 #if defined NORMAL_DBG
246         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
247 #endif
248
249         if (is_root) {
250                 ir_node* block = get_nodes_block(irn);
251                 ir_node** roots = (ir_node**)get_irn_link(block);
252                 if (roots == NULL) {
253                         roots = NEW_ARR_F(ir_node*, 0);
254                 }
255                 ARR_APP1(ir_node*, roots, irn);
256                 set_irn_link(block, roots);
257         }
258 }
259
260
261 static ir_node** sched_node(ir_node** sched, ir_node* irn)
262 {
263         if (irn_visited_else_mark(irn)) return sched;
264         if (is_End(irn))                return sched;
265
266         if (!is_Phi(irn) && !be_is_Keep(irn)) {
267                 ir_node*       block = get_nodes_block(irn);
268                 int            arity = get_irn_arity(irn);
269                 flag_and_cost* fc    = get_irn_fc(irn);
270                 irn_cost_pair* irns  = fc->costs;
271                 int            i;
272
273                 for (i = 0; i < arity; ++i) {
274                         ir_node* pred = irns[i].irn;
275                         if (get_nodes_block(pred) != block) continue;
276                         if (get_irn_mode(pred) == mode_M) continue;
277                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
278                         sched = sched_node(sched, pred);
279                 }
280         }
281
282         ARR_APP1(ir_node*, sched, irn);
283         return sched;
284 }
285
286
287 static int root_cmp(const void* a, const void* b)
288 {
289         const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
290         const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
291         int ret;
292         if (is_irn_forking(a1->irn)) {
293                 ret = 1;
294         } else if (is_irn_forking(b1->irn)) {
295                 ret = -1;
296         } else {
297                 ret = b1->cost - a1->cost;
298                 if (ret == 0) {
299                         /* place live-out nodes later */
300                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
301                 }
302         }
303 #if defined NORMAL_DBG
304         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
305 #endif
306         return ret;
307 }
308
309
310 static void normal_sched_block(ir_node* block, void* env)
311 {
312         ir_node**      roots = (ir_node**)get_irn_link(block);
313         ir_heights_t*  heights = (ir_heights_t*)env;
314         int            root_count;
315         irn_cost_pair* root_costs;
316         int i;
317         ir_node**      sched;
318
319 #if defined NORMAL_DBG
320         ir_fprintf(stderr, "sched walking block %+F\n", block);
321 #endif
322
323         if (roots == NULL) {
324 #if defined NORMAL_DBG
325                 fprintf(stderr, "has no roots\n");
326 #endif
327                 return;
328         }
329
330         root_count = ARR_LEN(roots);
331         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
332         for (i = 0; i < root_count; ++i) {
333                 root_costs[i].irn  = roots[i];
334                 root_costs[i].cost = get_irn_height(heights, roots[i]);
335 #if defined NORMAL_DBG
336                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
337 #endif
338         }
339         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
340 #if defined NORMAL_DBG
341         {
342                 int n = root_count;
343                 int i;
344
345                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
346                 for (i = 0; i < n; ++i) {
347                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
348                 }
349                 fprintf(stderr, "\n");
350         }
351 #endif
352
353         sched = NEW_ARR_F(ir_node*, 0);
354         for (i = 0; i < root_count; ++i) {
355                 ir_node* irn = root_costs[i].irn;
356                 assert(must_be_scheduled(irn));
357                 sched = sched_node(sched, irn);
358         }
359         set_irn_link(block, sched);
360         DEL_ARR_F(roots);
361
362 #if defined NORMAL_DBG
363         {
364                 int n = ARR_LEN(sched);
365                 int i;
366
367                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
368                 for (i = 0; i < n; ++i) {
369                         ir_fprintf(stderr, "  %+F\n", sched[i]);
370                 }
371                 fprintf(stderr, "\n");
372         }
373 #endif
374 }
375
376
377 static void *normal_init_graph(ir_graph *irg)
378 {
379         instance_t   *inst = XMALLOC(instance_t);
380         ir_heights_t *heights;
381
382         be_clear_links(irg);
383
384         obstack_init(&inst->obst);
385         inst->irg         = irg;
386
387         heights = heights_new(irg);
388
389         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
390         irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
391         irg_walk_graph(irg, collect_roots, NULL, NULL);
392         inc_irg_visited(irg);
393         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
394         irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
395         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
396
397         heights_free(heights);
398
399         return inst;
400 }
401
402 static void *normal_init_block(void *graph_env, ir_node *block)
403 {
404         instance_t* inst  = (instance_t*)graph_env;
405         ir_node**   sched = (ir_node**)get_irn_link(block);
406         ir_node*    first = NULL;
407         int         i;
408
409         /* turn into a list, so we can easily remove nodes.
410            The link field is used anyway. */
411         for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
412                 ir_node* irn = sched[i];
413                 if (!is_cfop(irn)) {
414                         set_irn_link(irn, first);
415                         first = irn;
416                 }
417         }
418         /* note: we can free sched here, there should be no attempt to schedule
419            a block twice */
420         DEL_ARR_F(sched);
421         set_irn_link(block, sched);
422         inst->curr_list = first;
423         return inst;
424 }
425
426 static void normal_finish_graph(void *env)
427 {
428         instance_t *inst = (instance_t*)env;
429
430         /* block uses the link field to store the schedule */
431         ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
432         obstack_free(&inst->obst, NULL);
433         xfree(inst);
434 }
435
436 static void sched_normal(ir_graph *irg)
437 {
438         static const list_sched_selector_t normal_selector = {
439                 normal_init_graph,
440                 normal_init_block,
441                 normal_select,
442                 NULL,              /* node_ready */
443                 NULL,              /* node_selected */
444                 NULL,              /* finish_block */
445                 normal_finish_graph
446         };
447         be_list_sched_graph(irg, &normal_selector);
448 }
449
450 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_normal);
451 void be_init_sched_normal(void)
452 {
453         be_register_scheduler("normal", sched_normal);
454 }