+ 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;
+ lv->hook_info.context = lv;
+ lv->hook_info.hook._hook_node_info = be_dump_liveness_block;
+
+ return lv;
+}
+
+void be_liveness_free(be_lv_t *lv)
+{
+ be_liveness_invalidate_sets(lv);
+ be_liveness_invalidate_chk(lv);
+
+ xfree(lv);
+}
+
+void be_liveness_remove(be_lv_t *lv, const ir_node *irn)
+{
+ 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);
+ }
+}
+
+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);
+ }
+}
+
+void be_liveness_update(be_lv_t *lv, ir_node *irn)
+{
+ be_liveness_remove(lv, irn);
+ be_liveness_introduce(lv, 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);
+
+ if (arch_irn_consider_in_reg_alloc(cls, op))
+ ir_nodeset_insert(nodeset, op);
+ }
+}
+
+
+
+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(lv, block, be_lv_state_end, node) {
+ if (!arch_irn_consider_in_reg_alloc(cls, node))
+ continue;
+
+ 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)
+{
+ const ir_node *bl = is_Block(pos) ? pos : 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
+ */
+ if (irn == pos)
+ return;
+
+ be_liveness_transfer(cls, irn, live);
+ }
+}
+
+static void collect_node(ir_node *irn, void *data)
+{
+ struct obstack *obst = (struct obstack*)data;
+ obstack_ptr_grow(obst, irn);
+}
+
+static void be_live_chk_compare(be_lv_t *lv, lv_chk_t *lvc)
+{
+ ir_graph *irg = lv->irg;
+
+ struct obstack obst;
+ ir_node **nodes;
+ ir_node **blocks;
+ int i, j;
+
+ obstack_init(&obst);
+
+ irg_block_walk_graph(irg, collect_node, NULL, &obst);
+ obstack_ptr_grow(&obst, NULL);
+ blocks = (ir_node**)obstack_finish(&obst);
+
+ irg_walk_graph(irg, collect_node, NULL, &obst);
+ obstack_ptr_grow(&obst, NULL);
+ nodes = (ir_node**)obstack_finish(&obst);
+
+ 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");