[jit] Fix fbd9fd71679f626fabe0b1bd7f923836f98252b4, a wrong version was commited.
[mono.git] / mono / mini / branch-opts.c
index 88c82bd28e053a265a88adbd05789ff69864aff4..bd2b2d804581152078c8cf1f68904e6b2711cc26 100644 (file)
@@ -5,6 +5,7 @@
  *   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"
 
@@ -32,8 +33,7 @@ mono_bb_is_fall_through (MonoCompile *cfg, MonoBasicBlock *bb)
 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;
@@ -94,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 {
@@ -132,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) {
@@ -308,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;
@@ -318,18 +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;
-                       }
-                       if (!(compare->next && MONO_IS_COND_BRANCH_OP (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;
-                       branch = compare->next;
 
                        true_bb = branch->inst_true_bb;
                        false_bb = branch->inst_false_bb;
@@ -474,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;
@@ -512,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);
@@ -537,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);
@@ -599,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;
                        }
                }
@@ -738,7 +755,8 @@ mono_if_conversion (MonoCompile *cfg)
                mono_handle_global_vregs (cfg);
                if (cfg->opt & (MONO_OPT_CONSPROP | MONO_OPT_COPYPROP))
                        mono_local_cprop (cfg);
-               mono_local_deadce (cfg);
+               if (cfg->opt & MONO_OPT_DEADCE)
+                       mono_local_deadce (cfg);
        }
 #endif
 }
@@ -798,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) {
@@ -951,21 +978,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)) {
@@ -976,6 +1009,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;
@@ -986,6 +1022,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) {
@@ -996,6 +1033,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
@@ -1243,7 +1290,7 @@ 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) {
+                                       if (bb->last_ins && bb->last_ins->opcode == OP_BR && !bbn->out_of_line) {
                                                bb->last_ins->opcode = OP_NOP;
                                                changed = TRUE;
                                                if (cfg->verbose_level > 2)
@@ -1392,7 +1439,7 @@ mono_optimize_branches (MonoCompile *cfg)
                                }
 
                                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;