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/utils/mono-math.h>
21 #include "mini-arch.h"
23 #ifndef MONO_MAX_XREGS
25 #define MONO_MAX_XREGS 0
26 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
27 #define MONO_ARCH_CALLEE_XREGS 0
32 #define MONO_ARCH_BANK_MIRRORED -2
34 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
36 #ifndef MONO_ARCH_NEED_SIMD_BANK
37 #error "MONO_ARCH_USE_SHARED_FP_SIMD_BANK needs MONO_ARCH_NEED_SIMD_BANK to work"
40 #define get_mirrored_bank(bank) (((bank) == MONO_REG_SIMD ) ? MONO_REG_DOUBLE : (((bank) == MONO_REG_DOUBLE ) ? MONO_REG_SIMD : -1))
42 #define is_hreg_mirrored(rs, bank, hreg) ((rs)->symbolic [(bank)] [(hreg)] == MONO_ARCH_BANK_MIRRORED)
48 #define get_mirrored_bank(bank) (-1)
50 #define is_hreg_mirrored(rs, bank, hreg) (0)
55 /* If the bank is mirrored return the true logical bank that the register in the
56 * physical register bank is allocated to.
58 static inline int translate_bank (MonoRegState *rs, int bank, int hreg) {
59 return is_hreg_mirrored (rs, bank, hreg) ? get_mirrored_bank (bank) : bank;
63 * Every hardware register belongs to a register type or register bank. bank 0
64 * contains the int registers, bank 1 contains the fp registers.
65 * int registers are used 99% of the time, so they are special cased in a lot of
69 static const int regbank_size [] = {
75 static const int regbank_load_ops [] = {
81 static const int regbank_store_ops [] = {
82 OP_STORER_MEMBASE_REG,
83 OP_STORER8_MEMBASE_REG,
87 static const int regbank_move_ops [] = {
93 #define regmask(reg) (((regmask_t)1) << (reg))
95 static const regmask_t regbank_callee_saved_regs [] = {
96 MONO_ARCH_CALLEE_SAVED_REGS,
97 MONO_ARCH_CALLEE_SAVED_FREGS,
98 MONO_ARCH_CALLEE_SAVED_XREGS,
101 static const regmask_t regbank_callee_regs [] = {
102 MONO_ARCH_CALLEE_REGS,
103 MONO_ARCH_CALLEE_FREGS,
104 MONO_ARCH_CALLEE_XREGS,
107 static const int regbank_spill_var_size[] = {
110 16 /*FIXME make this a constant. Maybe MONO_ARCH_SIMD_VECTOR_SIZE? */
113 #define DEBUG(a) MINI_DEBUG(cfg->verbose_level, 3, a;)
115 static inline GSList*
116 g_slist_append_mempool (MonoMemPool *mp, GSList *list, gpointer data)
121 new_list = mono_mempool_alloc (mp, sizeof (GSList));
122 new_list->data = data;
123 new_list->next = NULL;
129 last->next = new_list;
137 mono_regstate_assign (MonoRegState *rs)
139 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
140 /* The regalloc may fail if fp and simd logical regbanks share the same physical reg bank and
141 * if the values here are not the same.
143 g_assert(regbank_callee_regs [MONO_REG_SIMD] == regbank_callee_regs [MONO_REG_DOUBLE]);
144 g_assert(regbank_callee_saved_regs [MONO_REG_SIMD] == regbank_callee_saved_regs [MONO_REG_DOUBLE]);
145 g_assert(regbank_size [MONO_REG_SIMD] == regbank_size [MONO_REG_DOUBLE]);
148 if (rs->next_vreg > rs->vassign_size) {
149 g_free (rs->vassign);
150 rs->vassign_size = MAX (rs->next_vreg, 256);
151 rs->vassign = g_malloc (rs->vassign_size * sizeof (gint32));
154 memset (rs->isymbolic, 0, MONO_MAX_IREGS * sizeof (rs->isymbolic [0]));
155 memset (rs->fsymbolic, 0, MONO_MAX_FREGS * sizeof (rs->fsymbolic [0]));
157 rs->symbolic [0] = rs->isymbolic;
158 rs->symbolic [1] = rs->fsymbolic;
160 #ifdef MONO_ARCH_NEED_SIMD_BANK
161 memset (rs->xsymbolic, 0, MONO_MAX_XREGS * sizeof (rs->xsymbolic [0]));
162 rs->symbolic [2] = rs->xsymbolic;
167 mono_regstate_alloc_int (MonoRegState *rs, regmask_t allow)
169 regmask_t mask = allow & rs->ifree_mask;
171 #if defined(__x86_64__) && defined(__GNUC__)
178 __asm__("bsfq %1,%0\n\t"
179 : "=r" (i) : "rm" (mask));
181 rs->ifree_mask &= ~ ((regmask_t)1 << i);
187 for (i = 0; i < MONO_MAX_IREGS; ++i) {
188 if (mask & ((regmask_t)1 << i)) {
189 rs->ifree_mask &= ~ ((regmask_t)1 << i);
198 mono_regstate_free_int (MonoRegState *rs, int reg)
201 rs->ifree_mask |= (regmask_t)1 << reg;
202 rs->isymbolic [reg] = 0;
207 mono_regstate_alloc_general (MonoRegState *rs, regmask_t allow, int bank)
211 regmask_t mask = allow & rs->free_mask [bank];
212 for (i = 0; i < regbank_size [bank]; ++i) {
213 if (mask & ((regmask_t)1 << i)) {
214 rs->free_mask [bank] &= ~ ((regmask_t)1 << i);
216 mirrored_bank = get_mirrored_bank (bank);
217 if (mirrored_bank == -1)
220 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
228 mono_regstate_free_general (MonoRegState *rs, int reg, int bank)
233 rs->free_mask [bank] |= (regmask_t)1 << reg;
234 rs->symbolic [bank][reg] = 0;
236 mirrored_bank = get_mirrored_bank (bank);
237 if (mirrored_bank == -1)
239 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
240 rs->symbolic [mirrored_bank][reg] = 0;
245 mono_regname_full (int reg, int bank)
247 if (G_UNLIKELY (bank)) {
248 #if MONO_ARCH_NEED_SIMD_BANK
250 return mono_arch_xregname (reg);
252 g_assert (bank == 1);
253 return mono_arch_fregname (reg);
255 return mono_arch_regname (reg);
260 mono_call_inst_add_outarg_reg (MonoCompile *cfg, MonoCallInst *call, int vreg, int hreg, int bank)
264 regpair = (((guint32)hreg) << 24) + vreg;
265 if (G_UNLIKELY (bank)) {
266 g_assert (vreg >= regbank_size [bank]);
267 g_assert (hreg < regbank_size [bank]);
268 call->used_fregs |= 1 << hreg;
269 call->out_freg_args = g_slist_append_mempool (cfg->mempool, call->out_freg_args, (gpointer)(gssize)(regpair));
271 g_assert (vreg >= MONO_MAX_IREGS);
272 g_assert (hreg < MONO_MAX_IREGS);
273 call->used_iregs |= 1 << hreg;
274 call->out_ireg_args = g_slist_append_mempool (cfg->mempool, call->out_ireg_args, (gpointer)(gssize)(regpair));
279 resize_spill_info (MonoCompile *cfg, int bank)
281 MonoSpillInfo *orig_info = cfg->spill_info [bank];
282 int orig_len = cfg->spill_info_len [bank];
283 int new_len = orig_len ? orig_len * 2 : 16;
284 MonoSpillInfo *new_info;
287 g_assert (bank < MONO_NUM_REGBANKS);
289 new_info = mono_mempool_alloc (cfg->mempool, sizeof (MonoSpillInfo) * new_len);
291 memcpy (new_info, orig_info, sizeof (MonoSpillInfo) * orig_len);
292 for (i = orig_len; i < new_len; ++i)
293 new_info [i].offset = -1;
295 cfg->spill_info [bank] = new_info;
296 cfg->spill_info_len [bank] = new_len;
300 * returns the offset used by spillvar. It allocates a new
301 * spill variable if necessary.
304 mono_spillvar_offset (MonoCompile *cfg, int spillvar, int bank)
309 if (G_UNLIKELY (spillvar >= (cfg->spill_info_len [bank]))) {
310 while (spillvar >= cfg->spill_info_len [bank])
311 resize_spill_info (cfg, bank);
315 * Allocate separate spill slots for fp/non-fp variables since most processors prefer it.
317 info = &cfg->spill_info [bank][spillvar];
318 if (info->offset == -1) {
319 cfg->stack_offset += sizeof (mgreg_t) - 1;
320 cfg->stack_offset &= ~(sizeof (mgreg_t) - 1);
322 g_assert (bank < MONO_NUM_REGBANKS);
323 if (G_UNLIKELY (bank))
324 size = regbank_spill_var_size [bank];
326 size = sizeof (mgreg_t);
328 if (cfg->flags & MONO_CFG_HAS_SPILLUP) {
329 cfg->stack_offset += size - 1;
330 cfg->stack_offset &= ~(size - 1);
331 info->offset = cfg->stack_offset;
332 cfg->stack_offset += size;
334 cfg->stack_offset += size - 1;
335 cfg->stack_offset &= ~(size - 1);
336 cfg->stack_offset += size;
337 info->offset = - cfg->stack_offset;
344 #define is_hard_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS)
345 #define is_hard_freg(r) ((r) >= 0 && (r) < MONO_MAX_FREGS)
346 #define is_global_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_SAVED_REGS & (regmask (r))))
347 #define is_local_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_REGS & (regmask (r))))
348 #define is_global_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_SAVED_FREGS & (regmask (r))))
349 #define is_local_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_FREGS & (regmask (r))))
351 #define is_hard_reg(r,bank) (G_UNLIKELY (bank) ? ((r) >= 0 && (r) < regbank_size [bank]) : ((r) < MONO_MAX_IREGS))
352 #define is_soft_reg(r,bank) (!is_hard_reg((r),(bank)))
353 #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))
354 #define is_local_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_regs [bank] & regmask (r))) : is_local_ireg (r))
355 #define reg_is_freeable(r,bank) (G_UNLIKELY (bank) ? is_local_reg ((r), (bank)) : is_local_ireg ((r)))
357 #ifndef MONO_ARCH_INST_IS_FLOAT
358 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc) == 'f')
361 #define reg_is_fp(desc) (MONO_ARCH_INST_IS_FLOAT (desc))
362 #define dreg_is_fp(spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_DEST]))
363 #define sreg_is_fp(n,spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_SRC1+(n)]))
364 #define sreg1_is_fp(spec) sreg_is_fp (0,(spec))
365 #define sreg2_is_fp(spec) sreg_is_fp (1,(spec))
367 #define reg_is_simd(desc) ((desc) == 'x')
369 #ifdef MONO_ARCH_NEED_SIMD_BANK
371 #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)
375 #define reg_bank(desc) reg_is_fp ((desc))
379 #define sreg_bank(n,spec) reg_bank ((spec)[MONO_INST_SRC1+(n)])
380 #define sreg1_bank(spec) sreg_bank (0, (spec))
381 #define sreg2_bank(spec) sreg_bank (1, (spec))
382 #define dreg_bank(spec) reg_bank ((spec)[MONO_INST_DEST])
384 #define sreg_bank_ins(n,ins) sreg_bank ((n), ins_get_spec ((ins)->opcode))
385 #define sreg1_bank_ins(ins) sreg_bank_ins (0, (ins))
386 #define sreg2_bank_ins(ins) sreg_bank_ins (1, (ins))
387 #define dreg_bank_ins(ins) dreg_bank (ins_get_spec ((ins)->opcode))
389 #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)
391 #ifdef MONO_ARCH_IS_GLOBAL_IREG
392 #undef is_global_ireg
393 #define is_global_ireg(reg) MONO_ARCH_IS_GLOBAL_IREG ((reg))
402 regmask_t preferred_mask; /* the hreg where the register should be allocated, or 0 */
405 #ifndef DISABLE_LOGGING
407 mono_print_ins_index (int i, MonoInst *ins)
409 const char *spec = ins_get_spec (ins->opcode);
411 int sregs [MONO_MAX_SRC_REGS];
414 printf ("\t%-2d %s", i, mono_inst_name (ins->opcode));
416 printf (" %s", mono_inst_name (ins->opcode));
417 if (spec == MONO_ARCH_CPU_SPEC) {
418 /* This is a lowered opcode */
420 printf (" R%d <-", ins->dreg);
421 if (ins->sreg1 != -1)
422 printf (" R%d", ins->sreg1);
423 if (ins->sreg2 != -1)
424 printf (" R%d", ins->sreg2);
425 if (ins->sreg3 != -1)
426 printf (" R%d", ins->sreg3);
428 switch (ins->opcode) {
439 if (!ins->inst_false_bb)
440 printf (" [B%d]", ins->inst_true_bb->block_num);
442 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
449 printf (" [%d (", (int)ins->inst_c0);
450 for (i = 0; i < ins->inst_phi_args [0]; i++) {
453 printf ("R%d", ins->inst_phi_args [i + 1]);
459 case OP_OUTARG_VTRETADDR:
460 printf (" R%d", ((MonoInst*)ins->inst_p0)->dreg);
463 printf (" + 0x%lx", (long)ins->inst_offset);
470 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
474 if (spec [MONO_INST_DEST]) {
475 int bank = dreg_bank (spec);
476 if (is_soft_reg (ins->dreg, bank)) {
477 if (spec [MONO_INST_DEST] == 'b') {
478 if (ins->inst_offset == 0)
479 printf (" [R%d] <-", ins->dreg);
481 printf (" [R%d + 0x%lx] <-", ins->dreg, (long)ins->inst_offset);
484 printf (" R%d <-", ins->dreg);
485 } else if (spec [MONO_INST_DEST] == 'b') {
486 if (ins->inst_offset == 0)
487 printf (" [%s] <-", mono_arch_regname (ins->dreg));
489 printf (" [%s + 0x%lx] <-", mono_arch_regname (ins->dreg), (long)ins->inst_offset);
491 printf (" %s <-", mono_regname_full (ins->dreg, bank));
493 if (spec [MONO_INST_SRC1]) {
494 int bank = sreg1_bank (spec);
495 if (is_soft_reg (ins->sreg1, bank)) {
496 if (spec [MONO_INST_SRC1] == 'b')
497 printf (" [R%d + 0x%lx]", ins->sreg1, (long)ins->inst_offset);
499 printf (" R%d", ins->sreg1);
500 } else if (spec [MONO_INST_SRC1] == 'b')
501 printf (" [%s + 0x%lx]", mono_arch_regname (ins->sreg1), (long)ins->inst_offset);
503 printf (" %s", mono_regname_full (ins->sreg1, bank));
505 num_sregs = mono_inst_get_src_registers (ins, sregs);
506 for (j = 1; j < num_sregs; ++j) {
507 int bank = sreg_bank (j, spec);
508 if (is_soft_reg (sregs [j], bank))
509 printf (" R%d", sregs [j]);
511 printf (" %s", mono_regname_full (sregs [j], bank));
514 switch (ins->opcode) {
516 printf (" [%d]", (int)ins->inst_c0);
518 #if defined(TARGET_X86) || defined(TARGET_AMD64)
519 case OP_X86_PUSH_IMM:
521 case OP_ICOMPARE_IMM:
528 printf (" [%d]", (int)ins->inst_imm);
532 printf (" [%d]", (int)(gssize)ins->inst_p1);
535 printf (" [%lld]", (long long)ins->inst_l);
538 printf (" [%f]", *(double*)ins->inst_p0);
541 printf (" [%f]", *(float*)ins->inst_p0);
546 case OP_CALL_MEMBASE:
555 case OP_VCALL_MEMBASE:
558 case OP_VCALL2_MEMBASE:
560 case OP_VOIDCALL_MEMBASE:
561 case OP_VOIDCALLVIRT: {
562 MonoCallInst *call = (MonoCallInst*)ins;
565 if (ins->opcode == OP_VCALL || ins->opcode == OP_VCALL_REG || ins->opcode == OP_VCALL_MEMBASE) {
567 * These are lowered opcodes, but they are in the .md files since the old
568 * JIT passes them to backends.
571 printf (" R%d <-", ins->dreg);
575 char *full_name = mono_method_full_name (call->method, TRUE);
576 printf (" [%s]", full_name);
578 } else if (call->fptr) {
579 MonoJitICallInfo *info = mono_find_jit_icall_by_addr (call->fptr);
581 printf (" [%s]", info->name);
584 list = call->out_ireg_args;
589 regpair = (guint32)(gssize)(list->data);
590 hreg = regpair >> 24;
591 reg = regpair & 0xffffff;
593 printf (" [%s <- R%d]", mono_arch_regname (hreg), reg);
595 list = g_slist_next (list);
600 case OP_CALL_HANDLER:
601 printf (" [B%d]", ins->inst_target_bb->block_num);
633 if (!ins->inst_false_bb)
634 printf (" [B%d]", ins->inst_true_bb->block_num);
636 printf (" [B%dB%d]", ins->inst_true_bb->block_num, ins->inst_false_bb->block_num);
638 case OP_LIVERANGE_START:
639 case OP_LIVERANGE_END:
640 printf (" R%d", (int)ins->inst_c1);
646 if (spec [MONO_INST_CLOB])
647 printf (" clobbers: %c", spec [MONO_INST_CLOB]);
652 print_regtrack (RegTrack *t, int num)
658 for (i = 0; i < num; ++i) {
661 if (i >= MONO_MAX_IREGS) {
662 g_snprintf (buf, sizeof(buf), "R%d", i);
665 r = mono_arch_regname (i);
666 printf ("liveness: %s [%d - %d]\n", r, t [i].born_in, t[i].killed_in);
671 mono_print_ins_index (int i, MonoInst *ins)
674 #endif /* DISABLE_LOGGING */
677 mono_print_ins (MonoInst *ins)
679 mono_print_ins_index (-1, ins);
683 insert_before_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst* to_insert)
686 * If this function is called multiple times, the new instructions are inserted
687 * in the proper order.
689 mono_bblock_insert_before_ins (bb, ins, to_insert);
693 insert_after_ins (MonoBasicBlock *bb, MonoInst *ins, MonoInst **last, MonoInst* to_insert)
696 * If this function is called multiple times, the new instructions are inserted in
699 mono_bblock_insert_after_ins (bb, *last, to_insert);
705 * Force the spilling of the variable in the symbolic register 'reg'.
708 get_register_force_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int reg, int bank)
713 MonoRegState *rs = cfg->rs;
715 symbolic = rs->symbolic [bank];
716 sel = rs->vassign [reg];
718 /* the vreg we need to spill lives in another logical reg bank */
719 bank = translate_bank (cfg->rs, bank, sel);
721 /*i = rs->isymbolic [sel];
722 g_assert (i == reg);*/
724 spill = ++cfg->spill_count;
725 rs->vassign [i] = -spill - 1;
726 if (G_UNLIKELY (bank))
727 mono_regstate_free_general (rs, sel, bank);
729 mono_regstate_free_int (rs, sel);
730 /* we need to create a spill var and insert a load to sel after the current instruction */
731 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
733 load->inst_basereg = cfg->frame_reg;
734 load->inst_offset = mono_spillvar_offset (cfg, spill, bank);
735 insert_after_ins (bb, ins, last, load);
736 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)));
737 if (G_UNLIKELY (bank))
738 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
740 i = mono_regstate_alloc_int (rs, regmask (sel));
746 /* This isn't defined on older glib versions and on some platforms */
747 #ifndef G_GUINT64_FORMAT
748 #define G_GUINT64_FORMAT "ul"
752 get_register_spilling (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t regmask, int reg, int bank)
755 int i, sel, spill, num_sregs;
756 int sregs [MONO_MAX_SRC_REGS];
758 MonoRegState *rs = cfg->rs;
760 symbolic = rs->symbolic [bank];
762 g_assert (bank < MONO_NUM_REGBANKS);
764 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));
765 /* exclude the registers in the current instruction */
766 num_sregs = mono_inst_get_src_registers (ins, sregs);
767 for (i = 0; i < num_sregs; ++i) {
768 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))) {
769 if (is_soft_reg (sregs [i], bank))
770 regmask &= ~ (regmask (rs->vassign [sregs [i]]));
772 regmask &= ~ (regmask (sregs [i]));
773 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i + 1, mono_regname_full (sregs [i], bank), sregs [i]));
776 if ((dreg_bank_ins (ins) == bank) && (reg != ins->dreg) && reg_is_freeable (ins->dreg, bank)) {
777 regmask &= ~ (regmask (ins->dreg));
778 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins->dreg, bank)));
781 DEBUG (printf ("\t\tavailable regmask: 0x%08" G_GUINT64_FORMAT "\n", (guint64)regmask));
782 g_assert (regmask); /* need at least a register we can free */
784 /* we should track prev_use and spill the register that's farther */
785 if (G_UNLIKELY (bank)) {
786 for (i = 0; i < regbank_size [bank]; ++i) {
787 if (regmask & (regmask (i))) {
790 /* the vreg we need to load lives in another logical bank */
791 bank = translate_bank (cfg->rs, bank, sel);
793 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel, bank), rs->symbolic [bank] [sel]));
798 i = rs->symbolic [bank] [sel];
799 spill = ++cfg->spill_count;
800 rs->vassign [i] = -spill - 1;
801 mono_regstate_free_general (rs, sel, bank);
804 for (i = 0; i < MONO_MAX_IREGS; ++i) {
805 if (regmask & (regmask (i))) {
807 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel), rs->isymbolic [sel]));
812 i = rs->isymbolic [sel];
813 spill = ++cfg->spill_count;
814 rs->vassign [i] = -spill - 1;
815 mono_regstate_free_int (rs, sel);
818 /* we need to create a spill var and insert a load to sel after the current instruction */
819 MONO_INST_NEW (cfg, load, regbank_load_ops [bank]);
821 load->inst_basereg = cfg->frame_reg;
822 load->inst_offset = mono_spillvar_offset (cfg, spill, bank);
823 insert_after_ins (bb, ins, last, load);
824 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)));
825 if (G_UNLIKELY (bank))
826 i = mono_regstate_alloc_general (rs, regmask (sel), bank);
828 i = mono_regstate_alloc_int (rs, regmask (sel));
835 free_up_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, int hreg, int bank)
837 if (G_UNLIKELY (bank)) {
838 if (!(cfg->rs->free_mask [1] & (regmask (hreg)))) {
839 bank = translate_bank (cfg->rs, bank, hreg);
840 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->symbolic [bank] [hreg]));
841 get_register_force_spilling (cfg, bb, last, ins, cfg->rs->symbolic [bank] [hreg], bank);
842 mono_regstate_free_general (cfg->rs, hreg, bank);
846 if (!(cfg->rs->ifree_mask & (regmask (hreg)))) {
847 DEBUG (printf ("\tforced spill of R%d\n", cfg->rs->isymbolic [hreg]));
848 get_register_force_spilling (cfg, bb, last, ins, cfg->rs->isymbolic [hreg], bank);
849 mono_regstate_free_int (cfg->rs, hreg);
855 create_copy_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, int dest, int src, MonoInst *ins, const unsigned char *ip, int bank)
859 MONO_INST_NEW (cfg, copy, regbank_move_ops [bank]);
865 mono_bblock_insert_after_ins (bb, ins, copy);
868 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src, bank), mono_regname_full (dest, bank)));
873 create_spilled_store (MonoCompile *cfg, MonoBasicBlock *bb, int spill, int reg, int prev_reg, MonoInst **last, MonoInst *ins, int bank)
876 MONO_INST_NEW (cfg, store, regbank_store_ops [bank]);
878 store->inst_destbasereg = cfg->frame_reg;
879 store->inst_offset = mono_spillvar_offset (cfg, spill, bank);
881 mono_bblock_insert_after_ins (bb, ins, store);
884 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)));
888 /* flags used in reginfo->flags */
890 MONO_FP_NEEDS_LOAD_SPILL = regmask (0),
891 MONO_FP_NEEDS_SPILL = regmask (1),
892 MONO_FP_NEEDS_LOAD = regmask (2)
896 alloc_int_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info)
900 if (info && info->preferred_mask) {
901 val = mono_regstate_alloc_int (cfg->rs, info->preferred_mask & dest_mask);
903 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg, mono_arch_regname (val)));
908 val = mono_regstate_alloc_int (cfg->rs, dest_mask);
910 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, 0);
916 alloc_general_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, int bank)
920 val = mono_regstate_alloc_general (cfg->rs, dest_mask, bank);
923 val = get_register_spilling (cfg, bb, last, ins, dest_mask, sym_reg, bank);
929 alloc_reg (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst **last, MonoInst *ins, regmask_t dest_mask, int sym_reg, RegTrack *info, int bank)
931 if (G_UNLIKELY (bank))
932 return alloc_general_reg (cfg, bb, last, ins, dest_mask, sym_reg, bank);
934 return alloc_int_reg (cfg, bb, last, ins, dest_mask, sym_reg, info);
938 assign_reg (MonoCompile *cfg, MonoRegState *rs, int reg, int hreg, int bank)
940 if (G_UNLIKELY (bank)) {
943 g_assert (reg >= regbank_size [bank]);
944 g_assert (hreg < regbank_size [bank]);
945 g_assert (! is_global_freg (hreg));
947 rs->vassign [reg] = hreg;
948 rs->symbolic [bank] [hreg] = reg;
949 rs->free_mask [bank] &= ~ (regmask (hreg));
951 mirrored_bank = get_mirrored_bank (bank);
952 if (mirrored_bank == -1)
955 /* Make sure the other logical reg bank that this bank shares
956 * a single hard reg bank knows that this hard reg is not free.
958 rs->free_mask [mirrored_bank] = rs->free_mask [bank];
960 /* Mark the other logical bank that the this bank shares
961 * a single hard reg bank with as mirrored.
963 rs->symbolic [mirrored_bank] [hreg] = MONO_ARCH_BANK_MIRRORED;
967 g_assert (reg >= MONO_MAX_IREGS);
968 g_assert (hreg < MONO_MAX_IREGS);
970 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
971 g_assert (! is_global_ireg (hreg));
974 rs->vassign [reg] = hreg;
975 rs->isymbolic [hreg] = reg;
976 rs->ifree_mask &= ~ (regmask (hreg));
980 static inline regmask_t
981 get_callee_mask (const char spec)
983 if (G_UNLIKELY (reg_bank (spec)))
984 return regbank_callee_regs [reg_bank (spec)];
985 return MONO_ARCH_CALLEE_REGS;
988 static gint8 desc_to_fixed_reg [256];
989 static gboolean desc_to_fixed_reg_inited = FALSE;
992 * Local register allocation.
993 * We first scan the list of instructions and we save the liveness info of
994 * each register (when the register is first used, when it's value is set etc.).
995 * We also reverse the list of instructions because assigning registers backwards allows
996 * for more tricks to be used.
999 mono_local_regalloc (MonoCompile *cfg, MonoBasicBlock *bb)
1001 MonoInst *ins, *prev, *last;
1003 MonoRegState *rs = cfg->rs;
1007 unsigned char spec_src1, spec_dest;
1009 #if MONO_ARCH_USE_FPSTACK
1010 gboolean has_fp = FALSE;
1015 int sregs [MONO_MAX_SRC_REGS];
1020 if (!desc_to_fixed_reg_inited) {
1021 for (i = 0; i < 256; ++i)
1022 desc_to_fixed_reg [i] = MONO_ARCH_INST_FIXED_REG (i);
1023 desc_to_fixed_reg_inited = TRUE;
1025 /* Validate the cpu description against the info in mini-ops.h */
1026 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM)
1027 for (i = OP_LOAD; i < OP_LAST; ++i) {
1030 spec = ins_get_spec (i);
1031 ispec = INS_INFO (i);
1033 if ((spec [MONO_INST_DEST] && (ispec [MONO_INST_DEST] == ' ')))
1034 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1035 if ((spec [MONO_INST_SRC1] && (ispec [MONO_INST_SRC1] == ' ')))
1036 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1037 if ((spec [MONO_INST_SRC2] && (ispec [MONO_INST_SRC2] == ' ')))
1038 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i));
1043 rs->next_vreg = bb->max_vreg;
1044 mono_regstate_assign (rs);
1046 rs->ifree_mask = MONO_ARCH_CALLEE_REGS;
1047 for (i = 0; i < MONO_NUM_REGBANKS; ++i)
1048 rs->free_mask [i] = regbank_callee_regs [i];
1050 max = rs->next_vreg;
1052 if (cfg->reginfo && cfg->reginfo_len < max)
1053 cfg->reginfo = NULL;
1055 reginfo = cfg->reginfo;
1057 cfg->reginfo_len = MAX (1024, max * 2);
1058 reginfo = cfg->reginfo = mono_mempool_alloc (cfg->mempool, sizeof (RegTrack) * cfg->reginfo_len);
1061 g_assert (cfg->reginfo_len >= rs->next_vreg);
1063 if (cfg->verbose_level > 1) {
1064 /* print_regtrack reads the info of all variables */
1065 memset (cfg->reginfo, 0, cfg->reginfo_len * sizeof (RegTrack));
1069 * For large methods, next_vreg can be very large, so g_malloc0 time can
1070 * be prohibitive. So we manually init the reginfo entries used by the
1073 for (ins = bb->code; ins; ins = ins->next) {
1074 spec = ins_get_spec (ins->opcode);
1076 if ((ins->dreg != -1) && (ins->dreg < max)) {
1077 memset (®info [ins->dreg], 0, sizeof (RegTrack));
1078 #if SIZEOF_REGISTER == 4
1079 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_DEST])) {
1081 * In the new IR, the two vregs of the regpair do not alias the
1082 * original long vreg. shift the vreg here so the rest of the
1083 * allocator doesn't have to care about it.
1086 memset (®info [ins->dreg + 1], 0, sizeof (RegTrack));
1091 num_sregs = mono_inst_get_src_registers (ins, sregs);
1092 for (j = 0; j < num_sregs; ++j) {
1093 g_assert (sregs [j] != -1);
1094 if (sregs [j] < max) {
1095 memset (®info [sregs [j]], 0, sizeof (RegTrack));
1096 #if SIZEOF_REGISTER == 4
1097 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j])) {
1099 memset (®info [sregs [j] + 1], 0, sizeof (RegTrack));
1104 mono_inst_set_src_registers (ins, sregs);
1107 /*if (cfg->opt & MONO_OPT_COPYPROP)
1108 local_copy_prop (cfg, ins);*/
1111 DEBUG (printf ("\nLOCAL REGALLOC: BASIC BLOCK %d:\n", bb->block_num));
1112 /* forward pass on the instructions to collect register liveness info */
1113 MONO_BB_FOR_EACH_INS (bb, ins) {
1114 spec = ins_get_spec (ins->opcode);
1115 spec_dest = spec [MONO_INST_DEST];
1117 if (G_UNLIKELY (spec == MONO_ARCH_CPU_SPEC)) {
1118 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins->opcode));
1121 DEBUG (mono_print_ins_index (i, ins));
1123 num_sregs = mono_inst_get_src_registers (ins, sregs);
1125 #if MONO_ARCH_USE_FPSTACK
1126 if (dreg_is_fp (spec)) {
1129 for (j = 0; j < num_sregs; ++j) {
1130 if (sreg_is_fp (j, spec))
1136 for (j = 0; j < num_sregs; ++j) {
1137 int sreg = sregs [j];
1138 int sreg_spec = spec [MONO_INST_SRC1 + j];
1140 bank = sreg_bank (j, spec);
1141 g_assert (sreg != -1);
1142 if (is_soft_reg (sreg, bank))
1143 /* This means the vreg is not local to this bb */
1144 g_assert (reginfo [sreg].born_in > 0);
1145 rs->vassign [sreg] = -1;
1146 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1147 //reginfo [ins->sreg2].last_use = i;
1148 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec)) {
1149 /* The virtual register is allocated sequentially */
1150 rs->vassign [sreg + 1] = -1;
1151 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1152 //reginfo [ins->sreg2 + 1].last_use = i;
1153 if (reginfo [sreg + 1].born_in == 0 || reginfo [sreg + 1].born_in > i)
1154 reginfo [sreg + 1].born_in = i;
1160 mono_inst_set_src_registers (ins, sregs);
1165 bank = dreg_bank (spec);
1166 if (spec_dest != 'b') /* it's not just a base register */
1167 reginfo [ins->dreg].killed_in = i;
1168 g_assert (ins->dreg != -1);
1169 rs->vassign [ins->dreg] = -1;
1170 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1171 //reginfo [ins->dreg].last_use = i;
1172 if (reginfo [ins->dreg].born_in == 0 || reginfo [ins->dreg].born_in > i)
1173 reginfo [ins->dreg].born_in = i;
1175 dest_dreg = desc_to_fixed_reg [spec_dest];
1176 if (dest_dreg != -1)
1177 reginfo [ins->dreg].preferred_mask = (regmask (dest_dreg));
1179 #ifdef MONO_ARCH_INST_FIXED_MASK
1180 reginfo [ins->dreg].preferred_mask |= MONO_ARCH_INST_FIXED_MASK (spec_dest);
1183 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1184 /* The virtual register is allocated sequentially */
1185 rs->vassign [ins->dreg + 1] = -1;
1186 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1187 //reginfo [ins->dreg + 1].last_use = i;
1188 if (reginfo [ins->dreg + 1].born_in == 0 || reginfo [ins->dreg + 1].born_in > i)
1189 reginfo [ins->dreg + 1].born_in = i;
1190 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, -1) != -1)
1191 reginfo [ins->dreg + 1].preferred_mask = regpair_reg2_mask (spec_dest, -1);
1197 if (spec [MONO_INST_CLOB] == 'c') {
1198 /* A call instruction implicitly uses all registers in call->out_ireg_args */
1200 MonoCallInst *call = (MonoCallInst*)ins;
1203 list = call->out_ireg_args;
1209 regpair = (guint32)(gssize)(list->data);
1210 hreg = regpair >> 24;
1211 reg = regpair & 0xffffff;
1213 //reginfo [reg].prev_use = reginfo [reg].last_use;
1214 //reginfo [reg].last_use = i;
1216 list = g_slist_next (list);
1220 list = call->out_freg_args;
1226 regpair = (guint32)(gssize)(list->data);
1227 hreg = regpair >> 24;
1228 reg = regpair & 0xffffff;
1230 list = g_slist_next (list);
1240 DEBUG (print_regtrack (reginfo, rs->next_vreg));
1241 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb, prev, ins) {
1242 int prev_dreg, clob_dreg;
1243 int dest_dreg, clob_reg;
1244 int dest_sregs [MONO_MAX_SRC_REGS], prev_sregs [MONO_MAX_SRC_REGS];
1245 int dreg_high, sreg1_high;
1246 regmask_t dreg_mask, mask;
1247 regmask_t sreg_masks [MONO_MAX_SRC_REGS], sreg_fixed_masks [MONO_MAX_SRC_REGS];
1248 regmask_t dreg_fixed_mask;
1249 const unsigned char *ip;
1251 spec = ins_get_spec (ins->opcode);
1252 spec_src1 = spec [MONO_INST_SRC1];
1253 spec_dest = spec [MONO_INST_DEST];
1260 dreg_mask = get_callee_mask (spec_dest);
1261 for (j = 0; j < MONO_MAX_SRC_REGS; ++j) {
1262 prev_sregs [j] = -1;
1263 sreg_masks [j] = get_callee_mask (spec [MONO_INST_SRC1 + j]);
1264 dest_sregs [j] = desc_to_fixed_reg [(int)spec [MONO_INST_SRC1 + j]];
1265 #ifdef MONO_ARCH_INST_FIXED_MASK
1266 sreg_fixed_masks [j] = MONO_ARCH_INST_FIXED_MASK (spec [MONO_INST_SRC1 + j]);
1268 sreg_fixed_masks [j] = 0;
1272 DEBUG (printf ("processing:"));
1273 DEBUG (mono_print_ins_index (i, ins));
1282 dest_dreg = desc_to_fixed_reg [spec_dest];
1283 clob_reg = desc_to_fixed_reg [(int)spec [MONO_INST_CLOB]];
1284 sreg_masks [1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec));
1286 #ifdef MONO_ARCH_INST_FIXED_MASK
1287 dreg_fixed_mask = MONO_ARCH_INST_FIXED_MASK (spec_dest);
1289 dreg_fixed_mask = 0;
1292 num_sregs = mono_inst_get_src_registers (ins, sregs);
1295 * TRACK FIXED SREG2, 3, ...
1297 for (j = 1; j < num_sregs; ++j) {
1298 int sreg = sregs [j];
1299 int dest_sreg = dest_sregs [j];
1300 if (dest_sreg != -1) {
1301 if (rs->ifree_mask & (regmask (dest_sreg))) {
1302 if (is_global_ireg (sreg)) {
1304 /* Argument already in hard reg, need to copy */
1305 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1306 insert_before_ins (bb, ins, copy);
1307 for (k = 0; k < num_sregs; ++k) {
1309 sreg_masks [k] &= ~ (regmask (dest_sreg));
1313 val = rs->vassign [sreg];
1315 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg, mono_arch_regname (dest_sreg)));
1316 assign_reg (cfg, rs, sreg, dest_sreg, 0);
1317 } else if (val < -1) {
1319 g_assert_not_reached ();
1321 /* Argument already in hard reg, need to copy */
1322 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, val, NULL, ip, 0);
1325 insert_before_ins (bb, ins, copy);
1326 for (k = 0; k < num_sregs; ++k) {
1328 sreg_masks [k] &= ~ (regmask (dest_sreg));
1331 * Prevent the dreg from being allocate to dest_sreg
1332 * too, since it could force sreg1 to be allocated to
1333 * the same reg on x86.
1335 dreg_mask &= ~ (regmask (dest_sreg));
1339 gboolean need_spill = TRUE;
1340 gboolean need_assign = TRUE;
1343 dreg_mask &= ~ (regmask (dest_sreg));
1344 for (k = 0; k < num_sregs; ++k) {
1346 sreg_masks [k] &= ~ (regmask (dest_sreg));
1350 * First check if dreg is assigned to dest_sreg2, since we
1351 * can't spill a dreg.
1353 val = rs->vassign [ins->dreg];
1354 if (val == dest_sreg && ins->dreg != sreg) {
1356 * the destination register is already assigned to
1357 * dest_sreg2: we need to allocate another register for it
1358 * and then copy from this to dest_sreg2.
1361 new_dest = alloc_int_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg]);
1362 g_assert (new_dest >= 0);
1363 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins->dreg, mono_arch_regname (new_dest), mono_arch_regname (dest_sreg)));
1365 prev_dreg = ins->dreg;
1366 assign_reg (cfg, rs, ins->dreg, new_dest, 0);
1367 clob_dreg = ins->dreg;
1368 create_copy_ins (cfg, bb, tmp, dest_sreg, new_dest, ins, ip, 0);
1369 mono_regstate_free_int (rs, dest_sreg);
1373 if (is_global_ireg (sreg)) {
1374 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sreg, sreg, NULL, ip, 0);
1375 insert_before_ins (bb, ins, copy);
1376 need_assign = FALSE;
1379 val = rs->vassign [sreg];
1380 if (val == dest_sreg) {
1381 /* sreg2 is already assigned to the correct register */
1383 } else if (val < -1) {
1384 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1385 } else if (val >= 0) {
1386 /* sreg2 already assigned to another register */
1388 * We couldn't emit a copy from val to dest_sreg2, because
1389 * val might be spilled later while processing this
1390 * instruction. So we spill sreg2 so it can be allocated to
1393 DEBUG (printf ("\tforced spill of R%d\n", sreg));
1394 free_up_reg (cfg, bb, tmp, ins, val, 0);
1399 DEBUG (printf ("\tforced spill of R%d\n", rs->isymbolic [dest_sreg]));
1400 free_up_reg (cfg, bb, tmp, ins, dest_sreg, 0);
1404 if (rs->vassign [sreg] < -1) {
1408 /* Need to emit a spill store */
1409 spill = - rs->vassign [sreg] - 1;
1410 store = create_spilled_store (cfg, bb, spill, dest_sreg, sreg, tmp, NULL, bank);
1411 insert_before_ins (bb, ins, store);
1413 /* force-set sreg2 */
1414 assign_reg (cfg, rs, sregs [j], dest_sreg, 0);
1417 sregs [j] = dest_sreg;
1420 mono_inst_set_src_registers (ins, sregs);
1425 bank = dreg_bank (spec);
1426 if (spec_dest && is_soft_reg (ins->dreg, bank)) {
1427 prev_dreg = ins->dreg;
1430 if (spec_dest == 'b') {
1432 * The dest reg is read by the instruction, not written, so
1433 * avoid allocating sreg1/sreg2 to the same reg.
1435 if (dest_sregs [0] != -1)
1436 dreg_mask &= ~ (regmask (dest_sregs [0]));
1437 for (j = 1; j < num_sregs; ++j) {
1438 if (dest_sregs [j] != -1)
1439 dreg_mask &= ~ (regmask (dest_sregs [j]));
1442 val = rs->vassign [ins->dreg];
1443 if (is_soft_reg (ins->dreg, bank) && (val >= 0) && (!(regmask (val) & dreg_mask))) {
1444 /* DREG is already allocated to a register needed for sreg1 */
1445 get_register_force_spilling (cfg, bb, tmp, ins, ins->dreg, 0);
1446 mono_regstate_free_int (rs, val);
1451 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1452 * various complex situations.
1454 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1455 guint32 dreg2, dest_dreg2;
1457 g_assert (is_soft_reg (ins->dreg, bank));
1459 if (dest_dreg != -1) {
1460 if (rs->vassign [ins->dreg] != dest_dreg)
1461 free_up_reg (cfg, bb, tmp, ins, dest_dreg, 0);
1463 dreg2 = ins->dreg + 1;
1464 dest_dreg2 = MONO_ARCH_INST_REGPAIR_REG2 (spec_dest, dest_dreg);
1465 if (dest_dreg2 != -1) {
1466 if (rs->vassign [dreg2] != dest_dreg2)
1467 free_up_reg (cfg, bb, tmp, ins, dest_dreg2, 0);
1472 if (dreg_fixed_mask) {
1474 if (is_global_ireg (ins->dreg)) {
1476 * The argument is already in a hard reg, but that reg is
1477 * not usable by this instruction, so allocate a new one.
1479 val = mono_regstate_alloc_int (rs, dreg_fixed_mask);
1481 val = get_register_spilling (cfg, bb, tmp, ins, dreg_fixed_mask, -1, bank);
1482 mono_regstate_free_int (rs, val);
1488 dreg_mask &= dreg_fixed_mask;
1491 if (is_soft_reg (ins->dreg, bank)) {
1492 val = rs->vassign [ins->dreg];
1497 /* the register gets spilled after this inst */
1500 val = alloc_reg (cfg, bb, tmp, ins, dreg_mask, ins->dreg, ®info [ins->dreg], bank);
1501 assign_reg (cfg, rs, ins->dreg, val, bank);
1503 create_spilled_store (cfg, bb, spill, val, prev_dreg, tmp, ins, bank);
1506 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val, bank), ins->dreg));
1510 /* Handle regpairs */
1511 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest)) {
1512 int reg2 = prev_dreg + 1;
1515 g_assert (prev_dreg > -1);
1516 g_assert (!is_global_ireg (rs->vassign [prev_dreg]));
1517 mask = regpair_reg2_mask (spec_dest, rs->vassign [prev_dreg]);
1520 mask &= ~regmask (X86_ECX);
1522 val = rs->vassign [reg2];
1526 /* the register gets spilled after this inst */
1529 val = mono_regstate_alloc_int (rs, mask);
1531 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1533 create_spilled_store (cfg, bb, spill, val, reg2, tmp, ins, bank);
1536 if (! (mask & (regmask (val)))) {
1537 val = mono_regstate_alloc_int (rs, mask);
1539 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1541 /* Reallocate hreg to the correct register */
1542 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1544 mono_regstate_free_int (rs, rs->vassign [reg2]);
1548 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val), reg2));
1549 assign_reg (cfg, rs, reg2, val, bank);
1552 ins->backend.reg3 = val;
1554 if (reg_is_freeable (val, bank) && reg2 >= 0 && (reginfo [reg2].born_in >= i)) {
1555 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val), reg2));
1556 mono_regstate_free_int (rs, val);
1560 if (prev_dreg >= 0 && is_soft_reg (prev_dreg, bank) && (spec_dest != 'b')) {
1562 * In theory, we could free up the hreg even if the vreg is alive,
1563 * but branches inside bblocks force us to assign the same hreg
1564 * to a vreg every time it is encountered.
1566 int dreg = rs->vassign [prev_dreg];
1567 g_assert (dreg >= 0);
1568 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg, bank), prev_dreg, reginfo [prev_dreg].born_in));
1569 if (G_UNLIKELY (bank))
1570 mono_regstate_free_general (rs, dreg, bank);
1572 mono_regstate_free_int (rs, dreg);
1573 rs->vassign [prev_dreg] = -1;
1576 if ((dest_dreg != -1) && (ins->dreg != dest_dreg)) {
1577 /* this instruction only outputs to dest_dreg, need to copy */
1578 create_copy_ins (cfg, bb, tmp, ins->dreg, dest_dreg, ins, ip, bank);
1579 ins->dreg = dest_dreg;
1581 if (G_UNLIKELY (bank)) {
1582 /* the register we need to free up may be used in another logical regbank
1583 * so do a translate just in case.
1585 int translated_bank = translate_bank (cfg->rs, bank, dest_dreg);
1586 if (rs->symbolic [translated_bank] [dest_dreg] >= regbank_size [translated_bank])
1587 free_up_reg (cfg, bb, tmp, ins, dest_dreg, translated_bank);
1590 if (rs->isymbolic [dest_dreg] >= MONO_MAX_IREGS)
1591 free_up_reg (cfg, bb, tmp, ins, dest_dreg, bank);
1595 if (spec_dest == 'b') {
1597 * The dest reg is read by the instruction, not written, so
1598 * avoid allocating sreg1/sreg2 to the same reg.
1600 for (j = 0; j < num_sregs; ++j)
1601 if (!sreg_bank (j, spec))
1602 sreg_masks [j] &= ~ (regmask (ins->dreg));
1608 if ((clob_reg != -1) && (!(rs->ifree_mask & (regmask (clob_reg))))) {
1609 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs->isymbolic [clob_reg]));
1610 get_register_force_spilling (cfg, bb, tmp, ins, rs->isymbolic [clob_reg], 0);
1611 mono_regstate_free_int (rs, clob_reg);
1614 if (spec [MONO_INST_CLOB] == 'c') {
1615 int j, s, dreg, dreg2, cur_bank;
1618 clob_mask = MONO_ARCH_CALLEE_REGS;
1620 if (rs->ifree_mask != MONO_ARCH_CALLEE_REGS) {
1622 * Need to avoid spilling the dreg since the dreg is not really
1623 * clobbered by the call.
1625 if ((prev_dreg != -1) && !reg_bank (spec_dest))
1626 dreg = rs->vassign [prev_dreg];
1630 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest))
1631 dreg2 = rs->vassign [prev_dreg + 1];
1635 for (j = 0; j < MONO_MAX_IREGS; ++j) {
1637 if ((clob_mask & s) && !(rs->ifree_mask & s) && (j != ins->sreg1)) {
1638 if ((j != dreg) && (j != dreg2))
1639 get_register_force_spilling (cfg, bb, tmp, ins, rs->isymbolic [j], 0);
1640 else if (rs->isymbolic [j])
1641 /* The hreg is assigned to the dreg of this instruction */
1642 rs->vassign [rs->isymbolic [j]] = -1;
1643 mono_regstate_free_int (rs, j);
1648 for (cur_bank = 1; cur_bank < MONO_NUM_REGBANKS; ++ cur_bank) {
1649 if (rs->free_mask [cur_bank] != regbank_callee_regs [cur_bank]) {
1650 clob_mask = regbank_callee_regs [cur_bank];
1651 if ((prev_dreg != -1) && reg_bank (spec_dest))
1652 dreg = rs->vassign [prev_dreg];
1656 for (j = 0; j < regbank_size [cur_bank]; ++j) {
1658 /* we are looping though the banks in the outer loop
1659 * so, we don't need to deal with mirrored hregs
1660 * because we will get them in one of the other bank passes.
1662 if (is_hreg_mirrored (rs, cur_bank, j))
1666 if ((clob_mask & s) && !(rs->free_mask [cur_bank] & s) && (j != ins->sreg1)) {
1668 get_register_force_spilling (cfg, bb, tmp, ins, rs->symbolic [cur_bank] [j], cur_bank);
1669 else if (rs->symbolic [cur_bank] [j])
1670 /* The hreg is assigned to the dreg of this instruction */
1671 rs->vassign [rs->symbolic [cur_bank] [j]] = -1;
1672 mono_regstate_free_general (rs, j, cur_bank);
1680 * TRACK ARGUMENT REGS
1682 if (spec [MONO_INST_CLOB] == 'c') {
1683 MonoCallInst *call = (MonoCallInst*)ins;
1687 * This needs to be done before assigning sreg1, so sreg1 will
1688 * not be assigned one of the argument regs.
1692 * Assign all registers in call->out_reg_args to the proper
1693 * argument registers.
1696 list = call->out_ireg_args;
1702 regpair = (guint32)(gssize)(list->data);
1703 hreg = regpair >> 24;
1704 reg = regpair & 0xffffff;
1706 assign_reg (cfg, rs, reg, hreg, 0);
1708 sreg_masks [0] &= ~(regmask (hreg));
1710 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg), reg));
1712 list = g_slist_next (list);
1716 list = call->out_freg_args;
1722 regpair = (guint32)(gssize)(list->data);
1723 hreg = regpair >> 24;
1724 reg = regpair & 0xffffff;
1726 assign_reg (cfg, rs, reg, hreg, 1);
1728 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg, 1), reg));
1730 list = g_slist_next (list);
1738 bank = sreg1_bank (spec);
1739 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest) && (spec [MONO_INST_CLOB] == '1')) {
1740 int sreg1 = sregs [0];
1741 int dest_sreg1 = dest_sregs [0];
1743 g_assert (is_soft_reg (sreg1, bank));
1745 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1746 if (dest_sreg1 != -1)
1747 g_assert (dest_sreg1 == ins->dreg);
1748 val = mono_regstate_alloc_int (rs, regmask (ins->dreg));
1749 g_assert (val >= 0);
1751 if (rs->vassign [sreg1] >= 0 && rs->vassign [sreg1] != val)
1753 g_assert_not_reached ();
1755 assign_reg (cfg, rs, sreg1, val, bank);
1757 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val, bank), sreg1));
1759 g_assert ((regmask (dreg_high)) & regpair_reg2_mask (spec_src1, ins->dreg));
1760 val = mono_regstate_alloc_int (rs, regmask (dreg_high));
1761 g_assert (val >= 0);
1763 if (rs->vassign [sreg1 + 1] >= 0 && rs->vassign [sreg1 + 1] != val)
1765 g_assert_not_reached ();
1767 assign_reg (cfg, rs, sreg1 + 1, val, bank);
1769 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val, bank), sreg1 + 1));
1771 /* Skip rest of this section */
1772 dest_sregs [0] = -1;
1775 if (sreg_fixed_masks [0]) {
1777 if (is_global_ireg (sregs [0])) {
1779 * The argument is already in a hard reg, but that reg is
1780 * not usable by this instruction, so allocate a new one.
1782 val = mono_regstate_alloc_int (rs, sreg_fixed_masks [0]);
1784 val = get_register_spilling (cfg, bb, tmp, ins, sreg_fixed_masks [0], -1, bank);
1785 mono_regstate_free_int (rs, val);
1786 dest_sregs [0] = val;
1788 /* Fall through to the dest_sreg1 != -1 case */
1791 sreg_masks [0] &= sreg_fixed_masks [0];
1794 if (dest_sregs [0] != -1) {
1795 sreg_masks [0] = regmask (dest_sregs [0]);
1797 if ((rs->vassign [sregs [0]] != dest_sregs [0]) && !(rs->ifree_mask & (regmask (dest_sregs [0])))) {
1798 DEBUG (printf ("\tforced spill of R%d\n", rs->isymbolic [dest_sregs [0]]));
1799 get_register_force_spilling (cfg, bb, tmp, ins, rs->isymbolic [dest_sregs [0]], 0);
1800 mono_regstate_free_int (rs, dest_sregs [0]);
1802 if (is_global_ireg (sregs [0])) {
1803 /* The argument is already in a hard reg, need to copy */
1804 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], sregs [0], NULL, ip, 0);
1805 insert_before_ins (bb, ins, copy);
1806 sregs [0] = dest_sregs [0];
1810 if (is_soft_reg (sregs [0], bank)) {
1811 val = rs->vassign [sregs [0]];
1812 prev_sregs [0] = sregs [0];
1816 /* the register gets spilled after this inst */
1820 if ((ins->opcode == OP_MOVE) && !spill && !bank && is_local_ireg (ins->dreg) && (rs->ifree_mask & (regmask (ins->dreg)))) {
1822 * Allocate the same hreg to sreg1 as well so the
1823 * peephole can get rid of the move.
1825 sreg_masks [0] = regmask (ins->dreg);
1828 if (spec [MONO_INST_CLOB] == '1' && !dreg_bank (spec) && (rs->ifree_mask & (regmask (ins->dreg))))
1829 /* Allocate the same reg to sreg1 to avoid a copy later */
1830 sreg_masks [0] = regmask (ins->dreg);
1832 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [0], sregs [0], ®info [sregs [0]], bank);
1833 assign_reg (cfg, rs, sregs [0], val, bank);
1834 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val, bank), sregs [0]));
1837 MonoInst *store = create_spilled_store (cfg, bb, spill, val, prev_sregs [0], tmp, NULL, bank);
1839 * Need to insert before the instruction since it can
1842 insert_before_ins (bb, ins, store);
1845 else if ((dest_sregs [0] != -1) && (dest_sregs [0] != val)) {
1846 MonoInst *copy = create_copy_ins (cfg, bb, tmp, dest_sregs [0], val, NULL, ip, bank);
1847 insert_before_ins (bb, ins, copy);
1848 for (j = 1; j < num_sregs; ++j)
1849 sreg_masks [j] &= ~(regmask (dest_sregs [0]));
1850 val = dest_sregs [0];
1856 prev_sregs [0] = -1;
1858 mono_inst_set_src_registers (ins, sregs);
1860 for (j = 1; j < num_sregs; ++j)
1861 sreg_masks [j] &= ~(regmask (sregs [0]));
1863 /* Handle the case when sreg1 is a regpair but dreg is not */
1864 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1) && (spec [MONO_INST_CLOB] != '1')) {
1865 int reg2 = prev_sregs [0] + 1;
1868 g_assert (prev_sregs [0] > -1);
1869 g_assert (!is_global_ireg (rs->vassign [prev_sregs [0]]));
1870 mask = regpair_reg2_mask (spec_src1, rs->vassign [prev_sregs [0]]);
1871 val = rs->vassign [reg2];
1875 /* the register gets spilled after this inst */
1878 val = mono_regstate_alloc_int (rs, mask);
1880 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1882 g_assert_not_reached ();
1885 if (! (mask & (regmask (val)))) {
1886 /* The vreg is already allocated to a wrong hreg */
1888 g_assert_not_reached ();
1890 val = mono_regstate_alloc_int (rs, mask);
1892 val = get_register_spilling (cfg, bb, tmp, ins, mask, reg2, bank);
1894 /* Reallocate hreg to the correct register */
1895 create_copy_ins (cfg, bb, tmp, rs->vassign [reg2], val, ins, ip, bank);
1897 mono_regstate_free_int (rs, rs->vassign [reg2]);
1903 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val), reg2));
1904 assign_reg (cfg, rs, reg2, val, bank);
1907 /* Handle dreg==sreg1 */
1908 if (((dreg_is_fp (spec) && sreg1_is_fp (spec)) || spec [MONO_INST_CLOB] == '1') && ins->dreg != sregs [0]) {
1909 MonoInst *sreg2_copy = NULL;
1911 int bank = reg_bank (spec_src1);
1913 if (ins->dreg == sregs [1]) {
1915 * copying sreg1 to dreg could clobber sreg2, so allocate a new
1918 int reg2 = alloc_reg (cfg, bb, tmp, ins, dreg_mask, sregs [1], NULL, bank);
1920 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs [1], bank), mono_regname_full (reg2, bank)));
1921 sreg2_copy = create_copy_ins (cfg, bb, tmp, reg2, sregs [1], NULL, ip, bank);
1922 prev_sregs [1] = sregs [1] = reg2;
1924 if (G_UNLIKELY (bank))
1925 mono_regstate_free_general (rs, reg2, bank);
1927 mono_regstate_free_int (rs, reg2);
1930 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1)) {
1931 /* Copying sreg1_high to dreg could also clobber sreg2 */
1932 if (rs->vassign [prev_sregs [0] + 1] == sregs [1])
1934 g_assert_not_reached ();
1937 * sreg1 and dest are already allocated to the same regpair by the
1938 * SREG1 allocation code.
1940 g_assert (sregs [0] == ins->dreg);
1941 g_assert (dreg_high == sreg1_high);
1944 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs [0], bank), mono_regname_full (ins->dreg, bank)));
1945 copy = create_copy_ins (cfg, bb, tmp, ins->dreg, sregs [0], NULL, ip, bank);
1946 insert_before_ins (bb, ins, copy);
1949 insert_before_ins (bb, copy, sreg2_copy);
1952 * Need to prevent sreg2 to be allocated to sreg1, since that
1953 * would screw up the previous copy.
1955 sreg_masks [1] &= ~ (regmask (sregs [0]));
1956 /* we set sreg1 to dest as well */
1957 prev_sregs [0] = sregs [0] = ins->dreg;
1958 sreg_masks [1] &= ~ (regmask (ins->dreg));
1960 mono_inst_set_src_registers (ins, sregs);
1963 * TRACK SREG2, 3, ...
1965 for (j = 1; j < num_sregs; ++j) {
1968 bank = sreg_bank (j, spec);
1969 if (MONO_ARCH_INST_IS_REGPAIR (spec [MONO_INST_SRC1 + j]))
1970 g_assert_not_reached ();
1971 if (is_soft_reg (sregs [j], bank)) {
1972 val = rs->vassign [sregs [j]];
1977 /* the register gets spilled after this inst */
1980 val = alloc_reg (cfg, bb, tmp, ins, sreg_masks [j], sregs [j], ®info [sregs [j]], bank);
1981 assign_reg (cfg, rs, sregs [j], val, bank);
1982 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j + 1, mono_regname_full (val, bank), sregs [j]));
1984 MonoInst *store = create_spilled_store (cfg, bb, spill, val, prev_sregs [j], tmp, NULL, bank);
1986 * Need to insert before the instruction since it can
1989 insert_before_ins (bb, ins, store);
1993 for (k = j + 1; k < num_sregs; ++k)
1994 sreg_masks [k] &= ~ (regmask (sregs [j]));
1997 prev_sregs [j] = -1;
2000 mono_inst_set_src_registers (ins, sregs);
2002 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2003 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2004 mono_regstate_free_int (rs, ins->sreg1);
2006 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2007 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2008 mono_regstate_free_int (rs, ins->sreg2);
2011 DEBUG (mono_print_ins_index (i, ins));
2014 // FIXME: Set MAX_FREGS to 8
2015 // FIXME: Optimize generated code
2016 #if MONO_ARCH_USE_FPSTACK
2018 * Make a forward pass over the code, simulating the fp stack, making sure the
2019 * arguments required by the fp opcodes are at the top of the stack.
2022 MonoInst *prev = NULL;
2026 g_assert (num_sregs <= 2);
2028 for (ins = bb->code; ins; ins = ins->next) {
2029 spec = ins_get_spec (ins->opcode);
2031 DEBUG (printf ("processing:"));
2032 DEBUG (mono_print_ins_index (0, ins));
2034 if (ins->opcode == OP_FMOVE) {
2035 /* Do it by renaming the source to the destination on the stack */
2036 // FIXME: Is this correct ?
2037 for (i = 0; i < sp; ++i)
2038 if (fpstack [i] == ins->sreg1)
2039 fpstack [i] = ins->dreg;
2044 if (sreg1_is_fp (spec) && sreg2_is_fp (spec) && (fpstack [sp - 2] != ins->sreg1)) {
2045 /* Arg1 must be in %st(1) */
2049 while ((i < sp) && (fpstack [i] != ins->sreg1))
2053 if (sp - 1 - i > 0) {
2054 /* First move it to %st(0) */
2055 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2057 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2058 fxch->inst_imm = sp - 1 - i;
2064 tmp = fpstack [sp - 1];
2065 fpstack [sp - 1] = fpstack [i];
2069 /* Then move it to %st(1) */
2070 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2072 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2079 tmp = fpstack [sp - 1];
2080 fpstack [sp - 1] = fpstack [sp - 2];
2081 fpstack [sp - 2] = tmp;
2084 if (sreg2_is_fp (spec)) {
2087 if (fpstack [sp - 1] != ins->sreg2) {
2091 while ((i < sp) && (fpstack [i] != ins->sreg2))
2095 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2097 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2098 fxch->inst_imm = sp - 1 - i;
2104 tmp = fpstack [sp - 1];
2105 fpstack [sp - 1] = fpstack [i];
2112 if (sreg1_is_fp (spec)) {
2115 if (fpstack [sp - 1] != ins->sreg1) {
2119 while ((i < sp) && (fpstack [i] != ins->sreg1))
2123 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp - 1 - i));
2125 MONO_INST_NEW (cfg, fxch, OP_X86_FXCH);
2126 fxch->inst_imm = sp - 1 - i;
2132 tmp = fpstack [sp - 1];
2133 fpstack [sp - 1] = fpstack [i];
2140 if (dreg_is_fp (spec)) {
2142 fpstack [sp ++] = ins->dreg;
2145 if (G_UNLIKELY (cfg->verbose_level >= 2)) {
2147 for (i = 0; i < sp; ++i)
2148 printf ("%s%%fr%d", (i > 0) ? ", " : "", fpstack [i]);
2155 if (sp && bb != cfg->bb_exit && !(bb->out_count == 1 && bb->out_bb [0] == cfg->bb_exit)) {
2156 /* Remove remaining items from the fp stack */
2158 * These can remain for example as a result of a dead fmove like in
2159 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2162 MONO_INST_NEW (cfg, ins, OP_X86_FPOP);
2163 mono_add_ins_to_end (bb, ins);
2172 mono_opcode_to_cond (int opcode)
2183 case OP_COND_EXC_EQ:
2184 case OP_COND_EXC_IEQ:
2192 case OP_COND_EXC_NE_UN:
2193 case OP_COND_EXC_INE_UN:
2194 case OP_CMOV_INE_UN:
2195 case OP_CMOV_LNE_UN:
2219 case OP_COND_EXC_LT:
2220 case OP_COND_EXC_ILT:
2232 case OP_COND_EXC_GT:
2233 case OP_COND_EXC_IGT:
2242 case OP_COND_EXC_LE_UN:
2243 case OP_COND_EXC_ILE_UN:
2244 case OP_CMOV_ILE_UN:
2245 case OP_CMOV_LLE_UN:
2251 case OP_CMOV_IGE_UN:
2252 case OP_CMOV_LGE_UN:
2262 case OP_COND_EXC_LT_UN:
2263 case OP_COND_EXC_ILT_UN:
2264 case OP_CMOV_ILT_UN:
2265 case OP_CMOV_LLT_UN:
2275 case OP_COND_EXC_GT_UN:
2276 case OP_COND_EXC_IGT_UN:
2277 case OP_CMOV_IGT_UN:
2278 case OP_CMOV_LGT_UN:
2281 printf ("%s\n", mono_inst_name (opcode));
2282 g_assert_not_reached ();
2288 mono_negate_cond (CompRelation cond)
2312 g_assert_not_reached ();
2317 mono_opcode_to_type (int opcode, int cmp_opcode)
2319 if ((opcode >= CEE_BEQ) && (opcode <= CEE_BLT_UN))
2321 else if ((opcode >= OP_CEQ) && (opcode <= OP_CLT_UN))
2323 else if ((opcode >= OP_IBEQ) && (opcode <= OP_IBLT_UN))
2325 else if ((opcode >= OP_ICEQ) && (opcode <= OP_ICLT_UN))
2327 else if ((opcode >= OP_LBEQ) && (opcode <= OP_LBLT_UN))
2329 else if ((opcode >= OP_LCEQ) && (opcode <= OP_LCLT_UN))
2331 else if ((opcode >= OP_FBEQ) && (opcode <= OP_FBLT_UN))
2333 else if ((opcode >= OP_FCEQ) && (opcode <= OP_FCLT_UN))
2335 else if ((opcode >= OP_COND_EXC_IEQ) && (opcode <= OP_COND_EXC_ILT_UN))
2337 else if ((opcode >= OP_COND_EXC_EQ) && (opcode <= OP_COND_EXC_LT_UN)) {
2338 switch (cmp_opcode) {
2340 case OP_ICOMPARE_IMM:
2341 case OP_LCOMPARE_IMM:
2347 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode));
2353 mono_is_regsize_var (MonoType *t)
2357 t = mono_type_get_underlying_type (t);
2359 case MONO_TYPE_BOOLEAN:
2360 case MONO_TYPE_CHAR:
2370 case MONO_TYPE_FNPTR:
2371 #if SIZEOF_REGISTER == 8
2376 case MONO_TYPE_OBJECT:
2377 case MONO_TYPE_STRING:
2378 case MONO_TYPE_CLASS:
2379 case MONO_TYPE_SZARRAY:
2380 case MONO_TYPE_ARRAY:
2382 case MONO_TYPE_GENERICINST:
2383 if (!mono_type_generic_inst_is_valuetype (t))
2386 case MONO_TYPE_VALUETYPE:
2393 * mono_peephole_ins:
2395 * Perform some architecture independent peephole optimizations.
2398 mono_peephole_ins (MonoBasicBlock *bb, MonoInst *ins)
2400 MonoInst *last_ins = ins->prev;
2402 switch (ins->opcode) {
2404 /* remove unnecessary multiplication with 1 */
2405 if (ins->inst_imm == 1) {
2406 if (ins->dreg != ins->sreg1)
2407 ins->opcode = OP_MOVE;
2409 MONO_DELETE_INS (bb, ins);
2412 case OP_LOAD_MEMBASE:
2413 case OP_LOADI4_MEMBASE:
2415 * Note: if reg1 = reg2 the load op is removed
2417 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2418 * OP_LOAD_MEMBASE offset(basereg), reg2
2420 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2421 * OP_MOVE reg1, reg2
2424 (((ins->opcode == OP_LOADI4_MEMBASE) && (last_ins->opcode == OP_STOREI4_MEMBASE_REG)) ||
2425 ((ins->opcode == OP_LOAD_MEMBASE) && (last_ins->opcode == OP_STORE_MEMBASE_REG))) &&
2426 ins->inst_basereg == last_ins->inst_destbasereg &&
2427 ins->inst_offset == last_ins->inst_offset) {
2428 if (ins->dreg == last_ins->sreg1) {
2429 MONO_DELETE_INS (bb, ins);
2432 ins->opcode = OP_MOVE;
2433 ins->sreg1 = last_ins->sreg1;
2437 * Note: reg1 must be different from the basereg in the second load
2438 * Note: if reg1 = reg2 is equal then second load is removed
2440 * OP_LOAD_MEMBASE offset(basereg), reg1
2441 * OP_LOAD_MEMBASE offset(basereg), reg2
2443 * OP_LOAD_MEMBASE offset(basereg), reg1
2444 * OP_MOVE reg1, reg2
2446 } if (last_ins && (last_ins->opcode == OP_LOADI4_MEMBASE
2447 || last_ins->opcode == OP_LOAD_MEMBASE) &&
2448 ins->inst_basereg != last_ins->dreg &&
2449 ins->inst_basereg == last_ins->inst_basereg &&
2450 ins->inst_offset == last_ins->inst_offset) {
2452 if (ins->dreg == last_ins->dreg) {
2453 MONO_DELETE_INS (bb, ins);
2455 ins->opcode = OP_MOVE;
2456 ins->sreg1 = last_ins->dreg;
2459 //g_assert_not_reached ();
2463 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2464 * OP_LOAD_MEMBASE offset(basereg), reg
2466 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2467 * OP_ICONST reg, imm
2469 } else if (last_ins && (last_ins->opcode == OP_STOREI4_MEMBASE_IMM
2470 || last_ins->opcode == OP_STORE_MEMBASE_IMM) &&
2471 ins->inst_basereg == last_ins->inst_destbasereg &&
2472 ins->inst_offset == last_ins->inst_offset) {
2473 ins->opcode = OP_ICONST;
2474 ins->inst_c0 = last_ins->inst_imm;
2475 g_assert_not_reached (); // check this rule
2479 case OP_LOADI1_MEMBASE:
2480 case OP_LOADU1_MEMBASE:
2482 * Note: if reg1 = reg2 the load op is removed
2484 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2485 * OP_LOAD_MEMBASE offset(basereg), reg2
2487 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2488 * OP_MOVE reg1, reg2
2490 if (last_ins && (last_ins->opcode == OP_STOREI1_MEMBASE_REG) &&
2491 ins->inst_basereg == last_ins->inst_destbasereg &&
2492 ins->inst_offset == last_ins->inst_offset) {
2493 ins->opcode = (ins->opcode == OP_LOADI1_MEMBASE) ? OP_PCONV_TO_I1 : OP_PCONV_TO_U1;
2494 ins->sreg1 = last_ins->sreg1;
2497 case OP_LOADI2_MEMBASE:
2498 case OP_LOADU2_MEMBASE:
2500 * Note: if reg1 = reg2 the load op is removed
2502 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2503 * OP_LOAD_MEMBASE offset(basereg), reg2
2505 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2506 * OP_MOVE reg1, reg2
2508 if (last_ins && (last_ins->opcode == OP_STOREI2_MEMBASE_REG) &&
2509 ins->inst_basereg == last_ins->inst_destbasereg &&
2510 ins->inst_offset == last_ins->inst_offset) {
2511 #if SIZEOF_REGISTER == 8
2512 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_PCONV_TO_U2;
2514 /* The definition of OP_PCONV_TO_U2 is wrong */
2515 ins->opcode = (ins->opcode == OP_LOADI2_MEMBASE) ? OP_PCONV_TO_I2 : OP_ICONV_TO_U2;
2517 ins->sreg1 = last_ins->sreg1;
2527 if (ins->dreg == ins->sreg1) {
2528 MONO_DELETE_INS (bb, ins);
2534 * OP_MOVE sreg, dreg
2535 * OP_MOVE dreg, sreg
2537 if (last_ins && last_ins->opcode == OP_MOVE &&
2538 ins->sreg1 == last_ins->dreg &&
2539 ins->dreg == last_ins->sreg1) {
2540 MONO_DELETE_INS (bb, ins);
2544 MONO_DELETE_INS (bb, ins);