cleanup: Remove unnecessary #include "beirg.h".
[libfirm] / ir / be / beschedtrace.c
index 53189c7..2ad2581 100644 (file)
@@ -1,24 +1,45 @@
+/*
+ * Copyright (C) 1995-2011 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.
+ */
+
 /**
- * Implements a trace scheduler as presented in Muchnik[TM].
- * Originally implemented by Michael Beck.
- * @author Christian Wuerdig
- * @date   28.08.2006
- * @cvs-id $Id$
+ * @file
+ * @brief       Implements a trace scheduler as presented in Muchnik[TM].
+ * @author      Michael Beck
+ * @date        28.08.2006
  */
+#include "config.h"
 
 #include <stdlib.h>
 
 #include "iredges_t.h"
-
-#include "besched_t.h"
+#include "beirg.h"
+#include "besched.h"
 #include "belistsched.h"
-#include "benode_t.h"
+#include "benode.h"
+#include "belive.h"
+#include "bemodule.h"
 
 /* we need a special mark */
 static char _mark;
 #define MARK &_mark
 
-typedef struct _trace_irn {
+typedef struct trace_irn {
        sched_timestep_t delay;      /**< The delay for this node if already calculated, else 0. */
        sched_timestep_t etime;      /**< The earliest time of this node. */
        unsigned num_user;           /**< The number real users (mode datab) of this node */
@@ -28,22 +49,27 @@ typedef struct _trace_irn {
        unsigned is_root       : 1;  /**< is a root node of a block */
 } trace_irn_t;
 
-typedef struct _trace_env {
+typedef struct trace_env {
        trace_irn_t      *sched_info;               /**< trace scheduling information about the nodes */
-       const arch_env_t *arch_env;                 /**< the arch environment */
        sched_timestep_t curr_time;                 /**< current time of the scheduler */
-       void             *selector_env;             /**< the backend selector environment */
-       const list_sched_selector_t *selector;      /**< the actual backend selector */
        be_lv_t          *liveness;                 /**< The liveness for the irg */
        DEBUG_ONLY(firm_dbg_module_t *dbg;)
 } trace_env_t;
 
+/**
+ * Returns a random node from a nodeset
+ */
+static ir_node *get_nodeset_node(const ir_nodeset_t *nodeset)
+{
+       return ir_nodeset_first(nodeset);
+}
+
 /**
  * Returns non-zero if the node is a root node
  */
-static INLINE unsigned is_root_node(trace_env_t *env, ir_node *n)
+static inline unsigned is_root_node(trace_env_t *env, ir_node *n)
 {
-       int idx = get_irn_idx(n);
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        return env->sched_info[idx].is_root;
@@ -52,9 +78,9 @@ static INLINE unsigned is_root_node(trace_env_t *env, ir_node *n)
 /**
  * Mark a node as root node
  */
-static INLINE void mark_root_node(trace_env_t *env, ir_node *n)
+static inline void mark_root_node(trace_env_t *env, ir_node *n)
 {
-       int idx = get_irn_idx(n);
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        env->sched_info[idx].is_root = 1;
@@ -63,8 +89,9 @@ static INLINE void mark_root_node(trace_env_t *env, ir_node *n)
 /**
  * Get the current delay.
  */
-static INLINE sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n) {
-       int idx = get_irn_idx(n);
+static inline sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        return env->sched_info[idx].delay;
@@ -73,8 +100,9 @@ static INLINE sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n) {
 /**
  * Set the current delay.
  */
-static INLINE void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t delay) {
-       int idx = get_irn_idx(n);
+static inline void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t delay)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        env->sched_info[idx].delay = delay;
@@ -83,8 +111,9 @@ static INLINE void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t
 /**
  * Get the current etime.
  */
-static INLINE sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n) {
-       int idx = get_irn_idx(n);
+static inline sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        return env->sched_info[idx].etime;
@@ -93,8 +122,9 @@ static INLINE sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n) {
 /**
  * Set the current etime.
  */
-static INLINE void set_irn_etime(trace_env_t *env, ir_node *n, sched_timestep_t etime) {
-       int idx = get_irn_idx(n);
+static inline void set_irn_etime(trace_env_t *env, ir_node *n, sched_timestep_t etime)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        env->sched_info[idx].etime = etime;
@@ -103,8 +133,9 @@ static INLINE void set_irn_etime(trace_env_t *env, ir_node *n, sched_timestep_t
 /**
  * Get the number of users.
  */
-static INLINE unsigned get_irn_num_user(trace_env_t *env, ir_node *n) {
-       int idx = get_irn_idx(n);
+static inline unsigned get_irn_num_user(trace_env_t *env, ir_node *n)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        return env->sched_info[idx].num_user;
@@ -113,8 +144,9 @@ static INLINE unsigned get_irn_num_user(trace_env_t *env, ir_node *n) {
 /**
  * Set the number of users.
  */
-static INLINE void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_user) {
-       int idx = get_irn_idx(n);
+static inline void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_user)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        env->sched_info[idx].num_user = num_user;
@@ -123,8 +155,9 @@ static INLINE void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_u
 /**
  * Get the register difference.
  */
-static INLINE int get_irn_reg_diff(trace_env_t *env, ir_node *n) {
-       int idx = get_irn_idx(n);
+static inline int get_irn_reg_diff(trace_env_t *env, ir_node *n)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        return env->sched_info[idx].reg_diff;
@@ -133,8 +166,9 @@ static INLINE int get_irn_reg_diff(trace_env_t *env, ir_node *n) {
 /**
  * Set the register difference.
  */
-static INLINE void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff) {
-       int idx = get_irn_idx(n);
+static inline void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        env->sched_info[idx].reg_diff = reg_diff;
@@ -143,8 +177,9 @@ static INLINE void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff)
 /**
  * Get the pre-order position.
  */
-static INLINE int get_irn_preorder(trace_env_t *env, ir_node *n) {
-       int idx = get_irn_idx(n);
+static inline int get_irn_preorder(trace_env_t *env, ir_node *n)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        return env->sched_info[idx].preorder;
@@ -153,8 +188,9 @@ static INLINE int get_irn_preorder(trace_env_t *env, ir_node *n) {
 /**
  * Set the pre-order position.
  */
-static INLINE void set_irn_preorder(trace_env_t *env, ir_node *n, int pos) {
-       int idx = get_irn_idx(n);
+static inline void set_irn_preorder(trace_env_t *env, ir_node *n, int pos)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        env->sched_info[idx].preorder = pos;
@@ -163,8 +199,9 @@ static INLINE void set_irn_preorder(trace_env_t *env, ir_node *n, int pos) {
 /**
  * Get the pre-order position.
  */
-static INLINE unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n) {
-       int idx = get_irn_idx(n);
+static inline unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        return env->sched_info[idx].critical_path_len;
@@ -173,8 +210,9 @@ static INLINE unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n) {
 /**
  * Set the pre-order position.
  */
-static INLINE void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsigned len) {
-       int idx = get_irn_idx(n);
+static inline void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsigned len)
+{
+       unsigned const idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
        env->sched_info[idx].critical_path_len = len;
@@ -183,18 +221,25 @@ static INLINE void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsig
 /**
  * returns the exec-time for node n.
  */
-static sched_timestep_t exectime(trace_env_t *env, ir_node *n) {
+static sched_timestep_t exectime(trace_env_t *env, ir_node *n)
+{
+       (void) env;
        if (be_is_Keep(n) || is_Proj(n))
                return 0;
+#if 0
        if (env->selector->exectime)
                return env->selector->exectime(env->selector_env, n);
+#endif
        return 1;
 }
 
 /**
  * Calculates the latency for between two ops
  */
-static sched_timestep_t latency(trace_env_t *env, ir_node *pred, int pred_cycle, ir_node *curr, int curr_cycle) {
+static sched_timestep_t latency(trace_env_t *env, ir_node *pred, int pred_cycle, ir_node *curr, int curr_cycle)
+{
+       (void) pred_cycle;
+       (void) curr_cycle;
        /* a Keep hides a root */
        if (be_is_Keep(curr))
                return exectime(env, pred);
@@ -203,21 +248,24 @@ static sched_timestep_t latency(trace_env_t *env, ir_node *pred, int pred_cycle,
        if (is_Proj(curr))
                return 0;
 
+#if 0
        /* predecessors Proj's must be skipped */
        if (is_Proj(pred))
                pred = get_Proj_pred(pred);
 
        if (env->selector->latency)
                return env->selector->latency(env->selector_env, pred, pred_cycle, curr, curr_cycle);
+#endif
+
        return 1;
 }
 
 /**
  * Returns the number of users of a node having mode datab.
  */
-static int get_num_successors(ir_node *irn) {
+static int get_num_successors(ir_node *irn)
+{
        int sum = 0;
-       const ir_edge_t *edge;
 
        if (get_irn_mode(irn) == mode_T) {
                /* for mode_T nodes: count the users of all Projs */
@@ -245,20 +293,20 @@ static int get_num_successors(ir_node *irn) {
 /**
  * Returns the difference of regs_output - regs_input;
  */
-static int get_reg_difference(trace_env_t *env, ir_node *irn) {
+static int get_reg_difference(trace_env_t *env, ir_node *irn)
+{
        int num_out = 0;
        int num_in  = 0;
        int i;
        ir_node *block = get_nodes_block(irn);
 
        if (be_is_Call(irn)) {
-               /* we want calls prefered */
+               /* we want calls preferred */
                return -5;
        }
 
        if (get_irn_mode(irn) == mode_T) {
                /* mode_T nodes: num out regs == num Projs with mode datab */
-               const ir_edge_t *edge;
                foreach_out_edge(irn, edge) {
                        ir_node *proj = get_edge_src_irn(edge);
                        if (mode_is_datab(get_irn_mode(proj)))
@@ -272,10 +320,16 @@ static int get_reg_difference(trace_env_t *env, ir_node *irn) {
        for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
                ir_node *in = get_irn_n(irn, i);
 
-               if (! be_is_live_end(env->liveness, block, in) &&  /* if the value lives outside of block: do not count */
-                       mode_is_datab(get_irn_mode(in))             &&  /* must be data node */
-                       ! arch_irn_is(env->arch_env, in, ignore))       /* ignore "ignore" nodes :) */
-                       num_in++;
+               if (!mode_is_datab(get_irn_mode(in)))
+                       continue;
+
+               if (arch_irn_is_ignore(in))
+                       continue;
+
+               if (be_is_live_end(env->liveness, block, in))
+                       continue;
+
+               num_in++;
        }
 
        return num_out - num_in;
@@ -284,7 +338,8 @@ static int get_reg_difference(trace_env_t *env, ir_node *irn) {
 /**
  * descent into a dag and create a pre-order list.
  */
-static void descent(ir_node *root, ir_node *block, ir_node **list, trace_env_t *env, unsigned path_len) {
+static void descent(ir_node *root, ir_node *block, ir_node **list, trace_env_t *env, unsigned path_len)
+{
        int i;
 
        if (! is_Phi(root)) {
@@ -328,9 +383,8 @@ static void descent(ir_node *root, ir_node *block, ir_node **list, trace_env_t *
 /**
  * Returns non-zero if root is a root in the block block.
  */
-static int is_root(ir_node *root, ir_node *block) {
-       const ir_edge_t *edge;
-
+static int is_root(ir_node *root, ir_node *block)
+{
        foreach_out_edge(root, edge) {
                ir_node *succ = get_edge_src_irn(edge);
 
@@ -348,16 +402,20 @@ static int is_root(ir_node *root, ir_node *block) {
 /**
  * Performs initial block calculations for trace scheduling.
  */
-static void trace_preprocess_block(trace_env_t *env, ir_node *block) {
+static void trace_preprocess_block(trace_env_t *env, ir_node *block)
+{
        ir_node *root = NULL, *preord = NULL;
        ir_node *curr, *irn;
        int cur_pos;
-       const ir_edge_t *edge;
 
        /* First step: Find the root set. */
        foreach_out_edge(block, edge) {
                ir_node *succ = get_edge_src_irn(edge);
 
+               if (is_Anchor(succ)) {
+                       /* ignore a keep alive edge */
+                       continue;
+               }
                if (is_root(succ, block)) {
                        mark_root_node(env, succ);
                        set_irn_link(succ, root);
@@ -370,7 +428,7 @@ static void trace_preprocess_block(trace_env_t *env, ir_node *block) {
        /* Second step: calculate the pre-order list. */
        preord = NULL;
        for (curr = root; curr; curr = irn) {
-               irn = get_irn_link(curr);
+               irn = (ir_node*)get_irn_link(curr);
                DBG((env->dbg, LEVEL_2, "   DAG root %+F\n", curr));
                descent(curr, block, &preord, env, 0);
        }
@@ -379,10 +437,10 @@ static void trace_preprocess_block(trace_env_t *env, ir_node *block) {
        /* Third step: calculate the Delay. Note that our
        * list is now in pre-order, starting at root
        */
-       for (cur_pos = 0, curr = root; curr; curr = get_irn_link(curr), cur_pos++) {
+       for (cur_pos = 0, curr = root; curr; curr = (ir_node*)get_irn_link(curr), cur_pos++) {
                sched_timestep_t d;
 
-               if (arch_irn_class_is(env->arch_env, curr, branch)) {
+               if (is_cfop(curr)) {
                        /* assure, that branches can be executed last */
                        d = 0;
                }
@@ -416,8 +474,9 @@ static void trace_preprocess_block(trace_env_t *env, ir_node *block) {
 /**
  * This functions gets called after a node finally has been made ready.
  */
-static void trace_node_ready(void *data, ir_node *irn, ir_node *pred) {
-       trace_env_t *env = data;
+static void trace_node_ready(void *data, ir_node *irn, ir_node *pred)
+{
+       trace_env_t *env = (trace_env_t*)data;
        sched_timestep_t etime_p, etime;
 
        etime = env->curr_time;
@@ -434,9 +493,10 @@ static void trace_node_ready(void *data, ir_node *irn, ir_node *pred) {
 /**
  * Update the current time after irn has been selected.
  */
-static void trace_update_time(void *data, ir_node *irn) {
-       trace_env_t *env = data;
-       if (is_Phi(irn) || get_irn_opcode(irn) == iro_Start) {
+static void trace_update_time(void *data, ir_node *irn)
+{
+       trace_env_t *env = (trace_env_t*)data;
+       if (is_Phi(irn) || get_irn_opcode(irn) == beo_Start) {
                env->curr_time += get_irn_etime(env, irn);
        }
        else {
@@ -446,19 +506,20 @@ static void trace_update_time(void *data, ir_node *irn) {
 
 /**
  * Allocates memory and initializes trace scheduling environment.
- * @param birg   The backend irg object
+ * @param irg   The backend irg object
  * @return The environment
  */
-static trace_env_t *trace_init(const arch_env_t *arch_env, ir_graph *irg) {
-       trace_env_t *env = xcalloc(1, sizeof(*env));
+static trace_env_t *trace_init(ir_graph *irg)
+{
+       trace_env_t *env = XMALLOCZ(trace_env_t);
        int         nn   = get_irg_last_idx(irg);
 
-       env->arch_env   = arch_env;
        env->curr_time  = 0;
        env->sched_info = NEW_ARR_F(trace_irn_t, nn);
-       env->liveness   = be_liveness(irg);
+       env->liveness   = be_get_irg_liveness(irg);
        FIRM_DBG_REGISTER(env->dbg, "firm.be.sched.trace");
 
+       be_assure_live_chk(irg);
        memset(env->sched_info, 0, nn * sizeof(*(env->sched_info)));
 
        return env;
@@ -468,9 +529,9 @@ static trace_env_t *trace_init(const arch_env_t *arch_env, ir_graph *irg) {
  * Frees all memory allocated for trace scheduling environment.
  * @param env  The environment
  */
-static void trace_free(void *data) {
-       trace_env_t *env = data;
-       be_liveness_free(env->liveness);
+static void trace_free(void *data)
+{
+       trace_env_t *env = (trace_env_t*)data;
        DEL_ARR_F(env->sched_info);
        free(env);
 }
@@ -478,126 +539,122 @@ static void trace_free(void *data) {
 /**
  * Simple selector. Just assure that jumps are scheduled last.
  */
-static ir_node *basic_selection(const arch_env_t *arch_env, nodeset *ready_set) {
-       ir_node *irn = NULL;
-
+static ir_node *basic_selection(ir_nodeset_t *ready_set)
+{
        /* assure that branches and constants are executed last */
-       for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) {
-               if (! arch_irn_class_is(arch_env, irn, branch)) {
-                       nodeset_break(ready_set);
+       foreach_ir_nodeset(ready_set, irn, iter) {
+               if (!is_cfop(irn)) {
                        return irn;
                }
        }
 
        /* at last: schedule branches */
-       irn = nodeset_first(ready_set);
-       nodeset_break(ready_set);
-
-       return irn;
+       return get_nodeset_node(ready_set);
 }
 
 /**
 * The muchnik selector.
 */
-static ir_node *muchnik_select(void *block_env, nodeset *ready_set, nodeset *live_set)
+static ir_node *muchnik_select(void *block_env, ir_nodeset_t *ready_set)
 {
-       trace_env_t *env = block_env;
-       nodeset *mcands, *ecands;
+       trace_env_t *env = (trace_env_t*)block_env;
+       ir_nodeset_t mcands, ecands;
        sched_timestep_t max_delay = 0;
-       ir_node *irn;
 
        /* calculate the max delay of all candidates */
-       foreach_nodeset(ready_set, irn) {
+       foreach_ir_nodeset(ready_set, irn, iter) {
                sched_timestep_t d = get_irn_delay(env, irn);
 
                max_delay = d > max_delay ? d : max_delay;
        }
 
-       mcands = new_nodeset(8);
-       ecands = new_nodeset(8);
+       ir_nodeset_init_size(&mcands, 8);
+       ir_nodeset_init_size(&ecands, 8);
 
        /* build mcands and ecands */
-       foreach_nodeset(ready_set, irn) {
+       foreach_ir_nodeset(ready_set, irn, iter) {
                if (get_irn_delay(env, irn) == max_delay) {
-                       nodeset_insert(mcands, irn);
+                       ir_nodeset_insert(&mcands, irn);
                        if (get_irn_etime(env, irn) <= env->curr_time)
-                               nodeset_insert(ecands, irn);
+                               ir_nodeset_insert(&ecands, irn);
                }
        }
 
        /* select a node */
-       if (nodeset_count(mcands) == 1) {
-               irn = nodeset_first(mcands);
+       ir_node *irn;
+       if (ir_nodeset_size(&mcands) == 1) {
+               irn = get_nodeset_node(&mcands);
                DB((env->dbg, LEVEL_3, "\tirn = %+F, mcand = 1, max_delay = %u\n", irn, max_delay));
        }
        else {
-               int cnt = nodeset_count(ecands);
+               size_t cnt = ir_nodeset_size(&ecands);
                if (cnt == 1) {
-                       irn = nodeset_first(ecands);
+                       irn = get_nodeset_node(&ecands);
 
-                       if (arch_irn_class_is(env->arch_env, irn, branch)) {
+                       if (is_cfop(irn)) {
                                /* BEWARE: don't select a JUMP if others are still possible */
                                goto force_mcands;
                        }
                        DB((env->dbg, LEVEL_3, "\tirn = %+F, ecand = 1, max_delay = %u\n", irn, max_delay));
                }
                else if (cnt > 1) {
-                       DB((env->dbg, LEVEL_3, "\tecand = %d, max_delay = %u\n", cnt, max_delay));
-                       irn = basic_selection(env->arch_env, ecands);
+                       DB((env->dbg, LEVEL_3, "\tecand = %zu, max_delay = %u\n", cnt, max_delay));
+                       irn = basic_selection(&ecands);
                }
                else {
 force_mcands:
-                       DB((env->dbg, LEVEL_3, "\tmcand = %d\n", nodeset_count(mcands)));
-                       irn = basic_selection(env->arch_env, mcands);
+                       DB((env->dbg, LEVEL_3, "\tmcand = %zu\n", ir_nodeset_size(&mcands)));
+                       irn = basic_selection(&mcands);
                }
        }
 
        return irn;
 }
 
-static void *muchnik_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
+static void *muchnik_init_graph(ir_graph *irg)
 {
-       trace_env_t *env  = trace_init(arch_env, irg);
-       env->selector     = vtab;
-       env->selector_env = (void*) arch_env;
+       trace_env_t *env  = trace_init(irg);
        return (void *)env;
 }
 
 static void *muchnik_init_block(void *graph_env, ir_node *bl)
 {
-       trace_preprocess_block(graph_env, bl);
+       trace_env_t *env = (trace_env_t*) graph_env;
+       trace_preprocess_block(env, bl);
        return graph_env;
 }
 
-static const list_sched_selector_t muchnik_selector_struct = {
-       muchnik_init_graph,
-       muchnik_init_block,
-       muchnik_select,
-       NULL,                /* to_appear_in_schedule */
-       trace_node_ready,    /* node_ready */
-       trace_update_time,   /* node_selected */
-       NULL,                /* exectime */
-       NULL,                /* latency */
-       NULL,                /* finish_block */
-       trace_free           /* finish_graph */
-};
-
-const list_sched_selector_t *muchnik_selector = &muchnik_selector_struct;
+static void sched_muchnik(ir_graph *irg)
+{
+       static const list_sched_selector_t muchnik_selector = {
+               muchnik_init_graph,
+               muchnik_init_block,
+               muchnik_select,
+               trace_node_ready,    /* node_ready */
+               trace_update_time,   /* node_selected */
+               NULL,                /* finish_block */
+               trace_free           /* finish_graph */
+       };
+       be_list_sched_graph(irg, &muchnik_selector);
+}
 
 /**
  * Execute the heuristic function.
  */
-static ir_node *heuristic_select(void *block_env, nodeset *ns, nodeset *lv)
+static ir_node *heuristic_select(void *block_env, ir_nodeset_t *ns)
 {
-       trace_env_t *trace_env   = block_env;
-       ir_node     *irn, *cand  = NULL;
+       trace_env_t *trace_env   = (trace_env_t*)block_env;
+       ir_node     *cand        = NULL;
        int         max_prio     = INT_MIN;
        int         cur_prio     = INT_MIN;
-       int         cur_pressure = nodeset_count(lv);
-       int         reg_fact, cand_reg_fact;
+       int         reg_fact;
+       /* Note: register pressure calculation needs an overhaul, you need correct
+        * tracking for each register class indidually and weight by each class
+       int         cur_pressure = ir_nodeset_size(lv); */
+       int         cur_pressure = 1;
 
        /* prefer instructions which can be scheduled early */
-#define PRIO_TIME        8
+#define PRIO_TIME        3
        /* prefer instructions with lots of successors */
 #define PRIO_NUMSUCCS    8
        /* prefer instructions with long critical path */
@@ -610,14 +667,14 @@ static ir_node *heuristic_select(void *block_env, nodeset *ns, nodeset *lv)
 #define PRIO_CHG_PRESS   8
 
        /* priority based selection, heuristic inspired by mueller diss */
-       foreach_nodeset(ns, irn) {
+       foreach_ir_nodeset(ns, irn, iter) {
                /* make sure that branches are scheduled last */
-               if (! arch_irn_class_is(trace_env->arch_env, irn, branch)) {
+               if (!is_cfop(irn)) {
                        int rdiff = get_irn_reg_diff(trace_env, irn);
                        int sign  = rdiff < 0;
                        int chg   = (rdiff < 0 ? -rdiff : rdiff) << PRIO_CHG_PRESS;
 
-                       reg_fact = chg << cur_pressure;
+                       reg_fact = chg * cur_pressure;
                        if (reg_fact < chg)
                                reg_fact = INT_MAX - 2;
                        reg_fact = sign ? -reg_fact : reg_fact;
@@ -632,7 +689,6 @@ static ir_node *heuristic_select(void *block_env, nodeset *ns, nodeset *lv)
                        if (cur_prio > max_prio) {
                                cand          = irn;
                                max_prio      = cur_prio;
-                               cand_reg_fact = reg_fact;
                        }
 
                        DBG((trace_env->dbg, LEVEL_4, "checked NODE %+F\n", irn));
@@ -651,23 +707,29 @@ static ir_node *heuristic_select(void *block_env, nodeset *ns, nodeset *lv)
                DBG((trace_env->dbg, LEVEL_4, "heuristic selected %+F:\n", cand));
        }
        else {
-               cand = basic_selection(trace_env->arch_env, ns);
+               cand = basic_selection(ns);
        }
 
        return cand;
 }
 
-static const list_sched_selector_t heuristic_selector_struct = {
-       muchnik_init_graph,
-       muchnik_init_block,
-       heuristic_select,
-       NULL,                /* to_appear_in_schedule */
-       trace_node_ready,    /* node_ready */
-       trace_update_time,   /* node_selected */
-       NULL,                /* exectime */
-       NULL,                /* latency */
-       NULL,                /* finish_block */
-       trace_free           /* finish_graph */
-};
-
-const list_sched_selector_t *heuristic_selector = &heuristic_selector_struct;
+static void sched_heuristic(ir_graph *irg)
+{
+       static const list_sched_selector_t heuristic_selector = {
+               muchnik_init_graph,
+               muchnik_init_block,
+               heuristic_select,
+               trace_node_ready,    /* node_ready */
+               trace_update_time,   /* node_selected */
+               NULL,                /* finish_block */
+               trace_free           /* finish_graph */
+       };
+       be_list_sched_graph(irg, &heuristic_selector);
+}
+
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_trace)
+void be_init_sched_trace(void)
+{
+       be_register_scheduler("heur", sched_heuristic);
+       be_register_scheduler("muchnik", sched_muchnik);
+}