412e331f0ed31190fd6f43f2a2cd9f2ace244ccd
[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 #include "ircons.h"
11
12 #include "bitset.h"
13 #include "debug.h"
14
15 #include "../bearch.h"                /* the general register allocator interface */
16 #include "../benode_t.h"
17 #include "bearch_ia32_t.h"
18
19 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
20 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
21 #include "ia32_gen_decls.h"           /* interface declaration emitter */
22 #include "ia32_transform.h"
23 #include "ia32_emitter.h"
24 #include "ia32_map_regs.h"
25
26 #define DEBUG_MODULE "ir.be.isa.ia32"
27
28 /* TODO: ugly */
29 static set *cur_reg_set = NULL;
30
31 #undef is_Start
32 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
33
34 /**************************************************
35  *                         _ _              _  __
36  *                        | | |            (_)/ _|
37  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
38  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
39  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
40  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
41  *            __/ |
42  *           |___/
43  **************************************************/
44
45 static ir_node *my_skip_proj(const ir_node *n) {
46         while (is_Proj(n))
47                 n = get_Proj_pred(n);
48         return (ir_node *)n;
49 }
50
51 static int is_Call_Proj(const ir_node *n) {
52         if (is_Proj(n)                               &&
53                 is_Proj(get_Proj_pred(n))                &&
54                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
55                 is_ia32_Call(get_Proj_pred(get_Proj_pred(n))))
56         {
57                 return 1;
58         }
59
60         return 0;
61 }
62
63 static int is_Start_Proj(const ir_node *n) {
64         if (is_Proj(n)                               &&
65                 is_Proj(get_Proj_pred(n))                &&
66                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
67                 is_Start(get_Proj_pred(get_Proj_pred(n))))
68         {
69                 return 1;
70         }
71
72         return 0;
73 }
74
75 static int is_P_frame_base_Proj(const ir_node *n) {
76         if (is_Proj(n)                                    &&
77                 is_Start(get_Proj_pred(n)) &&
78                 get_Proj_proj(n) == pn_Start_P_frame_base)
79         {
80                 return 1;
81         }
82
83         return 0;
84 }
85
86 static int is_used_by_Keep(const ir_node *n) {
87         return be_is_Keep(get_edge_src_irn(get_irn_out_edge_first(n)));
88 }
89
90 /**
91  * Return register requirements for an ia32 node.
92  * If the node returns a tuple (mode_T) then the proj's
93  * will be asked for this information.
94  */
95 static const arch_register_req_t *ia32_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
96         const ia32_register_req_t *irn_req;
97         long                       node_pos = pos == -1 ? 0 : pos;
98         ir_mode                   *mode     = get_irn_mode(irn);
99         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
100         const ia32_irn_ops_t      *ops      = self;
101
102         if (mode == mode_T || mode == mode_M) {
103                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
104                 return NULL;
105         }
106
107         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
108
109
110         if (is_Call_Proj(irn) && is_used_by_Keep(irn)) {
111                 irn_req = ia32_projnum_reg_req_map[get_Proj_proj(irn)];
112                 memcpy(req, &(irn_req->req), sizeof(*req));
113                 return req;
114         }
115         else if (is_Start_Proj(irn)) {
116                 irn_req = ops->cg->reg_param_req[get_Proj_proj(irn)];
117                 assert(irn_req && "missing requirement for regparam");
118                 memcpy(req, &(irn_req->req), sizeof(*req));
119                 return req;
120         }
121         else if (is_Proj(irn)) {
122                 if (pos == -1) {
123                         node_pos = ia32_translate_proj_pos(irn);
124                 }
125                 else {
126                         node_pos = pos;
127                 }
128
129                 irn = my_skip_proj(irn);
130
131                 DBG((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
132         }
133
134         if (is_ia32_irn(irn)) {
135                 if (pos >= 0) {
136                         irn_req = get_ia32_in_req(irn, pos);
137                 }
138                 else {
139                         irn_req = get_ia32_out_req(irn, node_pos);
140                 }
141
142                 DBG((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
143
144                 memcpy(req, &(irn_req->req), sizeof(*req));
145
146                 if (arch_register_req_is(&(irn_req->req), should_be_same) ||
147                         arch_register_req_is(&(irn_req->req), should_be_different)) {
148                         assert(irn_req->pos >= 0 && "should be same/different constraint for in -> out NYI");
149                         req->other = get_irn_n(irn, irn_req->pos);
150                 }
151         }
152         else {
153                 /* treat Phi like Const with default requirements */
154                 if (is_Phi(irn)) {
155                         DBG((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
156                         if (mode_is_float(mode))
157                                 memcpy(req, &(ia32_default_req_ia32_floating_point.req), sizeof(*req));
158                         else if (mode_is_int(mode) || mode_is_reference(mode))
159                                 memcpy(req, &(ia32_default_req_ia32_general_purpose.req), sizeof(*req));
160                         else if (mode == mode_T || mode == mode_M) {
161                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
162                                 return NULL;
163                         }
164                         else
165                                 assert(0 && "unsupported Phi-Mode");
166                 }
167                 else if (is_Start(irn)) {
168                         DBG((mod, LEVEL_1, "returning reqs none for ProjX -> Start (%+F )\n", irn));
169                         switch (node_pos) {
170                                 case pn_Start_X_initial_exec:
171                                 case pn_Start_P_value_arg_base:
172                                 case pn_Start_P_globals:
173                                 case pn_Start_P_frame_base:
174                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
175                                         break;
176                                 case pn_Start_T_args:
177                                         assert(0 && "ProjT(pn_Start_T_args) should not be asked");
178                         }
179                 }
180                 else if (get_irn_op(irn) == op_Return && pos > 0) {
181                         DBG((mod, LEVEL_1, "returning reqs EAX for %+F\n", irn));
182                         memcpy(req, &(ia32_default_req_ia32_general_purpose_eax.req), sizeof(*req));
183                 }
184                 else {
185                         DBG((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
186                         req = NULL;
187                 }
188         }
189
190         return req;
191 }
192
193 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
194         int pos = 0;
195
196         if ((is_Call_Proj(irn) && is_used_by_Keep(irn)) ||
197                 is_P_frame_base_Proj(irn)                   ||
198                 is_Start_Proj(irn))
199         {
200                 /* don't skip the proj, we want to take the else below */
201         }
202         else if (is_Proj(irn)) {
203                 pos = ia32_translate_proj_pos(irn);
204                 irn = my_skip_proj(irn);
205         }
206
207         if (is_ia32_irn(irn)) {
208                 const arch_register_t **slots;
209
210                 slots      = get_ia32_slots(irn);
211                 slots[pos] = reg;
212         }
213         else {
214                 ia32_set_firm_reg(irn, reg, cur_reg_set);
215         }
216 }
217
218 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
219         int pos = 0;
220         const arch_register_t *reg = NULL;
221
222         if ((is_Call_Proj(irn) && is_used_by_Keep(irn)) ||
223                 is_P_frame_base_Proj(irn)                   ||
224                 is_Start_Proj(irn))
225         {
226                 /* don't skip the proj, we want to take the else below */
227         }
228         else if (is_Proj(irn)) {
229                 pos = ia32_translate_proj_pos(irn);
230                 irn = my_skip_proj(irn);
231         }
232
233         if (is_ia32_irn(irn)) {
234                 const arch_register_t **slots;
235                 slots = get_ia32_slots(irn);
236                 reg   = slots[pos];
237         }
238         else {
239                 reg = ia32_get_firm_reg(irn, cur_reg_set);
240         }
241
242         return reg;
243 }
244
245 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
246         irn = my_skip_proj(irn);
247         if (is_cfop(irn))
248                 return arch_irn_class_branch;
249         else if (is_ia32_Call(irn))
250                 return arch_irn_class_call;
251         else if (is_ia32_irn(irn))
252                 return arch_irn_class_normal;
253         else
254                 return 0;
255 }
256
257 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
258         irn = my_skip_proj(irn);
259         if (is_ia32_irn(irn))
260                 return get_ia32_flags(irn);
261         else {
262                 ir_printf("don't know flags of %+F\n", irn);
263                 return 0;
264         }
265 }
266
267 /* fill register allocator interface */
268
269 static const arch_irn_ops_if_t ia32_irn_ops_if = {
270         ia32_get_irn_reg_req,
271         ia32_set_irn_reg,
272         ia32_get_irn_reg,
273         ia32_classify,
274         ia32_get_flags
275 };
276
277 ia32_irn_ops_t ia32_irn_ops = {
278         &ia32_irn_ops_if,
279         NULL
280 };
281
282
283
284 /**************************************************
285  *                _                         _  __
286  *               | |                       (_)/ _|
287  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
288  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
289  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
290  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
291  *                        __/ |
292  *                       |___/
293  **************************************************/
294
295 static void check_for_alloca(ir_node *irn, void *env) {
296         int *has_alloca = env;
297
298         if (get_irn_opcode(irn) == iro_Alloc) {
299                 if (get_Alloc_where(irn) == stack_alloc) {
300                         *has_alloca = 1;
301                 }
302         }
303 }
304
305 /**
306  * Transforms the standard firm graph into
307  * an ia32 firm graph
308  */
309 static void ia32_prepare_graph(void *self) {
310         ia32_code_gen_t *cg = self;
311
312         if (! is_pseudo_ir_graph(cg->irg)) {
313                 /* If there is a alloca in the irg, we use %ebp for stack addressing */
314                 /* instead of %esp, as alloca destroys %esp.                         */
315
316                 cg->has_alloca = 0;
317
318                 /* check for alloca node */
319                 irg_walk_blkwise_graph(cg->irg, check_for_alloca, NULL, &(cg->has_alloca));
320
321                 if (cg->has_alloca) {
322                         ia32_general_purpose_regs[REG_EBP].type = arch_register_type_ignore;
323                 }
324
325                 irg_walk_blkwise_graph(cg->irg, NULL, ia32_transform_node, cg);
326         }
327 }
328
329
330
331 /**
332  * Set the register for P_frame_base Proj to %esp.
333  */
334 static void ia32_set_P_frame_base_Proj_reg(ir_node *irn, void *env) {
335         ia32_code_gen_t *cg = env;
336
337         if (is_P_frame_base_Proj(irn)) {
338                 if (cg->has_alloca) {
339                         arch_set_irn_register(cg->arch_env, irn, &ia32_general_purpose_regs[REG_EBP]);
340                 }
341                 else {
342                         arch_set_irn_register(cg->arch_env, irn, &ia32_general_purpose_regs[REG_ESP]);
343                 }
344         }
345 }
346
347
348
349 /**
350  * Dummy functions for hooks we don't need but which must be filled.
351  */
352 static void ia32_before_sched(void *self) {
353 }
354
355 static void ia32_before_ra(void *self) {
356 }
357
358
359 /**
360  * Creates a Store for a Spill
361  */
362 static ir_node *ia32_lower_spill(void *self, ir_node *spill) {
363         ia32_code_gen_t *cg    = self;
364         unsigned         offs  = be_get_spill_offset(spill);
365         dbg_info        *dbg   = get_irn_dbg_info(spill);
366         ir_node         *block = get_nodes_block(spill);
367         ir_node         *ptr   = get_irg_frame(cg->irg);
368         ir_node         *val   = be_get_Spill_context(spill);
369         ir_node         *mem   = new_rd_NoMem(cg->irg);
370         ir_mode         *mode  = get_irn_mode(spill);
371         ir_node         *res;
372
373         res = new_rd_ia32_Store(dbg, cg->irg, block, ptr, val, mem, mode);
374         set_ia32_am_offs(res, new_tarval_from_long(offs, mode_Iu));
375
376         return res;
377 }
378
379 /**
380  * Create a Load for a Spill
381  */
382 static ir_node *ia32_lower_reload(void *self, ir_node *reload) {
383         ia32_code_gen_t *cg    = self;
384         dbg_info        *dbg   = get_irn_dbg_info(reload);
385         ir_node         *block = get_nodes_block(reload);
386         ir_node         *ptr   = get_irg_frame(cg->irg);
387         ir_mode         *mode  = get_irn_mode(reload);
388         ir_node         *store = get_irn_n(reload, 0);
389         tarval          *tv    = get_ia32_am_offs(store);
390         ir_node         *res;
391
392         res = new_rd_ia32_Load(dbg, cg->irg, block, ptr, store, mode);
393         set_ia32_am_offs(res, tv);
394
395         return res;
396 }
397
398 /**
399  * Emits the code, closes the output file and frees
400  * the code generator interface.
401  */
402 static void ia32_codegen(void *self) {
403         ia32_code_gen_t *cg = self;
404         ir_graph       *irg = cg->irg;
405         FILE           *out = cg->out;
406
407         if (cg->emit_decls) {
408                 ia32_gen_decls(cg->out);
409                 cg->emit_decls = 0;
410         }
411
412         /* set the stack register */
413         if (! is_pseudo_ir_graph(irg))
414                 irg_walk_blkwise_graph(irg, NULL, ia32_set_P_frame_base_Proj_reg, cg);
415
416 //      ia32_finish_irg(irg);
417         ia32_gen_routine(out, irg, cg->arch_env);
418
419         cur_reg_set = NULL;
420
421         /* de-allocate code generator */
422         del_set(cg->reg_set);
423         free(self);
424 }
425
426 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
427
428 static const arch_code_generator_if_t ia32_code_gen_if = {
429         ia32_cg_init,
430         ia32_prepare_graph,
431         ia32_before_sched,   /* before scheduling hook */
432         ia32_before_ra,      /* before register allocation hook */
433         ia32_lower_spill,
434         ia32_lower_reload,
435         ia32_codegen         /* emit && done */
436 };
437
438 /**
439  * Initializes the code generator.
440  */
441 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
442         ia32_isa_t      *isa = (ia32_isa_t *)arch_env->isa;
443         ia32_code_gen_t *cg  = malloc(sizeof(*cg));
444
445         cg->impl       = &ia32_code_gen_if;
446         cg->irg        = irg;
447         cg->reg_set    = new_set(ia32_cmp_irn_reg_assoc, 1024);
448         cg->mod        = firm_dbg_register("be.transform.ia32");
449         cg->out        = F;
450         cg->arch_env   = arch_env;
451
452         isa->num_codegens++;
453
454         if (isa->num_codegens > 1)
455                 cg->emit_decls = 0;
456         else
457                 cg->emit_decls = 1;
458
459         cur_reg_set = cg->reg_set;
460
461         ia32_irn_ops.cg = cg;
462
463         return (arch_code_generator_t *)cg;
464 }
465
466
467
468 /*****************************************************************
469  *  ____             _                  _   _____  _____
470  * |  _ \           | |                | | |_   _|/ ____|  /\
471  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
472  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
473  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
474  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
475  *
476  *****************************************************************/
477
478 /**
479  * Initializes the backend ISA and opens the output file.
480  */
481 static void *ia32_init(void) {
482         static int inited = 0;
483         ia32_isa_t *isa   = malloc(sizeof(*isa));
484
485         isa->impl = &ia32_isa_if;
486
487         if(inited)
488                 return NULL;
489
490         inited = 1;
491
492         isa->num_codegens    = 0;
493         isa->reg_projnum_map = new_set(ia32_cmp_reg_projnum_assoc, 1024);
494
495         ia32_register_init(isa);
496         ia32_create_opcodes();
497
498         return isa;
499 }
500
501
502
503 /**
504  * Closes the output file and frees the ISA structure.
505  */
506 static void ia32_done(void *self) {
507         free(self);
508 }
509
510
511
512 static int ia32_get_n_reg_class(const void *self) {
513         return N_CLASSES;
514 }
515
516 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
517         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
518         return &ia32_reg_classes[i];
519 }
520
521 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
522         return &ia32_irn_ops;
523 }
524
525 const arch_irn_handler_t ia32_irn_handler = {
526         ia32_get_irn_ops
527 };
528
529 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
530         return &ia32_irn_handler;
531 }
532
533 long ia32_get_call_projnum_for_reg(const void *self, const arch_register_t *reg) {
534         ia32_isa_t *isa = (ia32_isa_t *)self;
535         return ia32_get_reg_projnum(reg, isa->reg_projnum_map);
536 }
537
538 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
539         return is_ia32_irn(irn);
540 }
541
542 /**
543  * Initializes the code generator interface.
544  */
545 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
546         return &ia32_code_gen_if;
547 }
548
549 list_sched_selector_t ia32_sched_selector;
550
551 /**
552  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
553  */
554 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
555         memcpy(&ia32_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
556         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
557         return &ia32_sched_selector;
558 }
559
560 #ifdef WITH_LIBCORE
561 static void ia32_register_options(lc_opt_entry_t *ent)
562 {
563 }
564 #endif /* WITH_LIBCORE */
565
566 const arch_isa_if_t ia32_isa_if = {
567 #ifdef WITH_LIBCORE
568         ia32_register_options,
569 #endif
570         ia32_init,
571         ia32_done,
572         ia32_get_n_reg_class,
573         ia32_get_reg_class,
574         ia32_get_irn_handler,
575         ia32_get_code_generator_if,
576         ia32_get_list_sched_selector,
577         ia32_get_call_projnum_for_reg
578 };