Start block isn't a special case anymore (and now get the old node nr).
[libfirm] / ir / be / beschedtrace.c
index 53189c7..2e3c409 100644 (file)
@@ -1,18 +1,39 @@
+/*
+ * 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.
+ */
+
 /**
- * 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
+ * @version     $Id$
  */
+#include "config.h"
 
 #include <stdlib.h>
 
 #include "iredges_t.h"
 
-#include "besched_t.h"
+#include "besched.h"
 #include "belistsched.h"
-#include "benode_t.h"
+#include "benode.h"
+#include "belive.h"
 
 /* we need a special mark */
 static char _mark;
@@ -30,7 +51,6 @@ typedef struct _trace_irn {
 
 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 */
@@ -38,10 +58,21 @@ typedef struct _trace_env {
        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)
+{
+       ir_nodeset_iterator_t iter;
+
+       ir_nodeset_iterator_init(&iter, nodeset);
+       return ir_nodeset_iterator_next(&iter);
+}
+
 /**
  * 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);
 
@@ -52,7 +83,7 @@ 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);
 
@@ -63,7 +94,7 @@ 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) {
+static inline sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -73,7 +104,7 @@ 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) {
+static inline void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t delay) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -83,7 +114,7 @@ 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) {
+static inline sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -93,7 +124,7 @@ 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) {
+static inline void set_irn_etime(trace_env_t *env, ir_node *n, sched_timestep_t etime) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -103,7 +134,7 @@ 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) {
+static inline unsigned get_irn_num_user(trace_env_t *env, ir_node *n) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -113,7 +144,7 @@ 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) {
+static inline void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_user) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -123,7 +154,7 @@ 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) {
+static inline int get_irn_reg_diff(trace_env_t *env, ir_node *n) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -133,7 +164,7 @@ 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) {
+static inline void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -143,7 +174,7 @@ 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) {
+static inline int get_irn_preorder(trace_env_t *env, ir_node *n) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -153,7 +184,7 @@ 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) {
+static inline void set_irn_preorder(trace_env_t *env, ir_node *n, int pos) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -163,7 +194,7 @@ 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) {
+static inline unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -173,7 +204,7 @@ 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) {
+static inline void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsigned len) {
        int idx = get_irn_idx(n);
 
        assert(idx < ARR_LEN(env->sched_info));
@@ -252,7 +283,7 @@ static int get_reg_difference(trace_env_t *env, ir_node *irn) {
        ir_node *block = get_nodes_block(irn);
 
        if (be_is_Call(irn)) {
-               /* we want calls prefered */
+               /* we want calls preferred */
                return -5;
        }
 
@@ -272,10 +303,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;
@@ -358,6 +395,16 @@ static void trace_preprocess_block(trace_env_t *env, ir_node *block) {
        foreach_out_edge(block, edge) {
                ir_node *succ = get_edge_src_irn(edge);
 
+               if (is_Block(succ)) {
+                       /* A Block-Block edge. This should be the MacroBlock
+                        * edge, ignore it. */
+                       assert(get_Block_MacroBlock(succ) == block && "Block-Block edge found");
+                       continue;
+               }
+               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);
@@ -382,7 +429,7 @@ static void trace_preprocess_block(trace_env_t *env, ir_node *block) {
        for (cur_pos = 0, curr = root; curr; curr = 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;
                }
@@ -449,16 +496,17 @@ static void trace_update_time(void *data, ir_node *irn) {
  * @param birg   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(const be_irg_t *birg) {
+       trace_env_t *env = XMALLOCZ(trace_env_t);
+       ir_graph    *irg = be_get_birg_irg(birg);
        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);
        FIRM_DBG_REGISTER(env->dbg, "firm.be.sched.trace");
 
+       be_liveness_assure_chk(env->liveness);
        memset(env->sched_info, 0, nn * sizeof(*(env->sched_info)));
 
        return env;
@@ -478,20 +526,20 @@ 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) {
+static ir_node *basic_selection(ir_nodeset_t *ready_set)
+{
        ir_node *irn = NULL;
+       ir_nodeset_iterator_t iter;
 
        /* 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);
+       irn = get_nodeset_node(ready_set);
 
        return irn;
 }
@@ -499,43 +547,45 @@ static ir_node *basic_selection(const arch_env_t *arch_env, nodeset *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, ir_nodeset_t *live_set)
 {
        trace_env_t *env = block_env;
-       nodeset *mcands, *ecands;
+       ir_nodeset_t mcands, ecands;
+       ir_nodeset_iterator_t iter;
        sched_timestep_t max_delay = 0;
        ir_node *irn;
+       (void) live_set;
 
        /* 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);
+       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);
+               int 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;
                        }
@@ -543,23 +593,23 @@ static ir_node *muchnik_select(void *block_env, nodeset *ready_set, nodeset *liv
                }
                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);
+                       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 = %d\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(const list_sched_selector_t *vtab, const be_irg_t *birg)
 {
-       trace_env_t *env  = trace_init(arch_env, irg);
+       trace_env_t *env  = trace_init(birg);
        env->selector     = vtab;
-       env->selector_env = (void*) arch_env;
+       env->selector_env = (void*) be_get_birg_arch_env(birg);
        return (void *)env;
 }
 
@@ -569,7 +619,7 @@ static void *muchnik_init_block(void *graph_env, ir_node *bl)
        return graph_env;
 }
 
-static const list_sched_selector_t muchnik_selector_struct = {
+const list_sched_selector_t muchnik_selector = {
        muchnik_init_graph,
        muchnik_init_block,
        muchnik_select,
@@ -582,22 +632,21 @@ static const list_sched_selector_t muchnik_selector_struct = {
        trace_free           /* finish_graph */
 };
 
-const list_sched_selector_t *muchnik_selector = &muchnik_selector_struct;
-
 /**
  * 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, ir_nodeset_t *lv)
 {
        trace_env_t *trace_env   = block_env;
        ir_node     *irn, *cand  = NULL;
        int         max_prio     = INT_MIN;
        int         cur_prio     = INT_MIN;
-       int         cur_pressure = nodeset_count(lv);
+       int         cur_pressure = ir_nodeset_size(lv);
        int         reg_fact, cand_reg_fact;
+       ir_nodeset_iterator_t iter;
 
        /* 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 +659,15 @@ 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; */
+                       reg_fact = chg * cur_pressure;
                        if (reg_fact < chg)
                                reg_fact = INT_MAX - 2;
                        reg_fact = sign ? -reg_fact : reg_fact;
@@ -651,13 +701,13 @@ 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 = {
+const list_sched_selector_t heuristic_selector = {
        muchnik_init_graph,
        muchnik_init_block,
        heuristic_select,
@@ -669,5 +719,3 @@ static const list_sched_selector_t heuristic_selector_struct = {
        NULL,                /* finish_block */
        trace_free           /* finish_graph */
 };
-
-const list_sched_selector_t *heuristic_selector = &heuristic_selector_struct;