2 * liveness.c: liveness analysis
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13 #include <mono/utils/mono-compiler.h>
19 #define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
21 #define DEBUG_LIVENESS
23 #define BITS_PER_CHUNK MONO_BITSET_BITS_PER_CHUNK
25 #define BB_ID_SHIFT 18
28 * The liveness2 pass can't handle long vars on 32 bit platforms because the component
29 * vars have the same 'idx'.
31 #if SIZEOF_REGISTER == 8
32 #define ENABLE_LIVENESS2
35 #ifdef ENABLE_LIVENESS2
36 static void mono_analyze_liveness2 (MonoCompile *cfg);
40 optimize_initlocals (MonoCompile *cfg);
42 /* mono_bitset_mp_new:
44 * allocates a MonoBitSet inside a memory pool
46 static inline MonoBitSet*
47 mono_bitset_mp_new (MonoMemPool *mp, guint32 size, guint32 max_size)
49 guint8 *mem = (guint8 *)mono_mempool_alloc0 (mp, size);
50 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
53 static inline MonoBitSet*
54 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 size, guint32 max_size)
56 guint8 *mem = (guint8 *)mono_mempool_alloc (mp, size);
57 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
60 G_GNUC_UNUSED static void
61 mono_bitset_print (MonoBitSet *set)
64 gboolean first = TRUE;
67 for (i = 0; i < mono_bitset_size (set); i++) {
69 if (mono_bitset_test (set, i)) {
80 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
85 if (g_slist_find (*visited, bb))
88 for (ins = bb->code; ins; ins = ins->next) {
89 const char *spec = INS_INFO (ins->opcode);
90 int regtype, srcindex, sreg, num_sregs;
91 int sregs [MONO_MAX_SRC_REGS];
93 if (ins->opcode == OP_NOP)
97 regtype = spec [MONO_INST_DEST];
98 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
100 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
101 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
102 int idx = var->inst_c0;
103 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
105 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
106 if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
107 /* Make the component vregs volatile as well (#612206) */
108 get_vreg_to_inst (cfg, MONO_LVREG_LS (var->dreg))->flags |= MONO_INST_VOLATILE;
109 get_vreg_to_inst (cfg, MONO_LVREG_MS (var->dreg))->flags |= MONO_INST_VOLATILE;
114 num_sregs = mono_inst_get_src_registers (ins, sregs);
115 for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
116 sreg = sregs [srcindex];
118 g_assert (sreg != -1);
119 if (get_vreg_to_inst (cfg, sreg)) {
120 MonoInst *var = get_vreg_to_inst (cfg, sreg);
121 int idx = var->inst_c0;
122 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
124 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
125 if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
126 /* Make the component vregs volatile as well (#612206) */
127 get_vreg_to_inst (cfg, MONO_LVREG_LS (var->dreg))->flags |= MONO_INST_VOLATILE;
128 get_vreg_to_inst (cfg, MONO_LVREG_MS (var->dreg))->flags |= MONO_INST_VOLATILE;
134 *visited = g_slist_append (*visited, bb);
137 * Need to visit all bblocks reachable from this one since they can be
138 * reached during exception handling.
140 for (i = 0; i < bb->out_count; ++i) {
141 visit_bb (cfg, bb->out_bb [i], visited);
146 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
149 GSList *visited = NULL;
150 MonoMethodHeader *header = cfg->header;
151 MonoExceptionClause *clause, *clause2;
156 * Determine which clauses are outer try clauses, i.e. they are not contained in any
157 * other non-try clause.
159 outer_try = (gboolean *)mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * header->num_clauses);
160 for (i = 0; i < header->num_clauses; ++i)
161 outer_try [i] = TRUE;
162 /* Iterate over the clauses backward, so outer clauses come first */
163 /* This avoids doing an O(2) search, since we can determine when inner clauses end */
164 for (i = header->num_clauses - 1; i >= 0; --i) {
165 clause = &header->clauses [i];
167 if (clause->flags != 0) {
168 outer_try [i] = TRUE;
169 /* Iterate over inner clauses */
170 for (j = i - 1; j >= 0; --j) {
171 clause2 = &header->clauses [j];
173 if (clause2->flags == 0 && MONO_OFFSET_IN_HANDLER (clause, clause2->try_offset)) {
174 outer_try [j] = FALSE;
177 if (clause2->try_offset < clause->try_offset)
178 /* End of inner clauses */
185 * Variables in exception handler register cannot be allocated to registers
186 * so make them volatile. See bug #42136. This will not be neccessary when
187 * the back ends could guarantee that the variables will be in the
188 * correct registers when a handler is called.
189 * This includes try blocks too, since a variable in a try block might be
190 * accessed after an exception handler has been run.
192 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
194 if (bb->region == -1)
197 if (MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY) && outer_try [MONO_REGION_CLAUSE_INDEX (bb->region)])
200 if (cfg->verbose_level > 2)
201 printf ("pessimize variables in bb %d.\n", bb->block_num);
203 visit_bb (cfg, bb, &visited);
205 g_slist_free (visited);
209 update_live_range (MonoMethodVar *var, int abs_pos)
211 if (var->range.first_use.abs_pos > abs_pos)
212 var->range.first_use.abs_pos = abs_pos;
214 if (var->range.last_use.abs_pos < abs_pos)
215 var->range.last_use.abs_pos = abs_pos;
219 analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
223 MonoMethodVar *vars = cfg->vars;
224 guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
226 /* Start inst_num from > 0, so last_use.abs_pos is only 0 for dead variables */
227 for (inst_num = 2, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
228 const char *spec = INS_INFO (ins->opcode);
230 int sregs [MONO_MAX_SRC_REGS];
232 #ifdef DEBUG_LIVENESS
233 if (cfg->verbose_level > 1) {
235 mono_print_ins (ins);
239 if (ins->opcode == OP_NOP)
242 if (ins->opcode == OP_LDADDR) {
243 MonoInst *var = (MonoInst *)ins->inst_p0;
244 int idx = var->inst_c0;
245 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
247 #ifdef DEBUG_LIVENESS
248 if (cfg->verbose_level > 1)
249 printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
251 update_live_range (&vars [idx], abs_pos + inst_num);
252 if (!mono_bitset_test_fast (bb->kill_set, idx))
253 mono_bitset_set_fast (bb->gen_set, idx);
254 vi->spill_costs += SPILL_COST_INCREMENT;
257 /* SREGs must come first, so MOVE r <- r is handled correctly */
258 num_sregs = mono_inst_get_src_registers (ins, sregs);
259 for (i = 0; i < num_sregs; ++i) {
261 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
262 MonoInst *var = get_vreg_to_inst (cfg, sreg);
263 int idx = var->inst_c0;
264 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
266 #ifdef DEBUG_LIVENESS
267 if (cfg->verbose_level > 1)
268 printf ("\tGEN: R%d(%d)\n", sreg, idx);
270 update_live_range (&vars [idx], abs_pos + inst_num);
271 if (!mono_bitset_test_fast (bb->kill_set, idx))
272 mono_bitset_set_fast (bb->gen_set, idx);
273 vi->spill_costs += SPILL_COST_INCREMENT;
278 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
279 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
280 int idx = var->inst_c0;
281 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
283 if (MONO_IS_STORE_MEMBASE (ins)) {
284 update_live_range (&vars [idx], abs_pos + inst_num);
285 if (!mono_bitset_test_fast (bb->kill_set, idx))
286 mono_bitset_set_fast (bb->gen_set, idx);
287 vi->spill_costs += SPILL_COST_INCREMENT;
289 #ifdef DEBUG_LIVENESS
290 if (cfg->verbose_level > 1)
291 printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
293 update_live_range (&vars [idx], abs_pos + inst_num + 1);
294 mono_bitset_set_fast (bb->kill_set, idx);
295 vi->spill_costs += SPILL_COST_INCREMENT;
301 /* generic liveness analysis code. CFG specific parts are
302 * in update_gen_kill_set()
305 mono_analyze_liveness (MonoCompile *cfg)
307 MonoBitSet *old_live_out_set;
308 int i, j, max_vars = cfg->num_varinfo;
310 gboolean *in_worklist;
311 MonoBasicBlock **worklist;
315 #ifdef DEBUG_LIVENESS
316 if (cfg->verbose_level > 1)
317 printf ("\nLIVENESS:\n");
320 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
322 cfg->comp_done |= MONO_COMP_LIVENESS;
327 bitsize = mono_bitset_alloc_size (max_vars, 0);
329 for (i = 0; i < max_vars; i ++) {
330 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
331 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
332 MONO_VARINFO (cfg, i)->spill_costs = 0;
335 for (i = 0; i < cfg->num_bblocks; ++i) {
336 MonoBasicBlock *bb = cfg->bblocks [i];
338 bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
339 bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
341 #ifdef DEBUG_LIVENESS
342 if (cfg->verbose_level > 1) {
343 printf ("BLOCK BB%d (", bb->block_num);
344 for (j = 0; j < bb->out_count; j++)
345 printf ("BB%d, ", bb->out_bb [j]->block_num);
351 analyze_liveness_bb (cfg, bb);
353 #ifdef DEBUG_LIVENESS
354 if (cfg->verbose_level > 1) {
355 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
356 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
361 old_live_out_set = mono_bitset_new (max_vars, 0);
362 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
364 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
368 * This is a backward dataflow analysis problem, so we process blocks in
369 * decreasing dfn order, this speeds up the iteration.
371 for (i = 0; i < cfg->num_bblocks; i ++) {
372 MonoBasicBlock *bb = cfg->bblocks [i];
374 worklist [l_end ++] = bb;
375 in_worklist [bb->dfn] = TRUE;
377 /* Initialized later */
378 bb->live_in_set = NULL;
379 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
384 if (cfg->verbose_level > 1)
385 printf ("\nITERATION:\n");
388 MonoBasicBlock *bb = worklist [--l_end];
389 MonoBasicBlock *out_bb;
392 in_worklist [bb->dfn] = FALSE;
394 #ifdef DEBUG_LIVENESS
395 if (cfg->verbose_level > 1) {
396 printf ("P: BB%d(%d): IN: ", bb->block_num, bb->dfn);
397 for (j = 0; j < bb->in_count; ++j)
398 printf ("BB%d ", bb->in_bb [j]->block_num);
400 for (j = 0; j < bb->out_count; ++j)
401 printf ("BB%d ", bb->out_bb [j]->block_num);
407 if (bb->out_count == 0)
412 if (!bb->live_in_set) {
413 /* First pass over this bblock */
418 mono_bitset_copyto_fast (bb->live_out_set, old_live_out_set);
421 for (j = 0; j < bb->out_count; j++) {
422 out_bb = bb->out_bb [j];
424 if (!out_bb->live_in_set) {
425 out_bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
427 mono_bitset_copyto_fast (out_bb->live_out_set, out_bb->live_in_set);
428 mono_bitset_sub_fast (out_bb->live_in_set, out_bb->kill_set);
429 mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
432 // FIXME: Do this somewhere else
433 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
435 mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
439 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
440 if (!bb->live_in_set)
441 bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
442 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
443 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
444 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
446 for (j = 0; j < bb->in_count; j++) {
447 MonoBasicBlock *in_bb = bb->in_bb [j];
449 * Some basic blocks do not seem to be in the
450 * cfg->bblocks array...
452 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
453 #ifdef DEBUG_LIVENESS
454 if (cfg->verbose_level > 1)
455 printf ("\tADD: %d\n", in_bb->block_num);
458 * Put the block at the top of the stack, so it
459 * will be processed right away.
461 worklist [l_end ++] = in_bb;
462 in_worklist [in_bb->dfn] = TRUE;
467 if (G_UNLIKELY (cfg->verbose_level > 1)) {
468 printf ("\tLIVE IN BB%d: ", bb->block_num);
469 mono_bitset_print (bb->live_in_set);
473 #ifdef DEBUG_LIVENESS
474 if (cfg->verbose_level > 1)
475 printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
478 mono_bitset_free (old_live_out_set);
481 g_free (in_worklist);
483 /* Compute live_in_set for bblocks skipped earlier */
484 for (i = 0; i < cfg->num_bblocks; ++i) {
485 MonoBasicBlock *bb = cfg->bblocks [i];
487 if (!bb->live_in_set) {
488 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
490 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
491 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
492 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
496 for (i = 0; i < cfg->num_bblocks; ++i) {
497 MonoBasicBlock *bb = cfg->bblocks [i];
499 guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
500 MonoMethodVar *vars = cfg->vars;
502 if (!bb->live_out_set)
505 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
506 for (j = 0; j < max; ++j) {
511 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
512 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
514 k = (j * BITS_PER_CHUNK);
515 while ((bits_in || bits_out)) {
517 update_live_range (&vars [k], abs_pos + 0);
519 update_live_range (&vars [k], abs_pos + ((1 << BB_ID_SHIFT) - 1));
528 * Arguments need to have their live ranges extended to the beginning of
529 * the method to account for the arg reg/memory -> global register copies
530 * in the prolog (bug #74992).
533 for (i = 0; i < max_vars; i ++) {
534 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
535 if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
536 if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
538 * Can't eliminate the this variable in gshared code, since
539 * it is needed during exception handling to identify the
541 * It is better to check for this here instead of marking the variable
542 * VOLATILE, since that would prevent it from being allocated to
545 if (!cfg->disable_deadce_vars && !(cfg->gshared && mono_method_signature (cfg->method)->hasthis && cfg->varinfo [vi->idx] == cfg->args [0]))
546 cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
548 vi->range.first_use.abs_pos = 0;
552 #ifdef DEBUG_LIVENESS
553 if (cfg->verbose_level > 1) {
554 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
555 MonoBasicBlock *bb = cfg->bblocks [i];
557 printf ("LIVE IN BB%d: ", bb->block_num);
558 mono_bitset_print (bb->live_in_set);
559 printf ("LIVE OUT BB%d: ", bb->block_num);
560 mono_bitset_print (bb->live_out_set);
563 for (i = 0; i < max_vars; i ++) {
564 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
566 printf ("V%d: [0x%x - 0x%x]\n", i, vi->range.first_use.abs_pos, vi->range.last_use.abs_pos);
571 if (!cfg->disable_initlocals_opt)
572 optimize_initlocals (cfg);
574 #ifdef ENABLE_LIVENESS2
575 /* This improves code size by about 5% but slows down compilation too much */
576 if (cfg->compile_aot)
577 mono_analyze_liveness2 (cfg);
582 * optimize_initlocals:
584 * Try to optimize away some of the redundant initialization code inserted because of
585 * 'locals init' using the liveness information.
588 optimize_initlocals (MonoCompile *cfg)
592 MonoBasicBlock *initlocals_bb;
594 used = mono_bitset_new (cfg->next_vreg + 1, 0);
596 mono_bitset_clear_all (used);
597 initlocals_bb = cfg->bb_entry->next_bb;
598 for (ins = initlocals_bb->code; ins; ins = ins->next) {
600 int sregs [MONO_MAX_SRC_REGS];
602 num_sregs = mono_inst_get_src_registers (ins, sregs);
603 for (i = 0; i < num_sregs; ++i)
604 mono_bitset_set_fast (used, sregs [i]);
606 if (MONO_IS_STORE_MEMBASE (ins))
607 mono_bitset_set_fast (used, ins->dreg);
610 for (ins = initlocals_bb->code; ins; ins = ins->next) {
611 const char *spec = INS_INFO (ins->opcode);
613 /* Look for statements whose dest is not used in this bblock and not live on exit. */
614 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
615 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
617 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))) {
618 //printf ("DEAD: "); mono_print_ins (ins);
619 if (cfg->disable_initlocals_opt_refs && var->type == STACK_OBJ)
621 if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST) || (ins->opcode == OP_R4CONST)) {
623 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
625 * We should shorten the liveness interval of these vars as well, but
626 * don't have enough info to do that.
637 mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
639 MonoLiveRange2 *prev_range, *next_range, *new_range;
641 g_assert (to >= from);
643 /* Optimize for extending the first interval backwards */
644 if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
645 interval->range->from = from;
649 /* Find a place in the list for the new range */
651 next_range = interval->range;
652 while ((next_range != NULL) && (next_range->from <= from)) {
653 prev_range = next_range;
654 next_range = next_range->next;
657 if (prev_range && prev_range->to == from) {
658 /* Merge with previous */
660 } else if (next_range && next_range->from == to) {
661 /* Merge with previous */
662 next_range->from = from;
665 new_range = (MonoLiveRange2 *)mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
666 new_range->from = from;
668 new_range->next = NULL;
671 prev_range->next = new_range;
673 interval->range = new_range;
675 new_range->next = next_range;
677 interval->last_range = new_range;
680 /* FIXME: Merge intersecting ranges */
684 mono_linterval_print (MonoLiveInterval *interval)
686 MonoLiveRange2 *range;
688 for (range = interval->range; range != NULL; range = range->next)
689 printf ("[%x-%x] ", range->from, range->to);
693 mono_linterval_print_nl (MonoLiveInterval *interval)
695 mono_linterval_print (interval);
700 * mono_linterval_convers:
702 * Return whenever INTERVAL covers the position POS.
705 mono_linterval_covers (MonoLiveInterval *interval, int pos)
707 MonoLiveRange2 *range;
709 for (range = interval->range; range != NULL; range = range->next) {
710 if (pos >= range->from && pos <= range->to)
712 if (range->from > pos)
720 * mono_linterval_get_intersect_pos:
722 * Determine whenever I1 and I2 intersect, and if they do, return the first
723 * point of intersection. Otherwise, return -1.
726 mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
728 MonoLiveRange2 *r1, *r2;
730 /* FIXME: Optimize this */
731 for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
732 for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
733 if (r2->to > r1->from && r2->from < r1->to) {
734 if (r2->from <= r1->from)
746 * mono_linterval_split
748 * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
752 mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
756 g_assert (pos > interval->range->from && pos <= interval->last_range->to);
758 *i1 = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
759 *i2 = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
761 for (r = interval->range; r; r = r->next) {
763 /* Add it to the first child */
764 mono_linterval_add_range (cfg, *i1, r->from, r->to);
765 } else if (pos > r->from && pos <= r->to) {
767 mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
768 mono_linterval_add_range (cfg, *i2, pos, r->to);
770 /* Add it to the second child */
771 mono_linterval_add_range (cfg, *i2, r->from, r->to);
777 #define LIVENESS_DEBUG(a) do { if (cfg->verbose_level > 1) do { a; } while (0); } while (0)
778 #define ENABLE_LIVENESS_DEBUG 1
780 #define LIVENESS_DEBUG(a)
783 #ifdef ENABLE_LIVENESS2
786 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
788 const char *spec = INS_INFO (ins->opcode);
791 int sregs [MONO_MAX_SRC_REGS];
793 LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
795 if (ins->opcode == OP_NOP || ins->opcode == OP_IL_SEQ_POINT)
799 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
800 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
801 int idx = var->inst_c0;
802 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
804 if (MONO_IS_STORE_MEMBASE (ins)) {
805 if (last_use [idx] == 0) {
806 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
807 last_use [idx] = inst_num;
810 if (last_use [idx] > 0) {
811 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
812 mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
816 /* Try dead code elimination */
817 if (!cfg->disable_deadce_vars && (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)) {
818 LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
822 int inst_num_add = 1;
823 MonoInst *next = ins->next;
824 while (next && next->opcode == OP_IL_SEQ_POINT) {
829 LIVENESS_DEBUG (printf ("\tdead def of R%d, add range to R%d: [%x, %x]\n", ins->dreg, ins->dreg, inst_num, inst_num + inst_num_add));
830 mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + inst_num_add);
837 num_sregs = mono_inst_get_src_registers (ins, sregs);
838 for (i = 0; i < num_sregs; ++i) {
840 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
841 MonoInst *var = get_vreg_to_inst (cfg, sreg);
842 int idx = var->inst_c0;
844 if (last_use [idx] == 0) {
845 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
846 last_use [idx] = inst_num;
853 mono_analyze_liveness2 (MonoCompile *cfg)
855 int bnum, idx, i, j, nins, max, max_vars, block_from, block_to, pos;
857 static guint32 disabled = -1;
860 disabled = g_getenv ("DISABLED") != NULL;
865 if (cfg->num_bblocks >= (1 << (32 - BB_ID_SHIFT - 1)))
866 /* Ranges would overflow */
869 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
870 MonoBasicBlock *bb = cfg->bblocks [bnum];
874 for (nins = 0, ins = bb->code; ins; ins = ins->next, ++nins)
877 if (nins >= ((1 << BB_ID_SHIFT) - 1))
878 /* Ranges would overflow */
882 LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
885 if (strstr (cfg->method->name, "test_") != cfg->method->name)
889 max_vars = cfg->num_varinfo;
890 last_use = g_new0 (gint32, max_vars);
892 for (idx = 0; idx < max_vars; ++idx) {
893 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
895 vi->interval = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
899 * Process bblocks in reverse order, so the addition of new live ranges
900 * to the intervals is faster.
902 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
903 MonoBasicBlock *bb = cfg->bblocks [bnum];
906 block_from = (bb->dfn << BB_ID_SHIFT) + 1; /* so pos > 0 */
907 if (bnum < cfg->num_bblocks - 1)
908 /* Beginning of the next bblock */
909 block_to = (cfg->bblocks [bnum + 1]->dfn << BB_ID_SHIFT) + 1;
911 block_to = (bb->dfn << BB_ID_SHIFT) + ((1 << BB_ID_SHIFT) - 1);
913 LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
915 memset (last_use, 0, max_vars * sizeof (gint32));
917 /* For variables in bb->live_out, set last_use to block_to */
919 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
920 for (j = 0; j < max; ++j) {
924 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
925 k = (j * BITS_PER_CHUNK);
928 LIVENESS_DEBUG (printf ("Var R%d live at exit, set last_use to %x\n", cfg->varinfo [k]->dreg, block_to));
929 last_use [k] = block_to;
937 last_use [cfg->ret->inst_c0] = block_to;
939 pos = block_from + 1;
940 MONO_BB_FOR_EACH_INS (bb, ins) pos++;
942 /* Process instructions backwards */
943 MONO_BB_FOR_EACH_INS_REVERSE (bb, ins) {
944 update_liveness2 (cfg, ins, FALSE, pos, last_use);
948 for (idx = 0; idx < max_vars; ++idx) {
949 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
951 if (last_use [idx] != 0) {
952 /* Live at exit, not written -> live on enter */
953 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]));
954 mono_linterval_add_range (cfg, vi->interval, block_from, last_use [idx]);
960 * Arguments need to have their live ranges extended to the beginning of
961 * the method to account for the arg reg/memory -> global register copies
962 * in the prolog (bug #74992).
964 for (i = 0; i < max_vars; i ++) {
965 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
966 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
967 mono_linterval_add_range (cfg, vi->interval, 0, 1);
971 for (idx = 0; idx < max_vars; ++idx) {
972 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
974 LIVENESS_DEBUG (printf ("LIVENESS R%d: ", cfg->varinfo [idx]->dreg));
975 LIVENESS_DEBUG (mono_linterval_print (vi->interval));
976 LIVENESS_DEBUG (printf ("\n"));
985 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
988 update_liveness_gc (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, gint32 *last_use, MonoMethodVar **vreg_to_varinfo, GSList **callsites)
990 if (ins->opcode == OP_GC_LIVENESS_DEF || ins->opcode == OP_GC_LIVENESS_USE) {
991 int vreg = ins->inst_c1;
992 MonoMethodVar *vi = vreg_to_varinfo [vreg];
994 int pc_offset = ins->backend.pc_offset;
996 LIVENESS_DEBUG (printf ("\t%x: ", pc_offset); mono_print_ins (ins));
998 if (ins->opcode == OP_GC_LIVENESS_DEF) {
999 if (last_use [idx] > 0) {
1000 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", vreg, pc_offset, last_use [idx]));
1004 if (last_use [idx] == 0) {
1005 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", vreg, pc_offset));
1006 last_use [idx] = pc_offset;
1009 } else if (ins->opcode == OP_GC_PARAM_SLOT_LIVENESS_DEF) {
1012 /* Add it to the last callsite */
1013 g_assert (*callsites);
1014 last = (GCCallSite *)(*callsites)->data;
1015 last->param_slots = g_slist_prepend_mempool (cfg->mempool, last->param_slots, ins);
1016 } else if (ins->flags & MONO_INST_GC_CALLSITE) {
1017 GCCallSite *callsite = (GCCallSite *)mono_mempool_alloc0 (cfg->mempool, sizeof (GCCallSite));
1020 LIVENESS_DEBUG (printf ("\t%x: ", ins->backend.pc_offset); mono_print_ins (ins));
1021 LIVENESS_DEBUG (printf ("\t\tlive: "));
1024 callsite->liveness = (guint8 *)mono_mempool_alloc0 (cfg->mempool, ALIGN_TO (cfg->num_varinfo, 8) / 8);
1025 callsite->pc_offset = ins->backend.pc_offset;
1026 for (i = 0; i < cfg->num_varinfo; ++i) {
1027 if (last_use [i] != 0) {
1028 LIVENESS_DEBUG (printf ("R%d", MONO_VARINFO (cfg, i)->vreg));
1029 callsite->liveness [i / 8] |= (1 << (i % 8));
1032 LIVENESS_DEBUG (printf ("\n"));
1033 *callsites = g_slist_prepend_mempool (cfg->mempool, *callsites, callsite);
1038 get_vreg_from_var (MonoCompile *cfg, MonoInst *var)
1040 if (var->opcode == OP_REGVAR)
1041 /* dreg contains a hreg, but inst_c0 still contains the var index */
1042 return MONO_VARINFO (cfg, var->inst_c0)->vreg;
1044 /* dreg still contains the vreg */
1049 * mono_analyze_liveness_gc:
1051 * Compute liveness bitmaps for each call site.
1052 * This function is a modified version of mono_analyze_liveness2 ().
1055 mono_analyze_liveness_gc (MonoCompile *cfg)
1057 int idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
1060 MonoMethodVar **vreg_to_varinfo = NULL;
1064 LIVENESS_DEBUG (printf ("\n------------ GC LIVENESS: ----------\n"));
1066 max_vars = cfg->num_varinfo;
1067 last_use = g_new0 (gint32, max_vars);
1070 * var->inst_c0 no longer contains the variable index, so compute a mapping now.
1072 vreg_to_varinfo = g_new0 (MonoMethodVar*, cfg->next_vreg);
1073 for (idx = 0; idx < max_vars; ++idx) {
1074 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
1076 vreg_to_varinfo [vi->vreg] = vi;
1080 reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
1082 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1085 block_from = bb->real_native_offset;
1086 block_to = bb->native_offset + bb->native_length;
1088 LIVENESS_DEBUG (printf ("GC LIVENESS BB%d:\n", bb->block_num));
1093 memset (last_use, 0, max_vars * sizeof (gint32));
1095 /* For variables in bb->live_out, set last_use to block_to */
1097 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
1098 for (j = 0; j < max; ++j) {
1102 if (!bb->live_out_set)
1103 /* The variables used in this bblock are volatile anyway */
1106 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
1107 k = (j * BITS_PER_CHUNK);
1109 if ((bits_out & 1) && cfg->varinfo [k]->flags & MONO_INST_GC_TRACK) {
1110 int vreg = get_vreg_from_var (cfg, cfg->varinfo [k]);
1111 LIVENESS_DEBUG (printf ("Var R%d live at exit, last_use set to %x.\n", vreg, block_to));
1112 last_use [k] = block_to;
1119 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
1120 if (nins >= reverse_len) {
1121 int new_reverse_len = reverse_len * 2;
1122 MonoInst **new_reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
1123 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
1124 reverse = new_reverse;
1125 reverse_len = new_reverse_len;
1128 reverse [nins] = ins;
1131 /* Process instructions backwards */
1133 for (i = nins - 1; i >= 0; --i) {
1134 MonoInst *ins = (MonoInst*)reverse [i];
1136 update_liveness_gc (cfg, bb, ins, last_use, vreg_to_varinfo, &callsites);
1138 /* The callsites should already be sorted by pc offset because we added them backwards */
1139 bb->gc_callsites = callsites;
1143 g_free (vreg_to_varinfo);
1146 #else /* !DISABLE_JIT */
1148 MONO_EMPTY_SOURCE_FILE (liveness);
1150 #endif /* !DISABLE_JIT */