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