cf47222367d49e0aa6222ec67df1148744b7a250
[libfirm] / ir / ana / irdom.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irdom.c
4  * Purpose:     Construct and access dominator / post dominator tree.
5  * Author:      Goetz Lindenmaier
6  * Modified by: Michael Beck, Rubino Geiss
7  * Created:     2.2002
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2003 Universitaet Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #ifdef HAVE_MALLOC_H
18 #include <malloc.h>
19 #endif
20 #ifdef HAVE_ALLOCA_H
21 #include <alloca.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #endif
26
27 #include "irouts.h"
28
29 #include "xmalloc.h"
30 #include "irgwalk.h"
31 #include "irdom_t.h"
32 #include "irgraph_t.h"   /* To access state field. */
33 #include "irnode_t.h"
34 #include "ircons_t.h"
35 #include "array.h"
36 #include "iredges.h"
37
38
39 #define get_dom_info(bl)  (&(bl)->attr.block.dom)
40 #define get_pdom_info(bl) (&(bl)->attr.block.pdom)
41
42 /*--------------------------------------------------------------------*/
43 /** Accessing the dominator and post dominator data structures       **/
44 /*--------------------------------------------------------------------*/
45
46 ir_node *get_Block_idom(const ir_node *bl) {
47   assert(is_Block(bl));
48   if (get_Block_dom_depth(bl) == -1) {
49     /* This block is not reachable from Start */
50     return new_Bad();
51   }
52   return get_dom_info(bl)->idom;
53 }
54
55 void set_Block_idom(ir_node *bl, ir_node *n) {
56         dom_info *bli = get_dom_info(bl);
57
58   assert(get_irn_op(bl) == op_Block);
59
60         /* Set the immediate dominator of bl to n */
61         bli->idom = n;
62
63         /*
64          * If we don't set the root of the dominator tree
65          * Append bl to the dominates queue of n.
66          */
67         if(n != NULL) {
68                 dom_info *ni = get_dom_info(n);
69
70                 bli->next = ni->first;
71                 ni->first = bl;
72         }
73 }
74
75 ir_node *get_Block_ipostdom(const ir_node *bl) {
76   assert(is_Block(bl));
77   if (get_Block_postdom_depth(bl) == -1) {
78     /* This block is not reachable from Start */
79     return new_Bad();
80   }
81   return get_pdom_info(bl)->idom;
82 }
83
84 void set_Block_ipostdom(ir_node *bl, ir_node *n) {
85         dom_info *bli = get_pdom_info(bl);
86
87   assert(get_irn_op(bl) == op_Block);
88
89         /* Set the immediate post dominator of bl to n */
90         bli->idom = n;
91
92         /*
93          * If we don't set the root of the post dominator tree
94          * Append bl to the post dominates queue of n.
95          */
96         if(n != NULL) {
97                 dom_info *ni = get_pdom_info(n);
98
99                 bli->next = ni->first;
100                 ni->first = bl;
101         }
102 }
103
104 int get_Block_dom_pre_num(const ir_node *bl) {
105   assert(get_irn_op(bl) == op_Block);
106   return get_dom_info(bl)->pre_num;
107 }
108
109 void set_Block_dom_pre_num(ir_node *bl, int num) {
110   assert(get_irn_op(bl) == op_Block);
111   get_dom_info(bl)->pre_num = num;
112 }
113
114 int get_Block_dom_depth(const ir_node *bl) {
115   assert(get_irn_op(bl) == op_Block);
116   return get_dom_info(bl)->dom_depth;
117 }
118
119 void set_Block_dom_depth(ir_node *bl, int depth) {
120   assert(get_irn_op(bl) == op_Block);
121   get_dom_info(bl)->dom_depth = depth;
122 }
123
124
125 int get_Block_postdom_pre_num(const ir_node *bl) {
126   assert(get_irn_op(bl) == op_Block);
127   return get_pdom_info(bl)->pre_num;
128 }
129
130 void set_Block_postdom_pre_num(ir_node *bl, int num) {
131   assert(get_irn_op(bl) == op_Block);
132   get_pdom_info(bl)->pre_num = num;
133 }
134
135 int get_Block_postdom_depth(const ir_node *bl) {
136   assert(get_irn_op(bl) == op_Block);
137   return get_pdom_info(bl)->dom_depth;
138 }
139
140 void set_Block_postdom_depth(ir_node *bl, int depth) {
141   assert(get_irn_op(bl) == op_Block);
142   get_pdom_info(bl)->dom_depth = depth;
143 }
144
145 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
146 {
147         assert(is_Block(bl));
148         return get_dom_info(bl)->tree_pre_num;
149 }
150
151 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
152 {
153         assert(is_Block(bl));
154         return get_dom_info(bl)->max_subtree_pre_num;
155 }
156
157 unsigned get_Block_pdom_tree_pre_num(const ir_node *bl)
158 {
159         assert(is_Block(bl));
160         return get_pdom_info(bl)->tree_pre_num;
161 }
162
163 unsigned get_Block_pdom_max_subtree_pre_num(const ir_node *bl)
164 {
165         assert(is_Block(bl));
166         return get_pdom_info(bl)->max_subtree_pre_num;
167 }
168
169 /* Check, if a block dominates another block. */
170 int block_dominates(const ir_node *a, const ir_node *b)
171 {
172         const dom_info *ai, *bi;
173
174         if (is_Block(a) && is_Block(b)) {
175                 ai = get_dom_info(a);
176                 bi = get_dom_info(b);
177                 return bi->tree_pre_num - ai->tree_pre_num
178                         <= ai->max_subtree_pre_num - ai->tree_pre_num;
179         }
180
181         return 0;
182 }
183
184 /* Returns the smallest common dominator block of two nodes. */
185 ir_node *node_smallest_common_dominator(ir_node *a, ir_node *b)
186 {
187         ir_node *bl_a   = is_Block(a) ? a : get_nodes_block(a);
188         ir_node *bl_b   = is_Block(b) ? b : get_nodes_block(b);
189         ir_node *dom_bl = NULL;
190
191         /* Check if block of a dominates block of b */
192         if (block_dominates(bl_a, bl_b))
193                 dom_bl = bl_a;
194         /* Check if block of b dominates block of a */
195         else if (block_dominates(bl_b, bl_a))
196                 dom_bl = bl_b;
197         else {
198                 /* walk up dominator tree and search for first block dominating a and b */
199                 while (! dom_bl) {
200                         bl_a = get_Block_idom(bl_a);
201
202                         assert(! is_Bad(bl_a) && "block is dead?");
203
204                         if (block_dominates(bl_a, bl_b))
205                                 dom_bl = bl_a;
206                 }
207         }
208
209         return dom_bl;
210 }
211
212 /* Returns the smallest common dominator block of all users of a node. */
213 ir_node *node_users_smallest_common_dominator(ir_node *irn, int handle_phi)
214 {
215         int n, j, i = 0, success;
216         ir_node **user_blocks, *dom_bl;
217         const ir_edge_t *edge;
218
219         assert(! is_Block(irn) && "WRONG USAGE of node_users_smallest_common_dominator");
220         assert(edges_activated(get_irn_irg(irn)) && "need edges activated");
221
222         n = get_irn_n_edges(irn);
223
224         /* get array to hold all block of the node users */
225         NEW_ARR_A(ir_node *, user_blocks, n);
226         foreach_out_edge(irn, edge) {
227                 ir_node *src = get_edge_src_irn(edge);
228
229                 if (is_Phi(src) && handle_phi) {
230                         /* get the corresponding cfg predecessor block if phi handling requested */
231                         j  = get_edge_src_pos(edge);
232                         assert(j >= 0 && "kaputt");
233                         user_blocks[i++] = get_Block_cfgpred_block(get_nodes_block(src), j);
234                 }
235                 else
236                         user_blocks[i++] = is_Block(src) ? src : get_nodes_block(src);
237         }
238
239         assert(i == n && "get_irn_n_edges probably broken");
240
241         /* in case of only one user: return the block of the user */
242         if (n == 1)
243                 return user_blocks[0];
244
245         i = 0;
246         /* search the smallest block dominating all user blocks */
247         do {
248                 dom_bl  = node_smallest_common_dominator(user_blocks[i], user_blocks[i + 1]);
249                 success = 1;
250
251                 /* check if this block dominates all remaining blocks as well */
252                 for (j = i + 2; j < n; j++) {
253                         if (! block_dominates(dom_bl, user_blocks[j]))
254                                 success = 0;
255                 }
256
257                 if (success)
258                         break;
259
260                 /* inherit the dominator block of the first (i + 1) users */
261                 user_blocks[++i] = dom_bl;
262         } while (i < n - 1);
263
264         assert(success && "no block found dominating all users");
265
266         return dom_bl;
267 }
268
269
270 /* Get the first node in the list of nodes dominated by a given block. */
271 ir_node *get_Block_dominated_first(const ir_node *bl)
272 {
273         assert(is_Block(bl));
274         return get_dom_info(bl)->first;
275 }
276
277 /* Get the next node in a list of nodes which are dominated by some
278  * other node. */
279 ir_node *get_Block_dominated_next(const ir_node *bl)
280 {
281         assert(is_Block(bl));
282         return get_dom_info(bl)->next;
283 }
284
285 /* Check, if a block post dominates another block. */
286 int block_postdominates(const ir_node *a, const ir_node *b)
287 {
288         const dom_info *ai, *bi;
289
290         if (is_Block(a) && is_Block(b)) {
291                 ai = get_pdom_info(a);
292                 bi = get_pdom_info(b);
293                 return bi->tree_pre_num - ai->tree_pre_num
294                         <= ai->max_subtree_pre_num - ai->tree_pre_num;
295         }
296
297         return 0;
298 }
299
300 /* Get the first node in the list of nodes post dominated by a given block. */
301 ir_node *get_Block_postdominated_first(const ir_node *bl)
302 {
303         assert(is_Block(bl));
304         return get_pdom_info(bl)->first;
305 }
306
307 /* Get the next node in a list of nodes which are post dominated by some
308  * other node. */
309 ir_node *get_Block_postdominated_next(const ir_node *bl)
310 {
311         assert(is_Block(bl));
312         return get_pdom_info(bl)->next;
313 }
314
315 /* Visit all nodes in the dominator subtree of a given node. */
316 void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
317                 irg_walk_func *post, void *env)
318 {
319         ir_node *p;
320
321         if(pre)
322                 pre(bl, env);
323
324         dominates_for_each(bl, p) {
325                 dom_tree_walk(p, pre, post, env);
326         }
327
328         if(post)
329                 post(bl, env);
330 }
331
332 /* Visit all nodes in the post dominator subtree of a given node. */
333 void postdom_tree_walk(ir_node *bl, irg_walk_func *pre,
334                 irg_walk_func *post, void *env)
335 {
336         ir_node *p;
337
338         if(pre)
339                 pre(bl, env);
340
341         postdominates_for_each(bl, p) {
342                 postdom_tree_walk(p, pre, post, env);
343         }
344
345         if(post)
346                 post(bl, env);
347 }
348
349 /* Walk over the dominator tree of an irg starting at the root. */
350 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
351                 irg_walk_func *post, void *env)
352 {
353         /* The root of the dominator tree should be the Start block. */
354         ir_node *root = get_irg_start_block(irg);
355
356   assert(irg->dom_state == dom_consistent
357                         && "The dominators of the irg must be consistent");
358         assert(root && "The start block of the graph is NULL?");
359         assert(get_dom_info(root)->idom == NULL
360                         && "The start node in the graph must be the root of the dominator tree");
361         dom_tree_walk(root, pre, post, env);
362 }
363
364 /* Walk over the post dominator tree of an irg starting at the root. */
365 void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
366                 irg_walk_func *post, void *env)
367 {
368         /* The root of the dominator tree should be the End block. */
369         ir_node *root = get_irg_end_block(irg);
370
371         assert(irg->pdom_state == dom_consistent
372                         && "The dominators of the irg must be consistent");
373         assert(root && "The end block of the graph is NULL?");
374         assert(get_pdom_info(root)->idom == NULL
375                         && "The End block node in the graph must be the root of the post dominator tree");
376         postdom_tree_walk(root, pre, post, env);
377 }
378
379
380 static void assign_tree_dom_pre_order(ir_node *bl, void *data)
381 {
382         unsigned *num = data;
383         dom_info *bi = get_dom_info(bl);
384
385         bi->tree_pre_num = (*num)++;
386 }
387
388 static void assign_tree_dom_pre_order_max(ir_node *bl, void *data)
389 {
390         dom_info *bi = get_dom_info(bl);
391         ir_node *p;
392         unsigned max = 0;
393         unsigned children = 0;
394
395         for(p = bi->first; p; p = get_dom_info(p)->next) {
396                 unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
397                 max = max > max_p ? max : max_p;
398                 children++;
399         }
400
401         bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
402         assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
403 }
404
405 static void assign_tree_postdom_pre_order(ir_node *bl, void *data)
406 {
407         unsigned *num = data;
408         dom_info *bi = get_pdom_info(bl);
409
410         bi->tree_pre_num = (*num)++;
411 }
412
413 static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
414 {
415         dom_info *bi = get_pdom_info(bl);
416         ir_node *p;
417         unsigned max = 0;
418         unsigned children = 0;
419
420         for(p = bi->first; p; p = get_pdom_info(p)->next) {
421                 unsigned max_p = get_pdom_info(p)->max_subtree_pre_num;
422                 max = max > max_p ? max : max_p;
423                 children++;
424         }
425
426         bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
427         assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
428 }
429
430 /*--------------------------------------------------------------------*/
431 /*  Building and Removing the dominator data structure                */
432 /*--------------------------------------------------------------------*/
433
434 /**
435  * count the number of blocks and clears the post dominance info
436  */
437 static void count_and_init_blocks_pdom(ir_node *bl, void *env) {
438         int *n_blocks = (int *) env;
439         (*n_blocks) ++;
440
441         memset(get_pdom_info(bl), 0, sizeof(dom_info));
442         set_Block_ipostdom(bl, NULL);
443         set_Block_postdom_pre_num(bl, -1);
444         set_Block_postdom_depth(bl, -1);
445 }
446
447 /** temporary type used while constructing the dominator / post dominator tree. */
448 typedef struct tmp_dom_info {
449   ir_node *block;               /**< backlink */
450
451   struct tmp_dom_info *semi;    /**< semidominator */
452   struct tmp_dom_info *parent;
453   struct tmp_dom_info *label;   /**< used for LINK and EVAL */
454   struct tmp_dom_info *ancestor;/**< used for LINK and EVAL */
455   struct tmp_dom_info *dom;     /**< After step 3, if the semidominator of w is
456                                      its immediate dominator, then w->dom is the
457                                      immediate dominator of w.  Otherwise w->dom
458                                      is a vertex v whose number is smaller than
459                                      w and whose immediate dominator is also w's
460                                      immediate dominator. After step 4, w->dom
461                                      is the immediate dominator of w.  */
462   struct tmp_dom_info *bucket;  /**< set of vertices with same semidominator */
463 } tmp_dom_info;
464
465 /** Struct to pass info through walker. */
466 typedef struct {
467   tmp_dom_info *d;
468   int used;
469 } dom_env;
470
471
472 /**
473  * Walks Blocks along the out data structure.  If recursion started with
474  * Start block misses control dead blocks.
475  */
476 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
477                               tmp_dom_info *tdi_list, int *used, int n_blocks) {
478   tmp_dom_info *tdi;
479   int i;
480
481   assert(is_Block(bl));
482   if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
483     return;
484   mark_Block_block_visited(bl);
485   set_Block_dom_pre_num(bl, *used);
486
487   assert(*used < n_blocks);
488   tdi = &tdi_list[*used];
489   ++(*used);
490
491   tdi->semi     = tdi;
492   tdi->label    = tdi;
493   tdi->ancestor = NULL;
494   tdi->bucket   = NULL;
495   tdi->parent   = parent;
496   tdi->block    = bl;
497
498   /* Iterate */
499   for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
500     ir_node *pred = get_Block_cfg_out_ka(bl, i);
501     assert(is_Block(pred));
502     init_tmp_dom_info(pred, tdi, tdi_list, used, n_blocks);
503   }
504 }
505
506 /**
507  * Walks Blocks along the control flow.  If recursion started with
508  * End block misses blocks in endless loops.
509  */
510 static void init_tmp_pdom_info(ir_node *bl, tmp_dom_info *parent,
511                                tmp_dom_info *tdi_list, int* used, int n_blocks) {
512   tmp_dom_info *tdi;
513   int i;
514
515   assert(is_Block(bl));
516   if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
517     return;
518   mark_Block_block_visited(bl);
519   set_Block_postdom_pre_num(bl, *used);
520
521   assert(*used < n_blocks);
522   tdi = &tdi_list[*used];
523   ++(*used);
524
525   tdi->semi = tdi;
526   tdi->label = tdi;
527   tdi->ancestor = NULL;
528   tdi->bucket = NULL;
529   tdi->parent = parent;
530   tdi->block = bl;
531
532   /* Iterate */
533   for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i) {
534     ir_node *pred = get_Block_cfgpred_block(bl, i);
535     if (is_Bad(pred))
536       continue;
537     assert(is_Block(pred));
538     init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
539   }
540
541   /* Handle keep-alives. Note that the preprocessing
542      in init_construction() had already killed all
543      phantom keep-alive edges. All remaining block keep-alives
544      are really edges to endless loops.
545    */
546   if (bl == get_irg_end_block(current_ir_graph)) {
547     ir_node *end = get_irg_end(current_ir_graph);
548
549     for (i = get_irn_arity(end) - 1; i >= 0; --i) {
550       ir_node *pred = get_irn_n(end, i);
551
552       if (is_Block(pred))
553         init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
554     }
555   }
556 }
557
558 static void dom_compress(tmp_dom_info *v)
559 {
560   assert (v->ancestor);
561   if (v->ancestor->ancestor) {
562     dom_compress (v->ancestor);
563     if (v->ancestor->label->semi < v->label->semi) {
564       v->label = v->ancestor->label;
565     }
566     v->ancestor = v->ancestor->ancestor;
567   }
568 }
569
570 /**
571  * if V is a root, return v, else return the vertex u, not being the
572  * root, with minimum u->semi on the path from v to its root.
573  */
574 INLINE static tmp_dom_info *dom_eval (tmp_dom_info *v)
575 {
576   if (!v->ancestor) return v;
577   dom_compress (v);
578   return v->label;
579 }
580
581 /** make V W's ancestor */
582 INLINE static void dom_link(tmp_dom_info *v, tmp_dom_info *w)
583 {
584   w->ancestor = v;
585 }
586
587 /**
588  * Walker: count the number of blocks and clears the dominance info
589  */
590 static void count_and_init_blocks_dom(ir_node *bl, void *env) {
591   int *n_blocks = (int *) env;
592   (*n_blocks) ++;
593
594   memset(get_dom_info(bl), 0, sizeof(dom_info));
595   set_Block_idom(bl, NULL);
596   set_Block_dom_pre_num(bl, -1);
597   set_Block_dom_depth(bl, -1);
598 }
599
600 /**
601  * Initialize the dominance/postdominance construction:
602  *
603  * - count the number of blocks
604  * - clear the dominance info
605  * - remove Block-keepalives of live blocks to reduce
606  *   the number of "phantom" block edges
607  *
608  * @param irg  the graph
609  * @param pre  a walker function that will be called for every block in the graph
610  */
611 static int init_construction(ir_graph *irg, irg_walk_func *pre) {
612   ir_graph *rem = current_ir_graph;
613   ir_node *end;
614   int arity;
615   int n_blocks = 0;
616
617   current_ir_graph = irg;
618
619   /* this visits only the reachable blocks */
620   irg_block_walk(get_irg_end_block(irg), pre, NULL, &n_blocks);
621
622   /* now visit the unreachable (from End) Blocks and remove unnecessary keep-alives */
623   end   = get_irg_end(irg);
624   arity = get_End_n_keepalives(end);
625   if (arity) {    /* we have keep-alives */
626     ir_node **in;
627     int i, j;
628
629     NEW_ARR_A(ir_node *, in, arity);
630     for (i = j = 0; i < arity; i++) {
631       ir_node *pred = get_End_keepalive(end, i);
632
633       if (get_irn_op(pred) == op_Block) {
634         if (Block_not_block_visited(pred)) {
635           /* we found a endless loop */
636           dec_irg_block_visited(irg);
637           irg_block_walk(pred, pre, NULL, &n_blocks);
638         }
639         else
640           continue;
641       }
642       in[j++] = pred;
643     }
644     if (j != arity) {
645       /* we kill some Block keep-alives */
646       set_End_keepalives(end, j, in);
647       set_irg_outs_inconsistent(irg);
648     }
649   }
650
651   current_ir_graph = rem;
652   return n_blocks;
653 }
654
655
656 /* Computes the dominator trees.  Sets a flag in irg to "dom_consistent".
657    If the control flow of the graph is changed this flag must be set to
658    "dom_inconsistent".  */
659 void compute_doms(ir_graph *irg) {
660   ir_graph *rem = current_ir_graph;
661   int n_blocks, used, i, j;
662   tmp_dom_info *tdi_list;   /* Ein Golf? */
663
664   current_ir_graph = irg;
665
666   /* Update graph state */
667   assert(get_irg_phase_state(irg) != phase_building);
668   irg->dom_state = dom_consistent;
669
670   /* Count the number of blocks in the graph. */
671   n_blocks = init_construction(irg, count_and_init_blocks_dom);
672
673   /* Memory for temporary information. */
674   tdi_list = xcalloc(n_blocks, sizeof(tdi_list[0]));
675
676   /* We need the out data structure. */
677   assure_irg_outs(irg);
678
679   /* this with a standard walker as passing the parent to the sons isn't
680      simple. */
681   used = 0;
682   inc_irg_block_visited(irg);
683   init_tmp_dom_info(get_irg_start_block(irg), NULL, tdi_list, &used, n_blocks);
684   /* If not all blocks are reachable from Start by out edges this assertion
685      fails.
686      assert(used == n_blocks && "Precondition for dom construction violated"); */
687   assert(used <= n_blocks && "Precondition for dom construction violated");
688   n_blocks = used;
689
690
691   for (i = n_blocks-1; i > 0; i--) {  /* Don't iterate the root, it's done. */
692     int irn_arity;
693     tmp_dom_info *w = &tdi_list[i];
694     tmp_dom_info *v;
695
696     /* Step 2 */
697     irn_arity = get_irn_arity(w->block);
698     for (j = 0; j < irn_arity;  j++) {
699       ir_node *pred = get_Block_cfgpred_block(w->block, j);
700       tmp_dom_info *u;
701
702       if (is_Bad(pred) || (get_Block_dom_pre_num (pred) == -1))
703         continue;       /* control-dead */
704
705       u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
706       if (u->semi < w->semi) w->semi = u->semi;
707     }
708
709     /* handle keep-alives if we are at the end block */
710     if (w->block == get_irg_end_block(irg)) {
711       ir_node *end = get_irg_end(irg);
712
713       irn_arity = get_irn_arity(end);
714       for (j = 0; j < irn_arity;  j++) {
715         ir_node *pred = get_irn_n(end, j);
716         tmp_dom_info *u;
717
718         if (is_no_Block(pred) || get_Block_dom_pre_num(pred) == -1)
719           continue;     /* control-dead */
720
721         u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
722         if (u->semi < w->semi) w->semi = u->semi;
723       }
724     }
725
726     /* Add w to w->semi's bucket.  w is in exactly one bucket, so
727        buckets can been implemented as linked lists. */
728     w->bucket = w->semi->bucket;
729     w->semi->bucket = w;
730
731     dom_link (w->parent, w);
732
733     /* Step 3 */
734     while (w->parent->bucket) {
735       tmp_dom_info *u;
736       v = w->parent->bucket;
737       /* remove v from w->parent->bucket */
738       w->parent->bucket = v->bucket;
739       v->bucket = NULL;
740
741       u = dom_eval (v);
742       if (u->semi < v->semi)
743         v->dom = u;
744       else
745         v->dom = w->parent;
746     }
747   }
748   /* Step 4 */
749   tdi_list[0].dom = NULL;
750   set_Block_idom(tdi_list[0].block, NULL);
751   set_Block_dom_depth(tdi_list[0].block, 1);
752   for (i = 1; i < n_blocks;  i++) {
753     tmp_dom_info *w = &tdi_list[i];
754     int depth;
755
756     if (! w->dom)
757       continue; /* control dead */
758
759     if (w->dom != w->semi) w->dom = w->dom->dom;
760     set_Block_idom(w->block, w->dom->block);
761
762     /* blocks dominated by dead one's are still dead */
763     depth = get_Block_dom_depth(w->dom->block);
764     if (depth > 0)
765       ++depth;
766     set_Block_dom_depth(w->block, depth);
767   }
768
769   /* clean up */
770   free(tdi_list);
771
772   /* Do a walk over the tree and assign the tree pre orders. */
773   {
774     unsigned tree_pre_order = 0;
775     dom_tree_walk_irg(irg, assign_tree_dom_pre_order,
776       assign_tree_dom_pre_order_max, &tree_pre_order);
777   }
778   current_ir_graph = rem;
779 }
780
781 void assure_doms(ir_graph *irg) {
782   if (get_irg_dom_state(irg) != dom_consistent)
783     compute_doms(irg);
784 }
785
786 void free_dom(ir_graph *irg) {
787   /* Update graph state */
788   assert(get_irg_phase_state(current_ir_graph) != phase_building);
789   current_ir_graph->dom_state = dom_none;
790
791   /* With the implementation right now there is nothing to free,
792      but better call it anyways... */
793 }
794
795 /* Computes the post dominator trees.  Sets a flag in irg to "dom_consistent".
796    If the control flow of the graph is changed this flag must be set to
797    "dom_inconsistent".  */
798 void compute_postdoms(ir_graph *irg) {
799   ir_graph *rem = current_ir_graph;
800   int n_blocks, used, i, j;
801   tmp_dom_info *tdi_list;
802
803   current_ir_graph = irg;
804
805   /* Update graph state */
806   assert(get_irg_phase_state(irg) != phase_building);
807   irg->pdom_state = dom_consistent;
808
809   /* Count the number of blocks in the graph. */
810   n_blocks = init_construction(irg, count_and_init_blocks_pdom);
811
812   /* Memory for temporary information. */
813   tdi_list = xcalloc(n_blocks, sizeof(tdi_list[0]));
814
815   /* We need the out data structure. */
816   assure_irg_outs(irg);
817
818   /* this with a standard walker as passing the parent to the sons isn't
819      simple. */
820   used = 0;
821   inc_irg_block_visited(irg);
822   init_tmp_pdom_info(get_irg_end_block(irg), NULL, tdi_list, &used, n_blocks);
823   /* If not all blocks are reachable from End by cfg edges this assertion
824      fails.
825      assert(used == n_blocks && "Precondition for dom construction violated"); */
826   n_blocks = used;
827
828
829   for (i = n_blocks-1; i > 0; i--) {  /* Don't iterate the root, it's done. */
830     int irn_arity;
831     tmp_dom_info *w = &tdi_list[i];
832     tmp_dom_info *v;
833
834     /* Step 2 */
835     irn_arity = get_Block_n_cfg_outs_ka(w->block);
836     for (j = 0;  j < irn_arity;  j++) {
837       ir_node *succ = get_Block_cfg_out_ka(w->block, j);
838       tmp_dom_info *u;
839
840       if (get_Block_postdom_pre_num (succ) == -1)
841         continue;       /* endless-loop */
842
843       u = dom_eval (&tdi_list[get_Block_postdom_pre_num(succ)]);
844       if (u->semi < w->semi) w->semi = u->semi;
845     }
846     /* Add w to w->semi's bucket.  w is in exactly one bucket, so
847        buckets can be implemented as linked lists. */
848     w->bucket = w->semi->bucket;
849     w->semi->bucket = w;
850
851     dom_link (w->parent, w);
852
853     /* Step 3 */
854     while (w->parent->bucket) {
855       tmp_dom_info *u;
856       v = w->parent->bucket;
857       /* remove v from w->parent->bucket */
858       w->parent->bucket = v->bucket;
859       v->bucket = NULL;
860
861       u = dom_eval(v);
862       if (u->semi < v->semi)
863         v->dom = u;
864       else
865         v->dom = w->parent;
866     }
867   }
868   /* Step 4 */
869   tdi_list[0].dom = NULL;
870   set_Block_ipostdom(tdi_list[0].block, NULL);
871   set_Block_postdom_depth(tdi_list[0].block, 1);
872   for (i = 1;  i < n_blocks;  i++) {
873     tmp_dom_info *w = &tdi_list[i];
874
875     if (w->dom != w->semi) w->dom = w->dom->dom;
876     set_Block_ipostdom(w->block, w->dom->block);
877     set_Block_postdom_depth(w->block, get_Block_postdom_depth(w->dom->block) + 1);
878   }
879
880   /* clean up */
881   free(tdi_list);
882
883   /* Do a walk over the tree and assign the tree pre orders. */
884   {
885     unsigned tree_pre_order = 0;
886     postdom_tree_walk_irg(irg, assign_tree_postdom_pre_order,
887       assign_tree_postdom_pre_order_max, &tree_pre_order);
888   }
889   current_ir_graph = rem;
890 }
891
892 void assure_postdoms(ir_graph *irg) {
893   if (get_irg_postdom_state(irg) != dom_consistent)
894     compute_postdoms(irg);
895 }
896
897 void free_postdom(ir_graph *irg) {
898   /* Update graph state */
899   assert(get_irg_phase_state(current_ir_graph) != phase_building);
900   current_ir_graph->pdom_state = dom_none;
901
902   /* With the implementation right now there is nothing to free,
903      but better call it anyways... */
904 }