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.
18 #define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
20 #define DEBUG_LIVENESS
22 #define BITS_PER_CHUNK MONO_BITSET_BITS_PER_CHUNK
24 #define BB_ID_SHIFT 18
27 * The liveness2 pass can't handle long vars on 32 bit platforms because the component
28 * vars have the same 'idx'.
30 #if SIZEOF_REGISTER == 8
31 #define ENABLE_LIVENESS2
34 #ifdef ENABLE_LIVENESS2
35 static void mono_analyze_liveness2 (MonoCompile *cfg);
39 optimize_initlocals (MonoCompile *cfg);
41 /* mono_bitset_mp_new:
43 * allocates a MonoBitSet inside a memory pool
45 static inline MonoBitSet*
46 mono_bitset_mp_new (MonoMemPool *mp, guint32 size, guint32 max_size)
48 guint8 *mem = (guint8 *)mono_mempool_alloc0 (mp, size);
49 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
52 static inline MonoBitSet*
53 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 size, guint32 max_size)
55 guint8 *mem = (guint8 *)mono_mempool_alloc (mp, size);
56 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
59 G_GNUC_UNUSED static void
60 mono_bitset_print (MonoBitSet *set)
63 gboolean first = TRUE;
66 for (i = 0; i < mono_bitset_size (set); i++) {
68 if (mono_bitset_test (set, i)) {
79 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
84 if (g_slist_find (*visited, bb))
87 for (ins = bb->code; ins; ins = ins->next) {
88 const char *spec = INS_INFO (ins->opcode);
89 int regtype, srcindex, sreg, num_sregs;
90 int sregs [MONO_MAX_SRC_REGS];
92 if (ins->opcode == OP_NOP)
96 regtype = spec [MONO_INST_DEST];
97 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
99 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
100 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
101 int idx = var->inst_c0;
102 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
104 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
105 if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
106 /* Make the component vregs volatile as well (#612206) */
107 get_vreg_to_inst (cfg, MONO_LVREG_LS (var->dreg))->flags |= MONO_INST_VOLATILE;
108 get_vreg_to_inst (cfg, MONO_LVREG_MS (var->dreg))->flags |= MONO_INST_VOLATILE;
113 num_sregs = mono_inst_get_src_registers (ins, sregs);
114 for (srcindex = 0; srcindex < num_sregs; ++srcindex) {
115 sreg = sregs [srcindex];
117 g_assert (sreg != -1);
118 if (get_vreg_to_inst (cfg, sreg)) {
119 MonoInst *var = get_vreg_to_inst (cfg, sreg);
120 int idx = var->inst_c0;
121 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
123 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
124 if (SIZEOF_REGISTER == 4 && (var->type == STACK_I8 || (var->type == STACK_R8 && COMPILE_SOFT_FLOAT (cfg)))) {
125 /* Make the component vregs volatile as well (#612206) */
126 get_vreg_to_inst (cfg, MONO_LVREG_LS (var->dreg))->flags |= MONO_INST_VOLATILE;
127 get_vreg_to_inst (cfg, MONO_LVREG_MS (var->dreg))->flags |= MONO_INST_VOLATILE;
133 *visited = g_slist_append (*visited, bb);
136 * Need to visit all bblocks reachable from this one since they can be
137 * reached during exception handling.
139 for (i = 0; i < bb->out_count; ++i) {
140 visit_bb (cfg, bb->out_bb [i], visited);
145 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
148 GSList *visited = NULL;
149 MonoMethodHeader *header = cfg->header;
150 MonoExceptionClause *clause, *clause2;
155 * Determine which clauses are outer try clauses, i.e. they are not contained in any
156 * other non-try clause.
158 outer_try = (gboolean *)mono_mempool_alloc0 (cfg->mempool, sizeof (gboolean) * header->num_clauses);
159 for (i = 0; i < header->num_clauses; ++i)
160 outer_try [i] = TRUE;
161 /* Iterate over the clauses backward, so outer clauses come first */
162 /* This avoids doing an O(2) search, since we can determine when inner clauses end */
163 for (i = header->num_clauses - 1; i >= 0; --i) {
164 clause = &header->clauses [i];
166 if (clause->flags != 0) {
167 outer_try [i] = TRUE;
168 /* Iterate over inner clauses */
169 for (j = i - 1; j >= 0; --j) {
170 clause2 = &header->clauses [j];
172 if (clause2->flags == 0 && MONO_OFFSET_IN_HANDLER (clause, clause2->try_offset)) {
173 outer_try [j] = FALSE;
176 if (clause2->try_offset < clause->try_offset)
177 /* End of inner clauses */
184 * Variables in exception handler register cannot be allocated to registers
185 * so make them volatile. See bug #42136. This will not be neccessary when
186 * the back ends could guarantee that the variables will be in the
187 * correct registers when a handler is called.
188 * This includes try blocks too, since a variable in a try block might be
189 * accessed after an exception handler has been run.
191 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
193 if (bb->region == -1)
196 if (MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY) && outer_try [MONO_REGION_CLAUSE_INDEX (bb->region)])
199 if (cfg->verbose_level > 2)
200 printf ("pessimize variables in bb %d.\n", bb->block_num);
202 visit_bb (cfg, bb, &visited);
204 g_slist_free (visited);
208 update_live_range (MonoMethodVar *var, int abs_pos)
210 if (var->range.first_use.abs_pos > abs_pos)
211 var->range.first_use.abs_pos = abs_pos;
213 if (var->range.last_use.abs_pos < abs_pos)
214 var->range.last_use.abs_pos = abs_pos;
218 analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
222 MonoMethodVar *vars = cfg->vars;
223 guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
225 /* Start inst_num from > 0, so last_use.abs_pos is only 0 for dead variables */
226 for (inst_num = 2, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
227 const char *spec = INS_INFO (ins->opcode);
229 int sregs [MONO_MAX_SRC_REGS];
231 #ifdef DEBUG_LIVENESS
232 if (cfg->verbose_level > 1) {
234 mono_print_ins (ins);
238 if (ins->opcode == OP_NOP)
241 if (ins->opcode == OP_LDADDR) {
242 MonoInst *var = (MonoInst *)ins->inst_p0;
243 int idx = var->inst_c0;
244 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
246 #ifdef DEBUG_LIVENESS
247 if (cfg->verbose_level > 1)
248 printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
250 update_live_range (&vars [idx], abs_pos + inst_num);
251 if (!mono_bitset_test_fast (bb->kill_set, idx))
252 mono_bitset_set_fast (bb->gen_set, idx);
253 vi->spill_costs += SPILL_COST_INCREMENT;
256 /* SREGs must come first, so MOVE r <- r is handled correctly */
257 num_sregs = mono_inst_get_src_registers (ins, sregs);
258 for (i = 0; i < num_sregs; ++i) {
260 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
261 MonoInst *var = get_vreg_to_inst (cfg, sreg);
262 int idx = var->inst_c0;
263 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
265 #ifdef DEBUG_LIVENESS
266 if (cfg->verbose_level > 1)
267 printf ("\tGEN: R%d(%d)\n", sreg, idx);
269 update_live_range (&vars [idx], abs_pos + inst_num);
270 if (!mono_bitset_test_fast (bb->kill_set, idx))
271 mono_bitset_set_fast (bb->gen_set, idx);
272 vi->spill_costs += SPILL_COST_INCREMENT;
277 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
278 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
279 int idx = var->inst_c0;
280 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
282 if (MONO_IS_STORE_MEMBASE (ins)) {
283 update_live_range (&vars [idx], abs_pos + inst_num);
284 if (!mono_bitset_test_fast (bb->kill_set, idx))
285 mono_bitset_set_fast (bb->gen_set, idx);
286 vi->spill_costs += SPILL_COST_INCREMENT;
288 #ifdef DEBUG_LIVENESS
289 if (cfg->verbose_level > 1)
290 printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
292 update_live_range (&vars [idx], abs_pos + inst_num + 1);
293 mono_bitset_set_fast (bb->kill_set, idx);
294 vi->spill_costs += SPILL_COST_INCREMENT;
300 /* generic liveness analysis code. CFG specific parts are
301 * in update_gen_kill_set()
304 mono_analyze_liveness (MonoCompile *cfg)
306 MonoBitSet *old_live_out_set;
307 int i, j, max_vars = cfg->num_varinfo;
309 gboolean *in_worklist;
310 MonoBasicBlock **worklist;
314 #ifdef DEBUG_LIVENESS
315 if (cfg->verbose_level > 1)
316 printf ("\nLIVENESS:\n");
319 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
321 cfg->comp_done |= MONO_COMP_LIVENESS;
326 bitsize = mono_bitset_alloc_size (max_vars, 0);
328 for (i = 0; i < max_vars; i ++) {
329 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
330 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
331 MONO_VARINFO (cfg, i)->spill_costs = 0;
334 for (i = 0; i < cfg->num_bblocks; ++i) {
335 MonoBasicBlock *bb = cfg->bblocks [i];
337 bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
338 bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
340 #ifdef DEBUG_LIVENESS
341 if (cfg->verbose_level > 1) {
342 printf ("BLOCK BB%d (", bb->block_num);
343 for (j = 0; j < bb->out_count; j++)
344 printf ("BB%d, ", bb->out_bb [j]->block_num);
350 analyze_liveness_bb (cfg, bb);
352 #ifdef DEBUG_LIVENESS
353 if (cfg->verbose_level > 1) {
354 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
355 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
360 old_live_out_set = mono_bitset_new (max_vars, 0);
361 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
363 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
367 * This is a backward dataflow analysis problem, so we process blocks in
368 * decreasing dfn order, this speeds up the iteration.
370 for (i = 0; i < cfg->num_bblocks; i ++) {
371 MonoBasicBlock *bb = cfg->bblocks [i];
373 worklist [l_end ++] = bb;
374 in_worklist [bb->dfn] = TRUE;
376 /* Initialized later */
377 bb->live_in_set = NULL;
378 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
383 if (cfg->verbose_level > 1)
384 printf ("\nITERATION:\n");
387 MonoBasicBlock *bb = worklist [--l_end];
388 MonoBasicBlock *out_bb;
391 in_worklist [bb->dfn] = FALSE;
393 #ifdef DEBUG_LIVENESS
394 if (cfg->verbose_level > 1) {
395 printf ("P: BB%d(%d): IN: ", bb->block_num, bb->dfn);
396 for (j = 0; j < bb->in_count; ++j)
397 printf ("BB%d ", bb->in_bb [j]->block_num);
399 for (j = 0; j < bb->out_count; ++j)
400 printf ("BB%d ", bb->out_bb [j]->block_num);
406 if (bb->out_count == 0)
411 if (!bb->live_in_set) {
412 /* First pass over this bblock */
417 mono_bitset_copyto_fast (bb->live_out_set, old_live_out_set);
420 for (j = 0; j < bb->out_count; j++) {
421 out_bb = bb->out_bb [j];
423 if (!out_bb->live_in_set) {
424 out_bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
426 mono_bitset_copyto_fast (out_bb->live_out_set, out_bb->live_in_set);
427 mono_bitset_sub_fast (out_bb->live_in_set, out_bb->kill_set);
428 mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
431 // FIXME: Do this somewhere else
432 if (bb->last_ins && bb->last_ins->opcode == OP_NOT_REACHED) {
434 mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
438 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
439 if (!bb->live_in_set)
440 bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
441 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
442 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
443 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
445 for (j = 0; j < bb->in_count; j++) {
446 MonoBasicBlock *in_bb = bb->in_bb [j];
448 * Some basic blocks do not seem to be in the
449 * cfg->bblocks array...
451 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
452 #ifdef DEBUG_LIVENESS
453 if (cfg->verbose_level > 1)
454 printf ("\tADD: %d\n", in_bb->block_num);
457 * Put the block at the top of the stack, so it
458 * will be processed right away.
460 worklist [l_end ++] = in_bb;
461 in_worklist [in_bb->dfn] = TRUE;
466 if (G_UNLIKELY (cfg->verbose_level > 1)) {
467 printf ("\tLIVE IN BB%d: ", bb->block_num);
468 mono_bitset_print (bb->live_in_set);
472 #ifdef DEBUG_LIVENESS
473 if (cfg->verbose_level > 1)
474 printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
477 mono_bitset_free (old_live_out_set);
480 g_free (in_worklist);
482 /* Compute live_in_set for bblocks skipped earlier */
483 for (i = 0; i < cfg->num_bblocks; ++i) {
484 MonoBasicBlock *bb = cfg->bblocks [i];
486 if (!bb->live_in_set) {
487 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
489 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
490 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
491 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
495 for (i = 0; i < cfg->num_bblocks; ++i) {
496 MonoBasicBlock *bb = cfg->bblocks [i];
498 guint32 abs_pos = (bb->dfn << BB_ID_SHIFT);
499 MonoMethodVar *vars = cfg->vars;
501 if (!bb->live_out_set)
504 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
505 for (j = 0; j < max; ++j) {
510 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
511 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
513 k = (j * BITS_PER_CHUNK);
514 while ((bits_in || bits_out)) {
516 update_live_range (&vars [k], abs_pos + 0);
518 update_live_range (&vars [k], abs_pos + ((1 << BB_ID_SHIFT) - 1));
527 * Arguments need to have their live ranges extended to the beginning of
528 * the method to account for the arg reg/memory -> global register copies
529 * in the prolog (bug #74992).
532 for (i = 0; i < max_vars; i ++) {
533 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
534 if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
535 if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
537 * Can't eliminate the this variable in gshared code, since
538 * it is needed during exception handling to identify the
540 * It is better to check for this here instead of marking the variable
541 * VOLATILE, since that would prevent it from being allocated to
544 if (!cfg->disable_deadce_vars && !(cfg->gshared && mono_method_signature (cfg->method)->hasthis && cfg->varinfo [vi->idx] == cfg->args [0]))
545 cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
547 vi->range.first_use.abs_pos = 0;
551 #ifdef DEBUG_LIVENESS
552 if (cfg->verbose_level > 1) {
553 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
554 MonoBasicBlock *bb = cfg->bblocks [i];
556 printf ("LIVE IN BB%d: ", bb->block_num);
557 mono_bitset_print (bb->live_in_set);
558 printf ("LIVE OUT BB%d: ", bb->block_num);
559 mono_bitset_print (bb->live_out_set);
562 for (i = 0; i < max_vars; i ++) {
563 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
565 printf ("V%d: [0x%x - 0x%x]\n", i, vi->range.first_use.abs_pos, vi->range.last_use.abs_pos);
570 if (!cfg->disable_initlocals_opt)
571 optimize_initlocals (cfg);
573 #ifdef ENABLE_LIVENESS2
574 /* This improves code size by about 5% but slows down compilation too much */
575 if (cfg->compile_aot)
576 mono_analyze_liveness2 (cfg);
581 * optimize_initlocals:
583 * Try to optimize away some of the redundant initialization code inserted because of
584 * 'locals init' using the liveness information.
587 optimize_initlocals (MonoCompile *cfg)
591 MonoBasicBlock *initlocals_bb;
593 used = mono_bitset_new (cfg->next_vreg + 1, 0);
595 mono_bitset_clear_all (used);
596 initlocals_bb = cfg->bb_entry->next_bb;
597 for (ins = initlocals_bb->code; ins; ins = ins->next) {
599 int sregs [MONO_MAX_SRC_REGS];
601 num_sregs = mono_inst_get_src_registers (ins, sregs);
602 for (i = 0; i < num_sregs; ++i)
603 mono_bitset_set_fast (used, sregs [i]);
605 if (MONO_IS_STORE_MEMBASE (ins))
606 mono_bitset_set_fast (used, ins->dreg);
609 for (ins = initlocals_bb->code; ins; ins = ins->next) {
610 const char *spec = INS_INFO (ins->opcode);
612 /* Look for statements whose dest is not used in this bblock and not live on exit. */
613 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
614 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
616 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))) {
617 //printf ("DEAD: "); mono_print_ins (ins);
618 if (cfg->disable_initlocals_opt_refs && var->type == STACK_OBJ)
620 if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST) || (ins->opcode == OP_R4CONST)) {
622 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
624 * We should shorten the liveness interval of these vars as well, but
625 * don't have enough info to do that.
636 mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
638 MonoLiveRange2 *prev_range, *next_range, *new_range;
640 g_assert (to >= from);
642 /* Optimize for extending the first interval backwards */
643 if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
644 interval->range->from = from;
648 /* Find a place in the list for the new range */
650 next_range = interval->range;
651 while ((next_range != NULL) && (next_range->from <= from)) {
652 prev_range = next_range;
653 next_range = next_range->next;
656 if (prev_range && prev_range->to == from) {
657 /* Merge with previous */
659 } else if (next_range && next_range->from == to) {
660 /* Merge with previous */
661 next_range->from = from;
664 new_range = (MonoLiveRange2 *)mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
665 new_range->from = from;
667 new_range->next = NULL;
670 prev_range->next = new_range;
672 interval->range = new_range;
674 new_range->next = next_range;
676 interval->last_range = new_range;
679 /* FIXME: Merge intersecting ranges */
683 mono_linterval_print (MonoLiveInterval *interval)
685 MonoLiveRange2 *range;
687 for (range = interval->range; range != NULL; range = range->next)
688 printf ("[%x-%x] ", range->from, range->to);
692 mono_linterval_print_nl (MonoLiveInterval *interval)
694 mono_linterval_print (interval);
699 * mono_linterval_convers:
701 * Return whenever INTERVAL covers the position POS.
704 mono_linterval_covers (MonoLiveInterval *interval, int pos)
706 MonoLiveRange2 *range;
708 for (range = interval->range; range != NULL; range = range->next) {
709 if (pos >= range->from && pos <= range->to)
711 if (range->from > pos)
719 * mono_linterval_get_intersect_pos:
721 * Determine whenever I1 and I2 intersect, and if they do, return the first
722 * point of intersection. Otherwise, return -1.
725 mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
727 MonoLiveRange2 *r1, *r2;
729 /* FIXME: Optimize this */
730 for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
731 for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
732 if (r2->to > r1->from && r2->from < r1->to) {
733 if (r2->from <= r1->from)
745 * mono_linterval_split
747 * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
751 mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
755 g_assert (pos > interval->range->from && pos <= interval->last_range->to);
757 *i1 = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
758 *i2 = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
760 for (r = interval->range; r; r = r->next) {
762 /* Add it to the first child */
763 mono_linterval_add_range (cfg, *i1, r->from, r->to);
764 } else if (pos > r->from && pos <= r->to) {
766 mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
767 mono_linterval_add_range (cfg, *i2, pos, r->to);
769 /* Add it to the second child */
770 mono_linterval_add_range (cfg, *i2, r->from, r->to);
776 #define LIVENESS_DEBUG(a) do { if (cfg->verbose_level > 1) do { a; } while (0); } while (0)
777 #define ENABLE_LIVENESS_DEBUG 1
779 #define LIVENESS_DEBUG(a)
782 #ifdef ENABLE_LIVENESS2
785 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
787 const char *spec = INS_INFO (ins->opcode);
790 int sregs [MONO_MAX_SRC_REGS];
792 LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
794 if (ins->opcode == OP_NOP || ins->opcode == OP_IL_SEQ_POINT)
798 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
799 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
800 int idx = var->inst_c0;
801 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
803 if (MONO_IS_STORE_MEMBASE (ins)) {
804 if (last_use [idx] == 0) {
805 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
806 last_use [idx] = inst_num;
809 if (last_use [idx] > 0) {
810 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
811 mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
815 /* Try dead code elimination */
816 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)) {
817 LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
821 int inst_num_add = 1;
822 MonoInst *next = ins->next;
823 while (next && next->opcode == OP_IL_SEQ_POINT) {
828 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));
829 mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + inst_num_add);
836 num_sregs = mono_inst_get_src_registers (ins, sregs);
837 for (i = 0; i < num_sregs; ++i) {
839 if ((spec [MONO_INST_SRC1 + i] != ' ') && get_vreg_to_inst (cfg, sreg)) {
840 MonoInst *var = get_vreg_to_inst (cfg, sreg);
841 int idx = var->inst_c0;
843 if (last_use [idx] == 0) {
844 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
845 last_use [idx] = inst_num;
852 mono_analyze_liveness2 (MonoCompile *cfg)
854 int bnum, idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
856 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);
893 reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
895 for (idx = 0; idx < max_vars; ++idx) {
896 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
898 vi->interval = (MonoLiveInterval *)mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
902 * Process bblocks in reverse order, so the addition of new live ranges
903 * to the intervals is faster.
905 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
906 MonoBasicBlock *bb = cfg->bblocks [bnum];
909 block_from = (bb->dfn << BB_ID_SHIFT) + 1; /* so pos > 0 */
910 if (bnum < cfg->num_bblocks - 1)
911 /* Beginning of the next bblock */
912 block_to = (cfg->bblocks [bnum + 1]->dfn << BB_ID_SHIFT) + 1;
914 block_to = (bb->dfn << BB_ID_SHIFT) + ((1 << BB_ID_SHIFT) - 1);
916 LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
918 memset (last_use, 0, max_vars * sizeof (gint32));
920 /* For variables in bb->live_out, set last_use to block_to */
922 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
923 for (j = 0; j < max; ++j) {
927 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
928 k = (j * BITS_PER_CHUNK);
931 LIVENESS_DEBUG (printf ("Var R%d live at exit, set last_use to %x\n", cfg->varinfo [k]->dreg, block_to));
932 last_use [k] = block_to;
940 last_use [cfg->ret->inst_c0] = block_to;
942 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
943 if (nins >= reverse_len) {
944 int new_reverse_len = reverse_len * 2;
945 MonoInst **new_reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
946 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
947 reverse = new_reverse;
948 reverse_len = new_reverse_len;
951 reverse [nins] = ins;
954 /* Process instructions backwards */
955 for (i = nins - 1; i >= 0; --i) {
956 MonoInst *ins = (MonoInst*)reverse [i];
958 update_liveness2 (cfg, ins, FALSE, pos, last_use);
963 for (idx = 0; idx < max_vars; ++idx) {
964 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
966 if (last_use [idx] != 0) {
967 /* Live at exit, not written -> live on enter */
968 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]));
969 mono_linterval_add_range (cfg, vi->interval, block_from, last_use [idx]);
975 * Arguments need to have their live ranges extended to the beginning of
976 * the method to account for the arg reg/memory -> global register copies
977 * in the prolog (bug #74992).
979 for (i = 0; i < max_vars; i ++) {
980 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
981 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
982 mono_linterval_add_range (cfg, vi->interval, 0, 1);
986 for (idx = 0; idx < max_vars; ++idx) {
987 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
989 LIVENESS_DEBUG (printf ("LIVENESS R%d: ", cfg->varinfo [idx]->dreg));
990 LIVENESS_DEBUG (mono_linterval_print (vi->interval));
991 LIVENESS_DEBUG (printf ("\n"));
1000 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
1003 update_liveness_gc (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, gint32 *last_use, MonoMethodVar **vreg_to_varinfo, GSList **callsites)
1005 if (ins->opcode == OP_GC_LIVENESS_DEF || ins->opcode == OP_GC_LIVENESS_USE) {
1006 int vreg = ins->inst_c1;
1007 MonoMethodVar *vi = vreg_to_varinfo [vreg];
1009 int pc_offset = ins->backend.pc_offset;
1011 LIVENESS_DEBUG (printf ("\t%x: ", pc_offset); mono_print_ins (ins));
1013 if (ins->opcode == OP_GC_LIVENESS_DEF) {
1014 if (last_use [idx] > 0) {
1015 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", vreg, pc_offset, last_use [idx]));
1019 if (last_use [idx] == 0) {
1020 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", vreg, pc_offset));
1021 last_use [idx] = pc_offset;
1024 } else if (ins->opcode == OP_GC_PARAM_SLOT_LIVENESS_DEF) {
1027 /* Add it to the last callsite */
1028 g_assert (*callsites);
1029 last = (GCCallSite *)(*callsites)->data;
1030 last->param_slots = g_slist_prepend_mempool (cfg->mempool, last->param_slots, ins);
1031 } else if (ins->flags & MONO_INST_GC_CALLSITE) {
1032 GCCallSite *callsite = (GCCallSite *)mono_mempool_alloc0 (cfg->mempool, sizeof (GCCallSite));
1035 LIVENESS_DEBUG (printf ("\t%x: ", ins->backend.pc_offset); mono_print_ins (ins));
1036 LIVENESS_DEBUG (printf ("\t\tlive: "));
1039 callsite->liveness = (guint8 *)mono_mempool_alloc0 (cfg->mempool, ALIGN_TO (cfg->num_varinfo, 8) / 8);
1040 callsite->pc_offset = ins->backend.pc_offset;
1041 for (i = 0; i < cfg->num_varinfo; ++i) {
1042 if (last_use [i] != 0) {
1043 LIVENESS_DEBUG (printf ("R%d", MONO_VARINFO (cfg, i)->vreg));
1044 callsite->liveness [i / 8] |= (1 << (i % 8));
1047 LIVENESS_DEBUG (printf ("\n"));
1048 *callsites = g_slist_prepend_mempool (cfg->mempool, *callsites, callsite);
1053 get_vreg_from_var (MonoCompile *cfg, MonoInst *var)
1055 if (var->opcode == OP_REGVAR)
1056 /* dreg contains a hreg, but inst_c0 still contains the var index */
1057 return MONO_VARINFO (cfg, var->inst_c0)->vreg;
1059 /* dreg still contains the vreg */
1064 * mono_analyze_liveness_gc:
1066 * Compute liveness bitmaps for each call site.
1067 * This function is a modified version of mono_analyze_liveness2 ().
1070 mono_analyze_liveness_gc (MonoCompile *cfg)
1072 int idx, i, j, nins, max, max_vars, block_from, block_to, pos, reverse_len;
1075 MonoMethodVar **vreg_to_varinfo = NULL;
1079 LIVENESS_DEBUG (printf ("\n------------ GC LIVENESS: ----------\n"));
1081 max_vars = cfg->num_varinfo;
1082 last_use = g_new0 (gint32, max_vars);
1085 * var->inst_c0 no longer contains the variable index, so compute a mapping now.
1087 vreg_to_varinfo = g_new0 (MonoMethodVar*, cfg->next_vreg);
1088 for (idx = 0; idx < max_vars; ++idx) {
1089 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
1091 vreg_to_varinfo [vi->vreg] = vi;
1095 reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
1097 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
1100 block_from = bb->real_native_offset;
1101 block_to = bb->native_offset + bb->native_length;
1103 LIVENESS_DEBUG (printf ("GC LIVENESS BB%d:\n", bb->block_num));
1108 memset (last_use, 0, max_vars * sizeof (gint32));
1110 /* For variables in bb->live_out, set last_use to block_to */
1112 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
1113 for (j = 0; j < max; ++j) {
1117 if (!bb->live_out_set)
1118 /* The variables used in this bblock are volatile anyway */
1121 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
1122 k = (j * BITS_PER_CHUNK);
1124 if ((bits_out & 1) && cfg->varinfo [k]->flags & MONO_INST_GC_TRACK) {
1125 int vreg = get_vreg_from_var (cfg, cfg->varinfo [k]);
1126 LIVENESS_DEBUG (printf ("Var R%d live at exit, last_use set to %x.\n", vreg, block_to));
1127 last_use [k] = block_to;
1134 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
1135 if (nins >= reverse_len) {
1136 int new_reverse_len = reverse_len * 2;
1137 MonoInst **new_reverse = (MonoInst **)mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
1138 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
1139 reverse = new_reverse;
1140 reverse_len = new_reverse_len;
1143 reverse [nins] = ins;
1146 /* Process instructions backwards */
1148 for (i = nins - 1; i >= 0; --i) {
1149 MonoInst *ins = (MonoInst*)reverse [i];
1151 update_liveness_gc (cfg, bb, ins, last_use, vreg_to_varinfo, &callsites);
1153 /* The callsites should already be sorted by pc offset because we added them backwards */
1154 bb->gc_callsites = callsites;
1158 g_free (vreg_to_varinfo);
1161 #endif /* DISABLE_JIT */