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