[runtime] Simplify mono-debug.c since we no longer have to serialize a lot of the...
[mono.git] / mono / mini / branch-opts.c
index ff90a9a885ce59a17d04699dde0c708b34b49e70..2be95e736424d43c562b3e52612273e3372788f1 100644 (file)
@@ -5,12 +5,25 @@
  *   Patrik Torstensson (Patrik.Torstesson at gmail.com)
  *
  * (C) 2005 Ximian, Inc.  http://www.ximian.com
+ * Copyright 2011 Xamarin Inc.  http://www.xamarin.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
@@ -20,8 +33,7 @@
 MonoInst *
 mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, const char * exname)
 {
-       MonoMethod *method = cfg->method;
-       MonoMethodHeader *header = mono_method_get_header (method);
+       MonoMethodHeader *header = cfg->header;
        MonoExceptionClause *clause;
        MonoClass *exclass;
        int i;
@@ -82,6 +94,9 @@ mono_branch_optimize_exception_target (MonoCompile *cfg, MonoBasicBlock *bb, con
                                                return jump;
                                        } 
 
+                                       return NULL;
+                               } else {
+                                       /* Branching to an outer clause could skip inner clauses */
                                        return NULL;
                                }
                        } else {
@@ -120,7 +135,7 @@ static const int long_cmov_opcodes [] = {
        OP_CMOV_LGT_UN
 };
 
-static int
+static G_GNUC_UNUSED int
 br_to_br_un (int opcode)
 {
        switch (opcode) {
@@ -296,7 +311,7 @@ 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;
@@ -306,16 +321,14 @@ 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;
 
                        true_bb = branch->inst_true_bb;
                        false_bb = branch->inst_false_bb;
@@ -460,7 +473,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;
@@ -498,16 +511,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);
@@ -523,6 +535,13 @@ mono_if_conversion (MonoCompile *cfg)
                        if (cfg->ret && ins1->dreg == cfg->ret->dreg)
                                continue;
 
+                       if (!(cfg->opt & MONO_OPT_DEADCE))
+                               /* 
+                                * It is possible that dreg is never set before, so we can't use
+                                * it as an sreg of the cmov instruction (#582322).
+                                */
+                               continue;
+
                        if (cfg->verbose_level > 2) {
                                printf ("\tBranch -> CMove optimization (2) in BB%d on\n", bb->block_num);
                                printf ("\t\t"); mono_print_ins (compare);
@@ -585,6 +604,18 @@ 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;
                        }
                }
@@ -723,8 +754,9 @@ 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_deadce (cfg);
+                       mono_local_cprop (cfg);
+               if (cfg->opt & MONO_OPT_DEADCE)
+                       mono_local_deadce (cfg);
        }
 #endif
 }
@@ -784,6 +816,15 @@ replace_in_block (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl
 static void
 replace_out_block_in_code (MonoBasicBlock *bb, MonoBasicBlock *orig, MonoBasicBlock *repl) {
        MonoInst *ins;
+
+#if defined(__native_client_codegen__)
+       /* Need to maintain this flag for the new block because */
+       /* we can't jump indirectly to a non-aligned block.     */
+       if (orig->flags & BB_INDIRECT_JUMP_TARGET)
+       {
+               repl->flags |= BB_INDIRECT_JUMP_TARGET;
+       }
+#endif
        
        for (ins = bb->code; ins != NULL; ins = ins->next) {
                switch (ins->opcode) {
@@ -898,11 +939,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) ||
-                               (!MONO_IS_BRANCH_OP (previous_bb->last_ins)))) {
+               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;
@@ -934,6 +971,12 @@ mono_merge_basic_blocks (MonoCompile *cfg, MonoBasicBlock *bb, MonoBasicBlock *b
        bb->has_array_access |= bbn->has_array_access;
        bb->extended |= bbn->extended;
 
+       /* Compute prev_bb if possible to avoid the linear search below */
+       prev_bb = NULL;
+       for (i = 0; i < bbn->in_count; ++i)
+               if (bbn->in_bb [0]->next_bb == bbn)
+                       prev_bb = bbn->in_bb [0];
+
        mono_unlink_bblock (cfg, bb, bbn);
        for (i = 0; i < bbn->out_count; ++i)
                mono_link_bblock (cfg, bb, bbn->out_bb [i]);
@@ -941,21 +984,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)) {
@@ -966,6 +1015,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;
@@ -976,8 +1028,11 @@ 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) {
+               for (prev_bb = cfg->bb_entry; prev_bb && prev_bb->next_bb != bbn; prev_bb = prev_bb->next_bb)
+                       ;
+       }
        if (prev_bb) {
                prev_bb->next_bb = bbn->next_bb;
        } else {
@@ -986,6 +1041,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
@@ -1056,7 +1121,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");
                }
@@ -1067,30 +1132,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) ||
-                                                                       !MONO_IS_BRANCH_OP (previous_bb->last_ins)) {
-                                                               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 {
@@ -1106,10 +1171,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);
                                                        }
@@ -1156,116 +1225,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
  *
@@ -1321,15 +1287,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;
@@ -1341,8 +1298,8 @@ mono_optimize_branches (MonoCompile *cfg)
                                        /* the block are in sequence anyway ... */
 
                                        /* branches to the following block can be removed */
-                                       if (bb->last_ins && bb->last_ins->opcode == OP_BR) {
-                                               bb->last_ins->opcode = OP_NOP;
+                                       if (bb->last_ins && bb->last_ins->opcode == OP_BR && !bbn->out_of_line) {
+                                               NULLIFY_INS (bb->last_ins);
                                                changed = TRUE;
                                                if (cfg->verbose_level > 2)
                                                        g_print ("br removal triggered %d -> %d\n", bb->block_num, bbn->block_num);
@@ -1401,16 +1358,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;
@@ -1483,7 +1436,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);
@@ -1493,17 +1446,8 @@ 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)) {
+                                       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) && !cfg->disable_out_of_line_bblocks) {
                                                /* Reverse the branch */
                                                bb->last_ins->opcode = mono_reverse_branch_op (bb->last_ins->opcode);
                                                bbn = bb->last_ins->inst_false_bb;