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