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