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