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)
17 #define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
19 #define DEBUG_LIVENESS
21 #define BITS_PER_CHUNK MONO_BITSET_BITS_PER_CHUNK
23 #define BB_ID_SHIFT 18
26 * The liveness2 pass can't handle long vars on 32 bit platforms because the component
27 * vars have the same 'idx'.
29 #if SIZEOF_REGISTER == 8
30 #define ENABLE_LIVENESS2
33 #ifdef ENABLE_LIVENESS2
34 static void mono_analyze_liveness2 (MonoCompile *cfg);
38 optimize_initlocals (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)
62 gboolean first = TRUE;
65 for (i = 0; i < mono_bitset_size (set); i++) {
67 if (mono_bitset_test (set, i)) {
78 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
83 if (g_slist_find (*visited, bb))
86 for (ins = bb->code; ins; ins = ins->next) {
87 const char *spec = INS_INFO (ins->opcode);
88 int regtype, srcindex, sreg, num_sregs;
89 int sregs [MONO_MAX_SRC_REGS];
91 if (ins->opcode == OP_NOP)
95 regtype = spec [MONO_INST_DEST];
96 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
98 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
99 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
100 int idx = var->inst_c0;
101 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
103 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
104 if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
105 /* Make the component vregs volatile as well (#612206) */
106 get_vreg_to_inst (cfg, var->dreg + 1)->flags |= MONO_INST_VOLATILE;
107 get_vreg_to_inst (cfg, var->dreg + 2)->flags |= MONO_INST_VOLATILE;
112 num_sregs = mono_inst_get_src_registers (ins, sregs);
113 for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
114 sreg = sregs [srcindex];
116 g_assert (sreg != -1);
117 if (get_vreg_to_inst (cfg, sreg)) {
118 MonoInst *var = get_vreg_to_inst (cfg, sreg);
119 int idx = var->inst_c0;
120 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
122 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
123 if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
124 /* Make the component vregs volatile as well (#612206) */
125 get_vreg_to_inst (cfg, var->dreg + 1)->flags |= MONO_INST_VOLATILE;
126 get_vreg_to_inst (cfg, var->dreg + 2)->flags |= MONO_INST_VOLATILE;
132 *visited = g_slist_append (*visited, bb);
135 * Need to visit all bblocks reachable from this one since they can be
136 * reached during exception handling.
138 for (i = 0; i < bb->out_count; ++i) {
139 visit_bb (cfg, bb->out_bb [i], visited);
144 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
147 GSList *visited = NULL;
148 MonoMethodHeader *header = cfg->header;
149 MonoExceptionClause *clause, *clause2;
154 * Determine which clauses are outer try clauses, i.e. they are not contained in any
155 * other non-try clause.
157 outer_try = mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * header->num_clauses);
158 for (i = 0; i < header->num_clauses; ++i)
159 outer_try [i] = TRUE;
160 /* Iterate over the clauses backward, so outer clauses come first */
161 /* This avoids doing an O(2) search, since we can determine when inner clauses end */
162 for (i = header->num_clauses - 1; i >= 0; --i) {
163 clause = &header->clauses [i];
165 if (clause->flags != 0) {
166 outer_try [i] = TRUE;
167 /* Iterate over inner clauses */
168 for (j = i - 1; j >= 0; --j) {
169 clause2 = &header->clauses [j];
171 if (clause2->flags == 0 && MONO_OFFSET_IN_HANDLER (clause, clause2->try_offset)) {
172 outer_try [j] = FALSE;
175 if (clause2->try_offset < clause->try_offset)
176 /* End of inner clauses */
183 * Variables in exception handler register cannot be allocated to registers
184 * so make them volatile. See bug #42136. This will not be neccessary when
185 * the back ends could guarantee that the variables will be in the
186 * correct registers when a handler is called.
187 * This includes try blocks too, since a variable in a try block might be
188 * accessed after an exception handler has been run.
190 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
192 if (bb->region == -1)
195 if (MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY) && outer_try [MONO_REGION_CLAUSE_INDEX (bb->region)])
198 if (cfg->verbose_level > 2)
199 printf ("pessimize variables in bb %d.\n", bb->block_num);
201 visit_bb (cfg, bb, &visited);
203 g_slist_free (visited);
207 update_live_range (MonoMethodVar *var, int abs_pos)
209 if (var->range.first_use.abs_pos > abs_pos)
210 var->range.first_use.abs_pos = abs_pos;
212 if (var->range.last_use.abs_pos < abs_pos)
213 var->range.last_use.abs_pos = abs_pos;
217 analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
221 MonoMethodVar *vars = cfg->vars;
222 guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
224 /* Start inst_num from > 0, so last_use.abs_pos is only 0 for dead variables */
225 for (inst_num = 2, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
226 const char *spec = INS_INFO (ins->opcode);
228 int sregs [MONO_MAX_SRC_REGS];
230 #ifdef DEBUG_LIVENESS
231 if (cfg->verbose_level > 1) {
233 mono_print_ins (ins);
237 if (ins->opcode == OP_NOP)
240 if (ins->opcode == OP_LDADDR) {
241 MonoInst *var = ins->inst_p0;
242 int idx = var->inst_c0;
243 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
245 #ifdef DEBUG_LIVENESS
246 if (cfg->verbose_level > 1)
247 printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
249 update_live_range (&vars [idx], abs_pos + inst_num);
250 if (!mono_bitset_test_fast (bb->kill_set, idx))
251 mono_bitset_set_fast (bb->gen_set, idx);
252 vi->spill_costs += SPILL_COST_INCREMENT;
255 /* SREGs must come first, so MOVE r <- r is handled correctly */
256 num_sregs = mono_inst_get_src_registers (ins, sregs);
257 for (i = 0; i < num_sregs; ++i) {
259 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
260 MonoInst *var = get_vreg_to_inst (cfg, sreg);
261 int idx = var->inst_c0;
262 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
264 #ifdef DEBUG_LIVENESS
265 if (cfg->verbose_level > 1)
266 printf ("\tGEN: R%d(%d)\n", sreg, idx);
268 update_live_range (&vars [idx], abs_pos + inst_num);
269 if (!mono_bitset_test_fast (bb->kill_set, idx))
270 mono_bitset_set_fast (bb->gen_set, idx);
271 vi->spill_costs += SPILL_COST_INCREMENT;
276 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
277 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
278 int idx = var->inst_c0;
279 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
281 if (MONO_IS_STORE_MEMBASE (ins)) {
282 update_live_range (&vars [idx], abs_pos + inst_num);
283 if (!mono_bitset_test_fast (bb->kill_set, idx))
284 mono_bitset_set_fast (bb->gen_set, idx);
285 vi->spill_costs += SPILL_COST_INCREMENT;
287 #ifdef DEBUG_LIVENESS
288 if (cfg->verbose_level > 1)
289 printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
291 update_live_range (&vars [idx], abs_pos + inst_num + 1);
292 mono_bitset_set_fast (bb->kill_set, idx);
293 vi->spill_costs += SPILL_COST_INCREMENT;
299 /* generic liveness analysis code. CFG specific parts are
300 * in update_gen_kill_set()
303 mono_analyze_liveness (MonoCompile *cfg)
305 MonoBitSet *old_live_out_set;
306 int i, j, max_vars = cfg->num_varinfo;
308 gboolean *in_worklist;
309 MonoBasicBlock **worklist;
313 #ifdef DEBUG_LIVENESS
314 if (cfg->verbose_level > 1)
315 printf ("\nLIVENESS:\n");
318 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
320 cfg->comp_done |= MONO_COMP_LIVENESS;
325 bitsize = mono_bitset_alloc_size (max_vars, 0);
327 for (i = 0; i < max_vars; i ++) {
328 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
329 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
330 MONO_VARINFO (cfg, i)->spill_costs = 0;
333 for (i = 0; i < cfg->num_bblocks; ++i) {
334 MonoBasicBlock *bb = cfg->bblocks [i];
336 bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
337 bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
339 #ifdef DEBUG_LIVENESS
340 if (cfg->verbose_level > 1) {
341 printf ("BLOCK BB%d (", bb->block_num);
342 for (j = 0; j < bb->out_count; j++)
343 printf ("BB%d, ", bb->out_bb [j]->block_num);
349 analyze_liveness_bb (cfg, bb);
351 #ifdef DEBUG_LIVENESS
352 if (cfg->verbose_level > 1) {
353 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
354 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
359 old_live_out_set = mono_bitset_new (max_vars, 0);
360 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
362 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
366 * This is a backward dataflow analysis problem, so we process blocks in
367 * decreasing dfn order, this speeds up the iteration.
369 for (i = 0; i < cfg->num_bblocks; i ++) {
370 MonoBasicBlock *bb = cfg->bblocks [i];
372 worklist [l_end ++] = bb;
373 in_worklist [bb->dfn] = TRUE;
375 /* Initialized later */
376 bb->live_in_set = NULL;
377 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
382 if (cfg->verbose_level > 1)
383 printf ("\nITERATION:\n");
386 MonoBasicBlock *bb = worklist [--l_end];
387 MonoBasicBlock *out_bb;
390 in_worklist [bb->dfn] = FALSE;
392 #ifdef DEBUG_LIVENESS
393 if (cfg->verbose_level > 1) {
394 printf ("P: BB%d(%d): IN: ", bb->block_num, bb->dfn);
395 for (j = 0; j < bb->in_count; ++j)
396 printf ("BB%d ", bb->in_bb [j]->block_num);
398 for (j = 0; j < bb->out_count; ++j)
399 printf ("BB%d ", bb->out_bb [j]->block_num);
405 if (bb->out_count == 0)
410 if (!bb->live_in_set) {
411 /* First pass over this bblock */
416 mono_bitset_copyto_fast (bb->live_out_set, old_live_out_set);
419 for (j = 0; j < bb->out_count; j++) {
420 out_bb = bb->out_bb [j];
422 if (!out_bb->live_in_set) {
423 out_bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
425 mono_bitset_copyto_fast (out_bb->live_out_set, out_bb->live_in_set);
426 mono_bitset_sub_fast (out_bb->live_in_set, out_bb->kill_set);
427 mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
430 // FIXME: Do this somewhere else
431 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
433 mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
437 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
438 if (!bb->live_in_set)
439 bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
440 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
441 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
442 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
444 for (j = 0; j < bb->in_count; j++) {
445 MonoBasicBlock *in_bb = bb->in_bb [j];
447 * Some basic blocks do not seem to be in the
448 * cfg->bblocks array...
450 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
451 #ifdef DEBUG_LIVENESS
452 if (cfg->verbose_level > 1)
453 printf ("\tADD: %d\n", in_bb->block_num);
456 * Put the block at the top of the stack, so it
457 * will be processed right away.
459 worklist [l_end ++] = in_bb;
460 in_worklist [in_bb->dfn] = TRUE;
465 if (G_UNLIKELY (cfg->verbose_level > 1)) {
466 printf ("\tLIVE IN BB%d: ", bb->block_num);
467 mono_bitset_print (bb->live_in_set);
471 #ifdef DEBUG_LIVENESS
472 if (cfg->verbose_level > 1)
473 printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
476 mono_bitset_free (old_live_out_set);
479 g_free (in_worklist);
481 /* Compute live_in_set for bblocks skipped earlier */
482 for (i = 0; i < cfg->num_bblocks; ++i) {
483 MonoBasicBlock *bb = cfg->bblocks [i];
485 if (!bb->live_in_set) {
486 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
488 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
489 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
490 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
494 for (i = 0; i < cfg->num_bblocks; ++i) {
495 MonoBasicBlock *bb = cfg->bblocks [i];
497 guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
498 MonoMethodVar *vars = cfg->vars;
500 if (!bb->live_out_set)
503 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
504 for (j = 0; j < max; ++j) {
509 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
510 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
512 k = (j * BITS_PER_CHUNK);
513 while ((bits_in || bits_out)) {
515 update_live_range (&vars [k], abs_pos + 0);
517 update_live_range (&vars [k], abs_pos + ((1 << BB_ID_SHIFT) - 1));
526 * Arguments need to have their live ranges extended to the beginning of
527 * the method to account for the arg reg/memory -> global register copies
528 * in the prolog (bug #74992).
531 for (i = 0; i < max_vars; i ++) {
532 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
533 if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
534 if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
536 * Can't eliminate the this variable in gshared code, since
537 * it is needed during exception handling to identify the
539 * It is better to check for this here instead of marking the variable
540 * VOLATILE, since that would prevent it from being allocated to
543 if (!cfg->disable_deadce_vars && !(cfg->generic_sharing_context && mono_method_signature (cfg->method)->hasthis && cfg->varinfo [vi->idx] == cfg->args [0]))
544 cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
546 vi->range.first_use.abs_pos = 0;
550 #ifdef DEBUG_LIVENESS
551 if (cfg->verbose_level > 1) {
552 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
553 MonoBasicBlock *bb = cfg->bblocks [i];
555 printf ("LIVE IN BB%d: ", bb->block_num);
556 mono_bitset_print (bb->live_in_set);
557 printf ("LIVE OUT BB%d: ", bb->block_num);
558 mono_bitset_print (bb->live_out_set);
561 for (i = 0; i < max_vars; i ++) {
562 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
564 printf ("V%d: [0x%x - 0x%x]\n", i, vi->range.first_use.abs_pos, vi->range.last_use.abs_pos);
569 if (!cfg->disable_initlocals_opt)
570 optimize_initlocals (cfg);
572 #ifdef ENABLE_LIVENESS2
573 /* This improves code size by about 5% but slows down compilation too much */
574 if (cfg->compile_aot)
575 mono_analyze_liveness2 (cfg);
580 * optimize_initlocals:
582 * Try to optimize away some of the redundant initialization code inserted because of
583 * 'locals init' using the liveness information.
586 optimize_initlocals (MonoCompile *cfg)
590 MonoBasicBlock *initlocals_bb;
592 used = mono_bitset_new (cfg->next_vreg + 1, 0);
594 mono_bitset_clear_all (used);
595 initlocals_bb = cfg->bb_entry->next_bb;
596 for (ins = initlocals_bb->code; ins; ins = ins->next) {
598 int sregs [MONO_MAX_SRC_REGS];
600 num_sregs = mono_inst_get_src_registers (ins, sregs);
601 for (i = 0; i < num_sregs; ++i)
602 mono_bitset_set_fast (used, sregs [i]);
604 if (MONO_IS_STORE_MEMBASE (ins))
605 mono_bitset_set_fast (used, ins->dreg);
608 for (ins = initlocals_bb->code; ins; ins = ins->next) {
609 const char *spec = INS_INFO (ins->opcode);
611 /* Look for statements whose dest is not used in this bblock and not live on exit. */
612 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
613 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
615 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))) {
616 //printf ("DEAD: "); mono_print_ins (ins);
617 if (cfg->disable_initlocals_opt_refs && var->type == STACK_OBJ)
619 if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST) || (ins->opcode == OP_R4CONST)) {
621 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
623 * We should shorten the liveness interval of these vars as well, but
624 * don't have enough info to do that.
635 mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
637 MonoLiveRange2 *prev_range, *next_range, *new_range;
639 g_assert (to >= from);
641 /* Optimize for extending the first interval backwards */
642 if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
643 interval->range->from = from;
647 /* Find a place in the list for the new range */
649 next_range = interval->range;
650 while ((next_range != NULL) && (next_range->from <= from)) {
651 prev_range = next_range;
652 next_range = next_range->next;
655 if (prev_range && prev_range->to == from) {
656 /* Merge with previous */
658 } else if (next_range && next_range->from == to) {
659 /* Merge with previous */
660 next_range->from = from;
663 new_range = mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
664 new_range->from = from;
666 new_range->next = NULL;
669 prev_range->next = new_range;
671 interval->range = new_range;
673 new_range->next = next_range;
675 interval->last_range = new_range;
678 /* FIXME: Merge intersecting ranges */
682 mono_linterval_print (MonoLiveInterval *interval)
684 MonoLiveRange2 *range;
686 for (range = interval->range; range != NULL; range = range->next)
687 printf ("[%x-%x] ", range->from, range->to);
691 mono_linterval_print_nl (MonoLiveInterval *interval)
693 mono_linterval_print (interval);
698 * mono_linterval_convers:
700 * Return whenever INTERVAL covers the position POS.
703 mono_linterval_covers (MonoLiveInterval *interval, int pos)
705 MonoLiveRange2 *range;
707 for (range = interval->range; range != NULL; range = range->next) {
708 if (pos >= range->from && pos <= range->to)
710 if (range->from > pos)
718 * mono_linterval_get_intersect_pos:
720 * Determine whenever I1 and I2 intersect, and if they do, return the first
721 * point of intersection. Otherwise, return -1.
724 mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
726 MonoLiveRange2 *r1, *r2;
728 /* FIXME: Optimize this */
729 for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
730 for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
731 if (r2->to > r1->from && r2->from < r1->to) {
732 if (r2->from <= r1->from)
744 * mono_linterval_split
746 * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
750 mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
754 g_assert (pos > interval->range->from && pos <= interval->last_range->to);
756 *i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
757 *i2 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
759 for (r = interval->range; r; r = r->next) {
761 /* Add it to the first child */
762 mono_linterval_add_range (cfg, *i1, r->from, r->to);
763 } else if (pos > r->from && pos <= r->to) {
765 mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
766 mono_linterval_add_range (cfg, *i2, pos, r->to);
768 /* Add it to the second child */
769 mono_linterval_add_range (cfg, *i2, r->from, r->to);
775 #define LIVENESS_DEBUG(a) do { if (cfg->verbose_level > 1) do { a; } while (0); } while (0)
776 #define ENABLE_LIVENESS_DEBUG 1
778 #define LIVENESS_DEBUG(a)
781 #ifdef ENABLE_LIVENESS2
784 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
786 const char *spec = INS_INFO (ins->opcode);
789 int sregs [MONO_MAX_SRC_REGS];
791 LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
793 if (ins->opcode == OP_NOP || ins->opcode == OP_IL_SEQ_POINT)
797 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
798 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
799 int idx = var->inst_c0;
800 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
802 if (MONO_IS_STORE_MEMBASE (ins)) {
803 if (last_use [idx] == 0) {
804 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
805 last_use [idx] = inst_num;
808 if (last_use [idx] > 0) {
809 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
810 mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
814 /* Try dead code elimination */
815 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)) {
816 LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
820 int inst_num_add = 1;
821 MonoInst *next = ins->next;
822 while (next && next->opcode == OP_IL_SEQ_POINT) {
827 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));
828 mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + inst_num_add);
835 num_sregs = mono_inst_get_src_registers (ins, sregs);
836 for (i = 0; i < num_sregs; ++i) {
838 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
839 MonoInst *var = get_vreg_to_inst (cfg, sreg);
840 int idx = var->inst_c0;
842 if (last_use [idx] == 0) {
843 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
844 last_use [idx] = inst_num;
851 mono_analyze_liveness2 (MonoCompile *cfg)
853 int bnum, idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
855 static guint32 disabled = -1;
859 disabled = g_getenv ("DISABLED") != NULL;
864 if (cfg->num_bblocks >= (1 << (32 - BB_ID_SHIFT - 1)))
865 /* Ranges would overflow */
868 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
869 MonoBasicBlock *bb = cfg->bblocks [bnum];
873 for (nins = 0, ins = bb->code; ins; ins = ins->next, ++nins)
876 if (nins >= ((1 << BB_ID_SHIFT) - 1))
877 /* Ranges would overflow */
881 LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
884 if (strstr (cfg->method->name, "test_") != cfg->method->name)
888 max_vars = cfg->num_varinfo;
889 last_use = g_new0 (gint32, max_vars);
892 reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
894 for (idx = 0; idx < max_vars; ++idx) {
895 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
897 vi->interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
901 * Process bblocks in reverse order, so the addition of new live ranges
902 * to the intervals is faster.
904 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
905 MonoBasicBlock *bb = cfg->bblocks [bnum];
908 block_from = (bb->dfn << BB_ID_SHIFT) + 1; /* so pos > 0 */
909 if (bnum < cfg->num_bblocks - 1)
910 /* Beginning of the next bblock */
911 block_to = (cfg->bblocks [bnum + 1]->dfn << BB_ID_SHIFT) + 1;
913 block_to = (bb->dfn << BB_ID_SHIFT) + ((1 << BB_ID_SHIFT) - 1);
915 LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
917 memset (last_use, 0, max_vars * sizeof (gint32));
919 /* For variables in bb->live_out, set last_use to block_to */
921 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
922 for (j = 0; j < max; ++j) {
926 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
927 k = (j * BITS_PER_CHUNK);
930 LIVENESS_DEBUG (printf ("Var R%d live at exit, set last_use to %x\n", cfg->varinfo [k]->dreg, block_to));
931 last_use [k] = block_to;
939 last_use [cfg->ret->inst_c0] = block_to;
941 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
942 if (nins >= reverse_len) {
943 int new_reverse_len = reverse_len * 2;
944 MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
945 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
946 reverse = new_reverse;
947 reverse_len = new_reverse_len;
950 reverse [nins] = ins;
953 /* Process instructions backwards */
954 for (i = nins - 1; i >= 0; --i) {
955 MonoInst *ins = (MonoInst*)reverse [i];
957 update_liveness2 (cfg, ins, FALSE, pos, last_use);
962 for (idx = 0; idx < max_vars; ++idx) {
963 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
965 if (last_use [idx] != 0) {
966 /* Live at exit, not written -> live on enter */
967 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]));
968 mono_linterval_add_range (cfg, vi->interval, block_from, last_use [idx]);
974 * Arguments need to have their live ranges extended to the beginning of
975 * the method to account for the arg reg/memory -> global register copies
976 * in the prolog (bug #74992).
978 for (i = 0; i < max_vars; i ++) {
979 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
980 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
981 mono_linterval_add_range (cfg, vi->interval, 0, 1);
985 for (idx = 0; idx < max_vars; ++idx) {
986 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
988 LIVENESS_DEBUG (printf ("LIVENESS R%d: ", cfg->varinfo [idx]->dreg));
989 LIVENESS_DEBUG (mono_linterval_print (vi->interval));
990 LIVENESS_DEBUG (printf ("\n"));
999 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
1002 update_liveness_gc (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, gint32 *last_use, MonoMethodVar **vreg_to_varinfo, GSList **callsites)
1004 if (ins->opcode == OP_GC_LIVENESS_DEF || ins->opcode == OP_GC_LIVENESS_USE) {
1005 int vreg = ins->inst_c1;
1006 MonoMethodVar *vi = vreg_to_varinfo [vreg];
1008 int pc_offset = ins->backend.pc_offset;
1010 LIVENESS_DEBUG (printf ("\t%x: ", pc_offset); mono_print_ins (ins));
1012 if (ins->opcode == OP_GC_LIVENESS_DEF) {
1013 if (last_use [idx] > 0) {
1014 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", vreg, pc_offset, last_use [idx]));
1018 if (last_use [idx] == 0) {
1019 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", vreg, pc_offset));
1020 last_use [idx] = pc_offset;
1023 } else if (ins->opcode == OP_GC_PARAM_SLOT_LIVENESS_DEF) {
1026 /* Add it to the last callsite */
1027 g_assert (*callsites);
1028 last = (*callsites)->data;
1029 last->param_slots = g_slist_prepend_mempool (cfg->mempool, last->param_slots, ins);
1030 } else if (ins->flags & MONO_INST_GC_CALLSITE) {
1031 GCCallSite *callsite = mono_mempool_alloc0 (cfg->mempool, sizeof (GCCallSite));
1034 LIVENESS_DEBUG (printf ("\t%x: ", ins->backend.pc_offset); mono_print_ins (ins));
1035 LIVENESS_DEBUG (printf ("\t\tlive: "));
1038 callsite->liveness = mono_mempool_alloc0 (cfg->mempool, ALIGN_TO (cfg->num_varinfo, 8) / 8);
1039 callsite->pc_offset = ins->backend.pc_offset;
1040 for (i = 0; i < cfg->num_varinfo; ++i) {
1041 if (last_use [i] != 0) {
1042 LIVENESS_DEBUG (printf ("R%d", MONO_VARINFO (cfg, i)->vreg));
1043 callsite->liveness [i / 8] |= (1 << (i % 8));
1046 LIVENESS_DEBUG (printf ("\n"));
1047 *callsites = g_slist_prepend_mempool (cfg->mempool, *callsites, callsite);
1052 get_vreg_from_var (MonoCompile *cfg, MonoInst *var)
1054 if (var->opcode == OP_REGVAR)
1055 /* dreg contains a hreg, but inst_c0 still contains the var index */
1056 return MONO_VARINFO (cfg, var->inst_c0)->vreg;
1058 /* dreg still contains the vreg */
1063 * mono_analyze_liveness_gc:
1065 * Compute liveness bitmaps for each call site.
1066 * This function is a modified version of mono_analyze_liveness2 ().
1069 mono_analyze_liveness_gc (MonoCompile *cfg)
1071 int idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
1074 MonoMethodVar **vreg_to_varinfo = NULL;
1078 LIVENESS_DEBUG (printf ("\n------------ GC LIVENESS: ----------\n"));
1080 max_vars = cfg->num_varinfo;
1081 last_use = g_new0 (gint32, max_vars);
1084 * var->inst_c0 no longer contains the variable index, so compute a mapping now.
1086 vreg_to_varinfo = g_new0 (MonoMethodVar*, cfg->next_vreg);
1087 for (idx = 0; idx < max_vars; ++idx) {
1088 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
1090 vreg_to_varinfo [vi->vreg] = vi;
1094 reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
1096 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1099 block_from = bb->real_native_offset;
1100 block_to = bb->native_offset + bb->native_length;
1102 LIVENESS_DEBUG (printf ("GC LIVENESS BB%d:\n", bb->block_num));
1107 memset (last_use, 0, max_vars * sizeof (gint32));
1109 /* For variables in bb->live_out, set last_use to block_to */
1111 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
1112 for (j = 0; j < max; ++j) {
1116 if (!bb->live_out_set)
1117 /* The variables used in this bblock are volatile anyway */
1120 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
1121 k = (j * BITS_PER_CHUNK);
1123 if ((bits_out & 1) && cfg->varinfo [k]->flags & MONO_INST_GC_TRACK) {
1124 int vreg = get_vreg_from_var (cfg, cfg->varinfo [k]);
1125 LIVENESS_DEBUG (printf ("Var R%d live at exit, last_use set to %x.\n", vreg, block_to));
1126 last_use [k] = block_to;
1133 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
1134 if (nins >= reverse_len) {
1135 int new_reverse_len = reverse_len * 2;
1136 MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
1137 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
1138 reverse = new_reverse;
1139 reverse_len = new_reverse_len;
1142 reverse [nins] = ins;
1145 /* Process instructions backwards */
1147 for (i = nins - 1; i >= 0; --i) {
1148 MonoInst *ins = (MonoInst*)reverse [i];
1150 update_liveness_gc (cfg, bb, ins, last_use, vreg_to_varinfo, &callsites);
1152 /* The callsites should already be sorted by pc offset because we added them backwards */
1153 bb->gc_callsites = callsites;
1157 g_free (vreg_to_varinfo);
1160 #endif /* DISABLE_JIT */