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