2 * Copyright (C) 1995-2008 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
32 #include "firm_types.h"
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
44 #include "belistsched.h"
48 #include "lc_opts_enum.h"
51 #define SCHED_INITIAL_GRANULARITY (1 << 14)
53 static void sched_renumber(const ir_node *block)
57 sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
59 sched_foreach(block, irn) {
60 inf = get_irn_sched_info(irn);
61 inf->time_step = step;
62 step += SCHED_INITIAL_GRANULARITY;
66 static inline void sched_set_time_stamp(const ir_node *irn)
68 sched_info_t *info = get_irn_sched_info(irn);
69 const sched_info_t *prev_info = get_irn_sched_info(info->prev);
70 const sched_info_t *next_info = get_irn_sched_info(info->next);
71 sched_timestep_t before_ts = prev_info->time_step;
72 sched_timestep_t after_ts = next_info->time_step;
75 * If we are the last, we can give us a big time step,
76 * else we have to compute our time step from our
79 if(before_ts >= after_ts) {
80 info->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
82 if (info->time_step <= before_ts) {
83 sched_renumber(get_nodes_block(irn));
86 sched_timestep_t ts = (before_ts + after_ts) / 2;
89 * If the resolution went out, we have to renumber
92 if(ts == before_ts || ts == after_ts)
93 sched_renumber(get_nodes_block(irn));
99 void sched_add_before(ir_node *before, ir_node *irn)
101 sched_info_t *info = get_irn_sched_info(irn);
102 ir_node *next = before;
103 sched_info_t *next_info = get_irn_sched_info(next);
104 ir_node *prev = next_info->prev;
105 sched_info_t *prev_info = get_irn_sched_info(prev);
106 assert(sched_is_scheduled(before));
107 assert(!sched_is_scheduled(irn));
108 assert(!is_Proj(before));
109 assert(!is_Proj(irn));
113 prev_info->next = irn;
114 next_info->prev = irn;
115 sched_set_time_stamp(irn);
118 void sched_add_after(ir_node *after, ir_node *irn)
120 sched_info_t *info = get_irn_sched_info(irn);
121 ir_node *prev = after;
122 sched_info_t *prev_info = get_irn_sched_info(prev);
123 ir_node *next = prev_info->next;
124 sched_info_t *next_info = get_irn_sched_info(next);
125 assert(sched_is_scheduled(after));
126 assert(!sched_is_scheduled(irn));
127 assert(!is_Proj(after));
128 assert(!is_Proj(irn));
132 prev_info->next = irn;
133 next_info->prev = irn;
134 sched_set_time_stamp(irn);
137 void sched_remove(ir_node *irn)
139 sched_info_t *info = get_irn_sched_info(irn);
140 ir_node *prev = info->prev;
141 ir_node *next = info->next;
142 sched_info_t *prev_info = get_irn_sched_info(prev);
143 sched_info_t *next_info = get_irn_sched_info(next);
144 assert(sched_is_scheduled(irn));
146 prev_info->next = next;
147 next_info->prev = prev;
154 static be_module_list_entry_t *schedulers;
155 static schedule_func scheduler;
157 void be_register_scheduler(const char *name, schedule_func func)
159 if (scheduler == NULL)
161 be_add_module_to_list(&schedulers, name, func);
164 void be_schedule_graph(ir_graph *irg)
169 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched);
170 void be_init_sched(void)
172 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
173 be_add_module_list_opt(be_grp, "scheduler", "scheduling algorithm",
174 &schedulers, (void**)&scheduler);