3 * Dominator computation on the control flow graph
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Paolo Molaro (lupus@ximian.com)
9 * (C) 2003 Ximian, Inc.
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 #include <mono/metadata/debug-helpers.h>
15 #include <mono/metadata/mempool.h>
16 #include <mono/metadata/mempool-internals.h>
23 * bb->dfn == 0 means either the bblock is ignored by the dfn calculation, or
24 * it is the entry bblock.
26 #define HAS_DFN(bb, entry) ((bb)->dfn || ((bb) == entry))
29 * Compute dominators and immediate dominators using the algorithm in the
30 * paper "A Simple, Fast Dominance Algorithm" by Keith D. Cooper,
31 * Timothy J. Harvey, and Ken Kennedy:
32 * http://citeseer.ist.psu.edu/cooper01simple.html
35 compute_dominators (MonoCompile *cfg)
37 int bindex, i, bitsize;
38 MonoBasicBlock *entry;
39 MonoBasicBlock **doms;
42 g_assert (!(cfg->comp_done & MONO_COMP_DOM));
44 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
46 entry = cfg->bblocks [0];
48 doms = g_new0 (MonoBasicBlock*, cfg->num_bblocks);
49 doms [entry->dfn] = entry;
51 if (cfg->verbose_level > 1) {
52 for (i = 0; i < cfg->num_bblocks; ++i) {
54 MonoBasicBlock *bb = cfg->bblocks [i];
56 printf ("BB%d IN: ", bb->block_num);
57 for (j = 0; j < bb->in_count; ++j)
58 printf ("%d ", bb->in_bb [j]->block_num);
67 for (bindex = 0; bindex < cfg->num_bblocks; ++bindex) {
68 MonoBasicBlock *bb = cfg->bblocks [bindex];
72 for (i = 0; i < bb->in_count; ++i) {
73 MonoBasicBlock *in_bb = bb->in_bb [i];
74 if ((in_bb != bb) && doms [in_bb->dfn]) {
79 if (bb != cfg->bblocks [0])
82 while (i < bb->in_count) {
83 MonoBasicBlock *in_bb = bb->in_bb [i];
85 if (HAS_DFN (in_bb, entry) && doms [in_bb->dfn]) {
87 MonoBasicBlock *f1 = idom;
88 MonoBasicBlock *f2 = in_bb;
91 if (f1->dfn < f2->dfn)
102 if (idom != doms [bb->dfn]) {
103 if (bb == cfg->bblocks [0])
106 doms [bb->dfn] = idom;
110 //printf ("A: bb=%d dfn=%d dom:%d\n", bb->block_num, bb->dfn, doms [bb->dfn]->block_num);
115 /* Compute bb->dominators for each bblock */
116 for (i = 0; i < cfg->num_bblocks; ++i) {
117 MonoBasicBlock *bb = cfg->bblocks [i];
119 MonoBitSet *dominators;
122 mem = (char *)mono_mempool_alloc0 (cfg->mempool, bitsize);
124 bb->dominators = dominators = mono_bitset_mem_new (mem, cfg->num_bblocks, 0);
127 mono_bitset_set_fast (dominators, bb->dfn);
130 for (cbb = doms [bb->dfn]; cbb->dfn; cbb = doms [cbb->dfn])
131 mono_bitset_set_fast (dominators, cbb->dfn);
133 bb->idom = doms [bb->dfn];
135 bb->idom->dominated = g_slist_prepend_mempool (cfg->mempool, bb->idom->dominated, bb);
139 mono_bitset_set_fast (dominators, 0);
144 cfg->comp_done |= MONO_COMP_DOM | MONO_COMP_IDOM;
146 if (cfg->verbose_level > 1) {
147 printf ("DTREE %s %d\n", mono_method_full_name (cfg->method, TRUE),
148 cfg->header->num_clauses);
149 for (i = 0; i < cfg->num_bblocks; ++i) {
150 MonoBasicBlock *bb = cfg->bblocks [i];
151 printf ("BB%d(dfn=%d) (IDOM=BB%d): ", bb->block_num, bb->dfn, bb->idom ? bb->idom->block_num : -1);
152 mono_blockset_print (cfg, bb->dominators, NULL, -1);
160 check_dominance_frontier (MonoBasicBlock *x, MonoBasicBlock *t)
164 t->flags |= BB_VISITED;
166 if (mono_bitset_test_fast (t->dominators, x->dfn)) {
167 for (i = 0; i < t->out_count; ++i) {
168 if (!(t->flags & BB_VISITED)) {
170 check_dominance_frontier (x, t->out_bb [i]);
172 for (j = 0; j < t->out_bb [i]->in_count; j++) {
173 if (t->out_bb [i]->in_bb [j] == t)
180 if (!mono_bitset_test_fast (x->dfrontier, t->dfn)) {
181 printf ("BB%d not in frontier of BB%d\n", t->block_num, x->block_num);
182 g_assert_not_reached ();
190 * Compute dominance frontiers using the algorithm from the same paper.
193 compute_dominance_frontier (MonoCompile *cfg)
198 g_assert (!(cfg->comp_done & MONO_COMP_DFRONTIER));
200 for (i = 0; i < cfg->num_bblocks; ++i)
201 cfg->bblocks [i]->flags &= ~BB_VISITED;
203 bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
204 mem = (char *)mono_mempool_alloc0 (cfg->mempool, bitsize * cfg->num_bblocks);
206 for (i = 0; i < cfg->num_bblocks; ++i) {
207 MonoBasicBlock *bb = cfg->bblocks [i];
208 bb->dfrontier = mono_bitset_mem_new (mem, cfg->num_bblocks, 0);
212 for (i = 0; i < cfg->num_bblocks; ++i) {
213 MonoBasicBlock *bb = cfg->bblocks [i];
215 if (bb->in_count > 1) {
216 for (j = 0; j < bb->in_count; ++j) {
217 MonoBasicBlock *p = bb->in_bb [j];
219 if (p->dfn || (p == cfg->bblocks [0])) {
220 while (p != bb->idom) {
221 mono_bitset_set_fast (p->dfrontier, bb->dfn);
230 for (i = 0; i < cfg->num_bblocks; ++i) {
231 MonoBasicBlock *bb = cfg->bblocks [i];
233 printf ("DFRONT %s BB%d: ", mono_method_full_name (cfg->method, TRUE), bb->block_num);
234 mono_blockset_print (cfg, bb->dfrontier, NULL, -1);
239 /* this is a check for the dominator frontier */
240 for (i = 0; i < m->num_bblocks; ++i) {
241 MonoBasicBlock *x = m->bblocks [i];
243 mono_bitset_foreach_bit ((x->dfrontier), j, (m->num_bblocks)) {
244 MonoBasicBlock *w = m->bblocks [j];
246 /* x must not strictly dominates w */
247 if (mono_bitset_test_fast (w->dominators, x->dfn) && w != x)
248 g_assert_not_reached ();
250 for (k = 0; k < m->num_bblocks; ++k)
251 m->bblocks [k]->flags &= ~BB_VISITED;
253 check_dominance_frontier (x, x);
258 cfg->comp_done |= MONO_COMP_DFRONTIER;
262 df_set (MonoCompile *m, MonoBitSet* dest, MonoBitSet *set)
266 mono_bitset_foreach_bit (set, i, m->num_bblocks) {
267 mono_bitset_union_fast (dest, m->bblocks [i]->dfrontier);
272 mono_compile_iterated_dfrontier (MonoCompile *m, MonoBitSet *set)
275 int bitsize, count1, count2;
277 bitsize = mono_bitset_alloc_size (m->num_bblocks, 0);
278 result = mono_bitset_mem_new (mono_mempool_alloc0 (m->mempool, bitsize), m->num_bblocks, 0);
280 df_set (m, result, set);
281 count2 = mono_bitset_count (result);
284 df_set (m, result, result);
285 count2 = mono_bitset_count (result);
286 } while (count2 > count1);
292 mono_compile_dominator_info (MonoCompile *cfg, int dom_flags)
294 if ((dom_flags & MONO_COMP_DOM) && !(cfg->comp_done & MONO_COMP_DOM))
295 compute_dominators (cfg);
296 if ((dom_flags & MONO_COMP_DFRONTIER) && !(cfg->comp_done & MONO_COMP_DFRONTIER))
297 compute_dominance_frontier (cfg);
301 * code to detect loops and loop nesting level
304 mono_compute_natural_loops (MonoCompile *cfg)
307 MonoBitSet *in_loop_blocks;
310 g_assert (!(cfg->comp_done & MONO_COMP_LOOPS));
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];
316 for (j = 0; j < n->out_count; j++) {
317 MonoBasicBlock *h = n->out_bb [j];
318 /* check for single block loops */
320 h->loop_blocks = g_list_prepend_mempool (cfg->mempool, h->loop_blocks, h);
323 /* check for back-edge from n to h */
324 else if (n != h && mono_bitset_test_fast (n->dominators, h->dfn)) {
327 /* already in loop_blocks? */
328 if (h->loop_blocks && g_list_find (h->loop_blocks, n)) {
332 mono_bitset_clear_all (in_loop_blocks);
333 if (h->loop_blocks) {
336 for (l = h->loop_blocks; l; l = l->next) {
337 MonoBasicBlock *b = (MonoBasicBlock *)l->data;
339 mono_bitset_set_fast (in_loop_blocks, b->dfn);
342 todo = g_slist_prepend (NULL, n);
345 MonoBasicBlock *cb = (MonoBasicBlock *)todo->data;
346 todo = g_slist_delete_link (todo, todo);
348 if ((cb->dfn && mono_bitset_test_fast (in_loop_blocks, cb->dfn)) || (!cb->dfn && g_list_find (h->loop_blocks, cb)))
351 h->loop_blocks = g_list_prepend_mempool (cfg->mempool, h->loop_blocks, cb);
354 mono_bitset_set_fast (in_loop_blocks, cb->dfn);
356 for (k = 0; k < cb->in_count; k++) {
357 MonoBasicBlock *prev = cb->in_bb [k];
358 /* add all previous blocks */
359 if (prev != h && !((prev->dfn && mono_bitset_test_fast (in_loop_blocks, prev->dfn)) || (!prev->dfn && g_list_find (h->loop_blocks, prev)))) {
360 todo = g_slist_prepend (todo, prev);
365 /* add the header if not already there */
366 if (!((h->dfn && mono_bitset_test_fast (in_loop_blocks, h->dfn)) || (!h->dfn && g_list_find (h->loop_blocks, h)))) {
367 h->loop_blocks = g_list_prepend_mempool (cfg->mempool, h->loop_blocks, h);
373 mono_bitset_free (in_loop_blocks);
375 cfg->comp_done |= MONO_COMP_LOOPS;
377 /* Compute loop_body_start for each loop */
378 bb_indexes = g_new0 (int, cfg->num_bblocks);
382 for (i = 0, bb = cfg->bb_entry; bb; i ++, bb = bb->next_bb) {
384 bb_indexes [bb->dfn] = i;
387 for (i = 0; i < cfg->num_bblocks; ++i) {
388 if (cfg->bblocks [i]->loop_blocks) {
389 /* The loop body start is the first bblock in the order they will be emitted */
390 MonoBasicBlock *h = cfg->bblocks [i];
391 MonoBasicBlock *body_start = h;
394 for (l = h->loop_blocks; l; l = l->next) {
395 MonoBasicBlock *cb = (MonoBasicBlock *)l->data;
397 if (cb->dfn && bb_indexes [cb->dfn] < bb_indexes [body_start->dfn]) {
402 body_start->loop_body_start = 1;
407 if (cfg->verbose_level > 1) {
408 for (i = 0; i < cfg->num_bblocks; ++i) {
409 if (cfg->bblocks [i]->loop_blocks) {
410 MonoBasicBlock *h = (MonoBasicBlock *)cfg->bblocks [i]->loop_blocks->data;
412 printf ("LOOP START %d\n", h->block_num);
413 for (l = h->loop_blocks; l; l = l->next) {
414 MonoBasicBlock *cb = (MonoBasicBlock *)l->data;
415 printf ("\tBB%d %d %p\n", cb->block_num, cb->nesting, cb->loop_blocks);
423 clear_idominators (MonoCompile *cfg)
427 for (i = 0; i < cfg->num_bblocks; ++i) {
428 if (cfg->bblocks[i]->dominated) {
429 cfg->bblocks[i]->dominated = NULL;
433 cfg->comp_done &= ~MONO_COMP_IDOM;
437 clear_loops (MonoCompile *cfg)
441 for (i = 0; i < cfg->num_bblocks; ++i) {
442 cfg->bblocks[i]->nesting = 0;
443 cfg->bblocks[i]->loop_blocks = NULL;
446 cfg->comp_done &= ~MONO_COMP_LOOPS;
450 mono_free_loop_info (MonoCompile *cfg)
452 if (cfg->comp_done & MONO_COMP_IDOM)
453 clear_idominators (cfg);
454 if (cfg->comp_done & MONO_COMP_LOOPS)
458 #else /* DISABLE_JIT */
461 mono_free_loop_info (MonoCompile *cfg)
465 #endif /* DISABLE_JIT */