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