2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mono / mini / branch-opts.c
index 3da6c0e12417b8b55b5a5651d9886840de07a932..3d27d244404c66266be048f9c53d9c1dc5739c8e 100644 (file)
@@ -7,8 +7,22 @@
  * (C) 2005 Ximian, Inc.  http://www.ximian.com
  */
  #include "mini.h"
+
+#ifndef DISABLE_JIT
  
- /*
+
+/*
+ * Returns true if @bb is a basic block which falls through the next block.
+ * TODO verify if it helps to check if the bb last ins is a branch to its successor. 
+ */
+static gboolean
+mono_bb_is_fall_through (MonoCompile *cfg, MonoBasicBlock *bb)
+{
+       return  bb->next_bb && bb->next_bb->region == bb->region && /*fall throught between regions is not really interesting or useful*/
+                       (bb->last_ins == NULL || !MONO_IS_BRANCH_OP (bb->last_ins)); /*and the last op can't be a branch too*/
+}
+
+/*
  * Used by the arch code to replace the exception handling
  * with a direct branch. This is safe to do if the 
  * exception object isn't used, no rethrow statement and
@@ -118,6 +132,143 @@ static const int long_cmov_opcodes [] = {
        OP_CMOV_LGT_UN
 };
 
+static int
+br_to_br_un (int opcode)
+{
+       switch (opcode) {
+       case OP_IBGT:
+               return OP_IBGT_UN;
+               break;
+       case OP_IBLE:
+               return OP_IBLE_UN;
+               break;
+       case OP_LBGT:
+               return OP_LBGT_UN;
+               break;
+       case OP_LBLE:
+               return OP_LBLE_UN;
+               break;
+       default:
+               g_assert_not_reached ();
+               return -1;
+       }
+}
+
+/**
+ * mono_replace_ins:
+ *
+ *   Replace INS with its decomposition which is stored in a series of bblocks starting
+ * at FIRST_BB and ending at LAST_BB. On enter, PREV points to the predecessor of INS. 
+ * On return, it will be set to the last ins of the decomposition.
+ */
+void
+mono_replace_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **prev, MonoBasicBlock *first_bb, MonoBasicBlock *last_bb)
+{
+       MonoInst *next = ins->next;
+
+       if (next && next->opcode == OP_NOP) {
+               /* Avoid NOPs following branches */
+               ins->next = next->next;
+               next = next->next;
+       }
+
+       if (first_bb == last_bb) {
+               /* 
+                * Only one replacement bb, merge the code into
+                * the current bb.
+                */
+
+               /* Delete links between the first_bb and its successors */
+               while (first_bb->out_count)
+                       mono_unlink_bblock (cfg, first_bb, first_bb->out_bb [0]);
+
+               /* Head */
+               if (*prev) {
+                       (*prev)->next = first_bb->code;
+                       first_bb->code->prev = (*prev);
+               } else {
+                       bb->code = first_bb->code;
+               }
+
+               /* Tail */
+               last_bb->last_ins->next = next;
+               if (next)
+                       next->prev = last_bb->last_ins;
+               else
+                       bb->last_ins = last_bb->last_ins;
+               *prev = last_bb->last_ins;
+               bb->has_array_access |= first_bb->has_array_access;
+       } else {
+               int i, count;
+               MonoBasicBlock **tmp_bblocks, *tmp;
+               MonoInst *last;
+
+               /* Multiple BBs */
+
+               /* Set region */
+               for (tmp = first_bb; tmp; tmp = tmp->next_bb)
+                       tmp->region = bb->region;
+
+               /* Split the original bb */
+               if (ins->next)
+                       ins->next->prev = NULL;
+               ins->next = NULL;
+               bb->last_ins = ins;
+
+               /* Merge the second part of the original bb into the last bb */
+               if (last_bb->last_ins) {
+                       last_bb->last_ins->next = next;
+                       if (next)
+                               next->prev = last_bb->last_ins;
+               } else {
+                       last_bb->code = next;
+               }
+               last_bb->has_array_access |= bb->has_array_access;
+
+               if (next) {
+                       for (last = next; last->next != NULL; last = last->next)
+                               ;
+                       last_bb->last_ins = last;
+               }
+
+               for (i = 0; i < bb->out_count; ++i)
+                       mono_link_bblock (cfg, last_bb, bb->out_bb [i]);
+
+               /* Merge the first (dummy) bb to the original bb */
+               if (*prev) {
+                       (*prev)->next = first_bb->code;
+                       first_bb->code->prev = (*prev);
+               } else {
+                       bb->code = first_bb->code;
+               }
+               bb->last_ins = first_bb->last_ins;
+               bb->has_array_access |= first_bb->has_array_access;
+
+               /* Delete the links between the original bb and its successors */
+               tmp_bblocks = bb->out_bb;
+               count = bb->out_count;
+               for (i = 0; i < count; ++i)
+                       mono_unlink_bblock (cfg, bb, tmp_bblocks [i]);
+
+               /* Add links between the original bb and the first_bb's successors */
+               for (i = 0; i < first_bb->out_count; ++i) {
+                       MonoBasicBlock *out_bb = first_bb->out_bb [i];
+
+                       mono_link_bblock (cfg, bb, out_bb);
+               }
+               /* Delete links between the first_bb and its successors */
+               for (i = 0; i < bb->out_count; ++i) {
+                       MonoBasicBlock *out_bb = bb->out_bb [i];
+
+                       mono_unlink_bblock (cfg, first_bb, out_bb);
+               }
+               last_bb->next_bb = bb->next_bb;
+               bb->next_bb = first_bb->next_bb;
+
+               *prev = NULL;
+       }
+}
+
 void
 mono_if_conversion (MonoCompile *cfg)
 {
@@ -157,20 +308,37 @@ mono_if_conversion (MonoCompile *cfg)
                bb2 = bb->out_bb [1];
 
                if (bb1->in_count == 1 && bb2->in_count == 1 && bb1->out_count == 1 && bb2->out_count == 1 && bb1->out_bb [0] == bb2->out_bb [0]) {
-                       MonoInst *prev, *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
+                       MonoInst *compare, *branch, *ins1, *ins2, *cmov, *move, *tmp;
+                       MonoBasicBlock *true_bb, *false_bb;
                        gboolean simple, ret;
                        int dreg, tmp_reg;
                        CompType comp_type;
 
+                       if (bb->last_ins && (bb->last_ins->opcode == OP_BR_REG || bb->last_ins->opcode == OP_BR))
+                               continue;
+
+                       /* Find the compare instruction */
+                       if (!bb->last_ins || !bb->last_ins->prev)
+                               continue;
+                       branch = bb->last_ins;
+                       compare = branch->prev;
+
+                       if (!MONO_IS_COND_BRANCH_OP (branch))
+                               /* This can happen if a cond branch is optimized away */
+                               continue;
+
+                       true_bb = branch->inst_true_bb;
+                       false_bb = branch->inst_false_bb;
+
                        /* 
                         * Check that bb1 and bb2 are 'simple' and both assign to the same
                         * variable.
                         */
                        /* FIXME: Get rid of the nops earlier */
-                       ins1 = bb1->code;
+                       ins1 = true_bb->code;
                        while (ins1 && ins1->opcode == OP_NOP)
                                ins1 = ins1->next;
-                       ins2 = bb2->code;
+                       ins2 = false_bb->code;
                        while (ins2 && ins2->opcode == OP_NOP)
                                ins2 = ins2->next;
                        if (!(ins1 && ins2 && ins1->dreg == ins2->dreg && ins1->dreg != -1))
@@ -192,21 +360,6 @@ mono_if_conversion (MonoCompile *cfg)
                        if (!(MONO_INS_HAS_NO_SIDE_EFFECT (ins1) && MONO_INS_HAS_NO_SIDE_EFFECT (ins2)))
                                continue;
 
-                       if (bb->last_ins && (bb->last_ins->opcode == OP_BR_REG || bb->last_ins->opcode == OP_BR))
-                               continue;
-
-                       /* Find the compare instruction */
-                       /* FIXME: Optimize this using prev */
-                       prev = NULL;
-                       compare = bb->code;
-                       g_assert (compare);
-                       while (compare->next && !MONO_IS_COND_BRANCH_OP (compare->next)) {
-                               prev = compare;
-                               compare = compare->next;
-                       }
-                       g_assert (compare->next && MONO_IS_COND_BRANCH_OP (compare->next));
-                       branch = compare->next;
-
                        /* Moving ins1/ins2 could change the comparison */
                        /* FIXME: */
                        if (!((compare->sreg1 != ins1->dreg) && (compare->sreg2 != ins1->dreg)))
@@ -252,8 +405,8 @@ mono_if_conversion (MonoCompile *cfg)
                        }
 
                        /* Remove ins1/ins2 from bb1/bb2 */
-                       MONO_REMOVE_INS (bb1, ins1);
-                       MONO_REMOVE_INS (bb2, ins2);
+                       MONO_REMOVE_INS (true_bb, ins1);
+                       MONO_REMOVE_INS (false_bb, ins2);
 
                        /* Move ins1 and ins2 before the comparison */
                        /* ins1 comes first to avoid ins1 overwriting an argument of ins2 */
@@ -287,16 +440,16 @@ mono_if_conversion (MonoCompile *cfg)
 
                        /* Rewrite the branch */
                        branch->opcode = OP_BR;
-                       branch->inst_target_bb = bb1->out_bb [0];
+                       branch->inst_target_bb = true_bb->out_bb [0];
                        mono_link_bblock (cfg, bb, branch->inst_target_bb);
 
                        /* Reorder bblocks */
-                       mono_unlink_bblock (cfg, bb, bb1);
-                       mono_unlink_bblock (cfg, bb, bb2);
-                       mono_unlink_bblock (cfg, bb1, bb1->out_bb [0]);
-                       mono_unlink_bblock (cfg, bb2, bb2->out_bb [0]);
-                       mono_remove_bblock (cfg, bb1);
-                       mono_remove_bblock (cfg, bb2);
+                       mono_unlink_bblock (cfg, bb, true_bb);
+                       mono_unlink_bblock (cfg, bb, false_bb);
+                       mono_unlink_bblock (cfg, true_bb, true_bb->out_bb [0]);
+                       mono_unlink_bblock (cfg, false_bb, false_bb->out_bb [0]);
+                       mono_remove_bblock (cfg, true_bb);
+                       mono_remove_bblock (cfg, false_bb);
 
                        /* Merge bb and its successor if possible */
                        if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
@@ -317,7 +470,7 @@ mono_if_conversion (MonoCompile *cfg)
 
                if ((bb2->in_count == 1 && bb2->out_count == 1 && bb2->out_bb [0] == bb1) ||
                        (bb1->in_count == 1 && bb1->out_count == 1 && bb1->out_bb [0] == bb2)) {
-                       MonoInst *prev, *compare, *branch, *ins1, *cmov, *tmp;
+                       MonoInst *compare, *branch, *ins1, *cmov, *tmp;
                        gboolean simple;
                        int dreg, tmp_reg;
                        CompType comp_type;
@@ -355,16 +508,15 @@ mono_if_conversion (MonoCompile *cfg)
                                continue;
 
                        /* Find the compare instruction */
-                       /* FIXME: Optimize this using prev */
-                       prev = NULL;
-                       compare = bb->code;
-                       g_assert (compare);
-                       while (compare->next && !MONO_IS_COND_BRANCH_OP (compare->next)) {
-                               prev = compare;
-                               compare = compare->next;
-                       }
-                       g_assert (compare->next && MONO_IS_COND_BRANCH_OP (compare->next));
-                       branch = compare->next;
+
+                       if (!bb->last_ins || !bb->last_ins->prev)
+                               continue;
+                       branch = bb->last_ins;
+                       compare = branch->prev;
+
+                       if (!MONO_IS_COND_BRANCH_OP (branch))
+                               /* This can happen if a cond branch is optimized away */
+                               continue;
 
                        /* FIXME: */
                        comp_type = mono_opcode_to_type (branch->opcode, compare->opcode);
@@ -442,11 +594,115 @@ mono_if_conversion (MonoCompile *cfg)
                        if ((bb->out_bb [0]->in_count == 1) && (bb->out_bb [0] != cfg->bb_exit) &&
                                (bb->region == bb->out_bb [0]->region)) {
                                mono_merge_basic_blocks (cfg, bb, bb->out_bb [0]);
+
+                               /* 
+                                * bbn might have fallen through to the next bb without a branch, 
+                                * have to add one now (#474718).
+                                * FIXME: Maybe need to do this more generally in 
+                                * merge_basic_blocks () ?
+                                */
+                               if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins)) && bb->out_count) {
+                                       MONO_INST_NEW (cfg, ins1, OP_BR);
+                                       ins1->inst_target_bb = bb->out_bb [0];
+                                       MONO_ADD_INS (bb, ins1);
+                               }
                                goto restart;
                        }
                }
        }
 
+       /*
+        * Optimize checks like: if (v < 0 || v > limit) by changing then to unsigned
+        * compares. This isn't really if conversion, but it easier to do here than in
+        * optimize_branches () since the IR is already optimized.
+        */
+       for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
+               MonoBasicBlock *bb1, *bb2, *true_bb, *false_bb, *next_bb;
+               MonoInst *branch1, *branch2, *compare1, *ins;
+
+               /* Look for the IR code generated from if (<var> < 0 || v > <limit>)
+                * after branch opts which is:
+                * BB:
+                * icompare_imm R [0]
+                * int_blt [BB1BB2]
+                * BB2:
+                * icompare_imm R [<limit>]
+                * int_ble [BB3BB1]
+                */
+               if (!(bb->out_count == 2 && !bb->extended))
+                       continue;
+
+               bb1 = bb->out_bb [0];
+               bb2 = bb->out_bb [1];
+
+               // FIXME: Add more cases
+
+               /* Check structure */
+               if (!(bb1->in_count == 2 && bb1->in_bb [0] == bb && bb1->in_bb [1] == bb2 && bb2->in_count == 1 && bb2->out_count == 2))
+                       continue;
+
+               next_bb = bb2;
+
+               /* Check first branch */
+               branch1 = bb->last_ins;
+               if (!(branch1 && ((branch1->opcode == OP_IBLT) || (branch1->opcode == OP_LBLT)) && (branch1->inst_false_bb == next_bb)))
+                       continue;
+
+               true_bb = branch1->inst_true_bb;
+
+               /* Check second branch */
+               branch2 = next_bb->last_ins;
+               if (!branch2)
+                       continue;
+
+               /* mcs sometimes generates inverted branches */
+               if (((branch2->opcode == OP_IBGT) || (branch2->opcode == OP_LBGT)) && branch2->inst_true_bb == branch1->inst_true_bb)
+                       false_bb = branch2->inst_false_bb;
+               else if (((branch2->opcode == OP_IBLE) || (branch2->opcode == OP_LBLE)) && branch2->inst_false_bb == branch1->inst_true_bb)
+                       false_bb = branch2->inst_true_bb;
+               else
+                       continue;
+
+               /* Check first compare */
+               compare1 = bb->last_ins->prev;
+               if (!(compare1 && ((compare1->opcode == OP_ICOMPARE_IMM) || (compare1->opcode == OP_LCOMPARE_IMM)) && compare1->inst_imm == 0))
+                       continue;
+
+               /* Check second bblock */
+               ins = next_bb->code;
+               if (!ins)
+                       continue;
+               if (((ins->opcode == OP_ICOMPARE_IMM) || (ins->opcode == OP_LCOMPARE_IMM)) && ins->sreg1 == compare1->sreg1 && ins->next == branch2) {
+                       /* The second arg must be positive */
+                       if (ins->inst_imm < 0)
+                               continue;
+               } else if (((ins->opcode == OP_LDLEN) || (ins->opcode == OP_STRLEN)) && ins->dreg != compare1->sreg1 && ins->next && ins->next->opcode == OP_ICOMPARE && ins->next->sreg1 == compare1->sreg1 && ins->next->sreg2 == ins->dreg && ins->next->next == branch2) {
+                       /* Another common case: if (index < 0 || index > arr.Length) */
+               } else {
+                       continue;
+               }
+
+               if (cfg->verbose_level > 2) {
+                       printf ("\tSigned->unsigned compare optimization in BB%d on\n", bb->block_num);
+                       printf ("\t\t"); mono_print_ins (compare1);
+                       printf ("\t\t"); mono_print_ins (compare1->next);
+                       printf ("\t\t"); mono_print_ins (ins);
+               }
+
+               /* Rewrite the first compare+branch */
+               MONO_DELETE_INS (bb, compare1);
+               branch1->opcode = OP_BR;
+               mono_unlink_bblock (cfg, bb, branch1->inst_true_bb);
+               mono_unlink_bblock (cfg, bb, branch1->inst_false_bb);
+               branch1->inst_target_bb = next_bb;
+               mono_link_bblock (cfg, bb, next_bb);            
+
+               /* Rewrite the second branch */
+               branch2->opcode = br_to_br_un (branch2->opcode);
+
+               mono_merge_basic_blocks (cfg, bb, next_bb);
+       }
+
 #if 0
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
                MonoBasicBlock *bb1, *bb2;
@@ -488,7 +744,7 @@ mono_if_conversion (MonoCompile *cfg)
                /* Merging bblocks could make some variables local */
                mono_handle_global_vregs (cfg);
                if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
-                       mono_local_cprop2 (cfg);
+                       mono_local_cprop (cfg);
                mono_local_deadce (cfg);
        }
 #endif
@@ -663,13 +919,7 @@ remove_block_if_useless (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *p
                }
                
                mono_unlink_bblock (cfg, bb, target_bb);
-               
-               if ((previous_bb != cfg->bb_entry) &&
-                               (previous_bb->region == bb->region) &&
-                               ((previous_bb->last_ins == NULL) ||
-                               ((previous_bb->last_ins->opcode != OP_BR) &&
-                               (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
-                               (previous_bb->last_ins->opcode != OP_SWITCH)))) {
+               if (previous_bb != cfg->bb_entry && mono_bb_is_fall_through (cfg, previous_bb)) {
                        for (i = 0; i < previous_bb->out_count; i++) {
                                if (previous_bb->out_bb [i] == target_bb) {
                                        MonoInst *jump;
@@ -708,21 +958,27 @@ mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *b
                mono_unlink_bblock (cfg, bbn, bbn->out_bb [0]);
 
        /* Handle the branch at the end of the bb */
-       for (inst = bb->code; inst != NULL; inst = inst->next) {
-               if (inst->opcode == OP_CALL_HANDLER) {
-                       g_assert (inst->inst_target_bb == bbn);
-                       NULLIFY_INS (inst);
+       if (bb->has_call_handler) {
+               for (inst = bb->code; inst != NULL; inst = inst->next) {
+                       if (inst->opcode == OP_CALL_HANDLER) {
+                               g_assert (inst->inst_target_bb == bbn);
+                               NULLIFY_INS (inst);
+                       }
                }
-               if (MONO_IS_JUMP_TABLE (inst)) {
-                       int i;
-                       MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (inst);
-                       for (i = 0; i < table->table_size; i++ ) {
-                               /* Might be already NULL from a previous merge */
-                               if (table->table [i])
-                                       g_assert (table->table [i] == bbn);
-                               table->table [i] = NULL;
+       }
+       if (bb->has_jump_table) {
+               for (inst = bb->code; inst != NULL; inst = inst->next) {
+                       if (MONO_IS_JUMP_TABLE (inst)) {
+                               int i;
+                               MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (inst);
+                               for (i = 0; i < table->table_size; i++ ) {
+                                       /* Might be already NULL from a previous merge */
+                                       if (table->table [i])
+                                               g_assert (table->table [i] == bbn);
+                                       table->table [i] = NULL;
+                               }
+                               /* Can't nullify this as later instructions depend on it */
                        }
-                       /* Can't nullify this as later instructions depend on it */
                }
        }
        if (bb->last_ins && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
@@ -733,6 +989,9 @@ mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *b
                NULLIFY_INS (bb->last_ins);
        }
 
+       bb->has_call_handler |= bbn->has_call_handler;
+       bb->has_jump_table |= bbn->has_jump_table;
+
        if (bb->last_ins) {
                if (bbn->code) {
                        bb->last_ins->next = bbn->code;
@@ -743,6 +1002,7 @@ mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *b
                bb->code = bbn->code;
                bb->last_ins = bbn->last_ins;
        }
+
        for (prev_bb = cfg->bb_entry; prev_bb && prev_bb->next_bb != bbn; prev_bb = prev_bb->next_bb)
                ;
        if (prev_bb) {
@@ -753,6 +1013,16 @@ mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *b
                        bb->next_bb = bbn->next_bb;
        }
        mono_nullify_basic_block (bbn);
+
+       /* 
+        * If bbn fell through to its next bblock, have to add a branch, since bb
+        * will not fall though to the same bblock (#513931).
+        */
+       if (bb->last_ins && bb->out_count == 1 && bb->out_bb [0] != bb->next_bb && !MONO_IS_BRANCH_OP (bb->last_ins)) {
+               MONO_INST_NEW (cfg, inst, OP_BR);
+               inst->inst_target_bb = bb->out_bb [0];
+               MONO_ADD_INS (bb, inst);
+       }
 }
 
 static void
@@ -823,7 +1093,7 @@ mono_remove_critical_edges (MonoCompile *cfg)
                        printf (")");
                        if (bb->last_ins != NULL) {
                                printf (" ");
-                               mono_print_tree (bb->last_ins);
+                               mono_print_ins (bb->last_ins);
                        }
                        printf ("\n");
                }
@@ -834,32 +1104,30 @@ mono_remove_critical_edges (MonoCompile *cfg)
                        int in_bb_index;
                        for (in_bb_index = 0; in_bb_index < bb->in_count; in_bb_index++) {
                                MonoBasicBlock *in_bb = bb->in_bb [in_bb_index];
-                               if (in_bb->out_count > 1) {
+                               /* 
+                                * Have to remove non-critical edges whose source ends with a BR_REG
+                                * ins too, since inserting a computation before the BR_REG could 
+                                * overwrite the sreg1 of the ins.
+                                */
+                               if ((in_bb->out_count > 1) || (in_bb->out_count == 1 && in_bb->last_ins && in_bb->last_ins->opcode == OP_BR_REG)) {
                                        MonoBasicBlock *new_bb = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoBasicBlock));
                                        new_bb->block_num = cfg->num_bblocks++;
 //                                     new_bb->real_offset = bb->real_offset;
                                        new_bb->region = bb->region;
                                        
                                        /* Do not alter the CFG while altering the BB list */
-                                       if (previous_bb->region == bb->region) {
+                                       if (mono_bb_is_fall_through (cfg, previous_bb)) {
                                                if (previous_bb != cfg->bb_entry) {
-                                                       /* If previous_bb "followed through" to bb, */
-                                                       /* keep it linked with a OP_BR */
-                                                       if ((previous_bb->last_ins == NULL) ||
-                                                                       ((previous_bb->last_ins->opcode != OP_BR) &&
-                                                                       (! (MONO_IS_COND_BRANCH_OP (previous_bb->last_ins))) &&
-                                                                       (previous_bb->last_ins->opcode != OP_SWITCH))) {
-                                                               int i;
-                                                               /* Make sure previous_bb really falls through bb */
-                                                               for (i = 0; i < previous_bb->out_count; i++) {
-                                                                       if (previous_bb->out_bb [i] == bb) {
-                                                                               MonoInst *jump;
-                                                                               MONO_INST_NEW (cfg, jump, OP_BR);
-                                                                               MONO_ADD_INS (previous_bb, jump);
-                                                                               jump->cil_code = previous_bb->cil_code;
-                                                                               jump->inst_target_bb = bb;
-                                                                               break;
-                                                                       }
+                                                       int i;
+                                                       /* Make sure previous_bb really falls through bb */
+                                                       for (i = 0; i < previous_bb->out_count; i++) {
+                                                               if (previous_bb->out_bb [i] == bb) {
+                                                                       MonoInst *jump;
+                                                                       MONO_INST_NEW (cfg, jump, OP_BR);
+                                                                       MONO_ADD_INS (previous_bb, jump);
+                                                                       jump->cil_code = previous_bb->cil_code;
+                                                                       jump->inst_target_bb = bb;
+                                                                       break;
                                                                }
                                                        }
                                                } else {
@@ -875,10 +1143,14 @@ mono_remove_critical_edges (MonoCompile *cfg)
                                                        MONO_ADD_INS (new_bb_after_entry, jump);
                                                        jump->cil_code = bb->cil_code;
                                                        jump->inst_target_bb = bb;
+
+                                                       mono_unlink_bblock (cfg, previous_bb, bb);
+                                                       mono_link_bblock (cfg, new_bb_after_entry, bb);
+                                                       mono_link_bblock (cfg, previous_bb, new_bb_after_entry);
                                                        
                                                        previous_bb->next_bb = new_bb_after_entry;
                                                        previous_bb = new_bb_after_entry;
-                                                       
+
                                                        if (cfg->verbose_level > 2) {
                                                                printf ("remove_critical_edges, added helper BB%d jumping to BB%d\n", new_bb_after_entry->block_num, bb->block_num);
                                                        }
@@ -925,116 +1197,13 @@ mono_remove_critical_edges (MonoCompile *cfg)
                        printf (")");
                        if (bb->last_ins != NULL) {
                                printf (" ");
-                               mono_print_tree (bb->last_ins);
+                               mono_print_ins (bb->last_ins);
                        }
                        printf ("\n");
                }
        }
 }
 
-/* checks that a and b represent the same instructions, conservatively,
- * it can return FALSE also for two trees that are equal.
- * FIXME: also make sure there are no side effects.
- */
-static int
-same_trees (MonoInst *a, MonoInst *b)
-{
-       int arity;
-       if (a->opcode != b->opcode)
-               return FALSE;
-       arity = mono_burg_arity [a->opcode];
-       if (arity == 1) {
-               if (a->ssa_op == b->ssa_op && a->ssa_op == MONO_SSA_LOAD && a->inst_i0 == b->inst_i0)
-                       return TRUE;
-               return same_trees (a->inst_left, b->inst_left);
-       } else if (arity == 2) {
-               return same_trees (a->inst_left, b->inst_left) && same_trees (a->inst_right, b->inst_right);
-       } else if (arity == 0) {
-               switch (a->opcode) {
-               case OP_ICONST:
-                       return a->inst_c0 == b->inst_c0;
-               default:
-                       return FALSE;
-               }
-       }
-       return FALSE;
-}
-
-static int
-get_unsigned_condbranch (int opcode)
-{
-       switch (opcode) {
-       case CEE_BLE: return CEE_BLE_UN;
-       case CEE_BLT: return CEE_BLT_UN;
-       case CEE_BGE: return CEE_BGE_UN;
-       case CEE_BGT: return CEE_BGT_UN;
-       }
-       g_assert_not_reached ();
-       return 0;
-}
-
-static int
-tree_is_unsigned (MonoInst* ins) {
-       switch (ins->opcode) {
-       case OP_ICONST:
-               return (int)ins->inst_c0 >= 0;
-       /* array lengths are positive as are string sizes */
-       case CEE_LDLEN:
-       case OP_STRLEN:
-               return TRUE;
-       case CEE_CONV_U1:
-       case CEE_CONV_U2:
-       case CEE_CONV_U4:
-       case CEE_CONV_OVF_U1:
-       case CEE_CONV_OVF_U2:
-       case CEE_CONV_OVF_U4:
-               return TRUE;
-       case CEE_LDIND_U1:
-       case CEE_LDIND_U2:
-       case CEE_LDIND_U4:
-               return TRUE;
-       default:
-               return FALSE;
-       }
-}
-
-/* check if an unsigned compare can be used instead of two signed compares
- * for (val < 0 || val > limit) conditionals.
- * Returns TRUE if the optimization has been applied.
- * Note that this can't be applied if the second arg is not positive...
- */
-static int
-try_unsigned_compare (MonoCompile *cfg, MonoBasicBlock *bb)
-{
-       MonoBasicBlock *truet, *falset;
-       MonoInst *cmp_inst = bb->last_ins->inst_left;
-       MonoInst *condb;
-       if (!cmp_inst->inst_right->inst_c0 == 0)
-               return FALSE;
-       truet = bb->last_ins->inst_true_bb;
-       falset = bb->last_ins->inst_false_bb;
-       if (falset->in_count != 1)
-               return FALSE;
-       condb = falset->last_ins;
-       /* target bb must have one instruction */
-       if (!condb || (condb != falset->code))
-               return FALSE;
-       if ((((condb->opcode == CEE_BLE || condb->opcode == CEE_BLT) && (condb->inst_false_bb == truet))
-                       || ((condb->opcode == CEE_BGE || condb->opcode == CEE_BGT) && (condb->inst_true_bb == truet)))
-                       && same_trees (cmp_inst->inst_left, condb->inst_left->inst_left)) {
-               if (!tree_is_unsigned (condb->inst_left->inst_right))
-                       return FALSE;
-               condb->opcode = get_unsigned_condbranch (condb->opcode);
-               /* change the original condbranch to just point to the new unsigned check */
-               bb->last_ins->opcode = OP_BR;
-               bb->last_ins->inst_target_bb = falset;
-               replace_out_block (bb, truet, NULL);
-               replace_in_block (truet, bb, NULL);
-               return TRUE;
-       }
-       return FALSE;
-}
-
 /*
  * Optimizes the branches on the Control Flow Graph
  *
@@ -1090,15 +1259,6 @@ mono_optimize_branches (MonoCompile *cfg)
 
                                /* conditional branches where true and false targets are the same can be also replaced with OP_BR */
                                if (bb->last_ins && (bb->last_ins->opcode != OP_BR) && MONO_IS_COND_BRANCH_OP (bb->last_ins)) {
-                                       if (!cfg->new_ir) {
-                                               MonoInst *pop;
-                                               MONO_INST_NEW (cfg, pop, CEE_POP);
-                                               pop->inst_left = bb->last_ins->inst_left->inst_left;
-                                               mono_add_ins_to_end (bb, pop);
-                                               MONO_INST_NEW (cfg, pop, CEE_POP);
-                                               pop->inst_left = bb->last_ins->inst_left->inst_right;
-                                               mono_add_ins_to_end (bb, pop);
-                                       }
                                        bb->last_ins->opcode = OP_BR;
                                        bb->last_ins->inst_target_bb = bb->last_ins->inst_true_bb;
                                        changed = TRUE;
@@ -1170,16 +1330,12 @@ mono_optimize_branches (MonoCompile *cfg)
                                        int branch_result;
                                        MonoBasicBlock *taken_branch_target = NULL, *untaken_branch_target = NULL;
 
-                                       if (cfg->new_ir) {
-                                               if (bb->last_ins->flags & MONO_INST_CFOLD_TAKEN)
-                                                       branch_result = BRANCH_TAKEN;
-                                               else if (bb->last_ins->flags & MONO_INST_CFOLD_NOT_TAKEN)
-                                                       branch_result = BRANCH_NOT_TAKEN;
-                                               else
-                                                       branch_result = BRANCH_UNDEF;
-                                       }
+                                       if (bb->last_ins->flags & MONO_INST_CFOLD_TAKEN)
+                                               branch_result = BRANCH_TAKEN;
+                                       else if (bb->last_ins->flags & MONO_INST_CFOLD_NOT_TAKEN)
+                                               branch_result = BRANCH_NOT_TAKEN;
                                        else
-                                               branch_result = mono_eval_cond_branch (bb->last_ins);
+                                               branch_result = BRANCH_UNDEF;
 
                                        if (branch_result == BRANCH_TAKEN) {
                                                taken_branch_target = bb->last_ins->inst_true_bb;
@@ -1252,7 +1408,7 @@ mono_optimize_branches (MonoCompile *cfg)
                                         * would require addition of an extra branch at the end of bbn 
                                         * slowing down loops.
                                         */
-                                       if (cfg->new_ir && bbn && bb->region == bbn->region && bbn->in_count == 1 && cfg->enable_extended_bblocks && bbn != cfg->bb_exit && !bb->extended && !bbn->out_of_line && !mono_bblocks_linked (bbn, bb)) {
+                                       if (bbn && bb->region == bbn->region && bbn->in_count == 1 && cfg->enable_extended_bblocks && bbn != cfg->bb_exit && !bb->extended && !bbn->out_of_line && !mono_bblocks_linked (bbn, bb)) {
                                                g_assert (bbn->in_bb [0] == bb);
                                                if (cfg->verbose_level > 2)
                                                        g_print ("merge false branch target triggered BB%d -> BB%d\n", bb->block_num, bbn->block_num);
@@ -1262,15 +1418,6 @@ mono_optimize_branches (MonoCompile *cfg)
                                        }
                                }
 
-                               /* detect and optimize to unsigned compares checks like: if (v < 0 || v > limit */
-                               if (bb->last_ins && bb->last_ins->opcode == CEE_BLT && !cfg->new_ir && bb->last_ins->inst_left->inst_right->opcode == OP_ICONST) {
-                                       if (try_unsigned_compare (cfg, bb)) {
-                                               /*g_print ("applied in bb %d (->%d) %s\n", bb->block_num, bb->last_ins->inst_target_bb->block_num, mono_method_full_name (cfg->method, TRUE));*/
-                                               changed = TRUE;
-                                               continue;
-                                       }
-                               }
-
                                if (bb->last_ins && MONO_IS_COND_BRANCH_NOFP (bb->last_ins)) {
                                        if (bb->last_ins->inst_false_bb && bb->last_ins->inst_false_bb->out_of_line && (bb->region == bb->last_ins->inst_false_bb->region)) {
                                                /* Reverse the branch */
@@ -1289,3 +1436,5 @@ mono_optimize_branches (MonoCompile *cfg)
                }
        } while (changed && (niterations > 0));
 }
+
+#endif /* DISABLE_JIT */