2 * mini-codegen.c: Arch independent code generation functionality
4 * (C) 2003 Ximian, Inc.
13 #include <mono/metadata/appdomain.h>
14 #include <mono/metadata/debug-helpers.h>
15 #include <mono/metadata/threads.h>
16 #include <mono/metadata/profiler-private.h>
17 #include <mono/metadata/mempool-internals.h>
18 #include <mono/utils/mono-math.h>
22 #include "mini-arch.h"
24 #ifndef MONO_MAX_XREGS
26 #define MONO_MAX_XREGS 0
27 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
28 #define MONO_ARCH_CALLEE_XREGS 0
33 #define MONO_ARCH_BANK_MIRRORED -2
35 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
37 #ifndef MONO_ARCH_NEED_SIMD_BANK
38 #error "MONO_ARCH_USE_SHARED_FP_SIMD_BANK needs MONO_ARCH_NEED_SIMD_BANK to work"
41 #define get_mirrored_bank(bank) (((bank) == MONO_REG_SIMD ) ? MONO_REG_DOUBLE : (((bank) == MONO_REG_DOUBLE ) ? MONO_REG_SIMD : -1))
43 #define is_hreg_mirrored(rs, bank, hreg) ((rs)->symbolic [(bank)] [(hreg)] == MONO_ARCH_BANK_MIRRORED)
49 #define get_mirrored_bank(bank) (-1)
51 #define is_hreg_mirrored(rs, bank, hreg) (0)
56 /* If the bank is mirrored return the true logical bank that the register in the
57 * physical register bank is allocated to.
59 static inline int translate_bank (MonoRegState *rs, int bank, int hreg) {
60 return is_hreg_mirrored (rs, bank, hreg) ? get_mirrored_bank (bank) : bank;
64 * Every hardware register belongs to a register type or register bank. bank 0
65 * contains the int registers, bank 1 contains the fp registers.
66 * int registers are used 99% of the time, so they are special cased in a lot of
70 static const int regbank_size [] = {
78 static const int regbank_load_ops [] = {
86 static const int regbank_store_ops [] = {
87 OP_STORER_MEMBASE_REG,
88 OP_STORER8_MEMBASE_REG,
89 OP_STORER_MEMBASE_REG,
90 OP_STORER_MEMBASE_REG,
94 static const int regbank_move_ops [] = {
102 #define regmask(reg) (((regmask_t)1) << (reg))
104 static const regmask_t regbank_callee_saved_regs [] = {
105 MONO_ARCH_CALLEE_SAVED_REGS,
106 MONO_ARCH_CALLEE_SAVED_FREGS,
107 MONO_ARCH_CALLEE_SAVED_REGS,
108 MONO_ARCH_CALLEE_SAVED_REGS,
109 MONO_ARCH_CALLEE_SAVED_XREGS,
112 static const regmask_t regbank_callee_regs [] = {
113 MONO_ARCH_CALLEE_REGS,
114 MONO_ARCH_CALLEE_FREGS,
115 MONO_ARCH_CALLEE_REGS,
116 MONO_ARCH_CALLEE_REGS,
117 MONO_ARCH_CALLEE_XREGS,
120 static const int regbank_spill_var_size[] = {
125 16 /*FIXME make this a constant. Maybe MONO_ARCH_SIMD_VECTOR_SIZE? */
128 #define DEBUG(a) MINI_DEBUG(cfg->verbose_level, 3, a;)
131 mono_regstate_assign (MonoRegState *rs)
133 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
134 /* The regalloc may fail if fp and simd logical regbanks share the same physical reg bank and
135 * if the values here are not the same.
137 g_assert(regbank_callee_regs [MONO_REG_SIMD] == regbank_callee_regs [MONO_REG_DOUBLE]);
138 g_assert(regbank_callee_saved_regs [MONO_REG_SIMD] == regbank_callee_saved_regs [MONO_REG_DOUBLE]);
139 g_assert(regbank_size [MONO_REG_SIMD] == regbank_size [MONO_REG_DOUBLE]);
142 if (rs->next_vreg > rs->vassign_size) {
143 g_free (rs->vassign);
144 rs->vassign_size = MAX (rs->next_vreg, 256);
145 rs->vassign = g_malloc (rs->vassign_size * sizeof (gint32));
148 memset (rs->isymbolic, 0, MONO_MAX_IREGS * sizeof (rs->isymbolic [0]));
149 memset (rs->fsymbolic, 0, MONO_MAX_FREGS * sizeof (rs->fsymbolic [0]));
151 rs->symbolic [MONO_REG_INT] = rs->isymbolic;
152 rs->symbolic [MONO_REG_DOUBLE] = rs->fsymbolic;
154 #ifdef MONO_ARCH_NEED_SIMD_BANK
155 memset (rs->xsymbolic, 0, MONO_MAX_XREGS * sizeof (rs->xsymbolic [0]));
156 rs->symbolic [MONO_REG_SIMD] = rs->xsymbolic;
161 mono_regstate_alloc_int (MonoRegState *rs, regmask_t allow)
163 regmask_t mask = allow & rs->ifree_mask;
165 #if defined(__x86_64__) && defined(__GNUC__)
172 __asm__("bsfq %1,%0\n\t"
173 : "=r" (i) : "rm" (mask));
175 rs->ifree_mask &= ~ ((regmask_t)1 << i);
181 for (i = 0; i < MONO_MAX_IREGS; ++i) {
182 if (mask & ((regmask_t)1 << i)) {
183 rs->ifree_mask &= ~ ((regmask_t)1 << i);
192 mono_regstate_free_int (MonoRegState *rs, int reg)
195 rs->ifree_mask |= (regmask_t)1 << reg;
196 rs->isymbolic [reg] = 0;
201 mono_regstate_alloc_general (MonoRegState *rs, regmask_t allow, int bank)
205 regmask_t mask = allow & rs->free_mask [bank];
206 for (i = 0; i < regbank_size [bank]; ++i) {
207 if (mask & ((regmask_t)1 << i)) {
208 rs->free_mask [bank] &= ~ ((regmask_t)1 << i);
210 mirrored_bank = get_mirrored_bank (bank);
211 if (mirrored_bank == -1)
214 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
222 mono_regstate_free_general (MonoRegState *rs, int reg, int bank)
227 rs->free_mask [bank] |= (regmask_t)1 << reg;
228 rs->symbolic [bank][reg] = 0;
230 mirrored_bank = get_mirrored_bank (bank);
231 if (mirrored_bank == -1)
233 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
234 rs->symbolic [mirrored_bank][reg] = 0;
239 mono_regname_full (int reg, int bank)
241 if (G_UNLIKELY (bank)) {
242 #if MONO_ARCH_NEED_SIMD_BANK
243 if (bank == MONO_REG_SIMD)
244 return mono_arch_xregname (reg);
246 if (bank == MONO_REG_INT_REF || bank == MONO_REG_INT_MP)
247 return mono_arch_regname (reg);
248 g_assert (bank == MONO_REG_DOUBLE);
249 return mono_arch_fregname (reg);
251 return mono_arch_regname (reg);
256 mono_call_inst_add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, int bank)
260 regpair = (((guint32)hreg) << 24) + vreg;
261 if (G_UNLIKELY (bank)) {
262 g_assert (vreg >= regbank_size [bank]);
263 g_assert (hreg < regbank_size [bank]);
264 call->used_fregs |= 1 << hreg;
265 call->out_freg_args = g_slist_append_mempool (cfg->mempool, call->out_freg_args, (gpointer)(gssize)(regpair));
267 g_assert (vreg >= MONO_MAX_IREGS);
268 g_assert (hreg < MONO_MAX_IREGS);
269 call->used_iregs |= 1 << hreg;
270 call->out_ireg_args = g_slist_append_mempool (cfg->mempool, call->out_ireg_args, (gpointer)(gssize)(regpair));
275 * mono_call_inst_add_outarg_vt:
277 * Register OUTARG_VT as belonging to CALL.
280 mono_call_inst_add_outarg_vt (MonoCompile *cfg, MonoCallInst *call, MonoInst *outarg_vt)
282 call->outarg_vts = g_slist_append_mempool (cfg->mempool, call->outarg_vts, outarg_vt);
286 resize_spill_info (MonoCompile *cfg, int bank)
288 MonoSpillInfo *orig_info = cfg->spill_info [bank];
289 int orig_len = cfg->spill_info_len [bank];
290 int new_len = orig_len ? orig_len * 2 : 16;
291 MonoSpillInfo *new_info;
294 g_assert (bank < MONO_NUM_REGBANKS);
296 new_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoSpillInfo) * new_len);
298 memcpy (new_info, orig_info, sizeof (MonoSpillInfo) * orig_len);
299 for (i = orig_len; i < new_len; ++i)
300 new_info [i].offset = -1;
302 cfg->spill_info [bank] = new_info;
303 cfg->spill_info_len [bank] = new_len;
307 * returns the offset used by spillvar. It allocates a new
308 * spill variable if necessary.
311 mono_spillvar_offset (MonoCompile *cfg, int spillvar, int bank)
316 if (G_UNLIKELY (spillvar >= (cfg->spill_info_len [bank]))) {
317 while (spillvar >= cfg->spill_info_len [bank])
318 resize_spill_info (cfg, bank);
322 * Allocate separate spill slots for fp/non-fp variables since most processors prefer it.
324 info = &cfg->spill_info [bank][spillvar];
325 if (info->offset == -1) {
326 cfg->stack_offset += sizeof (mgreg_t) - 1;
327 cfg->stack_offset &= ~(sizeof (mgreg_t) - 1);
329 g_assert (bank < MONO_NUM_REGBANKS);
330 if (G_UNLIKELY (bank))
331 size = regbank_spill_var_size [bank];
333 size = sizeof (mgreg_t);
335 if (cfg->flags & MONO_CFG_HAS_SPILLUP) {
336 cfg->stack_offset += size - 1;
337 cfg->stack_offset &= ~(size - 1);
338 info->offset = cfg->stack_offset;
339 cfg->stack_offset += size;
341 cfg->stack_offset += size - 1;
342 cfg->stack_offset &= ~(size - 1);
343 cfg->stack_offset += size;
344 info->offset = - cfg->stack_offset;
351 #define is_hard_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS)
352 #define is_hard_freg(r) ((r) >= 0 && (r) < MONO_MAX_FREGS)
353 #define is_global_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_SAVED_REGS & (regmask (r))))
354 #define is_local_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_REGS & (regmask (r))))
355 #define is_global_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_SAVED_FREGS & (regmask (r))))
356 #define is_local_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_FREGS & (regmask (r))))
358 #define is_hard_reg(r,bank) (G_UNLIKELY (bank) ? ((r) >= 0 && (r) < regbank_size [bank]) : ((r) < MONO_MAX_IREGS))
359 #define is_soft_reg(r,bank) (!is_hard_reg((r),(bank)))
360 #define is_global_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_saved_regs [bank] & regmask (r))) : is_global_ireg (r))
361 #define is_local_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_regs [bank] & regmask (r))) : is_local_ireg (r))
362 #define reg_is_freeable(r,bank) (G_UNLIKELY (bank) ? is_local_reg ((r), (bank)) : is_local_ireg ((r)))
364 #ifndef MONO_ARCH_INST_IS_FLOAT
365 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc) == 'f')
368 #define reg_is_fp(desc) (MONO_ARCH_INST_IS_FLOAT (desc))
369 #define dreg_is_fp(spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_DEST]))
370 #define sreg_is_fp(n,spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_SRC1+(n)]))
371 #define sreg1_is_fp(spec) sreg_is_fp (0,(spec))
372 #define sreg2_is_fp(spec) sreg_is_fp (1,(spec))
374 #define reg_is_simd(desc) ((desc) == 'x')
376 #ifdef MONO_ARCH_NEED_SIMD_BANK
378 #define reg_bank(desc) (G_UNLIKELY (reg_is_fp (desc)) ? MONO_REG_DOUBLE : G_UNLIKELY (reg_is_simd(desc)) ? MONO_REG_SIMD : MONO_REG_INT)
382 #define reg_bank(desc) reg_is_fp ((desc))
386 #define sreg_bank(n,spec) reg_bank ((spec)[MONO_INST_SRC1+(n)])
387 #define sreg1_bank(spec) sreg_bank (0, (spec))
388 #define sreg2_bank(spec) sreg_bank (1, (spec))
389 #define dreg_bank(spec) reg_bank ((spec)[MONO_INST_DEST])
391 #define sreg_bank_ins(n,ins) sreg_bank ((n), ins_get_spec ((ins)->opcode))
392 #define sreg1_bank_ins(ins) sreg_bank_ins (0, (ins))
393 #define sreg2_bank_ins(ins) sreg_bank_ins (1, (ins))
394 #define dreg_bank_ins(ins) dreg_bank (ins_get_spec ((ins)->opcode))
396 #define regpair_reg2_mask(desc,hreg1) ((MONO_ARCH_INST_REGPAIR_REG2 (desc,hreg1) != -1) ? (regmask (MONO_ARCH_INST_REGPAIR_REG2 (desc,hreg1))) : MONO_ARCH_CALLEE_REGS)
398 #ifdef MONO_ARCH_IS_GLOBAL_IREG
399 #undef is_global_ireg
400 #define is_global_ireg(reg) MONO_ARCH_IS_GLOBAL_IREG ((reg))
409 regmask_t preferred_mask; /* the hreg where the register should be allocated, or 0 */
412 #if !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT)
414 static const char* const patch_info_str[] = {
415 #define PATCH_INFO(a,b) "" #a,
416 #include "patch-info.h"
421 mono_print_ji (const MonoJumpInfo *ji)
424 case MONO_PATCH_INFO_RGCTX_FETCH: {
425 MonoJumpInfoRgctxEntry *entry = ji->data.rgctx_entry;
427 printf ("[RGCTX_FETCH ");
428 mono_print_ji (entry->data);
429 printf (" - %s]", mono_rgctx_info_type_to_str (entry->info_type));
432 case MONO_PATCH_INFO_METHODCONST: {
433 char *s = mono_method_full_name (ji->data.method, TRUE);
434 printf ("[METHODCONST - %s]", s);
439 printf ("[%s]", patch_info_str [ji->type]);
445 mono_print_ins_index (int i, MonoInst *ins)
447 const char *spec = ins_get_spec (ins->opcode);
449 int sregs [MONO_MAX_SRC_REGS];
452 printf ("\t%-2d %s", i, mono_inst_name (ins->opcode));
454 printf (" %s", mono_inst_name (ins->opcode));
455 if (spec == MONO_ARCH_CPU_SPEC) {
456 /* This is a lowered opcode */
458 printf (" R%d <-", ins->dreg);
459 if (ins->sreg1 != -1)
460 printf (" R%d", ins->sreg1);
461 if (ins->sreg2 != -1)
462 printf (" R%d", ins->sreg2);
463 if (ins->sreg3 != -1)
464 printf (" R%d", ins->sreg3);
466 switch (ins->opcode) {
477 if (!ins->inst_false_bb)
478 printf (" [B%d]", ins->inst_true_bb->block_num);
480 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
487 printf (" [%d (", (int)ins->inst_c0);
488 for (i = 0; i < ins->inst_phi_args [0]; i++) {
491 printf ("R%d", ins->inst_phi_args [i + 1]);
497 case OP_OUTARG_VTRETADDR:
498 printf (" R%d", ((MonoInst*)ins->inst_p0)->dreg);
501 case OP_GSHAREDVT_ARG_REGOFFSET:
502 printf (" + 0x%lx", (long)ins->inst_offset);
509 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
513 if (spec [MONO_INST_DEST]) {
514 int bank = dreg_bank (spec);
515 if (is_soft_reg (ins->dreg, bank)) {
516 if (spec [MONO_INST_DEST] == 'b') {
517 if (ins->inst_offset == 0)
518 printf (" [R%d] <-", ins->dreg);
520 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
523 printf (" R%d <-", ins->dreg);
524 } else if (spec [MONO_INST_DEST] == 'b') {
525 if (ins->inst_offset == 0)
526 printf (" [%s] <-", mono_arch_regname (ins->dreg));
528 printf (" [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
530 printf (" %s <-", mono_regname_full (ins->dreg, bank));
532 if (spec [MONO_INST_SRC1]) {
533 int bank = sreg1_bank (spec);
534 if (is_soft_reg (ins->sreg1, bank)) {
535 if (spec [MONO_INST_SRC1] == 'b')
536 printf (" [R%d + 0x%lx]", ins->sreg1, (long)ins->inst_offset);
538 printf (" R%d", ins->sreg1);
539 } else if (spec [MONO_INST_SRC1] == 'b')
540 printf (" [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
542 printf (" %s", mono_regname_full (ins->sreg1, bank));
544 num_sregs = mono_inst_get_src_registers (ins, sregs);
545 for (j = 1; j < num_sregs; ++j) {
546 int bank = sreg_bank (j, spec);
547 if (is_soft_reg (sregs [j], bank))
548 printf (" R%d", sregs [j]);
550 printf (" %s", mono_regname_full (sregs [j], bank));
553 switch (ins->opcode) {
555 printf (" [%d]", (int)ins->inst_c0);
557 #if defined(TARGET_X86) || defined(TARGET_AMD64)
558 case OP_X86_PUSH_IMM:
560 case OP_ICOMPARE_IMM:
568 printf (" [%d]", (int)ins->inst_imm);
572 printf (" [%d]", (int)(gssize)ins->inst_p1);
575 printf (" [%lld]", (long long)ins->inst_l);
578 printf (" [%f]", *(double*)ins->inst_p0);
581 printf (" [%f]", *(float*)ins->inst_p0);
584 case OP_CALL_MEMBASE:
590 case OP_VCALL_MEMBASE:
593 case OP_VCALL2_MEMBASE:
595 case OP_VOIDCALL_MEMBASE:
597 MonoCallInst *call = (MonoCallInst*)ins;
600 if (ins->opcode == OP_VCALL || ins->opcode == OP_VCALL_REG || ins->opcode == OP_VCALL_MEMBASE) {
602 * These are lowered opcodes, but they are in the .md files since the old
603 * JIT passes them to backends.
606 printf (" R%d <-", ins->dreg);
610 char *full_name = mono_method_full_name (call->method, TRUE);
611 printf (" [%s]", full_name);
613 } else if (call->fptr_is_patch) {
614 MonoJumpInfo *ji = (MonoJumpInfo*)call->fptr;
618 } else if (call->fptr) {
619 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
621 printf (" [%s]", info->name);
624 list = call->out_ireg_args;
629 regpair = (guint32)(gssize)(list->data);
630 hreg = regpair >> 24;
631 reg = regpair & 0xffffff;
633 printf (" [%s <- R%d]", mono_arch_regname (hreg), reg);
635 list = g_slist_next (list);
640 case OP_CALL_HANDLER:
641 printf (" [B%d]", ins->inst_target_bb->block_num);
663 if (!ins->inst_false_bb)
664 printf (" [B%d]", ins->inst_true_bb->block_num);
666 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
668 case OP_LIVERANGE_START:
669 case OP_LIVERANGE_END:
670 case OP_GC_LIVENESS_DEF:
671 case OP_GC_LIVENESS_USE:
672 printf (" R%d", (int)ins->inst_c1);
675 printf (" il: %x", (int)ins->inst_imm);
681 if (spec [MONO_INST_CLOB])
682 printf (" clobbers: %c", spec [MONO_INST_CLOB]);
687 print_regtrack (RegTrack *t, int num)
693 for (i = 0; i < num; ++i) {
696 if (i >= MONO_MAX_IREGS) {
697 g_snprintf (buf, sizeof(buf), "R%d", i);
700 r = mono_arch_regname (i);
701 printf ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].killed_in);
707 mono_print_ji (const MonoJumpInfo *ji)
712 mono_print_ins_index (int i, MonoInst *ins)
715 #endif /* !defined(DISABLE_LOGGING) && !defined(DISABLE_JIT) */
718 mono_print_ins (MonoInst *ins)
720 mono_print_ins_index (-1, ins);
724 insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst* to_insert)
727 * If this function is called multiple times, the new instructions are inserted
728 * in the proper order.
730 mono_bblock_insert_before_ins (bb, ins, to_insert);
734 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst **last, MonoInst* to_insert)
737 * If this function is called multiple times, the new instructions are inserted in
740 mono_bblock_insert_after_ins (bb, *last, to_insert);
746 get_vreg_bank (MonoCompile *cfg, int reg, int bank)
748 if (vreg_is_ref (cfg, reg))
749 return MONO_REG_INT_REF;
750 else if (vreg_is_mp (cfg, reg))
751 return MONO_REG_INT_MP;
757 * Force the spilling of the variable in the symbolic register 'reg', and free
758 * the hreg it was assigned to.
761 spill_vreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int reg, int bank)
766 MonoRegState *rs = cfg->rs;
768 symbolic = rs->symbolic [bank];
769 sel = rs->vassign [reg];
771 /* the vreg we need to spill lives in another logical reg bank */
772 bank = translate_bank (cfg->rs, bank, sel);
774 /*i = rs->isymbolic [sel];
775 g_assert (i == reg);*/
777 spill = ++cfg->spill_count;
778 rs->vassign [i] = -spill - 1;
779 if (G_UNLIKELY (bank))
780 mono_regstate_free_general (rs, sel, bank);
782 mono_regstate_free_int (rs, sel);
783 /* we need to create a spill var and insert a load to sel after the current instruction */
784 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
786 load->inst_basereg = cfg->frame_reg;
787 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, reg, bank));
788 insert_after_ins (bb, ins, last, load);
789 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)));
790 if (G_UNLIKELY (bank))
791 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
793 i = mono_regstate_alloc_int (rs, regmask (sel));
796 if (G_UNLIKELY (bank))
797 mono_regstate_free_general (rs, sel, bank);
799 mono_regstate_free_int (rs, sel);
802 /* This isn't defined on older glib versions and on some platforms */
803 #ifndef G_GUINT64_FORMAT
804 #define G_GUINT64_FORMAT "ul"
808 get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t regmask, int reg, int bank)
811 int i, sel, spill, num_sregs;
812 int sregs [MONO_MAX_SRC_REGS];
814 MonoRegState *rs = cfg->rs;
816 symbolic = rs->symbolic [bank];
818 g_assert (bank < MONO_NUM_REGBANKS);
820 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));
821 /* exclude the registers in the current instruction */
822 num_sregs = mono_inst_get_src_registers (ins, sregs);
823 for (i = 0; i < num_sregs; ++i) {
824 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))) {
825 if (is_soft_reg (sregs [i], bank))
826 regmask &= ~ (regmask (rs->vassign [sregs [i]]));
828 regmask &= ~ (regmask (sregs [i]));
829 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i + 1, mono_regname_full (sregs [i], bank), sregs [i]));
832 if ((dreg_bank_ins (ins) == bank) && (reg != ins->dreg) && reg_is_freeable (ins->dreg, bank)) {
833 regmask &= ~ (regmask (ins->dreg));
834 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins->dreg, bank)));
837 DEBUG (printf ("\t\tavailable regmask: 0x%08llu\n", (unsigned long long)regmask));
838 g_assert (regmask); /* need at least a register we can free */
840 /* we should track prev_use and spill the register that's farther */
841 if (G_UNLIKELY (bank)) {
842 for (i = 0; i < regbank_size [bank]; ++i) {
843 if (regmask & (regmask (i))) {
846 /* the vreg we need to load lives in another logical bank */
847 bank = translate_bank (cfg->rs, bank, sel);
849 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
854 i = rs->symbolic [bank] [sel];
855 spill = ++cfg->spill_count;
856 rs->vassign [i] = -spill - 1;
857 mono_regstate_free_general (rs, sel, bank);
860 for (i = 0; i < MONO_MAX_IREGS; ++i) {
861 if (regmask & (regmask (i))) {
863 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), rs->isymbolic [sel]));
868 i = rs->isymbolic [sel];
869 spill = ++cfg->spill_count;
870 rs->vassign [i] = -spill - 1;
871 mono_regstate_free_int (rs, sel);
874 /* we need to create a spill var and insert a load to sel after the current instruction */
875 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
877 load->inst_basereg = cfg->frame_reg;
878 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, i, bank));
879 insert_after_ins (bb, ins, last, load);
880 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)));
881 if (G_UNLIKELY (bank))
882 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
884 i = mono_regstate_alloc_int (rs, regmask (sel));
893 * Free up the hreg HREG by spilling the vreg allocated to it.
896 free_up_hreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int hreg, int bank)
898 if (G_UNLIKELY (bank)) {
899 if (!(cfg->rs->free_mask [bank] & (regmask (hreg)))) {
900 bank = translate_bank (cfg->rs, bank, hreg);
901 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
902 spill_vreg (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
906 if (!(cfg->rs->ifree_mask & (regmask (hreg)))) {
907 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->isymbolic [hreg]));
908 spill_vreg (cfg, bb, last, ins, cfg->rs->isymbolic [hreg], bank);
914 create_copy_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, int dest, int src, MonoInst *ins, const unsigned char *ip, int bank)
918 MONO_INST_NEW (cfg, copy, regbank_move_ops [bank]);
924 mono_bblock_insert_after_ins (bb, ins, copy);
927 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src, bank), mono_regname_full (dest, bank)));
931 static inline const char*
932 regbank_to_string (int bank)
934 if (bank == MONO_REG_INT_REF)
936 else if (bank == MONO_REG_INT_MP)
943 create_spilled_store (MonoCompile *cfg, MonoBasicBlock *bb, int spill, int reg, int prev_reg, MonoInst **last, MonoInst *ins, MonoInst *insert_before, int bank)
945 MonoInst *store, *def;
947 bank = get_vreg_bank (cfg, prev_reg, bank);
949 MONO_INST_NEW (cfg, store, regbank_store_ops [bank]);
951 store->inst_destbasereg = cfg->frame_reg;
952 store->inst_offset = mono_spillvar_offset (cfg, spill, bank);
954 mono_bblock_insert_after_ins (bb, ins, store);
956 } else if (insert_before) {
957 insert_before_ins (bb, insert_before, store);
959 g_assert_not_reached ();
961 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)));
963 if (((bank == MONO_REG_INT_REF) || (bank == MONO_REG_INT_MP)) && cfg->compute_gc_maps) {
964 g_assert (prev_reg != -1);
965 MONO_INST_NEW (cfg, def, OP_GC_SPILL_SLOT_LIVENESS_DEF);
966 def->inst_c0 = spill;
968 mono_bblock_insert_after_ins (bb, store, def);
972 /* flags used in reginfo->flags */
974 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
975 MONO_FP_NEEDS_SPILL = regmask (1),
976 MONO_FP_NEEDS_LOAD = regmask (2)
980 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
984 if (info && info->preferred_mask) {
985 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
987 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
992 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
994 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
1000 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
1004 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
1007 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1013 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
1015 if (G_UNLIKELY (bank))
1016 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
1018 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
1022 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
1024 if (G_UNLIKELY (bank)) {
1027 g_assert (reg >= regbank_size [bank]);
1028 g_assert (hreg < regbank_size [bank]);
1029 g_assert (! is_global_freg (hreg));
1031 rs->vassign [reg] = hreg;
1032 rs->symbolic [bank] [hreg] = reg;
1033 rs->free_mask [bank] &= ~ (regmask (hreg));
1035 mirrored_bank = get_mirrored_bank (bank);
1036 if (mirrored_bank == -1)
1039 /* Make sure the other logical reg bank that this bank shares
1040 * a single hard reg bank knows that this hard reg is not free.
1042 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
1044 /* Mark the other logical bank that the this bank shares
1045 * a single hard reg bank with as mirrored.
1047 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
1051 g_assert (reg >= MONO_MAX_IREGS);
1052 g_assert (hreg < MONO_MAX_IREGS);
1054 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
1055 g_assert (! is_global_ireg (hreg));
1058 rs->vassign [reg] = hreg;
1059 rs->isymbolic [hreg] = reg;
1060 rs->ifree_mask &= ~ (regmask (hreg));
1064 static inline regmask_t
1065 get_callee_mask (const char spec)
1067 if (G_UNLIKELY (reg_bank (spec)))
1068 return regbank_callee_regs [reg_bank (spec)];
1069 return MONO_ARCH_CALLEE_REGS;
1072 static gint8 desc_to_fixed_reg [256];
1073 static gboolean desc_to_fixed_reg_inited = FALSE;
1078 * Local register allocation.
1079 * We first scan the list of instructions and we save the liveness info of
1080 * each register (when the register is first used, when it's value is set etc.).
1081 * We also reverse the list of instructions because assigning registers backwards allows
1082 * for more tricks to be used.
1085 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1087 MonoInst *ins, *prev, *last;
1089 MonoRegState *rs = cfg->rs;
1093 unsigned char spec_src1, spec_dest;
1095 #if MONO_ARCH_USE_FPSTACK
1096 gboolean has_fp = FALSE;
1101 int sregs [MONO_MAX_SRC_REGS];
1106 if (!desc_to_fixed_reg_inited) {
1107 for (i = 0; i < 256; ++i)
1108 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1109 desc_to_fixed_reg_inited = TRUE;
1111 /* Validate the cpu description against the info in mini-ops.h */
1112 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM)
1113 for (i = OP_LOAD; i < OP_LAST; ++i) {
1116 spec = ins_get_spec (i);
1117 ispec = INS_INFO (i);
1119 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1120 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1121 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1122 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1123 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1124 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1129 rs->next_vreg = bb->max_vreg;
1130 mono_regstate_assign (rs);
1132 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1133 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1134 rs->free_mask [i] = regbank_callee_regs [i];
1136 max = rs->next_vreg;
1138 if (cfg->reginfo && cfg->reginfo_len < max)
1139 cfg->reginfo = NULL;
1141 reginfo = cfg->reginfo;
1143 cfg->reginfo_len = MAX (1024, max * 2);
1144 reginfo = cfg->reginfo = mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1147 g_assert (cfg->reginfo_len >= rs->next_vreg);
1149 if (cfg->verbose_level > 1) {
1150 /* print_regtrack reads the info of all variables */
1151 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1155 * For large methods, next_vreg can be very large, so g_malloc0 time can
1156 * be prohibitive. So we manually init the reginfo entries used by the
1159 for (ins = bb->code; ins; ins = ins->next) {
1160 spec = ins_get_spec (ins->opcode);
1162 if ((ins->dreg != -1) && (ins->dreg < max)) {
1163 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1164 #if SIZEOF_REGISTER == 4
1165 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1167 * In the new IR, the two vregs of the regpair do not alias the
1168 * original long vreg. shift the vreg here so the rest of the
1169 * allocator doesn't have to care about it.
1172 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1177 num_sregs = mono_inst_get_src_registers (ins, sregs);
1178 for (j = 0; j < num_sregs; ++j) {
1179 g_assert (sregs [j] != -1);
1180 if (sregs [j] < max) {
1181 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1182 #if SIZEOF_REGISTER == 4
1183 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1185 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1190 mono_inst_set_src_registers (ins, sregs);
1193 /*if (cfg->opt & MONO_OPT_COPYPROP)
1194 local_copy_prop (cfg, ins);*/
1197 DEBUG (printf ("\nLOCAL REGALLOC BLOCK %d:\n", bb->block_num));
1198 /* forward pass on the instructions to collect register liveness info */
1199 MONO_BB_FOR_EACH_INS (bb, ins) {
1200 spec = ins_get_spec (ins->opcode);
1201 spec_dest = spec [MONO_INST_DEST];
1203 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1204 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1207 DEBUG (mono_print_ins_index (i, ins));
1209 num_sregs = mono_inst_get_src_registers (ins, sregs);
1211 #if MONO_ARCH_USE_FPSTACK
1212 if (dreg_is_fp (spec)) {
1215 for (j = 0; j < num_sregs; ++j) {
1216 if (sreg_is_fp (j, spec))
1222 for (j = 0; j < num_sregs; ++j) {
1223 int sreg = sregs [j];
1224 int sreg_spec = spec [MONO_INST_SRC1 + j];
1226 bank = sreg_bank (j, spec);
1227 g_assert (sreg != -1);
1228 if (is_soft_reg (sreg, bank))
1229 /* This means the vreg is not local to this bb */
1230 g_assert (reginfo [sreg].born_in > 0);
1231 rs->vassign [sreg] = -1;
1232 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1233 //reginfo [ins->sreg2].last_use = i;
1234 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1235 /* The virtual register is allocated sequentially */
1236 rs->vassign [sreg + 1] = -1;
1237 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1238 //reginfo [ins->sreg2 + 1].last_use = i;
1239 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1240 reginfo [sreg + 1].born_in = i;
1246 mono_inst_set_src_registers (ins, sregs);
1251 bank = dreg_bank (spec);
1252 if (spec_dest != 'b') /* it's not just a base register */
1253 reginfo [ins->dreg].killed_in = i;
1254 g_assert (ins->dreg != -1);
1255 rs->vassign [ins->dreg] = -1;
1256 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1257 //reginfo [ins->dreg].last_use = i;
1258 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1259 reginfo [ins->dreg].born_in = i;
1261 dest_dreg = desc_to_fixed_reg [spec_dest];
1262 if (dest_dreg != -1)
1263 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1265 #ifdef MONO_ARCH_INST_FIXED_MASK
1266 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1269 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1270 /* The virtual register is allocated sequentially */
1271 rs->vassign [ins->dreg + 1] = -1;
1272 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1273 //reginfo [ins->dreg + 1].last_use = i;
1274 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1275 reginfo [ins->dreg + 1].born_in = i;
1276 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1277 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1283 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1284 /* A call instruction implicitly uses all registers in call->out_ireg_args */
1286 MonoCallInst *call = (MonoCallInst*)ins;
1289 list = call->out_ireg_args;
1295 regpair = (guint32)(gssize)(list->data);
1296 hreg = regpair >> 24;
1297 reg = regpair & 0xffffff;
1299 //reginfo [reg].prev_use = reginfo [reg].last_use;
1300 //reginfo [reg].last_use = i;
1302 list = g_slist_next (list);
1306 list = call->out_freg_args;
1312 regpair = (guint32)(gssize)(list->data);
1313 hreg = regpair >> 24;
1314 reg = regpair & 0xffffff;
1316 list = g_slist_next (list);
1326 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1327 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1328 int prev_dreg, clob_dreg;
1329 int dest_dreg, clob_reg;
1330 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1331 int dreg_high, sreg1_high;
1332 regmask_t dreg_mask, mask;
1333 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1334 regmask_t dreg_fixed_mask;
1335 const unsigned char *ip;
1337 spec = ins_get_spec (ins->opcode);
1338 spec_src1 = spec [MONO_INST_SRC1];
1339 spec_dest = spec [MONO_INST_DEST];
1346 dreg_mask = get_callee_mask (spec_dest);
1347 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1348 prev_sregs [j] = -1;
1349 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1350 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1351 #ifdef MONO_ARCH_INST_FIXED_MASK
1352 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1354 sreg_fixed_masks [j] = 0;
1358 DEBUG (printf ("processing:"));
1359 DEBUG (mono_print_ins_index (i, ins));
1368 dest_dreg = desc_to_fixed_reg [spec_dest];
1369 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1370 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1372 #ifdef MONO_ARCH_INST_FIXED_MASK
1373 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1375 dreg_fixed_mask = 0;
1378 num_sregs = mono_inst_get_src_registers (ins, sregs);
1381 * TRACK FIXED SREG2, 3, ...
1383 for (j = 1; j < num_sregs; ++j) {
1384 int sreg = sregs [j];
1385 int dest_sreg = dest_sregs [j];
1387 if (dest_sreg == -1)
1395 * We need to special case this, since on x86, there are only 3
1396 * free registers, and the code below assigns one of them to
1397 * sreg, so we can run out of registers when trying to assign
1398 * dreg. Instead, we just set up the register masks, and let the
1399 * normal sreg2 assignment code handle this. It would be nice to
1400 * do this for all the fixed reg cases too, but there is too much
1404 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1405 sreg_masks [j] = regmask (dest_sreg);
1406 for (k = 0; k < num_sregs; ++k) {
1408 sreg_masks [k] &= ~ (regmask (dest_sreg));
1412 * Spill sreg1/2 if they are assigned to dest_sreg.
1414 for (k = 0; k < num_sregs; ++k) {
1415 if (k != j && is_soft_reg (sregs [k], 0) && rs->vassign [sregs [k]] == dest_sreg)
1416 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1420 * We can also run out of registers while processing sreg2 if sreg3 is
1421 * assigned to another hreg, so spill sreg3 now.
1423 if (is_soft_reg (sreg, 0) && rs->vassign [sreg] >= 0 && rs->vassign [sreg] != dest_sreg) {
1424 spill_vreg (cfg, bb, tmp, ins, sreg, 0);
1429 if (rs->ifree_mask & (regmask (dest_sreg))) {
1430 if (is_global_ireg (sreg)) {
1432 /* Argument already in hard reg, need to copy */
1433 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1434 insert_before_ins (bb, ins, copy);
1435 for (k = 0; k < num_sregs; ++k) {
1437 sreg_masks [k] &= ~ (regmask (dest_sreg));
1440 val = rs->vassign [sreg];
1442 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1443 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1444 } else if (val < -1) {
1446 g_assert_not_reached ();
1448 /* Argument already in hard reg, need to copy */
1449 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1452 insert_before_ins (bb, ins, copy);
1453 for (k = 0; k < num_sregs; ++k) {
1455 sreg_masks [k] &= ~ (regmask (dest_sreg));
1458 * Prevent the dreg from being allocate to dest_sreg
1459 * too, since it could force sreg1 to be allocated to
1460 * the same reg on x86.
1462 dreg_mask &= ~ (regmask (dest_sreg));
1466 gboolean need_spill = TRUE;
1467 gboolean need_assign = TRUE;
1470 dreg_mask &= ~ (regmask (dest_sreg));
1471 for (k = 0; k < num_sregs; ++k) {
1473 sreg_masks [k] &= ~ (regmask (dest_sreg));
1477 * First check if dreg is assigned to dest_sreg2, since we
1478 * can't spill a dreg.
1480 if (spec [MONO_INST_DEST])
1481 val = rs->vassign [ins->dreg];
1484 if (val == dest_sreg && ins->dreg != sreg) {
1486 * the destination register is already assigned to
1487 * dest_sreg2: we need to allocate another register for it
1488 * and then copy from this to dest_sreg2.
1491 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1492 g_assert (new_dest >= 0);
1493 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1495 prev_dreg = ins->dreg;
1496 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1497 clob_dreg = ins->dreg;
1498 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1499 mono_regstate_free_int (rs, dest_sreg);
1503 if (is_global_ireg (sreg)) {
1504 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1505 insert_before_ins (bb, ins, copy);
1506 need_assign = FALSE;
1509 val = rs->vassign [sreg];
1510 if (val == dest_sreg) {
1511 /* sreg2 is already assigned to the correct register */
1513 } else if (val < -1) {
1514 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1515 } else if (val >= 0) {
1516 /* sreg2 already assigned to another register */
1518 * We couldn't emit a copy from val to dest_sreg2, because
1519 * val might be spilled later while processing this
1520 * instruction. So we spill sreg2 so it can be allocated to
1523 free_up_hreg (cfg, bb, tmp, ins, val, 0);
1528 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1532 if (rs->vassign [sreg] < -1) {
1535 /* Need to emit a spill store */
1536 spill = - rs->vassign [sreg] - 1;
1537 create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, ins, bank);
1539 /* force-set sreg2 */
1540 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1543 sregs [j] = dest_sreg;
1545 mono_inst_set_src_registers (ins, sregs);
1550 bank = dreg_bank (spec);
1551 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1552 prev_dreg = ins->dreg;
1555 if (spec_dest == 'b') {
1557 * The dest reg is read by the instruction, not written, so
1558 * avoid allocating sreg1/sreg2 to the same reg.
1560 if (dest_sregs [0] != -1)
1561 dreg_mask &= ~ (regmask (dest_sregs [0]));
1562 for (j = 1; j < num_sregs; ++j) {
1563 if (dest_sregs [j] != -1)
1564 dreg_mask &= ~ (regmask (dest_sregs [j]));
1567 val = rs->vassign [ins->dreg];
1568 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1569 /* DREG is already allocated to a register needed for sreg1 */
1570 spill_vreg (cfg, bb, tmp, ins, ins->dreg, 0);
1575 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1576 * various complex situations.
1578 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1579 guint32 dreg2, dest_dreg2;
1581 g_assert (is_soft_reg (ins->dreg, bank));
1583 if (dest_dreg != -1) {
1584 if (rs->vassign [ins->dreg] != dest_dreg)
1585 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, 0);
1587 dreg2 = ins->dreg + 1;
1588 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1589 if (dest_dreg2 != -1) {
1590 if (rs->vassign [dreg2] != dest_dreg2)
1591 free_up_hreg (cfg, bb, tmp, ins, dest_dreg2, 0);
1596 if (dreg_fixed_mask) {
1598 if (is_global_ireg (ins->dreg)) {
1600 * The argument is already in a hard reg, but that reg is
1601 * not usable by this instruction, so allocate a new one.
1603 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1605 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1606 mono_regstate_free_int (rs, val);
1612 dreg_mask &= dreg_fixed_mask;
1615 if (is_soft_reg (ins->dreg, bank)) {
1616 val = rs->vassign [ins->dreg];
1621 /* the register gets spilled after this inst */
1624 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1625 assign_reg (cfg, rs, ins->dreg, val, bank);
1627 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, NULL, bank);
1630 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1634 /* Handle regpairs */
1635 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1636 int reg2 = prev_dreg + 1;
1639 g_assert (prev_dreg > -1);
1640 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1641 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1644 mask &= ~regmask (X86_ECX);
1646 val = rs->vassign [reg2];
1650 /* the register gets spilled after this inst */
1653 val = mono_regstate_alloc_int (rs, mask);
1655 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1657 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, NULL, bank);
1660 if (! (mask & (regmask (val)))) {
1661 val = mono_regstate_alloc_int (rs, mask);
1663 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1665 /* Reallocate hreg to the correct register */
1666 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1668 mono_regstate_free_int (rs, rs->vassign [reg2]);
1672 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1673 assign_reg (cfg, rs, reg2, val, bank);
1676 ins->backend.reg3 = val;
1678 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1679 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1680 mono_regstate_free_int (rs, val);
1684 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1686 * In theory, we could free up the hreg even if the vreg is alive,
1687 * but branches inside bblocks force us to assign the same hreg
1688 * to a vreg every time it is encountered.
1690 int dreg = rs->vassign [prev_dreg];
1691 g_assert (dreg >= 0);
1692 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1693 if (G_UNLIKELY (bank))
1694 mono_regstate_free_general (rs, dreg, bank);
1696 mono_regstate_free_int (rs, dreg);
1697 rs->vassign [prev_dreg] = -1;
1700 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1701 /* this instruction only outputs to dest_dreg, need to copy */
1702 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1703 ins->dreg = dest_dreg;
1705 if (G_UNLIKELY (bank)) {
1706 /* the register we need to free up may be used in another logical regbank
1707 * so do a translate just in case.
1709 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1710 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1711 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1714 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1715 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, bank);
1719 if (spec_dest == 'b') {
1721 * The dest reg is read by the instruction, not written, so
1722 * avoid allocating sreg1/sreg2 to the same reg.
1724 for (j = 0; j < num_sregs; ++j)
1725 if (!sreg_bank (j, spec))
1726 sreg_masks [j] &= ~ (regmask (ins->dreg));
1732 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1733 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1734 free_up_hreg (cfg, bb, tmp, ins, clob_reg, 0);
1737 if (spec [MONO_INST_CLOB] == 'c') {
1738 int j, s, dreg, dreg2, cur_bank;
1741 clob_mask = MONO_ARCH_CALLEE_REGS;
1743 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1745 * Need to avoid spilling the dreg since the dreg is not really
1746 * clobbered by the call.
1748 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1749 dreg = rs->vassign [prev_dreg];
1753 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1754 dreg2 = rs->vassign [prev_dreg + 1];
1758 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1760 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1761 if ((j != dreg) && (j != dreg2))
1762 free_up_hreg (cfg, bb, tmp, ins, j, 0);
1763 else if (rs->isymbolic [j])
1764 /* The hreg is assigned to the dreg of this instruction */
1765 rs->vassign [rs->isymbolic [j]] = -1;
1766 mono_regstate_free_int (rs, j);
1771 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1772 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1773 clob_mask = regbank_callee_regs [cur_bank];
1774 if ((prev_dreg != -1) && reg_bank (spec_dest))
1775 dreg = rs->vassign [prev_dreg];
1779 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1781 /* we are looping though the banks in the outer loop
1782 * so, we don't need to deal with mirrored hregs
1783 * because we will get them in one of the other bank passes.
1785 if (is_hreg_mirrored (rs, cur_bank, j))
1789 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s)) {
1791 free_up_hreg (cfg, bb, tmp, ins, j, cur_bank);
1792 else if (rs->symbolic [cur_bank] [j])
1793 /* The hreg is assigned to the dreg of this instruction */
1794 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1795 mono_regstate_free_general (rs, j, cur_bank);
1803 * TRACK ARGUMENT REGS
1805 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1806 MonoCallInst *call = (MonoCallInst*)ins;
1810 * This needs to be done before assigning sreg1, so sreg1 will
1811 * not be assigned one of the argument regs.
1815 * Assign all registers in call->out_reg_args to the proper
1816 * argument registers.
1819 list = call->out_ireg_args;
1825 regpair = (guint32)(gssize)(list->data);
1826 hreg = regpair >> 24;
1827 reg = regpair & 0xffffff;
1829 assign_reg (cfg, rs, reg, hreg, 0);
1831 sreg_masks [0] &= ~(regmask (hreg));
1833 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1835 list = g_slist_next (list);
1839 list = call->out_freg_args;
1845 regpair = (guint32)(gssize)(list->data);
1846 hreg = regpair >> 24;
1847 reg = regpair & 0xffffff;
1849 assign_reg (cfg, rs, reg, hreg, 1);
1851 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1853 list = g_slist_next (list);
1861 bank = sreg1_bank (spec);
1862 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1863 int sreg1 = sregs [0];
1864 int dest_sreg1 = dest_sregs [0];
1866 g_assert (is_soft_reg (sreg1, bank));
1868 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1869 if (dest_sreg1 != -1)
1870 g_assert (dest_sreg1 == ins->dreg);
1871 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1872 g_assert (val >= 0);
1874 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1876 g_assert_not_reached ();
1878 assign_reg (cfg, rs, sreg1, val, bank);
1880 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1882 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1883 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1884 g_assert (val >= 0);
1886 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1888 g_assert_not_reached ();
1890 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1892 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1894 /* Skip rest of this section */
1895 dest_sregs [0] = -1;
1898 if (sreg_fixed_masks [0]) {
1900 if (is_global_ireg (sregs [0])) {
1902 * The argument is already in a hard reg, but that reg is
1903 * not usable by this instruction, so allocate a new one.
1905 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1907 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1908 mono_regstate_free_int (rs, val);
1909 dest_sregs [0] = val;
1911 /* Fall through to the dest_sreg1 != -1 case */
1914 sreg_masks [0] &= sreg_fixed_masks [0];
1917 if (dest_sregs [0] != -1) {
1918 sreg_masks [0] = regmask (dest_sregs [0]);
1920 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1921 free_up_hreg (cfg, bb, tmp, ins, dest_sregs [0], 0);
1923 if (is_global_ireg (sregs [0])) {
1924 /* The argument is already in a hard reg, need to copy */
1925 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1926 insert_before_ins (bb, ins, copy);
1927 sregs [0] = dest_sregs [0];
1931 if (is_soft_reg (sregs [0], bank)) {
1932 val = rs->vassign [sregs [0]];
1933 prev_sregs [0] = sregs [0];
1937 /* the register gets spilled after this inst */
1941 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1943 * Allocate the same hreg to sreg1 as well so the
1944 * peephole can get rid of the move.
1946 sreg_masks [0] = regmask (ins->dreg);
1949 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1950 /* Allocate the same reg to sreg1 to avoid a copy later */
1951 sreg_masks [0] = regmask (ins->dreg);
1953 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
1954 assign_reg (cfg, rs, sregs [0], val, bank);
1955 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
1959 * Need to insert before the instruction since it can
1962 create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, ins, bank);
1965 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
1966 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
1967 insert_before_ins (bb, ins, copy);
1968 for (j = 1; j < num_sregs; ++j)
1969 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
1970 val = dest_sregs [0];
1976 prev_sregs [0] = -1;
1978 mono_inst_set_src_registers (ins, sregs);
1980 for (j = 1; j < num_sregs; ++j)
1981 sreg_masks [j] &= ~(regmask (sregs [0]));
1983 /* Handle the case when sreg1 is a regpair but dreg is not */
1984 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
1985 int reg2 = prev_sregs [0] + 1;
1988 g_assert (prev_sregs [0] > -1);
1989 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
1990 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
1991 val = rs->vassign [reg2];
1995 /* the register gets spilled after this inst */
1998 val = mono_regstate_alloc_int (rs, mask);
2000 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2002 g_assert_not_reached ();
2005 if (! (mask & (regmask (val)))) {
2006 /* The vreg is already allocated to a wrong hreg */
2008 g_assert_not_reached ();
2010 val = mono_regstate_alloc_int (rs, mask);
2012 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
2014 /* Reallocate hreg to the correct register */
2015 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
2017 mono_regstate_free_int (rs, rs->vassign [reg2]);
2023 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
2024 assign_reg (cfg, rs, reg2, val, bank);
2027 /* Handle dreg==sreg1 */
2028 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
2029 MonoInst *sreg2_copy = NULL;
2031 int bank = reg_bank (spec_src1);
2033 if (ins->dreg == sregs [1]) {
2035 * copying sreg1 to dreg could clobber sreg2, so allocate a new
2038 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
2040 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
2041 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
2042 prev_sregs [1] = sregs [1] = reg2;
2044 if (G_UNLIKELY (bank))
2045 mono_regstate_free_general (rs, reg2, bank);
2047 mono_regstate_free_int (rs, reg2);
2050 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
2051 /* Copying sreg1_high to dreg could also clobber sreg2 */
2052 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
2054 g_assert_not_reached ();
2057 * sreg1 and dest are already allocated to the same regpair by the
2058 * SREG1 allocation code.
2060 g_assert (sregs [0] == ins->dreg);
2061 g_assert (dreg_high == sreg1_high);
2064 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
2065 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
2066 insert_before_ins (bb, ins, copy);
2069 insert_before_ins (bb, copy, sreg2_copy);
2072 * Need to prevent sreg2 to be allocated to sreg1, since that
2073 * would screw up the previous copy.
2075 sreg_masks [1] &= ~ (regmask (sregs [0]));
2076 /* we set sreg1 to dest as well */
2077 prev_sregs [0] = sregs [0] = ins->dreg;
2078 sreg_masks [1] &= ~ (regmask (ins->dreg));
2080 mono_inst_set_src_registers (ins, sregs);
2083 * TRACK SREG2, 3, ...
2085 for (j = 1; j < num_sregs; ++j) {
2088 bank = sreg_bank (j, spec);
2089 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
2090 g_assert_not_reached ();
2092 if (dest_sregs [j] != -1 && is_global_ireg (sregs [j])) {
2094 * Argument already in a global hard reg, copy it to the fixed reg, without
2095 * allocating it to the fixed reg.
2097 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [j], sregs [j], NULL, ip, 0);
2098 insert_before_ins (bb, ins, copy);
2099 sregs [j] = dest_sregs [j];
2100 } else if (is_soft_reg (sregs [j], bank)) {
2101 val = rs->vassign [sregs [j]];
2103 if (dest_sregs [j] != -1 && val >= 0 && dest_sregs [j] != val) {
2105 * The sreg is already allocated to a hreg, but not to the fixed
2106 * reg required by the instruction. Spill the sreg, so it can be
2107 * allocated to the fixed reg by the code below.
2109 /* Currently, this code should only be hit for CAS */
2110 spill_vreg (cfg, bb, tmp, ins, sregs [j], 0);
2111 val = rs->vassign [sregs [j]];
2117 /* the register gets spilled after this inst */
2120 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
2121 assign_reg (cfg, rs, sregs [j], val, bank);
2122 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
2125 * Need to insert before the instruction since it can
2128 create_spilled_store (cfg, bb, spill, val, sregs [j], tmp, NULL, ins, bank);
2132 for (k = j + 1; k < num_sregs; ++k)
2133 sreg_masks [k] &= ~ (regmask (sregs [j]));
2136 prev_sregs [j] = -1;
2139 mono_inst_set_src_registers (ins, sregs);
2142 /* Do this only for CAS for now */
2143 for (j = 1; j < num_sregs; ++j) {
2144 int sreg = sregs [j];
2145 int dest_sreg = dest_sregs [j];
2147 if (j == 2 && dest_sreg != -1) {
2150 g_assert (sreg == dest_sreg);
2152 for (k = 0; k < num_sregs; ++k) {
2154 g_assert (sregs [k] != dest_sreg);
2159 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2160 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2161 mono_regstate_free_int (rs, ins->sreg1);
2163 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2164 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2165 mono_regstate_free_int (rs, ins->sreg2);
2168 DEBUG (mono_print_ins_index (i, ins));
2171 // FIXME: Set MAX_FREGS to 8
2172 // FIXME: Optimize generated code
2173 #if MONO_ARCH_USE_FPSTACK
2175 * Make a forward pass over the code, simulating the fp stack, making sure the
2176 * arguments required by the fp opcodes are at the top of the stack.
2179 MonoInst *prev = NULL;
2183 g_assert (num_sregs <= 2);
2185 for (ins = bb->code; ins; ins = ins->next) {
2186 spec = ins_get_spec (ins->opcode);
2188 DEBUG (printf ("processing:"));
2189 DEBUG (mono_print_ins_index (0, ins));
2191 if (ins->opcode == OP_FMOVE) {
2192 /* Do it by renaming the source to the destination on the stack */
2193 // FIXME: Is this correct ?
2194 for (i = 0; i < sp; ++i)
2195 if (fpstack [i] == ins->sreg1)
2196 fpstack [i] = ins->dreg;
2201 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2202 /* Arg1 must be in %st(1) */
2206 while ((i < sp) && (fpstack [i] != ins->sreg1))
2210 if (sp - 1 - i > 0) {
2211 /* First move it to %st(0) */
2212 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2214 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2215 fxch->inst_imm = sp - 1 - i;
2217 mono_bblock_insert_after_ins (bb, prev, fxch);
2220 tmp = fpstack [sp - 1];
2221 fpstack [sp - 1] = fpstack [i];
2225 /* Then move it to %st(1) */
2226 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2228 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2231 mono_bblock_insert_after_ins (bb, prev, fxch);
2234 tmp = fpstack [sp - 1];
2235 fpstack [sp - 1] = fpstack [sp - 2];
2236 fpstack [sp - 2] = tmp;
2239 if (sreg2_is_fp (spec)) {
2242 if (fpstack [sp - 1] != ins->sreg2) {
2246 while ((i < sp) && (fpstack [i] != ins->sreg2))
2250 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2252 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2253 fxch->inst_imm = sp - 1 - i;
2255 mono_bblock_insert_after_ins (bb, prev, fxch);
2258 tmp = fpstack [sp - 1];
2259 fpstack [sp - 1] = fpstack [i];
2266 if (sreg1_is_fp (spec)) {
2269 if (fpstack [sp - 1] != ins->sreg1) {
2273 while ((i < sp) && (fpstack [i] != ins->sreg1))
2277 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2279 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2280 fxch->inst_imm = sp - 1 - i;
2282 mono_bblock_insert_after_ins (bb, prev, fxch);
2285 tmp = fpstack [sp - 1];
2286 fpstack [sp - 1] = fpstack [i];
2293 if (dreg_is_fp (spec)) {
2295 fpstack [sp ++] = ins->dreg;
2298 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2300 for (i = 0; i < sp; ++i)
2301 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2308 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2309 /* Remove remaining items from the fp stack */
2311 * These can remain for example as a result of a dead fmove like in
2312 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2315 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2316 mono_add_ins_to_end (bb, ins);
2325 mono_opcode_to_cond (int opcode)
2335 case OP_COND_EXC_EQ:
2336 case OP_COND_EXC_IEQ:
2345 case OP_COND_EXC_NE_UN:
2346 case OP_COND_EXC_INE_UN:
2347 case OP_CMOV_INE_UN:
2348 case OP_CMOV_LNE_UN:
2373 case OP_COND_EXC_LT:
2374 case OP_COND_EXC_ILT:
2385 case OP_COND_EXC_GT:
2386 case OP_COND_EXC_IGT:
2395 case OP_COND_EXC_LE_UN:
2396 case OP_COND_EXC_ILE_UN:
2397 case OP_CMOV_ILE_UN:
2398 case OP_CMOV_LLE_UN:
2405 case OP_CMOV_IGE_UN:
2406 case OP_CMOV_LGE_UN:
2415 case OP_COND_EXC_LT_UN:
2416 case OP_COND_EXC_ILT_UN:
2417 case OP_CMOV_ILT_UN:
2418 case OP_CMOV_LLT_UN:
2427 case OP_COND_EXC_GT_UN:
2428 case OP_COND_EXC_IGT_UN:
2429 case OP_CMOV_IGT_UN:
2430 case OP_CMOV_LGT_UN:
2433 printf ("%s\n", mono_inst_name (opcode));
2434 g_assert_not_reached ();
2440 mono_negate_cond (CompRelation cond)
2464 g_assert_not_reached ();
2469 mono_opcode_to_type (int opcode, int cmp_opcode)
2471 if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2473 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2475 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2477 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2479 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2481 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2483 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2485 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2487 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2488 switch (cmp_opcode) {
2490 case OP_ICOMPARE_IMM:
2496 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2501 #endif /* DISABLE_JIT */
2504 mono_is_regsize_var (MonoType *t)
2508 t = mono_type_get_underlying_type (t);
2510 case MONO_TYPE_BOOLEAN:
2511 case MONO_TYPE_CHAR:
2521 case MONO_TYPE_FNPTR:
2522 #if SIZEOF_REGISTER == 8
2527 case MONO_TYPE_OBJECT:
2528 case MONO_TYPE_STRING:
2529 case MONO_TYPE_CLASS:
2530 case MONO_TYPE_SZARRAY:
2531 case MONO_TYPE_ARRAY:
2533 case MONO_TYPE_GENERICINST:
2534 if (!mono_type_generic_inst_is_valuetype (t))
2537 case MONO_TYPE_VALUETYPE:
2547 * mono_peephole_ins:
2549 * Perform some architecture independent peephole optimizations.
2552 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2554 MonoInst *last_ins = ins->prev;
2556 switch (ins->opcode) {
2558 /* remove unnecessary multiplication with 1 */
2559 if (ins->inst_imm == 1) {
2560 if (ins->dreg != ins->sreg1)
2561 ins->opcode = OP_MOVE;
2563 MONO_DELETE_INS (bb, ins);
2566 case OP_LOAD_MEMBASE:
2567 case OP_LOADI4_MEMBASE:
2569 * Note: if reg1 = reg2 the load op is removed
2571 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2572 * OP_LOAD_MEMBASE offset(basereg), reg2
2574 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2575 * OP_MOVE reg1, reg2
2577 if (last_ins && last_ins->opcode == OP_GC_LIVENESS_DEF)
2578 last_ins = last_ins->prev;
2580 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2581 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2582 ins->inst_basereg == last_ins->inst_destbasereg &&
2583 ins->inst_offset == last_ins->inst_offset) {
2584 if (ins->dreg == last_ins->sreg1) {
2585 MONO_DELETE_INS (bb, ins);
2588 ins->opcode = OP_MOVE;
2589 ins->sreg1 = last_ins->sreg1;
2593 * Note: reg1 must be different from the basereg in the second load
2594 * Note: if reg1 = reg2 is equal then second load is removed
2596 * OP_LOAD_MEMBASE offset(basereg), reg1
2597 * OP_LOAD_MEMBASE offset(basereg), reg2
2599 * OP_LOAD_MEMBASE offset(basereg), reg1
2600 * OP_MOVE reg1, reg2
2602 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2603 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2604 ins->inst_basereg != last_ins->dreg &&
2605 ins->inst_basereg == last_ins->inst_basereg &&
2606 ins->inst_offset == last_ins->inst_offset) {
2608 if (ins->dreg == last_ins->dreg) {
2609 MONO_DELETE_INS (bb, ins);
2611 ins->opcode = OP_MOVE;
2612 ins->sreg1 = last_ins->dreg;
2615 //g_assert_not_reached ();
2619 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2620 * OP_LOAD_MEMBASE offset(basereg), reg
2622 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2623 * OP_ICONST reg, imm
2625 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2626 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2627 ins->inst_basereg == last_ins->inst_destbasereg &&
2628 ins->inst_offset == last_ins->inst_offset) {
2629 ins->opcode = OP_ICONST;
2630 ins->inst_c0 = last_ins->inst_imm;
2631 g_assert_not_reached (); // check this rule
2635 case OP_LOADI1_MEMBASE:
2636 case OP_LOADU1_MEMBASE:
2638 * Note: if reg1 = reg2 the load op is removed
2640 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2641 * OP_LOAD_MEMBASE offset(basereg), reg2
2643 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2644 * OP_MOVE reg1, reg2
2646 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2647 ins->inst_basereg == last_ins->inst_destbasereg &&
2648 ins->inst_offset == last_ins->inst_offset) {
2649 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2650 ins->sreg1 = last_ins->sreg1;
2653 case OP_LOADI2_MEMBASE:
2654 case OP_LOADU2_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_STOREI2_MEMBASE_REG) &&
2665 ins->inst_basereg == last_ins->inst_destbasereg &&
2666 ins->inst_offset == last_ins->inst_offset) {
2667 #if SIZEOF_REGISTER == 8
2668 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2670 /* The definition of OP_PCONV_TO_U2 is wrong */
2671 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2673 ins->sreg1 = last_ins->sreg1;
2683 if (ins->dreg == ins->sreg1) {
2684 MONO_DELETE_INS (bb, ins);
2690 * OP_MOVE sreg, dreg
2691 * OP_MOVE dreg, sreg
2693 if (last_ins && last_ins->opcode == ins->opcode &&
2694 ins->sreg1 == last_ins->dreg &&
2695 ins->dreg == last_ins->sreg1) {
2696 MONO_DELETE_INS (bb, ins);
2700 MONO_DELETE_INS (bb, ins);
2706 mini_exception_id_by_name (const char *name)
2708 if (strcmp (name, "IndexOutOfRangeException") == 0)
2709 return MONO_EXC_INDEX_OUT_OF_RANGE;
2710 if (strcmp (name, "OverflowException") == 0)
2711 return MONO_EXC_OVERFLOW;
2712 if (strcmp (name, "ArithmeticException") == 0)
2713 return MONO_EXC_ARITHMETIC;
2714 if (strcmp (name, "DivideByZeroException") == 0)
2715 return MONO_EXC_DIVIDE_BY_ZERO;
2716 if (strcmp (name, "InvalidCastException") == 0)
2717 return MONO_EXC_INVALID_CAST;
2718 if (strcmp (name, "NullReferenceException") == 0)
2719 return MONO_EXC_NULL_REF;
2720 if (strcmp (name, "ArrayTypeMismatchException") == 0)
2721 return MONO_EXC_ARRAY_TYPE_MISMATCH;
2722 if (strcmp (name, "ArgumentException") == 0)
2723 return MONO_EXC_ARGUMENT;
2724 g_error ("Unknown intrinsic exception %s\n", name);
2728 #endif /* DISABLE_JIT */