First set of licensing changes
[mono.git] / mono / mini / dominators.c
1 /*
2  * dominators.c: Dominator computation on the control flow graph
3  *
4  * Author:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *
8  * (C) 2003 Ximian, Inc.
9  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11  */
12 #include <string.h>
13 #include <mono/metadata/debug-helpers.h>
14 #include <mono/metadata/mempool.h>
15 #include <mono/metadata/mempool-internals.h>
16
17 #include "mini.h"
18
19 #ifndef DISABLE_JIT
20
21 /*
22  * bb->dfn == 0 means either the bblock is ignored by the dfn calculation, or
23  * it is the entry bblock.
24  */
25 #define HAS_DFN(bb, entry) ((bb)->dfn || ((bb) == entry))
26
27 /*
28  * Compute dominators and immediate dominators using the algorithm in the
29  * paper "A Simple, Fast Dominance Algorithm" by Keith D. Cooper, 
30  * Timothy J. Harvey, and Ken Kennedy:
31  * http://citeseer.ist.psu.edu/cooper01simple.html
32  */
33 static void
34 compute_dominators (MonoCompile *cfg)
35 {
36         int bindex, i, bitsize;
37         MonoBasicBlock *entry;
38         MonoBasicBlock **doms;
39         gboolean changed;
40
41         g_assert (!(cfg->comp_done & MONO_COMP_DOM));
42
43         bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
44
45         entry = cfg->bblocks [0];
46
47         doms = g_new0 (MonoBasicBlock*, cfg->num_bblocks);
48         doms [entry->dfn] = entry;
49
50         if (cfg->verbose_level > 1) {
51                 for (i = 0; i < cfg->num_bblocks; ++i) {
52                         int j;
53                         MonoBasicBlock *bb = cfg->bblocks [i];
54
55                         printf ("BB%d IN: ", bb->block_num);
56                         for (j = 0; j < bb->in_count; ++j)
57                                 printf ("%d ", bb->in_bb [j]->block_num);
58                         printf ("\n");
59                 }
60         }
61
62         changed = TRUE;
63         while (changed) {
64                 changed = FALSE;
65
66                 for (bindex = 0; bindex < cfg->num_bblocks; ++bindex) {
67                         MonoBasicBlock *bb = cfg->bblocks [bindex];
68                         MonoBasicBlock *idom;
69
70                         idom = NULL;
71                         for (i = 0; i < bb->in_count; ++i) {
72                                 MonoBasicBlock *in_bb = bb->in_bb [i];
73                                 if ((in_bb != bb) && doms [in_bb->dfn]) {
74                                         idom = in_bb;
75                                         break;
76                                 }
77                         }
78                         if (bb != cfg->bblocks [0])
79                                 g_assert (idom);
80
81                         while (i < bb->in_count) {
82                                 MonoBasicBlock *in_bb = bb->in_bb [i];
83
84                                 if (HAS_DFN (in_bb, entry) && doms [in_bb->dfn]) {
85                                         /* Intersect */
86                                         MonoBasicBlock *f1 = idom;
87                                         MonoBasicBlock *f2 = in_bb;
88
89                                         while (f1 != f2) {
90                                                 if (f1->dfn < f2->dfn)
91                                                         f2 = doms [f2->dfn];
92                                                 else
93                                                         f1 = doms [f1->dfn];
94                                         }
95
96                                         idom = f1;
97                                 }
98                                 i ++;
99                         }
100
101                         if (idom != doms [bb->dfn]) {
102                                 if (bb == cfg->bblocks [0])
103                                         doms [bb->dfn] = bb;
104                                 else {
105                                         doms [bb->dfn] = idom;
106                                         changed = TRUE;
107                                 }
108
109                                 //printf ("A: bb=%d dfn=%d dom:%d\n", bb->block_num, bb->dfn, doms [bb->dfn]->block_num);
110                         }
111                 }
112         }
113
114         /* Compute bb->dominators for each bblock */
115         for (i = 0; i < cfg->num_bblocks; ++i) {
116                 MonoBasicBlock *bb = cfg->bblocks [i];
117                 MonoBasicBlock *cbb;
118                 MonoBitSet *dominators;
119                 char *mem;
120
121                 mem = (char *)mono_mempool_alloc0 (cfg->mempool, bitsize);
122
123                 bb->dominators = dominators = mono_bitset_mem_new (mem, cfg->num_bblocks, 0);
124                 mem += bitsize;
125
126                 mono_bitset_set_fast (dominators, bb->dfn);
127
128                 if (bb->dfn) {
129                         for (cbb = doms [bb->dfn]; cbb->dfn; cbb = doms [cbb->dfn])
130                                 mono_bitset_set_fast (dominators, cbb->dfn);
131
132                         bb->idom = doms [bb->dfn];
133                         if (bb->idom)
134                                 bb->idom->dominated = g_slist_prepend_mempool (cfg->mempool, bb->idom->dominated, bb);
135                 }
136
137                 /* The entry bb */
138                 mono_bitset_set_fast (dominators, 0);
139         }
140
141         g_free (doms);
142
143         cfg->comp_done |= MONO_COMP_DOM | MONO_COMP_IDOM;
144
145         if (cfg->verbose_level > 1) {
146                 printf ("DTREE %s %d\n", mono_method_full_name (cfg->method, TRUE),
147                         cfg->header->num_clauses);
148                 for (i = 0; i < cfg->num_bblocks; ++i) {
149                         MonoBasicBlock *bb = cfg->bblocks [i];
150                         printf ("BB%d(dfn=%d) (IDOM=BB%d): ", bb->block_num, bb->dfn, bb->idom ? bb->idom->block_num : -1);
151                         mono_blockset_print (cfg, bb->dominators, NULL, -1);
152                 }
153         }
154 }
155
156 #if 0
157
158 static void
159 check_dominance_frontier (MonoBasicBlock *x, MonoBasicBlock *t)
160 {
161         int i, j;
162
163         t->flags |= BB_VISITED;
164
165         if (mono_bitset_test_fast (t->dominators, x->dfn)) {
166                 for (i = 0; i < t->out_count; ++i) {
167                         if (!(t->flags & BB_VISITED)) {
168                                 int found = FALSE;
169                                 check_dominance_frontier (x, t->out_bb [i]);
170                                 
171                                 for (j = 0; j < t->out_bb [i]->in_count; j++) {
172                                         if (t->out_bb [i]->in_bb [j] == t)
173                                                 found = TRUE;
174                                 }
175                                 g_assert (found);
176                         }
177                 }
178         } else {
179                 if (!mono_bitset_test_fast (x->dfrontier, t->dfn)) {
180                         printf ("BB%d not in frontier of BB%d\n", t->block_num, x->block_num);
181                         g_assert_not_reached ();
182                 }
183         }
184
185
186 #endif
187
188 /**
189  * Compute dominance frontiers using the algorithm from the same paper.
190  */
191 static void
192 compute_dominance_frontier (MonoCompile *cfg)
193 {
194         char *mem;
195         int i, j, bitsize;
196
197         g_assert (!(cfg->comp_done & MONO_COMP_DFRONTIER));
198
199         for (i = 0; i < cfg->num_bblocks; ++i)
200                 cfg->bblocks [i]->flags &= ~BB_VISITED;
201
202         bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
203         mem = (char *)mono_mempool_alloc0 (cfg->mempool, bitsize * cfg->num_bblocks);
204  
205         for (i = 0; i < cfg->num_bblocks; ++i) {
206                 MonoBasicBlock *bb = cfg->bblocks [i];
207                 bb->dfrontier = mono_bitset_mem_new (mem, cfg->num_bblocks, 0);
208                 mem += bitsize;
209         }
210
211         for (i = 0; i < cfg->num_bblocks; ++i) {
212                 MonoBasicBlock *bb = cfg->bblocks [i];
213
214                 if (bb->in_count > 1) {
215                         for (j = 0; j < bb->in_count; ++j) {
216                                 MonoBasicBlock *p = bb->in_bb [j];
217
218                                 if (p->dfn || (p == cfg->bblocks [0])) {
219                                         while (p != bb->idom) {
220                                                 mono_bitset_set_fast (p->dfrontier, bb->dfn);
221                                                 p = p->idom;
222                                         }
223                                 }
224                         }
225                 }
226         }
227
228 #if 0
229         for (i = 0; i < cfg->num_bblocks; ++i) {
230                 MonoBasicBlock *bb = cfg->bblocks [i];
231                 
232                 printf ("DFRONT %s BB%d: ", mono_method_full_name (cfg->method, TRUE), bb->block_num);
233                 mono_blockset_print (cfg, bb->dfrontier, NULL, -1);
234         }
235 #endif
236
237 #if 0
238         /* this is a check for the dominator frontier */
239         for (i = 0; i < m->num_bblocks; ++i) {
240                 MonoBasicBlock *x = m->bblocks [i];
241                 
242                 mono_bitset_foreach_bit ((x->dfrontier), j, (m->num_bblocks)) {
243                         MonoBasicBlock *w = m->bblocks [j];
244                         int k;
245                         /* x must not strictly dominates w */
246                         if (mono_bitset_test_fast (w->dominators, x->dfn) && w != x)
247                                 g_assert_not_reached ();
248                         
249                         for (k = 0; k < m->num_bblocks; ++k)
250                                 m->bblocks [k]->flags &= ~BB_VISITED;
251
252                         check_dominance_frontier (x, x);
253                 }
254         }
255 #endif
256
257         cfg->comp_done |= MONO_COMP_DFRONTIER;
258 }
259
260 static inline void
261 df_set (MonoCompile *m, MonoBitSet* dest, MonoBitSet *set) 
262 {
263         int i;
264
265         mono_bitset_foreach_bit (set, i, m->num_bblocks) {
266                 mono_bitset_union_fast (dest, m->bblocks [i]->dfrontier);
267         }
268 }
269
270 MonoBitSet*
271 mono_compile_iterated_dfrontier (MonoCompile *m, MonoBitSet *set)
272 {
273         MonoBitSet *result;
274         int bitsize, count1, count2;
275
276         bitsize = mono_bitset_alloc_size (m->num_bblocks, 0);
277         result = mono_bitset_mem_new (mono_mempool_alloc0 (m->mempool, bitsize), m->num_bblocks, 0);
278
279         df_set (m, result, set);
280         count2 = mono_bitset_count (result);
281         do {
282                 count1 = count2;
283                 df_set (m, result, result);
284                 count2 = mono_bitset_count (result);
285         } while (count2 > count1);
286         
287         return result;
288 }
289
290 void    
291 mono_compile_dominator_info (MonoCompile *cfg, int dom_flags)
292 {
293         if ((dom_flags & MONO_COMP_DOM) && !(cfg->comp_done & MONO_COMP_DOM))
294                 compute_dominators (cfg);
295         if ((dom_flags & MONO_COMP_DFRONTIER) && !(cfg->comp_done & MONO_COMP_DFRONTIER))
296                 compute_dominance_frontier (cfg);
297 }
298
299 /*
300  * code to detect loops and loop nesting level
301  */
302 void 
303 mono_compute_natural_loops (MonoCompile *cfg)
304 {
305         int i, j, k;
306         MonoBitSet *in_loop_blocks;
307         int *bb_indexes;
308
309         g_assert (!(cfg->comp_done & MONO_COMP_LOOPS));
310
311         in_loop_blocks = mono_bitset_new (cfg->num_bblocks + 1, 0);
312         for (i = 0; i < cfg->num_bblocks; ++i) {
313                 MonoBasicBlock *n = cfg->bblocks [i];
314
315                 for (j = 0; j < n->out_count; j++) {
316                         MonoBasicBlock *h = n->out_bb [j];
317                         /* check for single block loops */
318                         if (n == h) {
319                                 h->loop_blocks = g_list_prepend_mempool (cfg->mempool, h->loop_blocks, h);
320                                 h->nesting++;
321                         }
322                         /* check for back-edge from n to h */
323                         else if (n != h && mono_bitset_test_fast (n->dominators, h->dfn)) {
324                                 GSList *todo;
325
326                                 /* already in loop_blocks? */
327                                 if (h->loop_blocks && g_list_find (h->loop_blocks, n)) {
328                                         continue;
329                                 }
330
331                                 mono_bitset_clear_all (in_loop_blocks);
332                                 if (h->loop_blocks) {
333                                         GList *l;
334
335                                         for (l = h->loop_blocks; l; l = l->next) {
336                                                 MonoBasicBlock *b = (MonoBasicBlock *)l->data;
337                                                 if (b->dfn)
338                                                         mono_bitset_set_fast (in_loop_blocks, b->dfn);
339                                         }
340                                 }
341                                 todo = g_slist_prepend (NULL, n);       
342                         
343                                 while (todo) {
344                                         MonoBasicBlock *cb = (MonoBasicBlock *)todo->data;
345                                         todo = g_slist_delete_link (todo, todo);
346
347                                         if ((cb->dfn && mono_bitset_test_fast (in_loop_blocks, cb->dfn)) || (!cb->dfn && g_list_find (h->loop_blocks, cb)))
348                                                 continue;
349
350                                         h->loop_blocks = g_list_prepend_mempool (cfg->mempool, h->loop_blocks, cb);
351                                         cb->nesting++;
352                                         if (cb->dfn)
353                                                 mono_bitset_set_fast (in_loop_blocks, cb->dfn);
354
355                                         for (k = 0; k < cb->in_count; k++) {
356                                                 MonoBasicBlock *prev = cb->in_bb [k];
357                                                 /* add all previous blocks */
358                                                 if (prev != h && !((prev->dfn && mono_bitset_test_fast (in_loop_blocks, prev->dfn)) || (!prev->dfn && g_list_find (h->loop_blocks, prev)))) {
359                                                         todo = g_slist_prepend (todo, prev);
360                                                 }
361                                         }
362                                 }
363
364                                 /* add the header if not already there */
365                                 if (!((h->dfn && mono_bitset_test_fast (in_loop_blocks, h->dfn)) || (!h->dfn && g_list_find (h->loop_blocks, h)))) {
366                                         h->loop_blocks = g_list_prepend_mempool (cfg->mempool, h->loop_blocks, h);
367                                         h->nesting++;
368                                 }
369                         }
370                 }
371         }
372         mono_bitset_free (in_loop_blocks);
373
374         cfg->comp_done |= MONO_COMP_LOOPS;
375
376         /* Compute loop_body_start for each loop */
377         bb_indexes = g_new0 (int, cfg->num_bblocks);
378         {
379                 MonoBasicBlock *bb;
380
381                 for (i = 0, bb = cfg->bb_entry; bb; i ++, bb = bb->next_bb) {
382                         if (bb->dfn)
383                                 bb_indexes [bb->dfn] = i;
384                 }
385         }
386         for (i = 0; i < cfg->num_bblocks; ++i) {
387                 if (cfg->bblocks [i]->loop_blocks) {
388                         /* The loop body start is the first bblock in the order they will be emitted */
389                         MonoBasicBlock *h = cfg->bblocks [i];
390                         MonoBasicBlock *body_start = h;
391                         GList *l;
392
393                         for (l = h->loop_blocks; l; l = l->next) {
394                                 MonoBasicBlock *cb = (MonoBasicBlock *)l->data;
395
396                                 if (cb->dfn && bb_indexes [cb->dfn] < bb_indexes [body_start->dfn]) {
397                                         body_start = cb;
398                                 }
399                         }
400
401                         body_start->loop_body_start = 1;
402                 }
403         }
404         g_free (bb_indexes);
405
406         if (cfg->verbose_level > 1) {
407                 for (i = 0; i < cfg->num_bblocks; ++i) {
408                         if (cfg->bblocks [i]->loop_blocks) {
409                                 MonoBasicBlock *h = (MonoBasicBlock *)cfg->bblocks [i]->loop_blocks->data;
410                                 GList *l;
411                                 printf ("LOOP START %d\n", h->block_num);
412                                 for (l = h->loop_blocks; l; l = l->next) {
413                                         MonoBasicBlock *cb = (MonoBasicBlock *)l->data;
414                                         printf ("\tBB%d %d %p\n", cb->block_num, cb->nesting, cb->loop_blocks);
415                                 }
416                         }
417                 }
418         }
419 }
420
421 static void
422 clear_idominators (MonoCompile *cfg)
423 {
424         guint i;
425     
426         for (i = 0; i < cfg->num_bblocks; ++i) {
427                 if (cfg->bblocks[i]->dominated) {
428                         cfg->bblocks[i]->dominated = NULL;
429                 }
430         }
431
432         cfg->comp_done &= ~MONO_COMP_IDOM;   
433 }
434
435 static void
436 clear_loops (MonoCompile *cfg)
437 {
438         guint i;
439     
440         for (i = 0; i < cfg->num_bblocks; ++i) {
441                 cfg->bblocks[i]->nesting = 0;
442                 cfg->bblocks[i]->loop_blocks = NULL;
443         }
444
445         cfg->comp_done &= ~MONO_COMP_LOOPS;   
446 }
447
448 void
449 mono_free_loop_info (MonoCompile *cfg)
450 {
451     if (cfg->comp_done & MONO_COMP_IDOM)
452         clear_idominators (cfg);
453     if (cfg->comp_done & MONO_COMP_LOOPS)
454         clear_loops (cfg);
455 }
456
457 #else /* DISABLE_JIT */
458
459 void
460 mono_free_loop_info (MonoCompile *cfg)
461 {
462 }
463
464 #endif /* DISABLE_JIT */