Remove the unused parameter const arch_env_t *env from arch_get_register_req().
[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 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_STDLIB_H
31 # include <stdlib.h>
32 #endif
33
34 #include "impl.h"
35 #include "irprintf.h"
36 #include "irgwalk.h"
37 #include "firm_types.h"
38 #include "irgraph_t.h"
39 #include "iredges_t.h"
40 #include "ircons.h"
41 #include "irextbb.h"
42 #include "irgmod.h"
43 #include "debug.h"
44
45 #include "bemodule.h"
46 #include "bearch_t.h"
47 #include "besched_t.h"
48 #include "beutil.h"
49 #include "belistsched.h"
50
51 FIRM_IMPL1(have_sched_info, int, const ir_graph *)
52 FIRM_IMPL1(sched_get_time_step, int, const ir_node *)
53 FIRM_IMPL1(sched_has_next, int, const ir_node *)
54 FIRM_IMPL1(sched_has_prev, int, const ir_node *)
55 FIRM_IMPL1(sched_next, ir_node *, const ir_node *)
56 FIRM_IMPL1(sched_prev, ir_node *, const ir_node *)
57 FIRM_IMPL1(sched_is_scheduled, int, const ir_node *)
58 FIRM_IMPL1(sched_first, ir_node *, const ir_node *)
59 FIRM_IMPL1(sched_last, ir_node *, const ir_node *)
60 FIRM_IMPL2_VOID(sched_add_after, const ir_node *, const ir_node *)
61 FIRM_IMPL2_VOID(sched_add_before, const ir_node *, const ir_node *)
62 FIRM_IMPL1_VOID(sched_init_block, const ir_node *)
63 FIRM_IMPL1_VOID(sched_reset, const ir_node *)
64 FIRM_IMPL2(sched_comes_after, int, const ir_node *, const ir_node *)
65 FIRM_IMPL1_VOID(sched_remove, const ir_node *)
66
67 size_t sched_irn_data_offset = 0;
68
69 static void block_sched_dumper(ir_node *block, void *env)
70 {
71         FILE  *f = env;
72         const ir_node *curr;
73
74         ir_fprintf(f, "%+F:\n", block);
75
76         sched_foreach(block, curr) {
77                 sched_info_t *info = get_irn_sched_info(curr);
78                 ir_fprintf(f, "\t%6d: %+F\n", info->time_step, curr);
79         }
80 }
81
82 void be_sched_dump(FILE *f, ir_graph *irg)
83 {
84         irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
85 }
86
87 /* Init the scheduling stuff. */
88 void be_init_sched(void)
89 {
90         sched_irn_data_offset = firm_register_additional_node_data(sizeof(sched_info_t));
91 }
92
93 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);
94
95 void sched_renumber(const ir_node *block)
96 {
97         ir_node *irn;
98         sched_info_t *inf;
99         sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
100
101         sched_foreach(block, irn) {
102                 inf = get_irn_sched_info(irn);
103                 inf->time_step = step;
104                 step += SCHED_INITIAL_GRANULARITY;
105         }
106 }
107
108 int sched_skip_cf_predicator(const ir_node *irn, void *data) {
109         arch_env_t *ae = data;
110         return arch_irn_class_is(ae, irn, branch);
111 }
112
113 int sched_skip_phi_predicator(const ir_node *irn, void *data) {
114         (void) data;
115         return is_Phi(irn);
116 }
117
118 /* Skip nodes in a schedule. */
119 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
120 {
121         const ir_node *bl = get_block_const(from);
122         ir_node *curr;
123
124         if (forward) {
125                 if (is_Block(from))
126                         from = sched_next(from);
127                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_next(curr)) {
128                 }
129         } else {
130                 if (is_Block(from))
131                         from = sched_prev(from);
132                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_prev(curr)) {
133                 }
134         }
135
136         return curr;
137 }
138
139 //---------------------------------------------------------------------------
140
141 typedef struct remove_dead_nodes_env_t_ {
142         bitset_t *reachable;
143         ir_graph *irg;
144         be_lv_t  *lv;
145 } remove_dead_nodes_env_t;
146
147 /**
148  * Post-walker: remember all visited nodes in a bitset.
149  */
150 static void mark_dead_nodes_walker(ir_node *node, void *data)
151 {
152         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
153         bitset_set(env->reachable, get_irn_idx(node));
154 }
155
156 /**
157  * Post-block-walker:
158  * Walk through the schedule of every block and remove all dead nodes from it.
159  */
160 static void remove_dead_nodes_walker(ir_node *block, void *data)
161 {
162         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
163         ir_node                 *node, *next;
164
165         for (node = sched_first(block); ! sched_is_end(node); node = next) {
166                 /* get next node now, as after calling sched_remove it will be invalid */
167                 next = sched_next(node);
168
169                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
170                         continue;
171
172                 if(env->lv)
173                         be_liveness_remove(env->lv, node);
174                 sched_remove(node);
175                 kill_node(node);
176         }
177 }
178
179 void be_remove_dead_nodes_from_schedule(be_irg_t *birg)
180 {
181         ir_graph *irg = be_get_birg_irg(birg);
182
183         remove_dead_nodes_env_t env;
184         env.reachable = bitset_alloca(get_irg_last_idx(irg));
185         env.lv  = be_get_birg_liveness(birg);
186         env.irg = irg;
187
188         // mark all reachable nodes
189         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
190
191         // walk schedule and remove non-marked nodes
192         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
193 }
194
195 static void *sched_irn_init(ir_phase *ph, const ir_node *irn, void *old)
196 {
197         sched_info_t *info = old ? old : phase_alloc(ph, sizeof(*info));
198
199         info->idx  = get_irn_idx(irn);
200         INIT_LIST_HEAD(&info->list);
201         info->scheduled = 0;
202         info->time_step = 0;
203         return info;
204 }
205
206 void be_sched_init_phase(ir_graph *irg)
207 {
208         init_irg_phase(irg, PHASE_BE_SCHED, 0, sched_irn_init);
209 }
210
211 void be_sched_free_phase(ir_graph *irg)
212 {
213         free_irg_phase(irg, PHASE_BE_SCHED);
214 }