+
+ 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))) {
+ /*
+ * 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;
+ }
+ }
+
+#ifdef DEBUG_LIVENESS
+ if (cfg->verbose_level > 1) {
+ for (i = cfg->num_bblocks - 1; i >= 0; i--) {
+ MonoBasicBlock *bb = cfg->bblocks [i];
+
+ printf ("LIVE IN BB%d: ", bb->block_num);
+ mono_bitset_print (bb->live_in_set);
+ printf ("LIVE OUT BB%d: ", bb->block_num);
+ mono_bitset_print (bb->live_out_set);
+ }
+
+ for (i = 0; i < max_vars; i ++) {
+ MonoMethodVar *vi = MONO_VARINFO (cfg, i);
+
+ printf ("V%d: [0x%x - 0x%x]\n", i, vi->range.first_use.abs_pos, vi->range.last_use.abs_pos);
+ }
+ }
+#endif
+
+ if (!cfg->disable_initlocals_opt)
+ optimize_initlocals (cfg);
+
+#ifdef ENABLE_LIVENESS2
+ /* This improves code size by about 5% but slows down compilation too much */
+ if (cfg->compile_aot)
+ mono_analyze_liveness2 (cfg);
+#endif
+}
+
+/**
+ * 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->next_vreg + 1, 0);
+
+ mono_bitset_clear_all (used);
+ initlocals_bb = cfg->bb_entry->next_bb;
+ for (ins = initlocals_bb->code; ins; ins = ins->next) {
+ 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 (MONO_IS_STORE_MEMBASE (ins))
+ mono_bitset_set_fast (used, ins->dreg);
+ }
+
+ for (ins = initlocals_bb->code; ins; ins = ins->next) {
+ const char *spec = INS_INFO (ins->opcode);
+
+ /* Look for statements whose dest is not used in this bblock and not live on exit. */
+ if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
+ MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
+
+ if (var && !mono_bitset_test_fast (used, ins->dreg) && !mono_bitset_test_fast (initlocals_bb->live_out_set, var->inst_c0) && (var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
+ //printf ("DEAD: "); mono_print_ins (ins);
+ if (cfg->disable_initlocals_opt_refs && var->type == STACK_OBJ)
+ continue;
+ if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) {
+ NULLIFY_INS (ins);
+ MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
+ /*
+ * We should shorten the liveness interval of these vars as well, but
+ * don't have enough info to do that.
+ */
+ }
+ }
+ }
+ }
+
+ g_free (used);
+}
+
+void
+mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
+{
+ MonoLiveRange2 *prev_range, *next_range, *new_range;
+
+ g_assert (to >= from);
+
+ /* Optimize for extending the first interval backwards */
+ if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
+ interval->range->from = from;
+ return;
+ }
+
+ /* Find a place in the list for the new range */
+ prev_range = NULL;
+ next_range = interval->range;
+ while ((next_range != NULL) && (next_range->from <= from)) {
+ prev_range = next_range;
+ next_range = next_range->next;
+ }
+
+ if (prev_range && prev_range->to == from) {
+ /* Merge with previous */
+ prev_range->to = to;
+ } else if (next_range && next_range->from == to) {
+ /* Merge with previous */
+ next_range->from = from;
+ } else {
+ /* Insert it */
+ new_range = mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
+ new_range->from = from;
+ new_range->to = to;
+ new_range->next = NULL;
+
+ if (prev_range)
+ prev_range->next = new_range;
+ else
+ interval->range = new_range;
+ if (next_range)
+ new_range->next = next_range;
+ else
+ interval->last_range = new_range;
+ }
+
+ /* FIXME: Merge intersecting ranges */
+}
+
+void
+mono_linterval_print (MonoLiveInterval *interval)
+{
+ MonoLiveRange2 *range;
+
+ for (range = interval->range; range != NULL; range = range->next)
+ printf ("[%x-%x] ", range->from, range->to);
+}
+
+void
+mono_linterval_print_nl (MonoLiveInterval *interval)
+{
+ mono_linterval_print (interval);
+ printf ("\n");
+}
+
+/**
+ * mono_linterval_convers:
+ *
+ * Return whenever INTERVAL covers the position POS.
+ */
+gboolean
+mono_linterval_covers (MonoLiveInterval *interval, int pos)
+{
+ MonoLiveRange2 *range;
+
+ for (range = interval->range; range != NULL; range = range->next) {
+ if (pos >= range->from && pos <= range->to)
+ return TRUE;
+ if (range->from > pos)
+ return FALSE;
+ }
+
+ return FALSE;
+}
+
+/**
+ * mono_linterval_get_intersect_pos:
+ *
+ * Determine whenever I1 and I2 intersect, and if they do, return the first
+ * point of intersection. Otherwise, return -1.
+ */
+gint32
+mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
+{
+ MonoLiveRange2 *r1, *r2;
+
+ /* FIXME: Optimize this */
+ for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
+ for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
+ if (r2->to > r1->from && r2->from < r1->to) {
+ if (r2->from <= r1->from)
+ return r1->from;
+ else
+ return r2->from;
+ }
+ }
+ }
+
+ return -1;
+}
+
+/**
+ * mono_linterval_split
+ *
+ * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
+ * part of L2.
+ */
+void
+mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
+{
+ MonoLiveRange2 *r;
+
+ g_assert (pos > interval->range->from && pos <= interval->last_range->to);
+
+ *i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
+ *i2 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
+
+ for (r = interval->range; r; r = r->next) {
+ if (pos > r->to) {
+ /* Add it to the first child */
+ mono_linterval_add_range (cfg, *i1, r->from, r->to);
+ } else if (pos > r->from && pos <= r->to) {
+ /* Split at pos */
+ mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
+ mono_linterval_add_range (cfg, *i2, pos, r->to);
+ } else {
+ /* Add it to the second child */
+ mono_linterval_add_range (cfg, *i2, r->from, r->to);
+ }
+ }
+}
+
+#if 1
+#define LIVENESS_DEBUG(a) do { if (cfg->verbose_level > 1) do { a; } while (0); } while (0)
+#define ENABLE_LIVENESS_DEBUG 1
+#else
+#define LIVENESS_DEBUG(a)
+#endif
+
+#ifdef ENABLE_LIVENESS2
+
+static inline void
+update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
+{
+ 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 (ins->opcode == OP_NOP)
+ return;
+
+ /* DREG */
+ if ((spec [MONO_INST_DEST] != ' ') && 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);
+
+ if (MONO_IS_STORE_MEMBASE (ins)) {
+ if (last_use [idx] == 0) {
+ LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
+ last_use [idx] = inst_num;
+ }
+ } else {
+ if (last_use [idx] > 0) {
+ LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
+ mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
+ last_use [idx] = 0;
+ }
+ else {
+ /* Try dead code elimination */
+ 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 = -1;
+ MONO_INST_NULLIFY_SREGS (ins);
+ return;
+ }
+
+ LIVENESS_DEBUG (printf ("\tdead def of R%d, add range to R%d: [%x, %x]\n", ins->dreg, ins->dreg, inst_num, inst_num + 1));
+ mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + 1);
+ }
+ }
+ }
+
+ /* 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;
+ }
+ }
+ }
+}
+
+static void
+mono_analyze_liveness2 (MonoCompile *cfg)
+{
+ int bnum, idx, i, j, nins, rem, max, max_vars, block_from, block_to, pos, reverse_len;
+ gint32 *last_use;
+ static guint32 disabled = -1;
+ MonoInst **reverse;
+
+ if (disabled == -1)
+ disabled = getenv ("DISABLED") != NULL;
+
+ if (disabled)
+ return;
+
+ LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
+
+ /*
+ if (strstr (cfg->method->name, "test_") != cfg->method->name)
+ return;
+ */
+
+ max_vars = cfg->num_varinfo;
+ last_use = g_new0 (gint32, max_vars);
+
+ reverse_len = 1024;
+ reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
+
+ for (idx = 0; idx < max_vars; ++idx) {
+ MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
+
+ vi->interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
+ }
+
+ /*
+ * Process bblocks in reverse order, so the addition of new live ranges
+ * to the intervals is faster.