2 * liveness.c: liveness analysis
5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2002 Ximian, Inc.
14 #define SPILL_COST_INCREMENT (1 << (bb->nesting << 1))
16 //#define DEBUG_LIVENESS
18 #if SIZEOF_VOID_P == 8
19 #define BITS_PER_CHUNK 64
21 #define BITS_PER_CHUNK 32
24 static void mono_analyze_liveness2 (MonoCompile *cfg);
27 optimize_initlocals (MonoCompile *cfg);
30 optimize_initlocals2 (MonoCompile *cfg);
32 /* mono_bitset_mp_new:
34 * allocates a MonoBitSet inside a memory pool
36 static inline MonoBitSet*
37 mono_bitset_mp_new (MonoMemPool *mp, guint32 size, guint32 max_size)
39 guint8 *mem = mono_mempool_alloc0 (mp, size);
40 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
43 static inline MonoBitSet*
44 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 size, guint32 max_size)
46 guint8 *mem = mono_mempool_alloc (mp, size);
47 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
50 G_GNUC_UNUSED static void
51 mono_bitset_print (MonoBitSet *set)
56 for (i = 0; i < mono_bitset_size (set); i++) {
58 if (mono_bitset_test (set, i))
66 update_live_range (MonoCompile *cfg, int idx, int block_dfn, int tree_pos)
68 MonoLiveRange *range = &MONO_VARINFO (cfg, idx)->range;
69 guint32 abs_pos = (block_dfn << 16) | tree_pos;
71 if (range->first_use.abs_pos > abs_pos)
72 range->first_use.abs_pos = abs_pos;
74 if (range->last_use.abs_pos < abs_pos)
75 range->last_use.abs_pos = abs_pos;
79 update_gen_kill_set (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, int inst_num)
82 int max_vars = cfg->num_varinfo;
84 arity = mono_burg_arity [inst->opcode];
86 update_gen_kill_set (cfg, bb, inst->inst_i0, inst_num);
89 update_gen_kill_set (cfg, bb, inst->inst_i1, inst_num);
91 if ((inst->ssa_op & MONO_SSA_LOAD_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
92 MonoLocalVariableList* affected_variables;
93 MonoLocalVariableList local_affected_variable;
95 if (cfg->aliasing_info == NULL) {
96 if ((inst->ssa_op == MONO_SSA_LOAD) || (inst->ssa_op == MONO_SSA_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
97 local_affected_variable.variable_index = inst->inst_i0->inst_c0;
98 local_affected_variable.next = NULL;
99 affected_variables = &local_affected_variable;
101 affected_variables = NULL;
104 affected_variables = mono_aliasing_get_affected_variables_for_inst_traversing_code (cfg->aliasing_info, inst);
107 if (inst->ssa_op & MONO_SSA_LOAD) {
108 MonoLocalVariableList* affected_variable = affected_variables;
109 while (affected_variable != NULL) {
110 int idx = affected_variable->variable_index;
111 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
112 g_assert (idx < max_vars);
113 update_live_range (cfg, idx, bb->dfn, inst_num);
114 if (!mono_bitset_test_fast (bb->kill_set, idx))
115 mono_bitset_set_fast (bb->gen_set, idx);
116 if (inst->ssa_op == MONO_SSA_LOAD)
117 vi->spill_costs += SPILL_COST_INCREMENT;
119 affected_variable = affected_variable->next;
121 } else if ((inst->ssa_op == MONO_SSA_STORE) || (inst->opcode == OP_DUMMY_STORE)) {
122 MonoLocalVariableList* affected_variable = affected_variables;
123 while (affected_variable != NULL) {
124 int idx = affected_variable->variable_index;
125 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
126 g_assert (idx < max_vars);
128 //g_assert (inst->inst_i1->opcode != OP_PHI);
129 update_live_range (cfg, idx, bb->dfn, inst_num);
130 mono_bitset_set_fast (bb->kill_set, idx);
131 if (inst->ssa_op == MONO_SSA_STORE)
132 vi->spill_costs += SPILL_COST_INCREMENT;
134 affected_variable = affected_variable->next;
137 } else if (inst->opcode == OP_JMP) {
138 /* Keep arguments live! */
140 for (i = 0; i < cfg->num_varinfo; i++) {
141 if (cfg->varinfo [i]->opcode == OP_ARG) {
142 if (!mono_bitset_test_fast (bb->kill_set, i))
143 mono_bitset_set_fast (bb->gen_set, i);
150 update_volatile (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst)
152 int arity = mono_burg_arity [inst->opcode];
153 int max_vars = cfg->num_varinfo;
156 update_volatile (cfg, bb, inst->inst_i0);
159 update_volatile (cfg, bb, inst->inst_i1);
161 if (inst->ssa_op & MONO_SSA_LOAD_STORE) {
162 MonoLocalVariableList* affected_variables;
163 MonoLocalVariableList local_affected_variable;
165 if (cfg->aliasing_info == NULL) {
166 if ((inst->ssa_op == MONO_SSA_LOAD) || (inst->ssa_op == MONO_SSA_STORE)) {
167 local_affected_variable.variable_index = inst->inst_i0->inst_c0;
168 local_affected_variable.next = NULL;
169 affected_variables = &local_affected_variable;
171 affected_variables = NULL;
174 affected_variables = mono_aliasing_get_affected_variables_for_inst_traversing_code (cfg->aliasing_info, inst);
177 while (affected_variables != NULL) {
178 int idx = affected_variables->variable_index;
179 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
180 g_assert (idx < max_vars);
181 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
183 affected_variables = affected_variables->next;
189 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
194 if (g_slist_find (*visited, bb))
198 for (ins = bb->code; ins; ins = ins->next) {
199 const char *spec = INS_INFO (ins->opcode);
200 int regtype, srcindex, sreg;
202 if (ins->opcode == OP_NOP)
206 regtype = spec [MONO_INST_DEST];
207 g_assert (((ins->dreg == -1) && (regtype == ' ')) || ((ins->dreg != -1) && (regtype != ' ')));
209 if ((ins->dreg != -1) && get_vreg_to_inst (cfg, ins->dreg)) {
210 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
211 int idx = var->inst_c0;
212 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
214 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
218 for (srcindex = 0; srcindex < 2; ++srcindex) {
219 regtype = spec [(srcindex == 0) ? MONO_INST_SRC1 : MONO_INST_SRC2];
220 sreg = srcindex == 0 ? ins->sreg1 : ins->sreg2;
222 g_assert (((sreg == -1) && (regtype == ' ')) || ((sreg != -1) && (regtype != ' ')));
223 if ((sreg != -1) && get_vreg_to_inst (cfg, sreg)) {
224 MonoInst *var = get_vreg_to_inst (cfg, sreg);
225 int idx = var->inst_c0;
226 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
228 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
233 if (cfg->aliasing_info != NULL)
234 mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
236 for (ins = bb->code; ins; ins = ins->next) {
237 update_volatile (cfg, bb, ins);
241 *visited = g_slist_append (*visited, bb);
244 * Need to visit all bblocks reachable from this one since they can be
245 * reached during exception handling.
247 for (i = 0; i < bb->out_count; ++i) {
248 visit_bb (cfg, bb->out_bb [i], visited);
253 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
256 GSList *visited = NULL;
259 * Variables in exception handler register cannot be allocated to registers
260 * so make them volatile. See bug #42136. This will not be neccessary when
261 * the back ends could guarantee that the variables will be in the
262 * correct registers when a handler is called.
263 * This includes try blocks too, since a variable in a try block might be
264 * accessed after an exception handler has been run.
266 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
268 if (bb->region == -1 || MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
271 visit_bb (cfg, bb, &visited);
273 g_slist_free (visited);
277 update_live_range2 (MonoMethodVar *var, int abs_pos)
279 if (var->range.first_use.abs_pos > abs_pos)
280 var->range.first_use.abs_pos = abs_pos;
282 if (var->range.last_use.abs_pos < abs_pos)
283 var->range.last_use.abs_pos = abs_pos;
287 analyze_liveness_bb (MonoCompile *cfg, MonoBasicBlock *bb)
291 MonoMethodVar *vars = cfg->vars;
292 guint32 abs_pos = (bb->dfn << 16);
294 for (inst_num = 0, ins = bb->code; ins; ins = ins->next, inst_num += 2) {
295 const char *spec = INS_INFO (ins->opcode);
297 #ifdef DEBUG_LIVENESS
298 printf ("\t"); mono_print_ins (ins);
301 if (ins->opcode == OP_NOP)
304 if (ins->opcode == OP_LDADDR) {
305 MonoInst *var = ins->inst_p0;
306 int idx = var->inst_c0;
307 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
309 #ifdef DEBUG_LIVENESS
310 printf ("\tGEN: R%d(%d)\n", var->dreg, idx);
312 update_live_range2 (&vars [idx], abs_pos + inst_num);
313 if (!mono_bitset_test_fast (bb->kill_set, idx))
314 mono_bitset_set_fast (bb->gen_set, idx);
315 vi->spill_costs += SPILL_COST_INCREMENT;
318 /* SREGs must come first, so MOVE r <- r is handled correctly */
322 if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
323 MonoInst *var = get_vreg_to_inst (cfg, sreg);
324 int idx = var->inst_c0;
325 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
327 #ifdef DEBUG_LIVENESS
328 printf ("\tGEN: R%d(%d)\n", sreg, idx);
330 update_live_range2 (&vars [idx], abs_pos + inst_num);
331 if (!mono_bitset_test_fast (bb->kill_set, idx))
332 mono_bitset_set_fast (bb->gen_set, idx);
333 vi->spill_costs += SPILL_COST_INCREMENT;
338 if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
339 MonoInst *var = get_vreg_to_inst (cfg, sreg);
340 int idx = var->inst_c0;
341 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
343 #ifdef DEBUG_LIVENESS
344 printf ("\tGEN: R%d(%d)\n", sreg, idx);
346 update_live_range2 (&vars [idx], abs_pos + inst_num);
347 if (!mono_bitset_test_fast (bb->kill_set, idx))
348 mono_bitset_set_fast (bb->gen_set, idx);
349 vi->spill_costs += SPILL_COST_INCREMENT;
353 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
354 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
355 int idx = var->inst_c0;
356 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
358 if (MONO_IS_STORE_MEMBASE (ins)) {
359 update_live_range2 (&vars [idx], abs_pos + inst_num);
360 if (!mono_bitset_test_fast (bb->kill_set, idx))
361 mono_bitset_set_fast (bb->gen_set, idx);
362 vi->spill_costs += SPILL_COST_INCREMENT;
364 #ifdef DEBUG_LIVENESS
365 printf ("\tKILL: R%d(%d)\n", ins->dreg, idx);
367 update_live_range2 (&vars [idx], abs_pos + inst_num + 1);
368 mono_bitset_set_fast (bb->kill_set, idx);
369 vi->spill_costs += SPILL_COST_INCREMENT;
375 /* generic liveness analysis code. CFG specific parts are
376 * in update_gen_kill_set()
379 mono_analyze_liveness (MonoCompile *cfg)
381 MonoBitSet *old_live_out_set;
382 int i, j, max_vars = cfg->num_varinfo;
384 gboolean *in_worklist;
385 MonoBasicBlock **worklist;
389 #ifdef DEBUG_LIVENESS
390 printf ("LIVENESS %s\n", mono_method_full_name (cfg->method, TRUE));
393 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
395 cfg->comp_done |= MONO_COMP_LIVENESS;
400 bitsize = mono_bitset_alloc_size (max_vars, 0);
402 for (i = 0; i < max_vars; i ++) {
403 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
404 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
405 MONO_VARINFO (cfg, i)->spill_costs = 0;
408 for (i = 0; i < cfg->num_bblocks; ++i) {
409 MonoBasicBlock *bb = cfg->bblocks [i];
413 bb->gen_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
414 bb->kill_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
417 analyze_liveness_bb (cfg, bb);
419 if (cfg->aliasing_info != NULL)
420 mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
423 MONO_BB_FOR_EACH_INS (bb, inst) {
424 #ifdef DEBUG_LIVENESS
425 mono_print_tree (inst); printf ("\n");
427 update_gen_kill_set (cfg, bb, inst, tree_num);
432 #ifdef DEBUG_LIVENESS
433 printf ("BLOCK BB%d (", bb->block_num);
434 for (j = 0; j < bb->out_count; j++)
435 printf ("BB%d, ", bb->out_bb [j]->block_num);
438 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
439 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
443 old_live_out_set = mono_bitset_new (max_vars, 0);
444 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
446 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
450 * This is a backward dataflow analysis problem, so we process blocks in
451 * decreasing dfn order, this speeds up the iteration.
453 for (i = 0; i < cfg->num_bblocks; i ++) {
454 MonoBasicBlock *bb = cfg->bblocks [i];
456 worklist [l_end ++] = bb;
457 in_worklist [bb->dfn] = TRUE;
459 /* Initialized later */
460 bb->live_in_set = NULL;
461 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
467 MonoBasicBlock *bb = worklist [--l_end];
468 MonoBasicBlock *out_bb;
471 in_worklist [bb->dfn] = FALSE;
473 #ifdef DEBUG_LIVENESS
474 printf ("P: %d(%d): IN: ", bb->block_num, bb->dfn);
475 for (j = 0; j < bb->in_count; ++j)
476 printf ("BB%d ", bb->in_bb [j]->block_num);
478 for (j = 0; j < bb->out_count; ++j)
479 printf ("BB%d ", bb->out_bb [j]->block_num);
484 if (bb->out_count == 0)
489 if (!bb->live_in_set) {
490 /* First pass over this bblock */
495 mono_bitset_copyto_fast (bb->live_out_set, old_live_out_set);
498 for (j = 0; j < bb->out_count; j++) {
499 out_bb = bb->out_bb [j];
501 if (!out_bb->live_in_set) {
502 out_bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
504 mono_bitset_copyto_fast (out_bb->live_out_set, out_bb->live_in_set);
505 mono_bitset_sub_fast (out_bb->live_in_set, out_bb->kill_set);
506 mono_bitset_union_fast (out_bb->live_in_set, out_bb->gen_set);
509 mono_bitset_union_fast (bb->live_out_set, out_bb->live_in_set);
512 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
513 if (!bb->live_in_set)
514 bb->live_in_set = mono_bitset_mp_new_noinit (cfg->mempool, bitsize, max_vars);
515 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
516 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
517 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
519 for (j = 0; j < bb->in_count; j++) {
520 MonoBasicBlock *in_bb = bb->in_bb [j];
522 * Some basic blocks do not seem to be in the
523 * cfg->bblocks array...
525 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
526 #ifdef DEBUG_LIVENESS
527 printf ("\tADD: %d\n", in_bb->block_num);
530 * Put the block at the top of the stack, so it
531 * will be processed right away.
533 worklist [l_end ++] = in_bb;
534 in_worklist [in_bb->dfn] = TRUE;
540 #ifdef DEBUG_LIVENESS
541 printf ("IT: %d %d.\n", cfg->num_bblocks, out_iter);
544 mono_bitset_free (old_live_out_set);
547 g_free (in_worklist);
549 /* Compute live_in_set for bblocks skipped earlier */
550 for (i = 0; i < cfg->num_bblocks; ++i) {
551 MonoBasicBlock *bb = cfg->bblocks [i];
553 if (!bb->live_in_set) {
554 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, bitsize, max_vars);
556 mono_bitset_copyto_fast (bb->live_out_set, bb->live_in_set);
557 mono_bitset_sub_fast (bb->live_in_set, bb->kill_set);
558 mono_bitset_union_fast (bb->live_in_set, bb->gen_set);
562 for (i = 0; i < cfg->num_bblocks; ++i) {
563 MonoBasicBlock *bb = cfg->bblocks [i];
565 guint32 abs_pos = (bb->dfn << 16);
566 MonoMethodVar *vars = cfg->vars;
568 if (!bb->live_out_set)
571 rem = max_vars % BITS_PER_CHUNK;
572 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
573 for (j = 0; j < max; ++j) {
578 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
579 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
581 k = (j * BITS_PER_CHUNK);
582 while ((bits_in || bits_out)) {
584 update_live_range2 (&vars [k], abs_pos + 0);
586 update_live_range2 (&vars [k], abs_pos + 0xffff);
595 * Arguments need to have their live ranges extended to the beginning of
596 * the method to account for the arg reg/memory -> global register copies
597 * in the prolog (bug #74992).
600 for (i = 0; i < max_vars; i ++) {
601 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
602 if (cfg->varinfo [vi->idx]->opcode == OP_ARG) {
603 if (vi->range.last_use.abs_pos == 0 && !(cfg->varinfo [vi->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
604 cfg->varinfo [vi->idx]->flags |= MONO_INST_IS_DEAD;
605 vi->range.first_use.abs_pos = 0;
609 #ifdef DEBUG_LIVENESS
610 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
611 MonoBasicBlock *bb = cfg->bblocks [i];
613 printf ("LIVE IN BB%d: ", bb->block_num);
614 mono_bitset_print (bb->live_in_set);
615 printf ("LIVE OUT BB%d: ", bb->block_num);
616 mono_bitset_print (bb->live_out_set);
621 if (!cfg->disable_initlocals_opt)
622 optimize_initlocals2 (cfg);
624 /* This improves code size by about 5% but slows down compilation too much */
625 if (cfg->compile_aot)
626 mono_analyze_liveness2 (cfg);
629 if (!cfg->disable_initlocals_opt)
630 optimize_initlocals (cfg);
635 * optimize_initlocals:
637 * Try to optimize away some of the redundant initialization code inserted because of
638 * 'locals init' using the liveness information.
641 optimize_initlocals2 (MonoCompile *cfg)
645 MonoBasicBlock *initlocals_bb;
647 used = mono_bitset_new (cfg->next_vreg + 1, 0);
649 mono_bitset_clear_all (used);
650 initlocals_bb = cfg->bb_entry->next_bb;
651 for (ins = initlocals_bb->code; ins; ins = ins->next) {
652 const char *spec = INS_INFO (ins->opcode);
654 if (spec [MONO_INST_SRC1] != ' ')
655 mono_bitset_set_fast (used, ins->sreg1);
656 if (spec [MONO_INST_SRC2] != ' ')
657 mono_bitset_set_fast (used, ins->sreg2);
658 if (MONO_IS_STORE_MEMBASE (ins))
659 mono_bitset_set_fast (used, ins->dreg);
662 for (ins = initlocals_bb->code; ins; ins = ins->next) {
663 const char *spec = INS_INFO (ins->opcode);
665 /* Look for statements whose dest is not used in this bblock and not live on exit. */
666 if ((spec [MONO_INST_DEST] != ' ') && !MONO_IS_STORE_MEMBASE (ins)) {
667 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
669 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))) {
670 //printf ("DEAD: "); mono_print_ins (ins);
671 if ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST)) {
673 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;
675 * We should shorten the liveness interval of these vars as well, but
676 * don't have enough info to do that.
687 mono_linterval_add_range (MonoCompile *cfg, MonoLiveInterval *interval, int from, int to)
689 MonoLiveRange2 *prev_range, *next_range, *new_range;
691 g_assert (to >= from);
693 /* Optimize for extending the first interval backwards */
694 if (G_LIKELY (interval->range && (interval->range->from > from) && (interval->range->from == to))) {
695 interval->range->from = from;
699 /* Find a place in the list for the new range */
701 next_range = interval->range;
702 while ((next_range != NULL) && (next_range->from <= from)) {
703 prev_range = next_range;
704 next_range = next_range->next;
707 if (prev_range && prev_range->to == from) {
708 /* Merge with previous */
710 } else if (next_range && next_range->from == to) {
711 /* Merge with previous */
712 next_range->from = from;
715 new_range = mono_mempool_alloc (cfg->mempool, sizeof (MonoLiveRange2));
716 new_range->from = from;
718 new_range->next = NULL;
721 prev_range->next = new_range;
723 interval->range = new_range;
725 new_range->next = next_range;
727 interval->last_range = new_range;
730 /* FIXME: Merge intersecting ranges */
734 mono_linterval_print (MonoLiveInterval *interval)
736 MonoLiveRange2 *range;
738 for (range = interval->range; range != NULL; range = range->next)
739 printf ("[%x-%x] ", range->from, range->to);
743 mono_linterval_print_nl (MonoLiveInterval *interval)
745 mono_linterval_print (interval);
750 * mono_linterval_convers:
752 * Return whenever INTERVAL covers the position POS.
755 mono_linterval_covers (MonoLiveInterval *interval, int pos)
757 MonoLiveRange2 *range;
759 for (range = interval->range; range != NULL; range = range->next) {
760 if (pos >= range->from && pos <= range->to)
762 if (range->from > pos)
770 * mono_linterval_get_intersect_pos:
772 * Determine whenever I1 and I2 intersect, and if they do, return the first
773 * point of intersection. Otherwise, return -1.
776 mono_linterval_get_intersect_pos (MonoLiveInterval *i1, MonoLiveInterval *i2)
778 MonoLiveRange2 *r1, *r2;
780 /* FIXME: Optimize this */
781 for (r1 = i1->range; r1 != NULL; r1 = r1->next) {
782 for (r2 = i2->range; r2 != NULL; r2 = r2->next) {
783 if (r2->to > r1->from && r2->from < r1->to) {
784 if (r2->from <= r1->from)
796 * mono_linterval_split
798 * Split L at POS and store the newly created intervals into L1 and L2. POS becomes
802 mono_linterval_split (MonoCompile *cfg, MonoLiveInterval *interval, MonoLiveInterval **i1, MonoLiveInterval **i2, int pos)
806 g_assert (pos > interval->range->from && pos <= interval->last_range->to);
808 *i1 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
809 *i2 = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
811 for (r = interval->range; r; r = r->next) {
813 /* Add it to the first child */
814 mono_linterval_add_range (cfg, *i1, r->from, r->to);
815 } else if (pos > r->from && pos <= r->to) {
817 mono_linterval_add_range (cfg, *i1, r->from, pos - 1);
818 mono_linterval_add_range (cfg, *i2, pos, r->to);
820 /* Add it to the second child */
821 mono_linterval_add_range (cfg, *i2, r->from, r->to);
827 #define LIVENESS_DEBUG(a) do { a; } while (0)
829 #define LIVENESS_DEBUG(a)
833 update_liveness2 (MonoCompile *cfg, MonoInst *ins, gboolean set_volatile, int inst_num, gint32 *last_use)
835 const char *spec = INS_INFO (ins->opcode);
838 LIVENESS_DEBUG (printf ("\t%x: ", inst_num); mono_print_ins (ins));
840 if (ins->opcode == OP_NOP)
844 if ((spec [MONO_INST_DEST] != ' ') && get_vreg_to_inst (cfg, ins->dreg)) {
845 MonoInst *var = get_vreg_to_inst (cfg, ins->dreg);
846 int idx = var->inst_c0;
847 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
849 if (MONO_IS_STORE_MEMBASE (ins)) {
850 if (last_use [idx] == 0) {
851 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", ins->dreg, inst_num));
852 last_use [idx] = inst_num;
855 if (last_use [idx] > 0) {
856 LIVENESS_DEBUG (printf ("\tadd range to R%d: [%x, %x)\n", ins->dreg, inst_num, last_use [idx]));
857 mono_linterval_add_range (cfg, vi->interval, inst_num, last_use [idx]);
861 /* Try dead code elimination */
862 if ((var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)) && ((ins->opcode == OP_ICONST) || (ins->opcode == OP_I8CONST) || (ins->opcode == OP_R8CONST))) {
863 LIVENESS_DEBUG (printf ("\tdead def of R%d, eliminated\n", ins->dreg));
864 ins->opcode = OP_NOP;
865 ins->dreg = ins->sreg1 = ins->sreg2 = -1;
869 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));
870 mono_linterval_add_range (cfg, vi->interval, inst_num, inst_num + 1);
877 if ((spec [MONO_INST_SRC1] != ' ') && get_vreg_to_inst (cfg, sreg)) {
878 MonoInst *var = get_vreg_to_inst (cfg, sreg);
879 int idx = var->inst_c0;
881 if (last_use [idx] == 0) {
882 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
883 last_use [idx] = inst_num;
889 if ((spec [MONO_INST_SRC2] != ' ') && get_vreg_to_inst (cfg, sreg)) {
890 MonoInst *var = get_vreg_to_inst (cfg, sreg);
891 int idx = var->inst_c0;
893 if (last_use [idx] == 0) {
894 LIVENESS_DEBUG (printf ("\tlast use of R%d set to %x\n", sreg, inst_num));
895 last_use [idx] = inst_num;
901 mono_analyze_liveness2 (MonoCompile *cfg)
903 int bnum, idx, i, j, nins, rem, max, max_vars, block_from, block_to, pos, reverse_len;
905 static guint32 disabled = -1;
909 disabled = getenv ("DISABLED") != NULL;
914 LIVENESS_DEBUG (printf ("LIVENESS 2 %s\n", mono_method_full_name (cfg->method, TRUE)));
917 if (strstr (cfg->method->name, "test_") != cfg->method->name)
921 max_vars = cfg->num_varinfo;
922 last_use = g_new0 (gint32, max_vars);
925 reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * reverse_len);
927 for (idx = 0; idx < max_vars; ++idx) {
928 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
930 vi->interval = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoLiveInterval));
934 * Process bblocks in reverse order, so the addition of new live ranges
935 * to the intervals is faster.
937 for (bnum = cfg->num_bblocks - 1; bnum >= 0; --bnum) {
938 MonoBasicBlock *bb = cfg->bblocks [bnum];
941 block_from = (bb->dfn << 16) + 1; /* so pos > 0 */
942 if (bnum < cfg->num_bblocks - 1)
943 /* Beginning of the next bblock */
944 block_to = (cfg->bblocks [bnum + 1]->dfn << 16) + 1;
946 block_to = (bb->dfn << 16) + 0xffff;
948 LIVENESS_DEBUG (printf ("LIVENESS BLOCK BB%d:\n", bb->block_num));
950 memset (last_use, 0, max_vars * sizeof (gint32));
952 /* For variables in bb->live_out, set last_use to block_to */
954 rem = max_vars % BITS_PER_CHUNK;
955 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
956 for (j = 0; j < max; ++j) {
960 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
961 k = (j * BITS_PER_CHUNK);
964 LIVENESS_DEBUG (printf ("Var R%d live at exit, set last_use to %x\n", cfg->varinfo [k]->dreg, block_to));
965 last_use [k] = block_to;
973 last_use [cfg->ret->inst_c0] = block_to;
975 for (nins = 0, pos = block_from, ins = bb->code; ins; ins = ins->next, ++nins, ++pos) {
976 if (nins >= reverse_len) {
977 int new_reverse_len = reverse_len * 2;
978 MonoInst **new_reverse = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * new_reverse_len);
979 memcpy (new_reverse, reverse, sizeof (MonoInst*) * reverse_len);
980 reverse = new_reverse;
981 reverse_len = new_reverse_len;
984 reverse [nins] = ins;
987 /* Process instructions backwards */
988 for (i = nins - 1; i >= 0; --i) {
989 MonoInst *ins = (MonoInst*)reverse [i];
991 update_liveness2 (cfg, ins, FALSE, pos, last_use);
996 for (idx = 0; idx < max_vars; ++idx) {
997 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
999 if (last_use [idx] != 0) {
1000 /* Live at exit, not written -> live on enter */
1001 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]));
1002 mono_linterval_add_range (cfg, vi->interval, block_from, last_use [idx]);
1008 * Arguments need to have their live ranges extended to the beginning of
1009 * the method to account for the arg reg/memory -> global register copies
1010 * in the prolog (bug #74992).
1012 for (i = 0; i < max_vars; i ++) {
1013 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
1014 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
1015 mono_linterval_add_range (cfg, vi->interval, 0, 1);
1019 for (idx = 0; idx < max_vars; ++idx) {
1020 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
1022 LIVENESS_DEBUG (printf ("LIVENESS R%d: ", cfg->varinfo [idx]->dreg));
1023 LIVENESS_DEBUG (mono_linterval_print (vi->interval));
1024 LIVENESS_DEBUG (printf ("\n"));
1032 update_used (MonoCompile *cfg, MonoInst *inst, MonoBitSet *used)
1034 int arity = mono_burg_arity [inst->opcode];
1037 update_used (cfg, inst->inst_i0, used);
1040 update_used (cfg, inst->inst_i1, used);
1042 if (inst->ssa_op & MONO_SSA_LOAD_STORE) {
1043 if (inst->ssa_op == MONO_SSA_LOAD) {
1044 int idx = inst->inst_i0->inst_c0;
1046 mono_bitset_set_fast (used, idx);
1052 * optimize_initlocals:
1054 * Try to optimize away some of the redundant initialization code inserted because of
1055 * 'locals init' using the liveness information.
1058 optimize_initlocals (MonoCompile *cfg)
1062 MonoBasicBlock *initlocals_bb;
1064 used = mono_bitset_new (cfg->num_varinfo, 0);
1066 mono_bitset_clear_all (used);
1067 initlocals_bb = cfg->bb_entry->next_bb;
1068 MONO_BB_FOR_EACH_INS (initlocals_bb, ins)
1069 update_used (cfg, ins, used);
1071 MONO_BB_FOR_EACH_INS (initlocals_bb, ins) {
1072 if (ins->ssa_op == MONO_SSA_STORE) {
1073 int idx = ins->inst_i0->inst_c0;
1074 MonoInst *var = cfg->varinfo [idx];
1076 if (var && !mono_bitset_test_fast (used, idx) && !mono_bitset_test_fast (initlocals_bb->live_out_set, var->inst_c0) && (var != cfg->ret) && !(var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))) {
1077 if (ins->inst_i1 && ((ins->inst_i1->opcode == OP_ICONST) || (ins->inst_i1->opcode == OP_I8CONST))) {
1079 ins->ssa_op = MONO_SSA_NOP;
1080 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;