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