2 * Copyright (C) 1995-2010 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 sparc backend driver file.
23 * @author Hannes Rapp, Matthias Braun
28 #include "lc_opts_enum.h"
36 #include "iroptimize.h"
42 #include "lower_alloc.h"
43 #include "lower_builtins.h"
44 #include "lower_calls.h"
45 #include "lower_mode_b.h"
46 #include "lower_softfloat.h"
59 #include "bemachine.h"
63 #include "belistsched.h"
67 #include "bearch_sparc_t.h"
69 #include "sparc_new_nodes.h"
70 #include "gen_sparc_regalloc_if.h"
71 #include "sparc_transform.h"
72 #include "sparc_emitter.h"
73 #include "sparc_cconv.h"
75 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
77 static arch_irn_class_t sparc_classify(const ir_node *node)
80 return arch_irn_class_none;
83 static ir_entity *sparc_get_frame_entity(const ir_node *node)
85 if (is_sparc_FrameAddr(node)) {
86 const sparc_attr_t *attr = get_sparc_attr_const(node);
87 return attr->immediate_value_entity;
90 if (sparc_has_load_store_attr(node)) {
91 const sparc_load_store_attr_t *load_store_attr
92 = get_sparc_load_store_attr_const(node);
93 if (load_store_attr->is_frame_entity) {
94 return load_store_attr->base.immediate_value_entity;
102 * This function is called by the generic backend to correct offsets for
103 * nodes accessing the stack.
105 static void sparc_set_frame_offset(ir_node *node, int offset)
107 sparc_attr_t *attr = get_sparc_attr(node);
108 attr->immediate_value += offset;
110 /* must be a FrameAddr or a load/store node with frame_entity */
111 assert(is_sparc_FrameAddr(node) ||
112 get_sparc_load_store_attr_const(node)->is_frame_entity);
115 static int sparc_get_sp_bias(const ir_node *node)
117 if (is_sparc_Save(node)) {
118 const sparc_attr_t *attr = get_sparc_attr_const(node);
119 if (get_irn_arity(node) == 3)
120 panic("no support for _reg variant yet");
122 return -attr->immediate_value;
123 } else if (is_sparc_RestoreZero(node)) {
124 return SP_BIAS_RESET;
129 /* fill register allocator interface */
131 const arch_irn_ops_t sparc_irn_ops = {
133 sparc_get_frame_entity,
134 sparc_set_frame_offset,
136 NULL, /* get_inverse */
137 NULL, /* get_op_estimated_cost */
138 NULL, /* possible_memory_operand */
139 NULL, /* perform_memory_operand */
143 * Transforms the standard firm graph into
146 static void sparc_prepare_graph(ir_graph *irg)
148 sparc_transform_graph(irg);
151 static bool sparc_modifies_flags(const ir_node *node)
153 return arch_get_irn_flags(node) & sparc_arch_irn_flag_modifies_flags;
156 static bool sparc_modifies_fp_flags(const ir_node *node)
158 return arch_get_irn_flags(node) & sparc_arch_irn_flag_modifies_fp_flags;
161 static void sparc_before_ra(ir_graph *irg)
163 /* fixup flags register */
164 be_sched_fix_flags(irg, &sparc_reg_classes[CLASS_sparc_flags_class],
165 NULL, sparc_modifies_flags);
166 be_sched_fix_flags(irg, &sparc_reg_classes[CLASS_sparc_fpflags_class],
167 NULL, sparc_modifies_fp_flags);
170 static void sparc_init_graph(ir_graph *irg)
175 extern const arch_isa_if_t sparc_isa_if;
176 static sparc_isa_t sparc_isa_template = {
178 &sparc_isa_if, /* isa interface implementation */
183 &sparc_registers[REG_SP], /* stack pointer register */
184 &sparc_registers[REG_FRAME_POINTER],/* base pointer register */
185 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
186 3, /* power of two stack alignment
188 NULL, /* main environment */
189 7, /* costs for a spill instruction */
190 5, /* costs for a reload instruction */
191 true, /* custom abi handling */
193 NULL, /* constants */
194 SPARC_FPU_ARCH_FPU, /* FPU architecture */
198 * rewrite unsigned->float conversion.
199 * Sparc has no instruction for this so instead we do the following:
201 * int signed_x = unsigned_value_x;
202 * double res = signed_x;
204 * res += 4294967296. ;
205 * return (float) res;
207 static void rewrite_unsigned_float_Conv(ir_node *node)
209 ir_graph *irg = get_irn_irg(node);
210 dbg_info *dbgi = get_irn_dbg_info(node);
211 ir_node *lower_block = get_nodes_block(node);
216 ir_node *block = get_nodes_block(node);
217 ir_node *unsigned_x = get_Conv_op(node);
218 ir_mode *mode_u = get_irn_mode(unsigned_x);
219 ir_mode *mode_s = find_signed_mode(mode_u);
220 ir_mode *mode_d = mode_D;
221 ir_node *signed_x = new_rd_Conv(dbgi, block, unsigned_x, mode_s);
222 ir_node *res = new_rd_Conv(dbgi, block, signed_x, mode_d);
223 ir_node *zero = new_r_Const(irg, get_mode_null(mode_s));
224 ir_node *cmp = new_rd_Cmp(dbgi, block, signed_x, zero,
226 ir_node *cond = new_rd_Cond(dbgi, block, cmp);
227 ir_node *proj_true = new_r_Proj(cond, mode_X, pn_Cond_true);
228 ir_node *proj_false = new_r_Proj(cond, mode_X, pn_Cond_false);
229 ir_node *in_true[1] = { proj_true };
230 ir_node *in_false[1] = { proj_false };
231 ir_node *true_block = new_r_Block(irg, ARRAY_SIZE(in_true), in_true);
232 ir_node *false_block = new_r_Block(irg, ARRAY_SIZE(in_false),in_false);
233 ir_node *true_jmp = new_r_Jmp(true_block);
234 ir_node *false_jmp = new_r_Jmp(false_block);
235 ir_tarval *correction = new_tarval_from_double(4294967296., mode_d);
236 ir_node *c_const = new_r_Const(irg, correction);
237 ir_node *fadd = new_rd_Add(dbgi, true_block, res, c_const,
240 ir_node *lower_in[2] = { true_jmp, false_jmp };
241 ir_node *phi_in[2] = { fadd, res };
242 ir_mode *dest_mode = get_irn_mode(node);
246 set_irn_in(lower_block, ARRAY_SIZE(lower_in), lower_in);
247 phi = new_r_Phi(lower_block, ARRAY_SIZE(phi_in), phi_in, mode_d);
248 assert(get_Block_phis(lower_block) == NULL);
249 set_Block_phis(lower_block, phi);
250 set_Phi_next(phi, NULL);
252 res_conv = new_rd_Conv(dbgi, lower_block, phi, dest_mode);
254 exchange(node, res_conv);
258 static int sparc_rewrite_Conv(ir_node *node, void *ctx)
260 ir_mode *to_mode = get_irn_mode(node);
261 ir_node *op = get_Conv_op(node);
262 ir_mode *from_mode = get_irn_mode(op);
265 if (mode_is_float(to_mode) && mode_is_int(from_mode)
266 && get_mode_size_bits(from_mode) == 32
267 && !mode_is_signed(from_mode)) {
268 rewrite_unsigned_float_Conv(node);
275 static void sparc_handle_intrinsics(void)
277 ir_type *tp, *int_tp, *uint_tp;
279 size_t n_records = 0;
281 runtime_rt rt_iMod, rt_uMod;
283 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
285 int_tp = new_type_primitive(mode_Is);
286 uint_tp = new_type_primitive(mode_Iu);
288 /* we need to rewrite some forms of int->float conversions */
290 i_instr_record *map_Conv = &records[n_records++].i_instr;
292 map_Conv->kind = INTRINSIC_INSTR;
293 map_Conv->op = op_Conv;
294 map_Conv->i_mapper = sparc_rewrite_Conv;
296 /* SPARC has no signed mod instruction ... */
298 i_instr_record *map_Mod = &records[n_records++].i_instr;
300 tp = new_type_method(2, 1);
301 set_method_param_type(tp, 0, int_tp);
302 set_method_param_type(tp, 1, int_tp);
303 set_method_res_type(tp, 0, int_tp);
305 rt_iMod.ent = new_entity(get_glob_type(), ID(".rem"), tp);
306 set_entity_ld_ident(rt_iMod.ent, ID(".rem"));
307 rt_iMod.mode = mode_T;
308 rt_iMod.res_mode = mode_Is;
309 rt_iMod.mem_proj_nr = pn_Mod_M;
310 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
311 rt_iMod.exc_proj_nr = pn_Mod_X_except;
312 rt_iMod.res_proj_nr = pn_Mod_res;
314 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
316 map_Mod->kind = INTRINSIC_INSTR;
317 map_Mod->op = op_Mod;
318 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
319 map_Mod->ctx = &rt_iMod;
321 /* ... nor an unsigned mod. */
323 i_instr_record *map_Mod = &records[n_records++].i_instr;
325 tp = new_type_method(2, 1);
326 set_method_param_type(tp, 0, uint_tp);
327 set_method_param_type(tp, 1, uint_tp);
328 set_method_res_type(tp, 0, uint_tp);
330 rt_uMod.ent = new_entity(get_glob_type(), ID(".urem"), tp);
331 set_entity_ld_ident(rt_uMod.ent, ID(".urem"));
332 rt_uMod.mode = mode_T;
333 rt_uMod.res_mode = mode_Iu;
334 rt_uMod.mem_proj_nr = pn_Mod_M;
335 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
336 rt_uMod.exc_proj_nr = pn_Mod_X_except;
337 rt_uMod.res_proj_nr = pn_Mod_res;
339 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
341 map_Mod->kind = INTRINSIC_INSTR;
342 map_Mod->op = op_Mod;
343 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
344 map_Mod->ctx = &rt_uMod;
347 assert(n_records < ARRAY_SIZE(records));
348 lower_intrinsics(records, n_records, /*part_block_used=*/ true);
352 * Initializes the backend ISA
354 static arch_env_t *sparc_init(const be_main_env_t *env)
356 sparc_isa_t *isa = XMALLOC(sparc_isa_t);
357 *isa = sparc_isa_template;
358 isa->constants = pmap_create();
360 be_gas_elf_type_char = '#';
361 be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF;
362 be_gas_elf_variant = ELF_VARIANT_SPARC;
364 sparc_register_init();
365 sparc_create_opcodes(&sparc_irn_ops);
366 sparc_handle_intrinsics();
369 be_emit_init(env->file_handle);
370 be_gas_begin_compilation_unit(env);
376 * Closes the output file and frees the ISA structure.
378 static void sparc_done(void *self)
380 sparc_isa_t *isa = (sparc_isa_t*)self;
382 /* emit now all global declarations */
383 be_gas_end_compilation_unit(isa->base.main_env);
385 pmap_destroy(isa->constants);
392 * Get the register class which shall be used to store a value of a given mode.
393 * @param self The this pointer.
394 * @param mode The mode in question.
395 * @return A register class which can hold values of the given mode.
397 static const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
399 if (mode_is_float(mode))
400 return &sparc_reg_classes[CLASS_sparc_fp];
402 return &sparc_reg_classes[CLASS_sparc_gp];
406 * Returns the necessary byte alignment for storing a register of given class.
408 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
410 ir_mode *mode = arch_register_class_mode(cls);
411 return get_mode_size_bytes(mode);
414 static void sparc_lower_for_target(void)
416 size_t i, n_irgs = get_irp_n_irgs();
418 lower_calls_with_compounds(LF_RETURN_HIDDEN);
420 for (i = 0; i < n_irgs; ++i) {
421 ir_graph *irg = get_irp_irg(i);
422 /* Turn all small CopyBs into loads/stores and all bigger CopyBs into
424 lower_CopyB(irg, 31, 32, false);
427 if (sparc_isa_template.fpu_arch == SPARC_FPU_ARCH_SOFTFLOAT)
428 lower_floating_point();
430 lower_builtins(0, NULL);
434 for (i = 0; i < n_irgs; ++i) {
435 ir_graph *irg = get_irp_irg(i);
436 ir_lower_mode_b(irg, mode_Iu);
437 lower_switch(irg, 4, 256, false);
438 lower_alloc(irg, SPARC_STACK_ALIGNMENT, false, SPARC_MIN_STACKSIZE);
442 static int sparc_is_mux_allowed(ir_node *sel, ir_node *mux_false,
445 return ir_is_optimizable_mux(sel, mux_false, mux_true);
449 * Returns the libFirm configuration parameter for this backend.
451 static const backend_params *sparc_get_backend_params(void)
453 static const ir_settings_arch_dep_t arch_dep = {
454 1, /* also_use_subs */
455 1, /* maximum_shifts */
456 31, /* highest_shift_amount */
457 NULL, /* evaluate_cost_func */
460 32, /* max_bits_for_mulh */
462 static backend_params p = {
463 0, /* no inline assembly */
464 0, /* no support for RotL nodes */
466 1, /* modulo shift efficient */
467 0, /* non-modulo shift not efficient */
468 &arch_dep, /* will be set later */
469 sparc_is_mux_allowed, /* parameter for if conversion */
470 32, /* machine size */
471 NULL, /* float arithmetic mode */
472 NULL, /* long long type */
473 NULL, /* usigned long long type */
474 NULL, /* long double type */
475 0, /* no trampoline support: size 0 */
476 0, /* no trampoline support: align 0 */
477 NULL, /* no trampoline support: no trampoline builder */
478 4 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
481 ir_mode *mode_long_long
482 = new_int_mode("long long", irma_twos_complement, 64, 1, 64);
483 ir_type *type_long_long = new_type_primitive(mode_long_long);
484 ir_mode *mode_unsigned_long_long
485 = new_int_mode("unsigned long long", irma_twos_complement, 64, 0, 64);
486 ir_type *type_unsigned_long_long
487 = new_type_primitive(mode_unsigned_long_long);
489 p.type_long_long = type_long_long;
490 p.type_unsigned_long_long = type_unsigned_long_long;
492 if (sparc_isa_template.fpu_arch == SPARC_FPU_ARCH_SOFTFLOAT) {
493 p.mode_float_arithmetic = NULL;
494 p.type_long_double = NULL;
496 ir_type *type_long_double = new_type_primitive(mode_Q);
498 set_type_alignment_bytes(type_long_double, 8);
499 set_type_size_bytes(type_long_double, 16);
500 p.type_long_double = type_long_double;
505 static ir_graph **sparc_get_backend_irg_list(const void *self,
513 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
516 return ASM_CONSTRAINT_FLAG_INVALID;
519 static int sparc_is_valid_clobber(const char *clobber)
525 /* fpu set architectures. */
526 static const lc_opt_enum_int_items_t sparc_fpu_items[] = {
527 { "fpu", SPARC_FPU_ARCH_FPU },
528 { "softfloat", SPARC_FPU_ARCH_SOFTFLOAT },
532 static lc_opt_enum_int_var_t arch_fpu_var = {
533 &sparc_isa_template.fpu_arch, sparc_fpu_items
536 static const lc_opt_table_entry_t sparc_options[] = {
537 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &arch_fpu_var),
541 static ir_node *sparc_new_spill(ir_node *value, ir_node *after)
543 ir_node *block = get_block(after);
544 ir_graph *irg = get_irn_irg(value);
545 ir_node *frame = get_irg_frame(irg);
546 ir_node *mem = get_irg_no_mem(irg);
547 ir_mode *mode = get_irn_mode(value);
550 if (mode_is_float(mode)) {
551 store = create_stf(NULL, block, value, frame, mem, mode, NULL, 0, true);
553 store = new_bd_sparc_St_imm(NULL, block, value, frame, mem, mode, NULL,
556 sched_add_after(after, store);
560 static ir_node *sparc_new_reload(ir_node *value, ir_node *spill,
563 ir_node *block = get_block(before);
564 ir_graph *irg = get_irn_irg(value);
565 ir_node *frame = get_irg_frame(irg);
566 ir_mode *mode = get_irn_mode(value);
570 if (mode_is_float(mode)) {
571 load = create_ldf(NULL, block, frame, spill, mode, NULL, 0, true);
573 load = new_bd_sparc_Ld_imm(NULL, block, frame, spill, mode, NULL, 0,
576 sched_add_before(before, load);
577 assert((long)pn_sparc_Ld_res == (long)pn_sparc_Ldf_res);
578 res = new_r_Proj(load, mode, pn_sparc_Ld_res);
583 const arch_isa_if_t sparc_isa_if = {
585 sparc_lower_for_target,
587 NULL, /* handle intrinsics */
588 sparc_get_reg_class_for_mode,
590 sparc_get_reg_class_alignment,
591 sparc_get_backend_params,
592 sparc_get_backend_irg_list,
593 NULL, /* mark remat */
594 sparc_parse_asm_constraint,
595 sparc_is_valid_clobber,
598 NULL, /* get_pic_base */
599 NULL, /* before_abi */
604 NULL, /* register_saved_by */
609 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc)
610 void be_init_arch_sparc(void)
612 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
613 lc_opt_entry_t *sparc_grp = lc_opt_get_grp(be_grp, "sparc");
615 lc_opt_add_table(sparc_grp, sparc_options);
617 be_register_isa_if("sparc", &sparc_isa_if);
618 FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
619 sparc_init_transform();
620 sparc_init_emitter();