Merge "JDWP: assert no pending exception when using JNI"
diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc
index e864ae1..cda5c1a 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -2478,7 +2478,7 @@
   LocationSummary* locations = not_->GetLocations();
   Location out = locations->Out();
   Location in = locations->InAt(0);
-  switch (not_->InputAt(0)->GetType()) {
+  switch (not_->GetResultType()) {
     case Primitive::kPrimInt:
       __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
       break;
diff --git a/compiler/optimizing/code_generator_arm64.cc b/compiler/optimizing/code_generator_arm64.cc
index 0d7864f..729bab7 100644
--- a/compiler/optimizing/code_generator_arm64.cc
+++ b/compiler/optimizing/code_generator_arm64.cc
@@ -2202,7 +2202,7 @@
 }
 
 void InstructionCodeGeneratorARM64::VisitNot(HNot* instruction) {
-  switch (instruction->InputAt(0)->GetType()) {
+  switch (instruction->GetResultType()) {
     case Primitive::kPrimInt:
     case Primitive::kPrimLong:
       __ Mvn(OutputRegister(instruction), InputOperandAt(instruction, 0));
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 1101569..7b35cfd 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -2575,7 +2575,7 @@
   Location in = locations->InAt(0);
   Location out = locations->Out();
   DCHECK(in.Equals(out));
-  switch (not_->InputAt(0)->GetType()) {
+  switch (not_->GetResultType()) {
     case Primitive::kPrimInt:
       __ notl(out.AsRegister<Register>());
       break;
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 41a19e1..74adb31 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -2514,7 +2514,7 @@
   DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
             locations->Out().AsRegister<CpuRegister>().AsRegister());
   Location out = locations->Out();
-  switch (not_->InputAt(0)->GetType()) {
+  switch (not_->GetResultType()) {
     case Primitive::kPrimInt:
       __ notl(out.AsRegister<CpuRegister>());
       break;
diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc
index 93787b8..e51bbc3 100644
--- a/compiler/optimizing/nodes.cc
+++ b/compiler/optimizing/nodes.cc
@@ -893,28 +893,32 @@
     exit_block_->ReplaceWith(to);
 
     // Update all predecessors of the exit block (now the `to` block)
-    // to not `HReturn` but `HGoto` instead. Also collect the return
-    // values if any, and potentially make it a phi if there are multiple
-    // predecessors.
+    // to not `HReturn` but `HGoto` instead.
     HInstruction* return_value = nullptr;
-    for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
-      HBasicBlock* predecessor = to->GetPredecessors().Get(i);
+    bool returns_void = to->GetPredecessors().Get(0)->GetLastInstruction()->IsReturnVoid();
+    if (to->GetPredecessors().Size() == 1) {
+      HBasicBlock* predecessor = to->GetPredecessors().Get(0);
       HInstruction* last = predecessor->GetLastInstruction();
-      if (!last->IsReturnVoid()) {
-        if (return_value != nullptr) {
-          if (!return_value->IsPhi()) {
-            HPhi* phi = new (allocator) HPhi(allocator, kNoRegNumber, 0, invoke->GetType());
-            to->AddPhi(phi);
-            phi->AddInput(return_value);
-            return_value = phi;
-          }
-          return_value->AsPhi()->AddInput(last->InputAt(0));
-        } else {
-          return_value = last->InputAt(0);
-        }
+      if (!returns_void) {
+        return_value = last->InputAt(0);
       }
       predecessor->AddInstruction(new (allocator) HGoto());
       predecessor->RemoveInstruction(last);
+    } else {
+      if (!returns_void) {
+        // There will be multiple returns.
+        return_value = new (allocator) HPhi(allocator, kNoRegNumber, 0, invoke->GetType());
+        to->AddPhi(return_value->AsPhi());
+      }
+      for (size_t i = 0, e = to->GetPredecessors().Size(); i < e; ++i) {
+        HBasicBlock* predecessor = to->GetPredecessors().Get(i);
+        HInstruction* last = predecessor->GetLastInstruction();
+        if (!returns_void) {
+          return_value->AsPhi()->AddInput(last->InputAt(0));
+        }
+        predecessor->AddInstruction(new (allocator) HGoto());
+        predecessor->RemoveInstruction(last);
+      }
     }
 
     if (return_value != nullptr) {
diff --git a/compiler/optimizing/nodes.h b/compiler/optimizing/nodes.h
index de448cc..7e07564 100644
--- a/compiler/optimizing/nodes.h
+++ b/compiler/optimizing/nodes.h
@@ -1039,6 +1039,8 @@
   virtual bool CanThrow() const { return false; }
   bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
 
+  virtual bool ActAsNullConstant() const { return false; }
+
   // Does not apply for all instructions, but having this at top level greatly
   // simplifies the null check elimination.
   virtual bool CanBeNull() const {
@@ -1049,10 +1051,14 @@
   virtual bool CanDoImplicitNullCheck() const { return false; }
 
   void SetReferenceTypeInfo(ReferenceTypeInfo reference_type_info) {
+    DCHECK_EQ(GetType(), Primitive::kPrimNot);
     reference_type_info_ = reference_type_info;
   }
 
-  ReferenceTypeInfo GetReferenceTypeInfo() const { return reference_type_info_; }
+  ReferenceTypeInfo GetReferenceTypeInfo() const {
+    DCHECK_EQ(GetType(), Primitive::kPrimNot);
+    return reference_type_info_;
+  }
 
   void AddUseAt(HInstruction* user, size_t index) {
     DCHECK(user != nullptr);
@@ -1838,6 +1844,8 @@
 
   size_t ComputeHashCode() const OVERRIDE { return 0; }
 
+  bool ActAsNullConstant() const OVERRIDE { return true; }
+
   DECLARE_INSTRUCTION(NullConstant);
 
  private:
@@ -1852,11 +1860,16 @@
 
   int32_t GetValue() const { return value_; }
 
-  virtual bool InstructionDataEquals(HInstruction* other) const {
+  bool InstructionDataEquals(HInstruction* other) const OVERRIDE {
     return other->AsIntConstant()->value_ == value_;
   }
 
-  virtual size_t ComputeHashCode() const { return GetValue(); }
+  size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
+
+  // TODO: Null is represented by the `0` constant. In most cases we replace it
+  // with a HNullConstant but we don't do it when comparing (a != null). This
+  // method is an workaround until we fix the above.
+  bool ActAsNullConstant() const OVERRIDE { return value_ == 0; }
 
   DECLARE_INSTRUCTION(IntConstant);
 
@@ -3063,6 +3076,7 @@
   HBoundType(HInstruction* input, ReferenceTypeInfo bound_type)
       : HExpression(Primitive::kPrimNot, SideEffects::None()),
         bound_type_(bound_type) {
+    DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
     SetRawInputAt(0, input);
   }
 
diff --git a/compiler/optimizing/reference_type_propagation.cc b/compiler/optimizing/reference_type_propagation.cc
index 76b8d7e..479b87f 100644
--- a/compiler/optimizing/reference_type_propagation.cc
+++ b/compiler/optimizing/reference_type_propagation.cc
@@ -23,8 +23,6 @@
 
 namespace art {
 
-// TODO: handle: a !=/== null.
-
 void ReferenceTypePropagation::Run() {
   // To properly propagate type info we need to visit in the dominator-based order.
   // Reverse post order guarantees a node's dominators are visited first.
@@ -55,9 +53,46 @@
   }
 
   // Add extra nodes to bound types.
+  BoundTypeForIfNotNull(block);
   BoundTypeForIfInstanceOf(block);
 }
 
+void ReferenceTypePropagation::BoundTypeForIfNotNull(HBasicBlock* block) {
+  HInstruction* lastInstruction = block->GetLastInstruction();
+  if (!lastInstruction->IsIf()) {
+    return;
+  }
+  HInstruction* ifInput = lastInstruction->InputAt(0);
+  if (!ifInput->IsNotEqual() && !ifInput->IsEqual()) {
+    return;
+  }
+  HInstruction* input0 = ifInput->InputAt(0);
+  HInstruction* input1 = ifInput->InputAt(1);
+  HInstruction* obj;
+
+  if ((input0->GetType() == Primitive::kPrimNot) && input1->ActAsNullConstant()) {
+    obj = input0;
+  } else if ((input1->GetType() == Primitive::kPrimNot) && input0->ActAsNullConstant()) {
+    obj = input1;
+  } else {
+    return;
+  }
+
+  HBoundType* bound_type =
+      new (graph_->GetArena()) HBoundType(obj, ReferenceTypeInfo::CreateTop(false));
+
+  block->InsertInstructionBefore(bound_type, lastInstruction);
+  HBasicBlock* notNullBlock = ifInput->IsNotEqual()
+      ? lastInstruction->AsIf()->IfTrueSuccessor()
+      : lastInstruction->AsIf()->IfFalseSuccessor();
+  for (HUseIterator<HInstruction*> it(obj->GetUses()); !it.Done(); it.Advance()) {
+    HInstruction* user = it.Current()->GetUser();
+    if (notNullBlock->Dominates(user->GetBlock())) {
+      user->ReplaceInput(bound_type, it.Current()->GetIndex());
+    }
+  }
+}
+
 // Detects if `block` is the True block for the pattern
 // `if (x instanceof ClassX) { }`
 // If that's the case insert an HBoundType instruction to bound the type of `x`
diff --git a/compiler/optimizing/reference_type_propagation.h b/compiler/optimizing/reference_type_propagation.h
index e346dbf..815caab 100644
--- a/compiler/optimizing/reference_type_propagation.h
+++ b/compiler/optimizing/reference_type_propagation.h
@@ -51,6 +51,7 @@
   void UpdateBoundType(HBoundType* bound_type) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
   void UpdatePhi(HPhi* phi) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
+  void BoundTypeForIfNotNull(HBasicBlock* block);
   void BoundTypeForIfInstanceOf(HBasicBlock* block);
 
   void ProcessWorklist();
diff --git a/test/118-noimage-dex2oat/check b/test/118-noimage-dex2oat/check
new file mode 100755
index 0000000..57111bc
--- /dev/null
+++ b/test/118-noimage-dex2oat/check
@@ -0,0 +1,20 @@
+#!/bin/bash
+#
+# Copyright (C) 2014 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.
+
+# Strip the process pids and line numbers from exact error messages.
+sed -e '/^art E.*\] /d' "$2" > "$2.tmp"
+
+diff --strip-trailing-cr -q "$1" "$2.tmp" >/dev/null
diff --git a/test/444-checker-nce/src/Main.java b/test/444-checker-nce/src/Main.java
index 9fb9c46..656c791 100644
--- a/test/444-checker-nce/src/Main.java
+++ b/test/444-checker-nce/src/Main.java
@@ -214,6 +214,30 @@
     return m;
   }
 
+  // CHECK-START: Main Main.scopeIfNotNullRemove(Main) instruction_simplifier_after_types (before)
+  // CHECK:         NullCheck
+
+  // CHECK-START: Main Main.scopeIfNotNullRemove(Main) instruction_simplifier_after_types (after)
+  // CHECK-NOT:     NullCheck
+  public Main scopeIfNotNullRemove(Main m) {
+    if (m != null) {
+      return m.g();
+    }
+    return m;
+  }
+
+  // CHECK-START: Main Main.scopeIfKeep(Main) instruction_simplifier_after_types (before)
+  // CHECK:         NullCheck
+
+  // CHECK-START: Main Main.scopeIfKeep(Main) instruction_simplifier_after_types (after)
+  // CHECK:         NullCheck
+  public Main scopeIfKeep(Main m) {
+    if (m == null) {
+      m = new Main();
+    }
+    return m.g();
+  }
+
   public Main() {}
   public Main(int dummy) {}
 
diff --git a/test/452-multiple-returns2/expected.txt b/test/452-multiple-returns2/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/452-multiple-returns2/expected.txt
diff --git a/test/452-multiple-returns2/info.txt b/test/452-multiple-returns2/info.txt
new file mode 100644
index 0000000..cdd354b
--- /dev/null
+++ b/test/452-multiple-returns2/info.txt
@@ -0,0 +1,2 @@
+Tests inlining of a pattern not generated by DX: multiple
+returns in a single method.
diff --git a/test/452-multiple-returns2/smali/MultipleReturns.smali b/test/452-multiple-returns2/smali/MultipleReturns.smali
new file mode 100644
index 0000000..ff2b9b0
--- /dev/null
+++ b/test/452-multiple-returns2/smali/MultipleReturns.smali
@@ -0,0 +1,40 @@
+# 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 LMultipleReturns;
+
+.super Ljava/lang/Object;
+
+.method public static caller()I
+   .registers 1
+   invoke-static {},  LMultipleReturns;->$opt$CalleeReturnInt()I
+   move-result v0
+   return v0
+.end method
+
+.method public static $opt$CalleeReturnInt()I
+   .registers 2
+   const/4 v0, 0x0
+   const/4 v1, 0x1
+   if-eq v1, v0, :else
+   if-eq v1, v0, :else2
+   const/4 v0, 0x4
+   :else2
+   return v0
+   :else
+   if-eq v1, v0, :else3
+   const/4 v1, 0x3
+   :else3
+   return v1
+.end method
diff --git a/test/452-multiple-returns2/src/Main.java b/test/452-multiple-returns2/src/Main.java
new file mode 100644
index 0000000..8904c45
--- /dev/null
+++ b/test/452-multiple-returns2/src/Main.java
@@ -0,0 +1,32 @@
+/*
+ * 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("MultipleReturns");
+    Method m = c.getMethod("caller");
+    int result = (Integer)m.invoke(null);
+    if (result != 4) {
+      throw new Error("Expected 4, got " + result);
+    }
+  }
+}
diff --git a/test/453-not-byte/expected.txt b/test/453-not-byte/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/453-not-byte/expected.txt
diff --git a/test/453-not-byte/info.txt b/test/453-not-byte/info.txt
new file mode 100644
index 0000000..2c7f793
--- /dev/null
+++ b/test/453-not-byte/info.txt
@@ -0,0 +1,2 @@
+Regression test for optimizing, which was expecting int only on a not-int instruction.
+
diff --git a/test/453-not-byte/smali/NotByte.smali b/test/453-not-byte/smali/NotByte.smali
new file mode 100644
index 0000000..9165729
--- /dev/null
+++ b/test/453-not-byte/smali/NotByte.smali
@@ -0,0 +1,23 @@
+# 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 LNotByte;
+
+.super Ljava/lang/Object;
+
+.method public static notByte(B)I
+   .registers 1
+   not-int v0, v0
+   return v0
+.end method
diff --git a/test/453-not-byte/src/Main.java b/test/453-not-byte/src/Main.java
new file mode 100644
index 0000000..f055743
--- /dev/null
+++ b/test/453-not-byte/src/Main.java
@@ -0,0 +1,32 @@
+/*
+ * 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("NotByte");
+    Method m = c.getMethod("notByte", byte.class);
+    int result = (Integer)m.invoke(null, (byte)42);
+    if (result != -43) {
+      throw new Error("Expected -43, got " + result);
+    }
+  }
+}
diff --git a/tools/libcore_failures.txt b/tools/libcore_failures.txt
index d74a0cc..4502c02 100644
--- a/tools/libcore_failures.txt
+++ b/tools/libcore_failures.txt
@@ -45,6 +45,33 @@
           "org.apache.harmony.tests.java.text.MessageFormatTest#test19011159"]
 },
 {
+  description: "Failing due to a locale problem on hammerhead.",
+  result: EXEC_FAILED,
+  modes: [device],
+  names: ["libcore.icu.DateIntervalFormatTest#test10089890",
+          "libcore.icu.DateIntervalFormatTest#test10209343_when_not_this_year",
+          "libcore.icu.DateIntervalFormatTest#test10560853_for_single_day_events",
+          "libcore.icu.DateIntervalFormatTest#test10560853_when_time_not_displayed",
+          "libcore.icu.RelativeDateTimeFormatterTest#test_getRelativeDateTimeString",
+          "libcore.icu.RelativeDateTimeFormatterTest#test_getRelativeTimeSpanString",
+          "libcore.icu.RelativeDateTimeFormatterTest#test_getRelativeTimeSpanStringAbbrev",
+          "libcore.java.text.OldDateFormatTest#test_parseLString",
+          "libcore.java.text.SimpleDateFormatTest#testDstZoneNameWithNonDstTimestamp",
+          "libcore.java.text.SimpleDateFormatTest#testDstZoneWithNonDstTimestampForNonHourDstZone",
+          "libcore.java.text.SimpleDateFormatTest#testNonDstZoneNameWithDstTimestamp",
+          "libcore.java.text.SimpleDateFormatTest#testNonDstZoneWithDstTimestampForNonHourDstZone",
+          "org.apache.harmony.tests.java.text.SimpleDateFormatTest#test_parseLjava_lang_StringLjava_text_ParsePosition"]
+},
+{
+  description: "Failing due to switched off network stack on volantisg.",
+  result: EXEC_FAILED,
+  modes: [device],
+  names: ["libcore.javax.crypto.CipherTest#testCipherInitWithCertificate",
+          "org.apache.harmony.luni.tests.internal.net.www.protocol.http.HttpURLConnectionTest",
+          "org.apache.harmony.luni.tests.internal.net.www.protocol.https.HttpsURLConnectionTest",
+          "org.apache.harmony.luni.tests.java.net.URLConnectionTest"]
+},
+{
   description: "Test timeouts",
   result: EXEC_TIMEOUT,
   modes: [device],
diff --git a/tools/run-libcore-tests.sh b/tools/run-libcore-tests.sh
index e25d1b3..a19fd15 100755
--- a/tools/run-libcore-tests.sh
+++ b/tools/run-libcore-tests.sh
@@ -34,6 +34,7 @@
                   "libcore.java.math"
                   "libcore.java.text"
                   "libcore.java.util"
+                  "libcore.javax.crypto"
                   "libcore.javax.security"
                   "libcore.javax.sql"
                   "libcore.javax.xml"
@@ -41,6 +42,7 @@
                   "libcore.reflect"
                   "libcore.util"
                   "org.apache.harmony.annotation"
+                  "org.apache.harmony.crypto"
                   "org.apache.harmony.luni"
                   "org.apache.harmony.nio"
                   "org.apache.harmony.regex"