cleanup and rewrite dumper interface
[libfirm] / ir / be / besched.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  * @file
22  * @brief       Scheduling utilities for nodes in Blocks and Blocks.
23  * @author      Sebastian Hack
24  * @version     $Id$
25  */
26 #include "config.h"
27
28 #include <stdlib.h>
29
30 #include "irprintf.h"
31 #include "irgwalk.h"
32 #include "firm_types.h"
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
35 #include "ircons.h"
36 #include "irextbb.h"
37 #include "irgmod.h"
38 #include "debug.h"
39
40 #include "bemodule.h"
41 #include "bearch.h"
42 #include "besched.h"
43 #include "beutil.h"
44 #include "belistsched.h"
45 #include "belive.h"
46
47 size_t sched_irn_data_offset = 0;
48
49 static void block_sched_dumper(ir_node *block, void *env)
50 {
51         FILE  *f = env;
52         const ir_node *curr;
53
54         ir_fprintf(f, "%+F:\n", block);
55
56         sched_foreach(block, curr) {
57                 sched_info_t *info = get_irn_sched_info(curr);
58                 ir_fprintf(f, "\t%6d: %+F\n", info->time_step, curr);
59         }
60 }
61
62 void be_sched_dump(FILE *f, ir_graph *irg)
63 {
64         irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
65 }
66
67 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);
68 void be_init_sched(void)
69 {
70         sched_irn_data_offset = firm_register_additional_node_data(sizeof(sched_info_t));
71 }
72
73 void sched_renumber(const ir_node *block)
74 {
75         ir_node *irn;
76         sched_info_t *inf;
77         sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
78
79         sched_foreach(block, irn) {
80                 inf = get_irn_sched_info(irn);
81                 inf->time_step = step;
82                 step += SCHED_INITIAL_GRANULARITY;
83         }
84 }
85
86 int sched_skip_cf_predicator(const ir_node *irn, void *data)
87 {
88         (void)data;
89         return is_cfop(irn);
90 }
91
92 int sched_skip_phi_predicator(const ir_node *irn, void *data)
93 {
94         (void) data;
95         return is_Phi(irn);
96 }
97
98 /* Skip nodes in a schedule. */
99 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
100 {
101         const ir_node *bl = get_block_const(from);
102         ir_node *curr;
103
104         if (forward) {
105                 if (is_Block(from))
106                         from = sched_next(from);
107                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_next(curr)) {
108                 }
109         } else {
110                 if (is_Block(from))
111                         from = sched_prev(from);
112                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_prev(curr)) {
113                 }
114         }
115
116         return curr;
117 }
118
119 //---------------------------------------------------------------------------
120
121 typedef struct remove_dead_nodes_env_t_ {
122         bitset_t *reachable;
123         ir_graph *irg;
124         be_lv_t  *lv;
125 } remove_dead_nodes_env_t;
126
127 /**
128  * Post-walker: remember all visited nodes in a bitset.
129  */
130 static void mark_dead_nodes_walker(ir_node *node, void *data)
131 {
132         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
133         bitset_set(env->reachable, get_irn_idx(node));
134 }
135
136 /**
137  * Post-block-walker:
138  * Walk through the schedule of every block and remove all dead nodes from it.
139  */
140 static void remove_dead_nodes_walker(ir_node *block, void *data)
141 {
142         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
143         ir_node                 *node, *next;
144
145         for (node = sched_first(block); ! sched_is_end(node); node = next) {
146                 /* get next node now, as after calling sched_remove it will be invalid */
147                 next = sched_next(node);
148
149                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
150                         continue;
151
152                 if (env->lv)
153                         be_liveness_remove(env->lv, node);
154                 sched_remove(node);
155                 kill_node(node);
156         }
157 }
158
159 void be_remove_dead_nodes_from_schedule(be_irg_t *birg)
160 {
161         ir_graph *irg = be_get_birg_irg(birg);
162
163         remove_dead_nodes_env_t env;
164         env.reachable = bitset_alloca(get_irg_last_idx(irg));
165         env.lv  = be_get_birg_liveness(birg);
166         env.irg = irg;
167
168         // mark all reachable nodes
169         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
170
171         // walk schedule and remove non-marked nodes
172         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
173 }
174
175 int (sched_get_time_step)(const ir_node *node)
176 {
177         return _sched_get_time_step(node);
178 }
179
180 int (sched_has_next)(const ir_node *node)
181 {
182         return _sched_has_next(node);
183 }
184
185 int (sched_has_prev)(const ir_node *node)
186 {
187         return _sched_has_prev(node);
188 }
189
190 ir_node *(sched_next)(const ir_node *node)
191 {
192         return _sched_next(node);
193 }
194
195 ir_node *(sched_prev)(const ir_node *node)
196 {
197         return _sched_prev(node);
198 }
199
200 int (sched_is_scheduled)(const ir_node *node)
201 {
202         return _sched_is_scheduled(node);
203 }
204
205 ir_node *(sched_first)(const ir_node *node)
206 {
207         return _sched_first(node);
208 }
209
210 ir_node *(sched_last)(const ir_node *node)
211 {
212         return _sched_last(node);
213 }
214
215 void (sched_add_after)(ir_node *after, ir_node *node)
216 {
217         _sched_add_after(after, node);
218 }
219
220 void (sched_add_before)(ir_node *before, ir_node *node)
221 {
222         _sched_add_before(before, node);
223 }
224
225 void (sched_init_block)(ir_node *block)
226 {
227         _sched_init_block(block);
228 }
229
230 void (sched_remove)(ir_node *node)
231 {
232         _sched_remove(node);
233 }
234
235 void (sched_reset)(ir_node *node)
236 {
237         _sched_reset(node);
238 }
239
240 int (sched_comes_after)(const ir_node *n1, const ir_node *n2)
241 {
242         return _sched_comes_after(n1, n2);
243 }