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