Do not add a Proj to the schedule.
[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) && !is_Sync(irn);
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         if (arch_get_irn_register_req(irn)->type & arch_register_req_type_ignore)
120                 return 0;
121
122         return 1;
123 }
124
125
126 /* TODO high cost for store trees
127  */
128
129 static int normal_tree_cost(ir_node* irn, instance_t *inst)
130 {
131         flag_and_cost* fc;
132         int            arity;
133         ir_node*       last;
134         int            n_res;
135         int            cost;
136         int            n_op_res = 0;
137         int            i;
138
139         if (be_is_Keep(irn))
140                 return 0;
141
142         if (is_Proj(irn)) {
143                 return normal_tree_cost(get_Proj_pred(irn), inst);
144         }
145
146         arity = get_irn_arity(irn);
147         fc    = get_irn_fc(irn);
148
149         if (fc == NULL) {
150                 irn_cost_pair* costs;
151                 ir_node*       block = get_nodes_block(irn);
152
153                 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
154                 fc->no_root = 0;
155                 costs = fc->costs;
156
157                 for (i = 0; i < arity; ++i) {
158                         ir_node* pred = get_irn_n(irn, i);
159
160                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
161                                 cost = 0;
162                         } else if (get_nodes_block(pred) != block) {
163                                 cost = 1;
164                         } else {
165                                 flag_and_cost* pred_fc;
166                                 ir_node*       real_pred;
167
168                                 cost = normal_tree_cost(pred, inst);
169                                 if (!arch_irn_is_ignore(pred)) {
170                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
171                                         pred_fc = get_irn_fc(real_pred);
172                                         pred_fc->no_root = 1;
173 #if defined NORMAL_DBG
174                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
175 #endif
176                                 }
177                         }
178
179                         costs[i].irn  = pred;
180                         costs[i].cost = cost;
181                 }
182
183                 qsort(costs, arity, sizeof(*costs), cost_cmp);
184                 set_irn_link(irn, fc);
185         }
186
187         cost = 0;
188         last = 0;
189         for (i = 0; i < arity; ++i) {
190                 ir_node* op = fc->costs[i].irn;
191                 ir_mode* mode;
192                 if (op == last)
193                         continue;
194                 mode = get_irn_mode(op);
195                 if (mode == mode_M)
196                         continue;
197                 if (arch_get_irn_flags(op) & arch_irn_flags_not_scheduled)
198                         continue;
199                 if (mode != mode_T && arch_irn_is_ignore(op))
200                         continue;
201                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
202                 last = op;
203                 ++n_op_res;
204         }
205         n_res = count_result(irn);
206         cost = MAX(n_res, cost);
207
208 #if defined NORMAL_DBG
209         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
210 #endif
211
212         return cost;
213 }
214
215
216 static void normal_cost_walker(ir_node* irn, void* env)
217 {
218         instance_t *inst = (instance_t*)env;
219
220 #if defined NORMAL_DBG
221         ir_fprintf(stderr, "cost walking node %+F\n", irn);
222 #endif
223         if (is_Block(irn)) return;
224         if (!must_be_scheduled(irn)) return;
225         normal_tree_cost(irn, inst);
226 }
227
228
229 static void collect_roots(ir_node* irn, void* env)
230 {
231         int is_root;
232
233         (void)env;
234
235         if (is_Block(irn)) return;
236         if (!must_be_scheduled(irn)) return;
237
238         is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
239
240 #if defined NORMAL_DBG
241         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
242 #endif
243
244         if (is_root) {
245                 ir_node* block = get_nodes_block(irn);
246                 ir_node** roots = (ir_node**)get_irn_link(block);
247                 if (roots == NULL) {
248                         roots = NEW_ARR_F(ir_node*, 0);
249                 }
250                 ARR_APP1(ir_node*, roots, irn);
251                 set_irn_link(block, roots);
252         }
253 }
254
255
256 static ir_node** sched_node(ir_node** sched, ir_node* irn)
257 {
258         if (irn_visited_else_mark(irn)) return sched;
259         if (is_End(irn))                return sched;
260
261         if (!is_Phi(irn) && !be_is_Keep(irn)) {
262                 ir_node*       block = get_nodes_block(irn);
263                 int            arity = get_irn_arity(irn);
264                 flag_and_cost* fc    = get_irn_fc(irn);
265                 irn_cost_pair* irns  = fc->costs;
266                 int            i;
267
268                 for (i = 0; i < arity; ++i) {
269                         ir_node* pred = irns[i].irn;
270                         if (get_nodes_block(pred) != block) continue;
271                         if (get_irn_mode(pred) == mode_M) continue;
272                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
273                         sched = sched_node(sched, pred);
274                 }
275         }
276
277         ARR_APP1(ir_node*, sched, irn);
278         return sched;
279 }
280
281
282 static int root_cmp(const void* a, const void* b)
283 {
284         const irn_cost_pair* const a1 = (const irn_cost_pair*)a;
285         const irn_cost_pair* const b1 = (const irn_cost_pair*)b;
286         int ret;
287         if (is_irn_forking(a1->irn) && !is_irn_forking(b1->irn)) {
288                 ret = 1;
289         } else if (is_irn_forking(b1->irn) && !is_irn_forking(a1->irn)) {
290                 ret = -1;
291         } else {
292                 ret = b1->cost - a1->cost;
293                 if (ret == 0) {
294                         /* place live-out nodes later */
295                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
296                         if (ret == 0) {
297                                 /* compare node idx */
298                                 ret = get_irn_idx(a1->irn) - get_irn_idx(b1->irn);
299                         }
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 }