merge after_ra and finish phase, and move stack_bias fixing into backends
[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 "irgwalk.h"
28 #include "irprog.h"
29 #include "irprintf.h"
30 #include "ircons.h"
31 #include "irgmod.h"
32 #include "irdump.h"
33 #include "lower_calls.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 #include "../bestack.h"
50
51 #include "bearch_amd64_t.h"
52
53 #include "amd64_new_nodes.h"
54 #include "gen_amd64_regalloc_if.h"
55 #include "amd64_transform.h"
56 #include "amd64_emitter.h"
57
58 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
59
60 static arch_irn_class_t amd64_classify(const ir_node *irn)
61 {
62         (void) irn;
63         return arch_irn_class_none;
64 }
65
66 static ir_entity *amd64_get_frame_entity(const ir_node *node)
67 {
68         if (is_amd64_FrameAddr(node)) {
69                 const amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr_const(node);
70                 return attr->entity;
71
72         } else if (is_amd64_Store(node)) {
73                 const amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr_const(node);
74                 return attr->entity;
75
76         } else if (is_amd64_Load(node)) {
77                 const amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr_const(node);
78                 return attr->entity;
79         }
80
81         (void) node;
82         /* TODO: return the ir_entity assigned to the frame */
83         return NULL;
84 }
85
86 /**
87  * This function is called by the generic backend to correct offsets for
88  * nodes accessing the stack.
89  */
90 static void amd64_set_frame_offset(ir_node *irn, int offset)
91 {
92         if (is_amd64_FrameAddr(irn)) {
93                 amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr(irn);
94                 attr->fp_offset += offset;
95
96         } else if (is_amd64_Store(irn)) {
97                 amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr(irn);
98                 attr->fp_offset += offset;
99
100         } else if (is_amd64_Load(irn)) {
101                 amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr(irn);
102                 attr->fp_offset += offset;
103
104         }
105 }
106
107 static int amd64_get_sp_bias(const ir_node *irn)
108 {
109         (void) irn;
110         return 0;
111 }
112
113 /* fill register allocator interface */
114
115 static const arch_irn_ops_t amd64_irn_ops = {
116         amd64_classify,
117         amd64_get_frame_entity,
118         amd64_set_frame_offset,
119         amd64_get_sp_bias,
120         NULL,    /* get_inverse             */
121         NULL,    /* get_op_estimated_cost   */
122         NULL,    /* possible_memory_operand */
123         NULL,    /* perform_memory_operand  */
124 };
125
126
127
128 /**
129  * Transforms the standard firm graph into
130  * a amd64 firm graph
131  */
132 static void amd64_prepare_graph(ir_graph *irg)
133 {
134         amd64_irg_data_t *irg_data = amd64_get_irg_data(irg);
135         amd64_transform_graph(irg);
136
137         if (irg_data->dump)
138                 dump_ir_graph(irg, "transformed");
139 }
140
141 static void amd64_before_ra(ir_graph *irg)
142 {
143         be_sched_fix_flags(irg, &amd64_reg_classes[CLASS_amd64_flags], NULL, NULL);
144 }
145
146 static void transform_Reload(ir_node *node)
147 {
148         ir_graph  *irg    = get_irn_irg(node);
149         ir_node   *block  = get_nodes_block(node);
150         dbg_info  *dbgi   = get_irn_dbg_info(node);
151         ir_node   *ptr    = get_irg_frame(irg);
152         ir_node   *mem    = get_irn_n(node, n_be_Reload_mem);
153         ir_mode   *mode   = get_irn_mode(node);
154         ir_entity *entity = be_get_frame_entity(node);
155         const arch_register_t *reg;
156         ir_node   *proj;
157         ir_node   *load;
158
159         ir_node  *sched_point = sched_prev(node);
160
161         load = new_bd_amd64_Load(dbgi, block, ptr, mem, entity);
162         sched_add_after(sched_point, load);
163         sched_remove(node);
164
165         proj = new_rd_Proj(dbgi, load, mode, pn_amd64_Load_res);
166
167         reg = arch_get_irn_register(node);
168         arch_set_irn_register(proj, reg);
169
170         exchange(node, proj);
171 }
172
173 static void transform_Spill(ir_node *node)
174 {
175         ir_graph  *irg    = get_irn_irg(node);
176         ir_node   *block  = get_nodes_block(node);
177         dbg_info  *dbgi   = get_irn_dbg_info(node);
178         ir_node   *ptr    = get_irg_frame(irg);
179         ir_node   *mem    = get_irg_no_mem(irg);
180         ir_node   *val    = get_irn_n(node, n_be_Spill_val);
181         //ir_mode   *mode   = get_irn_mode(val);
182         ir_entity *entity = be_get_frame_entity(node);
183         ir_node   *sched_point;
184         ir_node   *store;
185
186         sched_point = sched_prev(node);
187         store = new_bd_amd64_Store(dbgi, block, ptr, val, mem, entity);
188
189         sched_remove(node);
190         sched_add_after(sched_point, store);
191
192         exchange(node, store);
193 }
194
195 static void amd64_after_ra_walker(ir_node *block, void *data)
196 {
197         ir_node *node, *prev;
198         (void) data;
199
200         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
201                 prev = sched_prev(node);
202
203                 if (be_is_Reload(node)) {
204                         transform_Reload(node);
205                 } else if (be_is_Spill(node)) {
206                         transform_Spill(node);
207                 }
208         }
209 }
210
211 static void amd64_set_frame_entity(ir_node *node, ir_entity *entity)
212 {
213         assert(be_is_Reload(node));
214         be_node_set_frame_entity(node, entity);
215 }
216
217 /**
218  * Collects nodes that need frame entities assigned.
219  */
220 static void amd64_collect_frame_entity_nodes(ir_node *node, void *data)
221 {
222         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
223                 be_fec_env_t  *env   = (be_fec_env_t*)data;
224                 const ir_mode *mode  = get_irn_mode(node);
225                 int            align = get_mode_size_bytes(mode);
226                 be_node_needs_frame_entity(env, node, mode, align);
227         }
228 }
229
230 /**
231  * Called immediatly before emit phase.
232  */
233 static void amd64_finish_irg(ir_graph *irg)
234 {
235         be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
236         bool               at_begin     = stack_layout->sp_relative ? true : false;
237         be_fec_env_t      *fec_env      = be_new_frame_entity_coalescer(irg);
238
239         /* create and coalesce frame entities */
240         irg_walk_graph(irg, NULL, amd64_collect_frame_entity_nodes, fec_env);
241         be_assign_entities(fec_env, amd64_set_frame_entity, at_begin);
242         be_free_frame_entity_coalescer(fec_env);
243
244         irg_block_walk_graph(irg, NULL, amd64_after_ra_walker, NULL);
245
246         /* fix stack entity offsets */
247         be_abi_fix_stack_nodes(irg);
248         be_abi_fix_stack_bias(irg);
249 }
250
251 /**
252  * Initializes the code generator.
253  */
254 static void amd64_init_graph(ir_graph *irg)
255 {
256         struct obstack   *obst     = be_get_be_obst(irg);
257         amd64_irg_data_t *irg_data = OALLOCZ(obst, amd64_irg_data_t);
258         irg_data->dump = (be_get_irg_options(irg)->dump_flags & DUMP_BE) ? 1 : 0;
259
260         be_birg_from_irg(irg)->isa_link = irg_data;
261 }
262
263
264 typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_node *block);
265
266 /**
267  * Used to create per-graph unique pseudo nodes.
268  */
269 static inline ir_node *create_const(ir_graph *irg, ir_node **place,
270                                     create_const_node_func func,
271                                     const arch_register_t* reg)
272 {
273         ir_node *block, *res;
274
275         if (*place != NULL)
276                 return *place;
277
278         block = get_irg_start_block(irg);
279         res = func(NULL, block);
280         arch_set_irn_register(res, reg);
281         *place = res;
282
283         return res;
284 }
285
286 extern const arch_isa_if_t amd64_isa_if;
287 static amd64_isa_t amd64_isa_template = {
288         {
289                 &amd64_isa_if,             /* isa interface implementation */
290                 N_AMD64_REGISTERS,
291                 amd64_registers,
292                 N_AMD64_CLASSES,
293                 amd64_reg_classes,
294                 &amd64_registers[REG_RSP],  /* stack pointer register */
295                 &amd64_registers[REG_RBP],  /* base pointer register */
296                 &amd64_reg_classes[CLASS_amd64_gp],  /* link pointer register class */
297                 3,                           /* power of two stack alignment for calls, 2^2 == 4 */
298                 NULL,                        /* main environment */
299                 7,                           /* costs for a spill instruction */
300                 5,                           /* costs for a reload instruction */
301                 false,                       /* no custom abi handling */
302         },
303 };
304
305 /**
306  * Initializes the backend ISA
307  */
308 static arch_env_t *amd64_init(FILE *outfile)
309 {
310         amd64_isa_t *isa = XMALLOC(amd64_isa_t);
311         *isa = amd64_isa_template;
312
313         be_emit_init(outfile);
314
315         amd64_register_init();
316         amd64_create_opcodes(&amd64_irn_ops);
317
318         return &isa->base;
319 }
320
321
322
323 /**
324  * Closes the output file and frees the ISA structure.
325  */
326 static void amd64_done(void *self)
327 {
328         amd64_isa_t *isa = (amd64_isa_t*)self;
329
330         /* emit now all global declarations */
331         be_gas_emit_decls(isa->base.main_env);
332
333         be_emit_exit();
334         free(self);
335 }
336
337
338 /**
339  * Get the register class which shall be used to store a value of a given mode.
340  * @param self The this pointer.
341  * @param mode The mode in question.
342  * @return A register class which can hold values of the given mode.
343  */
344 static const arch_register_class_t *amd64_get_reg_class_for_mode(const ir_mode *mode)
345 {
346         assert(!mode_is_float(mode));
347         return &amd64_reg_classes[CLASS_amd64_gp];
348 }
349
350
351
352 typedef struct {
353         be_abi_call_flags_bits_t flags;
354         ir_graph *irg;
355 } amd64_abi_env_t;
356
357 /**
358  * Get the between type for that call.
359  * @param self The callback object.
360  * @return The between type of for that call.
361  */
362 static ir_type *amd64_get_between_type(ir_graph *irg)
363 {
364         static ir_type *between_type = NULL;
365         static ir_entity *old_bp_ent = NULL;
366         (void) irg;
367
368         if(!between_type) {
369                 ir_entity *ret_addr_ent;
370                 ir_type *ret_addr_type = new_type_primitive(mode_P);
371                 ir_type *old_bp_type   = new_type_primitive(mode_P);
372
373                 between_type           = new_type_class(new_id_from_str("amd64_between_type"));
374                 old_bp_ent             = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
375                 ret_addr_ent           = new_entity(between_type, new_id_from_str("old_bp"), ret_addr_type);
376
377                 set_entity_offset(old_bp_ent, 0);
378                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
379                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
380         }
381
382         return between_type;
383 }
384
385 static const be_abi_callbacks_t amd64_abi_callbacks = {
386         amd64_get_between_type,
387 };
388
389 static const arch_register_t *gpreg_param_reg_std[] = {
390         &amd64_registers[REG_RDI],
391         &amd64_registers[REG_RSI],
392         &amd64_registers[REG_RDX],
393         &amd64_registers[REG_RCX],
394         &amd64_registers[REG_R8],
395         &amd64_registers[REG_R9],
396 };
397
398 static const arch_register_t *amd64_get_RegParam_reg(int n)
399 {
400         assert(n < 6 && n >=0 && "register param > 6 requested");
401         return gpreg_param_reg_std[n];
402 }
403
404 /**
405  * Get the ABI restrictions for procedure calls.
406  * @param self        The this pointer.
407  * @param method_type The type of the method (procedure) in question.
408  * @param abi         The abi object to be modified
409  */
410 static void amd64_get_call_abi(const void *self, ir_type *method_type,
411                            be_abi_call_t *abi)
412 {
413         ir_type  *tp;
414         ir_mode  *mode;
415         int       i, n = get_method_n_params(method_type);
416         be_abi_call_flags_t call_flags;
417         int no_reg = 0;
418
419         (void) self;
420
421         /* set abi flags for calls */
422         call_flags.bits.store_args_sequential = 0;
423         call_flags.bits.try_omit_fp           = 1;
424         call_flags.bits.fp_free               = 0;
425         call_flags.bits.call_has_imm          = 1;
426
427         /* set stack parameter passing style */
428         be_abi_call_set_flags(abi, call_flags, &amd64_abi_callbacks);
429
430         for (i = 0; i < n; i++) {
431                 tp   = get_method_param_type(method_type, i);
432                 mode = get_type_mode(tp);
433                 //d// printf ("MODE %p %p XX %d\n", mode, mode_Iu, i);
434
435                 if (!no_reg && i < 6 && mode_is_data (mode)) {
436                         //d// printf("TEST%d\n", i);
437                         be_abi_call_param_reg(abi, i, amd64_get_RegParam_reg (i),
438                                               ABI_CONTEXT_BOTH);
439                 /* default: all parameters on stack */
440                 } else {
441                         no_reg = 1;
442                         be_abi_call_param_stack(abi, i, mode, 8, 0, 0, ABI_CONTEXT_BOTH);
443                 }
444         }
445
446         /* TODO: set correct return register */
447         /* default: return value is in R0 resp. F0 */
448         if (get_method_n_ress(method_type) > 0) {
449                 tp   = get_method_res_type(method_type, 0);
450                 mode = get_type_mode(tp);
451
452                 /* FIXME: No floating point yet */
453                 /* be_abi_call_res_reg(abi, 0,
454                         mode_is_float(mode) ? &amd64_fp_regs[REG_F0] : &amd64_registers[REG_R0], ABI_CONTEXT_BOTH); */
455
456                 be_abi_call_res_reg(abi, 0,
457                         &amd64_registers[REG_RAX], ABI_CONTEXT_BOTH);
458         }
459 }
460
461 /**
462  * Returns the necessary byte alignment for storing a register of given class.
463  */
464 static int amd64_get_reg_class_alignment(const arch_register_class_t *cls)
465 {
466         ir_mode *mode = arch_register_class_mode(cls);
467         return get_mode_size_bytes(mode);
468 }
469
470 static void amd64_lower_for_target(void)
471 {
472         /* lower compound param handling */
473         lower_calls_with_compounds(LF_RETURN_HIDDEN);
474 }
475
476 static int amd64_is_mux_allowed(ir_node *sel, ir_node *mux_false,
477                                 ir_node *mux_true)
478 {
479         (void) sel;
480         (void) mux_false;
481         (void) mux_true;
482         return false;
483 }
484
485 /**
486  * Returns the libFirm configuration parameter for this backend.
487  */
488 static const backend_params *amd64_get_backend_params(void) {
489         static backend_params p = {
490                 0,     /* no inline assembly */
491                 1,     /* support Rotl nodes */
492                 0,     /* little endian */
493                 1,     /* modulo shift is efficient */
494                 0,     /* non-modulo shift is not efficient */
495                 NULL,  /* will be set later */
496                 amd64_is_mux_allowed,  /* parameter for if conversion */
497                 64,    /* machine size */
498                 NULL,  /* float arithmetic mode */
499                 NULL,  /* long long type */
500                 NULL,  /* unsigned long long type */
501                 NULL,  /* long double type (not supported yet) */
502                 0,     /* no trampoline support: size 0 */
503                 0,     /* no trampoline support: align 0 */
504                 NULL,  /* no trampoline support: no trampoline builder */
505                 8      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
506         };
507         return &p;
508 }
509
510 static ir_graph **amd64_get_backend_irg_list(const void *self,
511                                                 ir_graph ***irgs)
512 {
513         (void) self;
514         (void) irgs;
515         return NULL;
516 }
517
518 static asm_constraint_flags_t amd64_parse_asm_constraint(const char **c)
519 {
520         (void) c;
521         return ASM_CONSTRAINT_FLAG_INVALID;
522 }
523
524 static int amd64_is_valid_clobber(const char *clobber)
525 {
526         (void) clobber;
527         return 0;
528 }
529
530 static int amd64_register_saved_by(const arch_register_t *reg, int callee)
531 {
532         if (callee) {
533                 /* check for callee saved */
534                 if (reg->reg_class == &amd64_reg_classes[CLASS_amd64_gp]) {
535                         switch (reg->index) {
536                         case REG_GP_RBX:
537                         case REG_GP_RBP:
538                         case REG_GP_R12:
539                         case REG_GP_R13:
540                         case REG_GP_R14:
541                         case REG_GP_R15:
542                                 return 1;
543                         default:
544                                 return 0;
545                         }
546                 }
547         } else {
548                 /* check for caller saved */
549                 if (reg->reg_class == &amd64_reg_classes[CLASS_amd64_gp]) {
550                         switch (reg->index) {
551                         case REG_GP_RAX:
552                         case REG_GP_RCX:
553                         case REG_GP_RDX:
554                         case REG_GP_RSI:
555                         case REG_GP_RDI:
556                         case REG_GP_R8:
557                         case REG_GP_R9:
558                         case REG_GP_R10:
559                         case REG_GP_R11:
560                                 return 1;
561                         default:
562                                 return 0;
563                         }
564                 }
565         }
566         return 0;
567 }
568
569 const arch_isa_if_t amd64_isa_if = {
570         amd64_init,
571         amd64_lower_for_target,
572         amd64_done,
573         NULL,                /* handle intrinsics */
574         amd64_get_reg_class_for_mode,
575         amd64_get_call_abi,
576         amd64_get_reg_class_alignment,
577     amd64_get_backend_params,
578         amd64_get_backend_irg_list,
579         NULL,                    /* mark remat */
580         amd64_parse_asm_constraint,
581         amd64_is_valid_clobber,
582
583         amd64_init_graph,
584         NULL,              /* get_pic_base */
585         NULL,              /* before_abi */
586         amd64_prepare_graph,
587         amd64_before_ra,
588         amd64_finish_irg,
589         amd64_gen_routine,
590         amd64_register_saved_by,
591 };
592
593 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_amd64)
594 void be_init_arch_amd64(void)
595 {
596         be_register_isa_if("amd64", &amd64_isa_if);
597         FIRM_DBG_REGISTER(dbg, "firm.be.amd64.cg");
598         amd64_init_transform();
599 }