more verification of the final graph, always kill nodes in be_remove_dead_nodes_from_...
[libfirm] / ir / be / besched.c
1 /* $Id$ */
2
3 #ifdef HAVE_CONFIG_H
4 # include "config.h"
5 #endif
6
7 #ifdef HAVE_STDLIB_H
8 # include <stdlib.h>
9 #endif
10
11 #include "impl.h"
12 #include "irprintf.h"
13 #include "irgwalk.h"
14 #include "firm_types.h"
15 #include "irgraph_t.h"
16 #include "iredges_t.h"
17 #include "ircons.h"
18 #include "irextbb.h"
19 #include "debug.h"
20
21 #include "bemodule.h"
22 #include "bearch.h"
23 #include "besched_t.h"
24 #include "beutil.h"
25 #include "belistsched.h"
26
27 FIRM_IMPL1(sched_get_time_step, int, const ir_node *)
28 FIRM_IMPL1(sched_has_next, int, const ir_node *)
29 FIRM_IMPL1(sched_has_prev, int, const ir_node *)
30 FIRM_IMPL1(sched_next, ir_node *, const ir_node *)
31 FIRM_IMPL1(sched_prev, ir_node *, const ir_node *)
32 FIRM_IMPL1(sched_first, ir_node *, const ir_node *)
33 FIRM_IMPL1(sched_last, ir_node *, const ir_node *)
34 FIRM_IMPL2(sched_add_after, ir_node *, ir_node *, ir_node *)
35 FIRM_IMPL2(sched_add_before, ir_node *, ir_node *, ir_node *)
36 FIRM_IMPL1_VOID(sched_init_block, ir_node *)
37 FIRM_IMPL1_VOID(sched_reset, ir_node *)
38 FIRM_IMPL2(sched_comes_after, int, const ir_node *, const ir_node *)
39 FIRM_IMPL1_VOID(sched_remove, ir_node *)
40
41 size_t sched_irn_data_offset = 0;
42
43 static void block_sched_dumper(ir_node *block, void *env)
44 {
45         FILE  *f = env;
46         const ir_node *curr;
47
48         ir_fprintf(f, "%+F:\n", block);
49
50         sched_foreach(block, curr) {
51                 sched_info_t *info = get_irn_sched_info(curr);
52                 ir_fprintf(f, "\t%6d: %+F\n", info->time_step, curr);
53         }
54 }
55
56 void be_sched_dump(FILE *f, ir_graph *irg)
57 {
58         irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
59 }
60
61 /* Init the scheduling stuff. */
62 void be_init_sched(void)
63 {
64         sched_irn_data_offset = register_additional_node_data(sizeof(sched_info_t));
65 }
66
67 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);
68
69 void sched_renumber(const ir_node *block)
70 {
71         ir_node *irn;
72         sched_info_t *inf;
73         sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
74
75         sched_foreach(block, irn) {
76                 inf = get_irn_sched_info(irn);
77                 inf->time_step = step;
78                 step += SCHED_INITIAL_GRANULARITY;
79         }
80 }
81
82 int sched_skip_cf_predicator(const ir_node *irn, void *data) {
83         arch_env_t *ae = data;
84         return arch_irn_class_is(ae, irn, branch);
85 }
86
87 int sched_skip_phi_predicator(const ir_node *irn, void *data) {
88         return is_Phi(irn);
89 }
90
91 /* Skip nodes in a schedule. */
92 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
93 {
94         const ir_node *bl = get_block(from);
95         ir_node *curr;
96
97         if (forward) {
98                 if (is_Block(from))
99                         from = sched_next(from);
100                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_next(curr)) {
101                 }
102         } else {
103                 if (is_Block(from))
104                         from = sched_prev(from);
105                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_prev(curr)) {
106                 }
107         }
108
109         return curr;
110 }
111
112 //---------------------------------------------------------------------------
113
114 typedef struct remove_dead_nodes_env_t_ {
115         ir_graph *irg;
116         bitset_t *reachable;
117 } remove_dead_nodes_env_t;
118
119 /**
120  * Post-walker: remember all visited nodes in a bitset.
121  */
122 static void mark_dead_nodes_walker(ir_node *node, void *data)
123 {
124         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
125         bitset_set(env->reachable, get_irn_idx(node));
126 }
127
128 /**
129  * Post-block-walker:
130  * Walk through the schedule of every block and remove all dead nodes from it.
131  */
132 static void remove_dead_nodes_walker(ir_node *block, void *data)
133 {
134         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
135         ir_node                 *node, *next;
136
137         for (node = sched_first(block); ! sched_is_end(node); node = next) {
138                 /* get next node now, as after calling sched_remove it will be invalid */
139                 next = sched_next(node);
140
141                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
142                         continue;
143
144                 sched_remove(node);
145                 be_kill_node(node);
146         }
147 }
148
149 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
150 {
151         remove_dead_nodes_env_t env;
152         env.irg = irg;
153         env.reachable = bitset_alloca(get_irg_last_idx(irg));
154
155         // mark all reachable nodes
156         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
157
158         // walk schedule and remove non-marked nodes
159         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
160 }