added scheduling function (to_be_scheduled)
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "pseudo_irg.h"
6 #include "irgwalk.h"
7 #include "irprog.h"
8 #include "irprintf.h"
9 #include "iredges_t.h"
10
11 #include "bitset.h"
12 #include "debug.h"
13
14 #include "../bearch.h"                /* the general register allocator interface */
15 #include "../benode_t.h"
16 #include "bearch_ia32_t.h"
17
18 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
19 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
20 #include "ia32_gen_decls.h"           /* interface declaration emitter */
21 #include "ia32_transform.h"
22 #include "ia32_emitter.h"
23 #include "ia32_map_regs.h"
24
25 #define DEBUG_MODULE "ir.be.isa.ia32"
26
27 /* TODO: ugly */
28 static set *cur_reg_set = NULL;
29
30
31
32 /**************************************************
33  *                         _ _              _  __
34  *                        | | |            (_)/ _|
35  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
36  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
37  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
38  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
39  *            __/ |
40  *           |___/
41  **************************************************/
42
43 static ir_node *my_skip_proj(const ir_node *n) {
44         while (is_Proj(n))
45                 n = get_Proj_pred(n);
46         return (ir_node *)n;
47 }
48
49 static int is_Call_Proj(const ir_node *n) {
50         if (is_Proj(n)                               &&
51                 is_Proj(get_Proj_pred(n))                &&
52                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
53                 is_ia32_Call(get_Proj_pred(get_Proj_pred(n))))
54         {
55                 return 1;
56         }
57
58         return 0;
59 }
60
61 static int is_used_by_Keep(const ir_node *n) {
62         return be_is_Keep(get_edge_src_irn(get_irn_out_edge_first(n)));
63 }
64
65 /**
66  * Return register requirements for an ia32 node.
67  * If the node returns a tuple (mode_T) then the proj's
68  * will be asked for this information.
69  */
70 static const arch_register_req_t *ia32_get_irn_reg_req(const arch_irn_ops_t *self, arch_register_req_t *req, const ir_node *irn, int pos) {
71         const ia32_register_req_t *irn_req;
72         long                       node_pos = pos == -1 ? 0 : pos;
73         ir_mode                   *mode     = get_irn_mode(irn);
74         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
75
76         if (mode == mode_T || mode == mode_M) {
77                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
78                 return NULL;
79         }
80
81         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
82
83
84         if (is_Call_Proj(irn) && is_used_by_Keep(irn)) {
85                 irn_req = ia32_projnum_reg_req_map[get_Proj_proj(irn)];
86                 memcpy(req, &(irn_req->req), sizeof(*req));
87                 return req;
88         }
89         else if (is_Proj(irn)) {
90                 if (pos == -1) {
91                         node_pos = ia32_translate_proj_pos(irn);
92                 }
93                 else {
94                         node_pos = pos;
95                 }
96
97                 irn = my_skip_proj(irn);
98
99                 DBG((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
100         }
101
102         if (is_ia32_irn(irn)) {
103                 if (pos >= 0) {
104                         irn_req = get_ia32_in_req(irn, pos);
105                 }
106                 else {
107                         irn_req = get_ia32_out_req(irn, node_pos);
108                 }
109
110                 DBG((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
111
112                 memcpy(req, &(irn_req->req), sizeof(*req));
113
114                 if (arch_register_req_is(&(irn_req->req), should_be_same) ||
115                         arch_register_req_is(&(irn_req->req), should_be_different)) {
116                         assert(irn_req->pos >= 0 && "should be same/different constraint for in -> out NYI");
117                         req->other = get_irn_n(irn, irn_req->pos);
118                 }
119         }
120         else {
121                 /* treat Phi like Const with default requirements */
122                 if (is_Phi(irn)) {
123                         DBG((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
124                         if (mode_is_float(mode))
125                                 memcpy(req, &(ia32_default_req_ia32_floating_point.req), sizeof(*req));
126                         else if (mode_is_int(mode) || mode_is_reference(mode))
127                                 memcpy(req, &(ia32_default_req_ia32_general_purpose.req), sizeof(*req));
128                         else if (mode == mode_T || mode == mode_M) {
129                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
130                                 return NULL;
131                         }
132                         else
133                                 assert(0 && "unsupported Phi-Mode");
134                 }
135                 else if (get_irn_op(irn) == op_Start) {
136                         DBG((mod, LEVEL_1, "returning reqs none for ProjX -> Start (%+F )\n", irn));
137                         switch (node_pos) {
138                                 case pn_Start_X_initial_exec:
139                                 case pn_Start_P_value_arg_base:
140                                 case pn_Start_P_globals:
141                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
142                                         break;
143                                 case pn_Start_P_frame_base:
144                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
145                                         break;
146                                 case pn_Start_T_args:
147                                         assert(0 && "ProjT(pn_Start_T_args) should not be asked");
148                         }
149                 }
150                 else if (get_irn_op(irn) == op_Return && pos > 0) {
151                         DBG((mod, LEVEL_1, "returning reqs EAX for %+F\n", irn));
152                         memcpy(req, &(ia32_default_req_ia32_general_purpose_eax.req), sizeof(*req));
153                 }
154                 else {
155                         DBG((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
156                         req = NULL;
157                 }
158         }
159
160         return req;
161 }
162
163 static void ia32_set_irn_reg(const arch_irn_ops_t *self, ir_node *irn, const arch_register_t *reg) {
164         int pos = 0;
165
166         if (is_Call_Proj(irn) && is_used_by_Keep(irn)) {
167                 /* don't skip the proj, we want to take the else below */
168         }
169         if (is_Proj(irn)) {
170                 pos = ia32_translate_proj_pos(irn);
171                 irn = my_skip_proj(irn);
172         }
173
174         if (is_ia32_irn(irn)) {
175                 const arch_register_t **slots;
176
177                 slots      = get_ia32_slots(irn);
178                 slots[pos] = reg;
179         }
180         else {
181                 ia32_set_firm_reg(irn, reg, cur_reg_set);
182         }
183 }
184
185 static const arch_register_t *ia32_get_irn_reg(const arch_irn_ops_t *self, const ir_node *irn) {
186         int pos = 0;
187         const arch_register_t *reg = NULL;
188
189         if (is_Call_Proj(irn) && is_used_by_Keep(irn)) {
190                 /* don't skip the proj, we want to take the else below */
191         }
192         else if (is_Proj(irn)) {
193                 pos = ia32_translate_proj_pos(irn);
194                 irn = my_skip_proj(irn);
195         }
196
197         if (is_ia32_irn(irn)) {
198                 const arch_register_t **slots;
199                 slots = get_ia32_slots(irn);
200                 reg   = slots[pos];
201         }
202         else {
203                 reg = ia32_get_firm_reg(irn, cur_reg_set);
204         }
205
206         return reg;
207 }
208
209 static arch_irn_class_t ia32_classify(const arch_irn_ops_t *self, const ir_node *irn) {
210         irn = my_skip_proj(irn);
211         if (is_cfop(irn))
212                 return arch_irn_class_branch;
213         else if (is_ia32_Call(irn))
214                 return arch_irn_class_call;
215         else if (is_ia32_irn(irn))
216                 return arch_irn_class_normal;
217         else
218                 return 0;
219 }
220
221 static arch_irn_flags_t ia32_get_flags(const arch_irn_ops_t *self, const ir_node *irn) {
222         irn = my_skip_proj(irn);
223         if (is_ia32_irn(irn))
224                 return get_ia32_flags(irn);
225         else {
226                 ir_printf("don't know flags of %+F\n", irn);
227                 return 0;
228         }
229 }
230
231 /* fill register allocator interface */
232
233 static const arch_irn_ops_t ia32_irn_ops = {
234         ia32_get_irn_reg_req,
235         ia32_set_irn_reg,
236         ia32_get_irn_reg,
237         ia32_classify,
238         ia32_get_flags
239 };
240
241
242
243 /**************************************************
244  *                _                         _  __
245  *               | |                       (_)/ _|
246  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
247  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
248  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
249  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
250  *                        __/ |
251  *                       |___/
252  **************************************************/
253
254 /**
255  * Transforms the standard firm graph into
256  * an ia32 firm graph
257  */
258 static void ia32_prepare_graph(void *self) {
259         ia32_code_gen_t *cg  = self;
260
261         if (! is_pseudo_ir_graph(cg->irg))
262                 irg_walk_blkwise_graph(cg->irg, NULL, ia32_transform_node, cg);
263 }
264
265
266
267 /**
268  * Dummy functions for hooks we don't need but which must be filled.
269  */
270 static void ia32_before_sched(void *self) {
271 }
272
273 static void ia32_before_ra(void *self) {
274 }
275
276
277
278 /**
279  * Emits the code, closes the output file and frees
280  * the code generator interface.
281  */
282 static void ia32_codegen(void *self) {
283         ia32_code_gen_t *cg = self;
284         ir_graph       *irg = cg->irg;
285         FILE           *out = cg->out;
286
287         if (cg->emit_decls) {
288                 ia32_gen_decls(cg->out);
289                 cg->emit_decls = 0;
290         }
291
292 //      ia32_finish_irg(irg);
293         ia32_gen_routine(out, irg, cg->arch_env);
294
295         cur_reg_set = NULL;
296
297         /* de-allocate code generator */
298         del_set(cg->reg_set);
299         free(self);
300 }
301
302 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
303
304 static const arch_code_generator_if_t ia32_code_gen_if = {
305         ia32_cg_init,
306         ia32_prepare_graph,
307         ia32_before_sched,   /* before scheduling hook */
308         ia32_before_ra,      /* before register allocation hook */
309         ia32_codegen         /* emit && done */
310 };
311
312 /**
313  * Initializes the code generator.
314  */
315 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
316         ia32_isa_t      *isa = (ia32_isa_t *)arch_env->isa;
317         ia32_code_gen_t *cg  = malloc(sizeof(*cg));
318
319         cg->impl       = &ia32_code_gen_if;
320         cg->irg        = irg;
321         cg->reg_set    = new_set(ia32_cmp_irn_reg_assoc, 1024);
322         cg->mod        = firm_dbg_register("be.transform.ia32");
323         cg->out        = F;
324         cg->arch_env   = arch_env;
325
326         isa->num_codegens++;
327
328         if (isa->num_codegens > 1)
329                 cg->emit_decls = 0;
330         else
331                 cg->emit_decls = 1;
332
333         cur_reg_set = cg->reg_set;
334
335         return (arch_code_generator_t *)cg;
336 }
337
338
339
340 /*****************************************************************
341  *  ____             _                  _   _____  _____
342  * |  _ \           | |                | | |_   _|/ ____|  /\
343  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
344  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
345  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
346  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
347  *
348  *****************************************************************/
349
350 /**
351  * Initializes the backend ISA and opens the output file.
352  */
353 static void *ia32_init(void) {
354         static int inited = 0;
355         ia32_isa_t *isa   = malloc(sizeof(*isa));
356
357         isa->impl = &ia32_isa_if;
358
359         if(inited)
360                 return NULL;
361
362         inited = 1;
363
364         isa->num_codegens    = 0;
365         isa->reg_projnum_map = new_set(ia32_cmp_reg_projnum_assoc, 1024);
366
367         ia32_register_init(isa);
368         ia32_create_opcodes();
369
370         return isa;
371 }
372
373
374
375 /**
376  * Closes the output file and frees the ISA structure.
377  */
378 static void ia32_done(void *self) {
379         free(self);
380 }
381
382
383
384 static int ia32_get_n_reg_class(const void *self) {
385         return N_CLASSES;
386 }
387
388 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
389         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
390         return &ia32_reg_classes[i];
391 }
392
393 static const arch_irn_ops_t *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
394         return &ia32_irn_ops;
395 }
396
397 const arch_irn_handler_t ia32_irn_handler = {
398         ia32_get_irn_ops
399 };
400
401 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
402         return &ia32_irn_handler;
403 }
404
405 long ia32_get_call_projnum_for_reg(const void *self, const arch_register_t *reg) {
406         ia32_isa_t *isa = (ia32_isa_t *)self;
407         return ia32_get_reg_projnum(reg, isa->reg_projnum_map);
408 }
409
410 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
411         return is_ia32_irn(irn);
412 }
413
414 /**
415  * Initializes the code generator interface.
416  */
417 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
418         return &ia32_code_gen_if;
419 }
420
421 list_sched_selector_t ia32_sched_selector;
422
423 /**
424  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
425  */
426 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
427         memcpy(&ia32_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
428         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
429         return &ia32_sched_selector;
430 }
431
432 #ifdef WITH_LIBCORE
433 static void ia32_register_options(lc_opt_entry_t *ent)
434 {
435 }
436 #endif /* WITH_LIBCORE */
437
438 const arch_isa_if_t ia32_isa_if = {
439 #ifdef WITH_LIBCORE
440         ia32_register_options,
441 #endif
442         ia32_init,
443         ia32_done,
444         ia32_get_n_reg_class,
445         ia32_get_reg_class,
446         ia32_get_irn_handler,
447         ia32_get_code_generator_if,
448         ia32_get_list_sched_selector,
449         ia32_get_call_projnum_for_reg
450 };