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