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