cleanup: Remove duplicate MIN/MAX macros.
[libfirm] / ir / ana / domfront.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Algorithms for computing dominance frontiers.
9  * @author      Sebastian Hack, Daniel Grund
10  * @date        04.05.2005
11  */
12 #include "config.h"
13
14 #include "obst.h"
15 #include "pmap.h"
16 #include "irdom.h"
17 #include "array.h"
18 #include "irgraph.h"
19 #include "iredges_t.h"
20
21 /**
22  * A wrapper for get_Block_idom.
23  * This function returns the block itself, if the block is the start
24  * block. Returning NULL would make any != comparison true which
25  * suggests, that the start block is dominated by some other node.
26  * @param bl The block.
27  * @return The immediate dominator of the block.
28  */
29 static inline ir_node *get_idom(ir_node *bl)
30 {
31         ir_node *idom = get_Block_idom(bl);
32         return idom == NULL ? bl : idom;
33 }
34
35 /**
36  * Compute the dominance frontier for a given block.
37  *
38  * @param blk   the block where the calculation starts
39  *
40  * @return the list of all blocks in the dominance frontier of blk
41  */
42 static ir_node **compute_df(ir_node *blk, ir_dom_front_info_t *info)
43 {
44         ir_node *c;
45         ir_node **df_list = NEW_ARR_F(ir_node *, 0);
46
47         /* Add local dominance frontiers */
48         foreach_block_succ(blk, edge) {
49                 ir_node *y = get_edge_src_irn(edge);
50
51                 if (get_idom(y) != blk) {
52                         ARR_APP1(ir_node *, df_list, y);
53                 }
54         }
55
56         /*
57          * Go recursively down the dominance tree and add all blocks
58          * into the dominance frontiers of the children, which are not
59          * dominated by the given block.
60          */
61         for (c = get_Block_dominated_first(blk); c; c = get_Block_dominated_next(c)) {
62                 size_t i;
63                 ir_node **df_c_list = compute_df(c, info);
64
65                 for (i = ARR_LEN(df_c_list); i > 0;) {
66                         ir_node *w = df_c_list[--i];
67                         if (get_idom(w) != blk)
68                                 ARR_APP1(ir_node *, df_list, w);
69                 }
70         }
71
72         /* now copy the flexible array to the obstack */
73         ir_node **const df = DUP_ARR_D(ir_node*, &info->obst, df_list);
74         DEL_ARR_F(df_list);
75
76         pmap_insert(info->df_map, blk, df);
77         return df;
78 }
79
80 void ir_compute_dominance_frontiers(ir_graph *irg)
81 {
82         ir_dom_front_info_t *info = &irg->domfront;
83
84         assure_edges(irg);
85         obstack_init(&info->obst);
86         info->df_map = pmap_create();
87         assure_doms(irg);
88         compute_df(get_irg_start_block(irg), info);
89
90         add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
91 }
92
93 void ir_free_dominance_frontiers(ir_graph *irg)
94 {
95         clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
96
97         ir_dom_front_info_t *info = &irg->domfront;
98         if (info->df_map == NULL)
99                 return;
100
101         obstack_free(&info->obst, NULL);
102         pmap_destroy(info->df_map);
103         info->df_map = NULL;
104 }
105
106 /* Get the dominance frontier of a block. */
107 ir_node **ir_get_dominance_frontier(const ir_node *block)
108 {
109         ir_graph            *irg  = get_irn_irg(block);
110         ir_dom_front_info_t *info = &irg->domfront;
111         return pmap_get(ir_node*, info->df_map, block);
112 }