Adjust safepoint position of implicit null checks.

The safepoint records stack masks and register masks. For implicit
null checks, they need to have their position to the instruction
doing the null check (the next one).

Test: org.apache.harmony.tests.java.io.BufferedWriterTest#test_write_LStringII_Exception
Change-Id: I347d9fe38fb9df23fbc00f9aa36f1ee9c54bf912
diff --git a/compiler/optimizing/register_allocator_linear_scan.cc b/compiler/optimizing/register_allocator_linear_scan.cc
index 216fb57..1e00003 100644
--- a/compiler/optimizing/register_allocator_linear_scan.cc
+++ b/compiler/optimizing/register_allocator_linear_scan.cc
@@ -312,7 +312,7 @@
 
   for (size_t safepoint_index = safepoints_.size(); safepoint_index > 0; --safepoint_index) {
     HInstruction* safepoint = safepoints_[safepoint_index - 1u];
-    size_t safepoint_position = safepoint->GetLifetimePosition();
+    size_t safepoint_position = SafepointPosition::ComputePosition(safepoint);
 
     // Test that safepoints are ordered in the optimal way.
     DCHECK(safepoint_index == safepoints_.size() ||
diff --git a/compiler/optimizing/ssa_liveness_analysis.h b/compiler/optimizing/ssa_liveness_analysis.h
index cebd4ad..dfe6103 100644
--- a/compiler/optimizing/ssa_liveness_analysis.h
+++ b/compiler/optimizing/ssa_liveness_analysis.h
@@ -230,12 +230,25 @@
       : instruction_(instruction),
         next_(nullptr) {}
 
+  static size_t ComputePosition(HInstruction* instruction) {
+    // We special case instructions emitted at use site, as their
+    // safepoint position needs to be at their use.
+    if (instruction->IsEmittedAtUseSite()) {
+      // Currently only applies to implicit null checks, which are emitted
+      // at the next instruction.
+      DCHECK(instruction->IsNullCheck()) << instruction->DebugName();
+      return instruction->GetLifetimePosition() + 2;
+    } else {
+      return instruction->GetLifetimePosition();
+    }
+  }
+
   void SetNext(SafepointPosition* next) {
     next_ = next;
   }
 
   size_t GetPosition() const {
-    return instruction_->GetLifetimePosition();
+    return ComputePosition(instruction_);
   }
 
   SafepointPosition* GetNext() const {
@@ -922,7 +935,7 @@
     if (first_safepoint_ == nullptr) {
       first_safepoint_ = last_safepoint_ = safepoint;
     } else {
-      DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
+      DCHECK_LE(last_safepoint_->GetPosition(), safepoint->GetPosition());
       last_safepoint_->SetNext(safepoint);
       last_safepoint_ = safepoint;
     }