*/
#include "mini.h"
-#include "inssel.h"
-#include "aliasing.h"
#define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
//#define DEBUG_LIVENESS
-#if SIZEOF_VOID_P == 8
-#define BITS_PER_CHUNK 64
-#else
-#define BITS_PER_CHUNK 32
-#endif
+#define BITS_PER_CHUNK MONO_BITSET_BITS_PER_CHUNK
/*
* The liveness2 pass can't handle long vars on 32 bit platforms because the component
* vars have the same 'idx'.
*/
-#if SIZEOF_VOID_P == 8
-//#define ENABLE_LIVENESS2
+#if SIZEOF_REGISTER == 8
+#define ENABLE_LIVENESS2
#endif
#ifdef ENABLE_LIVENESS2
static void
optimize_initlocals (MonoCompile *cfg);
-static void
-optimize_initlocals2 (MonoCompile *cfg);
-
/* mono_bitset_mp_new:
*
* allocates a MonoBitSet inside a memory pool
printf ("}\n");
}
-static inline void
-update_live_range (MonoCompile *cfg, int idx, int block_dfn, int tree_pos)
-{
- MonoLiveRange *range = &MONO_VARINFO (cfg, idx)->range;
- guint32 abs_pos = (block_dfn << 16) | tree_pos;
-
- if (range->first_use.abs_pos > abs_pos)
- range->first_use.abs_pos = abs_pos;
-
- if (range->last_use.abs_pos < abs_pos)
- range->last_use.abs_pos = abs_pos;
-}
-
-static void
-update_gen_kill_set (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, int inst_num)
-{
- int arity;
- int max_vars = cfg->num_varinfo;
-
- arity = mono_burg_arity [inst->opcode];
- if (arity)
- update_gen_kill_set (cfg, bb, inst->inst_i0, inst_num);
-
- if (arity > 1)
- update_gen_kill_set (cfg, bb, inst->inst_i1, inst_num);
-
- if ((inst->ssa_op & MONO_SSA_LOAD_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
- MonoLocalVariableList* affected_variables;
- MonoLocalVariableList local_affected_variable;
-
- if (cfg->aliasing_info == NULL) {
- if ((inst->ssa_op == MONO_SSA_LOAD) || (inst->ssa_op == MONO_SSA_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
- local_affected_variable.variable_index = inst->inst_i0->inst_c0;
- local_affected_variable.next = NULL;
- affected_variables = &local_affected_variable;
- } else {
- affected_variables = NULL;
- }
- } else {
- affected_variables = mono_aliasing_get_affected_variables_for_inst_traversing_code (cfg->aliasing_info, inst);
- }
-
- if (inst->ssa_op & MONO_SSA_LOAD) {
- MonoLocalVariableList* affected_variable = affected_variables;
- while (affected_variable != NULL) {
- int idx = affected_variable->variable_index;
- MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
- g_assert (idx < max_vars);
- update_live_range (cfg, idx, bb->dfn, inst_num);
- if (!mono_bitset_test_fast (bb->kill_set, idx))
- mono_bitset_set_fast (bb->gen_set, idx);
- if (inst->ssa_op == MONO_SSA_LOAD)
- vi->spill_costs += SPILL_COST_INCREMENT;
-
- affected_variable = affected_variable->next;
- }
- } else if ((inst->ssa_op == MONO_SSA_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
- MonoLocalVariableList* affected_variable = affected_variables;
- while (affected_variable != NULL) {
- int idx = affected_variable->variable_index;
- MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
- g_assert (idx < max_vars);
- //if (arity > 0)
- //g_assert (inst->inst_i1->opcode != OP_PHI);
- update_live_range (cfg, idx, bb->dfn, inst_num);
- mono_bitset_set_fast (bb->kill_set, idx);
- if (inst->ssa_op == MONO_SSA_STORE)
- vi->spill_costs += SPILL_COST_INCREMENT;
-
- affected_variable = affected_variable->next;
- }
- }
- } else if (inst->opcode == OP_JMP) {
- /* Keep arguments live! */
- int i;
- for (i = 0; i < cfg->num_varinfo; i++) {
- if (cfg->varinfo [i]->opcode == OP_ARG) {
- if (!mono_bitset_test_fast (bb->kill_set, i))
- mono_bitset_set_fast (bb->gen_set, i);
- }
- }
- }
-}
-
-static void
-update_volatile (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst)
-{
- int arity = mono_burg_arity [inst->opcode];
- int max_vars = cfg->num_varinfo;
-
- if (arity)
- update_volatile (cfg, bb, inst->inst_i0);
-
- if (arity > 1)
- update_volatile (cfg, bb, inst->inst_i1);
-
- if (inst->ssa_op & MONO_SSA_LOAD_STORE) {
- MonoLocalVariableList* affected_variables;
- MonoLocalVariableList local_affected_variable;
-
- if (cfg->aliasing_info == NULL) {
- if ((inst->ssa_op == MONO_SSA_LOAD) || (inst->ssa_op == MONO_SSA_STORE)) {
- local_affected_variable.variable_index = inst->inst_i0->inst_c0;
- local_affected_variable.next = NULL;
- affected_variables = &local_affected_variable;
- } else {
- affected_variables = NULL;
- }
- } else {
- affected_variables = mono_aliasing_get_affected_variables_for_inst_traversing_code (cfg->aliasing_info, inst);
- }
-
- while (affected_variables != NULL) {
- int idx = affected_variables->variable_index;
- MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
- g_assert (idx < max_vars);
- cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
-
- affected_variables = affected_variables->next;
- }
- }
-}
-
static void
visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
{
if (g_slist_find (*visited, bb))
return;
- if (cfg->new_ir) {
- for (ins = bb->code; ins; ins = ins->next) {
- const char *spec = INS_INFO (ins->opcode);
- int regtype, srcindex, sreg;
+ for (ins = bb->code; ins; ins = ins->next) {
+ const char *spec = INS_INFO (ins->opcode);
+ int regtype, srcindex, sreg, num_sregs;
+ int sregs [MONO_MAX_SRC_REGS];
- if (ins->opcode == OP_NOP)
- continue;
+ if (ins->opcode == OP_NOP)
+ continue;
- /* DREG */
- regtype = spec [MONO_INST_DEST];
- g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
+ /* DREG */
+ regtype = spec [MONO_INST_DEST];
+ g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
- if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
- MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
+ if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
+ MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
+ int idx = var->inst_c0;
+ MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
+
+ cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
+ }
+
+ /* SREGS */
+ num_sregs = mono_inst_get_src_registers (ins, sregs);
+ for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
+ sreg = sregs [srcindex];
+
+ g_assert (sreg != -1);
+ if (get_vreg_to_inst (cfg, sreg)) {
+ MonoInst *var = get_vreg_to_inst (cfg, sreg);
int idx = var->inst_c0;
MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
}
-
- /* SREGS */
- for (srcindex = 0; srcindex < 2; ++srcindex) {
- regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 : MONO_INST_SRC2];
- sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
-
- g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
- if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
- MonoInst *var = get_vreg_to_inst (cfg, sreg);
- int idx = var->inst_c0;
- MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
-
- cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
- }
- }
- }
- } else {
- if (cfg->aliasing_info != NULL)
- mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
-
- for (ins = bb->code; ins; ins = ins->next) {
- update_volatile (cfg, bb, ins);
}
}
}
static inline void
-update_live_range2 (MonoMethodVar *var, int abs_pos)
+update_live_range (MonoMethodVar *var, int abs_pos)
{
if (var->range.first_use.abs_pos > abs_pos)
var->range.first_use.abs_pos = abs_pos;
for (inst_num = 0, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
const char *spec = INS_INFO (ins->opcode);
+ int num_sregs, i;
+ int sregs [MONO_MAX_SRC_REGS];
#ifdef DEBUG_LIVENESS
printf ("\t"); mono_print_ins (ins);
#ifdef DEBUG_LIVENESS
printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
#endif
- update_live_range2 (&vars [idx], abs_pos + inst_num);
+ update_live_range (&vars [idx], abs_pos + inst_num);
if (!mono_bitset_test_fast (bb->kill_set, idx))
mono_bitset_set_fast (bb->gen_set, idx);
vi->spill_costs += SPILL_COST_INCREMENT;
}
/* SREGs must come first, so MOVE r <- r is handled correctly */
-
- /* SREG1 */
- sreg = ins->sreg1;
- if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
- MonoInst *var = get_vreg_to_inst (cfg, sreg);
- int idx = var->inst_c0;
- MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
-
-#ifdef DEBUG_LIVENESS
- printf ("\tGEN: R%d(%d)\n", sreg, idx);
-#endif
- update_live_range2 (&vars [idx], abs_pos + inst_num);
- if (!mono_bitset_test_fast (bb->kill_set, idx))
- mono_bitset_set_fast (bb->gen_set, idx);
- vi->spill_costs += SPILL_COST_INCREMENT;
- }
-
- /* SREG2 */
- sreg = ins->sreg2;
- if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
- MonoInst *var = get_vreg_to_inst (cfg, sreg);
- int idx = var->inst_c0;
- MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
+ num_sregs = mono_inst_get_src_registers (ins, sregs);
+ for (i = 0; i < num_sregs; ++i) {
+ sreg = sregs [i];
+ if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
+ MonoInst *var = get_vreg_to_inst (cfg, sreg);
+ int idx = var->inst_c0;
+ MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
#ifdef DEBUG_LIVENESS
- printf ("\tGEN: R%d(%d)\n", sreg, idx);
+ printf ("\tGEN: R%d(%d)\n", sreg, idx);
#endif
- update_live_range2 (&vars [idx], abs_pos + inst_num);
- if (!mono_bitset_test_fast (bb->kill_set, idx))
- mono_bitset_set_fast (bb->gen_set, idx);
- vi->spill_costs += SPILL_COST_INCREMENT;
+ update_live_range (&vars [idx], abs_pos + inst_num);
+ if (!mono_bitset_test_fast (bb->kill_set, idx))
+ mono_bitset_set_fast (bb->gen_set, idx);
+ vi->spill_costs += SPILL_COST_INCREMENT;
+ }
}
/* DREG */
MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
if (MONO_IS_STORE_MEMBASE (ins)) {
- update_live_range2 (&vars [idx], abs_pos + inst_num);
+ update_live_range (&vars [idx], abs_pos + inst_num);
if (!mono_bitset_test_fast (bb->kill_set, idx))
mono_bitset_set_fast (bb->gen_set, idx);
vi->spill_costs += SPILL_COST_INCREMENT;
#ifdef DEBUG_LIVENESS
printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
#endif
- update_live_range2 (&vars [idx], abs_pos + inst_num + 1);
+ update_live_range (&vars [idx], abs_pos + inst_num + 1);
mono_bitset_set_fast (bb->kill_set, idx);
vi->spill_costs += SPILL_COST_INCREMENT;
}
for (i = 0; i < cfg->num_bblocks; ++i) {
MonoBasicBlock *bb = cfg->bblocks [i];
- MonoInst *inst;
- int tree_num;
bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
- if (cfg->new_ir) {
- analyze_liveness_bb (cfg, bb);
- } else {
- if (cfg->aliasing_info != NULL)
- mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
-
- tree_num = 0;
- MONO_BB_FOR_EACH_INS (bb, inst) {
-#ifdef DEBUG_LIVENESS
- mono_print_tree (inst); printf ("\n");
-#endif
- update_gen_kill_set (cfg, bb, inst, tree_num);
- tree_num ++;
- }
- }
+ analyze_liveness_bb (cfg, bb);
#ifdef DEBUG_LIVENESS
printf ("BLOCK BB%d (", bb->block_num);
k = (j * BITS_PER_CHUNK);
while ((bits_in || bits_out)) {
if (bits_in & 1)
- update_live_range2 (&vars [k], abs_pos + 0);
+ update_live_range (&vars [k], abs_pos + 0);
if (bits_out & 1)
- update_live_range2 (&vars [k], abs_pos + 0xffff);
+ update_live_range (&vars [k], abs_pos + 0xffff);
bits_in >>= 1;
bits_out >>= 1;
k ++;
for (i = 0; i < max_vars; i ++) {
MonoMethodVar *vi = MONO_VARINFO (cfg, i);
if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
- if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
- cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
+ if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
+ /*
+ * Can't eliminate the this variable in gshared code, since
+ * it is needed during exception handling to identify the
+ * method.
+ * It is better to check for this here instead of marking the variable
+ * VOLATILE, since that would prevent it from being allocated to
+ * registers.
+ */
+ if (!cfg->disable_deadce_vars && !(cfg->generic_sharing_context && mono_method_signature (cfg->method)->hasthis && cfg->varinfo [vi->idx] == cfg->args [0]))
+ cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
+ }
vi->range.first_use.abs_pos = 0;
}
}
}
#endif
- if (cfg->new_ir) {
- if (!cfg->disable_initlocals_opt)
- optimize_initlocals2 (cfg);
+ if (!cfg->disable_initlocals_opt)
+ optimize_initlocals (cfg);
#ifdef ENABLE_LIVENESS2
- /* This improves code size by about 5% but slows down compilation too much */
- /* FIXME: This crashes when compiling 2.0 mscorlib */
- if (FALSE && cfg->compile_aot)
- mono_analyze_liveness2 (cfg);
+ /* This improves code size by about 5% but slows down compilation too much */
+ if (cfg->compile_aot)
+ mono_analyze_liveness2 (cfg);
#endif
- }
- else {
- if (!cfg->disable_initlocals_opt)
- optimize_initlocals (cfg);
- }
}
/**
* 'locals init' using the liveness information.
*/
static void
-optimize_initlocals2 (MonoCompile *cfg)
+optimize_initlocals (MonoCompile *cfg)
{
MonoBitSet *used;
MonoInst *ins;
mono_bitset_clear_all (used);
initlocals_bb = cfg->bb_entry->next_bb;
for (ins = initlocals_bb->code; ins; ins = ins->next) {
- const char *spec = INS_INFO (ins->opcode);
+ int num_sregs, i;
+ int sregs [MONO_MAX_SRC_REGS];
+
+ num_sregs = mono_inst_get_src_registers (ins, sregs);
+ for (i = 0; i < num_sregs; ++i)
+ mono_bitset_set_fast (used, sregs [i]);
- if (spec [MONO_INST_SRC1] != ' ')
- mono_bitset_set_fast (used, ins->sreg1);
- if (spec [MONO_INST_SRC2] != ' ')
- mono_bitset_set_fast (used, ins->sreg2);
if (MONO_IS_STORE_MEMBASE (ins))
mono_bitset_set_fast (used, ins->dreg);
}
{
const char *spec = INS_INFO (ins->opcode);
int sreg;
+ int num_sregs, i;
+ int sregs [MONO_MAX_SRC_REGS];
LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
if ((var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) && !(var->flags & MONO_INST_VOLATILE)) {
LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
ins->opcode = OP_NOP;
- ins->dreg = ins->sreg1 = ins->sreg2 = -1;
+ ins->dreg = -1;
+ MONO_INST_NULLIFY_SREGS (ins);
return;
}
}
}
- /* SREG1 */
- sreg = ins->sreg1;
- if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
- MonoInst *var = get_vreg_to_inst (cfg, sreg);
- int idx = var->inst_c0;
-
- if (last_use [idx] == 0) {
- LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
- last_use [idx] = inst_num;
- }
- }
-
- /* SREG2 */
- sreg = ins->sreg2;
- if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
- MonoInst *var = get_vreg_to_inst (cfg, sreg);
- int idx = var->inst_c0;
+ /* SREGs */
+ num_sregs = mono_inst_get_src_registers (ins, sregs);
+ for (i = 0; i < num_sregs; ++i) {
+ sreg = sregs [i];
+ if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
+ MonoInst *var = get_vreg_to_inst (cfg, sreg);
+ int idx = var->inst_c0;
- if (last_use [idx] == 0) {
- LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
- last_use [idx] = inst_num;
+ if (last_use [idx] == 0) {
+ LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
+ last_use [idx] = inst_num;
+ }
}
}
}
}
#endif
-
-static void
-update_used (MonoCompile *cfg, MonoInst *inst, MonoBitSet *used)
-{
- int arity = mono_burg_arity [inst->opcode];
-
- if (arity)
- update_used (cfg, inst->inst_i0, used);
-
- if (arity > 1)
- update_used (cfg, inst->inst_i1, used);
-
- if (inst->ssa_op & MONO_SSA_LOAD_STORE) {
- if (inst->ssa_op == MONO_SSA_LOAD) {
- int idx = inst->inst_i0->inst_c0;
-
- mono_bitset_set_fast (used, idx);
- }
- }
-}
-
-/**
- * optimize_initlocals:
- *
- * Try to optimize away some of the redundant initialization code inserted because of
- * 'locals init' using the liveness information.
- */
-static void
-optimize_initlocals (MonoCompile *cfg)
-{
- MonoBitSet *used;
- MonoInst *ins;
- MonoBasicBlock *initlocals_bb;
-
- used = mono_bitset_new (cfg->num_varinfo, 0);
-
- mono_bitset_clear_all (used);
- initlocals_bb = cfg->bb_entry->next_bb;
- MONO_BB_FOR_EACH_INS (initlocals_bb, ins)
- update_used (cfg, ins, used);
-
- MONO_BB_FOR_EACH_INS (initlocals_bb, ins) {
- if (ins->ssa_op == MONO_SSA_STORE) {
- int idx = ins->inst_i0->inst_c0;
- MonoInst *var = cfg->varinfo [idx];
-
- if (var && !mono_bitset_test_fast (used, idx) && !mono_bitset_test_fast (initlocals_bb->live_out_set, var->inst_c0) && (var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
- if (ins->inst_i1 && ((ins->inst_i1->opcode == OP_ICONST) || (ins->inst_i1->opcode == OP_I8CONST))) {
- NULLIFY_INS (ins);
- ins->ssa_op = MONO_SSA_NOP;
- MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
- }
- }
- }
- }
-
- g_free (used);
-}
-