[jit] Rename MonoJitInfo.used_regs to 'unwind_info' to better reflect its current...
[mono.git] / mono / mini / ssa.c
index 799aa892a34f1328bf169ff83b04f91851e692c7..801826b96e8b098caf1a5f102a3cde6b0aabce33 100644 (file)
@@ -5,6 +5,7 @@
  *    Dietmar Maurer (dietmar@ximian.com)
  *
  * (C) 2003 Ximian, Inc.
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
  */
 #include <config.h>
 #include <string.h>
@@ -268,7 +269,7 @@ mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, gboole
                        }
                        else if (G_UNLIKELY (!var && lvreg_defined [ins->dreg] && (ins->dreg >= MONO_MAX_IREGS))) {
                                /* Perform renaming for local vregs */
-                               lvreg_stack [ins->dreg] = mono_alloc_preg (cfg);
+                               lvreg_stack [ins->dreg] = vreg_is_ref (cfg, ins->dreg) ? mono_alloc_ireg_ref (cfg) : mono_alloc_preg (cfg);
                                ins->dreg = lvreg_stack [ins->dreg];
                        }
                        else
@@ -340,8 +341,6 @@ mono_ssa_compute (MonoCompile *cfg)
 
        g_assert (!(cfg->comp_done & MONO_COMP_SSA));
 
-       /* we dont support methods containing exception clauses */
-       g_assert (mono_method_get_header (cfg->method)->num_clauses == 0);
        g_assert (!cfg->disable_ssa);
 
        if (cfg->verbose_level >= 4)
@@ -383,7 +382,7 @@ mono_ssa_compute (MonoCompile *cfg)
                MonoInst *var = cfg->varinfo [i];
 
 #if SIZEOF_REGISTER == 4
-               if (var->type == STACK_I8)
+               if (var->type == STACK_I8 && !COMPILE_LLVM (cfg))
                        continue;
 #endif
                if (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT))
@@ -407,7 +406,7 @@ mono_ssa_compute (MonoCompile *cfg)
 
                        /* fixme: create pruned SSA? we would need liveness information for that */
 
-                       if (bb == cfg->bb_exit && !cfg->compile_llvm)
+                       if (bb == cfg->bb_exit && !COMPILE_LLVM (cfg))
                                continue;
 
                        if ((cfg->comp_done & MONO_COMP_LIVENESS) && !mono_bitset_test_fast (bb->live_in_set, i)) {
@@ -472,6 +471,8 @@ mono_ssa_compute (MonoCompile *cfg)
        mono_ssa_rename_vars (cfg, cfg->num_varinfo, cfg->bb_entry, originals, stack, lvreg_stack, lvreg_defined, stack_history, stack_history_size);
        g_free (stack_history);
        g_free (originals);
+       g_free (lvreg_stack);
+       g_free (lvreg_defined);
 
        if (cfg->verbose_level >= 4)
                printf ("\nEND COMPUTE SSA.\n\n");
@@ -483,7 +484,7 @@ void
 mono_ssa_remove (MonoCompile *cfg)
 {
        MonoInst *ins, *var, *move;
-       int i, j, first;
+       int bbindex, i, j, first;
 
        g_assert (cfg->comp_done & MONO_COMP_SSA);
 
@@ -551,12 +552,12 @@ mono_ssa_remove (MonoCompile *cfg)
         * can coalesce them into the original variable.
         */
 
-       for (i = 0; i < cfg->num_bblocks; ++i) {
-               MonoBasicBlock *bb = cfg->bblocks [i];
+       for (bbindex = 0; bbindex < cfg->num_bblocks; ++bbindex) {
+               MonoBasicBlock *bb = cfg->bblocks [bbindex];
 
                for (ins = bb->code; ins; ins = ins->next) {
                        const char *spec = INS_INFO (ins->opcode);
-                       int num_sregs, j;
+                       int num_sregs;
                        int sregs [MONO_MAX_SRC_REGS];
 
                        if (ins->opcode == OP_NOP)
@@ -580,7 +581,7 @@ mono_ssa_remove (MonoCompile *cfg)
                        }
 
                        num_sregs = mono_inst_get_src_registers (ins, sregs);
-                       for (j = 0; j < num_sregs; ++j) {
+                       for (i = 0; i < num_sregs; ++i) {
                                MonoInst *var = get_vreg_to_inst (cfg, sregs [i]);
 
                                if (var) {
@@ -683,7 +684,6 @@ mono_ssa_copyprop (MonoCompile *cfg)
                                /* Rewrite all uses of var to be uses of var2 */
                                int dreg = var->dreg;
                                int sreg1 = var2->dreg;
-                               const char *spec;
 
                                l = info->uses;
                                while (l) {
@@ -693,8 +693,6 @@ mono_ssa_copyprop (MonoCompile *cfg)
                                        int num_sregs;
                                        int sregs [MONO_MAX_SRC_REGS];
 
-                                       spec = INS_INFO (ins->opcode);
-
                                        num_sregs = mono_inst_get_src_registers (ins, sregs);
                                        for (i = 0; i < num_sregs; ++i) {
                                                if (sregs [i] == dreg)
@@ -1107,7 +1105,7 @@ fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray
 
                                /* Unlink target bblocks */
                                for (i = 0; i < table->table_size; ++i) {
-                                       if (i != idx) {
+                                       if (table->table [i] != table->table [idx]) {
                                                remove_bb_from_phis (cfg, bb, table->table [i]);
                                                mono_unlink_bblock (cfg, bb, table->table [i]);
                                        }
@@ -1166,6 +1164,15 @@ mono_ssa_cprop (MonoCompile *cfg)
                        info->cpstate = 2;
        }
 
+       for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
+               /*
+                * FIXME: This should be bb->flags & BB_FLAG_EXCEPTION_HANDLER, but
+                * that would still allow unreachable try's to be removed.
+                */
+               if (bb->region)
+                       add_cprop_bb (cfg, bb, &bblock_list);
+       }
+
        cvars = NULL;
 
        while (bblock_list) {
@@ -1177,11 +1184,13 @@ mono_ssa_cprop (MonoCompile *cfg)
 
                g_assert (bb->flags &  BB_REACHABLE);
 
-               if (bb->out_count == 1) {
-                       if (!(bb->out_bb [0]->flags &  BB_REACHABLE)) {
-                               bb->out_bb [0]->flags |= BB_REACHABLE;
-                               bblock_list = g_list_prepend (bblock_list, bb->out_bb [0]);
-                       }
+               /* 
+                * Some bblocks are linked to 2 others even through they fall through to the
+                * next bblock.
+                */
+               if (!(bb->last_ins && MONO_IS_BRANCH_OP (bb->last_ins))) {
+                       for (i = 0; i < bb->out_count; ++i)
+                               add_cprop_bb (cfg, bb->out_bb [i], &bblock_list);
                }
 
                if (cfg->verbose_level > 1)
@@ -1341,4 +1350,120 @@ mono_ssa_strength_reduction (MonoCompile *cfg)
 }
 #endif
 
+void
+mono_ssa_loop_invariant_code_motion (MonoCompile *cfg)
+{
+       MonoBasicBlock *bb, *h, *idom;
+       MonoInst *ins, *n, *tins;
+       int i;
+
+       g_assert (cfg->comp_done & MONO_COMP_SSA);
+       if (!(cfg->comp_done & MONO_COMP_LOOPS) || !(cfg->comp_done & MONO_COMP_SSA_DEF_USE))
+               return;
+
+       for (bb = cfg->bb_entry->next_bb; bb; bb = bb->next_bb) {
+               GList *lp = bb->loop_blocks;
+
+               if (!lp)
+                       continue;
+               h = (MonoBasicBlock *)lp->data;
+               if (bb != h)
+                       continue;
+               MONO_BB_FOR_EACH_INS_SAFE (bb, n, ins) {
+                       gboolean is_class_init = FALSE;
+
+                       /*
+                        * Try to move instructions out of loop headers into the preceeding bblock.
+                        */
+                       if (ins->opcode == OP_VOIDCALL) {
+                               MonoCallInst *call = (MonoCallInst*)ins;
+
+                               if (call->fptr_is_patch) {
+                                       MonoJumpInfo *ji = (MonoJumpInfo*)call->fptr;
+
+                                       if (ji->type == MONO_PATCH_INFO_CLASS_INIT)
+                                               is_class_init = TRUE;
+                               }
+                       }
+                       if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS || ins->opcode == OP_AOTCONST || is_class_init) {
+                               gboolean skip;
+                               int sreg;
+
+                               idom = h->idom;
+                               /*
+                                * h->nesting is needed to work around:
+                                * http://llvm.org/bugs/show_bug.cgi?id=17868
+                                */
+                               if (!(idom && idom->last_ins && idom->last_ins->opcode == OP_BR && idom->last_ins->inst_target_bb == h && h->nesting == 1)) {
+                                       continue;
+                               }
+
+                               /*
+                                * Make sure there are no instructions with side effects before ins.
+                                */
+                               skip = FALSE;
+                               MONO_BB_FOR_EACH_INS (bb, tins) {
+                                       if (tins == ins)
+                                               break;
+                                       if (!MONO_INS_HAS_NO_SIDE_EFFECT (tins)) {
+                                               skip = TRUE;
+                                               break;
+                                       }
+                               }
+                               if (skip) {
+                                       /*
+                                         printf ("%s\n", mono_method_full_name (cfg->method, TRUE));
+                                         mono_print_ins (tins);
+                                       */
+                                       continue;
+                               }
+
+                               /* Make sure we don't move the instruction before the def of its sreg */
+                               if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS)
+                                       sreg = ins->sreg1;
+                               else
+                                       sreg = -1;
+                               if (sreg != -1) {
+                                       MonoInst *tins, *var;
+
+                                       skip = FALSE;
+                                       for (tins = ins->prev; tins; tins = tins->prev) {
+                                               const char *spec = INS_INFO (tins->opcode);
+
+                                               if (tins->opcode == OP_MOVE && tins->dreg == sreg) {
+                                                       sreg = tins->sreg1;
+                                               } if (spec [MONO_INST_DEST] != ' ' && tins->dreg == sreg) {
+                                                       skip = TRUE;
+                                                       break;
+                                               }
+                                       }
+                                       if (skip)
+                                               continue;
+                                       var = get_vreg_to_inst (cfg, sreg);
+                                       if (var && (var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
+                                               continue;
+                                       ins->sreg1 = sreg;
+                               }
+
+                               if (cfg->verbose_level > 1) {
+                                       printf ("licm in BB%d on ", bb->block_num);
+                                       mono_print_ins (ins);
+                               }
+                               //{ static int count = 0; count ++; printf ("%d\n", count); }
+                               MONO_REMOVE_INS (bb, ins);
+                               mono_bblock_insert_before_ins (idom, idom->last_ins, ins);
+                               if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN)
+                                       idom->has_array_access = TRUE;
+                       }
+               }
+       }
+
+       cfg->comp_done &=  ~MONO_COMP_SSA_DEF_USE;
+       for (i = 0; i < cfg->num_varinfo; i++) {
+               MonoMethodVar *info = MONO_VARINFO (cfg, i);
+               info->def = NULL;
+               info->uses = NULL;
+       }
+}
+
 #endif /* DISABLE_JIT */