Added mem dependency for IncSP
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #ifdef _WIN32
6 #include <malloc.h>
7 #else
8 #include <alloca.h>
9 #endif
10
11 #include "pseudo_irg.h"
12 #include "irgwalk.h"
13 #include "irprog.h"
14 #include "irprintf.h"
15 #include "iredges_t.h"
16 #include "ircons.h"
17 #include "irgmod.h"
18
19 #include "bitset.h"
20 #include "debug.h"
21
22 #include "../beabi.h"                 /* the general register allocator interface */
23 #include "../benode_t.h"
24 #include "../belower.h"
25 #include "../besched_t.h"
26 #include "bearch_ia32_t.h"
27
28 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
29 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
30 #include "ia32_gen_decls.h"           /* interface declaration emitter */
31 #include "ia32_transform.h"
32 #include "ia32_emitter.h"
33 #include "ia32_map_regs.h"
34 #include "ia32_optimize.h"
35
36 #define DEBUG_MODULE "firm.be.ia32.isa"
37
38 /* TODO: ugly */
39 static set *cur_reg_set = NULL;
40
41 #undef is_Start
42 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
43
44 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
45         if (! cg->noreg_gp) {
46                 cg->noreg_gp = be_new_NoReg(&ia32_gp_regs[REG_XXX], cg->irg, get_irg_start_block(cg->irg));
47         }
48
49         return cg->noreg_gp;
50 }
51
52 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
53         if (! cg->noreg_fp) {
54                 cg->noreg_fp = be_new_NoReg(&ia32_fp_regs[REG_XXXX], cg->irg, get_irg_start_block(cg->irg));
55         }
56
57         return cg->noreg_fp;
58 }
59
60 /**************************************************
61  *                         _ _              _  __
62  *                        | | |            (_)/ _|
63  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
64  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
65  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
66  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
67  *            __/ |
68  *           |___/
69  **************************************************/
70
71 static ir_node *my_skip_proj(const ir_node *n) {
72         while (is_Proj(n))
73                 n = get_Proj_pred(n);
74         return (ir_node *)n;
75 }
76
77 static int is_Call_Proj(const ir_node *n) {
78         if (is_Proj(n)                               &&
79                 is_Proj(get_Proj_pred(n))                &&
80                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
81                 is_ia32_Call(get_Proj_pred(get_Proj_pred(n))))
82         {
83                 return 1;
84         }
85
86         return 0;
87 }
88
89 static int is_Start_Proj(const ir_node *n) {
90         if (is_Proj(n)                               &&
91                 is_Proj(get_Proj_pred(n))                &&
92                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
93                 is_Start(get_Proj_pred(get_Proj_pred(n))))
94         {
95                 return 1;
96         }
97
98         return 0;
99 }
100
101 static int is_P_frame_base_Proj(const ir_node *n) {
102         if (is_Proj(n)                                    &&
103                 is_Start(get_Proj_pred(n)) &&
104                 get_Proj_proj(n) == pn_Start_P_frame_base)
105         {
106                 return 1;
107         }
108
109         return 0;
110 }
111
112 static int is_used_by_Keep(const ir_node *n) {
113         return be_is_Keep(get_edge_src_irn(get_irn_out_edge_first(n)));
114 }
115
116 /**
117  * Return register requirements for an ia32 node.
118  * If the node returns a tuple (mode_T) then the proj's
119  * will be asked for this information.
120  */
121 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) {
122         const ia32_register_req_t *irn_req;
123         long                       node_pos = pos == -1 ? 0 : pos;
124         ir_mode                   *mode     = get_irn_mode(irn);
125         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
126         const ia32_irn_ops_t      *ops      = self;
127
128         if (mode == mode_T || mode == mode_M) {
129                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
130                 return NULL;
131         }
132
133         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
134
135
136         if (is_Proj(irn)) {
137                 if (pos == -1) {
138                         node_pos = ia32_translate_proj_pos(irn);
139                 }
140                 else {
141                         node_pos = pos;
142                 }
143
144                 irn = my_skip_proj(irn);
145
146                 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
147         }
148
149         if (is_ia32_irn(irn)) {
150                 if (pos >= 0) {
151                         irn_req = get_ia32_in_req(irn, pos);
152                 }
153                 else {
154                         irn_req = get_ia32_out_req(irn, node_pos);
155                 }
156
157                 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
158
159                 memcpy(req, &(irn_req->req), sizeof(*req));
160
161                 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
162                         assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
163                         req->other_same = get_irn_n(irn, irn_req->same_pos);
164                 }
165
166                 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
167                         assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
168                         req->other_different = get_irn_n(irn, irn_req->different_pos);
169                 }
170         }
171         else {
172                 /* treat Phi like Const with default requirements */
173                 if (is_Phi(irn)) {
174                         DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
175                         if (mode_is_float(mode))
176                                 memcpy(req, &(ia32_default_req_ia32_fp.req), sizeof(*req));
177                         else if (mode_is_int(mode) || mode_is_reference(mode))
178                                 memcpy(req, &(ia32_default_req_ia32_gp.req), sizeof(*req));
179                         else if (mode == mode_T || mode == mode_M) {
180                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
181                                 return NULL;
182                         }
183                         else
184                                 assert(0 && "unsupported Phi-Mode");
185                 }
186                 else {
187                         DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
188                         req = NULL;
189                 }
190         }
191
192         return req;
193 }
194
195 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
196         int pos = 0;
197
198         if (is_Proj(irn)) {
199                 pos = ia32_translate_proj_pos(irn);
200                 irn = my_skip_proj(irn);
201         }
202
203         if (is_ia32_irn(irn)) {
204                 const arch_register_t **slots;
205
206                 slots      = get_ia32_slots(irn);
207                 slots[pos] = reg;
208         }
209         else {
210                 ia32_set_firm_reg(irn, reg, cur_reg_set);
211         }
212 }
213
214 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
215         int pos = 0;
216         const arch_register_t *reg = NULL;
217
218         if (is_Proj(irn)) {
219                 pos = ia32_translate_proj_pos(irn);
220                 irn = my_skip_proj(irn);
221         }
222
223         if (is_ia32_irn(irn)) {
224                 const arch_register_t **slots;
225                 slots = get_ia32_slots(irn);
226                 reg   = slots[pos];
227         }
228         else {
229                 reg = ia32_get_firm_reg(irn, cur_reg_set);
230         }
231
232         return reg;
233 }
234
235 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
236         irn = my_skip_proj(irn);
237         if (is_cfop(irn))
238                 return arch_irn_class_branch;
239         else if (is_ia32_Call(irn))
240                 return arch_irn_class_call;
241         else if (is_ia32_irn(irn))
242                 return arch_irn_class_normal;
243         else
244                 return 0;
245 }
246
247 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
248         irn = my_skip_proj(irn);
249         if (is_ia32_irn(irn))
250                 return get_ia32_flags(irn);
251         else {
252                 return 0;
253         }
254 }
255
256 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
257         if (get_ia32_use_frame(irn)) {
258                 /* TODO: correct offset */
259         }
260 }
261
262 /* fill register allocator interface */
263
264 static const arch_irn_ops_if_t ia32_irn_ops_if = {
265         ia32_get_irn_reg_req,
266         ia32_set_irn_reg,
267         ia32_get_irn_reg,
268         ia32_classify,
269         ia32_get_flags,
270         ia32_set_stack_bias
271 };
272
273 ia32_irn_ops_t ia32_irn_ops = {
274         &ia32_irn_ops_if,
275         NULL
276 };
277
278
279
280 /**************************************************
281  *                _                         _  __
282  *               | |                       (_)/ _|
283  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
284  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
285  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
286  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
287  *                        __/ |
288  *                       |___/
289  **************************************************/
290
291 /**
292  * Transforms the standard firm graph into
293  * an ia32 firm graph
294  */
295 static void ia32_prepare_graph(void *self) {
296         ia32_code_gen_t *cg = self;
297
298         irg_walk_blkwise_graph(cg->irg, ia32_place_consts, ia32_transform_node, cg);
299         dump_ir_block_graph_sched(cg->irg, "-transformed");
300         edges_deactivate(cg->irg);
301         edges_activate(cg->irg);
302         irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
303         dump_ir_block_graph_sched(cg->irg, "-am");
304 }
305
306
307
308 /**
309  * Stack reservation and StackParam lowering.
310  */
311 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
312
313 }
314
315
316
317 /**
318  * Dummy functions for hooks we don't need but which must be filled.
319  */
320 static void ia32_before_sched(void *self) {
321 }
322
323 static void ia32_before_ra(void *self) {
324 }
325
326
327 /**
328  * Creates a Store for a Spill
329  */
330 static ir_node *ia32_lower_spill(void *self, ir_node *spill) {
331         ia32_code_gen_t *cg    = self;
332         ir_graph        *irg   = cg->irg;
333         dbg_info        *dbg   = get_irn_dbg_info(spill);
334         ir_node         *block = get_nodes_block(spill);
335         ir_node         *ptr   = get_irg_frame(irg);
336         ir_node         *val   = be_get_Spill_context(spill);
337         ir_node         *mem   = new_rd_NoMem(irg);
338         ir_mode         *mode  = get_irn_mode(spill);
339         entity          *ent   = be_get_spill_entity(spill);
340         unsigned         offs  = get_entity_offset_bytes(ent);
341         ir_node         *noreg, *res;
342         char             buf[64];
343
344         DB((cg->mod, LEVEL_1, "lower_spill: got offset %d for %+F\n", offs, ent));
345
346         if (mode_is_float(mode)) {
347                 noreg = ia32_new_NoReg_fp(cg);
348                 res   = new_rd_ia32_fStore(dbg, irg, block, ptr, noreg, val, mem, mode);
349         }
350         else {
351                 noreg = ia32_new_NoReg_gp(cg);
352                 res   = new_rd_ia32_Store(dbg, irg, block, ptr, noreg, val, mem, mode);
353         }
354
355         snprintf(buf, sizeof(buf), "%d", offs);
356         add_ia32_am_offs(res, buf);
357
358         return res;
359 }
360
361 /**
362  * Create a Load for a Spill
363  */
364 static ir_node *ia32_lower_reload(void *self, ir_node *reload) {
365         ia32_code_gen_t *cg    = self;
366         ir_graph        *irg   = cg->irg;
367         dbg_info        *dbg   = get_irn_dbg_info(reload);
368         ir_node         *block = get_nodes_block(reload);
369         ir_node         *ptr   = get_irg_frame(irg);
370         ir_mode         *mode  = get_irn_mode(reload);
371         ir_node         *pred  = get_irn_n(reload, 0);
372         char             buf[64];
373         char            *ofs;
374         ir_node         *noreg, *res;
375
376         /* Get the offset to Load from. It can either be a Spill or a Store. */
377         if (be_is_Spill(pred)) {
378                 entity   *ent  = be_get_spill_entity(pred);
379                 unsigned  offs = get_entity_offset_bytes(ent);
380                 DB((cg->mod, LEVEL_1, "lower_reload: got offset %d for %+F\n", offs, ent));
381
382                 snprintf(buf, sizeof(buf), "%d", offs);
383         }
384         else if (is_ia32_Store(pred) || is_ia32_fStore(pred)) {
385                 ofs = get_ia32_am_offs(pred);
386                 strncpy(buf, ofs, sizeof(buf));
387                 free(ofs);
388         }
389         else {
390                 assert(0 && "unsupported Reload predecessor");
391         }
392
393         /* Create the Load */
394         if (mode_is_float(mode)) {
395                 noreg = ia32_new_NoReg_fp(cg);
396                 res   = new_rd_ia32_fLoad(dbg, irg, block, ptr, noreg, pred, mode_T);
397         }
398         else {
399                 noreg = ia32_new_NoReg_gp(cg);
400                 res   = new_rd_ia32_Load(dbg, irg, block, ptr, noreg, pred, mode_T);
401         }
402
403         /* Set offset */
404         add_ia32_am_offs(res, buf);
405
406         /* Return the result Proj */
407         return new_rd_Proj(dbg, irg, block, res, mode, 0);
408 }
409
410 /**
411  * Emits the code, closes the output file and frees
412  * the code generator interface.
413  */
414 static void ia32_codegen(void *self) {
415         ia32_code_gen_t *cg = self;
416         ir_graph       *irg = cg->irg;
417         FILE           *out = cg->out;
418
419         if (cg->emit_decls) {
420                 ia32_gen_decls(cg->out);
421                 cg->emit_decls = 0;
422         }
423
424         ia32_finish_irg(irg, cg);
425         //dump_ir_block_graph_sched(irg, "-finished");
426         ia32_gen_routine(out, irg, cg);
427
428         cur_reg_set = NULL;
429
430         pmap_destroy(cg->tv_ent);
431         pmap_destroy(cg->types);
432
433         /* de-allocate code generator */
434         del_set(cg->reg_set);
435         free(self);
436 }
437
438 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
439
440 static const arch_code_generator_if_t ia32_code_gen_if = {
441         ia32_cg_init,
442         ia32_prepare_graph,
443         ia32_before_sched,   /* before scheduling hook */
444         ia32_before_ra,      /* before register allocation hook */
445         ia32_lower_spill,
446         ia32_lower_reload,
447         ia32_codegen         /* emit && done */
448 };
449
450 /**
451  * Initializes the code generator.
452  */
453 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
454         ia32_isa_t      *isa = (ia32_isa_t *)arch_env->isa;
455         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
456
457         cg->impl     = &ia32_code_gen_if;
458         cg->irg      = irg;
459         cg->reg_set  = new_set(ia32_cmp_irn_reg_assoc, 1024);
460         cg->mod      = firm_dbg_register("firm.be.ia32.cg");
461         cg->out      = F;
462         cg->arch_env = arch_env;
463         cg->types    = pmap_create();
464         cg->tv_ent   = pmap_create();
465         cg->noreg_gp = NULL;
466         cg->noreg_fp = NULL;
467
468         isa->num_codegens++;
469
470         if (isa->num_codegens > 1)
471                 cg->emit_decls = 0;
472         else
473                 cg->emit_decls = 1;
474
475         cur_reg_set = cg->reg_set;
476
477         ia32_irn_ops.cg = cg;
478
479         return (arch_code_generator_t *)cg;
480 }
481
482
483
484 /*****************************************************************
485  *  ____             _                  _   _____  _____
486  * |  _ \           | |                | | |_   _|/ ____|  /\
487  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
488  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
489  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
490  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
491  *
492  *****************************************************************/
493
494 static ia32_isa_t ia32_isa_template = {
495         &ia32_isa_if,
496         &ia32_gp_regs[REG_ESP],
497         &ia32_gp_regs[REG_EBP],
498         -1,
499         0
500 };
501
502 /**
503  * Initializes the backend ISA.
504  */
505 static void *ia32_init(void) {
506         static int inited = 0;
507         ia32_isa_t *isa;
508
509         if(inited)
510                 return NULL;
511
512         isa = xcalloc(1, sizeof(*isa));
513         memcpy(isa, &ia32_isa_template, sizeof(*isa));
514
515         ia32_register_init(isa);
516         ia32_create_opcodes();
517
518         inited = 1;
519
520         return isa;
521 }
522
523
524
525 /**
526  * Closes the output file and frees the ISA structure.
527  */
528 static void ia32_done(void *self) {
529         free(self);
530 }
531
532
533
534 static int ia32_get_n_reg_class(const void *self) {
535         return N_CLASSES;
536 }
537
538 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
539         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
540         return &ia32_reg_classes[i];
541 }
542
543 /**
544  * Get the register class which shall be used to store a value of a given mode.
545  * @param self The this pointer.
546  * @param mode The mode in question.
547  * @return A register class which can hold values of the given mode.
548  */
549 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
550         if (mode_is_float(mode))
551                 return &ia32_reg_classes[CLASS_ia32_fp];
552         else
553                 return &ia32_reg_classes[CLASS_ia32_gp];
554 }
555
556 /**
557  * Get the ABI restrictions for procedure calls.
558  * @param self        The this pointer.
559  * @param method_type The type of the method (procedure) in question.
560  * @param abi         The abi object to be modified
561  */
562 void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
563         ir_type  *tp;
564         ir_mode  *mode;
565         unsigned  cc        = get_method_calling_convention(method_type);
566         int       n         = get_method_n_params(method_type);
567         int       biggest_n = -1;
568         int       stack_idx = 0;
569         int       i, ignore;
570         ir_mode **modes;
571         const arch_register_t *reg;
572
573         /* set stack parameter passing style */
574         be_abi_call_set_flags(abi, BE_ABI_FRAME_POINTER_DEDICATED, 4);
575
576         /* collect the mode for each type */
577         modes = alloca(n * sizeof(modes[0]));
578
579         for (i = 0; i < n; i++) {
580                 tp       = get_method_param_type(method_type, i);
581                 modes[i] = get_type_mode(tp);
582         }
583
584         /* set register parameters  */
585         if (cc & cc_reg_param) {
586                 /* determine the number of parameters passed via registers */
587                 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore, &ignore);
588
589                 /* loop over all parameters and set the register requirements */
590                 for (i = 0; i <= biggest_n; i++) {
591                         reg = ia32_get_RegParam_reg(n, modes, i, cc);
592                         assert(reg && "kaputt");
593                         be_abi_call_param_reg(abi, i, reg);
594                 }
595
596                 stack_idx = i;
597         }
598
599
600         /* set stack parameters */
601         for (i = stack_idx; i < n; i++) {
602                 be_abi_call_param_stack(abi, i);
603         }
604
605
606         /* set return registers */
607         n = get_method_n_ress(method_type);
608
609         assert(n <= 2 && "more than two results not supported");
610
611         /* In case of 64bit returns, we will have two 32bit values */
612         if (n == 2) {
613                 tp   = get_method_res_type(method_type, 0);
614                 mode = get_type_mode(tp);
615
616                 assert(!mode_is_float(mode) && "two FP results not supported");
617
618                 tp   = get_method_res_type(method_type, 1);
619                 mode = get_type_mode(tp);
620
621                 assert(!mode_is_float(mode) && "two FP results not supported");
622
623                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
624                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
625         }
626         else if (n == 1) {
627                 tp   = get_method_res_type(method_type, 0);
628                 mode = get_type_mode(tp);
629
630                 if (mode_is_float(mode)) {
631                         be_abi_call_res_reg(abi, 1, &ia32_fp_regs[REG_XMM0]);
632                 }
633                 else {
634                         be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EAX]);
635                 }
636         }
637 }
638
639
640 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
641         return &ia32_irn_ops;
642 }
643
644 const arch_irn_handler_t ia32_irn_handler = {
645         ia32_get_irn_ops
646 };
647
648 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
649         return &ia32_irn_handler;
650 }
651
652 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
653         return is_ia32_irn(irn);
654 }
655
656 /**
657  * Initializes the code generator interface.
658  */
659 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
660         return &ia32_code_gen_if;
661 }
662
663 list_sched_selector_t ia32_sched_selector;
664
665 /**
666  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
667  */
668 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
669         memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
670         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
671         return &ia32_sched_selector;
672 }
673
674 #ifdef WITH_LIBCORE
675 static void ia32_register_options(lc_opt_entry_t *ent)
676 {
677 }
678 #endif /* WITH_LIBCORE */
679
680 const arch_isa_if_t ia32_isa_if = {
681 #ifdef WITH_LIBCORE
682         ia32_register_options,
683 #endif
684         ia32_init,
685         ia32_done,
686         ia32_get_n_reg_class,
687         ia32_get_reg_class,
688         ia32_get_reg_class_for_mode,
689         ia32_get_call_abi,
690         ia32_get_irn_handler,
691         ia32_get_code_generator_if,
692         ia32_get_list_sched_selector
693 };