2 * mini-codegen.c: Arch independent code generation functionality
4 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
15 #include <mono/metadata/threads.h>
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/mempool-internals.h>
18 #include <mono/utils/mono-math.h>
22 #include "mini-arch.h"
24 #ifndef MONO_MAX_XREGS
26 #define MONO_MAX_XREGS 0
27 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
28 #define MONO_ARCH_CALLEE_XREGS 0
33 #define MONO_ARCH_BANK_MIRRORED -2
35 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
37 #ifndef MONO_ARCH_NEED_SIMD_BANK
38 #error "MONO_ARCH_USE_SHARED_FP_SIMD_BANK needs MONO_ARCH_NEED_SIMD_BANK to work"
41 #define get_mirrored_bank(bank) (((bank) == MONO_REG_SIMD ) ? MONO_REG_DOUBLE : (((bank) == MONO_REG_DOUBLE ) ? MONO_REG_SIMD : -1))
43 #define is_hreg_mirrored(rs, bank, hreg) ((rs)->symbolic [(bank)] [(hreg)] == MONO_ARCH_BANK_MIRRORED)
49 #define get_mirrored_bank(bank) (-1)
51 #define is_hreg_mirrored(rs, bank, hreg) (0)
56 /* If the bank is mirrored return the true logical bank that the register in the
57 * physical register bank is allocated to.
59 static inline int translate_bank (MonoRegState *rs, int bank, int hreg) {
60 return is_hreg_mirrored (rs, bank, hreg) ? get_mirrored_bank (bank) : bank;
64 * Every hardware register belongs to a register type or register bank. bank 0
65 * contains the int registers, bank 1 contains the fp registers.
66 * int registers are used 99% of the time, so they are special cased in a lot of
70 static const int regbank_size [] = {
78 static const int regbank_load_ops [] = {
86 static const int regbank_store_ops [] = {
87 OP_STORER_MEMBASE_REG,
88 OP_STORER8_MEMBASE_REG,
89 OP_STORER_MEMBASE_REG,
90 OP_STORER_MEMBASE_REG,
94 static const int regbank_move_ops [] = {
102 #define regmask(reg) (((regmask_t)1) << (reg))
104 static const regmask_t regbank_callee_saved_regs [] = {
105 MONO_ARCH_CALLEE_SAVED_REGS,
106 MONO_ARCH_CALLEE_SAVED_FREGS,
107 MONO_ARCH_CALLEE_SAVED_REGS,
108 MONO_ARCH_CALLEE_SAVED_REGS,
109 MONO_ARCH_CALLEE_SAVED_XREGS,
112 static const regmask_t regbank_callee_regs [] = {
113 MONO_ARCH_CALLEE_REGS,
114 MONO_ARCH_CALLEE_FREGS,
115 MONO_ARCH_CALLEE_REGS,
116 MONO_ARCH_CALLEE_REGS,
117 MONO_ARCH_CALLEE_XREGS,
120 static const int regbank_spill_var_size[] = {
125 16 /*FIXME make this a constant. Maybe MONO_ARCH_SIMD_VECTOR_SIZE? */
128 #define DEBUG(a) MINI_DEBUG(cfg->verbose_level, 3, a;)
131 mono_regstate_assign (MonoRegState *rs)
133 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
134 /* The regalloc may fail if fp and simd logical regbanks share the same physical reg bank and
135 * if the values here are not the same.
137 g_assert(regbank_callee_regs [MONO_REG_SIMD] == regbank_callee_regs [MONO_REG_DOUBLE]);
138 g_assert(regbank_callee_saved_regs [MONO_REG_SIMD] == regbank_callee_saved_regs [MONO_REG_DOUBLE]);
139 g_assert(regbank_size [MONO_REG_SIMD] == regbank_size [MONO_REG_DOUBLE]);
142 if (rs->next_vreg > rs->vassign_size) {
143 g_free (rs->vassign);
144 rs->vassign_size = MAX (rs->next_vreg, 256);
145 rs->vassign = g_malloc (rs->vassign_size * sizeof (gint32));
148 memset (rs->isymbolic, 0, MONO_MAX_IREGS * sizeof (rs->isymbolic [0]));
149 memset (rs->fsymbolic, 0, MONO_MAX_FREGS * sizeof (rs->fsymbolic [0]));
151 rs->symbolic [MONO_REG_INT] = rs->isymbolic;
152 rs->symbolic [MONO_REG_DOUBLE] = rs->fsymbolic;
154 #ifdef MONO_ARCH_NEED_SIMD_BANK
155 memset (rs->xsymbolic, 0, MONO_MAX_XREGS * sizeof (rs->xsymbolic [0]));
156 rs->symbolic [MONO_REG_SIMD] = rs->xsymbolic;
161 mono_regstate_alloc_int (MonoRegState *rs, regmask_t allow)
163 regmask_t mask = allow & rs->ifree_mask;
165 #if defined(__x86_64__) && defined(__GNUC__)
172 __asm__("bsfq %1,%0\n\t"
173 : "=r" (i) : "rm" (mask));
175 rs->ifree_mask &= ~ ((regmask_t)1 << i);
181 for (i = 0; i < MONO_MAX_IREGS; ++i) {
182 if (mask & ((regmask_t)1 << i)) {
183 rs->ifree_mask &= ~ ((regmask_t)1 << i);
192 mono_regstate_free_int (MonoRegState *rs, int reg)
195 rs->ifree_mask |= (regmask_t)1 << reg;
196 rs->isymbolic [reg] = 0;
201 mono_regstate_alloc_general (MonoRegState *rs, regmask_t allow, int bank)
205 regmask_t mask = allow & rs->free_mask [bank];
206 for (i = 0; i < regbank_size [bank]; ++i) {
207 if (mask & ((regmask_t)1 << i)) {
208 rs->free_mask [bank] &= ~ ((regmask_t)1 << i);
210 mirrored_bank = get_mirrored_bank (bank);
211 if (mirrored_bank == -1)
214 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
222 mono_regstate_free_general (MonoRegState *rs, int reg, int bank)
227 rs->free_mask [bank] |= (regmask_t)1 << reg;
228 rs->symbolic [bank][reg] = 0;
230 mirrored_bank = get_mirrored_bank (bank);
231 if (mirrored_bank == -1)
233 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
234 rs->symbolic [mirrored_bank][reg] = 0;
239 mono_regname_full (int reg, int bank)
241 if (G_UNLIKELY (bank)) {
242 #if MONO_ARCH_NEED_SIMD_BANK
243 if (bank == MONO_REG_SIMD)
244 return mono_arch_xregname (reg);
246 if (bank == MONO_REG_INT_REF || bank == MONO_REG_INT_MP)
247 return mono_arch_regname (reg);
248 g_assert (bank == MONO_REG_DOUBLE);
249 return mono_arch_fregname (reg);
251 return mono_arch_regname (reg);
256 mono_call_inst_add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, int bank)
260 regpair = (((guint32)hreg) << 24) + vreg;
261 if (G_UNLIKELY (bank)) {
262 g_assert (vreg >= regbank_size [bank]);
263 g_assert (hreg < regbank_size [bank]);
264 call->used_fregs |= 1 << hreg;
265 call->out_freg_args = g_slist_append_mempool (cfg->mempool, call->out_freg_args, (gpointer)(gssize)(regpair));
267 g_assert (vreg >= MONO_MAX_IREGS);
268 g_assert (hreg < MONO_MAX_IREGS);
269 call->used_iregs |= 1 << hreg;
270 call->out_ireg_args = g_slist_append_mempool (cfg->mempool, call->out_ireg_args, (gpointer)(gssize)(regpair));
275 * mono_call_inst_add_outarg_vt:
277 * Register OUTARG_VT as belonging to CALL.
280 mono_call_inst_add_outarg_vt (MonoCompile *cfg, MonoCallInst *call, MonoInst *outarg_vt)
282 call->outarg_vts = g_slist_append_mempool (cfg->mempool, call->outarg_vts, outarg_vt);
286 resize_spill_info (MonoCompile *cfg, int bank)
288 MonoSpillInfo *orig_info = cfg->spill_info [bank];
289 int orig_len = cfg->spill_info_len [bank];
290 int new_len = orig_len ? orig_len * 2 : 16;
291 MonoSpillInfo *new_info;
294 g_assert (bank < MONO_NUM_REGBANKS);
296 new_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoSpillInfo) * new_len);
298 memcpy (new_info, orig_info, sizeof (MonoSpillInfo) * orig_len);
299 for (i = orig_len; i < new_len; ++i)
300 new_info [i].offset = -1;
302 cfg->spill_info [bank] = new_info;
303 cfg->spill_info_len [bank] = new_len;
307 * returns the offset used by spillvar. It allocates a new
308 * spill variable if necessary.
311 mono_spillvar_offset (MonoCompile *cfg, int spillvar, int bank)
316 if (G_UNLIKELY (spillvar >= (cfg->spill_info_len [bank]))) {
317 while (spillvar >= cfg->spill_info_len [bank])
318 resize_spill_info (cfg, bank);
322 * Allocate separate spill slots for fp/non-fp variables since most processors prefer it.
324 info = &cfg->spill_info [bank][spillvar];
325 if (info->offset == -1) {
326 cfg->stack_offset += sizeof (mgreg_t) - 1;
327 cfg->stack_offset &= ~(sizeof (mgreg_t) - 1);
329 g_assert (bank < MONO_NUM_REGBANKS);
330 if (G_UNLIKELY (bank))
331 size = regbank_spill_var_size [bank];
333 size = sizeof (mgreg_t);
335 if (cfg->flags & MONO_CFG_HAS_SPILLUP) {
336 cfg->stack_offset += size - 1;
337 cfg->stack_offset &= ~(size - 1);
338 info->offset = cfg->stack_offset;
339 cfg->stack_offset += size;
341 cfg->stack_offset += size - 1;
342 cfg->stack_offset &= ~(size - 1);
343 cfg->stack_offset += size;
344 info->offset = - cfg->stack_offset;
351 #define is_hard_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS)
352 #define is_hard_freg(r) ((r) >= 0 && (r) < MONO_MAX_FREGS)
353 #define is_global_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_SAVED_REGS & (regmask (r))))
354 #define is_local_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_REGS & (regmask (r))))
355 #define is_global_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_SAVED_FREGS & (regmask (r))))
356 #define is_local_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_FREGS & (regmask (r))))
358 #define is_hard_reg(r,bank) (G_UNLIKELY (bank) ? ((r) >= 0 && (r) < regbank_size [bank]) : ((r) < MONO_MAX_IREGS))
359 #define is_soft_reg(r,bank) (!is_hard_reg((r),(bank)))
360 #define is_global_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_saved_regs [bank] & regmask (r))) : is_global_ireg (r))
361 #define is_local_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_regs [bank] & regmask (r))) : is_local_ireg (r))
362 #define reg_is_freeable(r,bank) (G_UNLIKELY (bank) ? is_local_reg ((r), (bank)) : is_local_ireg ((r)))
364 #ifndef MONO_ARCH_INST_IS_FLOAT
365 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc) == 'f')
368 #define reg_is_fp(desc) (MONO_ARCH_INST_IS_FLOAT (desc))
369 #define dreg_is_fp(spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_DEST]))
370 #define sreg_is_fp(n,spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_SRC1+(n)]))
371 #define sreg1_is_fp(spec) sreg_is_fp (0,(spec))
372 #define sreg2_is_fp(spec) sreg_is_fp (1,(spec))
374 #define reg_is_simd(desc) ((desc) == 'x')
376 #ifdef MONO_ARCH_NEED_SIMD_BANK
378 #define reg_bank(desc) (G_UNLIKELY (reg_is_fp (desc)) ? MONO_REG_DOUBLE : G_UNLIKELY (reg_is_simd(desc)) ? MONO_REG_SIMD : MONO_REG_INT)
382 #define reg_bank(desc) reg_is_fp ((desc))
386 #define sreg_bank(n,spec) reg_bank ((spec)[MONO_INST_SRC1+(n)])
387 #define sreg1_bank(spec) sreg_bank (0, (spec))
388 #define sreg2_bank(spec) sreg_bank (1, (spec))
389 #define dreg_bank(spec) reg_bank ((spec)[MONO_INST_DEST])
391 #define sreg_bank_ins(n,ins) sreg_bank ((n), ins_get_spec ((ins)->opcode))
392 #define sreg1_bank_ins(ins) sreg_bank_ins (0, (ins))
393 #define sreg2_bank_ins(ins) sreg_bank_ins (1, (ins))
394 #define dreg_bank_ins(ins) dreg_bank (ins_get_spec ((ins)->opcode))
396 #define regpair_reg2_mask(desc,hreg1) ((MONO_ARCH_INST_REGPAIR_REG2 (desc,hreg1) != -1) ? (regmask (MONO_ARCH_INST_REGPAIR_REG2 (desc,hreg1))) : MONO_ARCH_CALLEE_REGS)
398 #ifdef MONO_ARCH_IS_GLOBAL_IREG
399 #undef is_global_ireg
400 #define is_global_ireg(reg) MONO_ARCH_IS_GLOBAL_IREG ((reg))
409 regmask_t preferred_mask; /* the hreg where the register should be allocated, or 0 */
412 #if !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT)
414 static const char* const patch_info_str[] = {
415 #define PATCH_INFO(a,b) "" #a,
416 #include "patch-info.h"
421 mono_print_ji (const MonoJumpInfo *ji)
424 case MONO_PATCH_INFO_RGCTX_FETCH: {
425 MonoJumpInfoRgctxEntry *entry = ji->data.rgctx_entry;
427 printf ("[RGCTX_FETCH ");
428 mono_print_ji (entry->data);
429 printf (" - %s]", mono_rgctx_info_type_to_str (entry->info_type));
432 case MONO_PATCH_INFO_METHODCONST: {
433 char *s = mono_method_full_name (ji->data.method, TRUE);
434 printf ("[METHODCONST - %s]", s);
439 printf ("[%s]", patch_info_str [ji->type]);
445 mono_print_ins_index (int i, MonoInst *ins)
447 const char *spec = ins_get_spec (ins->opcode);
449 int sregs [MONO_MAX_SRC_REGS];
452 printf ("\t%-2d %s", i, mono_inst_name (ins->opcode));
454 printf (" %s", mono_inst_name (ins->opcode));
455 if (spec == MONO_ARCH_CPU_SPEC) {
456 gboolean dest_base = FALSE;
457 switch (ins->opcode) {
458 case OP_STOREV_MEMBASE:
465 /* This is a lowered opcode */
466 if (ins->dreg != -1) {
468 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
470 printf (" R%d <-", ins->dreg);
472 if (ins->sreg1 != -1)
473 printf (" R%d", ins->sreg1);
474 if (ins->sreg2 != -1)
475 printf (" R%d", ins->sreg2);
476 if (ins->sreg3 != -1)
477 printf (" R%d", ins->sreg3);
479 switch (ins->opcode) {
490 if (!ins->inst_false_bb)
491 printf (" [B%d]", ins->inst_true_bb->block_num);
493 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
500 printf (" [%d (", (int)ins->inst_c0);
501 for (i = 0; i < ins->inst_phi_args [0]; i++) {
504 printf ("R%d", ins->inst_phi_args [i + 1]);
510 case OP_OUTARG_VTRETADDR:
511 printf (" R%d", ((MonoInst*)ins->inst_p0)->dreg);
514 case OP_GSHAREDVT_ARG_REGOFFSET:
515 printf (" + 0x%lx", (long)ins->inst_offset);
522 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
526 if (spec [MONO_INST_DEST]) {
527 int bank = dreg_bank (spec);
528 if (is_soft_reg (ins->dreg, bank)) {
529 if (spec [MONO_INST_DEST] == 'b') {
530 if (ins->inst_offset == 0)
531 printf (" [R%d] <-", ins->dreg);
533 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
536 printf (" R%d <-", ins->dreg);
537 } else if (spec [MONO_INST_DEST] == 'b') {
538 if (ins->inst_offset == 0)
539 printf (" [%s] <-", mono_arch_regname (ins->dreg));
541 printf (" [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
543 printf (" %s <-", mono_regname_full (ins->dreg, bank));
545 if (spec [MONO_INST_SRC1]) {
546 int bank = sreg1_bank (spec);
547 if (is_soft_reg (ins->sreg1, bank)) {
548 if (spec [MONO_INST_SRC1] == 'b')
549 printf (" [R%d + 0x%lx]", ins->sreg1, (long)ins->inst_offset);
551 printf (" R%d", ins->sreg1);
552 } else if (spec [MONO_INST_SRC1] == 'b')
553 printf (" [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
555 printf (" %s", mono_regname_full (ins->sreg1, bank));
557 num_sregs = mono_inst_get_src_registers (ins, sregs);
558 for (j = 1; j < num_sregs; ++j) {
559 int bank = sreg_bank (j, spec);
560 if (is_soft_reg (sregs [j], bank))
561 printf (" R%d", sregs [j]);
563 printf (" %s", mono_regname_full (sregs [j], bank));
566 switch (ins->opcode) {
568 printf (" [%d]", (int)ins->inst_c0);
570 #if defined(TARGET_X86) || defined(TARGET_AMD64)
571 case OP_X86_PUSH_IMM:
573 case OP_ICOMPARE_IMM:
581 printf (" [%d]", (int)ins->inst_imm);
585 printf (" [%d]", (int)(gssize)ins->inst_p1);
588 printf (" [%lld]", (long long)ins->inst_l);
591 printf (" [%f]", *(double*)ins->inst_p0);
594 printf (" [%f]", *(float*)ins->inst_p0);
597 case OP_CALL_MEMBASE:
603 case OP_VCALL_MEMBASE:
606 case OP_VCALL2_MEMBASE:
608 case OP_VOIDCALL_MEMBASE:
610 MonoCallInst *call = (MonoCallInst*)ins;
613 if (ins->opcode == OP_VCALL || ins->opcode == OP_VCALL_REG || ins->opcode == OP_VCALL_MEMBASE) {
615 * These are lowered opcodes, but they are in the .md files since the old
616 * JIT passes them to backends.
619 printf (" R%d <-", ins->dreg);
623 char *full_name = mono_method_full_name (call->method, TRUE);
624 printf (" [%s]", full_name);
626 } else if (call->fptr_is_patch) {
627 MonoJumpInfo *ji = (MonoJumpInfo*)call->fptr;
631 } else if (call->fptr) {
632 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
634 printf (" [%s]", info->name);
637 list = call->out_ireg_args;
642 regpair = (guint32)(gssize)(list->data);
643 hreg = regpair >> 24;
644 reg = regpair & 0xffffff;
646 printf (" [%s <- R%d]", mono_arch_regname (hreg), reg);
648 list = g_slist_next (list);
653 case OP_CALL_HANDLER:
654 printf (" [B%d]", ins->inst_target_bb->block_num);
676 if (!ins->inst_false_bb)
677 printf (" [B%d]", ins->inst_true_bb->block_num);
679 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
681 case OP_LIVERANGE_START:
682 case OP_LIVERANGE_END:
683 case OP_GC_LIVENESS_DEF:
684 case OP_GC_LIVENESS_USE:
685 printf (" R%d", (int)ins->inst_c1);
688 printf (" il: %x", (int)ins->inst_imm);
694 if (spec [MONO_INST_CLOB])
695 printf (" clobbers: %c", spec [MONO_INST_CLOB]);
700 print_regtrack (RegTrack *t, int num)
706 for (i = 0; i < num; ++i) {
709 if (i >= MONO_MAX_IREGS) {
710 g_snprintf (buf, sizeof(buf), "R%d", i);
713 r = mono_arch_regname (i);
714 printf ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].killed_in);
720 mono_print_ji (const MonoJumpInfo *ji)
725 mono_print_ins_index (int i, MonoInst *ins)
728 #endif /* !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT) */
731 mono_print_ins (MonoInst *ins)
733 mono_print_ins_index (-1, ins);
737 insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst* to_insert)
740 * If this function is called multiple times, the new instructions are inserted
741 * in the proper order.
743 mono_bblock_insert_before_ins (bb, ins, to_insert);
747 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst **last, MonoInst* to_insert)
750 * If this function is called multiple times, the new instructions are inserted in
753 mono_bblock_insert_after_ins (bb, *last, to_insert);
759 get_vreg_bank (MonoCompile *cfg, int reg, int bank)
761 if (vreg_is_ref (cfg, reg))
762 return MONO_REG_INT_REF;
763 else if (vreg_is_mp (cfg, reg))
764 return MONO_REG_INT_MP;
770 * Force the spilling of the variable in the symbolic register 'reg', and free
771 * the hreg it was assigned to.
774 spill_vreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int reg, int bank)
779 MonoRegState *rs = cfg->rs;
781 symbolic = rs->symbolic [bank];
782 sel = rs->vassign [reg];
784 /* the vreg we need to spill lives in another logical reg bank */
785 bank = translate_bank (cfg->rs, bank, sel);
787 /*i = rs->isymbolic [sel];
788 g_assert (i == reg);*/
790 spill = ++cfg->spill_count;
791 rs->vassign [i] = -spill - 1;
792 if (G_UNLIKELY (bank))
793 mono_regstate_free_general (rs, sel, bank);
795 mono_regstate_free_int (rs, sel);
796 /* we need to create a spill var and insert a load to sel after the current instruction */
797 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
799 load->inst_basereg = cfg->frame_reg;
800 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, reg, bank));
801 insert_after_ins (bb, ins, last, load);
802 DEBUG (printf ("SPILLED LOAD (%d at 0x%08lx(%%ebp)) R%d (freed %s)\n", spill, (long)load->inst_offset, i, mono_regname_full (sel, bank)));
803 if (G_UNLIKELY (bank))
804 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
806 i = mono_regstate_alloc_int (rs, regmask (sel));
809 if (G_UNLIKELY (bank))
810 mono_regstate_free_general (rs, sel, bank);
812 mono_regstate_free_int (rs, sel);
815 /* This isn't defined on older glib versions and on some platforms */
816 #ifndef G_GUINT64_FORMAT
817 #define G_GUINT64_FORMAT "ul"
821 get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t regmask, int reg, int bank)
824 int i, sel, spill, num_sregs;
825 int sregs [MONO_MAX_SRC_REGS];
827 MonoRegState *rs = cfg->rs;
829 symbolic = rs->symbolic [bank];
831 g_assert (bank < MONO_NUM_REGBANKS);
833 DEBUG (printf ("\tstart regmask to assign R%d: 0x%08llu (R%d <- R%d R%d R%d)\n", reg, (unsigned long long)regmask, ins->dreg, ins->sreg1, ins->sreg2, ins->sreg3));
834 /* exclude the registers in the current instruction */
835 num_sregs = mono_inst_get_src_registers (ins, sregs);
836 for (i = 0; i < num_sregs; ++i) {
837 if ((sreg_bank_ins (i, ins) == bank) && (reg != sregs [i]) && (reg_is_freeable (sregs [i], bank) || (is_soft_reg (sregs [i], bank) && rs->vassign [sregs [i]] >= 0))) {
838 if (is_soft_reg (sregs [i], bank))
839 regmask &= ~ (regmask (rs->vassign [sregs [i]]));
841 regmask &= ~ (regmask (sregs [i]));
842 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i + 1, mono_regname_full (sregs [i], bank), sregs [i]));
845 if ((dreg_bank_ins (ins) == bank) && (reg != ins->dreg) && reg_is_freeable (ins->dreg, bank)) {
846 regmask &= ~ (regmask (ins->dreg));
847 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins->dreg, bank)));
850 DEBUG (printf ("\t\tavailable regmask: 0x%08llu\n", (unsigned long long)regmask));
851 g_assert (regmask); /* need at least a register we can free */
853 /* we should track prev_use and spill the register that's farther */
854 if (G_UNLIKELY (bank)) {
855 for (i = 0; i < regbank_size [bank]; ++i) {
856 if (regmask & (regmask (i))) {
859 /* the vreg we need to load lives in another logical bank */
860 bank = translate_bank (cfg->rs, bank, sel);
862 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
867 i = rs->symbolic [bank] [sel];
868 spill = ++cfg->spill_count;
869 rs->vassign [i] = -spill - 1;
870 mono_regstate_free_general (rs, sel, bank);
873 for (i = 0; i < MONO_MAX_IREGS; ++i) {
874 if (regmask & (regmask (i))) {
876 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), rs->isymbolic [sel]));
881 i = rs->isymbolic [sel];
882 spill = ++cfg->spill_count;
883 rs->vassign [i] = -spill - 1;
884 mono_regstate_free_int (rs, sel);
887 /* we need to create a spill var and insert a load to sel after the current instruction */
888 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
890 load->inst_basereg = cfg->frame_reg;
891 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, i, bank));
892 insert_after_ins (bb, ins, last, load);
893 DEBUG (printf ("\tSPILLED LOAD (%d at 0x%08lx(%%ebp)) R%d (freed %s)\n", spill, (long)load->inst_offset, i, mono_regname_full (sel, bank)));
894 if (G_UNLIKELY (bank))
895 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
897 i = mono_regstate_alloc_int (rs, regmask (sel));
906 * Free up the hreg HREG by spilling the vreg allocated to it.
909 free_up_hreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int hreg, int bank)
911 if (G_UNLIKELY (bank)) {
912 if (!(cfg->rs->free_mask [bank] & (regmask (hreg)))) {
913 bank = translate_bank (cfg->rs, bank, hreg);
914 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
915 spill_vreg (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
919 if (!(cfg->rs->ifree_mask & (regmask (hreg)))) {
920 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->isymbolic [hreg]));
921 spill_vreg (cfg, bb, last, ins, cfg->rs->isymbolic [hreg], bank);
927 create_copy_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, int dest, int src, MonoInst *ins, const unsigned char *ip, int bank)
931 MONO_INST_NEW (cfg, copy, regbank_move_ops [bank]);
937 mono_bblock_insert_after_ins (bb, ins, copy);
940 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src, bank), mono_regname_full (dest, bank)));
944 static inline const char*
945 regbank_to_string (int bank)
947 if (bank == MONO_REG_INT_REF)
949 else if (bank == MONO_REG_INT_MP)
956 create_spilled_store (MonoCompile *cfg, MonoBasicBlock *bb, int spill, int reg, int prev_reg, MonoInst **last, MonoInst *ins, MonoInst *insert_before, int bank)
958 MonoInst *store, *def;
960 bank = get_vreg_bank (cfg, prev_reg, bank);
962 MONO_INST_NEW (cfg, store, regbank_store_ops [bank]);
964 store->inst_destbasereg = cfg->frame_reg;
965 store->inst_offset = mono_spillvar_offset (cfg, spill, bank);
967 mono_bblock_insert_after_ins (bb, ins, store);
969 } else if (insert_before) {
970 insert_before_ins (bb, insert_before, store);
972 g_assert_not_reached ();
974 DEBUG (printf ("\t%sSPILLED STORE (%d at 0x%08lx(%%ebp)) R%d (from %s)\n", regbank_to_string (bank), spill, (long)store->inst_offset, prev_reg, mono_regname_full (reg, bank)));
976 if (((bank == MONO_REG_INT_REF) || (bank == MONO_REG_INT_MP)) && cfg->compute_gc_maps) {
977 g_assert (prev_reg != -1);
978 MONO_INST_NEW (cfg, def, OP_GC_SPILL_SLOT_LIVENESS_DEF);
979 def->inst_c0 = spill;
981 mono_bblock_insert_after_ins (bb, store, def);
985 /* flags used in reginfo->flags */
987 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
988 MONO_FP_NEEDS_SPILL = regmask (1),
989 MONO_FP_NEEDS_LOAD = regmask (2)
993 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
997 if (info && info->preferred_mask) {
998 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
1000 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
1005 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
1007 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
1013 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
1017 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
1020 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1026 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
1028 if (G_UNLIKELY (bank))
1029 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1031 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
1035 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
1037 if (G_UNLIKELY (bank)) {
1040 g_assert (reg >= regbank_size [bank]);
1041 g_assert (hreg < regbank_size [bank]);
1042 g_assert (! is_global_freg (hreg));
1044 rs->vassign [reg] = hreg;
1045 rs->symbolic [bank] [hreg] = reg;
1046 rs->free_mask [bank] &= ~ (regmask (hreg));
1048 mirrored_bank = get_mirrored_bank (bank);
1049 if (mirrored_bank == -1)
1052 /* Make sure the other logical reg bank that this bank shares
1053 * a single hard reg bank knows that this hard reg is not free.
1055 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
1057 /* Mark the other logical bank that the this bank shares
1058 * a single hard reg bank with as mirrored.
1060 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
1064 g_assert (reg >= MONO_MAX_IREGS);
1065 g_assert (hreg < MONO_MAX_IREGS);
1067 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
1068 g_assert (! is_global_ireg (hreg));
1071 rs->vassign [reg] = hreg;
1072 rs->isymbolic [hreg] = reg;
1073 rs->ifree_mask &= ~ (regmask (hreg));
1077 static inline regmask_t
1078 get_callee_mask (const char spec)
1080 if (G_UNLIKELY (reg_bank (spec)))
1081 return regbank_callee_regs [reg_bank (spec)];
1082 return MONO_ARCH_CALLEE_REGS;
1085 static gint8 desc_to_fixed_reg [256];
1086 static gboolean desc_to_fixed_reg_inited = FALSE;
1091 * Local register allocation.
1092 * We first scan the list of instructions and we save the liveness info of
1093 * each register (when the register is first used, when it's value is set etc.).
1094 * We also reverse the list of instructions because assigning registers backwards allows
1095 * for more tricks to be used.
1098 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1100 MonoInst *ins, *prev, *last;
1102 MonoRegState *rs = cfg->rs;
1106 unsigned char spec_src1, spec_dest;
1108 #if MONO_ARCH_USE_FPSTACK
1109 gboolean has_fp = FALSE;
1114 int sregs [MONO_MAX_SRC_REGS];
1119 if (!desc_to_fixed_reg_inited) {
1120 for (i = 0; i < 256; ++i)
1121 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1122 desc_to_fixed_reg_inited = TRUE;
1124 /* Validate the cpu description against the info in mini-ops.h */
1125 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM)
1126 for (i = OP_LOAD; i < OP_LAST; ++i) {
1129 spec = ins_get_spec (i);
1130 ispec = INS_INFO (i);
1132 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1133 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1134 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1135 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1136 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1137 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1142 rs->next_vreg = bb->max_vreg;
1143 mono_regstate_assign (rs);
1145 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1146 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1147 rs->free_mask [i] = regbank_callee_regs [i];
1149 max = rs->next_vreg;
1151 if (cfg->reginfo && cfg->reginfo_len < max)
1152 cfg->reginfo = NULL;
1154 reginfo = cfg->reginfo;
1156 cfg->reginfo_len = MAX (1024, max * 2);
1157 reginfo = cfg->reginfo = mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1160 g_assert (cfg->reginfo_len >= rs->next_vreg);
1162 if (cfg->verbose_level > 1) {
1163 /* print_regtrack reads the info of all variables */
1164 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1168 * For large methods, next_vreg can be very large, so g_malloc0 time can
1169 * be prohibitive. So we manually init the reginfo entries used by the
1172 for (ins = bb->code; ins; ins = ins->next) {
1173 spec = ins_get_spec (ins->opcode);
1175 if ((ins->dreg != -1) && (ins->dreg < max)) {
1176 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1177 #if SIZEOF_REGISTER == 4
1178 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1180 * In the new IR, the two vregs of the regpair do not alias the
1181 * original long vreg. shift the vreg here so the rest of the
1182 * allocator doesn't have to care about it.
1185 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1190 num_sregs = mono_inst_get_src_registers (ins, sregs);
1191 for (j = 0; j < num_sregs; ++j) {
1192 g_assert (sregs [j] != -1);
1193 if (sregs [j] < max) {
1194 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1195 #if SIZEOF_REGISTER == 4
1196 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1198 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1203 mono_inst_set_src_registers (ins, sregs);
1206 /*if (cfg->opt & MONO_OPT_COPYPROP)
1207 local_copy_prop (cfg, ins);*/
1210 DEBUG (printf ("\nLOCAL REGALLOC BLOCK %d:\n", bb->block_num));
1211 /* forward pass on the instructions to collect register liveness info */
1212 MONO_BB_FOR_EACH_INS (bb, ins) {
1213 spec = ins_get_spec (ins->opcode);
1214 spec_dest = spec [MONO_INST_DEST];
1216 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1217 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1220 DEBUG (mono_print_ins_index (i, ins));
1222 num_sregs = mono_inst_get_src_registers (ins, sregs);
1224 #if MONO_ARCH_USE_FPSTACK
1225 if (dreg_is_fp (spec)) {
1228 for (j = 0; j < num_sregs; ++j) {
1229 if (sreg_is_fp (j, spec))
1235 for (j = 0; j < num_sregs; ++j) {
1236 int sreg = sregs [j];
1237 int sreg_spec = spec [MONO_INST_SRC1 + j];
1239 bank = sreg_bank (j, spec);
1240 g_assert (sreg != -1);
1241 if (is_soft_reg (sreg, bank))
1242 /* This means the vreg is not local to this bb */
1243 g_assert (reginfo [sreg].born_in > 0);
1244 rs->vassign [sreg] = -1;
1245 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1246 //reginfo [ins->sreg2].last_use = i;
1247 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1248 /* The virtual register is allocated sequentially */
1249 rs->vassign [sreg + 1] = -1;
1250 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1251 //reginfo [ins->sreg2 + 1].last_use = i;
1252 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1253 reginfo [sreg + 1].born_in = i;
1259 mono_inst_set_src_registers (ins, sregs);
1264 bank = dreg_bank (spec);
1265 if (spec_dest != 'b') /* it's not just a base register */
1266 reginfo [ins->dreg].killed_in = i;
1267 g_assert (ins->dreg != -1);
1268 rs->vassign [ins->dreg] = -1;
1269 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1270 //reginfo [ins->dreg].last_use = i;
1271 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1272 reginfo [ins->dreg].born_in = i;
1274 dest_dreg = desc_to_fixed_reg [spec_dest];
1275 if (dest_dreg != -1)
1276 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1278 #ifdef MONO_ARCH_INST_FIXED_MASK
1279 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1282 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1283 /* The virtual register is allocated sequentially */
1284 rs->vassign [ins->dreg + 1] = -1;
1285 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1286 //reginfo [ins->dreg + 1].last_use = i;
1287 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1288 reginfo [ins->dreg + 1].born_in = i;
1289 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1290 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1296 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1297 /* A call instruction implicitly uses all registers in call->out_ireg_args */
1299 MonoCallInst *call = (MonoCallInst*)ins;
1302 list = call->out_ireg_args;
1308 regpair = (guint32)(gssize)(list->data);
1309 hreg = regpair >> 24;
1310 reg = regpair & 0xffffff;
1312 //reginfo [reg].prev_use = reginfo [reg].last_use;
1313 //reginfo [reg].last_use = i;
1315 list = g_slist_next (list);
1319 list = call->out_freg_args;
1325 regpair = (guint32)(gssize)(list->data);
1326 hreg = regpair >> 24;
1327 reg = regpair & 0xffffff;
1329 list = g_slist_next (list);
1339 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1340 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1341 int prev_dreg, clob_dreg;
1342 int dest_dreg, clob_reg;
1343 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1344 int dreg_high, sreg1_high;
1345 regmask_t dreg_mask, mask;
1346 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1347 regmask_t dreg_fixed_mask;
1348 const unsigned char *ip;
1350 spec = ins_get_spec (ins->opcode);
1351 spec_src1 = spec [MONO_INST_SRC1];
1352 spec_dest = spec [MONO_INST_DEST];
1359 dreg_mask = get_callee_mask (spec_dest);
1360 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1361 prev_sregs [j] = -1;
1362 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1363 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1364 #ifdef MONO_ARCH_INST_FIXED_MASK
1365 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1367 sreg_fixed_masks [j] = 0;
1371 DEBUG (printf ("processing:"));
1372 DEBUG (mono_print_ins_index (i, ins));
1381 dest_dreg = desc_to_fixed_reg [spec_dest];
1382 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1383 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1385 #ifdef MONO_ARCH_INST_FIXED_MASK
1386 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1388 dreg_fixed_mask = 0;
1391 num_sregs = mono_inst_get_src_registers (ins, sregs);
1394 * TRACK FIXED SREG2, 3, ...
1396 for (j = 1; j < num_sregs; ++j) {
1397 int sreg = sregs [j];
1398 int dest_sreg = dest_sregs [j];
1400 if (dest_sreg == -1)
1408 * We need to special case this, since on x86, there are only 3
1409 * free registers, and the code below assigns one of them to
1410 * sreg, so we can run out of registers when trying to assign
1411 * dreg. Instead, we just set up the register masks, and let the
1412 * normal sreg2 assignment code handle this. It would be nice to
1413 * do this for all the fixed reg cases too, but there is too much
1417 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1418 sreg_masks [j] = regmask (dest_sreg);
1419 for (k = 0; k < num_sregs; ++k) {
1421 sreg_masks [k] &= ~ (regmask (dest_sreg));
1425 * Spill sreg1/2 if they are assigned to dest_sreg.
1427 for (k = 0; k < num_sregs; ++k) {
1428 if (k != j && is_soft_reg (sregs [k], 0) && rs->vassign [sregs [k]] == dest_sreg)
1429 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1433 * We can also run out of registers while processing sreg2 if sreg3 is
1434 * assigned to another hreg, so spill sreg3 now.
1436 if (is_soft_reg (sreg, 0) && rs->vassign [sreg] >= 0 && rs->vassign [sreg] != dest_sreg) {
1437 spill_vreg (cfg, bb, tmp, ins, sreg, 0);
1442 if (rs->ifree_mask & (regmask (dest_sreg))) {
1443 if (is_global_ireg (sreg)) {
1445 /* Argument already in hard reg, need to copy */
1446 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1447 insert_before_ins (bb, ins, copy);
1448 for (k = 0; k < num_sregs; ++k) {
1450 sreg_masks [k] &= ~ (regmask (dest_sreg));
1453 val = rs->vassign [sreg];
1455 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1456 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1457 } else if (val < -1) {
1459 g_assert_not_reached ();
1461 /* Argument already in hard reg, need to copy */
1462 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1465 insert_before_ins (bb, ins, copy);
1466 for (k = 0; k < num_sregs; ++k) {
1468 sreg_masks [k] &= ~ (regmask (dest_sreg));
1471 * Prevent the dreg from being allocate to dest_sreg
1472 * too, since it could force sreg1 to be allocated to
1473 * the same reg on x86.
1475 dreg_mask &= ~ (regmask (dest_sreg));
1479 gboolean need_spill = TRUE;
1480 gboolean need_assign = TRUE;
1483 dreg_mask &= ~ (regmask (dest_sreg));
1484 for (k = 0; k < num_sregs; ++k) {
1486 sreg_masks [k] &= ~ (regmask (dest_sreg));
1490 * First check if dreg is assigned to dest_sreg2, since we
1491 * can't spill a dreg.
1493 if (spec [MONO_INST_DEST])
1494 val = rs->vassign [ins->dreg];
1497 if (val == dest_sreg && ins->dreg != sreg) {
1499 * the destination register is already assigned to
1500 * dest_sreg2: we need to allocate another register for it
1501 * and then copy from this to dest_sreg2.
1504 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1505 g_assert (new_dest >= 0);
1506 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1508 prev_dreg = ins->dreg;
1509 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1510 clob_dreg = ins->dreg;
1511 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1512 mono_regstate_free_int (rs, dest_sreg);
1516 if (is_global_ireg (sreg)) {
1517 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1518 insert_before_ins (bb, ins, copy);
1519 need_assign = FALSE;
1522 val = rs->vassign [sreg];
1523 if (val == dest_sreg) {
1524 /* sreg2 is already assigned to the correct register */
1526 } else if (val < -1) {
1527 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1528 } else if (val >= 0) {
1529 /* sreg2 already assigned to another register */
1531 * We couldn't emit a copy from val to dest_sreg2, because
1532 * val might be spilled later while processing this
1533 * instruction. So we spill sreg2 so it can be allocated to
1536 free_up_hreg (cfg, bb, tmp, ins, val, 0);
1541 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1545 if (rs->vassign [sreg] < -1) {
1548 /* Need to emit a spill store */
1549 spill = - rs->vassign [sreg] - 1;
1550 create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, ins, bank);
1552 /* force-set sreg2 */
1553 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1556 sregs [j] = dest_sreg;
1558 mono_inst_set_src_registers (ins, sregs);
1563 bank = dreg_bank (spec);
1564 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1565 prev_dreg = ins->dreg;
1568 if (spec_dest == 'b') {
1570 * The dest reg is read by the instruction, not written, so
1571 * avoid allocating sreg1/sreg2 to the same reg.
1573 if (dest_sregs [0] != -1)
1574 dreg_mask &= ~ (regmask (dest_sregs [0]));
1575 for (j = 1; j < num_sregs; ++j) {
1576 if (dest_sregs [j] != -1)
1577 dreg_mask &= ~ (regmask (dest_sregs [j]));
1580 val = rs->vassign [ins->dreg];
1581 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1582 /* DREG is already allocated to a register needed for sreg1 */
1583 spill_vreg (cfg, bb, tmp, ins, ins->dreg, 0);
1588 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1589 * various complex situations.
1591 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1592 guint32 dreg2, dest_dreg2;
1594 g_assert (is_soft_reg (ins->dreg, bank));
1596 if (dest_dreg != -1) {
1597 if (rs->vassign [ins->dreg] != dest_dreg)
1598 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, 0);
1600 dreg2 = ins->dreg + 1;
1601 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1602 if (dest_dreg2 != -1) {
1603 if (rs->vassign [dreg2] != dest_dreg2)
1604 free_up_hreg (cfg, bb, tmp, ins, dest_dreg2, 0);
1609 if (dreg_fixed_mask) {
1611 if (is_global_ireg (ins->dreg)) {
1613 * The argument is already in a hard reg, but that reg is
1614 * not usable by this instruction, so allocate a new one.
1616 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1618 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1619 mono_regstate_free_int (rs, val);
1625 dreg_mask &= dreg_fixed_mask;
1628 if (is_soft_reg (ins->dreg, bank)) {
1629 val = rs->vassign [ins->dreg];
1634 /* the register gets spilled after this inst */
1637 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1638 assign_reg (cfg, rs, ins->dreg, val, bank);
1640 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, NULL, bank);
1643 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1647 /* Handle regpairs */
1648 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1649 int reg2 = prev_dreg + 1;
1652 g_assert (prev_dreg > -1);
1653 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1654 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1657 mask &= ~regmask (X86_ECX);
1659 val = rs->vassign [reg2];
1663 /* the register gets spilled after this inst */
1666 val = mono_regstate_alloc_int (rs, mask);
1668 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1670 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, NULL, bank);
1673 if (! (mask & (regmask (val)))) {
1674 val = mono_regstate_alloc_int (rs, mask);
1676 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1678 /* Reallocate hreg to the correct register */
1679 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1681 mono_regstate_free_int (rs, rs->vassign [reg2]);
1685 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1686 assign_reg (cfg, rs, reg2, val, bank);
1689 ins->backend.reg3 = val;
1691 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1692 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1693 mono_regstate_free_int (rs, val);
1697 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1699 * In theory, we could free up the hreg even if the vreg is alive,
1700 * but branches inside bblocks force us to assign the same hreg
1701 * to a vreg every time it is encountered.
1703 int dreg = rs->vassign [prev_dreg];
1704 g_assert (dreg >= 0);
1705 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1706 if (G_UNLIKELY (bank))
1707 mono_regstate_free_general (rs, dreg, bank);
1709 mono_regstate_free_int (rs, dreg);
1710 rs->vassign [prev_dreg] = -1;
1713 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1714 /* this instruction only outputs to dest_dreg, need to copy */
1715 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1716 ins->dreg = dest_dreg;
1718 if (G_UNLIKELY (bank)) {
1719 /* the register we need to free up may be used in another logical regbank
1720 * so do a translate just in case.
1722 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1723 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1724 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1727 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1728 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, bank);
1732 if (spec_dest == 'b') {
1734 * The dest reg is read by the instruction, not written, so
1735 * avoid allocating sreg1/sreg2 to the same reg.
1737 for (j = 0; j < num_sregs; ++j)
1738 if (!sreg_bank (j, spec))
1739 sreg_masks [j] &= ~ (regmask (ins->dreg));
1745 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1746 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1747 free_up_hreg (cfg, bb, tmp, ins, clob_reg, 0);
1750 if (spec [MONO_INST_CLOB] == 'c') {
1751 int j, s, dreg, dreg2, cur_bank;
1754 clob_mask = MONO_ARCH_CALLEE_REGS;
1756 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1758 * Need to avoid spilling the dreg since the dreg is not really
1759 * clobbered by the call.
1761 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1762 dreg = rs->vassign [prev_dreg];
1766 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1767 dreg2 = rs->vassign [prev_dreg + 1];
1771 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1773 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1774 if ((j != dreg) && (j != dreg2))
1775 free_up_hreg (cfg, bb, tmp, ins, j, 0);
1776 else if (rs->isymbolic [j])
1777 /* The hreg is assigned to the dreg of this instruction */
1778 rs->vassign [rs->isymbolic [j]] = -1;
1779 mono_regstate_free_int (rs, j);
1784 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1785 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1786 clob_mask = regbank_callee_regs [cur_bank];
1787 if ((prev_dreg != -1) && reg_bank (spec_dest))
1788 dreg = rs->vassign [prev_dreg];
1792 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1794 /* we are looping though the banks in the outer loop
1795 * so, we don't need to deal with mirrored hregs
1796 * because we will get them in one of the other bank passes.
1798 if (is_hreg_mirrored (rs, cur_bank, j))
1802 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s)) {
1804 free_up_hreg (cfg, bb, tmp, ins, j, cur_bank);
1805 else if (rs->symbolic [cur_bank] [j])
1806 /* The hreg is assigned to the dreg of this instruction */
1807 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1808 mono_regstate_free_general (rs, j, cur_bank);
1816 * TRACK ARGUMENT REGS
1818 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1819 MonoCallInst *call = (MonoCallInst*)ins;
1823 * This needs to be done before assigning sreg1, so sreg1 will
1824 * not be assigned one of the argument regs.
1828 * Assign all registers in call->out_reg_args to the proper
1829 * argument registers.
1832 list = call->out_ireg_args;
1838 regpair = (guint32)(gssize)(list->data);
1839 hreg = regpair >> 24;
1840 reg = regpair & 0xffffff;
1842 assign_reg (cfg, rs, reg, hreg, 0);
1844 sreg_masks [0] &= ~(regmask (hreg));
1846 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1848 list = g_slist_next (list);
1852 list = call->out_freg_args;
1858 regpair = (guint32)(gssize)(list->data);
1859 hreg = regpair >> 24;
1860 reg = regpair & 0xffffff;
1862 assign_reg (cfg, rs, reg, hreg, 1);
1864 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1866 list = g_slist_next (list);
1874 bank = sreg1_bank (spec);
1875 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1876 int sreg1 = sregs [0];
1877 int dest_sreg1 = dest_sregs [0];
1879 g_assert (is_soft_reg (sreg1, bank));
1881 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1882 if (dest_sreg1 != -1)
1883 g_assert (dest_sreg1 == ins->dreg);
1884 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1885 g_assert (val >= 0);
1887 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1889 g_assert_not_reached ();
1891 assign_reg (cfg, rs, sreg1, val, bank);
1893 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1895 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1896 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1897 g_assert (val >= 0);
1899 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1901 g_assert_not_reached ();
1903 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1905 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1907 /* Skip rest of this section */
1908 dest_sregs [0] = -1;
1911 if (sreg_fixed_masks [0]) {
1913 if (is_global_ireg (sregs [0])) {
1915 * The argument is already in a hard reg, but that reg is
1916 * not usable by this instruction, so allocate a new one.
1918 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1920 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1921 mono_regstate_free_int (rs, val);
1922 dest_sregs [0] = val;
1924 /* Fall through to the dest_sreg1 != -1 case */
1927 sreg_masks [0] &= sreg_fixed_masks [0];
1930 if (dest_sregs [0] != -1) {
1931 sreg_masks [0] = regmask (dest_sregs [0]);
1933 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1934 free_up_hreg (cfg, bb, tmp, ins, dest_sregs [0], 0);
1936 if (is_global_ireg (sregs [0])) {
1937 /* The argument is already in a hard reg, need to copy */
1938 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1939 insert_before_ins (bb, ins, copy);
1940 sregs [0] = dest_sregs [0];
1944 if (is_soft_reg (sregs [0], bank)) {
1945 val = rs->vassign [sregs [0]];
1946 prev_sregs [0] = sregs [0];
1950 /* the register gets spilled after this inst */
1954 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1956 * Allocate the same hreg to sreg1 as well so the
1957 * peephole can get rid of the move.
1959 sreg_masks [0] = regmask (ins->dreg);
1962 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1963 /* Allocate the same reg to sreg1 to avoid a copy later */
1964 sreg_masks [0] = regmask (ins->dreg);
1966 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
1967 assign_reg (cfg, rs, sregs [0], val, bank);
1968 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
1972 * Need to insert before the instruction since it can
1975 create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, ins, bank);
1978 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
1979 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
1980 insert_before_ins (bb, ins, copy);
1981 for (j = 1; j < num_sregs; ++j)
1982 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
1983 val = dest_sregs [0];
1989 prev_sregs [0] = -1;
1991 mono_inst_set_src_registers (ins, sregs);
1993 for (j = 1; j < num_sregs; ++j)
1994 sreg_masks [j] &= ~(regmask (sregs [0]));
1996 /* Handle the case when sreg1 is a regpair but dreg is not */
1997 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
1998 int reg2 = prev_sregs [0] + 1;
2001 g_assert (prev_sregs [0] > -1);
2002 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
2003 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
2004 val = rs->vassign [reg2];
2008 /* the register gets spilled after this inst */
2011 val = mono_regstate_alloc_int (rs, mask);
2013 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2015 g_assert_not_reached ();
2018 if (! (mask & (regmask (val)))) {
2019 /* The vreg is already allocated to a wrong hreg */
2021 g_assert_not_reached ();
2023 val = mono_regstate_alloc_int (rs, mask);
2025 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2027 /* Reallocate hreg to the correct register */
2028 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
2030 mono_regstate_free_int (rs, rs->vassign [reg2]);
2036 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
2037 assign_reg (cfg, rs, reg2, val, bank);
2040 /* Handle dreg==sreg1 */
2041 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
2042 MonoInst *sreg2_copy = NULL;
2044 int bank = reg_bank (spec_src1);
2046 if (ins->dreg == sregs [1]) {
2048 * copying sreg1 to dreg could clobber sreg2, so allocate a new
2051 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
2053 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
2054 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
2055 prev_sregs [1] = sregs [1] = reg2;
2057 if (G_UNLIKELY (bank))
2058 mono_regstate_free_general (rs, reg2, bank);
2060 mono_regstate_free_int (rs, reg2);
2063 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
2064 /* Copying sreg1_high to dreg could also clobber sreg2 */
2065 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
2067 g_assert_not_reached ();
2070 * sreg1 and dest are already allocated to the same regpair by the
2071 * SREG1 allocation code.
2073 g_assert (sregs [0] == ins->dreg);
2074 g_assert (dreg_high == sreg1_high);
2077 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
2078 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
2079 insert_before_ins (bb, ins, copy);
2082 insert_before_ins (bb, copy, sreg2_copy);
2085 * Need to prevent sreg2 to be allocated to sreg1, since that
2086 * would screw up the previous copy.
2088 sreg_masks [1] &= ~ (regmask (sregs [0]));
2089 /* we set sreg1 to dest as well */
2090 prev_sregs [0] = sregs [0] = ins->dreg;
2091 sreg_masks [1] &= ~ (regmask (ins->dreg));
2093 mono_inst_set_src_registers (ins, sregs);
2096 * TRACK SREG2, 3, ...
2098 for (j = 1; j < num_sregs; ++j) {
2101 bank = sreg_bank (j, spec);
2102 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
2103 g_assert_not_reached ();
2105 if (dest_sregs [j] != -1 && is_global_ireg (sregs [j])) {
2107 * Argument already in a global hard reg, copy it to the fixed reg, without
2108 * allocating it to the fixed reg.
2110 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [j], sregs [j], NULL, ip, 0);
2111 insert_before_ins (bb, ins, copy);
2112 sregs [j] = dest_sregs [j];
2113 } else if (is_soft_reg (sregs [j], bank)) {
2114 val = rs->vassign [sregs [j]];
2116 if (dest_sregs [j] != -1 && val >= 0 && dest_sregs [j] != val) {
2118 * The sreg is already allocated to a hreg, but not to the fixed
2119 * reg required by the instruction. Spill the sreg, so it can be
2120 * allocated to the fixed reg by the code below.
2122 /* Currently, this code should only be hit for CAS */
2123 spill_vreg (cfg, bb, tmp, ins, sregs [j], 0);
2124 val = rs->vassign [sregs [j]];
2130 /* the register gets spilled after this inst */
2133 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
2134 assign_reg (cfg, rs, sregs [j], val, bank);
2135 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
2138 * Need to insert before the instruction since it can
2141 create_spilled_store (cfg, bb, spill, val, sregs [j], tmp, NULL, ins, bank);
2145 for (k = j + 1; k < num_sregs; ++k)
2146 sreg_masks [k] &= ~ (regmask (sregs [j]));
2149 prev_sregs [j] = -1;
2152 mono_inst_set_src_registers (ins, sregs);
2155 /* Do this only for CAS for now */
2156 for (j = 1; j < num_sregs; ++j) {
2157 int sreg = sregs [j];
2158 int dest_sreg = dest_sregs [j];
2160 if (j == 2 && dest_sreg != -1) {
2163 g_assert (sreg == dest_sreg);
2165 for (k = 0; k < num_sregs; ++k) {
2167 g_assert (sregs [k] != dest_sreg);
2172 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2173 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2174 mono_regstate_free_int (rs, ins->sreg1);
2176 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2177 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2178 mono_regstate_free_int (rs, ins->sreg2);
2181 DEBUG (mono_print_ins_index (i, ins));
2184 // FIXME: Set MAX_FREGS to 8
2185 // FIXME: Optimize generated code
2186 #if MONO_ARCH_USE_FPSTACK
2188 * Make a forward pass over the code, simulating the fp stack, making sure the
2189 * arguments required by the fp opcodes are at the top of the stack.
2192 MonoInst *prev = NULL;
2196 g_assert (num_sregs <= 2);
2198 for (ins = bb->code; ins; ins = ins->next) {
2199 spec = ins_get_spec (ins->opcode);
2201 DEBUG (printf ("processing:"));
2202 DEBUG (mono_print_ins_index (0, ins));
2204 if (ins->opcode == OP_FMOVE) {
2205 /* Do it by renaming the source to the destination on the stack */
2206 // FIXME: Is this correct ?
2207 for (i = 0; i < sp; ++i)
2208 if (fpstack [i] == ins->sreg1)
2209 fpstack [i] = ins->dreg;
2214 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2215 /* Arg1 must be in %st(1) */
2219 while ((i < sp) && (fpstack [i] != ins->sreg1))
2223 if (sp - 1 - i > 0) {
2224 /* First move it to %st(0) */
2225 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2227 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2228 fxch->inst_imm = sp - 1 - i;
2230 mono_bblock_insert_after_ins (bb, prev, fxch);
2233 tmp = fpstack [sp - 1];
2234 fpstack [sp - 1] = fpstack [i];
2238 /* Then move it to %st(1) */
2239 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2241 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2244 mono_bblock_insert_after_ins (bb, prev, fxch);
2247 tmp = fpstack [sp - 1];
2248 fpstack [sp - 1] = fpstack [sp - 2];
2249 fpstack [sp - 2] = tmp;
2252 if (sreg2_is_fp (spec)) {
2255 if (fpstack [sp - 1] != ins->sreg2) {
2259 while ((i < sp) && (fpstack [i] != ins->sreg2))
2263 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2265 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2266 fxch->inst_imm = sp - 1 - i;
2268 mono_bblock_insert_after_ins (bb, prev, fxch);
2271 tmp = fpstack [sp - 1];
2272 fpstack [sp - 1] = fpstack [i];
2279 if (sreg1_is_fp (spec)) {
2282 if (fpstack [sp - 1] != ins->sreg1) {
2286 while ((i < sp) && (fpstack [i] != ins->sreg1))
2290 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2292 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2293 fxch->inst_imm = sp - 1 - i;
2295 mono_bblock_insert_after_ins (bb, prev, fxch);
2298 tmp = fpstack [sp - 1];
2299 fpstack [sp - 1] = fpstack [i];
2306 if (dreg_is_fp (spec)) {
2308 fpstack [sp ++] = ins->dreg;
2311 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2313 for (i = 0; i < sp; ++i)
2314 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2321 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2322 /* Remove remaining items from the fp stack */
2324 * These can remain for example as a result of a dead fmove like in
2325 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2328 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2329 mono_add_ins_to_end (bb, ins);
2338 mono_opcode_to_cond (int opcode)
2348 case OP_COND_EXC_EQ:
2349 case OP_COND_EXC_IEQ:
2358 case OP_COND_EXC_NE_UN:
2359 case OP_COND_EXC_INE_UN:
2360 case OP_CMOV_INE_UN:
2361 case OP_CMOV_LNE_UN:
2386 case OP_COND_EXC_LT:
2387 case OP_COND_EXC_ILT:
2398 case OP_COND_EXC_GT:
2399 case OP_COND_EXC_IGT:
2408 case OP_COND_EXC_LE_UN:
2409 case OP_COND_EXC_ILE_UN:
2410 case OP_CMOV_ILE_UN:
2411 case OP_CMOV_LLE_UN:
2418 case OP_CMOV_IGE_UN:
2419 case OP_CMOV_LGE_UN:
2428 case OP_COND_EXC_LT_UN:
2429 case OP_COND_EXC_ILT_UN:
2430 case OP_CMOV_ILT_UN:
2431 case OP_CMOV_LLT_UN:
2440 case OP_COND_EXC_GT_UN:
2441 case OP_COND_EXC_IGT_UN:
2442 case OP_CMOV_IGT_UN:
2443 case OP_CMOV_LGT_UN:
2446 printf ("%s\n", mono_inst_name (opcode));
2447 g_assert_not_reached ();
2453 mono_negate_cond (CompRelation cond)
2477 g_assert_not_reached ();
2482 mono_opcode_to_type (int opcode, int cmp_opcode)
2484 if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2486 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2488 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2490 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2492 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2494 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2496 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2498 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2500 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2501 switch (cmp_opcode) {
2503 case OP_ICOMPARE_IMM:
2509 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2514 #endif /* DISABLE_JIT */
2517 mono_is_regsize_var (MonoType *t)
2521 t = mono_type_get_underlying_type (t);
2523 case MONO_TYPE_BOOLEAN:
2524 case MONO_TYPE_CHAR:
2534 case MONO_TYPE_FNPTR:
2535 #if SIZEOF_REGISTER == 8
2540 case MONO_TYPE_OBJECT:
2541 case MONO_TYPE_STRING:
2542 case MONO_TYPE_CLASS:
2543 case MONO_TYPE_SZARRAY:
2544 case MONO_TYPE_ARRAY:
2546 case MONO_TYPE_GENERICINST:
2547 if (!mono_type_generic_inst_is_valuetype (t))
2550 case MONO_TYPE_VALUETYPE:
2560 * mono_peephole_ins:
2562 * Perform some architecture independent peephole optimizations.
2565 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2567 MonoInst *last_ins = ins->prev;
2569 switch (ins->opcode) {
2571 /* remove unnecessary multiplication with 1 */
2572 if (ins->inst_imm == 1) {
2573 if (ins->dreg != ins->sreg1)
2574 ins->opcode = OP_MOVE;
2576 MONO_DELETE_INS (bb, ins);
2579 case OP_LOAD_MEMBASE:
2580 case OP_LOADI4_MEMBASE:
2582 * Note: if reg1 = reg2 the load op is removed
2584 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2585 * OP_LOAD_MEMBASE offset(basereg), reg2
2587 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2588 * OP_MOVE reg1, reg2
2590 if (last_ins && last_ins->opcode == OP_GC_LIVENESS_DEF)
2591 last_ins = last_ins->prev;
2593 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2594 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2595 ins->inst_basereg == last_ins->inst_destbasereg &&
2596 ins->inst_offset == last_ins->inst_offset) {
2597 if (ins->dreg == last_ins->sreg1) {
2598 MONO_DELETE_INS (bb, ins);
2601 ins->opcode = OP_MOVE;
2602 ins->sreg1 = last_ins->sreg1;
2606 * Note: reg1 must be different from the basereg in the second load
2607 * Note: if reg1 = reg2 is equal then second load is removed
2609 * OP_LOAD_MEMBASE offset(basereg), reg1
2610 * OP_LOAD_MEMBASE offset(basereg), reg2
2612 * OP_LOAD_MEMBASE offset(basereg), reg1
2613 * OP_MOVE reg1, reg2
2615 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2616 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2617 ins->inst_basereg != last_ins->dreg &&
2618 ins->inst_basereg == last_ins->inst_basereg &&
2619 ins->inst_offset == last_ins->inst_offset) {
2621 if (ins->dreg == last_ins->dreg) {
2622 MONO_DELETE_INS (bb, ins);
2624 ins->opcode = OP_MOVE;
2625 ins->sreg1 = last_ins->dreg;
2628 //g_assert_not_reached ();
2632 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2633 * OP_LOAD_MEMBASE offset(basereg), reg
2635 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2636 * OP_ICONST reg, imm
2638 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2639 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2640 ins->inst_basereg == last_ins->inst_destbasereg &&
2641 ins->inst_offset == last_ins->inst_offset) {
2642 ins->opcode = OP_ICONST;
2643 ins->inst_c0 = last_ins->inst_imm;
2644 g_assert_not_reached (); // check this rule
2648 case OP_LOADI1_MEMBASE:
2649 case OP_LOADU1_MEMBASE:
2651 * Note: if reg1 = reg2 the load op is removed
2653 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2654 * OP_LOAD_MEMBASE offset(basereg), reg2
2656 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2657 * OP_MOVE reg1, reg2
2659 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2660 ins->inst_basereg == last_ins->inst_destbasereg &&
2661 ins->inst_offset == last_ins->inst_offset) {
2662 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2663 ins->sreg1 = last_ins->sreg1;
2666 case OP_LOADI2_MEMBASE:
2667 case OP_LOADU2_MEMBASE:
2669 * Note: if reg1 = reg2 the load op is removed
2671 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2672 * OP_LOAD_MEMBASE offset(basereg), reg2
2674 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2675 * OP_MOVE reg1, reg2
2677 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2678 ins->inst_basereg == last_ins->inst_destbasereg &&
2679 ins->inst_offset == last_ins->inst_offset) {
2680 #if SIZEOF_REGISTER == 8
2681 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2683 /* The definition of OP_PCONV_TO_U2 is wrong */
2684 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2686 ins->sreg1 = last_ins->sreg1;
2696 if (ins->dreg == ins->sreg1) {
2697 MONO_DELETE_INS (bb, ins);
2703 * OP_MOVE sreg, dreg
2704 * OP_MOVE dreg, sreg
2706 if (last_ins && last_ins->opcode == ins->opcode &&
2707 ins->sreg1 == last_ins->dreg &&
2708 ins->dreg == last_ins->sreg1) {
2709 MONO_DELETE_INS (bb, ins);
2713 MONO_DELETE_INS (bb, ins);
2719 mini_exception_id_by_name (const char *name)
2721 if (strcmp (name, "IndexOutOfRangeException") == 0)
2722 return MONO_EXC_INDEX_OUT_OF_RANGE;
2723 if (strcmp (name, "OverflowException") == 0)
2724 return MONO_EXC_OVERFLOW;
2725 if (strcmp (name, "ArithmeticException") == 0)
2726 return MONO_EXC_ARITHMETIC;
2727 if (strcmp (name, "DivideByZeroException") == 0)
2728 return MONO_EXC_DIVIDE_BY_ZERO;
2729 if (strcmp (name, "InvalidCastException") == 0)
2730 return MONO_EXC_INVALID_CAST;
2731 if (strcmp (name, "NullReferenceException") == 0)
2732 return MONO_EXC_NULL_REF;
2733 if (strcmp (name, "ArrayTypeMismatchException") == 0)
2734 return MONO_EXC_ARRAY_TYPE_MISMATCH;
2735 if (strcmp (name, "ArgumentException") == 0)
2736 return MONO_EXC_ARGUMENT;
2737 g_error ("Unknown intrinsic exception %s\n", name);
2741 #endif /* DISABLE_JIT */