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