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 [] = {
76 static const int regbank_load_ops [] = {
82 static const int regbank_store_ops [] = {
83 OP_STORER_MEMBASE_REG,
84 OP_STORER8_MEMBASE_REG,
88 static const int regbank_move_ops [] = {
94 #define regmask(reg) (((regmask_t)1) << (reg))
96 static const regmask_t regbank_callee_saved_regs [] = {
97 MONO_ARCH_CALLEE_SAVED_REGS,
98 MONO_ARCH_CALLEE_SAVED_FREGS,
99 MONO_ARCH_CALLEE_SAVED_XREGS,
102 static const regmask_t regbank_callee_regs [] = {
103 MONO_ARCH_CALLEE_REGS,
104 MONO_ARCH_CALLEE_FREGS,
105 MONO_ARCH_CALLEE_XREGS,
108 static const int regbank_spill_var_size[] = {
111 16 /*FIXME make this a constant. Maybe MONO_ARCH_SIMD_VECTOR_SIZE? */
114 #define DEBUG(a) MINI_DEBUG(cfg->verbose_level, 3, a;)
117 mono_regstate_assign (MonoRegState *rs)
119 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
120 /* The regalloc may fail if fp and simd logical regbanks share the same physical reg bank and
121 * if the values here are not the same.
123 g_assert(regbank_callee_regs [MONO_REG_SIMD] == regbank_callee_regs [MONO_REG_DOUBLE]);
124 g_assert(regbank_callee_saved_regs [MONO_REG_SIMD] == regbank_callee_saved_regs [MONO_REG_DOUBLE]);
125 g_assert(regbank_size [MONO_REG_SIMD] == regbank_size [MONO_REG_DOUBLE]);
128 if (rs->next_vreg > rs->vassign_size) {
129 g_free (rs->vassign);
130 rs->vassign_size = MAX (rs->next_vreg, 256);
131 rs->vassign = g_malloc (rs->vassign_size * sizeof (gint32));
134 memset (rs->isymbolic, 0, MONO_MAX_IREGS * sizeof (rs->isymbolic [0]));
135 memset (rs->fsymbolic, 0, MONO_MAX_FREGS * sizeof (rs->fsymbolic [0]));
137 rs->symbolic [0] = rs->isymbolic;
138 rs->symbolic [1] = rs->fsymbolic;
140 #ifdef MONO_ARCH_NEED_SIMD_BANK
141 memset (rs->xsymbolic, 0, MONO_MAX_XREGS * sizeof (rs->xsymbolic [0]));
142 rs->symbolic [2] = rs->xsymbolic;
147 mono_regstate_alloc_int (MonoRegState *rs, regmask_t allow)
149 regmask_t mask = allow & rs->ifree_mask;
151 #if defined(__x86_64__) && defined(__GNUC__)
158 __asm__("bsfq %1,%0\n\t"
159 : "=r" (i) : "rm" (mask));
161 rs->ifree_mask &= ~ ((regmask_t)1 << i);
167 for (i = 0; i < MONO_MAX_IREGS; ++i) {
168 if (mask & ((regmask_t)1 << i)) {
169 rs->ifree_mask &= ~ ((regmask_t)1 << i);
178 mono_regstate_free_int (MonoRegState *rs, int reg)
181 rs->ifree_mask |= (regmask_t)1 << reg;
182 rs->isymbolic [reg] = 0;
187 mono_regstate_alloc_general (MonoRegState *rs, regmask_t allow, int bank)
191 regmask_t mask = allow & rs->free_mask [bank];
192 for (i = 0; i < regbank_size [bank]; ++i) {
193 if (mask & ((regmask_t)1 << i)) {
194 rs->free_mask [bank] &= ~ ((regmask_t)1 << i);
196 mirrored_bank = get_mirrored_bank (bank);
197 if (mirrored_bank == -1)
200 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
208 mono_regstate_free_general (MonoRegState *rs, int reg, int bank)
213 rs->free_mask [bank] |= (regmask_t)1 << reg;
214 rs->symbolic [bank][reg] = 0;
216 mirrored_bank = get_mirrored_bank (bank);
217 if (mirrored_bank == -1)
219 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
220 rs->symbolic [mirrored_bank][reg] = 0;
225 mono_regname_full (int reg, int bank)
227 if (G_UNLIKELY (bank)) {
228 #if MONO_ARCH_NEED_SIMD_BANK
230 return mono_arch_xregname (reg);
232 g_assert (bank == 1);
233 return mono_arch_fregname (reg);
235 return mono_arch_regname (reg);
240 mono_call_inst_add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, int bank)
244 regpair = (((guint32)hreg) << 24) + vreg;
245 if (G_UNLIKELY (bank)) {
246 g_assert (vreg >= regbank_size [bank]);
247 g_assert (hreg < regbank_size [bank]);
248 call->used_fregs |= 1 << hreg;
249 call->out_freg_args = g_slist_append_mempool (cfg->mempool, call->out_freg_args, (gpointer)(gssize)(regpair));
251 g_assert (vreg >= MONO_MAX_IREGS);
252 g_assert (hreg < MONO_MAX_IREGS);
253 call->used_iregs |= 1 << hreg;
254 call->out_ireg_args = g_slist_append_mempool (cfg->mempool, call->out_ireg_args, (gpointer)(gssize)(regpair));
259 resize_spill_info (MonoCompile *cfg, int bank)
261 MonoSpillInfo *orig_info = cfg->spill_info [bank];
262 int orig_len = cfg->spill_info_len [bank];
263 int new_len = orig_len ? orig_len * 2 : 16;
264 MonoSpillInfo *new_info;
267 g_assert (bank < MONO_NUM_REGBANKS);
269 new_info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo) * new_len);
271 memcpy (new_info, orig_info, sizeof (MonoSpillInfo) * orig_len);
272 for (i = orig_len; i < new_len; ++i)
273 new_info [i].offset = -1;
275 cfg->spill_info [bank] = new_info;
276 cfg->spill_info_len [bank] = new_len;
280 * returns the offset used by spillvar. It allocates a new
281 * spill variable if necessary.
284 mono_spillvar_offset (MonoCompile *cfg, int spillvar, int bank)
289 if (G_UNLIKELY (spillvar >= (cfg->spill_info_len [bank]))) {
290 while (spillvar >= cfg->spill_info_len [bank])
291 resize_spill_info (cfg, bank);
295 * Allocate separate spill slots for fp/non-fp variables since most processors prefer it.
297 info = &cfg->spill_info [bank][spillvar];
298 if (info->offset == -1) {
299 cfg->stack_offset += sizeof (mgreg_t) - 1;
300 cfg->stack_offset &= ~(sizeof (mgreg_t) - 1);
302 g_assert (bank < MONO_NUM_REGBANKS);
303 if (G_UNLIKELY (bank))
304 size = regbank_spill_var_size [bank];
306 size = sizeof (mgreg_t);
308 if (cfg->flags & MONO_CFG_HAS_SPILLUP) {
309 cfg->stack_offset += size - 1;
310 cfg->stack_offset &= ~(size - 1);
311 info->offset = cfg->stack_offset;
312 cfg->stack_offset += size;
314 cfg->stack_offset += size - 1;
315 cfg->stack_offset &= ~(size - 1);
316 cfg->stack_offset += size;
317 info->offset = - cfg->stack_offset;
324 #define is_hard_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS)
325 #define is_hard_freg(r) ((r) >= 0 && (r) < MONO_MAX_FREGS)
326 #define is_global_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_SAVED_REGS & (regmask (r))))
327 #define is_local_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_REGS & (regmask (r))))
328 #define is_global_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_SAVED_FREGS & (regmask (r))))
329 #define is_local_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_FREGS & (regmask (r))))
331 #define is_hard_reg(r,bank) (G_UNLIKELY (bank) ? ((r) >= 0 && (r) < regbank_size [bank]) : ((r) < MONO_MAX_IREGS))
332 #define is_soft_reg(r,bank) (!is_hard_reg((r),(bank)))
333 #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))
334 #define is_local_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_regs [bank] & regmask (r))) : is_local_ireg (r))
335 #define reg_is_freeable(r,bank) (G_UNLIKELY (bank) ? is_local_reg ((r), (bank)) : is_local_ireg ((r)))
337 #ifndef MONO_ARCH_INST_IS_FLOAT
338 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc) == 'f')
341 #define reg_is_fp(desc) (MONO_ARCH_INST_IS_FLOAT (desc))
342 #define dreg_is_fp(spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_DEST]))
343 #define sreg_is_fp(n,spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_SRC1+(n)]))
344 #define sreg1_is_fp(spec) sreg_is_fp (0,(spec))
345 #define sreg2_is_fp(spec) sreg_is_fp (1,(spec))
347 #define reg_is_simd(desc) ((desc) == 'x')
349 #ifdef MONO_ARCH_NEED_SIMD_BANK
351 #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)
355 #define reg_bank(desc) reg_is_fp ((desc))
359 #define sreg_bank(n,spec) reg_bank ((spec)[MONO_INST_SRC1+(n)])
360 #define sreg1_bank(spec) sreg_bank (0, (spec))
361 #define sreg2_bank(spec) sreg_bank (1, (spec))
362 #define dreg_bank(spec) reg_bank ((spec)[MONO_INST_DEST])
364 #define sreg_bank_ins(n,ins) sreg_bank ((n), ins_get_spec ((ins)->opcode))
365 #define sreg1_bank_ins(ins) sreg_bank_ins (0, (ins))
366 #define sreg2_bank_ins(ins) sreg_bank_ins (1, (ins))
367 #define dreg_bank_ins(ins) dreg_bank (ins_get_spec ((ins)->opcode))
369 #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)
371 #ifdef MONO_ARCH_IS_GLOBAL_IREG
372 #undef is_global_ireg
373 #define is_global_ireg(reg) MONO_ARCH_IS_GLOBAL_IREG ((reg))
382 regmask_t preferred_mask; /* the hreg where the register should be allocated, or 0 */
385 #ifndef DISABLE_LOGGING
387 mono_print_ins_index (int i, MonoInst *ins)
389 const char *spec = ins_get_spec (ins->opcode);
391 int sregs [MONO_MAX_SRC_REGS];
394 printf ("\t%-2d %s", i, mono_inst_name (ins->opcode));
396 printf (" %s", mono_inst_name (ins->opcode));
397 if (spec == MONO_ARCH_CPU_SPEC) {
398 /* This is a lowered opcode */
400 printf (" R%d <-", ins->dreg);
401 if (ins->sreg1 != -1)
402 printf (" R%d", ins->sreg1);
403 if (ins->sreg2 != -1)
404 printf (" R%d", ins->sreg2);
405 if (ins->sreg3 != -1)
406 printf (" R%d", ins->sreg3);
408 switch (ins->opcode) {
419 if (!ins->inst_false_bb)
420 printf (" [B%d]", ins->inst_true_bb->block_num);
422 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
429 printf (" [%d (", (int)ins->inst_c0);
430 for (i = 0; i < ins->inst_phi_args [0]; i++) {
433 printf ("R%d", ins->inst_phi_args [i + 1]);
439 case OP_OUTARG_VTRETADDR:
440 printf (" R%d", ((MonoInst*)ins->inst_p0)->dreg);
443 printf (" + 0x%lx", (long)ins->inst_offset);
450 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
454 if (spec [MONO_INST_DEST]) {
455 int bank = dreg_bank (spec);
456 if (is_soft_reg (ins->dreg, bank)) {
457 if (spec [MONO_INST_DEST] == 'b') {
458 if (ins->inst_offset == 0)
459 printf (" [R%d] <-", ins->dreg);
461 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
464 printf (" R%d <-", ins->dreg);
465 } else if (spec [MONO_INST_DEST] == 'b') {
466 if (ins->inst_offset == 0)
467 printf (" [%s] <-", mono_arch_regname (ins->dreg));
469 printf (" [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
471 printf (" %s <-", mono_regname_full (ins->dreg, bank));
473 if (spec [MONO_INST_SRC1]) {
474 int bank = sreg1_bank (spec);
475 if (is_soft_reg (ins->sreg1, bank)) {
476 if (spec [MONO_INST_SRC1] == 'b')
477 printf (" [R%d + 0x%lx]", ins->sreg1, (long)ins->inst_offset);
479 printf (" R%d", ins->sreg1);
480 } else if (spec [MONO_INST_SRC1] == 'b')
481 printf (" [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
483 printf (" %s", mono_regname_full (ins->sreg1, bank));
485 num_sregs = mono_inst_get_src_registers (ins, sregs);
486 for (j = 1; j < num_sregs; ++j) {
487 int bank = sreg_bank (j, spec);
488 if (is_soft_reg (sregs [j], bank))
489 printf (" R%d", sregs [j]);
491 printf (" %s", mono_regname_full (sregs [j], bank));
494 switch (ins->opcode) {
496 printf (" [%d]", (int)ins->inst_c0);
498 #if defined(TARGET_X86) || defined(TARGET_AMD64)
499 case OP_X86_PUSH_IMM:
501 case OP_ICOMPARE_IMM:
508 printf (" [%d]", (int)ins->inst_imm);
512 printf (" [%d]", (int)(gssize)ins->inst_p1);
515 printf (" [%lld]", (long long)ins->inst_l);
518 printf (" [%f]", *(double*)ins->inst_p0);
521 printf (" [%f]", *(float*)ins->inst_p0);
524 case OP_CALL_MEMBASE:
533 case OP_VCALL_MEMBASE:
536 case OP_VCALL2_MEMBASE:
538 case OP_VOIDCALL_MEMBASE:
539 case OP_VOIDCALLVIRT: {
540 MonoCallInst *call = (MonoCallInst*)ins;
543 if (ins->opcode == OP_VCALL || ins->opcode == OP_VCALL_REG || ins->opcode == OP_VCALL_MEMBASE) {
545 * These are lowered opcodes, but they are in the .md files since the old
546 * JIT passes them to backends.
549 printf (" R%d <-", ins->dreg);
553 char *full_name = mono_method_full_name (call->method, TRUE);
554 printf (" [%s]", full_name);
556 } else if (call->fptr) {
557 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
559 printf (" [%s]", info->name);
562 list = call->out_ireg_args;
567 regpair = (guint32)(gssize)(list->data);
568 hreg = regpair >> 24;
569 reg = regpair & 0xffffff;
571 printf (" [%s <- R%d]", mono_arch_regname (hreg), reg);
573 list = g_slist_next (list);
578 case OP_CALL_HANDLER:
579 printf (" [B%d]", ins->inst_target_bb->block_num);
601 if (!ins->inst_false_bb)
602 printf (" [B%d]", ins->inst_true_bb->block_num);
604 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
606 case OP_LIVERANGE_START:
607 case OP_LIVERANGE_END:
608 case OP_GC_LIVENESS_DEF:
609 case OP_GC_LIVENESS_USE:
610 printf (" R%d", (int)ins->inst_c1);
616 if (spec [MONO_INST_CLOB])
617 printf (" clobbers: %c", spec [MONO_INST_CLOB]);
622 print_regtrack (RegTrack *t, int num)
628 for (i = 0; i < num; ++i) {
631 if (i >= MONO_MAX_IREGS) {
632 g_snprintf (buf, sizeof(buf), "R%d", i);
635 r = mono_arch_regname (i);
636 printf ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].killed_in);
641 mono_print_ins_index (int i, MonoInst *ins)
644 #endif /* DISABLE_LOGGING */
647 mono_print_ins (MonoInst *ins)
649 mono_print_ins_index (-1, ins);
653 insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst* to_insert)
656 * If this function is called multiple times, the new instructions are inserted
657 * in the proper order.
659 mono_bblock_insert_before_ins (bb, ins, to_insert);
663 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst **last, MonoInst* to_insert)
666 * If this function is called multiple times, the new instructions are inserted in
669 mono_bblock_insert_after_ins (bb, *last, to_insert);
675 * Force the spilling of the variable in the symbolic register 'reg', and free
676 * the hreg it was assigned to.
679 spill_vreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int reg, int bank)
684 MonoRegState *rs = cfg->rs;
686 symbolic = rs->symbolic [bank];
687 sel = rs->vassign [reg];
689 /* the vreg we need to spill lives in another logical reg bank */
690 bank = translate_bank (cfg->rs, bank, sel);
692 /*i = rs->isymbolic [sel];
693 g_assert (i == reg);*/
695 spill = ++cfg->spill_count;
696 rs->vassign [i] = -spill - 1;
697 if (G_UNLIKELY (bank))
698 mono_regstate_free_general (rs, sel, bank);
700 mono_regstate_free_int (rs, sel);
701 /* we need to create a spill var and insert a load to sel after the current instruction */
702 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
704 load->inst_basereg = cfg->frame_reg;
705 load->inst_offset = mono_spillvar_offset (cfg, spill, bank);
706 insert_after_ins (bb, ins, last, load);
707 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)));
708 if (G_UNLIKELY (bank))
709 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
711 i = mono_regstate_alloc_int (rs, regmask (sel));
714 if (G_UNLIKELY (bank))
715 mono_regstate_free_general (rs, sel, bank);
717 mono_regstate_free_int (rs, sel);
720 /* This isn't defined on older glib versions and on some platforms */
721 #ifndef G_GUINT64_FORMAT
722 #define G_GUINT64_FORMAT "ul"
726 get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t regmask, int reg, int bank)
729 int i, sel, spill, num_sregs;
730 int sregs [MONO_MAX_SRC_REGS];
732 MonoRegState *rs = cfg->rs;
734 symbolic = rs->symbolic [bank];
736 g_assert (bank < MONO_NUM_REGBANKS);
738 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));
739 /* exclude the registers in the current instruction */
740 num_sregs = mono_inst_get_src_registers (ins, sregs);
741 for (i = 0; i < num_sregs; ++i) {
742 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))) {
743 if (is_soft_reg (sregs [i], bank))
744 regmask &= ~ (regmask (rs->vassign [sregs [i]]));
746 regmask &= ~ (regmask (sregs [i]));
747 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i + 1, mono_regname_full (sregs [i], bank), sregs [i]));
750 if ((dreg_bank_ins (ins) == bank) && (reg != ins->dreg) && reg_is_freeable (ins->dreg, bank)) {
751 regmask &= ~ (regmask (ins->dreg));
752 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins->dreg, bank)));
755 DEBUG (printf ("\t\tavailable regmask: 0x%08" G_GUINT64_FORMAT "\n", (guint64)regmask));
756 g_assert (regmask); /* need at least a register we can free */
758 /* we should track prev_use and spill the register that's farther */
759 if (G_UNLIKELY (bank)) {
760 for (i = 0; i < regbank_size [bank]; ++i) {
761 if (regmask & (regmask (i))) {
764 /* the vreg we need to load lives in another logical bank */
765 bank = translate_bank (cfg->rs, bank, sel);
767 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
772 i = rs->symbolic [bank] [sel];
773 spill = ++cfg->spill_count;
774 rs->vassign [i] = -spill - 1;
775 mono_regstate_free_general (rs, sel, bank);
778 for (i = 0; i < MONO_MAX_IREGS; ++i) {
779 if (regmask & (regmask (i))) {
781 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), rs->isymbolic [sel]));
786 i = rs->isymbolic [sel];
787 spill = ++cfg->spill_count;
788 rs->vassign [i] = -spill - 1;
789 mono_regstate_free_int (rs, sel);
792 /* we need to create a spill var and insert a load to sel after the current instruction */
793 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
795 load->inst_basereg = cfg->frame_reg;
796 load->inst_offset = mono_spillvar_offset (cfg, spill, bank);
797 insert_after_ins (bb, ins, last, load);
798 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)));
799 if (G_UNLIKELY (bank))
800 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
802 i = mono_regstate_alloc_int (rs, regmask (sel));
811 * Free up the hreg HREG by spilling the vreg allocated to it.
814 free_up_hreg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int hreg, int bank)
816 if (G_UNLIKELY (bank)) {
817 if (!(cfg->rs->free_mask [1] & (regmask (hreg)))) {
818 bank = translate_bank (cfg->rs, bank, hreg);
819 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
820 spill_vreg (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
824 if (!(cfg->rs->ifree_mask & (regmask (hreg)))) {
825 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->isymbolic [hreg]));
826 spill_vreg (cfg, bb, last, ins, cfg->rs->isymbolic [hreg], bank);
832 create_copy_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, int dest, int src, MonoInst *ins, const unsigned char *ip, int bank)
836 MONO_INST_NEW (cfg, copy, regbank_move_ops [bank]);
842 mono_bblock_insert_after_ins (bb, ins, copy);
845 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src, bank), mono_regname_full (dest, bank)));
850 create_spilled_store (MonoCompile *cfg, MonoBasicBlock *bb, int spill, int reg, int prev_reg, MonoInst **last, MonoInst *ins, int bank)
853 MONO_INST_NEW (cfg, store, regbank_store_ops [bank]);
855 store->inst_destbasereg = cfg->frame_reg;
856 store->inst_offset = mono_spillvar_offset (cfg, spill, bank);
858 mono_bblock_insert_after_ins (bb, ins, store);
861 DEBUG (printf ("\tSPILLED STORE (%d at 0x%08lx(%%ebp)) R%d (from %s)\n", spill, (long)store->inst_offset, prev_reg, mono_regname_full (reg, bank)));
865 /* flags used in reginfo->flags */
867 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
868 MONO_FP_NEEDS_SPILL = regmask (1),
869 MONO_FP_NEEDS_LOAD = regmask (2)
873 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
877 if (info && info->preferred_mask) {
878 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
880 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
885 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
887 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
893 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
897 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
900 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
906 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
908 if (G_UNLIKELY (bank))
909 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
911 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
915 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
917 if (G_UNLIKELY (bank)) {
920 g_assert (reg >= regbank_size [bank]);
921 g_assert (hreg < regbank_size [bank]);
922 g_assert (! is_global_freg (hreg));
924 rs->vassign [reg] = hreg;
925 rs->symbolic [bank] [hreg] = reg;
926 rs->free_mask [bank] &= ~ (regmask (hreg));
928 mirrored_bank = get_mirrored_bank (bank);
929 if (mirrored_bank == -1)
932 /* Make sure the other logical reg bank that this bank shares
933 * a single hard reg bank knows that this hard reg is not free.
935 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
937 /* Mark the other logical bank that the this bank shares
938 * a single hard reg bank with as mirrored.
940 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
944 g_assert (reg >= MONO_MAX_IREGS);
945 g_assert (hreg < MONO_MAX_IREGS);
947 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
948 g_assert (! is_global_ireg (hreg));
951 rs->vassign [reg] = hreg;
952 rs->isymbolic [hreg] = reg;
953 rs->ifree_mask &= ~ (regmask (hreg));
957 static inline regmask_t
958 get_callee_mask (const char spec)
960 if (G_UNLIKELY (reg_bank (spec)))
961 return regbank_callee_regs [reg_bank (spec)];
962 return MONO_ARCH_CALLEE_REGS;
965 static gint8 desc_to_fixed_reg [256];
966 static gboolean desc_to_fixed_reg_inited = FALSE;
971 * Local register allocation.
972 * We first scan the list of instructions and we save the liveness info of
973 * each register (when the register is first used, when it's value is set etc.).
974 * We also reverse the list of instructions because assigning registers backwards allows
975 * for more tricks to be used.
978 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
980 MonoInst *ins, *prev, *last;
982 MonoRegState *rs = cfg->rs;
986 unsigned char spec_src1, spec_dest;
988 #if MONO_ARCH_USE_FPSTACK
989 gboolean has_fp = FALSE;
994 int sregs [MONO_MAX_SRC_REGS];
999 if (!desc_to_fixed_reg_inited) {
1000 for (i = 0; i < 256; ++i)
1001 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1002 desc_to_fixed_reg_inited = TRUE;
1004 /* Validate the cpu description against the info in mini-ops.h */
1005 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM)
1006 for (i = OP_LOAD; i < OP_LAST; ++i) {
1009 spec = ins_get_spec (i);
1010 ispec = INS_INFO (i);
1012 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1013 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1014 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1015 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1016 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1017 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1022 rs->next_vreg = bb->max_vreg;
1023 mono_regstate_assign (rs);
1025 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1026 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1027 rs->free_mask [i] = regbank_callee_regs [i];
1029 max = rs->next_vreg;
1031 if (cfg->reginfo && cfg->reginfo_len < max)
1032 cfg->reginfo = NULL;
1034 reginfo = cfg->reginfo;
1036 cfg->reginfo_len = MAX (1024, max * 2);
1037 reginfo = cfg->reginfo = mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1040 g_assert (cfg->reginfo_len >= rs->next_vreg);
1042 if (cfg->verbose_level > 1) {
1043 /* print_regtrack reads the info of all variables */
1044 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1048 * For large methods, next_vreg can be very large, so g_malloc0 time can
1049 * be prohibitive. So we manually init the reginfo entries used by the
1052 for (ins = bb->code; ins; ins = ins->next) {
1053 spec = ins_get_spec (ins->opcode);
1055 if ((ins->dreg != -1) && (ins->dreg < max)) {
1056 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1057 #if SIZEOF_REGISTER == 4
1058 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1060 * In the new IR, the two vregs of the regpair do not alias the
1061 * original long vreg. shift the vreg here so the rest of the
1062 * allocator doesn't have to care about it.
1065 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1070 num_sregs = mono_inst_get_src_registers (ins, sregs);
1071 for (j = 0; j < num_sregs; ++j) {
1072 g_assert (sregs [j] != -1);
1073 if (sregs [j] < max) {
1074 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1075 #if SIZEOF_REGISTER == 4
1076 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1078 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1083 mono_inst_set_src_registers (ins, sregs);
1086 /*if (cfg->opt & MONO_OPT_COPYPROP)
1087 local_copy_prop (cfg, ins);*/
1090 DEBUG (printf ("\nLOCAL REGALLOC: BASIC BLOCK %d:\n", bb->block_num));
1091 /* forward pass on the instructions to collect register liveness info */
1092 MONO_BB_FOR_EACH_INS (bb, ins) {
1093 spec = ins_get_spec (ins->opcode);
1094 spec_dest = spec [MONO_INST_DEST];
1096 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1097 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1100 DEBUG (mono_print_ins_index (i, ins));
1102 num_sregs = mono_inst_get_src_registers (ins, sregs);
1104 #if MONO_ARCH_USE_FPSTACK
1105 if (dreg_is_fp (spec)) {
1108 for (j = 0; j < num_sregs; ++j) {
1109 if (sreg_is_fp (j, spec))
1115 for (j = 0; j < num_sregs; ++j) {
1116 int sreg = sregs [j];
1117 int sreg_spec = spec [MONO_INST_SRC1 + j];
1119 bank = sreg_bank (j, spec);
1120 g_assert (sreg != -1);
1121 if (is_soft_reg (sreg, bank))
1122 /* This means the vreg is not local to this bb */
1123 g_assert (reginfo [sreg].born_in > 0);
1124 rs->vassign [sreg] = -1;
1125 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1126 //reginfo [ins->sreg2].last_use = i;
1127 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1128 /* The virtual register is allocated sequentially */
1129 rs->vassign [sreg + 1] = -1;
1130 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1131 //reginfo [ins->sreg2 + 1].last_use = i;
1132 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1133 reginfo [sreg + 1].born_in = i;
1139 mono_inst_set_src_registers (ins, sregs);
1144 bank = dreg_bank (spec);
1145 if (spec_dest != 'b') /* it's not just a base register */
1146 reginfo [ins->dreg].killed_in = i;
1147 g_assert (ins->dreg != -1);
1148 rs->vassign [ins->dreg] = -1;
1149 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1150 //reginfo [ins->dreg].last_use = i;
1151 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1152 reginfo [ins->dreg].born_in = i;
1154 dest_dreg = desc_to_fixed_reg [spec_dest];
1155 if (dest_dreg != -1)
1156 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1158 #ifdef MONO_ARCH_INST_FIXED_MASK
1159 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1162 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1163 /* The virtual register is allocated sequentially */
1164 rs->vassign [ins->dreg + 1] = -1;
1165 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1166 //reginfo [ins->dreg + 1].last_use = i;
1167 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1168 reginfo [ins->dreg + 1].born_in = i;
1169 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1170 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1176 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1177 /* A call instruction implicitly uses all registers in call->out_ireg_args */
1179 MonoCallInst *call = (MonoCallInst*)ins;
1182 list = call->out_ireg_args;
1188 regpair = (guint32)(gssize)(list->data);
1189 hreg = regpair >> 24;
1190 reg = regpair & 0xffffff;
1192 //reginfo [reg].prev_use = reginfo [reg].last_use;
1193 //reginfo [reg].last_use = i;
1195 list = g_slist_next (list);
1199 list = call->out_freg_args;
1205 regpair = (guint32)(gssize)(list->data);
1206 hreg = regpair >> 24;
1207 reg = regpair & 0xffffff;
1209 list = g_slist_next (list);
1219 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1220 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1221 int prev_dreg, clob_dreg;
1222 int dest_dreg, clob_reg;
1223 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1224 int dreg_high, sreg1_high;
1225 regmask_t dreg_mask, mask;
1226 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1227 regmask_t dreg_fixed_mask;
1228 const unsigned char *ip;
1230 spec = ins_get_spec (ins->opcode);
1231 spec_src1 = spec [MONO_INST_SRC1];
1232 spec_dest = spec [MONO_INST_DEST];
1239 dreg_mask = get_callee_mask (spec_dest);
1240 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1241 prev_sregs [j] = -1;
1242 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1243 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1244 #ifdef MONO_ARCH_INST_FIXED_MASK
1245 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1247 sreg_fixed_masks [j] = 0;
1251 DEBUG (printf ("processing:"));
1252 DEBUG (mono_print_ins_index (i, ins));
1261 dest_dreg = desc_to_fixed_reg [spec_dest];
1262 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1263 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1265 #ifdef MONO_ARCH_INST_FIXED_MASK
1266 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1268 dreg_fixed_mask = 0;
1271 num_sregs = mono_inst_get_src_registers (ins, sregs);
1274 * TRACK FIXED SREG2, 3, ...
1276 for (j = 1; j < num_sregs; ++j) {
1277 int sreg = sregs [j];
1278 int dest_sreg = dest_sregs [j];
1280 if (dest_sreg == -1)
1288 * We need to special case this, since on x86, there are only 3
1289 * free registers, and the code below assigns one of them to
1290 * sreg, so we can run out of registers when trying to assign
1291 * dreg. Instead, we just set up the register masks, and let the
1292 * normal sreg2 assignment code handle this. It would be nice to
1293 * do this for all the fixed reg cases too, but there is too much
1297 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1298 sreg_masks [j] = regmask (dest_sreg);
1299 for (k = 0; k < num_sregs; ++k) {
1301 sreg_masks [k] &= ~ (regmask (dest_sreg));
1305 * Spill sreg1/2 if they are assigned to dest_sreg.
1307 for (k = 0; k < num_sregs; ++k) {
1308 if (k != j && is_soft_reg (sregs [k], 0) && rs->vassign [sregs [k]] == dest_sreg)
1309 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1313 * We can also run out of registers while processing sreg2 if sreg3 is
1314 * assigned to another hreg, so spill sreg3 now.
1316 if (is_soft_reg (sreg, 0) && rs->vassign [sreg] >= 0 && rs->vassign [sreg] != dest_sreg) {
1317 spill_vreg (cfg, bb, tmp, ins, sreg, 0);
1322 if (rs->ifree_mask & (regmask (dest_sreg))) {
1323 if (is_global_ireg (sreg)) {
1325 /* Argument already in hard reg, need to copy */
1326 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1327 insert_before_ins (bb, ins, copy);
1328 for (k = 0; k < num_sregs; ++k) {
1330 sreg_masks [k] &= ~ (regmask (dest_sreg));
1333 val = rs->vassign [sreg];
1335 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1336 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1337 } else if (val < -1) {
1339 g_assert_not_reached ();
1341 /* Argument already in hard reg, need to copy */
1342 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1345 insert_before_ins (bb, ins, copy);
1346 for (k = 0; k < num_sregs; ++k) {
1348 sreg_masks [k] &= ~ (regmask (dest_sreg));
1351 * Prevent the dreg from being allocate to dest_sreg
1352 * too, since it could force sreg1 to be allocated to
1353 * the same reg on x86.
1355 dreg_mask &= ~ (regmask (dest_sreg));
1359 gboolean need_spill = TRUE;
1360 gboolean need_assign = TRUE;
1363 dreg_mask &= ~ (regmask (dest_sreg));
1364 for (k = 0; k < num_sregs; ++k) {
1366 sreg_masks [k] &= ~ (regmask (dest_sreg));
1370 * First check if dreg is assigned to dest_sreg2, since we
1371 * can't spill a dreg.
1373 if (spec [MONO_INST_DEST])
1374 val = rs->vassign [ins->dreg];
1377 if (val == dest_sreg && ins->dreg != sreg) {
1379 * the destination register is already assigned to
1380 * dest_sreg2: we need to allocate another register for it
1381 * and then copy from this to dest_sreg2.
1384 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1385 g_assert (new_dest >= 0);
1386 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1388 prev_dreg = ins->dreg;
1389 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1390 clob_dreg = ins->dreg;
1391 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1392 mono_regstate_free_int (rs, dest_sreg);
1396 if (is_global_ireg (sreg)) {
1397 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1398 insert_before_ins (bb, ins, copy);
1399 need_assign = FALSE;
1402 val = rs->vassign [sreg];
1403 if (val == dest_sreg) {
1404 /* sreg2 is already assigned to the correct register */
1406 } else if (val < -1) {
1407 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1408 } else if (val >= 0) {
1409 /* sreg2 already assigned to another register */
1411 * We couldn't emit a copy from val to dest_sreg2, because
1412 * val might be spilled later while processing this
1413 * instruction. So we spill sreg2 so it can be allocated to
1416 free_up_hreg (cfg, bb, tmp, ins, val, 0);
1421 free_up_hreg (cfg, bb, tmp, ins, dest_sreg, 0);
1425 if (rs->vassign [sreg] < -1) {
1429 /* Need to emit a spill store */
1430 spill = - rs->vassign [sreg] - 1;
1431 store = create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, bank);
1432 insert_before_ins (bb, ins, store);
1434 /* force-set sreg2 */
1435 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1438 sregs [j] = dest_sreg;
1440 mono_inst_set_src_registers (ins, sregs);
1445 bank = dreg_bank (spec);
1446 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1447 prev_dreg = ins->dreg;
1450 if (spec_dest == 'b') {
1452 * The dest reg is read by the instruction, not written, so
1453 * avoid allocating sreg1/sreg2 to the same reg.
1455 if (dest_sregs [0] != -1)
1456 dreg_mask &= ~ (regmask (dest_sregs [0]));
1457 for (j = 1; j < num_sregs; ++j) {
1458 if (dest_sregs [j] != -1)
1459 dreg_mask &= ~ (regmask (dest_sregs [j]));
1462 val = rs->vassign [ins->dreg];
1463 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1464 /* DREG is already allocated to a register needed for sreg1 */
1465 spill_vreg (cfg, bb, tmp, ins, ins->dreg, 0);
1470 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1471 * various complex situations.
1473 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1474 guint32 dreg2, dest_dreg2;
1476 g_assert (is_soft_reg (ins->dreg, bank));
1478 if (dest_dreg != -1) {
1479 if (rs->vassign [ins->dreg] != dest_dreg)
1480 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, 0);
1482 dreg2 = ins->dreg + 1;
1483 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1484 if (dest_dreg2 != -1) {
1485 if (rs->vassign [dreg2] != dest_dreg2)
1486 free_up_hreg (cfg, bb, tmp, ins, dest_dreg2, 0);
1491 if (dreg_fixed_mask) {
1493 if (is_global_ireg (ins->dreg)) {
1495 * The argument is already in a hard reg, but that reg is
1496 * not usable by this instruction, so allocate a new one.
1498 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1500 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1501 mono_regstate_free_int (rs, val);
1507 dreg_mask &= dreg_fixed_mask;
1510 if (is_soft_reg (ins->dreg, bank)) {
1511 val = rs->vassign [ins->dreg];
1516 /* the register gets spilled after this inst */
1519 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1520 assign_reg (cfg, rs, ins->dreg, val, bank);
1522 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, bank);
1525 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1529 /* Handle regpairs */
1530 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1531 int reg2 = prev_dreg + 1;
1534 g_assert (prev_dreg > -1);
1535 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1536 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1539 mask &= ~regmask (X86_ECX);
1541 val = rs->vassign [reg2];
1545 /* the register gets spilled after this inst */
1548 val = mono_regstate_alloc_int (rs, mask);
1550 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1552 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, bank);
1555 if (! (mask & (regmask (val)))) {
1556 val = mono_regstate_alloc_int (rs, mask);
1558 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1560 /* Reallocate hreg to the correct register */
1561 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1563 mono_regstate_free_int (rs, rs->vassign [reg2]);
1567 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1568 assign_reg (cfg, rs, reg2, val, bank);
1571 ins->backend.reg3 = val;
1573 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1574 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1575 mono_regstate_free_int (rs, val);
1579 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1581 * In theory, we could free up the hreg even if the vreg is alive,
1582 * but branches inside bblocks force us to assign the same hreg
1583 * to a vreg every time it is encountered.
1585 int dreg = rs->vassign [prev_dreg];
1586 g_assert (dreg >= 0);
1587 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1588 if (G_UNLIKELY (bank))
1589 mono_regstate_free_general (rs, dreg, bank);
1591 mono_regstate_free_int (rs, dreg);
1592 rs->vassign [prev_dreg] = -1;
1595 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1596 /* this instruction only outputs to dest_dreg, need to copy */
1597 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1598 ins->dreg = dest_dreg;
1600 if (G_UNLIKELY (bank)) {
1601 /* the register we need to free up may be used in another logical regbank
1602 * so do a translate just in case.
1604 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1605 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1606 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1609 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1610 free_up_hreg (cfg, bb, tmp, ins, dest_dreg, bank);
1614 if (spec_dest == 'b') {
1616 * The dest reg is read by the instruction, not written, so
1617 * avoid allocating sreg1/sreg2 to the same reg.
1619 for (j = 0; j < num_sregs; ++j)
1620 if (!sreg_bank (j, spec))
1621 sreg_masks [j] &= ~ (regmask (ins->dreg));
1627 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1628 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1629 free_up_hreg (cfg, bb, tmp, ins, clob_reg, 0);
1632 if (spec [MONO_INST_CLOB] == 'c') {
1633 int j, s, dreg, dreg2, cur_bank;
1636 clob_mask = MONO_ARCH_CALLEE_REGS;
1638 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1640 * Need to avoid spilling the dreg since the dreg is not really
1641 * clobbered by the call.
1643 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1644 dreg = rs->vassign [prev_dreg];
1648 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1649 dreg2 = rs->vassign [prev_dreg + 1];
1653 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1655 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1656 if ((j != dreg) && (j != dreg2))
1657 free_up_hreg (cfg, bb, tmp, ins, j, 0);
1658 else if (rs->isymbolic [j])
1659 /* The hreg is assigned to the dreg of this instruction */
1660 rs->vassign [rs->isymbolic [j]] = -1;
1661 mono_regstate_free_int (rs, j);
1666 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1667 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1668 clob_mask = regbank_callee_regs [cur_bank];
1669 if ((prev_dreg != -1) && reg_bank (spec_dest))
1670 dreg = rs->vassign [prev_dreg];
1674 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1676 /* we are looping though the banks in the outer loop
1677 * so, we don't need to deal with mirrored hregs
1678 * because we will get them in one of the other bank passes.
1680 if (is_hreg_mirrored (rs, cur_bank, j))
1684 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s) && (j != ins->sreg1)) {
1686 free_up_hreg (cfg, bb, tmp, ins, j, cur_bank);
1687 else if (rs->symbolic [cur_bank] [j])
1688 /* The hreg is assigned to the dreg of this instruction */
1689 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1690 mono_regstate_free_general (rs, j, cur_bank);
1698 * TRACK ARGUMENT REGS
1700 if (spec [MONO_INST_CLOB] == 'c' && MONO_IS_CALL (ins)) {
1701 MonoCallInst *call = (MonoCallInst*)ins;
1705 * This needs to be done before assigning sreg1, so sreg1 will
1706 * not be assigned one of the argument regs.
1710 * Assign all registers in call->out_reg_args to the proper
1711 * argument registers.
1714 list = call->out_ireg_args;
1720 regpair = (guint32)(gssize)(list->data);
1721 hreg = regpair >> 24;
1722 reg = regpair & 0xffffff;
1724 assign_reg (cfg, rs, reg, hreg, 0);
1726 sreg_masks [0] &= ~(regmask (hreg));
1728 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1730 list = g_slist_next (list);
1734 list = call->out_freg_args;
1740 regpair = (guint32)(gssize)(list->data);
1741 hreg = regpair >> 24;
1742 reg = regpair & 0xffffff;
1744 assign_reg (cfg, rs, reg, hreg, 1);
1746 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1748 list = g_slist_next (list);
1756 bank = sreg1_bank (spec);
1757 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1758 int sreg1 = sregs [0];
1759 int dest_sreg1 = dest_sregs [0];
1761 g_assert (is_soft_reg (sreg1, bank));
1763 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1764 if (dest_sreg1 != -1)
1765 g_assert (dest_sreg1 == ins->dreg);
1766 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1767 g_assert (val >= 0);
1769 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1771 g_assert_not_reached ();
1773 assign_reg (cfg, rs, sreg1, val, bank);
1775 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1777 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1778 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1779 g_assert (val >= 0);
1781 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1783 g_assert_not_reached ();
1785 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1787 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1789 /* Skip rest of this section */
1790 dest_sregs [0] = -1;
1793 if (sreg_fixed_masks [0]) {
1795 if (is_global_ireg (sregs [0])) {
1797 * The argument is already in a hard reg, but that reg is
1798 * not usable by this instruction, so allocate a new one.
1800 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1802 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1803 mono_regstate_free_int (rs, val);
1804 dest_sregs [0] = val;
1806 /* Fall through to the dest_sreg1 != -1 case */
1809 sreg_masks [0] &= sreg_fixed_masks [0];
1812 if (dest_sregs [0] != -1) {
1813 sreg_masks [0] = regmask (dest_sregs [0]);
1815 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1816 free_up_hreg (cfg, bb, tmp, ins, dest_sregs [0], 0);
1818 if (is_global_ireg (sregs [0])) {
1819 /* The argument is already in a hard reg, need to copy */
1820 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1821 insert_before_ins (bb, ins, copy);
1822 sregs [0] = dest_sregs [0];
1826 if (is_soft_reg (sregs [0], bank)) {
1827 val = rs->vassign [sregs [0]];
1828 prev_sregs [0] = sregs [0];
1832 /* the register gets spilled after this inst */
1836 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1838 * Allocate the same hreg to sreg1 as well so the
1839 * peephole can get rid of the move.
1841 sreg_masks [0] = regmask (ins->dreg);
1844 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1845 /* Allocate the same reg to sreg1 to avoid a copy later */
1846 sreg_masks [0] = regmask (ins->dreg);
1848 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
1849 assign_reg (cfg, rs, sregs [0], val, bank);
1850 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
1853 MonoInst *store = create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, bank);
1855 * Need to insert before the instruction since it can
1858 insert_before_ins (bb, ins, store);
1861 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
1862 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
1863 insert_before_ins (bb, ins, copy);
1864 for (j = 1; j < num_sregs; ++j)
1865 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
1866 val = dest_sregs [0];
1872 prev_sregs [0] = -1;
1874 mono_inst_set_src_registers (ins, sregs);
1876 for (j = 1; j < num_sregs; ++j)
1877 sreg_masks [j] &= ~(regmask (sregs [0]));
1879 /* Handle the case when sreg1 is a regpair but dreg is not */
1880 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
1881 int reg2 = prev_sregs [0] + 1;
1884 g_assert (prev_sregs [0] > -1);
1885 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
1886 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
1887 val = rs->vassign [reg2];
1891 /* the register gets spilled after this inst */
1894 val = mono_regstate_alloc_int (rs, mask);
1896 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1898 g_assert_not_reached ();
1901 if (! (mask & (regmask (val)))) {
1902 /* The vreg is already allocated to a wrong hreg */
1904 g_assert_not_reached ();
1906 val = mono_regstate_alloc_int (rs, mask);
1908 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1910 /* Reallocate hreg to the correct register */
1911 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1913 mono_regstate_free_int (rs, rs->vassign [reg2]);
1919 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
1920 assign_reg (cfg, rs, reg2, val, bank);
1923 /* Handle dreg==sreg1 */
1924 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
1925 MonoInst *sreg2_copy = NULL;
1927 int bank = reg_bank (spec_src1);
1929 if (ins->dreg == sregs [1]) {
1931 * copying sreg1 to dreg could clobber sreg2, so allocate a new
1934 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
1936 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
1937 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
1938 prev_sregs [1] = sregs [1] = reg2;
1940 if (G_UNLIKELY (bank))
1941 mono_regstate_free_general (rs, reg2, bank);
1943 mono_regstate_free_int (rs, reg2);
1946 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
1947 /* Copying sreg1_high to dreg could also clobber sreg2 */
1948 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
1950 g_assert_not_reached ();
1953 * sreg1 and dest are already allocated to the same regpair by the
1954 * SREG1 allocation code.
1956 g_assert (sregs [0] == ins->dreg);
1957 g_assert (dreg_high == sreg1_high);
1960 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
1961 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
1962 insert_before_ins (bb, ins, copy);
1965 insert_before_ins (bb, copy, sreg2_copy);
1968 * Need to prevent sreg2 to be allocated to sreg1, since that
1969 * would screw up the previous copy.
1971 sreg_masks [1] &= ~ (regmask (sregs [0]));
1972 /* we set sreg1 to dest as well */
1973 prev_sregs [0] = sregs [0] = ins->dreg;
1974 sreg_masks [1] &= ~ (regmask (ins->dreg));
1976 mono_inst_set_src_registers (ins, sregs);
1979 * TRACK SREG2, 3, ...
1981 for (j = 1; j < num_sregs; ++j) {
1984 bank = sreg_bank (j, spec);
1985 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
1986 g_assert_not_reached ();
1988 if (dest_sregs [j] != -1 && is_global_ireg (sregs [j])) {
1990 * Argument already in a global hard reg, copy it to the fixed reg, without
1991 * allocating it to the fixed reg.
1993 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [j], sregs [j], NULL, ip, 0);
1994 insert_before_ins (bb, ins, copy);
1995 sregs [j] = dest_sregs [j];
1996 } else if (is_soft_reg (sregs [j], bank)) {
1997 val = rs->vassign [sregs [j]];
1999 if (dest_sregs [j] != -1 && val >= 0 && dest_sregs [j] != val) {
2001 * The sreg is already allocated to a hreg, but not to the fixed
2002 * reg required by the instruction. Spill the sreg, so it can be
2003 * allocated to the fixed reg by the code below.
2005 /* Currently, this code should only be hit for CAS */
2006 spill_vreg (cfg, bb, tmp, ins, sregs [j], 0);
2007 val = rs->vassign [sregs [j]];
2013 /* the register gets spilled after this inst */
2016 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
2017 assign_reg (cfg, rs, sregs [j], val, bank);
2018 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
2020 MonoInst *store = create_spilled_store (cfg, bb, spill, val, prev_sregs [j], tmp, NULL, bank);
2022 * Need to insert before the instruction since it can
2025 insert_before_ins (bb, ins, store);
2029 for (k = j + 1; k < num_sregs; ++k)
2030 sreg_masks [k] &= ~ (regmask (sregs [j]));
2033 prev_sregs [j] = -1;
2036 mono_inst_set_src_registers (ins, sregs);
2039 /* Do this only for CAS for now */
2040 for (j = 1; j < num_sregs; ++j) {
2041 int sreg = sregs [j];
2042 int dest_sreg = dest_sregs [j];
2044 if (j == 2 && dest_sreg != -1) {
2047 g_assert (sreg == dest_sreg);
2049 for (k = 0; k < num_sregs; ++k) {
2051 g_assert (sregs [k] != dest_sreg);
2056 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2057 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2058 mono_regstate_free_int (rs, ins->sreg1);
2060 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2061 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2062 mono_regstate_free_int (rs, ins->sreg2);
2065 DEBUG (mono_print_ins_index (i, ins));
2068 // FIXME: Set MAX_FREGS to 8
2069 // FIXME: Optimize generated code
2070 #if MONO_ARCH_USE_FPSTACK
2072 * Make a forward pass over the code, simulating the fp stack, making sure the
2073 * arguments required by the fp opcodes are at the top of the stack.
2076 MonoInst *prev = NULL;
2080 g_assert (num_sregs <= 2);
2082 for (ins = bb->code; ins; ins = ins->next) {
2083 spec = ins_get_spec (ins->opcode);
2085 DEBUG (printf ("processing:"));
2086 DEBUG (mono_print_ins_index (0, ins));
2088 if (ins->opcode == OP_FMOVE) {
2089 /* Do it by renaming the source to the destination on the stack */
2090 // FIXME: Is this correct ?
2091 for (i = 0; i < sp; ++i)
2092 if (fpstack [i] == ins->sreg1)
2093 fpstack [i] = ins->dreg;
2098 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2099 /* Arg1 must be in %st(1) */
2103 while ((i < sp) && (fpstack [i] != ins->sreg1))
2107 if (sp - 1 - i > 0) {
2108 /* First move it to %st(0) */
2109 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2111 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2112 fxch->inst_imm = sp - 1 - i;
2118 tmp = fpstack [sp - 1];
2119 fpstack [sp - 1] = fpstack [i];
2123 /* Then move it to %st(1) */
2124 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2126 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2133 tmp = fpstack [sp - 1];
2134 fpstack [sp - 1] = fpstack [sp - 2];
2135 fpstack [sp - 2] = tmp;
2138 if (sreg2_is_fp (spec)) {
2141 if (fpstack [sp - 1] != ins->sreg2) {
2145 while ((i < sp) && (fpstack [i] != ins->sreg2))
2149 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2151 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2152 fxch->inst_imm = sp - 1 - i;
2158 tmp = fpstack [sp - 1];
2159 fpstack [sp - 1] = fpstack [i];
2166 if (sreg1_is_fp (spec)) {
2169 if (fpstack [sp - 1] != ins->sreg1) {
2173 while ((i < sp) && (fpstack [i] != ins->sreg1))
2177 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2179 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2180 fxch->inst_imm = sp - 1 - i;
2186 tmp = fpstack [sp - 1];
2187 fpstack [sp - 1] = fpstack [i];
2194 if (dreg_is_fp (spec)) {
2196 fpstack [sp ++] = ins->dreg;
2199 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2201 for (i = 0; i < sp; ++i)
2202 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2209 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2210 /* Remove remaining items from the fp stack */
2212 * These can remain for example as a result of a dead fmove like in
2213 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2216 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2217 mono_add_ins_to_end (bb, ins);
2226 mono_opcode_to_cond (int opcode)
2236 case OP_COND_EXC_EQ:
2237 case OP_COND_EXC_IEQ:
2244 case OP_COND_EXC_NE_UN:
2245 case OP_COND_EXC_INE_UN:
2246 case OP_CMOV_INE_UN:
2247 case OP_CMOV_LNE_UN:
2268 case OP_COND_EXC_LT:
2269 case OP_COND_EXC_ILT:
2280 case OP_COND_EXC_GT:
2281 case OP_COND_EXC_IGT:
2289 case OP_COND_EXC_LE_UN:
2290 case OP_COND_EXC_ILE_UN:
2291 case OP_CMOV_ILE_UN:
2292 case OP_CMOV_LLE_UN:
2297 case OP_CMOV_IGE_UN:
2298 case OP_CMOV_LGE_UN:
2307 case OP_COND_EXC_LT_UN:
2308 case OP_COND_EXC_ILT_UN:
2309 case OP_CMOV_ILT_UN:
2310 case OP_CMOV_LLT_UN:
2319 case OP_COND_EXC_GT_UN:
2320 case OP_COND_EXC_IGT_UN:
2321 case OP_CMOV_IGT_UN:
2322 case OP_CMOV_LGT_UN:
2325 printf ("%s\n", mono_inst_name (opcode));
2326 g_assert_not_reached ();
2332 mono_negate_cond (CompRelation cond)
2356 g_assert_not_reached ();
2361 mono_opcode_to_type (int opcode, int cmp_opcode)
2363 if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2365 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2367 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2369 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2371 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2373 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2375 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2377 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2379 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2380 switch (cmp_opcode) {
2382 case OP_ICOMPARE_IMM:
2383 case OP_LCOMPARE_IMM:
2389 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2394 #endif /* DISABLE_JIT */
2397 mono_is_regsize_var (MonoType *t)
2401 t = mono_type_get_underlying_type (t);
2403 case MONO_TYPE_BOOLEAN:
2404 case MONO_TYPE_CHAR:
2414 case MONO_TYPE_FNPTR:
2415 #if SIZEOF_REGISTER == 8
2420 case MONO_TYPE_OBJECT:
2421 case MONO_TYPE_STRING:
2422 case MONO_TYPE_CLASS:
2423 case MONO_TYPE_SZARRAY:
2424 case MONO_TYPE_ARRAY:
2426 case MONO_TYPE_GENERICINST:
2427 if (!mono_type_generic_inst_is_valuetype (t))
2430 case MONO_TYPE_VALUETYPE:
2439 * mono_peephole_ins:
2441 * Perform some architecture independent peephole optimizations.
2444 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2446 MonoInst *last_ins = ins->prev;
2448 switch (ins->opcode) {
2450 /* remove unnecessary multiplication with 1 */
2451 if (ins->inst_imm == 1) {
2452 if (ins->dreg != ins->sreg1)
2453 ins->opcode = OP_MOVE;
2455 MONO_DELETE_INS (bb, ins);
2458 case OP_LOAD_MEMBASE:
2459 case OP_LOADI4_MEMBASE:
2461 * Note: if reg1 = reg2 the load op is removed
2463 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2464 * OP_LOAD_MEMBASE offset(basereg), reg2
2466 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2467 * OP_MOVE reg1, reg2
2469 if (last_ins && last_ins->opcode == OP_GC_LIVENESS_DEF)
2470 last_ins = last_ins->prev;
2472 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2473 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2474 ins->inst_basereg == last_ins->inst_destbasereg &&
2475 ins->inst_offset == last_ins->inst_offset) {
2476 if (ins->dreg == last_ins->sreg1) {
2477 MONO_DELETE_INS (bb, ins);
2480 ins->opcode = OP_MOVE;
2481 ins->sreg1 = last_ins->sreg1;
2485 * Note: reg1 must be different from the basereg in the second load
2486 * Note: if reg1 = reg2 is equal then second load is removed
2488 * OP_LOAD_MEMBASE offset(basereg), reg1
2489 * OP_LOAD_MEMBASE offset(basereg), reg2
2491 * OP_LOAD_MEMBASE offset(basereg), reg1
2492 * OP_MOVE reg1, reg2
2494 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2495 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2496 ins->inst_basereg != last_ins->dreg &&
2497 ins->inst_basereg == last_ins->inst_basereg &&
2498 ins->inst_offset == last_ins->inst_offset) {
2500 if (ins->dreg == last_ins->dreg) {
2501 MONO_DELETE_INS (bb, ins);
2503 ins->opcode = OP_MOVE;
2504 ins->sreg1 = last_ins->dreg;
2507 //g_assert_not_reached ();
2511 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2512 * OP_LOAD_MEMBASE offset(basereg), reg
2514 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2515 * OP_ICONST reg, imm
2517 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2518 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2519 ins->inst_basereg == last_ins->inst_destbasereg &&
2520 ins->inst_offset == last_ins->inst_offset) {
2521 ins->opcode = OP_ICONST;
2522 ins->inst_c0 = last_ins->inst_imm;
2523 g_assert_not_reached (); // check this rule
2527 case OP_LOADI1_MEMBASE:
2528 case OP_LOADU1_MEMBASE:
2530 * Note: if reg1 = reg2 the load op is removed
2532 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2533 * OP_LOAD_MEMBASE offset(basereg), reg2
2535 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2536 * OP_MOVE reg1, reg2
2538 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2539 ins->inst_basereg == last_ins->inst_destbasereg &&
2540 ins->inst_offset == last_ins->inst_offset) {
2541 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2542 ins->sreg1 = last_ins->sreg1;
2545 case OP_LOADI2_MEMBASE:
2546 case OP_LOADU2_MEMBASE:
2548 * Note: if reg1 = reg2 the load op is removed
2550 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2551 * OP_LOAD_MEMBASE offset(basereg), reg2
2553 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2554 * OP_MOVE reg1, reg2
2556 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2557 ins->inst_basereg == last_ins->inst_destbasereg &&
2558 ins->inst_offset == last_ins->inst_offset) {
2559 #if SIZEOF_REGISTER == 8
2560 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2562 /* The definition of OP_PCONV_TO_U2 is wrong */
2563 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2565 ins->sreg1 = last_ins->sreg1;
2575 if (ins->dreg == ins->sreg1) {
2576 MONO_DELETE_INS (bb, ins);
2582 * OP_MOVE sreg, dreg
2583 * OP_MOVE dreg, sreg
2585 if (last_ins && last_ins->opcode == ins->opcode &&
2586 ins->sreg1 == last_ins->dreg &&
2587 ins->dreg == last_ins->sreg1) {
2588 MONO_DELETE_INS (bb, ins);
2592 MONO_DELETE_INS (bb, ins);
2597 #endif /* DISABLE_JIT */