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 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
105 static const regmask_t regbank_callee_saved_regs [] = {
106 MONO_ARCH_CALLEE_SAVED_REGS,
107 MONO_ARCH_CALLEE_SAVED_FREGS,
108 MONO_ARCH_CALLEE_SAVED_REGS,
109 MONO_ARCH_CALLEE_SAVED_REGS,
110 MONO_ARCH_CALLEE_SAVED_XREGS,
114 static const regmask_t regbank_callee_regs [] = {
115 MONO_ARCH_CALLEE_REGS,
116 MONO_ARCH_CALLEE_FREGS,
117 MONO_ARCH_CALLEE_REGS,
118 MONO_ARCH_CALLEE_REGS,
119 MONO_ARCH_CALLEE_XREGS,
122 static const int regbank_spill_var_size[] = {
127 16 /*FIXME make this a constant. Maybe MONO_ARCH_SIMD_VECTOR_SIZE? */
130 #define DEBUG(a) MINI_DEBUG(cfg->verbose_level, 3, a;)
133 mono_regstate_assign (MonoRegState *rs)
135 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
136 /* The regalloc may fail if fp and simd logical regbanks share the same physical reg bank and
137 * if the values here are not the same.
139 g_assert(regbank_callee_regs [MONO_REG_SIMD] == regbank_callee_regs [MONO_REG_DOUBLE]);
140 g_assert(regbank_callee_saved_regs [MONO_REG_SIMD] == regbank_callee_saved_regs [MONO_REG_DOUBLE]);
141 g_assert(regbank_size [MONO_REG_SIMD] == regbank_size [MONO_REG_DOUBLE]);
144 if (rs->next_vreg > rs->vassign_size) {
145 g_free (rs->vassign);
146 rs->vassign_size = MAX (rs->next_vreg, 256);
147 rs->vassign = (gint32 *)g_malloc (rs->vassign_size * sizeof (gint32));
150 memset (rs->isymbolic, 0, MONO_MAX_IREGS * sizeof (rs->isymbolic [0]));
151 memset (rs->fsymbolic, 0, MONO_MAX_FREGS * sizeof (rs->fsymbolic [0]));
153 rs->symbolic [MONO_REG_INT] = rs->isymbolic;
154 rs->symbolic [MONO_REG_DOUBLE] = rs->fsymbolic;
156 #ifdef MONO_ARCH_NEED_SIMD_BANK
157 memset (rs->xsymbolic, 0, MONO_MAX_XREGS * sizeof (rs->xsymbolic [0]));
158 rs->symbolic [MONO_REG_SIMD] = rs->xsymbolic;
163 mono_regstate_alloc_int (MonoRegState *rs, regmask_t allow)
165 regmask_t mask = allow & rs->ifree_mask;
167 #if defined(__x86_64__) && defined(__GNUC__)
174 __asm__("bsfq %1,%0\n\t"
175 : "=r" (i) : "rm" (mask));
177 rs->ifree_mask &= ~ ((regmask_t)1 << i);
183 for (i = 0; i < MONO_MAX_IREGS; ++i) {
184 if (mask & ((regmask_t)1 << i)) {
185 rs->ifree_mask &= ~ ((regmask_t)1 << i);
194 mono_regstate_free_int (MonoRegState *rs, int reg)
197 rs->ifree_mask |= (regmask_t)1 << reg;
198 rs->isymbolic [reg] = 0;
203 mono_regstate_alloc_general (MonoRegState *rs, regmask_t allow, int bank)
207 regmask_t mask = allow & rs->free_mask [bank];
208 for (i = 0; i < regbank_size [bank]; ++i) {
209 if (mask & ((regmask_t)1 << i)) {
210 rs->free_mask [bank] &= ~ ((regmask_t)1 << i);
212 mirrored_bank = get_mirrored_bank (bank);
213 if (mirrored_bank == -1)
216 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
224 mono_regstate_free_general (MonoRegState *rs, int reg, int bank)
229 rs->free_mask [bank] |= (regmask_t)1 << reg;
230 rs->symbolic [bank][reg] = 0;
232 mirrored_bank = get_mirrored_bank (bank);
233 if (mirrored_bank == -1)
235 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
236 rs->symbolic [mirrored_bank][reg] = 0;
241 mono_regname_full (int reg, int bank)
243 if (G_UNLIKELY (bank)) {
244 #if MONO_ARCH_NEED_SIMD_BANK
245 if (bank == MONO_REG_SIMD)
246 return mono_arch_xregname (reg);
248 if (bank == MONO_REG_INT_REF || bank == MONO_REG_INT_MP)
249 return mono_arch_regname (reg);
250 g_assert (bank == MONO_REG_DOUBLE);
251 return mono_arch_fregname (reg);
253 return mono_arch_regname (reg);
258 mono_call_inst_add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, int bank)
262 regpair = (((guint32)hreg) << 24) + vreg;
263 if (G_UNLIKELY (bank)) {
264 g_assert (vreg >= regbank_size [bank]);
265 g_assert (hreg < regbank_size [bank]);
266 call->used_fregs |= 1 << hreg;
267 call->out_freg_args = g_slist_append_mempool (cfg->mempool, call->out_freg_args, (gpointer)(gssize)(regpair));
269 g_assert (vreg >= MONO_MAX_IREGS);
270 g_assert (hreg < MONO_MAX_IREGS);
271 call->used_iregs |= 1 << hreg;
272 call->out_ireg_args = g_slist_append_mempool (cfg->mempool, call->out_ireg_args, (gpointer)(gssize)(regpair));
277 * mono_call_inst_add_outarg_vt:
279 * Register OUTARG_VT as belonging to CALL.
282 mono_call_inst_add_outarg_vt (MonoCompile *cfg, MonoCallInst *call, MonoInst *outarg_vt)
284 call->outarg_vts = g_slist_append_mempool (cfg->mempool, call->outarg_vts, outarg_vt);
288 resize_spill_info (MonoCompile *cfg, int bank)
290 MonoSpillInfo *orig_info = cfg->spill_info [bank];
291 int orig_len = cfg->spill_info_len [bank];
292 int new_len = orig_len ? orig_len * 2 : 16;
293 MonoSpillInfo *new_info;
296 g_assert (bank < MONO_NUM_REGBANKS);
298 new_info = (MonoSpillInfo *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoSpillInfo) * new_len);
300 memcpy (new_info, orig_info, sizeof (MonoSpillInfo) * orig_len);
301 for (i = orig_len; i < new_len; ++i)
302 new_info [i].offset = -1;
304 cfg->spill_info [bank] = new_info;
305 cfg->spill_info_len [bank] = new_len;
309 * returns the offset used by spillvar. It allocates a new
310 * spill variable if necessary.
313 mono_spillvar_offset (MonoCompile *cfg, int spillvar, int bank)
318 if (G_UNLIKELY (spillvar >= (cfg->spill_info_len [bank]))) {
319 while (spillvar >= cfg->spill_info_len [bank])
320 resize_spill_info (cfg, bank);
324 * Allocate separate spill slots for fp/non-fp variables since most processors prefer it.
326 info = &cfg->spill_info [bank][spillvar];
327 if (info->offset == -1) {
328 cfg->stack_offset += sizeof (mgreg_t) - 1;
329 cfg->stack_offset &= ~(sizeof (mgreg_t) - 1);
331 g_assert (bank < MONO_NUM_REGBANKS);
332 if (G_UNLIKELY (bank))
333 size = regbank_spill_var_size [bank];
335 size = sizeof (mgreg_t);
337 if (cfg->flags & MONO_CFG_HAS_SPILLUP) {
338 cfg->stack_offset += size - 1;
339 cfg->stack_offset &= ~(size - 1);
340 info->offset = cfg->stack_offset;
341 cfg->stack_offset += size;
343 cfg->stack_offset += size - 1;
344 cfg->stack_offset &= ~(size - 1);
345 cfg->stack_offset += size;
346 info->offset = - cfg->stack_offset;
353 #define is_hard_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS)
354 #define is_hard_freg(r) ((r) >= 0 && (r) < MONO_MAX_FREGS)
355 #define is_global_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_SAVED_REGS & (regmask (r))))
356 #define is_local_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_REGS & (regmask (r))))
357 #define is_global_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_SAVED_FREGS & (regmask (r))))
358 #define is_local_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_FREGS & (regmask (r))))
360 #define is_hard_reg(r,bank) (G_UNLIKELY (bank) ? ((r) >= 0 && (r) < regbank_size [bank]) : ((r) < MONO_MAX_IREGS))
361 #define is_soft_reg(r,bank) (!is_hard_reg((r),(bank)))
362 #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))
363 #define is_local_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_regs [bank] & regmask (r))) : is_local_ireg (r))
364 #define reg_is_freeable(r,bank) (G_UNLIKELY (bank) ? is_local_reg ((r), (bank)) : is_local_ireg ((r)))
366 #ifndef MONO_ARCH_INST_IS_FLOAT
367 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc) == 'f')
370 #define reg_is_fp(desc) (MONO_ARCH_INST_IS_FLOAT (desc))
371 #define dreg_is_fp(spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_DEST]))
372 #define sreg_is_fp(n,spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_SRC1+(n)]))
373 #define sreg1_is_fp(spec) sreg_is_fp (0,(spec))
374 #define sreg2_is_fp(spec) sreg_is_fp (1,(spec))
376 #define reg_is_simd(desc) ((desc) == 'x')
378 #ifdef MONO_ARCH_NEED_SIMD_BANK
380 #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)
384 #define reg_bank(desc) reg_is_fp ((desc))
388 #define sreg_bank(n,spec) reg_bank ((spec)[MONO_INST_SRC1+(n)])
389 #define sreg1_bank(spec) sreg_bank (0, (spec))
390 #define sreg2_bank(spec) sreg_bank (1, (spec))
391 #define dreg_bank(spec) reg_bank ((spec)[MONO_INST_DEST])
393 #define sreg_bank_ins(n,ins) sreg_bank ((n), ins_get_spec ((ins)->opcode))
394 #define sreg1_bank_ins(ins) sreg_bank_ins (0, (ins))
395 #define sreg2_bank_ins(ins) sreg_bank_ins (1, (ins))
396 #define dreg_bank_ins(ins) dreg_bank (ins_get_spec ((ins)->opcode))
398 #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)
400 #ifdef MONO_ARCH_IS_GLOBAL_IREG
401 #undef is_global_ireg
402 #define is_global_ireg(reg) MONO_ARCH_IS_GLOBAL_IREG ((reg))
411 regmask_t preferred_mask; /* the hreg where the register should be allocated, or 0 */
414 #if !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT)
416 static const char* const patch_info_str[] = {
417 #define PATCH_INFO(a,b) "" #a,
418 #include "patch-info.h"
423 mono_ji_type_to_string (MonoJumpInfoType type)
425 return patch_info_str [type];
429 mono_print_ji (const MonoJumpInfo *ji)
432 case MONO_PATCH_INFO_RGCTX_FETCH: {
433 MonoJumpInfoRgctxEntry *entry = ji->data.rgctx_entry;
435 printf ("[RGCTX_FETCH ");
436 mono_print_ji (entry->data);
437 printf (" - %s]", mono_rgctx_info_type_to_str (entry->info_type));
440 case MONO_PATCH_INFO_METHODCONST: {
441 char *s = mono_method_full_name (ji->data.method, TRUE);
442 printf ("[METHODCONST - %s]", s);
446 case MONO_PATCH_INFO_INTERNAL_METHOD: {
447 printf ("[INTERNAL_METHOD - %s]", ji->data.name);
451 printf ("[%s]", patch_info_str [ji->type]);
457 mono_print_ins_index (int i, MonoInst *ins)
459 GString *buf = mono_print_ins_index_strbuf (i, ins);
460 printf ("%s\n", buf->str);
461 g_string_free (buf, TRUE);
465 mono_print_ins_index_strbuf (int i, MonoInst *ins)
467 const char *spec = ins_get_spec (ins->opcode);
468 GString *sbuf = g_string_new (NULL);
470 int sregs [MONO_MAX_SRC_REGS];
473 g_string_append_printf (sbuf, "\t%-2d %s", i, mono_inst_name (ins->opcode));
475 g_string_append_printf (sbuf, " %s", mono_inst_name (ins->opcode));
476 if (spec == MONO_ARCH_CPU_SPEC) {
477 gboolean dest_base = FALSE;
478 switch (ins->opcode) {
479 case OP_STOREV_MEMBASE:
486 /* This is a lowered opcode */
487 if (ins->dreg != -1) {
489 g_string_append_printf (sbuf, " [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
491 g_string_append_printf (sbuf, " R%d <-", ins->dreg);
493 if (ins->sreg1 != -1)
494 g_string_append_printf (sbuf, " R%d", ins->sreg1);
495 if (ins->sreg2 != -1)
496 g_string_append_printf (sbuf, " R%d", ins->sreg2);
497 if (ins->sreg3 != -1)
498 g_string_append_printf (sbuf, " R%d", ins->sreg3);
500 switch (ins->opcode) {
511 if (!ins->inst_false_bb)
512 g_string_append_printf (sbuf, " [B%d]", ins->inst_true_bb->block_num);
514 g_string_append_printf (sbuf, " [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
521 g_string_append_printf (sbuf, " [%d (", (int)ins->inst_c0);
522 for (i = 0; i < ins->inst_phi_args [0]; i++) {
524 g_string_append_printf (sbuf, ", ");
525 g_string_append_printf (sbuf, "R%d", ins->inst_phi_args [i + 1]);
527 g_string_append_printf (sbuf, ")]");
531 case OP_OUTARG_VTRETADDR:
532 g_string_append_printf (sbuf, " R%d", ((MonoInst*)ins->inst_p0)->dreg);
535 case OP_GSHAREDVT_ARG_REGOFFSET:
536 g_string_append_printf (sbuf, " + 0x%lx", (long)ins->inst_offset);
542 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
546 if (spec [MONO_INST_DEST]) {
547 int bank = dreg_bank (spec);
548 if (is_soft_reg (ins->dreg, bank)) {
549 if (spec [MONO_INST_DEST] == 'b') {
550 if (ins->inst_offset == 0)
551 g_string_append_printf (sbuf, " [R%d] <-", ins->dreg);
553 g_string_append_printf (sbuf, " [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
556 g_string_append_printf (sbuf, " R%d <-", ins->dreg);
557 } else if (spec [MONO_INST_DEST] == 'b') {
558 if (ins->inst_offset == 0)
559 g_string_append_printf (sbuf, " [%s] <-", mono_arch_regname (ins->dreg));
561 g_string_append_printf (sbuf, " [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
563 g_string_append_printf (sbuf, " %s <-", mono_regname_full (ins->dreg, bank));
565 if (spec [MONO_INST_SRC1]) {
566 int bank = sreg1_bank (spec);
567 if (is_soft_reg (ins->sreg1, bank)) {
568 if (spec [MONO_INST_SRC1] == 'b')
569 g_string_append_printf (sbuf, " [R%d + 0x%lx]", ins->sreg1, (long)ins->inst_offset);
571 g_string_append_printf (sbuf, " R%d", ins->sreg1);
572 } else if (spec [MONO_INST_SRC1] == 'b')
573 g_string_append_printf (sbuf, " [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
575 g_string_append_printf (sbuf, " %s", mono_regname_full (ins->sreg1, bank));
577 num_sregs = mono_inst_get_src_registers (ins, sregs);
578 for (j = 1; j < num_sregs; ++j) {
579 int bank = sreg_bank (j, spec);
580 if (is_soft_reg (sregs [j], bank))
581 g_string_append_printf (sbuf, " R%d", sregs [j]);
583 g_string_append_printf (sbuf, " %s", mono_regname_full (sregs [j], bank));
586 switch (ins->opcode) {
588 g_string_append_printf (sbuf, " [%d]", (int)ins->inst_c0);
590 #if defined(TARGET_X86) || defined(TARGET_AMD64)
591 case OP_X86_PUSH_IMM:
593 case OP_ICOMPARE_IMM:
601 case OP_STORE_MEMBASE_IMM:
602 g_string_append_printf (sbuf, " [%d]", (int)ins->inst_imm);
606 g_string_append_printf (sbuf, " [%d]", (int)(gssize)ins->inst_p1);
609 g_string_append_printf (sbuf, " [%lld]", (long long)ins->inst_l);
612 g_string_append_printf (sbuf, " [%f]", *(double*)ins->inst_p0);
615 g_string_append_printf (sbuf, " [%f]", *(float*)ins->inst_p0);
618 case OP_CALL_MEMBASE:
624 case OP_VCALL_MEMBASE:
627 case OP_VCALL2_MEMBASE:
629 case OP_VOIDCALL_MEMBASE:
631 MonoCallInst *call = (MonoCallInst*)ins;
634 if (ins->opcode == OP_VCALL || ins->opcode == OP_VCALL_REG || ins->opcode == OP_VCALL_MEMBASE) {
636 * These are lowered opcodes, but they are in the .md files since the old
637 * JIT passes them to backends.
640 g_string_append_printf (sbuf, " R%d <-", ins->dreg);
644 char *full_name = mono_method_full_name (call->method, TRUE);
645 g_string_append_printf (sbuf, " [%s]", full_name);
647 } else if (call->fptr_is_patch) {
648 MonoJumpInfo *ji = (MonoJumpInfo*)call->fptr;
650 g_string_append_printf (sbuf, " ");
652 } else if (call->fptr) {
653 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
655 g_string_append_printf (sbuf, " [%s]", info->name);
658 list = call->out_ireg_args;
663 regpair = (guint32)(gssize)(list->data);
664 hreg = regpair >> 24;
665 reg = regpair & 0xffffff;
667 g_string_append_printf (sbuf, " [%s <- R%d]", mono_arch_regname (hreg), reg);
669 list = g_slist_next (list);
671 list = call->out_freg_args;
676 regpair = (guint32)(gssize)(list->data);
677 hreg = regpair >> 24;
678 reg = regpair & 0xffffff;
680 g_string_append_printf (sbuf, " [%s <- R%d]", mono_arch_fregname (hreg), reg);
682 list = g_slist_next (list);
687 case OP_CALL_HANDLER:
688 g_string_append_printf (sbuf, " [B%d]", ins->inst_target_bb->block_num);
710 if (!ins->inst_false_bb)
711 g_string_append_printf (sbuf, " [B%d]", ins->inst_true_bb->block_num);
713 g_string_append_printf (sbuf, " [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
715 case OP_LIVERANGE_START:
716 case OP_LIVERANGE_END:
717 case OP_GC_LIVENESS_DEF:
718 case OP_GC_LIVENESS_USE:
719 g_string_append_printf (sbuf, " R%d", (int)ins->inst_c1);
721 case OP_IL_SEQ_POINT:
723 g_string_append_printf (sbuf, " il: 0x%x%s", (int)ins->inst_imm, ins->flags & MONO_INST_NONEMPTY_STACK ? ", nonempty-stack" : "");
730 case OP_COND_EXC_NE_UN:
731 case OP_COND_EXC_GE_UN:
732 case OP_COND_EXC_GT_UN:
733 case OP_COND_EXC_LE_UN:
734 case OP_COND_EXC_LT_UN:
739 case OP_COND_EXC_IEQ:
740 case OP_COND_EXC_IGE:
741 case OP_COND_EXC_IGT:
742 case OP_COND_EXC_ILE:
743 case OP_COND_EXC_ILT:
744 case OP_COND_EXC_INE_UN:
745 case OP_COND_EXC_IGE_UN:
746 case OP_COND_EXC_IGT_UN:
747 case OP_COND_EXC_ILE_UN:
748 case OP_COND_EXC_ILT_UN:
749 case OP_COND_EXC_IOV:
750 case OP_COND_EXC_INO:
752 case OP_COND_EXC_INC:
753 g_string_append_printf (sbuf, " %s", ins->inst_p1);
759 if (spec [MONO_INST_CLOB])
760 g_string_append_printf (sbuf, " clobbers: %c", spec [MONO_INST_CLOB]);
765 print_regtrack (RegTrack *t, int num)
771 for (i = 0; i < num; ++i) {
774 if (i >= MONO_MAX_IREGS) {
775 g_snprintf (buf, sizeof(buf), "R%d", i);
778 r = mono_arch_regname (i);
779 printf ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].killed_in);
785 mono_ji_type_to_string (MonoJumpInfoType type)
791 mono_print_ji (const MonoJumpInfo *ji)
796 mono_print_ins_index (int i, MonoInst *ins)
799 #endif /* !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT) */
802 mono_print_ins (MonoInst *ins)
804 mono_print_ins_index (-1, ins);
808 insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst* to_insert)
811 * If this function is called multiple times, the new instructions are inserted
812 * in the proper order.
814 mono_bblock_insert_before_ins (bb, ins, to_insert);
818 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst **last, MonoInst* to_insert)
821 * If this function is called multiple times, the new instructions are inserted in
824 mono_bblock_insert_after_ins (bb, *last, to_insert);
830 get_vreg_bank (MonoCompile *cfg, int reg, int bank)
832 if (vreg_is_ref (cfg, reg))
833 return MONO_REG_INT_REF;
834 else if (vreg_is_mp (cfg, reg))
835 return MONO_REG_INT_MP;
841 * Force the spilling of the variable in the symbolic register 'reg', and free
842 * the hreg it was assigned to.
845 spill_vreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int reg, int bank)
849 MonoRegState *rs = cfg->rs;
851 sel = rs->vassign [reg];
853 /* the vreg we need to spill lives in another logical reg bank */
854 bank = translate_bank (cfg->rs, bank, sel);
856 /*i = rs->isymbolic [sel];
857 g_assert (i == reg);*/
859 spill = ++cfg->spill_count;
860 rs->vassign [i] = -spill - 1;
861 if (G_UNLIKELY (bank))
862 mono_regstate_free_general (rs, sel, bank);
864 mono_regstate_free_int (rs, sel);
865 /* we need to create a spill var and insert a load to sel after the current instruction */
866 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
868 load->inst_basereg = cfg->frame_reg;
869 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, reg, bank));
870 insert_after_ins (bb, ins, last, load);
871 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)));
872 if (G_UNLIKELY (bank))
873 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
875 i = mono_regstate_alloc_int (rs, regmask (sel));
878 if (G_UNLIKELY (bank))
879 mono_regstate_free_general (rs, sel, bank);
881 mono_regstate_free_int (rs, sel);
884 /* This isn't defined on older glib versions and on some platforms */
885 #ifndef G_GUINT64_FORMAT
886 #define G_GUINT64_FORMAT "ul"
890 get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t regmask, int reg, int bank)
893 int i, sel, spill, num_sregs;
894 int sregs [MONO_MAX_SRC_REGS];
895 MonoRegState *rs = cfg->rs;
897 g_assert (bank < MONO_NUM_REGBANKS);
899 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));
900 /* exclude the registers in the current instruction */
901 num_sregs = mono_inst_get_src_registers (ins, sregs);
902 for (i = 0; i < num_sregs; ++i) {
903 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))) {
904 if (is_soft_reg (sregs [i], bank))
905 regmask &= ~ (regmask (rs->vassign [sregs [i]]));
907 regmask &= ~ (regmask (sregs [i]));
908 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i + 1, mono_regname_full (sregs [i], bank), sregs [i]));
911 if ((dreg_bank_ins (ins) == bank) && (reg != ins->dreg) && reg_is_freeable (ins->dreg, bank)) {
912 regmask &= ~ (regmask (ins->dreg));
913 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins->dreg, bank)));
916 DEBUG (printf ("\t\tavailable regmask: 0x%08llu\n", (unsigned long long)regmask));
917 g_assert (regmask); /* need at least a register we can free */
919 /* we should track prev_use and spill the register that's farther */
920 if (G_UNLIKELY (bank)) {
921 for (i = 0; i < regbank_size [bank]; ++i) {
922 if (regmask & (regmask (i))) {
925 /* the vreg we need to load lives in another logical bank */
926 bank = translate_bank (cfg->rs, bank, sel);
928 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
933 i = rs->symbolic [bank] [sel];
934 spill = ++cfg->spill_count;
935 rs->vassign [i] = -spill - 1;
936 mono_regstate_free_general (rs, sel, bank);
939 for (i = 0; i < MONO_MAX_IREGS; ++i) {
940 if (regmask & (regmask (i))) {
942 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), rs->isymbolic [sel]));
947 i = rs->isymbolic [sel];
948 spill = ++cfg->spill_count;
949 rs->vassign [i] = -spill - 1;
950 mono_regstate_free_int (rs, sel);
953 /* we need to create a spill var and insert a load to sel after the current instruction */
954 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
956 load->inst_basereg = cfg->frame_reg;
957 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, i, bank));
958 insert_after_ins (bb, ins, last, load);
959 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)));
960 if (G_UNLIKELY (bank))
961 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
963 i = mono_regstate_alloc_int (rs, regmask (sel));
972 * Free up the hreg HREG by spilling the vreg allocated to it.
975 free_up_hreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int hreg, int bank)
977 if (G_UNLIKELY (bank)) {
978 if (!(cfg->rs->free_mask [bank] & (regmask (hreg)))) {
979 bank = translate_bank (cfg->rs, bank, hreg);
980 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
981 spill_vreg (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
985 if (!(cfg->rs->ifree_mask & (regmask (hreg)))) {
986 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->isymbolic [hreg]));
987 spill_vreg (cfg, bb, last, ins, cfg->rs->isymbolic [hreg], bank);
993 create_copy_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, int dest, int src, MonoInst *ins, const unsigned char *ip, int bank)
997 MONO_INST_NEW (cfg, copy, regbank_move_ops [bank]);
1001 copy->cil_code = ip;
1003 mono_bblock_insert_after_ins (bb, ins, copy);
1006 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src, bank), mono_regname_full (dest, bank)));
1010 static inline const char*
1011 regbank_to_string (int bank)
1013 if (bank == MONO_REG_INT_REF)
1015 else if (bank == MONO_REG_INT_MP)
1022 create_spilled_store (MonoCompile *cfg, MonoBasicBlock *bb, int spill, int reg, int prev_reg, MonoInst **last, MonoInst *ins, MonoInst *insert_before, int bank)
1024 MonoInst *store, *def;
1026 bank = get_vreg_bank (cfg, prev_reg, bank);
1028 MONO_INST_NEW (cfg, store, regbank_store_ops [bank]);
1030 store->inst_destbasereg = cfg->frame_reg;
1031 store->inst_offset = mono_spillvar_offset (cfg, spill, bank);
1033 mono_bblock_insert_after_ins (bb, ins, store);
1035 } else if (insert_before) {
1036 insert_before_ins (bb, insert_before, store);
1038 g_assert_not_reached ();
1040 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)));
1042 if (((bank == MONO_REG_INT_REF) || (bank == MONO_REG_INT_MP)) && cfg->compute_gc_maps) {
1043 g_assert (prev_reg != -1);
1044 MONO_INST_NEW (cfg, def, OP_GC_SPILL_SLOT_LIVENESS_DEF);
1045 def->inst_c0 = spill;
1046 def->inst_c1 = bank;
1047 mono_bblock_insert_after_ins (bb, store, def);
1051 /* flags used in reginfo->flags */
1053 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
1054 MONO_FP_NEEDS_SPILL = regmask (1),
1055 MONO_FP_NEEDS_LOAD = regmask (2)
1059 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
1063 if (info && info->preferred_mask) {
1064 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
1066 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
1071 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
1073 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
1079 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
1083 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
1086 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1092 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
1094 if (G_UNLIKELY (bank))
1095 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1097 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
1101 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
1103 if (G_UNLIKELY (bank)) {
1106 g_assert (reg >= regbank_size [bank]);
1107 g_assert (hreg < regbank_size [bank]);
1108 g_assert (! is_global_freg (hreg));
1110 rs->vassign [reg] = hreg;
1111 rs->symbolic [bank] [hreg] = reg;
1112 rs->free_mask [bank] &= ~ (regmask (hreg));
1114 mirrored_bank = get_mirrored_bank (bank);
1115 if (mirrored_bank == -1)
1118 /* Make sure the other logical reg bank that this bank shares
1119 * a single hard reg bank knows that this hard reg is not free.
1121 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
1123 /* Mark the other logical bank that the this bank shares
1124 * a single hard reg bank with as mirrored.
1126 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
1130 g_assert (reg >= MONO_MAX_IREGS);
1131 g_assert (hreg < MONO_MAX_IREGS);
1132 #if !defined(TARGET_ARM) && !defined(TARGET_ARM64)
1133 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
1134 /* On arm64, rgctx_reg is a global hreg, and it is used to pass an argument */
1135 g_assert (! is_global_ireg (hreg));
1138 rs->vassign [reg] = hreg;
1139 rs->isymbolic [hreg] = reg;
1140 rs->ifree_mask &= ~ (regmask (hreg));
1144 static inline regmask_t
1145 get_callee_mask (const char spec)
1147 if (G_UNLIKELY (reg_bank (spec)))
1148 return regbank_callee_regs [reg_bank (spec)];
1149 return MONO_ARCH_CALLEE_REGS;
1152 static gint8 desc_to_fixed_reg [256];
1153 static gboolean desc_to_fixed_reg_inited = FALSE;
1158 * Local register allocation.
1159 * We first scan the list of instructions and we save the liveness info of
1160 * each register (when the register is first used, when it's value is set etc.).
1161 * We also reverse the list of instructions because assigning registers backwards allows
1162 * for more tricks to be used.
1165 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1167 MonoInst *ins, *prev, *last;
1169 MonoRegState *rs = cfg->rs;
1173 unsigned char spec_src1, spec_dest;
1175 #if MONO_ARCH_USE_FPSTACK
1176 gboolean has_fp = FALSE;
1181 int sregs [MONO_MAX_SRC_REGS];
1186 if (!desc_to_fixed_reg_inited) {
1187 for (i = 0; i < 256; ++i)
1188 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1189 desc_to_fixed_reg_inited = TRUE;
1191 /* Validate the cpu description against the info in mini-ops.h */
1192 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_ARM64)
1193 for (i = OP_LOAD; i < OP_LAST; ++i) {
1196 spec = ins_get_spec (i);
1197 ispec = INS_INFO (i);
1199 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1200 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1201 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1202 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1203 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1204 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1209 rs->next_vreg = bb->max_vreg;
1210 mono_regstate_assign (rs);
1212 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1213 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1214 rs->free_mask [i] = regbank_callee_regs [i];
1216 max = rs->next_vreg;
1218 if (cfg->reginfo && cfg->reginfo_len < max)
1219 cfg->reginfo = NULL;
1221 reginfo = (RegTrack *)cfg->reginfo;
1223 cfg->reginfo_len = MAX (1024, max * 2);
1224 reginfo = (RegTrack *)mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1225 cfg->reginfo = reginfo;
1228 g_assert (cfg->reginfo_len >= rs->next_vreg);
1230 if (cfg->verbose_level > 1) {
1231 /* print_regtrack reads the info of all variables */
1232 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1236 * For large methods, next_vreg can be very large, so g_malloc0 time can
1237 * be prohibitive. So we manually init the reginfo entries used by the
1240 for (ins = bb->code; ins; ins = ins->next) {
1241 gboolean modify = FALSE;
1243 spec = ins_get_spec (ins->opcode);
1245 if ((ins->dreg != -1) && (ins->dreg < max)) {
1246 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1247 #if SIZEOF_REGISTER == 4
1248 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1250 * In the new IR, the two vregs of the regpair do not alias the
1251 * original long vreg. shift the vreg here so the rest of the
1252 * allocator doesn't have to care about it.
1255 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1260 num_sregs = mono_inst_get_src_registers (ins, sregs);
1261 for (j = 0; j < num_sregs; ++j) {
1262 g_assert (sregs [j] != -1);
1263 if (sregs [j] < max) {
1264 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1265 #if SIZEOF_REGISTER == 4
1266 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1269 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1275 mono_inst_set_src_registers (ins, sregs);
1278 /*if (cfg->opt & MONO_OPT_COPYPROP)
1279 local_copy_prop (cfg, ins);*/
1282 DEBUG (printf ("\nLOCAL REGALLOC BLOCK %d:\n", bb->block_num));
1283 /* forward pass on the instructions to collect register liveness info */
1284 MONO_BB_FOR_EACH_INS (bb, ins) {
1285 spec = ins_get_spec (ins->opcode);
1286 spec_dest = spec [MONO_INST_DEST];
1288 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1289 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1292 DEBUG (mono_print_ins_index (i, ins));
1294 num_sregs = mono_inst_get_src_registers (ins, sregs);
1296 #if MONO_ARCH_USE_FPSTACK
1297 if (dreg_is_fp (spec)) {
1300 for (j = 0; j < num_sregs; ++j) {
1301 if (sreg_is_fp (j, spec))
1307 for (j = 0; j < num_sregs; ++j) {
1308 int sreg = sregs [j];
1309 int sreg_spec = spec [MONO_INST_SRC1 + j];
1311 bank = sreg_bank (j, spec);
1312 g_assert (sreg != -1);
1313 if (is_soft_reg (sreg, bank))
1314 /* This means the vreg is not local to this bb */
1315 g_assert (reginfo [sreg].born_in > 0);
1316 rs->vassign [sreg] = -1;
1317 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1318 //reginfo [ins->sreg2].last_use = i;
1319 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1320 /* The virtual register is allocated sequentially */
1321 rs->vassign [sreg + 1] = -1;
1322 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1323 //reginfo [ins->sreg2 + 1].last_use = i;
1324 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1325 reginfo [sreg + 1].born_in = i;
1331 mono_inst_set_src_registers (ins, sregs);
1336 bank = dreg_bank (spec);
1337 if (spec_dest != 'b') /* it's not just a base register */
1338 reginfo [ins->dreg].killed_in = i;
1339 g_assert (ins->dreg != -1);
1340 rs->vassign [ins->dreg] = -1;
1341 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1342 //reginfo [ins->dreg].last_use = i;
1343 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1344 reginfo [ins->dreg].born_in = i;
1346 dest_dreg = desc_to_fixed_reg [spec_dest];
1347 if (dest_dreg != -1)
1348 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1350 #ifdef MONO_ARCH_INST_FIXED_MASK
1351 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1354 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1355 /* The virtual register is allocated sequentially */
1356 rs->vassign [ins->dreg + 1] = -1;
1357 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1358 //reginfo [ins->dreg + 1].last_use = i;
1359 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1360 reginfo [ins->dreg + 1].born_in = i;
1361 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1362 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1373 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1374 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1376 int dest_dreg, clob_reg;
1377 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1378 int dreg_high, sreg1_high;
1379 regmask_t dreg_mask, mask;
1380 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1381 regmask_t dreg_fixed_mask;
1382 const unsigned char *ip;
1384 spec = ins_get_spec (ins->opcode);
1385 spec_src1 = spec [MONO_INST_SRC1];
1386 spec_dest = spec [MONO_INST_DEST];
1392 dreg_mask = get_callee_mask (spec_dest);
1393 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1394 prev_sregs [j] = -1;
1395 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1396 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1397 #ifdef MONO_ARCH_INST_FIXED_MASK
1398 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1400 sreg_fixed_masks [j] = 0;
1404 DEBUG (printf ("processing:"));
1405 DEBUG (mono_print_ins_index (i, ins));
1414 dest_dreg = desc_to_fixed_reg [spec_dest];
1415 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1416 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1418 #ifdef MONO_ARCH_INST_FIXED_MASK
1419 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1421 dreg_fixed_mask = 0;
1424 num_sregs = mono_inst_get_src_registers (ins, sregs);
1427 * TRACK FIXED SREG2, 3, ...
1429 for (j = 1; j < num_sregs; ++j) {
1430 int sreg = sregs [j];
1431 int dest_sreg = dest_sregs [j];
1433 if (dest_sreg == -1)
1441 * We need to special case this, since on x86, there are only 3
1442 * free registers, and the code below assigns one of them to
1443 * sreg, so we can run out of registers when trying to assign
1444 * dreg. Instead, we just set up the register masks, and let the
1445 * normal sreg2 assignment code handle this. It would be nice to
1446 * do this for all the fixed reg cases too, but there is too much
1450 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1451 sreg_masks [j] = regmask (dest_sreg);
1452 for (k = 0; k < num_sregs; ++k) {
1454 sreg_masks [k] &= ~ (regmask (dest_sreg));
1458 * Spill sreg1/2 if they are assigned to dest_sreg.
1460 for (k = 0; k < num_sregs; ++k) {
1461 if (k != j && is_soft_reg (sregs [k], 0) && rs->vassign [sregs [k]] == dest_sreg)
1462 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1466 * We can also run out of registers while processing sreg2 if sreg3 is
1467 * assigned to another hreg, so spill sreg3 now.
1469 if (is_soft_reg (sreg, 0) && rs->vassign [sreg] >= 0 && rs->vassign [sreg] != dest_sreg) {
1470 spill_vreg (cfg, bb, tmp, ins, sreg, 0);
1475 if (rs->ifree_mask & (regmask (dest_sreg))) {
1476 if (is_global_ireg (sreg)) {
1478 /* Argument already in hard reg, need to copy */
1479 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1480 insert_before_ins (bb, ins, copy);
1481 for (k = 0; k < num_sregs; ++k) {
1483 sreg_masks [k] &= ~ (regmask (dest_sreg));
1486 dreg_mask &= ~ (regmask (dest_sreg));
1488 val = rs->vassign [sreg];
1490 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1491 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1492 } else if (val < -1) {
1494 g_assert_not_reached ();
1496 /* Argument already in hard reg, need to copy */
1497 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1500 insert_before_ins (bb, ins, copy);
1501 for (k = 0; k < num_sregs; ++k) {
1503 sreg_masks [k] &= ~ (regmask (dest_sreg));
1506 * Prevent the dreg from being allocated to dest_sreg
1507 * too, since it could force sreg1 to be allocated to
1508 * the same reg on x86.
1510 dreg_mask &= ~ (regmask (dest_sreg));
1514 gboolean need_spill = TRUE;
1515 gboolean need_assign = TRUE;
1518 dreg_mask &= ~ (regmask (dest_sreg));
1519 for (k = 0; k < num_sregs; ++k) {
1521 sreg_masks [k] &= ~ (regmask (dest_sreg));
1525 * First check if dreg is assigned to dest_sreg2, since we
1526 * can't spill a dreg.
1528 if (spec [MONO_INST_DEST])
1529 val = rs->vassign [ins->dreg];
1532 if (val == dest_sreg && ins->dreg != sreg) {
1534 * the destination register is already assigned to
1535 * dest_sreg2: we need to allocate another register for it
1536 * and then copy from this to dest_sreg2.
1539 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1540 g_assert (new_dest >= 0);
1541 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1543 prev_dreg = ins->dreg;
1544 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1545 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1546 mono_regstate_free_int (rs, dest_sreg);
1550 if (is_global_ireg (sreg)) {
1551 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1552 insert_before_ins (bb, ins, copy);
1553 need_assign = FALSE;
1556 val = rs->vassign [sreg];
1557 if (val == dest_sreg) {
1558 /* sreg2 is already assigned to the correct register */
1560 } else if (val < -1) {
1561 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1562 } else if (val >= 0) {
1563 /* sreg2 already assigned to another register */
1565 * We couldn't emit a copy from val to dest_sreg2, because
1566 * val might be spilled later while processing this
1567 * instruction. So we spill sreg2 so it can be allocated to
1570 free_up_hreg (cfg, bb, tmp, ins, val, 0);
1575 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1579 if (rs->vassign [sreg] < -1) {
1582 /* Need to emit a spill store */
1583 spill = - rs->vassign [sreg] - 1;
1584 create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, ins, bank);
1586 /* force-set sreg2 */
1587 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1590 sregs [j] = dest_sreg;
1592 mono_inst_set_src_registers (ins, sregs);
1597 bank = dreg_bank (spec);
1598 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1599 prev_dreg = ins->dreg;
1602 if (spec_dest == 'b') {
1604 * The dest reg is read by the instruction, not written, so
1605 * avoid allocating sreg1/sreg2 to the same reg.
1607 if (dest_sregs [0] != -1)
1608 dreg_mask &= ~ (regmask (dest_sregs [0]));
1609 for (j = 1; j < num_sregs; ++j) {
1610 if (dest_sregs [j] != -1)
1611 dreg_mask &= ~ (regmask (dest_sregs [j]));
1614 val = rs->vassign [ins->dreg];
1615 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1616 /* DREG is already allocated to a register needed for sreg1 */
1617 spill_vreg (cfg, bb, tmp, ins, ins->dreg, 0);
1622 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1623 * various complex situations.
1625 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1626 guint32 dreg2, dest_dreg2;
1628 g_assert (is_soft_reg (ins->dreg, bank));
1630 if (dest_dreg != -1) {
1631 if (rs->vassign [ins->dreg] != dest_dreg)
1632 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, 0);
1634 dreg2 = ins->dreg + 1;
1635 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1636 if (dest_dreg2 != -1) {
1637 if (rs->vassign [dreg2] != dest_dreg2)
1638 free_up_hreg (cfg, bb, tmp, ins, dest_dreg2, 0);
1643 if (dreg_fixed_mask) {
1645 if (is_global_ireg (ins->dreg)) {
1647 * The argument is already in a hard reg, but that reg is
1648 * not usable by this instruction, so allocate a new one.
1650 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1652 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1653 mono_regstate_free_int (rs, val);
1659 dreg_mask &= dreg_fixed_mask;
1662 if (is_soft_reg (ins->dreg, bank)) {
1663 val = rs->vassign [ins->dreg];
1668 /* the register gets spilled after this inst */
1671 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1672 assign_reg (cfg, rs, ins->dreg, val, bank);
1674 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, NULL, bank);
1677 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1681 /* Handle regpairs */
1682 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1683 int reg2 = prev_dreg + 1;
1686 g_assert (prev_dreg > -1);
1687 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1688 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1691 mask &= ~regmask (X86_ECX);
1693 val = rs->vassign [reg2];
1697 /* the register gets spilled after this inst */
1700 val = mono_regstate_alloc_int (rs, mask);
1702 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1704 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, NULL, bank);
1707 if (! (mask & (regmask (val)))) {
1708 val = mono_regstate_alloc_int (rs, mask);
1710 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1712 /* Reallocate hreg to the correct register */
1713 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1715 mono_regstate_free_int (rs, rs->vassign [reg2]);
1719 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1720 assign_reg (cfg, rs, reg2, val, bank);
1723 ins->backend.reg3 = val;
1725 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1726 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1727 mono_regstate_free_int (rs, val);
1731 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1733 * In theory, we could free up the hreg even if the vreg is alive,
1734 * but branches inside bblocks force us to assign the same hreg
1735 * to a vreg every time it is encountered.
1737 int dreg = rs->vassign [prev_dreg];
1738 g_assert (dreg >= 0);
1739 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1740 if (G_UNLIKELY (bank))
1741 mono_regstate_free_general (rs, dreg, bank);
1743 mono_regstate_free_int (rs, dreg);
1744 rs->vassign [prev_dreg] = -1;
1747 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1748 /* this instruction only outputs to dest_dreg, need to copy */
1749 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1750 ins->dreg = dest_dreg;
1752 if (G_UNLIKELY (bank)) {
1753 /* the register we need to free up may be used in another logical regbank
1754 * so do a translate just in case.
1756 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1757 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1758 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1761 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1762 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, bank);
1766 if (spec_dest == 'b') {
1768 * The dest reg is read by the instruction, not written, so
1769 * avoid allocating sreg1/sreg2 to the same reg.
1771 for (j = 0; j < num_sregs; ++j)
1772 if (!sreg_bank (j, spec))
1773 sreg_masks [j] &= ~ (regmask (ins->dreg));
1779 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1780 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1781 free_up_hreg (cfg, bb, tmp, ins, clob_reg, 0);
1784 if (spec [MONO_INST_CLOB] == 'c') {
1785 int j, s, dreg, dreg2, cur_bank;
1788 clob_mask = MONO_ARCH_CALLEE_REGS;
1790 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1792 * Need to avoid spilling the dreg since the dreg is not really
1793 * clobbered by the call.
1795 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1796 dreg = rs->vassign [prev_dreg];
1800 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1801 dreg2 = rs->vassign [prev_dreg + 1];
1805 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1807 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1808 if ((j != dreg) && (j != dreg2))
1809 free_up_hreg (cfg, bb, tmp, ins, j, 0);
1810 else if (rs->isymbolic [j])
1811 /* The hreg is assigned to the dreg of this instruction */
1812 rs->vassign [rs->isymbolic [j]] = -1;
1813 mono_regstate_free_int (rs, j);
1818 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1819 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1820 clob_mask = regbank_callee_regs [cur_bank];
1821 if ((prev_dreg != -1) && reg_bank (spec_dest))
1822 dreg = rs->vassign [prev_dreg];
1826 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1828 /* we are looping though the banks in the outer loop
1829 * so, we don't need to deal with mirrored hregs
1830 * because we will get them in one of the other bank passes.
1832 if (is_hreg_mirrored (rs, cur_bank, j))
1836 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s)) {
1838 free_up_hreg (cfg, bb, tmp, ins, j, cur_bank);
1839 else if (rs->symbolic [cur_bank] [j])
1840 /* The hreg is assigned to the dreg of this instruction */
1841 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1842 mono_regstate_free_general (rs, j, cur_bank);
1850 * TRACK ARGUMENT REGS
1852 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1853 MonoCallInst *call = (MonoCallInst*)ins;
1857 * This needs to be done before assigning sreg1, so sreg1 will
1858 * not be assigned one of the argument regs.
1862 * Assign all registers in call->out_reg_args to the proper
1863 * argument registers.
1866 list = call->out_ireg_args;
1872 regpair = (guint32)(gssize)(list->data);
1873 hreg = regpair >> 24;
1874 reg = regpair & 0xffffff;
1876 assign_reg (cfg, rs, reg, hreg, 0);
1878 sreg_masks [0] &= ~(regmask (hreg));
1880 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1882 list = g_slist_next (list);
1886 list = call->out_freg_args;
1892 regpair = (guint32)(gssize)(list->data);
1893 hreg = regpair >> 24;
1894 reg = regpair & 0xffffff;
1896 assign_reg (cfg, rs, reg, hreg, 1);
1898 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1900 list = g_slist_next (list);
1908 bank = sreg1_bank (spec);
1909 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1910 int sreg1 = sregs [0];
1911 int dest_sreg1 = dest_sregs [0];
1913 g_assert (is_soft_reg (sreg1, bank));
1915 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1916 if (dest_sreg1 != -1)
1917 g_assert (dest_sreg1 == ins->dreg);
1918 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1919 g_assert (val >= 0);
1921 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1923 g_assert_not_reached ();
1925 assign_reg (cfg, rs, sreg1, val, bank);
1927 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1929 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1930 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1931 g_assert (val >= 0);
1933 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1935 g_assert_not_reached ();
1937 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1939 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1941 /* Skip rest of this section */
1942 dest_sregs [0] = -1;
1945 if (sreg_fixed_masks [0]) {
1947 if (is_global_ireg (sregs [0])) {
1949 * The argument is already in a hard reg, but that reg is
1950 * not usable by this instruction, so allocate a new one.
1952 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1954 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1955 mono_regstate_free_int (rs, val);
1956 dest_sregs [0] = val;
1958 /* Fall through to the dest_sreg1 != -1 case */
1961 sreg_masks [0] &= sreg_fixed_masks [0];
1964 if (dest_sregs [0] != -1) {
1965 sreg_masks [0] = regmask (dest_sregs [0]);
1967 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1968 free_up_hreg (cfg, bb, tmp, ins, dest_sregs [0], 0);
1970 if (is_global_ireg (sregs [0])) {
1971 /* The argument is already in a hard reg, need to copy */
1972 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1973 insert_before_ins (bb, ins, copy);
1974 sregs [0] = dest_sregs [0];
1978 if (is_soft_reg (sregs [0], bank)) {
1979 val = rs->vassign [sregs [0]];
1980 prev_sregs [0] = sregs [0];
1984 /* the register gets spilled after this inst */
1988 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1990 * Allocate the same hreg to sreg1 as well so the
1991 * peephole can get rid of the move.
1993 sreg_masks [0] = regmask (ins->dreg);
1996 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1997 /* Allocate the same reg to sreg1 to avoid a copy later */
1998 sreg_masks [0] = regmask (ins->dreg);
2000 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
2001 assign_reg (cfg, rs, sregs [0], val, bank);
2002 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
2006 * Need to insert before the instruction since it can
2009 create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, ins, bank);
2012 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
2013 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
2014 insert_before_ins (bb, ins, copy);
2015 for (j = 1; j < num_sregs; ++j)
2016 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
2017 val = dest_sregs [0];
2023 prev_sregs [0] = -1;
2025 mono_inst_set_src_registers (ins, sregs);
2027 for (j = 1; j < num_sregs; ++j)
2028 sreg_masks [j] &= ~(regmask (sregs [0]));
2030 /* Handle the case when sreg1 is a regpair but dreg is not */
2031 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
2032 int reg2 = prev_sregs [0] + 1;
2035 g_assert (prev_sregs [0] > -1);
2036 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
2037 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
2038 val = rs->vassign [reg2];
2042 /* the register gets spilled after this inst */
2045 val = mono_regstate_alloc_int (rs, mask);
2047 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2049 g_assert_not_reached ();
2052 if (! (mask & (regmask (val)))) {
2053 /* The vreg is already allocated to a wrong hreg */
2055 g_assert_not_reached ();
2057 val = mono_regstate_alloc_int (rs, mask);
2059 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2061 /* Reallocate hreg to the correct register */
2062 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
2064 mono_regstate_free_int (rs, rs->vassign [reg2]);
2070 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
2071 assign_reg (cfg, rs, reg2, val, bank);
2074 /* Handle dreg==sreg1 */
2075 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
2076 MonoInst *sreg2_copy = NULL;
2078 int bank = reg_bank (spec_src1);
2080 if (ins->dreg == sregs [1]) {
2082 * copying sreg1 to dreg could clobber sreg2, so allocate a new
2085 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
2087 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
2088 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
2089 prev_sregs [1] = sregs [1] = reg2;
2091 if (G_UNLIKELY (bank))
2092 mono_regstate_free_general (rs, reg2, bank);
2094 mono_regstate_free_int (rs, reg2);
2097 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
2098 /* Copying sreg1_high to dreg could also clobber sreg2 */
2099 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
2101 g_assert_not_reached ();
2104 * sreg1 and dest are already allocated to the same regpair by the
2105 * SREG1 allocation code.
2107 g_assert (sregs [0] == ins->dreg);
2108 g_assert (dreg_high == sreg1_high);
2111 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
2112 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
2113 insert_before_ins (bb, ins, copy);
2116 insert_before_ins (bb, copy, sreg2_copy);
2119 * Need to prevent sreg2 to be allocated to sreg1, since that
2120 * would screw up the previous copy.
2122 sreg_masks [1] &= ~ (regmask (sregs [0]));
2123 /* we set sreg1 to dest as well */
2124 prev_sregs [0] = sregs [0] = ins->dreg;
2125 sreg_masks [1] &= ~ (regmask (ins->dreg));
2127 mono_inst_set_src_registers (ins, sregs);
2130 * TRACK SREG2, 3, ...
2132 for (j = 1; j < num_sregs; ++j) {
2135 bank = sreg_bank (j, spec);
2136 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
2137 g_assert_not_reached ();
2139 if (dest_sregs [j] != -1 && is_global_ireg (sregs [j])) {
2141 * Argument already in a global hard reg, copy it to the fixed reg, without
2142 * allocating it to the fixed reg.
2144 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [j], sregs [j], NULL, ip, 0);
2145 insert_before_ins (bb, ins, copy);
2146 sregs [j] = dest_sregs [j];
2147 } else if (is_soft_reg (sregs [j], bank)) {
2148 val = rs->vassign [sregs [j]];
2150 if (dest_sregs [j] != -1 && val >= 0 && dest_sregs [j] != val) {
2152 * The sreg is already allocated to a hreg, but not to the fixed
2153 * reg required by the instruction. Spill the sreg, so it can be
2154 * allocated to the fixed reg by the code below.
2156 /* Currently, this code should only be hit for CAS */
2157 spill_vreg (cfg, bb, tmp, ins, sregs [j], 0);
2158 val = rs->vassign [sregs [j]];
2164 /* the register gets spilled after this inst */
2167 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
2168 assign_reg (cfg, rs, sregs [j], val, bank);
2169 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
2172 * Need to insert before the instruction since it can
2175 create_spilled_store (cfg, bb, spill, val, sregs [j], tmp, NULL, ins, bank);
2179 for (k = j + 1; k < num_sregs; ++k)
2180 sreg_masks [k] &= ~ (regmask (sregs [j]));
2183 prev_sregs [j] = -1;
2186 mono_inst_set_src_registers (ins, sregs);
2189 /* Do this only for CAS for now */
2190 for (j = 1; j < num_sregs; ++j) {
2191 int sreg = sregs [j];
2192 int dest_sreg = dest_sregs [j];
2194 if (j == 2 && dest_sreg != -1) {
2197 g_assert (sreg == dest_sreg);
2199 for (k = 0; k < num_sregs; ++k) {
2201 g_assert (sregs [k] != dest_sreg);
2206 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2207 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2208 mono_regstate_free_int (rs, ins->sreg1);
2210 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2211 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2212 mono_regstate_free_int (rs, ins->sreg2);
2215 DEBUG (mono_print_ins_index (i, ins));
2218 // FIXME: Set MAX_FREGS to 8
2219 // FIXME: Optimize generated code
2220 #if MONO_ARCH_USE_FPSTACK
2222 * Make a forward pass over the code, simulating the fp stack, making sure the
2223 * arguments required by the fp opcodes are at the top of the stack.
2226 MonoInst *prev = NULL;
2230 g_assert (num_sregs <= 2);
2232 for (ins = bb->code; ins; ins = ins->next) {
2233 spec = ins_get_spec (ins->opcode);
2235 DEBUG (printf ("processing:"));
2236 DEBUG (mono_print_ins_index (0, ins));
2238 if (ins->opcode == OP_FMOVE) {
2239 /* Do it by renaming the source to the destination on the stack */
2240 // FIXME: Is this correct ?
2241 for (i = 0; i < sp; ++i)
2242 if (fpstack [i] == ins->sreg1)
2243 fpstack [i] = ins->dreg;
2248 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2249 /* Arg1 must be in %st(1) */
2253 while ((i < sp) && (fpstack [i] != ins->sreg1))
2257 if (sp - 1 - i > 0) {
2258 /* First move it to %st(0) */
2259 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2261 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2262 fxch->inst_imm = sp - 1 - i;
2264 mono_bblock_insert_after_ins (bb, prev, fxch);
2267 tmp = fpstack [sp - 1];
2268 fpstack [sp - 1] = fpstack [i];
2272 /* Then move it to %st(1) */
2273 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2275 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2278 mono_bblock_insert_after_ins (bb, prev, fxch);
2281 tmp = fpstack [sp - 1];
2282 fpstack [sp - 1] = fpstack [sp - 2];
2283 fpstack [sp - 2] = tmp;
2286 if (sreg2_is_fp (spec)) {
2289 if (fpstack [sp - 1] != ins->sreg2) {
2293 while ((i < sp) && (fpstack [i] != ins->sreg2))
2297 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2299 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2300 fxch->inst_imm = sp - 1 - i;
2302 mono_bblock_insert_after_ins (bb, prev, fxch);
2305 tmp = fpstack [sp - 1];
2306 fpstack [sp - 1] = fpstack [i];
2313 if (sreg1_is_fp (spec)) {
2316 if (fpstack [sp - 1] != ins->sreg1) {
2320 while ((i < sp) && (fpstack [i] != ins->sreg1))
2324 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2326 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2327 fxch->inst_imm = sp - 1 - i;
2329 mono_bblock_insert_after_ins (bb, prev, fxch);
2332 tmp = fpstack [sp - 1];
2333 fpstack [sp - 1] = fpstack [i];
2340 if (dreg_is_fp (spec)) {
2342 fpstack [sp ++] = ins->dreg;
2345 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2347 for (i = 0; i < sp; ++i)
2348 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2355 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2356 /* Remove remaining items from the fp stack */
2358 * These can remain for example as a result of a dead fmove like in
2359 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2362 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2363 mono_add_ins_to_end (bb, ins);
2372 mono_opcode_to_cond (int opcode)
2384 case OP_COND_EXC_EQ:
2385 case OP_COND_EXC_IEQ:
2394 case OP_COND_EXC_NE_UN:
2395 case OP_COND_EXC_INE_UN:
2396 case OP_CMOV_INE_UN:
2397 case OP_CMOV_LNE_UN:
2424 case OP_COND_EXC_LT:
2425 case OP_COND_EXC_ILT:
2438 case OP_COND_EXC_GT:
2439 case OP_COND_EXC_IGT:
2448 case OP_COND_EXC_LE_UN:
2449 case OP_COND_EXC_ILE_UN:
2450 case OP_CMOV_ILE_UN:
2451 case OP_CMOV_LLE_UN:
2458 case OP_CMOV_IGE_UN:
2459 case OP_CMOV_LGE_UN:
2470 case OP_COND_EXC_LT_UN:
2471 case OP_COND_EXC_ILT_UN:
2472 case OP_CMOV_ILT_UN:
2473 case OP_CMOV_LLT_UN:
2484 case OP_COND_EXC_GT_UN:
2485 case OP_COND_EXC_IGT_UN:
2486 case OP_CMOV_IGT_UN:
2487 case OP_CMOV_LGT_UN:
2490 printf ("%s\n", mono_inst_name (opcode));
2491 g_assert_not_reached ();
2492 return (CompRelation)0;
2497 mono_negate_cond (CompRelation cond)
2521 g_assert_not_reached ();
2526 mono_opcode_to_type (int opcode, int cmp_opcode)
2528 if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2530 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2532 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2534 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2536 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2538 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2540 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2542 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2544 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2545 switch (cmp_opcode) {
2547 case OP_ICOMPARE_IMM:
2553 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2558 #endif /* DISABLE_JIT */
2561 mono_is_regsize_var (MonoType *t)
2563 t = mini_get_underlying_type (t);
2574 case MONO_TYPE_FNPTR:
2575 #if SIZEOF_REGISTER == 8
2580 case MONO_TYPE_OBJECT:
2581 case MONO_TYPE_STRING:
2582 case MONO_TYPE_CLASS:
2583 case MONO_TYPE_SZARRAY:
2584 case MONO_TYPE_ARRAY:
2586 case MONO_TYPE_GENERICINST:
2587 if (!mono_type_generic_inst_is_valuetype (t))
2590 case MONO_TYPE_VALUETYPE:
2600 * mono_peephole_ins:
2602 * Perform some architecture independent peephole optimizations.
2605 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2607 int filter = FILTER_IL_SEQ_POINT;
2608 MonoInst *last_ins = mono_inst_prev (ins, filter);
2610 switch (ins->opcode) {
2612 /* remove unnecessary multiplication with 1 */
2613 if (ins->inst_imm == 1) {
2614 if (ins->dreg != ins->sreg1)
2615 ins->opcode = OP_MOVE;
2617 MONO_DELETE_INS (bb, ins);
2620 case OP_LOAD_MEMBASE:
2621 case OP_LOADI4_MEMBASE:
2623 * Note: if reg1 = reg2 the load op is removed
2625 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2626 * OP_LOAD_MEMBASE offset(basereg), reg2
2628 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2629 * OP_MOVE reg1, reg2
2631 if (last_ins && last_ins->opcode == OP_GC_LIVENESS_DEF)
2632 last_ins = mono_inst_prev (ins, filter);
2634 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2635 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2636 ins->inst_basereg == last_ins->inst_destbasereg &&
2637 ins->inst_offset == last_ins->inst_offset) {
2638 if (ins->dreg == last_ins->sreg1) {
2639 MONO_DELETE_INS (bb, ins);
2642 ins->opcode = OP_MOVE;
2643 ins->sreg1 = last_ins->sreg1;
2647 * Note: reg1 must be different from the basereg in the second load
2648 * Note: if reg1 = reg2 is equal then second load is removed
2650 * OP_LOAD_MEMBASE offset(basereg), reg1
2651 * OP_LOAD_MEMBASE offset(basereg), reg2
2653 * OP_LOAD_MEMBASE offset(basereg), reg1
2654 * OP_MOVE reg1, reg2
2656 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2657 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2658 ins->inst_basereg != last_ins->dreg &&
2659 ins->inst_basereg == last_ins->inst_basereg &&
2660 ins->inst_offset == last_ins->inst_offset) {
2662 if (ins->dreg == last_ins->dreg) {
2663 MONO_DELETE_INS (bb, ins);
2665 ins->opcode = OP_MOVE;
2666 ins->sreg1 = last_ins->dreg;
2669 //g_assert_not_reached ();
2673 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2674 * OP_LOAD_MEMBASE offset(basereg), reg
2676 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2677 * OP_ICONST reg, imm
2679 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2680 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2681 ins->inst_basereg == last_ins->inst_destbasereg &&
2682 ins->inst_offset == last_ins->inst_offset) {
2683 ins->opcode = OP_ICONST;
2684 ins->inst_c0 = last_ins->inst_imm;
2685 g_assert_not_reached (); // check this rule
2689 case OP_LOADI1_MEMBASE:
2690 case OP_LOADU1_MEMBASE:
2692 * Note: if reg1 = reg2 the load op is removed
2694 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2695 * OP_LOAD_MEMBASE offset(basereg), reg2
2697 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2698 * OP_MOVE reg1, reg2
2700 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2701 ins->inst_basereg == last_ins->inst_destbasereg &&
2702 ins->inst_offset == last_ins->inst_offset) {
2703 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2704 ins->sreg1 = last_ins->sreg1;
2707 case OP_LOADI2_MEMBASE:
2708 case OP_LOADU2_MEMBASE:
2710 * Note: if reg1 = reg2 the load op is removed
2712 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2713 * OP_LOAD_MEMBASE offset(basereg), reg2
2715 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2716 * OP_MOVE reg1, reg2
2718 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2719 ins->inst_basereg == last_ins->inst_destbasereg &&
2720 ins->inst_offset == last_ins->inst_offset) {
2721 #if SIZEOF_REGISTER == 8
2722 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2724 /* The definition of OP_PCONV_TO_U2 is wrong */
2725 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2727 ins->sreg1 = last_ins->sreg1;
2737 if (ins->dreg == ins->sreg1) {
2738 MONO_DELETE_INS (bb, ins);
2744 * OP_MOVE sreg, dreg
2745 * OP_MOVE dreg, sreg
2747 if (last_ins && last_ins->opcode == ins->opcode &&
2748 ins->sreg1 == last_ins->dreg &&
2749 ins->dreg == last_ins->sreg1) {
2750 MONO_DELETE_INS (bb, ins);
2754 MONO_DELETE_INS (bb, ins);
2760 mini_exception_id_by_name (const char *name)
2762 if (strcmp (name, "IndexOutOfRangeException") == 0)
2763 return MONO_EXC_INDEX_OUT_OF_RANGE;
2764 if (strcmp (name, "OverflowException") == 0)
2765 return MONO_EXC_OVERFLOW;
2766 if (strcmp (name, "ArithmeticException") == 0)
2767 return MONO_EXC_ARITHMETIC;
2768 if (strcmp (name, "DivideByZeroException") == 0)
2769 return MONO_EXC_DIVIDE_BY_ZERO;
2770 if (strcmp (name, "InvalidCastException") == 0)
2771 return MONO_EXC_INVALID_CAST;
2772 if (strcmp (name, "NullReferenceException") == 0)
2773 return MONO_EXC_NULL_REF;
2774 if (strcmp (name, "ArrayTypeMismatchException") == 0)
2775 return MONO_EXC_ARRAY_TYPE_MISMATCH;
2776 if (strcmp (name, "ArgumentException") == 0)
2777 return MONO_EXC_ARGUMENT;
2778 g_error ("Unknown intrinsic exception %s\n", name);
2783 mini_type_is_hfa (MonoType *t, int *out_nfields, int *out_esize)
2787 MonoClassField *field;
2788 MonoType *ftype, *prev_ftype = NULL;
2791 klass = mono_class_from_mono_type (t);
2793 while ((field = mono_class_get_fields (klass, &iter))) {
2794 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
2796 ftype = mono_field_get_type (field);
2797 ftype = mini_native_type_replace_type (ftype);
2799 if (MONO_TYPE_ISSTRUCT (ftype)) {
2800 int nested_nfields, nested_esize;
2802 if (!mini_type_is_hfa (ftype, &nested_nfields, &nested_esize))
2804 if (nested_esize == 4)
2805 ftype = &mono_defaults.single_class->byval_arg;
2807 ftype = &mono_defaults.double_class->byval_arg;
2808 if (prev_ftype && prev_ftype->type != ftype->type)
2811 nfields += nested_nfields;
2813 if (!(!ftype->byref && (ftype->type == MONO_TYPE_R4 || ftype->type == MONO_TYPE_R8)))
2815 if (prev_ftype && prev_ftype->type != ftype->type)
2823 *out_nfields = nfields;
2824 *out_esize = prev_ftype->type == MONO_TYPE_R4 ? 4 : 8;
2829 mono_regstate_new (void)
2831 MonoRegState* rs = g_new0 (MonoRegState, 1);
2833 rs->next_vreg = MAX (MONO_MAX_IREGS, MONO_MAX_FREGS);
2834 #ifdef MONO_ARCH_NEED_SIMD_BANK
2835 rs->next_vreg = MAX (rs->next_vreg, MONO_MAX_XREGS);
2842 mono_regstate_free (MonoRegState *rs) {
2843 g_free (rs->vassign);
2847 #endif /* DISABLE_JIT */