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