Remove the unused parameter const arch_env_t *env from arch_irn_get_flags(), arch_irn...
[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 #include "array_t.h"
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 ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
53                               ir_nodeset_t *live_set)
54 {
55         ir_nodeset_iterator_t iter;
56         ir_node*  block;
57         ir_node*  irn;
58         ir_node** sched;
59         int sched_count;
60
61         (void)block_env;
62         (void)live_set;
63
64         ir_nodeset_iterator_init(&iter, ready_set);
65         irn = ir_nodeset_iterator_next(&iter);
66         block = get_nodes_block(irn);
67         sched = get_irn_link(block);
68         sched_count = ARR_LEN(sched);
69         for (; sched_count-- != 0; ++sched) {
70                 ir_node* irn = *sched;
71                 if (ir_nodeset_contains(ready_set, irn) &&
72                                 !arch_irn_class_is(irn, branch)) {
73 #if defined NORMAL_DBG
74                         ir_fprintf(stderr, "scheduling %+F\n", irn);
75 #endif
76                         return irn;
77                 }
78         }
79
80         return irn;
81 }
82
83
84 typedef struct irn_cost_pair {
85         ir_node* irn;
86         int      cost;
87 } irn_cost_pair;
88
89
90 static int cost_cmp(const void* a, const void* b)
91 {
92         const irn_cost_pair* const a1 = a;
93         const irn_cost_pair* const b1 = b;
94         int ret = b1->cost - a1->cost;
95         if (ret == 0)
96                 ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
97 #if defined NORMAL_DBG
98         ir_fprintf(stderr, "cost %+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(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         ir_node*       last;
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(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         last = 0;
184         for (i = 0; i < arity; ++i) {
185                 ir_node* op = fc->costs[i].irn;
186                 if (op == last)                 continue;
187                 if (get_irn_mode(op) == mode_M) continue;
188                 if (arch_irn_is(op, ignore))    continue;
189                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
190                 last = op;
191                 ++n_op_res;
192         }
193         n_res = count_result(irn);
194         cost = MAX(n_res, cost);
195
196 #if defined NORMAL_DBG
197         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
198 #endif
199
200         return cost;
201 }
202
203
204 static void normal_cost_walker(ir_node* irn, void* env)
205 {
206         (void)env;
207
208 #if defined NORMAL_DBG
209         ir_fprintf(stderr, "cost walking node %+F\n", irn);
210 #endif
211         if (is_Block(irn)) return;
212         if (!must_be_scheduled(irn)) return;
213         normal_tree_cost(irn);
214 }
215
216
217 static void collect_roots(ir_node* irn, void* env)
218 {
219         int is_root;
220
221         (void)env;
222
223         if (is_Block(irn)) return;
224         if (!must_be_scheduled(irn)) return;
225
226         is_root = be_is_Keep(irn) || !((flag_and_cost*)get_irn_link(irn))->no_root;
227
228 #if defined NORMAL_DBG
229         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
230 #endif
231
232         if (is_root) {
233                 ir_node* block = get_nodes_block(irn);
234                 ir_node** roots = get_irn_link(block);
235                 if (roots == NULL) {
236                         roots = NEW_ARR_F(ir_node*, 0);
237                 }
238                 ARR_APP1(ir_node*, roots, irn);
239                 set_irn_link(block, roots);
240         }
241 }
242
243
244 static ir_node** sched_node(ir_node** sched, ir_node* irn)
245 {
246         ir_node*       block = get_nodes_block(irn);
247         flag_and_cost* fc    = get_irn_link(irn);
248         irn_cost_pair* irns  = fc->costs;
249         int            arity = get_irn_arity(irn);
250         int            i;
251
252         if (irn_visited_else_mark(irn)) return sched;
253         if (is_End(irn))                return sched;
254
255         if (!is_Phi(irn) && !be_is_Keep(irn)) {
256                 for (i = 0; i < arity; ++i) {
257                         ir_node* pred = irns[i].irn;
258                         if (get_nodes_block(pred) != block) continue;
259                         if (get_irn_mode(pred) == mode_M) continue;
260                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
261                         sched = sched_node(sched, pred);
262                 }
263         }
264
265         ARR_APP1(ir_node*, sched, irn);
266         return sched;
267 }
268
269
270 static int root_cmp(const void* a, const void* b)
271 {
272         const irn_cost_pair* const a1 = a;
273         const irn_cost_pair* const b1 = b;
274         int ret;
275         if (is_irn_forking(a1->irn)) {
276                 ret = 1;
277         } else if (is_irn_forking(b1->irn)) {
278                 ret = -1;
279         } else {
280                 ret = b1->cost - a1->cost;
281                 if (ret == 0) {
282                         /* place live-out nodes later */
283                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
284                 }
285         }
286 #if defined NORMAL_DBG
287         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
288 #endif
289         return ret;
290 }
291
292
293 static void normal_sched_block(ir_node* block, void* env)
294 {
295         heights_t*     heights = env;
296         ir_node**      roots   = get_irn_link(block);
297         int            root_count;
298         irn_cost_pair* root_costs;
299         int i;
300         ir_node**      sched;
301
302 #if defined NORMAL_DBG
303         ir_fprintf(stderr, "sched walking block %+F\n", block);
304 #endif
305
306         if (roots == NULL) {
307 #if defined NORMAL_DBG
308                 fprintf(stderr, "has no roots\n");
309 #endif
310                 return;
311         }
312
313         root_count = ARR_LEN(roots);
314         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
315         for (i = 0; i < root_count; ++i) {
316                 root_costs[i].irn  = roots[i];
317                 root_costs[i].cost = get_irn_height(heights, roots[i]);
318 #if defined NORMAL_DBG
319                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
320 #endif
321         }
322         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
323 #if defined NORMAL_DBG
324         {
325                 int n = root_count;
326                 int i;
327
328                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
329                 for (i = 0; i < n; ++i) {
330                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
331                 }
332                 fprintf(stderr, "\n");
333         }
334 #endif
335
336         sched = NEW_ARR_F(ir_node*, 0);
337         for (i = 0; i < root_count; ++i) {
338                 ir_node* irn = root_costs[i].irn;
339                 assert(must_be_scheduled(irn));
340                 sched = sched_node(sched, irn);
341         }
342         set_irn_link(block, sched);
343         DEL_ARR_F(roots);
344
345 #if defined NORMAL_DBG
346         {
347                 int n = ARR_LEN(sched);
348                 int i;
349
350                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
351                 for (i = 0; i < n; ++i) {
352                         ir_fprintf(stderr, "  %+F\n", sched[i]);
353                 }
354                 fprintf(stderr, "\n");
355         }
356 #endif
357 }
358
359
360 static void *normal_init_graph(const list_sched_selector_t *vtab,
361                                const be_irg_t *birg)
362 {
363         ir_graph  *irg = be_get_birg_irg(birg);
364         heights_t *heights;
365
366         (void)vtab;
367
368         be_clear_links(irg);
369
370         heights = heights_new(irg);
371
372         irg_walk_graph(irg, normal_cost_walker,  NULL, NULL);
373         irg_walk_graph(irg, collect_roots, NULL, NULL);
374         inc_irg_visited(irg);
375         irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
376
377         heights_free(heights);
378
379         return NULL;
380 }
381
382
383 static void *normal_init_block(void *graph_env, ir_node *block)
384 {
385         (void)graph_env;
386         (void)block;
387
388         return NULL;
389 }
390
391
392 const list_sched_selector_t normal_selector = {
393         normal_init_graph,
394         normal_init_block,
395         normal_select,
396         NULL,              /* to_appear_in_schedule */
397         NULL,              /* node_ready */
398         NULL,              /* node_selected */
399         NULL,              /* exectime */
400         NULL,              /* latency */
401         NULL,              /* finish_block */
402         NULL               /* finish_graph */
403 };