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