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
25 optimize_initlocals (MonoCompile *cfg);
27 /* mono_bitset_mp_new:
29 * allocates a MonoBitSet inside a memory pool
31 static inline MonoBitSet*
32 mono_bitset_mp_new (MonoMemPool *mp, guint32 max_size)
34 int size = mono_bitset_alloc_size (max_size, 0);
37 mem = mono_mempool_alloc0 (mp, size);
38 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
41 static inline MonoBitSet*
42 mono_bitset_mp_new_noinit (MonoMemPool *mp, guint32 max_size)
44 int size = mono_bitset_alloc_size (max_size, 0);
47 mem = mono_mempool_alloc (mp, size);
48 return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
51 G_GNUC_UNUSED static void
52 mono_bitset_print (MonoBitSet *set)
57 for (i = 0; i < mono_bitset_size (set); i++) {
59 if (mono_bitset_test (set, i))
67 update_live_range (MonoCompile *cfg, int idx, int block_dfn, int tree_pos)
69 MonoLiveRange *range = &MONO_VARINFO (cfg, idx)->range;
70 guint32 abs_pos = (block_dfn << 16) | tree_pos;
72 if (range->first_use.abs_pos > abs_pos)
73 range->first_use.abs_pos = abs_pos;
75 if (range->last_use.abs_pos < abs_pos)
76 range->last_use.abs_pos = abs_pos;
80 update_gen_kill_set (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, int inst_num)
82 int arity = mono_burg_arity [inst->opcode];
83 int max_vars = cfg->num_varinfo;
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, int inst_num)
152 int arity = mono_burg_arity [inst->opcode];
153 int max_vars = cfg->num_varinfo;
156 update_volatile (cfg, bb, inst->inst_i0, inst_num);
159 update_volatile (cfg, bb, inst->inst_i1, inst_num);
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))
197 if (cfg->aliasing_info != NULL)
198 mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
201 MONO_BB_FOR_EACH_INS (bb, inst) {
202 update_volatile (cfg, bb, inst, tree_num);
206 *visited = g_slist_append (*visited, bb);
209 * Need to visit all bblocks reachable from this one since they can be
210 * reached during exception handling.
212 for (i = 0; i < bb->out_count; ++i) {
213 visit_bb (cfg, bb->out_bb [i], visited);
218 mono_liveness_handle_exception_clauses (MonoCompile *cfg)
221 GSList *visited = NULL;
224 * Variables in exception handler register cannot be allocated to registers
225 * so make them volatile. See bug #42136. This will not be neccessary when
226 * the back ends could guarantee that the variables will be in the
227 * correct registers when a handler is called.
228 * This includes try blocks too, since a variable in a try block might be
229 * accessed after an exception handler has been run.
231 for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
233 if (bb->region == -1 || MONO_BBLOCK_IS_IN_REGION (bb, MONO_REGION_TRY))
236 visit_bb (cfg, bb, &visited);
238 g_slist_free (visited);
241 /* generic liveness analysis code. CFG specific parts are
242 * in update_gen_kill_set()
245 mono_analyze_liveness (MonoCompile *cfg)
247 MonoBitSet *old_live_out_set;
248 int i, j, max_vars = cfg->num_varinfo;
250 gboolean *in_worklist;
251 MonoBasicBlock **worklist;
256 #ifdef DEBUG_LIVENESS
257 printf ("LIVENESS %s\n", mono_method_full_name (cfg->method, TRUE));
260 g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
262 cfg->comp_done |= MONO_COMP_LIVENESS;
267 bitsize = mono_bitset_alloc_size (max_vars, 0);
268 mem = mono_mempool_alloc0 (cfg->mempool, cfg->num_bblocks * bitsize * 4);
270 for (i = 0; i < cfg->num_bblocks; ++i) {
271 MonoBasicBlock *bb = cfg->bblocks [i];
273 bb->gen_set = mono_bitset_mem_new (mem, max_vars, MONO_BITSET_DONT_FREE);
275 bb->kill_set = mono_bitset_mem_new (mem, max_vars, MONO_BITSET_DONT_FREE);
277 /* Initialized later */
278 bb->live_in_set = NULL;
279 bb->live_out_set = mono_bitset_mem_new (mem, max_vars, MONO_BITSET_DONT_FREE);
282 for (i = 0; i < max_vars; i ++) {
283 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
284 MONO_VARINFO (cfg, i)->range.last_use .abs_pos = 0;
285 MONO_VARINFO (cfg, i)->spill_costs = 0;
288 for (i = 0; i < cfg->num_bblocks; ++i) {
289 MonoBasicBlock *bb = cfg->bblocks [i];
293 if (cfg->aliasing_info != NULL)
294 mono_aliasing_initialize_code_traversal (cfg->aliasing_info, bb);
297 MONO_BB_FOR_EACH_INS (bb, inst) {
298 #ifdef DEBUG_LIVENESS
299 mono_print_tree (inst); printf ("\n");
301 update_gen_kill_set (cfg, bb, inst, tree_num);
305 #ifdef DEBUG_LIVENESS
306 printf ("BLOCK BB%d (", bb->block_num);
307 for (j = 0; j < bb->out_count; j++)
308 printf ("BB%d, ", bb->out_bb [j]->block_num);
311 printf ("GEN BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
312 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
316 old_live_out_set = mono_bitset_new (max_vars, 0);
317 in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
319 worklist = g_new (MonoBasicBlock *, cfg->num_bblocks + 1);
323 * This is a backward dataflow analysis problem, so we process blocks in
324 * decreasing dfn order, this speeds up the iteration.
326 for (i = 0; i < cfg->num_bblocks; i ++) {
327 MonoBasicBlock *bb = cfg->bblocks [i];
329 worklist [l_end ++] = bb;
330 in_worklist [bb->dfn] = TRUE;
336 MonoBasicBlock *bb = worklist [--l_end];
337 MonoBasicBlock *out_bb;
340 in_worklist [bb->dfn] = FALSE;
342 #ifdef DEBUG_LIVENESS
343 printf ("P: %d(%d): IN: ", bb->block_num, bb->dfn);
344 for (j = 0; j < bb->in_count; ++j)
345 printf ("BB%d ", bb->in_bb [j]->block_num);
347 for (j = 0; j < bb->out_count; ++j)
348 printf ("BB%d ", bb->out_bb [j]->block_num);
352 if (bb->out_count == 0)
357 if (!bb->live_in_set) {
358 /* First pass over this bblock */
363 mono_bitset_copyto (bb->live_out_set, old_live_out_set);
366 for (j = 0; j < bb->out_count; j++) {
367 out_bb = bb->out_bb [j];
369 if (!out_bb->live_in_set) {
370 out_bb->live_in_set = mono_bitset_mem_new (mem, max_vars, MONO_BITSET_DONT_FREE);
373 mono_bitset_copyto (out_bb->live_out_set, out_bb->live_in_set);
374 mono_bitset_sub (out_bb->live_in_set, out_bb->kill_set);
375 mono_bitset_union (out_bb->live_in_set, out_bb->gen_set);
378 mono_bitset_union (bb->live_out_set, out_bb->live_in_set);
381 if (changed || !mono_bitset_equal (old_live_out_set, bb->live_out_set)) {
382 if (!bb->live_in_set) {
383 bb->live_in_set = mono_bitset_mem_new (mem, max_vars, MONO_BITSET_DONT_FREE);
386 mono_bitset_copyto (bb->live_out_set, bb->live_in_set);
387 mono_bitset_sub (bb->live_in_set, bb->kill_set);
388 mono_bitset_union (bb->live_in_set, bb->gen_set);
390 for (j = 0; j < bb->in_count; j++) {
391 MonoBasicBlock *in_bb = bb->in_bb [j];
393 * Some basic blocks do not seem to be in the
394 * cfg->bblocks array...
396 if (in_bb->gen_set && !in_worklist [in_bb->dfn]) {
397 #ifdef DEBUG_LIVENESS
398 printf ("\tADD: %d\n", in_bb->block_num);
401 * Put the block at the top of the stack, so it
402 * will be processed right away.
404 worklist [l_end ++] = in_bb;
405 in_worklist [in_bb->dfn] = TRUE;
411 //printf ("IT: %d %d %d.\n", iterations, in_iter, out_iter);
413 mono_bitset_free (old_live_out_set);
416 g_free (in_worklist);
418 /* Compute live_in_set for bblocks skipped earlier */
419 for (i = 0; i < cfg->num_bblocks; ++i) {
420 MonoBasicBlock *bb = cfg->bblocks [i];
422 if (!bb->live_in_set) {
423 bb->live_in_set = mono_bitset_mem_new (mem, max_vars, MONO_BITSET_DONT_FREE);
426 mono_bitset_copyto (bb->live_out_set, bb->live_in_set);
427 mono_bitset_sub (bb->live_in_set, bb->kill_set);
428 mono_bitset_union (bb->live_in_set, bb->gen_set);
433 * This code can be slow for large methods so inline the calls to
436 for (i = 0; i < cfg->num_bblocks; ++i) {
437 MonoBasicBlock *bb = cfg->bblocks [i];
440 if (!bb->live_out_set)
443 rem = max_vars % BITS_PER_CHUNK;
444 max = ((max_vars + (BITS_PER_CHUNK -1)) / BITS_PER_CHUNK);
445 for (j = 0; j < max; ++j) {
450 bits_in = mono_bitset_get_fast (bb->live_in_set, j);
451 bits_out = mono_bitset_get_fast (bb->live_out_set, j);
454 end = (j * BITS_PER_CHUNK) + rem;
456 end = (j * BITS_PER_CHUNK) + BITS_PER_CHUNK;
458 k = (j * BITS_PER_CHUNK);
459 while ((bits_in || bits_out)) {
461 update_live_range (cfg, k, bb->dfn, 0);
463 update_live_range (cfg, k, bb->dfn, 0xffff);
472 * Arguments need to have their live ranges extended to the beginning of
473 * the method to account for the arg reg/memory -> global register copies
474 * in the prolog (bug #74992).
477 for (i = 0; i < max_vars; i ++) {
478 MonoMethodVar *vi = MONO_VARINFO (cfg, i);
479 if (cfg->varinfo [vi->idx]->opcode == OP_ARG)
480 vi->range.first_use.abs_pos = 0;
483 #ifdef DEBUG_LIVENESS
484 for (i = cfg->num_bblocks - 1; i >= 0; i--) {
485 MonoBasicBlock *bb = cfg->bblocks [i];
487 printf ("LIVE IN BB%d: ", bb->block_num);
488 mono_bitset_print (bb->live_in_set);
489 printf ("LIVE OUT BB%d: ", bb->block_num);
490 mono_bitset_print (bb->live_out_set);
494 optimize_initlocals (cfg);
498 update_used (MonoCompile *cfg, MonoInst *inst, MonoBitSet *used)
500 int arity = mono_burg_arity [inst->opcode];
503 update_used (cfg, inst->inst_i0, used);
506 update_used (cfg, inst->inst_i1, used);
508 if (inst->ssa_op & MONO_SSA_LOAD_STORE) {
509 if (inst->ssa_op == MONO_SSA_LOAD) {
510 int idx = inst->inst_i0->inst_c0;
512 mono_bitset_set_fast (used, idx);
518 * optimize_initlocals:
520 * Try to optimize away some of the redundant initialization code inserted because of
521 * 'locals init' using the liveness information.
524 optimize_initlocals (MonoCompile *cfg)
528 MonoBasicBlock *initlocals_bb;
530 used = mono_bitset_new (cfg->num_varinfo, 0);
532 mono_bitset_clear_all (used);
533 initlocals_bb = cfg->bb_entry->next_bb;
534 MONO_BB_FOR_EACH_INS (initlocals_bb, ins)
535 update_used (cfg, ins, used);
537 MONO_BB_FOR_EACH_INS (initlocals_bb, ins) {
538 if (ins->ssa_op == MONO_SSA_STORE) {
539 int idx = ins->inst_i0->inst_c0;
540 MonoInst *var = cfg->varinfo [idx];
542 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))) {
543 if (ins->inst_i1 && ((ins->inst_i1->opcode == OP_ICONST) || (ins->inst_i1->opcode == OP_I8CONST))) {
545 ins->ssa_op = MONO_SSA_NOP;
546 MONO_VARINFO (cfg, var->inst_c0)->spill_costs -= 1;