belive: Inline be_lv_remove() into its only caller.
[libfirm] / ir / be / belive.c
index bb1f804..383df75 100644 (file)
@@ -1,20 +1,6 @@
 /*
- * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
- *
  * This file is part of libFirm.
- *
- * This file may be distributed and/or modified under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation and appearing in the file LICENSE.GPL included in the
- * packaging of this file.
- *
- * Licensees holding valid libFirm Professional Edition licenses may use
- * this file in accordance with the libFirm Commercial License.
- * Agreement provided with the Software.
- *
- * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE.
+ * Copyright (C) 2012 University of Karlsruhe.
  */
 
 /**
 
 #include "iredges_t.h"
 #include "irgwalk.h"
-#include "irprintf_t.h"
+#include "irprintf.h"
 #include "irdump_t.h"
 #include "irnodeset.h"
 
 #include "absgraph.h"
-#include "statev.h"
-
+#include "statev_t.h"
+#include "be_t.h"
+#include "bearch.h"
 #include "beutil.h"
 #include "belive_t.h"
-#include "beirg.h"
 #include "besched.h"
 #include "bemodule.h"
-#include "bedump.h"
 
 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
 #define LV_STD_SIZE             64
 
-/**
- * Filter out some nodes for which we never need liveness.
- *
- * @param irn  the node t check
- * @return 0 if no liveness info is needed, 1 else
- */
-static inline int is_liveness_node(const ir_node *irn)
-{
-       switch (get_irn_opcode(irn)) {
-       case iro_Block:
-       case iro_Bad:
-       case iro_End:
-       case iro_Anchor:
-       case iro_NoMem:
-               return 0;
-       default:
-               return 1;
-       }
-}
-
 int (be_is_live_in)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
 {
        return _be_is_live_xxx(lv, block, irn, be_lv_state_in);
@@ -83,7 +48,7 @@ int (be_is_live_end)(const be_lv_t *lv, const ir_node *block, const ir_node *irn
        return _be_is_live_xxx(lv, block, irn, be_lv_state_end);
 }
 
-static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, unsigned idx)
+static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, const ir_node *node)
 {
        be_lv_info_t *payload = arr + 1;
 
@@ -96,16 +61,15 @@ static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, unsigned idx)
                return 0;
 
        do {
-               int md          = lo + ((hi - lo) >> 1);
-               unsigned md_idx = payload[md].node.idx;
+               int md           = lo + ((hi - lo) >> 1);
+               ir_node *md_node = payload[md].node.node;
 
-               if (idx > md_idx)
+               if (node > md_node)
                        lo = md + 1;
-               else if (idx < md_idx)
+               else if (node < md_node)
                        hi = md;
                else {
                        res = md;
-                       assert(payload[res].node.idx == idx);
                        break;
                }
 
@@ -122,18 +86,16 @@ be_lv_info_node_t *be_lv_get(const be_lv_t *li, const ir_node *bl,
        be_lv_info_node_t *res = NULL;
 
        stat_ev_tim_push();
-       irn_live = (be_lv_info_t*)ir_nodehashmap_get(&li->map, bl);
+       irn_live = ir_nodehashmap_get(be_lv_info_t, &li->map, bl);
        if (irn_live != NULL) {
-               unsigned idx = get_irn_idx(irn);
-
                /* Get the position of the index in the array. */
-               int pos = _be_liveness_bsearch(irn_live, idx);
+               int pos = _be_liveness_bsearch(irn_live, irn);
 
                /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
                be_lv_info_node_t *rec = &irn_live[pos + 1].node;
 
                /* Check, if the irn is in deed in the array. */
-               if (rec->idx == idx)
+               if (rec->node == irn)
                        res = rec;
        }
        stat_ev_tim_pop("be_lv_get");
@@ -144,23 +106,21 @@ be_lv_info_node_t *be_lv_get(const be_lv_t *li, const ir_node *bl,
 static be_lv_info_node_t *be_lv_get_or_set(be_lv_t *li, ir_node *bl,
                                            ir_node *irn)
 {
-       be_lv_info_t *irn_live = (be_lv_info_t*)ir_nodehashmap_get(&li->map, bl);
+       be_lv_info_t *irn_live = ir_nodehashmap_get(be_lv_info_t, &li->map, bl);
        if (irn_live == NULL) {
                irn_live = OALLOCNZ(&li->obst, be_lv_info_t, LV_STD_SIZE);
                irn_live[0].head.n_size = LV_STD_SIZE-1;
                ir_nodehashmap_insert(&li->map, bl, irn_live);
        }
 
-       unsigned idx = get_irn_idx(irn);
-
        /* Get the position of the index in the array. */
-       unsigned pos = _be_liveness_bsearch(irn_live, idx);
+       unsigned pos = _be_liveness_bsearch(irn_live, irn);
 
        /* Get the record in question. 1 must be added, since the first record contains information about the array and must be skipped. */
        be_lv_info_node_t *res = &irn_live[pos + 1].node;
 
        /* Check, if the irn is in deed in the array. */
-       if (res->idx != idx) {
+       if (res->node != irn) {
                be_lv_info_t *payload;
                unsigned n_members = irn_live[0].head.n_members;
                unsigned n_size    = irn_live[0].head.n_size;
@@ -188,84 +148,53 @@ static be_lv_info_node_t *be_lv_get_or_set(be_lv_t *li, ir_node *bl,
 
                ++irn_live[0].head.n_members;
 
-               res = &payload[pos].node;
-               res->idx    = idx;
-               res->flags  = 0;
+               res        = &payload[pos].node;
+               res->node  = irn;
+               res->flags = 0;
        }
 
        return res;
 }
 
+typedef struct lv_remove_walker_t {
+       be_lv_t       *lv;
+       ir_node const *irn;
+} lv_remove_walker_t;
+
 /**
  * Removes a node from the list of live variables of a block.
- * @return 1 if the node was live at that block, 0 if not.
  */
-static int be_lv_remove(be_lv_t *li, const ir_node *bl,
-                        const ir_node *irn)
+static void lv_remove_irn_walker(ir_node *const bl, void *const data)
 {
-       be_lv_info_t *irn_live = (be_lv_info_t*)ir_nodehashmap_get(&li->map, bl);
-
+       lv_remove_walker_t *const w        = (lv_remove_walker_t*)data;
+       ir_node      const *const irn      = w->irn;
+       be_lv_info_t       *const irn_live = ir_nodehashmap_get(be_lv_info_t, &w->lv->map, bl);
        if (irn_live != NULL) {
                unsigned n   = irn_live[0].head.n_members;
-               unsigned idx = get_irn_idx(irn);
-               unsigned pos = _be_liveness_bsearch(irn_live, idx);
+               unsigned pos = _be_liveness_bsearch(irn_live, irn);
                be_lv_info_t *payload  = irn_live + 1;
                be_lv_info_node_t *res = &payload[pos].node;
 
                /* The node is in deed in the block's array. Let's remove it. */
-               if (res->idx == idx) {
+               if (res->node == irn) {
                        unsigned i;
 
                        for (i = pos + 1; i < n; ++i)
                                payload[i - 1] = payload[i];
 
-                       payload[n - 1].node.idx   = 0;
+                       payload[n - 1].node.node  = NULL;
                        payload[n - 1].node.flags = 0;
 
                        --irn_live[0].head.n_members;
                        DBG((dbg, LEVEL_3, "\tdeleting %+F from %+F at pos %d\n", irn, bl, pos));
-                       return 1;
                }
        }
-
-       return 0;
-}
-
-/**
- * Mark a node as live-in in a block.
- */
-static inline void mark_live_in(be_lv_t *lv, ir_node *block, ir_node *irn)
-{
-       be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
-       DBG((dbg, LEVEL_2, "marking %+F live in at %+F\n", irn, block));
-       n->flags |= be_lv_state_in;
-}
-
-/**
- * Mark a node as live-out in a block.
- */
-static inline void mark_live_out(be_lv_t *lv, ir_node *block, ir_node *irn)
-{
-       be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
-       DBG((dbg, LEVEL_2, "marking %+F live out at %+F\n", irn, block));
-       n->flags |= be_lv_state_out | be_lv_state_end;
-}
-
-/**
- * Mark a node as live-end in a block.
- */
-static inline void mark_live_end(be_lv_t *lv, ir_node *block, ir_node *irn)
-{
-       be_lv_info_node_t *n = be_lv_get_or_set(lv, block, irn);
-       DBG((dbg, LEVEL_2, "marking %+F live end at %+F\n", irn, block));
-       n->flags |= be_lv_state_end;
 }
 
 static struct {
        be_lv_t  *lv;         /**< The liveness object. */
        ir_node  *def;        /**< The node (value). */
        ir_node  *def_block;  /**< The block of def. */
-       bitset_t *visited;    /**< A set were all visited blocks are recorded. */
 } re;
 
 /**
@@ -273,44 +202,35 @@ static struct {
  * transitively, i.e. if the block is not the block of the value's
  * definition, all predecessors are also marked live.
  * @param block The block to mark the value live out of.
- * @param is_true_out Is the node real out there or only live at the end
- * of the block.
+ * @param state The liveness bits to set, either end or end+out.
  */
-static void live_end_at_block(ir_node *block, int is_true_out)
+static void live_end_at_block(ir_node *const block, be_lv_state_t const state)
 {
-       be_lv_t *lv  = re.lv;
-       ir_node *def = re.def;
-       bitset_t *visited;
+       be_lv_info_node_t *const n      = be_lv_get_or_set(re.lv, block, re.def);
+       be_lv_state_t      const before = n->flags;
 
-       mark_live_end(lv, block, def);
-       if (is_true_out)
-               mark_live_out(lv, block, def);
+       assert(state == be_lv_state_end || state == (be_lv_state_end | be_lv_state_out));
+       DBG((dbg, LEVEL_2, "marking %+F live %s at %+F\n", re.def, state & be_lv_state_out ? "end+out" : "end", block));
+       n->flags |= state;
 
-       visited = re.visited;
-       if (!bitset_is_set(visited, get_irn_idx(block))) {
-               bitset_set(visited, get_irn_idx(block));
+       /* There is no need to recurse further, if we where here before (i.e., any
+        * live state bits were set before). */
+       if (before != be_lv_state_none)
+               return;
 
-               /*
-                * If this block is not the definition block, we have to go up
-                * further.
-                */
-               if (re.def_block != block) {
-                       int i;
+       /* Stop going up further, if this is the block of the definition. */
+       if (re.def_block == block)
+               return;
 
-                       mark_live_in(lv, block, def);
+       DBG((dbg, LEVEL_2, "marking %+F live in at %+F\n", re.def, block));
+       n->flags |= be_lv_state_in;
 
-                       for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i)
-                               live_end_at_block(get_Block_cfgpred_block(block, i), 1);
-               }
+       for (int i = get_Block_n_cfgpreds(block); i-- != 0;) {
+               ir_node *const pred_block = get_Block_cfgpred_block(block, i);
+               live_end_at_block(pred_block, be_lv_state_end | be_lv_state_out);
        }
 }
 
-typedef struct lv_remove_walker_t {
-       be_lv_t       *lv;
-       const ir_node *irn;
-} lv_remove_walker_t;
-
-
 /**
  * Liveness analysis for a value.
  * Compute the set of all blocks a value is live in.
@@ -318,10 +238,7 @@ typedef struct lv_remove_walker_t {
  */
 static void liveness_for_node(ir_node *irn)
 {
-       ir_node *def_block;
-
-       bitset_clear_all(re.visited);
-       def_block = get_nodes_block(irn);
+       ir_node *const def_block = get_nodes_block(irn);
 
        re.def       = irn;
        re.def_block = def_block;
@@ -351,7 +268,7 @@ static void liveness_for_node(ir_node *irn)
                 */
                if (is_Phi(use)) {
                        ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
-                       live_end_at_block(pred_block, 0);
+                       live_end_at_block(pred_block, be_lv_state_end);
                }
 
                /*
@@ -361,22 +278,18 @@ static void liveness_for_node(ir_node *irn)
                else if (def_block != use_block) {
                        int i;
 
-                       mark_live_in(re.lv, use_block, irn);
+                       be_lv_info_node_t *const n = be_lv_get_or_set(re.lv, use_block, irn);
+                       DBG((dbg, LEVEL_2, "marking %+F live in at %+F\n", irn, use_block));
+                       n->flags |= be_lv_state_in;
 
                        for (i = get_Block_n_cfgpreds(use_block) - 1; i >= 0; --i) {
                                ir_node *pred_block = get_Block_cfgpred_block(use_block, i);
-                               live_end_at_block(pred_block, 1);
+                               live_end_at_block(pred_block, be_lv_state_end | be_lv_state_out);
                        }
                }
        }
 }
 
-static void lv_remove_irn_walker(ir_node *bl, void *data)
-{
-       lv_remove_walker_t *w = (lv_remove_walker_t*)data;
-       be_lv_remove(w->lv, bl, w->irn);
-}
-
 /**
  * Walker, collect all nodes for which we want calculate liveness info
  * on an obstack.
@@ -390,7 +303,6 @@ static void collect_liveness_nodes(ir_node *irn, void *data)
 
 void be_liveness_compute_sets(be_lv_t *lv)
 {
-       ir_node **nodes;
        int       i;
        int       n;
 
@@ -402,16 +314,14 @@ void be_liveness_compute_sets(be_lv_t *lv)
        obstack_init(&lv->obst);
 
        n = get_irg_last_idx(lv->irg);
-       nodes = NEW_ARR_F(ir_node *, n);
-       memset(nodes, 0, sizeof(nodes[0]) * n);
+       ir_node **const nodes = NEW_ARR_FZ(ir_node*, n);
 
        /* inserting the variables sorted by their ID is probably
         * more efficient since the binary sorted set insertion
         * will not need to move around the data. */
        irg_walk_graph(lv->irg, NULL, collect_liveness_nodes, nodes);
 
-       re.lv      = lv;
-       re.visited = bitset_malloc(n);
+       re.lv = lv;
 
        for (i = 0; i < n; ++i) {
                if (nodes[i] != NULL)
@@ -419,8 +329,6 @@ void be_liveness_compute_sets(be_lv_t *lv)
        }
 
        DEL_ARR_F(nodes);
-       free(re.visited);
-       register_hook(hook_node_info, &lv->hook_info);
 
        be_timer_pop(T_LIVE);
 
@@ -438,7 +346,6 @@ void be_liveness_invalidate_sets(be_lv_t *lv)
 {
        if (!lv->sets_valid)
                return;
-       unregister_hook(hook_node_info, &lv->hook_info);
        obstack_free(&lv->obst, NULL);
        ir_nodehashmap_destroy(&lv->map);
        lv->sets_valid = false;
@@ -459,8 +366,6 @@ be_lv_t *be_liveness_new(ir_graph *irg)
        be_lv_t *lv = XMALLOCZ(be_lv_t);
 
        lv->irg = irg;
-       lv->hook_info.context = lv;
-       lv->hook_info.hook._hook_node_info = be_dump_liveness_block;
 
        return lv;
 }
@@ -493,10 +398,8 @@ void be_liveness_introduce(be_lv_t *lv, ir_node *irn)
 {
        /* Don't compute liveness information for non-data nodes. */
        if (lv->sets_valid && is_liveness_node(irn)) {
-               re.lv      = lv;
-               re.visited = bitset_malloc(get_irg_last_idx(lv->irg));
+               re.lv = lv;
                liveness_for_node(irn);
-               bitset_free(re.visited);
        }
 }
 
@@ -509,31 +412,17 @@ void be_liveness_update(be_lv_t *lv, ir_node *irn)
 void be_liveness_transfer(const arch_register_class_t *cls,
                           ir_node *node, ir_nodeset_t *nodeset)
 {
-       int i, arity;
-
        /* You should better break out of your loop when hitting the first phi
         * function. */
        assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
 
-       if (get_irn_mode(node) == mode_T) {
-               foreach_out_edge(node, edge) {
-                       ir_node *proj = get_edge_src_irn(edge);
-
-                       if (arch_irn_consider_in_reg_alloc(cls, proj)) {
-                               ir_nodeset_remove(nodeset, proj);
-                       }
-               }
-       } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
-               ir_nodeset_remove(nodeset, node);
-       }
-
-       arity = get_irn_arity(node);
-       for (i = 0; i < arity; ++i) {
-               ir_node *op = get_irn_n(node, i);
+       be_foreach_definition(node, cls, value, req,
+               ir_nodeset_remove(nodeset, value);
+       );
 
-               if (arch_irn_consider_in_reg_alloc(cls, op))
-                       ir_nodeset_insert(nodeset, op);
-       }
+       be_foreach_use(node, cls, in_req, op, op_req,
+               ir_nodeset_insert(nodeset, op);
+       );
 }
 
 
@@ -542,37 +431,22 @@ void be_liveness_end_of_block(const be_lv_t *lv,
                               const arch_register_class_t *cls,
                               const ir_node *block, ir_nodeset_t *live)
 {
-       int i;
-
        assert(lv->sets_valid && "live sets must be computed");
-       be_lv_foreach(lv, block, be_lv_state_end, i) {
-               ir_node *node = be_lv_get_irn(lv, block, i);
-               if (!arch_irn_consider_in_reg_alloc(cls, node))
-                       continue;
-
+       be_lv_foreach_cls(lv, block, be_lv_state_end, cls, node) {
                ir_nodeset_insert(live, node);
        }
 }
 
 
 
-void be_liveness_nodes_live_at(const be_lv_t *lv,
-                               const arch_register_class_t *cls,
-                               const ir_node *pos, ir_nodeset_t *live)
+void be_liveness_nodes_live_before(be_lv_t const *const lv, arch_register_class_t const *const cls, ir_node const *const pos, ir_nodeset_t *const live)
 {
-       const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
-       ir_node *irn;
-
+       ir_node *const bl = get_nodes_block(pos);
        be_liveness_end_of_block(lv, cls, bl, live);
        sched_foreach_reverse(bl, irn) {
-               /*
-                * If we encounter the node we want to insert the Perm after,
-                * exit immediately, so that this node is still live
-                */
+               be_liveness_transfer(cls, irn, live);
                if (irn == pos)
                        return;
-
-               be_liveness_transfer(cls, irn, live);
        }
 }