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);
918 /* flags used in reginfo->flags */
920 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
921 MONO_FP_NEEDS_SPILL = regmask (1),
922 MONO_FP_NEEDS_LOAD = regmask (2)
926 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
930 if (info && info->preferred_mask) {
931 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
933 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
938 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
940 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
946 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
950 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
953 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
959 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
961 if (G_UNLIKELY (bank))
962 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
964 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
968 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
970 if (G_UNLIKELY (bank)) {
973 g_assert (reg >= regbank_size [bank]);
974 g_assert (hreg < regbank_size [bank]);
975 g_assert (! is_global_freg (hreg));
977 rs->vassign [reg] = hreg;
978 rs->symbolic [bank] [hreg] = reg;
979 rs->free_mask [bank] &= ~ (regmask (hreg));
981 mirrored_bank = get_mirrored_bank (bank);
982 if (mirrored_bank == -1)
985 /* Make sure the other logical reg bank that this bank shares
986 * a single hard reg bank knows that this hard reg is not free.
988 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
990 /* Mark the other logical bank that the this bank shares
991 * a single hard reg bank with as mirrored.
993 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
997 g_assert (reg >= MONO_MAX_IREGS);
998 g_assert (hreg < MONO_MAX_IREGS);
1000 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
1001 g_assert (! is_global_ireg (hreg));
1004 rs->vassign [reg] = hreg;
1005 rs->isymbolic [hreg] = reg;
1006 rs->ifree_mask &= ~ (regmask (hreg));
1010 static inline regmask_t
1011 get_callee_mask (const char spec)
1013 if (G_UNLIKELY (reg_bank (spec)))
1014 return regbank_callee_regs [reg_bank (spec)];
1015 return MONO_ARCH_CALLEE_REGS;
1018 static gint8 desc_to_fixed_reg [256];
1019 static gboolean desc_to_fixed_reg_inited = FALSE;
1024 * Local register allocation.
1025 * We first scan the list of instructions and we save the liveness info of
1026 * each register (when the register is first used, when it's value is set etc.).
1027 * We also reverse the list of instructions because assigning registers backwards allows
1028 * for more tricks to be used.
1031 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1033 MonoInst *ins, *prev, *last;
1035 MonoRegState *rs = cfg->rs;
1039 unsigned char spec_src1, spec_dest;
1041 #if MONO_ARCH_USE_FPSTACK
1042 gboolean has_fp = FALSE;
1047 int sregs [MONO_MAX_SRC_REGS];
1052 if (!desc_to_fixed_reg_inited) {
1053 for (i = 0; i < 256; ++i)
1054 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1055 desc_to_fixed_reg_inited = TRUE;
1057 /* Validate the cpu description against the info in mini-ops.h */
1058 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM)
1059 for (i = OP_LOAD; i < OP_LAST; ++i) {
1062 spec = ins_get_spec (i);
1063 ispec = INS_INFO (i);
1065 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1066 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1067 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1068 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1069 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1070 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1075 rs->next_vreg = bb->max_vreg;
1076 mono_regstate_assign (rs);
1078 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1079 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1080 rs->free_mask [i] = regbank_callee_regs [i];
1082 max = rs->next_vreg;
1084 if (cfg->reginfo && cfg->reginfo_len < max)
1085 cfg->reginfo = NULL;
1087 reginfo = cfg->reginfo;
1089 cfg->reginfo_len = MAX (1024, max * 2);
1090 reginfo = cfg->reginfo = mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1093 g_assert (cfg->reginfo_len >= rs->next_vreg);
1095 if (cfg->verbose_level > 1) {
1096 /* print_regtrack reads the info of all variables */
1097 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1101 * For large methods, next_vreg can be very large, so g_malloc0 time can
1102 * be prohibitive. So we manually init the reginfo entries used by the
1105 for (ins = bb->code; ins; ins = ins->next) {
1106 spec = ins_get_spec (ins->opcode);
1108 if ((ins->dreg != -1) && (ins->dreg < max)) {
1109 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1110 #if SIZEOF_REGISTER == 4
1111 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1113 * In the new IR, the two vregs of the regpair do not alias the
1114 * original long vreg. shift the vreg here so the rest of the
1115 * allocator doesn't have to care about it.
1118 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1123 num_sregs = mono_inst_get_src_registers (ins, sregs);
1124 for (j = 0; j < num_sregs; ++j) {
1125 g_assert (sregs [j] != -1);
1126 if (sregs [j] < max) {
1127 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1128 #if SIZEOF_REGISTER == 4
1129 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1131 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1136 mono_inst_set_src_registers (ins, sregs);
1139 /*if (cfg->opt & MONO_OPT_COPYPROP)
1140 local_copy_prop (cfg, ins);*/
1143 DEBUG (printf ("\nLOCAL REGALLOC BLOCK %d:\n", bb->block_num));
1144 /* forward pass on the instructions to collect register liveness info */
1145 MONO_BB_FOR_EACH_INS (bb, ins) {
1146 spec = ins_get_spec (ins->opcode);
1147 spec_dest = spec [MONO_INST_DEST];
1149 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1150 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1153 DEBUG (mono_print_ins_index (i, ins));
1155 num_sregs = mono_inst_get_src_registers (ins, sregs);
1157 #if MONO_ARCH_USE_FPSTACK
1158 if (dreg_is_fp (spec)) {
1161 for (j = 0; j < num_sregs; ++j) {
1162 if (sreg_is_fp (j, spec))
1168 for (j = 0; j < num_sregs; ++j) {
1169 int sreg = sregs [j];
1170 int sreg_spec = spec [MONO_INST_SRC1 + j];
1172 bank = sreg_bank (j, spec);
1173 g_assert (sreg != -1);
1174 if (is_soft_reg (sreg, bank))
1175 /* This means the vreg is not local to this bb */
1176 g_assert (reginfo [sreg].born_in > 0);
1177 rs->vassign [sreg] = -1;
1178 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1179 //reginfo [ins->sreg2].last_use = i;
1180 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1181 /* The virtual register is allocated sequentially */
1182 rs->vassign [sreg + 1] = -1;
1183 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1184 //reginfo [ins->sreg2 + 1].last_use = i;
1185 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1186 reginfo [sreg + 1].born_in = i;
1192 mono_inst_set_src_registers (ins, sregs);
1197 bank = dreg_bank (spec);
1198 if (spec_dest != 'b') /* it's not just a base register */
1199 reginfo [ins->dreg].killed_in = i;
1200 g_assert (ins->dreg != -1);
1201 rs->vassign [ins->dreg] = -1;
1202 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1203 //reginfo [ins->dreg].last_use = i;
1204 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1205 reginfo [ins->dreg].born_in = i;
1207 dest_dreg = desc_to_fixed_reg [spec_dest];
1208 if (dest_dreg != -1)
1209 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1211 #ifdef MONO_ARCH_INST_FIXED_MASK
1212 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1215 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1216 /* The virtual register is allocated sequentially */
1217 rs->vassign [ins->dreg + 1] = -1;
1218 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1219 //reginfo [ins->dreg + 1].last_use = i;
1220 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1221 reginfo [ins->dreg + 1].born_in = i;
1222 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1223 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1229 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1230 /* A call instruction implicitly uses all registers in call->out_ireg_args */
1232 MonoCallInst *call = (MonoCallInst*)ins;
1235 list = call->out_ireg_args;
1241 regpair = (guint32)(gssize)(list->data);
1242 hreg = regpair >> 24;
1243 reg = regpair & 0xffffff;
1245 //reginfo [reg].prev_use = reginfo [reg].last_use;
1246 //reginfo [reg].last_use = i;
1248 list = g_slist_next (list);
1252 list = call->out_freg_args;
1258 regpair = (guint32)(gssize)(list->data);
1259 hreg = regpair >> 24;
1260 reg = regpair & 0xffffff;
1262 list = g_slist_next (list);
1272 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1273 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1274 int prev_dreg, clob_dreg;
1275 int dest_dreg, clob_reg;
1276 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1277 int dreg_high, sreg1_high;
1278 regmask_t dreg_mask, mask;
1279 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1280 regmask_t dreg_fixed_mask;
1281 const unsigned char *ip;
1283 spec = ins_get_spec (ins->opcode);
1284 spec_src1 = spec [MONO_INST_SRC1];
1285 spec_dest = spec [MONO_INST_DEST];
1292 dreg_mask = get_callee_mask (spec_dest);
1293 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1294 prev_sregs [j] = -1;
1295 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1296 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1297 #ifdef MONO_ARCH_INST_FIXED_MASK
1298 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1300 sreg_fixed_masks [j] = 0;
1304 DEBUG (printf ("processing:"));
1305 DEBUG (mono_print_ins_index (i, ins));
1314 dest_dreg = desc_to_fixed_reg [spec_dest];
1315 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1316 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1318 #ifdef MONO_ARCH_INST_FIXED_MASK
1319 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1321 dreg_fixed_mask = 0;
1324 num_sregs = mono_inst_get_src_registers (ins, sregs);
1327 * TRACK FIXED SREG2, 3, ...
1329 for (j = 1; j < num_sregs; ++j) {
1330 int sreg = sregs [j];
1331 int dest_sreg = dest_sregs [j];
1333 if (dest_sreg == -1)
1341 * We need to special case this, since on x86, there are only 3
1342 * free registers, and the code below assigns one of them to
1343 * sreg, so we can run out of registers when trying to assign
1344 * dreg. Instead, we just set up the register masks, and let the
1345 * normal sreg2 assignment code handle this. It would be nice to
1346 * do this for all the fixed reg cases too, but there is too much
1350 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1351 sreg_masks [j] = regmask (dest_sreg);
1352 for (k = 0; k < num_sregs; ++k) {
1354 sreg_masks [k] &= ~ (regmask (dest_sreg));
1358 * Spill sreg1/2 if they are assigned to dest_sreg.
1360 for (k = 0; k < num_sregs; ++k) {
1361 if (k != j && is_soft_reg (sregs [k], 0) && rs->vassign [sregs [k]] == dest_sreg)
1362 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1366 * We can also run out of registers while processing sreg2 if sreg3 is
1367 * assigned to another hreg, so spill sreg3 now.
1369 if (is_soft_reg (sreg, 0) && rs->vassign [sreg] >= 0 && rs->vassign [sreg] != dest_sreg) {
1370 spill_vreg (cfg, bb, tmp, ins, sreg, 0);
1375 if (rs->ifree_mask & (regmask (dest_sreg))) {
1376 if (is_global_ireg (sreg)) {
1378 /* Argument already in hard reg, need to copy */
1379 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1380 insert_before_ins (bb, ins, copy);
1381 for (k = 0; k < num_sregs; ++k) {
1383 sreg_masks [k] &= ~ (regmask (dest_sreg));
1386 val = rs->vassign [sreg];
1388 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1389 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1390 } else if (val < -1) {
1392 g_assert_not_reached ();
1394 /* Argument already in hard reg, need to copy */
1395 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1398 insert_before_ins (bb, ins, copy);
1399 for (k = 0; k < num_sregs; ++k) {
1401 sreg_masks [k] &= ~ (regmask (dest_sreg));
1404 * Prevent the dreg from being allocate to dest_sreg
1405 * too, since it could force sreg1 to be allocated to
1406 * the same reg on x86.
1408 dreg_mask &= ~ (regmask (dest_sreg));
1412 gboolean need_spill = TRUE;
1413 gboolean need_assign = TRUE;
1416 dreg_mask &= ~ (regmask (dest_sreg));
1417 for (k = 0; k < num_sregs; ++k) {
1419 sreg_masks [k] &= ~ (regmask (dest_sreg));
1423 * First check if dreg is assigned to dest_sreg2, since we
1424 * can't spill a dreg.
1426 if (spec [MONO_INST_DEST])
1427 val = rs->vassign [ins->dreg];
1430 if (val == dest_sreg && ins->dreg != sreg) {
1432 * the destination register is already assigned to
1433 * dest_sreg2: we need to allocate another register for it
1434 * and then copy from this to dest_sreg2.
1437 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1438 g_assert (new_dest >= 0);
1439 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1441 prev_dreg = ins->dreg;
1442 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1443 clob_dreg = ins->dreg;
1444 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1445 mono_regstate_free_int (rs, dest_sreg);
1449 if (is_global_ireg (sreg)) {
1450 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1451 insert_before_ins (bb, ins, copy);
1452 need_assign = FALSE;
1455 val = rs->vassign [sreg];
1456 if (val == dest_sreg) {
1457 /* sreg2 is already assigned to the correct register */
1459 } else if (val < -1) {
1460 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1461 } else if (val >= 0) {
1462 /* sreg2 already assigned to another register */
1464 * We couldn't emit a copy from val to dest_sreg2, because
1465 * val might be spilled later while processing this
1466 * instruction. So we spill sreg2 so it can be allocated to
1469 free_up_hreg (cfg, bb, tmp, ins, val, 0);
1474 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1478 if (rs->vassign [sreg] < -1) {
1481 /* Need to emit a spill store */
1482 spill = - rs->vassign [sreg] - 1;
1483 create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, ins, bank);
1485 /* force-set sreg2 */
1486 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1489 sregs [j] = dest_sreg;
1491 mono_inst_set_src_registers (ins, sregs);
1496 bank = dreg_bank (spec);
1497 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1498 prev_dreg = ins->dreg;
1501 if (spec_dest == 'b') {
1503 * The dest reg is read by the instruction, not written, so
1504 * avoid allocating sreg1/sreg2 to the same reg.
1506 if (dest_sregs [0] != -1)
1507 dreg_mask &= ~ (regmask (dest_sregs [0]));
1508 for (j = 1; j < num_sregs; ++j) {
1509 if (dest_sregs [j] != -1)
1510 dreg_mask &= ~ (regmask (dest_sregs [j]));
1513 val = rs->vassign [ins->dreg];
1514 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1515 /* DREG is already allocated to a register needed for sreg1 */
1516 spill_vreg (cfg, bb, tmp, ins, ins->dreg, 0);
1521 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1522 * various complex situations.
1524 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1525 guint32 dreg2, dest_dreg2;
1527 g_assert (is_soft_reg (ins->dreg, bank));
1529 if (dest_dreg != -1) {
1530 if (rs->vassign [ins->dreg] != dest_dreg)
1531 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, 0);
1533 dreg2 = ins->dreg + 1;
1534 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1535 if (dest_dreg2 != -1) {
1536 if (rs->vassign [dreg2] != dest_dreg2)
1537 free_up_hreg (cfg, bb, tmp, ins, dest_dreg2, 0);
1542 if (dreg_fixed_mask) {
1544 if (is_global_ireg (ins->dreg)) {
1546 * The argument is already in a hard reg, but that reg is
1547 * not usable by this instruction, so allocate a new one.
1549 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1551 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1552 mono_regstate_free_int (rs, val);
1558 dreg_mask &= dreg_fixed_mask;
1561 if (is_soft_reg (ins->dreg, bank)) {
1562 val = rs->vassign [ins->dreg];
1567 /* the register gets spilled after this inst */
1570 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1571 assign_reg (cfg, rs, ins->dreg, val, bank);
1573 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, NULL, bank);
1576 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1580 /* Handle regpairs */
1581 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1582 int reg2 = prev_dreg + 1;
1585 g_assert (prev_dreg > -1);
1586 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1587 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1590 mask &= ~regmask (X86_ECX);
1592 val = rs->vassign [reg2];
1596 /* the register gets spilled after this inst */
1599 val = mono_regstate_alloc_int (rs, mask);
1601 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1603 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, NULL, bank);
1606 if (! (mask & (regmask (val)))) {
1607 val = mono_regstate_alloc_int (rs, mask);
1609 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1611 /* Reallocate hreg to the correct register */
1612 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1614 mono_regstate_free_int (rs, rs->vassign [reg2]);
1618 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1619 assign_reg (cfg, rs, reg2, val, bank);
1622 ins->backend.reg3 = val;
1624 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1625 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1626 mono_regstate_free_int (rs, val);
1630 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1632 * In theory, we could free up the hreg even if the vreg is alive,
1633 * but branches inside bblocks force us to assign the same hreg
1634 * to a vreg every time it is encountered.
1636 int dreg = rs->vassign [prev_dreg];
1637 g_assert (dreg >= 0);
1638 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1639 if (G_UNLIKELY (bank))
1640 mono_regstate_free_general (rs, dreg, bank);
1642 mono_regstate_free_int (rs, dreg);
1643 rs->vassign [prev_dreg] = -1;
1646 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1647 /* this instruction only outputs to dest_dreg, need to copy */
1648 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1649 ins->dreg = dest_dreg;
1651 if (G_UNLIKELY (bank)) {
1652 /* the register we need to free up may be used in another logical regbank
1653 * so do a translate just in case.
1655 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1656 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1657 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1660 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1661 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, bank);
1665 if (spec_dest == 'b') {
1667 * The dest reg is read by the instruction, not written, so
1668 * avoid allocating sreg1/sreg2 to the same reg.
1670 for (j = 0; j < num_sregs; ++j)
1671 if (!sreg_bank (j, spec))
1672 sreg_masks [j] &= ~ (regmask (ins->dreg));
1678 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1679 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1680 free_up_hreg (cfg, bb, tmp, ins, clob_reg, 0);
1683 if (spec [MONO_INST_CLOB] == 'c') {
1684 int j, s, dreg, dreg2, cur_bank;
1687 clob_mask = MONO_ARCH_CALLEE_REGS;
1689 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1691 * Need to avoid spilling the dreg since the dreg is not really
1692 * clobbered by the call.
1694 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1695 dreg = rs->vassign [prev_dreg];
1699 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1700 dreg2 = rs->vassign [prev_dreg + 1];
1704 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1706 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1707 if ((j != dreg) && (j != dreg2))
1708 free_up_hreg (cfg, bb, tmp, ins, j, 0);
1709 else if (rs->isymbolic [j])
1710 /* The hreg is assigned to the dreg of this instruction */
1711 rs->vassign [rs->isymbolic [j]] = -1;
1712 mono_regstate_free_int (rs, j);
1717 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1718 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1719 clob_mask = regbank_callee_regs [cur_bank];
1720 if ((prev_dreg != -1) && reg_bank (spec_dest))
1721 dreg = rs->vassign [prev_dreg];
1725 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1727 /* we are looping though the banks in the outer loop
1728 * so, we don't need to deal with mirrored hregs
1729 * because we will get them in one of the other bank passes.
1731 if (is_hreg_mirrored (rs, cur_bank, j))
1735 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s) && (j != ins->sreg1)) {
1737 free_up_hreg (cfg, bb, tmp, ins, j, cur_bank);
1738 else if (rs->symbolic [cur_bank] [j])
1739 /* The hreg is assigned to the dreg of this instruction */
1740 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1741 mono_regstate_free_general (rs, j, cur_bank);
1749 * TRACK ARGUMENT REGS
1751 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1752 MonoCallInst *call = (MonoCallInst*)ins;
1756 * This needs to be done before assigning sreg1, so sreg1 will
1757 * not be assigned one of the argument regs.
1761 * Assign all registers in call->out_reg_args to the proper
1762 * argument registers.
1765 list = call->out_ireg_args;
1771 regpair = (guint32)(gssize)(list->data);
1772 hreg = regpair >> 24;
1773 reg = regpair & 0xffffff;
1775 assign_reg (cfg, rs, reg, hreg, 0);
1777 sreg_masks [0] &= ~(regmask (hreg));
1779 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1781 list = g_slist_next (list);
1785 list = call->out_freg_args;
1791 regpair = (guint32)(gssize)(list->data);
1792 hreg = regpair >> 24;
1793 reg = regpair & 0xffffff;
1795 assign_reg (cfg, rs, reg, hreg, 1);
1797 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1799 list = g_slist_next (list);
1807 bank = sreg1_bank (spec);
1808 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1809 int sreg1 = sregs [0];
1810 int dest_sreg1 = dest_sregs [0];
1812 g_assert (is_soft_reg (sreg1, bank));
1814 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1815 if (dest_sreg1 != -1)
1816 g_assert (dest_sreg1 == ins->dreg);
1817 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1818 g_assert (val >= 0);
1820 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1822 g_assert_not_reached ();
1824 assign_reg (cfg, rs, sreg1, val, bank);
1826 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1828 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1829 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1830 g_assert (val >= 0);
1832 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1834 g_assert_not_reached ();
1836 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1838 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1840 /* Skip rest of this section */
1841 dest_sregs [0] = -1;
1844 if (sreg_fixed_masks [0]) {
1846 if (is_global_ireg (sregs [0])) {
1848 * The argument is already in a hard reg, but that reg is
1849 * not usable by this instruction, so allocate a new one.
1851 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1853 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1854 mono_regstate_free_int (rs, val);
1855 dest_sregs [0] = val;
1857 /* Fall through to the dest_sreg1 != -1 case */
1860 sreg_masks [0] &= sreg_fixed_masks [0];
1863 if (dest_sregs [0] != -1) {
1864 sreg_masks [0] = regmask (dest_sregs [0]);
1866 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1867 free_up_hreg (cfg, bb, tmp, ins, dest_sregs [0], 0);
1869 if (is_global_ireg (sregs [0])) {
1870 /* The argument is already in a hard reg, need to copy */
1871 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1872 insert_before_ins (bb, ins, copy);
1873 sregs [0] = dest_sregs [0];
1877 if (is_soft_reg (sregs [0], bank)) {
1878 val = rs->vassign [sregs [0]];
1879 prev_sregs [0] = sregs [0];
1883 /* the register gets spilled after this inst */
1887 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1889 * Allocate the same hreg to sreg1 as well so the
1890 * peephole can get rid of the move.
1892 sreg_masks [0] = regmask (ins->dreg);
1895 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1896 /* Allocate the same reg to sreg1 to avoid a copy later */
1897 sreg_masks [0] = regmask (ins->dreg);
1899 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
1900 assign_reg (cfg, rs, sregs [0], val, bank);
1901 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
1905 * Need to insert before the instruction since it can
1908 create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, ins, bank);
1911 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
1912 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
1913 insert_before_ins (bb, ins, copy);
1914 for (j = 1; j < num_sregs; ++j)
1915 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
1916 val = dest_sregs [0];
1922 prev_sregs [0] = -1;
1924 mono_inst_set_src_registers (ins, sregs);
1926 for (j = 1; j < num_sregs; ++j)
1927 sreg_masks [j] &= ~(regmask (sregs [0]));
1929 /* Handle the case when sreg1 is a regpair but dreg is not */
1930 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
1931 int reg2 = prev_sregs [0] + 1;
1934 g_assert (prev_sregs [0] > -1);
1935 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
1936 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
1937 val = rs->vassign [reg2];
1941 /* the register gets spilled after this inst */
1944 val = mono_regstate_alloc_int (rs, mask);
1946 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1948 g_assert_not_reached ();
1951 if (! (mask & (regmask (val)))) {
1952 /* The vreg is already allocated to a wrong hreg */
1954 g_assert_not_reached ();
1956 val = mono_regstate_alloc_int (rs, mask);
1958 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1960 /* Reallocate hreg to the correct register */
1961 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1963 mono_regstate_free_int (rs, rs->vassign [reg2]);
1969 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
1970 assign_reg (cfg, rs, reg2, val, bank);
1973 /* Handle dreg==sreg1 */
1974 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
1975 MonoInst *sreg2_copy = NULL;
1977 int bank = reg_bank (spec_src1);
1979 if (ins->dreg == sregs [1]) {
1981 * copying sreg1 to dreg could clobber sreg2, so allocate a new
1984 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
1986 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
1987 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
1988 prev_sregs [1] = sregs [1] = reg2;
1990 if (G_UNLIKELY (bank))
1991 mono_regstate_free_general (rs, reg2, bank);
1993 mono_regstate_free_int (rs, reg2);
1996 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
1997 /* Copying sreg1_high to dreg could also clobber sreg2 */
1998 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
2000 g_assert_not_reached ();
2003 * sreg1 and dest are already allocated to the same regpair by the
2004 * SREG1 allocation code.
2006 g_assert (sregs [0] == ins->dreg);
2007 g_assert (dreg_high == sreg1_high);
2010 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
2011 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
2012 insert_before_ins (bb, ins, copy);
2015 insert_before_ins (bb, copy, sreg2_copy);
2018 * Need to prevent sreg2 to be allocated to sreg1, since that
2019 * would screw up the previous copy.
2021 sreg_masks [1] &= ~ (regmask (sregs [0]));
2022 /* we set sreg1 to dest as well */
2023 prev_sregs [0] = sregs [0] = ins->dreg;
2024 sreg_masks [1] &= ~ (regmask (ins->dreg));
2026 mono_inst_set_src_registers (ins, sregs);
2029 * TRACK SREG2, 3, ...
2031 for (j = 1; j < num_sregs; ++j) {
2034 bank = sreg_bank (j, spec);
2035 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
2036 g_assert_not_reached ();
2038 if (dest_sregs [j] != -1 && is_global_ireg (sregs [j])) {
2040 * Argument already in a global hard reg, copy it to the fixed reg, without
2041 * allocating it to the fixed reg.
2043 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [j], sregs [j], NULL, ip, 0);
2044 insert_before_ins (bb, ins, copy);
2045 sregs [j] = dest_sregs [j];
2046 } else if (is_soft_reg (sregs [j], bank)) {
2047 val = rs->vassign [sregs [j]];
2049 if (dest_sregs [j] != -1 && val >= 0 && dest_sregs [j] != val) {
2051 * The sreg is already allocated to a hreg, but not to the fixed
2052 * reg required by the instruction. Spill the sreg, so it can be
2053 * allocated to the fixed reg by the code below.
2055 /* Currently, this code should only be hit for CAS */
2056 spill_vreg (cfg, bb, tmp, ins, sregs [j], 0);
2057 val = rs->vassign [sregs [j]];
2063 /* the register gets spilled after this inst */
2066 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
2067 assign_reg (cfg, rs, sregs [j], val, bank);
2068 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
2071 * Need to insert before the instruction since it can
2074 create_spilled_store (cfg, bb, spill, val, sregs [j], tmp, NULL, ins, bank);
2078 for (k = j + 1; k < num_sregs; ++k)
2079 sreg_masks [k] &= ~ (regmask (sregs [j]));
2082 prev_sregs [j] = -1;
2085 mono_inst_set_src_registers (ins, sregs);
2088 /* Do this only for CAS for now */
2089 for (j = 1; j < num_sregs; ++j) {
2090 int sreg = sregs [j];
2091 int dest_sreg = dest_sregs [j];
2093 if (j == 2 && dest_sreg != -1) {
2096 g_assert (sreg == dest_sreg);
2098 for (k = 0; k < num_sregs; ++k) {
2100 g_assert (sregs [k] != dest_sreg);
2105 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2106 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2107 mono_regstate_free_int (rs, ins->sreg1);
2109 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2110 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2111 mono_regstate_free_int (rs, ins->sreg2);
2114 DEBUG (mono_print_ins_index (i, ins));
2117 // FIXME: Set MAX_FREGS to 8
2118 // FIXME: Optimize generated code
2119 #if MONO_ARCH_USE_FPSTACK
2121 * Make a forward pass over the code, simulating the fp stack, making sure the
2122 * arguments required by the fp opcodes are at the top of the stack.
2125 MonoInst *prev = NULL;
2129 g_assert (num_sregs <= 2);
2131 for (ins = bb->code; ins; ins = ins->next) {
2132 spec = ins_get_spec (ins->opcode);
2134 DEBUG (printf ("processing:"));
2135 DEBUG (mono_print_ins_index (0, ins));
2137 if (ins->opcode == OP_FMOVE) {
2138 /* Do it by renaming the source to the destination on the stack */
2139 // FIXME: Is this correct ?
2140 for (i = 0; i < sp; ++i)
2141 if (fpstack [i] == ins->sreg1)
2142 fpstack [i] = ins->dreg;
2147 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2148 /* Arg1 must be in %st(1) */
2152 while ((i < sp) && (fpstack [i] != ins->sreg1))
2156 if (sp - 1 - i > 0) {
2157 /* First move it to %st(0) */
2158 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2160 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2161 fxch->inst_imm = sp - 1 - i;
2167 tmp = fpstack [sp - 1];
2168 fpstack [sp - 1] = fpstack [i];
2172 /* Then move it to %st(1) */
2173 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2175 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2182 tmp = fpstack [sp - 1];
2183 fpstack [sp - 1] = fpstack [sp - 2];
2184 fpstack [sp - 2] = tmp;
2187 if (sreg2_is_fp (spec)) {
2190 if (fpstack [sp - 1] != ins->sreg2) {
2194 while ((i < sp) && (fpstack [i] != ins->sreg2))
2198 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2200 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2201 fxch->inst_imm = sp - 1 - i;
2207 tmp = fpstack [sp - 1];
2208 fpstack [sp - 1] = fpstack [i];
2215 if (sreg1_is_fp (spec)) {
2218 if (fpstack [sp - 1] != ins->sreg1) {
2222 while ((i < sp) && (fpstack [i] != ins->sreg1))
2226 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2228 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2229 fxch->inst_imm = sp - 1 - i;
2235 tmp = fpstack [sp - 1];
2236 fpstack [sp - 1] = fpstack [i];
2243 if (dreg_is_fp (spec)) {
2245 fpstack [sp ++] = ins->dreg;
2248 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2250 for (i = 0; i < sp; ++i)
2251 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2258 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2259 /* Remove remaining items from the fp stack */
2261 * These can remain for example as a result of a dead fmove like in
2262 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2265 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2266 mono_add_ins_to_end (bb, ins);
2275 mono_opcode_to_cond (int opcode)
2285 case OP_COND_EXC_EQ:
2286 case OP_COND_EXC_IEQ:
2293 case OP_COND_EXC_NE_UN:
2294 case OP_COND_EXC_INE_UN:
2295 case OP_CMOV_INE_UN:
2296 case OP_CMOV_LNE_UN:
2317 case OP_COND_EXC_LT:
2318 case OP_COND_EXC_ILT:
2329 case OP_COND_EXC_GT:
2330 case OP_COND_EXC_IGT:
2338 case OP_COND_EXC_LE_UN:
2339 case OP_COND_EXC_ILE_UN:
2340 case OP_CMOV_ILE_UN:
2341 case OP_CMOV_LLE_UN:
2346 case OP_CMOV_IGE_UN:
2347 case OP_CMOV_LGE_UN:
2356 case OP_COND_EXC_LT_UN:
2357 case OP_COND_EXC_ILT_UN:
2358 case OP_CMOV_ILT_UN:
2359 case OP_CMOV_LLT_UN:
2368 case OP_COND_EXC_GT_UN:
2369 case OP_COND_EXC_IGT_UN:
2370 case OP_CMOV_IGT_UN:
2371 case OP_CMOV_LGT_UN:
2374 printf ("%s\n", mono_inst_name (opcode));
2375 g_assert_not_reached ();
2381 mono_negate_cond (CompRelation cond)
2405 g_assert_not_reached ();
2410 mono_opcode_to_type (int opcode, int cmp_opcode)
2412 if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2414 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2416 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2418 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2420 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2422 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2424 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2426 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2428 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2429 switch (cmp_opcode) {
2431 case OP_ICOMPARE_IMM:
2432 case OP_LCOMPARE_IMM:
2438 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2443 #endif /* DISABLE_JIT */
2446 mono_is_regsize_var (MonoType *t)
2450 t = mono_type_get_underlying_type (t);
2452 case MONO_TYPE_BOOLEAN:
2453 case MONO_TYPE_CHAR:
2463 case MONO_TYPE_FNPTR:
2464 #if SIZEOF_REGISTER == 8
2469 case MONO_TYPE_OBJECT:
2470 case MONO_TYPE_STRING:
2471 case MONO_TYPE_CLASS:
2472 case MONO_TYPE_SZARRAY:
2473 case MONO_TYPE_ARRAY:
2475 case MONO_TYPE_GENERICINST:
2476 if (!mono_type_generic_inst_is_valuetype (t))
2479 case MONO_TYPE_VALUETYPE:
2488 * mono_peephole_ins:
2490 * Perform some architecture independent peephole optimizations.
2493 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2495 MonoInst *last_ins = ins->prev;
2497 switch (ins->opcode) {
2499 /* remove unnecessary multiplication with 1 */
2500 if (ins->inst_imm == 1) {
2501 if (ins->dreg != ins->sreg1)
2502 ins->opcode = OP_MOVE;
2504 MONO_DELETE_INS (bb, ins);
2507 case OP_LOAD_MEMBASE:
2508 case OP_LOADI4_MEMBASE:
2510 * Note: if reg1 = reg2 the load op is removed
2512 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2513 * OP_LOAD_MEMBASE offset(basereg), reg2
2515 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2516 * OP_MOVE reg1, reg2
2518 if (last_ins && last_ins->opcode == OP_GC_LIVENESS_DEF)
2519 last_ins = last_ins->prev;
2521 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2522 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2523 ins->inst_basereg == last_ins->inst_destbasereg &&
2524 ins->inst_offset == last_ins->inst_offset) {
2525 if (ins->dreg == last_ins->sreg1) {
2526 MONO_DELETE_INS (bb, ins);
2529 ins->opcode = OP_MOVE;
2530 ins->sreg1 = last_ins->sreg1;
2534 * Note: reg1 must be different from the basereg in the second load
2535 * Note: if reg1 = reg2 is equal then second load is removed
2537 * OP_LOAD_MEMBASE offset(basereg), reg1
2538 * OP_LOAD_MEMBASE offset(basereg), reg2
2540 * OP_LOAD_MEMBASE offset(basereg), reg1
2541 * OP_MOVE reg1, reg2
2543 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2544 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2545 ins->inst_basereg != last_ins->dreg &&
2546 ins->inst_basereg == last_ins->inst_basereg &&
2547 ins->inst_offset == last_ins->inst_offset) {
2549 if (ins->dreg == last_ins->dreg) {
2550 MONO_DELETE_INS (bb, ins);
2552 ins->opcode = OP_MOVE;
2553 ins->sreg1 = last_ins->dreg;
2556 //g_assert_not_reached ();
2560 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2561 * OP_LOAD_MEMBASE offset(basereg), reg
2563 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2564 * OP_ICONST reg, imm
2566 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2567 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2568 ins->inst_basereg == last_ins->inst_destbasereg &&
2569 ins->inst_offset == last_ins->inst_offset) {
2570 ins->opcode = OP_ICONST;
2571 ins->inst_c0 = last_ins->inst_imm;
2572 g_assert_not_reached (); // check this rule
2576 case OP_LOADI1_MEMBASE:
2577 case OP_LOADU1_MEMBASE:
2579 * Note: if reg1 = reg2 the load op is removed
2581 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2582 * OP_LOAD_MEMBASE offset(basereg), reg2
2584 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2585 * OP_MOVE reg1, reg2
2587 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2588 ins->inst_basereg == last_ins->inst_destbasereg &&
2589 ins->inst_offset == last_ins->inst_offset) {
2590 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2591 ins->sreg1 = last_ins->sreg1;
2594 case OP_LOADI2_MEMBASE:
2595 case OP_LOADU2_MEMBASE:
2597 * Note: if reg1 = reg2 the load op is removed
2599 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2600 * OP_LOAD_MEMBASE offset(basereg), reg2
2602 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2603 * OP_MOVE reg1, reg2
2605 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2606 ins->inst_basereg == last_ins->inst_destbasereg &&
2607 ins->inst_offset == last_ins->inst_offset) {
2608 #if SIZEOF_REGISTER == 8
2609 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2611 /* The definition of OP_PCONV_TO_U2 is wrong */
2612 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2614 ins->sreg1 = last_ins->sreg1;
2624 if (ins->dreg == ins->sreg1) {
2625 MONO_DELETE_INS (bb, ins);
2631 * OP_MOVE sreg, dreg
2632 * OP_MOVE dreg, sreg
2634 if (last_ins && last_ins->opcode == ins->opcode &&
2635 ins->sreg1 == last_ins->dreg &&
2636 ins->dreg == last_ins->sreg1) {
2637 MONO_DELETE_INS (bb, ins);
2641 MONO_DELETE_INS (bb, ins);
2646 #endif /* DISABLE_JIT */