amd64: added be_Spill and be_Reload transformation.
[libfirm] / ir / be / amd64 / bearch_amd64.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    The main amd64 backend driver file.
23  * @version  $Id: bearch_amd64.c 26909 2010-01-05 15:56:54Z matze $
24  */
25 #include "config.h"
26
27 #include "pseudo_irg.h"
28 #include "irgwalk.h"
29 #include "irprog.h"
30 #include "irprintf.h"
31 #include "ircons.h"
32 #include "irgmod.h"
33 #include "irdump.h"
34
35 #include "bitset.h"
36 #include "debug.h"
37
38 #include "be.h"
39 #include "../bearch.h"
40 #include "../benode.h"
41 #include "../belower.h"
42 #include "../besched.h"
43 #include "../beabi.h"
44 #include "../bemodule.h"
45 #include "../begnuas.h"
46 #include "../belistsched.h"
47 #include "../beflags.h"
48 #include "../bespillslots.h"
49
50 #include "bearch_amd64_t.h"
51
52 #include "amd64_new_nodes.h"
53 #include "gen_amd64_regalloc_if.h"
54 #include "amd64_transform.h"
55 #include "amd64_emitter.h"
56
57 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
58
59 static arch_irn_class_t amd64_classify(const ir_node *irn)
60 {
61         (void) irn;
62         return 0;
63 }
64
65 static ir_entity *amd64_get_frame_entity(const ir_node *node)
66 {
67         if (is_amd64_FrameAddr(node)) {
68                 const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(node);
69                 return attr->entity;
70         }
71
72         (void) node;
73         /* TODO: return the ir_entity assigned to the frame */
74         return NULL;
75 }
76
77 static void amd64_set_frame_entity(ir_node *node, ir_entity *ent)
78 {
79         (void) node;
80         (void) ent;
81         /* TODO: set the ir_entity assigned to the frame */
82 }
83
84 /**
85  * This function is called by the generic backend to correct offsets for
86  * nodes accessing the stack.
87  */
88 static void amd64_set_frame_offset(ir_node *irn, int offset)
89 {
90         if (is_amd64_FrameAddr(irn)) {
91                 amd64_SymConst_attr_t *attr = get_irn_generic_attr(irn);
92                 attr->fp_offset += offset;
93         }
94 }
95
96 static int amd64_get_sp_bias(const ir_node *irn)
97 {
98         (void) irn;
99         return 0;
100 }
101
102 /* fill register allocator interface */
103
104 static const arch_irn_ops_t amd64_irn_ops = {
105         get_amd64_in_req,
106         amd64_classify,
107         amd64_get_frame_entity,
108         amd64_set_frame_entity,
109         amd64_set_frame_offset,
110         amd64_get_sp_bias,
111         NULL,    /* get_inverse             */
112         NULL,    /* get_op_estimated_cost   */
113         NULL,    /* possible_memory_operand */
114         NULL,    /* perform_memory_operand  */
115 };
116
117
118
119 /**
120  * Transforms the standard firm graph into
121  * a amd64 firm graph
122  */
123 static void amd64_prepare_graph(void *self)
124 {
125         amd64_code_gen_t *cg = self;
126
127         amd64_transform_graph (cg);
128
129         if (cg->dump)
130                 dump_ir_graph(cg->irg, "transformed");
131 }
132
133
134 /**
135  * Called immediatly before emit phase.
136  */
137 static void amd64_finish_irg(void *self)
138 {
139         amd64_code_gen_t *cg = self;
140         ir_graph         *irg = cg->irg;
141
142         dump_ir_graph(irg, "amd64-finished");
143 }
144
145 static void amd64_before_ra(void *self)
146 {
147         amd64_code_gen_t *cg = self;
148
149         be_sched_fix_flags(cg->birg, &amd64_reg_classes[CLASS_amd64_flags], 0);
150 }
151
152
153 static void transform_Reload(ir_node *node)
154 {
155         ir_graph  *irg    = get_irn_irg(node);
156         ir_node   *block  = get_nodes_block(node);
157         dbg_info  *dbgi   = get_irn_dbg_info(node);
158         ir_node   *ptr    = get_irg_frame(irg);
159         ir_node   *mem    = get_irn_n(node, be_pos_Reload_mem);
160         ir_mode   *mode   = get_irn_mode(node);
161         //ir_entity *entity = be_get_frame_entity(node);
162         const arch_register_t *reg;
163         ir_node   *proj;
164         ir_node   *load;
165
166         ir_node  *sched_point = sched_prev(node);
167
168         load = new_bd_amd64_Load(dbgi, block, ptr, mem);
169         sched_add_after(sched_point, load);
170         sched_remove(node);
171
172         proj = new_rd_Proj(dbgi, load, mode, pn_amd64_Load_res);
173
174         reg = arch_get_irn_register(node);
175         arch_set_irn_register(proj, reg);
176
177         exchange(node, proj);
178 }
179
180 static void transform_Spill(ir_node *node)
181 {
182         ir_graph  *irg    = get_irn_irg(node);
183         ir_node   *block  = get_nodes_block(node);
184         dbg_info  *dbgi   = get_irn_dbg_info(node);
185         ir_node   *ptr    = get_irg_frame(irg);
186         ir_node   *mem    = new_NoMem();
187         ir_node   *val    = get_irn_n(node, be_pos_Spill_val);
188         //ir_mode   *mode   = get_irn_mode(val);
189         //ir_entity *entity = be_get_frame_entity(node);
190         ir_node   *sched_point;
191         ir_node   *store;
192
193         sched_point = sched_prev(node);
194         store = new_bd_amd64_Store(dbgi, block, ptr, val, mem);
195
196         sched_remove(node);
197         sched_add_after(sched_point, store);
198
199         exchange(node, store);
200 }
201
202 static void amd64_after_ra_walker(ir_node *block, void *data)
203 {
204         ir_node *node, *prev;
205         (void) data;
206
207         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
208                 prev = sched_prev(node);
209
210                 if (be_is_Reload(node)) {
211                         transform_Reload(node);
212                 } else if (be_is_Spill(node)) {
213                         transform_Spill(node);
214                 }
215         }
216 }
217
218 static void amd64_after_ra(void *self)
219 {
220         amd64_code_gen_t *cg = self;
221         be_coalesce_spillslots(cg->birg);
222
223         irg_block_walk_graph(cg->irg, NULL, amd64_after_ra_walker, NULL);
224 }
225
226
227 /**
228  * Emits the code, closes the output file and frees
229  * the code generator interface.
230  */
231 static void amd64_emit_and_done(void *self)
232 {
233         amd64_code_gen_t *cg  = self;
234         ir_graph         *irg = cg->irg;
235
236         amd64_gen_routine(cg, irg);
237
238         /* de-allocate code generator */
239         free(cg);
240 }
241
242 static void *amd64_cg_init(be_irg_t *birg);
243
244 static const arch_code_generator_if_t amd64_code_gen_if = {
245         amd64_cg_init,
246         NULL,                    /* get_pic_base hook */
247         NULL,                    /* before abi introduce hook */
248         amd64_prepare_graph,
249         NULL,                    /* spill hook */
250         amd64_before_ra,      /* before register allocation hook */
251         amd64_after_ra,       /* after register allocation hook */
252         amd64_finish_irg,
253         amd64_emit_and_done
254 };
255
256 /**
257  * Initializes the code generator.
258  */
259 static void *amd64_cg_init(be_irg_t *birg)
260 {
261         const arch_env_t    *arch_env = be_get_birg_arch_env(birg);
262         amd64_isa_t      *isa      = (amd64_isa_t *) arch_env;
263         amd64_code_gen_t *cg       = XMALLOC(amd64_code_gen_t);
264
265         cg->impl     = &amd64_code_gen_if;
266         cg->irg      = be_get_birg_irg(birg);
267         cg->isa      = isa;
268         cg->birg     = birg;
269         cg->dump     = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
270
271         return (arch_code_generator_t *)cg;
272 }
273
274
275 typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_node *block);
276
277 /**
278  * Used to create per-graph unique pseudo nodes.
279  */
280 static inline ir_node *create_const(amd64_code_gen_t *cg, ir_node **place,
281                                     create_const_node_func func,
282                                     const arch_register_t* reg)
283 {
284         ir_node *block, *res;
285
286         if (*place != NULL)
287                 return *place;
288
289         block = get_irg_start_block(cg->irg);
290         res = func(NULL, block);
291         arch_set_irn_register(res, reg);
292         *place = res;
293
294         return res;
295 }
296
297 const arch_isa_if_t amd64_isa_if;
298 static amd64_isa_t amd64_isa_template = {
299         {
300                 &amd64_isa_if,             /* isa interface implementation */
301                 &amd64_gp_regs[REG_RSP],  /* stack pointer register */
302                 &amd64_gp_regs[REG_RBP],  /* base pointer register */
303                 &amd64_reg_classes[CLASS_amd64_gp],  /* link pointer register class */
304                 -1,                          /* stack direction */
305                 3,                           /* power of two stack alignment for calls, 2^2 == 4 */
306                 NULL,                        /* main environment */
307                 7,                           /* costs for a spill instruction */
308                 5,                           /* costs for a reload instruction */
309         },
310 };
311
312 /**
313  * Initializes the backend ISA
314  */
315 static arch_env_t *amd64_init(FILE *outfile)
316 {
317         static int run_once = 0;
318         amd64_isa_t *isa;
319
320         if(run_once)
321                 return NULL;
322         run_once = 1;
323
324         isa = XMALLOC(amd64_isa_t);
325         memcpy(isa, &amd64_isa_template, sizeof(*isa));
326
327         be_emit_init(outfile);
328
329         amd64_register_init();
330         amd64_create_opcodes(&amd64_irn_ops);
331
332         return &isa->arch_env;
333 }
334
335
336
337 /**
338  * Closes the output file and frees the ISA structure.
339  */
340 static void amd64_done(void *self)
341 {
342         amd64_isa_t *isa = self;
343
344         /* emit now all global declarations */
345         be_gas_emit_decls(isa->arch_env.main_env);
346
347         be_emit_exit();
348         free(self);
349 }
350
351
352 static unsigned amd64_get_n_reg_class(void)
353 {
354         return N_CLASSES;
355 }
356
357 static const arch_register_class_t *amd64_get_reg_class(unsigned i)
358 {
359         assert(i < N_CLASSES);
360         return &amd64_reg_classes[i];
361 }
362
363
364
365 /**
366  * Get the register class which shall be used to store a value of a given mode.
367  * @param self The this pointer.
368  * @param mode The mode in question.
369  * @return A register class which can hold values of the given mode.
370  */
371 static const arch_register_class_t *amd64_get_reg_class_for_mode(const ir_mode *mode)
372 {
373         assert(!mode_is_float(mode));
374         return &amd64_reg_classes[CLASS_amd64_gp];
375 }
376
377
378
379 typedef struct {
380         be_abi_call_flags_bits_t flags;
381         const arch_env_t *arch_env;
382         ir_graph *irg;
383 } amd64_abi_env_t;
384
385 static void *amd64_abi_init(const be_abi_call_t *call, const arch_env_t *arch_env, ir_graph *irg)
386 {
387         amd64_abi_env_t *env = XMALLOC(amd64_abi_env_t);
388         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
389         env->flags    = fl.bits;
390         env->irg      = irg;
391         env->arch_env = arch_env;
392         return env;
393 }
394
395 /**
396  * Get the between type for that call.
397  * @param self The callback object.
398  * @return The between type of for that call.
399  */
400 static ir_type *amd64_get_between_type(void *self)
401 {
402         static ir_type *between_type = NULL;
403         static ir_entity *old_bp_ent = NULL;
404         (void) self;
405
406         if(!between_type) {
407                 ir_entity *ret_addr_ent;
408                 ir_type *ret_addr_type = new_type_primitive(mode_P);
409                 ir_type *old_bp_type   = new_type_primitive(mode_P);
410
411                 between_type           = new_type_class(new_id_from_str("amd64_between_type"));
412                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
413                 ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
414
415                 set_entity_offset(old_bp_ent, 0);
416                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
417                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
418         }
419
420         return between_type;
421 }
422
423 /**
424  * Build the prolog, return the BASE POINTER register
425  */
426 static const arch_register_t *amd64_abi_prologue(void *self, ir_node **mem,
427                                                     pmap *reg_map, int *stack_bias)
428 {
429         amd64_abi_env_t  *env  = self;
430         const arch_env_t *aenv = env->arch_env;
431         (void) mem;
432         (void) stack_bias;
433         (void) aenv;
434         (void) reg_map;
435
436         if (!env->flags.try_omit_fp) {
437                 /* FIXME: maybe later here should be some code to generate
438                  * the usual abi prologue */
439                 return env->arch_env->bp;
440         }
441
442         return env->arch_env->sp;
443 }
444
445 /* Build the epilog */
446 static void amd64_abi_epilogue(void *self, ir_node *bl, ir_node **mem,
447                                pmap *reg_map)
448 {
449         amd64_abi_env_t  *env  = self;
450         const arch_env_t *aenv = env->arch_env;
451         ir_node          *curr_sp  = be_abi_reg_map_get(reg_map, aenv->sp);
452         ir_node          *curr_bp  = be_abi_reg_map_get(reg_map, aenv->bp);
453         (void) bl;
454         (void) mem;
455
456         if (env->flags.try_omit_fp) {
457                 curr_sp = be_new_IncSP(aenv->sp, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK, 0);
458         }
459
460         be_abi_reg_map_set(reg_map, aenv->sp, curr_sp);
461         be_abi_reg_map_set(reg_map, aenv->bp, curr_bp);
462 }
463
464 static const be_abi_callbacks_t amd64_abi_callbacks = {
465         amd64_abi_init,
466         free,
467         amd64_get_between_type,
468         amd64_abi_prologue,
469         amd64_abi_epilogue,
470 };
471
472 static const arch_register_t *gpreg_param_reg_std[] = {
473         &amd64_gp_regs[REG_RDI],
474         &amd64_gp_regs[REG_RSI],
475         &amd64_gp_regs[REG_RDX],
476         &amd64_gp_regs[REG_RCX],
477         &amd64_gp_regs[REG_R8],
478         &amd64_gp_regs[REG_R9],
479 };
480
481 static const arch_register_t *amd64_get_RegParam_reg(int n)
482 {
483         assert(n < 6 && n >=0 && "register param > 6 requested");
484         return gpreg_param_reg_std[n];
485 }
486
487 /**
488  * Get the ABI restrictions for procedure calls.
489  * @param self        The this pointer.
490  * @param method_type The type of the method (procedure) in question.
491  * @param abi         The abi object to be modified
492  */
493 static void amd64_get_call_abi(const void *self, ir_type *method_type,
494                            be_abi_call_t *abi)
495 {
496         ir_type  *tp;
497         ir_mode  *mode;
498         int       i, n = get_method_n_params(method_type);
499         be_abi_call_flags_t call_flags;
500         int no_reg = 0;
501
502         (void) self;
503
504         /* set abi flags for calls */
505         call_flags.bits.left_to_right         = 0;
506         call_flags.bits.store_args_sequential = 0;
507         call_flags.bits.try_omit_fp           = 1;
508         call_flags.bits.fp_free               = 0;
509         call_flags.bits.call_has_imm          = 1;
510
511         /* set stack parameter passing style */
512         be_abi_call_set_flags(abi, call_flags, &amd64_abi_callbacks);
513
514         for (i = 0; i < n; i++) {
515                 tp   = get_method_param_type(method_type, i);
516                 mode = get_type_mode(tp);
517                 //d// printf ("MODE %p %p XX %d\n", mode, mode_Iu, i);
518
519                 if (!no_reg && i < 6 && mode_is_data (mode)) {
520                         //d// printf("TEST%d\n", i);
521                         be_abi_call_param_reg(abi, i, amd64_get_RegParam_reg (i),
522                                               ABI_CONTEXT_BOTH);
523                 /* default: all parameters on stack */
524                 } else {
525                         no_reg = 1;
526                         be_abi_call_param_stack(abi, i, mode, 8, 0, 0, ABI_CONTEXT_BOTH);
527                 }
528         }
529
530         /* TODO: set correct return register */
531         /* default: return value is in R0 resp. F0 */
532         if (get_method_n_ress(method_type) > 0) {
533                 tp   = get_method_res_type(method_type, 0);
534                 mode = get_type_mode(tp);
535
536                 /* FIXME: No floating point yet */
537                 /* be_abi_call_res_reg(abi, 0,
538                         mode_is_float(mode) ? &amd64_fp_regs[REG_F0] : &amd64_gp_regs[REG_R0], ABI_CONTEXT_BOTH) */;
539
540                 be_abi_call_res_reg(abi, 0,
541                         &amd64_gp_regs[REG_RAX], ABI_CONTEXT_BOTH);
542         }
543 }
544
545 static int amd64_to_appear_in_schedule(void *block_env, const ir_node *irn)
546 {
547         (void) block_env;
548
549         if(!is_amd64_irn(irn))
550                 return -1;
551
552         return 1;
553 }
554
555 /**
556  * Initializes the code generator interface.
557  */
558 static const arch_code_generator_if_t *amd64_get_code_generator_if(
559                 void *self)
560 {
561         (void) self;
562         return &amd64_code_gen_if;
563 }
564
565 list_sched_selector_t amd64_sched_selector;
566
567 /**
568  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
569  */
570 static const list_sched_selector_t *amd64_get_list_sched_selector(
571                 const void *self, list_sched_selector_t *selector)
572 {
573         (void) self;
574         (void) selector;
575
576         amd64_sched_selector = trivial_selector;
577         amd64_sched_selector.to_appear_in_schedule = amd64_to_appear_in_schedule;
578         return &amd64_sched_selector;
579 }
580
581 static const ilp_sched_selector_t *amd64_get_ilp_sched_selector(
582                 const void *self)
583 {
584         (void) self;
585         return NULL;
586 }
587
588 /**
589  * Returns the necessary byte alignment for storing a register of given class.
590  */
591 static int amd64_get_reg_class_alignment(const arch_register_class_t *cls)
592 {
593         ir_mode *mode = arch_register_class_mode(cls);
594         return get_mode_size_bytes(mode);
595 }
596
597 /**
598  * Returns the libFirm configuration parameter for this backend.
599  */
600 static const backend_params *amd64_get_backend_params(void) {
601         static backend_params p = {
602                 0,     /* no dword lowering */
603                 0,     /* no inline assembly */
604                 NULL,  /* will be set later */
605                 NULL,  /* no creator function */
606                 NULL,  /* context for create_intrinsic_fkt */
607                 NULL,  /* parameter for if conversion */
608                 NULL,  /* float arithmetic mode */
609                 0,     /* no trampoline support: size 0 */
610                 0,     /* no trampoline support: align 0 */
611                 NULL,  /* no trampoline support: no trampoline builder */
612                 8      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
613         };
614         return &p;
615 }
616
617 static const be_execution_unit_t ***amd64_get_allowed_execution_units(
618                 const ir_node *irn)
619 {
620         (void) irn;
621         /* TODO */
622         assert(0);
623         return NULL;
624 }
625
626 static const be_machine_t *amd64_get_machine(const void *self)
627 {
628         (void) self;
629         /* TODO */
630         assert(0);
631         return NULL;
632 }
633
634 static ir_graph **amd64_get_backend_irg_list(const void *self,
635                                                 ir_graph ***irgs)
636 {
637         (void) self;
638         (void) irgs;
639         return NULL;
640 }
641
642 static asm_constraint_flags_t amd64_parse_asm_constraint(const char **c)
643 {
644         (void) c;
645         return ASM_CONSTRAINT_FLAG_INVALID;
646 }
647
648 static int amd64_is_valid_clobber(const char *clobber)
649 {
650         (void) clobber;
651         return 0;
652 }
653
654 const arch_isa_if_t amd64_isa_if = {
655         amd64_init,
656         amd64_done,
657         NULL,                /* handle intrinsics */
658         amd64_get_n_reg_class,
659         amd64_get_reg_class,
660         amd64_get_reg_class_for_mode,
661         amd64_get_call_abi,
662         amd64_get_code_generator_if,
663         amd64_get_list_sched_selector,
664         amd64_get_ilp_sched_selector,
665         amd64_get_reg_class_alignment,
666     amd64_get_backend_params,
667         amd64_get_allowed_execution_units,
668         amd64_get_machine,
669         amd64_get_backend_irg_list,
670         NULL,                    /* mark remat */
671         amd64_parse_asm_constraint,
672         amd64_is_valid_clobber
673 };
674
675 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_amd64);
676 void be_init_arch_amd64(void)
677 {
678         be_register_isa_if("amd64", &amd64_isa_if);
679         FIRM_DBG_REGISTER(dbg, "firm.be.amd64.cg");
680         amd64_init_transform();
681 }