Support dtor sections on Mach-O.
[libfirm] / ir / ana / ircfscc.c
index a8219fa..25f9731 100644 (file)
  * @date      7.2002
  * @version   $Id$
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
-#ifdef HAVE_STRING_H
 #include <string.h>
-#endif
 
 #include "irloop_t.h"
 #include "irnode_t.h"
@@ -80,16 +76,14 @@ typedef struct scc_info {
 } scc_info;
 
 /** Allocate a new scc_info on the given obstack */
-static INLINE scc_info *new_scc_info(struct obstack *obst) {
-       scc_info *info = obstack_alloc(obst, sizeof(*info));
-       memset(info, 0, sizeof(*info));
-       return info;
+static inline scc_info *new_scc_info(struct obstack *obst) {
+       return OALLOCZ(obst, scc_info);
 }
 
 /**
  * Marks the node n to be on the stack.
  */
-static INLINE void mark_irn_in_stack(ir_node *n) {
+static inline void mark_irn_in_stack(ir_node *n) {
        scc_info *info = get_irn_link(n);
        info->in_stack = 1;
 }
@@ -97,7 +91,7 @@ static INLINE void mark_irn_in_stack(ir_node *n) {
 /**
  * Marks the node n to be not on the stack.
  */
-static INLINE void mark_irn_not_in_stack(ir_node *n) {
+static inline void mark_irn_not_in_stack(ir_node *n) {
        scc_info *info = get_irn_link(n);
        info->in_stack = 0;
 }
@@ -105,7 +99,7 @@ static INLINE void mark_irn_not_in_stack(ir_node *n) {
 /**
  * Returns whether node n is on the stack.
  */
-static INLINE int irn_is_in_stack(ir_node *n) {
+static inline int irn_is_in_stack(ir_node *n) {
        scc_info *info = get_irn_link(n);
        return info->in_stack;
 }
@@ -113,7 +107,7 @@ static INLINE int irn_is_in_stack(ir_node *n) {
 /**
  * Sets node n uplink value.
  */
-static INLINE void set_irn_uplink(ir_node *n, int uplink) {
+static inline void set_irn_uplink(ir_node *n, int uplink) {
        scc_info *info = get_irn_link(n);
        info->uplink = uplink;
 }
@@ -121,7 +115,7 @@ static INLINE void set_irn_uplink(ir_node *n, int uplink) {
 /**
  * Return node n uplink value.
  */
-static INLINE int get_irn_uplink(ir_node *n) {
+static inline int get_irn_uplink(ir_node *n) {
        scc_info *info = get_irn_link(n);
        return info->uplink;
 }
@@ -129,7 +123,7 @@ static INLINE int get_irn_uplink(ir_node *n) {
 /**
  * Sets node n dfn value.
  */
-static INLINE void set_irn_dfn(ir_node *n, int dfn) {
+static inline void set_irn_dfn(ir_node *n, int dfn) {
        scc_info *info = get_irn_link(n);
        info->dfn = dfn;
 }
@@ -137,7 +131,7 @@ static INLINE void set_irn_dfn(ir_node *n, int dfn) {
 /**
  * Returns node n dfn value.
  */
-static INLINE int get_irn_dfn(ir_node *n) {
+static inline int get_irn_dfn(ir_node *n) {
        scc_info *info = get_irn_link(n);
        return info->dfn;
 }
@@ -154,7 +148,7 @@ static int tos = 0;
 /**
  * Initializes the IR-node stack
  */
-static INLINE void init_stack(void) {
+static inline void init_stack(void) {
        if (stack) {
                ARR_RESIZE(ir_node *, stack, 1000);
        } else {
@@ -163,10 +157,16 @@ static INLINE void init_stack(void) {
        tos = 0;
 }
 
+static void finish_stack(void)
+{
+       DEL_ARR_F(stack);
+       stack = NULL;
+}
+
 /**
  * Push a node n onto the IR-node stack.
  */
-static INLINE void push(ir_node *n) {
+static inline void push(ir_node *n) {
        if (tos == ARR_LEN(stack)) {
                int nlen = ARR_LEN(stack) * 2;
                ARR_RESIZE(ir_node *, stack, nlen);
@@ -178,7 +178,7 @@ static INLINE void push(ir_node *n) {
 /**
  * Pop a node from the IR-node stack and return it.
  */
-static INLINE ir_node *pop(void) {
+static inline ir_node *pop(void) {
        ir_node *n = stack[--tos];
        mark_irn_not_in_stack(n);
        return n;
@@ -188,7 +188,7 @@ static INLINE ir_node *pop(void) {
  * The nodes from tos up to n belong to the current loop.
  * Removes them from the stack and adds them to the current loop.
  */
-static INLINE void pop_scc_to_loop(ir_node *n) {
+static inline void pop_scc_to_loop(ir_node *n) {
        ir_node *m;
 
        do {
@@ -233,7 +233,7 @@ static void close_loop(ir_loop *l) {
  * Removes and unmarks all nodes up to n from the stack.
  * The nodes must be visited once more to assign them to a scc.
  */
-static INLINE void pop_scc_unmark_visit(ir_node *n) {
+static inline void pop_scc_unmark_visit(ir_node *n) {
        ir_node *m;
 
        do {
@@ -252,32 +252,10 @@ static INLINE void pop_scc_unmark_visit(ir_node *n) {
  * The loop is allocated on the outermost_ir_graphs's obstack.
  */
 static ir_loop *new_loop(void) {
-       ir_loop *father, *son;
-
-       father = current_loop;
-
-       son = obstack_alloc(outermost_ir_graph->obst, sizeof(*son));
-       memset(son, 0, sizeof(*son));
-       son->kind     = k_ir_loop;
-       son->children = NEW_ARR_F(loop_element, 0);
-       son->n_nodes  = 0;
-       son->n_sons   = 0;
-       if (father) {
-               son->outer_loop = father;
-               add_loop_son(father, son);
-               son->depth = father->depth+1;
-               if (son->depth > max_loop_depth)
-                       max_loop_depth = son->depth;
-       } else {  /* The root loop */
-               son->outer_loop = son;
-               son->depth      = 0;
-       }
-
-#ifdef DEBUG_libfirm
-       son->loop_nr = get_irp_new_node_nr();
-       son->link    = NULL;
-#endif
+       ir_loop *father = current_loop;
+       ir_loop *son    = alloc_loop(father, outermost_ir_graph->obst);
 
+       if (son->depth > max_loop_depth) max_loop_depth = son->depth;
        current_loop = son;
        return father;
 }
@@ -293,7 +271,7 @@ static ir_loop *new_loop(void) {
  * Clear the backedges for all nodes.
  * Called from a walker.
  */
-static INLINE void init_node(ir_node *n, void *env) {
+static inline void init_node(ir_node *n, void *env) {
        struct obstack *obst = env;
        if (is_Block(n))
                set_irn_link(n, new_scc_info(obst));
@@ -303,7 +281,7 @@ static INLINE void init_node(ir_node *n, void *env) {
 /**
  * Initializes the common global settings for the scc algorithm
  */
-static INLINE void init_scc_common(void) {
+static inline void init_scc_common(void) {
        current_dfn   = 1;
        loop_node_cnt = 0;
        init_stack();
@@ -313,16 +291,21 @@ static INLINE void init_scc_common(void) {
  * Initializes the scc algorithm for the intraprocedural case.
  * Add scc info to every block node.
  */
-static INLINE void init_scc(ir_graph *irg, struct obstack *obst) {
+static inline void init_scc(ir_graph *irg, struct obstack *obst) {
        init_scc_common();
        irg_walk_graph(irg, init_node, NULL, obst);
 }
 
+static inline void finish_scc(void)
+{
+       finish_stack();
+}
+
 #ifdef INTERPROCEDURAL_VIEW
 /**
  * Initializes the scc algorithm for the interprocedural case.
  */
-static INLINE void init_ip_scc(struct obstack *obst) {
+static inline void init_ip_scc(struct obstack *obst) {
        init_scc_common();
        cg_walk(init_node, NULL, obst);
 
@@ -357,20 +340,23 @@ static int is_outermost_StartBlock(ir_node *n) {
 static int is_head(ir_node *n, ir_node *root) {
        int i, arity;
        int some_outof_loop = 0, some_in_loop = 0;
+       (void) root;
 
        assert(is_Block(n));
 
        if (!is_outermost_StartBlock(n)) {
-               arity = get_irn_arity(n);
+               arity = get_Block_n_cfgpreds(n);
                for (i = 0; i < arity; i++) {
-                       ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
-                       if (is_backedge(n, i)) continue;
+                       ir_node *pred = get_Block_cfgpred_block(n, i);
+                       /* ignore Bad control flow: it cannot happen */
+                       if (is_Bad(pred))
+                               continue;
+                       if (is_backedge(n, i))
+                               continue;
                        if (!irn_is_in_stack(pred)) {
                                some_outof_loop = 1;
                        } else {
-                               if (get_irn_uplink(pred) < get_irn_uplink(root)) {
-                                       assert(get_irn_uplink(pred) >= get_irn_uplink(root));
-                               }
+                               assert(get_irn_uplink(pred) >= get_irn_uplink(root));
                                some_in_loop = 1;
                        }
                }
@@ -381,7 +367,7 @@ static int is_head(ir_node *n, ir_node *root) {
 
 /**
  * Returns non-zero if n is possible loop head of an endless loop.
- * I.e., it is a Block, Phi or Filter node and has only predecessors
+ * I.e., it is a Block node and has only predecessors
  * within the loop.
  *
  * @param n     the block node to check
@@ -389,28 +375,29 @@ static int is_head(ir_node *n, ir_node *root) {
  */
 static int is_endless_head(ir_node *n, ir_node *root) {
        int i, arity;
-       int some_outof_loop = 0, some_in_loop = 0;
+       int none_outof_loop = 1, some_in_loop = 0;
+       (void) root;
 
        assert(is_Block(n));
        /* Test for legal loop header: Block, Phi, ... */
        if (!is_outermost_StartBlock(n)) {
-               arity = get_irn_arity(n);
+               arity = get_Block_n_cfgpreds(n);
                for (i = 0; i < arity; i++) {
-                       ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
-                       assert(pred);
+                       ir_node *pred = get_Block_cfgpred_block(n, i);
+                       /* ignore Bad control flow: it cannot happen */
+                       if (is_Bad(pred))
+                               continue;
                        if (is_backedge(n, i))
                                continue;
                        if (!irn_is_in_stack(pred)) {
-                               some_outof_loop = 1; //printf(" some out of loop ");
+                               none_outof_loop = 0;
                        } else {
-                               if (get_irn_uplink(pred) < get_irn_uplink(root)) {
-                                       assert(get_irn_uplink(pred) >= get_irn_uplink(root));
-                               }
+                               assert(get_irn_uplink(pred) >= get_irn_uplink(root));
                                some_in_loop = 1;
                        }
                }
        }
-       return !some_outof_loop && some_in_loop;
+       return none_outof_loop && some_in_loop;
 }
 
 /**
@@ -421,9 +408,12 @@ static int smallest_dfn_pred(ir_node *n, int limit) {
        int i, index = -2, min = -1;
 
        if (!is_outermost_StartBlock(n)) {
-               int arity = get_irn_arity(n);
+               int arity = get_Block_n_cfgpreds(n);
                for (i = 0; i < arity; i++) {
-                       ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
+                       ir_node *pred = get_Block_cfgpred_block(n, i);
+                       /* ignore Bad control flow: it cannot happen */
+                       if (is_Bad(pred))
+                               continue;
                        if (is_backedge(n, i) || !irn_is_in_stack(pred))
                                continue;
                        if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
@@ -442,9 +432,12 @@ static int largest_dfn_pred(ir_node *n) {
        int i, index = -2, max = -1;
 
        if (!is_outermost_StartBlock(n)) {
-               int arity = get_irn_arity(n);
+               int arity = get_Block_n_cfgpreds(n);
                for (i = 0; i < arity; i++) {
-                       ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
+                       ir_node *pred = get_Block_cfgpred_block(n, i);
+                       /* ignore Bad control flow: it cannot happen */
+                       if (is_Bad(pred))
+                               continue;
                        if (is_backedge(n, i) || !irn_is_in_stack(pred))
                                continue;
                        if (get_irn_dfn(pred) > max) {
@@ -502,10 +495,10 @@ static ir_node *find_tail(ir_node *n) {
                        /* A dead loop not reachable from Start. */
                        for (i = tos-2; i >= 0; --i) {
                                m = stack[i];
-                               if (is_endless_head (m, n)) {
+                               if (is_endless_head(m, n)) {
                                        res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
                                        if (res_index == -2)  /* no smallest dfn pred found. */
-                                               res_index = largest_dfn_pred (m);
+                                               res_index = largest_dfn_pred(m);
                                        break;
                                }
                                if (m == n) break;   /* It's not an unreachable loop, either. */
@@ -516,13 +509,13 @@ static ir_node *find_tail(ir_node *n) {
        assert(res_index > -2);
 
        set_backedge(m, res_index);
-       return is_outermost_StartBlock(n) ? NULL : get_nodes_block(skip_Proj(get_irn_n(m, res_index)));
+       return is_outermost_StartBlock(n) ? NULL : get_Block_cfgpred_block(m, res_index);
 }
 
 /**
  * returns non.zero if l is the outermost loop.
  */
-INLINE static int is_outermost_loop(ir_loop *l) {
+inline static int is_outermost_loop(ir_loop *l) {
        return l == get_loop_outer_loop(l);
 }
 
@@ -538,8 +531,7 @@ static void cfscc(ir_node *n) {
 
        assert(is_Block(n));
 
-       if (irn_visited(n)) return;
-       mark_irn_visited(n);
+       if (irn_visited_else_mark(n)) return;
 
        /* Initialize the node */
        set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
@@ -549,14 +541,17 @@ static void cfscc(ir_node *n) {
        push(n);
 
        if (!is_outermost_StartBlock(n)) {
-               int arity = get_irn_arity(n);
+               int arity = get_Block_n_cfgpreds(n);
 
                for (i = 0; i < arity; i++) {
                        ir_node *m;
 
                        if (is_backedge(n, i))
                                continue;
-                       m = get_nodes_block(skip_Proj(get_irn_n(n, i)));
+                       m = get_Block_cfgpred_block(n, i);
+                       /* ignore Bad control flow: it cannot happen */
+                       if (is_Bad(m))
+                               continue;
 
                        cfscc(m);
                        if (irn_is_in_stack(m)) {
@@ -613,7 +608,7 @@ static void cfscc(ir_node *n) {
 #endif
 
                        /* Remove the cfloop from the stack ... */
-                       pop_scc_unmark_visit (n);
+                       pop_scc_unmark_visit(n);
 
                        /* The current backedge has been marked, that is temporarily eliminated,
                           by find tail. Start the scc algorithm
@@ -635,28 +630,6 @@ static void cfscc(ir_node *n) {
        }
 }
 
-/**
- * Mature all loops by removing the flexible arrays of a loop.
- */
-static void mature_loops(ir_loop *loop) {
-       loop_element *new_children = DUP_ARR_D(loop_element, outermost_ir_graph->obst, loop->children);
-       DEL_ARR_F(loop->children);
-       loop->children = new_children;
-
-       if (loop->n_sons > 0) {
-               /* we have child loops, mature them */
-               int i;
-
-               for (i = ARR_LEN(new_children) - 1; i >= 0; --i) {
-                       loop_element child = new_children[i];
-
-                       if (*child.kind == k_ir_loop) {
-                               mature_loops(child.son);
-                       }
-               }
-       }
-}
-
 /* Constructs control flow backedge information for irg. */
 int construct_cf_backedges(ir_graph *irg) {
        ir_graph *rem = current_ir_graph;
@@ -688,18 +661,26 @@ int construct_cf_backedges(ir_graph *irg) {
                if (is_Block(el))
                        cfscc(el);
        }
+       finish_scc();
+       obstack_free(&temp, NULL);
 
        assert(head_rem == current_loop);
-       mature_loops(current_loop);
+       mature_loops(current_loop, irg->obst);
        set_irg_loop(irg, current_loop);
        set_irg_loopinfo_state(irg, loopinfo_cf_consistent);
        assert(get_irg_loop(irg)->kind == k_ir_loop);
 
-       obstack_free(&temp, NULL);
        current_ir_graph = rem;
        return max_loop_depth;
 }
 
+void assure_cf_loop(ir_graph *irg) {
+       irg_loopinfo_state state = get_irg_loopinfo_state(irg);
+
+       if (state != loopinfo_cf_consistent)
+               construct_cf_backedges(irg);
+}
+
 #ifdef INTERPROCEDURAL_VIEW
 int construct_ip_cf_backedges (void) {
        ir_graph *rem = current_ir_graph;