+
+/* Get the first node in the list of nodes dominated by a given block. */
+ir_node *get_Block_dominated_first(const ir_node *bl) {
+ assert(is_Block(bl));
+ return get_dom_info(bl)->first;
+}
+
+/* Get the next node in a list of nodes which are dominated by some
+ * other node. */
+ir_node *get_Block_dominated_next(const ir_node *bl) {
+ assert(is_Block(bl));
+ return get_dom_info(bl)->next;
+}
+
+/* Check, if a block post dominates another block. */
+int block_postdominates(const ir_node *a, const ir_node *b) {
+ const ir_dom_info *ai, *bi;
+
+ if (is_Block(a) && is_Block(b)) {
+ ai = get_pdom_info(a);
+ bi = get_pdom_info(b);
+ return bi->tree_pre_num - ai->tree_pre_num
+ <= ai->max_subtree_pre_num - ai->tree_pre_num;
+ }
+
+ return 0;
+}
+
+/* Check, if a block strictly dominates another block. */
+int block_strictly_postdominates(const ir_node *a, const ir_node *b) {
+ return (a != b) && block_postdominates(a, b);
+}
+
+
+/* Get the first node in the list of nodes post dominated by a given block. */
+ir_node *get_Block_postdominated_first(const ir_node *bl) {
+ assert(is_Block(bl));
+ return get_pdom_info(bl)->first;
+}
+
+/* Get the next node in a list of nodes which are post dominated by some
+ * other node. */
+ir_node *get_Block_postdominated_next(const ir_node *bl) {
+ assert(is_Block(bl));
+ return get_pdom_info(bl)->next;
+}
+
+/* Visit all nodes in the dominator subtree of a given node. */
+void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
+ irg_walk_func *post, void *env)
+{
+ ir_node *p;
+
+ if(pre)
+ pre(bl, env);
+
+ dominates_for_each(bl, p) {
+ dom_tree_walk(p, pre, post, env);
+ }
+
+ if(post)
+ post(bl, env);
+}
+
+/* Visit all nodes in the post dominator subtree of a given node. */
+void postdom_tree_walk(ir_node *bl, irg_walk_func *pre,
+ irg_walk_func *post, void *env)
+{
+ ir_node *p;
+
+ if(pre)
+ pre(bl, env);
+
+ postdominates_for_each(bl, p) {
+ postdom_tree_walk(p, pre, post, env);
+ }
+
+ if(post)
+ post(bl, env);
+}
+
+/* Walk over the dominator tree of an irg starting at the root. */
+void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
+ irg_walk_func *post, void *env)
+{
+ /* The root of the dominator tree should be the Start block. */
+ ir_node *root = get_irg_start_block(irg);
+
+ assert(irg->dom_state == dom_consistent
+ && "The dominators of the irg must be consistent");
+ assert(root && "The start block of the graph is NULL?");
+ assert(get_dom_info(root)->idom == NULL
+ && "The start node in the graph must be the root of the dominator tree");
+ dom_tree_walk(root, pre, post, env);
+}
+
+/* Walk over the post dominator tree of an irg starting at the root. */
+void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
+ irg_walk_func *post, void *env)
+{
+ /* The root of the dominator tree should be the End block. */
+ ir_node *root = get_irg_end_block(irg);
+
+ assert(irg->pdom_state == dom_consistent
+ && "The dominators of the irg must be consistent");
+ assert(root && "The end block of the graph is NULL?");
+ assert(get_pdom_info(root)->idom == NULL
+ && "The End block node in the graph must be the root of the post dominator tree");
+ postdom_tree_walk(root, pre, post, env);
+}
+
+
+static void assign_tree_dom_pre_order(ir_node *bl, void *data)
+{
+ unsigned *num = data;
+ ir_dom_info *bi = get_dom_info(bl);
+
+ bi->tree_pre_num = (*num)++;
+}
+
+static void assign_tree_dom_pre_order_max(ir_node *bl, void *data)
+{
+ ir_dom_info *bi = get_dom_info(bl);
+ ir_node *p;
+ unsigned max = 0;
+ unsigned children = 0;
+ (void) data;
+
+ for(p = bi->first; p; p = get_dom_info(p)->next) {
+ unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
+ max = max > max_p ? max : max_p;
+ children++;
+ }
+
+ bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
+ assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
+}
+
+static void assign_tree_postdom_pre_order(ir_node *bl, void *data)
+{
+ unsigned *num = data;
+ ir_dom_info *bi = get_pdom_info(bl);
+
+ bi->tree_pre_num = (*num)++;
+}
+
+static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
+{
+ ir_dom_info *bi = get_pdom_info(bl);
+ ir_node *p;
+ unsigned max = 0;
+ unsigned children = 0;
+ (void) data;
+
+ for(p = bi->first; p; p = get_pdom_info(p)->next) {
+ unsigned max_p = get_pdom_info(p)->max_subtree_pre_num;
+ max = max > max_p ? max : max_p;
+ children++;
+ }
+
+ bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
+ assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
+}
+
+/*--------------------------------------------------------------------*/
+/* Building and Removing the dominator data structure */
+/*--------------------------------------------------------------------*/
+
+/**
+ * count the number of blocks and clears the post dominance info
+ */
+static void count_and_init_blocks_pdom(ir_node *bl, void *env) {
+ int *n_blocks = (int *) env;
+ (*n_blocks) ++;
+
+ memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
+ set_Block_ipostdom(bl, NULL);
+ set_Block_postdom_pre_num(bl, -1);
+ set_Block_postdom_depth(bl, -1);
+}
+
+/** temporary type used while constructing the dominator / post dominator tree. */