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