- Split bearch.h correctly into bearch.h and bearch_t.h
[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_t.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_is_scheduled, int, const ir_node *)
33 FIRM_IMPL1(sched_first, ir_node *, const ir_node *)
34 FIRM_IMPL1(sched_last, ir_node *, const ir_node *)
35 FIRM_IMPL2(sched_add_after, ir_node *, ir_node *, ir_node *)
36 FIRM_IMPL2(sched_add_before, ir_node *, ir_node *, ir_node *)
37 FIRM_IMPL1_VOID(sched_init_block, ir_node *)
38 FIRM_IMPL1_VOID(sched_reset, ir_node *)
39 FIRM_IMPL2(sched_comes_after, int, const ir_node *, const ir_node *)
40 FIRM_IMPL1_VOID(sched_remove, ir_node *)
41
42 size_t sched_irn_data_offset = 0;
43
44 static void block_sched_dumper(ir_node *block, void *env)
45 {
46         FILE  *f = env;
47         const ir_node *curr;
48
49         ir_fprintf(f, "%+F:\n", block);
50
51         sched_foreach(block, curr) {
52                 sched_info_t *info = get_irn_sched_info(curr);
53                 ir_fprintf(f, "\t%6d: %+F\n", info->time_step, curr);
54         }
55 }
56
57 void be_sched_dump(FILE *f, ir_graph *irg)
58 {
59         irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
60 }
61
62 /* Init the scheduling stuff. */
63 void be_init_sched(void)
64 {
65         sched_irn_data_offset = register_additional_node_data(sizeof(sched_info_t));
66 }
67
68 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);
69
70 void sched_renumber(const ir_node *block)
71 {
72         ir_node *irn;
73         sched_info_t *inf;
74         sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
75
76         sched_foreach(block, irn) {
77                 inf = get_irn_sched_info(irn);
78                 inf->time_step = step;
79                 step += SCHED_INITIAL_GRANULARITY;
80         }
81 }
82
83 int sched_skip_cf_predicator(const ir_node *irn, void *data) {
84         arch_env_t *ae = data;
85         return arch_irn_class_is(ae, irn, branch);
86 }
87
88 int sched_skip_phi_predicator(const ir_node *irn, void *data) {
89         return is_Phi(irn);
90 }
91
92 /* Skip nodes in a schedule. */
93 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
94 {
95         const ir_node *bl = get_block(from);
96         ir_node *curr;
97
98         if (forward) {
99                 if (is_Block(from))
100                         from = sched_next(from);
101                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_next(curr)) {
102                 }
103         } else {
104                 if (is_Block(from))
105                         from = sched_prev(from);
106                 for (curr = from; curr != bl && predicator(curr, data); curr = sched_prev(curr)) {
107                 }
108         }
109
110         return curr;
111 }
112
113 //---------------------------------------------------------------------------
114
115 typedef struct remove_dead_nodes_env_t_ {
116         ir_graph *irg;
117         bitset_t *reachable;
118 } remove_dead_nodes_env_t;
119
120 /**
121  * Post-walker: remember all visited nodes in a bitset.
122  */
123 static void mark_dead_nodes_walker(ir_node *node, void *data)
124 {
125         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
126         bitset_set(env->reachable, get_irn_idx(node));
127 }
128
129 /**
130  * Post-block-walker:
131  * Walk through the schedule of every block and remove all dead nodes from it.
132  */
133 static void remove_dead_nodes_walker(ir_node *block, void *data)
134 {
135         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
136         ir_node                 *node, *next;
137
138         for (node = sched_first(block); ! sched_is_end(node); node = next) {
139                 /* get next node now, as after calling sched_remove it will be invalid */
140                 next = sched_next(node);
141
142                 if (bitset_is_set(env->reachable, get_irn_idx(node)))
143                         continue;
144
145                 sched_remove(node);
146                 be_kill_node(node);
147         }
148 }
149
150 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
151 {
152         remove_dead_nodes_env_t env;
153         env.irg = irg;
154         env.reachable = bitset_alloca(get_irg_last_idx(irg));
155
156         // mark all reachable nodes
157         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
158
159         // walk schedule and remove non-marked nodes
160         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
161 }