amd64: small changes w.r.t. stack alignment.
[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 #include "config.h"
26
27 #include <stdlib.h>
28
29 #include "besched.h"
30 #include "belistsched.h"
31 #include "belive_t.h"
32 #include "beutil.h"
33 #include "height.h"
34 #include "irtools.h"
35 #include "irgwalk.h"
36 #include "benode.h"
37 #include "array_t.h"
38
39 // XXX there is no one time init for schedulers
40 //#define NORMAL_DBG
41 #include "irprintf.h"
42
43 /** An instance of the normal scheduler. */
44 typedef struct instance_t {
45         ir_graph*      irg;          /**< the IR graph of this instance */
46         struct obstack obst;         /**< obstack for temporary data */
47         ir_node*       curr_list;    /**< current block schedule list */
48 } instance_t;
49
50 static int must_be_scheduled(const ir_node* const irn)
51 {
52         return !is_Proj(irn) && !is_Sync(irn);
53 }
54
55
56 static ir_node *normal_select(void *block_env, ir_nodeset_t *ready_set,
57                               ir_nodeset_t *live_set)
58 {
59         instance_t* inst = block_env;
60         ir_node*    irn;
61         ir_node*    next;
62         ir_node*    last = NULL;
63         ir_nodeset_iterator_t iter;
64
65         (void)live_set;
66
67         for (irn = inst->curr_list; irn != NULL; last = irn, irn = next) {
68                 next = get_irn_link(irn);
69                 if (ir_nodeset_contains(ready_set, irn)) {
70 #if defined NORMAL_DBG
71                         ir_fprintf(stderr, "scheduling %+F\n", irn);
72 #endif
73                         if (last == NULL)
74                                 inst->curr_list = next;
75                         else
76                                 set_irn_link(last, next);
77                         return irn;
78                 }
79         }
80
81         ir_nodeset_iterator_init(&iter, ready_set);
82         irn = ir_nodeset_iterator_next(&iter);
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 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 (ret == 0)
98                 ret = (int)get_irn_idx(a1->irn) - (int)get_irn_idx(b1->irn);
99 #if defined NORMAL_DBG
100         ir_fprintf(stderr, "cost %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
101 #endif
102         return ret;
103 }
104
105
106 typedef struct flag_and_cost {
107         int no_root;
108         irn_cost_pair costs[];
109 } flag_and_cost;
110
111 #define get_irn_fc(irn)     ((flag_and_cost*)get_irn_link(irn))
112 #define set_irn_fc(irn, fc) set_irn_link(irn, fc)
113
114
115 static int count_result(const ir_node* irn)
116 {
117         const ir_mode* mode = get_irn_mode(irn);
118
119         if (mode == mode_M || mode == mode_X)
120                 return 0;
121
122         if (mode == mode_T)
123                 return 1;
124
125         if (arch_get_register_req_out(irn)->type & arch_register_req_type_ignore)
126                 return 0;
127
128         return 1;
129 }
130
131
132 /* TODO high cost for store trees
133  */
134
135 static int normal_tree_cost(ir_node* irn, instance_t *inst)
136 {
137         flag_and_cost* fc;
138         int            arity;
139         ir_node*       last;
140         int            n_res;
141         int            cost;
142         int            n_op_res = 0;
143         int            i;
144
145         if (be_is_Keep(irn))
146                 return 0;
147
148         if (is_Proj(irn)) {
149                 return normal_tree_cost(get_Proj_pred(irn), inst);
150         }
151
152         arity = get_irn_arity(irn);
153         fc    = get_irn_fc(irn);
154
155         if (fc == NULL) {
156                 irn_cost_pair* costs;
157                 int            i;
158                 ir_node*       block = get_nodes_block(irn);
159
160                 fc = OALLOCF(&inst->obst, flag_and_cost, costs, arity);
161                 fc->no_root = 0;
162                 costs = fc->costs;
163
164                 for (i = 0; i < arity; ++i) {
165                         ir_node* pred = get_irn_n(irn, i);
166                         int cost;
167
168                         if (is_Phi(irn) || get_irn_mode(pred) == mode_M || is_Block(pred)) {
169                                 cost = 0;
170                         } else if (get_nodes_block(pred) != block) {
171                                 cost = 1;
172                         } else {
173                                 flag_and_cost* pred_fc;
174                                 ir_node*       real_pred;
175
176                                 cost = normal_tree_cost(pred, inst);
177                                 if (be_is_Barrier(pred)) cost = 1; // XXX hack: the barrier causes all users to have a reguse of #regs
178                                 if (!arch_irn_is_ignore(pred)) {
179                                         real_pred = (is_Proj(pred) ? get_Proj_pred(pred) : pred);
180                                         pred_fc = get_irn_fc(real_pred);
181                                         pred_fc->no_root = 1;
182 #if defined NORMAL_DBG
183                                         ir_fprintf(stderr, "%+F says that %+F is no root\n", irn, real_pred);
184 #endif
185                                 }
186                         }
187
188                         costs[i].irn  = pred;
189                         costs[i].cost = cost;
190                 }
191
192                 qsort(costs, arity, sizeof(*costs), cost_cmp);
193                 set_irn_link(irn, fc);
194         }
195
196         cost = 0;
197         last = 0;
198         for (i = 0; i < arity; ++i) {
199                 ir_node* op = fc->costs[i].irn;
200                 ir_mode* mode;
201                 if (op == last)
202                         continue;
203                 mode = get_irn_mode(op);
204                 if (mode == mode_M)
205                         continue;
206                 if (mode != mode_T && arch_irn_is_ignore(op))
207                         continue;
208                 cost = MAX(fc->costs[i].cost + n_op_res, cost);
209                 last = op;
210                 ++n_op_res;
211         }
212         n_res = count_result(irn);
213         cost = MAX(n_res, cost);
214
215 #if defined NORMAL_DBG
216         ir_fprintf(stderr, "reguse of %+F is %d\n", irn, cost);
217 #endif
218
219         return cost;
220 }
221
222
223 static void normal_cost_walker(ir_node* irn, void* env)
224 {
225         instance_t *inst = env;
226
227 #if defined NORMAL_DBG
228         ir_fprintf(stderr, "cost walking node %+F\n", irn);
229 #endif
230         if (is_Block(irn)) return;
231         if (!must_be_scheduled(irn)) return;
232         normal_tree_cost(irn, inst);
233 }
234
235
236 static void collect_roots(ir_node* irn, void* env)
237 {
238         int is_root;
239
240         (void)env;
241
242         if (is_Block(irn)) return;
243         if (!must_be_scheduled(irn)) return;
244
245         is_root = be_is_Keep(irn) || !get_irn_fc(irn)->no_root;
246
247 #if defined NORMAL_DBG
248         ir_fprintf(stderr, "%+F is %sroot\n", irn, is_root ? "" : "no ");
249 #endif
250
251         if (is_root) {
252                 ir_node* block = get_nodes_block(irn);
253                 ir_node** roots = get_irn_link(block);
254                 if (roots == NULL) {
255                         roots = NEW_ARR_F(ir_node*, 0);
256                 }
257                 ARR_APP1(ir_node*, roots, irn);
258                 set_irn_link(block, roots);
259         }
260 }
261
262
263 static ir_node** sched_node(ir_node** sched, ir_node* irn)
264 {
265         if (irn_visited_else_mark(irn)) return sched;
266         if (is_End(irn))                return sched;
267
268         if (!is_Phi(irn) && !be_is_Keep(irn)) {
269                 ir_node*       block = get_nodes_block(irn);
270                 int            arity = get_irn_arity(irn);
271                 flag_and_cost* fc    = get_irn_fc(irn);
272                 irn_cost_pair* irns  = fc->costs;
273                 int            i;
274
275                 for (i = 0; i < arity; ++i) {
276                         ir_node* pred = irns[i].irn;
277                         if (get_nodes_block(pred) != block) continue;
278                         if (get_irn_mode(pred) == mode_M) continue;
279                         if (is_Proj(pred)) pred = get_Proj_pred(pred);
280                         sched = sched_node(sched, pred);
281                 }
282         }
283
284         ARR_APP1(ir_node*, sched, irn);
285         return sched;
286 }
287
288
289 static int root_cmp(const void* a, const void* b)
290 {
291         const irn_cost_pair* const a1 = a;
292         const irn_cost_pair* const b1 = b;
293         int ret;
294         if (is_irn_forking(a1->irn)) {
295                 ret = 1;
296         } else if (is_irn_forking(b1->irn)) {
297                 ret = -1;
298         } else {
299                 ret = b1->cost - a1->cost;
300                 if (ret == 0) {
301                         /* place live-out nodes later */
302                         ret = (count_result(a1->irn) != 0) - (count_result(b1->irn) != 0);
303                 }
304         }
305 #if defined NORMAL_DBG
306         ir_fprintf(stderr, "root %+F %s %+F\n", a1->irn, ret < 0 ? "<" : ret > 0 ? ">" : "=", b1->irn);
307 #endif
308         return ret;
309 }
310
311
312 static void normal_sched_block(ir_node* block, void* env)
313 {
314         ir_node**      roots = get_irn_link(block);
315         heights_t*     heights = env;
316         int            root_count;
317         irn_cost_pair* root_costs;
318         int i;
319         ir_node**      sched;
320
321 #if defined NORMAL_DBG
322         ir_fprintf(stderr, "sched walking block %+F\n", block);
323 #endif
324
325         if (roots == NULL) {
326 #if defined NORMAL_DBG
327                 fprintf(stderr, "has no roots\n");
328 #endif
329                 return;
330         }
331
332         root_count = ARR_LEN(roots);
333         NEW_ARR_A(irn_cost_pair, root_costs, root_count);
334         for (i = 0; i < root_count; ++i) {
335                 root_costs[i].irn  = roots[i];
336                 root_costs[i].cost = get_irn_height(heights, roots[i]);
337 #if defined NORMAL_DBG
338                 ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
339 #endif
340         }
341         qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
342 #if defined NORMAL_DBG
343         {
344                 int n = root_count;
345                 int i;
346
347                 ir_fprintf(stderr, "Root Scheduling of %+F:\n", block);
348                 for (i = 0; i < n; ++i) {
349                         ir_fprintf(stderr, "  %+F\n", root_costs[i].irn);
350                 }
351                 fprintf(stderr, "\n");
352         }
353 #endif
354
355         sched = NEW_ARR_F(ir_node*, 0);
356         for (i = 0; i < root_count; ++i) {
357                 ir_node* irn = root_costs[i].irn;
358                 assert(must_be_scheduled(irn));
359                 sched = sched_node(sched, irn);
360         }
361         set_irn_link(block, sched);
362         DEL_ARR_F(roots);
363
364 #if defined NORMAL_DBG
365         {
366                 int n = ARR_LEN(sched);
367                 int i;
368
369                 ir_fprintf(stderr, "Scheduling of %+F:\n", block);
370                 for (i = 0; i < n; ++i) {
371                         ir_fprintf(stderr, "  %+F\n", sched[i]);
372                 }
373                 fprintf(stderr, "\n");
374         }
375 #endif
376 }
377
378
379 static void *normal_init_graph(const list_sched_selector_t *vtab,
380                                const be_irg_t *birg)
381 {
382         instance_t* inst = XMALLOC(instance_t);
383         ir_graph*   irg = be_get_birg_irg(birg);
384         heights_t*  heights;
385
386         (void)vtab;
387
388         be_clear_links(irg);
389
390         obstack_init(&inst->obst);
391         inst->irg         = irg;
392
393         heights = heights_new(irg);
394
395         ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK);
396         irg_walk_graph(irg, normal_cost_walker,  NULL, inst);
397         irg_walk_graph(irg, collect_roots, NULL, NULL);
398         inc_irg_visited(irg);
399         ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
400         irg_block_walk_graph(irg, normal_sched_block, NULL, heights);
401         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
402
403         heights_free(heights);
404
405         return inst;
406 }
407
408 static void *normal_init_block(void *graph_env, ir_node *block)
409 {
410         instance_t* inst  = graph_env;
411         ir_node**   sched = get_irn_link(block);
412         ir_node*    first = NULL;
413         int         i;
414
415         /* turn into a list, so we can easily remove nodes.
416            The link field is used anyway. */
417         for (i = ARR_LEN(sched) - 1; i >= 0; --i) {
418                 ir_node* irn = sched[i];
419                 if (!is_cfop(irn)) {
420                         set_irn_link(irn, first);
421                         first = irn;
422                 }
423         }
424         /* note: we can free sched here, there should be no attempt to schedule
425            a block twice */
426         DEL_ARR_F(sched);
427         set_irn_link(block, sched);
428         inst->curr_list = first;
429         return inst;
430 }
431
432 static void normal_finish_graph(void *env)
433 {
434         instance_t *inst = env;
435
436         /* block uses the link field to store the schedule */
437         ir_free_resources(inst->irg, IR_RESOURCE_IRN_LINK);
438         obstack_free(&inst->obst, NULL);
439         xfree(inst);
440 }
441
442 const list_sched_selector_t normal_selector = {
443         normal_init_graph,
444         normal_init_block,
445         normal_select,
446         NULL,              /* to_appear_in_schedule */
447         NULL,              /* node_ready */
448         NULL,              /* node_selected */
449         NULL,              /* exectime */
450         NULL,              /* latency */
451         NULL,              /* finish_block */
452         normal_finish_graph
453 };