Don't run test-318 with gmcs.
[mono.git] / mono / mini / liveness.c
1 /*
2  * liveness.c: liveness analysis
3  *
4  * Author:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  */
9
10 #include "mini.h"
11 #include "inssel.h"
12
13 //#define DEBUG_LIVENESS
14
15 extern guint8 mono_burg_arity [];
16
17 /* mono_bitset_mp_new:
18  * 
19  * allocates a MonoBitSet inside a memory pool
20  */
21 static MonoBitSet* 
22 mono_bitset_mp_new (MonoMemPool *mp,  guint32 max_size)
23 {
24         int size = mono_bitset_alloc_size (max_size, 0);
25         gpointer mem;
26
27         mem = mono_mempool_alloc0 (mp, size);
28         return mono_bitset_mem_new (mem, max_size, MONO_BITSET_DONT_FREE);
29 }
30
31 G_GNUC_UNUSED static void
32 mono_bitset_print (MonoBitSet *set)
33 {
34         int i;
35
36         printf ("{");
37         for (i = 0; i < mono_bitset_size (set); i++) {
38
39                 if (mono_bitset_test (set, i))
40                         printf ("%d, ", i);
41
42         }
43         printf ("}\n");
44 }
45
46 static inline void
47 update_live_range (MonoCompile *cfg, int idx, int block_dfn, int tree_pos)
48 {
49         MonoLiveRange *range = &MONO_VARINFO (cfg, idx)->range;
50         guint32 abs_pos = (block_dfn << 16) | tree_pos;
51
52         if (range->first_use.abs_pos > abs_pos)
53                 range->first_use.abs_pos = abs_pos;
54
55         if (range->last_use.abs_pos < abs_pos)
56                 range->last_use.abs_pos = abs_pos;
57 }
58
59 static void
60 update_gen_kill_set (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, int inst_num)
61 {
62         int arity = mono_burg_arity [inst->opcode];
63         int max_vars = cfg->num_varinfo;
64
65         if (arity)
66                 update_gen_kill_set (cfg, bb, inst->inst_i0, inst_num);
67
68         if (arity > 1)
69                 update_gen_kill_set (cfg, bb, inst->inst_i1, inst_num);
70
71         if (inst->ssa_op == MONO_SSA_LOAD) {
72                 int idx = inst->inst_i0->inst_c0;
73                 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
74                 g_assert (idx < max_vars);
75                 if (bb->region != -1) {
76                         /*
77                          * Variables used in exception regions can't be allocated to 
78                          * registers.
79                          */
80                         cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
81                 }
82                 update_live_range (cfg, idx, bb->dfn, inst_num); 
83                 if (!mono_bitset_test (bb->kill_set, idx))
84                         mono_bitset_set (bb->gen_set, idx);
85                 vi->spill_costs += 1 + (bb->nesting * 2);
86         } else if (inst->ssa_op == MONO_SSA_STORE) {
87                 int idx = inst->inst_i0->inst_c0;
88                 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
89                 g_assert (idx < max_vars);
90                 g_assert (inst->inst_i1->opcode != OP_PHI);
91                 if (bb->region != -1) {
92                         /*
93                          * Variables used in exception regions can't be allocated to 
94                          * registers.
95                          */
96                         cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
97                 }
98                 update_live_range (cfg, idx, bb->dfn, inst_num); 
99                 mono_bitset_set (bb->kill_set, idx);
100                 vi->spill_costs += 1 + (bb->nesting * 2);
101         }
102
103
104 static void
105 update_volatile (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, int inst_num)
106 {
107         int arity = mono_burg_arity [inst->opcode];
108         int max_vars = cfg->num_varinfo;
109
110         if (arity)
111                 update_volatile (cfg, bb, inst->inst_i0, inst_num);
112
113         if (arity > 1)
114                 update_volatile (cfg, bb, inst->inst_i1, inst_num);
115
116         if ((inst->ssa_op == MONO_SSA_LOAD) || (inst->ssa_op == MONO_SSA_STORE)) {
117                 int idx = inst->inst_i0->inst_c0;
118                 MonoMethodVar *vi = MONO_VARINFO (cfg, idx);
119                 g_assert (idx < max_vars);
120                 cfg->varinfo [vi->idx]->flags |= MONO_INST_VOLATILE;
121         }
122
123
124 static void
125 visit_bb (MonoCompile *cfg, MonoBasicBlock *bb, GSList **visited)
126 {
127         int i, tree_num;
128         MonoInst *inst;
129
130         if (g_slist_find (*visited, bb))
131                 return;
132
133         for (tree_num = 0, inst = bb->code; inst; inst = inst->next, tree_num++) {
134                 update_volatile (cfg, bb, inst, tree_num);
135         }
136
137         *visited = g_slist_append (*visited, bb);
138
139         /* 
140          * Need to visit all bblocks reachable from this one since they can be
141          * reached during exception handling.
142          */
143         for (i = 0; i < bb->out_count; ++i) {
144                 visit_bb (cfg, bb->out_bb [i], visited);
145         }
146 }
147
148 static void
149 handle_exception_clauses (MonoCompile *cfg)
150 {
151         MonoBasicBlock *bb;
152         GSList *visited = NULL;
153
154         /*
155          * Variables in exception handler register cannot be allocated to registers
156          * so make them volatile. See bug #42136. This will not be neccessary when
157          * the back ends could guarantee that the variables will be in the
158          * correct registers when a handler is called.
159          */
160         for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
161                 if (bb->region == -1)
162                         continue;
163
164                 visit_bb (cfg, bb, &visited);
165         }
166         g_slist_free (visited);
167 }
168
169 /* generic liveness analysis code. CFG specific parts are 
170  * in update_gen_kill_set()
171  */
172 void
173 mono_analyze_liveness (MonoCompile *cfg)
174 {
175         MonoBitSet *old_live_in_set, *old_live_out_set, *tmp_in_set;
176         gboolean changes;
177         int i, j, max_vars = cfg->num_varinfo;
178         int iterations, out_iter, in_iter;
179         gboolean *changed_in, *changed_out, *new_changed_in, *in_worklist;
180         MonoBasicBlock **worklist;
181         guint32 l_begin, l_end;
182         static int count = 0;
183
184 #ifdef DEBUG_LIVENESS
185         printf ("LIVENESS %s\n", mono_method_full_name (cfg->method, TRUE));
186 #endif
187
188         g_assert (!(cfg->comp_done & MONO_COMP_LIVENESS));
189
190         cfg->comp_done |= MONO_COMP_LIVENESS;
191         
192         if (max_vars == 0)
193                 return;
194
195         for (i = 0; i < cfg->num_bblocks; ++i) {
196                 MonoBasicBlock *bb = cfg->bblocks [i];
197
198                 bb->gen_set = mono_bitset_mp_new (cfg->mempool, max_vars);
199                 bb->kill_set = mono_bitset_mp_new (cfg->mempool, max_vars);
200                 bb->live_in_set = mono_bitset_mp_new (cfg->mempool, max_vars);
201                 bb->live_out_set = mono_bitset_mp_new (cfg->mempool, max_vars);
202         }
203         for (i = 0; i < max_vars; i ++) {
204                 MONO_VARINFO (cfg, i)->range.first_use.abs_pos = ~ 0;
205                 MONO_VARINFO (cfg, i)->range.last_use .abs_pos =   0;
206                 MONO_VARINFO (cfg, i)->spill_costs = 0;
207         }
208
209         for (i = 0; i < cfg->num_bblocks; ++i) {
210                 MonoBasicBlock *bb = cfg->bblocks [i];
211                 MonoInst *inst;
212                 int tree_num;
213
214                 for (tree_num = 0, inst = bb->code; inst; inst = inst->next, tree_num++) {
215                         //mono_print_tree (inst); printf ("\n");
216                         update_gen_kill_set (cfg, bb, inst, tree_num);
217                 }
218
219 #ifdef DEBUG_LIVENESS
220                 printf ("BLOCK BB%d (", bb->block_num);
221                 for (j = 0; j < bb->out_count; j++) 
222                         printf ("BB%d, ", bb->out_bb [j]->block_num);
223                 
224                 printf (")\n");
225                 printf ("GEN  BB%d: ", bb->block_num); mono_bitset_print (bb->gen_set);
226                 printf ("KILL BB%d: ", bb->block_num); mono_bitset_print (bb->kill_set);
227 #endif
228         }
229
230         old_live_in_set = mono_bitset_new (max_vars, 0);
231         old_live_out_set = mono_bitset_new (max_vars, 0);
232         tmp_in_set = mono_bitset_new (max_vars, 0);
233         changed_in = g_new0 (gboolean, cfg->num_bblocks + 1);
234         changed_out = g_new0 (gboolean, cfg->num_bblocks + 1);
235         in_worklist = g_new0 (gboolean, cfg->num_bblocks + 1);
236         new_changed_in = g_new0 (gboolean, cfg->num_bblocks + 1);
237
238         for (i = 0; i < cfg->num_bblocks + 1; ++i) {
239                 changed_in [i] = TRUE;
240                 changed_out [i] = TRUE;
241         }
242
243         count ++;
244
245         worklist = g_new0 (MonoBasicBlock *, cfg->num_bblocks + 1);
246         l_begin = 0;
247         l_end = 0;
248
249         for (i = cfg->num_bblocks - 1; i >= 0; i--) {
250                 MonoBasicBlock *bb = cfg->bblocks [i];
251
252                 worklist [l_end ++] = bb;
253                 in_worklist [bb->dfn] = TRUE;
254         }
255
256         iterations = 0;
257         out_iter = 0;
258         in_iter = 0;
259         do {
260                 changes = FALSE;
261                 iterations ++;
262
263                 while (l_begin != l_end) {
264                         MonoBasicBlock *bb = worklist [l_begin ++];
265
266                         in_worklist [bb->dfn] = FALSE;
267
268                         if (l_begin == cfg->num_bblocks + 1)
269                                 l_begin = 0;
270
271                         if (bb->out_count > 0) {
272                                 out_iter ++;
273                                 mono_bitset_copyto (bb->live_out_set, old_live_out_set);
274
275                                 for (j = 0; j < bb->out_count; j++) {
276                                         MonoBasicBlock *out_bb = bb->out_bb [j];
277
278                                         mono_bitset_copyto (out_bb->live_out_set, tmp_in_set);
279                                         mono_bitset_sub (tmp_in_set, out_bb->kill_set);
280                                         mono_bitset_union (tmp_in_set, out_bb->gen_set);
281
282                                         mono_bitset_union (bb->live_out_set, tmp_in_set);
283
284                                 }
285                                 
286                                 changed_out [bb->dfn] = !mono_bitset_equal (old_live_out_set, bb->live_out_set);
287                                 if (changed_out [bb->dfn]) {
288                                         for (j = 0; j < bb->in_count; j++) {
289                                                 MonoBasicBlock *in_bb = bb->in_bb [j];
290                                                 /* 
291                                                  * Some basic blocks do not seem to be in the 
292                                                  * cfg->bblocks array...
293                                                  */
294                                                 if (in_bb->live_in_set)
295                                                         if (!in_worklist [in_bb->dfn]) {
296                                                                 worklist [l_end ++] = in_bb;
297                                                                 if (l_end == cfg->num_bblocks + 1)
298                                                                         l_end = 0;
299                                                                 in_worklist [in_bb->dfn] = TRUE;
300                                                         }
301                                         }
302
303                                         changes = TRUE;
304                                 }
305                         }
306                 }
307         } while (changes);
308
309         //printf ("IT: %d %d %d.\n", iterations, in_iter, out_iter);
310
311         mono_bitset_free (old_live_in_set);
312         mono_bitset_free (old_live_out_set);
313         mono_bitset_free (tmp_in_set);
314
315         g_free (changed_in);
316         g_free (changed_out);
317         g_free (new_changed_in);
318         g_free (worklist);
319         g_free (in_worklist);
320
321         for (i = cfg->num_bblocks - 1; i >= 0; i--) {
322                 MonoBasicBlock *bb = cfg->bblocks [i];
323
324                 mono_bitset_copyto (bb->live_out_set, bb->live_in_set);
325                 mono_bitset_sub (bb->live_in_set, bb->kill_set);
326                 mono_bitset_union (bb->live_in_set, bb->gen_set);
327         }
328
329         /*
330          * This code can be slow for large methods so inline the calls to
331          * mono_bitset_test.
332          */
333         for (i = 0; i < cfg->num_bblocks; ++i) {
334                 MonoBasicBlock *bb = cfg->bblocks [i];
335                 guint32 rem;
336
337                 rem = max_vars % 32;
338                 for (j = 0; j < (max_vars / 32) + 1; ++j) {
339                         guint32 bits_in;
340                         guint32 bits_out;
341                         int k, nbits;
342
343                         if (j > (max_vars / 32))
344                                 break;
345                         else
346                                 if (j == (max_vars / 32))
347                                         nbits = rem;
348                                 else
349                                         nbits = 32;
350
351                         bits_in = mono_bitset_test_bulk (bb->live_in_set, j * 32);
352                         bits_out = mono_bitset_test_bulk (bb->live_out_set, j * 32);
353                         for (k = 0; k < nbits; ++k) {
354                                 if (bits_in & (1 << k))
355                                         update_live_range (cfg, (j * 32) + k, bb->dfn, 0);
356                                 if (bits_out & (1 << k))
357                                         update_live_range (cfg, (j * 32) + k, bb->dfn, 0xffff);
358                         }
359                 }
360         }
361
362         handle_exception_clauses (cfg);
363
364 #ifdef DEBUG_LIVENESS
365         for (i = cfg->num_bblocks - 1; i >= 0; i--) {
366                 MonoBasicBlock *bb = cfg->bblocks [i];
367                 
368                 printf ("LIVE IN  BB%d: ", bb->block_num); 
369                 mono_bitset_print (bb->live_in_set); 
370                 printf ("LIVE OUT BB%d: ", bb->block_num); 
371                 mono_bitset_print (bb->live_out_set); 
372         }
373 #endif
374 }
375