irdom: move some functions to private API
[libfirm] / ir / be / besched.c
1 /*
2  * Copyright (C) 1995-2008 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  */
25 #include "config.h"
26
27 #include <stdlib.h>
28
29 #include "irprintf.h"
30 #include "irgwalk.h"
31 #include "firm_types.h"
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
34 #include "ircons.h"
35 #include "irextbb.h"
36 #include "irgmod.h"
37 #include "debug.h"
38
39 #include "bemodule.h"
40 #include "bearch.h"
41 #include "besched.h"
42 #include "beutil.h"
43 #include "belistsched.h"
44 #include "belive.h"
45
46 #include "lc_opts.h"
47 #include "lc_opts_enum.h"
48 #include "irtools.h"
49
50 #define SCHED_INITIAL_GRANULARITY (1 << 14)
51
52 static void sched_renumber(const ir_node *block)
53 {
54         ir_node *irn;
55         sched_info_t *inf;
56         sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
57
58         sched_foreach(block, irn) {
59                 inf = get_irn_sched_info(irn);
60                 inf->time_step = step;
61                 step += SCHED_INITIAL_GRANULARITY;
62         }
63 }
64
65 static inline void sched_set_time_stamp(const ir_node *irn)
66 {
67         sched_info_t       *info      = get_irn_sched_info(irn);
68         const sched_info_t *prev_info = get_irn_sched_info(info->prev);
69         const sched_info_t *next_info = get_irn_sched_info(info->next);
70         sched_timestep_t    before_ts = prev_info->time_step;
71         sched_timestep_t    after_ts  = next_info->time_step;
72
73         /*
74          * If we are the last, we can give us a big time step,
75          * else we have to compute our time step from our
76          * neighbours.
77          */
78         if(before_ts >= after_ts) {
79                 info->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
80                 /* overflow? */
81                 if (info->time_step <= before_ts) {
82                         sched_renumber(get_nodes_block(irn));
83                 }
84         } else {
85                 sched_timestep_t ts = (before_ts + after_ts) / 2;
86
87                 /*
88                  * If the resolution went out, we have to renumber
89                  * this block.
90                  */
91                 if(ts == before_ts || ts == after_ts)
92                         sched_renumber(get_nodes_block(irn));
93                 else
94                         info->time_step = ts;
95         }
96 }
97
98 void sched_add_before(ir_node *before, ir_node *irn)
99 {
100         sched_info_t *info      = get_irn_sched_info(irn);
101         ir_node      *next      = before;
102         sched_info_t *next_info = get_irn_sched_info(next);
103         ir_node      *prev      = next_info->prev;
104         sched_info_t *prev_info = get_irn_sched_info(prev);
105         assert(sched_is_scheduled(before));
106         assert(!sched_is_scheduled(irn));
107         assert(!is_Proj(before));
108         assert(!is_Proj(irn));
109
110         info->prev = prev;
111         info->next = next;
112         prev_info->next = irn;
113         next_info->prev = irn;
114         sched_set_time_stamp(irn);
115 }
116
117 void sched_add_after(ir_node *after, ir_node *irn)
118 {
119         sched_info_t *info      = get_irn_sched_info(irn);
120         ir_node      *prev      = after;
121         sched_info_t *prev_info = get_irn_sched_info(prev);
122         ir_node      *next      = prev_info->next;
123         sched_info_t *next_info = get_irn_sched_info(next);
124         assert(sched_is_scheduled(after));
125         assert(!sched_is_scheduled(irn));
126         assert(!is_Proj(after));
127         assert(!is_Proj(irn));
128
129         info->prev = prev;
130         info->next = next;
131         prev_info->next = irn;
132         next_info->prev = irn;
133         sched_set_time_stamp(irn);
134 }
135
136 void sched_remove(ir_node *irn)
137 {
138         sched_info_t *info      = get_irn_sched_info(irn);
139         ir_node      *prev      = info->prev;
140         ir_node      *next      = info->next;
141         sched_info_t *prev_info = get_irn_sched_info(prev);
142         sched_info_t *next_info = get_irn_sched_info(next);
143         assert(sched_is_scheduled(irn));
144
145         prev_info->next = next;
146         next_info->prev = prev;
147         info->next      = NULL;
148         info->prev      = NULL;
149 }
150
151
152
153 static be_module_list_entry_t *schedulers;
154 static schedule_func           scheduler;
155
156 void be_register_scheduler(const char *name, schedule_func func)
157 {
158         if (scheduler == NULL)
159                 scheduler = func;
160         be_add_module_to_list(&schedulers, name, func);
161 }
162
163 void be_schedule_graph(ir_graph *irg)
164 {
165         scheduler(irg);
166 }
167
168 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched)
169 void be_init_sched(void)
170 {
171         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
172         be_add_module_list_opt(be_grp, "scheduler", "scheduling algorithm",
173                                &schedulers, (void**)&scheduler);
174 }