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