Prevent SSA construction from running into endless loops.
[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);
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);
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         if (is_Block_dead(bl))
475                 return;
476
477         (*n_blocks) ++;
478
479         memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
480         set_Block_ipostdom(bl, NULL);
481         set_Block_postdom_pre_num(bl, -1);
482         set_Block_postdom_depth(bl, -1);
483 }
484
485 /** temporary type used while constructing the dominator / post dominator tree. */
486 typedef struct tmp_dom_info {
487         ir_node *block;               /**< backlink */
488
489         struct tmp_dom_info *semi;    /**< semidominator */
490         struct tmp_dom_info *parent;
491         struct tmp_dom_info *label;   /**< used for LINK and EVAL */
492         struct tmp_dom_info *ancestor;/**< used for LINK and EVAL */
493         struct tmp_dom_info *dom;     /**< After step 3, if the semidominator of w is
494                                            its immediate dominator, then w->dom is the
495                                            immediate dominator of w.  Otherwise w->dom
496                                            is a vertex v whose number is smaller than
497                                            w and whose immediate dominator is also w's
498                                            immediate dominator. After step 4, w->dom
499                                            is the immediate dominator of w.  */
500         struct tmp_dom_info *bucket;  /**< set of vertices with same semidominator */
501 } tmp_dom_info;
502
503 /** Struct to pass info through walker. */
504 typedef struct {
505         tmp_dom_info *d;
506         int used;
507 } dom_env;
508
509
510 /**
511  * Walks Blocks along the out data structure.  If recursion started with
512  * Start block misses control dead blocks.
513  */
514 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
515                               tmp_dom_info *tdi_list, int *used, int n_blocks)
516 {
517         tmp_dom_info *tdi;
518         int i;
519
520         if (is_Block_dead(bl))
521                 return;
522
523         assert(is_Block(bl));
524         if (Block_block_visited(bl))
525           return;
526         mark_Block_block_visited(bl);
527         set_Block_dom_pre_num(bl, *used);
528
529         assert(*used < n_blocks);
530         tdi = &tdi_list[*used];
531         ++(*used);
532
533         tdi->semi     = tdi;
534         tdi->label    = tdi;
535         tdi->ancestor = NULL;
536         tdi->bucket   = NULL;
537         tdi->parent   = parent;
538         tdi->block    = bl;
539
540         /* Iterate */
541         for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
542                 ir_node *pred = get_Block_cfg_out_ka(bl, i);
543                 /* can happen for half-optimized dead code (I've seen this in student
544                    projects */
545                 if (!is_Block(pred))
546                         continue;
547
548                 init_tmp_dom_info(pred, tdi, tdi_list, used, n_blocks);
549         }
550 }
551
552 /**
553  * Walks Blocks along the control flow.  If recursion started with
554  * End block misses blocks in endless loops.
555  */
556 static void init_tmp_pdom_info(ir_node *bl, tmp_dom_info *parent,
557                                tmp_dom_info *tdi_list, int* used, int n_blocks)
558 {
559         tmp_dom_info *tdi;
560         int i;
561
562         if (is_Block_dead(bl))
563                 return;
564
565         assert(is_Block(bl));
566         if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
567           return;
568         mark_Block_block_visited(bl);
569         set_Block_postdom_pre_num(bl, *used);
570
571         assert(*used < n_blocks);
572         tdi = &tdi_list[*used];
573         ++(*used);
574
575         tdi->semi = tdi;
576         tdi->label = tdi;
577         tdi->ancestor = NULL;
578         tdi->bucket = NULL;
579         tdi->parent = parent;
580         tdi->block = bl;
581
582         /* Iterate */
583         for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i) {
584                 ir_node *pred = get_Block_cfgpred_block(bl, i);
585                 if (is_Bad(pred))
586                         continue;
587                 assert(is_Block(pred));
588                 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
589         }
590
591         /* Handle keep-alives. Note that the preprocessing
592            in init_construction() had already killed all
593            phantom keep-alive edges. All remaining block keep-alives
594            are really edges to endless loops.
595          */
596         if (bl == get_irg_end_block(current_ir_graph)) {
597                 ir_node *end = get_irg_end(current_ir_graph);
598
599                 for (i = get_irn_arity(end) - 1; i >= 0; --i) {
600                         ir_node *pred = get_irn_n(end, i);
601
602                         if (is_Block(pred))
603                                 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
604                 }
605         }
606 }
607
608 static void dom_compress(tmp_dom_info *v)
609 {
610         assert (v->ancestor);
611         if (v->ancestor->ancestor) {
612                 dom_compress (v->ancestor);
613                 if (v->ancestor->label->semi < v->label->semi) {
614                         v->label = v->ancestor->label;
615                 }
616                 v->ancestor = v->ancestor->ancestor;
617         }
618 }
619
620 /**
621  * if V is a root, return v, else return the vertex u, not being the
622  * root, with minimum u->semi on the path from v to its root.
623  */
624 inline static tmp_dom_info *dom_eval(tmp_dom_info *v)
625 {
626         if (!v->ancestor) return v;
627         dom_compress (v);
628         return v->label;
629 }
630
631 /** make V W's ancestor */
632 inline static void dom_link(tmp_dom_info *v, tmp_dom_info *w)
633 {
634         w->ancestor = v;
635 }
636
637 /**
638  * Walker: count the number of blocks and clears the dominance info
639  */
640 static void count_and_init_blocks_dom(ir_node *bl, void *env)
641 {
642         int *n_blocks = (int *) env;
643         if (is_Block_dead(bl))
644                 return;
645
646         (*n_blocks) ++;
647
648         memset(get_dom_info(bl), 0, sizeof(ir_dom_info));
649         set_Block_idom(bl, NULL);
650         set_Block_dom_pre_num(bl, -1);
651         set_Block_dom_depth(bl, -1);
652 }
653
654 /* Computes the dominator trees.  Sets a flag in irg to "dom_consistent".
655    If the control flow of the graph is changed this flag must be set to
656    "dom_inconsistent".  */
657 void compute_doms(ir_graph *irg)
658 {
659         ir_graph *rem = current_ir_graph;
660         int n_blocks, used, i, j;
661         tmp_dom_info *tdi_list;   /* Ein Golf? */
662
663         current_ir_graph = irg;
664
665         /* Update graph state */
666         assert(get_irg_phase_state(irg) != phase_building);
667         irg->dom_state = dom_consistent;
668
669         /* Count the number of blocks in the graph. */
670         n_blocks = 0;
671         irg_block_walk_graph(irg, count_and_init_blocks_dom, NULL, &n_blocks);
672
673         /* Memory for temporary information. */
674         tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
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_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 {
783         if (get_irg_dom_state(irg) != dom_consistent)
784                 compute_doms(irg);
785 }
786
787 void free_dom(ir_graph *irg)
788 {
789         /* Update graph state */
790         assert(get_irg_phase_state(irg) != phase_building);
791         irg->dom_state = dom_none;
792
793         /* With the implementation right now there is nothing to free,
794            but better call it anyways... */
795 }
796
797 /* Computes the post dominator trees.  Sets a flag in irg to "dom_consistent".
798    If the control flow of the graph is changed this flag must be set to
799    "dom_inconsistent".  */
800 void compute_postdoms(ir_graph *irg)
801 {
802         ir_graph *rem = current_ir_graph;
803         int n_blocks, used, i, j;
804         tmp_dom_info *tdi_list;
805
806         current_ir_graph = irg;
807
808         /* Update graph state */
809         assert(get_irg_phase_state(irg) != phase_building);
810         irg->pdom_state = dom_consistent;
811
812         /* Count the number of blocks in the graph. */
813         n_blocks = 0;
814         irg_block_walk_graph(irg, count_and_init_blocks_pdom, NULL, &n_blocks);
815
816         /* Memory for temporary information. */
817         tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
818
819         /* We need the out data structure. */
820         assure_irg_outs(irg);
821
822         /* this with a standard walker as passing the parent to the sons isn't
823            simple. */
824         used = 0;
825         inc_irg_block_visited(irg);
826         init_tmp_pdom_info(get_irg_end_block(irg), NULL, tdi_list, &used, n_blocks);
827         /* If not all blocks are reachable from End by cfg edges this assertion
828            fails.
829            assert(used == n_blocks && "Precondition for dom construction violated"); */
830         n_blocks = used;
831
832
833         for (i = n_blocks-1; i > 0; i--) {  /* Don't iterate the root, it's done. */
834                 int irn_arity;
835                 tmp_dom_info *w = &tdi_list[i];
836                 tmp_dom_info *v;
837
838                 /* Step 2 */
839                 irn_arity = get_Block_n_cfg_outs_ka(w->block);
840                 for (j = 0;  j < irn_arity;  j++) {
841                         ir_node *succ = get_Block_cfg_out_ka(w->block, j);
842                         tmp_dom_info *u;
843
844                         if (get_Block_postdom_pre_num (succ) == -1)
845                                 continue;    /* endless-loop */
846
847                         u = dom_eval (&tdi_list[get_Block_postdom_pre_num(succ)]);
848                         if (u->semi < w->semi) w->semi = u->semi;
849                 }
850                 /* Add w to w->semi's bucket.  w is in exactly one bucket, so
851                    buckets can be implemented as linked lists. */
852                 w->bucket = w->semi->bucket;
853                 w->semi->bucket = w;
854
855                 dom_link (w->parent, w);
856
857                 /* Step 3 */
858                 while (w->parent->bucket) {
859                         tmp_dom_info *u;
860                         v = w->parent->bucket;
861                         /* remove v from w->parent->bucket */
862                         w->parent->bucket = v->bucket;
863                         v->bucket = NULL;
864
865                         u = dom_eval(v);
866                         if (u->semi < v->semi)
867                                 v->dom = u;
868                         else
869                                 v->dom = w->parent;
870                 }
871         }
872         /* Step 4 */
873         tdi_list[0].dom = NULL;
874         set_Block_ipostdom(tdi_list[0].block, NULL);
875         set_Block_postdom_depth(tdi_list[0].block, 1);
876         for (i = 1;  i < n_blocks;  i++) {
877                 tmp_dom_info *w = &tdi_list[i];
878
879                 if (w->dom != w->semi) w->dom = w->dom->dom;
880                 set_Block_ipostdom(w->block, w->dom->block);
881                 set_Block_postdom_depth(w->block, get_Block_postdom_depth(w->dom->block) + 1);
882         }
883
884         /* clean up */
885         free(tdi_list);
886
887         /* Do a walk over the tree and assign the tree pre orders. */
888         {
889                 unsigned tree_pre_order = 0;
890                 postdom_tree_walk_irg(irg, assign_tree_postdom_pre_order,
891                         assign_tree_postdom_pre_order_max, &tree_pre_order);
892         }
893         current_ir_graph = rem;
894 }
895
896 void assure_postdoms(ir_graph *irg)
897 {
898         if (get_irg_postdom_state(irg) != dom_consistent)
899                 compute_postdoms(irg);
900 }
901
902 void free_postdom(ir_graph *irg)
903 {
904         /* Update graph state */
905         assert(get_irg_phase_state(irg) != phase_building);
906         irg->pdom_state = dom_none;
907
908         /* With the implementation right now there is nothing to free,
909            but better call it anyways... */
910 }