2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Scheduling utilities for nodes in Blocks and Blocks.
23 * @author Sebastian Hack
37 #include "firm_types.h"
38 #include "irgraph_t.h"
39 #include "iredges_t.h"
46 #include "besched_t.h"
48 #include "belistsched.h"
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 *)
65 size_t sched_irn_data_offset = 0;
67 static void block_sched_dumper(ir_node *block, void *env)
72 ir_fprintf(f, "%+F:\n", block);
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);
80 void be_sched_dump(FILE *f, ir_graph *irg)
82 irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
85 /* Init the scheduling stuff. */
86 void be_init_sched(void)
88 sched_irn_data_offset = firm_register_additional_node_data(sizeof(sched_info_t));
91 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);
93 void sched_renumber(const ir_node *block)
97 sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
99 sched_foreach(block, irn) {
100 inf = get_irn_sched_info(irn);
101 inf->time_step = step;
102 step += SCHED_INITIAL_GRANULARITY;
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);
111 int sched_skip_phi_predicator(const ir_node *irn, void *data) {
116 /* Skip nodes in a schedule. */
117 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
119 const ir_node *bl = get_block(from);
124 from = sched_next(from);
125 for (curr = from; curr != bl && predicator(curr, data); curr = sched_next(curr)) {
129 from = sched_prev(from);
130 for (curr = from; curr != bl && predicator(curr, data); curr = sched_prev(curr)) {
137 //---------------------------------------------------------------------------
139 typedef struct remove_dead_nodes_env_t_ {
143 } remove_dead_nodes_env_t;
146 * Post-walker: remember all visited nodes in a bitset.
148 static void mark_dead_nodes_walker(ir_node *node, void *data)
150 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
151 bitset_set(env->reachable, get_irn_idx(node));
156 * Walk through the schedule of every block and remove all dead nodes from it.
158 static void remove_dead_nodes_walker(ir_node *block, void *data)
160 remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
161 ir_node *node, *next;
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);
167 if (bitset_is_set(env->reachable, get_irn_idx(node)))
171 be_liveness_remove(env->lv, node);
177 void be_remove_dead_nodes_from_schedule(be_irg_t *birg)
179 ir_graph *irg = be_get_birg_irg(birg);
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);
186 // mark all reachable nodes
187 irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
189 // walk schedule and remove non-marked nodes
190 irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);