2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief The main arm backend driver file.
23 * @author Matthias Braun, Oliver Richter, Tobias Gneist
28 #include "lc_opts_enum.h"
36 #include "iroptimize.h"
38 #include "lower_calls.h"
51 #include "bemachine.h"
54 #include "bespillslots.h"
55 #include "bespillutil.h"
57 #include "belistsched.h"
61 #include "bearch_arm_t.h"
63 #include "arm_new_nodes.h"
64 #include "gen_arm_regalloc_if.h"
65 #include "arm_transform.h"
66 #include "arm_optimize.h"
67 #include "arm_emitter.h"
68 #include "arm_map_regs.h"
70 static arch_irn_class_t arm_classify(const ir_node *irn)
73 /* TODO: we should mark reload/spill instructions and classify them here */
74 return arch_irn_class_none;
77 static ir_entity *arm_get_frame_entity(const ir_node *irn)
79 const arm_attr_t *attr = get_arm_attr_const(irn);
81 if (is_arm_FrameAddr(irn)) {
82 const arm_SymConst_attr_t *frame_attr = get_arm_SymConst_attr_const(irn);
83 return frame_attr->entity;
85 if (attr->is_load_store) {
86 const arm_load_store_attr_t *load_store_attr
87 = get_arm_load_store_attr_const(irn);
88 if (load_store_attr->is_frame_entity) {
89 return load_store_attr->entity;
96 * This function is called by the generic backend to correct offsets for
97 * nodes accessing the stack.
99 static void arm_set_stack_bias(ir_node *irn, int bias)
101 if (is_arm_FrameAddr(irn)) {
102 arm_SymConst_attr_t *attr = get_arm_SymConst_attr(irn);
103 attr->fp_offset += bias;
105 arm_load_store_attr_t *attr = get_arm_load_store_attr(irn);
106 assert(attr->base.is_load_store);
107 attr->offset += bias;
111 static int arm_get_sp_bias(const ir_node *irn)
113 /* We don't have any nodes changing the stack pointer.
114 We probably want to support post-/pre increment/decrement later */
119 /* fill register allocator interface */
121 static const arch_irn_ops_t arm_irn_ops = {
123 arm_get_frame_entity,
126 NULL, /* get_inverse */
127 NULL, /* get_op_estimated_cost */
128 NULL, /* possible_memory_operand */
129 NULL, /* perform_memory_operand */
133 * Transforms the standard Firm graph into
136 static void arm_prepare_graph(ir_graph *irg)
138 /* transform nodes into assembler instructions */
139 arm_transform_graph(irg);
141 /* do local optimizations (mainly CSE) */
142 local_optimize_graph(irg);
144 /* do code placement, to optimize the position of constants */
148 static void arm_collect_frame_entity_nodes(ir_node *node, void *data)
150 be_fec_env_t *env = (be_fec_env_t*)data;
154 const arm_load_store_attr_t *attr;
156 if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
157 mode = get_irn_mode(node);
158 align = get_mode_size_bytes(mode);
159 be_node_needs_frame_entity(env, node, mode, align);
163 switch (get_arm_irn_opcode(node)) {
171 attr = get_arm_load_store_attr_const(node);
172 entity = attr->entity;
173 mode = attr->load_store_mode;
174 align = get_mode_size_bytes(mode);
177 if (!attr->is_frame_entity)
179 be_node_needs_frame_entity(env, node, mode, align);
182 static void arm_set_frame_entity(ir_node *node, ir_entity *entity)
184 if (is_be_node(node)) {
185 be_node_set_frame_entity(node, entity);
187 arm_load_store_attr_t *attr = get_arm_load_store_attr(node);
188 attr->entity = entity;
192 static void transform_Reload(ir_node *node)
194 ir_node *block = get_nodes_block(node);
195 dbg_info *dbgi = get_irn_dbg_info(node);
196 ir_node *ptr = get_irn_n(node, n_be_Reload_frame);
197 ir_node *mem = get_irn_n(node, n_be_Reload_mem);
198 ir_mode *mode = get_irn_mode(node);
199 ir_entity *entity = be_get_frame_entity(node);
200 const arch_register_t *reg;
204 ir_node *sched_point = sched_prev(node);
206 load = new_bd_arm_Ldr(dbgi, block, ptr, mem, mode, entity, false, 0, true);
207 sched_add_after(sched_point, load);
210 proj = new_rd_Proj(dbgi, load, mode, pn_arm_Ldr_res);
212 reg = arch_get_irn_register(node);
213 arch_set_irn_register(proj, reg);
215 exchange(node, proj);
218 static void transform_Spill(ir_node *node)
220 ir_node *block = get_nodes_block(node);
221 dbg_info *dbgi = get_irn_dbg_info(node);
222 ir_node *ptr = get_irn_n(node, n_be_Spill_frame);
223 ir_graph *irg = get_irn_irg(node);
224 ir_node *mem = get_irg_no_mem(irg);
225 ir_node *val = get_irn_n(node, n_be_Spill_val);
226 ir_mode *mode = get_irn_mode(val);
227 ir_entity *entity = be_get_frame_entity(node);
228 ir_node *sched_point;
231 sched_point = sched_prev(node);
232 store = new_bd_arm_Str(dbgi, block, ptr, val, mem, mode, entity, false, 0,
236 sched_add_after(sched_point, store);
238 exchange(node, store);
241 static void arm_after_ra_walker(ir_node *block, void *data)
243 ir_node *node, *prev;
246 for (node = sched_last(block); !sched_is_begin(node); node = prev) {
247 prev = sched_prev(node);
249 if (be_is_Reload(node)) {
250 transform_Reload(node);
251 } else if (be_is_Spill(node)) {
252 transform_Spill(node);
258 * Called immediately before emit phase.
260 static void arm_finish_irg(ir_graph *irg)
262 be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
263 bool at_begin = stack_layout->sp_relative ? true : false;
264 be_fec_env_t *fec_env = be_new_frame_entity_coalescer(irg);
266 irg_walk_graph(irg, NULL, arm_collect_frame_entity_nodes, fec_env);
267 be_assign_entities(fec_env, arm_set_frame_entity, at_begin);
268 be_free_frame_entity_coalescer(fec_env);
270 irg_block_walk_graph(irg, NULL, arm_after_ra_walker, NULL);
272 /* fix stack entity offsets */
273 be_abi_fix_stack_nodes(irg);
274 be_abi_fix_stack_bias(irg);
276 /* do peephole optimizations and fix stack offsets */
277 arm_peephole_optimization(irg);
280 static void arm_before_ra(ir_graph *irg)
282 be_sched_fix_flags(irg, &arm_reg_classes[CLASS_arm_flags], NULL, NULL);
286 * Initializes the code generator.
288 static void arm_init_graph(ir_graph *irg)
295 * Maps all intrinsic calls that the backend support
296 * and map all instructions the backend did not support
299 static void arm_handle_intrinsics(void)
301 ir_type *tp, *int_tp, *uint_tp;
305 runtime_rt rt_iDiv, rt_uDiv, rt_iMod, rt_uMod;
307 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
309 int_tp = get_type_for_mode(mode_Is);
310 uint_tp = get_type_for_mode(mode_Iu);
312 /* ARM has neither a signed div instruction ... */
314 i_instr_record *map_Div = &records[n_records++].i_instr;
316 tp = new_type_method(2, 1);
317 set_method_param_type(tp, 0, int_tp);
318 set_method_param_type(tp, 1, int_tp);
319 set_method_res_type(tp, 0, int_tp);
321 rt_iDiv.ent = new_entity(get_glob_type(), ID("__divsi3"), tp);
322 set_entity_ld_ident(rt_iDiv.ent, ID("__divsi3"));
323 rt_iDiv.mode = mode_T;
324 rt_iDiv.res_mode = mode_Is;
325 rt_iDiv.mem_proj_nr = pn_Div_M;
326 rt_iDiv.regular_proj_nr = pn_Div_X_regular;
327 rt_iDiv.exc_proj_nr = pn_Div_X_except;
328 rt_iDiv.res_proj_nr = pn_Div_res;
330 add_entity_linkage(rt_iDiv.ent, IR_LINKAGE_CONSTANT);
331 set_entity_visibility(rt_iDiv.ent, ir_visibility_external);
333 map_Div->kind = INTRINSIC_INSTR;
334 map_Div->op = op_Div;
335 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
336 map_Div->ctx = &rt_iDiv;
338 /* ... nor an unsigned div instruction ... */
340 i_instr_record *map_Div = &records[n_records++].i_instr;
342 tp = new_type_method(2, 1);
343 set_method_param_type(tp, 0, uint_tp);
344 set_method_param_type(tp, 1, uint_tp);
345 set_method_res_type(tp, 0, uint_tp);
347 rt_uDiv.ent = new_entity(get_glob_type(), ID("__udivsi3"), tp);
348 set_entity_ld_ident(rt_uDiv.ent, ID("__udivsi3"));
349 rt_uDiv.mode = mode_T;
350 rt_uDiv.res_mode = mode_Iu;
351 rt_uDiv.mem_proj_nr = pn_Div_M;
352 rt_uDiv.regular_proj_nr = pn_Div_X_regular;
353 rt_uDiv.exc_proj_nr = pn_Div_X_except;
354 rt_uDiv.res_proj_nr = pn_Div_res;
356 set_entity_visibility(rt_uDiv.ent, ir_visibility_external);
358 map_Div->kind = INTRINSIC_INSTR;
359 map_Div->op = op_Div;
360 map_Div->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
361 map_Div->ctx = &rt_uDiv;
363 /* ... nor a signed mod instruction ... */
365 i_instr_record *map_Mod = &records[n_records++].i_instr;
367 tp = new_type_method(2, 1);
368 set_method_param_type(tp, 0, int_tp);
369 set_method_param_type(tp, 1, int_tp);
370 set_method_res_type(tp, 0, int_tp);
372 rt_iMod.ent = new_entity(get_glob_type(), ID("__modsi3"), tp);
373 set_entity_ld_ident(rt_iMod.ent, ID("__modsi3"));
374 rt_iMod.mode = mode_T;
375 rt_iMod.res_mode = mode_Is;
376 rt_iMod.mem_proj_nr = pn_Mod_M;
377 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
378 rt_iMod.exc_proj_nr = pn_Mod_X_except;
379 rt_iMod.res_proj_nr = pn_Mod_res;
381 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
383 map_Mod->kind = INTRINSIC_INSTR;
384 map_Mod->op = op_Mod;
385 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
386 map_Mod->ctx = &rt_iMod;
388 /* ... nor an unsigned mod. */
390 i_instr_record *map_Mod = &records[n_records++].i_instr;
392 tp = new_type_method(2, 1);
393 set_method_param_type(tp, 0, uint_tp);
394 set_method_param_type(tp, 1, uint_tp);
395 set_method_res_type(tp, 0, uint_tp);
397 rt_uMod.ent = new_entity(get_glob_type(), ID("__umodsi3"), tp);
398 set_entity_ld_ident(rt_uMod.ent, ID("__umodsi3"));
399 rt_uMod.mode = mode_T;
400 rt_uMod.res_mode = mode_Iu;
401 rt_uMod.mem_proj_nr = pn_Mod_M;
402 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
403 rt_uMod.exc_proj_nr = pn_Mod_X_except;
404 rt_uMod.res_proj_nr = pn_Mod_res;
406 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
408 map_Mod->kind = INTRINSIC_INSTR;
409 map_Mod->op = op_Mod;
410 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
411 map_Mod->ctx = &rt_uMod;
415 lower_intrinsics(records, n_records, /*part_block_used=*/0);
418 extern const arch_isa_if_t arm_isa_if;
419 static arm_isa_t arm_isa_template = {
421 &arm_isa_if, /* isa interface */
426 &arm_registers[REG_SP], /* stack pointer */
427 &arm_registers[REG_R11], /* base pointer */
428 &arm_reg_classes[CLASS_arm_gp], /* static link pointer class */
429 2, /* power of two stack alignment for calls, 2^2 == 4 */
430 NULL, /* main environment */
432 5, /* reload costs */
433 true, /* we do have custom abi handling */
435 ARM_FPU_ARCH_FPE, /* FPU architecture */
439 * Initializes the backend ISA and opens the output file.
441 static arch_env_t *arm_init(const be_main_env_t *env)
443 arm_isa_t *isa = XMALLOC(arm_isa_t);
444 *isa = arm_isa_template;
448 arm_create_opcodes(&arm_irn_ops);
449 arm_handle_intrinsics();
451 be_gas_emit_types = false;
453 be_emit_init(env->file_handle);
454 be_gas_begin_compilation_unit(env);
462 * Closes the output file and frees the ISA structure.
464 static void arm_done(void *self)
466 arm_isa_t *isa = (arm_isa_t*)self;
468 be_gas_end_compilation_unit(isa->base.main_env);
475 * Get the register class which shall be used to store a value of a given mode.
476 * @param self The this pointer.
477 * @param mode The mode in question.
478 * @return A register class which can hold values of the given mode.
480 static const arch_register_class_t *arm_get_reg_class_for_mode(const ir_mode *mode)
482 if (mode_is_float(mode))
483 return &arm_reg_classes[CLASS_arm_fpa];
485 return &arm_reg_classes[CLASS_arm_gp];
489 * Returns the necessary byte alignment for storing a register of given class.
491 static int arm_get_reg_class_alignment(const arch_register_class_t *cls)
494 /* ARM is a 32 bit CPU, no need for other alignment */
499 * Return irp irgs in the desired order.
501 static ir_graph **arm_get_irg_list(const void *self, ir_graph ***irg_list)
509 * Allows or disallows the creation of Psi nodes for the given Phi nodes.
510 * @return 1 if allowed, 0 otherwise
512 static int arm_is_mux_allowed(ir_node *sel, ir_node *mux_false,
521 static asm_constraint_flags_t arm_parse_asm_constraint(const char **c)
523 /* asm not supported */
525 return ASM_CONSTRAINT_FLAG_INVALID;
528 static int arm_is_valid_clobber(const char *clobber)
534 static void arm_lower_for_target(void)
536 size_t i, n_irgs = get_irp_n_irgs();
538 /* lower compound param handling */
539 lower_calls_with_compounds(LF_RETURN_HIDDEN);
541 for (i = 0; i < n_irgs; ++i) {
542 ir_graph *irg = get_irp_irg(i);
543 lower_switch(irg, 4, 256, false);
546 for (i = 0; i < n_irgs; ++i) {
547 ir_graph *irg = get_irp_irg(i);
548 /* Turn all small CopyBs into loads/stores and all bigger CopyBs into
550 * TODO: These constants need arm-specific tuning. */
551 lower_CopyB(irg, 31, 32, false);
556 * Returns the libFirm configuration parameter for this backend.
558 static const backend_params *arm_get_libfirm_params(void)
560 static ir_settings_arch_dep_t ad = {
562 1, /* Muls are fast enough on ARM but ... */
563 31, /* ... one shift would be possible better */
564 NULL, /* no evaluator function */
565 0, /* SMUL is needed, only in Arch M */
566 0, /* UMUL is needed, only in Arch M */
567 32, /* SMUL & UMUL available for 32 bit */
569 static backend_params p = {
570 0, /* don't support inline assembler yet */
571 1, /* support Rotl nodes */
573 1, /* modulo shift efficient */
574 0, /* non-modulo shift not efficient */
575 &ad, /* will be set later */
576 arm_is_mux_allowed, /* allow_ifconv function */
577 32, /* machine size */
578 NULL, /* float arithmetic mode (TODO) */
579 NULL, /* long long type */
580 NULL, /* unsigned long long type */
581 NULL, /* long double type */
582 0, /* no trampoline support: size 0 */
583 0, /* no trampoline support: align 0 */
584 NULL, /* no trampoline support: no trampoline builder */
585 4 /* alignment of stack parameter */
591 /* fpu set architectures. */
592 static const lc_opt_enum_int_items_t arm_fpu_items[] = {
593 { "softfloat", ARM_FPU_ARCH_SOFTFLOAT },
594 { "fpe", ARM_FPU_ARCH_FPE },
595 { "fpa", ARM_FPU_ARCH_FPA },
596 { "vfp1xd", ARM_FPU_ARCH_VFP_V1xD },
597 { "vfp1", ARM_FPU_ARCH_VFP_V1 },
598 { "vfp2", ARM_FPU_ARCH_VFP_V2 },
602 static lc_opt_enum_int_var_t arch_fpu_var = {
603 &arm_isa_template.fpu_arch, arm_fpu_items
606 static const lc_opt_table_entry_t arm_options[] = {
607 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &arch_fpu_var),
611 const arch_isa_if_t arm_isa_if = {
613 arm_lower_for_target,
615 NULL, /* handle_intrinsics */
616 arm_get_reg_class_for_mode,
618 arm_get_reg_class_alignment,
619 arm_get_libfirm_params,
621 NULL, /* mark remat */
622 arm_parse_asm_constraint,
623 arm_is_valid_clobber,
626 NULL, /* get_pic_base */
627 NULL, /* before_abi */
632 NULL, /* register_saved_by */
637 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_arm)
638 void be_init_arch_arm(void)
640 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
641 lc_opt_entry_t *arm_grp = lc_opt_get_grp(be_grp, "arm");
643 lc_opt_add_table(arm_grp, arm_options);
645 be_register_isa_if("arm", &arm_isa_if);
647 arm_init_transform();