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