Tweak liveness when instructions are used in environments.

Instructions remain live when debuggable, but only instructions
with object types remain live when non-debuggable.

Enable StackVisitor::GetThisObject for optimizing.

Change-Id: Id87b2cbf33a02450059acc9993995782e5f28987
diff --git a/compiler/optimizing/ssa_liveness_analysis.h b/compiler/optimizing/ssa_liveness_analysis.h
index 5787f0c..b57029d 100644
--- a/compiler/optimizing/ssa_liveness_analysis.h
+++ b/compiler/optimizing/ssa_liveness_analysis.h
@@ -302,7 +302,7 @@
       first_range_->start_ = from;
     } else {
       // Instruction without uses.
-      DCHECK(!defined_by_->HasUses());
+      DCHECK(!defined_by_->HasNonEnvironmentUses());
       DCHECK(from == defined_by_->GetLifetimePosition());
       first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
     }
@@ -798,6 +798,22 @@
   DISALLOW_COPY_AND_ASSIGN(LiveInterval);
 };
 
+/**
+ * Analysis that computes the liveness of instructions:
+ *
+ * (a) Non-environment uses of an instruction always make
+ *     the instruction live.
+ * (b) Environment uses of an instruction whose type is
+ *     object (that is, non-primitive), make the instruction live.
+ *     This is due to having to keep alive objects that have
+ *     finalizers deleting native objects.
+ * (c) When the graph has the debuggable property, environment uses
+ *     of an instruction that has a primitive type make the instruction live.
+ *     If the graph does not have the debuggable property, the environment
+ *     use has no effect, and may get a 'none' value after register allocation.
+ *
+ * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
+ */
 class SsaLivenessAnalysis : public ValueObject {
  public:
   SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
@@ -882,6 +898,12 @@
   // Update the live_out set of the block and returns whether it has changed.
   bool UpdateLiveOut(const HBasicBlock& block);
 
+  static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
+    if (instruction == nullptr) return false;
+    if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
+    return instruction->GetType() == Primitive::kPrimNot;
+  }
+
   const HGraph& graph_;
   CodeGenerator* const codegen_;
   GrowableArray<HBasicBlock*> linear_order_;