beifg: Simplify the quite complicated way to divide a number by 2 in be_ifg_stat().
[libfirm] / ir / lower / lower_switch.c
index 6ca76e2..e5f3744 100644 (file)
@@ -1,20 +1,6 @@
 /*
- * 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.
+ * Copyright (C) 2012 University of Karlsruhe.
  */
 
 /**
        i >= 0 && (outirn = get_irn_out(irn, i)); --i)
 
 typedef struct walk_env_t {
+       ir_nodeset_t  processed;
+       ir_mode      *selector_mode;
        unsigned      spare_size; /**< the allowed spare size for table switches */
        unsigned      small_switch;
-       bool          allow_out_of_bounds;
        bool          changed;    /**< indicates whether a change was performed */
-       ir_nodeset_t  processed;
 } walk_env_t;
 
 typedef struct case_data_t {
@@ -56,8 +42,8 @@ typedef struct case_data_t {
 
 typedef struct switch_info_t {
        ir_node     *switchn;
-       long         switch_min;
-       long         switch_max;
+       ir_tarval   *switch_min;
+       ir_tarval   *switch_max;
        ir_node     *default_block;
        unsigned     num_cases;
        case_data_t *cases;
@@ -67,31 +53,26 @@ typedef struct switch_info_t {
 /**
  * analyze enough to decide if we should lower the switch
  */
-static bool analyse_switch0(switch_info_t *info, ir_node *switchn)
+static void analyse_switch0(switch_info_t *info, ir_node *switchn)
 {
-       const ir_switch_table *table         = get_Switch_table(switchn);
-       size_t                 n_entries     = ir_switch_table_get_n_entries(table);
-       long                   switch_min    = LONG_MAX;
-       long                   switch_max    = LONG_MIN;
-       unsigned               num_cases     = 0;
-       size_t                 e;
-
-       for (e = 0; e < n_entries; ++e) {
+       const ir_switch_table *table      = get_Switch_table(switchn);
+       size_t                 n_entries  = ir_switch_table_get_n_entries(table);
+       ir_mode               *mode       = get_irn_mode(get_Switch_selector(switchn));
+       ir_tarval             *switch_min = get_mode_max(mode);
+       ir_tarval             *switch_max = get_mode_min(mode);
+       unsigned               num_cases  = 0;
+
+       for (size_t e = 0; e < n_entries; ++e) {
                const ir_switch_table_entry *entry
                        = ir_switch_table_get_entry_const(table, e);
-               long minval;
-               long maxval;
                if (entry->pn == 0)
                        continue;
 
-               if (!tarval_is_long(entry->min) || !tarval_is_long(entry->max))
-                       return false;
-               minval = get_tarval_long(entry->min);
-               maxval = get_tarval_long(entry->max);
-               if (minval < switch_min)
-                       switch_min = minval;
-               if (maxval > switch_max)
-                       switch_max = maxval;
+               if (tarval_cmp(entry->min, switch_min) == ir_relation_less)
+                       switch_min = entry->min;
+               if (tarval_cmp(entry->max, switch_max) == ir_relation_greater)
+                       switch_max = entry->max;
+
                ++num_cases;
        }
 
@@ -99,7 +80,6 @@ static bool analyse_switch0(switch_info_t *info, ir_node *switchn)
        info->switch_min = switch_min;
        info->switch_max = switch_max;
        info->num_cases  = num_cases;
-       return true;
 }
 
 static int casecmp(const void *a, const void *b)
@@ -165,6 +145,7 @@ static void analyse_switch1(switch_info_t *info)
 
        info->default_block = targets[pn_Switch_default];
        info->cases         = cases;
+       free(targets);
 }
 
 static void normalize_table(ir_node *switchn, ir_mode *new_mode,
@@ -199,40 +180,129 @@ static void normalize_table(ir_node *switchn, ir_mode *new_mode,
        }
 }
 
+static void create_out_of_bounds_check(switch_info_t *info)
+{
+       ir_node    *switchn       = info->switchn;
+       ir_graph   *irg           = get_irn_irg(switchn);
+       dbg_info   *dbgi          = get_irn_dbg_info(switchn);
+       ir_node    *selector      = get_Switch_selector(switchn);
+       ir_node    *block         = get_nodes_block(switchn);
+       ir_node   **default_preds = NEW_ARR_F(ir_node*, 0);
+       ir_node    *default_block = NULL;
+       ir_node    *max_const;
+       ir_node    *proj_true;
+       ir_node    *proj_false;
+       ir_node    *cmp;
+       ir_node    *oob_cond;
+       ir_node    *in[1];
+       ir_node    *new_block;
+       int         i;
+       ir_node    *proj;
+       size_t      n_default_preds;
+
+       assert(tarval_is_null(info->switch_min));
+
+       /* check for out-of-bounds */
+       max_const  = new_r_Const(irg, info->switch_max);
+       cmp        = new_rd_Cmp(dbgi, block, selector, max_const, ir_relation_less_equal);
+       oob_cond   = new_rd_Cond(dbgi, block, cmp);
+       proj_true  = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
+       proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);
+
+       ARR_APP1(ir_node*, default_preds, proj_false);
+
+       /* create new block containing the switch */
+       in[0] = proj_true;
+       new_block = new_r_Block(irg, 1, in);
+       set_nodes_block(switchn, new_block);
+
+       /* adjust projs */
+       foreach_out_irn(switchn, i, proj) {
+               long pn = get_Proj_proj(proj);
+               if (pn == pn_Switch_default) {
+                       assert(default_block == NULL);
+                       default_block = get_irn_out(proj, 0);
+                       ARR_APP1(ir_node*, default_preds, proj);
+               }
+               set_nodes_block(proj, new_block);
+       }
+
+       /* adapt default block */
+       n_default_preds = ARR_LEN(default_preds);
+       if (n_default_preds > 1) {
+               /* create new intermediate blocks so we don't have critical edges */
+               size_t p;
+               for (p = 0; p < n_default_preds; ++p) {
+                       ir_node *pred = default_preds[p];
+                       ir_node *split_block;
+                       ir_node *block_in[1];
+
+                       block_in[0] = pred;
+                       split_block = new_r_Block(irg, 1, block_in);
+
+                       default_preds[p] = new_r_Jmp(split_block);
+               }
+       }
+       set_irn_in(default_block, n_default_preds, default_preds);
+
+       DEL_ARR_F(default_preds);
+
+       clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
+}
+
 /**
  * normalize switch to work on an unsigned input with the first case at 0
  */
-static void normalize_switch(switch_info_t *info)
+static void normalize_switch(switch_info_t *info, ir_mode *selector_mode)
 {
-       ir_node   *switchn     = info->switchn;
-       ir_graph  *irg         = get_irn_irg(switchn);
-       ir_node   *block       = get_nodes_block(switchn);
-       ir_node   *selector    = get_Switch_selector(switchn);
-       ir_mode   *mode        = get_irn_mode(selector);
-       ir_tarval *delta       = NULL;
-       bool       change_mode = false;
-
+       ir_node   *switchn         = info->switchn;
+       ir_graph  *irg             = get_irn_irg(switchn);
+       ir_node   *block           = get_nodes_block(switchn);
+       ir_node   *selector        = get_Switch_selector(switchn);
+       ir_mode   *mode            = get_irn_mode(selector);
+       ir_tarval *delta           = NULL;
+       bool       needs_normalize = false;
+
+       ir_tarval *min = info->switch_min;
        if (mode_is_signed(mode)) {
-               mode        = find_unsigned_mode(mode);
-               selector    = new_r_Conv(block, selector, mode);
-               change_mode = true;
+               mode             = find_unsigned_mode(mode);
+               selector         = new_r_Conv(block, selector, mode);
+               min              = tarval_convert_to(min, mode);
+               info->switch_min = min;
+               info->switch_max = tarval_convert_to(info->switch_max, mode);
+               needs_normalize  = true;
        }
 
        /* normalize so switch_min is at 0 */
-       if (info->switch_min != 0) {
-               dbg_info *dbgi = get_irn_dbg_info(switchn);
-               ir_node  *min_const;
+       if (min != get_mode_null(mode)) {
+               ir_node  *min_const = new_r_Const(irg, min);
+               dbg_info *dbgi      = get_irn_dbg_info(switchn);
+               selector = new_rd_Sub(dbgi, block, selector, min_const, mode);
 
-               delta = new_tarval_from_long(info->switch_min, mode);
+               info->switch_max = tarval_sub(info->switch_max, min, mode);
+               info->switch_min = get_mode_null(mode);
+               delta            = min;
 
-               min_const = new_r_Const(irg, delta);
-               selector  = new_rd_Sub(dbgi, block, selector, min_const, mode);
+               needs_normalize = true;
+       }
 
-               info->switch_max -= info->switch_min;
-               info->switch_min  = 0;
+       /* if we have a selector_mode set, then the we will have a switch node,
+        * we have to construct an out-of-bounds check then and after that convert
+        * the switch/selector to the backends desired switch mode */
+       if (selector_mode != NULL) {
+               set_Switch_selector(switchn, selector);
+               create_out_of_bounds_check(info);
+
+               selector = new_r_Conv(block, selector, selector_mode);
+               mode     = selector_mode;
+               info->switch_min = tarval_convert_to(info->switch_min, mode);
+               info->switch_max = tarval_convert_to(info->switch_max, mode);
+               if (delta != NULL)
+                       delta = tarval_convert_to(delta, mode);
+               needs_normalize = true;
        }
 
-       if (delta != NULL || change_mode) {
+       if (needs_normalize) {
                set_Switch_selector(switchn, selector);
                normalize_table(switchn, mode, delta);
        }
@@ -329,78 +399,6 @@ static void create_if_cascade(switch_info_t *info, ir_node *block,
        }
 }
 
-static void create_out_of_bounds_check(switch_info_t *info)
-{
-       ir_node    *switchn       = info->switchn;
-       ir_graph   *irg           = get_irn_irg(switchn);
-       dbg_info   *dbgi          = get_irn_dbg_info(switchn);
-       ir_node    *selector      = get_Switch_selector(switchn);
-       ir_node    *block         = get_nodes_block(switchn);
-       ir_mode    *cmp_mode      = get_irn_mode(selector);
-       ir_node   **default_preds = NEW_ARR_F(ir_node*, 0);
-       ir_node    *default_block = NULL;
-       ir_node    *max_const;
-       ir_node    *proj_true;
-       ir_node    *proj_false;
-       ir_node    *cmp;
-       ir_node    *oob_cond;
-       ir_node    *in[1];
-       ir_node    *new_block;
-       int         i;
-       ir_node    *proj;
-       size_t      n_default_preds;
-
-       assert(info->switch_min == 0);
-
-       /* check for out-of-bounds */
-       max_const  = new_r_Const_long(irg, cmp_mode, info->switch_max);
-       cmp        = new_rd_Cmp(dbgi, block, selector, max_const, ir_relation_less_equal);
-       oob_cond   = new_rd_Cond(dbgi, block, cmp);
-       proj_true  = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
-       proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);
-
-       ARR_APP1(ir_node*, default_preds, proj_false);
-
-       /* create new block containing the switch */
-       in[0] = proj_true;
-       new_block = new_r_Block(irg, 1, in);
-       set_nodes_block(switchn, new_block);
-
-       /* adjust projs */
-       foreach_out_irn(switchn, i, proj) {
-               long pn = get_Proj_proj(proj);
-               if (pn == pn_Switch_default) {
-                       assert(default_block == NULL);
-                       default_block = get_irn_out(proj, 0);
-                       ARR_APP1(ir_node*, default_preds, proj);
-               }
-               set_nodes_block(proj, new_block);
-       }
-
-       /* adapt default block */
-       n_default_preds = ARR_LEN(default_preds);
-       if (n_default_preds > 1) {
-               /* create new intermediate blocks so we don't have critical edges */
-               size_t p;
-               for (p = 0; p < n_default_preds; ++p) {
-                       ir_node *pred = default_preds[p];
-                       ir_node *split_block;
-                       ir_node *block_in[1];
-
-                       block_in[0] = pred;
-                       split_block = new_r_Block(irg, 1, block_in);
-
-                       default_preds[p] = new_r_Jmp(split_block);
-               }
-       }
-       set_irn_in(default_block, n_default_preds, default_preds);
-
-       DEL_ARR_F(default_preds);
-
-       clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE
-                          | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
-}
-
 /**
  * Block-Walker: searches for Switch nodes
  */
@@ -410,9 +408,6 @@ static void find_switch_nodes(ir_node *block, void *ctx)
        ir_node      *projx;
        ir_node      *switchn;
        switch_info_t info;
-       unsigned long spare;
-       bool          lower_switch = false;
-       bool          could_analyze;
 
        /* because we split critical blocks only blocks with 1 predecessors may
         * contain Proj->Cond nodes */
@@ -432,40 +427,37 @@ static void find_switch_nodes(ir_node *block, void *ctx)
                return;
        ir_nodeset_insert(&env->processed, switchn);
 
-       could_analyze = analyse_switch0(&info, switchn);
-       /* the code can't handle values which are not representable in the host */
-       if (!could_analyze) {
-               ir_fprintf(stderr, "libfirm warning: Couldn't analyse %+F (this could go wrong in the backend)\n", switchn);
-               return;
-       }
+       analyse_switch0(&info, switchn);
 
        /*
         * Here we have: num_cases and [switch_min, switch_max] interval.
         * We do an if-cascade if there are too many spare numbers.
         */
-       spare = (unsigned long) info.switch_max
-               - (unsigned long) info.switch_min
-               - (unsigned long) info.num_cases + 1;
-       lower_switch |= spare >= env->spare_size;
-       lower_switch |= info.num_cases <= env->small_switch;
+       ir_mode   *mode  = get_irn_mode(get_Switch_selector(switchn));
+       ir_tarval *spare = tarval_sub(info.switch_max, info.switch_min, mode);
+       mode  = find_unsigned_mode(mode);
+       spare = tarval_convert_to(spare, mode);
+       ir_tarval *num_cases_minus_one
+               = new_tarval_from_long(info.num_cases-1, mode);
+       spare = tarval_sub(spare, num_cases_minus_one, mode);
+       ir_tarval *spare_size = new_tarval_from_long(env->spare_size, mode);
+       bool lower_switch = (info.num_cases <= env->small_switch
+        || (tarval_cmp(spare, spare_size) & ir_relation_greater_equal));
 
        if (!lower_switch) {
-               /* we won't decompose the switch. But we might have to add
-                * out-of-bounds checking */
-               if (!env->allow_out_of_bounds) {
-                       normalize_switch(&info);
-                       create_out_of_bounds_check(&info);
-               }
+               /* we won't decompose the switch. But we must add an out-of-bounds
+                * check */
+               normalize_switch(&info, env->selector_mode);
                return;
        }
 
-       normalize_switch(&info);
+       normalize_switch(&info, NULL);
        analyse_switch1(&info);
 
        /* Now create the if cascade */
-       env->changed   = true;
+       env->changed  = true;
        info.defusers = NEW_ARR_F(ir_node*, 0);
-       block          = get_nodes_block(switchn);
+       block         = get_nodes_block(switchn);
        create_if_cascade(&info, block, info.cases, info.num_cases);
 
        /* Connect new default case users */
@@ -473,19 +465,21 @@ static void find_switch_nodes(ir_node *block, void *ctx)
 
        DEL_ARR_F(info.defusers);
        xfree(info.cases);
-       clear_irg_state(get_irn_irg(block), IR_GRAPH_STATE_NO_CRITICAL_EDGES
-                                         | IR_GRAPH_STATE_CONSISTENT_DOMINANCE
-                                         | IR_GRAPH_STATE_VALID_EXTENDED_BLOCKS);
+       clear_irg_properties(get_irn_irg(block), IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES
+                                         | IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
 }
 
 void lower_switch(ir_graph *irg, unsigned small_switch, unsigned spare_size,
-                  int allow_out_of_bounds)
+                  ir_mode *selector_mode)
 {
+       if (mode_is_signed(selector_mode))
+               panic("expected unsigned mode for switch selector");
+
        walk_env_t env;
-       env.changed             = false;
+       env.selector_mode       = selector_mode;
        env.spare_size          = spare_size;
        env.small_switch        = small_switch;
-       env.allow_out_of_bounds = allow_out_of_bounds;
+       env.changed             = false;
        ir_nodeset_init(&env.processed);
 
        remove_critical_cf_edges(irg);