- stack alignment is now power of two
[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 = b1->cost - a1->cost;
97 #if defined NORMAL_DBG
98         ir_fprintf(stderr, "%+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
110 static int count_result(const ir_node* irn)
111 {
112         const ir_mode* mode = get_irn_mode(irn);
113         return
114                 mode != mode_M &&
115                 mode != mode_X &&
116                 !arch_irn_is(cur_arch_env, irn, ignore);
117 }
118
119
120 /* TODO high cost for store trees
121  */
122
123
124 static int normal_tree_cost(ir_node* irn)
125 {
126         flag_and_cost* fc    = get_irn_link(irn);
127         ir_node*       block = get_nodes_block(irn);
128         int            arity = get_irn_arity(irn);
129         int            n_res;
130         int            cost;
131         int            n_op_res = 0;
132         int            i;
133
134         if (be_is_Keep(irn))
135                 return 0;
136
137         if (is_Proj(irn)) {
138                 return normal_tree_cost(get_Proj_pred(irn));
139         }
140
141         if (fc == NULL) {
142                 irn_cost_pair* costs;
143                 int            i;
144
145                 fc = malloc(sizeof(*fc) + sizeof(*fc->costs) * arity);
146                 fc->no_root = 0;
147                 costs = fc->costs;
148
149                 for (i = 0; i < arity; ++i) {
150                         ir_node* pred = get_irn_n(irn, i);
151                         int cost;
152
153                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
154                                 cost = 0;
155                         } else if (get_nodes_block(pred) != block) {
156                                 cost = 1;
157                         } else {
158                                 flag_and_cost* pred_fc;
159                                 ir_node*       real_pred;
160
161                                 cost = normal_tree_cost(pred);
162                                 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
163                                 if (!arch_irn_is(cur_arch_env, pred, ignore)) {
164                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
165                                         pred_fc = get_irn_link(real_pred);
166                                         pred_fc->no_root = 1;
167 #if defined NORMAL_DBG
168                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
169 #endif
170                                 }
171                         }
172
173                         costs[i].irn  = pred;
174                         costs[i].cost = cost;
175                 }
176
177                 qsort(costs, arity, sizeof(*costs), cost_cmp);
178                 set_irn_link(irn, fc);
179         }
180
181         cost = 0;
182         for (i = 0; i < arity; ++i) {
183                 if (get_irn_mode(fc->costs[i].irn) == mode_M) continue;
184                 if (arch_irn_is(cur_arch_env, fc->costs[i].irn, ignore)) continue;
185                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
186                 ++n_op_res;
187         }
188         n_res = count_result(irn);
189         cost = MAX(n_res, cost);
190
191 #if defined NORMAL_DBG
192         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
193 #endif
194
195         return cost;
196 }
197
198
199 static void normal_cost_walker(ir_node* irn, void* env)
200 {
201         (void)env;
202
203 #if defined NORMAL_DBG
204         ir_fprintf(stderr, "cost walking node %+F\n", irn);
205 #endif
206         if (is_Block(irn)) return;
207         if (!must_be_scheduled(irn)) return;
208         normal_tree_cost(irn);
209 }
210
211
212 static void collect_roots(ir_node* irn, void* env)
213 {
214         int is_root;
215
216         (void)env;
217
218         if (is_Block(irn)) return;
219         if (!must_be_scheduled(irn)) return;
220
221         is_root = be_is_Keep(irn) || !((flag_and_cost*)get_irn_link(irn))->no_root;
222
223 #if defined NORMAL_DBG
224         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
225 #endif
226
227         if (is_root) {
228                 ir_node* block = get_nodes_block(irn);
229                 ir_node** roots = get_irn_link(block);
230                 if (roots == NULL) {
231                         roots = NEW_ARR_F(ir_node*, 0);
232                 }
233                 ARR_APP1(ir_node*, roots, irn);
234                 set_irn_link(block, roots);
235         }
236 }
237
238
239 static ir_node** sched_node(ir_node** sched, ir_node* irn)
240 {
241         ir_node*       block = get_nodes_block(irn);
242         flag_and_cost* fc    = get_irn_link(irn);
243         irn_cost_pair* irns  = fc->costs;
244         int            arity = get_irn_arity(irn);
245         int            i;
246
247         if (irn_visited(irn)) return sched;
248         if (is_End(irn))      return sched;
249
250         if (!is_Phi(irn) && !be_is_Keep(irn)) {
251                 for (i = 0; i < arity; ++i) {
252                         ir_node* pred = irns[i].irn;
253                         if (get_nodes_block(pred) != block) continue;
254                         if (get_irn_mode(pred) == mode_M) continue;
255                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
256                         sched = sched_node(sched, pred);
257                 }
258         }
259
260         mark_irn_visited(irn);
261         ARR_APP1(ir_node*, sched, irn);
262         return sched;
263 }
264
265
266 static int root_cmp(const void* a, const void* b)
267 {
268         const irn_cost_pair* const a1 = a;
269         const irn_cost_pair* const b1 = b;
270         int ret;
271         if (is_irn_forking(a1->irn)) {
272                 ret = 1;
273         } else if (is_irn_forking(b1->irn)) {
274                 ret = -1;
275         } else {
276                 ret = b1->cost - a1->cost;
277                 if (ret == 0) {
278                         /* place live-out nodes later */
279                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
280                 }
281         }
282 #if defined NORMAL_DBG
283         ir_fprintf(stderr, "%+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
284 #endif
285         return ret;
286 }
287
288
289 static void normal_sched_block(ir_node* block, void* env)
290 {
291         heights_t*     heights = env;
292         ir_node**      roots   = get_irn_link(block);
293         int            root_count;
294         irn_cost_pair* root_costs;
295         int i;
296         ir_node**      sched;
297
298 #if defined NORMAL_DBG
299         ir_fprintf(stderr, "sched walking block %+F\n", block);
300 #endif
301
302         if (roots == NULL) {
303 #if defined NORMAL_DBG
304                 fprintf(stderr, "has no roots\n");
305 #endif
306                 return;
307         }
308
309         root_count = ARR_LEN(roots);
310         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
311         for (i = 0; i < root_count; ++i) {
312                 root_costs[i].irn  = roots[i];
313                 root_costs[i].cost = get_irn_height(heights, roots[i]);
314 #if defined NORMAL_DBG
315                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
316 #endif
317         }
318         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
319 #if defined NORMAL_DBG
320         {
321                 int n = root_count;
322                 int i;
323
324                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
325                 for (i = 0; i < n; ++i) {
326                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
327                 }
328                 fprintf(stderr, "\n");
329         }
330 #endif
331
332         sched = NEW_ARR_F(ir_node*, 0);
333         for (i = 0; i < root_count; ++i) {
334                 ir_node* irn = root_costs[i].irn;
335                 assert(must_be_scheduled(irn));
336                 sched = sched_node(sched, irn);
337         }
338         set_irn_link(block, sched);
339         DEL_ARR_F(roots);
340
341 #if defined NORMAL_DBG
342         {
343                 int n = ARR_LEN(sched);
344                 int i;
345
346                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
347                 for (i = 0; i < n; ++i) {
348                         ir_fprintf(stderr, "  %+F\n", sched[i]);
349                 }
350                 fprintf(stderr, "\n");
351         }
352 #endif
353 }
354
355
356 static void *normal_init_graph(const list_sched_selector_t *vtab,
357                                const be_irg_t *birg)
358 {
359         ir_graph  *irg = be_get_birg_irg(birg);
360         heights_t *heights;
361
362         (void)vtab;
363
364         cur_arch_env = be_get_birg_arch_env(birg);
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 };