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 resize_spill_info (MonoCompile *cfg, int bank)
277 MonoSpillInfo *orig_info = cfg->spill_info [bank];
278 int orig_len = cfg->spill_info_len [bank];
279 int new_len = orig_len ? orig_len * 2 : 16;
280 MonoSpillInfo *new_info;
283 g_assert (bank < MONO_NUM_REGBANKS);
285 new_info = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoSpillInfo) * new_len);
287 memcpy (new_info, orig_info, sizeof (MonoSpillInfo) * orig_len);
288 for (i = orig_len; i < new_len; ++i)
289 new_info [i].offset = -1;
291 cfg->spill_info [bank] = new_info;
292 cfg->spill_info_len [bank] = new_len;
296 * returns the offset used by spillvar. It allocates a new
297 * spill variable if necessary.
300 mono_spillvar_offset (MonoCompile *cfg, int spillvar, int bank)
305 if (G_UNLIKELY (spillvar >= (cfg->spill_info_len [bank]))) {
306 while (spillvar >= cfg->spill_info_len [bank])
307 resize_spill_info (cfg, bank);
311 * Allocate separate spill slots for fp/non-fp variables since most processors prefer it.
313 info = &cfg->spill_info [bank][spillvar];
314 if (info->offset == -1) {
315 cfg->stack_offset += sizeof (mgreg_t) - 1;
316 cfg->stack_offset &= ~(sizeof (mgreg_t) - 1);
318 g_assert (bank < MONO_NUM_REGBANKS);
319 if (G_UNLIKELY (bank))
320 size = regbank_spill_var_size [bank];
322 size = sizeof (mgreg_t);
324 if (cfg->flags & MONO_CFG_HAS_SPILLUP) {
325 cfg->stack_offset += size - 1;
326 cfg->stack_offset &= ~(size - 1);
327 info->offset = cfg->stack_offset;
328 cfg->stack_offset += size;
330 cfg->stack_offset += size - 1;
331 cfg->stack_offset &= ~(size - 1);
332 cfg->stack_offset += size;
333 info->offset = - cfg->stack_offset;
340 #define is_hard_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS)
341 #define is_hard_freg(r) ((r) >= 0 && (r) < MONO_MAX_FREGS)
342 #define is_global_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_SAVED_REGS & (regmask (r))))
343 #define is_local_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_REGS & (regmask (r))))
344 #define is_global_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_SAVED_FREGS & (regmask (r))))
345 #define is_local_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_FREGS & (regmask (r))))
347 #define is_hard_reg(r,bank) (G_UNLIKELY (bank) ? ((r) >= 0 && (r) < regbank_size [bank]) : ((r) < MONO_MAX_IREGS))
348 #define is_soft_reg(r,bank) (!is_hard_reg((r),(bank)))
349 #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))
350 #define is_local_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_regs [bank] & regmask (r))) : is_local_ireg (r))
351 #define reg_is_freeable(r,bank) (G_UNLIKELY (bank) ? is_local_reg ((r), (bank)) : is_local_ireg ((r)))
353 #ifndef MONO_ARCH_INST_IS_FLOAT
354 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc) == 'f')
357 #define reg_is_fp(desc) (MONO_ARCH_INST_IS_FLOAT (desc))
358 #define dreg_is_fp(spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_DEST]))
359 #define sreg_is_fp(n,spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_SRC1+(n)]))
360 #define sreg1_is_fp(spec) sreg_is_fp (0,(spec))
361 #define sreg2_is_fp(spec) sreg_is_fp (1,(spec))
363 #define reg_is_simd(desc) ((desc) == 'x')
365 #ifdef MONO_ARCH_NEED_SIMD_BANK
367 #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)
371 #define reg_bank(desc) reg_is_fp ((desc))
375 #define sreg_bank(n,spec) reg_bank ((spec)[MONO_INST_SRC1+(n)])
376 #define sreg1_bank(spec) sreg_bank (0, (spec))
377 #define sreg2_bank(spec) sreg_bank (1, (spec))
378 #define dreg_bank(spec) reg_bank ((spec)[MONO_INST_DEST])
380 #define sreg_bank_ins(n,ins) sreg_bank ((n), ins_get_spec ((ins)->opcode))
381 #define sreg1_bank_ins(ins) sreg_bank_ins (0, (ins))
382 #define sreg2_bank_ins(ins) sreg_bank_ins (1, (ins))
383 #define dreg_bank_ins(ins) dreg_bank (ins_get_spec ((ins)->opcode))
385 #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)
387 #ifdef MONO_ARCH_IS_GLOBAL_IREG
388 #undef is_global_ireg
389 #define is_global_ireg(reg) MONO_ARCH_IS_GLOBAL_IREG ((reg))
398 regmask_t preferred_mask; /* the hreg where the register should be allocated, or 0 */
401 #ifndef DISABLE_LOGGING
403 mono_print_ins_index (int i, MonoInst *ins)
405 const char *spec = ins_get_spec (ins->opcode);
407 int sregs [MONO_MAX_SRC_REGS];
410 printf ("\t%-2d %s", i, mono_inst_name (ins->opcode));
412 printf (" %s", mono_inst_name (ins->opcode));
413 if (spec == MONO_ARCH_CPU_SPEC) {
414 /* This is a lowered opcode */
416 printf (" R%d <-", ins->dreg);
417 if (ins->sreg1 != -1)
418 printf (" R%d", ins->sreg1);
419 if (ins->sreg2 != -1)
420 printf (" R%d", ins->sreg2);
421 if (ins->sreg3 != -1)
422 printf (" R%d", ins->sreg3);
424 switch (ins->opcode) {
435 if (!ins->inst_false_bb)
436 printf (" [B%d]", ins->inst_true_bb->block_num);
438 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
445 printf (" [%d (", (int)ins->inst_c0);
446 for (i = 0; i < ins->inst_phi_args [0]; i++) {
449 printf ("R%d", ins->inst_phi_args [i + 1]);
455 case OP_OUTARG_VTRETADDR:
456 printf (" R%d", ((MonoInst*)ins->inst_p0)->dreg);
459 printf (" + 0x%lx", (long)ins->inst_offset);
466 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
470 if (spec [MONO_INST_DEST]) {
471 int bank = dreg_bank (spec);
472 if (is_soft_reg (ins->dreg, bank)) {
473 if (spec [MONO_INST_DEST] == 'b') {
474 if (ins->inst_offset == 0)
475 printf (" [R%d] <-", ins->dreg);
477 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
480 printf (" R%d <-", ins->dreg);
481 } else if (spec [MONO_INST_DEST] == 'b') {
482 if (ins->inst_offset == 0)
483 printf (" [%s] <-", mono_arch_regname (ins->dreg));
485 printf (" [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
487 printf (" %s <-", mono_regname_full (ins->dreg, bank));
489 if (spec [MONO_INST_SRC1]) {
490 int bank = sreg1_bank (spec);
491 if (is_soft_reg (ins->sreg1, bank)) {
492 if (spec [MONO_INST_SRC1] == 'b')
493 printf (" [R%d + 0x%lx]", ins->sreg1, (long)ins->inst_offset);
495 printf (" R%d", ins->sreg1);
496 } else if (spec [MONO_INST_SRC1] == 'b')
497 printf (" [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
499 printf (" %s", mono_regname_full (ins->sreg1, bank));
501 num_sregs = mono_inst_get_src_registers (ins, sregs);
502 for (j = 1; j < num_sregs; ++j) {
503 int bank = sreg_bank (j, spec);
504 if (is_soft_reg (sregs [j], bank))
505 printf (" R%d", sregs [j]);
507 printf (" %s", mono_regname_full (sregs [j], bank));
510 switch (ins->opcode) {
512 printf (" [%d]", (int)ins->inst_c0);
514 #if defined(TARGET_X86) || defined(TARGET_AMD64)
515 case OP_X86_PUSH_IMM:
517 case OP_ICOMPARE_IMM:
524 printf (" [%d]", (int)ins->inst_imm);
528 printf (" [%d]", (int)(gssize)ins->inst_p1);
531 printf (" [%lld]", (long long)ins->inst_l);
534 printf (" [%f]", *(double*)ins->inst_p0);
537 printf (" [%f]", *(float*)ins->inst_p0);
540 case OP_CALL_MEMBASE:
549 case OP_VCALL_MEMBASE:
552 case OP_VCALL2_MEMBASE:
554 case OP_VOIDCALL_MEMBASE:
555 case OP_VOIDCALLVIRT: {
556 MonoCallInst *call = (MonoCallInst*)ins;
559 if (ins->opcode == OP_VCALL || ins->opcode == OP_VCALL_REG || ins->opcode == OP_VCALL_MEMBASE) {
561 * These are lowered opcodes, but they are in the .md files since the old
562 * JIT passes them to backends.
565 printf (" R%d <-", ins->dreg);
569 char *full_name = mono_method_full_name (call->method, TRUE);
570 printf (" [%s]", full_name);
572 } else if (call->fptr) {
573 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
575 printf (" [%s]", info->name);
578 list = call->out_ireg_args;
583 regpair = (guint32)(gssize)(list->data);
584 hreg = regpair >> 24;
585 reg = regpair & 0xffffff;
587 printf (" [%s <- R%d]", mono_arch_regname (hreg), reg);
589 list = g_slist_next (list);
594 case OP_CALL_HANDLER:
595 printf (" [B%d]", ins->inst_target_bb->block_num);
617 if (!ins->inst_false_bb)
618 printf (" [B%d]", ins->inst_true_bb->block_num);
620 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
622 case OP_LIVERANGE_START:
623 case OP_LIVERANGE_END:
624 case OP_GC_LIVENESS_DEF:
625 case OP_GC_LIVENESS_USE:
626 printf (" R%d", (int)ins->inst_c1);
632 if (spec [MONO_INST_CLOB])
633 printf (" clobbers: %c", spec [MONO_INST_CLOB]);
638 print_regtrack (RegTrack *t, int num)
644 for (i = 0; i < num; ++i) {
647 if (i >= MONO_MAX_IREGS) {
648 g_snprintf (buf, sizeof(buf), "R%d", i);
651 r = mono_arch_regname (i);
652 printf ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].killed_in);
657 mono_print_ins_index (int i, MonoInst *ins)
660 #endif /* DISABLE_LOGGING */
663 mono_print_ins (MonoInst *ins)
665 mono_print_ins_index (-1, ins);
669 insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst* to_insert)
672 * If this function is called multiple times, the new instructions are inserted
673 * in the proper order.
675 mono_bblock_insert_before_ins (bb, ins, to_insert);
679 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst **last, MonoInst* to_insert)
682 * If this function is called multiple times, the new instructions are inserted in
685 mono_bblock_insert_after_ins (bb, *last, to_insert);
691 get_vreg_bank (MonoCompile *cfg, int reg, int bank)
693 if (vreg_is_ref (cfg, reg))
694 return MONO_REG_INT_REF;
695 else if (vreg_is_mp (cfg, reg))
696 return MONO_REG_INT_MP;
702 * Force the spilling of the variable in the symbolic register 'reg', and free
703 * the hreg it was assigned to.
706 spill_vreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int reg, int bank)
711 MonoRegState *rs = cfg->rs;
713 symbolic = rs->symbolic [bank];
714 sel = rs->vassign [reg];
716 /* the vreg we need to spill lives in another logical reg bank */
717 bank = translate_bank (cfg->rs, bank, sel);
719 /*i = rs->isymbolic [sel];
720 g_assert (i == reg);*/
722 spill = ++cfg->spill_count;
723 rs->vassign [i] = -spill - 1;
724 if (G_UNLIKELY (bank))
725 mono_regstate_free_general (rs, sel, bank);
727 mono_regstate_free_int (rs, sel);
728 /* we need to create a spill var and insert a load to sel after the current instruction */
729 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
731 load->inst_basereg = cfg->frame_reg;
732 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, reg, bank));
733 insert_after_ins (bb, ins, last, load);
734 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)));
735 if (G_UNLIKELY (bank))
736 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
738 i = mono_regstate_alloc_int (rs, regmask (sel));
741 if (G_UNLIKELY (bank))
742 mono_regstate_free_general (rs, sel, bank);
744 mono_regstate_free_int (rs, sel);
747 /* This isn't defined on older glib versions and on some platforms */
748 #ifndef G_GUINT64_FORMAT
749 #define G_GUINT64_FORMAT "ul"
753 get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t regmask, int reg, int bank)
756 int i, sel, spill, num_sregs;
757 int sregs [MONO_MAX_SRC_REGS];
759 MonoRegState *rs = cfg->rs;
761 symbolic = rs->symbolic [bank];
763 g_assert (bank < MONO_NUM_REGBANKS);
765 DEBUG (printf ("\tstart regmask to assign R%d: 0x%08" G_GUINT64_FORMAT " (R%d <- R%d R%d R%d)\n", reg, (guint64)regmask, ins->dreg, ins->sreg1, ins->sreg2, ins->sreg3));
766 /* exclude the registers in the current instruction */
767 num_sregs = mono_inst_get_src_registers (ins, sregs);
768 for (i = 0; i < num_sregs; ++i) {
769 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))) {
770 if (is_soft_reg (sregs [i], bank))
771 regmask &= ~ (regmask (rs->vassign [sregs [i]]));
773 regmask &= ~ (regmask (sregs [i]));
774 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i + 1, mono_regname_full (sregs [i], bank), sregs [i]));
777 if ((dreg_bank_ins (ins) == bank) && (reg != ins->dreg) && reg_is_freeable (ins->dreg, bank)) {
778 regmask &= ~ (regmask (ins->dreg));
779 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins->dreg, bank)));
782 DEBUG (printf ("\t\tavailable regmask: 0x%08" G_GUINT64_FORMAT "\n", (guint64)regmask));
783 g_assert (regmask); /* need at least a register we can free */
785 /* we should track prev_use and spill the register that's farther */
786 if (G_UNLIKELY (bank)) {
787 for (i = 0; i < regbank_size [bank]; ++i) {
788 if (regmask & (regmask (i))) {
791 /* the vreg we need to load lives in another logical bank */
792 bank = translate_bank (cfg->rs, bank, sel);
794 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
799 i = rs->symbolic [bank] [sel];
800 spill = ++cfg->spill_count;
801 rs->vassign [i] = -spill - 1;
802 mono_regstate_free_general (rs, sel, bank);
805 for (i = 0; i < MONO_MAX_IREGS; ++i) {
806 if (regmask & (regmask (i))) {
808 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), rs->isymbolic [sel]));
813 i = rs->isymbolic [sel];
814 spill = ++cfg->spill_count;
815 rs->vassign [i] = -spill - 1;
816 mono_regstate_free_int (rs, sel);
819 /* we need to create a spill var and insert a load to sel after the current instruction */
820 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
822 load->inst_basereg = cfg->frame_reg;
823 load->inst_offset = mono_spillvar_offset (cfg, spill, get_vreg_bank (cfg, i, bank));
824 insert_after_ins (bb, ins, last, load);
825 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)));
826 if (G_UNLIKELY (bank))
827 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
829 i = mono_regstate_alloc_int (rs, regmask (sel));
838 * Free up the hreg HREG by spilling the vreg allocated to it.
841 free_up_hreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int hreg, int bank)
843 if (G_UNLIKELY (bank)) {
844 if (!(cfg->rs->free_mask [1] & (regmask (hreg)))) {
845 bank = translate_bank (cfg->rs, bank, hreg);
846 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
847 spill_vreg (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
851 if (!(cfg->rs->ifree_mask & (regmask (hreg)))) {
852 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->isymbolic [hreg]));
853 spill_vreg (cfg, bb, last, ins, cfg->rs->isymbolic [hreg], bank);
859 create_copy_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, int dest, int src, MonoInst *ins, const unsigned char *ip, int bank)
863 MONO_INST_NEW (cfg, copy, regbank_move_ops [bank]);
869 mono_bblock_insert_after_ins (bb, ins, copy);
872 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src, bank), mono_regname_full (dest, bank)));
876 static inline const char*
877 regbank_to_string (int bank)
879 if (bank == MONO_REG_INT_REF)
881 else if (bank == MONO_REG_INT_MP)
888 create_spilled_store (MonoCompile *cfg, MonoBasicBlock *bb, int spill, int reg, int prev_reg, MonoInst **last, MonoInst *ins, MonoInst *insert_before, int bank)
890 MonoInst *store, *def;
892 bank = get_vreg_bank (cfg, prev_reg, bank);
894 MONO_INST_NEW (cfg, store, regbank_store_ops [bank]);
896 store->inst_destbasereg = cfg->frame_reg;
897 store->inst_offset = mono_spillvar_offset (cfg, spill, bank);
899 mono_bblock_insert_after_ins (bb, ins, store);
901 } else if (insert_before) {
902 insert_before_ins (bb, insert_before, store);
904 g_assert_not_reached ();
906 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)));
908 if (((bank == MONO_REG_INT_REF) || (bank == MONO_REG_INT_MP)) && cfg->compute_gc_maps) {
909 g_assert (prev_reg != -1);
910 MONO_INST_NEW (cfg, def, OP_GC_SPILL_SLOT_LIVENESS_DEF);
911 def->inst_c0 = spill;
913 mono_bblock_insert_after_ins (bb, store, def);
917 /* flags used in reginfo->flags */
919 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
920 MONO_FP_NEEDS_SPILL = regmask (1),
921 MONO_FP_NEEDS_LOAD = regmask (2)
925 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
929 if (info && info->preferred_mask) {
930 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
932 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
937 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
939 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
945 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
949 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
952 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
958 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
960 if (G_UNLIKELY (bank))
961 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
963 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
967 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
969 if (G_UNLIKELY (bank)) {
972 g_assert (reg >= regbank_size [bank]);
973 g_assert (hreg < regbank_size [bank]);
974 g_assert (! is_global_freg (hreg));
976 rs->vassign [reg] = hreg;
977 rs->symbolic [bank] [hreg] = reg;
978 rs->free_mask [bank] &= ~ (regmask (hreg));
980 mirrored_bank = get_mirrored_bank (bank);
981 if (mirrored_bank == -1)
984 /* Make sure the other logical reg bank that this bank shares
985 * a single hard reg bank knows that this hard reg is not free.
987 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
989 /* Mark the other logical bank that the this bank shares
990 * a single hard reg bank with as mirrored.
992 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
996 g_assert (reg >= MONO_MAX_IREGS);
997 g_assert (hreg < MONO_MAX_IREGS);
999 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
1000 g_assert (! is_global_ireg (hreg));
1003 rs->vassign [reg] = hreg;
1004 rs->isymbolic [hreg] = reg;
1005 rs->ifree_mask &= ~ (regmask (hreg));
1009 static inline regmask_t
1010 get_callee_mask (const char spec)
1012 if (G_UNLIKELY (reg_bank (spec)))
1013 return regbank_callee_regs [reg_bank (spec)];
1014 return MONO_ARCH_CALLEE_REGS;
1017 static gint8 desc_to_fixed_reg [256];
1018 static gboolean desc_to_fixed_reg_inited = FALSE;
1023 * Local register allocation.
1024 * We first scan the list of instructions and we save the liveness info of
1025 * each register (when the register is first used, when it's value is set etc.).
1026 * We also reverse the list of instructions because assigning registers backwards allows
1027 * for more tricks to be used.
1030 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1032 MonoInst *ins, *prev, *last;
1034 MonoRegState *rs = cfg->rs;
1038 unsigned char spec_src1, spec_dest;
1040 #if MONO_ARCH_USE_FPSTACK
1041 gboolean has_fp = FALSE;
1046 int sregs [MONO_MAX_SRC_REGS];
1051 if (!desc_to_fixed_reg_inited) {
1052 for (i = 0; i < 256; ++i)
1053 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1054 desc_to_fixed_reg_inited = TRUE;
1056 /* Validate the cpu description against the info in mini-ops.h */
1057 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM)
1058 for (i = OP_LOAD; i < OP_LAST; ++i) {
1061 spec = ins_get_spec (i);
1062 ispec = INS_INFO (i);
1064 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1065 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1066 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1067 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1068 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1069 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1074 rs->next_vreg = bb->max_vreg;
1075 mono_regstate_assign (rs);
1077 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1078 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1079 rs->free_mask [i] = regbank_callee_regs [i];
1081 max = rs->next_vreg;
1083 if (cfg->reginfo && cfg->reginfo_len < max)
1084 cfg->reginfo = NULL;
1086 reginfo = cfg->reginfo;
1088 cfg->reginfo_len = MAX (1024, max * 2);
1089 reginfo = cfg->reginfo = mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1092 g_assert (cfg->reginfo_len >= rs->next_vreg);
1094 if (cfg->verbose_level > 1) {
1095 /* print_regtrack reads the info of all variables */
1096 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1100 * For large methods, next_vreg can be very large, so g_malloc0 time can
1101 * be prohibitive. So we manually init the reginfo entries used by the
1104 for (ins = bb->code; ins; ins = ins->next) {
1105 spec = ins_get_spec (ins->opcode);
1107 if ((ins->dreg != -1) && (ins->dreg < max)) {
1108 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1109 #if SIZEOF_REGISTER == 4
1110 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1112 * In the new IR, the two vregs of the regpair do not alias the
1113 * original long vreg. shift the vreg here so the rest of the
1114 * allocator doesn't have to care about it.
1117 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1122 num_sregs = mono_inst_get_src_registers (ins, sregs);
1123 for (j = 0; j < num_sregs; ++j) {
1124 g_assert (sregs [j] != -1);
1125 if (sregs [j] < max) {
1126 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1127 #if SIZEOF_REGISTER == 4
1128 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1130 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1135 mono_inst_set_src_registers (ins, sregs);
1138 /*if (cfg->opt & MONO_OPT_COPYPROP)
1139 local_copy_prop (cfg, ins);*/
1142 DEBUG (printf ("\nLOCAL REGALLOC BLOCK %d:\n", bb->block_num));
1143 /* forward pass on the instructions to collect register liveness info */
1144 MONO_BB_FOR_EACH_INS (bb, ins) {
1145 spec = ins_get_spec (ins->opcode);
1146 spec_dest = spec [MONO_INST_DEST];
1148 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1149 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1152 DEBUG (mono_print_ins_index (i, ins));
1154 num_sregs = mono_inst_get_src_registers (ins, sregs);
1156 #if MONO_ARCH_USE_FPSTACK
1157 if (dreg_is_fp (spec)) {
1160 for (j = 0; j < num_sregs; ++j) {
1161 if (sreg_is_fp (j, spec))
1167 for (j = 0; j < num_sregs; ++j) {
1168 int sreg = sregs [j];
1169 int sreg_spec = spec [MONO_INST_SRC1 + j];
1171 bank = sreg_bank (j, spec);
1172 g_assert (sreg != -1);
1173 if (is_soft_reg (sreg, bank))
1174 /* This means the vreg is not local to this bb */
1175 g_assert (reginfo [sreg].born_in > 0);
1176 rs->vassign [sreg] = -1;
1177 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1178 //reginfo [ins->sreg2].last_use = i;
1179 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1180 /* The virtual register is allocated sequentially */
1181 rs->vassign [sreg + 1] = -1;
1182 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1183 //reginfo [ins->sreg2 + 1].last_use = i;
1184 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1185 reginfo [sreg + 1].born_in = i;
1191 mono_inst_set_src_registers (ins, sregs);
1196 bank = dreg_bank (spec);
1197 if (spec_dest != 'b') /* it's not just a base register */
1198 reginfo [ins->dreg].killed_in = i;
1199 g_assert (ins->dreg != -1);
1200 rs->vassign [ins->dreg] = -1;
1201 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1202 //reginfo [ins->dreg].last_use = i;
1203 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1204 reginfo [ins->dreg].born_in = i;
1206 dest_dreg = desc_to_fixed_reg [spec_dest];
1207 if (dest_dreg != -1)
1208 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1210 #ifdef MONO_ARCH_INST_FIXED_MASK
1211 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1214 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1215 /* The virtual register is allocated sequentially */
1216 rs->vassign [ins->dreg + 1] = -1;
1217 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1218 //reginfo [ins->dreg + 1].last_use = i;
1219 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1220 reginfo [ins->dreg + 1].born_in = i;
1221 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1222 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1228 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1229 /* A call instruction implicitly uses all registers in call->out_ireg_args */
1231 MonoCallInst *call = (MonoCallInst*)ins;
1234 list = call->out_ireg_args;
1240 regpair = (guint32)(gssize)(list->data);
1241 hreg = regpair >> 24;
1242 reg = regpair & 0xffffff;
1244 //reginfo [reg].prev_use = reginfo [reg].last_use;
1245 //reginfo [reg].last_use = i;
1247 list = g_slist_next (list);
1251 list = call->out_freg_args;
1257 regpair = (guint32)(gssize)(list->data);
1258 hreg = regpair >> 24;
1259 reg = regpair & 0xffffff;
1261 list = g_slist_next (list);
1271 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1272 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1273 int prev_dreg, clob_dreg;
1274 int dest_dreg, clob_reg;
1275 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1276 int dreg_high, sreg1_high;
1277 regmask_t dreg_mask, mask;
1278 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1279 regmask_t dreg_fixed_mask;
1280 const unsigned char *ip;
1282 spec = ins_get_spec (ins->opcode);
1283 spec_src1 = spec [MONO_INST_SRC1];
1284 spec_dest = spec [MONO_INST_DEST];
1291 dreg_mask = get_callee_mask (spec_dest);
1292 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1293 prev_sregs [j] = -1;
1294 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1295 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1296 #ifdef MONO_ARCH_INST_FIXED_MASK
1297 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1299 sreg_fixed_masks [j] = 0;
1303 DEBUG (printf ("processing:"));
1304 DEBUG (mono_print_ins_index (i, ins));
1313 dest_dreg = desc_to_fixed_reg [spec_dest];
1314 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1315 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1317 #ifdef MONO_ARCH_INST_FIXED_MASK
1318 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1320 dreg_fixed_mask = 0;
1323 num_sregs = mono_inst_get_src_registers (ins, sregs);
1326 * TRACK FIXED SREG2, 3, ...
1328 for (j = 1; j < num_sregs; ++j) {
1329 int sreg = sregs [j];
1330 int dest_sreg = dest_sregs [j];
1332 if (dest_sreg == -1)
1340 * We need to special case this, since on x86, there are only 3
1341 * free registers, and the code below assigns one of them to
1342 * sreg, so we can run out of registers when trying to assign
1343 * dreg. Instead, we just set up the register masks, and let the
1344 * normal sreg2 assignment code handle this. It would be nice to
1345 * do this for all the fixed reg cases too, but there is too much
1349 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1350 sreg_masks [j] = regmask (dest_sreg);
1351 for (k = 0; k < num_sregs; ++k) {
1353 sreg_masks [k] &= ~ (regmask (dest_sreg));
1357 * Spill sreg1/2 if they are assigned to dest_sreg.
1359 for (k = 0; k < num_sregs; ++k) {
1360 if (k != j && is_soft_reg (sregs [k], 0) && rs->vassign [sregs [k]] == dest_sreg)
1361 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1365 * We can also run out of registers while processing sreg2 if sreg3 is
1366 * assigned to another hreg, so spill sreg3 now.
1368 if (is_soft_reg (sreg, 0) && rs->vassign [sreg] >= 0 && rs->vassign [sreg] != dest_sreg) {
1369 spill_vreg (cfg, bb, tmp, ins, sreg, 0);
1374 if (rs->ifree_mask & (regmask (dest_sreg))) {
1375 if (is_global_ireg (sreg)) {
1377 /* Argument already in hard reg, need to copy */
1378 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1379 insert_before_ins (bb, ins, copy);
1380 for (k = 0; k < num_sregs; ++k) {
1382 sreg_masks [k] &= ~ (regmask (dest_sreg));
1385 val = rs->vassign [sreg];
1387 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1388 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1389 } else if (val < -1) {
1391 g_assert_not_reached ();
1393 /* Argument already in hard reg, need to copy */
1394 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1397 insert_before_ins (bb, ins, copy);
1398 for (k = 0; k < num_sregs; ++k) {
1400 sreg_masks [k] &= ~ (regmask (dest_sreg));
1403 * Prevent the dreg from being allocate to dest_sreg
1404 * too, since it could force sreg1 to be allocated to
1405 * the same reg on x86.
1407 dreg_mask &= ~ (regmask (dest_sreg));
1411 gboolean need_spill = TRUE;
1412 gboolean need_assign = TRUE;
1415 dreg_mask &= ~ (regmask (dest_sreg));
1416 for (k = 0; k < num_sregs; ++k) {
1418 sreg_masks [k] &= ~ (regmask (dest_sreg));
1422 * First check if dreg is assigned to dest_sreg2, since we
1423 * can't spill a dreg.
1425 if (spec [MONO_INST_DEST])
1426 val = rs->vassign [ins->dreg];
1429 if (val == dest_sreg && ins->dreg != sreg) {
1431 * the destination register is already assigned to
1432 * dest_sreg2: we need to allocate another register for it
1433 * and then copy from this to dest_sreg2.
1436 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1437 g_assert (new_dest >= 0);
1438 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1440 prev_dreg = ins->dreg;
1441 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1442 clob_dreg = ins->dreg;
1443 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1444 mono_regstate_free_int (rs, dest_sreg);
1448 if (is_global_ireg (sreg)) {
1449 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1450 insert_before_ins (bb, ins, copy);
1451 need_assign = FALSE;
1454 val = rs->vassign [sreg];
1455 if (val == dest_sreg) {
1456 /* sreg2 is already assigned to the correct register */
1458 } else if (val < -1) {
1459 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1460 } else if (val >= 0) {
1461 /* sreg2 already assigned to another register */
1463 * We couldn't emit a copy from val to dest_sreg2, because
1464 * val might be spilled later while processing this
1465 * instruction. So we spill sreg2 so it can be allocated to
1468 free_up_hreg (cfg, bb, tmp, ins, val, 0);
1473 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1477 if (rs->vassign [sreg] < -1) {
1480 /* Need to emit a spill store */
1481 spill = - rs->vassign [sreg] - 1;
1482 create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, ins, bank);
1484 /* force-set sreg2 */
1485 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1488 sregs [j] = dest_sreg;
1490 mono_inst_set_src_registers (ins, sregs);
1495 bank = dreg_bank (spec);
1496 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1497 prev_dreg = ins->dreg;
1500 if (spec_dest == 'b') {
1502 * The dest reg is read by the instruction, not written, so
1503 * avoid allocating sreg1/sreg2 to the same reg.
1505 if (dest_sregs [0] != -1)
1506 dreg_mask &= ~ (regmask (dest_sregs [0]));
1507 for (j = 1; j < num_sregs; ++j) {
1508 if (dest_sregs [j] != -1)
1509 dreg_mask &= ~ (regmask (dest_sregs [j]));
1512 val = rs->vassign [ins->dreg];
1513 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1514 /* DREG is already allocated to a register needed for sreg1 */
1515 spill_vreg (cfg, bb, tmp, ins, ins->dreg, 0);
1520 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1521 * various complex situations.
1523 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1524 guint32 dreg2, dest_dreg2;
1526 g_assert (is_soft_reg (ins->dreg, bank));
1528 if (dest_dreg != -1) {
1529 if (rs->vassign [ins->dreg] != dest_dreg)
1530 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, 0);
1532 dreg2 = ins->dreg + 1;
1533 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1534 if (dest_dreg2 != -1) {
1535 if (rs->vassign [dreg2] != dest_dreg2)
1536 free_up_hreg (cfg, bb, tmp, ins, dest_dreg2, 0);
1541 if (dreg_fixed_mask) {
1543 if (is_global_ireg (ins->dreg)) {
1545 * The argument is already in a hard reg, but that reg is
1546 * not usable by this instruction, so allocate a new one.
1548 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1550 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1551 mono_regstate_free_int (rs, val);
1557 dreg_mask &= dreg_fixed_mask;
1560 if (is_soft_reg (ins->dreg, bank)) {
1561 val = rs->vassign [ins->dreg];
1566 /* the register gets spilled after this inst */
1569 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1570 assign_reg (cfg, rs, ins->dreg, val, bank);
1572 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, NULL, bank);
1575 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1579 /* Handle regpairs */
1580 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1581 int reg2 = prev_dreg + 1;
1584 g_assert (prev_dreg > -1);
1585 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1586 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1589 mask &= ~regmask (X86_ECX);
1591 val = rs->vassign [reg2];
1595 /* the register gets spilled after this inst */
1598 val = mono_regstate_alloc_int (rs, mask);
1600 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1602 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, NULL, bank);
1605 if (! (mask & (regmask (val)))) {
1606 val = mono_regstate_alloc_int (rs, mask);
1608 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1610 /* Reallocate hreg to the correct register */
1611 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1613 mono_regstate_free_int (rs, rs->vassign [reg2]);
1617 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1618 assign_reg (cfg, rs, reg2, val, bank);
1621 ins->backend.reg3 = val;
1623 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1624 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1625 mono_regstate_free_int (rs, val);
1629 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1631 * In theory, we could free up the hreg even if the vreg is alive,
1632 * but branches inside bblocks force us to assign the same hreg
1633 * to a vreg every time it is encountered.
1635 int dreg = rs->vassign [prev_dreg];
1636 g_assert (dreg >= 0);
1637 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1638 if (G_UNLIKELY (bank))
1639 mono_regstate_free_general (rs, dreg, bank);
1641 mono_regstate_free_int (rs, dreg);
1642 rs->vassign [prev_dreg] = -1;
1645 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1646 /* this instruction only outputs to dest_dreg, need to copy */
1647 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1648 ins->dreg = dest_dreg;
1650 if (G_UNLIKELY (bank)) {
1651 /* the register we need to free up may be used in another logical regbank
1652 * so do a translate just in case.
1654 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1655 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1656 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1659 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1660 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, bank);
1664 if (spec_dest == 'b') {
1666 * The dest reg is read by the instruction, not written, so
1667 * avoid allocating sreg1/sreg2 to the same reg.
1669 for (j = 0; j < num_sregs; ++j)
1670 if (!sreg_bank (j, spec))
1671 sreg_masks [j] &= ~ (regmask (ins->dreg));
1677 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1678 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1679 free_up_hreg (cfg, bb, tmp, ins, clob_reg, 0);
1682 if (spec [MONO_INST_CLOB] == 'c') {
1683 int j, s, dreg, dreg2, cur_bank;
1686 clob_mask = MONO_ARCH_CALLEE_REGS;
1688 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1690 * Need to avoid spilling the dreg since the dreg is not really
1691 * clobbered by the call.
1693 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1694 dreg = rs->vassign [prev_dreg];
1698 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1699 dreg2 = rs->vassign [prev_dreg + 1];
1703 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1705 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1706 if ((j != dreg) && (j != dreg2))
1707 free_up_hreg (cfg, bb, tmp, ins, j, 0);
1708 else if (rs->isymbolic [j])
1709 /* The hreg is assigned to the dreg of this instruction */
1710 rs->vassign [rs->isymbolic [j]] = -1;
1711 mono_regstate_free_int (rs, j);
1716 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1717 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1718 clob_mask = regbank_callee_regs [cur_bank];
1719 if ((prev_dreg != -1) && reg_bank (spec_dest))
1720 dreg = rs->vassign [prev_dreg];
1724 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1726 /* we are looping though the banks in the outer loop
1727 * so, we don't need to deal with mirrored hregs
1728 * because we will get them in one of the other bank passes.
1730 if (is_hreg_mirrored (rs, cur_bank, j))
1734 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s) && (j != ins->sreg1)) {
1736 free_up_hreg (cfg, bb, tmp, ins, j, cur_bank);
1737 else if (rs->symbolic [cur_bank] [j])
1738 /* The hreg is assigned to the dreg of this instruction */
1739 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1740 mono_regstate_free_general (rs, j, cur_bank);
1748 * TRACK ARGUMENT REGS
1750 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1751 MonoCallInst *call = (MonoCallInst*)ins;
1755 * This needs to be done before assigning sreg1, so sreg1 will
1756 * not be assigned one of the argument regs.
1760 * Assign all registers in call->out_reg_args to the proper
1761 * argument registers.
1764 list = call->out_ireg_args;
1770 regpair = (guint32)(gssize)(list->data);
1771 hreg = regpair >> 24;
1772 reg = regpair & 0xffffff;
1774 assign_reg (cfg, rs, reg, hreg, 0);
1776 sreg_masks [0] &= ~(regmask (hreg));
1778 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1780 list = g_slist_next (list);
1784 list = call->out_freg_args;
1790 regpair = (guint32)(gssize)(list->data);
1791 hreg = regpair >> 24;
1792 reg = regpair & 0xffffff;
1794 assign_reg (cfg, rs, reg, hreg, 1);
1796 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1798 list = g_slist_next (list);
1806 bank = sreg1_bank (spec);
1807 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1808 int sreg1 = sregs [0];
1809 int dest_sreg1 = dest_sregs [0];
1811 g_assert (is_soft_reg (sreg1, bank));
1813 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1814 if (dest_sreg1 != -1)
1815 g_assert (dest_sreg1 == ins->dreg);
1816 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1817 g_assert (val >= 0);
1819 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1821 g_assert_not_reached ();
1823 assign_reg (cfg, rs, sreg1, val, bank);
1825 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1827 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1828 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1829 g_assert (val >= 0);
1831 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1833 g_assert_not_reached ();
1835 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1837 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1839 /* Skip rest of this section */
1840 dest_sregs [0] = -1;
1843 if (sreg_fixed_masks [0]) {
1845 if (is_global_ireg (sregs [0])) {
1847 * The argument is already in a hard reg, but that reg is
1848 * not usable by this instruction, so allocate a new one.
1850 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1852 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1853 mono_regstate_free_int (rs, val);
1854 dest_sregs [0] = val;
1856 /* Fall through to the dest_sreg1 != -1 case */
1859 sreg_masks [0] &= sreg_fixed_masks [0];
1862 if (dest_sregs [0] != -1) {
1863 sreg_masks [0] = regmask (dest_sregs [0]);
1865 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1866 free_up_hreg (cfg, bb, tmp, ins, dest_sregs [0], 0);
1868 if (is_global_ireg (sregs [0])) {
1869 /* The argument is already in a hard reg, need to copy */
1870 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1871 insert_before_ins (bb, ins, copy);
1872 sregs [0] = dest_sregs [0];
1876 if (is_soft_reg (sregs [0], bank)) {
1877 val = rs->vassign [sregs [0]];
1878 prev_sregs [0] = sregs [0];
1882 /* the register gets spilled after this inst */
1886 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1888 * Allocate the same hreg to sreg1 as well so the
1889 * peephole can get rid of the move.
1891 sreg_masks [0] = regmask (ins->dreg);
1894 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1895 /* Allocate the same reg to sreg1 to avoid a copy later */
1896 sreg_masks [0] = regmask (ins->dreg);
1898 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
1899 assign_reg (cfg, rs, sregs [0], val, bank);
1900 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
1904 * Need to insert before the instruction since it can
1907 create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, ins, bank);
1910 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
1911 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
1912 insert_before_ins (bb, ins, copy);
1913 for (j = 1; j < num_sregs; ++j)
1914 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
1915 val = dest_sregs [0];
1921 prev_sregs [0] = -1;
1923 mono_inst_set_src_registers (ins, sregs);
1925 for (j = 1; j < num_sregs; ++j)
1926 sreg_masks [j] &= ~(regmask (sregs [0]));
1928 /* Handle the case when sreg1 is a regpair but dreg is not */
1929 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
1930 int reg2 = prev_sregs [0] + 1;
1933 g_assert (prev_sregs [0] > -1);
1934 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
1935 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
1936 val = rs->vassign [reg2];
1940 /* the register gets spilled after this inst */
1943 val = mono_regstate_alloc_int (rs, mask);
1945 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1947 g_assert_not_reached ();
1950 if (! (mask & (regmask (val)))) {
1951 /* The vreg is already allocated to a wrong hreg */
1953 g_assert_not_reached ();
1955 val = mono_regstate_alloc_int (rs, mask);
1957 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1959 /* Reallocate hreg to the correct register */
1960 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1962 mono_regstate_free_int (rs, rs->vassign [reg2]);
1968 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
1969 assign_reg (cfg, rs, reg2, val, bank);
1972 /* Handle dreg==sreg1 */
1973 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
1974 MonoInst *sreg2_copy = NULL;
1976 int bank = reg_bank (spec_src1);
1978 if (ins->dreg == sregs [1]) {
1980 * copying sreg1 to dreg could clobber sreg2, so allocate a new
1983 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
1985 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
1986 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
1987 prev_sregs [1] = sregs [1] = reg2;
1989 if (G_UNLIKELY (bank))
1990 mono_regstate_free_general (rs, reg2, bank);
1992 mono_regstate_free_int (rs, reg2);
1995 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
1996 /* Copying sreg1_high to dreg could also clobber sreg2 */
1997 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
1999 g_assert_not_reached ();
2002 * sreg1 and dest are already allocated to the same regpair by the
2003 * SREG1 allocation code.
2005 g_assert (sregs [0] == ins->dreg);
2006 g_assert (dreg_high == sreg1_high);
2009 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
2010 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
2011 insert_before_ins (bb, ins, copy);
2014 insert_before_ins (bb, copy, sreg2_copy);
2017 * Need to prevent sreg2 to be allocated to sreg1, since that
2018 * would screw up the previous copy.
2020 sreg_masks [1] &= ~ (regmask (sregs [0]));
2021 /* we set sreg1 to dest as well */
2022 prev_sregs [0] = sregs [0] = ins->dreg;
2023 sreg_masks [1] &= ~ (regmask (ins->dreg));
2025 mono_inst_set_src_registers (ins, sregs);
2028 * TRACK SREG2, 3, ...
2030 for (j = 1; j < num_sregs; ++j) {
2033 bank = sreg_bank (j, spec);
2034 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
2035 g_assert_not_reached ();
2037 if (dest_sregs [j] != -1 && is_global_ireg (sregs [j])) {
2039 * Argument already in a global hard reg, copy it to the fixed reg, without
2040 * allocating it to the fixed reg.
2042 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [j], sregs [j], NULL, ip, 0);
2043 insert_before_ins (bb, ins, copy);
2044 sregs [j] = dest_sregs [j];
2045 } else if (is_soft_reg (sregs [j], bank)) {
2046 val = rs->vassign [sregs [j]];
2048 if (dest_sregs [j] != -1 && val >= 0 && dest_sregs [j] != val) {
2050 * The sreg is already allocated to a hreg, but not to the fixed
2051 * reg required by the instruction. Spill the sreg, so it can be
2052 * allocated to the fixed reg by the code below.
2054 /* Currently, this code should only be hit for CAS */
2055 spill_vreg (cfg, bb, tmp, ins, sregs [j], 0);
2056 val = rs->vassign [sregs [j]];
2062 /* the register gets spilled after this inst */
2065 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
2066 assign_reg (cfg, rs, sregs [j], val, bank);
2067 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
2070 * Need to insert before the instruction since it can
2073 create_spilled_store (cfg, bb, spill, val, sregs [j], tmp, NULL, ins, bank);
2077 for (k = j + 1; k < num_sregs; ++k)
2078 sreg_masks [k] &= ~ (regmask (sregs [j]));
2081 prev_sregs [j] = -1;
2084 mono_inst_set_src_registers (ins, sregs);
2087 /* Do this only for CAS for now */
2088 for (j = 1; j < num_sregs; ++j) {
2089 int sreg = sregs [j];
2090 int dest_sreg = dest_sregs [j];
2092 if (j == 2 && dest_sreg != -1) {
2095 g_assert (sreg == dest_sreg);
2097 for (k = 0; k < num_sregs; ++k) {
2099 g_assert (sregs [k] != dest_sreg);
2104 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2105 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2106 mono_regstate_free_int (rs, ins->sreg1);
2108 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2109 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2110 mono_regstate_free_int (rs, ins->sreg2);
2113 DEBUG (mono_print_ins_index (i, ins));
2116 // FIXME: Set MAX_FREGS to 8
2117 // FIXME: Optimize generated code
2118 #if MONO_ARCH_USE_FPSTACK
2120 * Make a forward pass over the code, simulating the fp stack, making sure the
2121 * arguments required by the fp opcodes are at the top of the stack.
2124 MonoInst *prev = NULL;
2128 g_assert (num_sregs <= 2);
2130 for (ins = bb->code; ins; ins = ins->next) {
2131 spec = ins_get_spec (ins->opcode);
2133 DEBUG (printf ("processing:"));
2134 DEBUG (mono_print_ins_index (0, ins));
2136 if (ins->opcode == OP_FMOVE) {
2137 /* Do it by renaming the source to the destination on the stack */
2138 // FIXME: Is this correct ?
2139 for (i = 0; i < sp; ++i)
2140 if (fpstack [i] == ins->sreg1)
2141 fpstack [i] = ins->dreg;
2146 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2147 /* Arg1 must be in %st(1) */
2151 while ((i < sp) && (fpstack [i] != ins->sreg1))
2155 if (sp - 1 - i > 0) {
2156 /* First move it to %st(0) */
2157 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2159 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2160 fxch->inst_imm = sp - 1 - i;
2166 tmp = fpstack [sp - 1];
2167 fpstack [sp - 1] = fpstack [i];
2171 /* Then move it to %st(1) */
2172 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2174 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2181 tmp = fpstack [sp - 1];
2182 fpstack [sp - 1] = fpstack [sp - 2];
2183 fpstack [sp - 2] = tmp;
2186 if (sreg2_is_fp (spec)) {
2189 if (fpstack [sp - 1] != ins->sreg2) {
2193 while ((i < sp) && (fpstack [i] != ins->sreg2))
2197 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2199 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2200 fxch->inst_imm = sp - 1 - i;
2206 tmp = fpstack [sp - 1];
2207 fpstack [sp - 1] = fpstack [i];
2214 if (sreg1_is_fp (spec)) {
2217 if (fpstack [sp - 1] != ins->sreg1) {
2221 while ((i < sp) && (fpstack [i] != ins->sreg1))
2225 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2227 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2228 fxch->inst_imm = sp - 1 - i;
2234 tmp = fpstack [sp - 1];
2235 fpstack [sp - 1] = fpstack [i];
2242 if (dreg_is_fp (spec)) {
2244 fpstack [sp ++] = ins->dreg;
2247 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2249 for (i = 0; i < sp; ++i)
2250 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2257 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2258 /* Remove remaining items from the fp stack */
2260 * These can remain for example as a result of a dead fmove like in
2261 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2264 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2265 mono_add_ins_to_end (bb, ins);
2274 mono_opcode_to_cond (int opcode)
2284 case OP_COND_EXC_EQ:
2285 case OP_COND_EXC_IEQ:
2292 case OP_COND_EXC_NE_UN:
2293 case OP_COND_EXC_INE_UN:
2294 case OP_CMOV_INE_UN:
2295 case OP_CMOV_LNE_UN:
2316 case OP_COND_EXC_LT:
2317 case OP_COND_EXC_ILT:
2328 case OP_COND_EXC_GT:
2329 case OP_COND_EXC_IGT:
2337 case OP_COND_EXC_LE_UN:
2338 case OP_COND_EXC_ILE_UN:
2339 case OP_CMOV_ILE_UN:
2340 case OP_CMOV_LLE_UN:
2345 case OP_CMOV_IGE_UN:
2346 case OP_CMOV_LGE_UN:
2355 case OP_COND_EXC_LT_UN:
2356 case OP_COND_EXC_ILT_UN:
2357 case OP_CMOV_ILT_UN:
2358 case OP_CMOV_LLT_UN:
2367 case OP_COND_EXC_GT_UN:
2368 case OP_COND_EXC_IGT_UN:
2369 case OP_CMOV_IGT_UN:
2370 case OP_CMOV_LGT_UN:
2373 printf ("%s\n", mono_inst_name (opcode));
2374 g_assert_not_reached ();
2380 mono_negate_cond (CompRelation cond)
2404 g_assert_not_reached ();
2409 mono_opcode_to_type (int opcode, int cmp_opcode)
2411 if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2413 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2415 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2417 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2419 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2421 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2423 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2425 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2427 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2428 switch (cmp_opcode) {
2430 case OP_ICOMPARE_IMM:
2431 case OP_LCOMPARE_IMM:
2437 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2442 #endif /* DISABLE_JIT */
2445 mono_is_regsize_var (MonoType *t)
2449 t = mono_type_get_underlying_type (t);
2451 case MONO_TYPE_BOOLEAN:
2452 case MONO_TYPE_CHAR:
2462 case MONO_TYPE_FNPTR:
2463 #if SIZEOF_REGISTER == 8
2468 case MONO_TYPE_OBJECT:
2469 case MONO_TYPE_STRING:
2470 case MONO_TYPE_CLASS:
2471 case MONO_TYPE_SZARRAY:
2472 case MONO_TYPE_ARRAY:
2474 case MONO_TYPE_GENERICINST:
2475 if (!mono_type_generic_inst_is_valuetype (t))
2478 case MONO_TYPE_VALUETYPE:
2487 * mono_peephole_ins:
2489 * Perform some architecture independent peephole optimizations.
2492 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2494 MonoInst *last_ins = ins->prev;
2496 switch (ins->opcode) {
2498 /* remove unnecessary multiplication with 1 */
2499 if (ins->inst_imm == 1) {
2500 if (ins->dreg != ins->sreg1)
2501 ins->opcode = OP_MOVE;
2503 MONO_DELETE_INS (bb, ins);
2506 case OP_LOAD_MEMBASE:
2507 case OP_LOADI4_MEMBASE:
2509 * Note: if reg1 = reg2 the load op is removed
2511 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2512 * OP_LOAD_MEMBASE offset(basereg), reg2
2514 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2515 * OP_MOVE reg1, reg2
2517 if (last_ins && last_ins->opcode == OP_GC_LIVENESS_DEF)
2518 last_ins = last_ins->prev;
2520 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2521 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2522 ins->inst_basereg == last_ins->inst_destbasereg &&
2523 ins->inst_offset == last_ins->inst_offset) {
2524 if (ins->dreg == last_ins->sreg1) {
2525 MONO_DELETE_INS (bb, ins);
2528 ins->opcode = OP_MOVE;
2529 ins->sreg1 = last_ins->sreg1;
2533 * Note: reg1 must be different from the basereg in the second load
2534 * Note: if reg1 = reg2 is equal then second load is removed
2536 * OP_LOAD_MEMBASE offset(basereg), reg1
2537 * OP_LOAD_MEMBASE offset(basereg), reg2
2539 * OP_LOAD_MEMBASE offset(basereg), reg1
2540 * OP_MOVE reg1, reg2
2542 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2543 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2544 ins->inst_basereg != last_ins->dreg &&
2545 ins->inst_basereg == last_ins->inst_basereg &&
2546 ins->inst_offset == last_ins->inst_offset) {
2548 if (ins->dreg == last_ins->dreg) {
2549 MONO_DELETE_INS (bb, ins);
2551 ins->opcode = OP_MOVE;
2552 ins->sreg1 = last_ins->dreg;
2555 //g_assert_not_reached ();
2559 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2560 * OP_LOAD_MEMBASE offset(basereg), reg
2562 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2563 * OP_ICONST reg, imm
2565 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2566 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2567 ins->inst_basereg == last_ins->inst_destbasereg &&
2568 ins->inst_offset == last_ins->inst_offset) {
2569 ins->opcode = OP_ICONST;
2570 ins->inst_c0 = last_ins->inst_imm;
2571 g_assert_not_reached (); // check this rule
2575 case OP_LOADI1_MEMBASE:
2576 case OP_LOADU1_MEMBASE:
2578 * Note: if reg1 = reg2 the load op is removed
2580 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2581 * OP_LOAD_MEMBASE offset(basereg), reg2
2583 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2584 * OP_MOVE reg1, reg2
2586 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2587 ins->inst_basereg == last_ins->inst_destbasereg &&
2588 ins->inst_offset == last_ins->inst_offset) {
2589 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2590 ins->sreg1 = last_ins->sreg1;
2593 case OP_LOADI2_MEMBASE:
2594 case OP_LOADU2_MEMBASE:
2596 * Note: if reg1 = reg2 the load op is removed
2598 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2599 * OP_LOAD_MEMBASE offset(basereg), reg2
2601 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2602 * OP_MOVE reg1, reg2
2604 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2605 ins->inst_basereg == last_ins->inst_destbasereg &&
2606 ins->inst_offset == last_ins->inst_offset) {
2607 #if SIZEOF_REGISTER == 8
2608 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2610 /* The definition of OP_PCONV_TO_U2 is wrong */
2611 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2613 ins->sreg1 = last_ins->sreg1;
2623 if (ins->dreg == ins->sreg1) {
2624 MONO_DELETE_INS (bb, ins);
2630 * OP_MOVE sreg, dreg
2631 * OP_MOVE dreg, sreg
2633 if (last_ins && last_ins->opcode == ins->opcode &&
2634 ins->sreg1 == last_ins->dreg &&
2635 ins->dreg == last_ins->sreg1) {
2636 MONO_DELETE_INS (bb, ins);
2640 MONO_DELETE_INS (bb, ins);
2645 #endif /* DISABLE_JIT */