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