3 * Arch independent code generation functionality
5 * (C) 2003 Ximian, Inc.
16 #include <mono/metadata/appdomain.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/profiler-private.h>
20 #include <mono/metadata/mempool-internals.h>
21 #include <mono/utils/mono-math.h>
24 #include "mini-runtime.h"
26 #include "mini-arch.h"
30 #ifndef MONO_MAX_XREGS
32 #define MONO_MAX_XREGS 0
33 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
34 #define MONO_ARCH_CALLEE_XREGS 0
38 #define MONO_ARCH_BANK_MIRRORED -2
40 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
42 #ifndef MONO_ARCH_NEED_SIMD_BANK
43 #error "MONO_ARCH_USE_SHARED_FP_SIMD_BANK needs MONO_ARCH_NEED_SIMD_BANK to work"
46 #define get_mirrored_bank(bank) (((bank) == MONO_REG_SIMD ) ? MONO_REG_DOUBLE : (((bank) == MONO_REG_DOUBLE ) ? MONO_REG_SIMD : -1))
48 #define is_hreg_mirrored(rs, bank, hreg) ((rs)->symbolic [(bank)] [(hreg)] == MONO_ARCH_BANK_MIRRORED)
54 #define get_mirrored_bank(bank) (-1)
56 #define is_hreg_mirrored(rs, bank, hreg) (0)
61 /* If the bank is mirrored return the true logical bank that the register in the
62 * physical register bank is allocated to.
64 static inline int translate_bank (MonoRegState
*rs
, int bank
, int hreg
) {
65 return is_hreg_mirrored (rs
, bank
, hreg
) ? get_mirrored_bank (bank
) : bank
;
69 * Every hardware register belongs to a register type or register bank. bank 0
70 * contains the int registers, bank 1 contains the fp registers.
71 * int registers are used 99% of the time, so they are special cased in a lot of
75 static const int regbank_size
[] = {
83 static const int regbank_load_ops
[] = {
91 static const int regbank_store_ops
[] = {
92 OP_STORER_MEMBASE_REG
,
93 OP_STORER8_MEMBASE_REG
,
94 OP_STORER_MEMBASE_REG
,
95 OP_STORER_MEMBASE_REG
,
99 static const int regbank_move_ops
[] = {
107 #define regmask(reg) (((regmask_t)1) << (reg))
109 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
110 static const regmask_t regbank_callee_saved_regs
[] = {
111 MONO_ARCH_CALLEE_SAVED_REGS
,
112 MONO_ARCH_CALLEE_SAVED_FREGS
,
113 MONO_ARCH_CALLEE_SAVED_REGS
,
114 MONO_ARCH_CALLEE_SAVED_REGS
,
115 MONO_ARCH_CALLEE_SAVED_XREGS
,
119 static const regmask_t regbank_callee_regs
[] = {
120 MONO_ARCH_CALLEE_REGS
,
121 MONO_ARCH_CALLEE_FREGS
,
122 MONO_ARCH_CALLEE_REGS
,
123 MONO_ARCH_CALLEE_REGS
,
124 MONO_ARCH_CALLEE_XREGS
,
127 static const int regbank_spill_var_size
[] = {
128 sizeof (target_mgreg_t
),
130 sizeof (target_mgreg_t
),
131 sizeof (target_mgreg_t
),
132 16 /*FIXME make this a constant. Maybe MONO_ARCH_SIMD_VECTOR_SIZE? */
135 #define DEBUG(a) MINI_DEBUG(cfg->verbose_level, 3, a;)
138 mono_regstate_assign (MonoRegState
*rs
)
140 #ifdef MONO_ARCH_USE_SHARED_FP_SIMD_BANK
141 /* The regalloc may fail if fp and simd logical regbanks share the same physical reg bank and
142 * if the values here are not the same.
144 g_assert(regbank_callee_regs
[MONO_REG_SIMD
] == regbank_callee_regs
[MONO_REG_DOUBLE
]);
145 g_assert(regbank_callee_saved_regs
[MONO_REG_SIMD
] == regbank_callee_saved_regs
[MONO_REG_DOUBLE
]);
146 g_assert(regbank_size
[MONO_REG_SIMD
] == regbank_size
[MONO_REG_DOUBLE
]);
149 if (rs
->next_vreg
> rs
->vassign_size
) {
150 g_free (rs
->vassign
);
151 rs
->vassign_size
= MAX (rs
->next_vreg
, 256);
152 rs
->vassign
= (gint32
*)g_malloc (rs
->vassign_size
* sizeof (gint32
));
155 memset (rs
->isymbolic
, 0, MONO_MAX_IREGS
* sizeof (rs
->isymbolic
[0]));
156 memset (rs
->fsymbolic
, 0, MONO_MAX_FREGS
* sizeof (rs
->fsymbolic
[0]));
158 rs
->symbolic
[MONO_REG_INT
] = rs
->isymbolic
;
159 rs
->symbolic
[MONO_REG_DOUBLE
] = rs
->fsymbolic
;
161 #ifdef MONO_ARCH_NEED_SIMD_BANK
162 memset (rs
->xsymbolic
, 0, MONO_MAX_XREGS
* sizeof (rs
->xsymbolic
[0]));
163 rs
->symbolic
[MONO_REG_SIMD
] = rs
->xsymbolic
;
168 mono_regstate_alloc_int (MonoRegState
*rs
, regmask_t allow
)
170 regmask_t mask
= allow
& rs
->ifree_mask
;
172 #if defined(__x86_64__) && defined(__GNUC__)
179 __asm__("bsfq %1,%0\n\t"
180 : "=r" (i
) : "rm" (mask
));
182 rs
->ifree_mask
&= ~ ((regmask_t
)1 << i
);
188 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
) {
189 if (mask
& ((regmask_t
)1 << i
)) {
190 rs
->ifree_mask
&= ~ ((regmask_t
)1 << i
);
199 mono_regstate_free_int (MonoRegState
*rs
, int reg
)
202 rs
->ifree_mask
|= (regmask_t
)1 << reg
;
203 rs
->isymbolic
[reg
] = 0;
208 mono_regstate_alloc_general (MonoRegState
*rs
, regmask_t allow
, int bank
)
212 regmask_t mask
= allow
& rs
->free_mask
[bank
];
213 for (i
= 0; i
< regbank_size
[bank
]; ++i
) {
214 if (mask
& ((regmask_t
)1 << i
)) {
215 rs
->free_mask
[bank
] &= ~ ((regmask_t
)1 << i
);
217 mirrored_bank
= get_mirrored_bank (bank
);
218 if (mirrored_bank
== -1)
221 rs
->free_mask
[mirrored_bank
] = rs
->free_mask
[bank
];
229 mono_regstate_free_general (MonoRegState
*rs
, int reg
, int bank
)
234 rs
->free_mask
[bank
] |= (regmask_t
)1 << reg
;
235 rs
->symbolic
[bank
][reg
] = 0;
237 mirrored_bank
= get_mirrored_bank (bank
);
238 if (mirrored_bank
== -1)
240 rs
->free_mask
[mirrored_bank
] = rs
->free_mask
[bank
];
241 rs
->symbolic
[mirrored_bank
][reg
] = 0;
246 mono_regname_full (int reg
, int bank
)
248 if (G_UNLIKELY (bank
)) {
249 #if MONO_ARCH_NEED_SIMD_BANK
250 if (bank
== MONO_REG_SIMD
)
251 return mono_arch_xregname (reg
);
253 if (bank
== MONO_REG_INT_REF
|| bank
== MONO_REG_INT_MP
)
254 return mono_arch_regname (reg
);
255 g_assert (bank
== MONO_REG_DOUBLE
);
256 return mono_arch_fregname (reg
);
258 return mono_arch_regname (reg
);
263 mono_call_inst_add_outarg_reg (MonoCompile
*cfg
, MonoCallInst
*call
, int vreg
, int hreg
, int bank
)
267 regpair
= (((guint32
)hreg
) << 24) + vreg
;
268 if (G_UNLIKELY (bank
)) {
269 g_assert (vreg
>= regbank_size
[bank
]);
270 g_assert (hreg
< regbank_size
[bank
]);
271 call
->used_fregs
|= (regmask_t
)1 << hreg
;
272 call
->out_freg_args
= g_slist_append_mempool (cfg
->mempool
, call
->out_freg_args
, (gpointer
)(gssize
)(regpair
));
274 g_assert (vreg
>= MONO_MAX_IREGS
);
275 g_assert (hreg
< MONO_MAX_IREGS
);
276 call
->used_iregs
|= (regmask_t
)1 << hreg
;
277 call
->out_ireg_args
= g_slist_append_mempool (cfg
->mempool
, call
->out_ireg_args
, (gpointer
)(gssize
)(regpair
));
282 * mono_call_inst_add_outarg_vt:
284 * Register OUTARG_VT as belonging to CALL.
287 mono_call_inst_add_outarg_vt (MonoCompile
*cfg
, MonoCallInst
*call
, MonoInst
*outarg_vt
)
289 call
->outarg_vts
= g_slist_append_mempool (cfg
->mempool
, call
->outarg_vts
, outarg_vt
);
293 resize_spill_info (MonoCompile
*cfg
, int bank
)
295 MonoSpillInfo
*orig_info
= cfg
->spill_info
[bank
];
296 int orig_len
= cfg
->spill_info_len
[bank
];
297 int new_len
= orig_len
? orig_len
* 2 : 16;
298 MonoSpillInfo
*new_info
;
301 g_assert (bank
< MONO_NUM_REGBANKS
);
303 new_info
= (MonoSpillInfo
*)mono_mempool_alloc0 (cfg
->mempool
, sizeof (MonoSpillInfo
) * new_len
);
305 memcpy (new_info
, orig_info
, sizeof (MonoSpillInfo
) * orig_len
);
306 for (i
= orig_len
; i
< new_len
; ++i
)
307 new_info
[i
].offset
= -1;
309 cfg
->spill_info
[bank
] = new_info
;
310 cfg
->spill_info_len
[bank
] = new_len
;
314 * returns the offset used by spillvar. It allocates a new
315 * spill variable if necessary.
318 mono_spillvar_offset (MonoCompile
*cfg
, int spillvar
, int bank
)
323 if (G_UNLIKELY (spillvar
>= (cfg
->spill_info_len
[bank
]))) {
324 while (spillvar
>= cfg
->spill_info_len
[bank
])
325 resize_spill_info (cfg
, bank
);
329 * Allocate separate spill slots for fp/non-fp variables since most processors prefer it.
331 info
= &cfg
->spill_info
[bank
][spillvar
];
332 if (info
->offset
== -1) {
333 cfg
->stack_offset
+= sizeof (target_mgreg_t
) - 1;
334 cfg
->stack_offset
&= ~(sizeof (target_mgreg_t
) - 1);
336 g_assert (bank
< MONO_NUM_REGBANKS
);
337 if (G_UNLIKELY (bank
))
338 size
= regbank_spill_var_size
[bank
];
340 size
= sizeof (target_mgreg_t
);
342 if (cfg
->flags
& MONO_CFG_HAS_SPILLUP
) {
343 cfg
->stack_offset
+= size
- 1;
344 cfg
->stack_offset
&= ~(size
- 1);
345 info
->offset
= cfg
->stack_offset
;
346 cfg
->stack_offset
+= size
;
348 cfg
->stack_offset
+= size
- 1;
349 cfg
->stack_offset
&= ~(size
- 1);
350 cfg
->stack_offset
+= size
;
351 info
->offset
= - cfg
->stack_offset
;
358 #define is_hard_ireg(r) ((r) >= 0 && (r) < MONO_MAX_IREGS)
359 #define is_hard_freg(r) ((r) >= 0 && (r) < MONO_MAX_FREGS)
360 #define is_global_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_SAVED_REGS & (regmask (r))))
361 #define is_local_ireg(r) (is_hard_ireg ((r)) && (MONO_ARCH_CALLEE_REGS & (regmask (r))))
362 #define is_global_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_SAVED_FREGS & (regmask (r))))
363 #define is_local_freg(r) (is_hard_freg ((r)) && (MONO_ARCH_CALLEE_FREGS & (regmask (r))))
365 #define is_hard_reg(r,bank) (G_UNLIKELY (bank) ? ((r) >= 0 && (r) < regbank_size [bank]) : ((r) < MONO_MAX_IREGS))
366 #define is_soft_reg(r,bank) (!is_hard_reg((r),(bank)))
367 #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))
368 #define is_local_reg(r,bank) (G_UNLIKELY (bank) ? (is_hard_reg ((r), (bank)) && (regbank_callee_regs [bank] & regmask (r))) : is_local_ireg (r))
369 #define reg_is_freeable(r,bank) (G_UNLIKELY (bank) ? is_local_reg ((r), (bank)) : is_local_ireg ((r)))
371 #ifndef MONO_ARCH_INST_IS_FLOAT
372 #define MONO_ARCH_INST_IS_FLOAT(desc) ((desc) == 'f')
375 #define reg_is_fp(desc) (MONO_ARCH_INST_IS_FLOAT (desc))
376 #define dreg_is_fp(spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_DEST]))
377 #define sreg_is_fp(n,spec) (MONO_ARCH_INST_IS_FLOAT (spec [MONO_INST_SRC1+(n)]))
378 #define sreg1_is_fp(spec) sreg_is_fp (0,(spec))
379 #define sreg2_is_fp(spec) sreg_is_fp (1,(spec))
381 #define reg_is_simd(desc) ((desc) == 'x')
383 #ifdef MONO_ARCH_NEED_SIMD_BANK
385 #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)
389 #define reg_bank(desc) reg_is_fp ((desc))
393 #define sreg_bank(n,spec) reg_bank ((spec)[MONO_INST_SRC1+(n)])
394 #define sreg1_bank(spec) sreg_bank (0, (spec))
395 #define sreg2_bank(spec) sreg_bank (1, (spec))
396 #define dreg_bank(spec) reg_bank ((spec)[MONO_INST_DEST])
398 #define sreg_bank_ins(n,ins) sreg_bank ((n), ins_get_spec ((ins)->opcode))
399 #define sreg1_bank_ins(ins) sreg_bank_ins (0, (ins))
400 #define sreg2_bank_ins(ins) sreg_bank_ins (1, (ins))
401 #define dreg_bank_ins(ins) dreg_bank (ins_get_spec ((ins)->opcode))
403 #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)
405 #ifdef MONO_ARCH_IS_GLOBAL_IREG
406 #undef is_global_ireg
407 #define is_global_ireg(reg) MONO_ARCH_IS_GLOBAL_IREG ((reg))
416 regmask_t preferred_mask
; /* the hreg where the register should be allocated, or 0 */
419 #if !defined(DISABLE_LOGGING)
422 mono_print_ins_index (int i
, MonoInst
*ins
)
424 GString
*buf
= mono_print_ins_index_strbuf (i
, ins
);
425 printf ("%s\n", buf
->str
);
426 g_string_free (buf
, TRUE
);
430 mono_print_ins_index_strbuf (int i
, MonoInst
*ins
)
432 const char *spec
= ins_get_spec (ins
->opcode
);
433 GString
*sbuf
= g_string_new (NULL
);
435 int sregs
[MONO_MAX_SRC_REGS
];
438 g_string_append_printf (sbuf
, "\t%-2d %s", i
, mono_inst_name (ins
->opcode
));
440 g_string_append_printf (sbuf
, " %s", mono_inst_name (ins
->opcode
));
441 if (spec
== (gpointer
)/*FIXME*/MONO_ARCH_CPU_SPEC
) {
442 gboolean dest_base
= FALSE
;
443 switch (ins
->opcode
) {
444 case OP_STOREV_MEMBASE
:
451 /* This is a lowered opcode */
452 if (ins
->dreg
!= -1) {
454 g_string_append_printf (sbuf
, " [R%d + 0x%lx] <-", ins
->dreg
, (long)ins
->inst_offset
);
456 g_string_append_printf (sbuf
, " R%d <-", ins
->dreg
);
458 if (ins
->sreg1
!= -1)
459 g_string_append_printf (sbuf
, " R%d", ins
->sreg1
);
460 if (ins
->sreg2
!= -1)
461 g_string_append_printf (sbuf
, " R%d", ins
->sreg2
);
462 if (ins
->sreg3
!= -1)
463 g_string_append_printf (sbuf
, " R%d", ins
->sreg3
);
465 switch (ins
->opcode
) {
476 if (!ins
->inst_false_bb
)
477 g_string_append_printf (sbuf
, " [B%d]", ins
->inst_true_bb
->block_num
);
479 g_string_append_printf (sbuf
, " [B%dB%d]", ins
->inst_true_bb
->block_num
, ins
->inst_false_bb
->block_num
);
486 g_string_append_printf (sbuf
, " [%d (", (int)ins
->inst_c0
);
487 for (i
= 0; i
< ins
->inst_phi_args
[0]; i
++) {
489 g_string_append_printf (sbuf
, ", ");
490 g_string_append_printf (sbuf
, "R%d", ins
->inst_phi_args
[i
+ 1]);
492 g_string_append_printf (sbuf
, ")]");
496 case OP_OUTARG_VTRETADDR
:
497 g_string_append_printf (sbuf
, " R%d", ((MonoInst
*)ins
->inst_p0
)->dreg
);
500 case OP_GSHAREDVT_ARG_REGOFFSET
:
501 g_string_append_printf (sbuf
, " + 0x%lx", (long)ins
->inst_offset
);
505 g_string_append_printf (sbuf
, " %s", m_class_get_name (ins
->klass
));
511 //g_error ("Unknown opcode: %s\n", mono_inst_name (ins->opcode));
515 if (spec
[MONO_INST_DEST
]) {
516 int bank
= dreg_bank (spec
);
517 if (is_soft_reg (ins
->dreg
, bank
)) {
518 if (spec
[MONO_INST_DEST
] == 'b') {
519 if (ins
->inst_offset
== 0)
520 g_string_append_printf (sbuf
, " [R%d] <-", ins
->dreg
);
522 g_string_append_printf (sbuf
, " [R%d + 0x%lx] <-", ins
->dreg
, (long)ins
->inst_offset
);
525 g_string_append_printf (sbuf
, " R%d <-", ins
->dreg
);
526 } else if (spec
[MONO_INST_DEST
] == 'b') {
527 if (ins
->inst_offset
== 0)
528 g_string_append_printf (sbuf
, " [%s] <-", mono_arch_regname (ins
->dreg
));
530 g_string_append_printf (sbuf
, " [%s + 0x%lx] <-", mono_arch_regname (ins
->dreg
), (long)ins
->inst_offset
);
532 g_string_append_printf (sbuf
, " %s <-", mono_regname_full (ins
->dreg
, bank
));
534 if (spec
[MONO_INST_SRC1
]) {
535 int bank
= sreg1_bank (spec
);
536 if (is_soft_reg (ins
->sreg1
, bank
)) {
537 if (spec
[MONO_INST_SRC1
] == 'b')
538 g_string_append_printf (sbuf
, " [R%d + 0x%lx]", ins
->sreg1
, (long)ins
->inst_offset
);
540 g_string_append_printf (sbuf
, " R%d", ins
->sreg1
);
541 } else if (spec
[MONO_INST_SRC1
] == 'b')
542 g_string_append_printf (sbuf
, " [%s + 0x%lx]", mono_arch_regname (ins
->sreg1
), (long)ins
->inst_offset
);
544 g_string_append_printf (sbuf
, " %s", mono_regname_full (ins
->sreg1
, bank
));
546 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
547 for (j
= 1; j
< num_sregs
; ++j
) {
548 int bank
= sreg_bank (j
, spec
);
549 if (is_soft_reg (sregs
[j
], bank
))
550 g_string_append_printf (sbuf
, " R%d", sregs
[j
]);
552 g_string_append_printf (sbuf
, " %s", mono_regname_full (sregs
[j
], bank
));
555 switch (ins
->opcode
) {
557 g_string_append_printf (sbuf
, " [%d]", (int)ins
->inst_c0
);
559 #if defined(TARGET_X86) || defined(TARGET_AMD64)
560 case OP_X86_PUSH_IMM
:
562 case OP_ICOMPARE_IMM
:
571 case OP_STORE_MEMBASE_IMM
:
572 g_string_append_printf (sbuf
, " [%d]", (int)ins
->inst_imm
);
576 g_string_append_printf (sbuf
, " [%d]", (int)(gssize
)ins
->inst_p1
);
579 g_string_append_printf (sbuf
, " [%lld]", (long long)ins
->inst_l
);
582 g_string_append_printf (sbuf
, " [%f]", *(double*)ins
->inst_p0
);
585 g_string_append_printf (sbuf
, " [%f]", *(float*)ins
->inst_p0
);
588 case OP_CALL_MEMBASE
:
594 case OP_VCALL_MEMBASE
:
597 case OP_VCALL2_MEMBASE
:
599 case OP_VOIDCALL_MEMBASE
:
601 case OP_TAILCALL_MEMBASE
:
604 case OP_RCALL_MEMBASE
: {
605 MonoCallInst
*call
= (MonoCallInst
*)ins
;
607 MonoJitICallId jit_icall_id
;
610 if (ins
->opcode
== OP_VCALL
|| ins
->opcode
== OP_VCALL_REG
|| ins
->opcode
== OP_VCALL_MEMBASE
) {
612 * These are lowered opcodes, but they are in the .md files since the old
613 * JIT passes them to backends.
616 g_string_append_printf (sbuf
, " R%d <-", ins
->dreg
);
619 if ((method
= call
->method
)) {
620 char *full_name
= mono_method_get_full_name (method
);
621 g_string_append_printf (sbuf
, " [%s]", full_name
);
623 } else if (call
->fptr_is_patch
) {
624 MonoJumpInfo
*ji
= (MonoJumpInfo
*)call
->fptr
;
626 g_string_append_printf (sbuf
, " ");
628 } else if ((jit_icall_id
= call
->jit_icall_id
)) {
629 g_string_append_printf (sbuf
, " [%s]", mono_find_jit_icall_info (jit_icall_id
)->name
);
632 list
= call
->out_ireg_args
;
637 regpair
= (guint32
)(gssize
)(list
->data
);
638 hreg
= regpair
>> 24;
639 reg
= regpair
& 0xffffff;
641 g_string_append_printf (sbuf
, " [%s <- R%d]", mono_arch_regname (hreg
), reg
);
643 list
= g_slist_next (list
);
645 list
= call
->out_freg_args
;
650 regpair
= (guint32
)(gssize
)(list
->data
);
651 hreg
= regpair
>> 24;
652 reg
= regpair
& 0xffffff;
654 g_string_append_printf (sbuf
, " [%s <- R%d]", mono_arch_fregname (hreg
), reg
);
656 list
= g_slist_next (list
);
661 case OP_CALL_HANDLER
:
662 g_string_append_printf (sbuf
, " [B%d]", ins
->inst_target_bb
->block_num
);
684 if (!ins
->inst_false_bb
)
685 g_string_append_printf (sbuf
, " [B%d]", ins
->inst_true_bb
->block_num
);
687 g_string_append_printf (sbuf
, " [B%dB%d]", ins
->inst_true_bb
->block_num
, ins
->inst_false_bb
->block_num
);
689 case OP_LIVERANGE_START
:
690 case OP_LIVERANGE_END
:
691 case OP_GC_LIVENESS_DEF
:
692 case OP_GC_LIVENESS_USE
:
693 g_string_append_printf (sbuf
, " R%d", (int)ins
->inst_c1
);
695 case OP_IL_SEQ_POINT
:
697 g_string_append_printf (sbuf
, " il: 0x%x%s", (int)ins
->inst_imm
, ins
->flags
& MONO_INST_NONEMPTY_STACK
? ", nonempty-stack" : "");
704 case OP_COND_EXC_NE_UN
:
705 case OP_COND_EXC_GE_UN
:
706 case OP_COND_EXC_GT_UN
:
707 case OP_COND_EXC_LE_UN
:
708 case OP_COND_EXC_LT_UN
:
713 case OP_COND_EXC_IEQ
:
714 case OP_COND_EXC_IGE
:
715 case OP_COND_EXC_IGT
:
716 case OP_COND_EXC_ILE
:
717 case OP_COND_EXC_ILT
:
718 case OP_COND_EXC_INE_UN
:
719 case OP_COND_EXC_IGE_UN
:
720 case OP_COND_EXC_IGT_UN
:
721 case OP_COND_EXC_ILE_UN
:
722 case OP_COND_EXC_ILT_UN
:
723 case OP_COND_EXC_IOV
:
724 case OP_COND_EXC_INO
:
726 case OP_COND_EXC_INC
:
727 g_string_append_printf (sbuf
, " %s", (const char*)ins
->inst_p1
);
733 if (spec
[MONO_INST_CLOB
])
734 g_string_append_printf (sbuf
, " clobbers: %c", spec
[MONO_INST_CLOB
]);
739 print_regtrack (RegTrack
*t
, int num
)
745 for (i
= 0; i
< num
; ++i
) {
748 if (i
>= MONO_MAX_IREGS
) {
749 g_snprintf (buf
, sizeof (buf
), "R%d", i
);
752 r
= mono_arch_regname (i
);
753 printf ("liveness: %s [%d - %d]\n", r
, t
[i
].born_in
, t
[i
].killed_in
);
759 mono_print_ins_index (int i
, MonoInst
*ins
)
762 #endif /* !defined(DISABLE_LOGGING) */
765 mono_print_ins (MonoInst
*ins
)
767 mono_print_ins_index (-1, ins
);
771 insert_before_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
* to_insert
)
774 * If this function is called multiple times, the new instructions are inserted
775 * in the proper order.
777 mono_bblock_insert_before_ins (bb
, ins
, to_insert
);
781 insert_after_ins (MonoBasicBlock
*bb
, MonoInst
*ins
, MonoInst
**last
, MonoInst
* to_insert
)
784 * If this function is called multiple times, the new instructions are inserted in
787 mono_bblock_insert_after_ins (bb
, *last
, to_insert
);
793 get_vreg_bank (MonoCompile
*cfg
, int reg
, int bank
)
795 if (vreg_is_ref (cfg
, reg
))
796 return MONO_REG_INT_REF
;
797 else if (vreg_is_mp (cfg
, reg
))
798 return MONO_REG_INT_MP
;
804 * Force the spilling of the variable in the symbolic register 'reg', and free
805 * the hreg it was assigned to.
808 spill_vreg (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
**last
, MonoInst
*ins
, int reg
, int bank
)
812 MonoRegState
*rs
= cfg
->rs
;
814 sel
= rs
->vassign
[reg
];
816 /* the vreg we need to spill lives in another logical reg bank */
817 bank
= translate_bank (cfg
->rs
, bank
, sel
);
819 /*i = rs->isymbolic [sel];
820 g_assert (i == reg);*/
822 spill
= ++cfg
->spill_count
;
823 rs
->vassign
[i
] = -spill
- 1;
824 if (G_UNLIKELY (bank
))
825 mono_regstate_free_general (rs
, sel
, bank
);
827 mono_regstate_free_int (rs
, sel
);
828 /* we need to create a spill var and insert a load to sel after the current instruction */
829 MONO_INST_NEW (cfg
, load
, regbank_load_ops
[bank
]);
831 load
->inst_basereg
= cfg
->frame_reg
;
832 load
->inst_offset
= mono_spillvar_offset (cfg
, spill
, get_vreg_bank (cfg
, reg
, bank
));
833 insert_after_ins (bb
, ins
, last
, load
);
834 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
)));
835 if (G_UNLIKELY (bank
))
836 i
= mono_regstate_alloc_general (rs
, regmask (sel
), bank
);
838 i
= mono_regstate_alloc_int (rs
, regmask (sel
));
841 if (G_UNLIKELY (bank
))
842 mono_regstate_free_general (rs
, sel
, bank
);
844 mono_regstate_free_int (rs
, sel
);
848 get_register_spilling (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
**last
, MonoInst
*ins
, regmask_t regmask
, int reg
, int bank
)
851 int i
, sel
, spill
, num_sregs
;
852 int sregs
[MONO_MAX_SRC_REGS
];
853 MonoRegState
*rs
= cfg
->rs
;
855 g_assert (bank
< MONO_NUM_REGBANKS
);
857 DEBUG (printf ("\tstart regmask to assign R%d: 0x%08llu (R%d <- R%d R%d R%d)\n", reg
, (unsigned long long)regmask
, ins
->dreg
, ins
->sreg1
, ins
->sreg2
, ins
->sreg3
));
858 /* exclude the registers in the current instruction */
859 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
860 for (i
= 0; i
< num_sregs
; ++i
) {
861 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))) {
862 if (is_soft_reg (sregs
[i
], bank
))
863 regmask
&= ~ (regmask (rs
->vassign
[sregs
[i
]]));
865 regmask
&= ~ (regmask (sregs
[i
]));
866 DEBUG (printf ("\t\texcluding sreg%d %s %d\n", i
+ 1, mono_regname_full (sregs
[i
], bank
), sregs
[i
]));
869 if ((dreg_bank_ins (ins
) == bank
) && (reg
!= ins
->dreg
) && reg_is_freeable (ins
->dreg
, bank
)) {
870 regmask
&= ~ (regmask (ins
->dreg
));
871 DEBUG (printf ("\t\texcluding dreg %s\n", mono_regname_full (ins
->dreg
, bank
)));
874 DEBUG (printf ("\t\tavailable regmask: 0x%08llu\n", (unsigned long long)regmask
));
875 g_assert (regmask
); /* need at least a register we can free */
877 /* we should track prev_use and spill the register that's farther */
878 if (G_UNLIKELY (bank
)) {
879 for (i
= 0; i
< regbank_size
[bank
]; ++i
) {
880 if (regmask
& (regmask (i
))) {
883 /* the vreg we need to load lives in another logical bank */
884 bank
= translate_bank (cfg
->rs
, bank
, sel
);
886 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_regname_full (sel
, bank
), rs
->symbolic
[bank
] [sel
]));
891 i
= rs
->symbolic
[bank
] [sel
];
892 spill
= ++cfg
->spill_count
;
893 rs
->vassign
[i
] = -spill
- 1;
894 mono_regstate_free_general (rs
, sel
, bank
);
897 for (i
= 0; i
< MONO_MAX_IREGS
; ++i
) {
898 if (regmask
& (regmask (i
))) {
900 DEBUG (printf ("\t\tselected register %s has assignment %d\n", mono_arch_regname (sel
), rs
->isymbolic
[sel
]));
905 i
= rs
->isymbolic
[sel
];
906 spill
= ++cfg
->spill_count
;
907 rs
->vassign
[i
] = -spill
- 1;
908 mono_regstate_free_int (rs
, sel
);
911 /* we need to create a spill var and insert a load to sel after the current instruction */
912 MONO_INST_NEW (cfg
, load
, regbank_load_ops
[bank
]);
914 load
->inst_basereg
= cfg
->frame_reg
;
915 load
->inst_offset
= mono_spillvar_offset (cfg
, spill
, get_vreg_bank (cfg
, i
, bank
));
916 insert_after_ins (bb
, ins
, last
, load
);
917 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
)));
918 if (G_UNLIKELY (bank
))
919 i
= mono_regstate_alloc_general (rs
, regmask (sel
), bank
);
921 i
= mono_regstate_alloc_int (rs
, regmask (sel
));
930 * Free up the hreg HREG by spilling the vreg allocated to it.
933 free_up_hreg (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
**last
, MonoInst
*ins
, int hreg
, int bank
)
935 if (G_UNLIKELY (bank
)) {
936 if (!(cfg
->rs
->free_mask
[bank
] & (regmask (hreg
)))) {
937 bank
= translate_bank (cfg
->rs
, bank
, hreg
);
938 DEBUG (printf ("\tforced spill of R%d\n", cfg
->rs
->symbolic
[bank
] [hreg
]));
939 spill_vreg (cfg
, bb
, last
, ins
, cfg
->rs
->symbolic
[bank
] [hreg
], bank
);
943 if (!(cfg
->rs
->ifree_mask
& (regmask (hreg
)))) {
944 DEBUG (printf ("\tforced spill of R%d\n", cfg
->rs
->isymbolic
[hreg
]));
945 spill_vreg (cfg
, bb
, last
, ins
, cfg
->rs
->isymbolic
[hreg
], bank
);
951 create_copy_ins (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
**last
, int dest
, int src
, MonoInst
*ins
, const unsigned char *ip
, int bank
)
955 MONO_INST_NEW (cfg
, copy
, regbank_move_ops
[bank
]);
961 mono_bblock_insert_after_ins (bb
, ins
, copy
);
964 DEBUG (printf ("\tforced copy from %s to %s\n", mono_regname_full (src
, bank
), mono_regname_full (dest
, bank
)));
968 static inline const char*
969 regbank_to_string (int bank
)
971 if (bank
== MONO_REG_INT_REF
)
973 else if (bank
== MONO_REG_INT_MP
)
980 create_spilled_store (MonoCompile
*cfg
, MonoBasicBlock
*bb
, int spill
, int reg
, int prev_reg
, MonoInst
**last
, MonoInst
*ins
, MonoInst
*insert_before
, int bank
)
982 MonoInst
*store
, *def
;
984 bank
= get_vreg_bank (cfg
, prev_reg
, bank
);
986 MONO_INST_NEW (cfg
, store
, regbank_store_ops
[bank
]);
988 store
->inst_destbasereg
= cfg
->frame_reg
;
989 store
->inst_offset
= mono_spillvar_offset (cfg
, spill
, bank
);
991 mono_bblock_insert_after_ins (bb
, ins
, store
);
993 } else if (insert_before
) {
994 insert_before_ins (bb
, insert_before
, store
);
996 g_assert_not_reached ();
998 DEBUG (printf ("\t%sSPILLED STORE (%d at 0x%08lx(%%ebp)) R%d (from %s)\n", regbank_to_string (bank
), spill
, (long)store
->inst_offset
, prev_reg
, mono_regname_full (reg
, bank
)));
1000 if (((bank
== MONO_REG_INT_REF
) || (bank
== MONO_REG_INT_MP
)) && cfg
->compute_gc_maps
) {
1001 g_assert (prev_reg
!= -1);
1002 MONO_INST_NEW (cfg
, def
, OP_GC_SPILL_SLOT_LIVENESS_DEF
);
1003 def
->inst_c0
= spill
;
1004 def
->inst_c1
= bank
;
1005 mono_bblock_insert_after_ins (bb
, store
, def
);
1009 /* flags used in reginfo->flags */
1011 MONO_FP_NEEDS_LOAD_SPILL
= regmask (0),
1012 MONO_FP_NEEDS_SPILL
= regmask (1),
1013 MONO_FP_NEEDS_LOAD
= regmask (2)
1017 alloc_int_reg (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
**last
, MonoInst
*ins
, regmask_t dest_mask
, int sym_reg
, RegTrack
*info
)
1021 if (info
&& info
->preferred_mask
) {
1022 val
= mono_regstate_alloc_int (cfg
->rs
, info
->preferred_mask
& dest_mask
);
1024 DEBUG (printf ("\tallocated preferred reg R%d to %s\n", sym_reg
, mono_arch_regname (val
)));
1029 val
= mono_regstate_alloc_int (cfg
->rs
, dest_mask
);
1031 val
= get_register_spilling (cfg
, bb
, last
, ins
, dest_mask
, sym_reg
, 0);
1037 alloc_general_reg (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
**last
, MonoInst
*ins
, regmask_t dest_mask
, int sym_reg
, int bank
)
1041 val
= mono_regstate_alloc_general (cfg
->rs
, dest_mask
, bank
);
1044 val
= get_register_spilling (cfg
, bb
, last
, ins
, dest_mask
, sym_reg
, bank
);
1046 #ifdef MONO_ARCH_HAVE_TRACK_FPREGS
1047 cfg
->arch
.used_fp_regs
|= 1 << val
;
1053 alloc_reg (MonoCompile
*cfg
, MonoBasicBlock
*bb
, MonoInst
**last
, MonoInst
*ins
, regmask_t dest_mask
, int sym_reg
, RegTrack
*info
, int bank
)
1055 if (G_UNLIKELY (bank
))
1056 return alloc_general_reg (cfg
, bb
, last
, ins
, dest_mask
, sym_reg
, bank
);
1058 return alloc_int_reg (cfg
, bb
, last
, ins
, dest_mask
, sym_reg
, info
);
1062 assign_reg (MonoCompile
*cfg
, MonoRegState
*rs
, int reg
, int hreg
, int bank
)
1064 if (G_UNLIKELY (bank
)) {
1067 g_assert (reg
>= regbank_size
[bank
]);
1068 g_assert (hreg
< regbank_size
[bank
]);
1069 g_assert (! is_global_freg (hreg
));
1071 rs
->vassign
[reg
] = hreg
;
1072 rs
->symbolic
[bank
] [hreg
] = reg
;
1073 rs
->free_mask
[bank
] &= ~ (regmask (hreg
));
1075 mirrored_bank
= get_mirrored_bank (bank
);
1076 if (mirrored_bank
== -1)
1079 /* Make sure the other logical reg bank that this bank shares
1080 * a single hard reg bank knows that this hard reg is not free.
1082 rs
->free_mask
[mirrored_bank
] = rs
->free_mask
[bank
];
1084 /* Mark the other logical bank that the this bank shares
1085 * a single hard reg bank with as mirrored.
1087 rs
->symbolic
[mirrored_bank
] [hreg
] = MONO_ARCH_BANK_MIRRORED
;
1091 g_assert (reg
>= MONO_MAX_IREGS
);
1092 g_assert (hreg
< MONO_MAX_IREGS
);
1093 #if !defined(TARGET_ARM) && !defined(TARGET_ARM64)
1094 /* this seems to trigger a gcc compilation bug sometime (hreg is 0) */
1095 /* On arm64, rgctx_reg is a global hreg, and it is used to pass an argument */
1096 g_assert (! is_global_ireg (hreg
));
1099 rs
->vassign
[reg
] = hreg
;
1100 rs
->isymbolic
[hreg
] = reg
;
1101 rs
->ifree_mask
&= ~ (regmask (hreg
));
1105 static inline regmask_t
1106 get_callee_mask (const char spec
)
1108 if (G_UNLIKELY (reg_bank (spec
)))
1109 return regbank_callee_regs
[reg_bank (spec
)];
1110 return MONO_ARCH_CALLEE_REGS
;
1113 static gint8 desc_to_fixed_reg
[256];
1114 static gboolean desc_to_fixed_reg_inited
= FALSE
;
1117 * Local register allocation.
1118 * We first scan the list of instructions and we save the liveness info of
1119 * each register (when the register is first used, when it's value is set etc.).
1120 * We also reverse the list of instructions because assigning registers backwards allows
1121 * for more tricks to be used.
1124 mono_local_regalloc (MonoCompile
*cfg
, MonoBasicBlock
*bb
)
1126 MonoInst
*ins
, *prev
, *last
;
1128 MonoRegState
*rs
= cfg
->rs
;
1132 unsigned char spec_src1
, spec_dest
;
1134 #if MONO_ARCH_USE_FPSTACK
1135 gboolean has_fp
= FALSE
;
1140 int sregs
[MONO_MAX_SRC_REGS
];
1145 if (!desc_to_fixed_reg_inited
) {
1146 for (i
= 0; i
< 256; ++i
)
1147 desc_to_fixed_reg
[i
] = MONO_ARCH_INST_FIXED_REG (i
);
1148 desc_to_fixed_reg_inited
= TRUE
;
1150 /* Validate the cpu description against the info in mini-ops.h */
1151 #if defined(TARGET_AMD64) || defined(TARGET_X86) || defined(TARGET_ARM) || defined(TARGET_ARM64) || defined (TARGET_RISCV)
1152 for (i
= OP_LOAD
; i
< OP_LAST
; ++i
) {
1155 spec
= ins_get_spec (i
);
1156 ispec
= INS_INFO (i
);
1158 if ((spec
[MONO_INST_DEST
] && (ispec
[MONO_INST_DEST
] == ' ')))
1159 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i
));
1160 if ((spec
[MONO_INST_SRC1
] && (ispec
[MONO_INST_SRC1
] == ' ')))
1161 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i
));
1162 if ((spec
[MONO_INST_SRC2
] && (ispec
[MONO_INST_SRC2
] == ' ')))
1163 printf ("Instruction metadata for %s inconsistent.\n", mono_inst_name (i
));
1168 rs
->next_vreg
= bb
->max_vreg
;
1169 mono_regstate_assign (rs
);
1171 rs
->ifree_mask
= MONO_ARCH_CALLEE_REGS
;
1172 for (i
= 0; i
< MONO_NUM_REGBANKS
; ++i
)
1173 rs
->free_mask
[i
] = regbank_callee_regs
[i
];
1175 max
= rs
->next_vreg
;
1177 if (cfg
->reginfo
&& cfg
->reginfo_len
< max
)
1178 cfg
->reginfo
= NULL
;
1180 reginfo
= (RegTrack
*)cfg
->reginfo
;
1182 cfg
->reginfo_len
= MAX (1024, max
* 2);
1183 reginfo
= (RegTrack
*)mono_mempool_alloc (cfg
->mempool
, sizeof (RegTrack
) * cfg
->reginfo_len
);
1184 cfg
->reginfo
= reginfo
;
1187 g_assert (cfg
->reginfo_len
>= rs
->next_vreg
);
1189 if (cfg
->verbose_level
> 1) {
1190 /* print_regtrack reads the info of all variables */
1191 memset (cfg
->reginfo
, 0, cfg
->reginfo_len
* sizeof (RegTrack
));
1195 * For large methods, next_vreg can be very large, so g_malloc0 time can
1196 * be prohibitive. So we manually init the reginfo entries used by the
1199 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
1200 gboolean modify
= FALSE
;
1202 spec
= ins_get_spec (ins
->opcode
);
1204 if ((ins
->dreg
!= -1) && (ins
->dreg
< max
)) {
1205 memset (®info
[ins
->dreg
], 0, sizeof (RegTrack
));
1206 #if SIZEOF_REGISTER == 4
1207 if (MONO_ARCH_INST_IS_REGPAIR (spec
[MONO_INST_DEST
])) {
1209 * In the new IR, the two vregs of the regpair do not alias the
1210 * original long vreg. shift the vreg here so the rest of the
1211 * allocator doesn't have to care about it.
1214 memset (®info
[ins
->dreg
+ 1], 0, sizeof (RegTrack
));
1219 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
1220 for (j
= 0; j
< num_sregs
; ++j
) {
1221 g_assert (sregs
[j
] != -1);
1222 if (sregs
[j
] < max
) {
1223 memset (®info
[sregs
[j
]], 0, sizeof (RegTrack
));
1224 #if SIZEOF_REGISTER == 4
1225 if (MONO_ARCH_INST_IS_REGPAIR (spec
[MONO_INST_SRC1
+ j
])) {
1228 memset (®info
[sregs
[j
] + 1], 0, sizeof (RegTrack
));
1234 mono_inst_set_src_registers (ins
, sregs
);
1237 /*if (cfg->opt & MONO_OPT_COPYPROP)
1238 local_copy_prop (cfg, ins);*/
1241 DEBUG (printf ("\nLOCAL REGALLOC BLOCK %d:\n", bb
->block_num
));
1242 /* forward pass on the instructions to collect register liveness info */
1243 MONO_BB_FOR_EACH_INS (bb
, ins
) {
1244 spec
= ins_get_spec (ins
->opcode
);
1245 spec_dest
= spec
[MONO_INST_DEST
];
1247 if (G_UNLIKELY (spec
== (gpointer
)/*FIXME*/MONO_ARCH_CPU_SPEC
)) {
1248 g_error ("Opcode '%s' missing from machine description file.", mono_inst_name (ins
->opcode
));
1251 DEBUG (mono_print_ins_index (i
, ins
));
1253 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
1255 #if MONO_ARCH_USE_FPSTACK
1256 if (dreg_is_fp (spec
)) {
1259 for (j
= 0; j
< num_sregs
; ++j
) {
1260 if (sreg_is_fp (j
, spec
))
1266 for (j
= 0; j
< num_sregs
; ++j
) {
1267 int sreg
= sregs
[j
];
1268 int sreg_spec
= spec
[MONO_INST_SRC1
+ j
];
1270 bank
= sreg_bank (j
, spec
);
1271 g_assert (sreg
!= -1);
1272 if (is_soft_reg (sreg
, bank
))
1273 /* This means the vreg is not local to this bb */
1274 g_assert (reginfo
[sreg
].born_in
> 0);
1275 rs
->vassign
[sreg
] = -1;
1276 //reginfo [ins->sreg2].prev_use = reginfo [ins->sreg2].last_use;
1277 //reginfo [ins->sreg2].last_use = i;
1278 if (MONO_ARCH_INST_IS_REGPAIR (sreg_spec
)) {
1279 /* The virtual register is allocated sequentially */
1280 rs
->vassign
[sreg
+ 1] = -1;
1281 //reginfo [ins->sreg2 + 1].prev_use = reginfo [ins->sreg2 + 1].last_use;
1282 //reginfo [ins->sreg2 + 1].last_use = i;
1283 if (reginfo
[sreg
+ 1].born_in
== 0 || reginfo
[sreg
+ 1].born_in
> i
)
1284 reginfo
[sreg
+ 1].born_in
= i
;
1290 mono_inst_set_src_registers (ins
, sregs
);
1295 bank
= dreg_bank (spec
);
1296 if (spec_dest
!= 'b') /* it's not just a base register */
1297 reginfo
[ins
->dreg
].killed_in
= i
;
1298 g_assert (ins
->dreg
!= -1);
1299 rs
->vassign
[ins
->dreg
] = -1;
1300 //reginfo [ins->dreg].prev_use = reginfo [ins->dreg].last_use;
1301 //reginfo [ins->dreg].last_use = i;
1302 if (reginfo
[ins
->dreg
].born_in
== 0 || reginfo
[ins
->dreg
].born_in
> i
)
1303 reginfo
[ins
->dreg
].born_in
= i
;
1305 dest_dreg
= desc_to_fixed_reg
[spec_dest
];
1306 if (dest_dreg
!= -1)
1307 reginfo
[ins
->dreg
].preferred_mask
= (regmask (dest_dreg
));
1309 #ifdef MONO_ARCH_INST_FIXED_MASK
1310 reginfo
[ins
->dreg
].preferred_mask
|= MONO_ARCH_INST_FIXED_MASK (spec_dest
);
1313 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest
)) {
1314 /* The virtual register is allocated sequentially */
1315 rs
->vassign
[ins
->dreg
+ 1] = -1;
1316 //reginfo [ins->dreg + 1].prev_use = reginfo [ins->dreg + 1].last_use;
1317 //reginfo [ins->dreg + 1].last_use = i;
1318 if (reginfo
[ins
->dreg
+ 1].born_in
== 0 || reginfo
[ins
->dreg
+ 1].born_in
> i
)
1319 reginfo
[ins
->dreg
+ 1].born_in
= i
;
1320 if (MONO_ARCH_INST_REGPAIR_REG2 (spec_dest
, -1) != -1)
1321 reginfo
[ins
->dreg
+ 1].preferred_mask
= regpair_reg2_mask (spec_dest
, -1);
1332 DEBUG (print_regtrack (reginfo
, rs
->next_vreg
));
1333 MONO_BB_FOR_EACH_INS_REVERSE_SAFE (bb
, prev
, ins
) {
1335 int dest_dreg
, clob_reg
;
1336 int dest_sregs
[MONO_MAX_SRC_REGS
], prev_sregs
[MONO_MAX_SRC_REGS
];
1337 int dreg_high
, sreg1_high
;
1338 regmask_t dreg_mask
, mask
;
1339 regmask_t sreg_masks
[MONO_MAX_SRC_REGS
], sreg_fixed_masks
[MONO_MAX_SRC_REGS
];
1340 regmask_t dreg_fixed_mask
;
1341 const unsigned char *ip
;
1343 spec
= ins_get_spec (ins
->opcode
);
1344 spec_src1
= spec
[MONO_INST_SRC1
];
1345 spec_dest
= spec
[MONO_INST_DEST
];
1351 dreg_mask
= get_callee_mask (spec_dest
);
1352 for (j
= 0; j
< MONO_MAX_SRC_REGS
; ++j
) {
1353 prev_sregs
[j
] = -1;
1354 sreg_masks
[j
] = get_callee_mask (spec
[MONO_INST_SRC1
+ j
]);
1355 dest_sregs
[j
] = desc_to_fixed_reg
[(int)spec
[MONO_INST_SRC1
+ j
]];
1356 #ifdef MONO_ARCH_INST_FIXED_MASK
1357 sreg_fixed_masks
[j
] = MONO_ARCH_INST_FIXED_MASK (spec
[MONO_INST_SRC1
+ j
]);
1359 sreg_fixed_masks
[j
] = 0;
1363 DEBUG (printf ("processing:"));
1364 DEBUG (mono_print_ins_index (i
, ins
));
1373 dest_dreg
= desc_to_fixed_reg
[spec_dest
];
1374 clob_reg
= desc_to_fixed_reg
[(int)spec
[MONO_INST_CLOB
]];
1375 sreg_masks
[1] &= ~ (MONO_ARCH_INST_SREG2_MASK (spec
));
1377 #ifdef MONO_ARCH_INST_FIXED_MASK
1378 dreg_fixed_mask
= MONO_ARCH_INST_FIXED_MASK (spec_dest
);
1380 dreg_fixed_mask
= 0;
1383 num_sregs
= mono_inst_get_src_registers (ins
, sregs
);
1386 * TRACK FIXED SREG2, 3, ...
1388 for (j
= 1; j
< num_sregs
; ++j
) {
1389 int sreg
= sregs
[j
];
1390 int dest_sreg
= dest_sregs
[j
];
1392 if (dest_sreg
== -1)
1400 * We need to special case this, since on x86, there are only 3
1401 * free registers, and the code below assigns one of them to
1402 * sreg, so we can run out of registers when trying to assign
1403 * dreg. Instead, we just set up the register masks, and let the
1404 * normal sreg2 assignment code handle this. It would be nice to
1405 * do this for all the fixed reg cases too, but there is too much
1409 /* Make sure sreg will be assigned to dest_sreg, and the other sregs won't */
1410 sreg_masks
[j
] = regmask (dest_sreg
);
1411 for (k
= 0; k
< num_sregs
; ++k
) {
1413 sreg_masks
[k
] &= ~ (regmask (dest_sreg
));
1417 * Spill sreg1/2 if they are assigned to dest_sreg.
1419 for (k
= 0; k
< num_sregs
; ++k
) {
1420 if (k
!= j
&& is_soft_reg (sregs
[k
], 0) && rs
->vassign
[sregs
[k
]] == dest_sreg
)
1421 free_up_hreg (cfg
, bb
, tmp
, ins
, dest_sreg
, 0);
1425 * We can also run out of registers while processing sreg2 if sreg3 is
1426 * assigned to another hreg, so spill sreg3 now.
1428 if (is_soft_reg (sreg
, 0) && rs
->vassign
[sreg
] >= 0 && rs
->vassign
[sreg
] != dest_sreg
) {
1429 spill_vreg (cfg
, bb
, tmp
, ins
, sreg
, 0);
1434 if (rs
->ifree_mask
& (regmask (dest_sreg
))) {
1435 if (is_global_ireg (sreg
)) {
1437 /* Argument already in hard reg, need to copy */
1438 MonoInst
*copy
= create_copy_ins (cfg
, bb
, tmp
, dest_sreg
, sreg
, NULL
, ip
, 0);
1439 insert_before_ins (bb
, ins
, copy
);
1440 for (k
= 0; k
< num_sregs
; ++k
) {
1442 sreg_masks
[k
] &= ~ (regmask (dest_sreg
));
1445 dreg_mask
&= ~ (regmask (dest_sreg
));
1447 val
= rs
->vassign
[sreg
];
1449 DEBUG (printf ("\tshortcut assignment of R%d to %s\n", sreg
, mono_arch_regname (dest_sreg
)));
1450 assign_reg (cfg
, rs
, sreg
, dest_sreg
, 0);
1451 } else if (val
< -1) {
1453 g_assert_not_reached ();
1455 /* Argument already in hard reg, need to copy */
1456 MonoInst
*copy
= create_copy_ins (cfg
, bb
, tmp
, dest_sreg
, val
, NULL
, ip
, 0);
1459 insert_before_ins (bb
, ins
, copy
);
1460 for (k
= 0; k
< num_sregs
; ++k
) {
1462 sreg_masks
[k
] &= ~ (regmask (dest_sreg
));
1465 * Prevent the dreg from being allocated to dest_sreg
1466 * too, since it could force sreg1 to be allocated to
1467 * the same reg on x86.
1469 dreg_mask
&= ~ (regmask (dest_sreg
));
1473 gboolean need_spill
= TRUE
;
1474 gboolean need_assign
= TRUE
;
1477 dreg_mask
&= ~ (regmask (dest_sreg
));
1478 for (k
= 0; k
< num_sregs
; ++k
) {
1480 sreg_masks
[k
] &= ~ (regmask (dest_sreg
));
1484 * First check if dreg is assigned to dest_sreg2, since we
1485 * can't spill a dreg.
1487 if (spec
[MONO_INST_DEST
])
1488 val
= rs
->vassign
[ins
->dreg
];
1491 if (val
== dest_sreg
&& ins
->dreg
!= sreg
) {
1493 * the destination register is already assigned to
1494 * dest_sreg2: we need to allocate another register for it
1495 * and then copy from this to dest_sreg2.
1498 new_dest
= alloc_int_reg (cfg
, bb
, tmp
, ins
, dreg_mask
, ins
->dreg
, ®info
[ins
->dreg
]);
1499 g_assert (new_dest
>= 0);
1500 DEBUG (printf ("\tchanging dreg R%d to %s from %s\n", ins
->dreg
, mono_arch_regname (new_dest
), mono_arch_regname (dest_sreg
)));
1502 prev_dreg
= ins
->dreg
;
1503 assign_reg (cfg
, rs
, ins
->dreg
, new_dest
, 0);
1504 create_copy_ins (cfg
, bb
, tmp
, dest_sreg
, new_dest
, ins
, ip
, 0);
1505 mono_regstate_free_int (rs
, dest_sreg
);
1509 if (is_global_ireg (sreg
)) {
1510 MonoInst
*copy
= create_copy_ins (cfg
, bb
, tmp
, dest_sreg
, sreg
, NULL
, ip
, 0);
1511 insert_before_ins (bb
, ins
, copy
);
1512 need_assign
= FALSE
;
1515 val
= rs
->vassign
[sreg
];
1516 if (val
== dest_sreg
) {
1517 /* sreg2 is already assigned to the correct register */
1519 } else if (val
< -1) {
1520 /* sreg2 is spilled, it can be assigned to dest_sreg2 */
1521 } else if (val
>= 0) {
1522 /* sreg2 already assigned to another register */
1524 * We couldn't emit a copy from val to dest_sreg2, because
1525 * val might be spilled later while processing this
1526 * instruction. So we spill sreg2 so it can be allocated to
1529 free_up_hreg (cfg
, bb
, tmp
, ins
, val
, 0);
1534 free_up_hreg (cfg
, bb
, tmp
, ins
, dest_sreg
, 0);
1538 if (rs
->vassign
[sreg
] < -1) {
1541 /* Need to emit a spill store */
1542 spill
= - rs
->vassign
[sreg
] - 1;
1543 create_spilled_store (cfg
, bb
, spill
, dest_sreg
, sreg
, tmp
, NULL
, ins
, bank
);
1545 /* force-set sreg2 */
1546 assign_reg (cfg
, rs
, sregs
[j
], dest_sreg
, 0);
1549 sregs
[j
] = dest_sreg
;
1551 mono_inst_set_src_registers (ins
, sregs
);
1556 bank
= dreg_bank (spec
);
1557 if (spec_dest
&& is_soft_reg (ins
->dreg
, bank
)) {
1558 prev_dreg
= ins
->dreg
;
1561 if (spec_dest
== 'b') {
1563 * The dest reg is read by the instruction, not written, so
1564 * avoid allocating sreg1/sreg2 to the same reg.
1566 if (dest_sregs
[0] != -1)
1567 dreg_mask
&= ~ (regmask (dest_sregs
[0]));
1568 for (j
= 1; j
< num_sregs
; ++j
) {
1569 if (dest_sregs
[j
] != -1)
1570 dreg_mask
&= ~ (regmask (dest_sregs
[j
]));
1573 val
= rs
->vassign
[ins
->dreg
];
1574 if (is_soft_reg (ins
->dreg
, bank
) && (val
>= 0) && (!(regmask (val
) & dreg_mask
))) {
1575 /* DREG is already allocated to a register needed for sreg1 */
1576 spill_vreg (cfg
, bb
, tmp
, ins
, ins
->dreg
, 0);
1581 * If dreg is a fixed regpair, free up both of the needed hregs to avoid
1582 * various complex situations.
1584 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest
)) {
1585 guint32 dreg2
, dest_dreg2
;
1587 g_assert (is_soft_reg (ins
->dreg
, bank
));
1589 if (dest_dreg
!= -1) {
1590 if (rs
->vassign
[ins
->dreg
] != dest_dreg
)
1591 free_up_hreg (cfg
, bb
, tmp
, ins
, dest_dreg
, 0);
1593 dreg2
= ins
->dreg
+ 1;
1594 dest_dreg2
= MONO_ARCH_INST_REGPAIR_REG2 (spec_dest
, dest_dreg
);
1595 if (dest_dreg2
!= -1) {
1596 if (rs
->vassign
[dreg2
] != dest_dreg2
)
1597 free_up_hreg (cfg
, bb
, tmp
, ins
, dest_dreg2
, 0);
1602 if (dreg_fixed_mask
) {
1604 if (is_global_ireg (ins
->dreg
)) {
1606 * The argument is already in a hard reg, but that reg is
1607 * not usable by this instruction, so allocate a new one.
1609 val
= mono_regstate_alloc_int (rs
, dreg_fixed_mask
);
1611 val
= get_register_spilling (cfg
, bb
, tmp
, ins
, dreg_fixed_mask
, -1, bank
);
1612 mono_regstate_free_int (rs
, val
);
1618 dreg_mask
&= dreg_fixed_mask
;
1621 if (is_soft_reg (ins
->dreg
, bank
)) {
1622 val
= rs
->vassign
[ins
->dreg
];
1627 /* the register gets spilled after this inst */
1630 val
= alloc_reg (cfg
, bb
, tmp
, ins
, dreg_mask
, ins
->dreg
, ®info
[ins
->dreg
], bank
);
1631 assign_reg (cfg
, rs
, ins
->dreg
, val
, bank
);
1633 create_spilled_store (cfg
, bb
, spill
, val
, prev_dreg
, tmp
, ins
, NULL
, bank
);
1636 DEBUG (printf ("\tassigned dreg %s to dest R%d\n", mono_regname_full (val
, bank
), ins
->dreg
));
1640 /* Handle regpairs */
1641 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest
)) {
1642 int reg2
= prev_dreg
+ 1;
1645 g_assert (prev_dreg
> -1);
1646 g_assert (!is_global_ireg (rs
->vassign
[prev_dreg
]));
1647 mask
= regpair_reg2_mask (spec_dest
, rs
->vassign
[prev_dreg
]);
1650 mask
&= ~regmask (X86_ECX
);
1652 val
= rs
->vassign
[reg2
];
1656 /* the register gets spilled after this inst */
1659 val
= mono_regstate_alloc_int (rs
, mask
);
1661 val
= get_register_spilling (cfg
, bb
, tmp
, ins
, mask
, reg2
, bank
);
1663 create_spilled_store (cfg
, bb
, spill
, val
, reg2
, tmp
, ins
, NULL
, bank
);
1666 if (! (mask
& (regmask (val
)))) {
1667 val
= mono_regstate_alloc_int (rs
, mask
);
1669 val
= get_register_spilling (cfg
, bb
, tmp
, ins
, mask
, reg2
, bank
);
1671 /* Reallocate hreg to the correct register */
1672 create_copy_ins (cfg
, bb
, tmp
, rs
->vassign
[reg2
], val
, ins
, ip
, bank
);
1674 mono_regstate_free_int (rs
, rs
->vassign
[reg2
]);
1678 DEBUG (printf ("\tassigned dreg-high %s to dest R%d\n", mono_arch_regname (val
), reg2
));
1679 assign_reg (cfg
, rs
, reg2
, val
, bank
);
1682 ins
->backend
.reg3
= val
;
1684 if (reg_is_freeable (val
, bank
) && reg2
>= 0 && (reginfo
[reg2
].born_in
>= i
)) {
1685 DEBUG (printf ("\tfreeable %s (R%d)\n", mono_arch_regname (val
), reg2
));
1686 mono_regstate_free_int (rs
, val
);
1690 if (prev_dreg
>= 0 && is_soft_reg (prev_dreg
, bank
) && (spec_dest
!= 'b')) {
1692 * In theory, we could free up the hreg even if the vreg is alive,
1693 * but branches inside bblocks force us to assign the same hreg
1694 * to a vreg every time it is encountered.
1696 int dreg
= rs
->vassign
[prev_dreg
];
1697 g_assert (dreg
>= 0);
1698 DEBUG (printf ("\tfreeable %s (R%d) (born in %d)\n", mono_regname_full (dreg
, bank
), prev_dreg
, reginfo
[prev_dreg
].born_in
));
1699 if (G_UNLIKELY (bank
))
1700 mono_regstate_free_general (rs
, dreg
, bank
);
1702 mono_regstate_free_int (rs
, dreg
);
1703 rs
->vassign
[prev_dreg
] = -1;
1706 if ((dest_dreg
!= -1) && (ins
->dreg
!= dest_dreg
)) {
1707 /* this instruction only outputs to dest_dreg, need to copy */
1708 create_copy_ins (cfg
, bb
, tmp
, ins
->dreg
, dest_dreg
, ins
, ip
, bank
);
1709 ins
->dreg
= dest_dreg
;
1711 if (G_UNLIKELY (bank
)) {
1712 /* the register we need to free up may be used in another logical regbank
1713 * so do a translate just in case.
1715 int translated_bank
= translate_bank (cfg
->rs
, bank
, dest_dreg
);
1716 if (rs
->symbolic
[translated_bank
] [dest_dreg
] >= regbank_size
[translated_bank
])
1717 free_up_hreg (cfg
, bb
, tmp
, ins
, dest_dreg
, translated_bank
);
1720 if (rs
->isymbolic
[dest_dreg
] >= MONO_MAX_IREGS
)
1721 free_up_hreg (cfg
, bb
, tmp
, ins
, dest_dreg
, bank
);
1725 if (spec_dest
== 'b') {
1727 * The dest reg is read by the instruction, not written, so
1728 * avoid allocating sreg1/sreg2 to the same reg.
1730 for (j
= 0; j
< num_sregs
; ++j
)
1731 if (!sreg_bank (j
, spec
))
1732 sreg_masks
[j
] &= ~ (regmask (ins
->dreg
));
1738 if ((clob_reg
!= -1) && (!(rs
->ifree_mask
& (regmask (clob_reg
))))) {
1739 DEBUG (printf ("\tforced spill of clobbered reg R%d\n", rs
->isymbolic
[clob_reg
]));
1740 free_up_hreg (cfg
, bb
, tmp
, ins
, clob_reg
, 0);
1743 if (spec
[MONO_INST_CLOB
] == 'c') {
1744 int j
, s
, dreg
, dreg2
, cur_bank
;
1747 clob_mask
= MONO_ARCH_CALLEE_REGS
;
1749 if (rs
->ifree_mask
!= MONO_ARCH_CALLEE_REGS
) {
1751 * Need to avoid spilling the dreg since the dreg is not really
1752 * clobbered by the call.
1754 if ((prev_dreg
!= -1) && !reg_bank (spec_dest
))
1755 dreg
= rs
->vassign
[prev_dreg
];
1759 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest
))
1760 dreg2
= rs
->vassign
[prev_dreg
+ 1];
1764 for (j
= 0; j
< MONO_MAX_IREGS
; ++j
) {
1766 if ((clob_mask
& s
) && !(rs
->ifree_mask
& s
) && (j
!= ins
->sreg1
)) {
1767 if ((j
!= dreg
) && (j
!= dreg2
))
1768 free_up_hreg (cfg
, bb
, tmp
, ins
, j
, 0);
1769 else if (rs
->isymbolic
[j
])
1770 /* The hreg is assigned to the dreg of this instruction */
1771 rs
->vassign
[rs
->isymbolic
[j
]] = -1;
1772 mono_regstate_free_int (rs
, j
);
1777 for (cur_bank
= 1; cur_bank
< MONO_NUM_REGBANKS
; ++ cur_bank
) {
1778 if (rs
->free_mask
[cur_bank
] != regbank_callee_regs
[cur_bank
]) {
1779 clob_mask
= regbank_callee_regs
[cur_bank
];
1780 if ((prev_dreg
!= -1) && reg_bank (spec_dest
))
1781 dreg
= rs
->vassign
[prev_dreg
];
1785 for (j
= 0; j
< regbank_size
[cur_bank
]; ++j
) {
1787 /* we are looping though the banks in the outer loop
1788 * so, we don't need to deal with mirrored hregs
1789 * because we will get them in one of the other bank passes.
1791 if (is_hreg_mirrored (rs
, cur_bank
, j
))
1795 if ((clob_mask
& s
) && !(rs
->free_mask
[cur_bank
] & s
)) {
1797 free_up_hreg (cfg
, bb
, tmp
, ins
, j
, cur_bank
);
1798 else if (rs
->symbolic
[cur_bank
] [j
])
1799 /* The hreg is assigned to the dreg of this instruction */
1800 rs
->vassign
[rs
->symbolic
[cur_bank
] [j
]] = -1;
1801 mono_regstate_free_general (rs
, j
, cur_bank
);
1809 * TRACK ARGUMENT REGS
1811 if (spec
[MONO_INST_CLOB
] == 'c' && MONO_IS_CALL (ins
)) {
1812 MonoCallInst
*call
= (MonoCallInst
*)ins
;
1816 * This needs to be done before assigning sreg1, so sreg1 will
1817 * not be assigned one of the argument regs.
1821 * Assign all registers in call->out_reg_args to the proper
1822 * argument registers.
1825 list
= call
->out_ireg_args
;
1831 regpair
= (guint32
)(gssize
)(list
->data
);
1832 hreg
= regpair
>> 24;
1833 reg
= regpair
& 0xffffff;
1835 assign_reg (cfg
, rs
, reg
, hreg
, 0);
1837 sreg_masks
[0] &= ~(regmask (hreg
));
1839 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_arch_regname (hreg
), reg
));
1841 list
= g_slist_next (list
);
1845 list
= call
->out_freg_args
;
1851 regpair
= (guint32
)(gssize
)(list
->data
);
1852 hreg
= regpair
>> 24;
1853 reg
= regpair
& 0xffffff;
1855 assign_reg (cfg
, rs
, reg
, hreg
, 1);
1857 DEBUG (printf ("\tassigned arg reg %s to R%d\n", mono_regname_full (hreg
, 1), reg
));
1859 list
= g_slist_next (list
);
1867 bank
= sreg1_bank (spec
);
1868 if (MONO_ARCH_INST_IS_REGPAIR (spec_dest
) && (spec
[MONO_INST_CLOB
] == '1')) {
1869 int sreg1
= sregs
[0];
1870 int dest_sreg1
= dest_sregs
[0];
1872 g_assert (is_soft_reg (sreg1
, bank
));
1874 /* To simplify things, we allocate the same regpair to sreg1 and dreg */
1875 if (dest_sreg1
!= -1)
1876 g_assert (dest_sreg1
== ins
->dreg
);
1877 val
= mono_regstate_alloc_int (rs
, regmask (ins
->dreg
));
1878 g_assert (val
>= 0);
1880 if (rs
->vassign
[sreg1
] >= 0 && rs
->vassign
[sreg1
] != val
)
1882 g_assert_not_reached ();
1884 assign_reg (cfg
, rs
, sreg1
, val
, bank
);
1886 DEBUG (printf ("\tassigned sreg1-low %s to R%d\n", mono_regname_full (val
, bank
), sreg1
));
1888 g_assert ((regmask (dreg_high
)) & regpair_reg2_mask (spec_src1
, ins
->dreg
));
1889 val
= mono_regstate_alloc_int (rs
, regmask (dreg_high
));
1890 g_assert (val
>= 0);
1892 if (rs
->vassign
[sreg1
+ 1] >= 0 && rs
->vassign
[sreg1
+ 1] != val
)
1894 g_assert_not_reached ();
1896 assign_reg (cfg
, rs
, sreg1
+ 1, val
, bank
);
1898 DEBUG (printf ("\tassigned sreg1-high %s to R%d\n", mono_regname_full (val
, bank
), sreg1
+ 1));
1900 /* Skip rest of this section */
1901 dest_sregs
[0] = -1;
1904 if (sreg_fixed_masks
[0]) {
1906 if (is_global_ireg (sregs
[0])) {
1908 * The argument is already in a hard reg, but that reg is
1909 * not usable by this instruction, so allocate a new one.
1911 val
= mono_regstate_alloc_int (rs
, sreg_fixed_masks
[0]);
1913 val
= get_register_spilling (cfg
, bb
, tmp
, ins
, sreg_fixed_masks
[0], -1, bank
);
1914 mono_regstate_free_int (rs
, val
);
1915 dest_sregs
[0] = val
;
1917 /* Fall through to the dest_sreg1 != -1 case */
1920 sreg_masks
[0] &= sreg_fixed_masks
[0];
1923 if (dest_sregs
[0] != -1) {
1924 sreg_masks
[0] = regmask (dest_sregs
[0]);
1926 if ((rs
->vassign
[sregs
[0]] != dest_sregs
[0]) && !(rs
->ifree_mask
& (regmask (dest_sregs
[0])))) {
1927 free_up_hreg (cfg
, bb
, tmp
, ins
, dest_sregs
[0], 0);
1929 if (is_global_ireg (sregs
[0])) {
1930 /* The argument is already in a hard reg, need to copy */
1931 MonoInst
*copy
= create_copy_ins (cfg
, bb
, tmp
, dest_sregs
[0], sregs
[0], NULL
, ip
, 0);
1932 insert_before_ins (bb
, ins
, copy
);
1933 sregs
[0] = dest_sregs
[0];
1937 if (is_soft_reg (sregs
[0], bank
)) {
1938 val
= rs
->vassign
[sregs
[0]];
1939 prev_sregs
[0] = sregs
[0];
1943 /* the register gets spilled after this inst */
1947 if ((ins
->opcode
== OP_MOVE
) && !spill
&& !bank
&& is_local_ireg (ins
->dreg
) && (rs
->ifree_mask
& (regmask (ins
->dreg
)))) {
1949 * Allocate the same hreg to sreg1 as well so the
1950 * peephole can get rid of the move.
1952 sreg_masks
[0] = regmask (ins
->dreg
);
1955 if (spec
[MONO_INST_CLOB
] == '1' && !dreg_bank (spec
) && (rs
->ifree_mask
& (regmask (ins
->dreg
))))
1956 /* Allocate the same reg to sreg1 to avoid a copy later */
1957 sreg_masks
[0] = regmask (ins
->dreg
);
1959 val
= alloc_reg (cfg
, bb
, tmp
, ins
, sreg_masks
[0], sregs
[0], ®info
[sregs
[0]], bank
);
1960 assign_reg (cfg
, rs
, sregs
[0], val
, bank
);
1961 DEBUG (printf ("\tassigned sreg1 %s to R%d\n", mono_regname_full (val
, bank
), sregs
[0]));
1965 * Need to insert before the instruction since it can
1968 create_spilled_store (cfg
, bb
, spill
, val
, prev_sregs
[0], tmp
, NULL
, ins
, bank
);
1971 else if ((dest_sregs
[0] != -1) && (dest_sregs
[0] != val
)) {
1972 MonoInst
*copy
= create_copy_ins (cfg
, bb
, tmp
, dest_sregs
[0], val
, NULL
, ip
, bank
);
1973 insert_before_ins (bb
, ins
, copy
);
1974 for (j
= 1; j
< num_sregs
; ++j
)
1975 sreg_masks
[j
] &= ~(regmask (dest_sregs
[0]));
1976 val
= dest_sregs
[0];
1982 prev_sregs
[0] = -1;
1984 mono_inst_set_src_registers (ins
, sregs
);
1986 for (j
= 1; j
< num_sregs
; ++j
)
1987 sreg_masks
[j
] &= ~(regmask (sregs
[0]));
1989 /* Handle the case when sreg1 is a regpair but dreg is not */
1990 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1
) && (spec
[MONO_INST_CLOB
] != '1')) {
1991 int reg2
= prev_sregs
[0] + 1;
1994 g_assert (prev_sregs
[0] > -1);
1995 g_assert (!is_global_ireg (rs
->vassign
[prev_sregs
[0]]));
1996 mask
= regpair_reg2_mask (spec_src1
, rs
->vassign
[prev_sregs
[0]]);
1997 val
= rs
->vassign
[reg2
];
2001 /* the register gets spilled after this inst */
2004 val
= mono_regstate_alloc_int (rs
, mask
);
2006 val
= get_register_spilling (cfg
, bb
, tmp
, ins
, mask
, reg2
, bank
);
2008 g_assert_not_reached ();
2011 if (! (mask
& (regmask (val
)))) {
2012 /* The vreg is already allocated to a wrong hreg */
2014 g_assert_not_reached ();
2016 val
= mono_regstate_alloc_int (rs
, mask
);
2018 val
= get_register_spilling (cfg
, bb
, tmp
, ins
, mask
, reg2
, bank
);
2020 /* Reallocate hreg to the correct register */
2021 create_copy_ins (cfg
, bb
, tmp
, rs
->vassign
[reg2
], val
, ins
, ip
, bank
);
2023 mono_regstate_free_int (rs
, rs
->vassign
[reg2
]);
2029 DEBUG (printf ("\tassigned sreg1 hreg %s to dest R%d\n", mono_arch_regname (val
), reg2
));
2030 assign_reg (cfg
, rs
, reg2
, val
, bank
);
2033 /* Handle dreg==sreg1 */
2034 if (((dreg_is_fp (spec
) && sreg1_is_fp (spec
)) || spec
[MONO_INST_CLOB
] == '1') && ins
->dreg
!= sregs
[0]) {
2035 MonoInst
*sreg2_copy
= NULL
;
2037 int bank
= reg_bank (spec_src1
);
2039 if (ins
->dreg
== sregs
[1]) {
2041 * copying sreg1 to dreg could clobber sreg2, so allocate a new
2044 int reg2
= alloc_reg (cfg
, bb
, tmp
, ins
, dreg_mask
, sregs
[1], NULL
, bank
);
2046 DEBUG (printf ("\tneed to copy sreg2 %s to reg %s\n", mono_regname_full (sregs
[1], bank
), mono_regname_full (reg2
, bank
)));
2047 sreg2_copy
= create_copy_ins (cfg
, bb
, tmp
, reg2
, sregs
[1], NULL
, ip
, bank
);
2048 prev_sregs
[1] = sregs
[1] = reg2
;
2050 if (G_UNLIKELY (bank
))
2051 mono_regstate_free_general (rs
, reg2
, bank
);
2053 mono_regstate_free_int (rs
, reg2
);
2056 if (MONO_ARCH_INST_IS_REGPAIR (spec_src1
)) {
2057 /* Copying sreg1_high to dreg could also clobber sreg2 */
2058 if (rs
->vassign
[prev_sregs
[0] + 1] == sregs
[1])
2060 g_assert_not_reached ();
2063 * sreg1 and dest are already allocated to the same regpair by the
2064 * SREG1 allocation code.
2066 g_assert (sregs
[0] == ins
->dreg
);
2067 g_assert (dreg_high
== sreg1_high
);
2070 DEBUG (printf ("\tneed to copy sreg1 %s to dreg %s\n", mono_regname_full (sregs
[0], bank
), mono_regname_full (ins
->dreg
, bank
)));
2071 copy
= create_copy_ins (cfg
, bb
, tmp
, ins
->dreg
, sregs
[0], NULL
, ip
, bank
);
2072 insert_before_ins (bb
, ins
, copy
);
2075 insert_before_ins (bb
, copy
, sreg2_copy
);
2078 * Need to prevent sreg2 to be allocated to sreg1, since that
2079 * would screw up the previous copy.
2081 sreg_masks
[1] &= ~ (regmask (sregs
[0]));
2082 /* we set sreg1 to dest as well */
2083 prev_sregs
[0] = sregs
[0] = ins
->dreg
;
2084 sreg_masks
[1] &= ~ (regmask (ins
->dreg
));
2086 mono_inst_set_src_registers (ins
, sregs
);
2089 * TRACK SREG2, 3, ...
2091 for (j
= 1; j
< num_sregs
; ++j
) {
2094 bank
= sreg_bank (j
, spec
);
2095 if (MONO_ARCH_INST_IS_REGPAIR (spec
[MONO_INST_SRC1
+ j
]))
2096 g_assert_not_reached ();
2098 if (dest_sregs
[j
] != -1 && is_global_ireg (sregs
[j
])) {
2100 * Argument already in a global hard reg, copy it to the fixed reg, without
2101 * allocating it to the fixed reg.
2103 MonoInst
*copy
= create_copy_ins (cfg
, bb
, tmp
, dest_sregs
[j
], sregs
[j
], NULL
, ip
, 0);
2104 insert_before_ins (bb
, ins
, copy
);
2105 sregs
[j
] = dest_sregs
[j
];
2106 } else if (is_soft_reg (sregs
[j
], bank
)) {
2107 val
= rs
->vassign
[sregs
[j
]];
2109 if (dest_sregs
[j
] != -1 && val
>= 0 && dest_sregs
[j
] != val
) {
2111 * The sreg is already allocated to a hreg, but not to the fixed
2112 * reg required by the instruction. Spill the sreg, so it can be
2113 * allocated to the fixed reg by the code below.
2115 /* Currently, this code should only be hit for CAS */
2116 spill_vreg (cfg
, bb
, tmp
, ins
, sregs
[j
], 0);
2117 val
= rs
->vassign
[sregs
[j
]];
2123 /* the register gets spilled after this inst */
2126 val
= alloc_reg (cfg
, bb
, tmp
, ins
, sreg_masks
[j
], sregs
[j
], ®info
[sregs
[j
]], bank
);
2127 assign_reg (cfg
, rs
, sregs
[j
], val
, bank
);
2128 DEBUG (printf ("\tassigned sreg%d %s to R%d\n", j
+ 1, mono_regname_full (val
, bank
), sregs
[j
]));
2131 * Need to insert before the instruction since it can
2134 create_spilled_store (cfg
, bb
, spill
, val
, sregs
[j
], tmp
, NULL
, ins
, bank
);
2138 for (k
= j
+ 1; k
< num_sregs
; ++k
)
2139 sreg_masks
[k
] &= ~ (regmask (sregs
[j
]));
2142 prev_sregs
[j
] = -1;
2145 mono_inst_set_src_registers (ins
, sregs
);
2148 /* Do this only for CAS for now */
2149 for (j
= 1; j
< num_sregs
; ++j
) {
2150 int sreg
= sregs
[j
];
2151 int dest_sreg
= dest_sregs
[j
];
2153 if (j
== 2 && dest_sreg
!= -1) {
2156 g_assert (sreg
== dest_sreg
);
2158 for (k
= 0; k
< num_sregs
; ++k
) {
2160 g_assert (sregs
[k
] != dest_sreg
);
2165 /*if (reg_is_freeable (ins->sreg1) && prev_sreg1 >= 0 && reginfo [prev_sreg1].born_in >= i) {
2166 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg1)));
2167 mono_regstate_free_int (rs, ins->sreg1);
2169 if (reg_is_freeable (ins->sreg2) && prev_sreg2 >= 0 && reginfo [prev_sreg2].born_in >= i) {
2170 DEBUG (printf ("freeable %s\n", mono_arch_regname (ins->sreg2)));
2171 mono_regstate_free_int (rs, ins->sreg2);
2174 DEBUG (mono_print_ins_index (i
, ins
));
2177 // FIXME: Set MAX_FREGS to 8
2178 // FIXME: Optimize generated code
2179 #if MONO_ARCH_USE_FPSTACK
2181 * Make a forward pass over the code, simulating the fp stack, making sure the
2182 * arguments required by the fp opcodes are at the top of the stack.
2185 MonoInst
*prev
= NULL
;
2189 g_assert (num_sregs
<= 2);
2191 for (ins
= bb
->code
; ins
; ins
= ins
->next
) {
2192 spec
= ins_get_spec (ins
->opcode
);
2194 DEBUG (printf ("processing:"));
2195 DEBUG (mono_print_ins_index (0, ins
));
2197 if (ins
->opcode
== OP_FMOVE
) {
2198 /* Do it by renaming the source to the destination on the stack */
2199 // FIXME: Is this correct ?
2200 for (i
= 0; i
< sp
; ++i
)
2201 if (fpstack
[i
] == ins
->sreg1
)
2202 fpstack
[i
] = ins
->dreg
;
2207 if (sreg1_is_fp (spec
) && sreg2_is_fp (spec
) && (fpstack
[sp
- 2] != ins
->sreg1
)) {
2208 /* Arg1 must be in %st(1) */
2212 while ((i
< sp
) && (fpstack
[i
] != ins
->sreg1
))
2216 if (sp
- 1 - i
> 0) {
2217 /* First move it to %st(0) */
2218 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp
- 1 - i
));
2220 MONO_INST_NEW (cfg
, fxch
, OP_X86_FXCH
);
2221 fxch
->inst_imm
= sp
- 1 - i
;
2223 mono_bblock_insert_after_ins (bb
, prev
, fxch
);
2226 tmp
= fpstack
[sp
- 1];
2227 fpstack
[sp
- 1] = fpstack
[i
];
2231 /* Then move it to %st(1) */
2232 DEBUG (printf ("\tswap %%st(0) and %%st(1)\n"));
2234 MONO_INST_NEW (cfg
, fxch
, OP_X86_FXCH
);
2237 mono_bblock_insert_after_ins (bb
, prev
, fxch
);
2240 tmp
= fpstack
[sp
- 1];
2241 fpstack
[sp
- 1] = fpstack
[sp
- 2];
2242 fpstack
[sp
- 2] = tmp
;
2245 if (sreg2_is_fp (spec
)) {
2248 if (fpstack
[sp
- 1] != ins
->sreg2
) {
2252 while ((i
< sp
) && (fpstack
[i
] != ins
->sreg2
))
2256 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp
- 1 - i
));
2258 MONO_INST_NEW (cfg
, fxch
, OP_X86_FXCH
);
2259 fxch
->inst_imm
= sp
- 1 - i
;
2261 mono_bblock_insert_after_ins (bb
, prev
, fxch
);
2264 tmp
= fpstack
[sp
- 1];
2265 fpstack
[sp
- 1] = fpstack
[i
];
2272 if (sreg1_is_fp (spec
)) {
2275 if (fpstack
[sp
- 1] != ins
->sreg1
) {
2279 while ((i
< sp
) && (fpstack
[i
] != ins
->sreg1
))
2283 DEBUG (printf ("\tswap %%st(0) and %%st(%d)\n", sp
- 1 - i
));
2285 MONO_INST_NEW (cfg
, fxch
, OP_X86_FXCH
);
2286 fxch
->inst_imm
= sp
- 1 - i
;
2288 mono_bblock_insert_after_ins (bb
, prev
, fxch
);
2291 tmp
= fpstack
[sp
- 1];
2292 fpstack
[sp
- 1] = fpstack
[i
];
2299 if (dreg_is_fp (spec
)) {
2301 fpstack
[sp
++] = ins
->dreg
;
2304 if (G_UNLIKELY (cfg
->verbose_level
>= 2)) {
2306 for (i
= 0; i
< sp
; ++i
)
2307 printf ("%s%%fr%d", (i
> 0) ? ", " : "", fpstack
[i
]);
2314 if (sp
&& bb
!= cfg
->bb_exit
&& !(bb
->out_count
== 1 && bb
->out_bb
[0] == cfg
->bb_exit
)) {
2315 /* Remove remaining items from the fp stack */
2317 * These can remain for example as a result of a dead fmove like in
2318 * System.Collections.Generic.EqualityComparer<double>.Equals ().
2321 MONO_INST_NEW (cfg
, ins
, OP_X86_FPOP
);
2322 mono_add_ins_to_end (bb
, ins
);
2331 mono_opcode_to_cond (int opcode
)
2343 case OP_COND_EXC_EQ
:
2344 case OP_COND_EXC_IEQ
:
2354 case OP_COND_EXC_NE_UN
:
2355 case OP_COND_EXC_INE_UN
:
2356 case OP_CMOV_INE_UN
:
2357 case OP_CMOV_LNE_UN
:
2384 case OP_COND_EXC_LT
:
2385 case OP_COND_EXC_ILT
:
2398 case OP_COND_EXC_GT
:
2399 case OP_COND_EXC_IGT
:
2408 case OP_COND_EXC_LE_UN
:
2409 case OP_COND_EXC_ILE_UN
:
2410 case OP_CMOV_ILE_UN
:
2411 case OP_CMOV_LLE_UN
:
2418 case OP_CMOV_IGE_UN
:
2419 case OP_CMOV_LGE_UN
:
2430 case OP_COND_EXC_LT_UN
:
2431 case OP_COND_EXC_ILT_UN
:
2432 case OP_CMOV_ILT_UN
:
2433 case OP_CMOV_LLT_UN
:
2444 case OP_COND_EXC_GT_UN
:
2445 case OP_COND_EXC_IGT_UN
:
2446 case OP_CMOV_IGT_UN
:
2447 case OP_CMOV_LGT_UN
:
2450 printf ("%s\n", mono_inst_name (opcode
));
2451 g_assert_not_reached ();
2452 return (CompRelation
)0;
2457 mono_negate_cond (CompRelation cond
)
2481 g_assert_not_reached ();
2486 mono_opcode_to_type (int opcode
, int cmp_opcode
)
2488 if ((opcode
>= OP_CEQ
) && (opcode
<= OP_CLT_UN
))
2490 else if ((opcode
>= OP_IBEQ
) && (opcode
<= OP_IBLT_UN
))
2492 else if ((opcode
>= OP_ICEQ
) && (opcode
<= OP_ICLT_UN
))
2494 else if ((opcode
>= OP_LBEQ
) && (opcode
<= OP_LBLT_UN
))
2496 else if ((opcode
>= OP_LCEQ
) && (opcode
<= OP_LCLT_UN
))
2498 else if ((opcode
>= OP_FBEQ
) && (opcode
<= OP_FBLT_UN
))
2500 else if ((opcode
>= OP_FCEQ
) && (opcode
<= OP_FCLT_UN
))
2502 else if ((opcode
>= OP_COND_EXC_IEQ
) && (opcode
<= OP_COND_EXC_ILT_UN
))
2504 else if ((opcode
>= OP_COND_EXC_EQ
) && (opcode
<= OP_COND_EXC_LT_UN
)) {
2505 switch (cmp_opcode
) {
2507 case OP_ICOMPARE_IMM
:
2513 g_error ("Unknown opcode '%s' in opcode_to_type", mono_inst_name (opcode
));
2519 * mono_peephole_ins:
2521 * Perform some architecture independent peephole optimizations.
2524 mono_peephole_ins (MonoBasicBlock
*bb
, MonoInst
*ins
)
2526 int filter
= FILTER_IL_SEQ_POINT
;
2527 MonoInst
*last_ins
= mono_inst_prev (ins
, filter
);
2529 switch (ins
->opcode
) {
2531 /* remove unnecessary multiplication with 1 */
2532 if (ins
->inst_imm
== 1) {
2533 if (ins
->dreg
!= ins
->sreg1
)
2534 ins
->opcode
= OP_MOVE
;
2536 MONO_DELETE_INS (bb
, ins
);
2539 case OP_LOAD_MEMBASE
:
2540 case OP_LOADI4_MEMBASE
:
2542 * Note: if reg1 = reg2 the load op is removed
2544 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2545 * OP_LOAD_MEMBASE offset(basereg), reg2
2547 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2548 * OP_MOVE reg1, reg2
2550 if (last_ins
&& last_ins
->opcode
== OP_GC_LIVENESS_DEF
)
2551 last_ins
= mono_inst_prev (ins
, filter
);
2553 (((ins
->opcode
== OP_LOADI4_MEMBASE
) && (last_ins
->opcode
== OP_STOREI4_MEMBASE_REG
)) ||
2554 ((ins
->opcode
== OP_LOAD_MEMBASE
) && (last_ins
->opcode
== OP_STORE_MEMBASE_REG
))) &&
2555 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
2556 ins
->inst_offset
== last_ins
->inst_offset
) {
2557 if (ins
->dreg
== last_ins
->sreg1
) {
2558 MONO_DELETE_INS (bb
, ins
);
2561 ins
->opcode
= OP_MOVE
;
2562 ins
->sreg1
= last_ins
->sreg1
;
2566 * Note: reg1 must be different from the basereg in the second load
2567 * Note: if reg1 = reg2 is equal then second load is removed
2569 * OP_LOAD_MEMBASE offset(basereg), reg1
2570 * OP_LOAD_MEMBASE offset(basereg), reg2
2572 * OP_LOAD_MEMBASE offset(basereg), reg1
2573 * OP_MOVE reg1, reg2
2575 } if (last_ins
&& (last_ins
->opcode
== OP_LOADI4_MEMBASE
2576 || last_ins
->opcode
== OP_LOAD_MEMBASE
) &&
2577 ins
->inst_basereg
!= last_ins
->dreg
&&
2578 ins
->inst_basereg
== last_ins
->inst_basereg
&&
2579 ins
->inst_offset
== last_ins
->inst_offset
) {
2581 if (ins
->dreg
== last_ins
->dreg
) {
2582 MONO_DELETE_INS (bb
, ins
);
2584 ins
->opcode
= OP_MOVE
;
2585 ins
->sreg1
= last_ins
->dreg
;
2588 //g_assert_not_reached ();
2592 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2593 * OP_LOAD_MEMBASE offset(basereg), reg
2595 * OP_STORE_MEMBASE_IMM imm, offset(basereg)
2596 * OP_ICONST reg, imm
2598 } else if (last_ins
&& (last_ins
->opcode
== OP_STOREI4_MEMBASE_IMM
2599 || last_ins
->opcode
== OP_STORE_MEMBASE_IMM
) &&
2600 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
2601 ins
->inst_offset
== last_ins
->inst_offset
) {
2602 ins
->opcode
= OP_ICONST
;
2603 ins
->inst_c0
= last_ins
->inst_imm
;
2604 g_assert_not_reached (); // check this rule
2608 case OP_LOADI1_MEMBASE
:
2609 case OP_LOADU1_MEMBASE
:
2611 * Note: if reg1 = reg2 the load op is removed
2613 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2614 * OP_LOAD_MEMBASE offset(basereg), reg2
2616 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2617 * OP_MOVE reg1, reg2
2619 if (last_ins
&& (last_ins
->opcode
== OP_STOREI1_MEMBASE_REG
) &&
2620 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
2621 ins
->inst_offset
== last_ins
->inst_offset
) {
2622 ins
->opcode
= (ins
->opcode
== OP_LOADI1_MEMBASE
) ? OP_PCONV_TO_I1
: OP_PCONV_TO_U1
;
2623 ins
->sreg1
= last_ins
->sreg1
;
2626 case OP_LOADI2_MEMBASE
:
2627 case OP_LOADU2_MEMBASE
:
2629 * Note: if reg1 = reg2 the load op is removed
2631 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2632 * OP_LOAD_MEMBASE offset(basereg), reg2
2634 * OP_STORE_MEMBASE_REG reg1, offset(basereg)
2635 * OP_MOVE reg1, reg2
2637 if (last_ins
&& (last_ins
->opcode
== OP_STOREI2_MEMBASE_REG
) &&
2638 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
2639 ins
->inst_offset
== last_ins
->inst_offset
) {
2640 #if SIZEOF_REGISTER == 8
2641 ins
->opcode
= (ins
->opcode
== OP_LOADI2_MEMBASE
) ? OP_PCONV_TO_I2
: OP_PCONV_TO_U2
;
2643 /* The definition of OP_PCONV_TO_U2 is wrong */
2644 ins
->opcode
= (ins
->opcode
== OP_LOADI2_MEMBASE
) ? OP_PCONV_TO_I2
: OP_ICONV_TO_U2
;
2646 ins
->sreg1
= last_ins
->sreg1
;
2649 case OP_LOADX_MEMBASE
:
2650 if (last_ins
&& last_ins
->opcode
== OP_STOREX_MEMBASE
&&
2651 ins
->inst_basereg
== last_ins
->inst_destbasereg
&&
2652 ins
->inst_offset
== last_ins
->inst_offset
) {
2653 if (ins
->dreg
== last_ins
->sreg1
) {
2654 MONO_DELETE_INS (bb
, ins
);
2657 ins
->opcode
= OP_XMOVE
;
2658 ins
->sreg1
= last_ins
->sreg1
;
2669 if (ins
->dreg
== ins
->sreg1
) {
2670 MONO_DELETE_INS (bb
, ins
);
2676 * OP_MOVE sreg, dreg
2677 * OP_MOVE dreg, sreg
2679 if (last_ins
&& last_ins
->opcode
== ins
->opcode
&&
2680 ins
->sreg1
== last_ins
->dreg
&&
2681 ins
->dreg
== last_ins
->sreg1
) {
2682 MONO_DELETE_INS (bb
, ins
);
2686 MONO_DELETE_INS (bb
, ins
);
2692 mini_exception_id_by_name (const char *name
)
2694 if (strcmp (name
, "NullReferenceException") == 0)
2695 return MONO_EXC_NULL_REF
;
2696 if (strcmp (name
, "IndexOutOfRangeException") == 0)
2697 return MONO_EXC_INDEX_OUT_OF_RANGE
;
2698 if (strcmp (name
, "OverflowException") == 0)
2699 return MONO_EXC_OVERFLOW
;
2700 if (strcmp (name
, "ArithmeticException") == 0)
2701 return MONO_EXC_ARITHMETIC
;
2702 if (strcmp (name
, "DivideByZeroException") == 0)
2703 return MONO_EXC_DIVIDE_BY_ZERO
;
2704 if (strcmp (name
, "InvalidCastException") == 0)
2705 return MONO_EXC_INVALID_CAST
;
2706 if (strcmp (name
, "ArrayTypeMismatchException") == 0)
2707 return MONO_EXC_ARRAY_TYPE_MISMATCH
;
2708 if (strcmp (name
, "ArgumentException") == 0)
2709 return MONO_EXC_ARGUMENT
;
2710 g_error ("Unknown intrinsic exception %s\n", name
);
2715 mini_type_is_hfa (MonoType
*t
, int *out_nfields
, int *out_esize
)
2719 MonoClassField
*field
;
2720 MonoType
*ftype
, *prev_ftype
= NULL
;
2723 klass
= mono_class_from_mono_type_internal (t
);
2725 while ((field
= mono_class_get_fields_internal (klass
, &iter
))) {
2726 if (field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
)
2728 ftype
= mono_field_get_type_internal (field
);
2729 ftype
= mini_native_type_replace_type (ftype
);
2731 if (MONO_TYPE_ISSTRUCT (ftype
)) {
2732 int nested_nfields
, nested_esize
;
2734 if (!mini_type_is_hfa (ftype
, &nested_nfields
, &nested_esize
))
2736 if (nested_esize
== 4)
2737 ftype
= m_class_get_byval_arg (mono_defaults
.single_class
);
2739 ftype
= m_class_get_byval_arg (mono_defaults
.double_class
);
2740 if (prev_ftype
&& prev_ftype
->type
!= ftype
->type
)
2743 nfields
+= nested_nfields
;
2745 if (!(!ftype
->byref
&& (ftype
->type
== MONO_TYPE_R4
|| ftype
->type
== MONO_TYPE_R8
)))
2747 if (prev_ftype
&& prev_ftype
->type
!= ftype
->type
)
2755 *out_nfields
= nfields
;
2756 *out_esize
= prev_ftype
->type
== MONO_TYPE_R4
? 4 : 8;
2761 mono_regstate_new (void)
2763 MonoRegState
* rs
= g_new0 (MonoRegState
, 1);
2765 rs
->next_vreg
= MAX (MONO_MAX_IREGS
, MONO_MAX_FREGS
);
2766 #ifdef MONO_ARCH_NEED_SIMD_BANK
2767 rs
->next_vreg
= MAX (rs
->next_vreg
, MONO_MAX_XREGS
);
2774 mono_regstate_free (MonoRegState
*rs
) {
2775 g_free (rs
->vassign
);
2779 #endif /* DISABLE_JIT */
2782 mono_is_regsize_var (MonoType
*t
)
2784 t
= mini_get_underlying_type (t
);
2795 case MONO_TYPE_FNPTR
:
2796 #if SIZEOF_REGISTER == 8
2801 case MONO_TYPE_OBJECT
:
2802 case MONO_TYPE_STRING
:
2803 case MONO_TYPE_CLASS
:
2804 case MONO_TYPE_SZARRAY
:
2805 case MONO_TYPE_ARRAY
:
2807 case MONO_TYPE_GENERICINST
:
2808 if (!mono_type_generic_inst_is_valuetype (t
))
2811 case MONO_TYPE_VALUETYPE
: