2 * liveness.c: liveness analysis
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2002 Ximian, Inc.
12 #define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
14 //#define DEBUG_LIVENESS
16 #if SIZEOF_REGISTER == 8
17 #define BITS_PER_CHUNK 64
19 #define BITS_PER_CHUNK 32
23 * The liveness2 pass can't handle long vars on 32 bit platforms because the component
24 * vars have the same 'idx'.
26 #if SIZEOF_REGISTER == 8
27 #define ENABLE_LIVENESS2
30 #ifdef ENABLE_LIVENESS2
31 static void mono_analyze_liveness2 (MonoCompile *cfg);
35 optimize_initlocals (MonoCompile *cfg);
37 /* mono_bitset_mp_new:
39 * allocates a MonoBitSet inside a memory pool
41 static inline MonoBitSet*
42 mono_bitset_mp_new (MonoMemPool *mp, guint32 size, guint32 max_size)
44 guint8 *mem = mono_mempool_alloc0 (mp, size);
45 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
48 static inline MonoBitSet*
49 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 size, guint32 max_size)
51 guint8 *mem = mono_mempool_alloc (mp, size);
52 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
55 G_GNUC_UNUSED static void
56 mono_bitset_print (MonoBitSet *set)
61 for (i = 0; i < mono_bitset_size (set); i++) {
63 if (mono_bitset_test (set, i))
71 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
76 if (g_slist_find (*visited, bb))
79 for (ins = bb->code; ins; ins = ins->next) {
80 const char *spec = INS_INFO (ins->opcode);
81 int regtype, srcindex, sreg, num_sregs;
82 int sregs [MONO_MAX_SRC_REGS];
84 if (ins->opcode == OP_NOP)
88 regtype = spec [MONO_INST_DEST];
89 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
91 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
92 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
93 int idx = var->inst_c0;
94 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
96 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
100 num_sregs = mono_inst_get_src_registers (ins, sregs);
101 g_assert (num_sregs <= 2);
102 for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
103 regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 : MONO_INST_SRC2];
104 sreg = sregs [srcindex];
106 g_assert (sreg != -1);
107 if (get_vreg_to_inst (cfg, sreg)) {
108 MonoInst *var = get_vreg_to_inst (cfg, sreg);
109 int idx = var->inst_c0;
110 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
112 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
117 *visited = g_slist_append (*visited, bb);
120 * Need to visit all bblocks reachable from this one since they can be
121 * reached during exception handling.
123 for (i = 0; i < bb->out_count; ++i) {
124 visit_bb (cfg, bb->out_bb [i], visited);
129 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
132 GSList *visited = NULL;
135 * Variables in exception handler register cannot be allocated to registers
136 * so make them volatile. See bug #42136. This will not be neccessary when
137 * the back ends could guarantee that the variables will be in the
138 * correct registers when a handler is called.
139 * This includes try blocks too, since a variable in a try block might be
140 * accessed after an exception handler has been run.
142 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
144 if (bb->region == -1 || MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
147 visit_bb (cfg, bb, &visited);
149 g_slist_free (visited);
153 update_live_range (MonoMethodVar *var, int abs_pos)
155 if (var->range.first_use.abs_pos > abs_pos)
156 var->range.first_use.abs_pos = abs_pos;
158 if (var->range.last_use.abs_pos < abs_pos)
159 var->range.last_use.abs_pos = abs_pos;
163 analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
167 MonoMethodVar *vars = cfg->vars;
168 guint32 abs_pos = (bb->dfn << 16);
170 for (inst_num = 0, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
171 const char *spec = INS_INFO (ins->opcode);
173 int sregs [MONO_MAX_SRC_REGS];
175 #ifdef DEBUG_LIVENESS
176 printf ("\t"); mono_print_ins (ins);
179 if (ins->opcode == OP_NOP)
182 if (ins->opcode == OP_LDADDR) {
183 MonoInst *var = ins->inst_p0;
184 int idx = var->inst_c0;
185 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
187 #ifdef DEBUG_LIVENESS
188 printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
190 update_live_range (&vars [idx], abs_pos + inst_num);
191 if (!mono_bitset_test_fast (bb->kill_set, idx))
192 mono_bitset_set_fast (bb->gen_set, idx);
193 vi->spill_costs += SPILL_COST_INCREMENT;
196 /* SREGs must come first, so MOVE r <- r is handled correctly */
197 num_sregs = mono_inst_get_src_registers (ins, sregs);
198 for (i = 0; i < num_sregs; ++i) {
200 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
201 MonoInst *var = get_vreg_to_inst (cfg, sreg);
202 int idx = var->inst_c0;
203 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
205 #ifdef DEBUG_LIVENESS
206 printf ("\tGEN: R%d(%d)\n", sreg, idx);
208 update_live_range (&vars [idx], abs_pos + inst_num);
209 if (!mono_bitset_test_fast (bb->kill_set, idx))
210 mono_bitset_set_fast (bb->gen_set, idx);
211 vi->spill_costs += SPILL_COST_INCREMENT;
216 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
217 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
218 int idx = var->inst_c0;
219 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
221 if (MONO_IS_STORE_MEMBASE (ins)) {
222 update_live_range (&vars [idx], abs_pos + inst_num);
223 if (!mono_bitset_test_fast (bb->kill_set, idx))
224 mono_bitset_set_fast (bb->gen_set, idx);
225 vi->spill_costs += SPILL_COST_INCREMENT;
227 #ifdef DEBUG_LIVENESS
228 printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
230 update_live_range (&vars [idx], abs_pos + inst_num + 1);
231 mono_bitset_set_fast (bb->kill_set, idx);
232 vi->spill_costs += SPILL_COST_INCREMENT;
238 /* generic liveness analysis code. CFG specific parts are
239 * in update_gen_kill_set()
242 mono_analyze_liveness (MonoCompile *cfg)
244 MonoBitSet *old_live_out_set;
245 int i, j, max_vars = cfg->num_varinfo;
247 gboolean *in_worklist;
248 MonoBasicBlock **worklist;
252 #ifdef DEBUG_LIVENESS
253 printf ("LIVENESS %s\n", mono_method_full_name (cfg->method, TRUE));
256 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
258 cfg->comp_done |= MONO_COMP_LIVENESS;
263 bitsize = mono_bitset_alloc_size (max_vars, 0);
265 for (i = 0; i < max_vars; i ++) {
266 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
267 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
268 MONO_VARINFO (cfg, i)->spill_costs = 0;
271 for (i = 0; i < cfg->num_bblocks; ++i) {
272 MonoBasicBlock *bb = cfg->bblocks [i];
274 bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
275 bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
277 analyze_liveness_bb (cfg, bb);
279 #ifdef DEBUG_LIVENESS
280 printf ("BLOCK BB%d (", bb->block_num);
281 for (j = 0; j < bb->out_count; j++)
282 printf ("BB%d, ", bb->out_bb [j]->block_num);
285 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
286 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
290 old_live_out_set = mono_bitset_new (max_vars, 0);
291 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
293 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
297 * This is a backward dataflow analysis problem, so we process blocks in
298 * decreasing dfn order, this speeds up the iteration.
300 for (i = 0; i < cfg->num_bblocks; i ++) {
301 MonoBasicBlock *bb = cfg->bblocks [i];
303 worklist [l_end ++] = bb;
304 in_worklist [bb->dfn] = TRUE;
306 /* Initialized later */
307 bb->live_in_set = NULL;
308 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
314 MonoBasicBlock *bb = worklist [--l_end];
315 MonoBasicBlock *out_bb;
318 in_worklist [bb->dfn] = FALSE;
320 #ifdef DEBUG_LIVENESS
321 printf ("P: %d(%d): IN: ", bb->block_num, bb->dfn);
322 for (j = 0; j < bb->in_count; ++j)
323 printf ("BB%d ", bb->in_bb [j]->block_num);
325 for (j = 0; j < bb->out_count; ++j)
326 printf ("BB%d ", bb->out_bb [j]->block_num);
331 if (bb->out_count == 0)
336 if (!bb->live_in_set) {
337 /* First pass over this bblock */
342 mono_bitset_copyto_fast (bb->live_out_set, old_live_out_set);
345 for (j = 0; j < bb->out_count; j++) {
346 out_bb = bb->out_bb [j];
348 if (!out_bb->live_in_set) {
349 out_bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
351 mono_bitset_copyto_fast (out_bb->live_out_set, out_bb->live_in_set);
352 mono_bitset_sub_fast (out_bb->live_in_set, out_bb->kill_set);
353 mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
356 mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
359 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
360 if (!bb->live_in_set)
361 bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
362 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
363 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
364 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
366 for (j = 0; j < bb->in_count; j++) {
367 MonoBasicBlock *in_bb = bb->in_bb [j];
369 * Some basic blocks do not seem to be in the
370 * cfg->bblocks array...
372 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
373 #ifdef DEBUG_LIVENESS
374 printf ("\tADD: %d\n", in_bb->block_num);
377 * Put the block at the top of the stack, so it
378 * will be processed right away.
380 worklist [l_end ++] = in_bb;
381 in_worklist [in_bb->dfn] = TRUE;
387 #ifdef DEBUG_LIVENESS
388 printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
391 mono_bitset_free (old_live_out_set);
394 g_free (in_worklist);
396 /* Compute live_in_set for bblocks skipped earlier */
397 for (i = 0; i < cfg->num_bblocks; ++i) {
398 MonoBasicBlock *bb = cfg->bblocks [i];
400 if (!bb->live_in_set) {
401 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
403 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
404 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
405 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
409 for (i = 0; i < cfg->num_bblocks; ++i) {
410 MonoBasicBlock *bb = cfg->bblocks [i];
412 guint32 abs_pos = (bb->dfn << 16);
413 MonoMethodVar *vars = cfg->vars;
415 if (!bb->live_out_set)
418 rem = max_vars % BITS_PER_CHUNK;
419 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
420 for (j = 0; j < max; ++j) {
425 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
426 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
428 k = (j * BITS_PER_CHUNK);
429 while ((bits_in || bits_out)) {
431 update_live_range (&vars [k], abs_pos + 0);
433 update_live_range (&vars [k], abs_pos + 0xffff);
442 * Arguments need to have their live ranges extended to the beginning of
443 * the method to account for the arg reg/memory -> global register copies
444 * in the prolog (bug #74992).
447 for (i = 0; i < max_vars; i ++) {
448 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
449 if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
450 if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
452 * Can't eliminate the this variable in gshared code, since
453 * it is needed during exception handling to identify the
455 * It is better to check for this here instead of marking the variable
456 * VOLATILE, since that would prevent it from being allocated to
459 if (!cfg->disable_deadce_vars && !(cfg->generic_sharing_context && mono_method_signature (cfg->method)->hasthis && cfg->varinfo [vi->idx] == cfg->args [0]))
460 cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
462 vi->range.first_use.abs_pos = 0;
466 #ifdef DEBUG_LIVENESS
467 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
468 MonoBasicBlock *bb = cfg->bblocks [i];
470 printf ("LIVE IN BB%d: ", bb->block_num);
471 mono_bitset_print (bb->live_in_set);
472 printf ("LIVE OUT BB%d: ", bb->block_num);
473 mono_bitset_print (bb->live_out_set);
477 if (!cfg->disable_initlocals_opt)
478 optimize_initlocals (cfg);
480 #ifdef ENABLE_LIVENESS2
481 /* This improves code size by about 5% but slows down compilation too much */
482 if (cfg->compile_aot)
483 mono_analyze_liveness2 (cfg);
488 * optimize_initlocals:
490 * Try to optimize away some of the redundant initialization code inserted because of
491 * 'locals init' using the liveness information.
494 optimize_initlocals (MonoCompile *cfg)
498 MonoBasicBlock *initlocals_bb;
500 used = mono_bitset_new (cfg->next_vreg + 1, 0);
502 mono_bitset_clear_all (used);
503 initlocals_bb = cfg->bb_entry->next_bb;
504 for (ins = initlocals_bb->code; ins; ins = ins->next) {
506 int sregs [MONO_MAX_SRC_REGS];
508 num_sregs = mono_inst_get_src_registers (ins, sregs);
509 for (i = 0; i < num_sregs; ++i)
510 mono_bitset_set_fast (used, sregs [i]);
512 if (MONO_IS_STORE_MEMBASE (ins))
513 mono_bitset_set_fast (used, ins->dreg);
516 for (ins = initlocals_bb->code; ins; ins = ins->next) {
517 const char *spec = INS_INFO (ins->opcode);
519 /* Look for statements whose dest is not used in this bblock and not live on exit. */
520 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
521 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
523 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))) {
524 //printf ("DEAD: "); mono_print_ins (ins);
525 if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) {
527 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
529 * We should shorten the liveness interval of these vars as well, but
530 * don't have enough info to do that.
541 mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
543 MonoLiveRange2 *prev_range, *next_range, *new_range;
545 g_assert (to >= from);
547 /* Optimize for extending the first interval backwards */
548 if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
549 interval->range->from = from;
553 /* Find a place in the list for the new range */
555 next_range = interval->range;
556 while ((next_range != NULL) && (next_range->from <= from)) {
557 prev_range = next_range;
558 next_range = next_range->next;
561 if (prev_range && prev_range->to == from) {
562 /* Merge with previous */
564 } else if (next_range && next_range->from == to) {
565 /* Merge with previous */
566 next_range->from = from;
569 new_range = mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
570 new_range->from = from;
572 new_range->next = NULL;
575 prev_range->next = new_range;
577 interval->range = new_range;
579 new_range->next = next_range;
581 interval->last_range = new_range;
584 /* FIXME: Merge intersecting ranges */
588 mono_linterval_print (MonoLiveInterval *interval)
590 MonoLiveRange2 *range;
592 for (range = interval->range; range != NULL; range = range->next)
593 printf ("[%x-%x] ", range->from, range->to);
597 mono_linterval_print_nl (MonoLiveInterval *interval)
599 mono_linterval_print (interval);
604 * mono_linterval_convers:
606 * Return whenever INTERVAL covers the position POS.
609 mono_linterval_covers (MonoLiveInterval *interval, int pos)
611 MonoLiveRange2 *range;
613 for (range = interval->range; range != NULL; range = range->next) {
614 if (pos >= range->from && pos <= range->to)
616 if (range->from > pos)
624 * mono_linterval_get_intersect_pos:
626 * Determine whenever I1 and I2 intersect, and if they do, return the first
627 * point of intersection. Otherwise, return -1.
630 mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
632 MonoLiveRange2 *r1, *r2;
634 /* FIXME: Optimize this */
635 for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
636 for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
637 if (r2->to > r1->from && r2->from < r1->to) {
638 if (r2->from <= r1->from)
650 * mono_linterval_split
652 * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
656 mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
660 g_assert (pos > interval->range->from && pos <= interval->last_range->to);
662 *i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
663 *i2 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
665 for (r = interval->range; r; r = r->next) {
667 /* Add it to the first child */
668 mono_linterval_add_range (cfg, *i1, r->from, r->to);
669 } else if (pos > r->from && pos <= r->to) {
671 mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
672 mono_linterval_add_range (cfg, *i2, pos, r->to);
674 /* Add it to the second child */
675 mono_linterval_add_range (cfg, *i2, r->from, r->to);
680 #ifdef ENABLE_LIVENESS2
683 #define LIVENESS_DEBUG(a) do { a; } while (0)
685 #define LIVENESS_DEBUG(a)
689 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
691 const char *spec = INS_INFO (ins->opcode);
694 int sregs [MONO_MAX_SRC_REGS];
696 LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
698 if (ins->opcode == OP_NOP)
702 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
703 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
704 int idx = var->inst_c0;
705 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
707 if (MONO_IS_STORE_MEMBASE (ins)) {
708 if (last_use [idx] == 0) {
709 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
710 last_use [idx] = inst_num;
713 if (last_use [idx] > 0) {
714 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
715 mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
719 /* Try dead code elimination */
720 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)) {
721 LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
722 ins->opcode = OP_NOP;
724 MONO_INST_NULLIFY_SREGS (ins);
728 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));
729 mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + 1);
735 num_sregs = mono_inst_get_src_registers (ins, sregs);
736 for (i = 0; i < num_sregs; ++i) {
738 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
739 MonoInst *var = get_vreg_to_inst (cfg, sreg);
740 int idx = var->inst_c0;
742 if (last_use [idx] == 0) {
743 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
744 last_use [idx] = inst_num;
751 mono_analyze_liveness2 (MonoCompile *cfg)
753 int bnum, idx, i, j, nins, rem, max, max_vars, block_from, block_to, pos, reverse_len;
755 static guint32 disabled = -1;
759 disabled = getenv ("DISABLED") != NULL;
764 LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
767 if (strstr (cfg->method->name, "test_") != cfg->method->name)
771 max_vars = cfg->num_varinfo;
772 last_use = g_new0 (gint32, max_vars);
775 reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
777 for (idx = 0; idx < max_vars; ++idx) {
778 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
780 vi->interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
784 * Process bblocks in reverse order, so the addition of new live ranges
785 * to the intervals is faster.
787 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
788 MonoBasicBlock *bb = cfg->bblocks [bnum];
791 block_from = (bb->dfn << 16) + 1; /* so pos > 0 */
792 if (bnum < cfg->num_bblocks - 1)
793 /* Beginning of the next bblock */
794 block_to = (cfg->bblocks [bnum + 1]->dfn << 16) + 1;
796 block_to = (bb->dfn << 16) + 0xffff;
798 LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
800 memset (last_use, 0, max_vars * sizeof (gint32));
802 /* For variables in bb->live_out, set last_use to block_to */
804 rem = max_vars % BITS_PER_CHUNK;
805 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
806 for (j = 0; j < max; ++j) {
810 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
811 k = (j * BITS_PER_CHUNK);
814 LIVENESS_DEBUG (printf ("Var R%d live at exit, set last_use to %x\n", cfg->varinfo [k]->dreg, block_to));
815 last_use [k] = block_to;
823 last_use [cfg->ret->inst_c0] = block_to;
825 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
826 if (nins >= reverse_len) {
827 int new_reverse_len = reverse_len * 2;
828 MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
829 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
830 reverse = new_reverse;
831 reverse_len = new_reverse_len;
834 reverse [nins] = ins;
837 /* Process instructions backwards */
838 for (i = nins - 1; i >= 0; --i) {
839 MonoInst *ins = (MonoInst*)reverse [i];
841 update_liveness2 (cfg, ins, FALSE, pos, last_use);
846 for (idx = 0; idx < max_vars; ++idx) {
847 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
849 if (last_use [idx] != 0) {
850 /* Live at exit, not written -> live on enter */
851 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]));
852 mono_linterval_add_range (cfg, vi->interval, block_from, last_use [idx]);
858 * Arguments need to have their live ranges extended to the beginning of
859 * the method to account for the arg reg/memory -> global register copies
860 * in the prolog (bug #74992).
862 for (i = 0; i < max_vars; i ++) {
863 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
864 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
865 mono_linterval_add_range (cfg, vi->interval, 0, 1);
869 for (idx = 0; idx < max_vars; ++idx) {
870 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
872 LIVENESS_DEBUG (printf ("LIVENESS R%d: ", cfg->varinfo [idx]->dreg));
873 LIVENESS_DEBUG (mono_linterval_print (vi->interval));
874 LIVENESS_DEBUG (printf ("\n"));