Fixed some obviously wrong conditions.
[libfirm] / ir / opt / combo.c
index 9ddf145..e1b8abb 100644 (file)
@@ -81,7 +81,7 @@
 #include "array_t.h"
 #include "error.h"
 #include "irnodeset.h"
-
+#include "irpass.h"
 #include "tv_t.h"
 
 #include "irprintf.h"
@@ -196,7 +196,6 @@ typedef struct environment_t {
        partition_t     *touched;       /**< the touched set. */
        partition_t     *initial;       /**< The initial partition. */
        set             *opcode2id_map; /**< The opcodeMode->id map. */
-       pmap            *type2id_map;   /**< The type->id map. */
        ir_node         **kept_memory;  /**< Array of memory nodes that must be kept. */
        int             end_idx;        /**< -1 for local and 0 for global congruences. */
        int             lambda_input;   /**< Captured argument for lambda_partition(). */
@@ -288,7 +287,7 @@ static void check_opcode(const partition_t *Z) {
                                key.u.intVal = get_Conv_strict(irn);
                                break;
                        case iro_Div:
-                               key.u.intVal = is_Div_remainderless(irn);
+                               key.u.intVal = get_Div_no_remainder(irn);
                                break;
                        case iro_Block:
                                key.u.block = irn;
@@ -319,7 +318,7 @@ static void check_opcode(const partition_t *Z) {
                                assert(key.u.intVal == get_Conv_strict(irn));
                                break;
                        case iro_Div:
-                               assert(key.u.intVal == is_Div_remainderless(irn));
+                               assert(key.u.intVal == get_Div_no_remainder(irn));
                                break;
                        case iro_Block:
                                assert(key.u.block == irn);
@@ -352,6 +351,8 @@ static void check_all_partitions(environment_t *env) {
                        assert(leader != node && leader->part == node->part);
                }
        }
+#else
+       (void) env;
 #endif
 }
 
@@ -359,13 +360,19 @@ static void check_all_partitions(environment_t *env) {
  * Check list.
  */
 static void do_check_list(const node_t *list, int ofs, const partition_t *Z) {
-       const node_t *e;
 
+#ifndef NDEBUG
+       const node_t *e;
 #define NEXT(e)  *((const node_t **)((char *)(e) + (ofs)))
        for (e = list; e != NULL; e = NEXT(e)) {
                assert(e->part == Z);
        }
 #undef NEXT
+#else
+       (void) list;
+       (void) ofs;
+       (void) Z;
+#endif
 }  /* ido_check_list */
 
 /**
@@ -658,7 +665,7 @@ static inline void add_to_worklist(partition_t *X, environment_t *env) {
  * @return a newly allocated partition
  */
 static inline partition_t *new_partition(environment_t *env) {
-       partition_t *part = obstack_alloc(&env->obst, sizeof(*part));
+       partition_t *part = OALLOC(&env->obst, partition_t);
 
        INIT_LIST_HEAD(&part->Leader);
        INIT_LIST_HEAD(&part->Follower);
@@ -717,7 +724,7 @@ static inline lattice_elem_t get_partition_type(const partition_t *X) {
  */
 static node_t *create_partition_node(ir_node *irn, partition_t *part, environment_t *env) {
        /* create a partition node and place it in the partition */
-       node_t *node = obstack_alloc(&env->obst, sizeof(*node));
+       node_t *node = OALLOC(&env->obst, node_t);
 
        INIT_LIST_HEAD(&node->node_list);
        INIT_LIST_HEAD(&node->cprop_list);
@@ -1431,13 +1438,10 @@ static void collect_touched(list_head *list, int idx, environment_t *env) {
 /**
  * Collect commutative nodes to the touched list.
  *
- * @param X     the partition of the list
  * @param list  the list which contains the nodes that must be evaluated
  * @param env   the environment
  */
-static void collect_commutative_touched(partition_t *X, list_head *list, environment_t *env) {
-       int     first      = 1;
-       int     both_input = 0;
+static void collect_commutative_touched(list_head *list, environment_t *env) {
        node_t  *x, *y;
 
        list_for_each_entry(node_t, x, list, node_list) {
@@ -1476,21 +1480,7 @@ static void collect_commutative_touched(partition_t *X, list_head *list, environ
                        /* Partitions of constants should not be split simply because their Nodes have unequal
                           functions or incongruent inputs. */
                        if (type_is_neither_top_nor_const(y->type)) {
-                               int    other_idx = edge->pos ^ 1;
-                               node_t *other    = get_irn_node(get_irn_n(succ, other_idx));
-                               int    equal     = X == other->part;
-
-                               /*
-                                * Note: op(a, a) is NOT congruent to op(a, b).
-                                * So, either all touch nodes must have both inputs congruent,
-                                * or not. We decide this by the first occurred node.
-                                */
-                               if (first) {
-                                       first      = 0;
-                                       both_input = equal;
-                               }
-                               if (both_input == equal)
-                                       add_to_touched(y, env);
+                               add_to_touched(y, env);
                        }
                }
        }
@@ -1518,13 +1508,16 @@ static void cause_splits(environment_t *env) {
                /* empty the touched set: already done, just clear the list */
                env->touched = NULL;
 
-               collect_commutative_touched(X, &X->Leader, env);
-               collect_commutative_touched(X, &X->Follower, env);
+               collect_commutative_touched(&X->Leader, env);
+               collect_commutative_touched(&X->Follower, env);
 
                for (Z = env->touched; Z != NULL; Z = N) {
-                       node_t   *e;
-                       node_t   *touched  = Z->touched;
-                       unsigned n_touched = Z->n_touched;
+                       node_t   *e, *n;
+                       node_t   *touched     = Z->touched;
+                       node_t   *touched_aa  = NULL;
+                       node_t   *touched_ab  = NULL;
+                       unsigned n_touched_aa = 0;
+                       unsigned n_touched_ab = 0;
 
                        assert(Z->touched != NULL);
 
@@ -1535,18 +1528,45 @@ static void cause_splits(environment_t *env) {
                        Z->on_touched = 0;
 
                        /* Empty local Z.touched. */
-                       for (e = touched; e != NULL; e = e->next) {
+                       for (e = touched; e != NULL; e = n) {
+                               node_t *left  = get_irn_node(get_irn_n(e->node, 0));
+                               node_t *right = get_irn_node(get_irn_n(e->node, 1));
+
                                assert(e->is_follower == 0);
                                e->on_touched = 0;
+                               n = e->next;
+
+                               /*
+                                * Note: op(a, a) is NOT congruent to op(a, b).
+                                * So, we must split the touched list.
+                                */
+                               if (left->part == right->part) {
+                                       e->next = touched_aa;
+                                       touched_aa = e;
+                                       ++n_touched_aa;
+                               } else {
+                                       e->next = touched_ab;
+                                       touched_ab = e;
+                                       ++n_touched_ab;
+                               }
                        }
+                       assert(n_touched_aa + n_touched_ab == Z->n_touched);
                        Z->touched   = NULL;
                        Z->n_touched = 0;
 
-                       if (0 < n_touched && n_touched < Z->n_leader) {
-                               DB((dbg, LEVEL_2, "Split part%d by touched\n", Z->nr));
-                               split(&Z, touched, env);
+                       if (0 < n_touched_aa && n_touched_aa < Z->n_leader) {
+                               partition_t *Z_prime = Z;
+                               DB((dbg, LEVEL_2, "Split part%d by touched_aa\n", Z_prime->nr));
+                               split(&Z_prime, touched_aa, env);
                        } else
-                               assert(n_touched <= Z->n_leader);
+                               assert(n_touched_aa <= Z->n_leader);
+
+                       if (0 < n_touched_ab && n_touched_ab < Z->n_leader) {
+                               partition_t *Z_prime = Z;
+                               DB((dbg, LEVEL_2, "Split part%d by touched_ab\n", Z_prime->nr));
+                               split(&Z_prime, touched_ab, env);
+                       } else
+                               assert(n_touched_ab <= Z->n_leader);
                }
        }
 
@@ -1673,7 +1693,7 @@ static void *lambda_opcode(const node_t *node, environment_t *env) {
                key.u.intVal = get_Conv_strict(irn);
                break;
        case iro_Div:
-               key.u.intVal = is_Div_remainderless(irn);
+               key.u.intVal = get_Div_no_remainder(irn);
                break;
        case iro_Block:
                /*
@@ -1914,7 +1934,7 @@ static void compute_Block(node_t *node) {
        int     i;
        ir_node *block = node->node;
 
-       if (block == get_irg_start_block(current_ir_graph) || has_Block_label(block)) {
+       if (block == get_irg_start_block(current_ir_graph) || has_Block_entity(block)) {
                /* start block and labelled blocks are always reachable */
                node->type.tv = tarval_reachable;
                return;
@@ -1956,7 +1976,7 @@ static void compute_Unknown(node_t *node) {
         * It would be safe to compute Top IF it can be assured, that only Cmp
         * nodes are inputs to Conds. We check that first.
         * This is the way Frontends typically build Firm, but some optimizations
-        * (cond_eval for instance) might replace them by Phib's...
+        * (jump threading for instance) might replace them by Phib's...
         */
        node->type.tv = tarval_UNKNOWN;
 }  /* compute_Unknown */
@@ -2376,7 +2396,7 @@ static void compute_Proj_Cond(node_t *node, ir_node *cond) {
                        node->type.tv = tarval_reachable;
                } else if (selector->type.tv == tarval_top) {
                        if (tarval_UNKNOWN == tarval_top &&
-                           pnc == get_Cond_defaultProj(cond)) {
+                           pnc == get_Cond_default_proj(cond)) {
                                /* a switch based of Top is always "default" */
                                node->type.tv = tarval_reachable;
                        } else {
@@ -2384,7 +2404,7 @@ static void compute_Proj_Cond(node_t *node, ir_node *cond) {
                        }
                } else {
                        long value = get_tarval_long(selector->type.tv);
-                       if (pnc == get_Cond_defaultProj(cond)) {
+                       if (pnc == get_Cond_default_proj(cond)) {
                                /* default switch, have to check ALL other cases */
                                int i;
 
@@ -2954,7 +2974,7 @@ static int only_one_reachable_proj(ir_node *n) {
  * @param block  the destination block
  */
 static int can_exchange(ir_node *pred, ir_node *block) {
-       if (is_Start(pred) || has_Block_label(block))
+       if (is_Start(pred) || has_Block_entity(block))
                return 0;
        else if (is_Jmp(pred))
                return 1;
@@ -3140,7 +3160,7 @@ static void exchange_leader(ir_node *irn, ir_node *leader) {
                ir_node  *block = get_nodes_block(leader);
                dbg_info *dbg   = get_irn_dbg_info(irn);
 
-               leader = new_rd_Conv(dbg, current_ir_graph, block, leader, mode, 0);
+               leader = new_rd_Conv(dbg, block, leader, mode);
        }
        exchange(irn, leader);
 }  /* exchange_leader */
@@ -3263,7 +3283,7 @@ static void apply_result(ir_node *irn, void *ctx) {
 
                                if (is_Cond(cond)) {
                                        if (only_one_reachable_proj(cond)) {
-                                               ir_node *jmp = new_r_Jmp(current_ir_graph, block->node);
+                                               ir_node *jmp = new_r_Jmp(block->node);
                                                set_irn_node(jmp, node);
                                                node->node = jmp;
                                                DB((dbg, LEVEL_1, "%+F is replaced by %+F\n", irn, jmp));
@@ -3305,7 +3325,7 @@ static void apply_result(ir_node *irn, void *ctx) {
                        } else if (is_entity(node->type.sym.entity_p)) {
                                if (! is_SymConst(irn)) {
                                        /* can be replaced by a SymConst */
-                                       ir_node *symc = new_r_SymConst(current_ir_graph, block->node, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
+                                       ir_node *symc = new_r_SymConst(current_ir_graph, get_irn_mode(irn), node->type.sym, symconst_addr_ent);
                                        set_irn_node(symc, node);
                                        node->node = symc;
 
@@ -3459,7 +3479,6 @@ void combo(ir_graph *irg) {
        env.dbg_list       = NULL;
 #endif
        env.opcode2id_map  = new_set(cmp_opcode, iro_Last * 4);
-       env.type2id_map    = pmap_create();
        env.kept_memory    = NEW_ARR_F(ir_node *, 0);
        env.end_idx        = get_opt_global_cse() ? 0 : -1;
        env.lambda_input   = 0;
@@ -3551,7 +3570,6 @@ void combo(ir_graph *irg) {
        DEBUG_ONLY(set_dump_node_vcgattr_hook(NULL));
 
        DEL_ARR_F(env.kept_memory);
-       pmap_destroy(env.type2id_map);
        del_set(env.opcode2id_map);
        obstack_free(&env.obst, NULL);
 
@@ -3559,3 +3577,9 @@ void combo(ir_graph *irg) {
        set_value_of_func(NULL);
        current_ir_graph = rem;
 }  /* combo */
+
+/* Creates an ir_graph pass for combo. */
+ir_graph_pass_t *combo_pass(const char *name)
+{
+       return def_graph_pass(name ? name : "combo", combo);
+}  /* combo_pass */