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
29 #include "lc_opts_enum.h"
37 #include "iroptimize.h"
42 #include "lower_calls.h"
43 #include "lower_softfloat.h"
51 #include "../bearch.h"
52 #include "../benode.h"
53 #include "../belower.h"
54 #include "../besched.h"
56 #include "../bemachine.h"
57 #include "../bemodule.h"
59 #include "../begnuas.h"
60 #include "../belistsched.h"
61 #include "../beflags.h"
63 #include "bearch_sparc_t.h"
65 #include "sparc_new_nodes.h"
66 #include "gen_sparc_regalloc_if.h"
67 #include "sparc_transform.h"
68 #include "sparc_emitter.h"
69 #include "sparc_cconv.h"
71 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
73 static arch_irn_class_t sparc_classify(const ir_node *node)
76 return arch_irn_class_none;
79 static ir_entity *sparc_get_frame_entity(const ir_node *node)
81 if (is_sparc_FrameAddr(node)) {
82 const sparc_attr_t *attr = get_sparc_attr_const(node);
83 return attr->immediate_value_entity;
86 if (sparc_has_load_store_attr(node)) {
87 const sparc_load_store_attr_t *load_store_attr
88 = get_sparc_load_store_attr_const(node);
89 if (load_store_attr->is_frame_entity) {
90 return load_store_attr->base.immediate_value_entity;
98 * This function is called by the generic backend to correct offsets for
99 * nodes accessing the stack.
101 static void sparc_set_frame_offset(ir_node *node, int offset)
103 sparc_attr_t *attr = get_sparc_attr(node);
104 attr->immediate_value += offset;
106 /* must be a FrameAddr or a load/store node with frame_entity */
107 assert(is_sparc_FrameAddr(node) ||
108 get_sparc_load_store_attr_const(node)->is_frame_entity);
111 static int sparc_get_sp_bias(const ir_node *node)
113 if (is_sparc_Save(node)) {
114 const sparc_attr_t *attr = get_sparc_attr_const(node);
115 if (get_irn_arity(node) == 3)
116 panic("no support for _reg variant yet");
118 return -attr->immediate_value;
119 } else if (is_sparc_RestoreZero(node)) {
120 return SP_BIAS_RESET;
125 /* fill register allocator interface */
127 const arch_irn_ops_t sparc_irn_ops = {
129 sparc_get_frame_entity,
130 sparc_set_frame_offset,
132 NULL, /* get_inverse */
133 NULL, /* get_op_estimated_cost */
134 NULL, /* possible_memory_operand */
135 NULL, /* perform_memory_operand */
139 * Transforms the standard firm graph into
142 static void sparc_prepare_graph(ir_graph *irg)
144 sparc_transform_graph(irg);
147 static bool sparc_modifies_flags(const ir_node *node)
149 return arch_get_irn_flags(node) & sparc_arch_irn_flag_modifies_flags;
152 static bool sparc_modifies_fp_flags(const ir_node *node)
154 return arch_get_irn_flags(node) & sparc_arch_irn_flag_modifies_fp_flags;
157 static void sparc_before_ra(ir_graph *irg)
159 /* fixup flags register */
160 be_sched_fix_flags(irg, &sparc_reg_classes[CLASS_sparc_flags_class],
161 NULL, sparc_modifies_flags);
162 be_sched_fix_flags(irg, &sparc_reg_classes[CLASS_sparc_fpflags_class],
163 NULL, sparc_modifies_fp_flags);
166 static void sparc_init_graph(ir_graph *irg)
171 extern const arch_isa_if_t sparc_isa_if;
172 static sparc_isa_t sparc_isa_template = {
174 &sparc_isa_if, /* isa interface implementation */
179 &sparc_registers[REG_SP], /* stack pointer register */
180 &sparc_registers[REG_FRAME_POINTER],/* base pointer register */
181 &sparc_reg_classes[CLASS_sparc_gp], /* link pointer register class */
182 3, /* power of two stack alignment
184 NULL, /* main environment */
185 7, /* costs for a spill instruction */
186 5, /* costs for a reload instruction */
187 true, /* custom abi handling */
189 NULL, /* constants */
190 SPARC_FPU_ARCH_FPU, /* FPU architecture */
194 * rewrite unsigned->float conversion.
195 * Sparc has no instruction for this so instead we do the following:
197 * int signed_x = unsigned_value_x;
198 * double res = signed_x;
200 * res += 4294967296. ;
201 * return (float) res;
203 static void rewrite_unsigned_float_Conv(ir_node *node)
205 ir_graph *irg = get_irn_irg(node);
206 dbg_info *dbgi = get_irn_dbg_info(node);
207 ir_node *lower_block = get_nodes_block(node);
212 ir_node *block = get_nodes_block(node);
213 ir_node *unsigned_x = get_Conv_op(node);
214 ir_mode *mode_u = get_irn_mode(unsigned_x);
215 ir_mode *mode_s = find_signed_mode(mode_u);
216 ir_mode *mode_d = mode_D;
217 ir_node *signed_x = new_rd_Conv(dbgi, block, unsigned_x, mode_s);
218 ir_node *res = new_rd_Conv(dbgi, block, signed_x, mode_d);
219 ir_node *zero = new_r_Const(irg, get_mode_null(mode_s));
220 ir_node *cmp = new_rd_Cmp(dbgi, block, signed_x, zero,
222 ir_node *cond = new_rd_Cond(dbgi, block, cmp);
223 ir_node *proj_true = new_r_Proj(cond, mode_X, pn_Cond_true);
224 ir_node *proj_false = new_r_Proj(cond, mode_X, pn_Cond_false);
225 ir_node *in_true[1] = { proj_true };
226 ir_node *in_false[1] = { proj_false };
227 ir_node *true_block = new_r_Block(irg, ARRAY_SIZE(in_true), in_true);
228 ir_node *false_block = new_r_Block(irg, ARRAY_SIZE(in_false),in_false);
229 ir_node *true_jmp = new_r_Jmp(true_block);
230 ir_node *false_jmp = new_r_Jmp(false_block);
231 ir_tarval *correction = new_tarval_from_double(4294967296., mode_d);
232 ir_node *c_const = new_r_Const(irg, correction);
233 ir_node *fadd = new_rd_Add(dbgi, true_block, res, c_const,
236 ir_node *lower_in[2] = { true_jmp, false_jmp };
237 ir_node *phi_in[2] = { fadd, res };
238 ir_mode *dest_mode = get_irn_mode(node);
242 set_irn_in(lower_block, ARRAY_SIZE(lower_in), lower_in);
243 phi = new_r_Phi(lower_block, ARRAY_SIZE(phi_in), phi_in, mode_d);
244 assert(get_Block_phis(lower_block) == NULL);
245 set_Block_phis(lower_block, phi);
246 set_Phi_next(phi, NULL);
248 res_conv = new_rd_Conv(dbgi, lower_block, phi, dest_mode);
250 exchange(node, res_conv);
254 static int sparc_rewrite_Conv(ir_node *node, void *ctx)
256 ir_mode *to_mode = get_irn_mode(node);
257 ir_node *op = get_Conv_op(node);
258 ir_mode *from_mode = get_irn_mode(op);
261 if (mode_is_float(to_mode) && mode_is_int(from_mode)
262 && get_mode_size_bits(from_mode) == 32
263 && !mode_is_signed(from_mode)) {
264 rewrite_unsigned_float_Conv(node);
271 static void sparc_handle_intrinsics(void)
273 ir_type *tp, *int_tp, *uint_tp;
275 size_t n_records = 0;
277 runtime_rt rt_iMod, rt_uMod;
279 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
281 int_tp = new_type_primitive(mode_Is);
282 uint_tp = new_type_primitive(mode_Iu);
284 /* we need to rewrite some forms of int->float conversions */
286 i_instr_record *map_Conv = &records[n_records++].i_instr;
288 map_Conv->kind = INTRINSIC_INSTR;
289 map_Conv->op = op_Conv;
290 map_Conv->i_mapper = sparc_rewrite_Conv;
292 /* SPARC has no signed mod instruction ... */
294 i_instr_record *map_Mod = &records[n_records++].i_instr;
296 tp = new_type_method(2, 1);
297 set_method_param_type(tp, 0, int_tp);
298 set_method_param_type(tp, 1, int_tp);
299 set_method_res_type(tp, 0, int_tp);
301 rt_iMod.ent = new_entity(get_glob_type(), ID(".rem"), tp);
302 set_entity_ld_ident(rt_iMod.ent, ID(".rem"));
303 rt_iMod.mode = mode_T;
304 rt_iMod.res_mode = mode_Is;
305 rt_iMod.mem_proj_nr = pn_Mod_M;
306 rt_iMod.regular_proj_nr = pn_Mod_X_regular;
307 rt_iMod.exc_proj_nr = pn_Mod_X_except;
308 rt_iMod.res_proj_nr = pn_Mod_res;
310 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
312 map_Mod->kind = INTRINSIC_INSTR;
313 map_Mod->op = op_Mod;
314 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
315 map_Mod->ctx = &rt_iMod;
317 /* ... nor an unsigned mod. */
319 i_instr_record *map_Mod = &records[n_records++].i_instr;
321 tp = new_type_method(2, 1);
322 set_method_param_type(tp, 0, uint_tp);
323 set_method_param_type(tp, 1, uint_tp);
324 set_method_res_type(tp, 0, uint_tp);
326 rt_uMod.ent = new_entity(get_glob_type(), ID(".urem"), tp);
327 set_entity_ld_ident(rt_uMod.ent, ID(".urem"));
328 rt_uMod.mode = mode_T;
329 rt_uMod.res_mode = mode_Iu;
330 rt_uMod.mem_proj_nr = pn_Mod_M;
331 rt_uMod.regular_proj_nr = pn_Mod_X_regular;
332 rt_uMod.exc_proj_nr = pn_Mod_X_except;
333 rt_uMod.res_proj_nr = pn_Mod_res;
335 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
337 map_Mod->kind = INTRINSIC_INSTR;
338 map_Mod->op = op_Mod;
339 map_Mod->i_mapper = (i_mapper_func)i_mapper_RuntimeCall;
340 map_Mod->ctx = &rt_uMod;
343 assert(n_records < ARRAY_SIZE(records));
344 lower_intrinsics(records, n_records, /*part_block_used=*/ true);
348 * Initializes the backend ISA
350 static arch_env_t *sparc_init(FILE *outfile)
352 sparc_isa_t *isa = XMALLOC(sparc_isa_t);
353 *isa = sparc_isa_template;
354 isa->constants = pmap_create();
356 be_emit_init(outfile);
358 sparc_register_init();
359 sparc_create_opcodes(&sparc_irn_ops);
360 sparc_handle_intrinsics();
367 * Closes the output file and frees the ISA structure.
369 static void sparc_done(void *self)
371 sparc_isa_t *isa = (sparc_isa_t*)self;
373 /* emit now all global declarations */
374 be_gas_emit_decls(isa->base.main_env);
376 pmap_destroy(isa->constants);
383 * Get the register class which shall be used to store a value of a given mode.
384 * @param self The this pointer.
385 * @param mode The mode in question.
386 * @return A register class which can hold values of the given mode.
388 static const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
390 if (mode_is_float(mode))
391 return &sparc_reg_classes[CLASS_sparc_fp];
393 return &sparc_reg_classes[CLASS_sparc_gp];
397 * Returns the necessary byte alignment for storing a register of given class.
399 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
401 ir_mode *mode = arch_register_class_mode(cls);
402 return get_mode_size_bytes(mode);
405 static ir_node *sparc_create_set(ir_node *cond)
407 return ir_create_cond_set(cond, mode_Iu);
410 static void sparc_lower_for_target(void)
412 size_t i, n_irgs = get_irp_n_irgs();
413 lower_mode_b_config_t lower_mode_b_config = {
418 lower_calls_with_compounds(LF_RETURN_HIDDEN);
420 if (sparc_isa_template.fpu_arch == SPARC_FPU_ARCH_SOFTFLOAT)
421 lower_floating_point();
425 for (i = 0; i < n_irgs; ++i) {
426 ir_graph *irg = get_irp_irg(i);
427 ir_lower_mode_b(irg, &lower_mode_b_config);
428 lower_switch(irg, 4, 256, false);
432 static int sparc_is_mux_allowed(ir_node *sel, ir_node *mux_false,
435 ir_graph *irg = get_irn_irg(sel);
436 ir_mode *mode = get_irn_mode(mux_true);
438 if (get_irg_phase_state(irg) == phase_low)
441 if (!mode_is_int(mode) && !mode_is_reference(mode) && mode != mode_b)
443 if (is_Const(mux_true) && is_Const_one(mux_true) &&
444 is_Const(mux_false) && is_Const_null(mux_false))
450 * Returns the libFirm configuration parameter for this backend.
452 static const backend_params *sparc_get_backend_params(void)
454 static const ir_settings_arch_dep_t arch_dep = {
455 1, /* also_use_subs */
456 1, /* maximum_shifts */
457 31, /* highest_shift_amount */
458 NULL, /* evaluate_cost_func */
461 32, /* max_bits_for_mulh */
463 static backend_params p = {
464 0, /* no inline assembly */
465 0, /* no support for RotL nodes */
467 1, /* modulo shift efficient */
468 0, /* non-modulo shift not efficient */
469 &arch_dep, /* will be set later */
470 sparc_is_mux_allowed, /* parameter for if conversion */
471 32, /* machine size */
472 NULL, /* float arithmetic mode */
473 NULL, /* long long type */
474 NULL, /* usigned long long type */
475 NULL, /* long double type */
476 0, /* no trampoline support: size 0 */
477 0, /* no trampoline support: align 0 */
478 NULL, /* no trampoline support: no trampoline builder */
479 4 /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
482 ir_mode *mode_long_long
483 = new_ir_mode("long long", irms_int_number, 64, 1, irma_twos_complement,
485 ir_type *type_long_long = new_type_primitive(mode_long_long);
486 ir_mode *mode_unsigned_long_long
487 = new_ir_mode("unsigned long long", irms_int_number, 64, 0,
488 irma_twos_complement, 64);
489 ir_type *type_unsigned_long_long
490 = new_type_primitive(mode_unsigned_long_long);
491 ir_mode *mode_long_double
492 = new_ir_mode("long double", irms_float_number, 128, 1,
494 ir_type *type_long_double = new_type_primitive(mode_long_double);
496 set_type_alignment_bytes(type_long_double, 8);
497 p.type_long_double = type_long_double;
498 p.type_long_long = type_long_long;
499 p.type_unsigned_long_long = type_unsigned_long_long;
503 static ir_graph **sparc_get_backend_irg_list(const void *self,
511 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
514 return ASM_CONSTRAINT_FLAG_INVALID;
517 static int sparc_is_valid_clobber(const char *clobber)
523 /* fpu set architectures. */
524 static const lc_opt_enum_int_items_t sparc_fpu_items[] = {
525 { "fpu", SPARC_FPU_ARCH_FPU },
526 { "softfloat", SPARC_FPU_ARCH_SOFTFLOAT },
530 static lc_opt_enum_int_var_t arch_fpu_var = {
531 &sparc_isa_template.fpu_arch, sparc_fpu_items
534 static const lc_opt_table_entry_t sparc_options[] = {
535 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &arch_fpu_var),
539 const arch_isa_if_t sparc_isa_if = {
541 sparc_lower_for_target,
543 NULL, /* handle intrinsics */
544 sparc_get_reg_class_for_mode,
546 sparc_get_reg_class_alignment,
547 sparc_get_backend_params,
548 sparc_get_backend_irg_list,
549 NULL, /* mark remat */
550 sparc_parse_asm_constraint,
551 sparc_is_valid_clobber,
554 NULL, /* get_pic_base */
555 NULL, /* before_abi */
560 NULL, /* register_saved_by */
563 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc)
564 void be_init_arch_sparc(void)
566 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
567 lc_opt_entry_t *sparc_grp = lc_opt_get_grp(be_grp, "sparc");
569 lc_opt_add_table(sparc_grp, sparc_options);
571 be_register_isa_if("sparc", &sparc_isa_if);
572 FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
573 sparc_init_transform();
574 sparc_init_emitter();