Merge pull request #199 from slide/master
[mono.git] / mono / mini / abcremoval.c
index a130276f7d27e8e5074a136d37306ac1ad55884d..5e97cba1516fa5fedebc55f7fdbf4901e0e8a17d 100644 (file)
@@ -14,6 +14,8 @@
 #include <mono/metadata/opcodes.h>
 #include <mono/metadata/mempool-internals.h>
 
+#include <config.h>
+
 #ifndef DISABLE_JIT
 
 #include "abcremoval.h"
@@ -314,6 +316,7 @@ get_relation_from_ins (MonoVariableRelationsEvaluationArea *area, MonoInst *ins,
                value->type = MONO_VARIABLE_SUMMARIZED_VALUE;
                value->value.variable.variable = ins->sreg1;
                value->value.variable.delta = 0;
+               area->defs [ins->dreg] = ins;
                break;
 
                /* FIXME: Add more opcodes */
@@ -997,6 +1000,34 @@ remove_abc_from_inst (MonoInst *ins, MonoVariableRelationsEvaluationArea *area)
        }
 }
 
+static gboolean
+eval_non_null (MonoVariableRelationsEvaluationArea *area, int reg)
+{
+       MonoRelationsEvaluationContext *context = &(area->contexts [reg]);
+
+       clean_contexts (area->contexts, area->cfg->next_vreg);
+       evaluate_relation_with_target_variable (area, reg, reg, NULL);
+                               
+       return context->ranges.zero.lower > 0;
+}
+
+static void
+add_non_null (MonoVariableRelationsEvaluationArea *area, MonoCompile *cfg, int reg,
+                         GSList **check_relations)
+{
+       MonoAdditionalVariableRelation *rel;
+
+       rel = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoAdditionalVariableRelation));
+       rel->variable = reg;
+       rel->relation.relation = MONO_GT_RELATION;
+       rel->relation.related_value.type = MONO_CONSTANT_SUMMARIZED_VALUE;
+       rel->relation.related_value.value.constant.value = 0;
+
+       apply_change_to_evaluation_area (area, rel);
+
+       *check_relations = g_slist_append_mempool (cfg->mempool, *check_relations, rel);
+}
+
 /*
  * Process a BB removing bounds checks from array accesses.
  * It does the following (in sequence):
@@ -1022,7 +1053,7 @@ process_block (MonoCompile *cfg, MonoBasicBlock *bb, MonoVariableRelationsEvalua
                printf ("\nProcessing block %d [dfn %d]...\n", bb->block_num, bb->dfn);
        }
 
-       if (bb->region)
+       if (bb->region != -1)
                return;
 
        get_relations_from_previous_bb (area, bb, &additional_relations);
@@ -1087,28 +1118,57 @@ process_block (MonoCompile *cfg, MonoBasicBlock *bb, MonoVariableRelationsEvalua
                }
 
                if (ins->opcode == OP_CHECK_THIS) {
-                       MonoRelationsEvaluationContext *context = &(area->contexts [ins->sreg1]);
-
-                       clean_contexts (area->contexts, area->cfg->next_vreg);
-                       evaluate_relation_with_target_variable (area, ins->sreg1, ins->sreg1, NULL);
-                               
-                       if (context->ranges.zero.lower > 0) {
+                       if (eval_non_null (area, ins->sreg1)) {
                                if (REPORT_ABC_REMOVAL)
                                        printf ("ARRAY-ACCESS: removed check_this instruction.\n");
                                NULLIFY_INS (ins);
                        }
                }
 
-               if (ins->opcode == OP_NOT_NULL) {
-                       rel = mono_mempool_alloc0 (cfg->mempool, sizeof (MonoAdditionalVariableRelation));
-                       rel->variable = ins->sreg1;
-                       rel->relation.relation = MONO_GT_RELATION;
-                       rel->relation.related_value.type = MONO_CONSTANT_SUMMARIZED_VALUE;
-                       rel->relation.related_value.value.constant.value = 0;
+               if (ins->opcode == OP_NOT_NULL)
+                       add_non_null (area, cfg, ins->sreg1, &check_relations);
 
-                       apply_change_to_evaluation_area (area, rel);
-
-                       check_relations = g_slist_append_mempool (cfg->mempool, check_relations, rel);
+               /* 
+                * FIXME: abcrem equates an array with its length,
+                * so a = new int [100] implies a != null, but a = new int [0] doesn't.
+                */
+               /*
+                * Eliminate MONO_INST_FAULT flags if possible.
+                */
+               if (COMPILE_LLVM (cfg) && (ins->opcode == OP_LDLEN ||
+                                                                  ins->opcode == OP_BOUNDS_CHECK ||
+                                                                  ins->opcode == OP_STRLEN ||
+                                                                  (MONO_IS_LOAD_MEMBASE (ins) && (ins->flags & MONO_INST_FAULT)) ||
+                                                                  (MONO_IS_STORE_MEMBASE (ins) && (ins->flags & MONO_INST_FAULT)))) {
+                       int reg;
+
+                       if (MONO_IS_STORE_MEMBASE (ins))
+                               reg = ins->inst_destbasereg;
+                       else if (MONO_IS_LOAD_MEMBASE (ins))
+                               reg = ins->inst_basereg;
+                       else
+                               reg = ins->sreg1;
+
+                       /*
+                        * This doesn't work because LLVM can move the non-faulting loads before the faulting
+                        * ones (test_0_llvm_moving_faulting_loads ()).
+                        * So only do it if we know the load cannot be moved before the instruction which ensures it is not
+                        * null (i.e. the def of its sreg).
+                        */
+                       if (area->defs [reg] && area->defs [reg]->opcode == OP_NEWARR) {
+                               if (REPORT_ABC_REMOVAL)
+                                       printf ("ARRAY-ACCESS: removed MONO_INST_FAULT flag.\n");
+                               ins->flags &= ~MONO_INST_FAULT;
+                       }
+                       /*
+                       if (eval_non_null (area, reg)) {
+                               if (REPORT_ABC_REMOVAL)
+                                       printf ("ARRAY-ACCESS: removed MONO_INST_FAULT flag.\n");
+                               ins->flags &= ~MONO_INST_FAULT;
+                       } else {
+                               add_non_null (area, cfg, reg, &check_relations);
+                       }
+                       */
                }
        }       
        
@@ -1203,12 +1263,14 @@ mono_perform_abc_removal (MonoCompile *cfg)
                mono_mempool_alloc (cfg->mempool, sizeof (MonoRelationsEvaluationContext) * (cfg->next_vreg));
        area.variable_value_kind = (MonoIntegerValueKind *)
                mono_mempool_alloc (cfg->mempool, sizeof (MonoIntegerValueKind) * (cfg->next_vreg));
+       area.defs = mono_mempool_alloc (cfg->mempool, sizeof (MonoInst*) * cfg->next_vreg);
        for (i = 0; i < cfg->next_vreg; i++) {
                area.variable_value_kind [i] = MONO_UNKNOWN_INTEGER_VALUE;
                area.relations [i].relation = MONO_EQ_RELATION;
                area.relations [i].relation_is_static_definition = TRUE;
                MAKE_VALUE_ANY (area.relations [i].related_value);
                area.relations [i].next = NULL;
+               area.defs [i] = NULL;
        }
 
        for (bb = cfg->bb_entry; bb; bb = bb->next_bb) {