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