Correctly caclulate the register use in the Op(x, x) case.
[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 (ret == 0)
99                 ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
100 #if defined NORMAL_DBG
101         ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
102 #endif
103         return ret;
104 }
105
106
107 typedef struct flag_and_cost {
108         int no_root;
109         irn_cost_pair costs[];
110 } flag_and_cost;
111
112
113 static int count_result(const ir_node* irn)
114 {
115         const ir_mode* mode = get_irn_mode(irn);
116         return
117                 mode != mode_M &&
118                 mode != mode_X &&
119                 !arch_irn_is(cur_arch_env, irn, ignore);
120 }
121
122
123 /* TODO high cost for store trees
124  */
125
126
127 static int normal_tree_cost(ir_node* irn)
128 {
129         flag_and_cost* fc    = get_irn_link(irn);
130         ir_node*       block = get_nodes_block(irn);
131         int            arity = get_irn_arity(irn);
132         ir_node*       last;
133         int            n_res;
134         int            cost;
135         int            n_op_res = 0;
136         int            i;
137
138         if (be_is_Keep(irn))
139                 return 0;
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                                 if (!arch_irn_is(cur_arch_env, pred, ignore)) {
168                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
169                                         pred_fc = get_irn_link(real_pred);
170                                         pred_fc->no_root = 1;
171 #if defined NORMAL_DBG
172                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
173 #endif
174                                 }
175                         }
176
177                         costs[i].irn  = pred;
178                         costs[i].cost = cost;
179                 }
180
181                 qsort(costs, arity, sizeof(*costs), cost_cmp);
182                 set_irn_link(irn, fc);
183         }
184
185         cost = 0;
186         last = 0;
187         for (i = 0; i < arity; ++i) {
188                 ir_node* op = fc->costs[i].irn;
189                 if (op == last)                            continue;
190                 if (get_irn_mode(op) == mode_M)            continue;
191                 if (arch_irn_is(cur_arch_env, op, ignore)) continue;
192                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
193                 last = op;
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;
279         if (is_irn_forking(a1->irn)) {
280                 ret = 1;
281         } else if (is_irn_forking(b1->irn)) {
282                 ret = -1;
283         } else {
284                 ret = b1->cost - a1->cost;
285                 if (ret == 0) {
286                         /* place live-out nodes later */
287                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
288                 }
289         }
290 #if defined NORMAL_DBG
291         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
292 #endif
293         return ret;
294 }
295
296
297 static void normal_sched_block(ir_node* block, void* env)
298 {
299         heights_t*     heights = env;
300         ir_node**      roots   = get_irn_link(block);
301         int            root_count;
302         irn_cost_pair* root_costs;
303         int i;
304         ir_node**      sched;
305
306 #if defined NORMAL_DBG
307         ir_fprintf(stderr, "sched walking block %+F\n", block);
308 #endif
309
310         if (roots == NULL) {
311 #if defined NORMAL_DBG
312                 fprintf(stderr, "has no roots\n");
313 #endif
314                 return;
315         }
316
317         root_count = ARR_LEN(roots);
318         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
319         for (i = 0; i < root_count; ++i) {
320                 root_costs[i].irn  = roots[i];
321                 root_costs[i].cost = get_irn_height(heights, roots[i]);
322 #if defined NORMAL_DBG
323                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
324 #endif
325         }
326         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
327 #if defined NORMAL_DBG
328         {
329                 int n = root_count;
330                 int i;
331
332                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
333                 for (i = 0; i < n; ++i) {
334                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
335                 }
336                 fprintf(stderr, "\n");
337         }
338 #endif
339
340         sched = NEW_ARR_F(ir_node*, 0);
341         for (i = 0; i < root_count; ++i) {
342                 ir_node* irn = root_costs[i].irn;
343                 assert(must_be_scheduled(irn));
344                 sched = sched_node(sched, irn);
345         }
346         set_irn_link(block, sched);
347         DEL_ARR_F(roots);
348
349 #if defined NORMAL_DBG
350         {
351                 int n = ARR_LEN(sched);
352                 int i;
353
354                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
355                 for (i = 0; i < n; ++i) {
356                         ir_fprintf(stderr, "  %+F\n", sched[i]);
357                 }
358                 fprintf(stderr, "\n");
359         }
360 #endif
361 }
362
363
364 static void *normal_init_graph(const list_sched_selector_t *vtab,
365                                const be_irg_t *birg)
366 {
367         ir_graph  *irg = be_get_birg_irg(birg);
368         heights_t *heights;
369
370         (void)vtab;
371
372         cur_arch_env = be_get_birg_arch_env(birg);
373
374         be_clear_links(irg);
375
376         heights = heights_new(irg);
377
378         irg_walk_graph(irg, normal_cost_walker,  NULL, NULL);
379         irg_walk_graph(irg, collect_roots, NULL, NULL);
380         inc_irg_visited(irg);
381         irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
382
383         heights_free(heights);
384
385         return NULL;
386 }
387
388
389 static void *normal_init_block(void *graph_env, ir_node *block)
390 {
391         (void)graph_env;
392         (void)block;
393
394         return NULL;
395 }
396
397
398 const list_sched_selector_t normal_selector = {
399         normal_init_graph,
400         normal_init_block,
401         normal_select,
402         NULL,              /* to_appear_in_schedule */
403         NULL,              /* node_ready */
404         NULL,              /* node_selected */
405         NULL,              /* exectime */
406         NULL,              /* latency */
407         NULL,              /* finish_block */
408         NULL               /* finish_graph */
409 };