Make the SSA builder honor the debuggable flag.

This requires to properly type phis that are only
used by environments, and discard phis with incomptable types.
The code generators do not handle these conflicting types. In
the process, ensure a phi has a type that does not depend
on the order of the inputs (for example (char, short) -> short),
and set int for int-like types. We can refine this later.

Change-Id: I60ab601d6d00b1cbf18623ee4ff1795aa28f84a1
diff --git a/test/457-regs/expected.txt b/test/457-regs/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/457-regs/expected.txt
diff --git a/test/457-regs/info.txt b/test/457-regs/info.txt
new file mode 100644
index 0000000..d950003
--- /dev/null
+++ b/test/457-regs/info.txt
@@ -0,0 +1 @@
+Tests debuggability of DEX registers.
diff --git a/test/457-regs/regs_jni.cc b/test/457-regs/regs_jni.cc
new file mode 100644
index 0000000..ce701e8
--- /dev/null
+++ b/test/457-regs/regs_jni.cc
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "arch/context.h"
+#include "jni.h"
+#include "mirror/art_method-inl.h"
+#include "scoped_thread_state_change.h"
+#include "stack.h"
+#include "thread.h"
+
+namespace art {
+
+namespace {
+
+class TestVisitor : public StackVisitor {
+ public:
+  TestVisitor(Thread* thread, Context* context)
+      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
+      : StackVisitor(thread, context) {}
+
+  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    mirror::ArtMethod* m = GetMethod();
+    std::string m_name(m->GetName());
+
+    if (m_name.compare("mergeOk") == 0) {
+      uint32_t value = 0;
+
+      CHECK(GetVReg(m, 0, kIntVReg, &value));
+      CHECK_EQ(value, 0u);
+
+      CHECK(GetVReg(m, 1, kIntVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      CHECK(GetVReg(m, 2, kIntVReg, &value));
+      CHECK_EQ(value, 2u);
+
+      CHECK(GetVReg(m, 3, kIntVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      CHECK(GetVReg(m, 4, kIntVReg, &value));
+      CHECK_EQ(value, 2u);
+      did_check_ = true;
+    } else if (m_name.compare("mergeNotOk") == 0) {
+      uint32_t value = 0;
+
+      CHECK(GetVReg(m, 0, kIntVReg, &value));
+      CHECK_EQ(value, 0u);
+
+      CHECK(GetVReg(m, 1, kIntVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      bool success = GetVReg(m, 2, kIntVReg, &value);
+      if (m->IsOptimized(sizeof(void*))) CHECK(!success);
+
+      CHECK(GetVReg(m, 3, kReferenceVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      CHECK(GetVReg(m, 4, kFloatVReg, &value));
+      uint32_t cast = bit_cast<float, uint32_t>(4.0f);
+      CHECK_EQ(value, cast);
+      did_check_ = true;
+    } else if (m_name.compare("phiEquivalent") == 0) {
+      uint32_t value = 0;
+
+      CHECK(GetVReg(m, 0, kIntVReg, &value));
+      // Quick doesn't like this one on x64.
+      CHECK_EQ(value, 0u);
+
+      CHECK(GetVReg(m, 1, kIntVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      CHECK(GetVReg(m, 2, kFloatVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      did_check_ = true;
+    } else if (m_name.compare("mergeReferences") == 0) {
+      uint32_t value = 0;
+
+      CHECK(GetVReg(m, 0, kIntVReg, &value));
+      CHECK_EQ(value, 0u);
+
+      CHECK(GetVReg(m, 1, kIntVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      CHECK(GetVReg(m, 2, kReferenceVReg, &value));
+      CHECK_EQ(value, 0u);
+
+      CHECK(GetVReg(m, 3, kReferenceVReg, &value));
+      CHECK_NE(value, 0u);
+
+      did_check_ = true;
+    } else if (m_name.compare("phiAllEquivalents") == 0) {
+      uint32_t value = 0;
+
+      CHECK(GetVReg(m, 0, kIntVReg, &value));
+      CHECK_EQ(value, 0u);
+
+      CHECK(GetVReg(m, 1, kIntVReg, &value));
+      CHECK_EQ(value, 1u);
+
+      CHECK(GetVReg(m, 2, kReferenceVReg, &value));
+      CHECK_EQ(value, 0u);
+
+      did_check_ = true;
+    }
+
+    return true;
+  }
+
+  bool did_check_ = false;
+};
+
+extern "C" JNIEXPORT void JNICALL Java_PhiLiveness_regsNativeCall(
+    JNIEnv*, jclass value ATTRIBUTE_UNUSED) {
+  ScopedObjectAccess soa(Thread::Current());
+  std::unique_ptr<Context> context(Context::Create());
+  TestVisitor visitor(soa.Self(), context.get());
+  visitor.WalkStack();
+  CHECK(visitor.did_check_);
+}
+
+extern "C" JNIEXPORT void JNICALL Java_PhiLiveness_regsNativeCallWithParameters(
+    JNIEnv*, jclass value ATTRIBUTE_UNUSED, jobject main, jint int_value, jfloat float_value) {
+  ScopedObjectAccess soa(Thread::Current());
+  std::unique_ptr<Context> context(Context::Create());
+  CHECK(soa.Decode<mirror::Object*>(main) == nullptr);
+  CHECK_EQ(int_value, 0);
+  int32_t cast = bit_cast<float, int32_t>(float_value);
+  CHECK_EQ(cast, 0);
+  TestVisitor visitor(soa.Self(), context.get());
+  visitor.WalkStack();
+  CHECK(visitor.did_check_);
+}
+
+}  // namespace
+
+}  // namespace art
diff --git a/test/457-regs/smali/PhiLiveness.smali b/test/457-regs/smali/PhiLiveness.smali
new file mode 100644
index 0000000..c8a6773
--- /dev/null
+++ b/test/457-regs/smali/PhiLiveness.smali
@@ -0,0 +1,82 @@
+# Copyright (C) 2015 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+.class public LPhiLiveness;
+
+.super Ljava/lang/Object;
+
+.method public static mergeOk(ZB)V
+   .registers 5
+   const/4 v0, 0x0
+   const/4 v1, 0x1
+   move v2, v3
+   if-eq v1, v0, :else
+   move v2, v4
+   :else
+   invoke-static {}, LPhiLiveness;->regsNativeCall()V
+   return-void
+.end method
+
+.method public static mergeNotOk(ZF)V
+   .registers 5
+   const/4 v0, 0x0
+   const/4 v1, 0x1
+   move v2, v3
+   if-eq v1, v0, :else
+   move v2, v4
+   :else
+   invoke-static {}, LPhiLiveness;->regsNativeCall()V
+   return-void
+.end method
+
+.method public static mergeReferences(LMain;)V
+   .registers 4
+   const/4 v0, 0x0
+   const/4 v1, 0x1
+   move-object v2, p0
+   if-eq v1, v0, :else
+   move v2, v0
+   :else
+   invoke-static {}, LPhiLiveness;->regsNativeCall()V
+   return-void
+.end method
+
+.method public static phiEquivalent()F
+   .registers 5
+   const/4 v0, 0x0
+   const/4 v1, 0x1
+   move v2, v0
+   if-eq v1, v0, :else
+   move v2, v1
+   :else
+   invoke-static {}, LPhiLiveness;->regsNativeCall()V
+   return v2
+.end method
+
+.method public static phiAllEquivalents(LMain;)V
+   .registers 4
+   const/4 v0, 0x0
+   const/4 v1, 0x1
+   move v2, v0
+   if-eq v1, v0, :else
+   move v2, v0
+   :else
+   invoke-static {v2, v2, v2}, LPhiLiveness;->regsNativeCallWithParameters(LMain;IF)V
+   return-void
+.end method
+
+.method public static native regsNativeCall()V
+.end method
+.method public static native regsNativeCallWithParameters(LMain;IF)V
+.end method
diff --git a/test/457-regs/src/Main.java b/test/457-regs/src/Main.java
new file mode 100644
index 0000000..0d82033
--- /dev/null
+++ b/test/457-regs/src/Main.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.reflect.Method;
+
+public class Main {
+
+  // Workaround for b/18051191.
+  class InnerClass {}
+
+  public static void main(String[] args) throws Exception {
+    Class<?> c = Class.forName("PhiLiveness");
+    Method m = c.getMethod("mergeOk", boolean.class, byte.class);
+    m.invoke(null, new Boolean(true), new Byte((byte)2));
+
+    m = c.getMethod("mergeNotOk", boolean.class, float.class);
+    m.invoke(null, new Boolean(true), new Float(4.0f));
+
+    m = c.getMethod("mergeReferences", Main.class);
+    m.invoke(null, new Main());
+
+    m = c.getMethod("phiEquivalent");
+    m.invoke(null);
+
+    m = c.getMethod("phiAllEquivalents", Main.class);
+    m.invoke(null, new Main());
+  }
+
+  static {
+    System.loadLibrary("arttest");
+  }
+}
diff --git a/test/Android.libarttest.mk b/test/Android.libarttest.mk
index 75c5d72..f4bab3f 100644
--- a/test/Android.libarttest.mk
+++ b/test/Android.libarttest.mk
@@ -29,7 +29,8 @@
   117-nopatchoat/nopatchoat.cc \
   118-noimage-dex2oat/noimage-dex2oat.cc \
   454-get-vreg/get_vreg_jni.cc \
-  455-set-vreg/set_vreg_jni.cc
+  455-set-vreg/set_vreg_jni.cc \
+  457-regs/regs_jni.cc
 
 ART_TARGET_LIBARTTEST_$(ART_PHONY_TEST_TARGET_SUFFIX) += $(ART_TARGET_TEST_OUT)/$(TARGET_ARCH)/libarttest.so
 ifdef TARGET_2ND_ARCH
diff --git a/test/Android.run-test.mk b/test/Android.run-test.mk
index da36056..740961e 100644
--- a/test/Android.run-test.mk
+++ b/test/Android.run-test.mk
@@ -321,7 +321,8 @@
 TEST_ART_BROKEN_NDEBUG_TESTS :=
 
 # Known broken tests for the default compiler (Quick).
-TEST_ART_BROKEN_DEFAULT_RUN_TESTS :=
+TEST_ART_BROKEN_DEFAULT_RUN_TESTS := \
+  457-regs
 
 ifneq (,$(filter default,$(COMPILER_TYPES)))
   ART_TEST_KNOWN_BROKEN += $(call all-run-test-names,$(TARGET_TYPES),$(RUN_TYPES),$(PREBUILD_TYPES), \
@@ -377,6 +378,18 @@
 
 TEST_ART_BROKEN_OPTIMIZING_RUN_TESTS :=
 
+# Tests that should fail when the optimizing compiler compiles them non-debuggable.
+TEST_ART_BROKEN_OPTIMIZING_NONDEBUGGABLE_RUN_TESTS := \
+  457-regs \
+
+ifneq (,$(filter optimizing,$(COMPILER_TYPES)))
+  ART_TEST_KNOWN_BROKEN += $(call all-run-test-names,$(TARGET_TYPES),$(RUN_TYPES),$(PREBUILD_TYPES), \
+      optimizing,$(RELOCATE_TYPES),$(TRACE_TYPES),$(GC_TYPES),$(JNI_TYPES), \
+      $(IMAGE_TYPES),$(PICTEST_TYPES),nondebuggable,$(TEST_ART_BROKEN_OPTIMIZING_NONDEBUGGABLE_RUN_TESTS),$(ALL_ADDRESS_SIZES))
+endif
+
+TEST_ART_BROKEN_OPTIMIZING_NONDEBUGGABLE_RUN_TESTS :=
+
 
 # Clear variables ahead of appending to them when defining tests.
 $(foreach target, $(TARGET_TYPES), $(eval ART_RUN_TEST_$(call name-to-var,$(target))_RULES :=))