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