remove license stuff from files
[libfirm] / ir / be / arm / bearch_arm.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 arm backend driver file.
9  * @author  Matthias Braun, Oliver Richter, Tobias Gneist
10  */
11 #include "config.h"
12
13 #include "lc_opts.h"
14 #include "lc_opts_enum.h"
15
16 #include "irgwalk.h"
17 #include "irprog.h"
18 #include "irprintf.h"
19 #include "ircons.h"
20 #include "irgmod.h"
21 #include "irgopt.h"
22 #include "iroptimize.h"
23 #include "irdump.h"
24 #include "lower_calls.h"
25 #include "error.h"
26
27 #include "bitset.h"
28 #include "debug.h"
29 #include "array_t.h"
30 #include "irtools.h"
31
32 #include "bearch.h"
33 #include "benode.h"
34 #include "belower.h"
35 #include "besched.h"
36 #include "be.h"
37 #include "bemodule.h"
38 #include "beirg.h"
39 #include "bespillslots.h"
40 #include "bespillutil.h"
41 #include "begnuas.h"
42 #include "belistsched.h"
43 #include "beflags.h"
44 #include "bestack.h"
45
46 #include "bearch_arm_t.h"
47
48 #include "arm_new_nodes.h"
49 #include "gen_arm_regalloc_if.h"
50 #include "arm_transform.h"
51 #include "arm_optimize.h"
52 #include "arm_emitter.h"
53 #include "arm_map_regs.h"
54
55 static ir_entity *arm_get_frame_entity(const ir_node *irn)
56 {
57         const arm_attr_t *attr = get_arm_attr_const(irn);
58
59         if (is_arm_FrameAddr(irn)) {
60                 const arm_SymConst_attr_t *frame_attr = get_arm_SymConst_attr_const(irn);
61                 return frame_attr->entity;
62         }
63         if (attr->is_load_store) {
64                 const arm_load_store_attr_t *load_store_attr
65                         = get_arm_load_store_attr_const(irn);
66                 if (load_store_attr->is_frame_entity) {
67                         return load_store_attr->entity;
68                 }
69         }
70         return NULL;
71 }
72
73 /**
74  * This function is called by the generic backend to correct offsets for
75  * nodes accessing the stack.
76  */
77 static void arm_set_stack_bias(ir_node *irn, int bias)
78 {
79         if (is_arm_FrameAddr(irn)) {
80                 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(irn);
81                 attr->fp_offset += bias;
82         } else {
83                 arm_load_store_attr_t *attr = get_arm_load_store_attr(irn);
84                 assert(attr->base.is_load_store);
85                 attr->offset += bias;
86         }
87 }
88
89 static int arm_get_sp_bias(const ir_node *irn)
90 {
91         /* We don't have any nodes changing the stack pointer.
92            We probably want to support post-/pre increment/decrement later */
93         (void) irn;
94         return 0;
95 }
96
97 /* fill register allocator interface */
98
99 static const arch_irn_ops_t arm_irn_ops = {
100         arm_get_frame_entity,
101         arm_set_stack_bias,
102         arm_get_sp_bias,
103         NULL,    /* get_op_estimated_cost   */
104         NULL,    /* possible_memory_operand */
105         NULL,    /* perform_memory_operand  */
106 };
107
108 /**
109  * Transforms the standard Firm graph into
110  * a ARM firm graph.
111  */
112 static void arm_prepare_graph(ir_graph *irg)
113 {
114         /* transform nodes into assembler instructions */
115         arm_transform_graph(irg);
116
117         /* do local optimizations (mainly CSE) */
118         local_optimize_graph(irg);
119
120         /* do code placement, to optimize the position of constants */
121         place_code(irg);
122 }
123
124 static void arm_collect_frame_entity_nodes(ir_node *node, void *data)
125 {
126         be_fec_env_t  *env = (be_fec_env_t*)data;
127         const ir_mode *mode;
128         int            align;
129         ir_entity     *entity;
130         const arm_load_store_attr_t *attr;
131
132         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
133                 mode  = get_irn_mode(node);
134                 align = get_mode_size_bytes(mode);
135                 be_node_needs_frame_entity(env, node, mode, align);
136                 return;
137         }
138
139         switch (get_arm_irn_opcode(node)) {
140         case iro_arm_Ldf:
141         case iro_arm_Ldr:
142                 break;
143         default:
144                 return;
145         }
146
147         attr   = get_arm_load_store_attr_const(node);
148         entity = attr->entity;
149         mode   = attr->load_store_mode;
150         align  = get_mode_size_bytes(mode);
151         if (entity != NULL)
152                 return;
153         if (!attr->is_frame_entity)
154                 return;
155         be_node_needs_frame_entity(env, node, mode, align);
156 }
157
158 static void arm_set_frame_entity(ir_node *node, ir_entity *entity)
159 {
160         if (is_be_node(node)) {
161                 be_node_set_frame_entity(node, entity);
162         } else {
163                 arm_load_store_attr_t *attr = get_arm_load_store_attr(node);
164                 attr->entity = entity;
165         }
166 }
167
168 static void transform_Reload(ir_node *node)
169 {
170         ir_node   *block  = get_nodes_block(node);
171         dbg_info  *dbgi   = get_irn_dbg_info(node);
172         ir_node   *ptr    = get_irn_n(node, n_be_Reload_frame);
173         ir_node   *mem    = get_irn_n(node, n_be_Reload_mem);
174         ir_mode   *mode   = get_irn_mode(node);
175         ir_entity *entity = be_get_frame_entity(node);
176         const arch_register_t *reg;
177         ir_node   *proj;
178         ir_node   *load;
179
180         load = new_bd_arm_Ldr(dbgi, block, ptr, mem, mode, entity, false, 0, true);
181         sched_replace(node, load);
182
183         proj = new_rd_Proj(dbgi, load, mode, pn_arm_Ldr_res);
184
185         reg = arch_get_irn_register(node);
186         arch_set_irn_register(proj, reg);
187
188         exchange(node, proj);
189 }
190
191 static void transform_Spill(ir_node *node)
192 {
193         ir_node   *block  = get_nodes_block(node);
194         dbg_info  *dbgi   = get_irn_dbg_info(node);
195         ir_node   *ptr    = get_irn_n(node, n_be_Spill_frame);
196         ir_graph  *irg    = get_irn_irg(node);
197         ir_node   *mem    = get_irg_no_mem(irg);
198         ir_node   *val    = get_irn_n(node, n_be_Spill_val);
199         ir_mode   *mode   = get_irn_mode(val);
200         ir_entity *entity = be_get_frame_entity(node);
201         ir_node   *store;
202
203         store = new_bd_arm_Str(dbgi, block, ptr, val, mem, mode, entity, false, 0,
204                                true);
205         sched_replace(node, store);
206
207         exchange(node, store);
208 }
209
210 static void arm_after_ra_walker(ir_node *block, void *data)
211 {
212         ir_node *node, *prev;
213         (void) data;
214
215         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
216                 prev = sched_prev(node);
217
218                 if (be_is_Reload(node)) {
219                         transform_Reload(node);
220                 } else if (be_is_Spill(node)) {
221                         transform_Spill(node);
222                 }
223         }
224 }
225
226 /**
227  * Called immediately before emit phase.
228  */
229 static void arm_finish_irg(ir_graph *irg)
230 {
231         be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
232         bool               at_begin     = stack_layout->sp_relative ? true : false;
233         be_fec_env_t      *fec_env      = be_new_frame_entity_coalescer(irg);
234
235         irg_walk_graph(irg, NULL, arm_collect_frame_entity_nodes, fec_env);
236         be_assign_entities(fec_env, arm_set_frame_entity, at_begin);
237         be_free_frame_entity_coalescer(fec_env);
238
239         irg_block_walk_graph(irg, NULL, arm_after_ra_walker, NULL);
240
241         /* fix stack entity offsets */
242         be_abi_fix_stack_nodes(irg);
243         be_abi_fix_stack_bias(irg);
244
245         /* do peephole optimizations and fix stack offsets */
246         arm_peephole_optimization(irg);
247 }
248
249 static void arm_before_ra(ir_graph *irg)
250 {
251         be_sched_fix_flags(irg, &arm_reg_classes[CLASS_arm_flags], NULL, NULL);
252 }
253
254 /**
255  * Maps all intrinsic calls that the backend support
256  * and map all instructions the backend did not support
257  * to runtime calls.
258  */
259 static void arm_handle_intrinsics(void)
260 {
261         ir_type *tp, *int_tp, *uint_tp;
262         i_record records[8];
263         int n_records = 0;
264
265         runtime_rt rt_iDiv, rt_uDiv, rt_iMod, rt_uMod;
266
267 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
268
269         int_tp  = get_type_for_mode(mode_Is);
270         uint_tp = get_type_for_mode(mode_Iu);
271
272         /* ARM has neither a signed div instruction ... */
273         {
274                 i_instr_record *map_Div = &records[n_records++].i_instr;
275
276                 tp = new_type_method(2, 1);
277                 set_method_param_type(tp, 0, int_tp);
278                 set_method_param_type(tp, 1, int_tp);
279                 set_method_res_type(tp, 0, int_tp);
280
281                 rt_iDiv.ent             = new_entity(get_glob_type(), ID("__divsi3"), tp);
282                 set_entity_ld_ident(rt_iDiv.ent, ID("__divsi3"));
283                 rt_iDiv.mode            = mode_T;
284                 rt_iDiv.res_mode        = mode_Is;
285                 rt_iDiv.mem_proj_nr     = pn_Div_M;
286                 rt_iDiv.regular_proj_nr = pn_Div_X_regular;
287                 rt_iDiv.exc_proj_nr     = pn_Div_X_except;
288                 rt_iDiv.res_proj_nr     = pn_Div_res;
289
290                 add_entity_linkage(rt_iDiv.ent, IR_LINKAGE_CONSTANT);
291                 set_entity_visibility(rt_iDiv.ent, ir_visibility_external);
292
293                 map_Div->kind     = INTRINSIC_INSTR;
294                 map_Div->op       = op_Div;
295                 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
296                 map_Div->ctx      = &rt_iDiv;
297         }
298         /* ... nor an unsigned div instruction ... */
299         {
300                 i_instr_record *map_Div = &records[n_records++].i_instr;
301
302                 tp = new_type_method(2, 1);
303                 set_method_param_type(tp, 0, uint_tp);
304                 set_method_param_type(tp, 1, uint_tp);
305                 set_method_res_type(tp, 0, uint_tp);
306
307                 rt_uDiv.ent             = new_entity(get_glob_type(), ID("__udivsi3"), tp);
308                 set_entity_ld_ident(rt_uDiv.ent, ID("__udivsi3"));
309                 rt_uDiv.mode            = mode_T;
310                 rt_uDiv.res_mode        = mode_Iu;
311                 rt_uDiv.mem_proj_nr     = pn_Div_M;
312                 rt_uDiv.regular_proj_nr = pn_Div_X_regular;
313                 rt_uDiv.exc_proj_nr     = pn_Div_X_except;
314                 rt_uDiv.res_proj_nr     = pn_Div_res;
315
316                 set_entity_visibility(rt_uDiv.ent, ir_visibility_external);
317
318                 map_Div->kind     = INTRINSIC_INSTR;
319                 map_Div->op       = op_Div;
320                 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
321                 map_Div->ctx      = &rt_uDiv;
322         }
323         /* ... nor a signed mod instruction ... */
324         {
325                 i_instr_record *map_Mod = &records[n_records++].i_instr;
326
327                 tp = new_type_method(2, 1);
328                 set_method_param_type(tp, 0, int_tp);
329                 set_method_param_type(tp, 1, int_tp);
330                 set_method_res_type(tp, 0, int_tp);
331
332                 rt_iMod.ent             = new_entity(get_glob_type(), ID("__modsi3"), tp);
333                 set_entity_ld_ident(rt_iMod.ent, ID("__modsi3"));
334                 rt_iMod.mode            = mode_T;
335                 rt_iMod.res_mode        = mode_Is;
336                 rt_iMod.mem_proj_nr     = pn_Mod_M;
337                 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
338                 rt_iMod.exc_proj_nr     = pn_Mod_X_except;
339                 rt_iMod.res_proj_nr     = pn_Mod_res;
340
341                 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
342
343                 map_Mod->kind     = INTRINSIC_INSTR;
344                 map_Mod->op       = op_Mod;
345                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
346                 map_Mod->ctx      = &rt_iMod;
347         }
348         /* ... nor an unsigned mod. */
349         {
350                 i_instr_record *map_Mod = &records[n_records++].i_instr;
351
352                 tp = new_type_method(2, 1);
353                 set_method_param_type(tp, 0, uint_tp);
354                 set_method_param_type(tp, 1, uint_tp);
355                 set_method_res_type(tp, 0, uint_tp);
356
357                 rt_uMod.ent             = new_entity(get_glob_type(), ID("__umodsi3"), tp);
358                 set_entity_ld_ident(rt_uMod.ent, ID("__umodsi3"));
359                 rt_uMod.mode            = mode_T;
360                 rt_uMod.res_mode        = mode_Iu;
361                 rt_uMod.mem_proj_nr     = pn_Mod_M;
362                 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
363                 rt_uMod.exc_proj_nr     = pn_Mod_X_except;
364                 rt_uMod.res_proj_nr     = pn_Mod_res;
365
366                 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
367
368                 map_Mod->kind     = INTRINSIC_INSTR;
369                 map_Mod->op       = op_Mod;
370                 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
371                 map_Mod->ctx      = &rt_uMod;
372         }
373
374         if (n_records > 0)
375                 lower_intrinsics(records, n_records, /*part_block_used=*/0);
376 }
377
378 extern const arch_isa_if_t arm_isa_if;
379 static arm_isa_t arm_isa_template = {
380         {
381                 &arm_isa_if,             /* isa interface */
382                 N_ARM_REGISTERS,
383                 arm_registers,
384                 N_ARM_CLASSES,
385                 arm_reg_classes,
386                 &arm_registers[REG_SP],  /* stack pointer */
387                 &arm_registers[REG_R11], /* base pointer */
388                 2,                       /* power of two stack alignment for calls, 2^2 == 4 */
389                 7,                       /* spill costs */
390                 5,                       /* reload costs */
391                 true,                    /* we do have custom abi handling */
392         },
393         ARM_FPU_ARCH_FPE,          /* FPU architecture */
394 };
395
396 static void arm_init(void)
397 {
398         arm_register_init();
399
400         arm_create_opcodes(&arm_irn_ops);
401 }
402
403 static void arm_finish(void)
404 {
405         arm_free_opcodes();
406 }
407
408 static arch_env_t *arm_begin_codegeneration(void)
409 {
410         arm_isa_t *isa = XMALLOC(arm_isa_t);
411         *isa = arm_isa_template;
412
413         be_gas_emit_types = false;
414
415         return &isa->base;
416 }
417
418 /**
419  * Closes the output file and frees the ISA structure.
420  */
421 static void arm_end_codegeneration(void *self)
422 {
423         free(self);
424 }
425
426 /**
427  * Allows or disallows the creation of Psi nodes for the given Phi nodes.
428  * @return 1 if allowed, 0 otherwise
429  */
430 static int arm_is_mux_allowed(ir_node *sel, ir_node *mux_false,
431                               ir_node *mux_true)
432 {
433         (void) sel;
434         (void) mux_false;
435         (void) mux_true;
436         return false;
437 }
438
439 static asm_constraint_flags_t arm_parse_asm_constraint(const char **c)
440 {
441         /* asm not supported */
442         (void) c;
443         return ASM_CONSTRAINT_FLAG_INVALID;
444 }
445
446 static int arm_is_valid_clobber(const char *clobber)
447 {
448         (void) clobber;
449         return 0;
450 }
451
452 static void arm_lower_for_target(void)
453 {
454         ir_mode *mode_gp = arm_reg_classes[CLASS_arm_gp].mode;
455         size_t i, n_irgs = get_irp_n_irgs();
456
457         /* lower compound param handling */
458         lower_calls_with_compounds(LF_RETURN_HIDDEN);
459
460         for (i = 0; i < n_irgs; ++i) {
461                 ir_graph *irg = get_irp_irg(i);
462                 lower_switch(irg, 4, 256, mode_gp);
463         }
464
465         for (i = 0; i < n_irgs; ++i) {
466                 ir_graph *irg = get_irp_irg(i);
467                 /* Turn all small CopyBs into loads/stores and all bigger CopyBs into
468                  * memcpy calls.
469                  * TODO:  These constants need arm-specific tuning. */
470                 lower_CopyB(irg, 31, 32, false);
471         }
472 }
473
474 /**
475  * Returns the libFirm configuration parameter for this backend.
476  */
477 static const backend_params *arm_get_libfirm_params(void)
478 {
479         static ir_settings_arch_dep_t ad = {
480                 1,    /* allow subs */
481                 1,    /* Muls are fast enough on ARM but ... */
482                 31,   /* ... one shift would be possible better */
483                 NULL, /* no evaluator function */
484                 0,    /* SMUL is needed, only in Arch M */
485                 0,    /* UMUL is needed, only in Arch M */
486                 32,   /* SMUL & UMUL available for 32 bit */
487         };
488         static backend_params p = {
489                 0,     /* don't support inline assembler yet */
490                 1,     /* support Rotl nodes */
491                 1,     /* big endian */
492                 1,     /* modulo shift efficient */
493                 0,     /* non-modulo shift not efficient */
494                 &ad,   /* will be set later */
495                 arm_is_mux_allowed, /* allow_ifconv function */
496                 32,    /* machine size */
497                 NULL,  /* float arithmetic mode (TODO) */
498                 NULL,  /* long long type */
499                 NULL,  /* unsigned long long type */
500                 NULL,  /* long double type */
501                 0,     /* no trampoline support: size 0 */
502                 0,     /* no trampoline support: align 0 */
503                 NULL,  /* no trampoline support: no trampoline builder */
504                 4      /* alignment of stack parameter */
505         };
506
507         return &p;
508 }
509
510 /* fpu set architectures. */
511 static const lc_opt_enum_int_items_t arm_fpu_items[] = {
512         { "softfloat", ARM_FPU_ARCH_SOFTFLOAT },
513         { "fpe",       ARM_FPU_ARCH_FPE },
514         { "fpa",       ARM_FPU_ARCH_FPA },
515         { "vfp1xd",    ARM_FPU_ARCH_VFP_V1xD },
516         { "vfp1",      ARM_FPU_ARCH_VFP_V1 },
517         { "vfp2",      ARM_FPU_ARCH_VFP_V2 },
518         { NULL,        0 }
519 };
520
521 static lc_opt_enum_int_var_t arch_fpu_var = {
522         &arm_isa_template.fpu_arch, arm_fpu_items
523 };
524
525 static const lc_opt_table_entry_t arm_options[] = {
526         LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &arch_fpu_var),
527         LC_OPT_LAST
528 };
529
530 const arch_isa_if_t arm_isa_if = {
531         arm_init,
532         arm_finish,
533         arm_get_libfirm_params,
534         arm_lower_for_target,
535         arm_parse_asm_constraint,
536         arm_is_valid_clobber,
537
538         arm_begin_codegeneration,
539         arm_end_codegeneration,
540         NULL,
541         NULL,  /* get call abi */
542         NULL,  /* mark remat */
543         NULL,  /* get_pic_base */
544         be_new_spill,
545         be_new_reload,
546         NULL,  /* register_saved_by */
547
548         arm_handle_intrinsics, /* handle_intrinsics */
549         NULL,  /* before_abi */
550         arm_prepare_graph,
551         arm_before_ra,
552         arm_finish_irg,
553         arm_gen_routine,
554 };
555
556 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_arm)
557 void be_init_arch_arm(void)
558 {
559         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
560         lc_opt_entry_t *arm_grp = lc_opt_get_grp(be_grp, "arm");
561
562         lc_opt_add_table(arm_grp, arm_options);
563
564         be_register_isa_if("arm", &arm_isa_if);
565
566         arm_init_transform();
567         arm_init_emitter();
568 }