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