remove $Id$, it doesn't work with git anyway
[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 /**
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.
396  */
397 static const arch_register_class_t *sparc_get_reg_class_for_mode(const ir_mode *mode)
398 {
399         if (mode_is_float(mode))
400                 return &sparc_reg_classes[CLASS_sparc_fp];
401         else
402                 return &sparc_reg_classes[CLASS_sparc_gp];
403 }
404
405 /**
406  * Returns the necessary byte alignment for storing a register of given class.
407  */
408 static int sparc_get_reg_class_alignment(const arch_register_class_t *cls)
409 {
410         ir_mode *mode = arch_register_class_mode(cls);
411         return get_mode_size_bytes(mode);
412 }
413
414 static void sparc_lower_for_target(void)
415 {
416         size_t i, n_irgs = get_irp_n_irgs();
417
418         lower_calls_with_compounds(LF_RETURN_HIDDEN);
419
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
423                  * memcpy calls. */
424                 lower_CopyB(irg, 31, 32, false);
425         }
426
427         if (sparc_isa_template.fpu_arch == SPARC_FPU_ARCH_SOFTFLOAT)
428                 lower_floating_point();
429
430         lower_builtins(0, NULL);
431
432         sparc_lower_64bit();
433
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);
439         }
440 }
441
442 static int sparc_is_mux_allowed(ir_node *sel, ir_node *mux_false,
443                                 ir_node *mux_true)
444 {
445         return ir_is_optimizable_mux(sel, mux_false, mux_true);
446 }
447
448 /**
449  * Returns the libFirm configuration parameter for this backend.
450  */
451 static const backend_params *sparc_get_backend_params(void)
452 {
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 */
458                 1,     /* allow mulhs */
459                 1,     /* allow mulhu */
460                 32,    /* max_bits_for_mulh */
461         };
462         static backend_params p = {
463                 0,     /* no inline assembly */
464                 0,     /* no support for RotL nodes */
465                 1,     /* big endian */
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) */
479         };
480
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);
488
489         p.type_long_long          = type_long_long;
490         p.type_unsigned_long_long = type_unsigned_long_long;
491
492         if (sparc_isa_template.fpu_arch == SPARC_FPU_ARCH_SOFTFLOAT) {
493                 p.mode_float_arithmetic = NULL;
494                 p.type_long_double      = NULL;
495         } else {
496                 ir_type *type_long_double = new_type_primitive(mode_Q);
497
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;
501         }
502         return &p;
503 }
504
505 static ir_graph **sparc_get_backend_irg_list(const void *self,
506                                              ir_graph ***irgs)
507 {
508         (void) self;
509         (void) irgs;
510         return NULL;
511 }
512
513 static asm_constraint_flags_t sparc_parse_asm_constraint(const char **c)
514 {
515         (void) c;
516         return ASM_CONSTRAINT_FLAG_INVALID;
517 }
518
519 static int sparc_is_valid_clobber(const char *clobber)
520 {
521         (void) clobber;
522         return 0;
523 }
524
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 },
529         { NULL,        0 }
530 };
531
532 static lc_opt_enum_int_var_t arch_fpu_var = {
533         &sparc_isa_template.fpu_arch, sparc_fpu_items
534 };
535
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),
538         LC_OPT_LAST
539 };
540
541 static ir_node *sparc_new_spill(ir_node *value, ir_node *after)
542 {
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);
548         ir_node  *store;
549
550         if (mode_is_float(mode)) {
551                 store = create_stf(NULL, block, value, frame, mem, mode, NULL, 0, true);
552         } else {
553                 store = new_bd_sparc_St_imm(NULL, block, value, frame, mem, mode, NULL,
554                                             0, true);
555         }
556         sched_add_after(after, store);
557         return store;
558 }
559
560 static ir_node *sparc_new_reload(ir_node *value, ir_node *spill,
561                                  ir_node *before)
562 {
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);
567         ir_node  *load;
568         ir_node  *res;
569
570         if (mode_is_float(mode)) {
571                 load = create_ldf(NULL, block, frame, spill, mode, NULL, 0, true);
572         } else {
573                 load = new_bd_sparc_Ld_imm(NULL, block, frame, spill, mode, NULL, 0,
574                                            true);
575         }
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);
579
580         return res;
581 }
582
583 const arch_isa_if_t sparc_isa_if = {
584         sparc_init,
585         sparc_lower_for_target,
586         sparc_done,
587         NULL,                /* handle intrinsics */
588         sparc_get_reg_class_for_mode,
589         NULL,
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,
596
597         sparc_init_graph,
598         NULL, /* get_pic_base */
599         NULL, /* before_abi */
600         sparc_prepare_graph,
601         sparc_before_ra,
602         sparc_finish,
603         sparc_emit_routine,
604         NULL, /* register_saved_by */
605         sparc_new_spill,
606         sparc_new_reload
607 };
608
609 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_sparc)
610 void be_init_arch_sparc(void)
611 {
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");
614
615         lc_opt_add_table(sparc_grp, sparc_options);
616
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();
621 }