New test.
[mono.git] / mono / mini / ssa.c
index f346975b07cd156471f7560f378b5ce3c28cb668..c8718a74107be5ae0c3851646ab542a129414ebc 100644 (file)
@@ -11,8 +11,6 @@
 
 #include "mini.h"
 
-extern guint8 mono_burg_arity [];
-
 #define USE_ORIGINAL_VARS
 #define CREATE_PRUNED_SSA
 
@@ -104,7 +102,7 @@ replace_usage (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **
 
        arity = mono_burg_arity [inst->opcode];
 
-       if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_MAYBE_LOAD) && 
+       if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) && 
            (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
                MonoInst *new_var;
                int idx = inst->inst_i0->inst_c0;
@@ -213,7 +211,7 @@ mono_ssa_rename_vars (MonoCompile *cfg, int max_vars, MonoBasicBlock *bb, MonoIn
                        idx = inst->inst_i0->inst_c0;
                        g_assert (idx < max_vars);
 
-                       if (!stack [idx] && bb == cfg->bb_init) {
+                       if ((!stack [idx]) && (bb == cfg->bb_init) && (inst->inst_i0->opcode != OP_ARG)) {
                                new_var = cfg->varinfo [idx];
                        } else {
                                new_var = mono_compile_create_var (cfg, inst->inst_i0->inst_vtype,  inst->inst_i0->opcode);
@@ -338,6 +336,8 @@ mono_ssa_compute (MonoCompile *cfg)
             
                        store->next = bb->code;
                        bb->code = store;
+                       if (!bb->last_ins)
+                               bb->last_ins = bb->code;
 
 #ifdef DEBUG_SSA
                        printf ("ADD PHI BB%d %s\n", cfg->bblocks [idx]->block_num, mono_method_full_name (cfg->method, TRUE));
@@ -416,7 +416,7 @@ mono_ssa_replace_copies (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, c
 
        arity = mono_burg_arity [inst->opcode];
 
-       if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_MAYBE_LOAD || inst->ssa_op == MONO_SSA_STORE) && 
+       if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN || inst->ssa_op == MONO_SSA_STORE) && 
            (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
                MonoInst *new_var;
                int idx = inst->inst_i0->inst_c0;
@@ -579,16 +579,37 @@ typedef struct {
        MonoInst *inst;
 } MonoVarUsageInfo;
 
-static void
+
+
+
+/*
+ * Returns TRUE if the tree can have side effects.
+ */
+static gboolean
 analyze_dev_use (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *root, MonoInst *inst)
 {
        MonoMethodVar *info;
        int i, idx, arity;
+       gboolean has_side_effects;
 
        if (!inst)
-               return;
+               return FALSE;
 
        arity = mono_burg_arity [inst->opcode];
+       switch (inst->opcode) {
+#define ANALYZE_DEV_USE_SPECIFIC_OPS 1
+#define OPDEF(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) case a1:
+#include "simple-cee-ops.h"
+#undef OPDEF
+#define MINI_OP(a1,a2) case a1:
+#include "simple-mini-ops.h"
+#undef MINI_OP
+#undef ANALYZE_DEV_USE_SPECIFIC_OPS
+               has_side_effects = FALSE;
+               break;
+       default:
+               has_side_effects = TRUE;
+       }
 
        if ((inst->ssa_op == MONO_SSA_STORE) && 
            (inst->inst_i0->opcode == OP_LOCAL /*|| inst->inst_i0->opcode == OP_ARG */)) {
@@ -619,7 +640,7 @@ analyze_dev_use (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *root, MonoInst
                }
        }
 
-       if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_MAYBE_LOAD) && 
+       if ((inst->ssa_op == MONO_SSA_LOAD || inst->ssa_op == MONO_SSA_ADDRESS_TAKEN) && 
            (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
                MonoVarUsageInfo *ui = mono_mempool_alloc (cfg->mempool, sizeof (MonoVarUsageInfo));
                idx = inst->inst_i0->inst_c0;   
@@ -631,11 +652,15 @@ analyze_dev_use (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *root, MonoInst
        } else {
                if (arity) {
                        //if (inst->ssa_op != MONO_SSA_STORE)
-                       analyze_dev_use (cfg, bb, root, inst->inst_left);
+                       if (analyze_dev_use (cfg, bb, root, inst->inst_left))
+                               has_side_effects = TRUE;
                        if (arity > 1)
-                               analyze_dev_use (cfg, bb, root, inst->inst_right);
+                               if (analyze_dev_use (cfg, bb, root, inst->inst_right))
+                                       has_side_effects = TRUE;
                }
        }
+       
+       return has_side_effects;
 }
 
 
@@ -704,7 +729,11 @@ mono_ssa_create_def_use (MonoCompile *cfg)
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {
                MonoInst *inst;
                for (inst = bb->code; inst; inst = inst->next) {
-                       analyze_dev_use (cfg, bb, inst, inst);
+                       gboolean has_side_effects = analyze_dev_use (cfg, bb, inst, inst);
+                       if (has_side_effects && (inst->ssa_op == MONO_SSA_STORE) && 
+                                       (inst->inst_i0->opcode == OP_LOCAL || inst->inst_i0->opcode == OP_ARG)) {
+                               inst->inst_i0->flags |= MONO_INST_DEFINITION_HAS_SIDE_EFFECTS;
+                       }
                }
        }
 
@@ -907,7 +936,7 @@ fold_tree (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, MonoInst **carr
                        inst->opcode = CEE_BR;
                        inst->inst_target_bb = target;
                }
-       } else if (inst->opcode == CEE_SWITCH && evaluate_const_tree (cfg, inst->inst_left, &a, carray) == 1) {
+       } else if (inst->opcode == CEE_SWITCH && (evaluate_const_tree (cfg, inst->inst_left, &a, carray) == 1) && (a >= 0) && (a < GPOINTER_TO_INT (inst->klass))) {
                bb->out_bb [0] = inst->inst_many_bb [a];
                bb->out_count = 1;
                inst->inst_target_bb = bb->out_bb [0];
@@ -943,8 +972,11 @@ visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, GList **cvars,
 
        if (inst->opcode == CEE_SWITCH) {
                int r1, i, a;
+               int cases = GPOINTER_TO_INT (inst->klass);
 
                r1 = evaluate_const_tree (cfg, inst->inst_left, &a, carray);
+               if ((r1 == 1) && ((a < 0) || (a >= cases)))
+                       r1 = 2;
                if (r1 == 1) {
                        MonoBasicBlock *tb = inst->inst_many_bb [a];
                        if (!(tb->flags &  BB_REACHABLE)) {
@@ -952,7 +984,7 @@ visit_inst (MonoCompile *cfg, MonoBasicBlock *bb, MonoInst *inst, GList **cvars,
                                *bblist = g_list_prepend (*bblist, tb);
                        }
                } else if (r1 == 2) {
-                       for (i = (int)inst->klass; i >= 0; i--) {
+                       for (i = GPOINTER_TO_INT (inst->klass); i >= 0; i--) {
                                MonoBasicBlock *tb = inst->inst_many_bb [i];
                                if (!(tb->flags &  BB_REACHABLE)) {
                                        tb->flags |= BB_REACHABLE;
@@ -1168,13 +1200,15 @@ mono_ssa_deadce (MonoCompile *cfg)
        for (i = 0; i < cfg->num_varinfo; i++) {
                MonoMethodVar *info = cfg->vars [i];
                work_list = g_list_prepend (work_list, info);
+               
+               //if ((info->def != NULL) && (info->def->inst_i1->opcode != OP_PHI)) printf ("SSA DEADCE TOTAL LOCAL\n");
        }
 
        while (work_list) {
                MonoMethodVar *info = (MonoMethodVar *)work_list->data;
                work_list = g_list_delete_link (work_list, work_list);
 
-               if (!info->uses && info->def && (!(cfg->varinfo [info->idx]->flags & (MONO_INST_VOLATILE|MONO_INST_INDIRECT)))) {
+               if (!info->uses && info->def && (!(cfg->varinfo [info->idx]->flags & (MONO_INST_DEFINITION_HAS_SIDE_EFFECTS|MONO_INST_VOLATILE|MONO_INST_INDIRECT)))) {
                        MonoInst *i1;
                        //printf ("ELIMINATE %s: ", mono_method_full_name (cfg->method, TRUE)); mono_print_tree (info->def); printf ("\n");
 
@@ -1190,6 +1224,7 @@ mono_ssa_deadce (MonoCompile *cfg)
                                        MonoMethodVar *u = cfg->vars [i1->inst_i0->inst_c0];
                                        add_to_dce_worklist (cfg, info, u, &work_list);
                        }
+                       //if (i1->opcode != OP_PHI) printf ("SSA DEADCE DEAD LOCAL\n");
 
                        info->def->opcode = CEE_NOP;
                        info->def->ssa_op = MONO_SSA_NOP;