- removed useless be_req_t which was a wrapper around an arch_register_req_t
[libfirm] / ir / be / mips / mips_scheduler.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   Mips implementation of list scheduler selector
23  * @author  Matthias Braun, Mehdi
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include "mips_scheduler.h"
29
30 #include "../besched_t.h"
31 #include "be.h"
32 #include "../beabi.h"
33 #include "iredges.h"
34 #include "ircons.h"
35 #include "gen_mips_regalloc_if.h"
36
37 #include "mips_new_nodes.h"
38
39 list_sched_selector_t mips_sched_selector;
40
41 typedef struct {
42         pset *div_set;
43         /**
44          * This array holds an entry for each register that specifies how much cycles
45          * have to pass before we can access that register again
46          * (because mips will write the register value back in the WB phase of the pipeline)
47          */
48         int busy_registers[N_mips_gp_REGS];
49         /// current block
50         ir_node* block;
51         ir_node* last_nop;
52 } mips_sched_env_t;
53
54 /* Matze: deprecated and totally broken */
55 #if 0
56
57 static void *mips_scheduler_init_graph(const list_sched_selector_t *vtab, ir_graph *irg)
58 {
59         mips_sched_env_t *sched_env = XMALLOCZ(mips_sched_env_t);
60
61         sched_env->div_set = new_pset(pset_default_ptr_cmp, 4);
62
63         return sched_env;
64 }
65
66 static void mips_scheduler_finish_graph(void* graph_env)
67 {
68         mips_sched_env_t *sched_env = (mips_sched_env_t*) graph_env;
69         del_pset(sched_env->div_set);
70 }
71
72 static void *mips_scheduler_init_block(void *graph_env, ir_node *block)
73 {
74         mips_sched_env_t *sched_env = (mips_sched_env_t*) graph_env;
75         assert(pset_count(sched_env->div_set) == 0);
76         srand(12234);
77         // TODO later we might have blocks that don't end in a jump
78         memset(&sched_env->busy_registers, 0, sizeof(sched_env->busy_registers));
79         sched_env->block = block;
80         sched_env->last_nop = NULL;
81         return sched_env;
82 }
83
84 static void mips_scheduler_finish_block(void* graph_env)
85 {
86         mips_sched_env_t *sched_env = (mips_sched_env_t*) graph_env;
87         // attach last nop to end node (so that firm doesn't discard it)
88         if(sched_env->last_nop != NULL) {
89                 ir_node* end = get_irg_end(get_irn_irg(sched_env->block));
90                 (void) end;
91                 // TODO
92         }
93         sched_env->block = NULL;
94 }
95
96 static int mips_scheduler_to_appear_in_schedule(void *block_env, const ir_node *irn)
97 {
98         return is_mips_irn(irn) && !is_mips_zero(irn) && !is_mips_reinterpret_conv(irn) && !is_mips_fallthrough(irn);
99 }
100
101 static void mips_collect_mflohis(pset* set, ir_node* node) {
102         // construct a list of nodes that need to be scheduled before
103         // we are allowed to schedule another div or mul instruction
104         const ir_edge_t *edge, *edge2;
105
106         if(is_mips_div(node)) {
107                 foreach_out_edge(node, edge) {
108                         const ir_node* node2 = get_edge_src_irn(edge);
109
110                         assert(is_Proj(node2));
111                         foreach_out_edge(node2, edge2) {
112                                 const ir_node* node3 = get_edge_src_irn(edge2);
113                                 if(is_mips_mfhi(node3) || is_mips_mflo(node3))
114                                         pset_insert_ptr(set, node3);
115                         }
116                 }
117         } else if(is_mips_mult(node)) {
118                 foreach_out_edge(node, edge) {
119                         const ir_node* node2 = get_edge_src_irn(edge);
120
121                         if(is_mips_mfhi(node2) || is_mips_mflo(node2))
122                                 pset_insert_ptr(set, node2);
123                 }
124         }
125 }
126
127 static int mips_scheduler_node_allowed(mips_sched_env_t *sched_env, ir_node* node)
128 {
129         if(pset_count(sched_env->div_set) != 0 && (is_mips_div(node) || is_mips_mult(node))) {
130                 return 0;
131         }
132
133         return 1;
134 }
135
136 static ir_node *mips_scheduler_select(void *block_env, nodeset *ready_set, nodeset *live_set)
137 {
138         mips_sched_env_t *sched_env = (mips_sched_env_t*) block_env;
139         ir_node *node = NULL;
140         ir_node *block = sched_env->block;
141         ir_node *condjmp = NULL;
142         ir_graph *irg = get_irn_irg(block);
143         int have_non_branch_nodes = 0;
144
145         // test all nodes in the ready set and take the first non-branch that
146         // is allowed
147         for (node = nodeset_first(ready_set); node != NULL; node = nodeset_next(ready_set)) {
148                 if (arch_irn_class_is(node, branch)) {
149                         if (is_irn_forking(node))
150                                 condjmp = node;
151                         continue;
152                 }
153
154                 have_non_branch_nodes = 1;
155
156                 if (mips_scheduler_node_allowed(sched_env, node))
157                 {
158                         nodeset_break(ready_set);
159
160                         // TODO update busy_registers
161
162                         if (is_mips_div(node) || is_mips_mult(node)) {
163                                 mips_collect_mflohis(sched_env->div_set, node);
164                         } else if(is_mips_mflo(node) || is_mips_mfhi(node)) {
165                                 pset_remove_ptr(sched_env->div_set, node);
166                         }
167
168                         return node;
169                 }
170         }
171
172         // if we arrive here no non-branch node was found that we can emit
173
174         // return a branch if there are just branches left
175         if(!have_non_branch_nodes) {
176                 // schedule conditional branches before non-conditional ones
177                 if(condjmp != NULL) {
178                         return condjmp;
179                 }
180                 node = nodeset_first(ready_set);
181                 assert(arch_irn_class_is(node, branch));
182                 nodeset_break(ready_set);
183                 return node;
184         }
185
186         // emit a nop
187         node = new_rd_mips_nop(NULL, irg, block, mode_M);
188         keep_alive(node);
189         return node;
190 }
191
192 #endif
193
194 static
195 int mips_to_appear_in_schedule(void *block_env, const ir_node *node)
196 {
197         (void) block_env;
198
199         if(!is_mips_irn(node))
200                 return -1;
201         if(is_mips_zero(node) || is_mips_Immediate(node))
202                 return 0;
203
204         return 1;
205 }
206
207 list_sched_selector_t  mips_selector;
208
209 /**
210  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
211  */
212 const list_sched_selector_t *mips_get_list_sched_selector(const void *self,
213                 list_sched_selector_t *selector)
214 {
215         (void) self;
216 #if 0
217         memset(&mips_sched_selector, 0, sizeof(mips_sched_selector));
218         mips_sched_selector.init_graph = mips_scheduler_init_graph;
219         mips_sched_selector.init_block = mips_scheduler_init_block;
220         mips_sched_selector.select = mips_scheduler_select;
221         mips_sched_selector.to_appear_in_schedule = mips_scheduler_to_appear_in_schedule;
222         mips_sched_selector.finish_block = mips_scheduler_finish_block;
223         mips_sched_selector.finish_graph = mips_scheduler_finish_graph;
224         //return &mips_sched_selector;
225 #endif
226         memcpy(&mips_selector, selector, sizeof(mips_selector));
227         mips_selector.to_appear_in_schedule = mips_to_appear_in_schedule;
228
229         return &mips_selector;
230 }
231
232 const ilp_sched_selector_t *mips_get_ilp_sched_selector(const void *self)
233 {
234         (void) self;
235         return NULL;
236 }