end block can also have 0 predecessors
[libfirm] / ir / be / beblocksched.c
index b936c60..69422b5 100644 (file)
  * @author      Matthias Braun, Christoph Mallon
  * @date        27.09.2006
  * @version     $Id$
+ *
+ * The goals of the greedy (and ILP) algorithm here works by assuming that
+ * we want to change as many jumps to fallthroughs as possible (executed jumps
+ * actually, we have to look at the execution frequencies). The algorithms
+ * do this by collecting execution frequencies of all branches (which is easily
+ * possible when all critical edges are split) then removes critical edges where
+ * possible as we don't need and want them anymore now. The algorithms then try
+ * to change as many edges to fallthroughs as possible, this is done by setting
+ * a next and prev pointers on blocks. The greedy algorithm sorts the edges by
+ * execution frequencies and tries to transform them to fallthroughs in this order
  */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 
 #include "iredges.h"
 #include "irgwalk.h"
+#include "irnode_t.h"
 #include "irgraph_t.h"
 #include "irloop.h"
 #include "irprintf.h"
+#include "execfreq.h"
 #include "irdump_t.h"
 #include "irtools.h"
 #include "debug.h"
@@ -80,7 +92,7 @@ static lc_opt_enum_int_var_t algo_var = {
 
 static const lc_opt_table_entry_t be_blocksched_options[] = {
        LC_OPT_ENT_ENUM_INT ("algo", "the block scheduling algorithm", &algo_var),
-       { NULL }
+       LC_OPT_LAST
 };
 
 /*
@@ -102,7 +114,7 @@ typedef struct _edge_t {
        ir_node *block;             /**< source block */
        int     pos;                /**< number of cfg predecessor (target) */
        double  execfreq;           /**< the frequency */
-       int     highest_execfreq;   /**< flag that indicates wether this edge is the edge with the highest
+       int     highest_execfreq;   /**< flag that indicates whether this edge is the edge with the highest
                                                                     execfreq pointing away from this block */
 } edge_t;
 
@@ -132,12 +144,15 @@ static void collect_egde_frequency(ir_node *block, void *data)
        entry->prev  = NULL;
        set_irn_link(block, entry);
 
-       if (block == get_irg_start_block(env->irg))
-               return;
-
-       arity = get_irn_arity(block);
+       arity = get_Block_n_cfgpreds(block);
 
-       if (arity == 1) {
+       if (arity == 0) {
+               assert(block == get_irg_start_block(env->irg)
+                               || block == get_irg_end_block(env->irg));
+               /* must be the start block (or end-block for endless loops), nothing to
+                * do here */
+               return;
+       } else if (arity == 1) {
                edge.block            = block;
                edge.pos              = 0;
                edge.execfreq         = get_block_execfreq(env->execfreqs, block);
@@ -188,18 +203,19 @@ static void coalesce_blocks(blocksched_env_t *env)
        for (i = 0; i < edge_count; ++i) {
                const edge_t *edge  = &env->edges[i];
                ir_node      *block = edge->block;
+               int           pos   = edge->pos;
                ir_node      *pred_block;
                blocksched_entry_t *entry, *pred_entry;
 
-               /* the block might have been removed already... */
-               if (is_Bad(get_Block_cfgpred(block, 0)))
-                       continue;
-
                /* only check edge with highest frequency */
                if (! edge->highest_execfreq)
                        continue;
 
-               pred_block = get_Block_cfgpred_block(block, edge->pos);
+               /* the block might have been removed already... */
+               if (is_Bad(get_Block_cfgpred(block, 0)))
+                       continue;
+
+               pred_block = get_Block_cfgpred_block(block, pos);
                entry      = get_irn_link(block);
                pred_entry = get_irn_link(pred_block);
 
@@ -221,6 +237,7 @@ static void coalesce_blocks(blocksched_env_t *env)
        for (i = 0; i < edge_count; ++i) {
                const edge_t *edge  = &env->edges[i];
                ir_node      *block = edge->block;
+               int           pos   = edge->pos;
                ir_node      *pred_block;
                blocksched_entry_t *entry, *pred_entry;
 
@@ -228,7 +245,11 @@ static void coalesce_blocks(blocksched_env_t *env)
                if (is_Bad(get_Block_cfgpred(block, 0)))
                        continue;
 
-               pred_block = get_Block_cfgpred_block(block, edge->pos);
+               /* we can't do fallthroughs in backedges */
+               if (is_backedge(block, pos))
+                       continue;
+
+               pred_block = get_Block_cfgpred_block(block, pos);
                entry      = get_irn_link(block);
                pred_entry = get_irn_link(pred_block);
 
@@ -360,6 +381,7 @@ static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry
        int                i = 0;
        ir_node            **block_list;
        blocksched_entry_t *entry;
+       (void) env;
 
        block_list = NEW_ARR_D(ir_node *, obst, count);
        DBG((dbg, LEVEL_1, "Blockschedule:\n"));