2 * liveness.c: liveness analysis
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2002 Ximian, Inc.
14 #define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
16 //#define DEBUG_LIVENESS
18 #if SIZEOF_VOID_P == 8
19 #define BITS_PER_CHUNK 64
21 #define BITS_PER_CHUNK 32
25 * The liveness2 pass can't handle long vars on 32 bit platforms because the component
26 * vars have the same 'idx'.
28 #if SIZEOF_VOID_P == 8
29 #define ENABLE_LIVENESS2
31 static void mono_analyze_liveness2 (MonoCompile *cfg);
35 optimize_initlocals (MonoCompile *cfg);
38 optimize_initlocals2 (MonoCompile *cfg);
40 /* mono_bitset_mp_new:
42 * allocates a MonoBitSet inside a memory pool
44 static inline MonoBitSet*
45 mono_bitset_mp_new (MonoMemPool *mp, guint32 size, guint32 max_size)
47 guint8 *mem = mono_mempool_alloc0 (mp, size);
48 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
51 static inline MonoBitSet*
52 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 size, guint32 max_size)
54 guint8 *mem = mono_mempool_alloc (mp, size);
55 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
58 G_GNUC_UNUSED static void
59 mono_bitset_print (MonoBitSet *set)
64 for (i = 0; i < mono_bitset_size (set); i++) {
66 if (mono_bitset_test (set, i))
74 update_live_range (MonoCompile *cfg, int idx, int block_dfn, int tree_pos)
76 MonoLiveRange *range = &MONO_VARINFO (cfg, idx)->range;
77 guint32 abs_pos = (block_dfn << 16) | tree_pos;
79 if (range->first_use.abs_pos > abs_pos)
80 range->first_use.abs_pos = abs_pos;
82 if (range->last_use.abs_pos < abs_pos)
83 range->last_use.abs_pos = abs_pos;
87 update_gen_kill_set (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, int inst_num)
90 int max_vars = cfg->num_varinfo;
92 arity = mono_burg_arity [inst->opcode];
94 update_gen_kill_set (cfg, bb, inst->inst_i0, inst_num);
97 update_gen_kill_set (cfg, bb, inst->inst_i1, inst_num);
99 if ((inst->ssa_op & MONO_SSA_LOAD_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
100 MonoLocalVariableList* affected_variables;
101 MonoLocalVariableList local_affected_variable;
103 if (cfg->aliasing_info == NULL) {
104 if ((inst->ssa_op == MONO_SSA_LOAD) || (inst->ssa_op == MONO_SSA_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
105 local_affected_variable.variable_index = inst->inst_i0->inst_c0;
106 local_affected_variable.next = NULL;
107 affected_variables = &local_affected_variable;
109 affected_variables = NULL;
112 affected_variables = mono_aliasing_get_affected_variables_for_inst_traversing_code (cfg->aliasing_info, inst);
115 if (inst->ssa_op & MONO_SSA_LOAD) {
116 MonoLocalVariableList* affected_variable = affected_variables;
117 while (affected_variable != NULL) {
118 int idx = affected_variable->variable_index;
119 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
120 g_assert (idx < max_vars);
121 update_live_range (cfg, idx, bb->dfn, inst_num);
122 if (!mono_bitset_test_fast (bb->kill_set, idx))
123 mono_bitset_set_fast (bb->gen_set, idx);
124 if (inst->ssa_op == MONO_SSA_LOAD)
125 vi->spill_costs += SPILL_COST_INCREMENT;
127 affected_variable = affected_variable->next;
129 } else if ((inst->ssa_op == MONO_SSA_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
130 MonoLocalVariableList* affected_variable = affected_variables;
131 while (affected_variable != NULL) {
132 int idx = affected_variable->variable_index;
133 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
134 g_assert (idx < max_vars);
136 //g_assert (inst->inst_i1->opcode != OP_PHI);
137 update_live_range (cfg, idx, bb->dfn, inst_num);
138 mono_bitset_set_fast (bb->kill_set, idx);
139 if (inst->ssa_op == MONO_SSA_STORE)
140 vi->spill_costs += SPILL_COST_INCREMENT;
142 affected_variable = affected_variable->next;
145 } else if (inst->opcode == OP_JMP) {
146 /* Keep arguments live! */
148 for (i = 0; i < cfg->num_varinfo; i++) {
149 if (cfg->varinfo [i]->opcode == OP_ARG) {
150 if (!mono_bitset_test_fast (bb->kill_set, i))
151 mono_bitset_set_fast (bb->gen_set, i);
158 update_volatile (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst)
160 int arity = mono_burg_arity [inst->opcode];
161 int max_vars = cfg->num_varinfo;
164 update_volatile (cfg, bb, inst->inst_i0);
167 update_volatile (cfg, bb, inst->inst_i1);
169 if (inst->ssa_op & MONO_SSA_LOAD_STORE) {
170 MonoLocalVariableList* affected_variables;
171 MonoLocalVariableList local_affected_variable;
173 if (cfg->aliasing_info == NULL) {
174 if ((inst->ssa_op == MONO_SSA_LOAD) || (inst->ssa_op == MONO_SSA_STORE)) {
175 local_affected_variable.variable_index = inst->inst_i0->inst_c0;
176 local_affected_variable.next = NULL;
177 affected_variables = &local_affected_variable;
179 affected_variables = NULL;
182 affected_variables = mono_aliasing_get_affected_variables_for_inst_traversing_code (cfg->aliasing_info, inst);
185 while (affected_variables != NULL) {
186 int idx = affected_variables->variable_index;
187 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
188 g_assert (idx < max_vars);
189 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
191 affected_variables = affected_variables->next;
197 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
202 if (g_slist_find (*visited, bb))
206 for (ins = bb->code; ins; ins = ins->next) {
207 const char *spec = INS_INFO (ins->opcode);
208 int regtype, srcindex, sreg;
210 if (ins->opcode == OP_NOP)
214 regtype = spec [MONO_INST_DEST];
215 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
217 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
218 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
219 int idx = var->inst_c0;
220 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
222 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
226 for (srcindex = 0; srcindex < 2; ++srcindex) {
227 regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 : MONO_INST_SRC2];
228 sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
230 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
231 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
232 MonoInst *var = get_vreg_to_inst (cfg, sreg);
233 int idx = var->inst_c0;
234 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
236 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
241 if (cfg->aliasing_info != NULL)
242 mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
244 for (ins = bb->code; ins; ins = ins->next) {
245 update_volatile (cfg, bb, ins);
249 *visited = g_slist_append (*visited, bb);
252 * Need to visit all bblocks reachable from this one since they can be
253 * reached during exception handling.
255 for (i = 0; i < bb->out_count; ++i) {
256 visit_bb (cfg, bb->out_bb [i], visited);
261 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
264 GSList *visited = NULL;
267 * Variables in exception handler register cannot be allocated to registers
268 * so make them volatile. See bug #42136. This will not be neccessary when
269 * the back ends could guarantee that the variables will be in the
270 * correct registers when a handler is called.
271 * This includes try blocks too, since a variable in a try block might be
272 * accessed after an exception handler has been run.
274 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
276 if (bb->region == -1 || MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
279 visit_bb (cfg, bb, &visited);
281 g_slist_free (visited);
285 update_live_range2 (MonoMethodVar *var, int abs_pos)
287 if (var->range.first_use.abs_pos > abs_pos)
288 var->range.first_use.abs_pos = abs_pos;
290 if (var->range.last_use.abs_pos < abs_pos)
291 var->range.last_use.abs_pos = abs_pos;
295 analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
299 MonoMethodVar *vars = cfg->vars;
300 guint32 abs_pos = (bb->dfn << 16);
302 for (inst_num = 0, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
303 const char *spec = INS_INFO (ins->opcode);
305 #ifdef DEBUG_LIVENESS
306 printf ("\t"); mono_print_ins (ins);
309 if (ins->opcode == OP_NOP)
312 if (ins->opcode == OP_LDADDR) {
313 MonoInst *var = ins->inst_p0;
314 int idx = var->inst_c0;
315 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
317 #ifdef DEBUG_LIVENESS
318 printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
320 update_live_range2 (&vars [idx], abs_pos + inst_num);
321 if (!mono_bitset_test_fast (bb->kill_set, idx))
322 mono_bitset_set_fast (bb->gen_set, idx);
323 vi->spill_costs += SPILL_COST_INCREMENT;
326 /* SREGs must come first, so MOVE r <- r is handled correctly */
330 if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
331 MonoInst *var = get_vreg_to_inst (cfg, sreg);
332 int idx = var->inst_c0;
333 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
335 #ifdef DEBUG_LIVENESS
336 printf ("\tGEN: R%d(%d)\n", sreg, idx);
338 update_live_range2 (&vars [idx], abs_pos + inst_num);
339 if (!mono_bitset_test_fast (bb->kill_set, idx))
340 mono_bitset_set_fast (bb->gen_set, idx);
341 vi->spill_costs += SPILL_COST_INCREMENT;
346 if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
347 MonoInst *var = get_vreg_to_inst (cfg, sreg);
348 int idx = var->inst_c0;
349 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
351 #ifdef DEBUG_LIVENESS
352 printf ("\tGEN: R%d(%d)\n", sreg, idx);
354 update_live_range2 (&vars [idx], abs_pos + inst_num);
355 if (!mono_bitset_test_fast (bb->kill_set, idx))
356 mono_bitset_set_fast (bb->gen_set, idx);
357 vi->spill_costs += SPILL_COST_INCREMENT;
361 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
362 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
363 int idx = var->inst_c0;
364 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
366 if (MONO_IS_STORE_MEMBASE (ins)) {
367 update_live_range2 (&vars [idx], abs_pos + inst_num);
368 if (!mono_bitset_test_fast (bb->kill_set, idx))
369 mono_bitset_set_fast (bb->gen_set, idx);
370 vi->spill_costs += SPILL_COST_INCREMENT;
372 #ifdef DEBUG_LIVENESS
373 printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
375 update_live_range2 (&vars [idx], abs_pos + inst_num + 1);
376 mono_bitset_set_fast (bb->kill_set, idx);
377 vi->spill_costs += SPILL_COST_INCREMENT;
383 /* generic liveness analysis code. CFG specific parts are
384 * in update_gen_kill_set()
387 mono_analyze_liveness (MonoCompile *cfg)
389 MonoBitSet *old_live_out_set;
390 int i, j, max_vars = cfg->num_varinfo;
392 gboolean *in_worklist;
393 MonoBasicBlock **worklist;
397 #ifdef DEBUG_LIVENESS
398 printf ("LIVENESS %s\n", mono_method_full_name (cfg->method, TRUE));
401 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
403 cfg->comp_done |= MONO_COMP_LIVENESS;
408 bitsize = mono_bitset_alloc_size (max_vars, 0);
410 for (i = 0; i < max_vars; i ++) {
411 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
412 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
413 MONO_VARINFO (cfg, i)->spill_costs = 0;
416 for (i = 0; i < cfg->num_bblocks; ++i) {
417 MonoBasicBlock *bb = cfg->bblocks [i];
421 bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
422 bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
425 analyze_liveness_bb (cfg, bb);
427 if (cfg->aliasing_info != NULL)
428 mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
431 MONO_BB_FOR_EACH_INS (bb, inst) {
432 #ifdef DEBUG_LIVENESS
433 mono_print_tree (inst); printf ("\n");
435 update_gen_kill_set (cfg, bb, inst, tree_num);
440 #ifdef DEBUG_LIVENESS
441 printf ("BLOCK BB%d (", bb->block_num);
442 for (j = 0; j < bb->out_count; j++)
443 printf ("BB%d, ", bb->out_bb [j]->block_num);
446 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
447 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
451 old_live_out_set = mono_bitset_new (max_vars, 0);
452 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
454 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
458 * This is a backward dataflow analysis problem, so we process blocks in
459 * decreasing dfn order, this speeds up the iteration.
461 for (i = 0; i < cfg->num_bblocks; i ++) {
462 MonoBasicBlock *bb = cfg->bblocks [i];
464 worklist [l_end ++] = bb;
465 in_worklist [bb->dfn] = TRUE;
467 /* Initialized later */
468 bb->live_in_set = NULL;
469 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
475 MonoBasicBlock *bb = worklist [--l_end];
476 MonoBasicBlock *out_bb;
479 in_worklist [bb->dfn] = FALSE;
481 #ifdef DEBUG_LIVENESS
482 printf ("P: %d(%d): IN: ", bb->block_num, bb->dfn);
483 for (j = 0; j < bb->in_count; ++j)
484 printf ("BB%d ", bb->in_bb [j]->block_num);
486 for (j = 0; j < bb->out_count; ++j)
487 printf ("BB%d ", bb->out_bb [j]->block_num);
492 if (bb->out_count == 0)
497 if (!bb->live_in_set) {
498 /* First pass over this bblock */
503 mono_bitset_copyto_fast (bb->live_out_set, old_live_out_set);
506 for (j = 0; j < bb->out_count; j++) {
507 out_bb = bb->out_bb [j];
509 if (!out_bb->live_in_set) {
510 out_bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
512 mono_bitset_copyto_fast (out_bb->live_out_set, out_bb->live_in_set);
513 mono_bitset_sub_fast (out_bb->live_in_set, out_bb->kill_set);
514 mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
517 mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
520 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
521 if (!bb->live_in_set)
522 bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
523 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
524 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
525 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
527 for (j = 0; j < bb->in_count; j++) {
528 MonoBasicBlock *in_bb = bb->in_bb [j];
530 * Some basic blocks do not seem to be in the
531 * cfg->bblocks array...
533 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
534 #ifdef DEBUG_LIVENESS
535 printf ("\tADD: %d\n", in_bb->block_num);
538 * Put the block at the top of the stack, so it
539 * will be processed right away.
541 worklist [l_end ++] = in_bb;
542 in_worklist [in_bb->dfn] = TRUE;
548 #ifdef DEBUG_LIVENESS
549 printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
552 mono_bitset_free (old_live_out_set);
555 g_free (in_worklist);
557 /* Compute live_in_set for bblocks skipped earlier */
558 for (i = 0; i < cfg->num_bblocks; ++i) {
559 MonoBasicBlock *bb = cfg->bblocks [i];
561 if (!bb->live_in_set) {
562 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
564 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
565 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
566 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
570 for (i = 0; i < cfg->num_bblocks; ++i) {
571 MonoBasicBlock *bb = cfg->bblocks [i];
573 guint32 abs_pos = (bb->dfn << 16);
574 MonoMethodVar *vars = cfg->vars;
576 if (!bb->live_out_set)
579 rem = max_vars % BITS_PER_CHUNK;
580 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
581 for (j = 0; j < max; ++j) {
586 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
587 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
589 k = (j * BITS_PER_CHUNK);
590 while ((bits_in || bits_out)) {
592 update_live_range2 (&vars [k], abs_pos + 0);
594 update_live_range2 (&vars [k], abs_pos + 0xffff);
603 * Arguments need to have their live ranges extended to the beginning of
604 * the method to account for the arg reg/memory -> global register copies
605 * in the prolog (bug #74992).
608 for (i = 0; i < max_vars; i ++) {
609 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
610 if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
611 if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
612 cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
613 vi->range.first_use.abs_pos = 0;
617 #ifdef DEBUG_LIVENESS
618 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
619 MonoBasicBlock *bb = cfg->bblocks [i];
621 printf ("LIVE IN BB%d: ", bb->block_num);
622 mono_bitset_print (bb->live_in_set);
623 printf ("LIVE OUT BB%d: ", bb->block_num);
624 mono_bitset_print (bb->live_out_set);
629 if (!cfg->disable_initlocals_opt)
630 optimize_initlocals2 (cfg);
632 #ifdef ENABLE_LIVENESS2
633 /* This improves code size by about 5% but slows down compilation too much */
634 if (cfg->compile_aot)
635 mono_analyze_liveness2 (cfg);
639 if (!cfg->disable_initlocals_opt)
640 optimize_initlocals (cfg);
645 * optimize_initlocals:
647 * Try to optimize away some of the redundant initialization code inserted because of
648 * 'locals init' using the liveness information.
651 optimize_initlocals2 (MonoCompile *cfg)
655 MonoBasicBlock *initlocals_bb;
657 used = mono_bitset_new (cfg->next_vreg + 1, 0);
659 mono_bitset_clear_all (used);
660 initlocals_bb = cfg->bb_entry->next_bb;
661 for (ins = initlocals_bb->code; ins; ins = ins->next) {
662 const char *spec = INS_INFO (ins->opcode);
664 if (spec [MONO_INST_SRC1] != ' ')
665 mono_bitset_set_fast (used, ins->sreg1);
666 if (spec [MONO_INST_SRC2] != ' ')
667 mono_bitset_set_fast (used, ins->sreg2);
668 if (MONO_IS_STORE_MEMBASE (ins))
669 mono_bitset_set_fast (used, ins->dreg);
672 for (ins = initlocals_bb->code; ins; ins = ins->next) {
673 const char *spec = INS_INFO (ins->opcode);
675 /* Look for statements whose dest is not used in this bblock and not live on exit. */
676 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
677 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
679 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))) {
680 //printf ("DEAD: "); mono_print_ins (ins);
681 if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) {
683 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
685 * We should shorten the liveness interval of these vars as well, but
686 * don't have enough info to do that.
697 mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
699 MonoLiveRange2 *prev_range, *next_range, *new_range;
701 g_assert (to >= from);
703 /* Optimize for extending the first interval backwards */
704 if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
705 interval->range->from = from;
709 /* Find a place in the list for the new range */
711 next_range = interval->range;
712 while ((next_range != NULL) && (next_range->from <= from)) {
713 prev_range = next_range;
714 next_range = next_range->next;
717 if (prev_range && prev_range->to == from) {
718 /* Merge with previous */
720 } else if (next_range && next_range->from == to) {
721 /* Merge with previous */
722 next_range->from = from;
725 new_range = mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
726 new_range->from = from;
728 new_range->next = NULL;
731 prev_range->next = new_range;
733 interval->range = new_range;
735 new_range->next = next_range;
737 interval->last_range = new_range;
740 /* FIXME: Merge intersecting ranges */
744 mono_linterval_print (MonoLiveInterval *interval)
746 MonoLiveRange2 *range;
748 for (range = interval->range; range != NULL; range = range->next)
749 printf ("[%x-%x] ", range->from, range->to);
753 mono_linterval_print_nl (MonoLiveInterval *interval)
755 mono_linterval_print (interval);
760 * mono_linterval_convers:
762 * Return whenever INTERVAL covers the position POS.
765 mono_linterval_covers (MonoLiveInterval *interval, int pos)
767 MonoLiveRange2 *range;
769 for (range = interval->range; range != NULL; range = range->next) {
770 if (pos >= range->from && pos <= range->to)
772 if (range->from > pos)
780 * mono_linterval_get_intersect_pos:
782 * Determine whenever I1 and I2 intersect, and if they do, return the first
783 * point of intersection. Otherwise, return -1.
786 mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
788 MonoLiveRange2 *r1, *r2;
790 /* FIXME: Optimize this */
791 for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
792 for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
793 if (r2->to > r1->from && r2->from < r1->to) {
794 if (r2->from <= r1->from)
806 * mono_linterval_split
808 * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
812 mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
816 g_assert (pos > interval->range->from && pos <= interval->last_range->to);
818 *i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
819 *i2 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
821 for (r = interval->range; r; r = r->next) {
823 /* Add it to the first child */
824 mono_linterval_add_range (cfg, *i1, r->from, r->to);
825 } else if (pos > r->from && pos <= r->to) {
827 mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
828 mono_linterval_add_range (cfg, *i2, pos, r->to);
830 /* Add it to the second child */
831 mono_linterval_add_range (cfg, *i2, r->from, r->to);
836 #ifdef ENABLE_LIVENESS2
839 #define LIVENESS_DEBUG(a) do { a; } while (0)
841 #define LIVENESS_DEBUG(a)
845 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
847 const char *spec = INS_INFO (ins->opcode);
850 LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
852 if (ins->opcode == OP_NOP)
856 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
857 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
858 int idx = var->inst_c0;
859 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
861 if (MONO_IS_STORE_MEMBASE (ins)) {
862 if (last_use [idx] == 0) {
863 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
864 last_use [idx] = inst_num;
867 if (last_use [idx] > 0) {
868 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
869 mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
873 /* Try dead code elimination */
874 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)) {
875 LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
876 ins->opcode = OP_NOP;
877 ins->dreg = ins->sreg1 = ins->sreg2 = -1;
881 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));
882 mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + 1);
889 if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
890 MonoInst *var = get_vreg_to_inst (cfg, sreg);
891 int idx = var->inst_c0;
893 if (last_use [idx] == 0) {
894 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
895 last_use [idx] = inst_num;
901 if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
902 MonoInst *var = get_vreg_to_inst (cfg, sreg);
903 int idx = var->inst_c0;
905 if (last_use [idx] == 0) {
906 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
907 last_use [idx] = inst_num;
913 mono_analyze_liveness2 (MonoCompile *cfg)
915 int bnum, idx, i, j, nins, rem, max, max_vars, block_from, block_to, pos, reverse_len;
917 static guint32 disabled = -1;
921 disabled = getenv ("DISABLED") != NULL;
926 LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
929 if (strstr (cfg->method->name, "test_") != cfg->method->name)
933 max_vars = cfg->num_varinfo;
934 last_use = g_new0 (gint32, max_vars);
937 reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
939 for (idx = 0; idx < max_vars; ++idx) {
940 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
942 vi->interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
946 * Process bblocks in reverse order, so the addition of new live ranges
947 * to the intervals is faster.
949 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
950 MonoBasicBlock *bb = cfg->bblocks [bnum];
953 block_from = (bb->dfn << 16) + 1; /* so pos > 0 */
954 if (bnum < cfg->num_bblocks - 1)
955 /* Beginning of the next bblock */
956 block_to = (cfg->bblocks [bnum + 1]->dfn << 16) + 1;
958 block_to = (bb->dfn << 16) + 0xffff;
960 LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
962 memset (last_use, 0, max_vars * sizeof (gint32));
964 /* For variables in bb->live_out, set last_use to block_to */
966 rem = max_vars % BITS_PER_CHUNK;
967 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
968 for (j = 0; j < max; ++j) {
972 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
973 k = (j * BITS_PER_CHUNK);
976 LIVENESS_DEBUG (printf ("Var R%d live at exit, set last_use to %x\n", cfg->varinfo [k]->dreg, block_to));
977 last_use [k] = block_to;
985 last_use [cfg->ret->inst_c0] = block_to;
987 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
988 if (nins >= reverse_len) {
989 int new_reverse_len = reverse_len * 2;
990 MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
991 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
992 reverse = new_reverse;
993 reverse_len = new_reverse_len;
996 reverse [nins] = ins;
999 /* Process instructions backwards */
1000 for (i = nins - 1; i >= 0; --i) {
1001 MonoInst *ins = (MonoInst*)reverse [i];
1003 update_liveness2 (cfg, ins, FALSE, pos, last_use);
1008 for (idx = 0; idx < max_vars; ++idx) {
1009 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
1011 if (last_use [idx] != 0) {
1012 /* Live at exit, not written -> live on enter */
1013 LIVENESS_DEBUG (printf ("Var R%d live at enter, add range to R%d: [%x, %x)\n", cfg->varinfo [idx]->dreg, cfg->varinfo [idx]->dreg, block_from, last_use [idx]));
1014 mono_linterval_add_range (cfg, vi->interval, block_from, last_use [idx]);
1020 * Arguments need to have their live ranges extended to the beginning of
1021 * the method to account for the arg reg/memory -> global register copies
1022 * in the prolog (bug #74992).
1024 for (i = 0; i < max_vars; i ++) {
1025 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
1026 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
1027 mono_linterval_add_range (cfg, vi->interval, 0, 1);
1031 for (idx = 0; idx < max_vars; ++idx) {
1032 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
1034 LIVENESS_DEBUG (printf ("LIVENESS R%d: ", cfg->varinfo [idx]->dreg));
1035 LIVENESS_DEBUG (mono_linterval_print (vi->interval));
1036 LIVENESS_DEBUG (printf ("\n"));
1046 update_used (MonoCompile *cfg, MonoInst *inst, MonoBitSet *used)
1048 int arity = mono_burg_arity [inst->opcode];
1051 update_used (cfg, inst->inst_i0, used);
1054 update_used (cfg, inst->inst_i1, used);
1056 if (inst->ssa_op & MONO_SSA_LOAD_STORE) {
1057 if (inst->ssa_op == MONO_SSA_LOAD) {
1058 int idx = inst->inst_i0->inst_c0;
1060 mono_bitset_set_fast (used, idx);
1066 * optimize_initlocals:
1068 * Try to optimize away some of the redundant initialization code inserted because of
1069 * 'locals init' using the liveness information.
1072 optimize_initlocals (MonoCompile *cfg)
1076 MonoBasicBlock *initlocals_bb;
1078 used = mono_bitset_new (cfg->num_varinfo, 0);
1080 mono_bitset_clear_all (used);
1081 initlocals_bb = cfg->bb_entry->next_bb;
1082 MONO_BB_FOR_EACH_INS (initlocals_bb, ins)
1083 update_used (cfg, ins, used);
1085 MONO_BB_FOR_EACH_INS (initlocals_bb, ins) {
1086 if (ins->ssa_op == MONO_SSA_STORE) {
1087 int idx = ins->inst_i0->inst_c0;
1088 MonoInst *var = cfg->varinfo [idx];
1090 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))) {
1091 if (ins->inst_i1 && ((ins->inst_i1->opcode == OP_ICONST) || (ins->inst_i1->opcode == OP_I8CONST))) {
1093 ins->ssa_op = MONO_SSA_NOP;
1094 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;