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