Fixed some warning about unused variables.
[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  * @version     $Id$
25  */
26 #include "config.h"
27
28 #include <stdlib.h>
29
30 #include "irprintf.h"
31 #include "irgwalk.h"
32 #include "firm_types.h"
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
35 #include "ircons.h"
36 #include "irextbb.h"
37 #include "irgmod.h"
38 #include "debug.h"
39
40 #include "bemodule.h"
41 #include "bearch.h"
42 #include "besched.h"
43 #include "beutil.h"
44 #include "belistsched.h"
45 #include "belive.h"
46
47 #include "lc_opts.h"
48 #include "lc_opts_enum.h"
49 #include "irtools.h"
50
51 #define SCHED_INITIAL_GRANULARITY (1 << 14)
52
53 static void sched_renumber(const ir_node *block)
54 {
55         ir_node *irn;
56         sched_info_t *inf;
57         sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
58
59         sched_foreach(block, irn) {
60                 inf = get_irn_sched_info(irn);
61                 inf->time_step = step;
62                 step += SCHED_INITIAL_GRANULARITY;
63         }
64 }
65
66 static inline void sched_set_time_stamp(const ir_node *irn)
67 {
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;
73
74         /*
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
77          * neighbours.
78          */
79         if(before_ts >= after_ts) {
80                 info->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
81                 /* overflow? */
82                 if (info->time_step <= before_ts) {
83                         sched_renumber(get_nodes_block(irn));
84                 }
85         } else {
86                 sched_timestep_t ts = (before_ts + after_ts) / 2;
87
88                 /*
89                  * If the resolution went out, we have to renumber
90                  * this block.
91                  */
92                 if(ts == before_ts || ts == after_ts)
93                         sched_renumber(get_nodes_block(irn));
94                 else
95                         info->time_step = ts;
96         }
97 }
98
99 void sched_add_before(ir_node *before, ir_node *irn)
100 {
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));
110
111         info->prev = prev;
112         info->next = next;
113         prev_info->next = irn;
114         next_info->prev = irn;
115         sched_set_time_stamp(irn);
116 }
117
118 void sched_add_after(ir_node *after, ir_node *irn)
119 {
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));
129
130         info->prev = prev;
131         info->next = next;
132         prev_info->next = irn;
133         next_info->prev = irn;
134         sched_set_time_stamp(irn);
135 }
136
137 void sched_remove(ir_node *irn)
138 {
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));
145
146         prev_info->next = next;
147         next_info->prev = prev;
148         info->next      = NULL;
149         info->prev      = NULL;
150 }
151
152
153
154 static be_module_list_entry_t *schedulers;
155 static schedule_func           scheduler;
156
157 void be_register_scheduler(const char *name, schedule_func func)
158 {
159         if (scheduler == NULL)
160                 scheduler = func;
161         be_add_module_to_list(&schedulers, name, func);
162 }
163
164 void be_schedule_graph(ir_graph *irg)
165 {
166         scheduler(irg);
167 }
168
169 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched)
170 void be_init_sched(void)
171 {
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);
175 }