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