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
32 #ifdef ENABLE_LIVENESS2
33 static void mono_analyze_liveness2 (MonoCompile *cfg);
37 optimize_initlocals (MonoCompile *cfg);
39 /* mono_bitset_mp_new:
41 * allocates a MonoBitSet inside a memory pool
43 static inline MonoBitSet*
44 mono_bitset_mp_new (MonoMemPool *mp, guint32 size, guint32 max_size)
46 guint8 *mem = mono_mempool_alloc0 (mp, size);
47 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
50 static inline MonoBitSet*
51 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 size, guint32 max_size)
53 guint8 *mem = mono_mempool_alloc (mp, size);
54 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
57 G_GNUC_UNUSED static void
58 mono_bitset_print (MonoBitSet *set)
63 for (i = 0; i < mono_bitset_size (set); i++) {
65 if (mono_bitset_test (set, i))
73 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
78 if (g_slist_find (*visited, bb))
81 for (ins = bb->code; ins; ins = ins->next) {
82 const char *spec = INS_INFO (ins->opcode);
83 int regtype, srcindex, sreg;
85 if (ins->opcode == OP_NOP)
89 regtype = spec [MONO_INST_DEST];
90 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
92 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
93 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
94 int idx = var->inst_c0;
95 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
97 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
101 for (srcindex = 0; srcindex < 2; ++srcindex) {
102 regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 : MONO_INST_SRC2];
103 sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
105 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
106 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
107 MonoInst *var = get_vreg_to_inst (cfg, sreg);
108 int idx = var->inst_c0;
109 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
111 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
116 *visited = g_slist_append (*visited, bb);
119 * Need to visit all bblocks reachable from this one since they can be
120 * reached during exception handling.
122 for (i = 0; i < bb->out_count; ++i) {
123 visit_bb (cfg, bb->out_bb [i], visited);
128 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
131 GSList *visited = NULL;
134 * Variables in exception handler register cannot be allocated to registers
135 * so make them volatile. See bug #42136. This will not be neccessary when
136 * the back ends could guarantee that the variables will be in the
137 * correct registers when a handler is called.
138 * This includes try blocks too, since a variable in a try block might be
139 * accessed after an exception handler has been run.
141 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
143 if (bb->region == -1 || MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
146 visit_bb (cfg, bb, &visited);
148 g_slist_free (visited);
152 update_live_range (MonoMethodVar *var, int abs_pos)
154 if (var->range.first_use.abs_pos > abs_pos)
155 var->range.first_use.abs_pos = abs_pos;
157 if (var->range.last_use.abs_pos < abs_pos)
158 var->range.last_use.abs_pos = abs_pos;
162 analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
166 MonoMethodVar *vars = cfg->vars;
167 guint32 abs_pos = (bb->dfn << 16);
169 for (inst_num = 0, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
170 const char *spec = INS_INFO (ins->opcode);
172 #ifdef DEBUG_LIVENESS
173 printf ("\t"); mono_print_ins (ins);
176 if (ins->opcode == OP_NOP)
179 if (ins->opcode == OP_LDADDR) {
180 MonoInst *var = ins->inst_p0;
181 int idx = var->inst_c0;
182 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
184 #ifdef DEBUG_LIVENESS
185 printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
187 update_live_range (&vars [idx], abs_pos + inst_num);
188 if (!mono_bitset_test_fast (bb->kill_set, idx))
189 mono_bitset_set_fast (bb->gen_set, idx);
190 vi->spill_costs += SPILL_COST_INCREMENT;
193 /* SREGs must come first, so MOVE r <- r is handled correctly */
197 if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
198 MonoInst *var = get_vreg_to_inst (cfg, sreg);
199 int idx = var->inst_c0;
200 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
202 #ifdef DEBUG_LIVENESS
203 printf ("\tGEN: R%d(%d)\n", sreg, idx);
205 update_live_range (&vars [idx], abs_pos + inst_num);
206 if (!mono_bitset_test_fast (bb->kill_set, idx))
207 mono_bitset_set_fast (bb->gen_set, idx);
208 vi->spill_costs += SPILL_COST_INCREMENT;
213 if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
214 MonoInst *var = get_vreg_to_inst (cfg, sreg);
215 int idx = var->inst_c0;
216 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
218 #ifdef DEBUG_LIVENESS
219 printf ("\tGEN: R%d(%d)\n", sreg, idx);
221 update_live_range (&vars [idx], abs_pos + inst_num);
222 if (!mono_bitset_test_fast (bb->kill_set, idx))
223 mono_bitset_set_fast (bb->gen_set, idx);
224 vi->spill_costs += SPILL_COST_INCREMENT;
228 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
229 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
230 int idx = var->inst_c0;
231 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
233 if (MONO_IS_STORE_MEMBASE (ins)) {
234 update_live_range (&vars [idx], abs_pos + inst_num);
235 if (!mono_bitset_test_fast (bb->kill_set, idx))
236 mono_bitset_set_fast (bb->gen_set, idx);
237 vi->spill_costs += SPILL_COST_INCREMENT;
239 #ifdef DEBUG_LIVENESS
240 printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
242 update_live_range (&vars [idx], abs_pos + inst_num + 1);
243 mono_bitset_set_fast (bb->kill_set, idx);
244 vi->spill_costs += SPILL_COST_INCREMENT;
250 /* generic liveness analysis code. CFG specific parts are
251 * in update_gen_kill_set()
254 mono_analyze_liveness (MonoCompile *cfg)
256 MonoBitSet *old_live_out_set;
257 int i, j, max_vars = cfg->num_varinfo;
259 gboolean *in_worklist;
260 MonoBasicBlock **worklist;
264 #ifdef DEBUG_LIVENESS
265 printf ("LIVENESS %s\n", mono_method_full_name (cfg->method, TRUE));
268 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
270 cfg->comp_done |= MONO_COMP_LIVENESS;
275 bitsize = mono_bitset_alloc_size (max_vars, 0);
277 for (i = 0; i < max_vars; i ++) {
278 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
279 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
280 MONO_VARINFO (cfg, i)->spill_costs = 0;
283 for (i = 0; i < cfg->num_bblocks; ++i) {
284 MonoBasicBlock *bb = cfg->bblocks [i];
286 bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
287 bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
289 analyze_liveness_bb (cfg, bb);
291 #ifdef DEBUG_LIVENESS
292 printf ("BLOCK BB%d (", bb->block_num);
293 for (j = 0; j < bb->out_count; j++)
294 printf ("BB%d, ", bb->out_bb [j]->block_num);
297 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
298 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
302 old_live_out_set = mono_bitset_new (max_vars, 0);
303 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
305 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
309 * This is a backward dataflow analysis problem, so we process blocks in
310 * decreasing dfn order, this speeds up the iteration.
312 for (i = 0; i < cfg->num_bblocks; i ++) {
313 MonoBasicBlock *bb = cfg->bblocks [i];
315 worklist [l_end ++] = bb;
316 in_worklist [bb->dfn] = TRUE;
318 /* Initialized later */
319 bb->live_in_set = NULL;
320 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
326 MonoBasicBlock *bb = worklist [--l_end];
327 MonoBasicBlock *out_bb;
330 in_worklist [bb->dfn] = FALSE;
332 #ifdef DEBUG_LIVENESS
333 printf ("P: %d(%d): IN: ", bb->block_num, bb->dfn);
334 for (j = 0; j < bb->in_count; ++j)
335 printf ("BB%d ", bb->in_bb [j]->block_num);
337 for (j = 0; j < bb->out_count; ++j)
338 printf ("BB%d ", bb->out_bb [j]->block_num);
343 if (bb->out_count == 0)
348 if (!bb->live_in_set) {
349 /* First pass over this bblock */
354 mono_bitset_copyto_fast (bb->live_out_set, old_live_out_set);
357 for (j = 0; j < bb->out_count; j++) {
358 out_bb = bb->out_bb [j];
360 if (!out_bb->live_in_set) {
361 out_bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
363 mono_bitset_copyto_fast (out_bb->live_out_set, out_bb->live_in_set);
364 mono_bitset_sub_fast (out_bb->live_in_set, out_bb->kill_set);
365 mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
368 mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
371 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
372 if (!bb->live_in_set)
373 bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
374 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
375 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
376 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
378 for (j = 0; j < bb->in_count; j++) {
379 MonoBasicBlock *in_bb = bb->in_bb [j];
381 * Some basic blocks do not seem to be in the
382 * cfg->bblocks array...
384 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
385 #ifdef DEBUG_LIVENESS
386 printf ("\tADD: %d\n", in_bb->block_num);
389 * Put the block at the top of the stack, so it
390 * will be processed right away.
392 worklist [l_end ++] = in_bb;
393 in_worklist [in_bb->dfn] = TRUE;
399 #ifdef DEBUG_LIVENESS
400 printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
403 mono_bitset_free (old_live_out_set);
406 g_free (in_worklist);
408 /* Compute live_in_set for bblocks skipped earlier */
409 for (i = 0; i < cfg->num_bblocks; ++i) {
410 MonoBasicBlock *bb = cfg->bblocks [i];
412 if (!bb->live_in_set) {
413 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
415 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
416 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
417 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
421 for (i = 0; i < cfg->num_bblocks; ++i) {
422 MonoBasicBlock *bb = cfg->bblocks [i];
424 guint32 abs_pos = (bb->dfn << 16);
425 MonoMethodVar *vars = cfg->vars;
427 if (!bb->live_out_set)
430 rem = max_vars % BITS_PER_CHUNK;
431 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
432 for (j = 0; j < max; ++j) {
437 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
438 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
440 k = (j * BITS_PER_CHUNK);
441 while ((bits_in || bits_out)) {
443 update_live_range (&vars [k], abs_pos + 0);
445 update_live_range (&vars [k], abs_pos + 0xffff);
454 * Arguments need to have their live ranges extended to the beginning of
455 * the method to account for the arg reg/memory -> global register copies
456 * in the prolog (bug #74992).
459 for (i = 0; i < max_vars; i ++) {
460 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
461 if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
462 if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
463 cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
464 vi->range.first_use.abs_pos = 0;
468 #ifdef DEBUG_LIVENESS
469 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
470 MonoBasicBlock *bb = cfg->bblocks [i];
472 printf ("LIVE IN BB%d: ", bb->block_num);
473 mono_bitset_print (bb->live_in_set);
474 printf ("LIVE OUT BB%d: ", bb->block_num);
475 mono_bitset_print (bb->live_out_set);
479 if (!cfg->disable_initlocals_opt)
480 optimize_initlocals (cfg);
482 #ifdef ENABLE_LIVENESS2
483 /* This improves code size by about 5% but slows down compilation too much */
484 if (cfg->compile_aot)
485 mono_analyze_liveness2 (cfg);
490 * optimize_initlocals:
492 * Try to optimize away some of the redundant initialization code inserted because of
493 * 'locals init' using the liveness information.
496 optimize_initlocals (MonoCompile *cfg)
500 MonoBasicBlock *initlocals_bb;
502 used = mono_bitset_new (cfg->next_vreg + 1, 0);
504 mono_bitset_clear_all (used);
505 initlocals_bb = cfg->bb_entry->next_bb;
506 for (ins = initlocals_bb->code; ins; ins = ins->next) {
507 const char *spec = INS_INFO (ins->opcode);
509 if (spec [MONO_INST_SRC1] != ' ')
510 mono_bitset_set_fast (used, ins->sreg1);
511 if (spec [MONO_INST_SRC2] != ' ')
512 mono_bitset_set_fast (used, ins->sreg2);
513 if (MONO_IS_STORE_MEMBASE (ins))
514 mono_bitset_set_fast (used, ins->dreg);
517 for (ins = initlocals_bb->code; ins; ins = ins->next) {
518 const char *spec = INS_INFO (ins->opcode);
520 /* Look for statements whose dest is not used in this bblock and not live on exit. */
521 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
522 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
524 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))) {
525 //printf ("DEAD: "); mono_print_ins (ins);
526 if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) {
528 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
530 * We should shorten the liveness interval of these vars as well, but
531 * don't have enough info to do that.
542 mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
544 MonoLiveRange2 *prev_range, *next_range, *new_range;
546 g_assert (to >= from);
548 /* Optimize for extending the first interval backwards */
549 if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
550 interval->range->from = from;
554 /* Find a place in the list for the new range */
556 next_range = interval->range;
557 while ((next_range != NULL) && (next_range->from <= from)) {
558 prev_range = next_range;
559 next_range = next_range->next;
562 if (prev_range && prev_range->to == from) {
563 /* Merge with previous */
565 } else if (next_range && next_range->from == to) {
566 /* Merge with previous */
567 next_range->from = from;
570 new_range = mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
571 new_range->from = from;
573 new_range->next = NULL;
576 prev_range->next = new_range;
578 interval->range = new_range;
580 new_range->next = next_range;
582 interval->last_range = new_range;
585 /* FIXME: Merge intersecting ranges */
589 mono_linterval_print (MonoLiveInterval *interval)
591 MonoLiveRange2 *range;
593 for (range = interval->range; range != NULL; range = range->next)
594 printf ("[%x-%x] ", range->from, range->to);
598 mono_linterval_print_nl (MonoLiveInterval *interval)
600 mono_linterval_print (interval);
605 * mono_linterval_convers:
607 * Return whenever INTERVAL covers the position POS.
610 mono_linterval_covers (MonoLiveInterval *interval, int pos)
612 MonoLiveRange2 *range;
614 for (range = interval->range; range != NULL; range = range->next) {
615 if (pos >= range->from && pos <= range->to)
617 if (range->from > pos)
625 * mono_linterval_get_intersect_pos:
627 * Determine whenever I1 and I2 intersect, and if they do, return the first
628 * point of intersection. Otherwise, return -1.
631 mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
633 MonoLiveRange2 *r1, *r2;
635 /* FIXME: Optimize this */
636 for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
637 for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
638 if (r2->to > r1->from && r2->from < r1->to) {
639 if (r2->from <= r1->from)
651 * mono_linterval_split
653 * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
657 mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
661 g_assert (pos > interval->range->from && pos <= interval->last_range->to);
663 *i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
664 *i2 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
666 for (r = interval->range; r; r = r->next) {
668 /* Add it to the first child */
669 mono_linterval_add_range (cfg, *i1, r->from, r->to);
670 } else if (pos > r->from && pos <= r->to) {
672 mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
673 mono_linterval_add_range (cfg, *i2, pos, r->to);
675 /* Add it to the second child */
676 mono_linterval_add_range (cfg, *i2, r->from, r->to);
681 #ifdef ENABLE_LIVENESS2
684 #define LIVENESS_DEBUG(a) do { a; } while (0)
686 #define LIVENESS_DEBUG(a)
690 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
692 const char *spec = INS_INFO (ins->opcode);
695 LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
697 if (ins->opcode == OP_NOP)
701 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
702 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
703 int idx = var->inst_c0;
704 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
706 if (MONO_IS_STORE_MEMBASE (ins)) {
707 if (last_use [idx] == 0) {
708 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
709 last_use [idx] = inst_num;
712 if (last_use [idx] > 0) {
713 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
714 mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
718 /* Try dead code elimination */
719 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)) {
720 LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
721 ins->opcode = OP_NOP;
722 ins->dreg = ins->sreg1 = ins->sreg2 = -1;
726 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));
727 mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + 1);
734 if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
735 MonoInst *var = get_vreg_to_inst (cfg, sreg);
736 int idx = var->inst_c0;
738 if (last_use [idx] == 0) {
739 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
740 last_use [idx] = inst_num;
746 if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
747 MonoInst *var = get_vreg_to_inst (cfg, sreg);
748 int idx = var->inst_c0;
750 if (last_use [idx] == 0) {
751 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
752 last_use [idx] = inst_num;
758 mono_analyze_liveness2 (MonoCompile *cfg)
760 int bnum, idx, i, j, nins, rem, max, max_vars, block_from, block_to, pos, reverse_len;
762 static guint32 disabled = -1;
766 disabled = getenv ("DISABLED") != NULL;
771 LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
774 if (strstr (cfg->method->name, "test_") != cfg->method->name)
778 max_vars = cfg->num_varinfo;
779 last_use = g_new0 (gint32, max_vars);
782 reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
784 for (idx = 0; idx < max_vars; ++idx) {
785 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
787 vi->interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
791 * Process bblocks in reverse order, so the addition of new live ranges
792 * to the intervals is faster.
794 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
795 MonoBasicBlock *bb = cfg->bblocks [bnum];
798 block_from = (bb->dfn << 16) + 1; /* so pos > 0 */
799 if (bnum < cfg->num_bblocks - 1)
800 /* Beginning of the next bblock */
801 block_to = (cfg->bblocks [bnum + 1]->dfn << 16) + 1;
803 block_to = (bb->dfn << 16) + 0xffff;
805 LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
807 memset (last_use, 0, max_vars * sizeof (gint32));
809 /* For variables in bb->live_out, set last_use to block_to */
811 rem = max_vars % BITS_PER_CHUNK;
812 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
813 for (j = 0; j < max; ++j) {
817 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
818 k = (j * BITS_PER_CHUNK);
821 LIVENESS_DEBUG (printf ("Var R%d live at exit, set last_use to %x\n", cfg->varinfo [k]->dreg, block_to));
822 last_use [k] = block_to;
830 last_use [cfg->ret->inst_c0] = block_to;
832 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
833 if (nins >= reverse_len) {
834 int new_reverse_len = reverse_len * 2;
835 MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
836 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
837 reverse = new_reverse;
838 reverse_len = new_reverse_len;
841 reverse [nins] = ins;
844 /* Process instructions backwards */
845 for (i = nins - 1; i >= 0; --i) {
846 MonoInst *ins = (MonoInst*)reverse [i];
848 update_liveness2 (cfg, ins, FALSE, pos, last_use);
853 for (idx = 0; idx < max_vars; ++idx) {
854 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
856 if (last_use [idx] != 0) {
857 /* Live at exit, not written -> live on enter */
858 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]));
859 mono_linterval_add_range (cfg, vi->interval, block_from, last_use [idx]);
865 * Arguments need to have their live ranges extended to the beginning of
866 * the method to account for the arg reg/memory -> global register copies
867 * in the prolog (bug #74992).
869 for (i = 0; i < max_vars; i ++) {
870 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
871 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
872 mono_linterval_add_range (cfg, vi->interval, 0, 1);
876 for (idx = 0; idx < max_vars; ++idx) {
877 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
879 LIVENESS_DEBUG (printf ("LIVENESS R%d: ", cfg->varinfo [idx]->dreg));
880 LIVENESS_DEBUG (mono_linterval_print (vi->interval));
881 LIVENESS_DEBUG (printf ("\n"));