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 = 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 = 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_print_ji (const MonoJumpInfo *ji)
426 case MONO_PATCH_INFO_RGCTX_FETCH: {
427 MonoJumpInfoRgctxEntry *entry = ji->data.rgctx_entry;
429 printf ("[RGCTX_FETCH ");
430 mono_print_ji (entry->data);
431 printf (" - %s]", mono_rgctx_info_type_to_str (entry->info_type));
434 case MONO_PATCH_INFO_METHODCONST: {
435 char *s = mono_method_full_name (ji->data.method, TRUE);
436 printf ("[METHODCONST - %s]", s);
441 printf ("[%s]", patch_info_str [ji->type]);
447 mono_print_ins_index (int i, MonoInst *ins)
449 const char *spec = ins_get_spec (ins->opcode);
451 int sregs [MONO_MAX_SRC_REGS];
454 printf ("\t%-2d %s", i, mono_inst_name (ins->opcode));
456 printf (" %s", mono_inst_name (ins->opcode));
457 if (spec == MONO_ARCH_CPU_SPEC) {
458 gboolean dest_base = FALSE;
459 switch (ins->opcode) {
460 case OP_STOREV_MEMBASE:
467 /* This is a lowered opcode */
468 if (ins->dreg != -1) {
470 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
472 printf (" R%d <-", ins->dreg);
474 if (ins->sreg1 != -1)
475 printf (" R%d", ins->sreg1);
476 if (ins->sreg2 != -1)
477 printf (" R%d", ins->sreg2);
478 if (ins->sreg3 != -1)
479 printf (" R%d", ins->sreg3);
481 switch (ins->opcode) {
492 if (!ins->inst_false_bb)
493 printf (" [B%d]", ins->inst_true_bb->block_num);
495 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
502 printf (" [%d (", (int)ins->inst_c0);
503 for (i = 0; i < ins->inst_phi_args [0]; i++) {
506 printf ("R%d", ins->inst_phi_args [i + 1]);
512 case OP_OUTARG_VTRETADDR:
513 printf (" R%d", ((MonoInst*)ins->inst_p0)->dreg);
516 case OP_GSHAREDVT_ARG_REGOFFSET:
517 printf (" + 0x%lx", (long)ins->inst_offset);
524 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
528 if (spec [MONO_INST_DEST]) {
529 int bank = dreg_bank (spec);
530 if (is_soft_reg (ins->dreg, bank)) {
531 if (spec [MONO_INST_DEST] == 'b') {
532 if (ins->inst_offset == 0)
533 printf (" [R%d] <-", ins->dreg);
535 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
538 printf (" R%d <-", ins->dreg);
539 } else if (spec [MONO_INST_DEST] == 'b') {
540 if (ins->inst_offset == 0)
541 printf (" [%s] <-", mono_arch_regname (ins->dreg));
543 printf (" [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
545 printf (" %s <-", mono_regname_full (ins->dreg, bank));
547 if (spec [MONO_INST_SRC1]) {
548 int bank = sreg1_bank (spec);
549 if (is_soft_reg (ins->sreg1, bank)) {
550 if (spec [MONO_INST_SRC1] == 'b')
551 printf (" [R%d + 0x%lx]", ins->sreg1, (long)ins->inst_offset);
553 printf (" R%d", ins->sreg1);
554 } else if (spec [MONO_INST_SRC1] == 'b')
555 printf (" [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
557 printf (" %s", mono_regname_full (ins->sreg1, bank));
559 num_sregs = mono_inst_get_src_registers (ins, sregs);
560 for (j = 1; j < num_sregs; ++j) {
561 int bank = sreg_bank (j, spec);
562 if (is_soft_reg (sregs [j], bank))
563 printf (" R%d", sregs [j]);
565 printf (" %s", mono_regname_full (sregs [j], bank));
568 switch (ins->opcode) {
570 printf (" [%d]", (int)ins->inst_c0);
572 #if defined(TARGET_X86) || defined(TARGET_AMD64)
573 case OP_X86_PUSH_IMM:
575 case OP_ICOMPARE_IMM:
583 printf (" [%d]", (int)ins->inst_imm);
587 printf (" [%d]", (int)(gssize)ins->inst_p1);
590 printf (" [%lld]", (long long)ins->inst_l);
593 printf (" [%f]", *(double*)ins->inst_p0);
596 printf (" [%f]", *(float*)ins->inst_p0);
599 case OP_CALL_MEMBASE:
605 case OP_VCALL_MEMBASE:
608 case OP_VCALL2_MEMBASE:
610 case OP_VOIDCALL_MEMBASE:
612 MonoCallInst *call = (MonoCallInst*)ins;
615 if (ins->opcode == OP_VCALL || ins->opcode == OP_VCALL_REG || ins->opcode == OP_VCALL_MEMBASE) {
617 * These are lowered opcodes, but they are in the .md files since the old
618 * JIT passes them to backends.
621 printf (" R%d <-", ins->dreg);
625 char *full_name = mono_method_full_name (call->method, TRUE);
626 printf (" [%s]", full_name);
628 } else if (call->fptr_is_patch) {
629 MonoJumpInfo *ji = (MonoJumpInfo*)call->fptr;
633 } else if (call->fptr) {
634 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
636 printf (" [%s]", info->name);
639 list = call->out_ireg_args;
644 regpair = (guint32)(gssize)(list->data);
645 hreg = regpair >> 24;
646 reg = regpair & 0xffffff;
648 printf (" [%s <- R%d]", mono_arch_regname (hreg), reg);
650 list = g_slist_next (list);
655 case OP_CALL_HANDLER:
656 printf (" [B%d]", ins->inst_target_bb->block_num);
678 if (!ins->inst_false_bb)
679 printf (" [B%d]", ins->inst_true_bb->block_num);
681 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
683 case OP_LIVERANGE_START:
684 case OP_LIVERANGE_END:
685 case OP_GC_LIVENESS_DEF:
686 case OP_GC_LIVENESS_USE:
687 printf (" R%d", (int)ins->inst_c1);
690 printf (" il: %x", (int)ins->inst_imm);
696 if (spec [MONO_INST_CLOB])
697 printf (" clobbers: %c", spec [MONO_INST_CLOB]);
702 print_regtrack (RegTrack *t, int num)
708 for (i = 0; i < num; ++i) {
711 if (i >= MONO_MAX_IREGS) {
712 g_snprintf (buf, sizeof(buf), "R%d", i);
715 r = mono_arch_regname (i);
716 printf ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].killed_in);
722 mono_print_ji (const MonoJumpInfo *ji)
727 mono_print_ins_index (int i, MonoInst *ins)
730 #endif /* !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT) */
733 mono_print_ins (MonoInst *ins)
735 mono_print_ins_index (-1, ins);
739 insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst* to_insert)
742 * If this function is called multiple times, the new instructions are inserted
743 * in the proper order.
745 mono_bblock_insert_before_ins (bb, ins, to_insert);
749 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst **last, MonoInst* to_insert)
752 * If this function is called multiple times, the new instructions are inserted in
755 mono_bblock_insert_after_ins (bb, *last, to_insert);
761 get_vreg_bank (MonoCompile *cfg, int reg, int bank)
763 if (vreg_is_ref (cfg, reg))
764 return MONO_REG_INT_REF;
765 else if (vreg_is_mp (cfg, reg))
766 return MONO_REG_INT_MP;
772 * Force the spilling of the variable in the symbolic register 'reg', and free
773 * the hreg it was assigned to.
776 spill_vreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int reg, int bank)
781 MonoRegState *rs = cfg->rs;
783 symbolic = rs->symbolic [bank];
784 sel = rs->vassign [reg];
786 /* the vreg we need to spill lives in another logical reg bank */
787 bank = translate_bank (cfg->rs, bank, sel);
789 /*i = rs->isymbolic [sel];
790 g_assert (i == reg);*/
792 spill = ++cfg->spill_count;
793 rs->vassign [i] = -spill - 1;
794 if (G_UNLIKELY (bank))
795 mono_regstate_free_general (rs, sel, bank);
797 mono_regstate_free_int (rs, sel);
798 /* we need to create a spill var and insert a load to sel after the current instruction */
799 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
801 load->inst_basereg = cfg->frame_reg;
802 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, reg, bank));
803 insert_after_ins (bb, ins, last, load);
804 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)));
805 if (G_UNLIKELY (bank))
806 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
808 i = mono_regstate_alloc_int (rs, regmask (sel));
811 if (G_UNLIKELY (bank))
812 mono_regstate_free_general (rs, sel, bank);
814 mono_regstate_free_int (rs, sel);
817 /* This isn't defined on older glib versions and on some platforms */
818 #ifndef G_GUINT64_FORMAT
819 #define G_GUINT64_FORMAT "ul"
823 get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t regmask, int reg, int bank)
826 int i, sel, spill, num_sregs;
827 int sregs [MONO_MAX_SRC_REGS];
829 MonoRegState *rs = cfg->rs;
831 symbolic = rs->symbolic [bank];
833 g_assert (bank < MONO_NUM_REGBANKS);
835 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));
836 /* exclude the registers in the current instruction */
837 num_sregs = mono_inst_get_src_registers (ins, sregs);
838 for (i = 0; i < num_sregs; ++i) {
839 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))) {
840 if (is_soft_reg (sregs [i], bank))
841 regmask &= ~ (regmask (rs->vassign [sregs [i]]));
843 regmask &= ~ (regmask (sregs [i]));
844 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i + 1, mono_regname_full (sregs [i], bank), sregs [i]));
847 if ((dreg_bank_ins (ins) == bank) && (reg != ins->dreg) && reg_is_freeable (ins->dreg, bank)) {
848 regmask &= ~ (regmask (ins->dreg));
849 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins->dreg, bank)));
852 DEBUG (printf ("\t\tavailable regmask: 0x%08llu\n", (unsigned long long)regmask));
853 g_assert (regmask); /* need at least a register we can free */
855 /* we should track prev_use and spill the register that's farther */
856 if (G_UNLIKELY (bank)) {
857 for (i = 0; i < regbank_size [bank]; ++i) {
858 if (regmask & (regmask (i))) {
861 /* the vreg we need to load lives in another logical bank */
862 bank = translate_bank (cfg->rs, bank, sel);
864 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
869 i = rs->symbolic [bank] [sel];
870 spill = ++cfg->spill_count;
871 rs->vassign [i] = -spill - 1;
872 mono_regstate_free_general (rs, sel, bank);
875 for (i = 0; i < MONO_MAX_IREGS; ++i) {
876 if (regmask & (regmask (i))) {
878 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), rs->isymbolic [sel]));
883 i = rs->isymbolic [sel];
884 spill = ++cfg->spill_count;
885 rs->vassign [i] = -spill - 1;
886 mono_regstate_free_int (rs, sel);
889 /* we need to create a spill var and insert a load to sel after the current instruction */
890 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
892 load->inst_basereg = cfg->frame_reg;
893 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, i, bank));
894 insert_after_ins (bb, ins, last, load);
895 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)));
896 if (G_UNLIKELY (bank))
897 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
899 i = mono_regstate_alloc_int (rs, regmask (sel));
908 * Free up the hreg HREG by spilling the vreg allocated to it.
911 free_up_hreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int hreg, int bank)
913 if (G_UNLIKELY (bank)) {
914 if (!(cfg->rs->free_mask [bank] & (regmask (hreg)))) {
915 bank = translate_bank (cfg->rs, bank, hreg);
916 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
917 spill_vreg (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
921 if (!(cfg->rs->ifree_mask & (regmask (hreg)))) {
922 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->isymbolic [hreg]));
923 spill_vreg (cfg, bb, last, ins, cfg->rs->isymbolic [hreg], bank);
929 create_copy_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, int dest, int src, MonoInst *ins, const unsigned char *ip, int bank)
933 MONO_INST_NEW (cfg, copy, regbank_move_ops [bank]);
939 mono_bblock_insert_after_ins (bb, ins, copy);
942 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src, bank), mono_regname_full (dest, bank)));
946 static inline const char*
947 regbank_to_string (int bank)
949 if (bank == MONO_REG_INT_REF)
951 else if (bank == MONO_REG_INT_MP)
958 create_spilled_store (MonoCompile *cfg, MonoBasicBlock *bb, int spill, int reg, int prev_reg, MonoInst **last, MonoInst *ins, MonoInst *insert_before, int bank)
960 MonoInst *store, *def;
962 bank = get_vreg_bank (cfg, prev_reg, bank);
964 MONO_INST_NEW (cfg, store, regbank_store_ops [bank]);
966 store->inst_destbasereg = cfg->frame_reg;
967 store->inst_offset = mono_spillvar_offset (cfg, spill, bank);
969 mono_bblock_insert_after_ins (bb, ins, store);
971 } else if (insert_before) {
972 insert_before_ins (bb, insert_before, store);
974 g_assert_not_reached ();
976 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)));
978 if (((bank == MONO_REG_INT_REF) || (bank == MONO_REG_INT_MP)) && cfg->compute_gc_maps) {
979 g_assert (prev_reg != -1);
980 MONO_INST_NEW (cfg, def, OP_GC_SPILL_SLOT_LIVENESS_DEF);
981 def->inst_c0 = spill;
983 mono_bblock_insert_after_ins (bb, store, def);
987 /* flags used in reginfo->flags */
989 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
990 MONO_FP_NEEDS_SPILL = regmask (1),
991 MONO_FP_NEEDS_LOAD = regmask (2)
995 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
999 if (info && info->preferred_mask) {
1000 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
1002 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
1007 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
1009 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
1015 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
1019 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
1022 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1028 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
1030 if (G_UNLIKELY (bank))
1031 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1033 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
1037 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
1039 if (G_UNLIKELY (bank)) {
1042 g_assert (reg >= regbank_size [bank]);
1043 g_assert (hreg < regbank_size [bank]);
1044 g_assert (! is_global_freg (hreg));
1046 rs->vassign [reg] = hreg;
1047 rs->symbolic [bank] [hreg] = reg;
1048 rs->free_mask [bank] &= ~ (regmask (hreg));
1050 mirrored_bank = get_mirrored_bank (bank);
1051 if (mirrored_bank == -1)
1054 /* Make sure the other logical reg bank that this bank shares
1055 * a single hard reg bank knows that this hard reg is not free.
1057 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
1059 /* Mark the other logical bank that the this bank shares
1060 * a single hard reg bank with as mirrored.
1062 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
1066 g_assert (reg >= MONO_MAX_IREGS);
1067 g_assert (hreg < MONO_MAX_IREGS);
1068 #if !defined(TARGET_ARM) && !defined(TARGET_ARM64)
1069 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
1070 /* On arm64, rgctx_reg is a global hreg, and it is used to pass an argument */
1071 g_assert (! is_global_ireg (hreg));
1074 rs->vassign [reg] = hreg;
1075 rs->isymbolic [hreg] = reg;
1076 rs->ifree_mask &= ~ (regmask (hreg));
1080 static inline regmask_t
1081 get_callee_mask (const char spec)
1083 if (G_UNLIKELY (reg_bank (spec)))
1084 return regbank_callee_regs [reg_bank (spec)];
1085 return MONO_ARCH_CALLEE_REGS;
1088 static gint8 desc_to_fixed_reg [256];
1089 static gboolean desc_to_fixed_reg_inited = FALSE;
1094 * Local register allocation.
1095 * We first scan the list of instructions and we save the liveness info of
1096 * each register (when the register is first used, when it's value is set etc.).
1097 * We also reverse the list of instructions because assigning registers backwards allows
1098 * for more tricks to be used.
1101 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1103 MonoInst *ins, *prev, *last;
1105 MonoRegState *rs = cfg->rs;
1109 unsigned char spec_src1, spec_dest;
1111 #if MONO_ARCH_USE_FPSTACK
1112 gboolean has_fp = FALSE;
1117 int sregs [MONO_MAX_SRC_REGS];
1122 if (!desc_to_fixed_reg_inited) {
1123 for (i = 0; i < 256; ++i)
1124 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1125 desc_to_fixed_reg_inited = TRUE;
1127 /* Validate the cpu description against the info in mini-ops.h */
1128 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_ARM64)
1129 for (i = OP_LOAD; i < OP_LAST; ++i) {
1132 spec = ins_get_spec (i);
1133 ispec = INS_INFO (i);
1135 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1136 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1137 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1138 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1139 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1140 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1145 rs->next_vreg = bb->max_vreg;
1146 mono_regstate_assign (rs);
1148 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1149 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1150 rs->free_mask [i] = regbank_callee_regs [i];
1152 max = rs->next_vreg;
1154 if (cfg->reginfo && cfg->reginfo_len < max)
1155 cfg->reginfo = NULL;
1157 reginfo = cfg->reginfo;
1159 cfg->reginfo_len = MAX (1024, max * 2);
1160 reginfo = cfg->reginfo = mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1163 g_assert (cfg->reginfo_len >= rs->next_vreg);
1165 if (cfg->verbose_level > 1) {
1166 /* print_regtrack reads the info of all variables */
1167 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1171 * For large methods, next_vreg can be very large, so g_malloc0 time can
1172 * be prohibitive. So we manually init the reginfo entries used by the
1175 for (ins = bb->code; ins; ins = ins->next) {
1176 spec = ins_get_spec (ins->opcode);
1178 if ((ins->dreg != -1) && (ins->dreg < max)) {
1179 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1180 #if SIZEOF_REGISTER == 4
1181 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1183 * In the new IR, the two vregs of the regpair do not alias the
1184 * original long vreg. shift the vreg here so the rest of the
1185 * allocator doesn't have to care about it.
1188 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1193 num_sregs = mono_inst_get_src_registers (ins, sregs);
1194 for (j = 0; j < num_sregs; ++j) {
1195 g_assert (sregs [j] != -1);
1196 if (sregs [j] < max) {
1197 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1198 #if SIZEOF_REGISTER == 4
1199 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1201 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1206 mono_inst_set_src_registers (ins, sregs);
1209 /*if (cfg->opt & MONO_OPT_COPYPROP)
1210 local_copy_prop (cfg, ins);*/
1213 DEBUG (printf ("\nLOCAL REGALLOC BLOCK %d:\n", bb->block_num));
1214 /* forward pass on the instructions to collect register liveness info */
1215 MONO_BB_FOR_EACH_INS (bb, ins) {
1216 spec = ins_get_spec (ins->opcode);
1217 spec_dest = spec [MONO_INST_DEST];
1219 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1220 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1223 DEBUG (mono_print_ins_index (i, ins));
1225 num_sregs = mono_inst_get_src_registers (ins, sregs);
1227 #if MONO_ARCH_USE_FPSTACK
1228 if (dreg_is_fp (spec)) {
1231 for (j = 0; j < num_sregs; ++j) {
1232 if (sreg_is_fp (j, spec))
1238 for (j = 0; j < num_sregs; ++j) {
1239 int sreg = sregs [j];
1240 int sreg_spec = spec [MONO_INST_SRC1 + j];
1242 bank = sreg_bank (j, spec);
1243 g_assert (sreg != -1);
1244 if (is_soft_reg (sreg, bank))
1245 /* This means the vreg is not local to this bb */
1246 g_assert (reginfo [sreg].born_in > 0);
1247 rs->vassign [sreg] = -1;
1248 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1249 //reginfo [ins->sreg2].last_use = i;
1250 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1251 /* The virtual register is allocated sequentially */
1252 rs->vassign [sreg + 1] = -1;
1253 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1254 //reginfo [ins->sreg2 + 1].last_use = i;
1255 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1256 reginfo [sreg + 1].born_in = i;
1262 mono_inst_set_src_registers (ins, sregs);
1267 bank = dreg_bank (spec);
1268 if (spec_dest != 'b') /* it's not just a base register */
1269 reginfo [ins->dreg].killed_in = i;
1270 g_assert (ins->dreg != -1);
1271 rs->vassign [ins->dreg] = -1;
1272 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1273 //reginfo [ins->dreg].last_use = i;
1274 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1275 reginfo [ins->dreg].born_in = i;
1277 dest_dreg = desc_to_fixed_reg [spec_dest];
1278 if (dest_dreg != -1)
1279 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1281 #ifdef MONO_ARCH_INST_FIXED_MASK
1282 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1285 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1286 /* The virtual register is allocated sequentially */
1287 rs->vassign [ins->dreg + 1] = -1;
1288 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1289 //reginfo [ins->dreg + 1].last_use = i;
1290 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1291 reginfo [ins->dreg + 1].born_in = i;
1292 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1293 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1299 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1300 /* A call instruction implicitly uses all registers in call->out_ireg_args */
1302 MonoCallInst *call = (MonoCallInst*)ins;
1305 list = call->out_ireg_args;
1311 regpair = (guint32)(gssize)(list->data);
1312 hreg = regpair >> 24;
1313 reg = regpair & 0xffffff;
1315 //reginfo [reg].prev_use = reginfo [reg].last_use;
1316 //reginfo [reg].last_use = i;
1318 list = g_slist_next (list);
1322 list = call->out_freg_args;
1328 regpair = (guint32)(gssize)(list->data);
1329 hreg = regpair >> 24;
1330 reg = regpair & 0xffffff;
1332 list = g_slist_next (list);
1342 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1343 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1344 int prev_dreg, clob_dreg;
1345 int dest_dreg, clob_reg;
1346 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1347 int dreg_high, sreg1_high;
1348 regmask_t dreg_mask, mask;
1349 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1350 regmask_t dreg_fixed_mask;
1351 const unsigned char *ip;
1353 spec = ins_get_spec (ins->opcode);
1354 spec_src1 = spec [MONO_INST_SRC1];
1355 spec_dest = spec [MONO_INST_DEST];
1362 dreg_mask = get_callee_mask (spec_dest);
1363 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1364 prev_sregs [j] = -1;
1365 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1366 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1367 #ifdef MONO_ARCH_INST_FIXED_MASK
1368 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1370 sreg_fixed_masks [j] = 0;
1374 DEBUG (printf ("processing:"));
1375 DEBUG (mono_print_ins_index (i, ins));
1384 dest_dreg = desc_to_fixed_reg [spec_dest];
1385 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1386 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1388 #ifdef MONO_ARCH_INST_FIXED_MASK
1389 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1391 dreg_fixed_mask = 0;
1394 num_sregs = mono_inst_get_src_registers (ins, sregs);
1397 * TRACK FIXED SREG2, 3, ...
1399 for (j = 1; j < num_sregs; ++j) {
1400 int sreg = sregs [j];
1401 int dest_sreg = dest_sregs [j];
1403 if (dest_sreg == -1)
1411 * We need to special case this, since on x86, there are only 3
1412 * free registers, and the code below assigns one of them to
1413 * sreg, so we can run out of registers when trying to assign
1414 * dreg. Instead, we just set up the register masks, and let the
1415 * normal sreg2 assignment code handle this. It would be nice to
1416 * do this for all the fixed reg cases too, but there is too much
1420 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1421 sreg_masks [j] = regmask (dest_sreg);
1422 for (k = 0; k < num_sregs; ++k) {
1424 sreg_masks [k] &= ~ (regmask (dest_sreg));
1428 * Spill sreg1/2 if they are assigned to dest_sreg.
1430 for (k = 0; k < num_sregs; ++k) {
1431 if (k != j && is_soft_reg (sregs [k], 0) && rs->vassign [sregs [k]] == dest_sreg)
1432 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1436 * We can also run out of registers while processing sreg2 if sreg3 is
1437 * assigned to another hreg, so spill sreg3 now.
1439 if (is_soft_reg (sreg, 0) && rs->vassign [sreg] >= 0 && rs->vassign [sreg] != dest_sreg) {
1440 spill_vreg (cfg, bb, tmp, ins, sreg, 0);
1445 if (rs->ifree_mask & (regmask (dest_sreg))) {
1446 if (is_global_ireg (sreg)) {
1448 /* Argument already in hard reg, need to copy */
1449 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1450 insert_before_ins (bb, ins, copy);
1451 for (k = 0; k < num_sregs; ++k) {
1453 sreg_masks [k] &= ~ (regmask (dest_sreg));
1456 dreg_mask &= ~ (regmask (dest_sreg));
1458 val = rs->vassign [sreg];
1460 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1461 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1462 } else if (val < -1) {
1464 g_assert_not_reached ();
1466 /* Argument already in hard reg, need to copy */
1467 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1470 insert_before_ins (bb, ins, copy);
1471 for (k = 0; k < num_sregs; ++k) {
1473 sreg_masks [k] &= ~ (regmask (dest_sreg));
1476 * Prevent the dreg from being allocated to dest_sreg
1477 * too, since it could force sreg1 to be allocated to
1478 * the same reg on x86.
1480 dreg_mask &= ~ (regmask (dest_sreg));
1484 gboolean need_spill = TRUE;
1485 gboolean need_assign = TRUE;
1488 dreg_mask &= ~ (regmask (dest_sreg));
1489 for (k = 0; k < num_sregs; ++k) {
1491 sreg_masks [k] &= ~ (regmask (dest_sreg));
1495 * First check if dreg is assigned to dest_sreg2, since we
1496 * can't spill a dreg.
1498 if (spec [MONO_INST_DEST])
1499 val = rs->vassign [ins->dreg];
1502 if (val == dest_sreg && ins->dreg != sreg) {
1504 * the destination register is already assigned to
1505 * dest_sreg2: we need to allocate another register for it
1506 * and then copy from this to dest_sreg2.
1509 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1510 g_assert (new_dest >= 0);
1511 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1513 prev_dreg = ins->dreg;
1514 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1515 clob_dreg = ins->dreg;
1516 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1517 mono_regstate_free_int (rs, dest_sreg);
1521 if (is_global_ireg (sreg)) {
1522 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1523 insert_before_ins (bb, ins, copy);
1524 need_assign = FALSE;
1527 val = rs->vassign [sreg];
1528 if (val == dest_sreg) {
1529 /* sreg2 is already assigned to the correct register */
1531 } else if (val < -1) {
1532 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1533 } else if (val >= 0) {
1534 /* sreg2 already assigned to another register */
1536 * We couldn't emit a copy from val to dest_sreg2, because
1537 * val might be spilled later while processing this
1538 * instruction. So we spill sreg2 so it can be allocated to
1541 free_up_hreg (cfg, bb, tmp, ins, val, 0);
1546 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1550 if (rs->vassign [sreg] < -1) {
1553 /* Need to emit a spill store */
1554 spill = - rs->vassign [sreg] - 1;
1555 create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, ins, bank);
1557 /* force-set sreg2 */
1558 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1561 sregs [j] = dest_sreg;
1563 mono_inst_set_src_registers (ins, sregs);
1568 bank = dreg_bank (spec);
1569 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1570 prev_dreg = ins->dreg;
1573 if (spec_dest == 'b') {
1575 * The dest reg is read by the instruction, not written, so
1576 * avoid allocating sreg1/sreg2 to the same reg.
1578 if (dest_sregs [0] != -1)
1579 dreg_mask &= ~ (regmask (dest_sregs [0]));
1580 for (j = 1; j < num_sregs; ++j) {
1581 if (dest_sregs [j] != -1)
1582 dreg_mask &= ~ (regmask (dest_sregs [j]));
1585 val = rs->vassign [ins->dreg];
1586 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1587 /* DREG is already allocated to a register needed for sreg1 */
1588 spill_vreg (cfg, bb, tmp, ins, ins->dreg, 0);
1593 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1594 * various complex situations.
1596 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1597 guint32 dreg2, dest_dreg2;
1599 g_assert (is_soft_reg (ins->dreg, bank));
1601 if (dest_dreg != -1) {
1602 if (rs->vassign [ins->dreg] != dest_dreg)
1603 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, 0);
1605 dreg2 = ins->dreg + 1;
1606 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1607 if (dest_dreg2 != -1) {
1608 if (rs->vassign [dreg2] != dest_dreg2)
1609 free_up_hreg (cfg, bb, tmp, ins, dest_dreg2, 0);
1614 if (dreg_fixed_mask) {
1616 if (is_global_ireg (ins->dreg)) {
1618 * The argument is already in a hard reg, but that reg is
1619 * not usable by this instruction, so allocate a new one.
1621 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1623 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1624 mono_regstate_free_int (rs, val);
1630 dreg_mask &= dreg_fixed_mask;
1633 if (is_soft_reg (ins->dreg, bank)) {
1634 val = rs->vassign [ins->dreg];
1639 /* the register gets spilled after this inst */
1642 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1643 assign_reg (cfg, rs, ins->dreg, val, bank);
1645 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, NULL, bank);
1648 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1652 /* Handle regpairs */
1653 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1654 int reg2 = prev_dreg + 1;
1657 g_assert (prev_dreg > -1);
1658 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1659 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1662 mask &= ~regmask (X86_ECX);
1664 val = rs->vassign [reg2];
1668 /* the register gets spilled after this inst */
1671 val = mono_regstate_alloc_int (rs, mask);
1673 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1675 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, NULL, bank);
1678 if (! (mask & (regmask (val)))) {
1679 val = mono_regstate_alloc_int (rs, mask);
1681 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1683 /* Reallocate hreg to the correct register */
1684 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1686 mono_regstate_free_int (rs, rs->vassign [reg2]);
1690 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1691 assign_reg (cfg, rs, reg2, val, bank);
1694 ins->backend.reg3 = val;
1696 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1697 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1698 mono_regstate_free_int (rs, val);
1702 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1704 * In theory, we could free up the hreg even if the vreg is alive,
1705 * but branches inside bblocks force us to assign the same hreg
1706 * to a vreg every time it is encountered.
1708 int dreg = rs->vassign [prev_dreg];
1709 g_assert (dreg >= 0);
1710 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1711 if (G_UNLIKELY (bank))
1712 mono_regstate_free_general (rs, dreg, bank);
1714 mono_regstate_free_int (rs, dreg);
1715 rs->vassign [prev_dreg] = -1;
1718 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1719 /* this instruction only outputs to dest_dreg, need to copy */
1720 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1721 ins->dreg = dest_dreg;
1723 if (G_UNLIKELY (bank)) {
1724 /* the register we need to free up may be used in another logical regbank
1725 * so do a translate just in case.
1727 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1728 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1729 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1732 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1733 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, bank);
1737 if (spec_dest == 'b') {
1739 * The dest reg is read by the instruction, not written, so
1740 * avoid allocating sreg1/sreg2 to the same reg.
1742 for (j = 0; j < num_sregs; ++j)
1743 if (!sreg_bank (j, spec))
1744 sreg_masks [j] &= ~ (regmask (ins->dreg));
1750 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1751 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1752 free_up_hreg (cfg, bb, tmp, ins, clob_reg, 0);
1755 if (spec [MONO_INST_CLOB] == 'c') {
1756 int j, s, dreg, dreg2, cur_bank;
1759 clob_mask = MONO_ARCH_CALLEE_REGS;
1761 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1763 * Need to avoid spilling the dreg since the dreg is not really
1764 * clobbered by the call.
1766 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1767 dreg = rs->vassign [prev_dreg];
1771 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1772 dreg2 = rs->vassign [prev_dreg + 1];
1776 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1778 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1779 if ((j != dreg) && (j != dreg2))
1780 free_up_hreg (cfg, bb, tmp, ins, j, 0);
1781 else if (rs->isymbolic [j])
1782 /* The hreg is assigned to the dreg of this instruction */
1783 rs->vassign [rs->isymbolic [j]] = -1;
1784 mono_regstate_free_int (rs, j);
1789 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1790 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1791 clob_mask = regbank_callee_regs [cur_bank];
1792 if ((prev_dreg != -1) && reg_bank (spec_dest))
1793 dreg = rs->vassign [prev_dreg];
1797 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1799 /* we are looping though the banks in the outer loop
1800 * so, we don't need to deal with mirrored hregs
1801 * because we will get them in one of the other bank passes.
1803 if (is_hreg_mirrored (rs, cur_bank, j))
1807 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s)) {
1809 free_up_hreg (cfg, bb, tmp, ins, j, cur_bank);
1810 else if (rs->symbolic [cur_bank] [j])
1811 /* The hreg is assigned to the dreg of this instruction */
1812 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1813 mono_regstate_free_general (rs, j, cur_bank);
1821 * TRACK ARGUMENT REGS
1823 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1824 MonoCallInst *call = (MonoCallInst*)ins;
1828 * This needs to be done before assigning sreg1, so sreg1 will
1829 * not be assigned one of the argument regs.
1833 * Assign all registers in call->out_reg_args to the proper
1834 * argument registers.
1837 list = call->out_ireg_args;
1843 regpair = (guint32)(gssize)(list->data);
1844 hreg = regpair >> 24;
1845 reg = regpair & 0xffffff;
1847 assign_reg (cfg, rs, reg, hreg, 0);
1849 sreg_masks [0] &= ~(regmask (hreg));
1851 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1853 list = g_slist_next (list);
1857 list = call->out_freg_args;
1863 regpair = (guint32)(gssize)(list->data);
1864 hreg = regpair >> 24;
1865 reg = regpair & 0xffffff;
1867 assign_reg (cfg, rs, reg, hreg, 1);
1869 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1871 list = g_slist_next (list);
1879 bank = sreg1_bank (spec);
1880 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1881 int sreg1 = sregs [0];
1882 int dest_sreg1 = dest_sregs [0];
1884 g_assert (is_soft_reg (sreg1, bank));
1886 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1887 if (dest_sreg1 != -1)
1888 g_assert (dest_sreg1 == ins->dreg);
1889 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1890 g_assert (val >= 0);
1892 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1894 g_assert_not_reached ();
1896 assign_reg (cfg, rs, sreg1, val, bank);
1898 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1900 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1901 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1902 g_assert (val >= 0);
1904 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1906 g_assert_not_reached ();
1908 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1910 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1912 /* Skip rest of this section */
1913 dest_sregs [0] = -1;
1916 if (sreg_fixed_masks [0]) {
1918 if (is_global_ireg (sregs [0])) {
1920 * The argument is already in a hard reg, but that reg is
1921 * not usable by this instruction, so allocate a new one.
1923 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1925 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1926 mono_regstate_free_int (rs, val);
1927 dest_sregs [0] = val;
1929 /* Fall through to the dest_sreg1 != -1 case */
1932 sreg_masks [0] &= sreg_fixed_masks [0];
1935 if (dest_sregs [0] != -1) {
1936 sreg_masks [0] = regmask (dest_sregs [0]);
1938 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1939 free_up_hreg (cfg, bb, tmp, ins, dest_sregs [0], 0);
1941 if (is_global_ireg (sregs [0])) {
1942 /* The argument is already in a hard reg, need to copy */
1943 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1944 insert_before_ins (bb, ins, copy);
1945 sregs [0] = dest_sregs [0];
1949 if (is_soft_reg (sregs [0], bank)) {
1950 val = rs->vassign [sregs [0]];
1951 prev_sregs [0] = sregs [0];
1955 /* the register gets spilled after this inst */
1959 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1961 * Allocate the same hreg to sreg1 as well so the
1962 * peephole can get rid of the move.
1964 sreg_masks [0] = regmask (ins->dreg);
1967 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1968 /* Allocate the same reg to sreg1 to avoid a copy later */
1969 sreg_masks [0] = regmask (ins->dreg);
1971 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
1972 assign_reg (cfg, rs, sregs [0], val, bank);
1973 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
1977 * Need to insert before the instruction since it can
1980 create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, ins, bank);
1983 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
1984 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
1985 insert_before_ins (bb, ins, copy);
1986 for (j = 1; j < num_sregs; ++j)
1987 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
1988 val = dest_sregs [0];
1994 prev_sregs [0] = -1;
1996 mono_inst_set_src_registers (ins, sregs);
1998 for (j = 1; j < num_sregs; ++j)
1999 sreg_masks [j] &= ~(regmask (sregs [0]));
2001 /* Handle the case when sreg1 is a regpair but dreg is not */
2002 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
2003 int reg2 = prev_sregs [0] + 1;
2006 g_assert (prev_sregs [0] > -1);
2007 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
2008 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
2009 val = rs->vassign [reg2];
2013 /* the register gets spilled after this inst */
2016 val = mono_regstate_alloc_int (rs, mask);
2018 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2020 g_assert_not_reached ();
2023 if (! (mask & (regmask (val)))) {
2024 /* The vreg is already allocated to a wrong hreg */
2026 g_assert_not_reached ();
2028 val = mono_regstate_alloc_int (rs, mask);
2030 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2032 /* Reallocate hreg to the correct register */
2033 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
2035 mono_regstate_free_int (rs, rs->vassign [reg2]);
2041 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
2042 assign_reg (cfg, rs, reg2, val, bank);
2045 /* Handle dreg==sreg1 */
2046 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
2047 MonoInst *sreg2_copy = NULL;
2049 int bank = reg_bank (spec_src1);
2051 if (ins->dreg == sregs [1]) {
2053 * copying sreg1 to dreg could clobber sreg2, so allocate a new
2056 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
2058 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
2059 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
2060 prev_sregs [1] = sregs [1] = reg2;
2062 if (G_UNLIKELY (bank))
2063 mono_regstate_free_general (rs, reg2, bank);
2065 mono_regstate_free_int (rs, reg2);
2068 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
2069 /* Copying sreg1_high to dreg could also clobber sreg2 */
2070 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
2072 g_assert_not_reached ();
2075 * sreg1 and dest are already allocated to the same regpair by the
2076 * SREG1 allocation code.
2078 g_assert (sregs [0] == ins->dreg);
2079 g_assert (dreg_high == sreg1_high);
2082 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
2083 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
2084 insert_before_ins (bb, ins, copy);
2087 insert_before_ins (bb, copy, sreg2_copy);
2090 * Need to prevent sreg2 to be allocated to sreg1, since that
2091 * would screw up the previous copy.
2093 sreg_masks [1] &= ~ (regmask (sregs [0]));
2094 /* we set sreg1 to dest as well */
2095 prev_sregs [0] = sregs [0] = ins->dreg;
2096 sreg_masks [1] &= ~ (regmask (ins->dreg));
2098 mono_inst_set_src_registers (ins, sregs);
2101 * TRACK SREG2, 3, ...
2103 for (j = 1; j < num_sregs; ++j) {
2106 bank = sreg_bank (j, spec);
2107 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
2108 g_assert_not_reached ();
2110 if (dest_sregs [j] != -1 && is_global_ireg (sregs [j])) {
2112 * Argument already in a global hard reg, copy it to the fixed reg, without
2113 * allocating it to the fixed reg.
2115 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [j], sregs [j], NULL, ip, 0);
2116 insert_before_ins (bb, ins, copy);
2117 sregs [j] = dest_sregs [j];
2118 } else if (is_soft_reg (sregs [j], bank)) {
2119 val = rs->vassign [sregs [j]];
2121 if (dest_sregs [j] != -1 && val >= 0 && dest_sregs [j] != val) {
2123 * The sreg is already allocated to a hreg, but not to the fixed
2124 * reg required by the instruction. Spill the sreg, so it can be
2125 * allocated to the fixed reg by the code below.
2127 /* Currently, this code should only be hit for CAS */
2128 spill_vreg (cfg, bb, tmp, ins, sregs [j], 0);
2129 val = rs->vassign [sregs [j]];
2135 /* the register gets spilled after this inst */
2138 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
2139 assign_reg (cfg, rs, sregs [j], val, bank);
2140 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
2143 * Need to insert before the instruction since it can
2146 create_spilled_store (cfg, bb, spill, val, sregs [j], tmp, NULL, ins, bank);
2150 for (k = j + 1; k < num_sregs; ++k)
2151 sreg_masks [k] &= ~ (regmask (sregs [j]));
2154 prev_sregs [j] = -1;
2157 mono_inst_set_src_registers (ins, sregs);
2160 /* Do this only for CAS for now */
2161 for (j = 1; j < num_sregs; ++j) {
2162 int sreg = sregs [j];
2163 int dest_sreg = dest_sregs [j];
2165 if (j == 2 && dest_sreg != -1) {
2168 g_assert (sreg == dest_sreg);
2170 for (k = 0; k < num_sregs; ++k) {
2172 g_assert (sregs [k] != dest_sreg);
2177 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2178 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2179 mono_regstate_free_int (rs, ins->sreg1);
2181 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2182 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2183 mono_regstate_free_int (rs, ins->sreg2);
2186 DEBUG (mono_print_ins_index (i, ins));
2189 // FIXME: Set MAX_FREGS to 8
2190 // FIXME: Optimize generated code
2191 #if MONO_ARCH_USE_FPSTACK
2193 * Make a forward pass over the code, simulating the fp stack, making sure the
2194 * arguments required by the fp opcodes are at the top of the stack.
2197 MonoInst *prev = NULL;
2201 g_assert (num_sregs <= 2);
2203 for (ins = bb->code; ins; ins = ins->next) {
2204 spec = ins_get_spec (ins->opcode);
2206 DEBUG (printf ("processing:"));
2207 DEBUG (mono_print_ins_index (0, ins));
2209 if (ins->opcode == OP_FMOVE) {
2210 /* Do it by renaming the source to the destination on the stack */
2211 // FIXME: Is this correct ?
2212 for (i = 0; i < sp; ++i)
2213 if (fpstack [i] == ins->sreg1)
2214 fpstack [i] = ins->dreg;
2219 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2220 /* Arg1 must be in %st(1) */
2224 while ((i < sp) && (fpstack [i] != ins->sreg1))
2228 if (sp - 1 - i > 0) {
2229 /* First move it to %st(0) */
2230 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2232 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2233 fxch->inst_imm = sp - 1 - i;
2235 mono_bblock_insert_after_ins (bb, prev, fxch);
2238 tmp = fpstack [sp - 1];
2239 fpstack [sp - 1] = fpstack [i];
2243 /* Then move it to %st(1) */
2244 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2246 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2249 mono_bblock_insert_after_ins (bb, prev, fxch);
2252 tmp = fpstack [sp - 1];
2253 fpstack [sp - 1] = fpstack [sp - 2];
2254 fpstack [sp - 2] = tmp;
2257 if (sreg2_is_fp (spec)) {
2260 if (fpstack [sp - 1] != ins->sreg2) {
2264 while ((i < sp) && (fpstack [i] != ins->sreg2))
2268 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2270 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2271 fxch->inst_imm = sp - 1 - i;
2273 mono_bblock_insert_after_ins (bb, prev, fxch);
2276 tmp = fpstack [sp - 1];
2277 fpstack [sp - 1] = fpstack [i];
2284 if (sreg1_is_fp (spec)) {
2287 if (fpstack [sp - 1] != ins->sreg1) {
2291 while ((i < sp) && (fpstack [i] != ins->sreg1))
2295 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2297 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2298 fxch->inst_imm = sp - 1 - i;
2300 mono_bblock_insert_after_ins (bb, prev, fxch);
2303 tmp = fpstack [sp - 1];
2304 fpstack [sp - 1] = fpstack [i];
2311 if (dreg_is_fp (spec)) {
2313 fpstack [sp ++] = ins->dreg;
2316 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2318 for (i = 0; i < sp; ++i)
2319 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2326 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2327 /* Remove remaining items from the fp stack */
2329 * These can remain for example as a result of a dead fmove like in
2330 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2333 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2334 mono_add_ins_to_end (bb, ins);
2343 mono_opcode_to_cond (int opcode)
2353 case OP_COND_EXC_EQ:
2354 case OP_COND_EXC_IEQ:
2363 case OP_COND_EXC_NE_UN:
2364 case OP_COND_EXC_INE_UN:
2365 case OP_CMOV_INE_UN:
2366 case OP_CMOV_LNE_UN:
2391 case OP_COND_EXC_LT:
2392 case OP_COND_EXC_ILT:
2403 case OP_COND_EXC_GT:
2404 case OP_COND_EXC_IGT:
2413 case OP_COND_EXC_LE_UN:
2414 case OP_COND_EXC_ILE_UN:
2415 case OP_CMOV_ILE_UN:
2416 case OP_CMOV_LLE_UN:
2423 case OP_CMOV_IGE_UN:
2424 case OP_CMOV_LGE_UN:
2433 case OP_COND_EXC_LT_UN:
2434 case OP_COND_EXC_ILT_UN:
2435 case OP_CMOV_ILT_UN:
2436 case OP_CMOV_LLT_UN:
2445 case OP_COND_EXC_GT_UN:
2446 case OP_COND_EXC_IGT_UN:
2447 case OP_CMOV_IGT_UN:
2448 case OP_CMOV_LGT_UN:
2451 printf ("%s\n", mono_inst_name (opcode));
2452 g_assert_not_reached ();
2458 mono_negate_cond (CompRelation cond)
2482 g_assert_not_reached ();
2487 mono_opcode_to_type (int opcode, int cmp_opcode)
2489 if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2491 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2493 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2495 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2497 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2499 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2501 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2503 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2505 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2506 switch (cmp_opcode) {
2508 case OP_ICOMPARE_IMM:
2514 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2519 #endif /* DISABLE_JIT */
2522 mono_is_regsize_var (MonoType *t)
2526 t = mono_type_get_underlying_type (t);
2528 case MONO_TYPE_BOOLEAN:
2529 case MONO_TYPE_CHAR:
2539 case MONO_TYPE_FNPTR:
2540 #if SIZEOF_REGISTER == 8
2545 case MONO_TYPE_OBJECT:
2546 case MONO_TYPE_STRING:
2547 case MONO_TYPE_CLASS:
2548 case MONO_TYPE_SZARRAY:
2549 case MONO_TYPE_ARRAY:
2551 case MONO_TYPE_GENERICINST:
2552 if (!mono_type_generic_inst_is_valuetype (t))
2555 case MONO_TYPE_VALUETYPE:
2565 * mono_peephole_ins:
2567 * Perform some architecture independent peephole optimizations.
2570 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2572 MonoInst *last_ins = ins->prev;
2574 switch (ins->opcode) {
2576 /* remove unnecessary multiplication with 1 */
2577 if (ins->inst_imm == 1) {
2578 if (ins->dreg != ins->sreg1)
2579 ins->opcode = OP_MOVE;
2581 MONO_DELETE_INS (bb, ins);
2584 case OP_LOAD_MEMBASE:
2585 case OP_LOADI4_MEMBASE:
2587 * Note: if reg1 = reg2 the load op is removed
2589 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2590 * OP_LOAD_MEMBASE offset(basereg), reg2
2592 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2593 * OP_MOVE reg1, reg2
2595 if (last_ins && last_ins->opcode == OP_GC_LIVENESS_DEF)
2596 last_ins = last_ins->prev;
2598 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2599 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2600 ins->inst_basereg == last_ins->inst_destbasereg &&
2601 ins->inst_offset == last_ins->inst_offset) {
2602 if (ins->dreg == last_ins->sreg1) {
2603 MONO_DELETE_INS (bb, ins);
2606 ins->opcode = OP_MOVE;
2607 ins->sreg1 = last_ins->sreg1;
2611 * Note: reg1 must be different from the basereg in the second load
2612 * Note: if reg1 = reg2 is equal then second load is removed
2614 * OP_LOAD_MEMBASE offset(basereg), reg1
2615 * OP_LOAD_MEMBASE offset(basereg), reg2
2617 * OP_LOAD_MEMBASE offset(basereg), reg1
2618 * OP_MOVE reg1, reg2
2620 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2621 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2622 ins->inst_basereg != last_ins->dreg &&
2623 ins->inst_basereg == last_ins->inst_basereg &&
2624 ins->inst_offset == last_ins->inst_offset) {
2626 if (ins->dreg == last_ins->dreg) {
2627 MONO_DELETE_INS (bb, ins);
2629 ins->opcode = OP_MOVE;
2630 ins->sreg1 = last_ins->dreg;
2633 //g_assert_not_reached ();
2637 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2638 * OP_LOAD_MEMBASE offset(basereg), reg
2640 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2641 * OP_ICONST reg, imm
2643 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2644 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2645 ins->inst_basereg == last_ins->inst_destbasereg &&
2646 ins->inst_offset == last_ins->inst_offset) {
2647 ins->opcode = OP_ICONST;
2648 ins->inst_c0 = last_ins->inst_imm;
2649 g_assert_not_reached (); // check this rule
2653 case OP_LOADI1_MEMBASE:
2654 case OP_LOADU1_MEMBASE:
2656 * Note: if reg1 = reg2 the load op is removed
2658 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2659 * OP_LOAD_MEMBASE offset(basereg), reg2
2661 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2662 * OP_MOVE reg1, reg2
2664 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2665 ins->inst_basereg == last_ins->inst_destbasereg &&
2666 ins->inst_offset == last_ins->inst_offset) {
2667 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2668 ins->sreg1 = last_ins->sreg1;
2671 case OP_LOADI2_MEMBASE:
2672 case OP_LOADU2_MEMBASE:
2674 * Note: if reg1 = reg2 the load op is removed
2676 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2677 * OP_LOAD_MEMBASE offset(basereg), reg2
2679 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2680 * OP_MOVE reg1, reg2
2682 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2683 ins->inst_basereg == last_ins->inst_destbasereg &&
2684 ins->inst_offset == last_ins->inst_offset) {
2685 #if SIZEOF_REGISTER == 8
2686 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2688 /* The definition of OP_PCONV_TO_U2 is wrong */
2689 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2691 ins->sreg1 = last_ins->sreg1;
2701 if (ins->dreg == ins->sreg1) {
2702 MONO_DELETE_INS (bb, ins);
2708 * OP_MOVE sreg, dreg
2709 * OP_MOVE dreg, sreg
2711 if (last_ins && last_ins->opcode == ins->opcode &&
2712 ins->sreg1 == last_ins->dreg &&
2713 ins->dreg == last_ins->sreg1) {
2714 MONO_DELETE_INS (bb, ins);
2718 MONO_DELETE_INS (bb, ins);
2724 mini_exception_id_by_name (const char *name)
2726 if (strcmp (name, "IndexOutOfRangeException") == 0)
2727 return MONO_EXC_INDEX_OUT_OF_RANGE;
2728 if (strcmp (name, "OverflowException") == 0)
2729 return MONO_EXC_OVERFLOW;
2730 if (strcmp (name, "ArithmeticException") == 0)
2731 return MONO_EXC_ARITHMETIC;
2732 if (strcmp (name, "DivideByZeroException") == 0)
2733 return MONO_EXC_DIVIDE_BY_ZERO;
2734 if (strcmp (name, "InvalidCastException") == 0)
2735 return MONO_EXC_INVALID_CAST;
2736 if (strcmp (name, "NullReferenceException") == 0)
2737 return MONO_EXC_NULL_REF;
2738 if (strcmp (name, "ArrayTypeMismatchException") == 0)
2739 return MONO_EXC_ARRAY_TYPE_MISMATCH;
2740 if (strcmp (name, "ArgumentException") == 0)
2741 return MONO_EXC_ARGUMENT;
2742 g_error ("Unknown intrinsic exception %s\n", name);
2746 #endif /* DISABLE_JIT */