belive: Avoid duplicate map lookups when calculating the liveness of a node in a...
[libfirm] / ir / be / belive.c
index 368d8f1..12749a7 100644 (file)
+/*
+ * 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.
+ */
+
 /**
- * Interblock liveness analysis.
- * @author Sebastian Hack
- * @date 6.12.2004
+ * @file
+ * @brief       Interblock liveness analysis.
+ * @author      Sebastian Hack
+ * @date        06.12.2004
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
-#include "impl.h"
+/* statev is expensive here, only enable when needed */
+#define DISABLE_STATEV
+
 #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_t.h"
+#include "be_t.h"
+#include "bearch.h"
 #include "beutil.h"
 #include "belive_t.h"
+#include "besched.h"
+#include "bemodule.h"
 
-FIRM_IMPL2(is_live_in, int, const ir_node *, const ir_node *)
-FIRM_IMPL2(is_live_out, int, const ir_node *, const ir_node *)
-FIRM_IMPL2(is_live_end, int, const ir_node *, const ir_node *)
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
-/** The offset of the liveness information in a firm node. */
-size_t live_irg_data_offset = 0;
+#define LV_STD_SIZE             64
+
+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);
+}
 
-void be_liveness_init(void)
+int (be_is_live_out)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
 {
-  live_irg_data_offset = register_additional_graph_data(sizeof(irn_live_t));
+       return _be_is_live_xxx(lv, block, irn, be_lv_state_out);
 }
 
-static INLINE void mark_live_in(ir_node *block, const ir_node *irn)
+int (be_is_live_end)(const be_lv_t *lv, const ir_node *block, const ir_node *irn)
 {
-  _get_or_set_live(block, irn, live_state_in);
+       return _be_is_live_xxx(lv, block, irn, be_lv_state_end);
 }
 
-static INLINE void mark_live_out(ir_node *block, const ir_node *irn)
+static inline unsigned _be_liveness_bsearch(be_lv_info_t *arr, const ir_node *node)
 {
-  _get_or_set_live(block, irn, live_state_out | live_state_end);
+       be_lv_info_t *payload = arr + 1;
+
+       unsigned n   = arr[0].head.n_members;
+       unsigned res = 0;
+       int lo       = 0;
+       int hi       = n;
+
+       if (n == 0)
+               return 0;
+
+       do {
+               int md           = lo + ((hi - lo) >> 1);
+               ir_node *md_node = payload[md].node.node;
+
+               if (node > md_node)
+                       lo = md + 1;
+               else if (node < md_node)
+                       hi = md;
+               else {
+                       res = md;
+                       break;
+               }
+
+               res = lo;
+       } while (lo < hi);
+
+       return res;
 }
 
-static INLINE void mark_live_end(ir_node *block, const ir_node *irn)
+be_lv_info_node_t *be_lv_get(const be_lv_t *li, const ir_node *bl,
+                             const ir_node *irn)
 {
-  _get_or_set_live(block, irn, live_state_end);
+       be_lv_info_t *irn_live;
+       be_lv_info_node_t *res = NULL;
+
+       stat_ev_tim_push();
+       irn_live = ir_nodehashmap_get(be_lv_info_t, &li->map, bl);
+       if (irn_live != NULL) {
+               /* Get the position of the index in the array. */
+               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->node == irn)
+                       res = rec;
+       }
+       stat_ev_tim_pop("be_lv_get");
+
+       return res;
+}
+
+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 = 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);
+       }
+
+       /* Get the position of the index in the array. */
+       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->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;
+               unsigned i;
+
+               if (n_members + 1 >= n_size) {
+                       /* double the array size. Remember that the first entry is
+                        * metadata about the array and not a real array element */
+                       unsigned old_size_bytes  = (n_size + 1) * sizeof(irn_live[0]);
+                       unsigned new_size        = (2 * n_size) + 1;
+                       size_t   new_size_bytes  = new_size * sizeof(irn_live[0]);
+                       be_lv_info_t *nw = OALLOCN(&li->obst, be_lv_info_t, new_size);
+                       memcpy(nw, irn_live, old_size_bytes);
+                       memset(((char*) nw) + old_size_bytes, 0,
+                              new_size_bytes - old_size_bytes);
+                       nw[0].head.n_size = new_size - 1;
+                       irn_live = nw;
+                       ir_nodehashmap_insert(&li->map, bl, nw);
+               }
+
+               payload = &irn_live[1];
+               for (i = n_members; i > pos; --i) {
+                       payload[i] = payload[i - 1];
+               }
+
+               ++irn_live[0].head.n_members;
+
+               res        = &payload[pos].node;
+               res->node  = irn;
+               res->flags = 0;
+       }
+
+       return res;
 }
 
+/**
+ * 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)
+{
+       be_lv_info_t *irn_live = ir_nodehashmap_get(be_lv_info_t, &li->map, bl);
+
+       if (irn_live != NULL) {
+               unsigned n   = irn_live[0].head.n_members;
+               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->node == irn) {
+                       unsigned i;
+
+                       for (i = pos + 1; i < n; ++i)
+                               payload[i - 1] = payload[i];
+
+                       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;
+}
+
+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;
+
 /**
  * Mark a node (value) live out at a certain block. Do this also
  * transitively, i.e. if the block is not the block of the value's
  * definition, all predecessors are also marked live.
- * @param def The node (value).
  * @param block The block to mark the value live out of.
- * @param visited A set were all visited blocks are recorded.
  * @param is_true_out Is the node real out there or only live at the end
  * of the block.
  */
-static void live_end_at_block(ir_node *def, ir_node *block,
-    pset *visited, int is_true_out)
+static void live_end_at_block(ir_node *block, int is_true_out)
 {
-  mark_live_end(block, def);
-  if(is_true_out)
-    mark_live_out(block, def);
-
-  if(!pset_find_ptr(visited, block)) {
+       be_lv_info_node_t *const n = be_lv_get_or_set(re.lv, block, re.def);
 
-    pset_insert_ptr(visited, block);
+       DBG((dbg, LEVEL_2, "marking %+F live %s at %+F\n", re.def, is_true_out ? "end+out" : "end", block));
+       n->flags |= is_true_out ? be_lv_state_out | be_lv_state_end : be_lv_state_out;
 
-    /*
-     * If this block is not the definition block, we have to go up
-     * further.
-     */
-    if(get_nodes_block(def) != block) {
-      int i, n;
+       bitset_t *const visited = re.visited;
+       if (!bitset_is_set(visited, get_irn_idx(block))) {
+               bitset_set(visited, get_irn_idx(block));
 
-      mark_live_in(block, def);
+               /*
+                * If this block is not the definition block, we have to go up
+                * further.
+                */
+               if (re.def_block != block) {
+                       int i;
 
-      for(i = 0, n = get_irn_arity(block); i < n; ++i)
-        live_end_at_block(def, get_Block_cfgpred_block(block, i), visited, 1);
-    }
+                       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);
+               }
+       }
 }
 
+typedef struct lv_remove_walker_t {
+       be_lv_t       *lv;
+       const ir_node *irn;
+} lv_remove_walker_t;
+
+
 /**
  * Liveness analysis for a value.
- * This functions is meant to be called by a firm walker, to compute the
- * set of all blocks a value is live in.
- * @param irn The node (value).
- * @param env Ignored.
+ * Compute the set of all blocks a value is live in.
+ * @param irn     The node (value).
  */
-static void liveness_for_node(ir_node *irn, void *env)
-{
-  const ir_edge_t *edge;
-  ir_node *def_block;
-  pset *visited;
-
-  /* Don't compute liveness information for non-data nodes. */
-  if(!is_data_node(irn))
-    return;
-
-  visited = pset_new_ptr(512);
-  def_block = get_nodes_block(irn);
-
-  /* Go over all uses of the value */
-  foreach_out_edge(irn, edge) {
-    ir_node *use = edge->src;
-    ir_node *use_block;
-
-    /*
-     * If the usage is no data node, skip this use, since it does not
-     * affect the liveness of the node.
-     */
-    if(!is_data_node(use))
-      continue;
-
-    /* Get the block where the usage is in. */
-    use_block = get_nodes_block(use);
-
-    /*
-     * If the use is a phi function, determine the corresponding block
-     * through which the value reaches the phi function and mark the
-     * value as live out of that block.
-     */
-    if(is_Phi(use)) {
+static void liveness_for_node(ir_node *irn)
+{
+       ir_node *def_block;
+
+       bitset_clear_all(re.visited);
+       def_block = get_nodes_block(irn);
+
+       re.def       = irn;
+       re.def_block = def_block;
+
+       /* Go over all uses of the value */
+       foreach_out_edge(irn, edge) {
+               ir_node *use = edge->src;
+               ir_node *use_block;
+
+               DBG((dbg, LEVEL_4, "%+F: use at %+F, pos %d in %+F\n", irn, use, edge->pos, get_block(use)));
+               assert(get_irn_n(use, edge->pos) == irn);
+
+               /*
+                * If the usage is no data node, skip this use, since it does not
+                * affect the liveness of the node.
+                */
+               if (!is_liveness_node(use))
+                       continue;
+
+               /* Get the block where the usage is in. */
+               use_block = get_nodes_block(use);
+
+               /*
+                * If the use is a phi function, determine the corresponding block
+                * through which the value reaches the phi function and mark the
+                * value as live out of that block.
+                */
+               if (is_Phi(use)) {
                        ir_node *pred_block = get_Block_cfgpred_block(use_block, edge->pos);
-                       live_end_at_block(irn, pred_block, visited, 0);
-    }
+                       live_end_at_block(pred_block, 0);
+               }
 
-    /*
-     * Else, the value is live in at this block. Mark it and call live
-     * out on the predecessors.
-     */
-    else if(def_block != use_block) {
-      int i, n;
+               /*
+                * Else, the value is live in at this block. Mark it and call live
+                * out on the predecessors.
+                */
+               else if (def_block != use_block) {
+                       int i;
+
+                       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);
+                       }
+               }
+       }
+}
 
-      mark_live_in(use_block, irn);
+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);
+}
 
-      for(i = 0, n = get_irn_arity(use_block); i < n; ++i) {
-        ir_node *pred_block = get_nodes_block(get_irn_n(use_block, i));
-        live_end_at_block(irn, pred_block, visited, 1);
-      }
-    }
-  }
+/**
+ * Walker, collect all nodes for which we want calculate liveness info
+ * on an obstack.
+ */
+static void collect_liveness_nodes(ir_node *irn, void *data)
+{
+       ir_node **nodes = (ir_node**)data;
+       if (is_liveness_node(irn))
+               nodes[get_irn_idx(irn)] = irn;
+}
+
+void be_liveness_compute_sets(be_lv_t *lv)
+{
+       int       i;
+       int       n;
+
+       if (lv->sets_valid)
+               return;
+
+       be_timer_push(T_LIVE);
+       ir_nodehashmap_init(&lv->map);
+       obstack_init(&lv->obst);
+
+       n = get_irg_last_idx(lv->irg);
+       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);
+
+       for (i = 0; i < n; ++i) {
+               if (nodes[i] != NULL)
+                       liveness_for_node(nodes[i]);
+       }
+
+       DEL_ARR_F(nodes);
+       free(re.visited);
+
+       be_timer_pop(T_LIVE);
+
+       lv->sets_valid = true;
+}
+
+void be_liveness_compute_chk(be_lv_t *lv)
+{
+       if (lv->lvc != NULL)
+               return;
+       lv->lvc = lv_chk_new(lv->irg);
+}
+
+void be_liveness_invalidate_sets(be_lv_t *lv)
+{
+       if (!lv->sets_valid)
+               return;
+       obstack_free(&lv->obst, NULL);
+       ir_nodehashmap_destroy(&lv->map);
+       lv->sets_valid = false;
+}
+
+void be_liveness_invalidate_chk(be_lv_t *lv)
+{
+       be_liveness_invalidate_sets(lv);
+
+       if (lv->lvc == NULL)
+               return;
+       lv_chk_free(lv->lvc);
+       lv->lvc = NULL;
+}
+
+be_lv_t *be_liveness_new(ir_graph *irg)
+{
+       be_lv_t *lv = XMALLOCZ(be_lv_t);
+
+       lv->irg = irg;
 
-  del_pset(visited);
+       return lv;
 }
 
-static int cmp_irn_live(const void *a, const void *b, size_t size)
+void be_liveness_free(be_lv_t *lv)
 {
-  const irn_live_t *p = a;
-  const irn_live_t *q = b;
+       be_liveness_invalidate_sets(lv);
+       be_liveness_invalidate_chk(lv);
 
-  return !(p->block == q->block && p->irn == q->irn);
+       xfree(lv);
 }
 
-void be_liveness(ir_graph *irg)
+void be_liveness_remove(be_lv_t *lv, const ir_node *irn)
 {
-  irg_live_info_t *live_info = get_irg_live_info(irg);
-  if(live_info->live)
-    del_set(live_info->live);
+       if (lv->sets_valid) {
+               lv_remove_walker_t w;
+
+               /*
+                * Removes a single irn from the liveness information.
+                * Since an irn can only be live at blocks dominated by the block of its
+                * definition, we only have to process that dominance subtree.
+                */
+               w.lv  = lv;
+               w.irn = irn;
+               dom_tree_walk(get_nodes_block(irn), lv_remove_irn_walker, NULL, &w);
+       }
+}
 
-       ir_fprintf(stderr, "%F\n", irg);
+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));
+               liveness_for_node(irn);
+               bitset_free(re.visited);
+       }
+}
 
-  live_info->live = new_set(cmp_irn_live, 8192);
-  irg_walk_graph(irg, liveness_for_node, NULL, NULL);
+void be_liveness_update(be_lv_t *lv, ir_node *irn)
+{
+       be_liveness_remove(lv, irn);
+       be_liveness_introduce(lv, irn);
 }
 
-static void dump_liveness_walker(ir_node *bl, void *data)
+void be_liveness_transfer(const arch_register_class_t *cls,
+                          ir_node *node, ir_nodeset_t *nodeset)
 {
-       FILE *f = data;
-       const irn_live_t *li;
-       char buf[64];
+       /* 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");
+
+       be_foreach_definition(node, cls, value, req,
+               ir_nodeset_remove(nodeset, value);
+       );
+
+       be_foreach_use(node, cls, in_req, op, op_req,
+               ir_nodeset_insert(nodeset, op);
+       );
+}
 
-       ir_fprintf(f, "%+F\n", bl);
-       live_foreach(bl, li) {
-               strcpy(buf, "");
 
-               if(live_is_in(li))
-                       strcat(buf, "in ");
 
-               if(live_is_end(li))
-                       strcat(buf, "end ");
+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)
+{
+       assert(lv->sets_valid && "live sets must be computed");
+       be_lv_foreach_cls(lv, block, be_lv_state_end, cls, node) {
+               ir_nodeset_insert(live, node);
+       }
+}
+
 
-               if(live_is_out(li))
-                       strcat(buf, "out ");
 
-               ir_fprintf(f, "\t%+20F %s\n", li->irn, buf);
+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)
+{
+       ir_node const *const bl = get_nodes_block(pos);
+       be_liveness_end_of_block(lv, cls, bl, live);
+       sched_foreach_reverse(bl, irn) {
+               be_liveness_transfer(cls, irn, live);
+               if (irn == pos)
+                       return;
        }
 }
 
-void be_liveness_dump(ir_graph *irg, FILE *f)
+static void collect_node(ir_node *irn, void *data)
 {
-       irg_block_walk_graph(irg, dump_liveness_walker, NULL, f);
+       struct obstack *obst = (struct obstack*)data;
+       obstack_ptr_grow(obst, irn);
 }
 
-static void dom_check(ir_node *irn, void *data)
+static void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc)
 {
-       if(!is_block(irn)) {
-               int i, n;
-               ir_node *bl = get_nodes_block(irn);
+       ir_graph *irg    = lv->irg;
+
+       struct obstack obst;
+       ir_node **nodes;
+       ir_node **blocks;
+       int i, j;
+
+       obstack_init(&obst);
 
-               for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
-                       ir_node *op     = get_irn_n(irn, i);
-                       ir_node *def_bl = get_nodes_block(op);
-                       ir_node *use_bl = bl;
+       irg_block_walk_graph(irg, collect_node, NULL, &obst);
+       obstack_ptr_grow(&obst, NULL);
+       blocks = (ir_node**)obstack_finish(&obst);
 
-                       if(is_Phi(irn))
-                               use_bl = get_Block_cfgpred_block(bl, i);
+       irg_walk_graph(irg, collect_node, NULL, &obst);
+       obstack_ptr_grow(&obst, NULL);
+       nodes = (ir_node**)obstack_finish(&obst);
 
-                       if(!block_dominates(def_bl, use_bl))
-                               ir_fprintf(stderr, "In %+F(%d:%+Fr): %+F must dominate %+F\n",
-                                               irn, i, op, def_bl, use_bl);
+       stat_ev_ctx_push("be_lv_chk_compare");
+       for (j = 0; nodes[j]; ++j) {
+               ir_node *irn = nodes[j];
+               if (is_Block(irn))
+                       continue;
+
+               for (i = 0; blocks[i]; ++i) {
+                       ir_node *bl = blocks[i];
+                       int lvr_in  = be_is_live_in (lv, bl, irn);
+                       int lvr_out = be_is_live_out(lv, bl, irn);
+                       int lvr_end = be_is_live_end(lv, bl, irn);
+
+                       int lvc_in  = lv_chk_bl_in (lvc, bl, irn);
+                       int lvc_out = lv_chk_bl_out(lvc, bl, irn);
+                       int lvc_end = lv_chk_bl_end(lvc, bl, irn);
+
+                       if (lvr_in - lvc_in != 0)
+                               ir_fprintf(stderr, "live in  info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_in, lvc_in);
+
+                       if (lvr_end - lvc_end != 0)
+                               ir_fprintf(stderr, "live end info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_end, lvc_end);
+
+                       if (lvr_out - lvc_out != 0)
+                               ir_fprintf(stderr, "live out info for %+F at %+F differs: nml: %d, chk: %d\n", irn, bl, lvr_out, lvc_out);
                }
        }
+       stat_ev_ctx_pop("be_lv_chk_compare");
+
+       obstack_free(&obst, NULL);
 }
 
-void be_check_dominance(ir_graph *irg)
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_live)
+void be_init_live(void)
 {
-       irg_walk_graph(irg, dom_check, NULL, NULL);
+       (void)be_live_chk_compare;
+       FIRM_DBG_REGISTER(dbg, "firm.be.liveness");
 }