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