be: remove unused reg_class_for_mode callback
[libfirm] / ir / be / sparc / bearch_sparc.c
1 /*
2  * Copyright (C) 1995-2010 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 sparc backend driver file.
23  * @author   Hannes Rapp, Matthias Braun
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 "irtools.h"
38 #include "irdump.h"
39 #include "iropt_t.h"
40 #include "lowering.h"
41 #include "lower_dw.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"
47
48 #include "bitset.h"
49 #include "debug.h"
50 #include "array_t.h"
51 #include "error.h"
52 #include "util.h"
53
54 #include "bearch.h"
55 #include "benode.h"
56 #include "belower.h"
57 #include "besched.h"
58 #include "be.h"
59 #include "bemachine.h"
60 #include "bemodule.h"
61 #include "beirg.h"
62 #include "begnuas.h"
63 #include "belistsched.h"
64 #include "beflags.h"
65 #include "beutil.h"
66
67 #include "bearch_sparc_t.h"
68
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"
74
75 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
76
77 static arch_irn_class_t sparc_classify(const ir_node *node)
78 {
79         (void) node;
80         return arch_irn_class_none;
81 }
82
83 static ir_entity *sparc_get_frame_entity(const ir_node *node)
84 {
85         if (is_sparc_FrameAddr(node)) {
86                 const sparc_attr_t *attr = get_sparc_attr_const(node);
87                 return attr->immediate_value_entity;
88         }
89
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;
95                 }
96         }
97
98         return NULL;
99 }
100
101 /**
102  * This function is called by the generic backend to correct offsets for
103  * nodes accessing the stack.
104  */
105 static void sparc_set_frame_offset(ir_node *node, int offset)
106 {
107         sparc_attr_t *attr = get_sparc_attr(node);
108         attr->immediate_value += offset;
109
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);
113 }
114
115 static int sparc_get_sp_bias(const ir_node *node)
116 {
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");
121
122                 return -attr->immediate_value;
123         } else if (is_sparc_RestoreZero(node)) {
124                 return SP_BIAS_RESET;
125         }
126         return 0;
127 }
128
129 /* fill register allocator interface */
130
131 const arch_irn_ops_t sparc_irn_ops = {
132         sparc_classify,
133         sparc_get_frame_entity,
134         sparc_set_frame_offset,
135         sparc_get_sp_bias,
136         NULL,    /* get_inverse             */
137         NULL,    /* get_op_estimated_cost   */
138         NULL,    /* possible_memory_operand */
139         NULL,    /* perform_memory_operand  */
140 };
141
142 /**
143  * Transforms the standard firm graph into
144  * a SPARC firm graph
145  */
146 static void sparc_prepare_graph(ir_graph *irg)
147 {
148         sparc_transform_graph(irg);
149 }
150
151 static bool sparc_modifies_flags(const ir_node *node)
152 {
153         return arch_get_irn_flags(node) & sparc_arch_irn_flag_modifies_flags;
154 }
155
156 static bool sparc_modifies_fp_flags(const ir_node *node)
157 {
158         return arch_get_irn_flags(node) & sparc_arch_irn_flag_modifies_fp_flags;
159 }
160
161 static void sparc_before_ra(ir_graph *irg)
162 {
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);
168 }
169
170 static void sparc_init_graph(ir_graph *irg)
171 {
172         (void) irg;
173 }
174
175 extern const arch_isa_if_t sparc_isa_if;
176 static sparc_isa_t sparc_isa_template = {
177         {
178                 &sparc_isa_if,                      /* isa interface implementation */
179                 N_SPARC_REGISTERS,
180                 sparc_registers,
181                 N_SPARC_CLASSES,
182                 sparc_reg_classes,
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
187                                                        for calls */
188                 NULL,                               /* main environment */
189                 7,                                  /* costs for a spill instruction */
190                 5,                                  /* costs for a reload instruction */
191                 true,                               /* custom abi handling */
192         },
193         NULL,               /* constants */
194         SPARC_FPU_ARCH_FPU, /* FPU architecture */
195 };
196
197 /**
198  * rewrite unsigned->float conversion.
199  * Sparc has no instruction for this so instead we do the following:
200  *
201  *   int    signed_x = unsigned_value_x;
202  *   double res      = signed_x;
203  *   if (signed_x < 0)
204  *       res += 4294967296. ;
205  *   return (float) res;
206  */
207 static void rewrite_unsigned_float_Conv(ir_node *node)
208 {
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);
212
213         part_block(node);
214
215         {
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,
225                                                     ir_relation_less);
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,
238                                                    mode_d);
239
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);
243                 ir_node  *phi;
244                 ir_node  *res_conv;
245
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);
251
252                 res_conv = new_rd_Conv(dbgi, lower_block, phi, dest_mode);
253
254                 exchange(node, res_conv);
255         }
256 }
257
258 static int sparc_rewrite_Conv(ir_node *node, void *ctx)
259 {
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);
263         (void) ctx;
264
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);
269                 return 1;
270         }
271
272         return 0;
273 }
274
275 static void sparc_handle_intrinsics(void)
276 {
277         ir_type *tp, *int_tp, *uint_tp;
278         i_record records[8];
279         size_t n_records = 0;
280
281         runtime_rt rt_iMod, rt_uMod;
282
283 #define ID(x) new_id_from_chars(x, sizeof(x)-1)
284
285         int_tp  = new_type_primitive(mode_Is);
286         uint_tp = new_type_primitive(mode_Iu);
287
288         /* we need to rewrite some forms of int->float conversions */
289         {
290                 i_instr_record *map_Conv = &records[n_records++].i_instr;
291
292                 map_Conv->kind     = INTRINSIC_INSTR;
293                 map_Conv->op       = op_Conv;
294                 map_Conv->i_mapper = sparc_rewrite_Conv;
295         }
296         /* SPARC has no signed mod instruction ... */
297         {
298                 i_instr_record *map_Mod = &records[n_records++].i_instr;
299
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);
304
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;
313
314                 set_entity_visibility(rt_iMod.ent, ir_visibility_external);
315
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;
320         }
321         /* ... nor an unsigned mod. */
322         {
323                 i_instr_record *map_Mod = &records[n_records++].i_instr;
324
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);
329
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;
338
339                 set_entity_visibility(rt_uMod.ent, ir_visibility_external);
340
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;
345         }
346
347         assert(n_records < ARRAY_SIZE(records));
348         lower_intrinsics(records, n_records, /*part_block_used=*/ true);
349 }
350
351 /**
352  * Initializes the backend ISA
353  */
354 static arch_env_t *sparc_init(const be_main_env_t *env)
355 {
356         sparc_isa_t *isa = XMALLOC(sparc_isa_t);
357         *isa = sparc_isa_template;
358         isa->constants = pmap_create();
359
360         be_gas_elf_type_char      = '#';
361         be_gas_object_file_format = OBJECT_FILE_FORMAT_ELF;
362         be_gas_elf_variant        = ELF_VARIANT_SPARC;
363
364         sparc_register_init();
365         sparc_create_opcodes(&sparc_irn_ops);
366         sparc_handle_intrinsics();
367         sparc_cconv_init();
368
369         be_emit_init(env->file_handle);
370         be_gas_begin_compilation_unit(env);
371
372         return &isa->base;
373 }
374
375 /**
376  * Closes the output file and frees the ISA structure.
377  */
378 static void sparc_done(void *self)
379 {
380         sparc_isa_t *isa = (sparc_isa_t*)self;
381
382         /* emit now all global declarations */
383         be_gas_end_compilation_unit(isa->base.main_env);
384
385         pmap_destroy(isa->constants);
386         be_emit_exit();
387         free(isa);
388 }
389
390 /**
391  * Returns the necessary byte alignment for storing a register of given class.
392  */
393 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
394 {
395         ir_mode *mode = arch_register_class_mode(cls);
396         return get_mode_size_bytes(mode);
397 }
398
399 static void sparc_lower_for_target(void)
400 {
401         size_t i, n_irgs = get_irp_n_irgs();
402
403         lower_calls_with_compounds(LF_RETURN_HIDDEN);
404
405         for (i = 0; i < n_irgs; ++i) {
406                 ir_graph *irg = get_irp_irg(i);
407                 /* Turn all small CopyBs into loads/stores and all bigger CopyBs into
408                  * memcpy calls. */
409                 lower_CopyB(irg, 31, 32, false);
410         }
411
412         if (sparc_isa_template.fpu_arch == SPARC_FPU_ARCH_SOFTFLOAT)
413                 lower_floating_point();
414
415         lower_builtins(0, NULL);
416
417         sparc_lower_64bit();
418
419         for (i = 0; i < n_irgs; ++i) {
420                 ir_graph *irg = get_irp_irg(i);
421                 ir_lower_mode_b(irg, mode_Iu);
422                 lower_switch(irg, 4, 256, false);
423                 lower_alloc(irg, SPARC_STACK_ALIGNMENT, false, SPARC_MIN_STACKSIZE);
424         }
425 }
426
427 static int sparc_is_mux_allowed(ir_node *sel, ir_node *mux_false,
428                                 ir_node *mux_true)
429 {
430         return ir_is_optimizable_mux(sel, mux_false, mux_true);
431 }
432
433 /**
434  * Returns the libFirm configuration parameter for this backend.
435  */
436 static const backend_params *sparc_get_backend_params(void)
437 {
438         static const ir_settings_arch_dep_t arch_dep = {
439                 1,     /* also_use_subs */
440                 1,     /* maximum_shifts */
441                 31,    /* highest_shift_amount */
442                 NULL,  /* evaluate_cost_func */
443                 1,     /* allow mulhs */
444                 1,     /* allow mulhu */
445                 32,    /* max_bits_for_mulh */
446         };
447         static backend_params p = {
448                 0,     /* no inline assembly */
449                 0,     /* no support for RotL nodes */
450                 1,     /* big endian */
451                 1,     /* modulo shift efficient */
452                 0,     /* non-modulo shift not efficient */
453                 &arch_dep,              /* will be set later */
454                 sparc_is_mux_allowed,   /* parameter for if conversion */
455                 32,    /* machine size */
456                 NULL,  /* float arithmetic mode */
457                 NULL,  /* long long type */
458                 NULL,  /* usigned long long type */
459                 NULL,  /* long double type */
460                 0,     /* no trampoline support: size 0 */
461                 0,     /* no trampoline support: align 0 */
462                 NULL,  /* no trampoline support: no trampoline builder */
463                 4      /* alignment of stack parameter: typically 4 (32bit) or 8 (64bit) */
464         };
465
466         ir_mode *mode_long_long
467                 = new_int_mode("long long", irma_twos_complement, 64, 1, 64);
468         ir_type *type_long_long = new_type_primitive(mode_long_long);
469         ir_mode *mode_unsigned_long_long
470                 = new_int_mode("unsigned long long", irma_twos_complement, 64, 0, 64);
471         ir_type *type_unsigned_long_long
472                 = new_type_primitive(mode_unsigned_long_long);
473
474         p.type_long_long          = type_long_long;
475         p.type_unsigned_long_long = type_unsigned_long_long;
476
477         if (sparc_isa_template.fpu_arch == SPARC_FPU_ARCH_SOFTFLOAT) {
478                 p.mode_float_arithmetic = NULL;
479                 p.type_long_double      = NULL;
480         } else {
481                 ir_type *type_long_double = new_type_primitive(mode_Q);
482
483                 set_type_alignment_bytes(type_long_double, 8);
484                 set_type_size_bytes(type_long_double, 16);
485                 p.type_long_double = type_long_double;
486         }
487         return &p;
488 }
489
490 static ir_graph **sparc_get_backend_irg_list(const void *self,
491                                              ir_graph ***irgs)
492 {
493         (void) self;
494         (void) irgs;
495         return NULL;
496 }
497
498 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
499 {
500         (void) c;
501         return ASM_CONSTRAINT_FLAG_INVALID;
502 }
503
504 static int sparc_is_valid_clobber(const char *clobber)
505 {
506         (void) clobber;
507         return 0;
508 }
509
510 /* fpu set architectures. */
511 static const lc_opt_enum_int_items_t sparc_fpu_items[] = {
512         { "fpu",       SPARC_FPU_ARCH_FPU },
513         { "softfloat", SPARC_FPU_ARCH_SOFTFLOAT },
514         { NULL,        0 }
515 };
516
517 static lc_opt_enum_int_var_t arch_fpu_var = {
518         &sparc_isa_template.fpu_arch, sparc_fpu_items
519 };
520
521 static const lc_opt_table_entry_t sparc_options[] = {
522         LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &arch_fpu_var),
523         LC_OPT_LAST
524 };
525
526 static ir_node *sparc_new_spill(ir_node *value, ir_node *after)
527 {
528         ir_node  *block = get_block(after);
529         ir_graph *irg   = get_irn_irg(value);
530         ir_node  *frame = get_irg_frame(irg);
531         ir_node  *mem   = get_irg_no_mem(irg);
532         ir_mode  *mode  = get_irn_mode(value);
533         ir_node  *store;
534
535         if (mode_is_float(mode)) {
536                 store = create_stf(NULL, block, value, frame, mem, mode, NULL, 0, true);
537         } else {
538                 store = new_bd_sparc_St_imm(NULL, block, value, frame, mem, mode, NULL,
539                                             0, true);
540         }
541         sched_add_after(after, store);
542         return store;
543 }
544
545 static ir_node *sparc_new_reload(ir_node *value, ir_node *spill,
546                                  ir_node *before)
547 {
548         ir_node  *block = get_block(before);
549         ir_graph *irg   = get_irn_irg(value);
550         ir_node  *frame = get_irg_frame(irg);
551         ir_mode  *mode  = get_irn_mode(value);
552         ir_node  *load;
553         ir_node  *res;
554
555         if (mode_is_float(mode)) {
556                 load = create_ldf(NULL, block, frame, spill, mode, NULL, 0, true);
557         } else {
558                 load = new_bd_sparc_Ld_imm(NULL, block, frame, spill, mode, NULL, 0,
559                                            true);
560         }
561         sched_add_before(before, load);
562         assert((long)pn_sparc_Ld_res == (long)pn_sparc_Ldf_res);
563         res = new_r_Proj(load, mode, pn_sparc_Ld_res);
564
565         return res;
566 }
567
568 const arch_isa_if_t sparc_isa_if = {
569         sparc_init,
570         sparc_lower_for_target,
571         sparc_done,
572         NULL,                /* handle intrinsics */
573         NULL,
574         sparc_get_reg_class_alignment,
575         sparc_get_backend_params,
576         sparc_get_backend_irg_list,
577         NULL,                    /* mark remat */
578         sparc_parse_asm_constraint,
579         sparc_is_valid_clobber,
580
581         sparc_init_graph,
582         NULL, /* get_pic_base */
583         NULL, /* before_abi */
584         sparc_prepare_graph,
585         sparc_before_ra,
586         sparc_finish,
587         sparc_emit_routine,
588         NULL, /* register_saved_by */
589         sparc_new_spill,
590         sparc_new_reload
591 };
592
593 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc)
594 void be_init_arch_sparc(void)
595 {
596         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
597         lc_opt_entry_t *sparc_grp = lc_opt_get_grp(be_grp, "sparc");
598
599         lc_opt_add_table(sparc_grp, sparc_options);
600
601         be_register_isa_if("sparc", &sparc_isa_if);
602         FIRM_DBG_REGISTER(dbg, "firm.be.sparc.cg");
603         sparc_init_transform();
604         sparc_init_emitter();
605 }