2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Algorithms for computing dominance frontiers.
9 * @author Sebastian Hack, Daniel Grund
19 #include "iredges_t.h"
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.
29 static inline ir_node *get_idom(ir_node *bl)
31 ir_node *idom = get_Block_idom(bl);
32 return idom == NULL ? bl : idom;
36 * Compute the dominance frontier for a given block.
38 * @param blk the block where the calculation starts
40 * @return the list of all blocks in the dominance frontier of blk
42 static ir_node **compute_df(ir_node *blk, ir_dom_front_info_t *info)
45 ir_node **df_list = NEW_ARR_F(ir_node *, 0);
47 /* Add local dominance frontiers */
48 foreach_block_succ(blk, edge) {
49 ir_node *y = get_edge_src_irn(edge);
51 if (get_idom(y) != blk) {
52 ARR_APP1(ir_node *, df_list, y);
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.
61 for (c = get_Block_dominated_first(blk); c; c = get_Block_dominated_next(c)) {
63 ir_node **df_c_list = compute_df(c, info);
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);
72 /* now copy the flexible array to the obstack */
73 ir_node **const df = DUP_ARR_D(ir_node*, &info->obst, df_list);
76 pmap_insert(info->df_map, blk, df);
80 void ir_compute_dominance_frontiers(ir_graph *irg)
82 ir_dom_front_info_t *info = &irg->domfront;
85 obstack_init(&info->obst);
86 info->df_map = pmap_create();
88 compute_df(get_irg_start_block(irg), info);
90 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
93 void ir_free_dominance_frontiers(ir_graph *irg)
95 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE_FRONTIERS);
97 ir_dom_front_info_t *info = &irg->domfront;
98 if (info->df_map == NULL)
101 obstack_free(&info->obst, NULL);
102 pmap_destroy(info->df_map);
106 /* Get the dominance frontier of a block. */
107 ir_node **ir_get_dominance_frontier(const ir_node *block)
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);