[generics.cs] enable one more test for interpreter
[mono.git] / mono / mini / ssa.c
index 25bc360bcab93864c78dd834aa573ff5e3f35068..d0b34b2781d177be5a558e56491edf369130ba84 100644 (file)
@@ -5,12 +5,15 @@
  *    Dietmar Maurer (dietmar@ximian.com)
  *
  * (C) 2003 Ximian, Inc.
+ * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 #include <config.h>
 #include <string.h>
 #include <mono/metadata/debug-helpers.h>
 #include <mono/metadata/mempool.h>
 #include <mono/metadata/mempool-internals.h>
+#include <mono/utils/mono-compiler.h>
 
 #ifndef DISABLE_JIT
 
 #include <alloca.h>
 #endif
 
-#define USE_ORIGINAL_VARS
 #define CREATE_PRUNED_SSA
 
 //#define DEBUG_SSA 1
 
 #define NEW_PHI(cfg,dest,val) do {     \
-               (dest) = mono_mempool_alloc0 ((cfg)->mempool, sizeof (MonoInst));       \
-               (dest)->opcode = OP_PHI;        \
-               (dest)->inst_c0 = (val);        \
-        (dest)->dreg = (dest)->sreg1 = (dest)->sreg2 = -1; \
+       MONO_INST_NEW ((cfg), (dest), OP_PHI); \
+       (dest)->inst_c0 = (val);                                                           \
        } while (0)
 
 typedef struct {
@@ -143,7 +143,7 @@ static inline void
 record_use (MonoCompile *cfg, MonoInst *var, MonoBasicBlock *bb, MonoInst *ins)
 {
        MonoMethodVar *info;
-       MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
+       MonoVarUsageInfo *ui = (MonoVarUsageInfo *)mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
 
        info = MONO_VARINFO (cfg, var->inst_c0);
        
@@ -238,8 +238,13 @@ mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, gboole
                                if (var->opcode == OP_ARG)
                                        originals_used [idx] = TRUE;
 
-                               /* FIXME: */
-                               g_assert (stack_history_len < stack_history_size);
+                               if (stack_history_len + 128 > stack_history_size) {
+                                       stack_history_size += 1024;
+                                       RenameInfo *new_history = mono_mempool_alloc (cfg->mempool, sizeof (RenameInfo) * stack_history_size);
+                                       memcpy (new_history, stack_history, stack_history_len * sizeof (RenameInfo));
+                                       stack_history = new_history;
+                               }
+
                                stack_history [stack_history_len].var = stack [idx];
                                stack_history [stack_history_len].idx = idx;
                                stack_history_len ++;
@@ -268,7 +273,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 +345,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)
@@ -356,7 +359,7 @@ mono_ssa_compute (MonoCompile *cfg)
        mono_compile_dominator_info (cfg, MONO_COMP_DOM | MONO_COMP_IDOM | MONO_COMP_DFRONTIER);
 
        bitsize = mono_bitset_alloc_size (cfg->num_bblocks, 0);
-       buf = buf_start = g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
+       buf = buf_start = (guint8 *)g_malloc0 (mono_bitset_alloc_size (cfg->num_bblocks, 0) * cfg->num_varinfo);
 
        for (i = 0; i < cfg->num_varinfo; ++i) {
                vinfo [i].def_in = mono_bitset_mem_new (buf, cfg->num_bblocks, 0);
@@ -383,7 +386,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 +410,7 @@ mono_ssa_compute (MonoCompile *cfg)
 
                        /* fixme: create pruned SSA? we would need liveness information for that */
 
-                       if (bb == cfg->bb_exit)
+                       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)) {
@@ -430,11 +433,15 @@ mono_ssa_compute (MonoCompile *cfg)
                                break;
                        case STACK_VTYPE:
                                ins->opcode = MONO_CLASS_IS_SIMD (cfg, var->klass) ? OP_XPHI : OP_VPHI;
-                               ins->klass = var->klass;
                                break;
                        }
 
-                       ins->inst_phi_args =  mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
+                       if (var->inst_vtype->byref)
+                               ins->klass = mono_defaults.int_class;
+                       else
+                               ins->klass = var->klass;
+
+                       ins->inst_phi_args = (int *)mono_mempool_alloc0 (cfg->mempool, sizeof (int) * (cfg->bblocks [idx]->in_count + 1));
                        ins->inst_phi_args [0] = cfg->bblocks [idx]->in_count;
 
                        /* For debugging */
@@ -457,7 +464,7 @@ mono_ssa_compute (MonoCompile *cfg)
 
        /* Renaming phase */
 
-       stack = alloca (sizeof (MonoInst *) * cfg->num_varinfo);
+       stack = (MonoInst **)alloca (sizeof (MonoInst *) * cfg->num_varinfo);
        memset (stack, 0, sizeof (MonoInst *) * cfg->num_varinfo);
 
        lvreg_stack = g_new0 (guint32, cfg->next_vreg);
@@ -468,6 +475,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");
@@ -475,12 +484,79 @@ mono_ssa_compute (MonoCompile *cfg)
        cfg->comp_done |= MONO_COMP_SSA;
 }
 
+/*
+ * mono_ssa_remove_gsharedvt:
+ *
+ *   Same as mono_ssa_remove, but only remove phi nodes for gsharedvt variables.
+ */
 void
-mono_ssa_remove (MonoCompile *cfg)
+mono_ssa_remove_gsharedvt (MonoCompile *cfg)
 {
        MonoInst *ins, *var, *move;
        int i, j, first;
 
+       /*
+        * When compiling gsharedvt code, we need to get rid of the VPHI instructions,
+        * since they cannot be handled later in the llvm backend.
+        */
+       g_assert (cfg->comp_done & MONO_COMP_SSA);
+
+       for (i = 0; i < cfg->num_bblocks; ++i) {
+               MonoBasicBlock *bb = cfg->bblocks [i];
+
+               if (cfg->verbose_level >= 4)
+                       printf ("\nREMOVE SSA %d:\n", bb->block_num);
+
+               for (ins = bb->code; ins; ins = ins->next) {
+                       if (!(MONO_IS_PHI (ins) && ins->opcode == OP_VPHI && mini_is_gsharedvt_variable_type (&ins->klass->byval_arg)))
+                               continue;
+
+                       g_assert (ins->inst_phi_args [0] == bb->in_count);
+                       var = get_vreg_to_inst (cfg, ins->dreg);
+
+                       /* Check for PHI nodes where all the inputs are the same */
+                       first = ins->inst_phi_args [1];
+
+                       for (j = 1; j < bb->in_count; ++j)
+                               if (first != ins->inst_phi_args [j + 1])
+                                       break;
+
+                       if ((bb->in_count > 1) && (j == bb->in_count)) {
+                               ins->opcode = op_phi_to_move (ins->opcode);
+                               if (ins->opcode == OP_VMOVE)
+                                       g_assert (ins->klass);
+                               ins->sreg1 = first;
+                       } else {
+                               for (j = 0; j < bb->in_count; j++) {
+                                       MonoBasicBlock *pred = bb->in_bb [j];
+                                       int sreg = ins->inst_phi_args [j + 1];
+
+                                       if (cfg->verbose_level >= 4)
+                                               printf ("\tADD R%d <- R%d in BB%d\n", var->dreg, sreg, pred->block_num);
+                                       if (var->dreg != sreg) {
+                                               MONO_INST_NEW (cfg, move, op_phi_to_move (ins->opcode));
+                                               if (move->opcode == OP_VMOVE) {
+                                                       g_assert (ins->klass);
+                                                       move->klass = ins->klass;
+                                               }
+                                               move->dreg = var->dreg;
+                                               move->sreg1 = sreg;
+                                               mono_add_ins_to_end (pred, move);
+                                       }
+                               }
+
+                               NULLIFY_INS (ins);
+                       }
+               }
+       }
+}
+
+void
+mono_ssa_remove (MonoCompile *cfg)
+{
+       MonoInst *ins, *var, *move;
+       int bbindex, i, j, first;
+
        g_assert (cfg->comp_done & MONO_COMP_SSA);
 
        for (i = 0; i < cfg->num_bblocks; ++i) {
@@ -525,8 +601,7 @@ mono_ssa_remove (MonoCompile *cfg)
                                                }
                                        }
 
-                                       ins->opcode = OP_NOP;
-                                       ins->dreg = -1;
+                                       NULLIFY_INS (ins);
                                }
                        }
                }
@@ -547,12 +622,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)
@@ -576,7 +651,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) {
@@ -679,7 +754,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) {
@@ -689,8 +763,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)
@@ -939,7 +1011,7 @@ visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars,
 
                if (MONO_IS_JUMP_TABLE (ins)) {
                        int i;
-                       MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
+                       MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)MONO_JUMP_TABLE_FROM_INS (ins);
 
                        if (!ins->next || ins->next->opcode != OP_PADD) {
                                /* The PADD was optimized away */
@@ -975,7 +1047,7 @@ visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, GList **cvars,
 
                if (ins->opcode == OP_SWITCH) {
                        int i;
-                       MonoJumpInfoBBTable *table = ins->inst_p0;
+                       MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)ins->inst_p0;
 
                        for (i = 0; i < table->table_size; i++)
                                if (table->table [i])
@@ -1051,7 +1123,7 @@ fold_ins (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *ins, MonoInst **carray
 
                if (MONO_IS_JUMP_TABLE (ins)) {
                        int i;
-                       MonoJumpInfoBBTable *table = MONO_JUMP_TABLE_FROM_INS (ins);
+                       MonoJumpInfoBBTable *table = (MonoJumpInfoBBTable *)MONO_JUMP_TABLE_FROM_INS (ins);
 
                        if (!ins->next || ins->next->opcode != OP_PADD) {
                                /* The PADD was optimized away */
@@ -1103,7 +1175,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]);
                                        }
@@ -1162,6 +1234,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) {
@@ -1173,11 +1254,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)
@@ -1274,12 +1357,10 @@ mono_ssa_deadce (MonoCompile *cfg)
                                MonoInst *src_var = get_vreg_to_inst (cfg, def->sreg1);
                                if (src_var && !(src_var->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))
                                        add_to_dce_worklist (cfg, info, MONO_VARINFO (cfg, src_var->inst_c0), &work_list);
-                               def->opcode = OP_NOP;
-                               def->dreg = def->sreg1 = def->sreg2 = -1;
+                               NULLIFY_INS (def);
                                info->reg = -1;
                        } else if ((def->opcode == OP_ICONST) || (def->opcode == OP_I8CONST) || MONO_IS_ZERO (def)) {
-                               def->opcode = OP_NOP;
-                               def->dreg = def->sreg1 = def->sreg2 = -1;
+                               NULLIFY_INS (def);
                                info->reg = -1;
                        } else if (MONO_IS_PHI (def)) {
                                int j;
@@ -1287,8 +1368,7 @@ mono_ssa_deadce (MonoCompile *cfg)
                                        MonoMethodVar *u = MONO_VARINFO (cfg, get_vreg_to_inst (cfg, def->inst_phi_args [j])->inst_c0);
                                        add_to_dce_worklist (cfg, info, u, &work_list);
                                }
-                               def->opcode = OP_NOP;
-                               def->dreg = def->sreg1 = def->sreg2 = -1;
+                               NULLIFY_INS (def);
                                info->reg = -1;
                        }
                        else if (def->opcode == OP_NOP) {
@@ -1337,4 +1417,112 @@ mono_ssa_strength_reduction (MonoCompile *cfg)
 }
 #endif
 
-#endif /* DISABLE_JIT */
+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) {
+                       /*
+                        * Try to move instructions out of loop headers into the preceeding bblock.
+                        */
+                       if (ins->opcode == OP_LDLEN || ins->opcode == OP_STRLEN || ins->opcode == OP_CHECK_THIS || ins->opcode == OP_AOTCONST || ins->opcode == OP_GENERIC_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;
+       }
+}
+
+#else /* !DISABLE_JIT */
+
+MONO_EMPTY_SOURCE_FILE (ssa);
+
+#endif /* !DISABLE_JIT */