+
+ if (cfg->new_ir) {
+ if (!cfg->disable_initlocals_opt)
+ optimize_initlocals2 (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);
+#endif
+ }
+ else {
+ if (!cfg->disable_initlocals_opt)
+ optimize_initlocals (cfg);
+ }
+}
+
+/**
+ * optimize_initlocals:
+ *
+ * Try to optimize away some of the redundant initialization code inserted because of
+ * 'locals init' using the liveness information.
+ */
+static void
+optimize_initlocals2 (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) {
+ const char *spec = INS_INFO (ins->opcode);
+
+ 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);
+ }
+
+ 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 ((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);
+ }
+ }
+}
+
+#ifdef ENABLE_LIVENESS2
+
+#if 0
+#define LIVENESS_DEBUG(a) do { a; } while (0)
+#else
+#define LIVENESS_DEBUG(a)
+#endif
+
+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;
+
+ 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 = ins->sreg1 = ins->sreg2 = -1;
+ 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);
+ }
+ }
+ }
+
+ /* 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;
+
+ 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;
+ }
+ }